sekrit-twc / zimg

Scaling, colorspace conversion, and dithering library
Do What The F*ck You Want To Public License
405 stars 77 forks source link

chroma integer overflow #171

Closed cosmin closed 2 years ago

cosmin commented 2 years ago

When the HDR original video contains extremely saturated color (close to the edge of BT.2020 gamut), zscale may hit integer overflow, turning bright green (negative Cb, negative Cr) into deep cyan/blue color (positive Cb, negative Cr).

For example, when using zscale as part of tonemapping to extract JPEG thumbnails to sRGB gamma 2.4 one can observe the following chroma integer overflow. This has been reproduced using latest release 3.0.3

ffmpeg -i input.mp4 -vf zscale=t=linear,tonemap=hable:desat=0,zscale=t=iec61966-2-1:m=470bg:p=709:r=pc,format=yuv420p -q:v 1 -frames:v 1 bad_frame.jpg

bad_frame

while using the colorspace filter instead of zscale produces the expected output

ffmpeg -i input.mp4 -vf zscale=t=linear,tonemap=hable:desat=0,colorspace=primaries=bt709:space=bt470bg:trc=iec61966-2-1:range=pc,format=yuv420p -q:v 1 -frames:v 1 good_frame.jpg

good_frame

I'm not sure whether Github will properly preserve the input video used for this example.

https://user-images.githubusercontent.com/1358/158000634-8b808946-8500-4e3f-b069-c26674c4196d.mp4

sekrit-twc commented 2 years ago

Please provide the code values that trigger this issue.

richardpl commented 2 years ago

@diegocr Please do not hijack other people bug reports, that thing was ffmpeg bug, and have been already fixed in master.