title: MediaCodec AAC -> PCM Decoder Configuration on Android
body: <p>I'm trying to implement  <code>AAC -&gt; PCM Decoder</code> by <code>MediaCodec</code>.</p>
  
  <p>Firstly, I successfully implemented <code>PCM -&gt; AAC Encoder</code> by  <code>MediaCodec</code> as intended as below</p>
  
  <pre><code>private boolean setEncoder(int rate)
  {
      encoder = MediaCodec.createEncoderByType("audio/mp4a-latm");
      MediaFormat format = new MediaFormat();
      format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
      format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
      format.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);
      format.setInteger(MediaFormat.KEY_BIT_RATE, 64 * 1024);//AAC-HE 64kbps
      format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectHE);
      encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
      return true;
  }
  </code></pre>
  
  <p><strong>INPUT: PCM Bitrate = 44100(Hz) x 16(bit) x 1(Monoral) =  705600 bit/s</strong></p>
  
  <p><strong>OUTPUT: AAC-HE Bitrate = 64 x 1024(bit) = 65536 bit/s</strong></p>
  
  <p><strong>So, the data size is approximately compressed <code>x11</code></strong> ,and I confirmed this working by observing a log</p>
  
  <ul>
  <li>AudioRecoder﹕ 4096 bytes read</li>
  <li>AudioEncoder﹕ 369 bytes encoded</li>
  </ul>
  
  <p>the data size is approximately compressed <code>x11</code>, so far so good.</p>
  
  <p>Now, I have a UDP server to receive the encoded data, then decode it.</p>
  
  <p>The decoder profile is set as follows:</p>
  
  <pre><code>private boolean setDecoder(int rate)
  {
      decoder = MediaCodec.createDecoderByType("audio/mp4a-latm");
      MediaFormat format = new MediaFormat();
      format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
      format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
      format.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);
      format.setInteger(MediaFormat.KEY_BIT_RATE, 64 * 1024);//AAC-HE 64kbps
      format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectHE);
      decoder.configure(format, null, null, 0);
  
      return true;
  }
  </code></pre>
  
  <p>Since UDPserver packet buffer size is <code>1024</code></p>
  
  <ul>
  <li>UDPserver   ﹕ 1024 bytes received</li>
  </ul>
  
  <p>and since this is the compressed AAC data, I would expect the decoding size will be </p>
  
  <p>approximately 1024 <code>x11</code>, however the actual result is</p>
  
  <ul>
  <li>AudioDecoder﹕ 8192 bytes decoded</li>
  </ul>
  
  <p>It's approximately <code>x8</code>, and I feel something wrong.</p>
  
  <p>Can anyone give me a thought on this issue? Thank you.</p>
  
  <p>PS.</p>
  
  <p>The decoder code is as follows just in case:</p>
  
  <pre><code>byte[] buffer2 = new byte[1024];
  DatagramPacket packet = new DatagramPacket(buffer2, buffer2.length);
  //..............
  //receive UDP packet here..............
  ds.receive(packet);
  sockAddress = packet.getSocketAddress();
  address = sockAddress.toString();
  
  // now decoding
  inputBuffers = decoder.getInputBuffers();
  outputBuffers = decoder.getOutputBuffers();
  inputBufferIndex = decoder.dequeueInputBuffer(-1);
  if (inputBufferIndex &gt;= 0)
  {
      inputBuffer = inputBuffers[inputBufferIndex];
      inputBuffer.clear();
      inputBuffer.put(buffer2);
      decoder.queueInputBuffer(inputBufferIndex, 0, buffer2.length, 0, 0);
  }
  
  bufferInfo = new MediaCodec.BufferInfo();
  outputBufferIndex = decoder.dequeueOutputBuffer(bufferInfo, 0);
  
  while (outputBufferIndex &gt;= 0)
  {
      outputBuffer = outputBuffers[outputBufferIndex];
      outData = new byte[bufferInfo.size];
      outputBuffer.get(outData);
  
      Log.d("AudioDecoder", outData.length + " bytes decoded");
  
      decoder.releaseOutputBuffer(outputBufferIndex, false);
      outputBufferIndex = decoder.dequeueOutputBuffer(bufferInfo, 0);
  }
  </code></pre>