title: H264 decoding using ffmpeg
body: <p>I'm trying to decode a video stream with ffmpeg library, that's how I do it basically:</p>
  
  <pre><code>void video_decode(const char *filename)
  {
      AVCodec *codec;
      AVCodecContext *c= NULL;
      int frame_count=0;
      FILE *f;
      AVFrame *frame;
      uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
      AVPacket avpkt;
      av_init_packet(&amp;avpkt);
      memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
      printf("Decoding video file...\n");
      /* find the h264 video decoder */
      codec = avcodec_find_decoder(AV_CODEC_ID_H264);
      c = avcodec_alloc_context3(codec);
      c-&gt;bit_rate = 400000;
      c-&gt;width = 1920;
      c-&gt;height = 1080;
  
      if (avcodec_open2(c, codec, NULL) &lt; 0) {
          fprintf(stderr, "Could not open codec\n");
          exit(1);
      }
      frame = av_frame_alloc();
      for (;;) {
          avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);         
      if (avpkt.size == 0)
              break;
          avpkt.data = inbuf;
          while(avpkt.size &gt; 0){
  
          int len, got_frame;
              len = avcodec_decode_video2(c, frame, &amp;got_frame, &amp;avpkt);          
          if (len &lt; 0) {
                  fprintf(stderr, "Errr while decding frame %d\n", frame_count);
                  exit (1);
              }
              if (got_frame) {
                  //Print out frame information..
          }
              if (avpkt.data) {
                  avpkt.size -= len;
                  avpkt.data += len;
              }
      }               
      }  
  }
  </code></pre>
  
  <p>But I got the following outputs:</p>
  
  <pre><code>Decoding video file...
  [h264 @ 0x303c040] decode_slice_header error
  [h264 @ 0x303c040] decode_slice_header error
  [h264 @ 0x303c040] decode_slice_header error
  [h264 @ 0x303c040] no frame!
  Errr while decding frame 0
  </code></pre>
  
  <p>Obviously the initiation of codec was incomplete. Do you have experience with h264 api? Any help would be appreciated.</p>