2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
20 #include "../../stdafx.h"
\r
22 #include "audio_decoder.h"
\r
24 #include <tbb/task_group.h>
\r
26 #if defined(_MSC_VER)
\r
27 #pragma warning (push)
\r
28 #pragma warning (disable : 4244)
\r
32 #define __STDC_CONSTANT_MACROS
\r
33 #define __STDC_LIMIT_MACROS
\r
34 #include <libavformat/avformat.h>
\r
35 #include <libavcodec/avcodec.h>
\r
37 #if defined(_MSC_VER)
\r
38 #pragma warning (pop)
\r
43 struct audio_decoder::implementation : boost::noncopyable
\r
45 std::shared_ptr<AVCodecContext> codec_context_;
\r
46 const core::video_format_desc format_desc_;
\r
48 std::shared_ptr<ReSampleContext> resampler_;
\r
50 std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>> buffer1_;
\r
51 std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>> buffer2_;
\r
52 std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>> audio_samples_;
\r
53 std::queue<std::shared_ptr<AVPacket>> packets_;
\r
57 explicit implementation(const std::shared_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc)
\r
58 : format_desc_(format_desc)
\r
62 index_ = av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);
\r
67 int errn = avcodec_open(context->streams[index_]->codec, dec);
\r
71 codec_context_.reset(context->streams[index_]->codec, avcodec_close);
\r
73 //nb_frames_ = context->streams[index_]->nb_frames;
\r
74 //if(nb_frames_ == 0)
\r
75 // nb_frames_ = context->streams[index_]->duration * context->streams[index_]->time_base.den;
\r
77 if(codec_context_ &&
\r
78 (codec_context_->sample_rate != static_cast<int>(format_desc_.audio_sample_rate) ||
\r
79 codec_context_->channels != static_cast<int>(format_desc_.audio_channels)) ||
\r
80 codec_context_->sample_fmt != AV_SAMPLE_FMT_S16)
\r
82 auto resampler = av_audio_resample_init(format_desc_.audio_channels, codec_context_->channels,
\r
83 format_desc_.audio_sample_rate, codec_context_->sample_rate,
\r
84 AV_SAMPLE_FMT_S16, codec_context_->sample_fmt,
\r
87 CASPAR_LOG(warning) << L" Invalid audio format. Resampling.";
\r
90 resampler_.reset(resampler, audio_resample_close);
\r
92 codec_context_ = nullptr;
\r
96 void push(const std::shared_ptr<AVPacket>& packet)
\r
101 if(packet && packet->stream_index != index_)
\r
104 packets_.push(packet);
\r
107 std::vector<std::shared_ptr<std::vector<int16_t>>> poll()
\r
109 std::vector<std::shared_ptr<std::vector<int16_t>>> result;
\r
111 if(!codec_context_)
\r
112 result.push_back(std::make_shared<std::vector<int16_t>>(format_desc_.audio_samples_per_frame, 0));
\r
113 else if(!packets_.empty())
\r
115 auto packet = std::move(packets_.front());
\r
121 av_init_packet(&pkt);
\r
122 pkt.data = packet->data;
\r
123 pkt.size = packet->size;
\r
125 for(int n = 0; n < 64 && pkt.size > 0; ++n)
\r
126 result.push_back(decode(pkt));
\r
130 avcodec_flush_buffers(codec_context_.get());
\r
131 result.push_back(nullptr);
\r
138 std::shared_ptr<std::vector<int16_t>> decode(AVPacket& pkt)
\r
140 buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2, 0);
\r
141 int written_bytes = buffer1_.size() - FF_INPUT_BUFFER_PADDING_SIZE;
\r
143 const int ret = avcodec_decode_audio3(codec_context_.get(), reinterpret_cast<int16_t*>(buffer1_.data()), &written_bytes, &pkt);
\r
146 BOOST_THROW_EXCEPTION(
\r
147 invalid_operation() <<
\r
148 boost::errinfo_api_function("avcodec_decode_audio2") <<
\r
149 boost::errinfo_errno(AVUNERROR(ret)));
\r
152 // There might be several frames in one packet.
\r
156 buffer1_.resize(written_bytes);
\r
160 buffer2_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2, 0);
\r
161 auto ret = audio_resample(resampler_.get(),
\r
162 reinterpret_cast<short*>(buffer2_.data()),
\r
163 reinterpret_cast<short*>(buffer1_.data()),
\r
164 buffer1_.size() / (av_get_bytes_per_sample(codec_context_->sample_fmt) * codec_context_->channels));
\r
165 buffer2_.resize(ret * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * format_desc_.audio_channels);
\r
166 std::swap(buffer1_, buffer2_);
\r
169 const auto n_samples = buffer1_.size() / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);
\r
170 const auto samples = reinterpret_cast<int16_t*>(buffer1_.data());
\r
172 return std::make_shared<std::vector<int16_t>>(samples, samples + n_samples);
\r
177 return !codec_context_ || !packets_.empty();
\r
181 audio_decoder::audio_decoder(const std::shared_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc) : impl_(new implementation(context, format_desc)){}
\r
182 void audio_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}
\r
183 bool audio_decoder::ready() const{return impl_->ready();}
\r
184 std::vector<std::shared_ptr<std::vector<int16_t>>> audio_decoder::poll(){return impl_->poll();}
\r
185 int64_t audio_decoder::nb_frames() const{return impl_->nb_frames_;}
\r