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 <common/concurrency/message.h>
\r
29 #include <core/video_format.h>
\r
31 #include <tbb/cache_aligned_allocator.h>
\r
33 #if defined(_MSC_VER)
\r
34 #pragma warning (push)
\r
35 #pragma warning (disable : 4244)
\r
39 #include <libavformat/avformat.h>
\r
40 #include <libavcodec/avcodec.h>
\r
42 #if defined(_MSC_VER)
\r
43 #pragma warning (pop)
\r
47 #include <semaphore.h>
\r
49 using namespace Concurrency;
\r
51 namespace caspar { namespace ffmpeg {
\r
53 struct audio_decoder::implementation : boost::noncopyable
\r
56 std::shared_ptr<AVCodecContext> codec_context_;
\r
58 audio_resampler resampler_;
\r
60 std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>> buffer1_;
\r
62 safe_ptr<semaphore> semaphore_;
\r
64 transformer<safe_ptr<AVPacket>, std::shared_ptr<core::audio_buffer>> transformer_;
\r
66 explicit implementation(audio_decoder::source_t& source, audio_decoder::target_t& target, AVFormatContext& context, const core::video_format_desc& format_desc)
\r
67 : codec_context_(open_codec(context, AVMEDIA_TYPE_AUDIO, index_))
\r
68 , resampler_(format_desc.audio_channels, codec_context_->channels,
\r
69 format_desc.audio_sample_rate, codec_context_->sample_rate,
\r
70 AV_SAMPLE_FMT_S32, codec_context_->sample_fmt)
\r
71 , buffer1_(AVCODEC_MAX_AUDIO_FRAME_SIZE*2)
\r
72 , semaphore_(make_safe<semaphore>(32))
\r
73 , transformer_([this](const safe_ptr<AVPacket>& packet){return (*this)(packet);}, &target,
\r
74 [this](const safe_ptr<AVPacket>& packet){return packet->stream_index == index_;})
\r
76 source.link_target(&transformer_);
\r
77 CASPAR_LOG(debug) << "[audio_decoder] " << context.streams[index_]->codec->codec->long_name;
\r
82 semaphore_->release();
\r
85 std::shared_ptr<core::audio_buffer> operator()(const safe_ptr<AVPacket>& packet)
\r
89 auto tok = make_safe<token>(semaphore_);
\r
91 if(packet == loop_packet(index_))
\r
93 avcodec_flush_buffers(codec_context_.get());
\r
94 return loop_audio();
\r
97 if(packet == eof_packet(index_))
\r
98 return eof_audio();
\r
100 auto result = safe_ptr<core::audio_buffer>(new core::audio_buffer(), [this, tok](core::audio_buffer* p)
\r
105 while(packet->size > 0)
\r
107 buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);
\r
108 int written_bytes = buffer1_.size() - FF_INPUT_BUFFER_PADDING_SIZE;
\r
110 int ret = THROW_ON_ERROR2(avcodec_decode_audio3(codec_context_.get(), reinterpret_cast<int16_t*>(buffer1_.data()), &written_bytes, packet.get()), "[audio_decoder]");
\r
112 // There might be several frames in one packet.
\r
113 packet->size -= ret;
\r
114 packet->data += ret;
\r
116 buffer1_.resize(written_bytes);
\r
118 buffer1_ = resampler_.resample(std::move(buffer1_));
\r
120 const auto n_samples = buffer1_.size() / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32);
\r
121 const auto samples = reinterpret_cast<int32_t*>(buffer1_.data());
\r
123 result->insert(result->end(), samples, samples + n_samples);
\r
129 CASPAR_LOG_CURRENT_EXCEPTION();
\r
130 return eof_audio();
\r
135 audio_decoder::audio_decoder(source_t& source, target_t& target, AVFormatContext& context, const core::video_format_desc& format_desc)
\r
136 : impl_(new implementation(source, target, context, format_desc))
\r
140 int64_t audio_decoder::nb_frames() const
\r