forked from HandBrake/HandBrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
5516 lines (4911 loc) · 158 KB
/
common.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
/* common.c
Copyright (c) 2003-2017 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 <stdarg.h>
#include <time.h>
#include <ctype.h>
#include <sys/time.h>
#include "hb.h"
#include "x264.h"
#include "lang.h"
#include "common.h"
#include "h264_common.h"
#include "h265_common.h"
#include "encx264.h"
#ifdef USE_QSV
#include "qsv_common.h"
#endif
#ifdef USE_X265
#include "x265.h"
#endif
#ifdef SYS_MINGW
#include <windows.h>
#endif
static int mixdown_get_opus_coupled_stream_count(int mixdown);
/**********************************************************************
* Global variables
*********************************************************************/
static hb_error_handler_t *error_handler = NULL;
/* Generic IDs for encoders, containers, etc. */
enum
{
HB_GID_NONE = -1, // encoders must NEVER use it
HB_GID_VCODEC_H264,
HB_GID_VCODEC_H265,
HB_GID_VCODEC_MPEG2,
HB_GID_VCODEC_MPEG4,
HB_GID_VCODEC_THEORA,
HB_GID_VCODEC_VP8,
HB_GID_VCODEC_VP9,
HB_GID_ACODEC_AAC,
HB_GID_ACODEC_AAC_HE,
HB_GID_ACODEC_AAC_PASS,
HB_GID_ACODEC_AC3,
HB_GID_ACODEC_AC3_PASS,
HB_GID_ACODEC_AUTO_PASS,
HB_GID_ACODEC_DTS_PASS,
HB_GID_ACODEC_DTSHD_PASS,
HB_GID_ACODEC_EAC3,
HB_GID_ACODEC_EAC3_PASS,
HB_GID_ACODEC_FLAC,
HB_GID_ACODEC_FLAC_PASS,
HB_GID_ACODEC_MP3,
HB_GID_ACODEC_MP3_PASS,
HB_GID_ACODEC_TRUEHD_PASS,
HB_GID_ACODEC_VORBIS,
HB_GID_ACODEC_OPUS,
HB_GID_MUX_MKV,
HB_GID_MUX_MP4,
};
#define HB_VIDEO_CLOCK 27000000 // 27MHz clock
#define HB_VIDEO_FPS_MIN 1
#define HB_VIDEO_FPS_MAX 1000
int hb_video_rate_clock = HB_VIDEO_CLOCK;
int hb_video_rate_min = HB_VIDEO_CLOCK / HB_VIDEO_FPS_MAX; // Min clock rate from *max* frame rate
int hb_video_rate_max = HB_VIDEO_CLOCK / HB_VIDEO_FPS_MIN; // Max clock rate from *min* frame rate
typedef struct
{
hb_rate_t item;
hb_rate_t *next;
int enabled;
} hb_rate_internal_t;
hb_rate_t *hb_video_rates_first_item = NULL;
hb_rate_t *hb_video_rates_last_item = NULL;
hb_rate_internal_t hb_video_rates[] =
{
// legacy framerates (disabled)
{ { "23.976 (NTSC Film)", 1126125, }, NULL, 0, },
{ { "25 (PAL Film/Video)", 1080000, }, NULL, 0, },
{ { "29.97 (NTSC Video)", 900900, }, NULL, 0, },
// actual framerates
{ { "5", 5400000, }, NULL, 1, },
{ { "10", 2700000, }, NULL, 1, },
{ { "12", 2250000, }, NULL, 1, },
{ { "15", 1800000, }, NULL, 1, },
{ { "20", 1350000, }, NULL, 1, },
{ { "23.976", 1126125, }, NULL, 1, },
{ { "24", 1125000, }, NULL, 1, },
{ { "25", 1080000, }, NULL, 1, },
{ { "29.97", 900900, }, NULL, 1, },
{ { "30", 900000, }, NULL, 1, },
{ { "48", 562500, }, NULL, 1, },
{ { "50", 540000, }, NULL, 1, },
{ { "59.94", 450450, }, NULL, 1, },
{ { "60", 450000, }, NULL, 1, },
{ { "72", 375000, }, NULL, 1, },
{ { "75", 360000, }, NULL, 1, },
{ { "90", 300000, }, NULL, 1, },
{ { "100", 270000, }, NULL, 1, },
{ { "120", 225000, }, NULL, 1, },
};
int hb_video_rates_count = sizeof(hb_video_rates) / sizeof(hb_video_rates[0]);
hb_rate_t *hb_audio_rates_first_item = NULL;
hb_rate_t *hb_audio_rates_last_item = NULL;
hb_rate_internal_t hb_audio_rates[] =
{
{ { "8", 8000, }, NULL, 1, },
{ { "11.025", 11025, }, NULL, 1, },
{ { "12", 12000, }, NULL, 1, },
{ { "16", 16000, }, NULL, 1, },
{ { "22.05", 22050, }, NULL, 1, },
{ { "24", 24000, }, NULL, 1, },
{ { "32", 32000, }, NULL, 1, },
{ { "44.1", 44100, }, NULL, 1, },
{ { "48", 48000, }, NULL, 1, },
};
int hb_audio_rates_count = sizeof(hb_audio_rates) / sizeof(hb_audio_rates[0]);
hb_rate_t *hb_audio_bitrates_first_item = NULL;
hb_rate_t *hb_audio_bitrates_last_item = NULL;
hb_rate_internal_t hb_audio_bitrates[] =
{
// AC3-compatible bitrates
{ { "32", 32, }, NULL, 1, },
{ { "40", 40, }, NULL, 1, },
{ { "48", 48, }, NULL, 1, },
{ { "56", 56, }, NULL, 1, },
{ { "64", 64, }, NULL, 1, },
{ { "80", 80, }, NULL, 1, },
{ { "96", 96, }, NULL, 1, },
{ { "112", 112, }, NULL, 1, },
{ { "128", 128, }, NULL, 1, },
{ { "160", 160, }, NULL, 1, },
{ { "192", 192, }, NULL, 1, },
{ { "224", 224, }, NULL, 1, },
{ { "256", 256, }, NULL, 1, },
{ { "320", 320, }, NULL, 1, },
{ { "384", 384, }, NULL, 1, },
{ { "448", 448, }, NULL, 1, },
{ { "512", 512, }, NULL, 1, },
{ { "576", 576, }, NULL, 1, },
{ { "640", 640, }, NULL, 1, },
// additional bitrates
{ { "768", 768, }, NULL, 1, },
{ { "960", 960, }, NULL, 1, },
{ { "1152", 1152, }, NULL, 1, },
{ { "1344", 1344, }, NULL, 1, },
{ { "1536", 1536, }, NULL, 1, },
{ { "2304", 2304, }, NULL, 1, },
{ { "3072", 3072, }, NULL, 1, },
{ { "4608", 4608, }, NULL, 1, },
{ { "6144", 6144, }, NULL, 1, },
};
int hb_audio_bitrates_count = sizeof(hb_audio_bitrates) / sizeof(hb_audio_bitrates[0]);
typedef struct
{
hb_dither_t item;
hb_dither_t *next;
int enabled;
} hb_dither_internal_t;
hb_dither_t *hb_audio_dithers_first_item = NULL;
hb_dither_t *hb_audio_dithers_last_item = NULL;
hb_dither_internal_t hb_audio_dithers[] =
{
{ { "default", "auto", AV_RESAMPLE_DITHER_NONE - 1, }, NULL, 1, },
{ { "none", "none", AV_RESAMPLE_DITHER_NONE, }, NULL, 1, },
{ { "rectangular", "rectangular", AV_RESAMPLE_DITHER_RECTANGULAR, }, NULL, 1, },
{ { "triangular", "triangular", AV_RESAMPLE_DITHER_TRIANGULAR, }, NULL, 1, },
{ { "triangular with high pass", "triangular_hp", AV_RESAMPLE_DITHER_TRIANGULAR_HP, }, NULL, 1, },
{ { "triangular with noise shaping", "triangular_ns", AV_RESAMPLE_DITHER_TRIANGULAR_NS, }, NULL, 1, },
};
int hb_audio_dithers_count = sizeof(hb_audio_dithers) / sizeof(hb_audio_dithers[0]);
typedef struct
{
hb_mixdown_t item;
hb_mixdown_t *next;
int enabled;
} hb_mixdown_internal_t;
hb_mixdown_t *hb_audio_mixdowns_first_item = NULL;
hb_mixdown_t *hb_audio_mixdowns_last_item = NULL;
hb_mixdown_internal_t hb_audio_mixdowns[] =
{
// legacy mixdowns, back to HB 0.9.4 whenever possible (disabled)
{ { "AC3 Passthru", "", HB_AMIXDOWN_NONE, }, NULL, 0, },
{ { "DTS Passthru", "", HB_AMIXDOWN_NONE, }, NULL, 0, },
{ { "DTS-HD Passthru", "", HB_AMIXDOWN_NONE, }, NULL, 0, },
{ { "6-channel discrete", "6ch", HB_AMIXDOWN_5POINT1, }, NULL, 0, },
// actual mixdowns
{ { "None", "none", HB_AMIXDOWN_NONE, }, NULL, 1, },
{ { "Mono", "mono", HB_AMIXDOWN_MONO, }, NULL, 1, },
{ { "Mono (Left Only)", "left_only", HB_AMIXDOWN_LEFT, }, NULL, 1, },
{ { "Mono (Right Only)", "right_only", HB_AMIXDOWN_RIGHT, }, NULL, 1, },
{ { "Stereo", "stereo", HB_AMIXDOWN_STEREO, }, NULL, 1, },
{ { "Dolby Surround", "dpl1", HB_AMIXDOWN_DOLBY, }, NULL, 1, },
{ { "Dolby Pro Logic II", "dpl2", HB_AMIXDOWN_DOLBYPLII, }, NULL, 1, },
{ { "5.1 Channels", "5point1", HB_AMIXDOWN_5POINT1, }, NULL, 1, },
{ { "6.1 Channels", "6point1", HB_AMIXDOWN_6POINT1, }, NULL, 1, },
{ { "7.1 Channels", "7point1", HB_AMIXDOWN_7POINT1, }, NULL, 1, },
{ { "7.1 (5F/2R/LFE)", "5_2_lfe", HB_AMIXDOWN_5_2_LFE, }, NULL, 1, },
};
int hb_audio_mixdowns_count = sizeof(hb_audio_mixdowns) / sizeof(hb_audio_mixdowns[0]);
typedef struct
{
hb_encoder_t item;
hb_encoder_t *next;
int enabled;
int gid;
} hb_encoder_internal_t;
hb_encoder_t *hb_video_encoders_first_item = NULL;
hb_encoder_t *hb_video_encoders_last_item = NULL;
hb_encoder_internal_t hb_video_encoders[] =
{
// legacy encoders, back to HB 0.9.4 whenever possible (disabled)
{ { "FFmpeg", "ffmpeg", NULL, HB_VCODEC_FFMPEG_MPEG4, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_MPEG4, },
{ { "MPEG-4 (FFmpeg)", "ffmpeg4", NULL, HB_VCODEC_FFMPEG_MPEG4, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_MPEG4, },
{ { "MPEG-2 (FFmpeg)", "ffmpeg2", NULL, HB_VCODEC_FFMPEG_MPEG2, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_MPEG2, },
{ { "VP3 (Theora)", "libtheora", NULL, HB_VCODEC_THEORA, HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_THEORA, },
// actual encoders
{ { "H.264 (x264)", "x264", "H.264 (libx264)", HB_VCODEC_X264_8BIT, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_H264, },
{ { "H.264 10-bit (x264)", "x264_10bit", "H.264 10-bit (libx264)", HB_VCODEC_X264_10BIT, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_H264, },
{ { "H.264 (Intel QSV)", "qsv_h264", "H.264 (Intel Media SDK)", HB_VCODEC_QSV_H264, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_H264, },
{ { "H.265 (x265)", "x265", "H.265 (libx265)", HB_VCODEC_X265_8BIT, HB_MUX_AV_MP4|HB_MUX_AV_MKV, }, NULL, 1, HB_GID_VCODEC_H265, },
{ { "H.265 10-bit (x265)", "x265_10bit", "H.265 10-bit (libx265)", HB_VCODEC_X265_10BIT, HB_MUX_AV_MP4|HB_MUX_AV_MKV, }, NULL, 1, HB_GID_VCODEC_H265, },
{ { "H.265 12-bit (x265)", "x265_12bit", "H.265 12-bit (libx265)", HB_VCODEC_X265_12BIT, HB_MUX_AV_MP4|HB_MUX_AV_MKV, }, NULL, 1, HB_GID_VCODEC_H265, },
{ { "H.265 16-bit (x265)", "x265_16bit", "H.265 16-bit (libx265)", HB_VCODEC_X265_16BIT, HB_MUX_AV_MP4|HB_MUX_AV_MKV, }, NULL, 1, HB_GID_VCODEC_H265, },
{ { "H.265 (Intel QSV)", "qsv_h265", "H.265 (Intel Media SDK)", HB_VCODEC_QSV_H265, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_H265, },
{ { "MPEG-4", "mpeg4", "MPEG-4 (libavcodec)", HB_VCODEC_FFMPEG_MPEG4, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_MPEG4, },
{ { "MPEG-2", "mpeg2", "MPEG-2 (libavcodec)", HB_VCODEC_FFMPEG_MPEG2, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_MPEG2, },
{ { "VP8", "VP8", "VP8 (libvpx)", HB_VCODEC_FFMPEG_VP8, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_VP8, },
{ { "VP9", "VP9", "VP9 (libvpx)", HB_VCODEC_FFMPEG_VP9, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_VP9, },
{ { "Theora", "theora", "Theora (libtheora)", HB_VCODEC_THEORA, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_THEORA, },
};
int hb_video_encoders_count = sizeof(hb_video_encoders) / sizeof(hb_video_encoders[0]);
static int hb_video_encoder_is_enabled(int encoder)
{
#ifdef USE_QSV
if (encoder & HB_VCODEC_QSV_MASK)
{
return hb_qsv_video_encoder_is_enabled(encoder);
}
#endif
switch (encoder)
{
// the following encoders are always enabled
case HB_VCODEC_THEORA:
case HB_VCODEC_FFMPEG_MPEG4:
case HB_VCODEC_FFMPEG_MPEG2:
case HB_VCODEC_FFMPEG_VP8:
case HB_VCODEC_FFMPEG_VP9:
return 1;
#ifdef USE_X265
case HB_VCODEC_X265_8BIT:
case HB_VCODEC_X265_10BIT:
case HB_VCODEC_X265_12BIT:
case HB_VCODEC_X265_16BIT:
{
const x265_api *api;
api = x265_api_query(hb_video_encoder_get_depth(encoder), X265_BUILD, NULL);
return (api != NULL);
};
#endif
case HB_VCODEC_X264_8BIT:
case HB_VCODEC_X264_10BIT:
{
const x264_api_t *api;
api = hb_x264_api_get(hb_video_encoder_get_depth(encoder));
return (api != NULL);
}
default:
return 0;
}
}
hb_encoder_t *hb_audio_encoders_first_item = NULL;
hb_encoder_t *hb_audio_encoders_last_item = NULL;
hb_encoder_internal_t hb_audio_encoders[] =
{
// legacy encoders, back to HB 0.9.4 whenever possible (disabled)
{ { "", "dts", NULL, HB_ACODEC_DCA_PASS, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_DTS_PASS, },
{ { "AAC (faac)", "faac", NULL, 0, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_AAC, },
{ { "AAC (ffmpeg)", "ffaac", NULL, HB_ACODEC_FFAAC, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_AAC, },
{ { "AC3 (ffmpeg)", "ffac3", NULL, HB_ACODEC_AC3, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_AC3, },
{ { "MP3 (lame)", "lame", NULL, HB_ACODEC_LAME, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_MP3, },
{ { "Vorbis (vorbis)", "libvorbis", NULL, HB_ACODEC_VORBIS, HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_VORBIS, },
{ { "FLAC (ffmpeg)", "ffflac", NULL, HB_ACODEC_FFFLAC, HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_FLAC, },
{ { "FLAC (24-bit)", "ffflac24", NULL, HB_ACODEC_FFFLAC24, HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_FLAC, },
// generic names
{ { "AAC", "aac", NULL, 0, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_AAC, },
{ { "HE-AAC", "haac", NULL, 0, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_AAC_HE, },
// actual encoders
{ { "AAC (CoreAudio)", "ca_aac", "AAC (Apple AudioToolbox)", HB_ACODEC_CA_AAC, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_AAC, },
{ { "HE-AAC (CoreAudio)", "ca_haac", "HE-AAC (Apple AudioToolbox)", HB_ACODEC_CA_HAAC, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_AAC_HE, },
{ { "AAC (FDK)", "fdk_aac", "AAC (libfdk_aac)", HB_ACODEC_FDK_AAC, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_AAC, },
{ { "HE-AAC (FDK)", "fdk_haac", "HE-AAC (libfdk_aac)", HB_ACODEC_FDK_HAAC, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_AAC_HE, },
{ { "AAC (avcodec)", "av_aac", "AAC (libavcodec)", HB_ACODEC_FFAAC, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_AAC, },
{ { "AAC Passthru", "copy:aac", "AAC Passthru", HB_ACODEC_AAC_PASS, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_AAC_PASS, },
{ { "AC3", "ac3", "AC3 (libavcodec)", HB_ACODEC_AC3, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_AC3, },
{ { "AC3 Passthru", "copy:ac3", "AC3 Passthru", HB_ACODEC_AC3_PASS, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_AC3_PASS, },
{ { "E-AC3", "eac3", "E-AC3 (libavcodec)", HB_ACODEC_FFEAC3, HB_MUX_AV_MKV, }, NULL, 1, HB_GID_ACODEC_EAC3, },
{ { "E-AC3 Passthru", "copy:eac3", "E-AC3 Passthru", HB_ACODEC_EAC3_PASS, HB_MUX_AV_MKV, }, NULL, 1, HB_GID_ACODEC_EAC3_PASS, },
{ { "TrueHD Passthru", "copy:truehd","TrueHD Passthru", HB_ACODEC_TRUEHD_PASS, HB_MUX_AV_MKV, }, NULL, 1, HB_GID_ACODEC_TRUEHD_PASS,},
{ { "DTS Passthru", "copy:dts", "DTS Passthru", HB_ACODEC_DCA_PASS, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_DTS_PASS, },
{ { "DTS-HD Passthru", "copy:dtshd", "DTS-HD Passthru", HB_ACODEC_DCA_HD_PASS, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_DTSHD_PASS, },
{ { "MP3", "mp3", "MP3 (libmp3lame)", HB_ACODEC_LAME, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_MP3, },
{ { "MP3 Passthru", "copy:mp3", "MP3 Passthru", HB_ACODEC_MP3_PASS, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_MP3_PASS, },
{ { "Vorbis", "vorbis", "Vorbis (libvorbis)", HB_ACODEC_VORBIS, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_VORBIS, },
{ { "FLAC 16-bit", "flac16", "FLAC 16-bit (libavcodec)", HB_ACODEC_FFFLAC, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_FLAC, },
{ { "FLAC 24-bit", "flac24", "FLAC 24-bit (libavcodec)", HB_ACODEC_FFFLAC24, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_FLAC, },
{ { "FLAC Passthru", "copy:flac", "FLAC Passthru", HB_ACODEC_FLAC_PASS, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_FLAC_PASS, },
{ { "Opus", "opus", "Opus (libopus)", HB_ACODEC_OPUS, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_OPUS, },
{ { "Auto Passthru", "copy", "Auto Passthru", HB_ACODEC_AUTO_PASS, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_AUTO_PASS, },
};
int hb_audio_encoders_count = sizeof(hb_audio_encoders) / sizeof(hb_audio_encoders[0]);
static int hb_audio_encoder_is_enabled(int encoder)
{
if (encoder & HB_ACODEC_PASS_FLAG)
{
// Passthru encoders are always enabled
return 1;
}
switch (encoder)
{
#ifdef __APPLE__
case HB_ACODEC_CA_AAC:
case HB_ACODEC_CA_HAAC:
return 1;
#endif
#ifdef USE_LIBAV_AAC
case HB_ACODEC_FFAAC:
return avcodec_find_encoder_by_name("aac") != NULL;
#endif
case HB_ACODEC_FDK_AAC:
case HB_ACODEC_FDK_HAAC:
return avcodec_find_encoder_by_name("libfdk_aac") != NULL;
case HB_ACODEC_AC3:
return avcodec_find_encoder(AV_CODEC_ID_AC3) != NULL;
case HB_ACODEC_FFEAC3:
return avcodec_find_encoder(AV_CODEC_ID_EAC3) != NULL;
case HB_ACODEC_FFFLAC:
case HB_ACODEC_FFFLAC24:
return avcodec_find_encoder(AV_CODEC_ID_FLAC) != NULL;
case HB_ACODEC_OPUS:
return avcodec_find_encoder(AV_CODEC_ID_OPUS) != NULL;
// the following encoders are always enabled
case HB_ACODEC_LAME:
case HB_ACODEC_VORBIS:
return 1;
default:
return 0;
}
}
typedef struct
{
hb_container_t item;
hb_container_t *next;
int enabled;
int gid;
} hb_container_internal_t;
hb_container_t *hb_containers_first_item = NULL;
hb_container_t *hb_containers_last_item = NULL;
hb_container_internal_t hb_containers[] =
{
// legacy muxers, back to HB 0.9.4 whenever possible (disabled)
{ { "M4V file", "m4v", NULL, "m4v", 0, }, NULL, 0, HB_GID_MUX_MP4, },
{ { "MP4 file", "mp4", NULL, "mp4", 0, }, NULL, 0, HB_GID_MUX_MP4, },
{ { "MKV file", "mkv", NULL, "mkv", 0, }, NULL, 0, HB_GID_MUX_MKV, },
// actual muxers
{ { "MPEG-4 (avformat)", "av_mp4", "MPEG-4 (libavformat)", "mp4", HB_MUX_AV_MP4, }, NULL, 1, HB_GID_MUX_MP4, },
{ { "MPEG-4 (mp4v2)", "mp4v2", "MPEG-4 (libmp4v2)", "mp4", HB_MUX_MP4V2, }, NULL, 1, HB_GID_MUX_MP4, },
{ { "Matroska (avformat)", "av_mkv", "Matroska (libavformat)", "mkv", HB_MUX_AV_MKV, }, NULL, 1, HB_GID_MUX_MKV, },
{ { "Matroska (libmkv)", "libmkv", "Matroska (libmkv)", "mkv", HB_MUX_LIBMKV, }, NULL, 1, HB_GID_MUX_MKV, },
};
int hb_containers_count = sizeof(hb_containers) / sizeof(hb_containers[0]);
static int hb_container_is_enabled(int format)
{
switch (format)
{
case HB_MUX_AV_MP4:
case HB_MUX_AV_MKV:
return 1;
default:
return 0;
}
}
void hb_common_global_init()
{
static int common_init_done = 0;
if (common_init_done)
return;
int i, j;
// video framerates
for (i = 0; i < hb_video_rates_count; i++)
{
if (hb_video_rates[i].enabled)
{
if (hb_video_rates_first_item == NULL)
{
hb_video_rates_first_item = &hb_video_rates[i].item;
}
else
{
((hb_rate_internal_t*)hb_video_rates_last_item)->next =
&hb_video_rates[i].item;
}
hb_video_rates_last_item = &hb_video_rates[i].item;
}
}
// fallbacks are static for now (no setup required)
// audio samplerates
for (i = 0; i < hb_audio_rates_count; i++)
{
if (hb_audio_rates[i].enabled)
{
if (hb_audio_rates_first_item == NULL)
{
hb_audio_rates_first_item = &hb_audio_rates[i].item;
}
else
{
((hb_rate_internal_t*)hb_audio_rates_last_item)->next =
&hb_audio_rates[i].item;
}
hb_audio_rates_last_item = &hb_audio_rates[i].item;
}
}
// fallbacks are static for now (no setup required)
// audio bitrates
for (i = 0; i < hb_audio_bitrates_count; i++)
{
if (hb_audio_bitrates[i].enabled)
{
if (hb_audio_bitrates_first_item == NULL)
{
hb_audio_bitrates_first_item = &hb_audio_bitrates[i].item;
}
else
{
((hb_rate_internal_t*)hb_audio_bitrates_last_item)->next =
&hb_audio_bitrates[i].item;
}
hb_audio_bitrates_last_item = &hb_audio_bitrates[i].item;
}
}
// fallbacks are static for now (no setup required)
// audio dithers
for (i = 0; i < hb_audio_dithers_count; i++)
{
if (hb_audio_dithers[i].enabled)
{
if (hb_audio_dithers_first_item == NULL)
{
hb_audio_dithers_first_item = &hb_audio_dithers[i].item;
}
else
{
((hb_dither_internal_t*)hb_audio_dithers_last_item)->next =
&hb_audio_dithers[i].item;
}
hb_audio_dithers_last_item = &hb_audio_dithers[i].item;
}
}
// fallbacks are static for now (no setup required)
// audio mixdowns
for (i = 0; i < hb_audio_mixdowns_count; i++)
{
if (hb_audio_mixdowns[i].enabled)
{
if (hb_audio_mixdowns_first_item == NULL)
{
hb_audio_mixdowns_first_item = &hb_audio_mixdowns[i].item;
}
else
{
((hb_mixdown_internal_t*)hb_audio_mixdowns_last_item)->next =
&hb_audio_mixdowns[i].item;
}
hb_audio_mixdowns_last_item = &hb_audio_mixdowns[i].item;
}
}
// fallbacks are static for now (no setup required)
// video encoders
for (i = 0; i < hb_video_encoders_count; i++)
{
if (hb_video_encoders[i].enabled)
{
// we still need to check
hb_video_encoders[i].enabled =
hb_video_encoder_is_enabled(hb_video_encoders[i].item.codec);
}
if (hb_video_encoders[i].enabled)
{
if (hb_video_encoders_first_item == NULL)
{
hb_video_encoders_first_item = &hb_video_encoders[i].item;
}
else
{
((hb_encoder_internal_t*)hb_video_encoders_last_item)->next =
&hb_video_encoders[i].item;
}
hb_video_encoders_last_item = &hb_video_encoders[i].item;
}
}
// setup fallbacks
for (i = 0; i < hb_video_encoders_count; i++)
{
if (!hb_video_encoders[i].enabled)
{
if ((hb_video_encoders[i].item.codec & HB_VCODEC_MASK) &&
(hb_video_encoder_is_enabled(hb_video_encoders[i].item.codec)))
{
// we have a specific fallback and it's enabled
continue;
}
for (j = 0; j < hb_video_encoders_count; j++)
{
if (hb_video_encoders[j].enabled &&
hb_video_encoders[j].gid == hb_video_encoders[i].gid)
{
hb_video_encoders[i].item.codec = hb_video_encoders[j].item.codec;
break;
}
}
}
}
// audio encoders
for (i = 0; i < hb_audio_encoders_count; i++)
{
if (hb_audio_encoders[i].enabled)
{
// we still need to check
hb_audio_encoders[i].enabled =
hb_audio_encoder_is_enabled(hb_audio_encoders[i].item.codec);
}
if (hb_audio_encoders[i].enabled)
{
if (hb_audio_encoders_first_item == NULL)
{
hb_audio_encoders_first_item = &hb_audio_encoders[i].item;
}
else
{
((hb_encoder_internal_t*)hb_audio_encoders_last_item)->next =
&hb_audio_encoders[i].item;
}
hb_audio_encoders_last_item = &hb_audio_encoders[i].item;
}
}
// setup fallbacks
for (i = 0; i < hb_audio_encoders_count; i++)
{
if (!hb_audio_encoders[i].enabled)
{
if ((hb_audio_encoders[i].item.codec & HB_ACODEC_MASK) &&
(hb_audio_encoder_is_enabled(hb_audio_encoders[i].item.codec)))
{
// we have a specific fallback and it's enabled
continue;
}
for (j = 0; j < hb_audio_encoders_count; j++)
{
if (hb_audio_encoders[j].enabled &&
hb_audio_encoders[j].gid == hb_audio_encoders[i].gid)
{
hb_audio_encoders[i].item.codec = hb_audio_encoders[j].item.codec;
break;
}
}
if (hb_audio_encoders[i].gid == HB_GID_ACODEC_AAC_HE)
{
// try to find an AAC fallback if no HE-AAC encoder is available
for (j = 0; j < hb_audio_encoders_count; j++)
{
if (hb_audio_encoders[j].enabled &&
hb_audio_encoders[j].gid == HB_GID_ACODEC_AAC)
{
hb_audio_encoders[i].item.codec = hb_audio_encoders[j].item.codec;
break;
}
}
}
}
}
// video containers
for (i = 0; i < hb_containers_count; i++)
{
if (hb_containers[i].enabled)
{
// we still need to check
hb_containers[i].enabled =
hb_container_is_enabled(hb_containers[i].item.format);
}
if (hb_containers[i].enabled)
{
if (hb_containers_first_item == NULL)
{
hb_containers_first_item = &hb_containers[i].item;
}
else
{
((hb_container_internal_t*)hb_containers_last_item)->next =
&hb_containers[i].item;
}
hb_containers_last_item = &hb_containers[i].item;
}
}
// setup fallbacks
for (i = 0; i < hb_containers_count; i++)
{
if (!hb_containers[i].enabled)
{
if ((hb_containers[i].item.format & HB_MUX_MASK) &&
(hb_container_is_enabled(hb_containers[i].item.format)))
{
// we have a specific fallback and it's enabled
continue;
}
for (j = 0; j < hb_containers_count; j++)
{
if (hb_containers[j].enabled &&
hb_containers[j].gid == hb_containers[i].gid)
{
hb_containers[i].item.format = hb_containers[j].item.format;
break;
}
}
}
}
// we're done, yay!
common_init_done = 1;
}
int hb_video_framerate_get_from_name(const char *name)
{
if (name == NULL || *name == '\0')
goto fail;
int i;
for (i = 0; i < hb_video_rates_count; i++)
{
if (!strcasecmp(hb_video_rates[i].item.name, name))
{
return hb_video_rates[i].item.rate;
}
}
fail:
return -1;
}
const char* hb_video_framerate_get_name(int framerate)
{
if (framerate > hb_video_rates_first_item->rate ||
framerate < hb_video_rates_last_item ->rate)
goto fail;
const hb_rate_t *video_framerate = NULL;
while ((video_framerate = hb_video_framerate_get_next(video_framerate)) != NULL)
{
if (video_framerate->rate == framerate)
{
return video_framerate->name;
}
}
fail:
return NULL;
}
const char* hb_video_framerate_sanitize_name(const char *name)
{
return hb_video_framerate_get_name(hb_video_framerate_get_from_name(name));
}
void hb_video_framerate_get_limits(int *low, int *high, int *clock)
{
*low = hb_video_rate_min;
*high = hb_video_rate_max;
*clock = hb_video_rate_clock;
}
const hb_rate_t* hb_video_framerate_get_next(const hb_rate_t *last)
{
if (last == NULL)
{
return hb_video_rates_first_item;
}
return ((hb_rate_internal_t*)last)->next;
}
int hb_video_framerate_get_close(hb_rational_t *framerate, double thresh)
{
double fps_in;
const hb_rate_t * rate = NULL;
int result = -1;
double closest = thresh;
fps_in = (double)framerate->num / framerate->den;
while ((rate = hb_video_framerate_get_next(rate)) != NULL)
{
double fps = (double)hb_video_rate_clock / rate->rate;
if (ABS(fps - fps_in) < closest)
{
result = rate->rate;
closest = ABS(fps - fps_in);
}
}
return result;
}
int hb_audio_samplerate_is_supported(int samplerate, uint32_t codec)
{
switch (codec)
{
case HB_ACODEC_AC3:
case HB_ACODEC_FFEAC3:
case HB_ACODEC_CA_HAAC:
// ca_haac can't do samplerates < 32 kHz
// libav's E-AC-3 encoder can't do samplerates < 32 kHz
// AC-3 < 32 kHz suffers from poor hardware compatibility
if (samplerate < 32000)
return 0;
else
return 1;
case HB_ACODEC_FDK_HAAC:
// fdk_haac can't do samplerates < 16 kHz
if (samplerate < 16000)
return 0;
else
return 1;
case HB_ACODEC_OPUS:
switch (samplerate)
{
// Opus only supports samplerates 8kHz, 12kHz, 16kHz,
// 24kHz, 48kHz
case 8000:
case 12000:
case 16000:
case 24000:
case 48000:
return 1;
default:
return 0;
}
default:
return 1;
}
}
int hb_audio_samplerate_get_sr_shift(int samplerate)
{
/* sr_shift: 0 -> 48000, 44100, 32000 Hz
* 1 -> 24000, 22050, 16000 Hz
* 2 -> 12000, 11025, 8000 Hz
*
* also, since samplerates are sanitized downwards:
*
* (samplerate < 32000) implies (samplerate <= 24000)
*/
return ((samplerate < 16000) ? 2 : (samplerate < 32000) ? 1 : 0);
}
int hb_audio_samplerate_get_from_name(const char *name)
{
if (name == NULL || *name == '\0')
goto fail;
int i;
for (i = 0; i < hb_audio_rates_count; i++)
{
if (!strcasecmp(hb_audio_rates[i].item.name, name))
{
return hb_audio_rates[i].item.rate;
}
}
// maybe the samplerate was specified in Hz
i = atoi(name);
if (i >= hb_audio_rates_first_item->rate &&
i <= hb_audio_rates_last_item ->rate)
{
return hb_audio_samplerate_find_closest(i, HB_ACODEC_INVALID);
}
fail:
return -1;
}
const char* hb_audio_samplerate_get_name(int samplerate)
{
if (samplerate < hb_audio_rates_first_item->rate ||
samplerate > hb_audio_rates_last_item ->rate)
goto fail;
const hb_rate_t *audio_samplerate = NULL;
while ((audio_samplerate = hb_audio_samplerate_get_next(audio_samplerate)) != NULL)
{
if (audio_samplerate->rate == samplerate)
{
return audio_samplerate->name;
}
}
fail:
return NULL;
}
const hb_rate_t* hb_audio_samplerate_get_next(const hb_rate_t *last)
{
if (last == NULL)
{
return hb_audio_rates_first_item;
}
return ((hb_rate_internal_t*)last)->next;
}
const hb_rate_t* hb_audio_samplerate_get_next_for_codec(const hb_rate_t *last,
uint32_t codec)
{
while ((last = hb_audio_samplerate_get_next(last)) != NULL)
if (hb_audio_samplerate_is_supported(last->rate, codec))
return last;
// None found or end of list
return NULL;
}
int hb_audio_samplerate_find_closest(int samplerate, uint32_t codec)
{
const hb_rate_t * rate, * prev, * next;
rate = prev = next = hb_audio_samplerate_get_next_for_codec(NULL, codec);
while (rate != NULL && next->rate < samplerate)
{
rate = hb_audio_samplerate_get_next_for_codec(rate, codec);
if (rate != NULL)
{
prev = next;
next = rate;
}
}
int delta_prev = samplerate - prev->rate;
int delta_next = next->rate - samplerate;
if (delta_prev <= delta_next)
{
return prev->rate;
}
else
{
return next->rate;
}
}
// Given an input bitrate, find closest match in the set of allowed bitrates
static int hb_audio_bitrate_find_closest(int bitrate)
{
// Check if bitrate mode was disabled
if (bitrate <= 0)
return bitrate;
int closest_bitrate = hb_audio_bitrates_first_item->rate;
const hb_rate_t *audio_bitrate = NULL;
while ((audio_bitrate = hb_audio_bitrate_get_next(audio_bitrate)) != NULL)
{
if (bitrate == audio_bitrate->rate)
{
// valid bitrate
closest_bitrate = audio_bitrate->rate;
break;
}
if (bitrate > audio_bitrate->rate)
{
// bitrates are sanitized downwards
closest_bitrate = audio_bitrate->rate;
}
}
return closest_bitrate;
}
// Given an input bitrate, sanitize it.
// Check low and high limits and make sure it is in the set of allowed bitrates.
int hb_audio_bitrate_get_best(uint32_t codec, int bitrate, int samplerate,
int mixdown)
{
int low, high;
hb_audio_bitrate_get_limits(codec, samplerate, mixdown, &low, &high);
if (bitrate > high)
bitrate = high;
if (bitrate < low)
bitrate = low;
return hb_audio_bitrate_find_closest(bitrate);
}
// Get the default bitrate for a given codec/samplerate/mixdown triplet.
int hb_audio_bitrate_get_default(uint32_t codec, int samplerate, int mixdown)
{
if ((codec & HB_ACODEC_PASS_FLAG) || !(codec & HB_ACODEC_MASK))
goto fail;
int bitrate, nchannels, nlfe, sr_shift;
/* full-bandwidth channels, sr_shift */
nlfe = hb_mixdown_get_low_freq_channel_count(mixdown);
nchannels = hb_mixdown_get_discrete_channel_count(mixdown) - nlfe;
sr_shift = hb_audio_samplerate_get_sr_shift(samplerate);
switch (codec)
{
case HB_ACODEC_FFFLAC:
case HB_ACODEC_FFFLAC24:
goto fail;
// 96, 224, 640 Kbps
case HB_ACODEC_AC3:
bitrate = (nchannels * 128) - (32 * (nchannels < 5));
break;
// Our E-AC-3 encoder is pretty similar to our AC-3 encoder but it does
// allow for higher bitrates, should some users want a bit more quality
// at the expense of compression efficiency - still, let's remain
// compatible with passthru over S/PDIF by default: 384, 768, 1536 Kbps
case HB_ACODEC_FFEAC3:
bitrate = (nchannels * 384) - (128 * (nchannels - 2) * (nchannels > 2));
break;
case HB_ACODEC_CA_HAAC:
case HB_ACODEC_FDK_HAAC:
bitrate = nchannels * 32;
break;
case HB_ACODEC_OPUS:
{
int coupled = mixdown_get_opus_coupled_stream_count(mixdown);
int uncoupled = nchannels + nlfe - 2 * coupled;
bitrate = coupled * 96 + uncoupled * 64;
} break;
default:
bitrate = nchannels * 80;
break;
}
// sample_rate adjustment
bitrate >>= sr_shift;
return hb_audio_bitrate_get_best(codec, bitrate, samplerate, mixdown);
fail:
return -1;
}
/* Get the bitrate low and high limits for a codec/samplerate/mixdown triplet.
*
* Encoder 1.0 channel 2.0 channels 5.1 channels 6.1 channels 7.1 channels
* --------------------------------------------------------------------------------------
*
* ffaac
* -----
* supported samplerates: 8 - 48 kHz
* libavcodec/aacenc.c defines a maximum bitrate: