]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/audio/audio_decoder.cpp
[ffmpeg] Fixed compilation problems in Linux
[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_                          = -1;
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(const spl::shared_ptr<AVFormatContext>& context, int out_samplerate)
85                 : codec_context_(open_codec(*context, AVMEDIA_TYPE_AUDIO, index_, false))
86                 , out_samplerate_(out_samplerate)
87                 , buffer_(10 * out_samplerate_ * codec_context_->channels) // 10 seconds of audio
88         {       
89                 if(!swr_)
90                         BOOST_THROW_EXCEPTION(bad_alloc());
91                 
92                 THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]");
93
94                 codec_context_->refcounted_frames = 1;
95         }
96
97         void push(const std::shared_ptr<AVPacket>& packet)
98         {                       
99                 if(!packet)
100                         return;
101
102                 if(packet->stream_index == index_ || packet->data == nullptr)
103                         packets_.push(spl::make_shared_ptr(packet));
104         }       
105         
106         std::shared_ptr<core::mutable_audio_buffer> poll()
107         {
108                 if(packets_.empty())
109                         return nullptr;
110                                 
111                 auto packet = packets_.front();
112
113                 if(packet->data == nullptr)
114                 {
115                         packets_.pop();
116                         avcodec_flush_buffers(codec_context_.get());
117                         return flush_audio();
118                 }
119
120                 auto audio = decode(*packet);
121
122                 if(packet->size == 0)                                   
123                         packets_.pop();
124
125                 return audio;
126         }
127
128         std::shared_ptr<core::mutable_audio_buffer> decode(AVPacket& pkt)
129         {
130                 auto decoded_frame = create_frame();
131
132                 int got_frame = 0;
133                 auto len = THROW_ON_ERROR2(avcodec_decode_audio4(codec_context_.get(), decoded_frame.get(), &got_frame, &pkt), "[audio_decoder]");
134
135                 if (len == 0)
136                 {
137                         pkt.size = 0;
138                         return nullptr;
139                 }
140
141                 pkt.data += len;
142                 pkt.size -= len;
143
144                 if (!got_frame)
145                         return nullptr;
146
147                 const uint8_t **in = const_cast<const uint8_t**>(decoded_frame->extended_data);
148                 uint8_t* out[] = { reinterpret_cast<uint8_t*>(buffer_.data()) };
149
150                 const auto channel_samples = swr_convert(
151                                 swr_.get(),
152                                 out,
153                                 static_cast<int>(buffer_.size()) / codec_context_->channels,
154                                 in,
155                                 decoded_frame->nb_samples);
156
157                 return std::make_shared<core::mutable_audio_buffer>(
158                                 buffer_.begin(),
159                                 buffer_.begin() + channel_samples * decoded_frame->channels);
160         }
161
162         bool ready() const
163         {
164                 return packets_.size() > 10;
165         }
166
167         std::wstring print() const
168         {               
169                 return L"[audio-decoder] " + u16(codec_context_->codec->long_name);
170         }
171 };
172
173 audio_decoder::audio_decoder(const spl::shared_ptr<AVFormatContext>& context, int out_samplerate) : impl_(new implementation(context, out_samplerate)){}
174 void audio_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}
175 bool audio_decoder::ready() const{return impl_->ready();}
176 std::shared_ptr<core::mutable_audio_buffer> audio_decoder::poll() { return impl_->poll(); }
177 int     audio_decoder::num_channels() const { return impl_->codec_context_->channels; }
178 uint64_t audio_decoder::ffmpeg_channel_layout() const { return impl_->codec_context_->channel_layout; }
179 std::wstring audio_decoder::print() const{return impl_->print();}
180
181 }}