X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fffmpeg%2Fproducer%2Faudio%2Faudio_decoder.cpp;h=fe69a59cf6aa459f8667ebaf2bdf0864d99bb00e;hb=61974506572c47b0650c4b5bfa65cd18621d5fec;hp=a8bb817b8363618e6474753ebbf3a4215cbd9746;hpb=58439eaecd6ed455b187e9965cf333073ead46de;p=casparcg diff --git a/modules/ffmpeg/producer/audio/audio_decoder.cpp b/modules/ffmpeg/producer/audio/audio_decoder.cpp index a8bb817b8..fe69a59cf 100644 --- a/modules/ffmpeg/producer/audio/audio_decoder.cpp +++ b/modules/ffmpeg/producer/audio/audio_decoder.cpp @@ -1,172 +1,181 @@ -/* -* copyright (c) 2010 Sveriges Television AB -* -* This file is part of CasparCG. -* -* CasparCG is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* CasparCG is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. - -* You should have received a copy of the GNU General Public License -* along with CasparCG. If not, see . -* -*/ -#include "../../stdafx.h" - -#include "audio_decoder.h" -#include "audio_resampler.h" - -#include "../util.h" -#include "../../ffmpeg_error.h" - -#include - -#include - -#if defined(_MSC_VER) -#pragma warning (push) -#pragma warning (disable : 4244) -#endif -extern "C" -{ - #include - #include -} -#if defined(_MSC_VER) -#pragma warning (pop) -#endif - -namespace caspar { namespace ffmpeg { - -struct audio_decoder::implementation : public Concurrency::agent, boost::noncopyable -{ - audio_decoder::token_t& active_token_; - audio_decoder::source_t& source_; - audio_decoder::target_t& target_; - - std::shared_ptr codec_context_; - const core::video_format_desc format_desc_; - int index_; - std::unique_ptr resampler_; - - std::vector> buffer1_; - - int64_t nb_frames_; -public: - explicit implementation(audio_decoder::token_t& active_token, - audio_decoder::source_t& source, - audio_decoder::target_t& target, - const safe_ptr& context, - const core::video_format_desc& format_desc) - : active_token_(active_token) - , source_(source) - , target_(target) - , format_desc_(format_desc) - , nb_frames_(0) - { - try - { - AVCodec* dec; - index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0), "[audio_decoder]"); - - THROW_ON_ERROR2(avcodec_open(context->streams[index_]->codec, dec), "[audio_decoder]"); - - codec_context_.reset(context->streams[index_]->codec, avcodec_close); - - buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2); - - resampler_.reset(new audio_resampler(format_desc_.audio_channels, codec_context_->channels, - format_desc_.audio_sample_rate, codec_context_->sample_rate, - AV_SAMPLE_FMT_S32, codec_context_->sample_fmt)); - } - catch(...) - { - index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0), "[audio_decoder]"); - - CASPAR_LOG_CURRENT_EXCEPTION(); - CASPAR_LOG(warning) << "[audio_decoder] Failed to open audio-stream. Running without audio."; - } - - start(); - } - - ~implementation() - { - agent::wait(this); - } - - virtual void run() - { - try - { - while(Concurrency::receive(active_token_)) - { - auto packet = Concurrency::receive(source_); - if(packet == eof_packet()) - { - Concurrency::send(target_, eof_audio()); - break; - } - - if(packet == loop_packet()) - { - if(codec_context_) - avcodec_flush_buffers(codec_context_.get()); - Concurrency::send(target_, loop_audio()); - } - else if(!codec_context_) - Concurrency::send(target_, empty_audio()); - else - Concurrency::send(target_, decode(*packet)); - } - } - catch(...) - { - CASPAR_LOG_CURRENT_EXCEPTION(); - } - - std::shared_ptr packet; - Concurrency::try_receive(source_, packet); - - done(); - } - - std::shared_ptr decode(AVPacket& pkt) - { - buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2); - int written_bytes = buffer1_.size() - FF_INPUT_BUFFER_PADDING_SIZE; - - int ret = THROW_ON_ERROR2(avcodec_decode_audio3(codec_context_.get(), reinterpret_cast(buffer1_.data()), &written_bytes, &pkt), "[audio_decoder]"); - - // There might be several frames in one packet. - pkt.size -= ret; - pkt.data += ret; - - buffer1_.resize(written_bytes); - - buffer1_ = resampler_->resample(std::move(buffer1_)); - - const auto n_samples = buffer1_.size() / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32); - const auto samples = reinterpret_cast(buffer1_.data()); - - return std::make_shared(samples, samples + n_samples); - } -}; - -audio_decoder::audio_decoder(token_t& active_token, - source_t& source, - target_t& target, - const safe_ptr& context, - const core::video_format_desc& format_desc) - : impl_(new implementation(active_token, source, target, context, format_desc)) -{ -} -int64_t audio_decoder::nb_frames() const{return impl_->nb_frames_;} - -}} \ No newline at end of file +/* +* Copyright 2013 Sveriges Television AB http://casparcg.com/ +* +* This file is part of CasparCG (www.casparcg.com). +* +* CasparCG is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* CasparCG is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with CasparCG. If not, see . +* +* Author: Robert Nagy, ronag89@gmail.com +*/ + +#include "../../StdAfx.h" + +#include "audio_decoder.h" + +#include "../util/util.h" +#include "../../ffmpeg_error.h" + +#include +#include + +#include + +#include + +#if defined(_MSC_VER) +#pragma warning (push) +#pragma warning (disable : 4244) +#endif +extern "C" +{ + #include + #include + #include +} +#if defined(_MSC_VER) +#pragma warning (pop) +#endif + +namespace caspar { namespace ffmpeg { + +struct audio_decoder::implementation : boost::noncopyable +{ + int index_ = -1; + const spl::shared_ptr codec_context_; + const int out_samplerate_; + + cache_aligned_vector buffer_; + + std::queue> packets_; + + std::shared_ptr swr_ { + swr_alloc_set_opts( + nullptr, + codec_context_->channel_layout + ? codec_context_->channel_layout + : av_get_default_channel_layout(codec_context_->channels), + AV_SAMPLE_FMT_S32, + out_samplerate_, + codec_context_->channel_layout + ? codec_context_->channel_layout + : av_get_default_channel_layout(codec_context_->channels), + codec_context_->sample_fmt, + codec_context_->sample_rate, + 0, + nullptr), + [](SwrContext* p) + { + swr_free(&p); + } + }; + +public: + explicit implementation(const spl::shared_ptr& context, int out_samplerate) + : codec_context_(open_codec(*context, AVMEDIA_TYPE_AUDIO, index_, false)) + , out_samplerate_(out_samplerate) + , buffer_(10 * out_samplerate_ * codec_context_->channels) // 10 seconds of audio + { + if(!swr_) + BOOST_THROW_EXCEPTION(bad_alloc()); + + THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]"); + + codec_context_->refcounted_frames = 1; + } + + void push(const std::shared_ptr& packet) + { + if(!packet) + return; + + if(packet->stream_index == index_ || packet->data == nullptr) + packets_.push(spl::make_shared_ptr(packet)); + } + + std::shared_ptr poll() + { + if(packets_.empty()) + return nullptr; + + auto packet = packets_.front(); + + if(packet->data == nullptr) + { + packets_.pop(); + avcodec_flush_buffers(codec_context_.get()); + return flush_audio(); + } + + auto audio = decode(*packet); + + if(packet->size == 0) + packets_.pop(); + + return audio; + } + + std::shared_ptr decode(AVPacket& pkt) + { + auto decoded_frame = create_frame(); + + int got_frame = 0; + auto len = THROW_ON_ERROR2(avcodec_decode_audio4(codec_context_.get(), decoded_frame.get(), &got_frame, &pkt), "[audio_decoder]"); + + if (len == 0) + { + pkt.size = 0; + return nullptr; + } + + pkt.data += len; + pkt.size -= len; + + if (!got_frame) + return nullptr; + + const uint8_t **in = const_cast(decoded_frame->extended_data); + uint8_t* out[] = { reinterpret_cast(buffer_.data()) }; + + const auto channel_samples = swr_convert( + swr_.get(), + out, + static_cast(buffer_.size()) / codec_context_->channels, + in, + decoded_frame->nb_samples); + + return std::make_shared( + buffer_.begin(), + buffer_.begin() + channel_samples * decoded_frame->channels); + } + + bool ready() const + { + return packets_.size() > 10; + } + + std::wstring print() const + { + return L"[audio-decoder] " + u16(codec_context_->codec->long_name); + } +}; + +audio_decoder::audio_decoder(const spl::shared_ptr& context, int out_samplerate) : impl_(new implementation(context, out_samplerate)){} +void audio_decoder::push(const std::shared_ptr& packet){impl_->push(packet);} +bool audio_decoder::ready() const{return impl_->ready();} +std::shared_ptr audio_decoder::poll() { return impl_->poll(); } +int audio_decoder::num_channels() const { return impl_->codec_context_->channels; } +uint64_t audio_decoder::ffmpeg_channel_layout() const { return impl_->codec_context_->channel_layout; } +std::wstring audio_decoder::print() const{return impl_->print();} + +}}