2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../stdafx.h"
24 #include "audio_decoder.h"
26 #include "../util/util.h"
27 #include "../../ffmpeg_error.h"
29 #include <core/video_format.h>
30 #include <core/mixer/audio/audio_util.h>
32 #include <common/cache_aligned_vector.h>
37 #pragma warning (push)
38 #pragma warning (disable : 4244)
42 #include <libavformat/avformat.h>
43 #include <libavcodec/avcodec.h>
44 #include <libswresample/swresample.h>
50 namespace caspar { namespace ffmpeg {
52 struct audio_decoder::implementation : boost::noncopyable
55 const spl::shared_ptr<AVCodecContext> codec_context_;
56 const int out_samplerate_;
58 cache_aligned_vector<int32_t> buffer_;
60 std::queue<spl::shared_ptr<AVPacket>> packets_;
62 std::shared_ptr<SwrContext> swr_ {
65 codec_context_->channel_layout
66 ? codec_context_->channel_layout
67 : av_get_default_channel_layout(codec_context_->channels),
70 codec_context_->channel_layout
71 ? codec_context_->channel_layout
72 : av_get_default_channel_layout(codec_context_->channels),
73 codec_context_->sample_fmt,
74 codec_context_->sample_rate,
84 explicit implementation(const spl::shared_ptr<AVFormatContext>& context, int out_samplerate)
85 : codec_context_(open_codec(*context, AVMEDIA_TYPE_AUDIO, index_, false))
86 , out_samplerate_(out_samplerate)
87 , buffer_(10 * out_samplerate_ * codec_context_->channels) // 10 seconds of audio
90 CASPAR_THROW_EXCEPTION(bad_alloc());
92 THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]");
94 codec_context_->refcounted_frames = 1;
97 void push(const std::shared_ptr<AVPacket>& packet)
102 if(packet->stream_index == index_ || packet->data == nullptr)
103 packets_.push(spl::make_shared_ptr(packet));
106 std::shared_ptr<core::mutable_audio_buffer> poll()
111 auto packet = packets_.front();
113 if(packet->data == nullptr)
116 avcodec_flush_buffers(codec_context_.get());
117 return flush_audio();
120 auto audio = decode(*packet);
122 if(packet->size == 0)
128 std::shared_ptr<core::mutable_audio_buffer> decode(AVPacket& pkt)
130 auto decoded_frame = create_frame();
133 auto len = THROW_ON_ERROR2(avcodec_decode_audio4(codec_context_.get(), decoded_frame.get(), &got_frame, &pkt), "[audio_decoder]");
147 const uint8_t **in = const_cast<const uint8_t**>(decoded_frame->extended_data);
148 uint8_t* out[] = { reinterpret_cast<uint8_t*>(buffer_.data()) };
150 const auto channel_samples = swr_convert(
153 static_cast<int>(buffer_.size()) / codec_context_->channels,
155 decoded_frame->nb_samples);
157 return std::make_shared<core::mutable_audio_buffer>(
159 buffer_.begin() + channel_samples * decoded_frame->channels);
164 return packets_.size() > 10;
167 std::wstring print() const
169 return L"[audio-decoder] " + u16(codec_context_->codec->long_name);
173 audio_decoder::audio_decoder(const spl::shared_ptr<AVFormatContext>& context, int out_samplerate) : impl_(new implementation(context, out_samplerate)){}
174 void audio_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}
175 bool audio_decoder::ready() const{return impl_->ready();}
176 std::shared_ptr<core::mutable_audio_buffer> audio_decoder::poll() { return impl_->poll(); }
177 int audio_decoder::num_channels() const { return impl_->codec_context_->channels; }
178 uint64_t audio_decoder::ffmpeg_channel_layout() const { return impl_->codec_context_->channel_layout; }
179 std::wstring audio_decoder::print() const{return impl_->print();}