3

I get Frames in loop and decode it with ffmpeg getting AVFrame as e result of it.

So I must get neccessary pixel data of frame into char* and give as a callback function's parameter. So how I can generete such char* array? In internet I saw some examples such as:

for(y=0; y<height; y++)
{
 fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);
}

or something like this. Is it true? And which size would be my char* ? As I see we write width*3 *height bytes?

halfelf
  • 9,737
  • 13
  • 54
  • 63
mmmaaak
  • 803
  • 1
  • 18
  • 38

1 Answers1

2
for(y=0; y<height; y++) { 
    fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);
}

Yes that's correct.
This is writing a line of 3byte/pixel (presumably RGB) from the AVFrame->data pointer.

ps. The reason for doing it like this is that the start of each row of data begins on a new 4byte memory boundary - the computer is more efficent at accessing memory in multiples of 32bits (4bytes). So if your pixel size (3bytes) and width aren't a multiple of 4 then you need to do this rather than simply copy width*height*3 bytes of data.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • As I understood it is a PPM format of pixel-data. How can I save full pixel data in bmp for example... ? – mmmaaak Apr 02 '12 at 14:42
  • @mmmaaak - PPM is the easiest format to write directly. BMP is essentially the same but with a more complex header, if you are on windows the lib already contains code to write bmps, just search msdn – Martin Beckett Apr 02 '12 at 15:47
  • Related answer: http://stackoverflow.com/questions/1047200/extract-rgb-values-from-a-avframe-ffmpeg-in-c – Nav Nov 02 '12 at 11:01