]> 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 9ce8f7e14a29ce1309e4bc44f97b61e30427b7fe..4e2ba9664fb22d640905e8b52af3b6ac04de1e31 100644 (file)
 #include "../../stdafx.h"\r
 \r
 #include "video_decoder.h"\r
-#include "../../ffmpeg_error.h"\r
 \r
-#include <common/memory/memcpy.h>\r
+#include "../util.h"\r
+#include "../filter/filter.h"\r
+\r
+#include "../../ffmpeg_error.h"\r
+#include "../../tbb_avcodec.h"\r
 \r
-#include <core/video_format.h>\r
-#include <core/producer/frame/basic_frame.h>\r
-#include <core/mixer/write_frame.h>\r
-#include <core/producer/frame/image_transform.h>\r
-#include <core/producer/frame/pixel_format.h>\r
+#include <core/producer/frame/frame_transform.h>\r
 #include <core/producer/frame/frame_factory.h>\r
 \r
-#include <tbb/parallel_for.h>\r
+#include <boost/range/algorithm_ext/push_back.hpp>\r
+#include <boost/filesystem.hpp>\r
+\r
+#include <queue>\r
 \r
 #if defined(_MSC_VER)\r
 #pragma warning (push)\r
 #endif\r
 extern "C" \r
 {\r
-       #define __STDC_CONSTANT_MACROS\r
-       #define __STDC_LIMIT_MACROS\r
-       #include <libswscale/swscale.h>\r
-       #include <libavformat/avformat.h>\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 {\r
+namespace caspar { namespace ffmpeg {\r
        \r
-core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)\r
+struct video_decoder::implementation : boost::noncopyable\r
 {\r
-       switch(pix_fmt)\r
-       {\r
-       case PIX_FMT_GRAY8:             return core::pixel_format::gray;\r
-       case PIX_FMT_BGRA:              return core::pixel_format::bgra;\r
-       case PIX_FMT_ARGB:              return core::pixel_format::argb;\r
-       case PIX_FMT_RGBA:              return core::pixel_format::rgba;\r
-       case PIX_FMT_ABGR:              return core::pixel_format::abgr;\r
-       case PIX_FMT_YUV444P:   return core::pixel_format::ycbcr;\r
-       case PIX_FMT_YUV422P:   return core::pixel_format::ycbcr;\r
-       case PIX_FMT_YUV420P:   return core::pixel_format::ycbcr;\r
-       case PIX_FMT_YUV411P:   return core::pixel_format::ycbcr;\r
-       case PIX_FMT_YUV410P:   return core::pixel_format::ycbcr;\r
-       case PIX_FMT_YUVA420P:  return core::pixel_format::ycbcra;\r
-       default:                                return core::pixel_format::invalid;\r
-       }\r
-}\r
+       const safe_ptr<core::frame_factory>             frame_factory_;\r
+       std::shared_ptr<AVCodecContext>                 codec_context_;\r
+       int                                                                             index_;\r
 \r
-core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width, size_t height)\r
-{\r
-       // Get linesizes\r
-       AVPicture dummy_pict;   \r
-       avpicture_fill(&dummy_pict, nullptr, pix_fmt, width, height);\r
+       std::queue<std::shared_ptr<AVPacket>>   packets_;\r
 \r
-       core::pixel_format_desc desc;\r
-       desc.pix_fmt = get_pixel_format(pix_fmt);\r
-               \r
-       switch(desc.pix_fmt)\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 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
-       case core::pixel_format::gray:\r
+               try\r
                {\r
-                       desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 1));                                             \r
-                       return desc;\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
-       case core::pixel_format::bgra:\r
-       case core::pixel_format::argb:\r
-       case core::pixel_format::rgba:\r
-       case core::pixel_format::abgr:\r
+               catch(...)\r
                {\r
-                       desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));                                             \r
-                       return desc;\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
-       case core::pixel_format::ycbcr:\r
-       case core::pixel_format::ycbcra:\r
-               {               \r
-                       // Find chroma height\r
-                       size_t size2 = dummy_pict.data[2] - dummy_pict.data[1];\r
-                       size_t h2 = size2/dummy_pict.linesize[1];                       \r
-\r
-                       desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));\r
-                       desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));\r
-                       desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));\r
-\r
-                       if(desc.pix_fmt == core::pixel_format::ycbcra)                                          \r
-                               desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));       \r
-                       return desc;\r
-               }               \r
-       default:                \r
-               desc.pix_fmt = core::pixel_format::invalid;\r
-               return desc;\r
        }\r
