title: Error in video streaming using libavformat: VBV buffer size not set, muxing may fail
body: <p>I stream a video using libavformat as follows:</p>
  
  <pre><code>static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec,
                          enum AVCodecID codec_id)
  {
  AVCodecContext *c;
  AVStream *st;
  /* find the encoder */
  *codec = avcodec_find_encoder(codec_id);
  if (!(*codec)) {
      fprintf(stderr, "Could not find encoder for '%s'\n",
              avcodec_get_name(codec_id));
      exit(1);
  }
  st = avformat_new_stream(oc, *codec);
  if (!st) {
      fprintf(stderr, "Could not allocate stream\n");
      exit(1);
  }
  st-&gt;id = oc-&gt;nb_streams-1;
  c = st-&gt;codec;
  switch ((*codec)-&gt;type) {
  case AVMEDIA_TYPE_AUDIO:
      c-&gt;sample_fmt  = (*codec)-&gt;sample_fmts ?
          (*codec)-&gt;sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
      c-&gt;bit_rate    = 64000;
      c-&gt;sample_rate = 44100;
      c-&gt;channels    = 2;
      break;
  case AVMEDIA_TYPE_VIDEO:
      c-&gt;codec_id = codec_id;
      c-&gt;bit_rate = 400000;
      /* Resolution must be a multiple of two. */
      c-&gt;width    = outframe_width;
      c-&gt;height   = outframe_height;
      /* timebase: This is the fundamental unit of time (in seconds) in terms
       * of which frame timestamps are represented. For fixed-fps content,
       * timebase should be 1/framerate and timestamp increments should be
       * identical to 1. */
      c-&gt;time_base.den = STREAM_FRAME_RATE;
      c-&gt;time_base.num = 1;
      c-&gt;gop_size      = 12; /* emit one intra frame every twelve frames at most */
      c-&gt;pix_fmt       = STREAM_PIX_FMT;
      if (c-&gt;codec_id == AV_CODEC_ID_MPEG2VIDEO) {
          /* just for testing, we also add B frames */
          c-&gt;max_b_frames = 2;
      }
      if (c-&gt;codec_id == AV_CODEC_ID_MPEG1VIDEO) {
          /* Needed to avoid using macroblocks in which some coeffs overflow.
           * This does not happen with normal video, it just happens here as
           * the motion of the chroma plane does not match the luma plane. */
          c-&gt;mb_decision = 2;
      }
  break;
  default:
      break;
  }
  /* Some formats want stream headers to be separate. */
  if (oc-&gt;oformat-&gt;flags &amp; AVFMT_GLOBALHEADER)
      c-&gt;flags |= CODEC_FLAG_GLOBAL_HEADER;
  return st;
  }
  </code></pre>
  
  <p>But when I run this code, I get the following error/warning:</p>
  
  <pre><code>[mpeg @ 01f3f040] VBV buffer size not set, muxing may fail
  </code></pre>
  
  <p>Do you know how I can set the VBV buffer size in the code? In fact, when I use ffplay to display the streamed video, ffplay doesn't show anything for short videos but for long videos, it start displaying the video immediately. So, it looks like ffplay needs a buffer to be filled up by some amount so that it can start displaying the stream. Am I right?</p>