--- /dev/null
+#include "../../stdafx.h"\r
+\r
+#include "audio_decoder.h"\r
+\r
+#if defined(_MSC_VER)\r
+#pragma warning (push)\r
+#pragma warning (disable : 4244)\r
+#endif\r
+extern "C" \r
+{\r
+ #define __STDC_CONSTANT_MACROS\r
+ #define __STDC_LIMIT_MACROS\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 {\r
+\r
+struct audio_decoder::implementation : boost::noncopyable\r
+{\r
+ typedef std::vector<short, tbb::cache_aligned_allocator<short>> buffer;\r
+ \r
+ AVCodecContext* codec_context_;\r
+\r
+ buffer audio_buffer_; \r
+ buffer current_chunk_;\r
+\r
+ const size_t audio_frame_size_;\r
+\r
+ static const size_t SAMPLE_RATE = 48000;\r
+ static const size_t N_CHANNELS = 2;\r
+\r
+public:\r
+ explicit implementation(AVCodecContext* codec_context, double fps) \r
+ : codec_context_(codec_context)\r
+ , audio_buffer_(4*SAMPLE_RATE*2+FF_INPUT_BUFFER_PADDING_SIZE/2)\r
+ , audio_frame_size_(static_cast<size_t>(static_cast<double>(SAMPLE_RATE) / fps) * N_CHANNELS)\r
+ {\r
+ if(!codec_context)\r
+ BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("codec_context")); \r
+ }\r
+ \r
+ std::vector<std::vector<short>> execute(const aligned_buffer& audio_packet)\r
+ { \r
+ int written_bytes = audio_buffer_.size()*2 - FF_INPUT_BUFFER_PADDING_SIZE;\r
+ const int result = avcodec_decode_audio2(codec_context_, audio_buffer_.data(), &written_bytes, audio_packet.data(), audio_packet.size());\r
+\r
+ if(result <= 0 || codec_context_->sample_rate != SAMPLE_RATE || codec_context_->channels != 2)\r
+ BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Invalid audio stream"));\r
+ \r
+ current_chunk_.insert(current_chunk_.end(), audio_buffer_.data(), audio_buffer_.data() + written_bytes/2);\r
+\r
+ std::vector<std::vector<short>> chunks_;\r
+ \r
+ const auto last = current_chunk_.end() - current_chunk_.size() % audio_frame_size_;\r
+\r
+ for(auto it = current_chunk_.begin(); it != last; it += audio_frame_size_) \r
+ chunks_.push_back(std::vector<short>(it, it + audio_frame_size_)); \r
+\r
+ current_chunk_.erase(current_chunk_.begin(), last);\r
+ \r
+ return chunks_;\r
+ }\r
+};\r
+\r
+audio_decoder::audio_decoder(AVCodecContext* codec_context, double fps) : impl_(new implementation(codec_context, fps)){}\r
+std::vector<std::vector<short>> audio_decoder::execute(const aligned_buffer& audio_packet){return impl_->execute(audio_packet);}\r
+}
\ No newline at end of file
--- /dev/null
+#pragma once\r
+\r
+#include <tbb/cache_aligned_allocator.h>\r
+\r
+#include <boost/noncopyable.hpp>\r
+\r
+#include <memory>\r
+#include <vector>\r
+\r
+struct AVCodecContext;\r
+\r
+namespace caspar {\r
+ \r
+typedef std::vector<unsigned char, tbb::cache_aligned_allocator<unsigned char>> aligned_buffer;\r
+\r
+class audio_decoder : boost::noncopyable\r
+{\r
+public:\r
+ explicit audio_decoder(AVCodecContext* codec_context, double fps);\r
+ std::vector<std::vector<short>> execute(const aligned_buffer& audio_packet);\r
+private:\r
+ struct implementation;\r
+ std::shared_ptr<implementation> impl_;\r
+};\r
+\r
+}
\ No newline at end of file