]> 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.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 using namespace Concurrency;\r
46 \r
47 namespace caspar { namespace ffmpeg {\r
48         \r
49 struct audio_decoder::implementation : public agent, boost::noncopyable\r
50 {       \r
51         int                                                                                                                     index_;\r
52         std::shared_ptr<AVCodecContext>                                                         codec_context_;         \r
53         \r
54         audio_resampler                                                                                         resampler_;\r
55         \r
56         std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>>      buffer1_;\r
57 \r
58         overwrite_buffer<bool>                                  is_running_;\r
59         unbounded_buffer<safe_ptr<AVPacket>>    source_;\r
60         ITarget<safe_ptr<core::audio_buffer>>&  target_;\r
61         \r
62 public:\r
63         explicit implementation(audio_decoder::source_t& source, audio_decoder::target_t& target, AVFormatContext& context, const core::video_format_desc& format_desc) \r
64                 : codec_context_(open_codec(context, AVMEDIA_TYPE_AUDIO, index_))\r
65                 , resampler_(format_desc.audio_channels,        codec_context_->channels,\r
66                                          format_desc.audio_sample_rate, codec_context_->sample_rate,\r
67                                          AV_SAMPLE_FMT_S32,                             codec_context_->sample_fmt)\r
68                 , buffer1_(AVCODEC_MAX_AUDIO_FRAME_SIZE*2)\r
69                 , source_([this](const safe_ptr<AVPacket>& packet)\r
70                         {\r
71                                 return packet->stream_index == index_;\r
72                         })\r
73                 , target_(target)\r
74         {                               \r
75                 CASPAR_LOG(debug) << "[audio_decoder] " << context.streams[index_]->codec->codec->long_name;\r
76 \r
77                 source.link_target(&source_);\r
78 \r
79                 start();\r
80         }\r
81 \r
82         ~implementation()\r
83         {\r
84                 send(is_running_, false);\r
85                 agent::wait(this);\r
86         }\r
87 \r
88         virtual void run()\r
89         {\r
90                 try\r
91                 {\r
92                         send(is_running_, true);\r
93                         while(is_running_.value())\r
94                         {                               \r
95                                 auto packet = receive(source_);\r
96                         \r
97                                 if(packet == loop_packet(index_))\r
98                                 {\r
99                                         send(target_, loop_audio());\r
100                                         continue;\r
101                                 }\r
102 \r
103                                 if(packet == eof_packet(index_))\r
104                                         break;\r
105 \r
106                                 auto result = std::make_shared<core::audio_buffer>();\r
107 \r
108                                 while(packet->size > 0)\r
109                                 {\r
110                                         buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
111                                         int written_bytes = buffer1_.size() - FF_INPUT_BUFFER_PADDING_SIZE;\r
112                 \r
113                                         int ret = THROW_ON_ERROR2(avcodec_decode_audio3(codec_context_.get(), reinterpret_cast<int16_t*>(buffer1_.data()), &written_bytes, packet.get()), "[audio_decoder]");\r
114 \r
115                                         // There might be several frames in one packet.\r
116                                         packet->size -= ret;\r
117                                         packet->data += ret;\r
118                         \r
119                                         buffer1_.resize(written_bytes);\r
120 \r
121                                         buffer1_ = resampler_.resample(std::move(buffer1_));\r
122                 \r
123                                         const auto n_samples = buffer1_.size() / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32);\r
124                                         const auto samples = reinterpret_cast<int32_t*>(buffer1_.data());\r
125 \r
126                                         send(target_, make_safe<core::audio_buffer>(samples, samples + n_samples));\r
127                                 }\r
128                                 Concurrency::wait(5);\r
129                         }\r
130                 }\r
131                 catch(...)\r
132                 {\r
133                         CASPAR_LOG_CURRENT_EXCEPTION();\r
134                 }\r
135 \r
136                 send(is_running_, false);\r
137                 send(target_, eof_audio());\r
138 \r
139                 done();\r
140         }\r
141 };\r
142 \r
143 audio_decoder::audio_decoder(audio_decoder::source_t& source, audio_decoder::target_t& target, AVFormatContext& context, const core::video_format_desc& format_desc)\r
144         : impl_(new implementation(source, target, context, format_desc))\r
145 {\r
146 }\r
147 \r
148 int64_t audio_decoder::nb_frames() const\r
149 {\r
150         return 0;\r
151 }\r
152 \r
153 }}