]> git.sesse.net Git - casparcg/blobdiff - core/producer/framerate/framerate_producer.cpp
[ffmpeg] Reimplemented support for playing all audio streams in a clip and treating...
[casparcg] / core / producer / framerate / framerate_producer.cpp
index d84b1f1d39e4c38482039a1679dae7c7ff8bf68f..75d103a1b97623ad66eb9451a74c89241bed52d5 100644 (file)
@@ -49,7 +49,7 @@ draw_frame drop_and_skip(const draw_frame& source, const draw_frame&, const boos
 
 // Blends next frame with current frame when the distance is not 0.
 // Completely sharp when distance is 0 but blurry when in between.
-draw_frame blend(const draw_frame& source, const draw_frame& destination, const boost::rational<int64_t>& distance)
+draw_frame blend2(const draw_frame& source, const draw_frame& destination, const boost::rational<int64_t>& distance)
 {
        if (destination == draw_frame::empty())
                return source;
@@ -70,8 +70,8 @@ draw_frame blend(const draw_frame& source, const draw_frame& destination, const
 // * A distance of 0.0 gives 50% previous, 50% current and 0% next.
 // * A distance of 0.5 gives 25% previous, 50% current and 25% next.
 // * A distance of 0.75 gives 12.5% previous, 50% current and 37.5% next.
-// This is blurrier than blend, but gives a more even bluriness, instead of sharp, blurry, sharp, blurry.
-struct blend_all
+// This is blurrier than blend2, but gives a more even bluriness, instead of sharp, blurry, sharp, blurry.
+struct blend3
 {
        draw_frame previous_frame       = draw_frame::empty();
        draw_frame last_source          = draw_frame::empty();
@@ -93,7 +93,7 @@ struct blend_all
                bool has_previous = previous_frame != draw_frame::empty();
 
                if (!has_previous)
-                       return blend(source, destination, distance);
+                       return blend2(source, destination, distance);
 
                auto middle                                                                                     = last_source;
                auto next_frame                                                                         = destination;
@@ -176,7 +176,7 @@ public:
                double source   = boost::rational_cast<double>(source_);
                double delta    = boost::rational_cast<double>(dest_) - source;
                double result   = tweener_(time_, source, delta, duration_);
-               
+
                return boost::rational<int64_t>(static_cast<int64_t>(result * 1000000.0), 1000000);
        }
 
@@ -193,10 +193,9 @@ class framerate_producer : public frame_producer_base
        std::function<boost::rational<int>()>                           get_source_framerate_;
        boost::rational<int>                                                            source_framerate_                               = -1;
        audio_channel_layout                                                            source_channel_layout_                  = audio_channel_layout::invalid();
-       boost::rational<int>                                                            original_destination_framerate_;
-       field_mode                                                                                      original_destination_fieldmode_;
-       boost::rational<int>                                                            destination_framerate_;
-       field_mode                                                                                      destination_fieldmode_;
+       const boost::rational<int>                                                      original_destination_framerate_;
+       const field_mode                                                                        original_destination_fieldmode_;
+       field_mode                                                                                      destination_fieldmode_                  = field_mode::empty;
        std::vector<int>                                                                        destination_audio_cadence_;
        boost::rational<std::int64_t>                                           speed_;
        speed_tweener                                                                           user_speed_;
@@ -204,7 +203,7 @@ class framerate_producer : public frame_producer_base
                        const draw_frame& source,
                        const draw_frame& destination,
                        const boost::rational<int64_t>& distance)>      interpolator_                                   = drop_and_skip;
-       
+
        boost::rational<std::int64_t>                                           current_frame_number_                   = 0;
        draw_frame                                                                                      previous_frame_                                 = draw_frame::empty();
        draw_frame                                                                                      next_frame_                                             = draw_frame::empty();
@@ -212,6 +211,7 @@ class framerate_producer : public frame_producer_base
 
        unsigned int                                                                            output_repeat_                                  = 0;
        unsigned int                                                                            output_frame_                                   = 0;
+       draw_frame                                                                                      last_frame_                                             = draw_frame::empty();
 public:
        framerate_producer(
                        spl::shared_ptr<frame_producer> source,
@@ -232,13 +232,15 @@ public:
 
        draw_frame receive_impl() override
        {
+               // destination field mode initially unknown but known after the first update_source_framerate().
+               auto field1 = do_render_progressive_frame(true);
+
                if (destination_fieldmode_ == field_mode::progressive)
                {
-                       return do_render_progressive_frame(true);
+                       return field1;
                }
                else
                {
-                       auto field1 = do_render_progressive_frame(true);
                        auto field2 = do_render_progressive_frame(false);
 
                        return draw_frame::interlace(field1, field2, destination_fieldmode_);
@@ -262,12 +264,14 @@ public:
                }
                else if (boost::iequals(params.at(1), L"interpolation"))
                {
-                       if (boost::iequals(params.at(2), L"blend"))
-                               interpolator_ = &blend;
-                       else if (boost::iequals(params.at(2), L"blend_all"))
-                               interpolator_ = blend_all();
-                       else
+                       if (boost::iequals(params.at(2), L"blend2"))
+                               interpolator_ = &blend2;
+                       else if (boost::iequals(params.at(2), L"blend3"))
+                               interpolator_ = blend3();
+                       else if (boost::iequals(params.at(2), L"drop_and_skip"))
                                interpolator_ = &drop_and_skip;
+                       else
+                               CASPAR_THROW_EXCEPTION(user_error() << msg_info("Valid interpolations are DROP_AND_SKIP, BLEND2 and BLEND3"));
                }
                else if (boost::iequals(params.at(1), L"output_repeat")) // Only for debugging purposes
                {
@@ -334,7 +338,7 @@ private:
 
                if (output_repeat_ && output_frame_++ % output_repeat_)
                {
-                       auto frame = draw_frame::still(last_frame());
+                       auto frame = last_frame_;
 
                        frame.transform().audio_transform.volume = 0.0;
 
@@ -359,6 +363,8 @@ private:
 
                fast_forward_integer_frames(integer_next_frame - integer_current_frame);
 
+               last_frame_ = result;
+
                if (sound)
                        return attach_sound(result);
                else
@@ -479,19 +485,21 @@ private:
                if (source_framerate_ == source_framerate)
                        return;
 
-               source_framerate_               = source_framerate;
-               destination_framerate_  = original_destination_framerate_;
-               destination_fieldmode_  = original_destination_fieldmode_;
+               output_repeat_                          = 0;
+               output_frame_                           = 0;
+               source_framerate_                       = source_framerate;
+               auto destination_framerate      = original_destination_framerate_;
+               destination_fieldmode_          = original_destination_fieldmode_;
 
                // Coarse adjustment to correct fps family (23.98 - 30 vs 47.95 - 60)
                if (destination_fieldmode_ != field_mode::progressive)  // Interlaced output
                {
-                       auto diff_double        = boost::abs(source_framerate_ - destination_framerate_ * 2);
-                       auto diff_keep          = boost::abs(source_framerate_ - destination_framerate_);
+                       auto diff_double        = boost::abs(source_framerate_ - destination_framerate * 2);
+                       auto diff_keep          = boost::abs(source_framerate_ - destination_framerate);
 
                        if (diff_double < diff_keep)                                            // Double rate interlaced
                        {
-                               destination_framerate_ *= 2;
+                               destination_framerate *= 2;
                        }
                        else                                                                                            // Progressive non interlaced
                        {
@@ -500,41 +508,43 @@ private:
                }
                else                                                                                                    // Progressive
                {
-                       auto diff_halve = boost::abs(source_framerate_ * 2      - destination_framerate_);
-                       auto diff_keep  = boost::abs(source_framerate_          - destination_framerate_);
+                       auto diff_halve = boost::abs(source_framerate_ * 2      - destination_framerate);
+                       auto diff_keep  = boost::abs(source_framerate_          - destination_framerate);
 
                        if (diff_halve < diff_keep)                                                     // Repeat every frame two times
                        {
-                               destination_framerate_  /= 2;
+                               destination_framerate   /= 2;
                                output_repeat_                  = 2;
                        }
                }
 
-               speed_ = boost::rational<int64_t>(source_framerate_ / destination_framerate_);
+               speed_ = boost::rational<int64_t>(source_framerate_ / destination_framerate);
 
                // drop_and_skip will only be used by default for exact framerate multiples (half, same and double)
                // for all other framerates a frame interpolator will be chosen.
                if (speed_ != 1 && speed_ * 2 != 1 && speed_ != 2)
                {
                        auto high_source_framerate              = source_framerate_ > 47;
-                       auto high_destination_framerate = destination_framerate_ > 47
+                       auto high_destination_framerate = destination_framerate > 47
                                        || destination_fieldmode_ != field_mode::progressive;
 
-                       if (high_source_framerate && high_destination_framerate)        // The bluriness of blend_all is acceptable on high framerates.
-                               interpolator_ = blend_all();
-                       else                                                                                                            // blend_all is mostly too blurry on low framerates. blend provides a compromise.
-                               interpolator_ = &blend;
+                       if (high_source_framerate && high_destination_framerate)        // The bluriness of blend3 is acceptable on high framerates.
+                               interpolator_   = blend3();
+                       else                                                                                                            // blend3 is mostly too blurry on low framerates. blend2 provides a compromise.
+                               interpolator_   = &blend2;
 
                        CASPAR_LOG(warning) << source_->print() << L" Frame blending frame rate conversion required to conform to channel frame rate.";
                }
+               else
+                       interpolator_           = &drop_and_skip;
        }
 };
 
 void describe_framerate_producer(help_sink& sink)
 {
        sink.para()->text(L"Framerate conversion control / Slow motion examples:");
-       sink.example(L">> CALL 1-10 FRAMERATE INTERPOLATION BLEND", L"enables 2 frame blend interpolation.");
-       sink.example(L">> CALL 1-10 FRAMERATE INTERPOLATION BLEND_ALL", L"enables 3 frame blend interpolation.");
+       sink.example(L">> CALL 1-10 FRAMERATE INTERPOLATION BLEND2", L"enables 2 frame blend interpolation.");
+       sink.example(L">> CALL 1-10 FRAMERATE INTERPOLATION BLEND3", L"enables 3 frame blend interpolation.");
        sink.example(L">> CALL 1-10 FRAMERATE INTERPOLATION DROP_AND_SKIP", L"disables frame interpolation.");
        sink.example(L">> CALL 1-10 FRAMERATE SPEED 0.25", L"immediately changes the speed to 25%. Sound will be disabled.");
        sink.example(L">> CALL 1-10 FRAMERATE SPEED 0.25 50", L"changes the speed to 25% linearly over 50 frames. Sound will be disabled.");
@@ -558,4 +568,3 @@ spl::shared_ptr<frame_producer> create_framerate_producer(
 }
 
 }}
-