]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/audio/audio_decoder.cpp
2.0. audio: Audio pipeline is now in 32 bit.
[casparcg] / modules / ffmpeg / producer / audio / audio_decoder.cpp
index d8eac2bb99b7ff51a1789ea8f5babae5d975b2b4..dd0a9e58675e55e488a5775fa716183d137fbed0 100644 (file)
 \r
 #include "audio_decoder.h"\r
 \r
+#include "audio_resampler.h"\r
+\r
+#include "../../ffmpeg_error.h"\r
+\r
+#include <core/video_format.h>\r
+\r
+#include <tbb/cache_aligned_allocator.h>\r
+\r
+#include <queue>\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
@@ -37,94 +45,125 @@ extern "C"
 #endif\r
 \r
 namespace caspar {\r
-\r
+       \r
 struct audio_decoder::implementation : boost::noncopyable\r
 {      \r
-       input&                                                  input_;\r
-       AVCodecContext&                                 codec_context_;         \r
-       const core::video_format_desc   format_desc_;\r
-       std::vector<short>                              current_chunk_;\r
-       std::vector<std::vector<short>> chunks_;                \r
-       size_t                                                  frame_number_;\r
-       bool                                                    restarting_;\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
+       std::queue<std::shared_ptr<AVPacket>>                                           packets_;\r
+\r
+       int64_t                                                                                                         nb_frames_;\r
 public:\r
-       explicit implementation(input& input, const core::video_format_desc& format_desc) \r
-               : input_(input)\r
-               , codec_context_(*input_.get_audio_codec_context())\r
-               , format_desc_(format_desc)     \r
-               , frame_number_(0)\r
-               , restarting_(false)\r
-       {\r
-               if(codec_context_.sample_rate != static_cast<int>(format_desc_.audio_sample_rate) || \r
-                  codec_context_.channels != static_cast<int>(format_desc_.audio_channels))\r
-               {       \r
-                       BOOST_THROW_EXCEPTION(\r
-                               file_read_error()  <<\r
-                               msg_info("Invalid sample-rate or number of channels.") <<\r
-                               arg_value_info(boost::lexical_cast<std::string>(codec_context_.sample_rate)) << \r
-                               arg_name_info("codec_context"));\r
+       explicit implementation(const safe_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc) \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
-       }\r
-               \r
-       std::deque<std::pair<int, std::vector<short>>> receive()\r
-       {\r
-               std::deque<std::pair<int, std::vector<short>>> result;\r
-               \r
-               std::shared_ptr<AVPacket> pkt;\r
-               for(int n = 0; n < 32 && result.empty() && input_.try_pop_audio_packet(pkt); ++n)       \r
-                       result = decode(pkt);\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
-               return result;\r
+                       CASPAR_LOG_CURRENT_EXCEPTION();\r
+                       CASPAR_LOG(warning) << "[audio_decoder] Failed to open audio-stream. Running without audio.";                   \r
+               }\r
        }\r
 \r
-       std::deque<std::pair<int, std::vector<short>>> decode(const std::shared_ptr<AVPacket>& audio_packet)\r
+       void push(const std::shared_ptr<AVPacket>& packet)\r
        {                       \r
-               std::deque<std::pair<int, std::vector<short>>> result;\r
+               if(packet && packet->stream_index != index_)\r
+                       return;\r
 \r
-               if(!audio_packet)\r
-               {       \r
-                       avcodec_flush_buffers(&codec_context_);\r
-                       current_chunk_.clear();\r
-                       frame_number_ = 0;\r
-                       restarting_ = false;\r
-                       return result;\r
-               }\r
+               packets_.push(packet);\r
+       }       \r
+       \r
+       std::vector<std::shared_ptr<std::vector<int32_t>>> poll()\r
+       {\r
+               std::vector<std::shared_ptr<std::vector<int32_t>>> result;\r
 \r
-               if(restarting_)\r
+               if(packets_.empty())\r
                        return result;\r
-                               \r
-               auto s = current_chunk_.size();\r
-               current_chunk_.resize(s + 4*format_desc_.audio_sample_rate*2+FF_INPUT_BUFFER_PADDING_SIZE/2, 0);\r
+\r
+               if(!codec_context_)\r
+                       return empty_poll();\r
                \r
-               int written_bytes = (current_chunk_.size() - s)*2 - FF_INPUT_BUFFER_PADDING_SIZE;\r
-               const int errn = avcodec_decode_audio3(&codec_context_, &current_chunk_[s], &written_bytes, audio_packet.get());\r
-               if(errn < 0)\r
-               {       \r
-                       BOOST_THROW_EXCEPTION(\r
-                               invalid_operation() <<\r
-                               boost::errinfo_api_function("avcodec_decode_audio2") <<\r
-                               boost::errinfo_errno(AVUNERROR(errn)));\r
+               auto packet = packets_.front();\r
+\r
+               if(packet)              \r
+               {\r
+                       result.push_back(decode(*packet));\r
+                       if(packet->size == 0)                                   \r
+                               packets_.pop();\r
                }\r
+               else                    \r
+               {       \r
+                       avcodec_flush_buffers(codec_context_.get());\r
+                       result.push_back(nullptr);\r
+                       packets_.pop();\r
+               }               \r
 \r
-               current_chunk_.resize(s + written_bytes/2);\r
+               return result;\r
+       }\r
+\r
+       std::vector<std::shared_ptr<std::vector<int32_t>>> empty_poll()\r
+       {\r
+               auto packet = packets_.front();\r
+               packets_.pop();\r
 \r
-               const auto last = current_chunk_.end() - current_chunk_.size() % format_desc_.audio_samples_per_frame;\r
+               if(!packet)                     \r
+                       return boost::assign::list_of(nullptr);\r
                \r
-               for(auto it = current_chunk_.begin(); it != last; it += format_desc_.audio_samples_per_frame)           \r
-                       result.push_back(std::make_pair(frame_number_++, std::vector<short>(it, it + format_desc_.audio_samples_per_frame)));           \r
+               return boost::assign::list_of(std::make_shared<std::vector<int32_t>>(format_desc_.audio_samples_per_frame, 0)); \r
+       }\r
 \r
-               current_chunk_.erase(current_chunk_.begin(), last);\r
+       std::shared_ptr<std::vector<int32_t>> 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
-               return result;\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<std::vector<int32_t>>(samples, samples + n_samples);\r
        }\r
 \r
-       void restart()\r
+       bool ready() const\r
        {\r
-               restarting_ = true;\r
+               return !packets_.empty();\r
        }\r
 };\r
 \r
-audio_decoder::audio_decoder(input& input, const core::video_format_desc& format_desc) : impl_(new implementation(input, format_desc)){}\r
-std::deque<std::pair<int, std::vector<short>>> audio_decoder::receive(){return impl_->receive();}\r
-void audio_decoder::restart(){impl_->restart();}\r
+audio_decoder::audio_decoder(const safe_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc) : impl_(new implementation(context, format_desc)){}\r
+void audio_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
+bool audio_decoder::ready() const{return impl_->ready();}\r
+std::vector<std::shared_ptr<std::vector<int32_t>>> audio_decoder::poll(){return impl_->poll();}\r
+int64_t audio_decoder::nb_frames() const{return impl_->nb_frames_;}\r
 }
\ No newline at end of file