title: Decoding mJPEG with libavcodec
body: <p>I am creating video conference application. I have discovered that webcams (at least 3 I have) provide higher resolutions and framerates for mJPEG output format. So far I was using YUY2, converted in I420 for compression with X264. To transcode mJPEG to I420, I need to decode it first. I am trying to decode images from webcam with libavcodec. This is my code.</p>
  
  <p>Initialization:</p>
  
  <pre><code>// mJPEG to I420 conversion
  AVCodecContext * _transcoder = nullptr;
  AVFrame * _outputFrame;
  AVPacket _inputPacket;
  
  avcodec_register_all();
  _outputFrame = av_frame_alloc();
  av_frame_unref(_outputFrame);
  av_init_packet(&amp;_inputPacket);
  
  AVCodec * codecDecode = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
  
  _transcoder = avcodec_alloc_context3(codecDecode);
  avcodec_get_context_defaults3(_transcoder, codecDecode);
  _transcoder-&gt;flags2 |= CODEC_FLAG2_FAST;
  _transcoder-&gt;pix_fmt = AVPixelFormat::AV_PIX_FMT_YUV420P;
  _transcoder-&gt;width = width;
  _transcoder-&gt;height = height;
  avcodec_open2(_transcoder, codecDecode, nullptr);
  </code></pre>
  
  <p>Decoding:</p>
  
  <pre><code>_inputPacket.size = size;
  _inputPacket.data = data;
  
  int got_picture;
  int decompressed_size = avcodec_decode_video2(_transcoder, _outputFrame, &amp;got_picture, &amp;_inputPacket);
  </code></pre>
  
  <p>But so far, what I am getting is a green screen. Where am I wrong?</p>
  
  <p>UPD:
  I have enabled libavcodec logging, but there are not warnings or errors.
  Also I have discovered that _outputframe has AV_PIX_FMT_YUVJ422P as format and colorspace, which does not fit any on values in libavcodec's enums (the actual value is 156448160). </p>