title: C++ h264 ffmpeg/libav encode/decode(lossless) issues
body: <p>Insights to encode/decode video with ffmpeg h264 (lossless)</p>
  
  <p>So I got something working on the encoding part, encode an avi in 264 however VLC wont play it, however Totem will.
  Decoding the same file proves troublesome. (I want the exact same data/frame going in as going out), I get these ;</p>
  
  <pre><code>saving frame   5
  Video decoding
  [h264 @ 0x1d19880] decode_slice_header error
  frame :6
  saving frame   6
  Video decoding
  [h264 @ 0x1d19880] error while decoding MB 15 7, bytestream -27
  [h264 @ 0x1d19880] concealing 194 DC, 194 AC, 194 MV errors in I frame
  frame :7
  saving frame   7
  Video decoding
  [h264 @ 0x1d19880] decode_slice_header error
  </code></pre>
  
  <p>and ultimatly this</p>
  
  <pre><code>[H264 Decoder @ 0x7f1320766040] frame :11
  Broken frame packetizing
  [h264 @ 0x1d19880] SPS changed in the middle of the frame
  [h264 @ 0x1d19880] decode_slice_header error
  [h264 @ 0x1d19880] no frame!
  Error while decoding frame 11
  </code></pre>
  
  <p>GAME OVER.</p>
  
  <p>Now I suspect that I have to go back to 1. the encoding part, there is problary a good reason VLC wont play it!</p>
  
  <p>I encode like this.</p>
  
  <pre><code>void encode(char *Y,char *U,char *V){
  av_init_packet(&amp;pkt);
  pkt.data = NULL;    // packet data will be allocated by the encoder
  pkt.size = 0;
  fflush(stdout);
  
  frame-&gt;data[0] = (uint8_t*)Y;
  frame-&gt;data[1] = (uint8_t*)U;
  frame-&gt;data[2] = (uint8_t*)V;
  frame-&gt;pts = ++i;
  
  ret = avcodec_encode_video2(c, &amp;pkt, frame, &amp;got_output);
  if (ret &lt; 0) {
      fprintf(stderr, "Error encoding frame\n");
      exit (EXIT_FAILURE);
  }
  if (got_output) {
      printf("Write frame %3d (size=%5d)\n", i, pkt.size);
      fwrite(pkt.data, 1, pkt.size, f);
      av_free_packet(&amp;pkt);
  }
  }
  </code></pre>
  
  <p>And the codec is setup like this:</p>
  
  <pre><code>AVCodecID dasd = AV_CODEC_ID_H264;
  codec = avcodec_find_encoder(dasd);
  c = avcodec_alloc_context3(codec);
  c-&gt;bit_rate = 400000;
  c-&gt;width = 320;
  c-&gt;height = 240;
  c-&gt;time_base= (AVRational){1,25};
  c-&gt;gop_size = 10; 
  c-&gt;max_b_frames=1;
  c-&gt;pix_fmt = AV_PIX_FMT_YUV420P;
  av_opt_set(c-&gt;priv_data, "preset", "slow", 0);
  avcodec_open2(c, codec, NULL);
  </code></pre>
  
  <p>Since I am going for lossless i am not dealing with delayed frames(is this a correct assumption?)
  I may not actually be encoding lossless, it seems like I may have to go with something like</p>
  
  <pre><code>AVDictionary *param;
  av_dict_set(&amp;param, "qp", "0", 0);
  </code></pre>
  
  <p>And then open...</p>
  
  <p>So I guess me questions is these :</p>
  
  <ul>
  <li>What are the correct codec params for lossless encoding (and advice if h264 is a terrible idea in this regard).</li>
  <li>Do I need to handle delayed frames when going for lossless?</li>
  <li>Why is VLC mad at me?</li>
  </ul>
  
  <p>Thanks.</p>