]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/audio/audio_decoder.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[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 <tbb/task_group.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         std::shared_ptr<AVCodecContext>                                                         codec_context_;         \r
46         const core::video_format_desc                                                           format_desc_;\r
47         int                                                                                                                     index_;\r
48         std::shared_ptr<ReSampleContext>                                                        resampler_;\r
49 \r
50         std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>>      buffer1_;\r
51         std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>>      buffer2_;\r
52         std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>     audio_samples_; \r
53         std::queue<std::shared_ptr<AVPacket>>                                           packets_;\r
54 public:\r
55         explicit implementation(const std::shared_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc) \r
56                 : format_desc_(format_desc)     \r
57         {                          \r
58                 AVCodec* dec;\r
59                 index_ = av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);\r
60 \r
61                 if(index_ < 0)\r
62                         return;\r
63 \r
64                 int errn = avcodec_open(context->streams[index_]->codec, dec);\r
65                 if(errn < 0)\r
66                         return;\r
67                                 \r
68                 codec_context_.reset(context->streams[index_]->codec, avcodec_close);\r
69 \r
70                 if(codec_context_ &&\r
71                    (codec_context_->sample_rate != static_cast<int>(format_desc_.audio_sample_rate) || \r
72                     codec_context_->channels    != static_cast<int>(format_desc_.audio_channels)) ||\r
73                         codec_context_->sample_fmt      != AV_SAMPLE_FMT_S16)\r
74                 {       \r
75                         auto resampler = av_audio_resample_init(format_desc_.audio_channels,    codec_context_->channels,\r
76                                                                                                         format_desc_.audio_sample_rate, codec_context_->sample_rate,\r
77                                                                                                         AV_SAMPLE_FMT_S16,                              codec_context_->sample_fmt,\r
78                                                                                                         16, 10, 0, 0.8);\r
79 \r
80                         CASPAR_LOG(warning) << L" Invalid audio format.";\r
81 \r
82                         if(resampler)\r
83                                 resampler_.reset(resampler, audio_resample_close);\r
84                         else\r
85                                 codec_context_ = nullptr;\r
86                 }               \r
87         }\r
88 \r
89         void push(const std::shared_ptr<AVPacket>& packet)\r
90         {                       \r
91                 if(!codec_context_)\r
92                         return;\r
93 \r
94                 if(packet && packet->stream_index != index_)\r
95                         return;\r
96 \r
97                 packets_.push(packet);\r
98         }       \r
99         \r
100         std::vector<std::shared_ptr<std::vector<int16_t>>> poll()\r
101         {\r
102                 std::vector<std::shared_ptr<std::vector<int16_t>>> result;\r
103 \r
104                 if(!codec_context_)\r
105                         result.push_back(std::make_shared<std::vector<int16_t>>(format_desc_.audio_samples_per_frame, 0));\r
106                 else if(!packets_.empty())\r
107                 {                       \r
108                         if(packets_.front())            \r
109                         {\r
110                                 AVPacket pkt;\r
111                                 av_init_packet(&pkt);\r
112                                 pkt.data = packets_.front()->data;\r
113                                 pkt.size = packets_.front()->size;\r
114 \r
115                                 for(int n = 0; n < 64 && pkt.size > 0; ++n)\r
116                                         result.push_back(decode(pkt));\r
117                         }\r
118                         else                    \r
119                         {       \r
120                                 avcodec_flush_buffers(codec_context_.get());\r
121                                 result.push_back(nullptr);\r
122                         }\r
123 \r
124                         packets_.pop();\r
125                 }\r
126 \r
127                 return result;\r
128         }\r
129 \r
130         std::shared_ptr<std::vector<int16_t>> decode(AVPacket& pkt)\r
131         {               \r
132                 buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2, 0);\r
133                 int written_bytes = buffer1_.size() - FF_INPUT_BUFFER_PADDING_SIZE;\r
134 \r
135                 const int ret = avcodec_decode_audio3(codec_context_.get(), reinterpret_cast<int16_t*>(buffer1_.data()), &written_bytes, &pkt);\r
136                 if(ret < 0)\r
137                 {       \r
138                         BOOST_THROW_EXCEPTION(\r
139                                 invalid_operation() <<\r
140                                 boost::errinfo_api_function("avcodec_decode_audio2") <<\r
141                                 boost::errinfo_errno(AVUNERROR(ret)));\r
142                 }\r
143 \r
144                 // There might be several frames in one packet.\r
145                 pkt.size -= ret;\r
146                 pkt.data += ret;\r
147                         \r
148                 buffer1_.resize(written_bytes);\r
149 \r
150                 if(resampler_)\r
151                 {\r
152                         buffer2_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2, 0);\r
153                         auto ret = audio_resample(resampler_.get(),\r
154                                                                                 reinterpret_cast<short*>(buffer2_.data()), \r
155                                                                                 reinterpret_cast<short*>(buffer1_.data()), \r
156                                                                                 buffer1_.size() / (av_get_bytes_per_sample(codec_context_->sample_fmt) * codec_context_->channels)); \r
157                         buffer2_.resize(ret * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * format_desc_.audio_channels);\r
158                         std::swap(buffer1_, buffer2_);\r
159                 }\r
160 \r
161                 const auto n_samples = buffer1_.size() / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);\r
162                 const auto samples = reinterpret_cast<int16_t*>(buffer1_.data());\r
163 \r
164                 return std::make_shared<std::vector<int16_t>>(samples, samples + n_samples);\r
165         }\r
166 \r
167         bool ready() const\r
168         {\r
169                 return !codec_context_ || !packets_.empty();\r
170         }\r
171 };\r
172 \r
173 audio_decoder::audio_decoder(const std::shared_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc) : impl_(new implementation(context, format_desc)){}\r
174 void audio_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
175 bool audio_decoder::ready() const{return impl_->ready();}\r
176 std::vector<std::shared_ptr<std::vector<int16_t>>> audio_decoder::poll(){return impl_->poll();}\r
177 }