]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/video/video_decoder.cpp
2.0. Updated namespaces.
[casparcg] / modules / ffmpeg / producer / video / video_decoder.cpp
index d2827fa1e805ad376ce8c5f9544a10024525478d..4e2ba9664fb22d640905e8b52af3b6ac04de1e31 100644 (file)
 #include "../../ffmpeg_error.h"\r
 #include "../../tbb_avcodec.h"\r
 \r
-#include <core/producer/frame/image_transform.h>\r
+#include <core/producer/frame/frame_transform.h>\r
 #include <core/producer/frame/frame_factory.h>\r
 \r
 #include <boost/range/algorithm_ext/push_back.hpp>\r
-\r
-#include <tbb/task_group.h>\r
+#include <boost/filesystem.hpp>\r
 \r
 #include <queue>\r
 \r
@@ -42,8 +41,6 @@
 #endif\r
 extern "C" \r
 {\r
-       #define __STDC_CONSTANT_MACROS\r
-       #define __STDC_LIMIT_MACROS\r
        #include <libavcodec/avcodec.h>\r
        #include <libavformat/avformat.h>\r
 }\r
@@ -51,26 +48,32 @@ extern "C"
 #pragma warning (pop)\r
 #endif\r
 \r
-namespace caspar {\r
-               \r
+namespace caspar { namespace ffmpeg {\r
+       \r
 struct video_decoder::implementation : boost::noncopyable\r
 {\r
        const safe_ptr<core::frame_factory>             frame_factory_;\r
        std::shared_ptr<AVCodecContext>                 codec_context_;\r
        int                                                                             index_;\r
 \r
-       std::queue<std::shared_ptr<AVPacket>>   packet_buffer_;\r
+       std::queue<std::shared_ptr<AVPacket>>   packets_;\r
 \r
        filter                                                                  filter_;\r
 \r
        double                                                                  fps_;\r
        int64_t                                                                 nb_frames_;\r
+\r
+       size_t                                                                  width_;\r
+       size_t                                                                  height_;\r
+\r
 public:\r
-       explicit implementation(const std::shared_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter) \r
+       explicit implementation(const safe_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter) \r
                : frame_factory_(frame_factory)\r
                , filter_(filter)\r
                , fps_(frame_factory_->get_video_format_desc().fps)\r
                , nb_frames_(0)\r
+               , width_(0)\r
+               , height_(0)\r
        {\r
                try\r
                {\r
@@ -78,86 +81,100 @@ public:
                        index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0), "[video_decoder]");\r
                                                \r
                        THROW_ON_ERROR2(tbb_avcodec_open(context->streams[index_]->codec, dec), "[video_decoder]");\r
+                                                               \r
+                       codec_context_.reset(context->streams[index_]->codec, tbb_avcodec_close);\r
+               \r
+                       CASPAR_LOG(debug) << "[video_decoder] " << context->streams[index_]->codec->codec->long_name;\r
+\r
+                       // Some files give an invalid time_base numerator, try to fix it.\r
+\r
+                       fix_meta_data(*context);\r
+                       \r
+                       fps_ = static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num);\r
+                       nb_frames_ = context->streams[index_]->nb_frames;\r
+\r
+                       if(double_rate(filter))\r
+                               fps_ *= 2;\r
+\r
+                       width_  = codec_context_->width;\r
+                       height_ = codec_context_->height;\r
                }\r
                catch(...)\r
                {\r
+                       index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0), "[video_decoder]");\r
+\r
                        CASPAR_LOG_CURRENT_EXCEPTION();\r
-                       CASPAR_LOG(warning) << "[video_decoder] Failed to open video. Running without audio.";\r
-                       return;\r
+                       CASPAR_LOG(warning) << "[video_decoder] Failed to open video-stream. Running without video.";   \r
                }\r
