]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/util/util.cpp
2.1.0: Put "array" into its own file, common/memory/array.
[casparcg] / modules / ffmpeg / producer / util / util.cpp
index dd170c15514e7d5903aff2ac5ea9885cbcda7815..7677bd807e83af4c7978db066cd95f1c866e1ad5 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/frame/frame.h>\r
 #include <core/producer/frame_producer.h>\r
-#include <core/mixer/write_frame.h>\r
 \r
-#include <common/exception/exceptions.h>\r
-#include <common/utility/assert.h>\r
-#include <common/memory/memcpy.h>\r
+#include <common/except.h>\r
+#include <common/memory/array.h>\r
 \r
 #include <tbb/parallel_for.h>\r
 \r
+#include <common/assert.h>\r
 #include <boost/filesystem.hpp>\r
 #include <boost/lexical_cast.hpp>\r
 \r
+#include <asmlib.h>\r
+\r
 #if defined(_MSC_VER)\r
 #pragma warning (push)\r
 #pragma warning (disable : 4244)\r
@@ -85,7 +87,7 @@ std::shared_ptr<AVFrame>                      empty_video()
        return video;\r
 }\r
 \r
-core::field_mode::type get_mode(const AVFrame& frame)\r
+core::field_mode get_mode(const AVFrame& frame)\r
 {\r
        if(!frame.interlaced_frame)\r
                return core::field_mode::progressive;\r
@@ -93,7 +95,7 @@ core::field_mode::type get_mode(const AVFrame& frame)
        return frame.top_field_first ? core::field_mode::upper : core::field_mode::lower;\r
 }\r
 \r
-core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)\r
+core::pixel_format get_pixel_format(PixelFormat pix_fmt)\r
 {\r
        switch(pix_fmt)\r
        {\r
@@ -113,16 +115,15 @@ core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)
        }\r
 }\r
 \r
-core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width, size_t height)\r
+core::pixel_format_desc pixel_format_desc(PixelFormat pix_fmt, int width, int height)\r
 {\r
        // Get linesizes\r
        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)\r
