ffmpeg解码音频的两种方式(一)av_parser_parse解析器

使用ffmpeg解析器自动提取音频单帧数据:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
//SDL
#include "sdl/SDL.h"
#include "sdl/SDL_thread.h"
};

#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio

//Output PCM
#define OUTPUT_PCM 1
//Use SDL
#define USE_SDL 1

static  Uint8  *audio_chunk; 
static  Uint32  audio_len; 
static  Uint8  *audio_pos; 

void  fill_audio(void *udata,Uint8 *stream,int len){ 
	//SDL 2.0
	SDL_memset(stream, 0, len);
	if(audio_len==0)		/*  Only  play  if  we  have  data  left  */ 
			return; 
	len=(len>audio_len?audio_len:len);	/*  Mix  as  much  data  as  possible  */ 

	SDL_MixAudio(stream,audio_pos,len,SDL_MIX_MAXVOLUME);
	audio_pos += len; 
	audio_len -= len; 
} 
//-----------------

static char err_buf[128] = {0};
static char* av_get_err(int errnum)
{  
    av_strerror(errnum, err_buf, 128);
    return err_buf;
}

int nWriteBytes=0;
int _tmain(int argc, _TCHAR* argv[])
{
	AVFormatContext	*pFormatCtx;
	int				i, audioStream;
	AVCodecContext	*pCodecCtx;
	AVCodec			*pCodec;
	AVCodecParserContext * pParser;

	//char url[]="WavinFlag.aac";
	//char url[]="1.mp2";
	char url[]="2.mp3";

	av_register_all();
	pCodec=avcodec_find_decoder(AV_CODEC_ID_MP3/*AV_CODEC_ID_AAC*/);//AV_CODEC_ID_MP2
	if(pCodec==NULL){
		printf("Codec not found.\n");
		return -1;
	}

	pCodecCtx = avcodec_alloc_context3(pCodec);

	// Open codec
	if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
		printf("Could not open codec.\n");
		return -1;
	}

	pParser = av_parser_init(/*AV_CODEC_ID_AAC*/AV_CODEC_ID_MP3);
	if(!pParser){
		printf("av_parser_init AV_CODEC_ID_MP3 failed.\n");
		return -1;
	}

	FILE *pFile=NULL;
	FILE *pInFile = NULL;
#if OUTPUT_PCM
	pFile=fopen("output.pcm", "wb");
	pInFile=fopen(url, "rb");
#endif
	
	//Out Audio Param
	uint64_t out_channel_layout=AV_CH_LAYOUT_STEREO;
	int out_nb_samples=/*1024*/1152;
	AVSampleFormat out_sample_fmt=AV_SAMPLE_FMT_S16;
	int out_sample_rate=44100;
	int out_channels=av_get_channel_layout_nb_channels(out_channel_layout);
	//输出内存大小
	int out_buffer_size=av_samples_get_buffer_size(NULL,out_channels ,out_nb_samples,out_sample_fmt, 1);

	uint8_t *out_buffer=(uint8_t *)av_malloc(MAX_AUDIO_FRAME_SIZE*2);
	
	AVFrame *pFrame = av_frame_alloc();
	avcodec_get_frame_defaults(pFrame);

//SDL------------------
#if USE_SDL
	//Init
	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {  
		printf( "Could not initialize SDL - %s\n", SDL_GetError()); 
		return -1;
	}
	//SDL_AudioSpec
	SDL_AudioSpec wanted_spec;
	wanted_spec.freq = out_sample_rate; 
	wanted_spec.format = AUDIO_S16SYS; 
	wanted_spec.channels = out_channels; 
	wanted_spec.silence = 0; 
	wanted_spec.samples = out_nb_samples/*pCodecCtx->frame_size*/; 
	wanted_spec.callback = fill_audio; 
	wanted_spec.userdata = pCodecCtx; 
		
	if (SDL_OpenAudio(&wanted_spec, NULL)<0){ 
		printf("can't open audio.\n"); 
		return -1; 
	} 
