]> 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 "../../ffmpeg_error.h"\r
25 \r
26 #include <core/video_format.h>\r
27 \r
28 #include <tbb/cache_aligned_allocator.h>\r
29 \r
30 #include <queue>\r
31 \r
32 #if defined(_MSC_VER)\r
33 #pragma warning (push)\r
34 #pragma warning (disable : 4244)\r
35 #endif\r
36 extern "C" \r
37 {\r
38         #include <libavformat/avformat.h>\r
39         #include <libavcodec/avcodec.h>\r
40 }\r
41 #if defined(_MSC_VER)\r
42 #pragma warning (pop)\r
43 #endif\r
44 \r
45 namespace caspar {\r
46         \r
47 struct audio_decoder::implementation : boost::noncopyable\r
48 {       \r
49         std::shared_ptr<AVCodecContext>                                                         codec_context_;         \r
50         const core::video_format_desc                                                           format_desc_;\r
51         int                                                                                                                     index_;\r
52         std::shared_ptr<ReSampleContext>                                                        resampler_;\r
53 \r
54         std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>>      buffer1_;\r
55         std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>>      buffer2_;\r
56         std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>     audio_samples_; \r
57         std::queue<std::shared_ptr<AVPacket>>                                           packets_;\r
58 \r
59         int64_t                                                                                                         nb_frames_;\r
60         double                                                                                                          duration_;\r
61 public:\r
62         explicit implementation(const safe_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc) \r
63                 : format_desc_(format_desc)     \r
64                 , nb_frames_(0)\r
65                 , duration_(std::numeric_limits<double>::max())\r
66         {                               \r
67                 try\r
68                 {\r
69                         AVCodec* dec;\r
70                         index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0), "[audio_decoder]");\r
71 \r
72                         THROW_ON_ERROR2(avcodec_open(context->streams[index_]->codec, dec), "[audio_decoder]");\r
73                         \r
74                         codec_context_.reset(context->streams[index_]->codec, avcodec_close);\r
75 \r
76                         buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
77 \r
78                         //nb_frames_ = context->streams[index_]->nb_frames;\r
79                         //if(nb_frames_ == 0)\r
80                         //      nb_frames_ = context->streams[index_]->duration / (codec_context_->channels*codec_context_->sample_rate);\r
81                         duration_ = context->streams[index_]->duration / static_cast<double>(codec_context_->sample_rate);\r
82 \r
83                         if(codec_context_->sample_rate  != static_cast<int>(format_desc_.audio_sample_rate) || \r
84                            codec_context_->channels             != static_cast<int>(format_desc_.audio_channels) ||\r
85                            codec_context_->sample_fmt   != AV_SAMPLE_FMT_S16)\r
86                         {       \r
87                                 auto resampler = av_audio_resample_init(format_desc_.audio_channels,    codec_context_->channels,\r
88                                                                                                                 format_desc_.audio_sample_rate, codec_context_->sample_rate,\r
89                                                                                                                 AV_SAMPLE_FMT_S16,                              codec_context_->sample_fmt,\r
90                                                                                                                 16, 10, 0, 0.8);\r
91 \r
92                                 buffer2_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
93 \r
94                                 CASPAR_LOG(warning) << L"Invalid audio format. Resampling." <<\r
95                                                                            L" sample_rate:" << static_cast<int>(codec_context_->sample_rate)  <<\r
96                                                                            L" audio_channels:" << static_cast<int>(codec_context_->channels)  <<\r
97                                                                            L" sample_fmt:" << static_cast<int>(codec_context_->sample_fmt);\r
98 \r
99                                 if(resampler)\r
100                                         resampler_.reset(resampler, audio_resample_close);\r
101                                 else\r
102                                         codec_context_ = nullptr;\r
103                         }               \r
104                 }\r
105                 catch(...)\r
106                 {\r
107                         index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0), "[audio_decoder]");\r
108 \r
109                         CASPAR_LOG_CURRENT_EXCEPTION();\r
110                         CASPAR_LOG(warning) << "[audio_decoder] Failed to open audio-stream. Running without audio.";                   \r
111                 }\r
112         }\r
113 \r
114         void push(const std::shared_ptr<AVPacket>& packet)\r
115         {                       \r
116                 if(packet && packet->stream_index != index_)\r
117                         return;\r
118 \r
119                 packets_.push(packet);\r
120         }       \r
121         \r
122         std::vector<std::shared_ptr<std::vector<int16_t>>> poll()\r
123         {\r
124                 std::vector<std::shared_ptr<std::vector<int16_t>>> result;\r
125 \r
126                 if(packets_.empty())\r
127                         return result;\r
128 \r
129                 if(!codec_context_)\r
130                         return empty_poll();\r
131                 \r
132                 auto packet = packets_.front();\r
133 \r
134                 if(packet)              \r
135                 {\r
136                         result.push_back(decode(*packet));\r
137                         if(packet->size == 0)                                   \r
138                                 packets_.pop();\r
139                 }\r
140                 else                    \r
141                 {       \r
142                         avcodec_flush_buffers(codec_context_.get());\r
143                         result.push_back(nullptr);\r
144                         packets_.pop();\r
145                 }               \r
146 \r
147                 return result;\r
148         }\r
149 \r
150         std::vector<std::shared_ptr<std::vector<int16_t>>> empty_poll()\r
151         {\r
152                 auto packet = packets_.front();\r
153                 packets_.pop();\r
154 \r
155                 if(!packet)                     \r
156                         return boost::assign::list_of(nullptr);\r
157                 \r
158                 return boost::assign::list_of(std::make_shared<std::vector<int16_t>>(format_desc_.audio_samples_per_frame, 0)); \r
159         }\r
160 \r
161         std::shared_ptr<std::vector<int16_t>> decode(AVPacket& pkt)\r
162         {               \r
163                 int written_bytes = buffer1_.size() - FF_INPUT_BUFFER_PADDING_SIZE;\r
164 \r
165                 int ret = THROW_ON_ERROR2(avcodec_decode_audio3(codec_context_.get(), reinterpret_cast<int16_t*>(buffer1_.data()), &written_bytes, &pkt), "[audio_decoder]");\r
166 \r
167                 // There might be several frames in one packet.\r
168                 pkt.size -= ret;\r
169                 pkt.data += ret;\r
170                         \r
171                 if(resampler_)\r
172                 {\r
173                         auto ret = audio_resample(resampler_.get(),\r
174                                                                                 reinterpret_cast<short*>(buffer2_.data()), \r
175                                                                                 reinterpret_cast<short*>(buffer1_.data()), \r
176                                                                                 written_bytes / (av_get_bytes_per_sample(codec_context_->sample_fmt) * codec_context_->channels)); \r
177                         written_bytes = ret * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * format_desc_.audio_channels;\r
178                         std::swap(buffer1_, buffer2_);\r
179                 }\r
180 \r
181                 const auto n_samples = written_bytes / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);\r
182                 const auto samples = reinterpret_cast<int16_t*>(buffer1_.data());\r
183 \r
184                 return std::make_shared<std::vector<int16_t>>(samples, samples + n_samples);\r
185         }\r
186 \r
187         bool ready() const\r
188         {\r
189                 return !packets_.empty();\r
190         }\r
191 };\r
192 \r
193 audio_decoder::audio_decoder(const safe_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc) : impl_(new implementation(context, format_desc)){}\r
194 void audio_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
195 bool audio_decoder::ready() const{return impl_->ready();}\r
196 std::vector<std::shared_ptr<std::vector<int16_t>>> audio_decoder::poll(){return impl_->poll();}\r
197 int64_t audio_decoder::nb_frames() const{return impl_->nb_frames_;}\r
198 double audio_decoder::duration() const {return impl_->duration_;}\r
199 }