]> 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 c1198d015d1a316a4a693064d5a9660ed9cda379..7677bd807e83af4c7978db066cd95f1c866e1ad5 100644 (file)
@@ -1,3 +1,24 @@
+/*\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
+*\r
+* This file is part of CasparCG (www.casparcg.com).\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
+* Author: Robert Nagy, ronag89@gmail.com\r
+*/\r
+\r
 #include "../../stdafx.h"\r
 \r
 #include "util.h"\r
 #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
@@ -40,12 +63,6 @@ extern "C"
 \r
 namespace caspar { namespace ffmpeg {\r
                \r
-safe_ptr<AVPacket> flush_packet()\r
-{\r
-       static auto packet = create_packet();\r
-       return packet;\r
-}\r
-\r
 std::shared_ptr<core::audio_buffer> flush_audio()\r
 {\r
        static std::shared_ptr<core::audio_buffer> audio(new core::audio_buffer());\r
@@ -70,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
@@ -78,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
@@ -98,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
@@ -127,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
@@ -156,41 +172,54 @@ 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
+\r
+               if(pix_fmt == PIX_FMT_UYVY422)\r
+                       target_pix_fmt = PIX_FMT_YUV422P;\r
+               else if(pix_fmt == PIX_FMT_YUYV422)\r
+                       target_pix_fmt = PIX_FMT_YUV422P;\r
+               else if(pix_fmt == PIX_FMT_UYYVYY411)\r
+                       target_pix_fmt = PIX_FMT_YUV411P;\r
+               else if(pix_fmt == PIX_FMT_YUV420P10)\r
+                       target_pix_fmt = PIX_FMT_YUV420P;\r
+               else if(pix_fmt == PIX_FMT_YUV422P10)\r
+                       target_pix_fmt = PIX_FMT_YUV422P;\r
+               else if(pix_fmt == PIX_FMT_YUV444P10)\r
+                       target_pix_fmt = PIX_FMT_YUV444P;\r
+               \r
+               auto target_desc = pixel_format_desc(target_pix_fmt, width, height);\r
 \r
-               write = frame_factory->create_frame(tag, get_pixel_format_desc(PIX_FMT_BGRA, width, height));\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 << 20 | height << 8 | pix_fmt;\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
                if(!pool.try_pop(sws_context))\r
                {\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
+                       sws_context.reset(sws_getContext(width, height, pix_fmt, width, height, target_pix_fmt, SWS_BILINEAR, nullptr, nullptr, &param), sws_freeContext);\r
                }\r
                        \r
                if(!sws_context)\r
@@ -198,63 +227,123 @@ safe_ptr<core::write_frame> make_write_frame(const void* tag, const safe_ptr<AVF
                        BOOST_THROW_EXCEPTION(operation_failed() << msg_info("Could not create software scaling context.") << \r
                                                                        boost::errinfo_api_function("sws_getContext"));\r
                }       \r
-\r
-               // Use sws_scale when provided colorspace has no hw-accel.\r
-               safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);     \r
+               \r
+               spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);      \r
                avcodec_get_frame_defaults(av_frame.get());                     \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
+               if(target_pix_fmt == PIX_FMT_BGRA)\r
+               {\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(int n = 0; n < target_desc.planes.size(); ++n)\r
+                       {\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
@@ -279,79 +368,57 @@ AVRational fix_time_base(AVRational time_base)
        return time_base;\r
 }\r
 \r
-void fix_meta_data(AVFormatContext& context)\r
-{\r
+double read_fps(AVFormatContext& context, double fail_value)\r
+{                                              \r
        auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
        auto audio_index = av_find_best_stream(&context, AVMEDIA_TYPE_AUDIO, -1, -1, 0, 0);\r
-\r
+       \r
        if(video_index > -1)\r
        {\r
-               auto video_context = context.streams[video_index]->codec;\r
-               auto video_stream  = context.streams[video_index];\r
+               const auto video_context = context.streams[video_index]->codec;\r
+               const auto video_stream  = context.streams[video_index];\r
                                                \r
-               if(boost::filesystem2::path(context.filename).extension() == ".flv")\r
+               AVRational time_base = video_context->time_base;\r
+\r
+               if(boost::filesystem::path(context.filename).extension().string() == ".flv")\r
                {\r
                        try\r
                        {\r
                                auto meta = read_flv_meta_info(context.filename);\r
-                               double fps = boost::lexical_cast<double>(meta["framerate"]);\r
-                               video_context->time_base.num = 1000000;\r
-                               video_context->time_base.den = static_cast<int>(fps*1000000.0);\r
-                               video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);\r
+                               return boost::lexical_cast<double>(meta["framerate"]);\r
+                       }\r
+                       catch(...)\r
+                       {\r
+                               return 0.0;\r
                        }\r
-                       catch(...){}\r
                }\r
                else\r
                {\r
-                       video_context->time_base.num *= video_context->ticks_per_frame;\r
+                       time_base.num *= video_context->ticks_per_frame;\r
 \r
-                       if(!is_sane_fps(video_context->time_base))\r
+                       if(!is_sane_fps(time_base))\r
                        {                       \r
-                               video_context->time_base = fix_time_base(video_context->time_base);\r
+                               time_base = fix_time_base(time_base);\r
 \r
-                               if(!is_sane_fps(video_context->time_base) && audio_index > -1)\r
+                               if(!is_sane_fps(time_base) && audio_index > -1)\r
                                {\r
                                        auto& audio_context = *context.streams[audio_index]->codec;\r
                                        auto& audio_stream  = *context.streams[audio_index];\r
 \r
                                        double duration_sec = audio_stream.duration / static_cast<double>(audio_context.sample_rate);\r
                                                                \r
-                                       video_context->time_base.num = static_cast<int>(duration_sec*100000.0);\r
-                                       video_context->time_base.den = static_cast<int>(video_stream->nb_frames*100000);\r
+                                       time_base.num = static_cast<int>(duration_sec*100000.0);\r
+                                       time_base.den = static_cast<int>(video_stream->nb_frames*100000);\r
                                }\r
                        }\r
-                               \r
-                       //if(audio_index > -1) // Check for invalid double frame-rate\r
-                       //{\r
-                       //      auto& audio_context             = *context.streams[audio_index]->codec;\r
-                       //      auto& audio_stream              = *context.streams[audio_index];\r
-                       //      \r
-                       //      double duration_sec             = audio_stream.duration / static_cast<double>(audio_context.sample_rate);\r
-                       //      double fps                              = static_cast<double>(video_context->time_base.den) / static_cast<double>(video_context->time_base.num);\r
-\r
-                       //      double fps_nb_frames    = static_cast<double>(duration_sec*fps);\r
-                       //      double stream_nb_frames = static_cast<double>(video_stream->nb_frames);\r
-                       //      double diff                             = std::abs(fps_nb_frames - stream_nb_frames*2.0);\r
-                       //      if(diff < fps_nb_frames*0.05)\r
-                       //              video_context->time_base.num *= 2;\r
-                       //}\r
-                       //else\r
-                       //{\r
-                       //      video_context->time_base.den = video_stream->r_frame_rate.num;\r
-                       //      video_context->time_base.num = video_stream->r_frame_rate.den;\r
-                       //}\r
                }\r
-\r
-               auto nb_frames = static_cast<double>(video_stream->duration*video_stream->time_base.num)/static_cast<double>(video_stream->time_base.den);\r
-               nb_frames = (nb_frames*video_context->time_base.den)/video_context->time_base.num;\r
-               video_stream->nb_frames = static_cast<int64_t>(nb_frames+0.5);\r
-\r
-               double fps = static_cast<double>(video_context->time_base.den) / static_cast<double>(video_context->time_base.num);\r
+               \r
+               double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);\r
 \r
                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
@@ -360,14 +427,47 @@ void fix_meta_data(AVFormatContext& context)
                                closest_fps = format.fps;\r
                }\r
        \r
-               video_context->time_base.num = 1000000;\r
-               video_context->time_base.den = static_cast<int>(closest_fps*1000000.0);\r
+               return closest_fps;\r
        }\r
+\r
+       return fail_value;      \r
 }\r
 \r