#endif
	printf("Bitrate:\t %3d\n", pFormatCtx->bit_rate);
	//printf("Decoder Name:\t %s\n", pCodecCtx->codec->long_name);
	//printf("Channels:\t %d\n", pCodecCtx->channels);
	//printf("Sample per Second\t %d \n", pCodecCtx->sample_rate);

	uint32_t ret,len = 0;
	int got_picture;
	int index = 0;
	struct SwrContext *au_convert_ctx;
	au_convert_ctx = swr_alloc();
	//au_convert_ctx=swr_alloc_set_opts(au_convert_ctx,out_channel_layout, out_sample_fmt, out_sample_rate,
	//	pCodecCtx->channel_layout,pCodecCtx->sample_fmt , pCodecCtx->sample_rate,0, NULL);
	//swr_init(au_convert_ctx);
	
	int data_size = 0;
	int size = 0;
	int cnt=0;
	int offset=0;

	//FILE *myout=fopen("output_log.txt","wb+");
	FILE *myout=stdout;

	unsigned char *aacframe=(unsigned char *)malloc(1024*5);
	unsigned char *aacbuffer=(unsigned char *)malloc(1024*1024);

	FILE *ifile = fopen(url, "rb");
	if(!ifile){
		printf("Open file error");
		return -1;
	}

	printf(" aac 帧: NUM - Profile - Frequency - Size |\n");
	
	while(!feof(ifile)){
		data_size = fread(aacbuffer+offset, 1, 1024*1024-offset, ifile);
		unsigned char* input_data = aacbuffer+offset;

		uint8_t *pParserBufOut = NULL;
		int pParserOutLength = 0;

		while(data_size>0){
			int nParserLen = av_parser_parse2(pParser, pCodecCtx, &pParserBufOut, &pParserOutLength,
												(uint8_t *)input_data, data_size,0, 0, 0);			
			if(nParserLen>0){
				input_data += nParserLen;
				data_size  -= nParserLen;

				if(pParserOutLength>0){
					AVPacket avpkt;
					av_init_packet(&avpkt);
					avpkt.data = (uint8_t *)pParserBufOut;  
					avpkt.size = pParserOutLength;

					//此处不需要循环,每次解析的都是一个es流的一个单帧,直接解码成功
					while(avpkt.size>0){
						ret = avcodec_decode_audio4( pCodecCtx, pFrame,&got_picture, &avpkt);
						if ( ret < 0 ) {
							printf("Error in decoding audio frame.\n");
							return -1;
						}
						//printf("avcodec_decode_audio4 %s \n", av_get_err(ret));
						//if(AVERROR_INVALIDDATA==ret){
						//	continue;
						//}

						//需要解码后初始化,否则pCodecCtx的参数尚未获取到(纯净版不用预先指定参数)
						static bool bSwrInit=false;
						if(!bSwrInit){
							au_convert_ctx=swr_alloc_set_opts(au_convert_ctx,out_channel_layout, out_sample_fmt, out_sample_rate,
								pCodecCtx->channel_layout,pCodecCtx->sample_fmt , pCodecCtx->sample_rate,0, NULL);
							swr_init(au_convert_ctx);
							bSwrInit=true;
						}
						
						if(got_picture > 0){					
							int nBytes = swr_convert(au_convert_ctx,&out_buffer, MAX_AUDIO_FRAME_SIZE,(const uint8_t **)pFrame->data , pFrame->nb_samples);
							if(nBytes<0){
								printf("swr_convert failed \n");
								return 0;
							}
							nWriteBytes = av_samples_get_buffer_size(NULL, out_channels, nBytes, out_sample_fmt, 1);
							if(nWriteBytes<0){
								printf("av_samples_get_buffer_size failed \n");
								return 0;
							}
							fwrite(out_buffer, 1, nWriteBytes, pFile);
						}

						//设置音频数据缓冲,PCM数据
						audio_chunk = (Uint8 *) out_buffer; 
						//设置音频数据长度
						audio_len = nWriteBytes;
						audio_pos = audio_chunk;
						//回放音频数据 
						SDL_PauseAudio(0);
						while(audio_len>0)//等待直到音频数据播放完毕! 
							SDL_Delay(1); 
	
						avpkt.data+=ret;
						avpkt.size-=ret;
					}
					av_free_packet(&avpkt);
				}
			}
		}		
	}
	fclose(ifile);
	free(aacbuffer);
	fclose(pFile);

#if USE_SDL
	SDL_CloseAudio();//关闭音频设备 
	SDL_Quit();
#endif
	
	swr_free(&au_convert_ctx);
	av_free(out_buffer);
	avcodec_close(pCodecCtx);
	av_parser_close(pParser);
	
	return 0;
}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值