forked from HandBrake/HandBrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
work.c
2159 lines (1970 loc) · 72.1 KB
/
work.c
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
/* work.c
Copyright (c) 2003-2021 HandBrake Team
This file is part of the HandBrake source code
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License v2.
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
#include "handbrake/handbrake.h"
#include "libavformat/avformat.h"
#include "handbrake/decomb.h"
#include "handbrake/hbavfilter.h"
#if HB_PROJECT_FEATURE_QSV
#include "handbrake/qsv_common.h"
#include "handbrake/qsv_filter_pp.h"
#endif
typedef struct
{
hb_list_t * jobs;
hb_job_t ** current_job;
hb_error_code * error;
volatile int * die;
} hb_work_t;
static void work_func();
static void do_job( hb_job_t *);
static void filter_loop( void * );
#define FIFO_UNBOUNDED 65536
#define FIFO_UNBOUNDED_WAKE 65535
#define FIFO_LARGE 32
#define FIFO_LARGE_WAKE 16
#define FIFO_SMALL 16
#define FIFO_SMALL_WAKE 15
#define FIFO_MINI 4
#define FIFO_MINI_WAKE 3
/**
* Allocates work object and launches work thread with work_func.
* @param jobs Handle to hb_list_t.
* @param die Handle to user inititated exit indicator.
* @param error Handle to error indicator.
*/
hb_thread_t * hb_work_init( hb_list_t * jobs, volatile int * die, hb_error_code * error, hb_job_t ** job )
{
hb_work_t * work = calloc( sizeof( hb_work_t ), 1 );
work->jobs = jobs;
work->current_job = job;
work->die = die;
work->error = error;
return hb_thread_init( "work", work_func, work, HB_LOW_PRIORITY );
}
static void InitWorkState(hb_job_t * job, int pass, int pass_count)
{
hb_state_t state;
memset(&state, 0, sizeof(state));
state.state = HB_STATE_WORKING;
state.sequence_id = job->sequence_id;
#define p state.param.working
p.pass_id = job->pass_id;
p.pass = pass;
p.pass_count = pass_count;
p.progress = 0.0;
p.rate_cur = 0.0;
p.rate_avg = 0.0;
p.eta_seconds = 0;
p.hours = -1;
p.minutes = -1;
p.seconds = -1;
#undef p
hb_set_state( job->h, &state );
}
static void SetWorkStateInfo(hb_job_t *job)
{
hb_state_t state;
if (job == NULL)
{
return;
}
hb_get_state2(job->h, &state);
state.param.working.error = *job->done_error;
hb_set_state( job->h, &state );
}
/**
* Iterates through job list and calls do_job for each job.
* @param _work Handle work object.
*/
static void work_func( void * _work )
{
hb_work_t * work = _work;
hb_job_t * job;
time_t t = time(NULL);
hb_log("Starting work at: %s", asctime(localtime(&t)));
hb_log( "%d job(s) to process", hb_list_count( work->jobs ) );
while( !*work->die && ( job = hb_list_item( work->jobs, 0 ) ) )
{
hb_handle_t * h = job->h;
hb_list_rem( work->jobs, job );
hb_list_t * passes = hb_list_init();
// JSON jobs get special treatment. We want to perform the title
// scan for the JSON job automatically. This requires that we delay
// filling the job struct till we have performed the title scan
// because the default values for the job come from the title.
if (job->json != NULL)
{
hb_deep_log(1, "json job:\n%s", job->json);
// Initialize state sequence_id
InitWorkState(job, 0, 0);
// Perform title scan for json job
hb_json_job_scan(job->h, job->json);
// Expand json string to full job struct
hb_job_t *new_job = hb_json_to_job(job->h, job->json);
if (new_job == NULL)
{
hb_job_close(&job);
hb_list_close(&passes);
*work->error = HB_ERROR_INIT;
*work->die = 1;
break;
}
new_job->h = job->h;
new_job->sequence_id = job->sequence_id;
hb_job_close(&job);
job = new_job;
}
#if HB_PROJECT_FEATURE_QSV
if (hb_qsv_available())
{
hb_qsv_setup_job(job);
}
#endif
hb_job_setup_passes(job->h, job, passes);
hb_job_close(&job);
int pass_count, pass;
pass_count = hb_list_count(passes);
for (pass = 0; pass < pass_count && !*work->die; pass++)
{
job = hb_list_item(passes, pass);
job->die = work->die;
job->done_error = work->error;
*(work->current_job) = job;
InitWorkState(job, pass + 1, pass_count);
do_job( job );
}
SetWorkStateInfo(job);
*(work->current_job) = NULL;
// Clean job passes
for (pass = 0; pass < pass_count; pass++)
{
job = hb_list_item(passes, pass);
hb_job_close(&job);
}
hb_list_close(&passes);
// Force rescan of next source processed by this hb_handle_t
// TODO: Fix this ugly hack!
hb_force_rescan(h);
}
t = time(NULL);
hb_log("Finished work at: %s", asctime(localtime(&t)));
free( work );
}
hb_work_object_t * hb_get_work( hb_handle_t *h, int id )
{
hb_work_object_t * w;
for( w = hb_objects; w; w = w->next )
{
if( w->id == id )
{
hb_work_object_t *wc = malloc( sizeof(*w) );
*wc = *w;
wc->h = h;
return wc;
}
}
return NULL;
}
hb_work_object_t* hb_audio_decoder(hb_handle_t *h, int codec)
{
hb_work_object_t * w = NULL;
if (codec & HB_ACODEC_FF_MASK)
{
w = hb_get_work(h, WORK_DECAVCODEC);
}
switch (codec)
{
case HB_ACODEC_LPCM:
w = hb_get_work(h, WORK_DECLPCM);
break;
default:
break;
}
return w;
}
hb_work_object_t* hb_video_decoder(hb_handle_t *h, int vcodec, int param)
{
hb_work_object_t * w;
w = hb_get_work(h, vcodec);
if (w == NULL)
{
hb_error("Invalid video decoder: codec %d, param %d", vcodec, param);
return NULL;
}
w->codec_param = param;
return w;
}
hb_work_object_t* hb_video_encoder(hb_handle_t *h, int vcodec)
{
hb_work_object_t * w = NULL;
switch (vcodec)
{
case HB_VCODEC_FFMPEG_MPEG4:
w = hb_get_work(h, WORK_ENCAVCODEC);
w->codec_param = AV_CODEC_ID_MPEG4;
break;
case HB_VCODEC_FFMPEG_MPEG2:
w = hb_get_work(h, WORK_ENCAVCODEC);
w->codec_param = AV_CODEC_ID_MPEG2VIDEO;
break;
case HB_VCODEC_FFMPEG_VP8:
w = hb_get_work(h, WORK_ENCAVCODEC);
w->codec_param = AV_CODEC_ID_VP8;
break;
case HB_VCODEC_FFMPEG_VP9:
w = hb_get_work(h, WORK_ENCAVCODEC);
w->codec_param = AV_CODEC_ID_VP9;
break;
case HB_VCODEC_X264_8BIT:
case HB_VCODEC_X264_10BIT:
w = hb_get_work(h, WORK_ENCX264);
break;
case HB_VCODEC_QSV_H264:
case HB_VCODEC_QSV_H265:
case HB_VCODEC_QSV_H265_10BIT:
w = hb_get_work(h, WORK_ENCQSV);
break;
case HB_VCODEC_THEORA:
w = hb_get_work(h, WORK_ENCTHEORA);
break;
#if HB_PROJECT_FEATURE_X265
case HB_VCODEC_X265_8BIT:
case HB_VCODEC_X265_10BIT:
case HB_VCODEC_X265_12BIT:
case HB_VCODEC_X265_16BIT:
w = hb_get_work(h, WORK_ENCX265);
break;
#endif
#if HB_PROJECT_FEATURE_VCE
case HB_VCODEC_FFMPEG_VCE_H264:
w = hb_get_work(h, WORK_ENCAVCODEC);
w->codec_param = AV_CODEC_ID_H264;
break;
case HB_VCODEC_FFMPEG_VCE_H265:
w = hb_get_work(h, WORK_ENCAVCODEC);
w->codec_param = AV_CODEC_ID_HEVC;
break;
#endif
#if HB_PROJECT_FEATURE_NVENC
case HB_VCODEC_FFMPEG_NVENC_H264:
w = hb_get_work(h, WORK_ENCAVCODEC);
w->codec_param = AV_CODEC_ID_H264;
break;
case HB_VCODEC_FFMPEG_NVENC_H265:
w = hb_get_work(h, WORK_ENCAVCODEC);
w->codec_param = AV_CODEC_ID_HEVC;
break;
#endif
#ifdef __APPLE__
case HB_VCODEC_FFMPEG_VT_H264:
w = hb_get_work(h, WORK_ENCAVCODEC);
w->codec_param = AV_CODEC_ID_H264;
break;
case HB_VCODEC_FFMPEG_VT_H265:
case HB_VCODEC_FFMPEG_VT_H265_10BIT:
w = hb_get_work(h, WORK_ENCAVCODEC);
w->codec_param = AV_CODEC_ID_HEVC;
break;
#endif
#if HB_PROJECT_FEATURE_MF
case HB_VCODEC_FFMPEG_MF_H264:
w = hb_get_work(h, WORK_ENCAVCODEC);
w->codec_param = AV_CODEC_ID_H264;
break;
case HB_VCODEC_FFMPEG_MF_H265:
w = hb_get_work(h, WORK_ENCAVCODEC);
w->codec_param = AV_CODEC_ID_HEVC;
break;
#endif
default:
hb_error("Unknown video codec (0x%x)", vcodec );
}
return w;
}
hb_work_object_t* hb_audio_encoder(hb_handle_t *h, int codec)
{
if (codec & HB_ACODEC_FF_MASK)
{
return hb_get_work(h, WORK_ENCAVCODEC_AUDIO);
}
switch (codec)
{
case HB_ACODEC_AC3:
case HB_ACODEC_LAME: return hb_get_work(h, WORK_ENCAVCODEC_AUDIO);
case HB_ACODEC_VORBIS: return hb_get_work(h, WORK_ENCVORBIS);
case HB_ACODEC_CA_AAC: return hb_get_work(h, WORK_ENC_CA_AAC);
case HB_ACODEC_CA_HAAC: return hb_get_work(h, WORK_ENC_CA_HAAC);
default: break;
}
return NULL;
}
/**
* Displays job parameters in the debug log.
* @param job Handle work hb_job_t.
*/
void hb_display_job_info(hb_job_t *job)
{
int i;
hb_title_t *title = job->title;
hb_audio_t *audio;
hb_subtitle_t *subtitle;
hb_log("job configuration:");
hb_log( " * source");
hb_log( " + %s", title->path );
if( job->pts_to_start || job->pts_to_stop )
{
int64_t stop;
int hr_start, min_start, hr_stop, min_stop;
float sec_start, sec_stop;
stop = job->pts_to_start + job->pts_to_stop;
hr_start = job->pts_to_start / (90000 * 60 * 60);
min_start = job->pts_to_start / (90000 * 60);
sec_start = (float)job->pts_to_start / 90000.0 - min_start * 60;
min_start %= 60;
hr_stop = stop / (90000 * 60 * 60);
min_stop = stop / (90000 * 60);
sec_stop = (float)stop / 90000.0 - min_stop * 60;
min_stop %= 60;
if (job->pts_to_stop)
{
hb_log(" + title %d, start %02d:%02d:%02.2f stop %02d:%02d:%02.2f",
title->index,
hr_start, min_start, sec_start,
hr_stop, min_stop, sec_stop);
}
else
{
hb_log(" + title %d, start %02d:%02d:%02.2f",
title->index,
hr_start, min_start, sec_start);
}
}
else if( job->frame_to_start || job->frame_to_stop )
{
hb_log(" + title %d, frames %d to %d", title->index,
job->frame_to_start, job->frame_to_start +
job->frame_to_stop - 1);
}
else
{
hb_log( " + title %d, chapter(s) %d to %d", title->index,
job->chapter_start, job->chapter_end );
}
if( title->container_name != NULL )
hb_log( " + container: %s", title->container_name);
if( title->data_rate )
{
hb_log( " + data rate: %d kbps", title->data_rate / 1000 );
}
hb_log( " * destination");
hb_log( " + %s", job->file );
hb_log(" + container: %s", hb_container_get_long_name(job->mux));
switch (job->mux)
{
case HB_MUX_AV_MP4:
if (job->mp4_optimize)
hb_log(" + optimized for HTTP streaming (fast start)");
if (job->ipod_atom)
hb_log(" + compatibility atom for iPod 5G");
break;
default:
break;
}
if (job->align_av_start)
{
hb_log(" + align initial A/V stream timestamps");
}
if (job->inline_parameter_sets)
{
hb_log(" + optimized for adaptive streaming (inline parameter sets)");
}
if( job->chapter_markers )
{
hb_log( " + chapter markers" );
}
hb_log(" * video track");
#if HB_PROJECT_FEATURE_QSV
if (hb_qsv_decode_is_enabled(job))
{
hb_log(" + decoder: %s %d-bit",
hb_qsv_decode_get_codec_name(title->video_codec_param), hb_get_bit_depth(job->pix_fmt));
}
else
#endif
{
hb_log(" + decoder: %s %d-bit", title->video_codec_name, hb_get_bit_depth(job->pix_fmt));
}
if( title->video_bitrate )
{
hb_log( " + bitrate %d kbps", title->video_bitrate / 1000 );
}
// Filters can modify dimensions. So show them first.
if( hb_list_count( job->list_filter ) )
{
hb_log(" + %s", hb_list_count( job->list_filter) > 1 ? "filters" : "filter" );
for( i = 0; i < hb_list_count( job->list_filter ); i++ )
{
hb_filter_object_t * filter = hb_list_item( job->list_filter, i );
if (filter->aliased && global_verbosity_level < 2)
{
continue;
}
char * settings = hb_filter_settings_string(filter->id,
filter->settings);
if (settings != NULL)
hb_log(" + %s (%s)", filter->name, settings);
else
hb_log(" + %s (default settings)", filter->name);
free(settings);
if (filter->info)
{
hb_filter_info_t * info;
info = filter->info(filter);
if (info != NULL &&
info->human_readable_desc != NULL &&
info->human_readable_desc[0] != 0)
{
char * line, * pos = NULL;
char * tmp = strdup(info->human_readable_desc);
for (line = strtok_r(tmp, "\n", &pos); line != NULL;
line = strtok_r(NULL, "\n", &pos))
{
hb_log(" + %s", line);
}
free(tmp);
}
hb_filter_info_close(&info);
}
}
}
hb_log( " + Output geometry" );
hb_log( " + storage dimensions: %d x %d", job->width, job->height );
hb_log( " + pixel aspect ratio: %d : %d", job->par.num, job->par.den );
hb_log( " + display dimensions: %d x %d",
job->width * job->par.num / job->par.den, job->height );
if( !job->indepth_scan )
{
/* Video encoder */
hb_log(" + encoder: %s",
hb_video_encoder_get_long_name(job->vcodec));
if (job->encoder_preset && *job->encoder_preset &&
hb_video_encoder_get_presets(job->vcodec) != NULL)
{
hb_log(" + preset: %s", job->encoder_preset);
}
if (job->encoder_tune && *job->encoder_tune)
{
switch (job->vcodec)
{
case HB_VCODEC_X264_8BIT:
case HB_VCODEC_X264_10BIT:
case HB_VCODEC_X265_8BIT:
case HB_VCODEC_X265_10BIT:
case HB_VCODEC_X265_12BIT:
case HB_VCODEC_X265_16BIT:
hb_log(" + tune: %s", job->encoder_tune);
default:
break;
}
}
if (job->encoder_options != NULL && *job->encoder_options &&
job->vcodec != HB_VCODEC_THEORA)
{
hb_log(" + options: %s", job->encoder_options);
}
if (job->encoder_profile && *job->encoder_profile)
{
switch (job->vcodec)
{
case HB_VCODEC_X264_8BIT:
case HB_VCODEC_X264_10BIT:
case HB_VCODEC_X265_8BIT:
case HB_VCODEC_X265_10BIT:
case HB_VCODEC_X265_12BIT:
case HB_VCODEC_X265_16BIT:
case HB_VCODEC_QSV_H264:
case HB_VCODEC_QSV_H265:
case HB_VCODEC_QSV_H265_10BIT:
case HB_VCODEC_FFMPEG_VCE_H264:
case HB_VCODEC_FFMPEG_VCE_H265:
case HB_VCODEC_FFMPEG_NVENC_H264:
case HB_VCODEC_FFMPEG_NVENC_H265:
case HB_VCODEC_FFMPEG_VT_H264:
case HB_VCODEC_FFMPEG_VT_H265:
case HB_VCODEC_FFMPEG_VT_H265_10BIT:
case HB_VCODEC_FFMPEG_MF_H264:
case HB_VCODEC_FFMPEG_MF_H265:
hb_log(" + profile: %s", job->encoder_profile);
default:
break;
}
}
if (job->encoder_level && *job->encoder_level)
{
switch (job->vcodec)
{
case HB_VCODEC_X264_8BIT:
case HB_VCODEC_X264_10BIT:
case HB_VCODEC_X265_8BIT:
case HB_VCODEC_X265_10BIT:
case HB_VCODEC_X265_12BIT:
case HB_VCODEC_QSV_H264:
case HB_VCODEC_QSV_H265:
case HB_VCODEC_QSV_H265_10BIT:
case HB_VCODEC_FFMPEG_VCE_H264:
case HB_VCODEC_FFMPEG_VCE_H265:
case HB_VCODEC_FFMPEG_NVENC_H264:
case HB_VCODEC_FFMPEG_NVENC_H265:
case HB_VCODEC_FFMPEG_VT_H264:
// VT h.265 currently only supports auto level
// case HB_VCODEC_FFMPEG_VT_H265:
// MF h.264/h.265 currently only supports auto level
// case HB_VCODEC_FFMPEG_MF_H264:
// case HB_VCODEC_FFMPEG_MF_H265:
hb_log(" + level: %s", job->encoder_level);
default:
break;
}
}
if (job->vquality > HB_INVALID_VIDEO_QUALITY)
{
hb_log(" + quality: %.2f (%s)", job->vquality,
hb_video_quality_get_name(job->vcodec));
}
else
{
hb_log( " + bitrate: %d kbps, pass: %d", job->vbitrate, job->pass_id );
if(job->pass_id == HB_PASS_ENCODE_1ST && job->fastfirstpass == 1 &&
((job->vcodec & HB_VCODEC_X264_MASK) ||
(job->vcodec & HB_VCODEC_X265_MASK)))
{
hb_log( " + fast first pass" );
if (job->vcodec & HB_VCODEC_X264_MASK)
{
hb_log( " + options: ref=1:8x8dct=0:me=dia:trellis=0" );
hb_log( " analyse=i4x4 (if originally enabled, else analyse=none)" );
hb_log( " subq=2 (if originally greater than 2, else subq unchanged)" );
}
}
}
hb_log(" + color profile: %d-%d-%d",
job->color_prim, job->color_transfer, job->color_matrix);
if (job->color_transfer == HB_COLR_TRA_SMPTEST2084)
{
if (job->mastering.has_primaries || job->mastering.has_luminance)
{
hb_log(" + mastering display metadata: r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f) min_luminance=%f, max_luminance=%f",
hb_q2d(job->mastering.display_primaries[0][0]),
hb_q2d(job->mastering.display_primaries[0][1]),
hb_q2d(job->mastering.display_primaries[1][0]),
hb_q2d(job->mastering.display_primaries[1][1]),
hb_q2d(job->mastering.display_primaries[2][0]),
hb_q2d(job->mastering.display_primaries[2][1]),
hb_q2d(job->mastering.white_point[0]), hb_q2d(job->mastering.white_point[1]),
hb_q2d(job->mastering.min_luminance), hb_q2d(job->mastering.max_luminance));
}
if (job->coll.max_cll && job->coll.max_fall)
{
hb_log(" + content light level: max_cll=%u, max_fall=%u",
job->coll.max_cll,
job->coll.max_fall);
}
}
}
if (job->indepth_scan)
{
hb_log( " * Foreign Audio Search: %s%s%s",
job->select_subtitle_config.dest == RENDERSUB ? "Render/Burn-in" : "Passthrough",
job->select_subtitle_config.force ? ", Forced Only" : "",
job->select_subtitle_config.default_track ? ", Default" : "" );
}
for( i = 0; i < hb_list_count( job->list_subtitle ); i++ )
{
subtitle = hb_list_item( job->list_subtitle, i );
if( subtitle )
{
if( job->indepth_scan )
{
hb_log( " + subtitle, %s (track %d, id 0x%x, %s)",
subtitle->lang, subtitle->track, subtitle->id,
subtitle->format == PICTURESUB ? "Picture" : "Text");
}
else if (subtitle->source == IMPORTSRT)
{
/* For SRT, print offset and charset too */
hb_log(" * subtitle track %d, %s (track %d, id 0x%x, Text) -> "
"%s%s, offset: %"PRId64", charset: %s",
subtitle->out_track, subtitle->lang, subtitle->track,
subtitle->id,
subtitle->config.dest == RENDERSUB ? "Render/Burn-in"
: "Passthrough",
subtitle->config.default_track ? ", Default" : "",
subtitle->config.offset, subtitle->config.src_codeset);
}
else if (subtitle->source == IMPORTSSA)
{
/* For SSA, print offset */
hb_log(" * subtitle track %d, %s (track %d, id 0x%x, Text) -> "
"%s%s, offset: %"PRId64,
subtitle->out_track, subtitle->lang, subtitle->track,
subtitle->id,
subtitle->config.dest == RENDERSUB ? "Render/Burn-in"
: "Passthrough",
subtitle->config.default_track ? ", Default" : "",
subtitle->config.offset);
}
else
{
hb_log(" * subtitle track %d, %s (track %d, id 0x%x, %s) -> "
"%s%s%s",
subtitle->out_track, subtitle->lang, subtitle->track,
subtitle->id,
subtitle->format == PICTURESUB ? "Picture" : "Text",
subtitle->config.dest == RENDERSUB ? "Render/Burn-in"
: "Passthrough",
subtitle->config.force ? ", Forced Only" : "",
subtitle->config.default_track ? ", Default" : "" );
}
if (subtitle->config.name )
{
hb_log(" + name: %s", subtitle->config.name);
}
}
}
if( !job->indepth_scan )
{
for( i = 0; i < hb_list_count( job->list_audio ); i++ )
{
audio = hb_list_item( job->list_audio, i );
hb_log( " * audio track %d", audio->config.out.track );
if( audio->config.out.name )
hb_log( " + name: %s", audio->config.out.name );
hb_log( " + decoder: %s (track %d, id 0x%x)", audio->config.lang.description, audio->config.in.track + 1, audio->id );
if (audio->config.in.bitrate >= 1000)
hb_log(" + bitrate: %d kbps, samplerate: %d Hz",
audio->config.in.bitrate / 1000,
audio->config.in.samplerate);
else
hb_log(" + samplerate: %d Hz",
audio->config.in.samplerate);
if( audio->config.out.codec & HB_ACODEC_PASS_FLAG )
{
hb_log(" + %s",
hb_audio_encoder_get_name(audio->config.out.codec));
}
else
{
hb_log(" + mixdown: %s",
hb_mixdown_get_name(audio->config.out.mixdown));
if( audio->config.out.normalize_mix_level != 0 )
{
hb_log( " + normalized mixing levels" );
}
if( audio->config.out.gain != 0.0 )
{
hb_log( " + gain: %.fdB", audio->config.out.gain );
}
if (audio->config.out.dynamic_range_compression > 0.0f &&
hb_audio_can_apply_drc(audio->config.in.codec,
audio->config.in.codec_param,
audio->config.out.codec))
{
hb_log( " + dynamic range compression: %f", audio->config.out.dynamic_range_compression );
}
if (hb_audio_dither_is_supported(audio->config.out.codec,
audio->config.in.sample_bit_depth))
{
hb_log(" + dither: %s",
hb_audio_dither_get_description(audio->config.out.dither_method));
}
hb_log(" + encoder: %s",
hb_audio_encoder_get_long_name(audio->config.out.codec));
if (audio->config.out.bitrate > 0)
{
hb_log(" + bitrate: %d kbps, samplerate: %d Hz",
audio->config.out.bitrate, audio->config.out.samplerate);
}
else if (audio->config.out.quality != HB_INVALID_AUDIO_QUALITY)
{
hb_log(" + quality: %.2f, samplerate: %d Hz",
audio->config.out.quality, audio->config.out.samplerate);
}
else if (audio->config.out.samplerate > 0)
{
hb_log(" + samplerate: %d Hz",
audio->config.out.samplerate);
}
if (audio->config.out.compression_level >= 0)
{
hb_log(" + compression level: %.2f",
audio->config.out.compression_level);
}
}
}
}
}
/* Corrects framerates when actual duration and frame count numbers are known. */
void correct_framerate( hb_interjob_t * interjob, hb_job_t * job )
{
if (interjob->total_time <= 0 || interjob->out_frame_count <= 0 ||
job->cfr == 1)
{
// Invalid or uninitialized frame statistics
// Or CFR output
return;
}
// compute actual output vrate from first pass
int64_t num, den;
num = interjob->out_frame_count * 90000LL;
den = interjob->total_time;
hb_limit_rational64(&num, &den, num, den, INT_MAX);
job->vrate.num = num;
job->vrate.den = den;
den = hb_video_framerate_get_close(&job->vrate, 2.);
if (den > 0)
{
int low, high, clock;
hb_video_framerate_get_limits(&low, &high, &clock);
job->vrate.num = clock;
job->vrate.den = den;
}
if (ABS(((double)job->orig_vrate.num / job->orig_vrate.den) -
((double) job->vrate.num / job->vrate.den)) > 0.05)
{
hb_log("work: correcting framerate, %d/%d -> %d/%d",
job->orig_vrate.num, job->orig_vrate.den,
job->vrate.num, job->vrate.den);
}
}
static int bit_depth_is_supported(hb_job_t * job, int bit_depth)
{
for (int i = 0; i < hb_list_count(job->list_filter); i++)
{
hb_filter_object_t *filter = hb_list_item(job->list_filter, i);
switch (filter->id) {
case HB_FILTER_DETELECINE:
case HB_FILTER_COMB_DETECT:
case HB_FILTER_DECOMB:
case HB_FILTER_DENOISE:
case HB_FILTER_NLMEANS:
case HB_FILTER_CHROMA_SMOOTH:
case HB_FILTER_LAPSHARP:
case HB_FILTER_UNSHARP:
case HB_FILTER_GRAYSCALE:
return 0;
}
}
if (hb_video_encoder_get_depth(job->vcodec) < bit_depth)
{
return 0;
}
return 1;
}
static int pix_fmt_is_supported(hb_job_t * job, int pix_fmt)
{
for (int i = 0; i < hb_list_count(job->list_filter); i++)
{
hb_filter_object_t *filter = hb_list_item(job->list_filter, i);
switch (filter->id)
{
case HB_FILTER_DETELECINE:
case HB_FILTER_COMB_DETECT:
case HB_FILTER_DECOMB:
case HB_FILTER_DENOISE:
case HB_FILTER_NLMEANS:
case HB_FILTER_CHROMA_SMOOTH:
case HB_FILTER_LAPSHARP:
case HB_FILTER_UNSHARP:
case HB_FILTER_GRAYSCALE:
if (pix_fmt != AV_PIX_FMT_YUV420P)
{
return 0;
}
}
}
return 1;
}
static int get_best_pix_ftm(hb_job_t * job)
{
int bit_depth = hb_get_bit_depth(job->title->pix_fmt);
#if HB_PROJECT_FEATURE_QSV && (defined( _WIN32 ) || defined( __MINGW32__ ))
if (hb_qsv_encoder_info_get(hb_qsv_get_adapter_index(), job->vcodec))
{
if (hb_qsv_full_path_is_enabled(job))
{
if (job->title->pix_fmt == AV_PIX_FMT_YUV420P10 && job->vcodec == HB_VCODEC_QSV_H265_10BIT)
{
return AV_PIX_FMT_P010LE;
}
else
{
return AV_PIX_FMT_NV12;
}
}
else
{
// system memory usage: QSV encoder only or QSV decoder + SW filters + QSV encoder
return AV_PIX_FMT_YUV420P;
}
}
#endif
if (job->vcodec == HB_VCODEC_FFMPEG_MF_H264 || job->vcodec == HB_VCODEC_FFMPEG_MF_H265)
{
if (pix_fmt_is_supported(job, AV_PIX_FMT_NV12))
{
return AV_PIX_FMT_NV12;
}
}
if (bit_depth >= 12 && bit_depth_is_supported(job, 12))
{
return AV_PIX_FMT_YUV420P12;
}
if (bit_depth >= 10 && bit_depth_is_supported(job, 10))
{
return AV_PIX_FMT_YUV420P10;
}
return AV_PIX_FMT_YUV420P;
}
static void analyze_subtitle_scan( hb_job_t * job )
{
hb_subtitle_t *subtitle;
int subtitle_highest = 0;
int subtitle_lowest = 0;
int subtitle_lowest_id = 0;
int subtitle_forced_id = 0;
int subtitle_forced_hits = 0;
int subtitle_hit = 0;
int i;
// Before closing the title print out our subtitle stats if we need to
// find the highest and lowest.
for (i = 0; i < hb_list_count(job->list_subtitle); i++)
{
subtitle = hb_list_item(job->list_subtitle, i);
hb_log("Subtitle track %d (id 0x%x) '%s': %d hits (%d forced)",
subtitle->track, subtitle->id, subtitle->lang,
subtitle->hits, subtitle->forced_hits);
if (subtitle->hits == 0)
continue;
if (subtitle_highest < subtitle->hits)
{
subtitle_highest = subtitle->hits;
}
if (subtitle_lowest == 0 ||
subtitle_lowest > subtitle->hits)
{
subtitle_lowest = subtitle->hits;
subtitle_lowest_id = subtitle->id;
}
// pick the track with fewest forced hits
if (subtitle->forced_hits > 0 &&
(subtitle_forced_hits == 0 ||
subtitle_forced_hits > subtitle->forced_hits))
{
subtitle_forced_id = subtitle->id;
subtitle_forced_hits = subtitle->forced_hits;
}
}
if (subtitle_forced_id && job->select_subtitle_config.force)
{
// If there is a subtitle stream with forced subtitles and forced-only
// is set, then select it in preference to the lowest.
subtitle_hit = subtitle_forced_id;
hb_log("Found a subtitle candidate with id 0x%x (contains forced subs)",
subtitle_hit );
}
else if (subtitle_lowest > 0 && subtitle_lowest < subtitle_highest * 0.1)
{
// OK we have more than one, and the lowest is lower,
// but how much lower to qualify for turning it on by
// default?
//
// Let's say 10% as a default.
subtitle_hit = subtitle_lowest_id;
hb_log( "Found a subtitle candidate with id 0x%x", subtitle_hit );
}
else
{
hb_log( "No candidate detected during subtitle scan" );
}
for (i = 0; i < hb_list_count( job->list_subtitle ); i++)
{
subtitle = hb_list_item( job->list_subtitle, i );
if (subtitle->id == subtitle_hit)
{
hb_interjob_t *interjob = hb_interjob_get(job->h);
subtitle->config = job->select_subtitle_config;
// Remove from list since we are taking ownership
// of the subtitle.
hb_list_rem(job->list_subtitle, subtitle);
interjob->select_subtitle = subtitle;
break;
}