]> 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 #include "audio_resampler.h"\r
24 \r
25 #include "../util/util.h"\r
26 #include "../../ffmpeg_error.h"\r
27 \r
28 #include <common/concurrency/governor.h>\r
29 #include <common/exception/win32_exception.h>\r
30 #include <core/video_format.h>\r
31 \r
32 #include <tbb/cache_aligned_allocator.h>\r
33 \r
34 #if defined(_MSC_VER)\r
35 #pragma warning (push)\r
36 #pragma warning (disable : 4244)\r
37 #endif\r
38 extern "C" \r
39 {\r
40         #include <libavformat/avformat.h>\r
41         #include <libavcodec/avcodec.h>\r
42 }\r
43 #if defined(_MSC_VER)\r
44 #pragma warning (pop)\r
45 #endif\r
46 \r
47 #undef Yield\r
48 using namespace Concurrency;\r
49 \r
50 namespace caspar { namespace ffmpeg {\r
51         \r
52 struct audio_decoder::implementation : public agent, boost::noncopyable\r
53 {       \r
54         ITarget<audio_decoder::target_element_t>&                                       target_;\r
55 \r
56         int                                                                                                                     index_;\r
57         const safe_ptr<AVCodecContext>                                                          codec_context_;         \r
58         \r
59         audio_resampler                                                                                         resampler_;\r
60         \r
61         std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>>      buffer1_;\r
62 \r
63         unbounded_buffer<audio_decoder::source_element_t>                       source_;\r
64 \r
65         governor                                                                                                        governor_;\r
66 \r
67         tbb::atomic<bool>                                                                                       is_running_;\r
68         \r
69 public:\r
70         explicit implementation(audio_decoder::source_t& source, audio_decoder::target_t& target, AVFormatContext& context, const core::video_format_desc& format_desc) \r
71                 : target_(target)\r
72                 , codec_context_(open_codec(context, AVMEDIA_TYPE_AUDIO, index_))\r
73                 , resampler_(format_desc.audio_channels,        codec_context_->channels,\r
74                                          format_desc.audio_sample_rate, codec_context_->sample_rate,\r
75                                          AV_SAMPLE_FMT_S32,                             codec_context_->sample_fmt)\r
76                 , buffer1_(AVCODEC_MAX_AUDIO_FRAME_SIZE*2)\r
77                 , source_([this](const audio_decoder::source_element_t& packet){return packet->stream_index == index_;})\r
78                 , governor_(2)\r
79         {               \r
80                 CASPAR_LOG(debug) << "[audio_decoder] " << context.streams[index_]->codec->codec->long_name;\r
81 \r
82                 source.link_target(&source_);\r
83                 \r
84                 is_running_ = true;\r
85 \r
86                 start();\r
87         }\r
88 \r
89         ~implementation()\r
90         {\r
91                 is_running_ = false;\r
92                 governor_.cancel();\r
93                 agent::wait(this);\r
94         }\r
95 \r
96         virtual void run()\r
97         {\r
98                 win32_exception::install_handler();\r
99 \r
100                 try\r
101                 {\r
102                         while(is_running_)\r
103                         {               \r
104                                 auto ticket = governor_.acquire();\r
105                                 auto packet = receive(source_);\r
106                         \r
107                                 if(packet == flush_packet(index_))\r
108                                 {\r
109                                         avcodec_flush_buffers(codec_context_.get());\r
110                                         send(target_, flush_audio());\r
111                                         continue;\r
112                                 }\r
113 \r
114                                 if(packet == eof_packet(index_))\r
115                                         break;\r
116 \r
117                                 auto result = std::make_shared<core::audio_buffer>();\r
118                                 \r
119                                 while(packet->size > 0)\r
120                                 {\r
121                                         buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
122                                         int written_bytes = buffer1_.size() - FF_INPUT_BUFFER_PADDING_SIZE;\r
123                 \r
124                                         int ret = THROW_ON_ERROR2(avcodec_decode_audio3(codec_context_.get(), reinterpret_cast<int16_t*>(buffer1_.data()), &written_bytes, packet.get()), "[audio_decoder]");\r
125 \r
126                                         // There might be several frames in one packet.\r
127                                         packet->size -= ret;\r
128                                         packet->data += ret;\r
129                         \r
130                                         buffer1_.resize(written_bytes);\r
131 \r
132                                         buffer1_ = resampler_.resample(std::move(buffer1_));\r
133                 \r
134                                         const auto n_samples = buffer1_.size() / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32);\r
135                                         const auto samples = reinterpret_cast<int32_t*>(buffer1_.data());\r
136 \r
137                                         auto audio = make_safe<core::audio_buffer>(samples, samples + n_samples);\r
138 \r
139                                         send(target_, safe_ptr<core::audio_buffer>(audio.get(), [audio, ticket](core::audio_buffer*){}));\r
140                                         Context::Yield();\r
141                                 }\r
142                         }\r
143                 }\r
144                 catch(...)\r
145                 {\r
146                         CASPAR_LOG_CURRENT_EXCEPTION();\r
147                 }\r
148 \r
149                 send(target_, eof_audio());\r
150 \r
151                 done();\r
152         }\r
153 };\r
154 \r
155 audio_decoder::audio_decoder(audio_decoder::source_t& source, audio_decoder::target_t& target, AVFormatContext& context, const core::video_format_desc& format_desc)\r
156         : impl_(new implementation(source, target, context, format_desc))\r
157 {\r
158 }\r
159 \r
160 int64_t audio_decoder::nb_frames() const\r
161 {\r
162         return 0;\r
163 }\r
164 \r
165 }}