google / guetzli

Perceptual JPEG encoder
Apache License 2.0
12.9k stars 977 forks source link

Support YUV422 input #40

Open seanmavley opened 7 years ago

seanmavley commented 7 years ago

This is the image: image

./guetzli_linux_x86-64 image.jpg output.jpg gives the error:

Invalid input JPEG file
Guetzli processing failed

Is the JPEG file really invalid? What qualifies as invalid or valid? The image above came directly from a an android phone's camera.

robryk commented 7 years ago

The error message is inaccurate. This is indeed a valid JPEG file, albeit a weird one:

$ djpeg -v -v -v < 0d577a52-0a90-11e7-8627-bd160ddbd036.jpg >/dev/null
...
Start Of Frame 0xc0: width=3072, height=1728, components=3
    Component 1: 2hx1v q=0
    Component 2: 1hx1v q=1
    Component 3: 1hx1v q=1
...

This indicates that this image is not exactly YUV420: it has all channels downsampled vertically by 2x, and chroma channels downsampled horizontally 2x. Edit: I misread. This is YUV422. I will figure out tomorrow European time how to deal with such inputs. In the meantime, as a workaround, you can first convert the image to a PNG using e.g. ImageMagick, and then supply the resulting PNG to Guetzli:

$ convert 0d577a52-0a90-11e7-8627-bd160ddbd036.jpg 0d577a52-0a90-11e7-8627-bd160ddbd036.png
$ ~/guetzli-1.0/guetzli-linux 0d577a52-0a90-11e7-8627-bd160ddbd036.png output.jpg

For my own curiosity, could you say more about the software that produced that image (e.g. what model of a phone it was)?

seanmavley commented 7 years ago

For my own curiosity, could you say more about the software that produced that image (e.g. what model of a phone it was)?

The EXIF only mentions Custom Software, however to be precise, the image was taken with an Infinix Hot X507 running Android 4.4 (or so - just borrowed the phone from a friend to take picture), using the default Camera app

cecilemuller commented 7 years ago

If it helps testing, here are four examples exhibiting the same behavior, each from a different brand of camera (Sony, Canon, Panasonic, and Theta): https://github.com/wildpeaks/example-guetzli-photos

dapperfu commented 7 years ago

I'll add that I'm getting the same error with JPEG pictures straight from my DSLR.

Camera Model Name               : Canon EOS 70D
Canon Firmware Version          : Firmware Version 1.1.1
Color Data Version              : 10 (1DX/5DmkIII/6D/70D/100D/650D/700D/M)
Y Cb Cr Sub Sampling            : YCbCr4:2:2 (2 1)
$ djpeg -v -v -v < IMG_9819.JPG > /dev/null 
...
Start Of Frame 0xc0: width=5472, height=3648, components=3
    Component 1: 2hx1v q=0
    Component 2: 1hx1v q=1
    Component 3: 1hx1v q=1
...
alecperkins commented 7 years ago

I see the same error from a Nikon D7200 (using a build on OS X): sample image. Converting to png with ImageMagick first worked great, though I feel like I need a beefier computer to make it a practical addition to my workflow!

mallorn commented 7 years ago

Same error here on a Nikon D200, D300, and D7200. Also fails on images saved from GIMP 2.8.14 under Fedora Core 20.

Slightly different output for GIMP images:

Start Of Frame 0xc2: width=3872, height=2592, components=3 Component 1: 1hx2v q=0 Component 2: 1hx1v q=1 Component 3: 1hx1v q=1

asm-man commented 7 years ago

@seanmavle I found the innards of your jpg file fascinating. Several odd things were inside:

  1. Embedded thumbnail image (160x128 pixels) with different proportions (1.25:1) to the main image (1.77:1);
  2. Four different third-party APPn segments (APP5, APP6, APP7, APP8);
  3. 29 x trailing bytes of 0x00 after the EOI marker. It's possible that one or another of these "extras" is confusing Guetzli. My "JPGExtra" program stripped 58,588 bytes of such extras from your image file. I'd love to test running your stripped-down JPEG file through Guetzli, but I don't yet have Guetzli installed on my system. You might try this yourself, using the stripped-down JPEG file I've attached, or using my free JPGExtra program (or any other metadata removal software) to strip the file yourself. If this works, it may give us a clue as to what's causing Guetzli to fail. 0d577a52-0a90-11e7-8627-bd160ddbd036_stripped
asm-man commented 7 years ago

