title: How do I set libavcodec to use 4:2:2 chroma when encoding MPEG-2 4:2:2 profile?
body: <p>I have a project using libavcodec (ffmpeg). I'm using it to encode MPEG-2 video at 4:2:2 Profile, Main Level. I have the pixel format PIX_FMT_YUV422P selected in the AVCodecContext, however the video output I'm getting has all the colours wrong, and looks to me like the encoder is incorrectly reading the buffers as though it thinks it is 4:2:0 chroma rather than 4:2:2. Here's my codec setup:</p>
  
  <pre><code>// 
  // AVFormatContext* _avFormatContext previously defined as mpeg2video
  //
  
  //
  // Set up the video stream for output
  //
  AVVideoStream* _avVideoStream = av_new_stream(_avFormatContext, 0);
  if (!_avVideoStream)
  {
      err = ccErrWFFFmpegUnableToAllocateStream;
      goto bail;
  }
  _avCodecContext = _avVideoStream-&gt;codec;
  _avCodecContext-&gt;codec_id = CODEC_ID_MPEG2VIDEO;
  _avCodecContext-&gt;codec_type = CODEC_TYPE_VIDEO;
  
  //
  // Set up required parameters
  //
  _avCodecContext-&gt;rc_max_rate = _avCodecContext-&gt;rc_min_rate = _avCodecContext-&gt;bit_rate = src-&gt;_avCodecContext-&gt;bit_rate;
  _avCodecContext-&gt;flags = CODEC_FLAG_INTERLACED_DCT;
  _avCodecContext-&gt;flags2 = CODEC_FLAG2_INTRA_VLC | CODEC_FLAG2_NON_LINEAR_QUANT;
  _avCodecContext-&gt;qmin = 1;
  _avCodecContext-&gt;qmax = 1;
  _avCodecContext-&gt;rc_buffer_size = _avCodecContext-&gt;rc_initial_buffer_occupancy = 2000000;
  _avCodecContext-&gt;rc_buffer_aggressivity = 0.25;
  _avCodecContext-&gt;profile = 0;
  _avCodecContext-&gt;level = 5;
  _avCodecContext-&gt;width = f-&gt;GetWidth(); // f is a private Frame class with width, height properties etc.
  _avCodecContext-&gt;height = f-&gt;GetHeight();
  _avCodecContext-&gt;time_base.den = 25;
  _avCodecContext-&gt;time_base.num = 1;
  _avCodecContext-&gt;gop_size = 12;
  _avCodecContext-&gt;max_b_frames = 2;
  _avCodecContext-&gt;pix_fmt = PIX_FMT_YUV422P;
  
  if (_avFormatContext-&gt;oformat-&gt;flags &amp; AVFMT_GLOBALHEADER)
  {
      _avCodecContext-&gt;flags |= CODEC_FLAG_GLOBAL_HEADER;
  }
  
  if (av_set_parameters(_avFormatContext, NULL) &lt; 0)
  {
      err = ccErrWFFFmpegUnableToSetParameters;
      goto bail;
  }
  
  //
  // Set up video codec for encoding
  //
  AVCodec* _avCodec = avcodec_find_encoder(_avCodecContext-&gt;codec_id);
  if (!_avCodec)
  {
      err = ccErrWFFFmpegUnableToFindCodecForOutput;
      goto bail;
  }
  if (avcodec_open(_avCodecContext, _avCodec) &lt; 0)
  {
      err = ccErrWFFFmpegUnableToOpenCodecForOutput;
      goto bail;
  }
  </code></pre>
  
  <p>A screengrab of the resulting video frame can be seen at <a href="http://ftp.limeboy.com/images/screen_grab.png" rel="nofollow noreferrer">http://ftp.limeboy.com/images/screen_grab.png</a> (the input was standard colour bars).</p>
  
  <p>I've checked by outputting debug frames to TGA format at various points in the process, and I can confirm that it is all fine and dandy up until the point that libavcodec encodes the frame.</p>
  
  <p>Any assistance most appreciated!</p>
  
  <p>Cheers,
  Mike.</p>