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