-/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG.\r
-*\r
-* CasparCG is free software: you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation, either version 3 of the License, or\r
-* (at your option) any later version.\r
-*\r
-* CasparCG is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-* GNU General Public License for more details.\r
-\r
-* You should have received a copy of the GNU General Public License\r
-* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-*/\r
-#include "../../stdafx.h"\r
-\r
-#include "audio_decoder.h"\r
-#include "audio_resampler.h"\r
-\r
-#include "../util.h"\r
-#include "../../ffmpeg_error.h"\r
-\r
-#include <core/video_format.h>\r
-\r
-#include <tbb/cache_aligned_allocator.h>\r
-\r
-#if defined(_MSC_VER)\r
-#pragma warning (push)\r
-#pragma warning (disable : 4244)\r
-#endif\r
-extern "C" \r
-{\r
- #include <libavformat/avformat.h>\r
- #include <libavcodec/avcodec.h>\r
-}\r
-#if defined(_MSC_VER)\r
-#pragma warning (pop)\r
-#endif\r
-\r
-namespace caspar { namespace ffmpeg {\r
- \r
-struct audio_decoder::implementation : public Concurrency::agent, boost::noncopyable\r
-{\r
- audio_decoder::source_t& source_;\r
- audio_decoder::target_t& target_;\r
-\r
- std::shared_ptr<AVCodecContext> codec_context_; \r
- const core::video_format_desc format_desc_;\r
- int index_;\r
- std::unique_ptr<audio_resampler> resampler_;\r
-\r
- std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>> buffer1_;\r
- \r
- int64_t nb_frames_;\r
-public:\r
- explicit implementation(audio_decoder::source_t& source,\r
- audio_decoder::target_t& target,\r
- const safe_ptr<AVFormatContext>& context, \r
- const core::video_format_desc& format_desc) \r
- : source_(source)\r
- , target_(target)\r
- , format_desc_(format_desc) \r
- , nb_frames_(0)\r
- { \r
- try\r
- {\r
- AVCodec* dec;\r
- index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0), "[audio_decoder]");\r
-\r
- THROW_ON_ERROR2(avcodec_open(context->streams[index_]->codec, dec), "[audio_decoder]");\r
- \r
- codec_context_.reset(context->streams[index_]->codec, avcodec_close);\r
-\r
- buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
-\r
- resampler_.reset(new audio_resampler(format_desc_.audio_channels, codec_context_->channels,\r
- format_desc_.audio_sample_rate, codec_context_->sample_rate,\r
- AV_SAMPLE_FMT_S32, codec_context_->sample_fmt)); \r
- }\r
- catch(...)\r
- {\r
- index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0), "[audio_decoder]");\r
-\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- CASPAR_LOG(warning) << "[audio_decoder] Failed to open audio-stream. Running without audio."; \r
- }\r
-\r
- start();\r
- }\r
-\r
- ~implementation()\r
- {\r
- agent::wait(this);\r
- } \r
-\r
- virtual void run()\r
- {\r
- try\r
- {\r
- while(true)\r
- {\r
- auto packet = Concurrency::receive(source_, 5000);\r
- if(packet == eof_packet()) \r
- break;\r
- \r
- if(packet == loop_packet()) \r
- { \r
- if(codec_context_)\r
- avcodec_flush_buffers(codec_context_.get());\r
- Concurrency::send(target_, loop_audio());\r
- } \r
- else if(packet->stream_index == index_)\r
- {\r
- if(!codec_context_)\r
- Concurrency::send(target_, empty_audio()); \r
- else \r
- Concurrency::send(target_, decode(*packet)); \r
- }\r
- }\r
- }\r
- catch(...)\r
- {\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- }\r
- \r
- Concurrency::send(target_, eof_audio());\r
-\r
- done();\r
- }\r
- \r
- std::shared_ptr<core::audio_buffer> decode(AVPacket& pkt)\r
- { \r
- buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
- int written_bytes = buffer1_.size() - FF_INPUT_BUFFER_PADDING_SIZE;\r
- \r
- int ret = THROW_ON_ERROR2(avcodec_decode_audio3(codec_context_.get(), reinterpret_cast<int16_t*>(buffer1_.data()), &written_bytes, &pkt), "[audio_decoder]");\r
-\r
- // There might be several frames in one packet.\r
- pkt.size -= ret;\r
- pkt.data += ret;\r
- \r
- buffer1_.resize(written_bytes);\r
-\r
- buffer1_ = resampler_->resample(std::move(buffer1_));\r
- \r
- const auto n_samples = buffer1_.size() / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32);\r
- const auto samples = reinterpret_cast<int32_t*>(buffer1_.data());\r
-\r
- return std::make_shared<core::audio_buffer>(samples, samples + n_samples);\r
- }\r
-};\r
-\r
-audio_decoder::audio_decoder(source_t& source,\r
- target_t& target,\r
- const safe_ptr<AVFormatContext>& context, \r
- const core::video_format_desc& format_desc)\r
- : impl_(new implementation(source, target, context, format_desc))\r
-{\r
-}\r
-int64_t audio_decoder::nb_frames() const{return impl_->nb_frames_;}\r
-\r
-}}
\ No newline at end of file
+/*
+* Copyright 2013 Sveriges Television AB http://casparcg.com/
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../../StdAfx.h"
+
+#include "audio_decoder.h"
+
+#include "../util/util.h"
+#include "../../ffmpeg_error.h"
+
+#include <core/video_format.h>
+#include <core/mixer/audio/audio_util.h>
+
+#include <common/cache_aligned_vector.h>
+
+#include <queue>
+
+#if defined(_MSC_VER)
+#pragma warning (push)
+#pragma warning (disable : 4244)
+#endif
+extern "C"
+{
+ #include <libavformat/avformat.h>
+ #include <libavcodec/avcodec.h>
+ #include <libswresample/swresample.h>
+}
+#if defined(_MSC_VER)
+#pragma warning (pop)
+#endif
+
+namespace caspar { namespace ffmpeg {
+
+struct audio_decoder::implementation : boost::noncopyable
+{
+ int index_;
+ 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);
+ }
+ };
+
+public:
+ explicit implementation(int stream_index, const spl::shared_ptr<AVFormatContext>& context, int out_samplerate)
+ : index_(stream_index)
+ , 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;
+ }
+
+ 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;
+
+ auto packet = packets_.front();
+
+ if(packet->data == nullptr)
+ {
+ packets_.pop();
+ avcodec_flush_buffers(codec_context_.get());
+ return flush_audio();
+ }
+
+ auto audio = decode(*packet);
+
+ if(packet->size == 0)
+ packets_.pop();
+
+ return audio;
+ }
+
+ 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(), decoded_frame.get(), &got_frame, &pkt), "[audio_decoder]");
+
+ if (len == 0)
+ {
+ pkt.size = 0;
+ return nullptr;
+ }
+
+ pkt.data += len;
+ pkt.size -= len;
+
+ if (!got_frame)
+ return nullptr;
+
+ 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);
+ }
+
+ bool ready() const
+ {
+ return packets_.size() > 10;
+ }
+
+ std::wstring print() const
+ {
+ return L"[audio-decoder] " + u16(codec_context_->codec->long_name);
+ }
+
+ uint64_t ffmpeg_channel_layout() const
+ {
+ if (codec_context_->channel_layout == 0)
+ return av_get_default_channel_layout(codec_context_->channels);
+ else
+ return codec_context_->channel_layout;
+ }
+};
+
+audio_decoder::audio_decoder(int stream_index, const spl::shared_ptr<AVFormatContext>& context, int out_samplerate) : impl_(new implementation(stream_index, 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_->ffmpeg_channel_layout(); }
+std::wstring audio_decoder::print() const{return impl_->print();}
+
+}}