]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/audio/audio_decoder.cpp
f94a39c35efed8f068b7760afd81b19921b5123f
[casparcg] / modules / ffmpeg / producer / audio / audio_decoder.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@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 "../input/input.h"
28 #include "../../ffmpeg_error.h"
29
30 #include <core/video_format.h>
31 #include <core/frame/audio_channel_layout.h>
32
33 #include <common/log.h>
34 #include <common/cache_aligned_vector.h>
35
36 #include <queue>
37
38 #if defined(_MSC_VER)
39 #pragma warning (push)
40 #pragma warning (disable : 4244)
41 #endif
42 extern "C" 
43 {
44         #include <libavformat/avformat.h>
45         #include <libavcodec/avcodec.h>
46         #include <libswresample/swresample.h>
47 }
48 #if defined(_MSC_VER)
49 #pragma warning (pop)
50 #endif
51
52 namespace caspar { namespace ffmpeg {
53         
54 uint64_t get_ffmpeg_channel_layout(AVCodecContext* dec)
55 {
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);
57         return layout;
58 }
59
60 struct audio_decoder::impl : boost::noncopyable
61 {       
62         core::monitor::subject                                                                          monitor_subject_;
63         input&                                                                                                          input_;
64         int                                                                                                                     index_;
65         const core::video_format_desc                                                           format_desc_;
66         const spl::shared_ptr<AVCodecContext>                                           codec_context_          = open_codec(input_.context(), AVMEDIA_TYPE_AUDIO, index_, false);
67
68         std::shared_ptr<SwrContext>                                                                     swr_                            {
69                                                                                                                                                                                 swr_alloc_set_opts(
70                                                                                                                                                                                                 nullptr,
71                                                                                                                                                                                                 create_channel_layout_bitmask(codec_context_->channels),//get_ffmpeg_channel_layout(codec_context_.get()),
72                                                                                                                                                                                                 AV_SAMPLE_FMT_S32,
73                                                                                                                                                                                                 format_desc_.audio_sample_rate,
74                                                                                                                                                                                                 create_channel_layout_bitmask(codec_context_->channels),//get_ffmpeg_channel_layout(codec_context_.get()),
75                                                                                                                                                                                                 codec_context_->sample_fmt,
76                                                                                                                                                                                                 codec_context_->sample_rate,
77                                                                                                                                                                                                 0,
78                                                                                                                                                                                                 nullptr),
79                                                                                                                                                                                 [](SwrContext* p){swr_free(&p); }
80                                                                                                                                                                         };
81
82         cache_aligned_vector<uint8_t>                                                           buffer_;
83         core::audio_channel_layout                                                                      channel_layout_;
84
85         std::shared_ptr<AVPacket>                                                                       current_packet_;
86         
87 public:
88         explicit impl(input& in, const core::video_format_desc& format_desc, const std::wstring& channel_layout_spec) 
89                 : input_(in)
90                 , format_desc_(format_desc)
91                 , buffer_(480000 * 4)
92                 , channel_layout_(get_audio_channel_layout(*codec_context_, channel_layout_spec))
93         {
94                 if(!swr_)
95                         CASPAR_THROW_EXCEPTION(bad_alloc());
96
97                 THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]");
98         }
99                 
100         std::shared_ptr<AVFrame> poll()
101         {               
102                 if(!current_packet_ && !input_.try_pop_audio(current_packet_))
103                         return nullptr;
104                 
105                 std::shared_ptr<AVFrame> audio;
106
107                 if(!current_packet_)    
108                 {
109                         avcodec_flush_buffers(codec_context_.get());    
110                 }
111                 else if(!current_packet_->data)
112                 {
113                         if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)                       
114                                 audio = decode(*current_packet_);
115                         
116                         if(!audio)
117                                 current_packet_.reset();
118                 }
119                 else
120                 {
121                         audio = decode(*current_packet_);
122                         
123                         if(current_packet_->size == 0)
124                                 current_packet_.reset();
125                 }
126         
127                 return audio;
128         }
129
130         std::shared_ptr<AVFrame> decode(AVPacket& pkt)
131         {               
132                 auto frame = create_frame();
133                 
134                 int got_frame = 0;
135                 auto len = THROW_ON_ERROR2(avcodec_decode_audio4(codec_context_.get(), frame.get(), &got_frame, &pkt), "[audio_decoder]");
136                                         
137                 if(len == 0)
138                 {
139                         pkt.size = 0;
140                         return nullptr;
141                 }
142
143         pkt.data += len;
144         pkt.size -= len;
145
146                 if(!got_frame)
147                         return nullptr;
148                                                         
149                 const uint8_t **in      = const_cast<const uint8_t**>(frame->extended_data);
150                 uint8_t* out[]          = {buffer_.data()};
151
152                 auto channel_samples = swr_convert(swr_.get(), 
153                                                                                         out, static_cast<int>(buffer_.size()) / codec_context_->channels / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32),
154                                                                                         in, frame->nb_samples);
155
156                 frame->data[0]          = buffer_.data();
157                 frame->linesize[0]      = channel_samples * codec_context_->channels * av_get_bytes_per_sample(AV_SAMPLE_FMT_S32);
158                 frame->nb_samples       = channel_samples;
159                 frame->format           = AV_SAMPLE_FMT_S32;
160
161                 monitor_subject_  << core::monitor::message("/file/audio/sample-rate")  % codec_context_->sample_rate
162                                                 << core::monitor::message("/file/audio/channels")       % codec_context_->channels
163                                                 << core::monitor::message("/file/audio/format")         % u8(av_get_sample_fmt_name(codec_context_->sample_fmt))
164                                                 << core::monitor::message("/file/audio/codec")          % u8(codec_context_->codec->long_name);                 
165
166                 return frame;
167         }
168         
169         uint32_t nb_frames() const
170         {
171                 return 0;
172         }
173
174         std::wstring print() const
175         {               
176                 return L"[audio-decoder] " + u16(codec_context_->codec->long_name);
177         }
178 };
179
180 audio_decoder::audio_decoder(input& input, const core::video_format_desc& format_desc, const std::wstring& channel_layout_spec) : impl_(new impl(input, format_desc, channel_layout_spec)){}
181 audio_decoder::audio_decoder(audio_decoder&& other) : impl_(std::move(other.impl_)){}
182 audio_decoder& audio_decoder::operator=(audio_decoder&& other){impl_ = std::move(other.impl_); return *this;}
183 std::shared_ptr<AVFrame> audio_decoder::operator()(){return impl_->poll();}
184 uint32_t audio_decoder::nb_frames() const{return impl_->nb_frames();}
185 const core::audio_channel_layout& audio_decoder::channel_layout() const { return impl_->channel_layout_; }
186 std::wstring audio_decoder::print() const{return impl_->print();}
187 core::monitor::subject& audio_decoder::monitor_output() { return impl_->monitor_subject_;}
188
189 }}