2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../StdAfx.h"
24 #include "audio_decoder.h"
26 #include "../util/util.h"
27 #include "../input/input.h"
28 #include "../../ffmpeg_error.h"
30 #include <core/video_format.h>
31 #include <core/frame/audio_channel_layout.h>
33 #include <common/log.h>
34 #include <common/cache_aligned_vector.h>
39 #pragma warning (push)
40 #pragma warning (disable : 4244)
44 #include <libavformat/avformat.h>
45 #include <libavcodec/avcodec.h>
46 #include <libswresample/swresample.h>
52 namespace caspar { namespace ffmpeg {
54 uint64_t get_ffmpeg_channel_layout(AVCodecContext* dec)
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);
60 struct audio_decoder::impl : boost::noncopyable
62 core::monitor::subject monitor_subject_;
66 const core::video_format_desc format_desc_;
67 const spl::shared_ptr<AVCodecContext> codec_context_ = open_codec(input_.context(), AVMEDIA_TYPE_AUDIO, actual_index_, false);
69 std::shared_ptr<SwrContext> swr_ {
72 create_channel_layout_bitmask(codec_context_->channels),//get_ffmpeg_channel_layout(codec_context_.get()),
74 format_desc_.audio_sample_rate,
75 create_channel_layout_bitmask(codec_context_->channels),//get_ffmpeg_channel_layout(codec_context_.get()),
76 codec_context_->sample_fmt,
77 codec_context_->sample_rate,
80 [](SwrContext* p){swr_free(&p); }
83 cache_aligned_vector<uint8_t> buffer_;
85 std::shared_ptr<AVPacket> current_packet_;
90 const core::video_format_desc& format_desc,
91 int audio_stream_index)
93 , index_(audio_stream_index)
94 , actual_index_(input_.get_actual_audio_stream_index(index_))
95 , format_desc_(format_desc)
99 CASPAR_THROW_EXCEPTION(bad_alloc());
101 THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]");
104 std::shared_ptr<AVFrame> poll()
106 if(!current_packet_ && !input_.try_pop_audio(current_packet_, index_))
109 std::shared_ptr<AVFrame> audio;
113 avcodec_flush_buffers(codec_context_.get());
115 else if(!current_packet_->data)
117 if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)
118 audio = decode(*current_packet_);
121 current_packet_.reset();
125 audio = decode(*current_packet_);
127 if(current_packet_->size == 0)
128 current_packet_.reset();
134 std::shared_ptr<AVFrame> decode(AVPacket& pkt)
136 auto frame = create_frame();
139 auto len = THROW_ON_ERROR2(avcodec_decode_audio4(codec_context_.get(), frame.get(), &got_frame, &pkt), "[audio_decoder]");
153 const uint8_t **in = const_cast<const uint8_t**>(frame->extended_data);
154 uint8_t* out[] = {buffer_.data()};
156 auto channel_samples = swr_convert(swr_.get(),
157 out, static_cast<int>(buffer_.size()) / codec_context_->channels / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32),
158 in, frame->nb_samples);
160 frame->data[0] = buffer_.data();
161 frame->linesize[0] = channel_samples * codec_context_->channels * av_get_bytes_per_sample(AV_SAMPLE_FMT_S32);
162 frame->nb_samples = channel_samples;
163 frame->format = AV_SAMPLE_FMT_S32;
165 monitor_subject_ << core::monitor::message("/file/audio/sample-rate") % codec_context_->sample_rate
166 << core::monitor::message("/file/audio/channels") % codec_context_->channels
167 << core::monitor::message("/file/audio/format") % u8(av_get_sample_fmt_name(codec_context_->sample_fmt))
168 << core::monitor::message("/file/audio/codec") % u8(codec_context_->codec->long_name);
173 uint32_t nb_frames() const
178 std::wstring print() const
180 return L"[audio-decoder] " + u16(codec_context_->codec->long_name);
184 audio_decoder::audio_decoder(input& input, const core::video_format_desc& format_desc, int audio_stream_index) : impl_(new impl(input, format_desc, audio_stream_index)){}
185 audio_decoder::audio_decoder(audio_decoder&& other) : impl_(std::move(other.impl_)){}
186 audio_decoder& audio_decoder::operator=(audio_decoder&& other){impl_ = std::move(other.impl_); return *this;}
187 std::shared_ptr<AVFrame> audio_decoder::operator()(){return impl_->poll();}
188 uint32_t audio_decoder::nb_frames() const{return impl_->nb_frames();}
189 std::wstring audio_decoder::print() const{return impl_->print();}
190 core::monitor::subject& audio_decoder::monitor_output() { return impl_->monitor_subject_;}