Page 1 of 1

ffmpeg poor YUV to RGB conversion

PostPosted: Tue Sep 12, 2017 9:27 am
by noiseshaper
Hi,

I'm using ffmpeg-3.2.4-win32, static and dev versions from zeranoe page, to decode some h264 mp4 videos I have created with x264 encoder.

When I decode their frames to png using ffmpeg.exe I get poor chroma resolution, quite blocky in the static zones of the video. If I decode them to jpg, the output is OK.

Doing some tests with ffmpeg API, I've narrowed the problem to the conversion from YUV to RGB performed with sws_scale(), since the YUV output is OK.

You can see an example here (put proper google drive url, can't post proper links yet).

drive google com/file/d/0B5KI1D-N1kHpV3lGaERJRjNkcms/view

1: Original frame before compressing.

2: YUV 4:2:0 conversion before compressing

3: Video output from MPC HC + LAV decoder. This is YUV420p output after decoding.

4: Video frames decoded with ffmpeg.exe to jpeg. Output is ok, just some jpeg macroblocking is visible.

5: Video frames decoded with ffmpeg.exe to png, very blocky chroma. Using ffmpeg API avcodec_decode_video2() and sws_scale() conversion to RGB produces the same blocky chroma.

The command lines for decoding the frames to jpg and png are these:

Code: Select all
ffmpeg -i testcase.mp4 -vframes 5 -qscale:v 2 output%03d.jpg


Code: Select all
ffmpeg -i testcase.mp4 -vframes 5 output%03d.png


You can download mp4 and bat files that showcase the effect here:

drive google com/drive/folders/0B5KI1D-N1kHpcUdGd2IyM2pXMTg

Thanks in advance.

Re: ffmpeg poor YUV to RGB conversion

PostPosted: Tue Sep 12, 2017 10:11 am
by noiseshaper
Ok, I found the fix in an old ffmpeg bugtrack at trac ffmpeg org/ticket/1582

In order to do good quality YUV to RGB conversion, you have to add in ffmpeg.exe:

Code: Select all
-sws_flags full_chroma_int+accurate_rnd


Or in ffmpeg API sws_getContext() the flags:

Code: Select all
SWS_FULL_CHR_H_INT | SWS_ACCURATE_RND


With these, the RGB output is OK.

Hope this helps, thanks.