]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/audio/audio_decoder.cpp
2.0.0.2: Added some utility algorithms.
[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 #include <common/utility/algorithm.h>\r
25 \r
26 #if defined(_MSC_VER)\r
27 #pragma warning (push)\r
28 #pragma warning (disable : 4244)\r
29 #endif\r
30 extern "C" \r
31 {\r
32         #define __STDC_CONSTANT_MACROS\r
33         #define __STDC_LIMIT_MACROS\r
34         #include <libavformat/avformat.h>\r
35         #include <libavcodec/avcodec.h>\r
36 }\r
37 #if defined(_MSC_VER)\r
38 #pragma warning (pop)\r
39 #endif\r
40 \r
41 namespace caspar {\r
42 \r
43 struct audio_decoder::implementation : boost::noncopyable\r
44 {       \r
45         AVCodecContext* codec_context_;\r
46                 \r
47         const core::video_format_desc format_desc_;\r
48 \r
49         std::vector<short> current_chunk_;\r
50 \r
51 public:\r
52         explicit implementation(AVCodecContext* codec_context, const core::video_format_desc& format_desc) \r
53                 : codec_context_(codec_context)\r
54                 , format_desc_(format_desc)             \r
55         {\r
56                 if(!codec_context)\r
57                 {       \r
58                         BOOST_THROW_EXCEPTION(\r
59                                 null_argument() << \r
60                                 arg_name_info("codec_context"));        \r
61                 }\r
62 \r
63                 if(codec_context_->sample_rate != static_cast<int>(format_desc_.audio_sample_rate) || \r
64                    codec_context_->channels != static_cast<int>(format_desc_.audio_channels))\r
65                 {       \r
66                         BOOST_THROW_EXCEPTION(\r
67                                 file_read_error()  <<\r
68                                 msg_info("Invalid sample-rate or number of channels.") <<\r
69                                 arg_value_info(boost::lexical_cast<std::string>(codec_context_->sample_rate)) << \r
70                                 arg_name_info("codec_context"));\r
71                 }\r
72         }\r
73                 \r
74         std::vector<std::vector<short>> execute(const std::shared_ptr<aligned_buffer>& audio_packet)\r
75         {                               \r
76                 if(!audio_packet)\r
77                         return std::vector<std::vector<short>>();\r
78 \r
79                 if(audio_packet->empty()) // Need to flush\r
80                 {\r
81                         avcodec_flush_buffers(codec_context_);\r
82                         return std::vector<std::vector<short>>();\r
83                 }\r
84 \r
85                 auto s = current_chunk_.size();\r
86                 current_chunk_.resize(s + 4*format_desc_.audio_sample_rate*2+FF_INPUT_BUFFER_PADDING_SIZE/2);\r
87                 \r
88                 int written_bytes = (current_chunk_.size() - s)*2 - FF_INPUT_BUFFER_PADDING_SIZE;\r
89                 const int errn = avcodec_decode_audio2(codec_context_, &current_chunk_[s], &written_bytes, audio_packet->data(), audio_packet->size());\r
90                 if(errn < 0)\r
91                 {       \r
92                         BOOST_THROW_EXCEPTION(\r
93                                 invalid_operation() <<\r
94                                 boost::errinfo_api_function("avcodec_decode_audio2") <<\r
95                                 boost::errinfo_errno(AVUNERROR(errn)));\r
96                 }\r
97 \r
98                 current_chunk_.resize(s + written_bytes/2);\r
99 \r
100                 auto chunks = split(current_chunk_, format_desc_.audio_samples_per_frame);\r
101 \r
102                 current_chunk_ = chunks.back();\r
103                 chunks.pop_back();\r
104                 \r
105                 return chunks;\r
106         }\r
107 };\r
108 \r
109 audio_decoder::audio_decoder(AVCodecContext* codec_context, const core::video_format_desc& format_desc) : impl_(new implementation(codec_context, format_desc)){}\r
110 std::vector<std::vector<short>> audio_decoder::execute(const std::shared_ptr<aligned_buffer>& audio_packet){return impl_->execute(audio_packet);}\r
111 }