]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/util/util.cpp
2.1.0: cpu/image_mixer: Added support for image conversion and blending. Only interla...
[casparcg] / modules / ffmpeg / producer / util / util.cpp
index 8db7236a99e7e42a3abe73550eb75a6492055ebe..f06e3084655a42edf6813978c37df3149d0c3e01 100644 (file)
 #include <tbb/concurrent_unordered_map.h>\r
 #include <tbb/concurrent_queue.h>\r
 \r
-#include <core/producer/frame/frame_transform.h>\r
-#include <core/producer/frame/frame_factory.h>\r
+#include <core/frame/frame_transform.h>\r
+#include <core/frame/frame_factory.h>\r
 #include <core/producer/frame_producer.h>\r
-#include <core/mixer/write_frame.h>\r
+#include <core/frame/write_frame.h>\r
 \r
-#include <common/exception/exceptions.h>\r
+#include <common/except.h>\r
 \r
 #include <tbb/parallel_for.h>\r
 \r
@@ -120,10 +120,9 @@ core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, int width, in
        AVPicture dummy_pict;   \r
        avpicture_fill(&dummy_pict, nullptr, pix_fmt == CASPAR_PIX_FMT_LUMA ? PIX_FMT_GRAY8 : pix_fmt, width, height);\r
 \r
-       core::pixel_format_desc desc;\r
-       desc.pix_fmt = get_pixel_format(pix_fmt);\r
+       core::pixel_format_desc desc = get_pixel_format(pix_fmt);\r
                \r
-       switch(desc.pix_fmt.value())\r
+       switch(desc.format.value())\r
        {\r
        case core::pixel_format::gray:\r
        case core::pixel_format::luma:\r
@@ -150,12 +149,12 @@ core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, int width, in
                        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
+                       if(desc.format == 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
+               desc.format = core::pixel_format::invalid;\r
                return desc;\r
        }\r
 }\r
@@ -172,12 +171,12 @@ int make_alpha_format(int format)
        }\r
 }\r
 \r
-safe_ptr<core::write_frame> make_write_frame(const void* tag, const safe_ptr<AVFrame>& decoded_frame, const safe_ptr<core::frame_factory>& frame_factory, int flags)\r
+spl::shared_ptr<core::write_frame> make_write_frame(const void* tag, const spl::shared_ptr<AVFrame>& decoded_frame, const spl::shared_ptr<core::frame_factory>& frame_factory, int flags)\r
 {                      \r
        static tbb::concurrent_unordered_map<int, tbb::concurrent_queue<std::shared_ptr<SwsContext>>> sws_contexts_;\r
        \r
        if(decoded_frame->width < 1 || decoded_frame->height < 1)\r
-               return make_safe<core::write_frame>(tag);\r
+               return frame_factory->create_frame(tag, core::pixel_format_desc(core::pixel_format::invalid));\r
 \r
        const auto width  = decoded_frame->width;\r
        const auto height = decoded_frame->height;\r
@@ -188,7 +187,7 @@ safe_ptr<core::write_frame> make_write_frame(const void* tag, const safe_ptr<AVF
 \r
        std::shared_ptr<core::write_frame> write;\r
 \r
-       if(desc.pix_fmt == core::pixel_format::invalid)\r
+       if(desc.format == core::pixel_format::invalid)\r
        {\r
                auto pix_fmt = static_cast<PixelFormat>(decoded_frame->format);\r
                auto target_pix_fmt = PIX_FMT_BGRA;\r
@@ -209,7 +208,6 @@ safe_ptr<core::write_frame> make_write_frame(const void* tag, const safe_ptr<AVF
                auto target_desc = get_pixel_format_desc(target_pix_fmt, width, height);\r
 \r
                write = frame_factory->create_frame(tag, target_desc);\r
-               write->set_type(get_mode(*decoded_frame));\r
 \r
                std::shared_ptr<SwsContext> sws_context;\r
 \r
@@ -231,12 +229,12 @@ safe_ptr<core::write_frame> make_write_frame(const void* tag, const safe_ptr<AVF
                                                                        boost::errinfo_api_function("sws_getContext"));\r
                }       \r
                \r
-               safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);     \r
+               spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);      \r
                avcodec_get_frame_defaults(av_frame.get());                     \r
                if(target_pix_fmt == PIX_FMT_BGRA)\r
                {\r
-                       auto size = avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data().begin(), PIX_FMT_BGRA, width, height);\r
-                       CASPAR_VERIFY(size == write->image_data().size()); \r
+                       auto size = avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data(0).begin(), PIX_FMT_BGRA, width, height);\r
+                       CASPAR_VERIFY(size == write->image_data(0).size()); \r
                }\r
                else\r
                {\r
@@ -250,14 +248,11 @@ safe_ptr<core::write_frame> make_write_frame(const void* tag, const safe_ptr<AVF
                }\r
 \r
                sws_scale(sws_context.get(), decoded_frame->data, decoded_frame->linesize, 0, height, av_frame->data, av_frame->linesize);      \r
-               pool.push(sws_context);\r
-\r
-               write->commit();                \r
+               pool.push(sws_context); \r
        }\r
        else\r
        {\r
                write = frame_factory->create_frame(tag, desc);\r
-               write->set_type(get_mode(*decoded_frame));\r
 \r
                for(int n = 0; n < static_cast<int>(desc.planes.size()); ++n)\r
                {\r
@@ -276,8 +271,6 @@ safe_ptr<core::write_frame> make_write_frame(const void* tag, const safe_ptr<AVF
                                for(int y = r.begin(); y != r.end(); ++y)\r
                                        A_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);\r
                        }, ap);\r
