]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/video/video_decoder.cpp
2.0.0.2: ffmpeg_producer: Improved error-handling.
[casparcg] / modules / ffmpeg / producer / video / video_decoder.cpp
index 638ddd0b458ac4a39903c28c04bfe18be5db2717..50487bf52ceb4ef2bcf504ae652101cf74b4b962 100644 (file)
@@ -1,19 +1,36 @@
+/*\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
+#include "../../ffmpeg_error.h"\r
 \r
-#include <common/memory/safe_ptr.h>\r
+#include <common/memory/memcpy.h>\r
 \r
 #include <core/video_format.h>\r
-\r
 #include <core/producer/frame/basic_frame.h>\r
 #include <core/producer/frame/write_frame.h>\r
 #include <core/producer/frame/image_transform.h>\r
 \r
 #include <tbb/parallel_for.h>\r
 \r
-#include <algorithm>\r
-\r
 #if defined(_MSC_VER)\r
 #pragma warning (push)\r
 #pragma warning (disable : 4244)\r
@@ -35,17 +52,18 @@ core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)
 {\r
        switch(pix_fmt)\r
        {\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
+       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
 \r
@@ -60,6 +78,11 @@ core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width,
                \r
        switch(desc.pix_fmt)\r
        {\r
+       case core::pixel_format::gray:\r
+               {\r
+                       desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 1));                                             \r
+                       return desc;\r
+               }\r
        case core::pixel_format::bgra:\r
        case core::pixel_format::argb:\r
        case core::pixel_format::rgba:\r
@@ -91,15 +114,13 @@ core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width,
 \r
 struct video_decoder::implementation : boost::noncopyable\r
 {      \r
-       std::shared_ptr<core::frame_factory> frame_factory_;\r
-       std::shared_ptr<SwsContext> sws_context_;\r
-\r
-       AVCodecContext* codec_context_;\r
-\r
-       const int width_;\r
-       const int height_;\r
-       const PixelFormat pix_fmt_;\r
-       core::pixel_format_desc desc_;\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
@@ -110,11 +131,6 @@ public:
                , pix_fmt_(codec_context_->pix_fmt)\r
                , desc_(get_pixel_format_desc(pix_fmt_, width_, height_))\r
        {\r
-               double frame_time = static_cast<double>(codec_context_->time_base.num) / static_cast<double>(codec_context_->time_base.den);\r
-               double format_frame_time = 1.0/frame_factory->get_video_format_desc().fps;\r
-               if(abs(frame_time - format_frame_time) > 0.0001)\r
-                       BOOST_THROW_EXCEPTION(file_read_error() << msg_info("Invalid video framerate.") << arg_value_info(boost::lexical_cast<std::string>(frame_time)));\r
-\r
                if(desc_.pix_fmt == core::pixel_format::invalid)\r
                {\r
                        CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";\r
@@ -129,17 +145,35 @@ public:
                }\r
        }\r
        \r
-       safe_ptr<core::write_frame> execute(const aligned_buffer& video_packet)\r
+       std::shared_ptr<core::write_frame> execute(void* tag, const std::shared_ptr<aligned_buffer>& video_packet)\r
        {                               \r
+               if(!video_packet)\r
+                       return nullptr;\r
+\r
+               if(video_packet->empty()) // Need to flush\r
+               {\r
+                       avcodec_flush_buffers(codec_context_);\r
+                       return nullptr;\r
+               }\r
+\r
                safe_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
 \r
                int frame_finished = 0;\r
-               const int result = avcodec_decode_video(codec_context_, decoded_frame.get(), &frame_finished, video_packet.data(), video_packet.size());\r
+               const int errn = avcodec_decode_video(codec_context_, decoded_frame.get(), &frame_finished, video_packet->data(), video_packet->size());\r
                \r
-               if(result < 0)\r
-                       BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("avcodec_decode_video failed"));\r
+               if(errn < 0)\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
+               }\r
                \r
-               auto write = frame_factory_->create_frame(desc_);\r
+               if(frame_finished == 0)\r
+                       return nullptr;\r
+\r
+               auto write = frame_factory_->create_frame(tag, desc_);\r
                if(sws_context_ == nullptr)\r
                {\r
                        tbb::parallel_for(0, static_cast<int>(desc_.planes.size()), 1, [&](int n)\r
@@ -149,14 +183,16 @@ public:
                                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(0, static_cast<int>(desc_.planes[n].height), 1, [&](int y)\r
                                {\r
-                                       std::copy_n(decoded + y*decoded_linesize, plane.linesize, result + y*plane.linesize);\r
+                                       fast_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);\r
                                });\r
                        });\r
                }\r
                else\r
                {\r
+                       // Uses sws_scale when we don't support the provided colorspace.\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
@@ -164,6 +200,7 @@ public:
                        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
@@ -172,6 +209,6 @@ public:
 };\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
-safe_ptr<core::write_frame> video_decoder::execute(const aligned_buffer& video_packet){return impl_->execute(video_packet);}\r
+std::shared_ptr<core::write_frame> video_decoder::execute(void* tag, const std::shared_ptr<aligned_buffer>& video_packet){return impl_->execute(tag, video_packet);}\r
 \r
 }
\ No newline at end of file