Search code examples
libavcodeclibavformat

Read uncompressed AVI using libavcodec


I am trying to read an uncompressed AVI file - basic uncompressed AVI are BGR bitmaps.

if(frame.format == AV_PIX_FMT_BGR24)
{
  int data_size = frame.linesize[0]*frame.height;
  my_data_ptr = new uint8_t [data_size];
  memcpy(my_data_ptr, frame.data[0], data_size);
}

I would expect frame.linesize[0] to be width*3 but I find it is -width*3.

If manually set data_size so it is not negative the memcpy results in a seg fault.

Are packed pixel formats stored in a special way ?


Solution

  • The negative line size indicates that the frame is inverted (raw AVI are coded this way).

    The data pointer points to the top line and if you add the negative linesize to the pointer you get to the next line. This means you are reading the image backwards, so using a non negative linesize for the memcpy in the code aboves means you will read the image forwards and go into unallocated memory after the first line.