29

I tried to find what each cell of AVFrame.linesize[] means, but I didn't found.

As I understood linesize[0] is the width, linesize[1] is the height.

  1. If I'm right what does other cells mean?
  2. why after avcodec_decode_video2(codecCtxDecode, frameDecoded, &frameFinished, &packet); only linesize[0] has the value and other cells are always 0?

UPDATED

I think AVFrame.data[i] and AVFrame.linesize[i] are the data of specific color in the row and the length of the row, am I correct?

Roman R.
  • 68,205
  • 6
  • 94
  • 158
theateist
  • 13,879
  • 17
  • 69
  • 109

2 Answers2

44

In the case of planar data, such as YUV420, linesize[i] contains stride for the i-th plane.

For example, for frame 640x480 data[0] contains pointer to Y component, data[1] and data[2] contains pointers to U and V planes. In this case, linesize[0] == 640, linesize[1] == linesize[2] == 320 (because the U and V planes is less than Y plane half)

In the case of pixel data (RGB24), there is only one plane (data[0]) and linesize[0] == width * channels (640 * 3 for RGB24)

Regexident
  • 29,441
  • 10
  • 93
  • 100
pogorskiy
  • 4,705
  • 1
  • 22
  • 21
  • hi, After `avcodec_decode_video2(dc, yuvFrame, &got_picture, &h264packet);` if i print `linesize`, it's not as you stated. why the linesize is such huge(a large 8 digit number) ? – nmxprime Jan 21 '14 at 12:00
  • 2
    this is super old but the reason it's a huge 8 bit number is because it's a pointer to an array, not an integer. – Matt Wolfe Jan 03 '19 at 17:27
  • 2
    How come then, if I have a YUV420p frame with resolution 500x500, `linesize[0] == 512`? – Benjamin Crawford Ctrl-Alt-Tut May 12 '21 at 11:57
  • 1
    @BenjaminCrawfordCtrl-Alt-Tut Each next line in Y plane begin 512 bytes after previous even though it contains 500 significant bytes – pogorskiy May 12 '21 at 15:18
  • linesize[0] may be greater than the width. Check this one too https://stackoverflow.com/a/57666844/3871242 – Minnie Nov 25 '21 at 09:12
  • @pogorskiy You mentioned that ***linesize[0] == 640, linesize[1] == linesize[2] == 320*** Aren't UV planes interleaved on a single plane in case of 4:2:0, why to to mention plane 2 then ? If I am wrong could you correct me ? – pentanol Mar 28 '22 at 13:40
  • Note that `width` and `linesize[0]` might not correspond (same for `width/2` and `linesize[1]` and `linesize[2]`. if ffmpeg decides to add padding, it happened to me while I was trying to encode an image of width 1200, not divisible by 32! – Antonio Jan 27 '23 at 15:40
13

Have a look at description of video frame formats:

You will see that formats are split into two big groups: packed and planar, depending on whether the components are kept separately or interleaved. Strides have slightly different meaning for those, and basically they are number of bytes you need to skip to advance by a row.

Roman R.
  • 68,205
  • 6
  • 94
  • 158