Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:59:57+00:00 2026-06-17T22:59:57+00:00

I have a video feed that sends me audio using the ADPCM codec. However,

  • 0

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?

The answer to this may be similar to the answer to this question.

I have successfully decoded the frame with this code:

int len = avcodec_decode_audio4(pAudioCodecCtx, pAudioFrame, &frameFinished, &packet);

Is the secret here to use a reverse encode function?

Here is what I have so far in my audio decode function:

<!-- language: c -->
if(packet_queue_get(env, javaThread, pAudioPacketQueue, &packet, 1) < 0) {
    LOGE("audio - after get packet failed");
    return;
}
LOGD("Dequeued audio packet");

// calculate frame size
int frameSize;
if (pPcmAudioCodecCtx->frame_size) {
    frameSize = pPcmAudioCodecCtx->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->codec_id) *
                    pPcmAudioCodecCtx->channels);
    frameSize = nb_samples;
}

int pcmBytesPerSample = av_get_bytes_per_sample(pPcmAudioCodecCtx->sample_fmt);
int pcmFrameBytes = frameSize * pcmBytesPerSample * pPcmAudioCodecCtx->channels;

uint8_t *pDataStart = packet.data;
while(packet.size > 0) {
    int len = avcodec_decode_audio4(pAudioCodecCtx, pAudioFrame, &frameFinished, &packet);
    LOGD("Decoded ADPCM frame");

    if (len < 0) {
        LOGE("Error while decoding audio");
        return;
    }

    if (frameFinished) {
        // store frame data in FIFO buffer
        uint8_t *inputBuffer = pAudioFrame->data[0];
        int inputBufferSize = pAudioFrame->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) >= 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->nb_samples = pcmFrameBytes / (pPcmAudioCodecCtx->channels *
                    pcmBytesPerSample);

            avcodec_fill_audio_frame(pPcmAudioFrame, pPcmAudioCodecCtx->channels,
                    pPcmAudioCodecCtx->sample_fmt,
                    pAudioPcmOutBuffer, pcmFrameBytes, 1);
            LOGD("Filled frame audio with data");

            // fill audio play buffer
            int dataSize = pPcmAudioFrame->linesize[0];
            LOGD("Data to output: %d", dataSize);
            jbyteArray audioPlayBuffer = (jbyteArray) env->GetObjectField(ffmpegCtx, env->GetFieldID(cls, "audioPlayBuffer", "[B"));
            jbyte *bytes = env->GetByteArrayElements(audioPlayBuffer, NULL);
            memcpy(bytes, pPcmAudioFrame->data[0], dataSize);
            env->ReleaseByteArrayElements(audioPlayBuffer, bytes, 0);
            LOGD("Copied data into Java array");

            env->CallVoidMethod(player, env->GetMethodID(playerCls, "updateAudio", "(I)V"), dataSize);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:59:58+00:00Added an answer on June 17, 2026 at 10:59 pm

    It turns out that the audio_decode_ functions return 16 bit PCM format, and that I just didn’t know how to access it properly.

    Here is the altered code inside the packet loop that plays the audio based on avcodec_decode_audio4.

    int len = avcodec_decode_audio4(pAudioCodecCtx, pAudioFrame, &frameFinished, &packet);
    
    if (len < 0) {
        LOGE("Error while decoding audio");
        return;
    }
    
    if (frameFinished) {
        int planeSize;
        uint8_t *pcmBuffer = pAudioFrame->extended_data[0];
        int dataSize = av_samples_get_buffer_size(&planeSize, pAudioCodecCtx->channels,
                                                           pAudioFrame->nb_samples,
                                                           pAudioCodecCtx->sample_fmt, 1);
        // fill audio play buffer
        jbyteArray audioPlayBuffer = (jbyteArray) env->GetObjectField(ffmpegCtx, env->GetFieldID(cls, "audioPlayBuffer", "[B"));
        jbyte *bytes = env->GetByteArrayElements(audioPlayBuffer, NULL);
        memcpy(bytes, pcmBuffer, dataSize);
        env->ReleaseByteArrayElements(audioPlayBuffer, bytes, 0);
        env->CallVoidMethod(player, env->GetMethodID(playerCls, "updateAudio", "(I)V"), dataSize);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an art project that will require processing a live video feed to
we have a mRSS feed that contains video information. e.g <video fileTitletesting fileurl=http://www.example.com/test.flv> when
I have an ASP.NET application that I need to show a video feed from
I have a page, videos.php, that loads a video feed through Ajax. On the
I have a C# Windows Form Application and a live video feed. I need
I have video served by Ooyala that plays fine on all devices. The problem
I have a video that is 60 seconds long. I am working on adding
We have a video (13 minutes long) which we would like to control using
I have a Flash-file with some ActionScript code that loads an RSS-feed and animates
I am using vlc to capture a video and audio stream and display it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.