@alecperkins Your sample image file also had some fascinating stuff embedded inside:

  1. 160x120 pixel thumbnail image, embedded in the Exif header in the conventional manner;
  2. Block of 232 bytes of 00h, tacked onto the end of file (ie. past the End Of Image marker, which is usually at the end of the file);
  3. Large 640x424 pixel thumbnail image, again tacked onto the end of the file;
  4. Huge 1620x1080 pixel thumbnail image, once again tacked onto the end of the file. It's possible that this unconventional suffix, containing either the 2nd or 3rd thumbnail, may be the source of the problem. As I wrote to @seanmavle, stripping your jpg of such "extras" with my JPGExtra program or other metadata removal tool may solve the problem, and if so, may give us a clue as to what is causing the problem for Guetzli.
robryk commented 7 years ago

@asm-man We know that the problem is the 2x1,1x1,1x1 downsampling used in these images.

asm-man commented 7 years ago

@robryk Oh, right, thanks for that. It just seemed possible that the other weird stuff in the files might be at fault. Cheers.

seanmavley commented 7 years ago

@asm-man Got the same error for the stripped down file you shared.

Smilebags commented 7 years ago

I acknowledge that the error shouldn't occur if the image is valid, but you're unlikely to see as good a result re-encoding a JPEG image which has already been encoded with something else - you might see a slightly smaller file, but best case scenario for image quality is 'only slightly worse than input'. Ideally this would be used on images which have not undergone lossy compression.

clouless commented 7 years ago

I got the same error for v1.0 under alpine linux with a photo taken with the Sony Alpha 6000

Camera is set to best quality JPEG Output.

Test-Image:

Exif:

