]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/util/util.cpp
Updated CHANGES.txt and fixed minor problems
[casparcg] / modules / ffmpeg / producer / util / util.cpp
index f1f34c08e8b3760694c26628d64a63357bd54215..92fd8c7f0e29ed4e5aac086782acf16d9e5dea14 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
+* Copyright 2013 Sveriges Television AB http://casparcg.com/\r
 *\r
 * This file is part of CasparCG (www.casparcg.com).\r
 *\r
@@ -273,7 +273,7 @@ safe_ptr<core::write_frame> make_write_frame(const void* tag, const safe_ptr<AVF
                        CASPAR_ASSERT(decoded);\r
                        CASPAR_ASSERT(write->image_data(n).begin());\r
 \r
-                       if(decoded_linesize != static_cast<int>(plane.width))\r
+                       if(decoded_linesize != static_cast<int>(plane.linesize))\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
@@ -336,7 +336,15 @@ double read_fps(AVFormatContext& context, double fail_value)
        {\r
                const auto video_context = context.streams[video_index]->codec;\r
                const auto video_stream  = context.streams[video_index];\r
-                                               \r
+                               \r
+               auto frame_rate_time_base = video_stream->avg_frame_rate;\r
+               std::swap(frame_rate_time_base.num, frame_rate_time_base.den);\r
+\r
+               if(is_sane_fps(frame_rate_time_base))\r
+               {\r
+                       return static_cast<double>(frame_rate_time_base.den) / static_cast<double>(frame_rate_time_base.num);\r
+               }\r
+\r
                AVRational time_base = video_context->time_base;\r
 \r
                if(boost::filesystem2::path(context.filename).extension() == ".flv")\r
@@ -392,38 +400,6 @@ double read_fps(AVFormatContext& context, double fail_value)
        return fail_value;      \r
 }\r
 \r
-void fix_meta_data(AVFormatContext& context)\r
-{\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::filesystem2::path(context.filename).extension() == ".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
 safe_ptr<AVPacket> create_packet()\r
 {\r
        safe_ptr<AVPacket> packet(new AVPacket, [](AVPacket* p)\r
@@ -447,16 +423,6 @@ safe_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType t
        return safe_ptr<AVCodecContext>(context.streams[index]->codec, tbb_avcodec_close);\r
 }\r
 \r
-safe_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_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
 {\r
        std::wostringstream fps_ss;\r
@@ -507,6 +473,34 @@ bool is_valid_file(const std::wstring filename)
        return is_valid_file(filename, invalid_exts);\r
 }\r
 \r
+bool try_get_duration(const std::wstring filename, std::int64_t& duration, boost::rational<std::int64_t>& time_base)\r
+{              \r
+       AVFormatContext* weak_context = nullptr;\r
+       if(avformat_open_input(&weak_context, narrow(filename).c_str(), nullptr, nullptr) < 0)\r
+               return false;\r
+\r
+       std::shared_ptr<AVFormatContext> context(weak_context, av_close_input_file);\r
+       \r
+       context->probesize = context->probesize / 10;\r
+       context->max_analyze_duration = context->probesize / 10;\r
+\r
+       if(avformat_find_stream_info(context.get(), nullptr) < 0)\r
+               return false;\r
+\r
+       const auto fps = read_fps(*context, 1.0);\r
+               \r
+       const auto rational_fps = boost::rational<std::int64_t>(static_cast<int>(fps * AV_TIME_BASE), AV_TIME_BASE);\r
+       \r
+       duration = boost::rational_cast<std::int64_t>(context->duration * rational_fps / AV_TIME_BASE);\r
+\r
+       if (rational_fps == 0)\r
+               return false;\r
+\r
+       time_base = 1/rational_fps;\r
+\r
+       return true;\r
+}\r
+\r
 std::wstring probe_stem(const std::wstring stem, const std::vector<std::wstring>& invalid_exts)\r
 {\r
        auto stem2 = boost::filesystem2::wpath(stem);\r
@@ -519,6 +513,18 @@ std::wstring probe_stem(const std::wstring stem, const std::vector<std::wstring>
        return L"";\r
 }\r
 \r
+std::wstring probe_stem(const std::wstring stem)\r
+{\r
+       auto stem2 = boost::filesystem2::wpath(stem);\r
+       auto dir = stem2.parent_path();\r
+       for(auto it = boost::filesystem2::wdirectory_iterator(dir); it != boost::filesystem2::wdirectory_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
+       }\r
+       return L"";\r
+}\r
+\r
 core::channel_layout get_audio_channel_layout(\r
                const AVCodecContext& context, const std::wstring& custom_channel_order)\r
 {\r