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