ffmpeg对音频解码的一般步骤

以下是我自己写的小demo

 

#include <errno.h>
#include <android/log.h>
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>

AVFormatContext *fmt_ctx=NULL;
AVCodecContext *codec_ctx;
AVCodec *codec;
AVPacket packet;
AVFrame *frame;
int frame_ptr;

void mylog(const char * fmt, ...);

void Java_com_test_multi_MultiPlayerActivity_test(){
	avcodec_register_all();
	av_register_all();
    if(avformat_open_input(&fmt_ctx,"/sdcard/wma",NULL,NULL)!=0)
		{mylog("errno %d",errno);}
    if(avformat_find_stream_info(fmt_ctx,NULL)<0)
    	{mylog("errno %d",errno);}
    codec_ctx=fmt_ctx->streams[0]->codec;
    codec = avcodec_find_decoder(codec_ctx->codec_id);
    mylog("id=%d",codec->id);
    if (avcodec_open2(codec_ctx, codec,NULL) < 0)
    	{mylog("errno %d",errno);}
    while(av_read_frame(fmt_ctx,&packet)==0){
    	frame=avcodec_alloc_frame();
    	avcodec_decode_audio4(codec_ctx, frame, &frame_ptr,&packet);
    	mylog("frame_ptr %d",frame_ptr);
    	if(frame_ptr){
    		int data_size = av_samples_get_buffer_size(frame->linesize,codec_ctx->channels,frame->nb_samples,codec_ctx->sample_fmt, 0);
            mylog("data count %d",data_size);
            /*int i=0;
            for(;i<data_size;++i){
            	mylog("data %d",(frame->data[0])[i]);
            }*/
    	}
    	av_free_packet(&packet);
    }
    avformat_close_input(&fmt_ctx);
}

void mylog(const char* fmt,...){
	va_list arg_ptr;
	va_start(arg_ptr, fmt);
	__android_log_vprint(ANDROID_LOG_ERROR, "multi",fmt, arg_ptr);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值