]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/audio/audio_decoder.cpp
2.0. Updated namespaces.
[casparcg] / modules / ffmpeg / producer / audio / audio_decoder.cpp
index ae9ef01c45b33e6e59e0c95cc279c706162442ab..3f5fe7f7269e8fa77ea1186d67ea16669c7aded0 100644 (file)
@@ -21,6 +21,8 @@
 \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
@@ -42,18 +44,17 @@ extern "C"
 #pragma warning (pop)\r
 #endif\r
 \r
-namespace caspar {\r
+namespace caspar { namespace ffmpeg {\r
        \r
 struct audio_decoder::implementation : boost::noncopyable\r
 {      \r
        std::shared_ptr<AVCodecContext>                                                         codec_context_;         \r
        const core::video_format_desc                                                           format_desc_;\r
        int                                                                                                                     index_;\r
-       std::shared_ptr<ReSampleContext>                                                        resampler_;\r
+       std::unique_ptr<audio_resampler>                                                        resampler_;\r
 \r
        std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>>      buffer1_;\r
-       std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>>      buffer2_;\r
-       std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>     audio_samples_; \r
+\r
        std::queue<std::shared_ptr<AVPacket>>                                           packets_;\r
 \r
        int64_t                                                                                                         nb_frames_;\r
@@ -73,27 +74,9 @@ public:
 \r
                        buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\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
-                          codec_context_->sample_fmt   != AV_SAMPLE_FMT_S16)\r
-                       {       \r
-                               auto resampler = av_audio_resample_init(format_desc_.audio_channels,    codec_context_->channels,\r
-                                                                                                               format_desc_.audio_sample_rate, codec_context_->sample_rate,\r
-                                                                                                               AV_SAMPLE_FMT_S16,                              codec_context_->sample_fmt,\r
-                                                                                                               16, 10, 0, 0.8);\r
-\r
-                               buffer2_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
-\r
-                               CASPAR_LOG(warning) << L"Invalid audio format. Resampling." <<\r
-                                                                          L" sample_rate:" << static_cast<int>(codec_context_->sample_rate)  <<\r
-                                                                          L" audio_channels:" << static_cast<int>(codec_context_->channels)  <<\r
-                                                                          L" sample_fmt:" << static_cast<int>(codec_context_->sample_fmt);\r
-\r
-                               if(resampler)\r
-                                       resampler_.reset(resampler, audio_resample_close);\r
-                               else\r
-                                       codec_context_ = nullptr;\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
@@ -112,9 +95,9 @@ public:
                packets_.push(packet);\r
        }       \r
        \r
-       std::vector<std::shared_ptr<std::vector<int16_t>>> poll()\r
+       std::vector<std::shared_ptr<core::audio_buffer>> poll()\r
        {\r
-               std::vector<std::shared_ptr<std::vector<int16_t>>> result;\r
+               std::vector<std::shared_ptr<core::audio_buffer>> result;\r
 \r
                if(packets_.empty())\r
                        return result;\r
@@ -140,7 +123,7 @@ public:
                return result;\r
        }\r
 \r
-       std::vector<std::shared_ptr<std::vector<int16_t>>> empty_poll()\r
+       std::vector<std::shared_ptr<core::audio_buffer>> empty_poll()\r
        {\r
                auto packet = packets_.front();\r
                packets_.pop();\r
@@ -148,33 +131,28 @@ public:
                if(!packet)                     \r
                        return boost::assign::list_of(nullptr);\r
                \r
-               return boost::assign::list_of(std::make_shared<std::vector<int16_t>>(format_desc_.audio_samples_per_frame, 0)); \r
+               return boost::assign::list_of(std::make_shared<core::audio_buffer>(format_desc_.audio_samples_per_frame, 0));   \r
        }\r
 \r
-       std::shared_ptr<std::vector<int16_t>> decode(AVPacket& pkt)\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
+               \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
-               if(resampler_)\r
-               {\r
-                       auto ret = audio_resample(resampler_.get(),\r
-                                                                               reinterpret_cast<short*>(buffer2_.data()), \r
-                                                                               reinterpret_cast<short*>(buffer1_.data()), \r
-                                                                               written_bytes / (av_get_bytes_per_sample(codec_context_->sample_fmt) * codec_context_->channels)); \r
-                       written_bytes = ret * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * format_desc_.audio_channels;\r
-                       std::swap(buffer1_, buffer2_);\r
-               }\r
+               buffer1_.resize(written_bytes);\r
 \r
-               const auto n_samples = written_bytes / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);\r
-               const auto samples = reinterpret_cast<int16_t*>(buffer1_.data());\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<int16_t>>(samples, samples + n_samples);\r
+               return std::make_shared<core::audio_buffer>(samples, samples + n_samples);\r
        }\r
 \r
        bool ready() const\r
@@ -186,6 +164,7 @@ public:
 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<int16_t>>> audio_decoder::poll(){return impl_->poll();}\r
+std::vector<std::shared_ptr<core::audio_buffer>> 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
+\r
+}}
\ No newline at end of file