-safe_ptr<AVPacket> create_packet()\r
+void fix_meta_data(AVFormatContext& context)\r
 {\r
-       safe_ptr<AVPacket> packet(new AVPacket, [](AVPacket* p)\r
+       auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
+\r
+       if(video_index > -1)\r
+       {\r
+               auto video_stream   = context.streams[video_index];\r
+               auto video_context  = context.streams[video_index]->codec;\r
+                                               \r
+               if(boost::filesystem::path(context.filename).extension().string() == ".flv")\r
+               {\r
+                       try\r
+                       {\r
+                               auto meta = read_flv_meta_info(context.filename);\r
+                               double fps = boost::lexical_cast<double>(meta["framerate"]);\r
+                               video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);\r
+                       }\r
+                       catch(...){}\r
+               }\r
+               else\r
+               {\r
+                       auto stream_time = video_stream->time_base;\r
+                       auto duration    = video_stream->duration;\r
+                       auto codec_time  = video_context->time_base;\r
+                       auto ticks               = video_context->ticks_per_frame;\r
+\r
+                       if(video_stream->nb_frames == 0)\r
+                               video_stream->nb_frames = (duration*stream_time.num*codec_time.den)/(stream_time.den*codec_time.num*ticks);     \r
+               }\r
+       }\r
+}\r
+\r
+spl::shared_ptr<AVPacket> create_packet()\r
+{\r
+       spl::shared_ptr<AVPacket> packet(new AVPacket, [](AVPacket* p)\r
        {\r
                av_free_packet(p);\r
                delete p;\r
@@ -377,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
@@ -385,18 +485,61 @@ 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, narrow(filename).c_str(), nullptr, nullptr), filename);\r
-       safe_ptr<AVFormatContext> context(weak_context, av_close_input_file);                   \r
+       THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(filename).c_str(), nullptr, nullptr), filename);\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(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
+\r
+       return boost::lexical_cast<std::wstring>(width) + L"x" + boost::lexical_cast<std::wstring>(height) + (!interlaced ? L"p" : L"i") + fps_ss.str();\r
+}\r
+\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
+       for(auto file_it = std::istreambuf_iterator<char>(file); file_it != std::istreambuf_iterator<char>() && buf.size() < 2048; ++file_it)\r
+               buf.push_back(*file_it);\r
+\r
+       if(buf.empty())\r
+               return nullptr;\r
+\r
+       AVProbeData pb;\r
+       pb.filename = u8(filename).c_str();\r
+       pb.buf          = buf.data();\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
+}\r
+\r
+std::wstring probe_stem(const std::wstring stem)\r
+{\r
+       auto stem2 = boost::filesystem::path(stem);\r
+       auto dir = stem2.parent_path();\r
+       for(auto it = boost::filesystem::directory_iterator(dir); it != boost::filesystem::directory_iterator(); ++it)\r
+       {\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
 //\r
 //void av_dup_frame(AVFrame* frame)\r
 //{\r