title: MediaCodec Crashed During Decoding
body: <p>I am trying to play video from H264 stream coming from vlc over udp.  </p>
  
  <p>But not getting any success so far.
  I take reference from this example EncodeDecodeTest.java but still not working</p>
  
  <p><a href="http://bigflake.com/mediacodec/" rel="nofollow noreferrer">http://bigflake.com/mediacodec/</a></p>
  
  <p>Details Before Exception occured</p>
  
  <pre><code>    E/VDO_LOG: [Vdec_Drv_H264_open] VAL_CHIP_NAME_MT6752, VAL_CHIP_NAME_MT6582, VAL_CHIP_NAME_MT8135, VAL_CHIP_NAME_MT6592, VAL_CHIP_NAME_MT8127, VAL_CHIP_NAME_ROME
      E/VDO_LOG: [Err] invalid NALU - 1 18 47 170 90!!
       E/VDO_LOG: [Err] invalid NALU - 1 224 2 10 128!!
       E/VDO_LOG: [Err] invalid NALU - 1 234 107 24 1!!
       E/VDO_LOG: [Err, h264_dec_init] Fail to open Vdec Drv H264 instance! -11
       E/VDO_LOG: [Err] Error code -10
       E/MtkOmxVdec: [0xb7a47e38] Error!! Cannot init driver
       E/VDO_LOG: [Err, h264_dec_deInit] Invalid input argument
       E/M4U_L: ~MTKM4UDrv 66.
       E/VDO_LOG: [eHalEMICtrl]Profile_Change(BWCPT_VIDEO_PLAYBACK, false)
      [BWC INFO](29451): get_bwc_mm_property success
      [BWC INFO](29451): BWC_SETTING::DumpInfo-------
       [BWC INFO](29451):          sensor_size =      0 x      0
       [BWC INFO](29451):              vr_size =    320 x    560
      [BWC INFO](29451):            disp_size =      0 x      0
       [BWC INFO](29451):              tv_size =      0 x      0
      [BWC INFO](29451):                  fps =      0
      [BWC INFO](29451):      venc_codec_type =      0
     [BWC INFO](29451):      vdec_codec_type =      2
       [BWC INFO](29451): ----------------------------
      [BWC INFO](29451): Read DDR type string:LPDDR3
     [BWC INFO](29451): DDR Type = 2
       [BWC INFO](29451): smi_bw_ctrl_set: scen 2, turn off
       [BWC INFO](29451): emi_bw_ctrl_set: CON_SCE_VP OFF
      [BWC INFO](29451): set_bwc_mm_property: propterty_id=0, value1=0, value2=0
       [BWC INFO](29451): Profile_Change:[BWCPT_VIDEO_PLAYBACK]:OFF,current concurrency is 0x0
  </code></pre>
  
  <p>Exception</p>
  
  <pre><code>     E/VDO_LOG: [eValDeInit] close VCodec_ValFd, VCodec_ValFd = -1
       E/VDO_LOG: [ERROR] fail to open mcdi_file
       E/MtkOmxVdec: [0xb7a47e38] InitVideoDecodeHW failed
       E/MtkOmxVdec: [0xb7a47e38] something wrong when decoding....
       E/ACodec: [OMX.MTK.VIDEO.DECODER.AVC] ERROR(0x80001005)
       E/ACodec: signalError(omxError 0x80001005, internalError -2147483648)
       E/MediaCodec: Codec reported err 0xfffffbb1, actionCode 0, while in state 6
       E/AndroidRuntime: FATAL EXCEPTION: Thread-2966
      Process: com.rvale.videocodec, PID: 29410
      java.lang.IllegalStateException
           at android.media.MediaCodec.native_dequeueOutputBuffer(Native Method)
           at android.media.MediaCodec.dequeueOutputBuffer(MediaCodec.java:1041)
           at com.rvale.videocodec.VideoPlayer$MediaCodecThread.run(VideoPlayer.java:432)
  </code></pre>
  
  <p>Media Codec Configuration</p>
  
  <pre><code>try {
              mediaCodec = MediaCodec.createDecoderByType("video/avc");
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          mediaFormat=MediaFormat.createVideoFormat("video/avc", 464,290);//new MediaFormat();
  
  
           mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE,23);
          MediaCodecInfo mediaCodecInfo=selectCodec(MediaFormat.MIMETYPE_VIDEO_AVC);
          int colorFormat = selectColorFormat(mediaCodecInfo, MediaFormat.MIMETYPE_VIDEO_AVC);
         //mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT,colorFormat);
          info = new MediaCodec.BufferInfo();
          assert mediaCodec != null;
          mediaCodec.configure(mediaFormat,getHolder().getSurface(),null,0);
  </code></pre>
  
  <p>This is how I am Feeding to decoder, Adding NAL Start as mentioned in this post </p>
  
  <p><a href="https://stackoverflow.com/questions/7665217/how-to-process-raw-udp-packets-so-that-they-can-be-decoded-by-a-decoder-filter-i">How to process raw UDP packets so that they can be decoded by a decoder filter in a directshow source filter</a></p>
  
  <pre><code>public void feedToDecoder(byte[] buffer)
      {
  
          byte[] combined=new byte[NAL_START.length+buffer.length];
           System.arraycopy(NAL_START,0,combined,0,NAL_START.length);
           System.arraycopy(buffer,0,combined,NAL_START.length,buffer.length);
           int inputBufferId = mediaCodec.dequeueInputBuffer(-1);
          if (inputBufferId &gt;= 0) {
              ByteBuffer inputBuffer = mediaCodec.getInputBuffer(inputBufferId);
              // fill inputBuffer with valid data
               inputBuffer.put(ByteBuffer.wrap(combined));
              mediaCodec.queueInputBuffer(inputBufferId, 0,buffer.length,info.presentationTimeUs,0);
          }
          info.presentationTimeUs+=1;
      } 
  </code></pre>
  
  <p>Deqeue Buffer</p>
  
  <pre><code>MediaCodec.BufferInfo bufferInfo=new MediaCodec.BufferInfo();
          while(decoding)
          {
              bufferInfo.presentationTimeUs+=1;
              int outputBufferId = mediaCodec.dequeueOutputBuffer(bufferInfo,-1);
              if (outputBufferId &gt;= 0) {
                  ByteBuffer outputBuffer = mediaCodec.getOutputBuffer(outputBufferId);
                   mediaCodec.releaseOutputBuffer(outputBufferId, true);
  
              } else if (outputBufferId == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                  // Subsequent data will conform to new format.
                  // Can ignore if using getOutputFormat(outputBufferId)
              }
          }
  </code></pre>
  
  <p>PS : I tried to decode packet Online and it successfully extracted, I am removing first 28 bytes before feeding it to decoder.</p>
  
  <p>Why this exception occurred and how to resolve it ?</p>
  
  <p>Image At Initial phase of decoding
  <a href="https://i.stack.imgur.com/Y9sYp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y9sYp.png" alt="image"></a></p>