FFmpeg and types of samples

≡放荡痞女 提交于 2019-12-04 16:49:46

Keep a counter of decoded bytes that is accumulated with len after each call to avcodec_decode_audio for a single AVPacket. Then use it as offset at the Java byte array:

int decoded = 0;
  ...
int len = avcodec_decode_audio3(aCodecCtx, (uint8_t *) pAudioBuffer, &data_size, &packet);
  ...
memcpy(bytes + decoded, (uint8_t *) pAudioBuffer, len);
  ...
size -= len;
decoded += len;

You're overwriting the beginning of the buffer over and over instead of appending.

here is my audio decode code, hope it would help you.

int16_t* samples = (int16_t*)av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
int sample_size;

while(!m_quit && av_read_frame_wrapper(m_pAVFormatContext, &packet) >= 0) {
    if(packet.stream_index == m_audioStream) {
        org_data = packet.data;
        org_size = packet.size;
        LOGV("audio packet size = %d", packet.size);
        while (!m_quit && packet.size > 0) {
            sample_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
            len = avcodec_decode_audio3(m_pAVCodecContext, samples, &sample_size, &packet);
            LOGV("sample_size = %d, len = %d", sample_size, len);
            if (len < 0) {
                LOGE("Error when decoding...");
                break;
            }

            if (sample_size > 0) {
                //begin = now_ms();

                //audio_output_native(samples, sample_size);

                /* use audio queue */
                SampleNode* node = new SampleNode(samples, sample_size);
                m_pSampleQueue->enqueue(node);

                //end = now_ms();

                //LOGV("sample_size > 0, output audio samples cost %llu ms\n", end - begin);
            }
            packet.size -= len;
            packet.data += len;
        }

        packet.size = org_size;
        packet.data = org_data;

        av_free_packet(&packet);
    } else {
        av_free_packet(&packet);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!