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);
}

猜你喜欢

转载自kongweile.iteye.com/blog/1576137