-                                                               \r
-               codec_context_.reset(context->streams[index_]->codec, tbb_avcodec_close);\r
-               \r
-               // Some files give an invalid time_base numerator, try to fix it.\r
-               if(codec_context_ && codec_context_->time_base.num == 1)\r
-                       codec_context_->time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(codec_context_->time_base.den)))-1));    \r
-               \r
-               nb_frames_ = context->streams[index_]->nb_frames;\r
-               if(nb_frames_ == 0)\r
-                       nb_frames_ = context->streams[index_]->duration;// * context->streams[index_]->time_base.den;\r
-\r
-               fps_ = static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num);\r
-               if(double_rate(filter))\r
-                       fps_ *= 2;\r
        }\r
 \r
        void push(const std::shared_ptr<AVPacket>& packet)\r
        {\r
-               if(!codec_context_)\r
-                       return;\r
-\r
                if(packet && packet->stream_index != index_)\r
                        return;\r
 \r
-               packet_buffer_.push(packet);\r
+               packets_.push(packet);\r
        }\r
 \r
        std::vector<std::shared_ptr<AVFrame>> poll()\r
        {               \r
                std::vector<std::shared_ptr<AVFrame>> result;\r
 \r
+               if(packets_.empty())\r
+                       return result;\r
+\r
                if(!codec_context_)\r
-               {\r
-                       std::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);\r
-                       frame->data[0] = nullptr;\r
-                       result.push_back(frame);\r
-               }\r
-               else if(!packet_buffer_.empty())\r
-               {\r
-                       auto packet = packet_buffer_.front();\r
+                       return empty_poll();\r
+\r
+               auto packet = packets_.front();\r
                                        \r
-                       if(packet)\r
-                       {\r
-                               auto frame = decode(*packet);\r
+               if(packet)\r
+               {                       \r
+                       BOOST_FOREACH(auto& frame, decode(*packet))\r
                                boost::range::push_back(result, filter_.execute(frame));\r
-                               if(packet->size == 0)\r
-                                       packet_buffer_.pop();\r
-                       }\r
-                       else\r
+\r
+                       if(packet->size == 0)\r
+                               packets_.pop();\r
+               }\r
+               else\r
+               {\r
+                       if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
                        {\r
-                               if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
-                               {\r
-                                       AVPacket pkt;\r
-                                       av_init_packet(&pkt);\r
-                                       pkt.data = nullptr;\r
-                                       pkt.size = 0;\r
-                                       auto frame = decode(pkt);\r
+                               AVPacket pkt;\r
+                               av_init_packet(&pkt);\r
+                               pkt.data = nullptr;\r
+                               pkt.size = 0;\r
+\r
+                               BOOST_FOREACH(auto& frame, decode(pkt))\r
                                        boost::range::push_back(result, filter_.execute(frame));        \r
-                               }\r
-\r
-                               if(result.empty())\r
-                               {                                       \r
-                                       packet_buffer_.pop();\r
-                                       avcodec_flush_buffers(codec_context_.get());\r
-                                       result.push_back(nullptr);\r
-                               }\r
+                       }\r
+\r
+                       if(result.empty())\r
+                       {                                       \r
+                               packets_.pop();\r
+                               avcodec_flush_buffers(codec_context_.get());\r
+                               result.push_back(nullptr);\r
                        }\r
                }\r
                \r
                return result;\r
        }\r
 \r
-       std::shared_ptr<AVFrame> decode(AVPacket& pkt)\r
+       std::vector<std::shared_ptr<AVFrame>> empty_poll()\r
+       {                               \r
+               auto packet = packets_.front();\r
+               packets_.pop();\r
+\r
+               if(!packet)                     \r
+                       return boost::assign::list_of(nullptr);\r
+\r
+               std::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);\r
+               frame->data[0] = nullptr;\r
+\r
+               return boost::assign::list_of(frame);                                   \r
+       }\r
+\r
+       std::vector<std::shared_ptr<AVFrame>> decode(AVPacket& pkt)\r
        {\r
                std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
 \r
@@ -170,19 +187,18 @@ public:
                pkt.data = nullptr;\r
                pkt.size = 0;\r
 \r
-               if(frame_finished != 0) \r
-               {\r
-                       if(decoded_frame->repeat_pict != 0)\r
-                               CASPAR_LOG(warning) << "video_decoder: repeat_pict not implemented.";\r
-                       return decoded_frame;\r
-               }\r
+               if(frame_finished == 0) \r
+                       return std::vector<std::shared_ptr<AVFrame>>();\r
 \r
-               return nullptr;\r
+               if(decoded_frame->repeat_pict % 2 > 0)\r
+                       CASPAR_LOG(warning) << "[video_decoder]: Field repeat_pict not implemented.";\r
+               \r
+               return std::vector<std::shared_ptr<AVFrame>>(1 + decoded_frame->repeat_pict/2, decoded_frame);\r
        }\r
        \r
        bool ready() const\r
        {\r
-               return !codec_context_ || !packet_buffer_.empty();\r
+               return !packets_.empty();\r
        }\r
        \r
        double fps() const\r
@@ -191,10 +207,13 @@ public:
        }\r
 };\r
 \r
-video_decoder::video_decoder(const std::shared_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter) : impl_(new implementation(context, frame_factory, filter)){}\r
+video_decoder::video_decoder(const safe_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter) : impl_(new implementation(context, frame_factory, filter)){}\r
 void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
 std::vector<std::shared_ptr<AVFrame>> video_decoder::poll(){return impl_->poll();}\r
 bool video_decoder::ready() const{return impl_->ready();}\r
 double video_decoder::fps() const{return impl_->fps();}\r
 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}\r
-}
\ No newline at end of file
+size_t video_decoder::width() const{return impl_->width_;}\r
+size_t video_decoder::height() const{return impl_->height_;}\r
+\r
+}}
\ No newline at end of file