]> git.sesse.net Git - casparcg/blob - core/producer/framerate/framerate_producer.cpp
#467 [framerate_producer] Fixed nb_frames() and frame_number() calculation
[casparcg] / core / producer / framerate / framerate_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "framerate_producer.h"
25
26 #include "../frame_producer.h"
27 #include "../../frame/audio_channel_layout.h"
28 #include "../../frame/draw_frame.h"
29 #include "../../frame/frame.h"
30 #include "../../frame/frame_transform.h"
31 #include "../../frame/pixel_format.h"
32 #include "../../monitor/monitor.h"
33 #include "../../help/help_sink.h"
34
35 #include <common/future.h>
36 #include <common/tweener.h>
37
38 #include <functional>
39 #include <queue>
40 #include <future>
41 #include <stack>
42
43 namespace caspar { namespace core {
44
45 draw_frame drop_and_skip(const draw_frame& source, const draw_frame&, const boost::rational<int64_t>&)
46 {
47         return source;
48 }
49
50 // Blends next frame with current frame when the distance is not 0.
51 // Completely sharp when distance is 0 but blurry when in between.
52 draw_frame blend(const draw_frame& source, const draw_frame& destination, const boost::rational<int64_t>& distance)
53 {
54         if (destination == draw_frame::empty())
55                 return source;
56
57         auto under                                      = source;
58         auto over                                       = destination;
59         double float_distance           = boost::rational_cast<double>(distance);
60
61         under.transform().image_transform.is_mix        = true;
62         under.transform().image_transform.opacity       = 1 - float_distance;
63         over.transform().image_transform.is_mix         = true;
64         over.transform().image_transform.opacity        = float_distance;
65
66         return draw_frame::over(under, over);
67 }
68
69 // Blends a moving window with a width of 1 frame duration.
70 // * A distance of 0.0 gives 50% previous, 50% current and 0% next.
71 // * A distance of 0.5 gives 25% previous, 50% current and 25% next.
72 // * A distance of 0.75 gives 12.5% previous, 50% current and 37.5% next.
73 // This is blurrier than blend, but gives a more even bluriness, instead of sharp, blurry, sharp, blurry.
74 struct blend_all
75 {
76         draw_frame previous_frame       = draw_frame::empty();
77         draw_frame last_source          = draw_frame::empty();
78         draw_frame last_destination     = draw_frame::empty();
79
80         draw_frame operator()(const draw_frame& source, const draw_frame& destination, const boost::rational<int64_t>& distance)
81         {
82                 if (last_source != draw_frame::empty() && last_source != source)
83                 {
84                         if (last_destination == source)
85                                 previous_frame = last_source;
86                         else // A two frame jump
87                                 previous_frame = last_destination;
88                 }
89
90                 last_source                     = source;
91                 last_destination        = destination;
92
93                 bool has_previous = previous_frame != draw_frame::empty();
94
95                 if (!has_previous)
96                         return blend(source, destination, distance);
97
98                 auto middle                                                                                     = last_source;
99                 auto next_frame                                                                         = destination;
100                 previous_frame.transform().image_transform.is_mix       = true;
101                 middle.transform().image_transform.is_mix                       = true;
102                 next_frame.transform().image_transform.is_mix           = true;
103
104                 double float_distance                                                           = boost::rational_cast<double>(distance);
105                 previous_frame.transform().image_transform.opacity      = std::max(0.0, 0.5 - float_distance * 0.5);
106                 middle.transform().image_transform.opacity                      = 0.5;
107                 next_frame.transform().image_transform.opacity          = 1.0 - previous_frame.transform().image_transform.opacity - middle.transform().image_transform.opacity;
108
109                 std::vector<draw_frame> combination { previous_frame, middle, next_frame };
110
111                 return draw_frame(std::move(combination));
112         }
113 };
114
115 class audio_extractor : public frame_visitor
116 {
117         std::stack<core::audio_transform>                               transform_stack_;
118         std::function<void(const const_frame& frame)>   on_frame_;
119 public:
120         audio_extractor(std::function<void(const const_frame& frame)> on_frame)
121                 : on_frame_(std::move(on_frame))
122         {
123                 transform_stack_.push(audio_transform());
124         }
125
126         void push(const frame_transform& transform) override
127         {
128                 transform_stack_.push(transform_stack_.top() * transform.audio_transform);
129         }
130
131         void pop() override
132         {
133                 transform_stack_.pop();
134         }
135
136         void visit(const const_frame& frame) override
137         {
138                 if (!frame.audio_data().empty() && !transform_stack_.top().is_still)
139                         on_frame_(frame);
140         }
141 };
142
143 // Like tweened_transform but for framerates
144 class speed_tweener
145 {
146         boost::rational<int64_t>        source_         = 1LL;
147         boost::rational<int64_t>        dest_           = 1LL;
148         int                                                     duration_       = 0;
149         int                                                     time_           = 0;
150         tweener                                         tweener_;
151 public:
152         speed_tweener() = default;
153         speed_tweener(
154                         const boost::rational<int64_t>& source,
155                         const boost::rational<int64_t>& dest,
156                         int duration,
157                         const tweener& tween)
158                 : source_(source)
159                 , dest_(dest)
160                 , duration_(duration)
161                 , time_(0)
162                 , tweener_(tween)
163         {
164         }
165
166         const boost::rational<int64_t>& dest() const
167         {
168                 return dest_;
169         }
170
171         boost::rational<int64_t> fetch() const
172         {
173                 if (time_ == duration_)
174                         return dest_;
175
176                 double source   = boost::rational_cast<double>(source_);
177                 double delta    = boost::rational_cast<double>(dest_) - source;
178                 double result   = tweener_(time_, source, delta, duration_);
179                 
180                 return boost::rational<int64_t>(static_cast<int64_t>(result * 1000000.0), 1000000);
181         }
182
183         boost::rational<int64_t> fetch_and_tick()
184         {
185                 time_ = std::min(time_ + 1, duration_);
186                 return fetch();
187         }
188 };
189
190 class framerate_producer : public frame_producer_base
191 {
192         spl::shared_ptr<frame_producer>                                         source_;
193         boost::rational<int>                                                            source_framerate_;
194         audio_channel_layout                                                            source_channel_layout_          = audio_channel_layout::invalid();
195         boost::rational<int>                                                            destination_framerate_;
196         field_mode                                                                                      destination_fieldmode_;
197         std::vector<int>                                                                        destination_audio_cadence_;
198         boost::rational<std::int64_t>                                           speed_;
199         speed_tweener                                                                           user_speed_;
200         std::function<draw_frame (
201                         const draw_frame& source,
202                         const draw_frame& destination,
203                         const boost::rational<int64_t>& distance)>      interpolator_                           = drop_and_skip;
204         
205         boost::rational<std::int64_t>                                           current_frame_number_           = 0;
206         draw_frame                                                                                      previous_frame_                         = draw_frame::empty();
207         draw_frame                                                                                      next_frame_                                     = draw_frame::empty();
208         mutable_audio_buffer                                                            audio_samples_;
209
210         unsigned int                                                                            output_repeat_                          = 0;
211         unsigned int                                                                            output_frame_                           = 0;
212 public:
213         framerate_producer(
214                         spl::shared_ptr<frame_producer> source,
215                         boost::rational<int> source_framerate,
216                         boost::rational<int> destination_framerate,
217                         field_mode destination_fieldmode,
218                         std::vector<int> destination_audio_cadence)
219                 : source_(std::move(source))
220                 , source_framerate_(std::move(source_framerate))
221                 , destination_framerate_(std::move(destination_framerate))
222                 , destination_fieldmode_(destination_fieldmode)
223                 , destination_audio_cadence_(std::move(destination_audio_cadence))
224         {
225                 // Coarse adjustment to correct fps family (23.98 - 30 vs 47.95 - 60)
226                 if (destination_fieldmode_ != field_mode::progressive)  // Interlaced output
227                 {
228                         auto diff_double        = boost::abs(source_framerate_ - destination_framerate_ * 2);
229                         auto diff_keep          = boost::abs(source_framerate_ - destination_framerate_);
230
231                         if (diff_double < diff_keep)                                            // Double rate interlaced
232                         {
233                                 destination_framerate_ *= 2;
234                         }
235                         else                                                                                            // Progressive non interlaced
236                         {
237                                 destination_fieldmode_ = field_mode::progressive;
238                         }
239                 }
240                 else                                                                                                    // Progressive
241                 {
242                         auto diff_halve = boost::abs(source_framerate_ * 2      - destination_framerate_);
243                         auto diff_keep  = boost::abs(source_framerate_          - destination_framerate_);
244
245                         if (diff_halve < diff_keep)                                                     // Repeat every frame two times
246                         {
247                                 destination_framerate_  /= 2;
248                                 output_repeat_                  = 2;
249                         }
250                 }
251
252                 speed_ = boost::rational<int64_t>(source_framerate_ / destination_framerate_);
253
254                 // drop_and_skip will only be used by default for exact framerate multiples (half, same and double)
255                 // for all other framerates a frame interpolator will be chosen.
256                 if (speed_ != 1 && speed_ * 2 != 1 && speed_ != 2)
257                 {
258                         auto high_source_framerate              = source_framerate_ > 47;
259                         auto high_destination_framerate = destination_framerate_ > 47
260                                         || destination_fieldmode_ != field_mode::progressive;
261
262                         if (high_source_framerate && high_destination_framerate)        // The bluriness of blend_all is acceptable on high framerates.
263                                 interpolator_ = blend_all();
264                         else                                                                                                            // blend_all is mostly too blurry on low framerates. blend provides a compromise.
265                                 interpolator_ = &blend;
266
267                         CASPAR_LOG(warning) << source_->print() << L" Frame blending frame rate conversion required to conform to channel frame rate.";
268                 }
269
270                 // Note: Uses 1 step rotated cadence for 1001 modes (1602, 1602, 1601, 1602, 1601)
271                 // This cadence fills the audio mixer most optimally.
272                 boost::range::rotate(destination_audio_cadence_, std::end(destination_audio_cadence_) - 1);
273         }
274
275         draw_frame receive_impl() override
276         {
277                 if (destination_fieldmode_ == field_mode::progressive)
278                 {
279                         return do_render_progressive_frame(true);
280                 }
281                 else
282                 {
283                         auto field1 = do_render_progressive_frame(true);
284                         auto field2 = do_render_progressive_frame(false);
285
286                         return draw_frame::interlace(field1, field2, destination_fieldmode_);
287                 }
288         }
289
290         std::future<std::wstring> call(const std::vector<std::wstring>& params) override
291         {
292                 if (!boost::iequals(params.at(0), L"framerate"))
293                         return source_->call(params);
294
295                 if (boost::iequals(params.at(1), L"speed"))
296                 {
297                         auto destination_user_speed = boost::rational<std::int64_t>(
298                                         static_cast<std::int64_t>(boost::lexical_cast<double>(params.at(2)) * 1000000.0),
299                                         1000000);
300                         auto frames = params.size() > 3 ? boost::lexical_cast<int>(params.at(3)) : 0;
301                         auto easing = params.size() > 4 ? params.at(4) : L"linear";
302
303                         user_speed_ = speed_tweener(user_speed_.fetch(), destination_user_speed, frames, tweener(easing));
304                 }
305                 else if (boost::iequals(params.at(1), L"interpolation"))
306                 {
307                         if (boost::iequals(params.at(2), L"blend"))
308                                 interpolator_ = &blend;
309                         else if (boost::iequals(params.at(2), L"blend_all"))
310                                 interpolator_ = blend_all();
311                         else
312                                 interpolator_ = &drop_and_skip;
313                 }
314                 else if (boost::iequals(params.at(1), L"output_repeat")) // Only for debugging purposes
315                 {
316                         output_repeat_ = boost::lexical_cast<unsigned int>(params.at(2));
317                 }
318
319                 return make_ready_future<std::wstring>(L"");
320         }
321
322         monitor::subject& monitor_output() override
323         {
324                 return source_->monitor_output();
325         }
326
327         std::wstring print() const override
328         {
329                 return source_->print();
330         }
331
332         std::wstring name() const override
333         {
334                 return source_->name();
335         }
336
337         boost::property_tree::wptree info() const override
338         {
339                 auto info = source_->info();
340
341                 auto incorrect_frame_number = info.get_child_optional(L"frame-number");
342                 if (incorrect_frame_number)
343                         incorrect_frame_number->put_value(frame_number());
344
345                 auto incorrect_nb_frames = info.get_child_optional(L"nb-frames");
346                 if (incorrect_nb_frames)
347                         incorrect_nb_frames->put_value(nb_frames());
348
349                 return info;
350         }
351
352         uint32_t nb_frames() const override
353         {
354                 auto source_nb_frames = source_->nb_frames();
355                 auto multiple = boost::rational_cast<double>(1 / get_speed() * (output_repeat_ != 0 ? 2 : 1));
356
357                 return static_cast<uint32_t>(source_nb_frames * multiple);
358         }
359
360         uint32_t frame_number() const override
361         {
362                 auto source_frame_number = source_->frame_number() - 1; // next frame already received
363                 auto multiple = boost::rational_cast<double>(1 / get_speed() * (output_repeat_ != 0 ? 2 : 1));
364
365                 return static_cast<uint32_t>(source_frame_number * multiple);
366         }
367
368         constraints& pixel_constraints() override
369         {
370                 return source_->pixel_constraints();
371         }
372 private:
373         draw_frame do_render_progressive_frame(bool sound)
374         {
375                 user_speed_.fetch_and_tick();
376
377                 if (output_repeat_ && ++output_frame_ % output_repeat_)
378                 {
379                         auto frame = draw_frame::still(last_frame());
380
381                         frame.transform().audio_transform.volume = 0.0;
382
383                         return attach_sound(frame);
384                 }
385
386                 if (previous_frame_ == draw_frame::empty())
387                         previous_frame_ = pop_frame_from_source();
388
389                 auto current_frame_number       = current_frame_number_;
390                 auto distance                           = current_frame_number_ - boost::rational_cast<int64_t>(current_frame_number_);
391                 bool needs_next                         = distance > 0 || !enough_sound();
392
393                 if (needs_next && next_frame_ == draw_frame::empty())
394                         next_frame_ = pop_frame_from_source();
395
396                 auto result = interpolator_(previous_frame_, next_frame_, distance);
397
398                 auto next_frame_number          = current_frame_number_ += get_speed();
399                 auto integer_current_frame      = boost::rational_cast<std::int64_t>(current_frame_number);
400                 auto integer_next_frame         = boost::rational_cast<std::int64_t>(next_frame_number);
401
402                 fast_forward_integer_frames(integer_next_frame - integer_current_frame);
403
404                 if (sound)
405                         return attach_sound(result);
406                 else
407                         return result;
408         }
409
410         void fast_forward_integer_frames(std::int64_t num_frames)
411         {
412                 if (num_frames == 0)
413                         return;
414
415                 for (std::int64_t i = 0; i < num_frames; ++i)
416                 {
417                         if (next_frame_ == draw_frame::empty())
418                                 previous_frame_ = pop_frame_from_source();
419                         else
420                         {
421                                 previous_frame_ = std::move(next_frame_);
422
423                                 next_frame_ = pop_frame_from_source();
424                         }
425                 }
426         }
427
428         boost::rational<std::int64_t> get_speed() const
429         {
430                 return speed_ * user_speed_.fetch();
431         }
432
433         draw_frame pop_frame_from_source()
434         {
435                 auto frame = source_->receive();
436
437                 if (user_speed_.fetch() == 1)
438                 {
439                         audio_extractor extractor([this](const const_frame& frame)
440                         {
441                                 if (source_channel_layout_ != frame.audio_channel_layout())
442                                 {
443                                         source_channel_layout_ = frame.audio_channel_layout();
444
445                                         // Insert silence samples so that the audio mixer is guaranteed to be filled.
446                                         auto min_num_samples_per_frame  = *boost::min_element(destination_audio_cadence_);
447                                         auto max_num_samples_per_frame  = *boost::max_element(destination_audio_cadence_);
448                                         auto cadence_safety_samples             = max_num_samples_per_frame - min_num_samples_per_frame;
449                                         audio_samples_.resize(source_channel_layout_.num_channels * cadence_safety_samples, 0);
450                                 }
451
452                                 auto& buffer = frame.audio_data();
453                                 audio_samples_.insert(audio_samples_.end(), buffer.begin(), buffer.end());
454                         });
455
456                         frame.accept(extractor);
457                 }
458                 else
459                 {
460                         source_channel_layout_ = audio_channel_layout::invalid();
461                         audio_samples_.clear();
462                 }
463
464                 frame.transform().audio_transform.volume = 0.0;
465
466                 return frame;
467         }
468
469         draw_frame attach_sound(draw_frame frame)
470         {
471                 if (user_speed_.fetch() != 1 || source_channel_layout_ == audio_channel_layout::invalid())
472                         return frame;
473
474                 mutable_audio_buffer buffer;
475
476                 if (destination_audio_cadence_.front() * source_channel_layout_.num_channels == audio_samples_.size())
477                 {
478                         buffer.swap(audio_samples_);
479                 }
480                 else if (audio_samples_.size() >= destination_audio_cadence_.front() * source_channel_layout_.num_channels)
481                 {
482                         auto begin      = audio_samples_.begin();
483                         auto end        = begin + destination_audio_cadence_.front() * source_channel_layout_.num_channels;
484
485                         buffer.insert(buffer.begin(), begin, end);
486                         audio_samples_.erase(begin, end);
487                 }
488                 else
489                 {
490                         auto needed = destination_audio_cadence_.front();
491                         auto got = audio_samples_.size() / source_channel_layout_.num_channels;
492                         if (got != 0) // If at end of stream we don't care
493                                 CASPAR_LOG(debug) << print() << L" Too few audio samples. Needed " << needed << L" but got " << got;
494                         buffer.swap(audio_samples_);
495                         buffer.resize(needed * source_channel_layout_.num_channels, 0);
496                 }
497
498                 boost::range::rotate(destination_audio_cadence_, std::begin(destination_audio_cadence_) + 1);
499
500                 auto audio_frame = mutable_frame(
501                                 {},
502                                 std::move(buffer),
503                                 this,
504                                 pixel_format_desc(),
505                                 source_channel_layout_);
506                 return draw_frame::over(frame, draw_frame(std::move(audio_frame)));
507         }
508
509         bool enough_sound() const
510         {
511                 return source_channel_layout_ == core::audio_channel_layout::invalid()
512                                 || user_speed_.fetch() != 1
513                                 || audio_samples_.size() / source_channel_layout_.num_channels >= destination_audio_cadence_.at(0);
514         }
515 };
516
517 void describe_framerate_producer(help_sink& sink)
518 {
519         sink.para()->text(L"Framerate conversion control / Slow motion examples:");
520         sink.example(L">> CALL 1-10 FRAMERATE INTERPOLATION BLEND", L"enables 2 frame blend interpolation.");
521         sink.example(L">> CALL 1-10 FRAMERATE INTERPOLATION BLEND_ALL", L"enables 3 frame blend interpolation.");
522         sink.example(L">> CALL 1-10 FRAMERATE INTERPOLATION DROP_AND_SKIP", L"disables frame interpolation.");
523         sink.example(L">> CALL 1-10 FRAMERATE SPEED 0.25", L"immediately changes the speed to 25%. Sound will be disabled.");
524         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.");
525         sink.example(L">> CALL 1-10 FRAMERATE SPEED 0.25 50 easeinoutsine", L"changes the speed to 25% over 50 frames using specified easing curve. Sound will be disabled.");
526         sink.example(L">> CALL 1-10 FRAMERATE SPEED 1 50", L"changes the speed to 100% linearly over 50 frames. Sound will be enabled when the destination speed of 100% has been reached.");
527 }
528
529 spl::shared_ptr<frame_producer> create_framerate_producer(
530                 spl::shared_ptr<frame_producer> source,
531                 boost::rational<int> source_framerate,
532                 boost::rational<int> destination_framerate,
533                 field_mode destination_fieldmode,
534                 std::vector<int> destination_audio_cadence)
535 {
536         return spl::make_shared<framerate_producer>(
537                         std::move(source),
538                         std::move(source_framerate),
539                         std::move(destination_framerate),
540                         destination_fieldmode,
541                         std::move(destination_audio_cadence));
542 }
543
544 }}
545