-\r
-                       write->commit(n);\r
                }\r
        }\r
 \r
@@ -288,12 +281,80 @@ safe_ptr<core::write_frame> make_write_frame(const void* tag, const safe_ptr<AVF
        }\r
        \r
        // Fix field-order if needed\r
-       if(write->get_type() == core::field_mode::lower && frame_factory->get_video_format_desc().field_mode == core::field_mode::upper)\r
+       if(get_mode(*decoded_frame) == core::field_mode::lower && frame_factory->get_video_format_desc().field_mode == core::field_mode::upper)\r
                write->get_frame_transform().fill_translation[1] += 1.0/static_cast<double>(frame_factory->get_video_format_desc().height);\r
-       else if(write->get_type() == core::field_mode::upper && frame_factory->get_video_format_desc().field_mode == core::field_mode::lower)\r
+       else if(get_mode(*decoded_frame) == core::field_mode::upper && frame_factory->get_video_format_desc().field_mode == core::field_mode::lower)\r
                write->get_frame_transform().fill_translation[1] -= 1.0/static_cast<double>(frame_factory->get_video_format_desc().height);\r
 \r
-       return make_safe_ptr(write);\r
+       return spl::make_shared_ptr(write);\r
+}\r
+\r
+spl::shared_ptr<AVFrame> make_av_frame(caspar::core::data_frame& frame)\r
+{\r
+       std::array<void*, 4> data = {};\r
+       for(int n = 0; n < frame.get_pixel_format_desc().planes.size(); ++n)\r
+               data[n] = frame.image_data(n).begin();\r
+\r
+       return make_av_frame(data, frame.get_pixel_format_desc());\r
+}\r
+\r
+spl::shared_ptr<AVFrame> make_av_frame(std::array<void*, 4> data, const core::pixel_format_desc& pix_desc)\r
+{\r
+       spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);      \r
+       avcodec_get_frame_defaults(av_frame.get());\r
+       \r
+       auto planes              = pix_desc.planes;\r
+       auto format              = pix_desc.format.value();\r
+\r
+       av_frame->width  = planes[0].width;\r
+       av_frame->height = planes[0].height;\r
+       for(int n = 0; n < planes.size(); ++n)  \r
+       {\r
+               av_frame->data[n]         = reinterpret_cast<uint8_t*>(data[n]);\r
+               av_frame->linesize[n] = planes[n].linesize;     \r
+       }\r
+       switch(format)\r
+       {\r
+       case core::pixel_format::rgba:\r
+               av_frame->format = PIX_FMT_RGBA; \r
+               break;\r
+       case core::pixel_format::argb:\r
+               av_frame->format = PIX_FMT_ARGB; \r
+               break;\r
+       case core::pixel_format::bgra:\r
+               av_frame->format = PIX_FMT_BGRA; \r
+               break;\r
+       case core::pixel_format::abgr:\r
+               av_frame->format = PIX_FMT_ABGR; \r
+               break;\r
+       case core::pixel_format::gray:\r
+               av_frame->format = PIX_FMT_GRAY8; \r
+               break;\r
+       case core::pixel_format::ycbcr:\r
+       {\r
+               int y_w = planes[0].width;\r
+               int y_h = planes[0].height;\r
+               int c_w = planes[1].width;\r
+               int c_h = planes[1].height;\r
+\r
+               if(c_h == y_h && c_w == y_w)\r
+                       av_frame->format = PIX_FMT_YUV444P;\r
+               else if(c_h == y_h && c_w*2 == y_w)\r
+                       av_frame->format = PIX_FMT_YUV422P;\r
+               else if(c_h == y_h && c_w*4 == y_w)\r
+                       av_frame->format = PIX_FMT_YUV411P;\r
+               else if(c_h*2 == y_h && c_w*2 == y_w)\r
+                       av_frame->format = PIX_FMT_YUV420P;\r
+               else if(c_h*2 == y_h && c_w*4 == y_w)\r
+                       av_frame->format = PIX_FMT_YUV410P;\r
+\r
+               break;\r
+       }\r
+       case core::pixel_format::ycbcra:\r
+               av_frame->format = PIX_FMT_YUVA420P;\r
+               break;\r
+       }\r
+       return av_frame;\r
 }\r
 \r
 bool is_sane_fps(AVRational time_base)\r
