void SimpleAudio::Init() { #ifdef USE_FFMPEG avcodec_register_all(); av_register_all(); InitFFmpeg(); frame_ = av_frame_alloc(); // Get Audio Codec ctx int audioCodecId = GetAudioCodecID(audioType); if (!audioCodecId) { ERROR_LOG(ME, "This version of FFMPEG does not support Audio codec type: %08x. Update your submodule.", audioType); return; } // Find decoder codec_ = avcodec_find_decoder((AVCodecID)audioCodecId); if (!codec_) { // Eh, we shouldn't even have managed to compile. But meh. ERROR_LOG(ME, "This version of FFMPEG does not support AV_CODEC_ctx for audio (%s). Update your submodule.", GetCodecName(audioType)); return; } // Allocate codec context codecCtx_ = avcodec_alloc_context3(codec_); if (!codecCtx_) { ERROR_LOG(ME, "Failed to allocate a codec context"); return; } codecCtx_->channels = channels_; codecCtx_->channel_layout = channels_ == 2 ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO; codecCtx_->sample_rate = sample_rate_; codecOpen_ = false; #endif // USE_FFMPEG }
SimpleAudio::SimpleAudio(u32 ctxPtr, int audioType) : codec_(0), codecCtx_(0), swrCtx_(0), ctxPtr(ctxPtr), audioType(audioType), outSamples(0), wanted_resample_freq(44100){ #ifdef USE_FFMPEG avcodec_register_all(); av_register_all(); InitFFmpeg(); frame_ = av_frame_alloc(); // Get Audio Codec ctx if (!GetAudioCodecID(audioType)){ ERROR_LOG(ME, "This version of FFMPEG does not support Audio codec type: %08x. Update your submodule.", audioType); return; } // Find decoder codec_ = avcodec_find_decoder(audioCodecId); if (!codec_) { // Eh, we shouldn't even have managed to compile. But meh. ERROR_LOG(ME, "This version of FFMPEG does not support AV_CODEC_ctx for audio (%s). Update your submodule.", GetCodecName(audioType)); return; } // Allocate codec context codecCtx_ = avcodec_alloc_context3(codec_); if (!codecCtx_) { ERROR_LOG(ME, "Failed to allocate a codec context"); return; } codecCtx_->channels = 2; codecCtx_->channel_layout = AV_CH_LAYOUT_STEREO; codecCtx_->sample_rate = 44100; // Open codec AVDictionary *opts = 0; if (avcodec_open2(codecCtx_, codec_, &opts) < 0) { ERROR_LOG(ME, "Failed to open codec"); av_dict_free(&opts); return; } av_dict_free(&opts); #endif // USE_FFMPEG }
bool SimpleAudio::ResetCodecCtx(int channels, int samplerate) { #ifdef USE_FFMPEG if (codecCtx_) avcodec_close(codecCtx_); // Find decoder int audioCodecId = GetAudioCodecID(audioType); codec_ = avcodec_find_decoder((AVCodecID)audioCodecId); if (!codec_) { // Eh, we shouldn't even have managed to compile. But meh. ERROR_LOG(ME, "This version of FFMPEG does not support AV_CODEC_ctx for audio (%s). Update your submodule.", GetCodecName(audioType)); return false; } codecCtx_->channels = channels; codecCtx_->channel_layout = channels==2?AV_CH_LAYOUT_STEREO:AV_CH_LAYOUT_MONO; codecCtx_->sample_rate = samplerate; codecOpen_ = false; return true; #endif return false; }