Home > Software engineering >  How to view/save AVFrame have format AV_PIX_FMT_YUVJ420P to file
How to view/save AVFrame have format AV_PIX_FMT_YUVJ420P to file

Time:02-11

I have a AVFrame and I want to save it to file. If I only store frame->data[0] to file, the image will be Grey image, how to view full color? I use C language.

Do you have any suggestions on what I should read to understand and do these things by myself?

CodePudding user response:

AV_PIX_FMT_YUVJ420P is a planar format. data[0] is just a Y frame (grayscale), for the full image with the color you need to take into consideration: data[1] and data[2] for the U and V part of the frame respectively.

And it seems this format (AV_PIX_FMT_YUVJ420P) is deprecated in favor of the more common AV_PIX_FMT_YUV420P format, use this if it's up to you.

CodePudding user response:

A relatively simple way to save and view the image is writing Y, U and V (planar) data to binary file, and using FFmpeg CLI to convert the binary file to RGB.


Some background:

yuvj420p in FFmpeg (libav) terminology applies YUV420 "full range" format.
I suppose the j in yuvj comes from JPEG - JPEG images uses "full range" YUV420 format.

Most of the video files use "limited range" (or TV range) YUV format.

  • In "limited range", Y range is [16, 235], U range is [16, 240] and V range is [0, 240].
  • In "full range", Y range is [0, 255], U range is [0, 255] and V range is [0, 255].

yuvj420p is deprecated, and supposed to be marked using yuv420p combined with dst_range 1 (or src_range 1) in FFmpeg CLI. I never looked for a way to define "full range" in C.


yuvj420p in FFmpeg (libav) applies "planar" format.
Separate planes for Y channel, for U channel and for V channel.
Y plane is given in full resolution, and U, V are down-scaled by a factor of x2 in each axis.

Illustration:

Y - data[0]:  YYYYYYYYYYYY
              YYYYYYYYYYYY
              YYYYYYYYYYYY
              YYYYYYYYYYYY
            
U - data[1]:  UUUUUU
              UUUUUU
              UUUUUU

V - data[2]:  VVVVVV
              VVVVVV
              VVVVVV

In C, each "plane" is stored in a separate buffer in memory.
When writing the data to a binary file, we may simply write the buffers to the file one after the other.


For demonstration, I am reusing my following enter image description here

  • Related