@@ -415,9 +476,9 @@ void fix_meta_data(AVFormatContext& context)
        }\r
 }\r
 \r
-safe_ptr<AVPacket> create_packet()\r
+spl::shared_ptr<AVPacket> create_packet()\r
 {\r
-       safe_ptr<AVPacket> packet(new AVPacket, [](AVPacket* p)\r
+       spl::shared_ptr<AVPacket> packet(new AVPacket, [](AVPacket* p)\r
        {\r
                av_free_packet(p);\r
                delete p;\r
@@ -427,7 +488,7 @@ safe_ptr<AVPacket> create_packet()
        return packet;\r
 }\r
 \r
-safe_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType type, int& index)\r
+spl::shared_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType type, int& index)\r
 {      \r
        AVCodec* decoder;\r
        index = THROW_ON_ERROR2(av_find_best_stream(&context, type, -1, -1, &decoder, 0), "");\r
@@ -435,14 +496,14 @@ safe_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType t
        //      decoder = decoder->next;\r
 \r
        THROW_ON_ERROR2(tbb_avcodec_open(context.streams[index]->codec, decoder), "");\r
-       return safe_ptr<AVCodecContext>(context.streams[index]->codec, tbb_avcodec_close);\r
+       return spl::shared_ptr<AVCodecContext>(context.streams[index]->codec, tbb_avcodec_close);\r
 }\r
 \r
-safe_ptr<AVFormatContext> open_input(const std::wstring& filename)\r
+spl::shared_ptr<AVFormatContext> open_input(const std::wstring& filename)\r
 {\r
        AVFormatContext* weak_context = nullptr;\r
        THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(filename).c_str(), nullptr, nullptr), filename);\r
-       safe_ptr<AVFormatContext> context(weak_context, av_close_input_file);                   \r
+       spl::shared_ptr<AVFormatContext> context(weak_context, av_close_input_file);                    \r
        THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), filename);\r
        fix_meta_data(*context);\r
        return context;\r
@@ -458,7 +519,7 @@ std::wstring print_mode(int width, int height, double fps, bool interlaced)
 \r
 bool is_valid_file(const std::wstring filename)\r
 {                      \r
-       if(boost::filesystem::path(filename2).extension() == ".m2t")\r
+       if(boost::filesystem::path(filename).extension() == ".m2t")\r
                return true;\r
 \r
        std::ifstream file(filename);\r