]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/audio/audio_decoder.cpp
2.0.2: - graph: Fixed potential buffer overflow.
[casparcg] / modules / ffmpeg / producer / audio / audio_decoder.cpp
index dd0a9e58675e55e488a5775fa716183d137fbed0..1ac7fdeaa796fe61c3db3c1602f62b11e747ca5a 100644 (file)
@@ -1,28 +1,31 @@
 /*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
 *\r
-*  This file is part of CasparCG.\r
+* This file is part of CasparCG (www.casparcg.com).\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
+* 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
+* 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
+* Author: Robert Nagy, ronag89@gmail.com\r
 */\r
+\r
 #include "../../stdafx.h"\r
 \r
 #include "audio_decoder.h"\r
 \r
 #include "audio_resampler.h"\r
 \r
+#include "../util/util.h"\r
 #include "../../ffmpeg_error.h"\r
 \r
 #include <core/video_format.h>\r
@@ -44,97 +47,69 @@ 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::unique_ptr<audio_resampler>                                                        resampler_;\r
+       const safe_ptr<AVCodecContext>                                                          codec_context_;         \r
+       const core::video_format_desc                                                           format_desc_;\r
+\r
+       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
+       std::queue<safe_ptr<AVPacket>>                                                          packets_;\r
 \r
-       int64_t                                                                                                         nb_frames_;\r
+       const int64_t                                                                                           nb_frames_;\r
+       tbb::atomic<size_t>                                                                                     file_frame_number_;\r
 public:\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
-               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
+               , codec_context_(open_codec(*context, AVMEDIA_TYPE_AUDIO, index_))\r
+               , 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
+               , buffer1_(AVCODEC_MAX_AUDIO_FRAME_SIZE*2)\r
+               , nb_frames_(0)//context->streams[index_]->nb_frames)\r
+       {               \r
+               file_frame_number_ = 0;\r
+               CASPAR_LOG(debug) << "[audio_decoder] " << context->streams[index_]->codec->codec->long_name;      \r
        }\r
 \r
        void push(const std::shared_ptr<AVPacket>& packet)\r
        {                       \r
-               if(packet && packet->stream_index != index_)\r
+               if(!packet)\r
                        return;\r
 \r
-               packets_.push(packet);\r
+               if(packet->stream_index == index_ || packet->data == nullptr)\r
+                       packets_.push(make_safe_ptr(packet));\r
        }       \r
        \r
-       std::vector<std::shared_ptr<std::vector<int32_t>>> poll()\r
+       std::shared_ptr<core::audio_buffer> poll()\r
        {\r
-               std::vector<std::shared_ptr<std::vector<int32_t>>> result;\r
-\r
                if(packets_.empty())\r
-                       return result;\r
-\r
-               if(!codec_context_)\r
-                       return empty_poll();\r
-               \r
+                       return nullptr;\r
+                               \r
                auto packet = packets_.front();\r
 \r
-               if(packet)              \r
+               if(packet->data == nullptr)\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
+                       file_frame_number_ = static_cast<size_t>(packet->pos);\r
+                       avcodec_flush_buffers(codec_context_.get());\r
+                       return flush_audio();\r
+               }\r
 \r
-               return result;\r
-       }\r
+               auto audio = decode(*packet);\r
 \r
-       std::vector<std::shared_ptr<std::vector<int32_t>>> empty_poll()\r
-       {\r
-               auto packet = packets_.front();\r
-               packets_.pop();\r
+               if(packet->size == 0)                                   \r
+                       packets_.pop();\r
 \r
-               if(!packet)                     \r
-                       return boost::assign::list_of(nullptr);\r
-               \r
-               return boost::assign::list_of(std::make_shared<std::vector<int32_t>>(format_desc_.audio_samples_per_frame, 0)); \r
+               return audio;\r
        }\r
 \r
-       std::shared_ptr<std::vector<int32_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
@@ -147,23 +122,32 @@ public:
                        \r
                buffer1_.resize(written_bytes);\r
 \r
-               buffer1_ = resampler_->resample(std::move(buffer1_));\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
+               ++file_frame_number_;\r
+\r
+               return std::make_shared<core::audio_buffer>(samples, samples + n_samples);\r
        }\r
 \r
        bool ready() const\r
        {\r
-               return !packets_.empty();\r
+               return packets_.size() > 10;\r
+       }\r
+\r
+       uint32_t nb_frames() const\r
+       {\r
+               return 0;//std::max<int64_t>(nb_frames_, file_frame_number_);\r
        }\r
 };\r
 \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
+std::shared_ptr<core::audio_buffer> audio_decoder::poll(){return impl_->poll();}\r
+uint32_t audio_decoder::nb_frames() const{return impl_->nb_frames();}\r
+uint32_t audio_decoder::file_frame_number() const{return impl_->file_frame_number_;}\r
+\r
+}}
\ No newline at end of file