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(int stream_index, const spl::shared_ptr<AVFormatContext>& context, int out_samplerate)
85 : index_(stream_index)
86 , codec_context_(open_codec(*context, AVMEDIA_TYPE_AUDIO, index_, false))
87 , out_samplerate_(out_samplerate)
88 , buffer_(10 * out_samplerate_ * codec_context_->channels) // 10 seconds of audio
91 CASPAR_THROW_EXCEPTION(bad_alloc());
93 THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]");
95 codec_context_->refcounted_frames = 1;
98 void push(const std::shared_ptr<AVPacket>& packet)
103 if(packet->stream_index == index_ || packet->data == nullptr)
104 packets_.push(spl::make_shared_ptr(packet));
107 std::shared_ptr<core::mutable_audio_buffer> poll()
112 auto packet = packets_.front();
114 if(packet->data == nullptr)
117 avcodec_flush_buffers(codec_context_.get());
118 return flush_audio();
121 auto audio = decode(*packet);
123 if(packet->size == 0)
129 std::shared_ptr<core::mutable_audio_buffer> decode(AVPacket& pkt)
131 auto decoded_frame = create_frame();
134 auto len = THROW_ON_ERROR2(avcodec_decode_audio4(codec_context_.get(), decoded_frame.get(), &got_frame, &pkt), "[audio_decoder]");
148 const uint8_t **in = const_cast<const uint8_t**>(decoded_frame->extended_data);
149 uint8_t* out[] = { reinterpret_cast<uint8_t*>(buffer_.data()) };
151 const auto channel_samples = swr_convert(
154 static_cast<int>(buffer_.size()) / codec_context_->channels,
156 decoded_frame->nb_samples);
158 return std::make_shared<core::mutable_audio_buffer>(
160 buffer_.begin() + channel_samples * decoded_frame->channels);
165 return packets_.size() > 10;
168 std::wstring print() const
170 return L"[audio-decoder] " + u16(codec_context_->codec->long_name);
173 uint64_t ffmpeg_channel_layout() const
175 if (codec_context_->channel_layout == 0)
176 return av_get_default_channel_layout(codec_context_->channels);
178 return codec_context_->channel_layout;
182 audio_decoder::audio_decoder(int stream_index, const spl::shared_ptr<AVFormatContext>& context, int out_samplerate) : impl_(new implementation(stream_index, context, out_samplerate)){}
183 void audio_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}
184 bool audio_decoder::ready() const{return impl_->ready();}
185 std::shared_ptr<core::mutable_audio_buffer> audio_decoder::poll() { return impl_->poll(); }
186 int audio_decoder::num_channels() const { return impl_->codec_context_->channels; }
187 uint64_t audio_decoder::ffmpeg_channel_layout() const { return impl_->ffmpeg_channel_layout(); }
188 std::wstring audio_decoder::print() const{return impl_->print();}