title: Decode compressed frame to memory using LibAV: avcodec_decode_video2?
body: <p>I am completely new to libAV.</p>
  
  <p>I have a single video frame coming from somewhere else (it's in memory, not in a file). It should be H.264 encoded keyframe.</p>
  
  <p>I'm trying to decode with <code>avcodec_decode_video2</code> is that the right function for my needs?</p>
  
  <p>I'm encountering a problem using this code (basically taken from <a href="https://stackoverflow.com/questions/20786781/ffmpeg-decode-raw-buffer-with-avcodec-decode-video2">FFmpeg decode raw buffer with avcodec_decode_video2</a> ): </p>
  
  <pre><code>AVCodecContext  *m_pCodecCtx;
  AVCodec         *m_pCodec;
  AVFrame         *m_pFrame;
  
  m_pCodec= avcodec_find_decoder(CODEC_ID_H264);
  m_pCodecCtx = avcodec_alloc_context3(m_pCodec);
  avcodec_open2(m_pCodecCtx,m_pCodec,0);
  
  // since avcodec_alloc_frame() isn't existing anymore, I changed to following:
  //m_pFrame=m_fc.avcodec_alloc_frame(); // and what is/was m_fc?!?
  m_pFrame = av_frame_alloc();
  
  
  AVPacket        packet;
  av_init_packet(&amp;packet);
  
  packet.data = (unsigned char*)mData;
  packet.size=(int)mDataSize;
  
  // this is for decoding a keyframe?
  packet.flags |= AV_PKT_FLAG_KEY;
  
  int framefinished=0;
  int nres=avcodec_decode_video2(m_pCodecCtx,m_pFrame,&amp;framefinished,&amp;packet);
  
  if(framefinished)
  {
      std::cout &lt;&lt; "decoded frame" &lt;&lt; std::endl
  }
  </code></pre>
  
  <p>unfortunately <code>framefinished</code> always returns 0 while <code>nres</code> is 18331 which equals <code>mDataSize</code></p>
  
  <p>Where's my fault? 
  Do I have to specify codec information more clearly? 
  Is my packet broken or incomplete?
  Am I using the wrong libAV function?</p>
  
  <p>In the sample code of the framework I'm using, the image data is succesfully streamed to a file, using <code>av_interleaved_write_frame</code> (and a <code>AvFormatContext</code>). Can this help me configuring my packet or the codec?</p>