15

I get the general idea that the frame.data[] is interpreted depending on which pixel format is the video (RGB or YUV). But is there any general way to get all the pixel data from the frame? I just want to compute the hash of the frame data, without interpret it to display the image.

According to AVFrame.h:

uint8_t* AVFrame::data[AV_NUM_DATA_POINTERS]

pointer to the picture/channel planes.

int AVFrame::linesize[AV_NUM_DATA_POINTERS]

For video, size in bytes of each picture line.

Does this mean that if I just extract from data[i] for linesize[i] bytes then I get the full pixel information about the frame?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
vivienlwt
  • 163
  • 1
  • 1
  • 7

2 Answers2

15

linesize[i] contains stride for the i-th plane.

To obtain the whole buffer, use the function from avcodec.h

/**
 * Copy pixel data from an AVPicture into a buffer, always assume a
 * linesize alignment of 1. */   
int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt,
                 int width, int height,
                 unsigned char *dest, int dest_size);

Use

int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height);

to calculate the required buffer size.

pogorskiy
  • 4,705
  • 1
  • 22
  • 21
  • Thanks, this is what i'm looking for :D btw, does plane means one color channel such as R/G/B or Y/U/V? I tried to search for the definition, but with these keywords I didn't get much information. – vivienlwt Oct 15 '13 at 12:52
  • How come we manually copy ,without `avpicture_layout`, such as `for(number of vertical lines[]){ memory copy upto linesize to a buffer and increment buffer pointer by linesize }` will do the job?? – nmxprime Jan 21 '14 at 12:04
  • I think you can typecast `AVFrame` to `AVPicture` here, since `AVFrame` contains the same fields as `AVPicture` See also strict aliasing rule: http://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule – Ciro Santilli OurBigBook.com Mar 02 '16 at 16:30
0

avpicture_* API is deprecated. Now you can use av_image_copy_to_buffer() and av_image_get_buffer_size() to get image buffer.

You can also avoid creating new buffer memory like above (av_image_copy_to_buffer()) by using AVFrame::data[] with the size of each array/plane can be get from av_image_fill_plane_sizes(). Only do this if you clearly understand the pixel format.

Find more here: https://www.ffmpeg.org/doxygen/trunk/group__lavu__picture.html

irous
  • 401
  • 3
  • 8