]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/video/video_decoder.cpp
[ffmpeg] Copied flush logic when seeking from 2.0, as well as current frame in clip...
[casparcg] / modules / ffmpeg / producer / video / video_decoder.cpp
index cc0eaaef0d5303b0cd06cc3efd427af4488c442c..bc379a8c0b9378d1aab1517dbef8d9f9e2d1e1f8 100644 (file)
-/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
-*\r
-*  This file is part of CasparCG.\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
-*\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
-*/\r
-#include "../../stdafx.h"\r
-\r
-#include "video_decoder.h"\r
-\r
-#include "../util.h"\r
-#include "../filter/filter.h"\r
-\r
-#include "../../ffmpeg_error.h"\r
-#include "../../tbb_avcodec.h"\r
-\r
-#include <core/producer/frame/basic_frame.h>\r
-\r
-#if defined(_MSC_VER)\r
-#pragma warning (push)\r
-#pragma warning (disable : 4244)\r
-#endif\r
-extern "C" \r
-{\r
-       #include <libavcodec/avcodec.h>\r
-       #include <libavformat/avformat.h>\r
-}\r
-#if defined(_MSC_VER)\r
-#pragma warning (pop)\r
-#endif\r
-\r
-namespace caspar { namespace ffmpeg {\r
-       \r
-struct video_decoder::implementation : public Concurrency::agent, boost::noncopyable\r
-{\r
-       video_decoder::token_t&                                 active_token_;\r
-       video_decoder::source_t&                                source_;\r
-       video_decoder::target_t&                                target_;\r
-\r
-       std::shared_ptr<AVCodecContext>                 codec_context_;\r
-       int                                                                             index_;\r
-\r
-       filter                                                                  filter_;\r
-\r
-       double                                                                  fps_;\r
-       int64_t                                                                 nb_frames_;\r
-\r
-       size_t                                                                  width_;\r
-       size_t                                                                  height_;\r
-       bool                                                                    is_progressive_;\r
-\r
-public:\r
-       explicit implementation(video_decoder::token_t& active_token,\r
-                                                       video_decoder::source_t& source,\r
-                                                       video_decoder::target_t& target,\r
-                                                       const safe_ptr<AVFormatContext>& context,\r
-                                                       double fps,\r
-                                                       const std::wstring& filter) \r
-               : active_token_(active_token)\r
-               , source_(source)\r
-               , target_(target)\r
-               , filter_(filter.empty() ? L"copy" : filter)\r
-               , fps_(fps)\r
-               , nb_frames_(0)\r
-               , width_(0)\r
-               , height_(0)\r
-               , is_progressive_(true)\r
-       {\r
-               try\r
-               {\r
-                       AVCodec* dec;\r
-                       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-stream. Running without video.";   \r
-               }\r
-\r
-               start();\r
-       }\r
-       \r
-       ~implementation()\r
-       {\r
-               agent::wait(this);\r
-       }\r
-       \r
-       virtual void run()\r
-       {\r
-               try\r
-               {\r
-                       while(Concurrency::receive(active_token_))\r
-                       {\r
-                               auto packet = Concurrency::receive(source_);\r
-                               if(packet == eof_packet())\r
-                               {\r
-                                       Concurrency::send(target_, eof_video());\r
-                                       break;\r
-                               }\r
-\r
-                               if(packet == loop_packet())\r
-                               {       \r
-                                       if(codec_context_)\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
-                               \r
-                                                       BOOST_FOREACH(auto& frame1, decode(pkt))\r
-                                                       {\r
-                                                               BOOST_FOREACH(auto& frame2, filter_.execute(frame1))\r
-                                                                       Concurrency::send(target_, std::shared_ptr<AVFrame>(frame2));\r
-                                                       }       \r
-                                               }\r
-\r
-                                               avcodec_flush_buffers(codec_context_.get());\r
-                                       }\r
-\r
-                                       Concurrency::send(target_, loop_video());       \r
-                               }\r
-                               else if(!codec_context_)\r
-                               {\r
-                                       Concurrency::send(target_, empty_video());      \r
-                               }\r
-                               else\r
-                               {\r
-                                       while(packet->size > 0)\r
-                                       {\r
-                                               BOOST_FOREACH(auto& frame1, decode(*packet))\r
-                                               {\r
-                                                       BOOST_FOREACH(auto& frame2, filter_.execute(frame1))\r
-                                                               Concurrency::send(target_, std::shared_ptr<AVFrame>(frame2));\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-               catch(...)\r
-               {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-               }\r
-\r
-               std::shared_ptr<AVPacket> packet;\r
-               Concurrency::try_receive(source_, packet);              \r
-               \r
-               done();\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
-               int frame_finished = 0;\r
-               THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt), "[video_decocer]");\r
-               \r
-               // If a decoder consumes less then the whole packet then something is wrong\r
-               // that might be just harmless padding at the end, or a problem with the\r
-               // AVParser or demuxer which puted more then one frame in a AVPacket.\r
-               pkt.data = nullptr;\r
-               pkt.size = 0;\r
-\r
-               if(frame_finished == 0) \r
-                       return std::vector<std::shared_ptr<AVFrame>>();\r
-\r
-               if(decoded_frame->repeat_pict % 2 > 0)\r
-                       CASPAR_LOG(warning) << "[video_decoder]: Field repeat_pict not implemented.";\r
-               \r
-               is_progressive_ = decoded_frame->interlaced_frame == 0;\r
-\r
-               return std::vector<std::shared_ptr<AVFrame>>(1 + decoded_frame->repeat_pict/2, decoded_frame);\r
-       }\r
-               \r
-       double fps() const\r
-       {\r
-               return fps_;\r
-       }\r
-};\r
-\r
-video_decoder::video_decoder(token_t& active_token,\r
-                                                        source_t& source,\r
-                                                        target_t& target,\r
-                                                        const safe_ptr<AVFormatContext>& context, \r
-                                                        double fps, \r
-                                                        const std::wstring& filter) \r
-       : impl_(new implementation(active_token, source, target, context, fps, filter))\r
-{\r
-}\r
-\r
-double video_decoder::fps() const{return impl_->fps();}\r
-int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}\r
-size_t video_decoder::width() const{return impl_->width_;}\r
-size_t video_decoder::height() const{return impl_->height_;}\r
-bool video_decoder::is_progressive() const{return impl_->is_progressive_;}\r
-\r
-}}
\ No newline at end of file
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../../StdAfx.h"
+
+#include "video_decoder.h"
+
+#include "../util/util.h"
+#include "../input/input.h"
+
+#include "../../ffmpeg_error.h"
+
+#include <common/log.h>
+#include <core/frame/frame_transform.h>
+#include <core/frame/frame_factory.h>
+
+#include <boost/filesystem.hpp>
+
+#include <tbb/atomic.h>
+
+#include <queue>
+
+#if defined(_MSC_VER)
+#pragma warning (push)
+#pragma warning (disable : 4244)
+#endif
+extern "C" 
+{
+       #include <libavcodec/avcodec.h>
+       #include <libavformat/avformat.h>
+}
+#if defined(_MSC_VER)
+#pragma warning (pop)
+#endif
+
+namespace caspar { namespace ffmpeg {
+       
+struct video_decoder::impl : boost::noncopyable
+{
+       core::monitor::subject                                  monitor_subject_;
+       input*                                                                  input_;
+       int                                                                             index_                          = -1;
+       const spl::shared_ptr<AVCodecContext>   codec_context_;
+
+       std::queue<spl::shared_ptr<AVPacket>>   packets_;
+       
+       const AVStream*                                                 stream_;
+       const uint32_t                                                  nb_frames_;
+       const int                                                               width_;
+       const int                                                               height_;
+
+       tbb::atomic<bool>                                               is_progressive_;
+       tbb::atomic<uint32_t>                                   file_frame_number_;
+       boost::rational<int>                                    framerate_;
+       
+       std::shared_ptr<AVPacket>                               current_packet_;
+
+public:
+       explicit impl(input& in, bool single_threaded)
+               : input_(&in)
+               , codec_context_(open_codec(input_->context(), AVMEDIA_TYPE_VIDEO, index_, single_threaded))
+               , stream_(input_->context().streams[index_])
+               , nb_frames_(static_cast<uint32_t>(stream_->nb_frames))
+               , width_(codec_context_->width)
+               , height_(codec_context_->height)
+               , framerate_(read_framerate(input_->context(), 0))
+       {
+               is_progressive_ = false;
+               file_frame_number_ = 0;
+       }
+       
+       std::shared_ptr<AVFrame> poll()
+       {                       
+               if(!current_packet_ && !input_->try_pop_video(current_packet_))
+                       return nullptr;
+               
+               std::shared_ptr<AVFrame> frame;
+
+               if (!current_packet_->data)
+               {
+                       if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)                       
+                               frame = decode(*current_packet_);
+                       
+                       if (!frame)
+                       {
+                               file_frame_number_ = static_cast<uint32_t>(current_packet_->pos);
+                               avcodec_flush_buffers(codec_context_.get());
+                               current_packet_.reset();
+                               frame = flush();
+                       }
+               }
+               else
+               {
+                       frame = decode(*current_packet_);
+                       
+                       if(current_packet_->size == 0)
+                               current_packet_.reset();
+               }
+                       
+               return frame;
+       }
+
+       std::shared_ptr<AVFrame> decode(AVPacket& pkt)
+       {
+               auto frame = create_frame();
+
+               int got_frame = 0;
+               auto len = THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), frame.get(), &got_frame, &pkt), "[video_decocer]");
+                               
+               if(len == 0)
+               {
+                       pkt.size = 0;
+                       return nullptr;
+               }
+
+        pkt.data += len;
+        pkt.size -= len;
+
+               if(got_frame == 0)      
+                       return nullptr;
+               
+               ++file_frame_number_;
+
+               is_progressive_ = !frame->interlaced_frame;
+               
+               if(frame->repeat_pict > 0)
+                       CASPAR_LOG(warning) << "[video_decoder] repeat_pict not implemented.";
+                               
+               monitor_subject_  << core::monitor::message("/file/video/width")        % width_
+                                               << core::monitor::message("/file/video/height") % height_
+                                               << core::monitor::message("/file/video/field")  % u8(!frame->interlaced_frame ? "progressive" : (frame->top_field_first ? "upper" : "lower"))
+                                               << core::monitor::message("/file/video/codec")  % u8(codec_context_->codec->long_name);
+               
+               return frame;
+       }
+       
+       uint32_t nb_frames() const
+       {
+               return std::max(nb_frames_, static_cast<uint32_t>(file_frame_number_));
+       }
+
+       std::wstring print() const
+       {               
+               return L"[video-decoder] " + u16(codec_context_->codec->long_name);
+       }
+};
+
+video_decoder::video_decoder(input& in, bool single_threaded) : impl_(new impl(in, single_threaded)){}
+video_decoder::video_decoder(video_decoder&& other) : impl_(std::move(other.impl_)){}
+video_decoder& video_decoder::operator=(video_decoder&& other){impl_ = std::move(other.impl_); return *this;}
+std::shared_ptr<AVFrame> video_decoder::operator()(){return impl_->poll();}
+int video_decoder::width() const{return impl_->width_;}
+int video_decoder::height() const{return impl_->height_;}
+uint32_t video_decoder::nb_frames() const{return impl_->nb_frames();}
+uint32_t video_decoder::file_frame_number() const{return impl_->file_frame_number_;}
+boost::rational<int> video_decoder::framerate() const { return impl_->framerate_; }
+bool video_decoder::is_progressive() const{return impl_->is_progressive_;}
+std::wstring video_decoder::print() const{return impl_->print();}
+core::monitor::subject& video_decoder::monitor_output() { return impl_->monitor_subject_; }
+}}