ExifTool Version Number         : 10.20
File Name                       : sony-alpha-6000-hires.JPG
Directory                       : .
File Size                       : 8.6 MB
File Modification Date/Time     : 2017:01:21 14:09:42+01:00
File Access Date/Time           : 2017:03:18 11:52:03+01:00
File Inode Change Date/Time     : 2017:03:18 11:51:07+01:00
File Permissions                : rwxr--r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
Exif Byte Order                 : Little-endian (Intel, II)
Image Description               : 
Make                            : SONY
Camera Model Name               : ILCE-6000
Orientation                     : Horizontal (normal)
X Resolution                    : 350
Y Resolution                    : 350
Resolution Unit                 : inches
Software                        : ILCE-6000 v3.20
Modify Date                     : 2017:01:21 14:09:43
Y Cb Cr Positioning             : Co-sited
Exposure Time                   : 1/1000
F Number                        : 16.0
Exposure Program                : Manual
ISO                             : 2000
Sensitivity Type                : Recommended Exposure Index
Recommended Exposure Index      : 2000
Exif Version                    : 0230
Date/Time Original              : 2017:01:21 14:09:43
Create Date                     : 2017:01:21 14:09:43
Components Configuration        : Y, Cb, Cr, -
Compressed Bits Per Pixel       : 3
Brightness Value                : 9.0796875
Exposure Compensation           : 0
Max Aperture Value              : 3.5
Metering Mode                   : Multi-segment
Light Source                    : Unknown
Flash                           : Off, Did not fire
Focal Length                    : 16.0 mm
Rating                          : 0
Brightness                      : 0
Long Exposure Noise Reduction   : On (unused)
High ISO Noise Reduction        : Normal
HDR                             : Off; Uncorrected image
WB Shift AB GM                  : 0 0
Face Info Offset                : 94
Sony Date Time                  : 2017:01:21 14:09:43
Sony Image Width                : 6000
Faces Detected                  : 0
Face Info Length                : 37
Meta Version                    : DC7303320222000
Creative Style                  : Standard
Color Temperature               : Auto
Color Compensation Filter       : 0
Scene Mode                      : Standard
Zone Matching                   : ISO Setting Used
Dynamic Range Optimizer         : Auto
Image Stabilization             : On
Color Mode                      : Standard
Full Image Size                 : 6000x4000
Preview Image Size              : 1616x1080
File Format                     : ARW 2.3.1
Quality                         : RAW + JPEG
Flash Exposure Compensation     : 0
White Balance Fine Tune         : 0
Sony Model ID                   : ILCE-6000
Teleconverter                   : None
Multi Frame Noise Reduction     : Off
Picture Effect                  : Off
Soft Skin Effect                : Off
Vignetting Correction           : Auto
Lateral Chromatic Aberration    : Auto
Distortion Correction Setting   : Auto
Lens Type                       : E-Mount, T-Mount, Other Lens or no lens
Lens Spec                       : E PZ 16-50mm F3.5-5.6 OSS
Auto Portrait Framed            : No
Flash Action                    : Did not fire
Focus Mode                      : AF-A
AF Area Mode Setting            : Multi
Flexible Spot Position          : 0 0
AF Zone Selected                : n/a
AF Points Used                  : (none)
Focal Plane AF Points Used      : (none)
Flash Level                     : Normal
Release Mode                    : Normal
Sequence Number                 : Single
Anti-Blur                       : On (Shooting)
Intelligent Auto                : Off
White Balance                   : Auto
Shot Number Since Power Up      : 7
Sequence Image Number           : 1
Sequence File Number            : 1
Sequence Length                 : 1 file
Camera Orientation              : Horizontal (normal)
Quality 2                       : RAW + JPEG
Sony Image Height               : 4000
Model Release Year              : 2014
Ambient Temperature             : 11 C
AF Area Mode                    : Multi
Focus Position 2                : 243
Lens Zoom Position              : 0%
Sony ISO                        : 1966
Base ISO                        : 100
Stops Above Base ISO            : 4.3
Sony Exposure Time 2            : 1/1024
Sony Max Aperture Value         : 3.7
Picture Effect 2                : Off
Distortion Correction           : Applied
Battery Temperature             : 39.4 C
Battery Level                   : 22%
Lens Mount 2                    : E-mount
Lens Type 3                     : Sony E PZ 16-50mm F3.5-5.6 OSS
Camera E-mount Version          : 1.50
Lens E-mount Version            : 1.20
Lens Firmware Version           : Ver.01
Release Mode 3                  : Normal
Self Timer                      : Off
Flash Mode                      : Rear Sync
HDR Setting                     : Off
Picture Profile                 : Standard/Neutral - Gamma Still (PP2)
WB RGB Levels                   : 679 256 438
Min Focal Length                : 16.0 mm
Max Focal Length                : 50.0 mm
Flash Status                    : Built-in Flash present
Image Count                     : 6165
Sony F Number                   : 16.0
Image Count 2                   : 6165
Sony Date Time 2                : 2017:01:21 13:09:44
Release Mode 2                  : Normal
Internal Serial Number          : 6bff0f0d
Lens Mount                      : E-mount
Lens Format                     : APS-C
Lens Type 2                     : Sony E PZ 16-50mm F3.5-5.6 OSS
Lens Spec Features              : E PZ OSS
User Comment                    : 
Flashpix Version                : 0100
Color Space                     : sRGB
Exif Image Width                : 6000
Exif Image Height               : 4000
Interoperability Index          : R98 - DCF basic file (sRGB)
Interoperability Version        : 0100
File Source                     : Digital Camera
Scene Type                      : Directly photographed
Custom Rendered                 : Normal
Exposure Mode                   : Manual
Digital Zoom Ratio              : 1
Focal Length In 35mm Format     : 24 mm
Scene Capture Type              : Standard
Contrast                        : Normal
Saturation                      : Normal
Sharpness                       : Normal
Lens Info                       : 16-50mm f/3.5-5.6
Lens Model                      : E PZ 16-50mm F3.5-5.6 OSS
PrintIM Version                 : 0300
Compression                     : JPEG (old-style)
Thumbnail Offset                : 38472
Thumbnail Length                : 9894
MPF Version                     : 0100
Number Of Images                : 2
MP Image Flags                  : Dependent child image
MP Image Format                 : JPEG
MP Image Type                   : Large Thumbnail (full HD equivalent)
MP Image Length                 : 773935
MP Image Start                  : 8203776
Dependent Image 1 Entry Number  : 0
Dependent Image 2 Entry Number  : 0
Image Width                     : 6000
Image Height                    : 4000
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:2:2 (2 1)
Aperture                        : 16.0
Blue Balance                    : 1.710938
Focus Distance 2                : 28.01 m
Image Size                      : 6000x4000
Lens ID                         : Sony E PZ 16-50mm F3.5-5.6 OSS
Preview Image                   : (Binary data 773935 bytes, use -b option to extract)
Megapixels                      : 24.0
Red Balance                     : 2.652344
Scale Factor To 35 mm Equivalent: 1.5
Shutter Speed                   : 1/1000
Thumbnail Image                 : (Binary data 9894 bytes, use -b option to extract)
Circle Of Confusion             : 0.020 mm
Field Of View                   : 73.7 deg
Focal Length                    : 16.0 mm (35 mm equivalent: 24.0 mm)
Hyperfocal Distance             : 0.80 m
Light Value                     : 13.6
robryk commented 7 years ago

@Smilebags Many cameras produce a JPEG. It's not an ideal situation, but it's what we have to live with.

@everyone: Thanks for all the examples. I know why it's happening and don't need more examples. The current workaround is to first convert to a PNG.

Smilebags commented 7 years ago

Yes, and the jpeg encoding in cameras generally isn't very good. Shoot raw and bring it into editing software uncompressed to convert to PNG.

seanmavley commented 7 years ago

I guess the recent commit provides better error message and the suggested workaround. Solved. Although in the future, I hope that conversion to PNG is done internally for us

robryk commented 7 years ago

Let's leave this issue open as an FR to actually support these images.