]> 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) 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 "../util/util.h"\r
27 #include "../input/input.h"\r
28 #include "../../ffmpeg_error.h"\r
29 \r
30 #include <core/video_format.h>\r
31 \r
32 #include <common/log.h>\r
33 \r
34 #include <tbb/cache_aligned_allocator.h>\r
35 \r
36 #include <queue>\r
37 \r
38 #if defined(_MSC_VER)\r
39 #pragma warning (push)\r
40 #pragma warning (disable : 4244)\r
41 #endif\r
42 extern "C" \r
43 {\r
44         #include <libavformat/avformat.h>\r
45         #include <libavcodec/avcodec.h>\r
46         #include <libswresample/swresample.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 uint64_t get_channel_layout(AVCodecContext* dec)\r
55 {\r
56         auto layout = (dec->channel_layout && dec->channels == av_get_channel_layout_nb_channels(dec->channel_layout)) ? dec->channel_layout : av_get_default_channel_layout(dec->channels);\r
57         return layout;\r
58 }\r
59 \r
60 struct audio_decoder::impl : boost::noncopyable\r
61 {       \r
62         monitor::basic_subject                                                                          event_subject_;\r
63         input*                                                                                                          input_;\r
64         int                                                                                                                     index_;\r
65         const std::shared_ptr<AVCodecContext>                                           codec_context_;         \r
66         const core::video_format_desc                                                           format_desc_;\r
67 \r
68         std::shared_ptr<SwrContext>                                                                     swr_;\r
69 \r
70         std::vector<uint8_t, tbb::cache_aligned_allocator<int8_t>>      buffer_;\r
71 \r
72         std::queue<spl::shared_ptr<AVPacket>>                                           packets_;\r
73         \r
74 public:\r
75         impl()\r
76                 : input_(nullptr)\r
77         {\r
78         }\r
79 \r
80         explicit impl(input& in, const core::video_format_desc& format_desc) \r
81                 : input_(&in)\r
82                 , format_desc_(format_desc)     \r
83                 , codec_context_(open_codec(input_->context(), AVMEDIA_TYPE_AUDIO, index_))\r
84                 , swr_(swr_alloc_set_opts(nullptr,\r
85                                                                                 av_get_default_channel_layout(format_desc_.audio_channels), AV_SAMPLE_FMT_S32, format_desc_.audio_sample_rate,\r
86                                                                                 get_channel_layout(codec_context_.get()), codec_context_->sample_fmt, codec_context_->sample_rate,\r
87                                                                                 0, nullptr), [](SwrContext* p){swr_free(&p);})\r
88                 , buffer_(AVCODEC_MAX_AUDIO_FRAME_SIZE*4)\r
89         {               \r
90                 if(!swr_)\r
91                         CASPAR_THROW_EXCEPTION(bad_alloc());\r
92 \r
93                 THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]");\r
94         }\r
95                 \r
96         std::shared_ptr<core::audio_buffer> poll()\r
97         {               \r
98                 auto result = std::make_shared<core::audio_buffer>();\r
99 \r
100                 if(!codec_context_)\r
101                         return empty_audio();\r
102 \r
103                 std::shared_ptr<AVPacket> packet;\r
104                 if(!input_->try_pop_audio(packet))\r
105                         return result;\r
106 \r
107                 if(packet)\r
108                 {\r
109                         if(!packet->data && (codec_context_->codec->capabilities & CODEC_CAP_DELAY))\r
110                                 while(decode(*packet, *result));\r
111 \r
112                         while(packet->size > 0)         \r
113                                 decode(*packet, *result);\r
114                 }               \r
115                 else            \r
116                         avcodec_flush_buffers(codec_context_.get());\r
117 \r
118                 return result;\r
119         }\r
120 \r
121         bool decode(AVPacket& pkt, core::audio_buffer& result)\r
122         {               \r
123                 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
124 \r
125                 int got_frame = 0;\r
126                 auto len = THROW_ON_ERROR2(avcodec_decode_audio4(codec_context_.get(), decoded_frame.get(), &got_frame, &pkt), "[audio_decoder]");\r
127                                         \r
128                 if(len == 0)\r
129                 {\r
130                         pkt.size = 0;\r
131                         return false;\r
132                 }\r
133 \r
134         pkt.data += len;\r
135         pkt.size -= len;\r
136 \r
137                 if(!got_frame)\r
138                         return false;\r
139                                                         \r
140                 const uint8_t *in[] = {decoded_frame->data[0]};\r
141                 uint8_t* out[]          = {buffer_.data()};\r
142 \r
143                 auto channel_samples = swr_convert(swr_.get(), \r
144                                                                                         out, static_cast<int>(buffer_.size()) / format_desc_.audio_channels / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32), \r
145                                                                                         in, decoded_frame->nb_samples);\r
146                         \r
147                 auto ptr = reinterpret_cast<int32_t*>(buffer_.data());\r
148                 result.insert(result.end(), ptr, ptr + channel_samples * format_desc_.audio_channels);          \r
149                 \r
150                 event_subject_  << monitor::event("file/audio/sample-rate")     % codec_context_->sample_rate\r
151                                                 << monitor::event("file/audio/channels")        % codec_context_->channels\r
152                                                 << monitor::event("file/audio/format")          % u8(av_get_sample_fmt_name(codec_context_->sample_fmt))\r
153                                                 << monitor::event("file/audio/codec")           % u8(codec_context_->codec->long_name);                         \r
154 \r
155                 return true;\r
156         }\r
157         \r
158         uint32_t nb_frames() const\r
159         {\r
160                 return 0;\r
161         }\r
162 \r
163         std::wstring print() const\r
164         {               \r
165                 return L"[audio-decoder] " + u16(codec_context_->codec->long_name);\r
166         }\r
167 };\r
168 \r
169 audio_decoder::audio_decoder() : impl_(new impl()){}\r
170 audio_decoder::audio_decoder(input& input, const core::video_format_desc& format_desc) : impl_(new impl(input, format_desc)){}\r
171 audio_decoder::audio_decoder(audio_decoder&& other) : impl_(std::move(other.impl_)){}\r
172 audio_decoder& audio_decoder::operator=(audio_decoder&& other){impl_ = std::move(other.impl_); return *this;}\r
173 std::shared_ptr<core::audio_buffer> audio_decoder::operator()(){return impl_->poll();}\r
174 uint32_t audio_decoder::nb_frames() const{return impl_->nb_frames();}\r
175 std::wstring audio_decoder::print() const{return impl_->print();}\r
176 void audio_decoder::subscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.subscribe(o);}\r
177 void audio_decoder::unsubscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.unsubscribe(o);}\r
178 \r
179 }}