title: How do I convert ADPCM to PCM using FFmpeg?
body: <p>I have a video feed that sends me audio using the ADPCM codec.  However, android only supports PCM format.  How can I convert the ADPCM audio feed into a PCM audio feed?</p>
  
  <p>The answer to this may be similar to the answer to <a href="https://stackoverflow.com/questions/14376218/convert-m4a-to-pcm-using-libavcodec">this question</a>.</p>
  
  <p>I have successfully decoded the frame with this code:</p>
  
  <pre><code>int len = avcodec_decode_audio4(pAudioCodecCtx, pAudioFrame, &amp;frameFinished, &amp;packet);
  </code></pre>
  
  <p>Is the secret here to use a reverse <strong>encode</strong> function?</p>
  
  <p>Here is what I have so far in my audio decode function:</p>
  
  <pre><code>&lt;!-- language: c --&gt;
  if(packet_queue_get(env, javaThread, pAudioPacketQueue, &amp;packet, 1) &lt; 0) {
      LOGE("audio - after get packet failed");
      return;
  }
  LOGD("Dequeued audio packet");
  
  // calculate frame size
  int frameSize;
  if (pPcmAudioCodecCtx-&gt;frame_size) {
      frameSize = pPcmAudioCodecCtx-&gt;frame_size;
  } else {
      /* if frame_size is not set, the number of samples must be
       * calculated from the buffer size */
      int64_t nb_samples = (int64_t)AUDIO_PCM_OUTBUFF_SIZE * 8 /
              (av_get_bits_per_sample(pPcmAudioCodecCtx-&gt;codec_id) *
                      pPcmAudioCodecCtx-&gt;channels);
      frameSize = nb_samples;
  }
  
  int pcmBytesPerSample = av_get_bytes_per_sample(pPcmAudioCodecCtx-&gt;sample_fmt);
  int pcmFrameBytes = frameSize * pcmBytesPerSample * pPcmAudioCodecCtx-&gt;channels;
  
  uint8_t *pDataStart = packet.data;
  while(packet.size &gt; 0) {
      int len = avcodec_decode_audio4(pAudioCodecCtx, pAudioFrame, &amp;frameFinished, &amp;packet);
      LOGD("Decoded ADPCM frame");
  
      if (len &lt; 0) {
          LOGE("Error while decoding audio");
          return;
      }
  
      if (frameFinished) {
          // store frame data in FIFO buffer
          uint8_t *inputBuffer = pAudioFrame-&gt;data[0];
          int inputBufferSize = pAudioFrame-&gt;linesize[0];
          av_fifo_generic_write(fifoBuffer, inputBuffer, inputBufferSize, NULL);
          LOGD("Added ADPCM frame to FIFO buffer");
  
          // check if fifo buffer has enough data for a PCM frame
          while (av_fifo_size(fifoBuffer) &gt;= pcmFrameBytes) {
              LOGI("PCM frame data in FIFO buffer");
  
              // read frame's worth of data from FIFO buffer
              av_fifo_generic_read(fifoBuffer, pAudioPcmOutBuffer, pcmFrameBytes, NULL);
              LOGD("Read data from FIFO buffer into pcm frame");
  
  
              avcodec_get_frame_defaults(pPcmAudioFrame);
              LOGD("Got frame defaults");
  
              pPcmAudioFrame-&gt;nb_samples = pcmFrameBytes / (pPcmAudioCodecCtx-&gt;channels *
                      pcmBytesPerSample);
  
              avcodec_fill_audio_frame(pPcmAudioFrame, pPcmAudioCodecCtx-&gt;channels,
                      pPcmAudioCodecCtx-&gt;sample_fmt,
                      pAudioPcmOutBuffer, pcmFrameBytes, 1);
              LOGD("Filled frame audio with data");
  
              // fill audio play buffer
              int dataSize = pPcmAudioFrame-&gt;linesize[0];
              LOGD("Data to output: %d", dataSize);
              jbyteArray audioPlayBuffer = (jbyteArray) env-&gt;GetObjectField(ffmpegCtx, env-&gt;GetFieldID(cls, "audioPlayBuffer", "[B"));
              jbyte *bytes = env-&gt;GetByteArrayElements(audioPlayBuffer, NULL);
              memcpy(bytes, pPcmAudioFrame-&gt;data[0], dataSize);
              env-&gt;ReleaseByteArrayElements(audioPlayBuffer, bytes, 0);
              LOGD("Copied data into Java array");
  
              env-&gt;CallVoidMethod(player, env-&gt;GetMethodID(playerCls, "updateAudio", "(I)V"), dataSize);
          }
  </code></pre>