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
43 typedef std::vector<short, tbb::cache_aligned_allocator<short>> buffer;
\r
45 AVCodecContext* codec_context_;
\r
47 buffer audio_buffer_;
\r
48 buffer current_chunk_;
\r
50 const size_t audio_frame_size_;
\r
52 static const size_t SAMPLE_RATE = 48000;
\r
53 static const size_t N_CHANNELS = 2;
\r
56 explicit implementation(AVCodecContext* codec_context, double fps)
\r
57 : codec_context_(codec_context)
\r
58 , audio_buffer_(4*SAMPLE_RATE*2+FF_INPUT_BUFFER_PADDING_SIZE/2)
\r
59 , audio_frame_size_(static_cast<size_t>(static_cast<double>(SAMPLE_RATE) / fps) * N_CHANNELS)
\r
62 BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("codec_context"));
\r
65 std::vector<std::vector<short>> execute(const std::shared_ptr<aligned_buffer>& audio_packet)
\r
68 return std::vector<std::vector<short>>();
\r
70 if(audio_packet->empty()) // Need to flush
\r
72 avcodec_flush_buffers(codec_context_);
\r
73 return std::vector<std::vector<short>>();
\r
76 int written_bytes = audio_buffer_.size()*2 - FF_INPUT_BUFFER_PADDING_SIZE;
\r
77 const int errn = avcodec_decode_audio2(codec_context_, audio_buffer_.data(), &written_bytes, audio_packet->data(), audio_packet->size());
\r
79 if(errn < 0 || codec_context_->sample_rate != SAMPLE_RATE || codec_context_->channels != 2)
\r
81 BOOST_THROW_EXCEPTION(
\r
82 invalid_operation() <<
\r
83 boost::errinfo_api_function("avcodec_decode_audio2") <<
\r
84 boost::errinfo_errno(AVUNERROR(errn)));
\r
87 current_chunk_.insert(current_chunk_.end(), audio_buffer_.data(), audio_buffer_.data() + written_bytes/2);
\r
89 std::vector<std::vector<short>> chunks;
\r
91 const auto last = current_chunk_.end() - current_chunk_.size() % audio_frame_size_;
\r
93 for(auto it = current_chunk_.begin(); it != last; it += audio_frame_size_)
\r
94 chunks.push_back(std::vector<short>(it, it + audio_frame_size_));
\r
96 current_chunk_.erase(current_chunk_.begin(), last);
\r
102 audio_decoder::audio_decoder(AVCodecContext* codec_context, double fps) : impl_(new implementation(codec_context, fps)){}
\r
103 std::vector<std::vector<short>> audio_decoder::execute(const std::shared_ptr<aligned_buffer>& audio_packet){return impl_->execute(audio_packet);}
\r