-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathvideo_pattern_generator.cpp
More file actions
1014 lines (963 loc) · 45.8 KB
/
Copy pathvideo_pattern_generator.cpp
File metadata and controls
1014 lines (963 loc) · 45.8 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
/**
* @file utils/video_pattern_generator.cpp
* @author Colin Perkins <csp@csperkins.org
* @author Alvaro Saurin <saurin@dcs.gla.ac.uk>
* @author Martin Benes <martinbenesh@gmail.com>
* @author Lukas Hejtmanek <xhejtman@ics.muni.cz>
* @author Petr Holub <hopet@ics.muni.cz>
* @author Milos Liska <xliska@fi.muni.cz>
* @author Jiri Matela <matela@ics.muni.cz>
* @author Dalibor Matura <255899@mail.muni.cz>
* @author Ian Wesley-Smith <iwsmith@cct.lsu.edu>
* @author Martin Pulec <martin.pulec@cesnet.cz>
*/
/*
* Copyright (c) 2005-2006 University of Glasgow
* Copyright (c) 2005-2026 CESNET, zájmové sdružení právnických osob
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of CESNET nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* @todo
* Do the rendering in 16 bits
*/
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <iterator> // for size
#include <memory>
#include <stdexcept>
#include <random>
#include <utility>
#include <vector>
#include "color_space.h"
#include "debug.h"
#include "pixfmt_conv.h"
#include "utils/bitmap_font.h" // for FONT_H
#include "utils/color_out.h"
#include "utils/macros.h" // for IS_KEY_PREFIX, MAX, STR_LEN
#include "utils/string_view_utils.hpp"
#include "utils/text.h"
#include "video_codec.h"
#include "video_capture/testcard_common.h"
#include "video_pattern_generator.h"
#define BLANK_USAGE "blank[=<color>|help]"
#define MOD_NAME "[vid. patt. generator] "
constexpr size_t headroom = 128; // headroom for cases when dst color_spec has wider block size
constexpr int rg48_bpp = 6;
using namespace std::string_literals;
using std::copy;
using std::cout;
using std::default_random_engine;
using std::exception;
using std::for_each;
using std::make_unique;
using std::min;
using std::stoi;
using std::stoll;
using std::string;
using std::string_view;
using std::to_string;
using std::unique_ptr;
using std::uniform_int_distribution;
using std::vector;
enum class generator_depth {
bits8, ///< RGBA
bits16 ///< RG48
};
/**
* @todo move to some of common files (video* ?) and link
* externally later when there will be more users of the function.
* @reutrns 4B color (uint32_t) or -1 if error
*/
static long long
get_color_by_name(const char *col)
{
static const struct {
const char *name;
uint32_t val;
} colors[] = {
{ "white", 0xFFFFFFU },
{ "gray", 0x7F7F7FU },
{ "black", 0x000000U },
{ "red", 0x0000FFU },
{ "green", 0x00FF00U },
{ "blue", 0xFF0000U },
{ "yellow", 0x00FFFFU },
{ "magenta", 0xFF00FFU },
{ "cyan", 0xFFFF00U },
};
if (strcmp(col, "help") == 0) {
color_printf("color in format " TBOLD("0x<AABBGGRR>") " or a symbolic name\n");
color_printf("Leading zeros can be omitted, eg. "
"`0xFF` produces a red pattern.\n");
color_printf("\nfollowing symbolic names can be also used:\n");
for (unsigned i = 0; i < std::size(colors); ++i) {
color_printf("\t- " TBOLD("%s") "\n", colors[i].name);
}
color_printf("\n");
return -1;
}
char *endptr = nullptr;
errno = 0;
unsigned long val = strtoul(col, &endptr, 0);
if (errno == 0 && endptr[0] == '\0') {
return (uint32_t) val;
}
for (unsigned i = 0; i < std::size(colors); ++i) {
if (strcasecmp(col, colors[i].name) == 0) {
return 0xFFU << 24 | colors[i].val;
}
}
MSG(ERROR, "Unknown color: %s. Use 'help' for possible values.\n", col);
return -1;
}
class image_pattern {
public:
static unique_ptr<image_pattern> create(string const &pattern, string const ¶ms);
auto init(int width, int height, enum generator_depth depth) noexcept {
size_t data_len = width * height * rg48_bpp + headroom;
vector<unsigned char> out(data_len);
auto actual_bit_depth = fill(width, height, out.data());
if (depth == generator_depth::bits8 && actual_bit_depth == generator_depth::bits16) {
convert_rg48_to_rgba(width, height, out.data());
}
if (depth == generator_depth::bits16 && actual_bit_depth == generator_depth::bits8) {
convert_rgba_to_rg48(width, height, out.data());
}
return out;
}
virtual ~image_pattern() = default;
image_pattern() = default;
image_pattern(const image_pattern &) = delete;
image_pattern & operator=(const image_pattern &) = delete;
image_pattern(image_pattern &&) = delete;
image_pattern && operator=(image_pattern &&) = delete;
private:
/// @retval bit depth used by the generator (either 8 or 16)
virtual enum generator_depth fill(int width, int height, unsigned char *data) = 0;
/// @note in-place
virtual void convert_rgba_to_rg48(int width, int height, unsigned char *data) {
for (int y = height - 1; y >= 0; --y) {
for (int x = width - 1; x >= 0; --x) {
unsigned char *in_pix = data + 4 * (y * width + x);
unsigned char *out_pix = data + 6 * (y * width + x);
unsigned char r = *in_pix++;
unsigned char g = *in_pix++;
unsigned char b = *in_pix++;
*out_pix++ = 0;
*out_pix++ = r;
*out_pix++ = 0;
*out_pix++ = g;
*out_pix++ = 0;
*out_pix++ = b;
}
}
}
/// @note in-place
virtual void convert_rg48_to_rgba(int width, int height, unsigned char *data) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
unsigned char *in_pix = data + 6 * (y * width + x);
unsigned char *out_pix = data + 4 * (y * width + x);
*out_pix++ = in_pix[1];
*out_pix++ = in_pix[3];
*out_pix++ = in_pix[5];
*out_pix++ = 0xFFU;
}
}
}
};
class image_pattern_bars : public image_pattern {
public:
explicit image_pattern_bars(string const &init) {
if (init == "help"s) {
col() << "Testcard bar usage:\n\t"
<< SBOLD(SRED("-t testcard:pattern=bars")
<< "[=<text>[,XYpt]]")
<< " - optionally annotate with text (+ opt font size)" << "\n";
throw 1;
}
annotate = init;
const char *last_comma = strrchr(init.c_str(), ',');
if (last_comma != nullptr) {
char *endptr = nullptr;
long val = strtol(last_comma + 1, &endptr, 10);
if (strcmp(endptr, "pt") == 0) {
annotate.resize(annotate.rfind(','));
scale = (val + FONT_H - 1) / FONT_H;
}
}
}
private:
enum generator_depth fill(int width, int height, unsigned char *data) override {
int col_num = 0;
int rect_size = COL_NUM;
struct testcard_rect r{};
struct testcard_pixmap pixmap{};
pixmap.w = width;
pixmap.h = height;
pixmap.data = data;
rect_size = (width + rect_size - 1) / rect_size;
for (int j = 0; j < height; j += rect_size) {
uint32_t grey = 0xFF010101U;
if (j == rect_size * 2) {
r.w = width;
r.h = rect_size / 4;
r.x = 0;
r.y = j;
testcard_fillRect(&pixmap, &r, 0xFFFFFFFFU);
r.h = rect_size - (rect_size * 3 / 4);
r.y = j + rect_size * 3 / 4;
testcard_fillRect(&pixmap, &r, 0xFF000000U);
}
for (int i = 0; i < width; i += rect_size) {
r.x = i;
r.y = j;
r.w = rect_size;
r.h = min<int>(rect_size, height - r.y);
LOG(LOG_LEVEL_DEBUG) << MOD_NAME << "Fill rect at " << r.x << "," << r.y << "\n";
if (j != rect_size * 2) {
testcard_fillRect(&pixmap, &r,
rect_colors[col_num]);
col_num = (col_num + 1) % COL_NUM;
} else {
r.h = rect_size / 2;
r.y += rect_size / 4;
testcard_fillRect(&pixmap, &r, grey);
grey += 0x00010101U * (255 / COL_NUM);
}
}
}
if (!annotate.empty()) {
draw_line_scaled((char *) data, width * 4,
annotate.c_str(), 0xFFFFFFFF,
0xFF000000, scale);
}
return generator_depth::bits8;
}
string annotate;
int scale = 6;
};
/**
* @todo Proper SMPTE test pattern has different in bottom third.
*/
template<uint8_t f, int columns>
class image_pattern_ebu_smpte_bars : public image_pattern {
static constexpr uint32_t bars[] = {
uint32_t{0xFFU << 24U | f << 16U | f << 8U | f },
uint32_t{0xFFU << 24U | 0U << 16U | f << 8U | f },
uint32_t{0xFFU << 24U | f << 16U | f << 8U | 0U },
uint32_t{0xFFU << 24U | 0U << 16U | f << 8U | 0U },
uint32_t{0xFFU << 24U | f << 16U | 0U << 8U | f },
uint32_t{0xFFU << 24U | 0U << 16U | 0U << 8U | f },
uint32_t{0xFFU << 24U | f << 16U | 0U << 8U | 0U },
uint32_t{0xFFU << 24U | 0U << 16U | 0U << 8U | 0U },
};
enum generator_depth fill(int width, int height, unsigned char *data) override {
int col_num = 0;
const int rect_size = (width + columns - 1) / columns;
struct testcard_rect r{};
struct testcard_pixmap pixmap{};
pixmap.w = width;
pixmap.h = height;
pixmap.data = data;
for (int j = 0; j < height; j += rect_size) {
for (int i = 0; i < width; i += rect_size) {
r.x = i;
r.y = j;
r.w = rect_size;
r.h = min<int>(rect_size, height - r.y);
log_msg(LOG_LEVEL_DEBUG, MOD_NAME "Fill rect at %d,%d\n", r.x, r.y);
testcard_fillRect(&pixmap, &r,
bars[col_num]);
col_num = (col_num + 1) % columns;
}
}
return generator_depth::bits8;
}
friend class image_pattern_smpte_bars;
};
class image_pattern_smpte_bars : public image_pattern_ebu_smpte_bars<0xBFU, 7> {
static constexpr uint32_t bottom_bars[] = {
uint32_t{0xFFU << 24U | 105 << 16U | 63 << 8U | 0U },
uint32_t{0xFFFFFFFFU },
uint32_t{0xFFU << 24U | 119U << 16U | 0U << 8U | 0U },
uint32_t{0xFF000000U },
uint32_t{0xFF000000U },
uint32_t{0xFF000000U },
};
enum generator_depth fill(int width, int height, unsigned char *data) override {
auto ret = image_pattern_ebu_smpte_bars<0xBFU, 7>::fill(width, height, data); // upper 2 3rds
assert(ret == generator_depth::bits8);
int columns = 7;
struct testcard_pixmap pixmap{ .w = width, .h = height, .data = data };
const int mid_strip_height = height / 3 - width / 6;
struct testcard_rect r{ .x = 0, .y = height / 3 * 2, .w = (width + columns - 1) / columns, .h = mid_strip_height};
for (int i = 0; i < columns; i += 1) {
r.x = i * r.w;
log_msg(LOG_LEVEL_DEBUG, MOD_NAME "Fill rect at %d,%d\n", r.x, r.y);
if (i % 2 == 1) {
testcard_fillRect(&pixmap, &r, 0);
} else {
testcard_fillRect(
&pixmap, &r,
image_pattern_ebu_smpte_bars<
0xBFU, 7>::bars[columns - 1 - i]);
}
}
columns = 6;
r.w = (width + columns - 1) / columns;
r.h = width / 6;
r.y += mid_strip_height;
for (int i = 0; i < columns; i += 1) {
r.x = i * r.w;
log_msg(LOG_LEVEL_DEBUG, MOD_NAME "Fill rect at %d,%d\n", r.x, r.y);
testcard_fillRect(&pixmap, &r,
bottom_bars[i]);
}
// pluge - skipping a "superblack" and black bar
r.x = 5 * (width / 7);
r.w = (width / 7) / 3;
r.x += 2 * r.w;
log_msg(LOG_LEVEL_DEBUG, MOD_NAME "Fill rect at %d,%d\n", r.x, r.y);
testcard_fillRect(&pixmap, &r,
0xFFU << 24 | 0x0A0A0A);
return generator_depth::bits8;
}
};
class image_pattern_blank : public image_pattern {
public:
explicit image_pattern_blank(string const &init) {
if (init == "help"s) {
color_printf("Testcard " TBOLD("blank") " usage:\n");
color_printf("\t" TRED(TBOLD(
"-t testcard:patt=" BLANK_USAGE)) "\n\n");
(void) get_color_by_name("help");
color_printf(
"Defaults to 0xFF000000.\n");
throw 1;
}
if (!init.empty()) {
const long long c = get_color_by_name(init.c_str());
if (c == -1) {
throw 1;
}
color = c;
}
}
private:
enum generator_depth fill(int width, int height, unsigned char *data) override {
for (int i = 0; i < width * height; ++i) {
(reinterpret_cast<uint32_t *>(data))[i] = color;
}
return generator_depth::bits8;
}
uint32_t color = 0xFF000000U;
};
class image_pattern_gradient : public image_pattern {
public:
explicit image_pattern_gradient(const string &config) {
if (!config.empty()) {
long long c = get_color_by_name(config.c_str());
if (c == -1) {
throw 1;
}
color = c;
}
}
static constexpr uint32_t red = 0xFFU;
private:
enum generator_depth fill(int width, int height, unsigned char *data) override {
auto *ptr = reinterpret_cast<uint16_t *>(data);
for (int j = 0; j < height; j += 1) {
uint16_t r = sin(static_cast<double>(j) / height * M_PI) * (color & 0xFFU) / 0xFFU * 0xFF'FFU;
uint16_t g = sin(static_cast<double>(j) / height * M_PI) * ((color >> 8) & 0xFFU) / 0xFFU * 0xFF'FFU;
uint16_t b = sin(static_cast<double>(j) / height * M_PI) * ((color >> 16) & 0xFFU) / 0xFFU * 0xFF'FFU;
for (int i = 0; i < width; i += 1) {
*ptr++ = r;
*ptr++ = g;
*ptr++ = b;
}
}
return generator_depth::bits16;
}
uint32_t color = image_pattern_gradient::red;;
};
class image_pattern_gradient2 : public image_pattern {
public:
explicit image_pattern_gradient2(string const &config) {
if (config.empty()) {
return;
}
if (config == "help"s) {
cout << "Testcard gradient2 usage:\n\t-t testcard:gradient2[=maxval] - maxval is 16-bit number\n";
throw 1;
}
val_max = stol(config, nullptr, 0);
}
private:
unsigned int val_max = 0XFFFFU;
enum generator_depth fill(int width, int height, unsigned char *data) override {
assert(width > 1); // avoid division by zero
auto *ptr = reinterpret_cast<uint16_t *>(data);
for (int j = 0; j < height; j += 1) {
for (int i = 0; i < width; i += 1) {
unsigned int gray = i * val_max / (width - 1);
*ptr++ = gray;
*ptr++ = gray;
*ptr++ = gray;
}
}
return generator_depth::bits16;
}
};
class image_pattern_uv_plane : public image_pattern {
public:
explicit image_pattern_uv_plane(string const &y_lvl) {
if (!y_lvl.empty()) {
y_level = LIMIT_LO(16) + stof(y_lvl) * (LIMIT_HI_Y(16) - LIMIT_LO(16));
}
}
private:
int y_level = (LIMIT_HI_Y(16) + LIMIT_LO(16)) / 2;
enum generator_depth fill(int width, int height, unsigned char *data) override {
assert(width > 1 && height > 1); // avoid division by zero
auto *ptr = reinterpret_cast<uint16_t *>(data);
auto *conv = get_decoder_from_to(Y416, RG48);
int scale_cbcr = LIMIT_HI_CBCR(16) - LIMIT_LO(16);
for (int j = 0; j < height; j += 1) {
for (int i = 0; i < width; i += 1) {
uint16_t uyva[4];
uyva[0] = LIMIT_LO(16) + i * scale_cbcr / (width - 1);
uyva[1] = y_level;
uyva[2] = LIMIT_LO(16) + j * scale_cbcr / (height - 1);
uyva[3] = 0xFF'FF;
conv((unsigned char *) ptr, (unsigned char *) uyva, 6, DEFAULT_R_SHIFT, DEFAULT_G_SHIFT, DEFAULT_B_SHIFT);
ptr += 3;
}
}
return generator_depth::bits16;
}
};
class image_pattern_noise : public image_pattern {
default_random_engine rand_gen;
enum generator_depth fill(int width, int height, unsigned char *data) override {
uniform_int_distribution<> dist(0, 0xFFFF);
for_each(reinterpret_cast<uint16_t *>(data), reinterpret_cast<uint16_t *>(data) + 3 * width * height, [&](uint16_t & c) { c = dist(rand_gen); });
return generator_depth::bits16;
}
};
struct image_pattern_strips : public image_pattern
{
explicit image_pattern_strips(string const &config)
{
for (int i = 0; i < COL_NUM; ++i) {
pattern[3 + i] = rect_colors[i];
}
parse_fmt(config);
}
private:
void parse_fmt(string const &config)
{
if (config.empty()) {
return;
}
if (config == "help"s) {
color_printf(
"\t" TBOLD("-t "
"testcard:patt=strips[=[vert|hor|"
"diag][,w[idth]=W]]") "\n");
throw 1;
}
char conf[STR_LEN];
snprintf_ch(conf, "%s", config.c_str());
char *tmp = conf;
char *endptr = nullptr;
while (char *item = strtok_r(tmp, ",", &endptr)) {
tmp = nullptr;
if (strncmp(item, "hor", 3) == 0) {
type = COLS;
} else if (strncmp(item, "ver", 3) == 0) {
type = ROWS;
} else if (strncmp(item, "dia", 3) == 0) {
type = DIAG;
} else if (IS_KEY_PREFIX(item, "width")) {
fill_w = stoi(strchr(item, '=') + 1);
} else {
throw std::runtime_error(
string("Wrong option: ") + config);
}
}
}
enum { ROWS, COLS, DIAG } type = DIAG;
uint32_t pattern[3 + COL_NUM] = { RGBA_WHITE, RGBA_BLACK, RGBA_GRAY };
int fill_w = 10;
enum generator_depth fill(int width, int height,
unsigned char *data) override
{
auto *ptr = reinterpret_cast<uint32_t *>(data);
for (int j = 0; j < height; j += 1) {
for (int i = 0; i < width; i += 1) {
const int col_idx = type == DIAG ? i + j
: type == ROWS ? i
: j;
*ptr++ = pattern[(col_idx / fill_w) %
std::size(pattern)];
}
}
return generator_depth::bits8;
}
};
class image_pattern_raw : public image_pattern {
public:
explicit image_pattern_raw(string config) {
if (config.empty()) {
throw std::runtime_error("Empty raw pattern is not allowed!");
}
if (config.substr(0, "0x"s.length()) == "0x") { // strip optional "0x"
config = config.substr(2);
}
while (!config.empty()) {
unsigned char byte = 0;
if (sscanf(config.c_str(), "%2hhx", &byte) == 1) {
m_pattern.push_back(byte);
}
config = config.substr(min<size_t>(config.size(), 2));
}
}
void raw_fill(unsigned char *data, size_t data_len) {
while (data_len >= m_pattern.size()) {
copy(m_pattern.begin(), m_pattern.end(), data);
data += m_pattern.size();
data_len -= m_pattern.size();
}
}
private:
enum generator_depth fill(int width, int height, unsigned char *data) override {
memset(data, 0, width * height * 3); // placeholder only
return generator_depth::bits8;
}
vector<unsigned char> m_pattern;
};
class image_pattern_text : public image_pattern {
public:
explicit image_pattern_text(const string & config) {
if (config == "help"s) {
col() << "Testcard text usage:\n\t"
<< SBOLD(SRED("-t testcard:pattern=text")
<< "[=pattern][,bg=0x<col>|help]"
"[,fg=<col>|help]")
<< "\n";
throw 1;
}
if (!config.empty()) {
string_view sv = config;
bool text_set = false;
while (!sv.empty()) {
auto tok = tokenize(sv, ',');
if (tok.substr(0,3) == "bg=" || tok.substr(0,3) == "fg=") {
auto key = tokenize(tok, '=');
auto val = tokenize(tok, '=');
long long c = get_color_by_name(
((string) val).c_str());
if (c == -1) {
throw 1;
}
if (key == "bg") {
bg = c;
} else {
fg = c;
}
} else if (!text_set) {
text = tok;
text_set = true;
} else {
throw std::runtime_error("Testcard text - wrong option: " + string(tok));
}
}
}
}
private:
enum generator_depth fill(int width, int height, unsigned char *data) override {
std::fill((uint32_t *)(void *) data, (uint32_t *)(void *) (data + width * height * 4), bg);
string line;
for (int i = 0; i < width; i += 8 * (text.size() + 1)) {
line += " " + text;
}
for (int i = 0; i < height / 16; ++i) {
draw_line((char *) data + (i * 16 * width * 4), width * 4, line.c_str(), fg, false);
}
return generator_depth::bits8;
}
string text = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
uint32_t bg = 0xFFCC00CCU;
uint32_t fg = 0xFFFFFFFFU;
};
class image_pattern_diagonal : public image_pattern{
public:
explicit image_pattern_diagonal(const string & config) {
if (config == "help"s) {
col()
<< "Testcard diagonal usage:\n\t"
<< SBOLD(
SRED("-t testcard:pattern=diagonal")
<< "[,bg=<color>|help][,fg=<color>|"
"help][,stride=<stride>][,line_"
"width=<width>]")
<< "\n";
throw 1;
}
if (!config.empty()) {
string_view sv = config;
while (!sv.empty()) {
auto tok = tokenize(sv, ',');
auto key = tokenize(tok, '=');
auto val = tokenize(tok, '=');
if (key == "bg" || key == "fg") {
long long c = get_color_by_name(
((string) val).c_str());
if (c == -1) {
throw 1;
}
if (key == "bg") {
bg = c;
} else {
fg = c;
}
} else if (key == "stride") {
stride = stol((string) val, nullptr, 0);
} else if (key == "line_width") {
line_width = stol((string) val, nullptr, 0);
} else {
throw std::runtime_error("Testcard diagonal - wrong option: " + string(tok));
}
}
}
}
private:
enum generator_depth fill(int width, int height, unsigned char *data) override {
std::fill((uint32_t *)(void *) data, (uint32_t *)(void *) (data + width * height * 4), bg);
for(int y = 0; y < height; y++){
for(int x = y % stride; x < width - line_width; x += stride){
for(int i = 0; i < line_width; i++){
*((uint32_t *) (void *) (data + (y*width + x + i) * 4)) = fg;
}
}
for(int x = stride - (y % stride); x < width - line_width; x += stride){
for(int i = 0; i < line_width; i++){
*((uint32_t *) (void *) (data + (y*width + x + i) * 4)) = fg;
}
}
}
return generator_depth::bits8;
}
uint32_t bg = 0xFF000000U;
uint32_t fg = 0xFFFFFFFFU;
int stride = 80;
int line_width = 4;
};
unique_ptr<image_pattern> image_pattern::create(string const &pattern, string const ¶ms) {
if (pattern == "bars") {
return make_unique<image_pattern_bars>(params);
}
if (pattern == "blank") {
return make_unique<image_pattern_blank>(params);
}
if (pattern == "ebu_bars") {
return make_unique<image_pattern_ebu_smpte_bars<0xFFU, 8>>();
}
if (pattern == "gradient") {
return make_unique<image_pattern_gradient>(params);
}
if (pattern == "gradient2") {
return make_unique<image_pattern_gradient2>(params);
}
if (pattern == "noise") {
return make_unique<image_pattern_noise>();
}
if (pattern == "raw") {
return make_unique<image_pattern_raw>(params);
}
if (pattern == "smpte_bars") {
return make_unique<image_pattern_smpte_bars>();
}
if (pattern == "strips") {
return make_unique<image_pattern_strips>(params);
}
if (pattern == "text") {
return make_unique<image_pattern_text>(params);
}
if (pattern == "uv_plane") {
return make_unique<image_pattern_uv_plane>(params);
}
if (pattern == "diagonal") {
return make_unique<image_pattern_diagonal>(params);
}
throw std::runtime_error("Unknown pattern: "s + pattern + "!"s);
}
struct video_pattern_generator {
virtual char *get_next() = 0;
virtual ~video_pattern_generator() {}
};
struct still_image_video_pattern_generator : public video_pattern_generator {
still_image_video_pattern_generator(string const &pattern, string const ¶ms, int w, int h, codec_t c, int o)
: width(w), height(h), color_spec(c), offset(o)
{
unique_ptr<image_pattern> generator;
try {
generator = image_pattern::create(pattern, params);
} catch (exception const &e) {
LOG(LOG_LEVEL_ERROR) << MOD_NAME << e.what() << "\n";
throw 1;
}
if (!generator) {
throw 2;
}
data = generator->init(width, height, generator_depth::bits8);
codec_t codec_src = RGBA;
if (get_decoder_from_to(RG48, color_spec) != NULL) {
data = generator->init(width, height, generator_depth::bits16);
codec_src = RG48;
}
vector<unsigned char> src;
data.swap(src);
long data_len = vc_get_datalen(width, height, color_spec);
data.resize(data_len * 2);
testcard_convert_buffer(codec_src, color_spec, data.data(), src.data(), width, height);
if (auto *raw_generator = dynamic_cast<image_pattern_raw *>(generator.get())) {
raw_generator->raw_fill(data.data(), data_len);
}
memcpy(data.data() + data_len, data.data(), data_len);
}
int width;
int height;
codec_t color_spec;
vector<unsigned char> data;
int offset;
long cur_pos = 0;
long data_len = vc_get_datalen(width, height, color_spec);
long linesize = vc_get_linesize(width, color_spec);
char *get_next() override {
auto ret = (char *) data.data() + cur_pos;
cur_pos += offset;
if (cur_pos >= data_len) {
cur_pos = 0;
}
return ret;
}
};
struct gray_video_pattern_generator : public video_pattern_generator {
gray_video_pattern_generator(int w, int h, codec_t c, string const& opts)
: width(w), height(h), color_spec(c)
{
if (!opts.empty()) {
if (opts == "help") {
col() << "Usage:\n\t" SBOLD("gray[:step]") << " - interframe color increment (default " << DEFAULT_STEP << ")\n";
throw 1;
}
step = stoi(opts);
}
int col = 0;
while (col < 0xFF) {
int pixels = get_pf_block_pixels(color_spec);
unsigned char rgba[MAX_PFB_SIZE * 4];
for (int i = 0; i < pixels * 4; ++i) {
rgba[i] = (i + 1) % 4 != 0 ? col : 0xFFU; // handle alpha
}
int dst_bs = get_pf_block_bytes(color_spec);
unsigned char dst[MAX_PFB_SIZE];
testcard_convert_buffer(RGBA, color_spec, dst, rgba, pixels, 1);
auto next_frame = vector<unsigned char>(data_len);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width / pixels; x += 1) {
memcpy(next_frame.data() + y * vc_get_linesize(width, color_spec) + x * dst_bs, dst, dst_bs);
}
}
data.push_back(std::move(next_frame));
col += step;
}
}
char *get_next() override {
auto *out = (char *) data[cur_idx++].data();
if (cur_idx * step >= 0xFF) {
cur_idx = 0;
}
return out;
}
private:
constexpr static int DEFAULT_STEP = 1;
int step = DEFAULT_STEP;
int width;
int height;
codec_t color_spec;
int cur_idx = 0;
long data_len = vc_get_datalen(width, height, color_spec);
vector<vector<unsigned char>> data;
};
struct interlaced_video_pattern_generator : public video_pattern_generator {
interlaced_video_pattern_generator(int w, int h, codec_t color_spec)
: width(w), height(h), linesize(vc_get_linesize(width, color_spec))
{
if (width % step != 0) {
throw std::runtime_error(
string("[interlaced] width must be divisible by ") +
std::to_string(step) + "!");
}
size_t rgb_linesize = vc_get_linesize(width, RGB);
vector<char> rgb(3 * h * rgb_linesize + 4 * (width / step) * rgb_linesize);
memset(rgb.data(), 255, h * rgb_linesize);
char *ptr = rgb.data() + h * rgb_linesize;
auto fill = [&](int col1, int col2) {
for (int i = 0; i < width; i += step) {
size_t fill_len = rgb_linesize - i * 3;
memset(ptr, col1, fill_len);
memset(ptr + fill_len, col2, rgb_linesize - fill_len);
ptr += rgb_linesize;
fill_len = MAX(0, (int) rgb_linesize - (i + 2 * step) * 3);
memset(ptr, col1, fill_len);
memset(ptr + fill_len, col2, rgb_linesize - fill_len);
ptr += rgb_linesize;
}
};
fill(255, 0);
memset(ptr, 0, h * rgb_linesize);
ptr += h * rgb_linesize;
fill(0, 255);
memset(ptr, 255, h * rgb_linesize);
vector<char> rgba(rgb.size() / 3 * 4);
for (unsigned i = 0; i < rgb.size(); i += 3) {
rgba[i / 3 * 4] = rgb[i];
rgba[i / 3 * 4 + 1] = rgb[i + 1];
rgba[i / 3 * 4 + 2] = rgb[i + 2];
rgba[i / 3 * 4 + 3] = 0xff;
}
data.resize(3 * h * linesize + 4 * linesize * (w / step) +
MAX_PADDING);
testcard_convert_buffer(RGBA, color_spec, (unsigned char *) data.data(), (unsigned char *) rgba.data(), width, 3 * height + 4 * (width / step));
}
char *get_next() override {
auto *out = (char *) data.data() + cur_idx * linesize;
cur_idx += 8;
if (cur_idx >= 2 * height + 4 * width / step) {
cur_idx = 0;
}
return out;
}
private:
constexpr static int step = 3;
int width;
int height;
size_t linesize;
int cur_idx = 0;
vector<char> data;
};
video_pattern_generator_t
video_pattern_generator_create(const char *config, int width, int height, codec_t color_spec, int offset)
{
if (string(config) == "help") {
col() << "Pattern to use, one of: \n";
for (const auto *p :
{
"bars[=<text>[,XYpt]]",
BLANK_USAGE,
"diagonal*",
"ebu_bars",
"gradient[=<color>|help]",
"gradient2*",
"gray",
"interlaced",
"noise",
"raw=0xXX[YYZZ..]",
"smpte_bars",
"strips*",
"uv_plane[=<y_lvl>]",
}) {
col() << "\t- " << SBOLD(p) << "\n";
}
col() << "\nNotes:\n";
col() << "\t- patterns " SBOLD("'gradient'") ", "
SBOLD("'gradient2'") ", " SBOLD("'noise'") " and "
SBOLD("'uv_plane'") " generate higher bit-depth "
"patterns with";
for (codec_t c = VIDEO_CODEC_FIRST; c != VIDEO_CODEC_COUNT;
c = static_cast<codec_t>(static_cast<int>(c) + 1)) {
if (get_decoder_from_to(RG48, c) != NULL && get_bits_per_component(c) > 8) {
col() << " " << SBOLD(get_codec_name(c));
}
}
col() << "\n";
col() << "\t- pattern "
<< SBOLD("'raw'") " generates repeating sequence of given "
"bytes without any color conversion\n";
col() << "\t- patterns marked with "
<< SBOLD("'*'") " provide help as its option\n";
return nullptr;
}
assert(width > 0 && height > 0);
try {
string pattern = config;
string params;
if (string::size_type delim = pattern.find('='); delim != string::npos) {
params = pattern.substr(delim + 1);
pattern = pattern.substr(0, delim);
}
if (pattern == "gray" || pattern == "grey") {
return new gray_video_pattern_generator{width, height, color_spec, params};
}
if (pattern == "interlaced") {
return new interlaced_video_pattern_generator{width, height, color_spec};
}
return new still_image_video_pattern_generator{pattern, params, width, height, color_spec, offset};
} catch (exception const &e) {
LOG(LOG_LEVEL_ERROR) << MOD_NAME << e.what() << "\n";
return nullptr;
} catch (...) {
return nullptr;
}
}
char *video_pattern_generator_next_frame(video_pattern_generator_t s)
{
return s->get_next();
}