-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathdisplay.cpp
More file actions
3665 lines (2994 loc) · 145 KB
/
display.cpp
File metadata and controls
3665 lines (2994 loc) · 145 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "display.h"
#include <libgen.h>
#include <algorithm>
#include <atomic>
#include <future>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <regex>
#include <sstream>
#include <stdexcept>
#include <string>
#include <thread>
#include "controls.h"
#include "ffmpeg.h"
#include "format_converter.h"
#include "png_saver.h"
#include "scope_window.h"
#include "source_code_pro_regular_ttf.h"
#include "version.h"
#include "video_compare_icon.h"
#include "vmaf_calculator.h"
extern "C" {
#include <libavfilter/avfilter.h>
#include <libavutil/imgutils.h>
#include <libswresample/swresample.h>
#include <libswscale/swscale.h>
}
static const SDL_Color BACKGROUND_COLOR = {54, 69, 79, 0};
static const SDL_Color LOOP_OFF_LABEL_COLOR = {0, 0, 0, 0};
static const SDL_Color LOOP_FW_LABEL_COLOR = {80, 127, 255, 0};
static const SDL_Color LOOP_PP_LABEL_COLOR = {191, 95, 60, 0};
static const SDL_Color TEXT_COLOR = {255, 255, 255, 0};
static const SDL_Color HELP_TEXT_PRIMARY_COLOR = {255, 255, 255, 0};
static const SDL_Color HELP_TEXT_ALTERNATE_COLOR = {255, 255, 192, 0};
static const SDL_Color POSITION_COLOR = {255, 255, 192, 0};
static const SDL_Color TARGET_COLOR = {200, 200, 140, 0};
static const SDL_Color ZOOM_COLOR = {255, 165, 0, 0};
static const SDL_Color PLAYBACK_SPEED_COLOR = {0, 192, 160, 0};
static const SDL_Color BUFFER_COLOR = {160, 225, 192, 0};
static const int BACKGROUND_ALPHA = 100;
static const int MOUSE_WHEEL_SCROLL_STEPS_TO_DOUBLE = 12;
static const float ZOOM_STEP_SIZE = pow(2.0F, 1.0F / float(MOUSE_WHEEL_SCROLL_STEPS_TO_DOUBLE));
static const float ZOOM_SLOWDOWN_RATIO = 3.0F;
static const int PLAYBACK_SPEED_KEY_PRESSES_TO_DOUBLE = 6;
static const float PLAYBACK_SPEED_STEP_SIZE = pow(2.0F, 1.0F / float(PLAYBACK_SPEED_KEY_PRESSES_TO_DOUBLE));
static const float PLAYBACK_SPEED_SLOWDOWN_RATIO = 5.0F;
static const float RELATIVE_SEEK_SLOWDOWN_RATIO = 4.0F;
static const int HELP_TEXT_LINE_SPACING = 1;
static const int HELP_TEXT_HORIZONTAL_MARGIN = 26;
static const int MIN_WINDOW_WIDTH = 4;
static const int MIN_WINDOW_HEIGHT = 1;
auto frame_deleter = [](AVFrame* frame) {
av_freep(&frame->data[0]);
av_frame_free(&frame);
};
using AVFramePtr = std::unique_ptr<AVFrame, decltype(frame_deleter)>;
template <typename T>
inline T check_sdl(T value, const std::string& message) {
if (!value) {
throw std::runtime_error{"SDL " + message + " - " + SDL_GetError()};
}
return value;
}
template <typename T>
inline T clamp_range(T v, T lo, T hi) {
return (v < lo) ? lo : (v > hi) ? hi : v;
}
inline uint32_t clamp_u32(int v, uint32_t hi) {
return (v < 0) ? 0u : (v > (int)hi ? hi : (uint32_t)v);
}
inline int clamp_int_to_byte_range(int value) {
return clamp_range(value, 0, 255);
}
inline int clamp_int_to_10_bpc_range(int value) {
return clamp_range(value, 0, 1023);
}
inline uint8_t clamp_int_to_byte(int value) {
return static_cast<uint8_t>(clamp_int_to_byte_range(value));
}
inline uint16_t clamp_int_to_10_bpc(int value) {
return static_cast<uint16_t>(clamp_int_to_10_bpc_range(value));
}
inline int luma709(int r, int g, int b) {
return (217 * r + 733 * g + 74 * b) >> 10;
}
template <int Bpc>
struct BitDepthTraits;
template <>
struct BitDepthTraits<8> {
using P = uint8_t;
static constexpr uint32_t MaxCode = 255u;
static constexpr int PackShift = 0; // stored as 8b
static inline int to10(int v) { return v; } // already 8-bit working domain
static inline P from10(uint32_t v) { return (P)clamp_u32((int)v, MaxCode); }
};
template <>
struct BitDepthTraits<10> {
using P = uint16_t;
static constexpr uint32_t MaxCode = 1023u;
static constexpr int PackShift = 6; // stored as 16b with <<6
static inline int to10(int v) { return v; } // values in working domain are 10b
static inline P from10(uint32_t v) { return (P)(clamp_u32((int)v, MaxCode) << PackShift); }
};
// Credits to Kemin Zhou for this approach which does not require Boost or C++17
// https://stackoverflow.com/questions/4430780/how-can-i-extract-the-file-name-and-extension-from-a-path-in-c
std::string get_file_name_and_extension(const std::string& file_path) {
char* buff = new char[file_path.size() + 1];
strcpy(buff, file_path.c_str());
const std::string result = std::string(basename(buff));
delete[] buff;
return result;
}
std::string get_file_stem(const std::string& file_path) {
std::string tmp = get_file_name_and_extension(file_path);
const std::string::size_type i = tmp.rfind('.');
if (i != std::string::npos) {
tmp = tmp.substr(0, i);
}
return tmp;
}
static bool should_suffix_right_file_number_1(const std::string& left_file_name, const std::string& right_file_name) {
return get_file_name_and_extension(left_file_name) == get_file_name_and_extension(right_file_name);
}
static std::string format_right_file_label(const std::string& left_file_name, const std::string& right_file_name, const size_t right_file_number) {
if (right_file_number >= 2 || (right_file_number == 1 && should_suffix_right_file_number_1(left_file_name, right_file_name))) {
return right_file_name + string_sprintf(" <%zu>", right_file_number);
}
return right_file_name;
}
std::string format_window_title(const std::string& left_file_name, const std::string& right_file_name) {
return string_sprintf("%s | %s", get_file_name_and_extension(left_file_name).c_str(), get_file_name_and_extension(right_file_name).c_str());
}
std::string strip_ffmpeg_patterns(const std::string& input) {
static const std::regex pattern_regex(R"(%\d*d|\*|\?)");
return std::regex_replace(input, pattern_regex, "");
};
inline float round_3(float value) {
return std::round(value * 1000.0F) / 1000.0F;
}
static std::string format_position_difference(const float position1, const float position2) {
// round both for the sake of consistency with the displayed positions
const float position1_rounded = round_3(position1);
const float position2_rounded = round_3(position2);
// absolute difference very close to 0.001 -> we are in sync!
if (std::abs(position1_rounded - position2_rounded) < 9.99e-4) {
return "";
} else if (position1 < position2) {
return " (-" + format_position(position2_rounded - position1_rounded, true) + ")";
}
return " (+" + format_position(position1_rounded - position2_rounded, true) + ")";
}
static std::string to_hex(const uint32_t value, const int width) {
std::stringstream sstream;
sstream << std::setfill('0') << std::setw(width) << std::hex << value;
return sstream.str();
}
static std::string format_libav_version(unsigned version) {
int major = (version >> 16) & 0xff;
int minor = (version >> 8) & 0xff;
int micro = version & 0xff;
return string_sprintf("%2u.%2u.%3u", major, minor, micro);
}
auto get_metadata_int_value = [](const AVFrame* frame, const std::string& key, const int default_value) -> int {
const AVDictionaryEntry* entry = av_dict_get(frame->metadata, key.c_str(), nullptr, 0);
return entry ? std::atoi(entry->value) : default_value;
};
SDL::SDL() {
check_sdl(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == 0, "SDL init");
check_sdl(TTF_Init() == 0, "TTF init");
}
SDL::~SDL() {
SDL_Quit();
}
Display::Display(const int display_number,
const Mode mode,
const bool verbose,
const bool fit_window_to_usable_bounds,
const bool high_dpi_allowed,
const AspectLockMode aspect_lock_mode,
const AspectViewMode aspect_view_mode,
const bool use_10_bpc,
const bool fast_input_alignment,
const bool bilinear_texture_filtering,
const std::tuple<int, int> window_size,
const unsigned width,
const unsigned height,
const double duration,
const float wheel_sensitivity,
const bool start_in_subtraction_mode,
const bool start_in_fullscreen,
const std::string& left_file_name,
const std::string& right_file_name)
: display_number_{display_number},
mode_{mode},
fit_window_to_usable_bounds_{fit_window_to_usable_bounds},
high_dpi_allowed_{high_dpi_allowed},
aspect_lock_mode_{aspect_lock_mode},
aspect_view_mode_{aspect_view_mode},
use_10_bpc_{use_10_bpc},
fast_input_alignment_{fast_input_alignment},
bilinear_texture_filtering_{bilinear_texture_filtering},
video_width_{0},
video_height_{0},
duration_{duration},
start_in_fullscreen_{start_in_fullscreen},
subtraction_mode_{start_in_subtraction_mode},
pending_verbose_print_{verbose},
wheel_sensitivity_{wheel_sensitivity} {
const int auto_width = mode == Mode::HStack ? width * 2 : width;
const int auto_height = mode == Mode::VStack ? height * 2 : height;
int window_x;
int window_y;
int window_width;
int window_height;
// account for window frame and title bar
constexpr int border_width = 10;
#ifdef __linux__
constexpr int border_height = 40;
#else
constexpr int border_height = 34;
#endif
SDL_Rect bounds;
check_sdl(SDL_GetDisplayUsableBounds(display_number, &bounds) == 0, "get display usable bounds");
if (!fit_window_to_usable_bounds) {
if (std::get<0>(window_size) < 0 && std::get<1>(window_size) < 0) {
window_width = auto_width;
window_height = auto_height;
} else {
if (std::get<0>(window_size) < 0) {
window_height = std::get<1>(window_size);
window_width = static_cast<float>(auto_width) / static_cast<float>(auto_height) * window_height;
} else if (std::get<1>(window_size) < 0) {
window_width = std::get<0>(window_size);
window_height = static_cast<float>(auto_height) / static_cast<float>(auto_width) * window_width;
} else {
window_width = std::get<0>(window_size);
window_height = std::get<1>(window_size);
}
}
window_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display_number);
window_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display_number);
if (high_dpi_allowed_) {
window_width /= 2;
window_height /= 2;
}
} else {
const int usable_width = std::max(bounds.w - border_width, MIN_WINDOW_WIDTH);
const int usable_height = std::max(bounds.h - border_height, MIN_WINDOW_HEIGHT);
const float aspect_ratio = static_cast<float>(auto_width) / static_cast<float>(auto_height);
const float usable_aspect_ratio = static_cast<float>(usable_width) / static_cast<float>(usable_height);
if (usable_aspect_ratio > aspect_ratio) {
window_height = usable_height;
window_width = static_cast<int>(window_height * aspect_ratio);
} else {
window_width = usable_width;
window_height = static_cast<int>(window_width / aspect_ratio);
}
window_x = bounds.x + (usable_width - window_width + border_width) / 2;
window_y = bounds.y + (usable_height - window_height + border_height) / 2 + border_width;
#ifdef __linux__
window_y -= 2 * border_width + 4;
#endif
}
if (window_width < MIN_WINDOW_WIDTH) {
throw std::runtime_error{"Window width cannot be less than " + std::to_string(MIN_WINDOW_WIDTH)};
}
if (window_height < MIN_WINDOW_HEIGHT) {
throw std::runtime_error{"Window height cannot be less than " + std::to_string(MIN_WINDOW_HEIGHT)};
}
const int create_window_flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
window_ = check_sdl(SDL_CreateWindow(format_window_title(left_file_name, right_file_name).c_str(), window_x, window_y, window_width, window_height, high_dpi_allowed_ ? create_window_flags | SDL_WINDOW_ALLOW_HIGHDPI : create_window_flags),
"window");
SDL_RWops* embedded_icon = check_sdl(SDL_RWFromConstMem(VIDEO_COMPARE_ICON_BMP, VIDEO_COMPARE_ICON_BMP_LEN), "get pointer to icon");
SDL_Surface* icon_surface = check_sdl(SDL_LoadBMP_RW(embedded_icon, 1), "load icon");
#ifdef _WIN32
SDL_Surface* resized_icon_surface = SDL_CreateRGBSurface(0, 64, 64, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
SDL_BlitScaled(icon_surface, nullptr, resized_icon_surface, nullptr);
SDL_SetWindowIcon(window_, resized_icon_surface);
SDL_FreeSurface(resized_icon_surface);
#else
SDL_SetWindowIcon(window_, icon_surface);
#endif
SDL_FreeSurface(icon_surface);
renderer_ = check_sdl(SDL_CreateRenderer(window_, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC), "renderer");
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
SDL_RenderClear(renderer_);
SDL_RenderPresent(renderer_);
SDL_GL_GetDrawableSize(window_, &drawable_width_, &drawable_height_);
SDL_GetWindowSize(window_, &window_width_, &window_height_);
window_aspect_ratio_ = static_cast<float>(window_width_) / static_cast<float>(std::max(1, window_height_));
startup_window_size_ = {window_width_, window_height_};
saved_window_size_ = startup_window_size_;
// Check if window is larger than display and warn user
const int usable_width = bounds.w - border_width;
const int usable_height = bounds.h - border_height;
if (window_width_ > usable_width || window_height_ > usable_height) {
std::cout << "WARNING: Window size (" << window_width_ << "x" << window_height_ << ") exceeds display area (" << usable_width << "x" << usable_height
<< "). Consider reducing the window size (use -W flag to resize) or using a larger display." << std::endl;
set_pending_message("Window exceeds display area (use -W flag to resize)");
}
drawable_to_window_width_factor_ = static_cast<float>(drawable_width_) / static_cast<float>(window_width_);
drawable_to_window_height_factor_ = static_cast<float>(drawable_height_) / static_cast<float>(window_height_);
content_window_ = SDL_Rect{0, 0, window_width_, window_height_};
video_to_window_width_factor_ = 1.0F;
video_to_window_height_factor_ = 1.0F;
font_scale_ = (drawable_to_window_width_factor_ + drawable_to_window_height_factor_) / 2.0F;
border_extension_ = 3 * font_scale_;
double_border_extension_ = border_extension_ * 2;
line1_y_ = 20;
line2_y_ = line1_y_ + 30 * font_scale_;
if (mode_ != Mode::VStack) {
max_text_width_ = drawable_width_ / 2 - double_border_extension_ - line1_y_;
} else {
max_text_width_ = drawable_width_ - double_border_extension_ - line1_y_;
}
rebuild_fonts();
normal_mode_cursor_ = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
pan_mode_cursor_ = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
selection_mode_cursor_ = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR);
SDL_RenderSetLogicalSize(renderer_, drawable_width_, drawable_height_);
// Store left/right names before reinitializing dimensions since it may refresh title text.
left_file_name_ = left_file_name;
right_file_name_ = right_file_name;
last_window_title_.clear();
reinitialize_video_dimensions(width, height);
rebuild_side_ui_textures();
refresh_display_side_mapping();
rebuild_help_textures();
if (start_in_fullscreen_) {
set_fullscreen(true);
}
}
Display::~Display() {
SDL_DestroyTexture(video_texture_linear_);
SDL_DestroyTexture(video_texture_nn_);
SDL_DestroyTexture(side_ui_[LEFT.as_simple_index()].text_texture);
SDL_DestroyTexture(side_ui_[RIGHT.as_simple_index()].text_texture);
if (message_texture_ != nullptr) {
SDL_DestroyTexture(message_texture_);
}
for (auto help_texture : help_textures_) {
SDL_DestroyTexture(help_texture);
}
TTF_CloseFont(small_font_);
TTF_CloseFont(big_font_);
SDL_FreeCursor(normal_mode_cursor_);
SDL_FreeCursor(pan_mode_cursor_);
SDL_FreeCursor(selection_mode_cursor_);
delete[] diff_buffer_;
if (left_buffer_ != nullptr) {
delete[] left_buffer_;
}
if (right_buffer_ != nullptr) {
delete[] right_buffer_;
}
SDL_DestroyRenderer(renderer_);
SDL_DestroyWindow(window_);
}
void Display::recreate_video_textures_for_current_mode() {
if (video_texture_linear_ != nullptr) {
SDL_DestroyTexture(video_texture_linear_);
video_texture_linear_ = nullptr;
}
if (video_texture_nn_ != nullptr) {
SDL_DestroyTexture(video_texture_nn_);
video_texture_nn_ = nullptr;
}
auto create_video_texture = [&](const std::string& scale_quality) {
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, scale_quality.c_str());
return check_sdl(SDL_CreateTexture(renderer_, use_10_bpc_ ? SDL_PIXELFORMAT_ARGB2101010 : SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, mode_ == Mode::HStack ? video_width_ * 2 : video_width_,
mode_ == Mode::VStack ? video_height_ * 2 : video_height_),
"video texture " + scale_quality);
};
video_texture_linear_ = create_video_texture("linear");
video_texture_nn_ = create_video_texture("nearest");
}
void Display::apply_window_size_and_relayout(const int target_w, const int target_h, const bool force_layout_refresh) {
SDL_SetWindowSize(window_, target_w, target_h);
handle_window_resize(true, force_layout_refresh);
}
void Display::set_fullscreen(const bool fullscreen) {
int current_window_w = window_width_;
int current_window_h = window_height_;
SDL_GetWindowSize(window_, ¤t_window_w, ¤t_window_h);
const Uint32 flags_before = SDL_GetWindowFlags(window_);
const bool sdl_fullscreen_before = (flags_before & SDL_WINDOW_FULLSCREEN) != 0;
if (fullscreen == sdl_fullscreen_before) {
return;
}
if (fullscreen) {
windowed_size_before_fullscreen_ = {current_window_w, current_window_h};
}
if (SDL_SetWindowFullscreen(window_, fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0) != 0) {
set_pending_message(string_sprintf("Unable to %s fullscreen (%s)", fullscreen ? "enter" : "exit", SDL_GetError()));
return;
}
const bool sdl_fullscreen_after = (SDL_GetWindowFlags(window_) & SDL_WINDOW_FULLSCREEN) != 0;
if (sdl_fullscreen_after != fullscreen) {
set_pending_message(string_sprintf("Unable to %s fullscreen mode", fullscreen ? "enter" : "exit"));
return;
}
if (!fullscreen && windowed_size_before_fullscreen_[0] > 0 && windowed_size_before_fullscreen_[1] > 0) {
apply_window_size_and_relayout(std::max(MIN_WINDOW_WIDTH, windowed_size_before_fullscreen_[0]), std::max(MIN_WINDOW_HEIGHT, windowed_size_before_fullscreen_[1]), true);
} else {
handle_window_resize(true, true);
}
}
bool Display::detect_fullscreen_like_state() const {
const Uint32 flags = SDL_GetWindowFlags(window_);
if ((flags & SDL_WINDOW_FULLSCREEN) != 0) {
return true;
}
if ((flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_BORDERLESS)) == 0) {
return false;
}
int window_x = 0;
int window_y = 0;
int window_w = 0;
int window_h = 0;
SDL_GetWindowSize(window_, &window_w, &window_h);
SDL_GetWindowPosition(window_, &window_x, &window_y);
int display_index = SDL_GetWindowDisplayIndex(window_);
if (display_index < 0) {
display_index = display_number_;
}
SDL_Rect display_bounds{};
SDL_Rect usable_bounds{};
if (SDL_GetDisplayBounds(display_index, &display_bounds) != 0 || SDL_GetDisplayUsableBounds(display_index, &usable_bounds) != 0) {
return false;
}
constexpr int tolerance = 4;
const auto near = [&](const int a, const int b) { return std::abs(a - b) <= tolerance; };
const bool fills_display_bounds = near(window_x, display_bounds.x) && near(window_y, display_bounds.y) && near(window_w, display_bounds.w) && near(window_h, display_bounds.h);
const bool fills_usable_bounds = near(window_x, usable_bounds.x) && near(window_y, usable_bounds.y) && near(window_w, usable_bounds.w) && near(window_h, usable_bounds.h);
return fills_display_bounds || fills_usable_bounds;
}
std::array<int, 2> Display::compute_mode_switch_target_window_size() const {
const float target_aspect_ratio = std::max(compute_active_content_aspect_ratio(), 0.001F);
const double current_area = static_cast<double>(std::max(window_width_, MIN_WINDOW_WIDTH)) * static_cast<double>(std::max(window_height_, MIN_WINDOW_HEIGHT));
int target_w = std::max(MIN_WINDOW_WIDTH, static_cast<int>(std::round(std::sqrt(current_area * target_aspect_ratio))));
int target_h = std::max(MIN_WINDOW_HEIGHT, static_cast<int>(std::round(std::sqrt(current_area / target_aspect_ratio))));
int display_index = SDL_GetWindowDisplayIndex(window_);
if (display_index < 0) {
display_index = display_number_;
}
SDL_Rect bounds;
if (SDL_GetDisplayUsableBounds(display_index, &bounds) == 0) {
const int max_w = std::max(1, bounds.w);
const int max_h = std::max(1, bounds.h);
if (target_w > max_w || target_h > max_h) {
const float scale = std::min(static_cast<float>(max_w) / static_cast<float>(std::max(1, target_w)), static_cast<float>(max_h) / static_cast<float>(std::max(1, target_h)));
target_w = std::max(1, static_cast<int>(std::round(static_cast<float>(target_w) * scale)));
target_h = std::max(1, static_cast<int>(std::round(static_cast<float>(target_h) * scale)));
}
const int min_w = std::min(MIN_WINDOW_WIDTH, max_w);
const int min_h = std::min(MIN_WINDOW_HEIGHT, max_h);
target_w = std::max(min_w, std::min(target_w, max_w));
target_h = std::max(min_h, std::min(target_h, max_h));
}
return {target_w, target_h};
}
void Display::resize_window_for_mode_switch() {
const auto target_size = compute_mode_switch_target_window_size();
const int target_w = target_size[0];
const int target_h = target_size[1];
const float target_aspect_ratio = std::max(compute_active_content_aspect_ratio(), 0.001F);
// Keep WINDOW aspect lock consistent with the newly selected display mode.
if (aspect_lock_mode_ == AspectLockMode::Window) {
window_aspect_ratio_ = target_aspect_ratio;
}
if (is_fullscreen_) {
// Keep the future windowed restore size in sync with mode changes done in fullscreen.
windowed_size_before_fullscreen_ = target_size;
handle_window_resize(true, true);
return;
}
apply_window_size_and_relayout(target_w, target_h, true);
}
void Display::reinitialize_video_dimensions(const unsigned width, const unsigned height) {
const int new_video_width = static_cast<int>(width);
const int new_video_height = static_cast<int>(height);
if (new_video_width <= 0 || new_video_height <= 0) {
throw std::runtime_error("Video dimensions must be positive");
}
if (video_width_ == new_video_width && video_height_ == new_video_height) {
return;
}
video_width_ = new_video_width;
video_height_ = new_video_height;
recreate_video_textures_for_current_mode();
if (diff_buffer_ != nullptr) {
delete[] diff_buffer_;
diff_buffer_ = nullptr;
}
diff_buffer_ = new uint8_t[video_width_ * video_height_ * 3 * (use_10_bpc_ ? sizeof(uint16_t) : sizeof(uint8_t))];
diff_planes_ = {diff_buffer_, nullptr, nullptr};
diff_pitches_ = {video_width_ * 3 * (use_10_bpc_ ? sizeof(uint16_t) : sizeof(uint8_t)), 0, 0};
if (left_buffer_ != nullptr) {
delete[] left_buffer_;
left_buffer_ = nullptr;
}
if (right_buffer_ != nullptr) {
delete[] right_buffer_;
right_buffer_ = nullptr;
}
left_planes_ = {nullptr, nullptr, nullptr};
right_planes_ = {nullptr, nullptr, nullptr};
move_offset_ = Vector2D((global_center_.x() - 0.5F) * static_cast<float>(video_width_), (global_center_.y() - 0.5F) * static_cast<float>(video_height_));
// Force relayout because video dimensions changed even if window size did not.
handle_window_resize(true, true);
update_window_title_with_current_roi();
}
void Display::print_verbose_info() {
std::cout << "Main program version: " << VersionInfo::version << std::endl;
std::cout << "Video size: " << video_width_ << "x" << video_height_ << std::endl;
std::cout << "Video duration: " << format_duration(duration_) << std::endl;
std::cout << "Display mode: " << mode_to_string(mode_) << std::endl;
std::cout << "Fit to usable bounds: " << std::boolalpha << fit_window_to_usable_bounds_ << std::endl;
std::cout << "High-DPI allowed: " << std::boolalpha << high_dpi_allowed_ << std::endl;
std::cout << "Aspect lock mode: " << aspect_lock_mode_to_string(aspect_lock_mode_) << std::endl;
std::cout << "Aspect view mode: " << aspect_view_mode_to_string(aspect_view_mode_) << std::endl;
std::cout << "Use 10 bpc: " << std::boolalpha << use_10_bpc_ << std::endl;
std::cout << "Fast input alignment: " << std::boolalpha << fast_input_alignment_ << std::endl;
std::cout << "Bilinear filtering: " << std::boolalpha << bilinear_texture_filtering_ << std::endl;
std::cout << "Mouse whl sensitivity: " << wheel_sensitivity_ << std::endl;
SDL_version sdl_linked_version;
SDL_GetVersion(&sdl_linked_version);
std::cout << "SDL version: " << string_sprintf("%u.%u.%u", sdl_linked_version.major, sdl_linked_version.minor, sdl_linked_version.patch) << std::endl;
const SDL_version* sdl_ttf_linked_version = TTF_Linked_Version();
std::cout << "SDL_ttf version: " << string_sprintf("%u.%u.%u", sdl_ttf_linked_version->major, sdl_ttf_linked_version->minor, sdl_ttf_linked_version->patch) << std::endl;
SDL_RendererInfo info;
SDL_GetRendererInfo(renderer_, &info);
std::cout << "SDL renderer: " << info.name << std::endl;
int current_display_number = SDL_GetWindowDisplayIndex(window_);
std::cout << "SDL display number: " << current_display_number << std::endl;
SDL_DisplayMode desktop_display_mode;
SDL_GetDesktopDisplayMode(current_display_number, &desktop_display_mode);
std::cout << "SDL desktop size: " << desktop_display_mode.w << "x" << desktop_display_mode.h << std::endl;
std::cout << "SDL GL drawable size: " << drawable_width_ << "x" << drawable_height_ << std::endl;
std::cout << "SDL window size: " << window_width_ << "x" << window_height_ << std::endl;
auto stringify_format_and_bpp = [&](Uint32 pixel_format) -> std::string { return string_sprintf("%s (%d bpp)", SDL_GetPixelFormatName(pixel_format), SDL_BITSPERPIXEL(pixel_format)); };
Uint32 window_pixel_format = SDL_GetWindowPixelFormat(window_);
std::cout << "SDL window px format: " << stringify_format_and_bpp(window_pixel_format) << std::endl;
Uint32 video_pixel_format;
SDL_QueryTexture(video_texture_linear_, &video_pixel_format, nullptr, nullptr, nullptr);
std::cout << "SDL video px format: " << stringify_format_and_bpp(video_pixel_format) << std::endl;
std::cout << "FFmpeg version: " << av_version_info() << std::endl;
std::cout << "libavutil version: " << format_libav_version(avutil_version()) << std::endl;
std::cout << "libavcodec version: " << format_libav_version(avcodec_version()) << std::endl;
std::cout << "libavformat version: " << format_libav_version(avformat_version()) << std::endl;
std::cout << "libavfilter version: " << format_libav_version(avfilter_version()) << std::endl;
std::cout << "libswscale version: " << format_libav_version(swscale_version()) << std::endl;
std::cout << "libswresample version: " << format_libav_version(swresample_version()) << std::endl;
std::cout << "libavcodec configuration: " << avcodec_configuration() << std::endl << std::endl;
}
void Display::rebuild_fonts() {
if (small_font_ != nullptr) {
TTF_CloseFont(small_font_);
small_font_ = nullptr;
}
if (big_font_ != nullptr) {
TTF_CloseFont(big_font_);
big_font_ = nullptr;
}
SDL_RWops* embedded_font_small = check_sdl(SDL_RWFromConstMem(SOURCE_CODE_PRO_REGULAR_TTF, SOURCE_CODE_PRO_REGULAR_TTF_LEN), "get pointer to font");
SDL_RWops* embedded_font_big = check_sdl(SDL_RWFromConstMem(SOURCE_CODE_PRO_REGULAR_TTF, SOURCE_CODE_PRO_REGULAR_TTF_LEN), "get pointer to font");
small_font_ = check_sdl(TTF_OpenFontRW(embedded_font_small, 1, static_cast<int>(16 * font_scale_)), "font open");
big_font_ = check_sdl(TTF_OpenFontRW(embedded_font_big, 1, static_cast<int>(24 * font_scale_)), "font open");
}
void Display::rebuild_side_ui_textures() {
auto rebuild_side = [&](Side side, const std::string& label) {
auto& ui = side_ui_[side.as_simple_index()];
if (ui.text_texture != nullptr) {
SDL_DestroyTexture(ui.text_texture);
ui.text_texture = nullptr;
}
SDL_Surface* text_surface = render_text_with_fallback(label);
ui.text_texture = SDL_CreateTextureFromSurface(renderer_, text_surface);
ui.text_width = text_surface->w;
ui.text_height = text_surface->h;
SDL_FreeSurface(text_surface);
};
side_ui_[LEFT.as_simple_index()].file_stem = strip_ffmpeg_patterns(get_file_stem(left_file_name_));
side_ui_[RIGHT.as_simple_index()].file_stem = strip_ffmpeg_patterns(get_file_stem(right_file_name_));
rebuild_side(LEFT, left_file_name_);
rebuild_side(RIGHT, format_right_file_label(left_file_name_, right_file_name_, active_right_index_ + 1));
}
void Display::rebuild_help_textures() {
// Rebuild all help textures because wrapping and layout depend on drawable width.
for (auto help_texture : help_textures_) {
SDL_DestroyTexture(help_texture);
}
help_textures_.clear();
help_total_height_ = 0;
bool primary_color = true;
// Helper to render one line and track its height for scrolling math.
auto add_help_texture = [&](TTF_Font* font, const std::string& text) {
int h;
SDL_Surface* surface = TTF_RenderUTF8_Blended_Wrapped(font, text.c_str(), primary_color ? HELP_TEXT_PRIMARY_COLOR : HELP_TEXT_ALTERNATE_COLOR, drawable_width_ - HELP_TEXT_HORIZONTAL_MARGIN * 2);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer_, surface);
SDL_FreeSurface(surface);
SDL_QueryTexture(texture, nullptr, nullptr, nullptr, &h);
help_total_height_ += h;
help_textures_.push_back(texture);
};
add_help_texture(small_font_, " ");
const auto& sections = get_control_sections();
for (size_t i = 0; i < sections.size(); ++i) {
const auto& section = sections[i];
// Force section headers to white, underlined, and uppercase.
primary_color = true;
TTF_SetFontStyle(big_font_, TTF_STYLE_BOLD | TTF_STYLE_UNDERLINE);
add_help_texture(big_font_, to_upper_case(section.title));
TTF_SetFontStyle(big_font_, TTF_STYLE_NORMAL);
// Reset so the first section item toggles to secondary color (light yellow).
primary_color = true;
for (size_t j = 0; j < section.entries.size(); ++j) {
const auto& entry = section.entries[j];
primary_color = !primary_color;
if (entry.key.empty()) {
add_help_texture(small_font_, entry.description);
add_help_texture(small_font_, " ");
} else {
add_help_texture(small_font_, string_sprintf(" %-16s %s", entry.key.c_str(), entry.description.c_str()));
}
}
if (i + 1 < sections.size()) {
add_help_texture(small_font_, " ");
}
}
}
void Display::clamp_overlay_offsets() {
auto clamp_offset = [&](int& y_offset, const int total_height, const std::vector<SDL_Texture*>& textures) {
const int min_offset = drawable_height_ - total_height - static_cast<int>(textures.size()) * HELP_TEXT_LINE_SPACING;
y_offset = std::max(y_offset, min_offset);
y_offset = std::min(y_offset, 0);
};
clamp_offset(help_y_offset_, help_total_height_, help_textures_);
clamp_offset(metadata_y_offset_, metadata_total_height_, metadata_textures_);
}
float Display::compute_content_aspect_ratio() const {
const float content_w = static_cast<float>(video_width_) * ((mode_ == Mode::HStack) ? 2.0F : 1.0F);
const float content_h = static_cast<float>(video_height_) * ((mode_ == Mode::VStack) ? 2.0F : 1.0F);
return content_w / std::max(content_h, 1.0F);
}
float Display::compute_active_content_aspect_ratio() const {
auto apply_mode_layout_multiplier = [&](const float single_frame_ratio) {
if (mode_ == Mode::HStack) {
return single_frame_ratio * 2.0F;
}
if (mode_ == Mode::VStack) {
return single_frame_ratio * 0.5F;
}
return single_frame_ratio;
};
switch (aspect_view_mode_) {
case AspectViewMode::Stretch:
return static_cast<float>(std::max(window_width_, 1)) / static_cast<float>(std::max(window_height_, 1));
case AspectViewMode::Preset16x9:
return apply_mode_layout_multiplier(16.0F / 9.0F);
case AspectViewMode::Preset4x3:
return apply_mode_layout_multiplier(4.0F / 3.0F);
case AspectViewMode::Preset1x1:
return apply_mode_layout_multiplier(1.0F);
case AspectViewMode::Original:
default:
break;
}
return compute_content_aspect_ratio();
}
void Display::update_content_window_layout() {
const int safe_window_w = std::max(1, window_width_);
const int safe_window_h = std::max(1, window_height_);
content_window_ = SDL_Rect{0, 0, safe_window_w, safe_window_h};
if (aspect_view_mode_ != AspectViewMode::Stretch) {
const float content_aspect_ratio = std::max(compute_active_content_aspect_ratio(), 0.001F);
const float window_aspect_ratio = static_cast<float>(safe_window_w) / static_cast<float>(safe_window_h);
if (window_aspect_ratio > content_aspect_ratio) {
content_window_.h = safe_window_h;
content_window_.w = std::max(1, static_cast<int>(std::round(static_cast<float>(content_window_.h) * content_aspect_ratio)));
content_window_.x = (safe_window_w - content_window_.w) / 2;
} else {
content_window_.w = safe_window_w;
content_window_.h = std::max(1, static_cast<int>(std::round(static_cast<float>(content_window_.w) / content_aspect_ratio)));
content_window_.y = (safe_window_h - content_window_.h) / 2;
}
}
const float content_w = static_cast<float>(std::max(1, video_width_)) * ((mode_ == Mode::HStack) ? 2.0F : 1.0F);
const float content_h = static_cast<float>(std::max(1, video_height_)) * ((mode_ == Mode::VStack) ? 2.0F : 1.0F);
video_to_window_width_factor_ = content_w / static_cast<float>(std::max(1, content_window_.w));
video_to_window_height_factor_ = content_h / static_cast<float>(std::max(1, content_window_.h));
}
void Display::handle_window_resize(const bool reset_forced_size_guard, const bool force_layout_refresh) {
if (reset_forced_size_guard) {
last_forced_window_size_ = {-1, -1};
}
int new_drawable_w = 0;
int new_drawable_h = 0;
int new_window_w = 0;
int new_window_h = 0;
// Query both logical window size and drawable size since they can diverge (e.g. high-DPI).
SDL_GL_GetDrawableSize(window_, &new_drawable_w, &new_drawable_h);
SDL_GetWindowSize(window_, &new_window_w, &new_window_h);
bool was_fullscreen = is_fullscreen_;
is_fullscreen_ = detect_fullscreen_like_state();
auto force_window_size = [&](int width, int height) {
last_forced_window_size_ = {width, height};
SDL_SetWindowSize(window_, width, height);
};
const bool skip_forced_size = (last_forced_window_size_[0] == new_window_w && last_forced_window_size_[1] == new_window_h);
// Enforce minimum window size early to keep downstream math well-defined.
if (!skip_forced_size && (new_window_w < MIN_WINDOW_WIDTH || new_window_h < MIN_WINDOW_HEIGHT)) {
force_window_size(std::max(new_window_w, MIN_WINDOW_WIDTH), std::max(new_window_h, MIN_WINDOW_HEIGHT));
return;
}
// If aspect-ratio locking is enabled, snap the resize to the selected ratio.
if (!skip_forced_size && !is_fullscreen_ && aspect_lock_mode_ != AspectLockMode::Off) {
const float target_aspect_ratio = (aspect_lock_mode_ == AspectLockMode::Window) ? window_aspect_ratio_ : compute_active_content_aspect_ratio();
const float safe_target_aspect_ratio = std::max(target_aspect_ratio, 0.001F);
const float current_ratio = static_cast<float>(new_window_w) / static_cast<float>(new_window_h);
if (std::abs(current_ratio - safe_target_aspect_ratio) > 0.001F) {
int target_w = new_window_w;
int target_h = new_window_h;
if (current_ratio > safe_target_aspect_ratio) {
target_w = static_cast<int>(std::round(new_window_h * safe_target_aspect_ratio));
if (target_w < MIN_WINDOW_WIDTH) {
target_w = MIN_WINDOW_WIDTH;
target_h = std::max(MIN_WINDOW_HEIGHT, static_cast<int>(std::round(static_cast<float>(target_w) / safe_target_aspect_ratio)));
}
} else {
target_h = static_cast<int>(std::round(new_window_w / safe_target_aspect_ratio));
if (target_h < MIN_WINDOW_HEIGHT) {
target_h = MIN_WINDOW_HEIGHT;
target_w = std::max(MIN_WINDOW_WIDTH, static_cast<int>(std::round(static_cast<float>(target_h) * safe_target_aspect_ratio)));
}
}
if (target_w != new_window_w || target_h != new_window_h) {
force_window_size(target_w, target_h);
return;
}
}
}
// Ignore invalid sizes (can occur during platform-specific resize transitions).
if (new_drawable_w <= 0 || new_drawable_h <= 0 || new_window_w <= 0 || new_window_h <= 0) {
return;
}
// No change means we can skip the expensive rebuilds below.
if (!force_layout_refresh && new_drawable_w == drawable_width_ && new_drawable_h == drawable_height_ && new_window_w == window_width_ && new_window_h == window_height_) {
return;
}
drawable_width_ = new_drawable_w;
drawable_height_ = new_drawable_h;
window_width_ = new_window_w;
window_height_ = new_window_h;
drawable_to_window_width_factor_ = static_cast<float>(drawable_width_) / static_cast<float>(window_width_);
drawable_to_window_height_factor_ = static_cast<float>(drawable_height_) / static_cast<float>(window_height_);
update_content_window_layout();
font_scale_ = (drawable_to_window_width_factor_ + drawable_to_window_height_factor_) / 2.0F;
border_extension_ = 3 * font_scale_;
double_border_extension_ = border_extension_ * 2;
line1_y_ = 20;
line2_y_ = line1_y_ + 30 * font_scale_;
// Keep text clipping consistent with the new drawable width.
if (mode_ != Mode::VStack) {
max_text_width_ = drawable_width_ / 2 - double_border_extension_ - line1_y_;
} else {
max_text_width_ = drawable_width_ - double_border_extension_ - line1_y_;
}
// Rebuild cached UI assets that are size-dependent (fonts, help, metadata, labels).
SDL_RenderSetLogicalSize(renderer_, drawable_width_, drawable_height_);
rebuild_fonts();
rebuild_side_ui_textures();
rebuild_help_textures();
metadata_dirty_ = true;
// Clamp overlay scroll positions to the new size and refresh ROI-dependent title.
clamp_overlay_offsets();
update_window_title_with_current_roi();
// Trigger a synthetic no-op mouse move after fullscreen transitions to force
// slider/UI refresh without requiring physical mouse movement.
if (was_fullscreen != is_fullscreen_) {
int global_mouse_x = 0;
int global_mouse_y = 0;
SDL_GetGlobalMouseState(&global_mouse_x, &global_mouse_y);
SDL_WarpMouseGlobal(global_mouse_x, global_mouse_y);
set_pending_message(string_sprintf("Fullscreen mode set to '%s'", is_fullscreen_ ? "ON" : "OFF"));
}
}
void Display::convert_to_packed_10_bpc(std::array<uint8_t*, 3> in_planes, std::array<size_t, 3> in_pitches, std::array<uint32_t*, 3> out_planes, std::array<size_t, 3> out_pitches, const SDL_Rect& roi) {
row_workers_.run_dynamic(
roi.h,
[=](const int start_row, const int end_row) {
uint16_t* p_in = reinterpret_cast<uint16_t*>(in_planes[0] + roi.x * 6 + in_pitches[0] * (roi.y + start_row));
uint32_t* p_out = out_planes[0] + roi.x + out_pitches[0] * (roi.y + start_row) / sizeof(uint32_t);
for (int y = start_row; y < end_row; y++) {
for (int in_x = 0, out_x = 0; out_x < roi.w; in_x += 3, out_x++) {
const uint32_t r = p_in[in_x] >> 6;
const uint32_t g = p_in[in_x + 1] >> 6;
const uint32_t b = p_in[in_x + 2] >> 6;
p_out[out_x] = (r << 20) | (g << 10) | (b);
}
p_in += in_pitches[0] / sizeof(uint16_t);
p_out += out_pitches[0] / sizeof(uint32_t);
}
},
suggest_block_rows_by_bytes(roi.w, roi.h, sizeof(uint16_t), 3));
}
template <int Bpc>