ios ffmpeg audio decode
阿新 • • 發佈:2019-01-31
ffmepg解碼音訊。
#import "FFMpegAudioDecoder.h" #ifdef __cplusplus extern "C" { #endif #include <libavutil/opt.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> #ifdef __cplusplus }; #endif @interface FFMpegAudioDecoder(){ AVCodec *_audioCodec; AVCodecContext *_pCodecCtx; AVFrame *_pFrame; char* _pcmBuf; } @end @implementation FFMpegAudioDecoder -(int)intDecoder{ av_register_all(); //NSString* codecName = @"libfdk_aac"; //_audioCodec = avcodec_find_decoder_by_name([codecName UTF8String]); _audioCodec = avcodec_find_decoder(AV_CODEC_ID_AAC); _pCodecCtx = avcodec_alloc_context3(_audioCodec); if (!_audioCodec) return -1; _pCodecCtx->codec_id = AV_CODEC_ID_AAC; _pCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO; if(avcodec_open2(_pCodecCtx, _audioCodec,NULL) >= 0) _pFrame = av_frame_alloc();// Allocate video frame else return -1; return 0; } -(int)decode:(char*)pktaudio len:(int)len{ AVPacket pkt = { .data = (uint8_t*)pktaudio, .size = len, }; int got_frame = 0; int decLen = avcodec_decode_audio4(_pCodecCtx, _pFrame, &got_frame, &pkt); if (!got_frame) { NSLog(@"avcodec_decode_audio4 failed ."); return -1; } NSLog(@"avcodec_decode_audio4 succ len : %d",decLen); return 0; } -(void)free{ if(_pCodecCtx){ avcodec_close(_pCodecCtx); _audioCodec = NULL; } if (_pFrame) { av_frame_free(&_pFrame); _pFrame = NULL; } if (_pcmBuf) { free(_pcmBuf); _pcmBuf = NULL; } } @end