]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/audio/audio_decoder.cpp
[ffmpeg] Reimplemented support for playing all audio streams in a clip and treating...
[casparcg] / modules / ffmpeg / producer / audio / audio_decoder.cpp
1 /*
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "audio_decoder.h"
25
26 #include "../util/util.h"
27 #include "../../ffmpeg_error.h"
28
29 #include <core/video_format.h>
30 #include <core/mixer/audio/audio_util.h>
31
32 #include <common/cache_aligned_vector.h>
33
34 #include <queue>
35
36 #if defined(_MSC_VER)
37 #pragma warning (push)
38 #pragma warning (disable : 4244)
39 #endif
40 extern "C"
41 {
42         #include <libavformat/avformat.h>
43         #include <libavcodec/avcodec.h>
44         #include <libswresample/swresample.h>
45 }
46 #if defined(_MSC_VER)
47 #pragma warning (pop)
48 #endif
49
50 namespace caspar { namespace ffmpeg {
51
52 struct audio_decoder::implementation : boost::noncopyable
53 {
54         int                                                                             index_;
55         const spl::shared_ptr<AVCodecContext>   codec_context_;
56         const int                                                               out_samplerate_;
57
58         cache_aligned_vector<int32_t>                   buffer_;
59
60         std::queue<spl::shared_ptr<AVPacket>>   packets_;
61
62         std::shared_ptr<SwrContext>                             swr_                            {
63                                                                                                                                         swr_alloc_set_opts(
64                                                                                                                                                         nullptr,
65                                                                                                                                                         codec_context_->channel_layout
66                                                                                                                                                                         ? codec_context_->channel_layout
67                                                                                                                                                                         : av_get_default_channel_layout(codec_context_->channels),
68                                                                                                                                                         AV_SAMPLE_FMT_S32,
69                                                                                                                                                         out_samplerate_,
70                                                                                                                                                         codec_context_->channel_layout
71                                                                                                                                                                         ? codec_context_->channel_layout
72                                                                                                                                                                         : av_get_default_channel_layout(codec_context_->channels),
73                                                                                                                                                         codec_context_->sample_fmt,
74                                                                                                                                                         codec_context_->sample_rate,
75                                                                                                                                                         0,
76                                                                                                                                                         nullptr),
77                                                                                                                                         [](SwrContext* p)
78                                                                                                                                         {
79                                                                                                                                                 swr_free(&p);
80                                                                                                                                         }
81                                                                                                                                 };
82
83 public:
84         explicit implementation(int stream_index, const spl::shared_ptr<AVFormatContext>& context, int out_samplerate)
85                 : index_(stream_index)
86                 , codec_context_(open_codec(*context, AVMEDIA_TYPE_AUDIO, index_, false))
87                 , out_samplerate_(out_samplerate)
88                 , buffer_(10 * out_samplerate_ * codec_context_->channels) // 10 seconds of audio
89         {
90                 if(!swr_)
91                         CASPAR_THROW_EXCEPTION(bad_alloc());
92
93                 THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]");
94
95                 codec_context_->refcounted_frames = 1;
96         }
97
98         void push(const std::shared_ptr<AVPacket>& packet)
99         {
100                 if(!packet)
101                         return;
102
103                 if(packet->stream_index == index_ || packet->data == nullptr)
104                         packets_.push(spl::make_shared_ptr(packet));
105         }
106
107         std::shared_ptr<core::mutable_audio_buffer> poll()
108         {
109                 if(packets_.empty())
110                         return nullptr;
111
112                 auto packet = packets_.front();
113
114                 if(packet->data == nullptr)
115                 {
116                         packets_.pop();
117                         avcodec_flush_buffers(codec_context_.get());
118                         return flush_audio();
119                 }
120
121                 auto audio = decode(*packet);
122
123                 if(packet->size == 0)
124                         packets_.pop();
125
126                 return audio;
127         }
128
129         std::shared_ptr<core::mutable_audio_buffer> decode(AVPacket& pkt)
130         {
131                 auto decoded_frame = create_frame();
132
133                 int got_frame = 0;
134                 auto len = THROW_ON_ERROR2(avcodec_decode_audio4(codec_context_.get(), decoded_frame.get(), &got_frame, &pkt), "[audio_decoder]");
135
136                 if (len == 0)
137                 {
138                         pkt.size = 0;
139                         return nullptr;
140                 }
141
142                 pkt.data += len;
143                 pkt.size -= len;
144
145                 if (!got_frame)
146                         return nullptr;
147
148                 const uint8_t **in = const_cast<const uint8_t**>(decoded_frame->extended_data);
149                 uint8_t* out[] = { reinterpret_cast<uint8_t*>(buffer_.data()) };
150
151                 const auto channel_samples = swr_convert(
152                                 swr_.get(),
153                                 out,
154                                 static_cast<int>(buffer_.size()) / codec_context_->channels,
155                                 in,
156                                 decoded_frame->nb_samples);
157
158                 return std::make_shared<core::mutable_audio_buffer>(
159                                 buffer_.begin(),
160                                 buffer_.begin() + channel_samples * decoded_frame->channels);
161         }
162
163         bool ready() const
164         {
165                 return packets_.size() > 10;
166         }
167
168         std::wstring print() const
169         {
170                 return L"[audio-decoder] " + u16(codec_context_->codec->long_name);
171         }
172
173         uint64_t ffmpeg_channel_layout() const
174         {
175                 if (codec_context_->channel_layout == 0)
176                         return av_get_default_channel_layout(codec_context_->channels);
177                 else
178                         return codec_context_->channel_layout;
179         }
180 };
181
182 audio_decoder::audio_decoder(int stream_index, const spl::shared_ptr<AVFormatContext>& context, int out_samplerate) : impl_(new implementation(stream_index, context, out_samplerate)){}
183 void audio_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}
184 bool audio_decoder::ready() const{return impl_->ready();}
185 std::shared_ptr<core::mutable_audio_buffer> audio_decoder::poll() { return impl_->poll(); }
186 int     audio_decoder::num_channels() const { return impl_->codec_context_->channels; }
187 uint64_t audio_decoder::ffmpeg_channel_layout() const { return impl_->ffmpeg_channel_layout(); }
188 std::wstring audio_decoder::print() const{return impl_->print();}
189
190 }}