]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/audio/audio_decoder.cpp
2.0.1: ffmpeg: Replaced TBB implementation with better Concurrency Runtime based...
[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 #include "audio_resampler.h"\r
24 \r
25 #include "../util.h"\r
26 #include "../../ffmpeg_error.h"\r
27 \r
28 #include <core/video_format.h>\r
29 \r
30 #include <tbb/cache_aligned_allocator.h>\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 { namespace ffmpeg {\r
46         \r
47 struct audio_decoder::implementation : public Concurrency::agent, boost::noncopyable\r
48 {\r
49         audio_decoder::token_t&  active_token_;\r
50         audio_decoder::source_t& source_;\r
51         audio_decoder::target_t& target_;\r
52 \r
53         std::shared_ptr<AVCodecContext>                                                         codec_context_;         \r
54         const core::video_format_desc                                                           format_desc_;\r
55         int                                                                                                                     index_;\r
56         std::unique_ptr<audio_resampler>                                                        resampler_;\r
57 \r
58         std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>>      buffer1_;\r
59         \r
60         int64_t                                                                                                         nb_frames_;\r
61 public:\r
62         explicit implementation(audio_decoder::token_t& active_token,\r
63                                                         audio_decoder::source_t& source,\r
64                                                         audio_decoder::target_t& target,\r
65                                                         const safe_ptr<AVFormatContext>& context, \r
66                                                         const core::video_format_desc& format_desc) \r
67                 : active_token_(active_token)\r
68                 , source_(source)\r
69                 , target_(target)\r
70                 , format_desc_(format_desc)     \r
71                 , nb_frames_(0)\r
72         {                               \r
73                 try\r
74                 {\r
75                         AVCodec* dec;\r
76                         index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0), "[audio_decoder]");\r
77 \r
78                         THROW_ON_ERROR2(avcodec_open(context->streams[index_]->codec, dec), "[audio_decoder]");\r
79                         \r
80                         codec_context_.reset(context->streams[index_]->codec, avcodec_close);\r
81 \r
82                         buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
83 \r
84                         resampler_.reset(new audio_resampler(format_desc_.audio_channels,    codec_context_->channels,\r
85                                                                                                  format_desc_.audio_sample_rate, codec_context_->sample_rate,\r
86                                                                                                  AV_SAMPLE_FMT_S32,                              codec_context_->sample_fmt));  \r
87                 }\r
88                 catch(...)\r
89                 {\r
90                         index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0), "[audio_decoder]");\r
91 \r
92                         CASPAR_LOG_CURRENT_EXCEPTION();\r
93                         CASPAR_LOG(warning) << "[audio_decoder] Failed to open audio-stream. Running without audio.";                   \r
94                 }\r
95 \r
96                 start();\r
97         }\r
98 \r
99         ~implementation()\r
100         {\r
101                 agent::wait(this);\r
102         }       \r
103 \r
104         virtual void run()\r
105         {\r
106                 try\r
107                 {\r
108                         while(Concurrency::receive(active_token_))\r
109                         {\r
110                                 auto packet = Concurrency::receive(source_);\r
111                                 if(packet == eof_packet())\r
112                                 {\r
113                                         Concurrency::send(target_, eof_audio());\r
114                                         break;\r
115                                 }\r
116 \r
117                                 if(packet == loop_packet())     \r
118                                 {       \r
119                                         if(codec_context_)\r
120                                                 avcodec_flush_buffers(codec_context_.get());\r
121                                         Concurrency::send(target_, loop_audio());\r
122                                 }       \r
123                                 else if(!codec_context_)\r
124                                         Concurrency::send(target_, empty_audio());                                      \r
125                                 else            \r
126                                         Concurrency::send(target_, decode(*packet));            \r
127                         }\r
128                 }\r
129                 catch(...)\r
130                 {\r
131                         CASPAR_LOG_CURRENT_EXCEPTION();\r
132                 }\r
133 \r
134                 std::shared_ptr<AVPacket> packet;\r
135                 Concurrency::try_receive(source_, packet);                                              \r
136 \r
137                 done();\r
138         }\r
139         \r
140         std::shared_ptr<core::audio_buffer> decode(AVPacket& pkt)\r
141         {               \r
142                 buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
143                 int written_bytes = buffer1_.size() - FF_INPUT_BUFFER_PADDING_SIZE;\r
144                 \r
145                 int ret = THROW_ON_ERROR2(avcodec_decode_audio3(codec_context_.get(), reinterpret_cast<int16_t*>(buffer1_.data()), &written_bytes, &pkt), "[audio_decoder]");\r
146 \r
147                 // There might be several frames in one packet.\r
148                 pkt.size -= ret;\r
149                 pkt.data += ret;\r
150                         \r
151                 buffer1_.resize(written_bytes);\r
152 \r
153                 buffer1_ = resampler_->resample(std::move(buffer1_));\r
154                 \r
155                 const auto n_samples = buffer1_.size() / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32);\r
156                 const auto samples = reinterpret_cast<int32_t*>(buffer1_.data());\r
157 \r
158                 return std::make_shared<core::audio_buffer>(samples, samples + n_samples);\r
159         }\r
160 };\r
161 \r
162 audio_decoder::audio_decoder(token_t& active_token,\r
163                                                          source_t& source,\r
164                                                          target_t& target,\r
165                                                          const safe_ptr<AVFormatContext>& context, \r
166                                                          const core::video_format_desc& format_desc)\r
167         : impl_(new implementation(active_token, source, target, context, format_desc))\r
168 {\r
169 }\r
170 int64_t audio_decoder::nb_frames() const{return impl_->nb_frames_;}\r
171 \r
172 }}