ffmpeg(3):ffmpeg结合SDL2.0解码音频流
来源:互联网 发布:卓讯数据库打开 编辑:程序博客网 时间:2024/05/01 03:46
我自己解码的时候遇到点问题,暂时先占位。后面若搞定了,再添加。
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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代码,详细代码这里下载。
0 0
- ffmpeg(3):ffmpeg结合SDL2.0解码音频流
- ffmpeg (三):ffmpeg结合SDL2.0解码音频流
- ffmpeg(2):ffmpeg结合SDL2.0解码视频流
- ffmpeg (二):ffmpeg结合SDL2.0解码视频流
- ffmpeg SDL2.0播放音频
- ffmpeg音频解码
- FFMPEG音频解码浅析
- FFmpeg - 音频解码过程
- FFmpeg - 音频解码
- FFMPEG 解码音频
- FFMPEG音频解码浅析
- ffmpeg 开源视、音频解码
- FFmpeg解码音频
- ffmpeg and SDL2.0
- 如何在Android用FFmpeg+SDL2.0解码显示图像
- 如何在Android用FFmpeg+SDL2.0解码声音
- 如何在Android用FFmpeg+SDL2.0解码图像线程
- ffmpeg+sdl2.0做一个简单的音频播放器
- 友元函数的几点注意事项和实例
- A+B Problem(V)
- UML之活动图
- 常用快捷键—Webstorm入门指南
- 学校1026: 大数加法
- ffmpeg(3):ffmpeg结合SDL2.0解码音频流
- 另一种阶乘问题
- ffmpeg(4):ffmpeg, ffplay, ffprobe用法
- 第 二周项目2有序的结构体数组
- BZOJ 2333: [SCOI2011]棘手的操作
- Storm 原理机制杂记
- 史上最短论文
- 壮志难酬
- Myeclipse导入项目注释乱码问题的解决