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 #if defined(_MSC_VER)
\r
25 #pragma warning (push)
\r
26 #pragma warning (disable : 4244)
\r
30 #define __STDC_CONSTANT_MACROS
\r
31 #define __STDC_LIMIT_MACROS
\r
32 #include <libavformat/avformat.h>
\r
33 #include <libavcodec/avcodec.h>
\r
35 #if defined(_MSC_VER)
\r
36 #pragma warning (pop)
\r
41 struct audio_decoder::implementation : boost::noncopyable
\r
44 AVCodecContext& codec_context_;
\r
45 const core::video_format_desc format_desc_;
\r
46 std::vector<short> current_chunk_;
\r
47 std::vector<std::vector<short>> chunks_;
\r
48 size_t frame_number_;
\r
51 explicit implementation(input& input, const core::video_format_desc& format_desc)
\r
53 , codec_context_(*input_.get_audio_codec_context())
\r
54 , format_desc_(format_desc)
\r
56 , restarting_(false)
\r
58 if(codec_context_.sample_rate != static_cast<int>(format_desc_.audio_sample_rate) ||
\r
59 codec_context_.channels != static_cast<int>(format_desc_.audio_channels))
\r
61 BOOST_THROW_EXCEPTION(
\r
62 file_read_error() <<
\r
63 msg_info("Invalid sample-rate or number of channels.") <<
\r
64 arg_value_info(boost::lexical_cast<std::string>(codec_context_.sample_rate)) <<
\r
65 arg_name_info("codec_context"));
\r
69 std::deque<std::pair<int, std::vector<short>>> receive()
\r
71 std::deque<std::pair<int, std::vector<short>>> result;
\r
73 std::shared_ptr<AVPacket> pkt;
\r
74 for(int n = 0; n < 32 && result.empty() && input_.try_pop_audio_packet(pkt); ++n)
\r
75 result = decode(pkt);
\r
80 std::deque<std::pair<int, std::vector<short>>> decode(const std::shared_ptr<AVPacket>& audio_packet)
\r
82 std::deque<std::pair<int, std::vector<short>>> result;
\r
86 avcodec_flush_buffers(&codec_context_);
\r
87 current_chunk_.clear();
\r
89 restarting_ = false;
\r
96 auto s = current_chunk_.size();
\r
97 current_chunk_.resize(s + 4*format_desc_.audio_sample_rate*2+FF_INPUT_BUFFER_PADDING_SIZE/2, 0);
\r
99 int written_bytes = (current_chunk_.size() - s)*2 - FF_INPUT_BUFFER_PADDING_SIZE;
\r
100 const int errn = avcodec_decode_audio3(&codec_context_, ¤t_chunk_[s], &written_bytes, audio_packet.get());
\r
103 BOOST_THROW_EXCEPTION(
\r
104 invalid_operation() <<
\r
105 boost::errinfo_api_function("avcodec_decode_audio2") <<
\r
106 boost::errinfo_errno(AVUNERROR(errn)));
\r
109 current_chunk_.resize(s + written_bytes/2);
\r
111 const auto last = current_chunk_.end() - current_chunk_.size() % format_desc_.audio_samples_per_frame;
\r
113 for(auto it = current_chunk_.begin(); it != last; it += format_desc_.audio_samples_per_frame)
\r
114 result.push_back(std::make_pair(frame_number_++, std::vector<short>(it, it + format_desc_.audio_samples_per_frame)));
\r
116 current_chunk_.erase(current_chunk_.begin(), last);
\r
123 restarting_ = true;
\r
127 audio_decoder::audio_decoder(input& input, const core::video_format_desc& format_desc) : impl_(new implementation(input, format_desc)){}
\r
128 std::deque<std::pair<int, std::vector<short>>> audio_decoder::receive(){return impl_->receive();}
\r
129 void audio_decoder::restart(){impl_->restart();}
\r