+       switch(desc.format.value())\r
        {\r
        case core::pixel_format::gray:\r
        case core::pixel_format::luma:\r
@@ -142,26 +143,26 @@ core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width,
        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
+                       int size2 = static_cast<int>(dummy_pict.data[2] - dummy_pict.data[1]);\r
+                       int 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
+                       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
 \r
 int make_alpha_format(int format)\r
 {\r
-       switch(get_pixel_format(static_cast<PixelFormat>(format)))\r
+       switch(get_pixel_format(static_cast<PixelFormat>(format)).value())\r
        {\r
        case core::pixel_format::ycbcr:\r
        case core::pixel_format::ycbcra:\r
@@ -171,23 +172,21 @@ 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 hints)\r
+core::mutable_frame make_frame(const void* tag, const spl::shared_ptr<AVFrame>& decoded_frame, double fps, const spl::shared_ptr<core::frame_factory>& frame_factory, int flags)\r
 {                      \r
-       static tbb::concurrent_unordered_map<size_t, tbb::concurrent_queue<std::shared_ptr<SwsContext>>> sws_contexts_;\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
-       auto desc                 = get_pixel_format_desc(static_cast<PixelFormat>(decoded_frame->format), width, height);\r
+       auto desc                 = pixel_format_desc(static_cast<PixelFormat>(decoded_frame->format), width, height);\r
        \r
-       if(hints & core::frame_producer::ALPHA_HINT)\r
-               desc = get_pixel_format_desc(static_cast<PixelFormat>(make_alpha_format(decoded_frame->format)), width, height);\r
-\r
-       std::shared_ptr<core::write_frame> write;\r
-\r
-       if(desc.pix_fmt == core::pixel_format::invalid)\r
+       if(flags & core::frame_producer::flags::alpha_only)\r
+               desc = pixel_format_desc(static_cast<PixelFormat>(make_alpha_format(decoded_frame->format)), width, height);\r
+       \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
@@ -205,16 +204,15 @@ safe_ptr<core::write_frame> make_write_frame(const void* tag, const safe_ptr<AVF
                else if(pix_fmt == PIX_FMT_YUV444P10)\r
                        target_pix_fmt = PIX_FMT_YUV444P;\r
                \r
-               auto target_desc = get_pixel_format_desc(target_pix_fmt, width, height);\r
+               auto target_desc = 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
+               auto write = frame_factory->create_frame(tag, target_desc, fps, get_mode(*decoded_frame));\r
 \r
                std::shared_ptr<SwsContext> sws_context;\r
 \r
                //CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";\r
 \r
-               size_t key = ((width << 22) & 0xFFC00000) | ((height << 6) & 0x003FC000) | ((pix_fmt << 7) & 0x00007F00) | ((target_pix_fmt << 0) & 0x0000007F);\r
+               int key = ((width << 22) & 0xFFC00000) | ((height << 6) & 0x003FC000) | ((pix_fmt << 7) & 0x00007F00) | ((target_pix_fmt << 0) & 0x0000007F);\r
                        \r
                auto& pool = sws_contexts_[key];\r
                                                \r
@@ -230,74 +228,122 @@ 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
                        av_frame->width  = width;\r
                        av_frame->height = height;\r
-                       for(size_t n = 0; n < target_desc.planes.size(); ++n)\r
+                       for(int n = 0; n < target_desc.planes.size(); ++n)\r
                        {\r
-                               av_frame->data[n]               = write->image_data(n).begin();\r
+                               av_frame->data[n]               = write.image_data(n).begin();\r
                                av_frame->linesize[n]   = target_desc.planes[n].linesize;\r
                        }\r
                }\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
+               pool.push(sws_context); \r
 \r
-               write->commit();                \r
+               return std::move(write);\r
        }\r
        else\r
        {\r
-               write = frame_factory->create_frame(tag, desc);\r
-               write->set_type(get_mode(*decoded_frame));\r
-\r
+               auto write = frame_factory->create_frame(tag, desc, fps, get_mode(*decoded_frame));\r
+               \r
                for(int n = 0; n < static_cast<int>(desc.planes.size()); ++n)\r
                {\r
                        auto plane            = desc.planes[n];\r
-                       auto result           = write->image_data(n).begin();\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
                        CASPAR_ASSERT(decoded);\r
-                       CASPAR_ASSERT(write->image_data(n).begin());\r
+                       CASPAR_ASSERT(write.image_data(n).begin());\r
 \r
-                       if(decoded_linesize != static_cast<int>(plane.width))\r
-                       {\r
-                               // Copy line by line since ffmpeg sometimes pads each line.\r
-                               tbb::parallel_for<size_t>(0, desc.planes[n].height, [&](size_t y)\r
-                               {\r
-                                       fast_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);\r
-                               });\r
-                       }\r
-                       else\r
+                       // Copy line by line since ffmpeg sometimes pads each line.\r
+                       tbb::affinity_partitioner ap;\r
+                       tbb::parallel_for(tbb::blocked_range<int>(0, desc.planes[n].height), [&](const tbb::blocked_range<int>& r)\r
                        {\r
-                               fast_memcpy(result, decoded, plane.size);\r
-                       }\r
-\r
-                       write->commit(n);\r
+                               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
+       \r
+               return std::move(write);\r
        }\r
+}\r
 \r
-       if(decoded_frame->height == 480) // NTSC DV\r
-       {\r
-               write->get_frame_transform().fill_translation[1] += 2.0/static_cast<double>(frame_factory->get_video_format_desc().height);\r
-               write->get_frame_transform().fill_scale[1] = 1.0 - 6.0*1.0/static_cast<double>(frame_factory->get_video_format_desc().height);\r
-       }\r
+spl::shared_ptr<AVFrame> make_av_frame(core::mutable_frame& frame)\r
+{\r
+       std::array<uint8_t*, 4> data = {};\r
+       for(int n = 0; n < frame.pixel_format_desc().planes.size(); ++n)\r
+               data[n] = frame.image_data(n).begin();\r
+\r
+       return make_av_frame(data, frame.pixel_format_desc());\r
+}\r
+\r
+spl::shared_ptr<AVFrame> make_av_frame(std::array<uint8_t*, 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
-       // 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
-               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
-               write->get_frame_transform().fill_translation[1] -= 1.0/static_cast<double>(frame_factory->get_video_format_desc().height);\r
+       auto planes              = pix_desc.planes;\r
+       auto format              = pix_desc.format.value();\r
 \r
-       return make_safe_ptr(write);\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]         = 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
@@ -334,7 +380,7 @@ double read_fps(AVFormatContext& context, double fail_value)
                                                \r
                AVRational time_base = video_context->time_base;\r
 \r
-               if(boost::filesystem2::path(context.filename).extension() == ".flv")\r
+               if(boost::filesystem::path(context.filename).extension().string() == ".flv")\r
                {\r
                        try\r
                        {\r
@@ -372,7 +418,7 @@ double read_fps(AVFormatContext& context, double fail_value)
                double closest_fps = 0.0;\r
                for(int n = 0; n < core::video_format::count; ++n)\r
                {\r
-                       auto format = core::video_format_desc::get(static_cast<core::video_format::type>(n));\r
+                       auto format = core::video_format_desc(core::video_format(n));\r
 \r
                        double diff1 = std::abs(format.fps - fps);\r
                        double diff2 = std::abs(closest_fps - fps);\r
@@ -396,7 +442,7 @@ void fix_meta_data(AVFormatContext& context)
                auto video_stream   = context.streams[video_index];\r
                auto video_context  = context.streams[video_index]->codec;\r
                                                \r
-               if(boost::filesystem2::path(context.filename).extension() == ".flv")\r
+               if(boost::filesystem::path(context.filename).extension().string() == ".flv")\r
                {\r
                        try\r
                        {\r
@@ -419,9 +465,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
@@ -431,7 +477,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
@@ -439,20 +485,20 @@ 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
 }\r
 \r
-std::wstring print_mode(size_t width, size_t height, double fps, bool interlaced)\r
+std::wstring print_mode(int width, int height, double fps, bool interlaced)\r
 {\r
        std::wostringstream fps_ss;\r
        fps_ss << std::fixed << std::setprecision(2) << (!interlaced ? fps : 2.0 * fps);\r
@@ -462,6 +508,9 @@ std::wstring print_mode(size_t width, size_t height, double fps, bool interlaced
 \r
 bool is_valid_file(const std::wstring filename)\r
 {                      \r
+       if(boost::filesystem::path(filename).extension() == ".m2t")\r
+               return true;\r
+\r
        std::ifstream file(filename);\r
 \r
        std::vector<unsigned char> buf;\r
@@ -474,7 +523,7 @@ bool is_valid_file(const std::wstring filename)
        AVProbeData pb;\r
        pb.filename = u8(filename).c_str();\r
        pb.buf          = buf.data();\r
-       pb.buf_size = buf.size();\r
+       pb.buf_size = static_cast<int>(buf.size());\r
 \r
        int score = 0;\r
        return av_probe_input_format2(&pb, true, &score) != nullptr;\r
@@ -482,12 +531,12 @@ bool is_valid_file(const std::wstring filename)
 \r
 std::wstring probe_stem(const std::wstring stem)\r
 {\r
-       auto stem2 = boost::filesystem2::wpath(stem);\r
+       auto stem2 = boost::filesystem::path(stem);\r
        auto dir = stem2.parent_path();\r
-       for(auto it = boost::filesystem2::wdirectory_iterator(dir); it != boost::filesystem2::wdirectory_iterator(); ++it)\r
+       for(auto it = boost::filesystem::directory_iterator(dir); it != boost::filesystem::directory_iterator(); ++it)\r
        {\r
-               if(boost::iequals(it->path().stem(), stem2.filename()) && is_valid_file(it->path().file_string()))\r
-                       return it->path().file_string();\r
+               if(boost::iequals(it->path().stem().wstring(), stem2.filename().wstring()) && is_valid_file(it->path().wstring()))\r
+                       return it->path().wstring();\r
        }\r
        return L"";\r
 }\r