[FFmpeg-devel] img_convert + program crashes

Umakant Goyal umakantgoyal1
Wed Mar 25 17:24:04 CET 2009

Hi All,

  I am getting RAW data from webcam and trying to encode using libavcodec.
  RAW data is coming in RGB24 format, so before encoding it, i am converting
  it to YUV420P format. Because, avcodec_encode_video API can encode YUV
  but when i try to convert raw data from RGB to YUV420P, my program
  Sometimes it works fine for first time,but then my programe crahes in
second attempt.
  My Code snippet is as follows:

 AVFrame *picture;
 AVFrame *picture1;
 picture = avcodec_alloc_frame();
 picture1 = avcodec_alloc_frame();
 if (NULL == picture || NULL == picture1)
  LogDump ("Fail to Allocate Frame");
  avcodec_get_frame_defaults (picture);
  avcodec_get_frame_defaults (picture1);
  int width = p_context->width;
  int height = p_context->height;
  char szBuf [40];

  /*int size = avpicture_fill((AVPicture*)picture,(uint8_t*)mediabuffer,
         p_context->pix_fmt, width, height);

  int size = avpicture_get_size(PIX_FMT_YUV420P, width, height);

  uint8_t* gpFrameYUV_buf = new uint8_t [size];
  if (NULL == gpFrameYUV_buf)
   LogDump ("Fail to Allocate Memory to Going to [gpFrameYUV_buf]");

  avpicture_fill( (AVPicture *)picture, gpFrameYUV_buf, PIX_FMT_YUV420P,
       width, height);

  size = avpicture_fill((AVPicture*)picture1,(uint8_t*)mediabuffer,
         PIX_FMT_RGB24, width, 220);

  sprintf (szBuf, "Size of Raw Data [%d]", size);
  LogDump (szBuf);

  size = img_convert((AVPicture*)picture, PIX_FMT_YUV420P,
       (AVPicture*)picture1, PIX_FMT_RGB24,
       width, height);

  if (size == 0)
   LogDump ("Going to Encode");
   int OutbufSize = 100000 ;
   unsigned char* OutBuf = new unsigned char [OutbufSize];
   int out_size = avcodec_encode_video(p_context, (uint8_t*)OutBuf,
              OutbufSize, picture);

   Any help would be highly appreciated.

More information about the ffmpeg-devel mailing list