-
Notifications
You must be signed in to change notification settings - Fork 17
/
frameblend.cpp
1176 lines (984 loc) · 36 KB
/
frameblend.cpp
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
#define __STDC_CONSTANT_MACROS
#define __STDC_LIMIT_MACROS
#include <sys/types.h>
#include <signal.h>
#include <stdint.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <math.h>
extern "C" {
#include <libavutil/opt.h>
#include <libavutil/avutil.h>
#include <libavutil/pixfmt.h>
#include <libavutil/pixdesc.h>
#include <libavutil/samplefmt.h>
#include <libavutil/pixelutils.h>
#include <libavcodec/avcodec.h>
#include <libavcodec/version.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavformat/version.h>
#include <libswscale/swscale.h>
#include <libswscale/version.h>
#include <libswresample/swresample.h>
#include <libswresample/version.h>
}
using namespace std;
#include <map>
#include <string>
#include <vector>
#include <stdexcept>
bool squelch_frameblend_near_match = false;
bool fullframealt = false;
int framealt = 1;
double gamma_correction = -1;
int underscan = 0;
bool use_422_colorspace = false;
AVRational output_field_rate = { 60000, 1001 }; // NTSC 60Hz default
int output_width = -1;
int output_height = -1;
int output_ar_n = 1,output_ar_d = 1;
#define RGBTRIPLET(r,g,b) (((uint32_t)(r) << (uint32_t)16) + ((uint32_t)(g) << (uint32_t)8) + ((uint32_t)(b) << (uint32_t)0))
AVFormatContext* output_avfmt = NULL;
AVStream* output_avstream_video = NULL; // do not free
AVCodecContext* output_avstream_video_codec_context = NULL; // do not free
AVFrame* output_avstream_video_frame = NULL; // ARGB
AVFrame* output_avstream_video_encode_frame = NULL; // 4:2:2 or 4:2:0
struct SwsContext* output_avstream_video_resampler = NULL;
class InputFile {
public:
InputFile() {
input_avfmt = NULL;
input_avstream_video = NULL;
input_avstream_video_frame = NULL;
input_avstream_video_frame_rgb = NULL;
input_avstream_video_resampler = NULL;
input_avstream_video_codec_context = NULL;
next_pts = next_dts = -1LL;
avpkt_valid = false;
eof_stream = false;
eof = false;
}
~InputFile() {
close_input();
}
public:
double video_frame_to_output_f(void) {
if (input_avstream_video_frame != NULL) {
if (input_avstream_video_frame->pts != AV_NOPTS_VALUE) {
double n = input_avstream_video_frame->pts;
n *= (signed long long)input_avstream_video->time_base.num * (signed long long)output_field_rate.num;
n /= (signed long long)input_avstream_video->time_base.den * (signed long long)output_field_rate.den;
return n;
}
}
return AV_NOPTS_VALUE;
}
double video_frame_rgb_to_output_f(void) {
if (input_avstream_video_frame_rgb != NULL) {
if (input_avstream_video_frame_rgb->pts != AV_NOPTS_VALUE) {
double n = input_avstream_video_frame_rgb->pts;
n *= (signed long long)input_avstream_video->time_base.num * (signed long long)output_field_rate.num;
n /= (signed long long)input_avstream_video->time_base.den * (signed long long)output_field_rate.den;
return n;
}
}
return AV_NOPTS_VALUE;
}
void reset_on_dup(void) {
path.clear();
}
bool open_input(void) {
if (input_avfmt == NULL) {
if (avformat_open_input(&input_avfmt,path.c_str(),NULL,NULL) < 0) {
fprintf(stderr,"Failed to open input file\n");
close_input();
return false;
}
if (avformat_find_stream_info(input_avfmt,NULL) < 0)
fprintf(stderr,"WARNING: Did not find stream info on input\n");
/* scan streams for one video, one audio */
{
size_t i;
AVStream *is;
int ac=0,vc=0;
AVCodecParameters *ispar;
fprintf(stderr,"Input format: %u streams found\n",input_avfmt->nb_streams);
for (i=0;i < (size_t)input_avfmt->nb_streams;i++) {
is = input_avfmt->streams[i];
if (is == NULL) continue;
ispar = is->codecpar;
if (ispar == NULL) continue;
if (ispar->codec_type == AVMEDIA_TYPE_VIDEO) {
if (input_avstream_video == NULL && vc == 0) {
if ((input_avstream_video_codec_context=avcodec_alloc_context3(avcodec_find_decoder(ispar->codec_id))) != NULL) {
if (avcodec_parameters_to_context(input_avstream_video_codec_context,ispar) < 0)
fprintf(stderr,"WARNING: parameters to context failed\n");
if (avcodec_open2(input_avstream_video_codec_context,avcodec_find_decoder(ispar->codec_id),NULL) >= 0) {
input_avstream_video = is;
fprintf(stderr,"Found video stream idx=%zu\n",i);
}
else {
fprintf(stderr,"Found video stream but not able to decode\n");
avcodec_free_context(&input_avstream_video_codec_context);
}
}
}
vc++;
}
}
if (input_avstream_video == NULL) {
fprintf(stderr,"Video not found\n");
close_input();
return 1;
}
}
}
/* prepare video decoding */
if (input_avstream_video != NULL) {
input_avstream_video_frame = av_frame_alloc();
if (input_avstream_video_frame == NULL) {
fprintf(stderr,"Failed to alloc video frame\n");
close_input();
return 1;
}
}
input_avstream_video_resampler_format = AV_PIX_FMT_NONE;
input_avstream_video_resampler_height = -1;
input_avstream_video_resampler_width = -1;
eof_stream = false;
got_video = false;
adj_time = 0;
t = pt = -1;
eof = false;
avpkt_init();
next_pts = next_dts = -1LL;
return (input_avfmt != NULL);
}
uint32_t *copy_rgba(const AVFrame * const src) {
assert(src != NULL);
assert(src->data[0] != NULL);
assert(src->linesize[0] != 0);
assert(src->height != 0);
assert(src->linesize[0] >= (src->width * 4));
uint32_t *r = (uint32_t*)(new uint8_t[src->linesize[0] * src->height]);
memcpy(r,src->data[0],src->linesize[0] * src->height);
return r;
}
bool next_packet(void) {
if (eof) return false;
if (input_avfmt == NULL) return false;
do {
if (eof_stream) break;
avpkt_release();
avpkt_init();
if (av_read_frame(input_avfmt,avpkt) < 0) {
eof_stream = true;
return false;
}
if (avpkt->stream_index >= input_avfmt->nb_streams)
continue;
// ugh... this can happen if the source is an AVI file
if (avpkt->pts == AV_NOPTS_VALUE) avpkt->pts = avpkt->dts;
/* track time and keep things monotonic for our code */
if (avpkt->pts != AV_NOPTS_VALUE) {
t = avpkt->pts * av_q2d(input_avfmt->streams[avpkt->stream_index]->time_base);
if (pt < 0)
adj_time = -t;
else if ((t+1.5) < pt) { // time code jumps backwards (1.5 is safe for DVD timecode resets)
adj_time += pt - t;
fprintf(stderr,"Time code jump backwards %.6f->%.6f. adj_time=%.6f\n",pt,t,adj_time);
}
else if (t > (pt+5)) { // time code jumps forwards
adj_time += pt - t;
fprintf(stderr,"Time code jump forwards %.6f->%.6f. adj_time=%.6f\n",pt,t,adj_time);
}
pt = t;
}
if (pt < 0)
continue;
if (avpkt->pts != AV_NOPTS_VALUE) {
avpkt->pts += (adj_time * input_avfmt->streams[avpkt->stream_index]->time_base.den) /
input_avfmt->streams[avpkt->stream_index]->time_base.num;
}
if (avpkt->dts != AV_NOPTS_VALUE) {
avpkt->dts += (adj_time * input_avfmt->streams[avpkt->stream_index]->time_base.den) /
input_avfmt->streams[avpkt->stream_index]->time_base.num;
}
got_video = false;
if (input_avstream_video != NULL && avpkt->stream_index == input_avstream_video->index) {
if (got_video) fprintf(stderr,"Video content lost\n");
// AVRational m = (AVRational){output_field_rate.den, output_field_rate.num};
// av_packet_rescale_ts(avpkt,input_avstream_video->time_base,m); // convert to FIELD number
handle_frame(/*&*/(*avpkt)); // will set got_video
break;
}
avpkt_release();
} while (1);
if (eof_stream) {
avpkt_release();
handle_frame(); // will set got_video
if (!got_video) eof = true;
else fprintf(stderr,"Got latent frame\n");
}
return true;
}
void frame_copy_scale(void) {
if (input_avstream_video_frame_rgb == NULL) {
fprintf(stderr,"New input frame\n");
input_avstream_video_frame_rgb = av_frame_alloc();
if (input_avstream_video_frame_rgb == NULL) {
fprintf(stderr,"Failed to alloc video frame\n");
return;
}
input_avstream_video_frame_rgb->format = AV_PIX_FMT_BGRA;
input_avstream_video_frame_rgb->height = output_height;
input_avstream_video_frame_rgb->width = output_width;
if (av_frame_get_buffer(input_avstream_video_frame_rgb,64) < 0) {
fprintf(stderr,"Failed to alloc render frame\n");
return;
}
memset(input_avstream_video_frame_rgb->data[0],0,input_avstream_video_frame_rgb->linesize[0]*input_avstream_video_frame_rgb->height);
fprintf(stderr,"RGB is %d, %d\n",input_avstream_video_frame_rgb->width,input_avstream_video_frame_rgb->height);
}
if (input_avstream_video_resampler != NULL) { // pixel format change or width/height change = free resampler and reinit
if (input_avstream_video_resampler_format != input_avstream_video_frame->format ||
input_avstream_video_resampler_width != input_avstream_video_frame->width ||
input_avstream_video_resampler_height != input_avstream_video_frame->height) {
sws_freeContext(input_avstream_video_resampler);
input_avstream_video_resampler = NULL;
}
}
if (input_avstream_video_resampler == NULL) {
int final_w,final_h;
final_w = (input_avstream_video_frame_rgb->width * (100 - underscan)) / 100;
final_h = (input_avstream_video_frame_rgb->height * (100 - underscan)) / 100;
if (final_w < 1) final_w = 1;
if (final_h < 1) final_h = 1;
input_avstream_video_resampler = sws_getContext(
// source
input_avstream_video_frame->width,
input_avstream_video_frame->height,
(AVPixelFormat)input_avstream_video_frame->format,
// dest
final_w,// input_avstream_video_frame_rgb->width,
final_h,// input_avstream_video_frame_rgb->height,
(AVPixelFormat)input_avstream_video_frame_rgb->format,
// opt
SWS_BILINEAR, NULL, NULL, NULL);
if (input_avstream_video_resampler != NULL) {
fprintf(stderr,"sws_getContext new context\n");
input_avstream_video_resampler_format = (AVPixelFormat)input_avstream_video_frame->format;
input_avstream_video_resampler_width = input_avstream_video_frame->width;
input_avstream_video_resampler_height = input_avstream_video_frame->height;
input_avstream_video_resampler_x = (input_avstream_video_frame_rgb->width - final_w) / 2;
input_avstream_video_resampler_y = (input_avstream_video_frame_rgb->height - final_h) / 2;
assert(input_avstream_video_resampler_x >= 0);
assert(input_avstream_video_resampler_y >= 0);
fprintf(stderr,"dst %d, %d\n",input_avstream_video_frame_rgb->width,input_avstream_video_frame_rgb->height);
fprintf(stderr,"ofs %d, %d\n",input_avstream_video_resampler_x,input_avstream_video_resampler_y);
}
else {
fprintf(stderr,"sws_getContext fail\n");
}
}
if (input_avstream_video_resampler != NULL) {
input_avstream_video_frame_rgb->pts = input_avstream_video_frame->pts;
input_avstream_video_frame_rgb->pkt_dts = input_avstream_video_frame->pkt_dts;
input_avstream_video_frame_rgb->top_field_first = input_avstream_video_frame->top_field_first;
input_avstream_video_frame_rgb->interlaced_frame = input_avstream_video_frame->interlaced_frame;
unsigned char *dst_planes[8] = {NULL};
dst_planes[0] = input_avstream_video_frame_rgb->data[0];
dst_planes[0] += input_avstream_video_resampler_y * input_avstream_video_frame_rgb->linesize[0];
dst_planes[0] += input_avstream_video_resampler_x * 4;
if (sws_scale(input_avstream_video_resampler,
// source
input_avstream_video_frame->data,
input_avstream_video_frame->linesize,
0,input_avstream_video_frame->height,
// dest
dst_planes,//input_avstream_video_frame_rgb->data,
input_avstream_video_frame_rgb->linesize) <= 0)
fprintf(stderr,"WARNING: sws_scale failed\n");
}
}
void handle_frame(void) {
avcodec_send_packet(input_avstream_video_codec_context,NULL);
if (avcodec_receive_frame(input_avstream_video_codec_context,input_avstream_video_frame) >= 0) {
got_video = true;
}
else {
got_video = false;
fprintf(stderr,"No video decoded\n");
}
}
void handle_frame(AVPacket &pkt) {
avcodec_send_packet(input_avstream_video_codec_context,&pkt);
if (avcodec_receive_frame(input_avstream_video_codec_context,input_avstream_video_frame) >= 0) {
got_video = true;
}
else {
got_video = false;
fprintf(stderr,"No video decoded\n");
}
}
void avpkt_init(void) {
if (!avpkt_valid) {
avpkt_valid = true;
avpkt = av_packet_alloc();
}
}
void avpkt_release(void) {
if (avpkt_valid) {
avpkt_valid = false;
av_packet_free(&avpkt);
}
got_video = false;
}
void close_input(void) {
eof = true;
avpkt_release();
if (input_avstream_video_codec_context != NULL) {
avcodec_close(input_avstream_video_codec_context);
avcodec_free_context(&input_avstream_video_codec_context);
assert(input_avstream_video_codec_context == NULL);
input_avstream_video = NULL;
}
if (input_avstream_video_frame != NULL)
av_frame_free(&input_avstream_video_frame);
if (input_avstream_video_frame_rgb != NULL)
av_frame_free(&input_avstream_video_frame_rgb);
if (input_avstream_video_resampler != NULL) {
sws_freeContext(input_avstream_video_resampler);
input_avstream_video_resampler = NULL;
}
avformat_close_input(&input_avfmt);
}
public:
std::string path;
uint32_t color;
bool eof;
bool eof_stream;
bool got_video;
public:
AVFormatContext* input_avfmt;
AVStream* input_avstream_video; // do not free
AVCodecContext* input_avstream_video_codec_context;
AVFrame* input_avstream_video_frame;
AVFrame* input_avstream_video_frame_rgb;
struct SwsContext* input_avstream_video_resampler;
AVPixelFormat input_avstream_video_resampler_format;
int input_avstream_video_resampler_height;
int input_avstream_video_resampler_width;
int input_avstream_video_resampler_y;
int input_avstream_video_resampler_x;
signed long long next_pts;
signed long long next_dts;
AVPacket* avpkt = NULL;
bool avpkt_valid;
double adj_time;
double t,pt;
};
std::vector<InputFile> input_files;
std::string output_file;
InputFile ¤t_input_file(void) {
if (input_files.empty()) {
std::string what = "input files empty";
throw std::out_of_range(/*&*/what);
}
return *(input_files.rbegin()); /* last one */
}
InputFile &new_input_file(void) {
if (!input_files.empty()) {
/* copy the last one, except for some fields */
{
InputFile &last = current_input_file();
input_files.push_back(last);
}
{
InputFile &last = current_input_file();
last.reset_on_dup();
}
}
else {
/* make a new one with defaults */
input_files.push_back(InputFile());
}
return current_input_file();
}
volatile int DIE = 0;
void sigma(int x) {
if (++DIE >= 20) abort();
}
void preset_NTSC() {
output_field_rate.num = 60000;
output_field_rate.den = 1001;
}
static void help(const char *arg0) {
fprintf(stderr,"%s [options]\n",arg0);
fprintf(stderr," -i <input file> you can specify more than one input file, in order of layering\n");
fprintf(stderr," -o <output file>\n");
fprintf(stderr," -or <frame rate>\n");
fprintf(stderr," -width <x>\n");
fprintf(stderr," -height <x>\n");
fprintf(stderr," -sqnr Squelch frame interpolation when frame rates match (1%% margin)\n");
fprintf(stderr," -ffa Full frame alternate interpolation\n");
fprintf(stderr," -fa <x> Interpolate alternate frames\n");
fprintf(stderr," -gamma <x> Interpolate with gamma correction (number, ntsc, vga)\n");
fprintf(stderr," -underscan <x> Underscan the image during rendering\n");
fprintf(stderr," -422 Render to 4:2:2 colorspace\n");
fprintf(stderr," -420 Render to 4:2:0 colorspace\n");
}
static int parse_argv(int argc,char **argv) {
const char *a;
int i;
for (i=1;i < argc;) {
a = argv[i++];
if (*a == '-') {
do { a++; } while (*a == '-');
if (!strcmp(a,"h") || !strcmp(a,"help")) {
help(argv[0]);
return 1;
}
else if (!strcmp(a,"width")) {
a = argv[i++];
if (a == NULL) return 1;
output_width = (int)strtoul(a,NULL,0);
if (output_width < 32) return 1;
}
else if (!strcmp(a,"height")) {
a = argv[i++];
if (a == NULL) return 1;
output_height = (int)strtoul(a,NULL,0);
if (output_height < 32) return 1;
}
else if (!strcmp(a,"sqnr")) {
squelch_frameblend_near_match = true;
}
else if (!strcmp(a,"ffa")) {
fullframealt = true;
}
else if (!strcmp(a,"fa")) {
a = argv[i++];
if (a == NULL) return 1;
framealt = atoi(a);
if (framealt < 1) framealt = 1;
if (framealt > 8) framealt = 8;
}
else if (!strcmp(a,"gamma")) {
a = argv[i++];
if (a == NULL) return 1;
if (isdigit(*a))
gamma_correction = atof(a);
else if (!strcmp(a,"vga") || !strcmp(a,"ntsc"))
gamma_correction = 2.2;
}
else if (!strcmp(a,"i")) {
a = argv[i++];
if (a == NULL) return 1;
new_input_file().path = a;
}
else if (!strcmp(a,"or")) {
a = argv[i++];
if (a == NULL) return 1;
int d = 1;
double n = strtof(a,(char**)(&a));
if (*a == ':' || *a == '/' || *a == '\\') {
a++;
d = strtoul(a,(char**)(&a),10);
if (d < 1) d = 1;
}
if (n < 0) n = 0;
/* this code can cause problems below 5fps */
if ((n/d) < 5) {
n = 5;
d = 1;
}
if (d > 1) {
output_field_rate.num = (long)floor(n + 0.5);
output_field_rate.den = (long)d;
}
else {
output_field_rate.num = (long)floor((n * 10000) + 0.5);
output_field_rate.den = (long)10000;
}
}
else if (!strcmp(a,"o")) {
a = argv[i++];
if (a == NULL) return 1;
output_file = a;
}
else if (!strcmp(a,"underscan")) {
a = argv[i++];
if (a == NULL) return 1;
underscan = atoi(a);
if (underscan < 0) underscan = 0;
if (underscan > 99) underscan = 99;
}
else if (!strcmp(a,"422")) {
use_422_colorspace = true;
}
else if (!strcmp(a,"420")) {
use_422_colorspace = false;
}
else {
fprintf(stderr,"Unknown switch '%s'\n",a);
return 1;
}
}
else {
fprintf(stderr,"Unhandled arg '%s'\n",a);
return 1;
}
}
if (output_file.empty()) {
fprintf(stderr,"No output file specified\n");
return 1;
}
if (input_files.empty()) {
fprintf(stderr,"No input files specified\n");
return 1;
}
return 0;
}
void output_frame(AVFrame *frame,unsigned long long field_number) {
int gotit = 0;
AVPacket* pkt = av_packet_alloc();
frame->key_frame = (field_number % (15ULL * 2ULL)) == 0 ? 1 : 0;
{
frame->interlaced_frame = 0;
frame->pts = field_number;
pkt->pts = field_number;
pkt->dts = field_number;
}
fprintf(stderr,"\x0D" "Output field %llu ",field_number); fflush(stderr);
avcodec_send_frame(output_avstream_video_codec_context,frame);
while (avcodec_receive_packet(output_avstream_video_codec_context,pkt) >= 0) {
pkt->stream_index = output_avstream_video->index;
av_packet_rescale_ts(pkt,output_avstream_video_codec_context->time_base,output_avstream_video->time_base);
if (av_interleaved_write_frame(output_avfmt,pkt) < 0)
fprintf(stderr,"AV write frame failed video\n");
}
av_packet_free(&pkt);
}
// This code assumes ARGB and the frame match resolution/
void composite_layer(AVFrame *dstframe,AVFrame *srcframe,InputFile &inputfile) {
uint32_t *dscan,*sscan;
unsigned int x,y;
unsigned int shr;
if (dstframe == NULL || srcframe == NULL) return;
if (dstframe->data[0] == NULL || srcframe->data[0] == 0) return;
if (dstframe->linesize[0] < (dstframe->width*4)) return; // ARGB
if (srcframe->linesize[0] < (srcframe->width*4)) return; // ARGB
if (dstframe->width != srcframe->width) return;
if (dstframe->height != srcframe->height) return;
for (y=0;y < dstframe->height;y++) {
sscan = (uint32_t*)(srcframe->data[0] + (srcframe->linesize[0] * y));
dscan = (uint32_t*)(dstframe->data[0] + (dstframe->linesize[0] * y));
for (x=0;x < dstframe->width;x++,dscan++,sscan++) {
*dscan = *sscan;
}
}
}
int clamp255(int x) {
if (x > 255)
return 255;
if (x < 0)
return 0;
return x;
}
double gamma_dec(double x) {
return pow(x,gamma_correction);
}
unsigned long gamma_dec16_table[256];
unsigned long gamma_enc16_table[8192 + 1];
bool gamma16_init = false;
void gamma16_do_init(void);
unsigned long gamma_dec16(unsigned long x) {
if (!gamma16_init) gamma16_do_init();
if (x > 255u) x = 255u;
return gamma_dec16_table[x];
}
double gamma_enc(double x) {
return pow(x,1.0 / gamma_correction);
}
unsigned long gamma_enc16(unsigned long x) {
if (!gamma16_init) gamma16_do_init();
if (x > 8192u) x = 8192u;
return gamma_enc16_table[x];
}
void gamma16_do_init(void) {
gamma16_init = true;
for (unsigned int i=0;i < 256;i++)
gamma_dec16_table[i] = (unsigned long)(gamma_dec(i / 255.0) * 8192);
for (unsigned int i=0;i <= 8192;i++)
gamma_enc16_table[i] = (unsigned long)(gamma_enc(i / 8192.0) * 255);
}
int main(int argc,char **argv) {
preset_NTSC();
if (parse_argv(argc,argv))
return 1;
/* open all input files */
for (std::vector<InputFile>::iterator i=input_files.begin();i!=input_files.end();i++) {
if (!(*i).open_input()) {
fprintf(stderr,"Failed to open %s\n",(*i).path.c_str());
return 1;
}
}
/* pick output */
if (output_width < 1 || output_height < 1) {
for (std::vector<InputFile>::iterator i=input_files.begin();i!=input_files.end();i++) {
if ((*i).input_avstream_video_codec_context != NULL) {
output_width = (*i).input_avstream_video_codec_context->width;
output_height = (*i).input_avstream_video_codec_context->height;
output_ar_n = (*i).input_avstream_video_codec_context->sample_aspect_ratio.num;
output_ar_d = (*i).input_avstream_video_codec_context->sample_aspect_ratio.den;
break;
}
}
}
fprintf(stderr,"Output frame: %d x %d with %d:%d PAR\n",output_width,output_height,output_ar_n,output_ar_d);
/* no decision, no frame */
if (output_width < 16 || output_height < 16) {
fprintf(stderr,"None or invalid output dimensions\n");
return 1;
}
/* open output file */
assert(output_avfmt == NULL);
if (avformat_alloc_output_context2(&output_avfmt,NULL,NULL,output_file.c_str()) < 0) {
fprintf(stderr,"Failed to open output file\n");
return 1;
}
{
output_avstream_video = avformat_new_stream(output_avfmt, NULL);
if (output_avstream_video == NULL) {
fprintf(stderr,"Unable to create output video stream\n");
return 1;
}
output_avstream_video_codec_context = avcodec_alloc_context3(avcodec_find_encoder(AV_CODEC_ID_H264));
if (output_avstream_video_codec_context == NULL) {
fprintf(stderr,"Output stream video no codec context?\n");
return 1;
}
output_avstream_video_codec_context->width = output_width;
output_avstream_video_codec_context->height = output_height;
output_avstream_video_codec_context->sample_aspect_ratio = (AVRational){output_ar_n,output_ar_d};
output_avstream_video_codec_context->pix_fmt = use_422_colorspace ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUV420P;
output_avstream_video_codec_context->gop_size = 15;
output_avstream_video_codec_context->max_b_frames = 0;
output_avstream_video_codec_context->bit_rate = 25000000;
output_avstream_video_codec_context->time_base = (AVRational){output_field_rate.den, output_field_rate.num};
output_avstream_video->time_base = output_avstream_video_codec_context->time_base;
if (output_avfmt->oformat->flags & AVFMT_GLOBALHEADER)
output_avstream_video_codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
if (avcodec_open2(output_avstream_video_codec_context,avcodec_find_encoder(AV_CODEC_ID_H264),NULL) < 0) {
fprintf(stderr,"Output stream cannot open codec\n");
return 1;
}
if (avcodec_parameters_from_context(output_avstream_video->codecpar,output_avstream_video_codec_context) < 0)
fprintf(stderr,"WARNING: parameters from context failed\n");
}
if (!(output_avfmt->oformat->flags & AVFMT_NOFILE)) {
if (avio_open(&output_avfmt->pb, output_file.c_str(), AVIO_FLAG_WRITE) < 0) {
fprintf(stderr,"Output file cannot open file\n");
return 1;
}
}
if (avformat_write_header(output_avfmt,NULL) < 0) {
fprintf(stderr,"Failed to write header\n");
return 1;
}
/* soft break on CTRL+C */
signal(SIGINT,sigma);
signal(SIGHUP,sigma);
signal(SIGQUIT,sigma);
signal(SIGTERM,sigma);
/* prepare video encoding */
output_avstream_video_frame = av_frame_alloc();
if (output_avstream_video_frame == NULL) {
fprintf(stderr,"Failed to alloc video frame\n");
return 1;
}
output_avstream_video_frame->format = AV_PIX_FMT_BGRA;
output_avstream_video_frame->height = output_height;
output_avstream_video_frame->width = output_width;
if (av_frame_get_buffer(output_avstream_video_frame,64) < 0) {
fprintf(stderr,"Failed to alloc render frame\n");
return 1;
}
{
output_avstream_video_encode_frame = av_frame_alloc();
if (output_avstream_video_encode_frame == NULL) {
fprintf(stderr,"Failed to alloc video frame3\n");
return 1;
}
output_avstream_video_encode_frame->colorspace = AVCOL_SPC_SMPTE170M;
output_avstream_video_encode_frame->color_range = AVCOL_RANGE_MPEG;
output_avstream_video_encode_frame->format = output_avstream_video_codec_context->pix_fmt;
output_avstream_video_encode_frame->height = output_height;
output_avstream_video_encode_frame->width = output_width;
if (av_frame_get_buffer(output_avstream_video_encode_frame,64) < 0) {
fprintf(stderr,"Failed to alloc render frame2\n");
return 1;
}
}
if (output_avstream_video_resampler == NULL) {
output_avstream_video_resampler = sws_getContext(
// source
output_avstream_video_frame->width,
output_avstream_video_frame->height,
(AVPixelFormat)output_avstream_video_frame->format,
// dest
output_avstream_video_encode_frame->width,
output_avstream_video_encode_frame->height,
(AVPixelFormat)output_avstream_video_encode_frame->format,
// opt
SWS_BILINEAR, NULL, NULL, NULL);
if (output_avstream_video_resampler == NULL) {
fprintf(stderr,"Failed to alloc ARGB -> codec converter\n");
return 1;
}
}
/* run all inputs and render to output, until done */
{
signed long long outbase=0;
memset(output_avstream_video_frame->data[0],0x00,output_avstream_video_frame->linesize[0] * output_avstream_video_frame->height);
for (std::vector<InputFile>::iterator ifi=input_files.begin();ifi!=input_files.end();ifi++) {
signed long long current=0;
if (DIE) break;
InputFile &input_file = *ifi;
while (!input_file.eof && !DIE) {
if (!input_file.got_video)
input_file.next_packet();
else
break;
}
if (input_file.input_avstream_video_frame != NULL && input_file.got_video) {
input_file.frame_copy_scale();
input_file.got_video = false;
}
std::vector<uint32_t*> frames; /* they're all the same RGBA frame, so just store a pointer */
std::vector<double> frame_t;
if (input_file.input_avstream_video_frame_rgb != NULL) {
frame_t.push_back(input_file.video_frame_rgb_to_output_f());
frames.push_back(input_file.copy_rgba(input_file.input_avstream_video_frame_rgb));
}
while (!DIE) {
while (!input_file.eof && !DIE && input_file.video_frame_to_output_f() < (current + 30LL)) {
input_file.next_packet();
if (input_file.input_avstream_video_frame != NULL && input_file.got_video) {
input_file.frame_copy_scale();
input_file.got_video = false;
if (input_file.input_avstream_video_frame_rgb != NULL) {
frame_t.push_back(input_file.video_frame_rgb_to_output_f());
frames.push_back(input_file.copy_rgba(input_file.input_avstream_video_frame_rgb));
}
}
}
if (input_file.eof &&
(input_file.video_frame_to_output_f() < -1000/*AV_NOPTS_VALUE*/ ||
current > (unsigned long long)ceil(input_file.video_frame_to_output_f())))
break;
/* cross-blending weights for this frame period */
std::vector< pair<size_t,double> > weights;
assert(frames.size() == frame_t.size());
size_t cutoff = 0;
/* scan for first frame to render */
if (frames.size() > 1) {
if (framealt > 1) {
for (size_t i=(size_t)((unsigned long long)current % (unsigned long long)framealt);(i+(size_t)framealt) < frames.size();i += (size_t)framealt) {
double bt = frame_t[i];
double et = frame_t[i+framealt];
if (i != 0) {
if ((et + 2.0) < current) {
cutoff = i - (i % framealt);
}
}
if (bt < current)
bt = current;
if (bt > (current + (fullframealt ? framealt : 1)))
bt = (current + (fullframealt ? framealt : 1));
if (et < current)
et = current;
if (et > (current + (fullframealt ? framealt : 1)))
et = (current + (fullframealt ? framealt : 1));
assert(bt <= et);
if (bt < et)
weights.push_back(pair<size_t,double>(i,(et-bt) / (fullframealt ? framealt : 1)));
}
}
else {
for (size_t i=0;(i+1ul) < frames.size();i++) {
double bt = frame_t[i];
double et = frame_t[i+1];
if (i != 0) {
if ((et + 2.0) < current) {
cutoff = i;
}
}
if (bt < current)
bt = current;
if (bt > (current + 1ll))
bt = (current + 1ll);
if (et < current)
et = current;
if (et > (current + 1ll))
et = (current + 1ll);
assert(bt <= et);
if (bt < et)
weights.push_back(pair<size_t,double>(i,et-bt));
}
}
}
if (weights.size() == 0 && frames.size() > cutoff)
weights.push_back(pair<size_t,double>(cutoff,1.0));
if (squelch_frameblend_near_match) {
if (weights.size() == 2 || weights.size() == 3) {
double sq = 1.0;
assert(weights[0].first < frame_t.size());
assert(weights[1].first < frame_t.size());