ffmpeg yuv420p to yuv422p and back
It seems that ffmpeg performs some interpolation when upsampling from 420 to 422 and antialiasing when downsampling back from 422 to 420 - at least that's the default behavior of similar Matlab library: mathworks.com/help/vision/ref/chromaresampling.html. And it looks like this library actually has the options to achieve the behavior of simply copying/dropping chrominance values which would result in no difference between the original yuv420p and "converted back", in case anyone also needs this.
Related videos on Youtube
IvanIvanovich
Updated on September 18, 2022Comments
-
IvanIvanovich almost 2 years
So I have a raw video in yuv420p format -
input.yuv
, and I know its frame size. I execute 2 commands:ffmpeg -pix_fmt yuv420p -s 352x288 -i input.yuv -pix_fmt yuv422p input_yuv422p.yuv
and then
ffmpeg -pix_fmt yuv422p -s 352x288 -i input_yuv422p.yuv -pix_fmt yuv420p input_decoded.yuv
The problem is that files
input.yuv
andinput_decoded.yuv
differ. My understanding is that when we convert toyuv422p
fromyuv420p
- we should essentially copy existingU
andV
components to produce more samples to fill in; then, when converting back - we should simply drop these samples, and receive the original file back, but that's not what I see. Am I doing something wrong here, and is it possible to receive originalyuv420p
back?-
slhck over 5 yearsRounding errors, I guess? Can you quantify the differences in luma/chroma?
-
IvanIvanovich over 5 yearsHm, if we simply copy bytes when doing
420p
to422p
and drop bytes when converting back - there should be no possible source for rounding errors. Maybe my assumption about how the conversion420p
to422p
is performed is wrong, andffmpeg
does some kind of interpolation indeed. Will check differences now, thanks. -
IvanIvanovich over 5 yearsSo I get this PSNR between original and converted back:
PSNR y:inf u:51.837944 v:51.936866
so only chroma differs. Is it possible to somehow instructffmpeg
to use the simple copying/dropping bytes when converting betweenyuv420p
andyuv422p
forward/backward? -
slhck over 5 yearsNo, don't think so. In principle copying the bytes is what would be needed, but the image is decoded and encoded back to the new format, hence possible interpolation errors. You could check a hex dump of a small example (or use rawpixels.net) to see where the error comes from — or how big it is.
-
IvanIvanovich over 5 yearsYeah, I guess ffmpeg performs some interpolation when upsampling from 420 to 422 and antialiasing when downsampling back from 422 to 420 - at least that's the default behavior of similar Matlab library: mathworks.com/help/vision/ref/chromaresampling.html. And it looks like this library actually has the options to achieve the behavior I wanted, which would result in no difference between the original yuv420p and "converted back", in case anyone also needs this. How do I close this question as answered?)
-
slhck over 5 yearsYou can post your own answer below using the button.
-