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
23 #include "audio_resampler.h"
\r
25 #include "../util.h"
\r
26 #include "../../ffmpeg_error.h"
\r
28 #include <core/video_format.h>
\r
30 #include <tbb/cache_aligned_allocator.h>
\r
32 #if defined(_MSC_VER)
\r
33 #pragma warning (push)
\r
34 #pragma warning (disable : 4244)
\r
38 #include <libavformat/avformat.h>
\r
39 #include <libavcodec/avcodec.h>
\r
41 #if defined(_MSC_VER)
\r
42 #pragma warning (pop)
\r
45 namespace caspar { namespace ffmpeg {
\r
47 struct audio_decoder::implementation : public Concurrency::agent, boost::noncopyable
\r
49 audio_decoder::source_t& source_;
\r
50 audio_decoder::target_t& target_;
\r
52 std::shared_ptr<AVCodecContext> codec_context_;
\r
53 const core::video_format_desc format_desc_;
\r
55 std::unique_ptr<audio_resampler> resampler_;
\r
57 std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>> buffer1_;
\r
62 explicit implementation(audio_decoder::source_t& source,
\r
63 audio_decoder::target_t& target,
\r
64 const safe_ptr<AVFormatContext>& context,
\r
65 const core::video_format_desc& format_desc)
\r
68 , format_desc_(format_desc)
\r
73 index_ = THROW_ON_ERROR3(av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0), "[audio_decoder]", ffmpeg_stream_not_found);
\r
74 CASPAR_VERIFY(index_ > -1, ffmpeg_error());
\r
76 THROW_ON_ERROR2(avcodec_open(context->streams[index_]->codec, dec), "[audio_decoder]");
\r
77 CASPAR_VERIFY(context->streams[index_]->codec, ffmpeg_error());
\r
79 codec_context_.reset(context->streams[index_]->codec, avcodec_close);
\r
81 buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);
\r
83 resampler_.reset(new audio_resampler(format_desc_.audio_channels, codec_context_->channels,
\r
84 format_desc_.audio_sample_rate, codec_context_->sample_rate,
\r
85 AV_SAMPLE_FMT_S32, codec_context_->sample_fmt));
\r
86 CASPAR_VERIFY(resampler_, ffmpeg_error());
\r
102 auto packet = Concurrency::receive(source_, [this](const std::shared_ptr<AVPacket>& packet)
\r
104 return packet && packet->stream_index == index_;
\r
107 if(packet == eof_packet(index_))
\r
110 if(packet == loop_packet(index_))
\r
112 avcodec_flush_buffers(codec_context_.get());
\r
113 Concurrency::send(target_, loop_audio());
\r
115 else if(packet->stream_index == index_)
\r
117 Concurrency::send(target_, decode(*packet));
\r
118 Concurrency::wait(0);
\r
124 CASPAR_LOG_CURRENT_EXCEPTION();
\r
127 Concurrency::send(target_, eof_audio());
\r
132 std::shared_ptr<core::audio_buffer> decode(AVPacket& pkt)
\r
134 buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);
\r
135 int written_bytes = buffer1_.size() - FF_INPUT_BUFFER_PADDING_SIZE;
\r
137 int ret = THROW_ON_ERROR2(avcodec_decode_audio3(codec_context_.get(), reinterpret_cast<int16_t*>(buffer1_.data()), &written_bytes, &pkt), "[audio_decoder]");
\r
139 // There might be several frames in one packet.
\r
143 buffer1_.resize(written_bytes);
\r
145 buffer1_ = resampler_->resample(std::move(buffer1_));
\r
147 const auto n_samples = buffer1_.size() / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32);
\r
148 const auto samples = reinterpret_cast<int32_t*>(buffer1_.data());
\r
150 return n_samples > 0 ? std::make_shared<core::audio_buffer>(samples, samples + n_samples) : nullptr;
\r
154 audio_decoder::audio_decoder(source_t& source,
\r
156 const safe_ptr<AVFormatContext>& context,
\r
157 const core::video_format_desc& format_desc)
\r
158 : impl_(new implementation(source, target, context, format_desc))
\r
161 int64_t audio_decoder::nb_frames() const{return impl_->nb_frames_;}
\r