ffmpeg(3):ffmpeg结合SDL2.0解码音频流

我自己解码的时候遇到点问题,暂时先占位。后面若搞定了,再添加。



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// 2014/3/9 -- 19:24更新

    FILE *pFile;
    pFile = fopen("wavinflag.pcm", "wb");

    while( av_read_frame(pFormatCtx, packet) >= 0 ) {
        if( packet->stream_index == audioStream ) {
            int got_picture;
            int ret = avcodec_decode_audio4( audioCodecCtx, audioFrame, &got_picture, packet);
            if( ret < 0 ) {
                printf("Error in decoding audio frame.\n");
                exit(0);
            }
            if( got_picture ) {
                int in_samples = audioFrame->nb_samples;
                short *sample_buffer = (short*)malloc(audioFrame->nb_samples * 2 * 2);
                memset(sample_buffer, 0, audioFrame->nb_samples * 4);
                printf("in_samples = %d\n", in_samples);

                int i=0;
                float *inputChannel0 = (float*)(audioFrame->extended_data[0]);

                // Mono
                if( audioFrame->channels == 1 ) {
                    for( i=0; i<in_samples; i++ ) {
                        float sample = *inputChannel0++;
                        if( sample < -1.0f ) {
                            sample = -1.0f;
                        } else if( sample > 1.0f ) {
                            sample = 1.0f;
                        }

                        sample_buffer[i] = (int16_t)(sample * 32767.0f);
                    }
                } else { // Stereo
                    float* inputChannel1 = (float*)(audioFrame->extended_data[1]);
                    for( i=0; i<in_samples; i++) {
                        sample_buffer[i*2] = (int16_t)((*inputChannel0++) * 32767.0f);
                        sample_buffer[i*2+1] = (int16_t)((*inputChannel1++) * 32767.0f);
                    }
                }

                fwrite(sample_buffer, 2, in_samples*2, pFile);
            }
        }

        // Free the packet that was allocated by av_read_frame
        av_free_packet(packet);
    }

在使用ffmpeg解码aac的时候,如果使用avcodec_decode_audio4函数解码,那么解码出来的会是AV_SAMPLE_FMT_FLTP 格式的数据( float, 4bit , planar), 如果我们希望得到16bit的数据(如AV_SAMPLE_FMT_S16P数据),那么我们需要转换一下:

解決方式: 
   將 samples 由 32bits 转为16bits. 参考 ffmpeg samplefmt.h 
   若 sample 是 AV_SAMPLE_FMT_FLTP,則 sample 會是 float 格式,且值域为 [-1.0, 1.0] 
   若 sample 是 AV_SAMPLE_FMT_S16, 則 sample 會是 int16 格式,且值域为 [-32767, +32767] 

这里SDL2.0播放pcm有点问题,还没弄会。

将FLTP转为S16代码,详细代码这里下载。


你可能感兴趣的:(ffmpeg(3):ffmpeg结合SDL2.0解码音频流)