]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/audio/audio_decoder.cpp
2.0. audio_decoder: Fixed audio regression bug.
[casparcg] / modules / ffmpeg / producer / audio / audio_decoder.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\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
10 *\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
15 \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
18 *\r
19 */\r
20 #include "../../stdafx.h"\r
21 \r
22 #include "audio_decoder.h"\r
23 \r
24 #if defined(_MSC_VER)\r
25 #pragma warning (push)\r
26 #pragma warning (disable : 4244)\r
27 #endif\r
28 extern "C" \r
29 {\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
34 }\r
35 #if defined(_MSC_VER)\r
36 #pragma warning (pop)\r
37 #endif\r
38 \r
39 namespace caspar {\r
40 \r
41 struct audio_decoder::implementation : boost::noncopyable\r
42 {       \r
43         input&                                                  input_;\r
44         AVCodecContext&                                 codec_context_;         \r
45         const core::video_format_desc   format_desc_;\r
46 \r
47         std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>                     current_chunk_; \r
48 \r
49         size_t                                                  frame_number_;\r
50         bool                                                    wait_for_eof_;\r
51 \r
52         std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>> buffer_;\r
53 public:\r
54         explicit implementation(input& input, const core::video_format_desc& format_desc) \r
55                 : input_(input)\r
56                 , codec_context_(*input_.get_audio_codec_context())\r
57                 , format_desc_(format_desc)     \r
58                 , frame_number_(0)\r
59                 , wait_for_eof_(false)\r
60                 , buffer_(4*format_desc_.audio_sample_rate*2+FF_INPUT_BUFFER_PADDING_SIZE/2, 0)\r
61         {\r
62                 if(codec_context_.sample_rate != static_cast<int>(format_desc_.audio_sample_rate) || \r
63                    codec_context_.channels != static_cast<int>(format_desc_.audio_channels))\r
64                 {       \r
65                         BOOST_THROW_EXCEPTION(\r
66                                 file_read_error()  <<\r
67                                 msg_info("Invalid sample-rate or number of channels.") <<\r
68                                 arg_value_info(boost::lexical_cast<std::string>(codec_context_.sample_rate)) << \r
69                                 arg_name_info("codec_context"));\r
70                 }\r
71         }\r
72                 \r
73         std::deque<std::pair<int, std::vector<int16_t>>> receive()\r
74         {\r
75                 std::deque<std::pair<int, std::vector<int16_t>>> result;\r
76                 \r
77                 std::shared_ptr<AVPacket> pkt;\r
78                 for(int n = 0; n < 32 && result.empty() && input_.try_pop_audio_packet(pkt); ++n)       \r
79                         result = decode(pkt);\r
80 \r
81                 return result;\r
82         }\r
83 \r
84         std::deque<std::pair<int, std::vector<int16_t>>> decode(const std::shared_ptr<AVPacket>& audio_packet)\r
85         {                       \r
86                 std::deque<std::pair<int, std::vector<int16_t>>> result;\r
87 \r
88                 if(!audio_packet) // eof\r
89                 {       \r
90                         avcodec_flush_buffers(&codec_context_);\r
91                         current_chunk_.clear();\r
92                         frame_number_ = 0;\r
93                         wait_for_eof_ = false;\r
94                         return result;\r
95                 }\r
96 \r
97                 if(wait_for_eof_)\r
98                         return result;\r
99                                                 \r
100                 int written_bytes = buffer_.size()-FF_INPUT_BUFFER_PADDING_SIZE/2;\r
101                 const int errn = avcodec_decode_audio3(&codec_context_, buffer_.data(), &written_bytes, audio_packet.get());\r
102                 if(errn < 0)\r
103                 {       \r
104                         BOOST_THROW_EXCEPTION(\r
105                                 invalid_operation() <<\r
106                                 boost::errinfo_api_function("avcodec_decode_audio2") <<\r
107                                 boost::errinfo_errno(AVUNERROR(errn)));\r
108                 }\r
109 \r
110                 current_chunk_.insert(current_chunk_.end(), buffer_.begin(), buffer_.begin() + written_bytes/2);\r
111 \r
112                 const auto last = current_chunk_.end() - current_chunk_.size() % format_desc_.audio_samples_per_frame;\r
113                 \r
114                 for(auto it = current_chunk_.begin(); it != last; it += format_desc_.audio_samples_per_frame)           \r
115                         result.push_back(std::make_pair(frame_number_++, std::vector<int16_t>(it, it + format_desc_.audio_samples_per_frame)));         \r
116 \r
117                 current_chunk_.erase(current_chunk_.begin(), last);\r
118 \r
119                 return result;\r
120         }\r
121 \r
122         void restart()\r
123         {\r
124                 wait_for_eof_ = true;\r
125         }\r
126 };\r
127 \r
128 audio_decoder::audio_decoder(input& input, const core::video_format_desc& format_desc) : impl_(new implementation(input, format_desc)){}\r
129 std::deque<std::pair<int, std::vector<int16_t>>> audio_decoder::receive(){return impl_->receive();}\r
130 void audio_decoder::restart(){impl_->restart();}\r
131 }