title: using FFmpeg, how to decode H264 packets
body: <p>I'm new to FFmpeg and struggling to decode H264 packets which can be obtained as an array of uint8_t.</p>
  
  <p>After many of investigations, I think it should be able to just put the array into an AVPacket like the below</p>
  
  <pre><code>AVPacket *avpkt = (AVPacket *)malloc(sizeof(AVPacket) * 1);
  av_init_packet(avpkt);  
  avpkt-&gt;data = ct;   // ct is the array
  avpkt-&gt;length =....
  </code></pre>
  
  <p>and decode by <code>avcodec_decode_video2()</code>. </p>
  
  <p>A part of the code is like:</p>
  
  <pre><code>...
  codec = avcodec_find_decoder(CODEC_ID_H264);
  gVideoCodecCtx = avcodec_alloc_context();
  gFrame = avcodec_alloc_frame();
  avcodec_decode_video2(gVideoCodecCtx, gFrame, &amp;frameFinished, packet);
  ...
  </code></pre>
  
  <p>I guess I set all required properties properly but this function returns only <code>-1</code></p>
  
  <p>I just found the <code>-1</code> is coming from </p>
  
  <pre><code>ret = avctx-&gt;codec-&gt;decode(avctx, picture, got_picture_ptr, avpkt);
  </code></pre>
  
  <p>in the <code>avcodec_decode_video2();</code></p>
  
  <p>Actually, what I'm wondering is how can I decode H264 packets (without RTP header) by <code>avcodec_decode_video2()</code>?</p>
  
  <hr>
  
  <p><strong>Updated:</strong></p>
  
  <p>OK, I'm still trying to find a solution. What I'm doing now is the below</p>
  
  <p>** The H264 stream in this RTP stream is encoded by FU-A</p>
  
  <ol>
  <li><p>Receive an RTP packet</p></li>
  <li><p>Look if the second byte of the RTP header is > 0 which means it's the first packet (and possibly will be followed)</p></li>
  <li><p>See if the next RTP packet has > 0 at its second byte also, then it means the previous frame was a complete NAL or if this is &lt; 0, the packet should be appended to the previous packet.</p></li>
  <li><p>Remove all RTP header of the packets so it has only like FU indicator | FU header | NAL</p></li>
  <li><p>Try play it with <code>avcodec_decode_video2()</code></p></li>
  </ol>
  
  <p>But it's only returning <code>-1</code>..... <br>
  Am I supposed to remove FU indicator and header too??</p>
  
  <p>Any suggestion will be very appreciated</p>