]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/audio/audio_decoder.cpp
2.1.0: -ffmpeg_producer: seek while paused.
[casparcg] / modules / ffmpeg / producer / audio / audio_decoder.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "audio_decoder.h"\r
25 \r
26 #include "audio_resampler.h"\r
27 \r
28 #include "../util/util.h"\r
29 #include "../../ffmpeg_error.h"\r
30 \r
31 #include <core/video_format.h>\r
32 \r
33 #include <common/log.h>\r
34 \r
35 #include <tbb/cache_aligned_allocator.h>\r
36 \r
37 #include <queue>\r
38 \r
39 #if defined(_MSC_VER)\r
40 #pragma warning (push)\r
41 #pragma warning (disable : 4244)\r
42 #endif\r
43 extern "C" \r
44 {\r
45         #include <libavformat/avformat.h>\r
46         #include <libavcodec/avcodec.h>\r
47 }\r
48 #if defined(_MSC_VER)\r
49 #pragma warning (pop)\r
50 #endif\r
51 \r
52 namespace caspar { namespace ffmpeg {\r
53         \r
54 struct audio_decoder::impl : boost::noncopyable\r
55 {       \r
56         monitor::basic_subject                                                                          event_subject_;\r
57         int                                                                                                                     index_;\r
58         const std::shared_ptr<AVCodecContext>                                           codec_context_;         \r
59         const core::video_format_desc                                                           format_desc_;\r
60 \r
61         boost::optional<audio_resampler>                                                        resampler_;\r
62 \r
63         std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>>      buffer1_;\r
64 \r
65         std::queue<spl::shared_ptr<AVPacket>>                                           packets_;\r
66 \r
67         const int64_t                                                                                           nb_frames_;\r
68         tbb::atomic<uint32_t>                                                                           file_frame_number_;\r
69 public:\r
70         explicit impl() \r
71                 : nb_frames_(0)//context->streams[index_]->nb_frames)\r
72         {               \r
73                 file_frame_number_ = 0;   \r
74         }\r
75 \r
76         explicit impl(const std::shared_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc) \r
77                 : format_desc_(format_desc)     \r
78                 , codec_context_(open_codec(*context, AVMEDIA_TYPE_AUDIO, index_))\r
79                 , resampler_(audio_resampler(format_desc.audio_channels,        codec_context_->channels,\r
80                                          format_desc.audio_sample_rate, codec_context_->sample_rate,\r
81                                          AV_SAMPLE_FMT_S32,                             codec_context_->sample_fmt))\r
82                 , buffer1_(AVCODEC_MAX_AUDIO_FRAME_SIZE*2)\r
83                 , nb_frames_(0)//context->streams[index_]->nb_frames)\r
84         {               \r
85                 file_frame_number_ = 0;   \r
86         }\r
87 \r
88         void push(const std::shared_ptr<AVPacket>& packet)\r
89         {                       \r
90                 if(!packet)\r
91                         return;\r
92 \r
93                 if(packet->stream_index == index_ || packet->data == nullptr)\r
94                         packets_.push(spl::make_shared_ptr(packet));\r
95         }       \r
96         \r
97         std::shared_ptr<core::audio_buffer> poll()\r
98         {\r
99                 if(packets_.empty())\r
100                         return nullptr;\r
101                                 \r
102                 auto packet = packets_.front();\r
103                 \r
104                 if(!codec_context_)             \r
105                 {\r
106                         packets_.pop();\r
107                         return packet->data == nullptr ? flush_audio() : empty_audio();\r
108                 }\r
109                 else\r
110                 {\r
111                         if(packet->data == nullptr)\r
112                         {\r
113                                 packets_.pop();\r
114                                 file_frame_number_ = static_cast<uint32_t>(packet->pos);\r
115                                 avcodec_flush_buffers(codec_context_.get());\r
116                                 return flush_audio();\r
117                         }\r
118 \r
119                         auto audio = decode(*packet);\r
120 \r
121                         if(packet->size == 0)                                   \r
122                                 packets_.pop();\r
123 \r
124                         return audio;\r
125                 }\r
126         }\r
127 \r
128         std::shared_ptr<core::audio_buffer> decode(AVPacket& pkt)\r
129         {               \r
130                 buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
131                 int written_bytes = static_cast<int>(buffer1_.size()) - FF_INPUT_BUFFER_PADDING_SIZE;\r
132                 \r
133                 int ret = THROW_ON_ERROR2(avcodec_decode_audio3(codec_context_.get(), reinterpret_cast<int16_t*>(buffer1_.data()), &written_bytes, &pkt), "[audio_decoder]");\r
134 \r
135                 // There might be several frames in one packet.\r
136                 pkt.size -= ret;\r
137                 pkt.data += ret;\r
138                         \r
139                 buffer1_.resize(written_bytes);\r
140 \r
141                 buffer1_ = resampler_->resample(std::move(buffer1_));\r
142                 \r
143                 const auto n_samples = buffer1_.size() / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32);\r
144                 const auto samples = reinterpret_cast<int32_t*>(buffer1_.data());\r
145                 \r
146                 event_subject_  << monitor::event("file/audio/sample-rate")     % codec_context_->sample_rate\r
147                                                 << monitor::event("file/audio/channels")        % codec_context_->channels\r
148                                                 << monitor::event("file/audio/format")          % u8(av_get_sample_fmt_name(codec_context_->sample_fmt))\r
149                                                 << monitor::event("file/audio/codec")           % u8(codec_context_->codec->long_name);\r
150 \r
151                 ++file_frame_number_;\r
152 \r
153                 return std::make_shared<core::audio_buffer>(samples, samples + n_samples);\r
154         }\r
155 \r
156         bool ready() const\r
157         {\r
158                 return !codec_context_ || !packets_.empty();\r
159         }\r
160         \r
161         void clear()\r
162         {\r
163                 while(!packets_.empty())\r
164                         packets_.pop();\r
165         }\r
166 \r
167         uint32_t nb_frames() const\r
168         {\r
169                 return 0;//std::max<int64_t>(nb_frames_, file_frame_number_);\r
170         }\r
171 \r
172         std::wstring print() const\r
173         {               \r
174                 return L"[audio-decoder] " + u16(codec_context_->codec->long_name);\r
175         }\r
176 };\r
177 \r
178 audio_decoder::audio_decoder() : impl_(new impl()){}\r
179 audio_decoder::audio_decoder(const std::shared_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc) : impl_(new impl(context, format_desc)){}\r
180 audio_decoder::audio_decoder(audio_decoder&& other) : impl_(std::move(other.impl_)){}\r
181 audio_decoder& audio_decoder::operator=(audio_decoder&& other){impl_ = std::move(other.impl_); return *this;}\r
182 void audio_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
183 bool audio_decoder::ready() const{return impl_->ready();}\r
184 std::shared_ptr<core::audio_buffer> audio_decoder::poll(){return impl_->poll();}\r
185 uint32_t audio_decoder::nb_frames() const{return impl_->nb_frames();}\r
186 uint32_t audio_decoder::file_frame_number() const{return impl_->file_frame_number_;}\r
187 std::wstring audio_decoder::print() const{return impl_->print();}\r
188 void audio_decoder::clear(){impl_->clear();}\r
189 void audio_decoder::subscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.subscribe(o);}\r
190 void audio_decoder::unsubscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.unsubscribe(o);}\r
191 \r
192 }}