-}\r
-\r
-struct video_decoder::implementation : boost::noncopyable\r
-{      \r
-       std::shared_ptr<SwsContext>                                     sws_context_;\r
-       const std::shared_ptr<core::frame_factory>      frame_factory_;\r
-       AVCodecContext&                                                         codec_context_;\r
-       const int                                                                       width_;\r
-       const int                                                                       height_;\r
-       const PixelFormat                                                       pix_fmt_;\r
-       core::pixel_format_desc                                         desc_;\r
 \r
-public:\r
-       explicit implementation(AVCodecContext& codec_context, const safe_ptr<core::frame_factory>& frame_factory) \r
-               : frame_factory_(frame_factory)\r
-               , codec_context_(codec_context)\r
-               , width_(codec_context_.width)\r
-               , height_(codec_context_.height)\r
-               , pix_fmt_(codec_context_.pix_fmt)\r
-               , desc_(get_pixel_format_desc(pix_fmt_, width_, height_))\r
+       void push(const std::shared_ptr<AVPacket>& packet)\r
        {\r
-               if(desc_.pix_fmt == core::pixel_format::invalid)\r
-               {\r
-                       CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";\r
-\r
-                       desc_ = get_pixel_format_desc(PIX_FMT_BGRA, width_, height_);\r
-                       double param;\r
-                       sws_context_.reset(sws_getContext(width_, height_, pix_fmt_, width_, height_, PIX_FMT_BGRA, SWS_BILINEAR, nullptr, nullptr, &param), sws_freeContext);\r
-                       if(!sws_context_)\r
-                               BOOST_THROW_EXCEPTION(operation_failed() <<\r
-                                                                         msg_info("Could not create software scaling context.") << \r
-                                                                         boost::errinfo_api_function("sws_getContext"));\r
-               }\r
+               if(packet && packet->stream_index != index_)\r
+                       return;\r
+\r
+               packets_.push(packet);\r
        }\r
-       \r
-       std::vector<safe_ptr<core::write_frame>> execute(packet&& video_packet)\r
-       {                               \r
-               std::vector<safe_ptr<core::write_frame>> result;\r
 \r
-               switch(video_packet.type)\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
+                       return empty_poll();\r
+\r
+               auto packet = packets_.front();\r
+                                       \r
+               if(packet)\r
+               {                       \r
+                       BOOST_FOREACH(auto& frame, decode(*packet))\r
+                               boost::range::push_back(result, filter_.execute(frame));\r
+\r
+                       if(packet->size == 0)\r
+                               packets_.pop();\r
+               }\r
+               else\r
                {\r
-               case flush_packet:\r
-                       avcodec_flush_buffers(&codec_context_);\r
-                       break;\r
-               case data_packet:               \r
-                       safe_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
-\r
-                       int frame_finished = 0;\r
-                       const int errn = avcodec_decode_video2(&codec_context_, decoded_frame.get(), &frame_finished, video_packet.av_packet.get());\r
-               \r
-                       if(errn < 0)\r
+                       if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
                        {\r
-                               BOOST_THROW_EXCEPTION(\r
-                                       invalid_operation() <<\r
-                                       msg_info(av_error_str(errn)) <<\r
-                                       boost::errinfo_api_function("avcodec_decode_video") <<\r
-                                       boost::errinfo_errno(AVUNERROR(errn)));\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(frame_finished != 0)         \r
-                               result.push_back(make_write_frame(decoded_frame));\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
-       safe_ptr<core::write_frame> make_write_frame(safe_ptr<AVFrame> decoded_frame)\r
-       {               \r
-               auto write = frame_factory_->create_frame(this, desc_);\r
-               if(sws_context_ == nullptr)\r
-               {\r
-                       tbb::parallel_for(0, static_cast<int>(desc_.planes.size()), 1, [&](int n)\r
-                       {\r
-                               auto plane            = desc_.planes[n];\r
-                               auto result           = write->image_data(n).begin();\r
-                               auto decoded          = decoded_frame->data[n];\r
-                               auto decoded_linesize = decoded_frame->linesize[n];\r
-                               \r
-                               // Copy line by line since ffmpeg sometimes pads each line.\r
-                               tbb::parallel_for(tbb::blocked_range<size_t>(0, static_cast<int>(desc_.planes[n].height)), [&](const tbb::blocked_range<size_t>& r)\r
-                               {\r
-                                       for(size_t y = r.begin(); y != r.end(); ++y)\r
-                                               memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);\r
-                               });\r
-                       });\r
-               }\r
-               else\r
-               {\r
-                       // Use sws_scale when provided colorspace has no hw-accel.\r
-                       safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);     \r
-                       avcodec_get_frame_defaults(av_frame.get());                     \r
-                       avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data().begin(), PIX_FMT_BGRA, width_, height_);\r
-                \r
-                       sws_scale(sws_context_.get(), decoded_frame->data, decoded_frame->linesize, 0, height_, av_frame->data, av_frame->linesize);    \r
-               }       \r
-\r
-               // DVVIDEO is in lower field. Make it upper field if needed.\r
-               if(codec_context_.codec_id == CODEC_ID_DVVIDEO && frame_factory_->get_video_format_desc().mode == core::video_mode::upper)\r
-                       write->get_image_transform().set_fill_translation(0.0f, 1.0/static_cast<double>(height_));\r
-\r
-               return write;\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
+               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
+               return std::vector<std::shared_ptr<AVFrame>>(1 + decoded_frame->repeat_pict/2, decoded_frame);\r
+       }\r
+       \r
+       bool ready() const\r
+       {\r
+               return !packets_.empty();\r
+       }\r
+       \r
+       double fps() const\r
+       {\r
+               return fps_;\r
        }\r
 };\r
 \r
-video_decoder::video_decoder(AVCodecContext& codec_context, const safe_ptr<core::frame_factory>& frame_factory) : impl_(new implementation(codec_context, frame_factory)){}\r
-std::vector<safe_ptr<core::write_frame>> video_decoder::execute(packet&& video_packet){return impl_->execute(std::move(video_packet));}\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
+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
+}}
\ No newline at end of file