/*
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+* Copyright 2013 Sveriges Television AB http://casparcg.com/
*
* This file is part of CasparCG (www.casparcg.com).
*
#include "audio_decoder.h"
#include "../util/util.h"
-#include "../input/input.h"
#include "../../ffmpeg_error.h"
#include <core/video_format.h>
+#include <core/mixer/audio/audio_util.h>
-#include <common/log.h>
-
-#include <tbb/cache_aligned_allocator.h>
+#include <common/cache_aligned_vector.h>
#include <queue>
#pragma warning (push)
#pragma warning (disable : 4244)
#endif
-extern "C"
+extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#endif
namespace caspar { namespace ffmpeg {
-
-uint64_t get_channel_layout(AVCodecContext* dec)
-{
- 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);
- return layout;
-}
-
-struct audio_decoder::impl : boost::noncopyable
-{
- monitor::basic_subject event_subject_;
- input* input_;
- int index_;
- const spl::shared_ptr<AVCodecContext> codec_context_;
- const core::video_format_desc format_desc_;
- std::shared_ptr<SwrContext> swr_;
-
- std::vector<uint8_t, tbb::cache_aligned_allocator<int8_t>> buffer_;
+struct audio_decoder::implementation : boost::noncopyable
+{
+ int index_ = -1;
+ const spl::shared_ptr<AVCodecContext> codec_context_;
+ const int out_samplerate_;
+
+ cache_aligned_vector<int32_t> buffer_;
+
+ std::queue<spl::shared_ptr<AVPacket>> packets_;
+
+ std::shared_ptr<SwrContext> swr_ {
+ swr_alloc_set_opts(
+ nullptr,
+ codec_context_->channel_layout
+ ? codec_context_->channel_layout
+ : av_get_default_channel_layout(codec_context_->channels),
+ AV_SAMPLE_FMT_S32,
+ out_samplerate_,
+ codec_context_->channel_layout
+ ? codec_context_->channel_layout
+ : av_get_default_channel_layout(codec_context_->channels),
+ codec_context_->sample_fmt,
+ codec_context_->sample_rate,
+ 0,
+ nullptr),
+ [](SwrContext* p)
+ {
+ swr_free(&p);
+ }
+ };
- std::shared_ptr<AVPacket> current_packet_;
-
public:
- explicit impl(input& in, const core::video_format_desc& format_desc)
- : input_(&in)
- , format_desc_(format_desc)
- , codec_context_(open_codec(input_->context(), AVMEDIA_TYPE_AUDIO, index_))
- , swr_(swr_alloc_set_opts(nullptr,
- av_get_default_channel_layout(format_desc_.audio_channels), AV_SAMPLE_FMT_S32, format_desc_.audio_sample_rate,
- get_channel_layout(codec_context_.get()), codec_context_->sample_fmt, codec_context_->sample_rate,
- 0, nullptr), [](SwrContext* p){swr_free(&p);})
- , buffer_(AVCODEC_MAX_AUDIO_FRAME_SIZE*4)
- {
+ explicit implementation(const spl::shared_ptr<AVFormatContext>& context, int out_samplerate)
+ : codec_context_(open_codec(*context, AVMEDIA_TYPE_AUDIO, index_, false))
+ , out_samplerate_(out_samplerate)
+ , buffer_(10 * out_samplerate_ * codec_context_->channels) // 10 seconds of audio
+ {
if(!swr_)
CASPAR_THROW_EXCEPTION(bad_alloc());
THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]");
+
+ codec_context_->refcounted_frames = 1;
}
-
- std::shared_ptr<AVFrame> poll()
- {
- if(!current_packet_ && !input_->try_pop_audio(current_packet_))
+
+ void push(const std::shared_ptr<AVPacket>& packet)
+ {
+ if(!packet)
+ return;
+
+ if(packet->stream_index == index_ || packet->data == nullptr)
+ packets_.push(spl::make_shared_ptr(packet));
+ }
+
+ std::shared_ptr<core::mutable_audio_buffer> poll()
+ {
+ if(packets_.empty())
return nullptr;
-
- std::shared_ptr<AVFrame> audio;
- if(!current_packet_)
- {
- avcodec_flush_buffers(codec_context_.get());
- }
- else if(!current_packet_->data)
- {
- if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)
- audio = decode(*current_packet_);
-
- if(!audio)
- current_packet_.reset();
- }
- else
+ auto packet = packets_.front();
+
+ if(packet->data == nullptr)
{
- audio = decode(*current_packet_);
-
- if(current_packet_->size == 0)
- current_packet_.reset();
+ packets_.pop();
+ avcodec_flush_buffers(codec_context_.get());
+ return flush_audio();
}
-
- return audio ? audio : poll();
+
+ auto audio = decode(*packet);
+
+ if(packet->size == 0)
+ packets_.pop();
+
+ return audio;
}
- std::shared_ptr<AVFrame> decode(AVPacket& pkt)
- {
- auto frame = create_frame();
-
+ std::shared_ptr<core::mutable_audio_buffer> decode(AVPacket& pkt)
+ {
+ auto decoded_frame = create_frame();
+
int got_frame = 0;
- auto len = THROW_ON_ERROR2(avcodec_decode_audio4(codec_context_.get(), frame.get(), &got_frame, &pkt), "[audio_decoder]");
-
- if(len == 0)
+ auto len = THROW_ON_ERROR2(avcodec_decode_audio4(codec_context_.get(), decoded_frame.get(), &got_frame, &pkt), "[audio_decoder]");
+
+ if (len == 0)
{
pkt.size = 0;
return nullptr;
}
- pkt.data += len;
- pkt.size -= len;
+ pkt.data += len;
+ pkt.size -= len;
- if(!got_frame)
+ if (!got_frame)
return nullptr;
-
- const uint8_t *in[] = {frame->data[0]};
- uint8_t* out[] = {buffer_.data()};
-
- auto channel_samples = swr_convert(swr_.get(),
- out, static_cast<int>(buffer_.size()) / format_desc_.audio_channels / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32),
- in, frame->nb_samples);
-
- frame->data[0] = buffer_.data();
- frame->linesize[0] = channel_samples * format_desc_.audio_channels * av_get_bytes_per_sample(AV_SAMPLE_FMT_S32);
- frame->nb_samples = channel_samples;
- frame->format = AV_SAMPLE_FMT_S32;
-
- event_subject_ << monitor::event("file/audio/sample-rate") % codec_context_->sample_rate
- << monitor::event("file/audio/channels") % codec_context_->channels
- << monitor::event("file/audio/format") % u8(av_get_sample_fmt_name(codec_context_->sample_fmt))
- << monitor::event("file/audio/codec") % u8(codec_context_->codec->long_name);
-
- return frame;
+
+ const uint8_t **in = const_cast<const uint8_t**>(decoded_frame->extended_data);
+ uint8_t* out[] = { reinterpret_cast<uint8_t*>(buffer_.data()) };
+
+ const auto channel_samples = swr_convert(
+ swr_.get(),
+ out,
+ static_cast<int>(buffer_.size()) / codec_context_->channels,
+ in,
+ decoded_frame->nb_samples);
+
+ return std::make_shared<core::mutable_audio_buffer>(
+ buffer_.begin(),
+ buffer_.begin() + channel_samples * decoded_frame->channels);
}
-
- uint32_t nb_frames() const
+
+ bool ready() const
{
- return 0;
+ return packets_.size() > 10;
}
std::wstring print() const
- {
+ {
return L"[audio-decoder] " + u16(codec_context_->codec->long_name);
}
};
-audio_decoder::audio_decoder(input& input, const core::video_format_desc& format_desc) : impl_(new impl(input, format_desc)){}
-audio_decoder::audio_decoder(audio_decoder&& other) : impl_(std::move(other.impl_)){}
-audio_decoder& audio_decoder::operator=(audio_decoder&& other){impl_ = std::move(other.impl_); return *this;}
-std::shared_ptr<AVFrame> audio_decoder::operator()(){return impl_->poll();}
-uint32_t audio_decoder::nb_frames() const{return impl_->nb_frames();}
+audio_decoder::audio_decoder(const spl::shared_ptr<AVFormatContext>& context, int out_samplerate) : impl_(new implementation(context, out_samplerate)){}
+void audio_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}
+bool audio_decoder::ready() const{return impl_->ready();}
+std::shared_ptr<core::mutable_audio_buffer> audio_decoder::poll() { return impl_->poll(); }
+int audio_decoder::num_channels() const { return impl_->codec_context_->channels; }
+uint64_t audio_decoder::ffmpeg_channel_layout() const { return impl_->codec_context_->channel_layout; }
std::wstring audio_decoder::print() const{return impl_->print();}
-void audio_decoder::subscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.subscribe(o);}
-void audio_decoder::unsubscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.unsubscribe(o);}
-}}
\ No newline at end of file
+}}