title: Decoding mp4/mkv using FFMPEG fails
body: <p>I'm using a project based on the latest FFmpeg git source tree, and linking to the shared DLL's published by Zeranoe at <a href="https://ffmpeg.zeranoe.com/builds/" rel="noreferrer">https://ffmpeg.zeranoe.com/builds/</a></p>
  
  <p>The playback code works and loops. It plays back h265 files (raw), mpeg, avi, and mpg files. However as soon as an mp4 or mkv container is specified as input file, regardless of what's inside,a lot of errors are dumped from the codec. It doesn't matter if it's HEVC or h264.</p>
  
  <pre><code>[h264 @ 00000000xyz] No start code is found
  [h264 @ 00000000xyz] Error splitting the input into NAL units.
  </code></pre>
  
  <p>To make everything really strange, ffplay.exe plays these files just fine.</p>
  
  <p>I realize that I can probably fix this by converting files into a raw format first, but I would like to be able to read and parse mp4 files a they are. Since I am using the pre-build libs of Zeraneo, my guess would be that something was not enabled during the build, but then I would expect ffplay to fail too. Do I need to set a flag in the format_context or codec_context, or provide some sort of filter identifier?</p>
  
  <p>Movies that play fine came from <a href="http://bbb3d.renderfarming.net/download.html" rel="noreferrer">http://bbb3d.renderfarming.net/download.html</a>, <a href="http://www.w6rz.net/" rel="noreferrer">http://www.w6rz.net/</a> and <a href="http://www.sample-videos.com/" rel="noreferrer">http://www.sample-videos.com/</a></p>
  
  <p>These work:</p>
  
  <pre><code>big_buck_bunny_480p_surround-fix.avi
  bigbuckbunny_480x272.h265
  </code></pre>
  
  <p>Being a total noob at ffmpeg, please help me understand what is wrong and how to fix it. If the pre-build libs are the culprit, then the second question is if someone has a convenient cmake setup to build this for windows X64 and x32 debug and release targets.</p>
  
  <p>Here's the source for initializing ffmpeg for reading</p>
  
  <pre><code>avdevice_register_all();
  avfilter_register_all();
  av_register_all();
  avformat_network_init();
  </code></pre>
  
  <p>The format is parsed as follows:</p>
  
  <pre><code>m_FormatContext = avformat_alloc_context();
  if (avformat_open_input(&amp;m_FormatContext, file.GetPath().ToString().c_str(), NULL, NULL) != 0) 
  {
      //std::cout &lt;&lt; "failed to open input" &lt;&lt; std::endl;
      success = false;
  }
  // find stream info
  if (success)
  {
      if (avformat_find_stream_info(m_FormatContext, NULL) &lt; 0) 
      {
          //std::cout &lt;&lt; "failed to get stream info" &lt;&lt; std::endl;
          success = false;
      }
  }
  </code></pre>
  
  <p>The stream is opened as follows:</p>
  
  <pre><code>m_VideoStream = avstream;
  m_FormatContext = formatContext;
  if (m_VideoStream)
  {
      m_StreamIndex = m_VideoStream-&gt;stream_identifier;
      AVCodecParameters *codecpar = m_VideoStream-&gt;codecpar;      
      if (codecpar)
      {
          AVCodecID codec_id = codecpar-&gt;codec_id;
          m_Decoder = avcodec_find_decoder(codec_id);
          if (m_Decoder)
          {
              m_CodecContext = avcodec_alloc_context3(m_Decoder);
              if (m_CodecContext)
              {
                  m_CodecContext-&gt;width = codecpar-&gt;width;                    
                  m_CodecContext-&gt;height = codecpar-&gt;height;
                  m_VideoSize = i3(codecpar-&gt;width, codecpar-&gt;height,1);
                  success = 0 == avcodec_open2(m_CodecContext, m_Decoder, NULL);
                  if (success)
                  {
                      if(m_CodecContext)
                      {
                          int size = av_image_get_buffer_size(format, m_CodecContext-&gt;width, m_CodecContext-&gt;height, 1);      
                          if (size &gt; 0)
                          {
                              av_frame = av_frame_alloc();
                              gl_frame = av_frame_alloc();        
                              uint8_t *internal_buffer = (uint8_t *)av_malloc(size * sizeof(uint8_t));
                              av_image_fill_arrays((uint8_t**)((AVPicture *)gl_frame-&gt;data), (int*) ((AVPicture *)gl_frame-&gt;linesize), internal_buffer, format, m_CodecContext-&gt;width, m_CodecContext-&gt;height,1);
                              m_Packet = (AVPacket *)av_malloc(sizeof(AVPacket));
                          }
                      }
                  }
                  if (!success) 
                  {
                      avcodec_close(m_CodecContext);
                      avcodec_free_context(&amp;m_CodecContext);
                      m_CodecContext = NULL;
                      m_Decoder = NULL;
                      m_VideoStream = NULL;
                  }
              }
              else
              {
                  m_Decoder = NULL;
                  m_VideoStream = NULL;
              }
          }
      }
  }
  </code></pre>
  
  <p>And decoding on a single thread:</p>
  
  <pre><code>do 
  {
      if (av_read_frame(m_FormatContext, m_Packet) &lt; 0) 
      {
          av_packet_unref(m_Packet);
          m_AllPacketsSent = true;
      }
      else
      {
          if (m_Packet-&gt;stream_index == m_StreamIndex) 
          {                   
              avcodec_send_packet(m_CodecContext, m_Packet);
          }
      }
  
      int frame_finished = avcodec_receive_frame(m_CodecContext, av_frame);
      if (frame_finished == 0) 
      {
          if (!conv_ctx) 
          {
              conv_ctx = sws_getContext(m_CodecContext-&gt;width, 
                  m_CodecContext-&gt;height, m_CodecContext-&gt;pix_fmt, 
                  m_CodecContext-&gt;width, m_CodecContext-&gt;height, format, SWS_BICUBIC, NULL, NULL, NULL);
          }
  
          sws_scale(conv_ctx, av_frame-&gt;data, av_frame-&gt;linesize, 0, m_CodecContext-&gt;height, gl_frame-&gt;data, gl_frame-&gt;linesize);
  
          switch(format)
          {
              case AV_PIX_FMT_BGR32_1:
              case AV_PIX_FMT_RGB32_1:
              case AV_PIX_FMT_0BGR32:
              case AV_PIX_FMT_0RGB32:
              case AV_PIX_FMT_BGR32:  
              case AV_PIX_FMT_RGB32:              
              {
                  m_CodecContext-&gt;bits_per_raw_sample = 32; break;                    
              }
              default:
              {
                  FWASSERT(format == AV_PIX_FMT_RGB32, "The format changed, update the bits per raw sample!"); break;
              }
          }
  
  
          size_t bufferSize = m_CodecContext-&gt;width * m_CodecContext-&gt;height * m_CodecContext-&gt;bits_per_raw_sample / 8;
          m_Buffer.Realloc(bufferSize, false,  gl_frame-&gt;data[0]);
          m_VideoSize = i3(m_CodecContext-&gt;width, m_CodecContext-&gt;height,1);
          result = true;
          // sends the image buffer straight to the locked texture here..
          // glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, codec_ctx-&gt;width, codec_ctx-&gt;height, GL_RGB, GL_UNSIGNED_BYTE, gl_frame-&gt;data[0]);
      }
  
      av_packet_unref(m_Packet);
  } while (m_Packet-&gt;stream_index != m_StreamIndex);
  
  m_FrameDecoded = result;
  </code></pre>
  
  <p>Any insight is appreciated!</p>