WinGui: Fix another instance of the Caliburn vs Json.net sillyness where objects...
[HandBrake.git] / libhb / common.c
blobc64fed5911f97becaaea6a7b9d47d469a1b7e198
1 /* common.c
3 Copyright (c) 2003-2015 HandBrake Team
4 This file is part of the HandBrake source code
5 Homepage: <http://handbrake.fr/>.
6 It may be used under the terms of the GNU General Public License v2.
7 For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
8 */
10 #include <stdarg.h>
11 #include <time.h>
12 #include <ctype.h>
13 #include <sys/time.h>
15 #include "hb.h"
16 #include "x264.h"
17 #include "lang.h"
18 #include "common.h"
19 #include "h264_common.h"
20 #include "h265_common.h"
21 #ifdef USE_QSV
22 #include "qsv_common.h"
23 #endif
25 #ifdef USE_X265
26 #include "x265.h"
27 #endif
29 #ifdef SYS_MINGW
30 #include <windows.h>
31 #endif
33 /**********************************************************************
34 * Global variables
35 *********************************************************************/
36 static hb_error_handler_t *error_handler = NULL;
38 /* Generic IDs for encoders, containers, etc. */
39 enum
41 HB_GID_NONE = -1, // encoders must NEVER use it
42 HB_GID_VCODEC_H264,
43 HB_GID_VCODEC_H265,
44 HB_GID_VCODEC_MPEG2,
45 HB_GID_VCODEC_MPEG4,
46 HB_GID_VCODEC_THEORA,
47 HB_GID_VCODEC_VP8,
48 HB_GID_ACODEC_AAC,
49 HB_GID_ACODEC_AAC_HE,
50 HB_GID_ACODEC_AAC_PASS,
51 HB_GID_ACODEC_AC3,
52 HB_GID_ACODEC_AC3_PASS,
53 HB_GID_ACODEC_AUTO_PASS,
54 HB_GID_ACODEC_DTS_PASS,
55 HB_GID_ACODEC_DTSHD_PASS,
56 HB_GID_ACODEC_EAC3,
57 HB_GID_ACODEC_EAC3_PASS,
58 HB_GID_ACODEC_FLAC,
59 HB_GID_ACODEC_FLAC_PASS,
60 HB_GID_ACODEC_MP3,
61 HB_GID_ACODEC_MP3_PASS,
62 HB_GID_ACODEC_TRUEHD_PASS,
63 HB_GID_ACODEC_VORBIS,
64 HB_GID_MUX_MKV,
65 HB_GID_MUX_MP4,
68 typedef struct
70 hb_rate_t item;
71 hb_rate_t *next;
72 int enabled;
73 } hb_rate_internal_t;
74 hb_rate_t *hb_video_rates_first_item = NULL;
75 hb_rate_t *hb_video_rates_last_item = NULL;
76 hb_rate_internal_t hb_video_rates[] =
78 // legacy framerates (disabled)
79 { { "23.976 (NTSC Film)", 1126125, }, NULL, 0, },
80 { { "25 (PAL Film/Video)", 1080000, }, NULL, 0, },
81 { { "29.97 (NTSC Video)", 900900, }, NULL, 0, },
82 // actual framerates
83 { { "5", 5400000, }, NULL, 1, },
84 { { "10", 2700000, }, NULL, 1, },
85 { { "12", 2250000, }, NULL, 1, },
86 { { "15", 1800000, }, NULL, 1, },
87 { { "23.976", 1126125, }, NULL, 1, },
88 { { "24", 1125000, }, NULL, 1, },
89 { { "25", 1080000, }, NULL, 1, },
90 { { "29.97", 900900, }, NULL, 1, },
91 { { "30", 900000, }, NULL, 1, },
92 { { "50", 540000, }, NULL, 1, },
93 { { "59.94", 450450, }, NULL, 1, },
94 { { "60", 450000, }, NULL, 1, },
96 int hb_video_rates_count = sizeof(hb_video_rates) / sizeof(hb_video_rates[0]);
98 hb_rate_t *hb_audio_rates_first_item = NULL;
99 hb_rate_t *hb_audio_rates_last_item = NULL;
100 hb_rate_internal_t hb_audio_rates[] =
102 { { "8", 8000, }, NULL, 1, },
103 { { "11.025", 11025, }, NULL, 1, },
104 { { "12", 12000, }, NULL, 1, },
105 { { "16", 16000, }, NULL, 1, },
106 { { "22.05", 22050, }, NULL, 1, },
107 { { "24", 24000, }, NULL, 1, },
108 { { "32", 32000, }, NULL, 1, },
109 { { "44.1", 44100, }, NULL, 1, },
110 { { "48", 48000, }, NULL, 1, },
112 int hb_audio_rates_count = sizeof(hb_audio_rates) / sizeof(hb_audio_rates[0]);
114 hb_rate_t *hb_audio_bitrates_first_item = NULL;
115 hb_rate_t *hb_audio_bitrates_last_item = NULL;
116 hb_rate_internal_t hb_audio_bitrates[] =
118 // AC3-compatible bitrates
119 { { "32", 32, }, NULL, 1, },
120 { { "40", 40, }, NULL, 1, },
121 { { "48", 48, }, NULL, 1, },
122 { { "56", 56, }, NULL, 1, },
123 { { "64", 64, }, NULL, 1, },
124 { { "80", 80, }, NULL, 1, },
125 { { "96", 96, }, NULL, 1, },
126 { { "112", 112, }, NULL, 1, },
127 { { "128", 128, }, NULL, 1, },
128 { { "160", 160, }, NULL, 1, },
129 { { "192", 192, }, NULL, 1, },
130 { { "224", 224, }, NULL, 1, },
131 { { "256", 256, }, NULL, 1, },
132 { { "320", 320, }, NULL, 1, },
133 { { "384", 384, }, NULL, 1, },
134 { { "448", 448, }, NULL, 1, },
135 { { "512", 512, }, NULL, 1, },
136 { { "576", 576, }, NULL, 1, },
137 { { "640", 640, }, NULL, 1, },
138 // additional bitrates
139 { { "768", 768, }, NULL, 1, },
140 { { "960", 960, }, NULL, 1, },
141 { { "1152", 1152, }, NULL, 1, },
142 { { "1344", 1344, }, NULL, 1, },
143 { { "1536", 1536, }, NULL, 1, },
144 { { "2304", 2304, }, NULL, 1, },
145 { { "3072", 3072, }, NULL, 1, },
146 { { "4608", 4608, }, NULL, 1, },
147 { { "6144", 6144, }, NULL, 1, },
149 int hb_audio_bitrates_count = sizeof(hb_audio_bitrates) / sizeof(hb_audio_bitrates[0]);
151 typedef struct
153 hb_dither_t item;
154 hb_dither_t *next;
155 int enabled;
156 } hb_dither_internal_t;
157 hb_dither_t *hb_audio_dithers_first_item = NULL;
158 hb_dither_t *hb_audio_dithers_last_item = NULL;
159 hb_dither_internal_t hb_audio_dithers[] =
161 { { "default", "auto", AV_RESAMPLE_DITHER_NONE - 1, }, NULL, 1, },
162 { { "none", "none", AV_RESAMPLE_DITHER_NONE, }, NULL, 1, },
163 { { "rectangular", "rectangular", AV_RESAMPLE_DITHER_RECTANGULAR, }, NULL, 1, },
164 { { "triangular", "triangular", AV_RESAMPLE_DITHER_TRIANGULAR, }, NULL, 1, },
165 { { "triangular with high pass", "triangular_hp", AV_RESAMPLE_DITHER_TRIANGULAR_HP, }, NULL, 1, },
166 { { "triangular with noise shaping", "triangular_ns", AV_RESAMPLE_DITHER_TRIANGULAR_NS, }, NULL, 1, },
168 int hb_audio_dithers_count = sizeof(hb_audio_dithers) / sizeof(hb_audio_dithers[0]);
170 typedef struct
172 hb_mixdown_t item;
173 hb_mixdown_t *next;
174 int enabled;
175 } hb_mixdown_internal_t;
176 hb_mixdown_t *hb_audio_mixdowns_first_item = NULL;
177 hb_mixdown_t *hb_audio_mixdowns_last_item = NULL;
178 hb_mixdown_internal_t hb_audio_mixdowns[] =
180 // legacy mixdowns, back to HB 0.9.4 whenever possible (disabled)
181 { { "AC3 Passthru", "", HB_AMIXDOWN_NONE, }, NULL, 0, },
182 { { "DTS Passthru", "", HB_AMIXDOWN_NONE, }, NULL, 0, },
183 { { "DTS-HD Passthru", "", HB_AMIXDOWN_NONE, }, NULL, 0, },
184 { { "6-channel discrete", "6ch", HB_AMIXDOWN_5POINT1, }, NULL, 0, },
185 // actual mixdowns
186 { { "None", "none", HB_AMIXDOWN_NONE, }, NULL, 1, },
187 { { "Mono", "mono", HB_AMIXDOWN_MONO, }, NULL, 1, },
188 { { "Mono (Left Only)", "left_only", HB_AMIXDOWN_LEFT, }, NULL, 1, },
189 { { "Mono (Right Only)", "right_only", HB_AMIXDOWN_RIGHT, }, NULL, 1, },
190 { { "Stereo", "stereo", HB_AMIXDOWN_STEREO, }, NULL, 1, },
191 { { "Dolby Surround", "dpl1", HB_AMIXDOWN_DOLBY, }, NULL, 1, },
192 { { "Dolby Pro Logic II", "dpl2", HB_AMIXDOWN_DOLBYPLII, }, NULL, 1, },
193 { { "5.1 Channels", "5point1", HB_AMIXDOWN_5POINT1, }, NULL, 1, },
194 { { "6.1 Channels", "6point1", HB_AMIXDOWN_6POINT1, }, NULL, 1, },
195 { { "7.1 Channels", "7point1", HB_AMIXDOWN_7POINT1, }, NULL, 1, },
196 { { "7.1 (5F/2R/LFE)", "5_2_lfe", HB_AMIXDOWN_5_2_LFE, }, NULL, 1, },
198 int hb_audio_mixdowns_count = sizeof(hb_audio_mixdowns) / sizeof(hb_audio_mixdowns[0]);
200 typedef struct
202 hb_encoder_t item;
203 hb_encoder_t *next;
204 int enabled;
205 int gid;
206 } hb_encoder_internal_t;
207 hb_encoder_t *hb_video_encoders_first_item = NULL;
208 hb_encoder_t *hb_video_encoders_last_item = NULL;
209 hb_encoder_internal_t hb_video_encoders[] =
211 // legacy encoders, back to HB 0.9.4 whenever possible (disabled)
212 { { "FFmpeg", "ffmpeg", NULL, HB_VCODEC_FFMPEG_MPEG4, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_MPEG4, },
213 { { "MPEG-4 (FFmpeg)", "ffmpeg4", NULL, HB_VCODEC_FFMPEG_MPEG4, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_MPEG4, },
214 { { "MPEG-2 (FFmpeg)", "ffmpeg2", NULL, HB_VCODEC_FFMPEG_MPEG2, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_MPEG2, },
215 { { "VP3 (Theora)", "libtheora", NULL, HB_VCODEC_THEORA, HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_THEORA, },
216 // actual encoders
217 { { "H.264 (x264)", "x264", "H.264 (libx264)", HB_VCODEC_X264, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_H264, },
218 { { "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, },
219 { { "H.265 (x265)", "x265", "H.265 (libx265)", HB_VCODEC_X265, HB_MUX_AV_MP4|HB_MUX_AV_MKV, }, NULL, 1, HB_GID_VCODEC_H265, },
220 { { "MPEG-4", "mpeg4", "MPEG-4 (libavcodec)", HB_VCODEC_FFMPEG_MPEG4, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_MPEG4, },
221 { { "MPEG-2", "mpeg2", "MPEG-2 (libavcodec)", HB_VCODEC_FFMPEG_MPEG2, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_MPEG2, },
222 { { "VP8", "VP8", "VP8 (libvpx)", HB_VCODEC_FFMPEG_VP8, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_VP8, },
223 { { "Theora", "theora", "Theora (libtheora)", HB_VCODEC_THEORA, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_THEORA, },
225 int hb_video_encoders_count = sizeof(hb_video_encoders) / sizeof(hb_video_encoders[0]);
226 static int hb_video_encoder_is_enabled(int encoder)
228 #ifdef USE_QSV
229 if (encoder & HB_VCODEC_QSV_MASK)
231 return hb_qsv_video_encoder_is_enabled(encoder);
233 #endif
234 switch (encoder)
236 // the following encoders are always enabled
237 case HB_VCODEC_X264:
238 case HB_VCODEC_THEORA:
239 case HB_VCODEC_FFMPEG_MPEG4:
240 case HB_VCODEC_FFMPEG_MPEG2:
241 case HB_VCODEC_FFMPEG_VP8:
242 #ifdef USE_X265
243 case HB_VCODEC_X265:
244 #endif
245 return 1;
247 default:
248 return 0;
252 hb_encoder_t *hb_audio_encoders_first_item = NULL;
253 hb_encoder_t *hb_audio_encoders_last_item = NULL;
254 hb_encoder_internal_t hb_audio_encoders[] =
256 // legacy encoders, back to HB 0.9.4 whenever possible (disabled)
257 { { "", "dts", NULL, HB_ACODEC_DCA_PASS, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_DTS_PASS, },
258 { { "AAC (faac)", "faac", NULL, 0, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_AAC, },
259 { { "AAC (ffmpeg)", "ffaac", NULL, HB_ACODEC_FFAAC, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_AAC, },
260 { { "AC3 (ffmpeg)", "ffac3", NULL, HB_ACODEC_AC3, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_AC3, },
261 { { "MP3 (lame)", "lame", NULL, HB_ACODEC_LAME, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_MP3, },
262 { { "Vorbis (vorbis)", "libvorbis", NULL, HB_ACODEC_VORBIS, HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_VORBIS, },
263 { { "FLAC (ffmpeg)", "ffflac", NULL, HB_ACODEC_FFFLAC, HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_FLAC, },
264 { { "FLAC (24-bit)", "ffflac24", NULL, HB_ACODEC_FFFLAC24, HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_FLAC, },
265 // generic names
266 { { "AAC", "aac", NULL, 0, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_AAC, },
267 { { "HE-AAC", "haac", NULL, 0, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_ACODEC_AAC_HE, },
268 // actual encoders
269 { { "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, },
270 { { "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, },
271 { { "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, },
272 { { "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, },
273 { { "AAC (avcodec)", "av_aac", "AAC (libavcodec)", HB_ACODEC_FFAAC, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_AAC, },
274 { { "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, },
275 { { "AC3", "ac3", "AC3 (libavcodec)", HB_ACODEC_AC3, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_AC3, },
276 { { "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, },
277 { { "E-AC3", "eac3", "E-AC3 (libavcodec)", HB_ACODEC_FFEAC3, HB_MUX_AV_MKV, }, NULL, 1, HB_GID_ACODEC_EAC3, },
278 { { "E-AC3 Passthru", "copy:eac3", "E-AC3 Passthru", HB_ACODEC_EAC3_PASS, HB_MUX_AV_MKV, }, NULL, 1, HB_GID_ACODEC_EAC3_PASS, },
279 { { "TrueHD Passthru", "copy:truehd","TrueHD Passthru", HB_ACODEC_TRUEHD_PASS, HB_MUX_AV_MKV, }, NULL, 1, HB_GID_ACODEC_TRUEHD_PASS,},
280 { { "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, },
281 { { "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, },
282 { { "MP3", "mp3", "MP3 (libmp3lame)", HB_ACODEC_LAME, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_MP3, },
283 { { "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, },
284 { { "Vorbis", "vorbis", "Vorbis (libvorbis)", HB_ACODEC_VORBIS, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_VORBIS, },
285 { { "FLAC 16-bit", "flac16", "FLAC 16-bit (libavcodec)", HB_ACODEC_FFFLAC, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_FLAC, },
286 { { "FLAC 24-bit", "flac24", "FLAC 24-bit (libavcodec)", HB_ACODEC_FFFLAC24, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_FLAC, },
287 { { "FLAC Passthru", "copy:flac", "FLAC Passthru", HB_ACODEC_FLAC_PASS, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_FLAC_PASS, },
288 { { "Auto Passthru", "copy", "Auto Passthru", HB_ACODEC_AUTO_PASS, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_ACODEC_AUTO_PASS, },
290 int hb_audio_encoders_count = sizeof(hb_audio_encoders) / sizeof(hb_audio_encoders[0]);
291 static int hb_audio_encoder_is_enabled(int encoder)
293 if (encoder & HB_ACODEC_PASS_FLAG)
295 // Passthru encoders are always enabled
296 return 1;
298 switch (encoder)
300 #ifdef __APPLE__
301 case HB_ACODEC_CA_AAC:
302 case HB_ACODEC_CA_HAAC:
303 return 1;
304 #endif
306 #ifdef USE_LIBAV_AAC
307 case HB_ACODEC_FFAAC:
308 return avcodec_find_encoder_by_name("aac") != NULL;
309 #endif
311 case HB_ACODEC_FDK_AAC:
312 case HB_ACODEC_FDK_HAAC:
313 return avcodec_find_encoder_by_name("libfdk_aac") != NULL;
315 case HB_ACODEC_AC3:
316 return avcodec_find_encoder(AV_CODEC_ID_AC3) != NULL;
318 case HB_ACODEC_FFEAC3:
319 return avcodec_find_encoder(AV_CODEC_ID_EAC3) != NULL;
321 case HB_ACODEC_FFFLAC:
322 case HB_ACODEC_FFFLAC24:
323 return avcodec_find_encoder(AV_CODEC_ID_FLAC) != NULL;
325 // the following encoders are always enabled
326 case HB_ACODEC_LAME:
327 case HB_ACODEC_VORBIS:
328 return 1;
330 default:
331 return 0;
335 typedef struct
337 hb_container_t item;
338 hb_container_t *next;
339 int enabled;
340 int gid;
341 } hb_container_internal_t;
342 hb_container_t *hb_containers_first_item = NULL;
343 hb_container_t *hb_containers_last_item = NULL;
344 hb_container_internal_t hb_containers[] =
346 // legacy muxers, back to HB 0.9.4 whenever possible (disabled)
347 { { "M4V file", "m4v", NULL, "m4v", 0, }, NULL, 0, HB_GID_MUX_MP4, },
348 { { "MP4 file", "mp4", NULL, "mp4", 0, }, NULL, 0, HB_GID_MUX_MP4, },
349 { { "MKV file", "mkv", NULL, "mkv", 0, }, NULL, 0, HB_GID_MUX_MKV, },
350 // actual muxers
351 { { "MPEG-4 (avformat)", "av_mp4", "MPEG-4 (libavformat)", "mp4", HB_MUX_AV_MP4, }, NULL, 1, HB_GID_MUX_MP4, },
352 { { "MPEG-4 (mp4v2)", "mp4v2", "MPEG-4 (libmp4v2)", "mp4", HB_MUX_MP4V2, }, NULL, 1, HB_GID_MUX_MP4, },
353 { { "Matroska (avformat)", "av_mkv", "Matroska (libavformat)", "mkv", HB_MUX_AV_MKV, }, NULL, 1, HB_GID_MUX_MKV, },
354 { { "Matroska (libmkv)", "libmkv", "Matroska (libmkv)", "mkv", HB_MUX_LIBMKV, }, NULL, 1, HB_GID_MUX_MKV, },
356 int hb_containers_count = sizeof(hb_containers) / sizeof(hb_containers[0]);
357 static int hb_container_is_enabled(int format)
359 switch (format)
361 case HB_MUX_AV_MP4:
362 case HB_MUX_AV_MKV:
363 return 1;
365 default:
366 return 0;
370 void hb_common_global_init()
372 static int common_init_done = 0;
373 if (common_init_done)
374 return;
376 int i, j;
378 // video framerates
379 for (i = 0; i < hb_video_rates_count; i++)
381 if (hb_video_rates[i].enabled)
383 if (hb_video_rates_first_item == NULL)
385 hb_video_rates_first_item = &hb_video_rates[i].item;
387 else
389 ((hb_rate_internal_t*)hb_video_rates_last_item)->next =
390 &hb_video_rates[i].item;
392 hb_video_rates_last_item = &hb_video_rates[i].item;
395 // fallbacks are static for now (no setup required)
397 // audio samplerates
398 for (i = 0; i < hb_audio_rates_count; i++)
400 if (hb_audio_rates[i].enabled)
402 if (hb_audio_rates_first_item == NULL)
404 hb_audio_rates_first_item = &hb_audio_rates[i].item;
406 else
408 ((hb_rate_internal_t*)hb_audio_rates_last_item)->next =
409 &hb_audio_rates[i].item;
411 hb_audio_rates_last_item = &hb_audio_rates[i].item;
414 // fallbacks are static for now (no setup required)
416 // audio bitrates
417 for (i = 0; i < hb_audio_bitrates_count; i++)
419 if (hb_audio_bitrates[i].enabled)
421 if (hb_audio_bitrates_first_item == NULL)
423 hb_audio_bitrates_first_item = &hb_audio_bitrates[i].item;
425 else
427 ((hb_rate_internal_t*)hb_audio_bitrates_last_item)->next =
428 &hb_audio_bitrates[i].item;
430 hb_audio_bitrates_last_item = &hb_audio_bitrates[i].item;
433 // fallbacks are static for now (no setup required)
435 // audio dithers
436 for (i = 0; i < hb_audio_dithers_count; i++)
438 if (hb_audio_dithers[i].enabled)
440 if (hb_audio_dithers_first_item == NULL)
442 hb_audio_dithers_first_item = &hb_audio_dithers[i].item;
444 else
446 ((hb_dither_internal_t*)hb_audio_dithers_last_item)->next =
447 &hb_audio_dithers[i].item;
449 hb_audio_dithers_last_item = &hb_audio_dithers[i].item;
452 // fallbacks are static for now (no setup required)
454 // audio mixdowns
455 for (i = 0; i < hb_audio_mixdowns_count; i++)
457 if (hb_audio_mixdowns[i].enabled)
459 if (hb_audio_mixdowns_first_item == NULL)
461 hb_audio_mixdowns_first_item = &hb_audio_mixdowns[i].item;
463 else
465 ((hb_mixdown_internal_t*)hb_audio_mixdowns_last_item)->next =
466 &hb_audio_mixdowns[i].item;
468 hb_audio_mixdowns_last_item = &hb_audio_mixdowns[i].item;
471 // fallbacks are static for now (no setup required)
473 // video encoders
474 for (i = 0; i < hb_video_encoders_count; i++)
476 if (hb_video_encoders[i].enabled)
478 // we still need to check
479 hb_video_encoders[i].enabled =
480 hb_video_encoder_is_enabled(hb_video_encoders[i].item.codec);
482 if (hb_video_encoders[i].enabled)
484 if (hb_video_encoders_first_item == NULL)
486 hb_video_encoders_first_item = &hb_video_encoders[i].item;
488 else
490 ((hb_encoder_internal_t*)hb_video_encoders_last_item)->next =
491 &hb_video_encoders[i].item;
493 hb_video_encoders_last_item = &hb_video_encoders[i].item;
496 // setup fallbacks
497 for (i = 0; i < hb_video_encoders_count; i++)
499 if (!hb_video_encoders[i].enabled)
501 if ((hb_video_encoders[i].item.codec & HB_VCODEC_MASK) &&
502 (hb_video_encoder_is_enabled(hb_video_encoders[i].item.codec)))
504 // we have a specific fallback and it's enabled
505 continue;
507 for (j = 0; j < hb_video_encoders_count; j++)
509 if (hb_video_encoders[j].enabled &&
510 hb_video_encoders[j].gid == hb_video_encoders[i].gid)
512 hb_video_encoders[i].item.codec = hb_video_encoders[j].item.codec;
513 break;
519 // audio encoders
520 for (i = 0; i < hb_audio_encoders_count; i++)
522 if (hb_audio_encoders[i].enabled)
524 // we still need to check
525 hb_audio_encoders[i].enabled =
526 hb_audio_encoder_is_enabled(hb_audio_encoders[i].item.codec);
528 if (hb_audio_encoders[i].enabled)
530 if (hb_audio_encoders_first_item == NULL)
532 hb_audio_encoders_first_item = &hb_audio_encoders[i].item;
534 else
536 ((hb_encoder_internal_t*)hb_audio_encoders_last_item)->next =
537 &hb_audio_encoders[i].item;
539 hb_audio_encoders_last_item = &hb_audio_encoders[i].item;
542 // setup fallbacks
543 for (i = 0; i < hb_audio_encoders_count; i++)
545 if (!hb_audio_encoders[i].enabled)
547 if ((hb_audio_encoders[i].item.codec & HB_ACODEC_MASK) &&
548 (hb_audio_encoder_is_enabled(hb_audio_encoders[i].item.codec)))
550 // we have a specific fallback and it's enabled
551 continue;
553 for (j = 0; j < hb_audio_encoders_count; j++)
555 if (hb_audio_encoders[j].enabled &&
556 hb_audio_encoders[j].gid == hb_audio_encoders[i].gid)
558 hb_audio_encoders[i].item.codec = hb_audio_encoders[j].item.codec;
559 break;
562 if ((hb_audio_encoders[i].item.codec & HB_ACODEC_MASK) == 0 &&
563 (hb_audio_encoders[i].gid == HB_GID_ACODEC_AAC_HE))
565 // try to find an AAC fallback if no HE-AAC encoder is available
566 for (j = 0; j < hb_audio_encoders_count; j++)
568 if (hb_audio_encoders[j].enabled &&
569 hb_audio_encoders[j].gid == HB_GID_ACODEC_AAC)
571 hb_audio_encoders[i].item.codec = hb_audio_encoders[j].item.codec;
572 break;
579 // video containers
580 for (i = 0; i < hb_containers_count; i++)
582 if (hb_containers[i].enabled)
584 // we still need to check
585 hb_containers[i].enabled =
586 hb_container_is_enabled(hb_containers[i].item.format);
588 if (hb_containers[i].enabled)
590 if (hb_containers_first_item == NULL)
592 hb_containers_first_item = &hb_containers[i].item;
594 else
596 ((hb_container_internal_t*)hb_containers_last_item)->next =
597 &hb_containers[i].item;
599 hb_containers_last_item = &hb_containers[i].item;
602 // setup fallbacks
603 for (i = 0; i < hb_containers_count; i++)
605 if (!hb_containers[i].enabled)
607 if ((hb_containers[i].item.format & HB_MUX_MASK) &&
608 (hb_container_is_enabled(hb_containers[i].item.format)))
610 // we have a specific fallback and it's enabled
611 continue;
613 for (j = 0; j < hb_containers_count; j++)
615 if (hb_containers[j].enabled &&
616 hb_containers[j].gid == hb_containers[i].gid)
618 hb_containers[i].item.format = hb_containers[j].item.format;
619 break;
625 // we're done, yay!
626 common_init_done = 1;
629 int hb_video_framerate_get_from_name(const char *name)
631 if (name == NULL || *name == '\0')
632 goto fail;
634 int i;
635 for (i = 0; i < hb_video_rates_count; i++)
637 if (!strcasecmp(hb_video_rates[i].item.name, name))
639 return hb_video_rates[i].item.rate;
643 fail:
644 return -1;
647 const char* hb_video_framerate_get_name(int framerate)
649 if (framerate > hb_video_rates_first_item->rate ||
650 framerate < hb_video_rates_last_item ->rate)
651 goto fail;
653 const hb_rate_t *video_framerate = NULL;
654 while ((video_framerate = hb_video_framerate_get_next(video_framerate)) != NULL)
656 if (video_framerate->rate == framerate)
658 return video_framerate->name;
662 fail:
663 return NULL;
666 const char* hb_video_framerate_sanitize_name(const char *name)
668 return hb_video_framerate_get_name(hb_video_framerate_get_from_name(name));
671 const hb_rate_t* hb_video_framerate_get_next(const hb_rate_t *last)
673 if (last == NULL)
675 return hb_video_rates_first_item;
677 return ((hb_rate_internal_t*)last)->next;
680 int hb_audio_samplerate_get_best(uint32_t codec, int samplerate, int *sr_shift)
682 int best_samplerate;
683 if (samplerate < 32000 && (codec == HB_ACODEC_AC3 ||
684 codec == HB_ACODEC_FFEAC3 ||
685 codec == HB_ACODEC_CA_HAAC))
687 // ca_haac can't do samplerates < 32 kHz
688 // libav's E-AC-3 encoder can't do samplerates < 32 kHz
689 // AC-3 < 32 kHz suffers from poor hardware compatibility
690 best_samplerate = 32000;
692 else if (samplerate < 16000 && codec == HB_ACODEC_FDK_HAAC)
694 // fdk_haac can't do samplerates < 16 kHz
695 best_samplerate = 16000;
697 else
699 best_samplerate = hb_audio_rates_first_item->rate;
700 const hb_rate_t *audio_samplerate = NULL;
701 while ((audio_samplerate = hb_audio_samplerate_get_next(audio_samplerate)) != NULL)
703 if (samplerate == audio_samplerate->rate)
705 // valid samplerate
706 best_samplerate = audio_samplerate->rate;
707 break;
709 if (samplerate > audio_samplerate->rate)
711 // samplerates are sanitized downwards
712 best_samplerate = audio_samplerate->rate;
716 if (sr_shift != NULL)
718 /* sr_shift: 0 -> 48000, 44100, 32000 Hz
719 * 1 -> 24000, 22050, 16000 Hz
720 * 2 -> 12000, 11025, 8000 Hz
722 * also, since samplerates are sanitized downwards:
724 * (samplerate < 32000) implies (samplerate <= 24000)
726 *sr_shift = ((best_samplerate < 16000) ? 2 :
727 (best_samplerate < 32000) ? 1 : 0);
729 return best_samplerate;
732 int hb_audio_samplerate_get_from_name(const char *name)
734 if (name == NULL || *name == '\0')
735 goto fail;
737 int i;
738 for (i = 0; i < hb_audio_rates_count; i++)
740 if (!strcasecmp(hb_audio_rates[i].item.name, name))
742 return hb_audio_rates[i].item.rate;
746 // maybe the samplerate was specified in Hz
747 i = atoi(name);
748 if (i >= hb_audio_rates_first_item->rate &&
749 i <= hb_audio_rates_last_item ->rate)
751 return hb_audio_samplerate_get_best(0, i, NULL);
754 fail:
755 return -1;
758 const char* hb_audio_samplerate_get_name(int samplerate)
760 if (samplerate < hb_audio_rates_first_item->rate ||
761 samplerate > hb_audio_rates_last_item ->rate)
762 goto fail;
764 const hb_rate_t *audio_samplerate = NULL;
765 while ((audio_samplerate = hb_audio_samplerate_get_next(audio_samplerate)) != NULL)
767 if (audio_samplerate->rate == samplerate)
769 return audio_samplerate->name;
773 fail:
774 return NULL;
777 const hb_rate_t* hb_audio_samplerate_get_next(const hb_rate_t *last)
779 if (last == NULL)
781 return hb_audio_rates_first_item;
783 return ((hb_rate_internal_t*)last)->next;
786 // Given an input bitrate, find closest match in the set of allowed bitrates
787 static int hb_audio_bitrate_find_closest(int bitrate)
789 // Check if bitrate mode was disabled
790 if (bitrate <= 0)
791 return bitrate;
793 int closest_bitrate = hb_audio_bitrates_first_item->rate;
794 const hb_rate_t *audio_bitrate = NULL;
795 while ((audio_bitrate = hb_audio_bitrate_get_next(audio_bitrate)) != NULL)
797 if (bitrate == audio_bitrate->rate)
799 // valid bitrate
800 closest_bitrate = audio_bitrate->rate;
801 break;
803 if (bitrate > audio_bitrate->rate)
805 // bitrates are sanitized downwards
806 closest_bitrate = audio_bitrate->rate;
809 return closest_bitrate;
812 // Given an input bitrate, sanitize it.
813 // Check low and high limits and make sure it is in the set of allowed bitrates.
814 int hb_audio_bitrate_get_best(uint32_t codec, int bitrate, int samplerate,
815 int mixdown)
817 int low, high;
818 hb_audio_bitrate_get_limits(codec, samplerate, mixdown, &low, &high);
819 if (bitrate > high)
820 bitrate = high;
821 if (bitrate < low)
822 bitrate = low;
823 return hb_audio_bitrate_find_closest(bitrate);
826 // Get the default bitrate for a given codec/samplerate/mixdown triplet.
827 int hb_audio_bitrate_get_default(uint32_t codec, int samplerate, int mixdown)
829 if ((codec & HB_ACODEC_PASS_FLAG) || !(codec & HB_ACODEC_MASK))
830 goto fail;
832 int bitrate, nchannels, sr_shift;
833 /* full-bandwidth channels, sr_shift */
834 nchannels = (hb_mixdown_get_discrete_channel_count(mixdown) -
835 hb_mixdown_get_low_freq_channel_count(mixdown));
836 hb_audio_samplerate_get_best(codec, samplerate, &sr_shift);
838 switch (codec)
840 case HB_ACODEC_FFFLAC:
841 case HB_ACODEC_FFFLAC24:
842 goto fail;
844 // 96, 224, 640 Kbps
845 case HB_ACODEC_AC3:
846 bitrate = (nchannels * 128) - (32 * (nchannels < 5));
847 break;
849 // Our E-AC-3 encoder is pretty similar to our AC-3 encoder but it does
850 // allow for higher bitrates, should some users want a bit more quality
851 // at the expense of compression efficiency - still, let's remain
852 // compatible with passthru over S/PDIF by default: 384, 768, 1536 Kbps
853 case HB_ACODEC_FFEAC3:
854 bitrate = (nchannels * 384) - (128 * (nchannels - 2) * (nchannels > 2));
855 break;
857 case HB_ACODEC_CA_HAAC:
858 case HB_ACODEC_FDK_HAAC:
859 bitrate = nchannels * 32;
860 break;
862 default:
863 bitrate = nchannels * 80;
864 break;
866 // sample_rate adjustment
867 bitrate >>= sr_shift;
868 return hb_audio_bitrate_get_best(codec, bitrate, samplerate, mixdown);
870 fail:
871 return -1;
874 /* Get the bitrate low and high limits for a codec/samplerate/mixdown triplet.
876 * Encoder 1.0 channel 2.0 channels 5.1 channels 6.1 channels 7.1 channels
877 * --------------------------------------------------------------------------------------
879 * ffaac
880 * -----
881 * supported samplerates: 8 - 48 kHz
882 * libavcodec/aacenc.c defines a maximum bitrate:
883 * -> 6144 * samplerate / 1024 bps (per channel, incl. LFE).
884 * But output bitrates don't go as high as the theoretical maximums:
885 * 12 kHz 61 (72) 123 (144)
886 * 24 kHz 121 (144) 242 (288)
887 * 48 kHz 236 (288) 472 (576)
888 * Also, ffaac isn't a great encoder, so you don't want to allow too low a bitrate.
889 * Limits: minimum of 32 Kbps per channel
890 * maximum of 192 Kbps per channel at 32 kHz, adjusted for sr_shift
891 * maximum of 256 Kbps per channel at 44.1-48 kHz, adjusted for sr_shift
893 * vorbis
894 * ------
895 * supported samplerates: 8 - 48 kHz
896 * lib/modes/setup_*.h provides a range of allowed bitrates for various configurations.
897 * for each samplerate, the highest minimums and lowest maximums are:
898 * 8 kHz Minimum 8 Kbps, maximum 32 Kbps (per channel, incl. LFE).
899 * 12 kHz Minimum 14 Kbps, maximum 44 Kbps (per channel, incl. LFE).
900 * 16 kHz Minimum 16 Kbps, maximum 86 Kbps (per channel, incl. LFE).
901 * 24 kHz Minimum 22 Kbps, maximum 86 Kbps (per channel, incl. LFE).
902 * 32 kHz Minimum 26 Kbps, maximum 190 Kbps (per channel, incl. LFE).
903 * 48 kHz Minimum 28 Kbps, maximum 240 Kbps (per channel, incl. LFE).
904 * Limits: minimum of 14/22/28 Kbps per channel (8-12, 16-24, 32-48 kHz)
905 * maximum of 32/86/190/240 Kbps per channel (8-12, 16-24, 32, 44.1-48 kHz)
907 * lame
908 * ----
909 * supported samplerates: 8 - 48 kHz
910 * lame_init_params() allows the following bitrates:
911 * 12 kHz Minimum 8 Kbps, maximum 64 Kbps
912 * 24 kHz Minimum 8 Kbps, maximum 160 Kbps
913 * 48 kHz Minimum 32 Kbps, maximum 320 Kbps
914 * Limits: minimum of 8/8/32 Kbps (8-12, 16-24, 32-48 kHz)
915 * maximum of 64/160/320 Kbps (8-12, 16-24, 32-48 kHz)
917 * ffac3
918 * -----
919 * supported samplerates: 32 - 48 kHz (< 32 kHz disabled for compatibility reasons)
920 * Dolby's encoder has a min. of 224 Kbps for 5 full-bandwidth channels (5.0, 5.1)
921 * The maximum AC3 bitrate is 640 Kbps
922 * Limits: minimum of 224/5 Kbps per full-bandwidth channel, maximum of 640 Kbps
924 * ffeac3
925 * ------
926 * supported samplerates: 32 - 48 kHz (< 32 kHz not supported by libav encoder)
927 * Dolby's encoder has a min. of 224 Kbps for 5 full-bandwidth channels (5.0, 5.1)
928 * The maximum bitrate is 128 bits per sample per second
929 * Limits: minimum of 224/5 Kbps per full-bandwidth channel
930 * maximum of 128/1000 * samplerate Kbps
932 * ca_aac
933 * ------
934 * supported samplerates: 8 - 48 kHz
935 * Core Audio API provides a range of allowed bitrates:
936 * 8 kHz 8 - 24 16 - 48 40 - 112 48 - 144 56 - 160
937 * 12 kHz 12 - 32 24 - 64 64 - 160 72 - 192 96 - 224
938 * 16 kHz 12 - 48 24 - 96 64 - 224 72 - 288 96 - 320
939 * 24 kHz 16 - 64 32 - 128 80 - 320 96 - 384 112 - 448
940 * 32 kHz 24 - 96 48 - 192 128 - 448 144 - 576 192 - 640
941 * 48 kHz 32 - 256 64 - 320 160 - 768 192 - 960 224 - 960
942 * Limits:
943 * 8 kHz -> minimum of 8 Kbps and maximum of 24 Kbps per full-bandwidth channel
944 * 12 kHz -> minimum of 12 Kbps and maximum of 32 Kbps per full-bandwidth channel
945 * 16 kHz -> minimum of 12 Kbps and maximum of 48 Kbps per full-bandwidth channel
946 * 24 kHz -> minimum of 16 Kbps and maximum of 64 Kbps per full-bandwidth channel
947 * 32 kHz -> minimum of 24 Kbps and maximum of 96 Kbps per full-bandwidth channel
948 * 48 kHz -> minimum of 32 Kbps and maximum of 160 Kbps per full-bandwidth channel
949 * 48 kHz -> maximum of +96 Kbps for Mono
950 * Note: encCoreAudioInit() will sanitize any mistake made here.
952 * ca_haac
953 * -------
954 * supported samplerates: 32 - 48 kHz
955 * Core Audio API provides a range of allowed bitrates:
956 * 32 kHz 12 - 40 24 - 80 64 - 192 N/A 96 - 256
957 * 48 kHz 16 - 40 32 - 80 80 - 192 N/A 112 - 256
958 * Limits: minimum of 12 Kbps per full-bandwidth channel (<= 32 kHz)
959 * minimum of 16 Kbps per full-bandwidth channel ( > 32 kHz)
960 * maximum of 40 Kbps per full-bandwidth channel
961 * Note: encCoreAudioInit() will sanitize any mistake made here.
963 * fdk_aac
964 * -------
965 * supported samplerates: 8 - 48 kHz
966 * libfdk limits the bitrate to the following values:
967 * 8 kHz 48 96 240
968 * 12 kHz 72 144 360
969 * 16 kHz 96 192 480
970 * 24 kHz 144 288 720
971 * 32 kHz 192 384 960
972 * 48 kHz 288 576 1440
973 * Limits: minimum of samplerate * 2/3 Kbps per full-bandwidth channel (see ca_aac)
974 * maximum of samplerate * 6.0 Kbps per full-bandwidth channel
976 * fdk_haac
977 * --------
978 * supported samplerates: 16 - 48 kHz
979 * libfdk limits the bitrate to the following values:
980 * 16 kHz 8 - 48 16 - 96 45 - 199
981 * 24 kHz 8 - 63 16 - 127 45 - 266
982 * 32 kHz 8 - 63 16 - 127 45 - 266
983 * 48 kHz 12 - 63 16 - 127 50 - 266
984 * Limits: minimum of 12 Kbps per full-bandwidth channel (<= 32 kHz) (see ca_haac)
985 * minimum of 16 Kbps per full-bandwidth channel ( > 32 kHz) (see ca_haac)
986 * maximum of 48, 96 or 192 Kbps (1.0, 2.0, 5.1) (<= 16 kHz)
987 * maximum of 64, 128 or 256 Kbps (1.0, 2.0, 5.1) ( > 16 kHz)
989 void hb_audio_bitrate_get_limits(uint32_t codec, int samplerate, int mixdown,
990 int *low, int *high)
993 * samplerate == 0 means "auto" (same as source) and the UIs know the source
994 * samplerate -- except where there isn't a source (audio defaults panel);
995 * but we have enough info to return the global bitrate limits for this
996 * mixdown, since the first/last samplerate are known to us and non-zero.
998 if (samplerate == 0)
1000 int dummy;
1001 hb_audio_bitrate_get_limits(codec, hb_audio_rates_first_item->rate, mixdown, low, &dummy);
1002 hb_audio_bitrate_get_limits(codec, hb_audio_rates_last_item->rate, mixdown, &dummy, high);
1003 return;
1006 /* samplerate, sr_shift */
1007 int sr_shift;
1008 samplerate = hb_audio_samplerate_get_best(codec, samplerate, &sr_shift);
1010 /* LFE, full-bandwidth channels */
1011 int lfe_count, nchannels;
1012 lfe_count = hb_mixdown_get_low_freq_channel_count(mixdown);
1013 nchannels = hb_mixdown_get_discrete_channel_count(mixdown) - lfe_count;
1015 switch (codec)
1017 // Bitrates don't apply to "lossless" audio
1018 case HB_ACODEC_FFFLAC:
1019 case HB_ACODEC_FFFLAC24:
1020 *low = *high = -1;
1021 return;
1023 case HB_ACODEC_AC3:
1024 *low = 224 * nchannels / 5;
1025 *high = 640;
1026 break;
1028 case HB_ACODEC_FFEAC3:
1029 *low = 224 * nchannels / 5;
1030 *high = 128 * samplerate / 1000;
1031 break;
1033 case HB_ACODEC_CA_AAC:
1035 switch (samplerate)
1037 case 8000:
1038 *low = nchannels * 8;
1039 *high = nchannels * 24;
1040 break;
1042 case 11025:
1043 case 12000:
1044 *low = nchannels * 12;
1045 *high = nchannels * 32;
1046 break;
1048 case 16000:
1049 *low = nchannels * 12;
1050 *high = nchannels * 48;
1051 break;
1053 case 22050:
1054 case 24000:
1055 *low = nchannels * 16;
1056 *high = nchannels * 64;
1057 break;
1059 case 32000:
1060 *low = nchannels * 24;
1061 *high = nchannels * 96;
1062 break;
1064 case 44100:
1065 case 48000:
1066 default:
1067 *low = nchannels * 32;
1068 *high = nchannels * (160 + (96 * (nchannels == 1)));
1069 break;
1070 } break;
1073 case HB_ACODEC_CA_HAAC:
1074 *low = nchannels * (12 + (4 * (samplerate >= 44100)));
1075 *high = nchannels * 40;
1076 break;
1078 case HB_ACODEC_FDK_AAC:
1079 *low = nchannels * samplerate * 2 / 3000;
1080 *high = nchannels * samplerate * 6 / 1000;
1081 break;
1083 case HB_ACODEC_FDK_HAAC:
1084 *low = (nchannels * (12 + (4 * (samplerate >= 44100))));
1085 *high = (nchannels - (nchannels > 2)) * (48 +
1086 (16 *
1087 (samplerate >= 22050)));
1088 break;
1090 case HB_ACODEC_FFAAC:
1091 *low = ((nchannels + lfe_count) * 32);
1092 *high = ((nchannels + lfe_count) *
1093 ((192 + (64 * ((samplerate << sr_shift) >= 44100)))
1094 >> sr_shift));
1095 break;
1097 case HB_ACODEC_LAME:
1098 *low = 8 + (24 * (sr_shift < 1));
1099 *high = 64 + (96 * (sr_shift < 2)) + (160 * (sr_shift < 1));
1100 break;
1102 case HB_ACODEC_VORBIS:
1103 *low = (nchannels + lfe_count) * (14 +
1104 (8 * (sr_shift < 2)) +
1105 (6 * (sr_shift < 1)));
1106 *high = (nchannels + lfe_count) * (32 +
1107 ( 54 * (sr_shift < 2)) +
1108 (104 * (sr_shift < 1)) +
1109 ( 50 * (samplerate >= 44100)));
1110 break;
1112 // Bitrates don't apply to passthrough audio, but may apply if we
1113 // fall back to an encoder when the source can't be passed through.
1114 default:
1115 *low = hb_audio_bitrates_first_item->rate;
1116 *high = hb_audio_bitrates_last_item ->rate;
1117 break;
1120 // sanitize max. bitrate
1121 if (*high < hb_audio_bitrates_first_item->rate)
1122 *high = hb_audio_bitrates_first_item->rate;
1123 if (*high > hb_audio_bitrates_last_item ->rate)
1124 *high = hb_audio_bitrates_last_item ->rate;
1127 const hb_rate_t* hb_audio_bitrate_get_next(const hb_rate_t *last)
1129 if (last == NULL)
1131 return hb_audio_bitrates_first_item;
1133 return ((hb_rate_internal_t*)last)->next;
1136 // Get limits and hints for the UIs.
1138 // granularity sets the minimum step increments that should be used
1139 // (it's ok to round up to some nice multiple if you like)
1141 // direction says whether 'low' limit is highest or lowest
1142 // quality (direction 0 == lowest value is worst quality)
1143 void hb_video_quality_get_limits(uint32_t codec, float *low, float *high,
1144 float *granularity, int *direction)
1146 #ifdef USE_QSV
1147 if (codec & HB_VCODEC_QSV_MASK)
1149 return hb_qsv_video_quality_get_limits(codec, low, high, granularity,
1150 direction);
1152 #endif
1154 switch (codec)
1156 case HB_VCODEC_X264:
1157 #ifdef USE_X265
1158 case HB_VCODEC_X265:
1159 #endif
1160 *direction = 1;
1161 *granularity = 0.1;
1162 *low = 0.;
1163 *high = 51.;
1164 break;
1166 case HB_VCODEC_THEORA:
1167 *direction = 0;
1168 *granularity = 1.;
1169 *low = 0.;
1170 *high = 63.;
1171 break;
1173 case HB_VCODEC_FFMPEG_VP8:
1174 *direction = 1;
1175 *granularity = 1.;
1176 *low = 0.;
1177 *high = 63.;
1178 break;
1180 case HB_VCODEC_FFMPEG_MPEG2:
1181 case HB_VCODEC_FFMPEG_MPEG4:
1182 default:
1183 *direction = 1;
1184 *granularity = 1.;
1185 *low = 1.;
1186 *high = 31.;
1187 break;
1191 const char* hb_video_quality_get_name(uint32_t codec)
1193 #ifdef USE_QSV
1194 if (codec & HB_VCODEC_QSV_MASK)
1196 return hb_qsv_video_quality_get_name(codec);
1198 #endif
1200 switch (codec)
1202 case HB_VCODEC_X264:
1203 #ifdef USE_X265
1204 case HB_VCODEC_X265:
1205 #endif
1206 return "RF";
1208 case HB_VCODEC_FFMPEG_VP8:
1209 return "CQ";
1211 default:
1212 return "QP";
1216 const char* const* hb_video_encoder_get_presets(int encoder)
1218 #ifdef USE_QSV
1219 if (encoder & HB_VCODEC_QSV_MASK)
1221 return hb_qsv_preset_get_names();
1223 #endif
1225 switch (encoder)
1227 case HB_VCODEC_X264:
1228 return x264_preset_names;
1230 #ifdef USE_X265
1231 case HB_VCODEC_X265:
1232 return x265_preset_names;
1233 #endif
1234 default:
1235 return NULL;
1239 const char* const* hb_video_encoder_get_tunes(int encoder)
1241 switch (encoder)
1243 case HB_VCODEC_X264:
1244 return x264_tune_names;
1246 #ifdef USE_X265
1247 case HB_VCODEC_X265:
1248 return x265_tune_names;
1249 #endif
1250 default:
1251 return NULL;
1255 const char* const* hb_video_encoder_get_profiles(int encoder)
1257 #ifdef USE_QSV
1258 if (encoder & HB_VCODEC_QSV_MASK)
1260 return hb_qsv_profile_get_names(encoder);
1262 #endif
1264 switch (encoder)
1266 case HB_VCODEC_X264:
1267 return hb_h264_profile_names;
1269 case HB_VCODEC_X265:
1270 return hb_h265_profile_names;
1272 default:
1273 return NULL;
1277 const char* const* hb_video_encoder_get_levels(int encoder)
1279 #ifdef USE_QSV
1280 if (encoder & HB_VCODEC_QSV_MASK)
1282 return hb_qsv_level_get_names(encoder);
1284 #endif
1286 switch (encoder)
1288 case HB_VCODEC_X264:
1289 return hb_h264_level_names;
1291 default:
1292 return NULL;
1296 // Get limits and hints for the UIs.
1298 // granularity sets the minimum step increments that should be used
1299 // (it's ok to round up to some nice multiple if you like)
1301 // direction says whether 'low' limit is highest or lowest
1302 // quality (direction 0 == lowest value is worst quality)
1303 void hb_audio_quality_get_limits(uint32_t codec, float *low, float *high,
1304 float *granularity, int *direction)
1306 switch (codec)
1308 case HB_ACODEC_FFAAC:
1309 *direction = 0;
1310 *granularity = 1.;
1311 *low = 1.;
1312 *high = 10.;
1313 break;
1315 case HB_ACODEC_FDK_HAAC:
1316 case HB_ACODEC_FDK_AAC:
1317 *direction = 0;
1318 *granularity = 1.;
1319 *low = 1.;
1320 *high = 5.;
1321 break;
1323 case HB_ACODEC_LAME:
1324 *direction = 1;
1325 *granularity = 0.5;
1326 *low = 0.;
1327 *high = 10.;
1328 break;
1330 case HB_ACODEC_VORBIS:
1331 *direction = 0;
1332 *granularity = 0.5;
1333 *low = -2.;
1334 *high = 10.;
1335 break;
1337 case HB_ACODEC_CA_AAC:
1338 *direction = 0;
1339 *granularity = 9.;
1340 *low = 1.;
1341 *high = 127.;
1342 break;
1344 default:
1345 *direction = 0;
1346 *granularity = 1.;
1347 *low = *high = HB_INVALID_AUDIO_QUALITY;
1348 break;
1352 float hb_audio_quality_get_best(uint32_t codec, float quality)
1354 int direction;
1355 float low, high, granularity;
1356 hb_audio_quality_get_limits(codec, &low, &high, &granularity, &direction);
1357 if (quality > high)
1358 quality = high;
1359 if (quality < low)
1360 quality = low;
1361 return quality;
1364 float hb_audio_quality_get_default(uint32_t codec)
1366 switch (codec)
1368 case HB_ACODEC_FFAAC:
1369 return 5.;
1371 case HB_ACODEC_FDK_HAAC:
1372 case HB_ACODEC_FDK_AAC:
1373 return 3.;
1375 case HB_ACODEC_LAME:
1376 return 2.;
1378 case HB_ACODEC_VORBIS:
1379 return 5.;
1381 case HB_ACODEC_CA_AAC:
1382 return 91.;
1384 default:
1385 return HB_INVALID_AUDIO_QUALITY;
1389 // Get limits and hints for the UIs.
1391 // granularity sets the minimum step increments that should be used
1392 // (it's ok to round up to some nice multiple if you like)
1394 // direction says whether 'low' limit is highest or lowest
1395 // compression level (direction 0 == lowest value is worst compression level)
1396 void hb_audio_compression_get_limits(uint32_t codec, float *low, float *high,
1397 float *granularity, int *direction)
1399 switch (codec)
1401 case HB_ACODEC_FFFLAC:
1402 case HB_ACODEC_FFFLAC24:
1403 *direction = 0;
1404 *granularity = 1.;
1405 *high = 12.;
1406 *low = 0.;
1407 break;
1409 case HB_ACODEC_LAME:
1410 *direction = 1;
1411 *granularity = 1.;
1412 *high = 9.;
1413 *low = 0.;
1414 break;
1416 default:
1417 *direction = 0;
1418 *granularity = 1.;
1419 *low = *high = -1.;
1420 break;
1424 float hb_audio_compression_get_best(uint32_t codec, float compression)
1426 int direction;
1427 float low, high, granularity;
1428 hb_audio_compression_get_limits(codec, &low, &high, &granularity, &direction);
1429 if( compression > high )
1430 compression = high;
1431 if( compression < low )
1432 compression = low;
1433 return compression;
1436 float hb_audio_compression_get_default(uint32_t codec)
1438 switch (codec)
1440 case HB_ACODEC_FFFLAC:
1441 case HB_ACODEC_FFFLAC24:
1442 return 5.;
1444 case HB_ACODEC_LAME:
1445 return 2.;
1447 default:
1448 return -1.;
1452 int hb_audio_dither_get_default()
1454 // "auto"
1455 return hb_audio_dithers_first_item->method;
1458 int hb_audio_dither_get_default_method()
1461 * input could be s16 (possibly already dithered) converted to flt, so
1462 * let's use a "low-risk" dither algorithm (standard triangular).
1464 return AV_RESAMPLE_DITHER_TRIANGULAR;
1467 int hb_audio_dither_is_supported(uint32_t codec)
1469 // encoder's input sample format must be s16(p)
1470 switch (codec)
1472 case HB_ACODEC_FFFLAC:
1473 case HB_ACODEC_FDK_AAC:
1474 case HB_ACODEC_FDK_HAAC:
1475 return 1;
1477 default:
1478 return 0;
1482 int hb_audio_dither_get_from_name(const char *name)
1484 if (name == NULL || *name == '\0')
1485 goto fail;
1487 int i;
1488 for ( i = 0; i < hb_audio_dithers_count; i++)
1490 if (!strcasecmp(hb_audio_dithers[i].item.short_name, name) ||
1491 !strcasecmp(hb_audio_dithers[i].item.description, name))
1493 return hb_audio_dithers[i].item.method;
1497 fail:
1498 return hb_audio_dither_get_default();
1501 const char* hb_audio_dither_get_description(int method)
1503 if (method < hb_audio_dithers_first_item->method ||
1504 method > hb_audio_dithers_last_item ->method)
1505 goto fail;
1507 const hb_dither_t *audio_dither = NULL;
1508 while ((audio_dither = hb_audio_dither_get_next(audio_dither)) != NULL)
1510 if (audio_dither->method == method)
1512 return audio_dither->description;
1516 fail:
1517 return NULL;
1520 const hb_dither_t* hb_audio_dither_get_next(const hb_dither_t *last)
1522 if (last == NULL)
1524 return hb_audio_dithers_first_item;
1526 return ((hb_dither_internal_t*)last)->next;
1529 int hb_mixdown_is_supported(int mixdown, uint32_t codec, uint64_t layout)
1531 return (hb_mixdown_has_codec_support(mixdown, codec) &&
1532 hb_mixdown_has_remix_support(mixdown, layout));
1535 int hb_mixdown_has_codec_support(int mixdown, uint32_t codec)
1537 // Passthru, only "None" mixdown is supported
1538 if (codec & HB_ACODEC_PASS_FLAG)
1539 return (mixdown == HB_AMIXDOWN_NONE);
1541 // Not passthru, "None" mixdown never supported
1542 if (mixdown == HB_AMIXDOWN_NONE)
1543 return 0;
1545 switch (codec)
1547 case HB_ACODEC_VORBIS:
1548 case HB_ACODEC_FFFLAC:
1549 case HB_ACODEC_FFFLAC24:
1550 return (mixdown <= HB_AMIXDOWN_7POINT1);
1552 case HB_ACODEC_LAME:
1553 return (mixdown <= HB_AMIXDOWN_DOLBYPLII);
1555 case HB_ACODEC_CA_AAC:
1556 case HB_ACODEC_CA_HAAC:
1557 return ((mixdown <= HB_AMIXDOWN_5POINT1) ||
1558 (mixdown == HB_AMIXDOWN_5_2_LFE));
1560 default:
1561 return (mixdown <= HB_AMIXDOWN_5POINT1);
1565 int hb_mixdown_has_remix_support(int mixdown, uint64_t layout)
1568 * Where there isn't a source (e.g. audio defaults panel), we have no input
1569 * layout; assume remix support, as the mixdown will be sanitized later on.
1571 if (!layout)
1573 return 1;
1575 switch (mixdown)
1577 // stereo + front left/right of center
1578 case HB_AMIXDOWN_5_2_LFE:
1579 return ((layout & AV_CH_FRONT_LEFT_OF_CENTER) &&
1580 (layout & AV_CH_FRONT_RIGHT_OF_CENTER) &&
1581 (layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO);
1583 // 7.0 or better
1584 case HB_AMIXDOWN_7POINT1:
1585 return ((layout & AV_CH_LAYOUT_7POINT0) == AV_CH_LAYOUT_7POINT0);
1587 // 6.0 or better
1588 case HB_AMIXDOWN_6POINT1:
1589 return ((layout & AV_CH_LAYOUT_7POINT0) == AV_CH_LAYOUT_7POINT0 ||
1590 (layout & AV_CH_LAYOUT_6POINT0) == AV_CH_LAYOUT_6POINT0 ||
1591 (layout & AV_CH_LAYOUT_HEXAGONAL) == AV_CH_LAYOUT_HEXAGONAL);
1593 // stereo + either of front center, side or back left/right, back center
1594 case HB_AMIXDOWN_5POINT1:
1595 return ((layout & AV_CH_LAYOUT_2_1) == AV_CH_LAYOUT_2_1 ||
1596 (layout & AV_CH_LAYOUT_2_2) == AV_CH_LAYOUT_2_2 ||
1597 (layout & AV_CH_LAYOUT_QUAD) == AV_CH_LAYOUT_QUAD ||
1598 (layout & AV_CH_LAYOUT_SURROUND) == AV_CH_LAYOUT_SURROUND);
1600 // stereo + either of side or back left/right, back center
1601 // also, allow Dolby Surrounbd output if the input is already Dolby
1602 case HB_AMIXDOWN_DOLBY:
1603 case HB_AMIXDOWN_DOLBYPLII:
1604 return ((layout & AV_CH_LAYOUT_2_1) == AV_CH_LAYOUT_2_1 ||
1605 (layout & AV_CH_LAYOUT_2_2) == AV_CH_LAYOUT_2_2 ||
1606 (layout & AV_CH_LAYOUT_QUAD) == AV_CH_LAYOUT_QUAD ||
1607 (layout == AV_CH_LAYOUT_STEREO_DOWNMIX &&
1608 mixdown == HB_AMIXDOWN_DOLBY));
1610 // more than 1 channel
1611 case HB_AMIXDOWN_STEREO:
1612 return (av_get_channel_layout_nb_channels(layout) > 1);
1614 // regular stereo (not Dolby)
1615 case HB_AMIXDOWN_LEFT:
1616 case HB_AMIXDOWN_RIGHT:
1617 return (layout == AV_CH_LAYOUT_STEREO);
1619 // mono remix always supported
1620 // HB_AMIXDOWN_NONE always supported (for Passthru)
1621 case HB_AMIXDOWN_MONO:
1622 case HB_AMIXDOWN_NONE:
1623 return 1;
1625 // unknown mixdown, should never happen
1626 default:
1627 return 0;
1631 int hb_mixdown_get_discrete_channel_count(int amixdown)
1633 switch (amixdown)
1635 case HB_AMIXDOWN_5_2_LFE:
1636 case HB_AMIXDOWN_7POINT1:
1637 return 8;
1639 case HB_AMIXDOWN_6POINT1:
1640 return 7;
1642 case HB_AMIXDOWN_5POINT1:
1643 return 6;
1645 case HB_AMIXDOWN_MONO:
1646 case HB_AMIXDOWN_LEFT:
1647 case HB_AMIXDOWN_RIGHT:
1648 return 1;
1650 case HB_AMIXDOWN_NONE:
1651 return 0;
1653 default:
1654 return 2;
1658 int hb_mixdown_get_low_freq_channel_count(int amixdown)
1660 switch (amixdown)
1662 case HB_AMIXDOWN_5POINT1:
1663 case HB_AMIXDOWN_6POINT1:
1664 case HB_AMIXDOWN_7POINT1:
1665 case HB_AMIXDOWN_5_2_LFE:
1666 return 1;
1668 default:
1669 return 0;
1673 int hb_mixdown_get_best(uint32_t codec, uint64_t layout, int mixdown)
1675 // Passthru, only "None" mixdown is supported
1676 if (codec & HB_ACODEC_PASS_FLAG)
1677 return HB_AMIXDOWN_NONE;
1679 int best_mixdown = HB_INVALID_AMIXDOWN;
1680 const hb_mixdown_t *audio_mixdown = hb_mixdown_get_next(NULL);
1681 // test all non-None mixdowns while the value is <= the requested mixdown
1682 // HB_INVALID_AMIXDOWN means the highest supported mixdown was requested
1683 while ((audio_mixdown = hb_mixdown_get_next(audio_mixdown)) != NULL)
1685 if ((mixdown == HB_INVALID_AMIXDOWN || audio_mixdown->amixdown <= mixdown) &&
1686 (hb_mixdown_is_supported(audio_mixdown->amixdown, codec, layout)))
1688 best_mixdown = audio_mixdown->amixdown;
1691 return best_mixdown;
1694 int hb_mixdown_get_default(uint32_t codec, uint64_t layout)
1696 int mixdown;
1697 switch (codec)
1699 // the FLAC encoder defaults to the best mixdown up to 7.1
1700 case HB_ACODEC_FFFLAC:
1701 case HB_ACODEC_FFFLAC24:
1702 mixdown = HB_AMIXDOWN_7POINT1;
1703 break;
1705 // the (E-)AC-3 encoder defaults to the best mixdown up to 5.1
1706 case HB_ACODEC_AC3:
1707 case HB_ACODEC_FFEAC3:
1708 mixdown = HB_AMIXDOWN_5POINT1;
1709 break;
1711 // other encoders default to the best mixdown up to DPLII
1712 default:
1713 mixdown = HB_AMIXDOWN_DOLBYPLII;
1714 break;
1717 // return the best available mixdown up to the selected default
1718 return hb_mixdown_get_best(codec, layout, mixdown);
1721 hb_mixdown_t* hb_mixdown_get_from_mixdown(int mixdown)
1723 int i;
1724 for (i = 0; i < hb_audio_mixdowns_count; i++)
1726 if (hb_audio_mixdowns[i].item.amixdown == mixdown)
1728 return &hb_audio_mixdowns[i].item;
1732 return NULL;
1735 int hb_mixdown_get_from_name(const char *name)
1737 if (name == NULL || *name == '\0')
1738 goto fail;
1740 int i;
1741 for (i = 0; i < hb_audio_mixdowns_count; i++)
1743 if (!strcasecmp(hb_audio_mixdowns[i].item.name, name) ||
1744 !strcasecmp(hb_audio_mixdowns[i].item.short_name, name))
1746 return hb_audio_mixdowns[i].item.amixdown;
1750 fail:
1751 return HB_INVALID_AMIXDOWN;
1754 const char* hb_mixdown_get_name(int mixdown)
1756 if (mixdown < hb_audio_mixdowns_first_item->amixdown ||
1757 mixdown > hb_audio_mixdowns_last_item ->amixdown)
1758 goto fail;
1760 const hb_mixdown_t *audio_mixdown = NULL;
1761 while ((audio_mixdown = hb_mixdown_get_next(audio_mixdown)) != NULL)
1763 if (audio_mixdown->amixdown == mixdown)
1765 return audio_mixdown->name;
1769 fail:
1770 return NULL;
1773 const char* hb_mixdown_get_short_name(int mixdown)
1775 if (mixdown < hb_audio_mixdowns_first_item->amixdown ||
1776 mixdown > hb_audio_mixdowns_last_item ->amixdown)
1777 goto fail;
1779 const hb_mixdown_t *audio_mixdown = NULL;
1780 while ((audio_mixdown = hb_mixdown_get_next(audio_mixdown)) != NULL)
1782 if (audio_mixdown->amixdown == mixdown)
1784 return audio_mixdown->short_name;
1788 fail:
1789 return NULL;
1792 const char* hb_mixdown_sanitize_name(const char *name)
1794 return hb_mixdown_get_name(hb_mixdown_get_from_name(name));
1797 const hb_mixdown_t* hb_mixdown_get_next(const hb_mixdown_t *last)
1799 if (last == NULL)
1801 return hb_audio_mixdowns_first_item;
1803 return ((hb_mixdown_internal_t*)last)->next;
1806 int hb_video_encoder_get_default(int muxer)
1808 if (!(muxer & HB_MUX_MASK))
1809 goto fail;
1811 const hb_encoder_t *video_encoder = NULL;
1812 while ((video_encoder = hb_video_encoder_get_next(video_encoder)) != NULL)
1814 if (video_encoder->muxers & muxer)
1816 return video_encoder->codec;
1820 fail:
1821 return HB_VCODEC_INVALID;
1824 hb_encoder_t * hb_video_encoder_get_from_codec(int codec)
1826 int i;
1827 for (i = 0; i < hb_video_encoders_count; i++)
1829 if (hb_video_encoders[i].item.codec == codec)
1831 return &hb_video_encoders[i].item;
1835 return NULL;
1838 int hb_video_encoder_get_from_name(const char *name)
1840 if (name == NULL || *name == '\0')
1841 goto fail;
1843 int i;
1844 for (i = 0; i < hb_video_encoders_count; i++)
1846 if (!strcasecmp(hb_video_encoders[i].item.name, name) ||
1847 !strcasecmp(hb_video_encoders[i].item.short_name, name))
1849 return hb_video_encoders[i].item.codec;
1853 fail:
1854 return HB_VCODEC_INVALID;
1857 const char* hb_video_encoder_get_name(int encoder)
1859 if (!(encoder & HB_VCODEC_MASK))
1860 goto fail;
1862 const hb_encoder_t *video_encoder = NULL;
1863 while ((video_encoder = hb_video_encoder_get_next(video_encoder)) != NULL)
1865 if (video_encoder->codec == encoder)
1867 return video_encoder->name;
1871 fail:
1872 return NULL;
1875 const char* hb_video_encoder_get_short_name(int encoder)
1877 if (!(encoder & HB_VCODEC_MASK))
1878 goto fail;
1880 const hb_encoder_t *video_encoder = NULL;
1881 while ((video_encoder = hb_video_encoder_get_next(video_encoder)) != NULL)
1883 if (video_encoder->codec == encoder)
1885 return video_encoder->short_name;
1889 fail:
1890 return NULL;
1893 const char* hb_video_encoder_get_long_name(int encoder)
1895 if (!(encoder & HB_VCODEC_MASK))
1896 goto fail;
1898 const hb_encoder_t *video_encoder = NULL;
1899 while ((video_encoder = hb_video_encoder_get_next(video_encoder)) != NULL)
1901 if (video_encoder->codec == encoder)
1903 return video_encoder->long_name;
1907 fail:
1908 return NULL;
1911 const char* hb_video_encoder_sanitize_name(const char *name)
1913 return hb_video_encoder_get_name(hb_video_encoder_get_from_name(name));
1916 const hb_encoder_t* hb_video_encoder_get_next(const hb_encoder_t *last)
1918 if (last == NULL)
1920 return hb_video_encoders_first_item;
1922 return ((hb_encoder_internal_t*)last)->next;
1925 // for a valid passthru, return the matching encoder for that codec (if any),
1926 // else return -1 (i.e. drop the track)
1927 int hb_audio_encoder_get_fallback_for_passthru(int passthru)
1929 int gid;
1930 const hb_encoder_t *audio_encoder = NULL;
1931 switch (passthru)
1933 case HB_ACODEC_AAC_PASS:
1934 gid = HB_GID_ACODEC_AAC;
1935 break;
1937 case HB_ACODEC_AC3_PASS:
1938 gid = HB_GID_ACODEC_AC3;
1939 break;
1941 case HB_ACODEC_EAC3_PASS:
1942 gid = HB_GID_ACODEC_EAC3;
1943 break;
1945 case HB_ACODEC_FLAC_PASS:
1946 gid = HB_GID_ACODEC_FLAC;
1947 break;
1949 case HB_ACODEC_MP3_PASS:
1950 gid = HB_GID_ACODEC_MP3;
1951 break;
1953 default:
1954 gid = HB_GID_NONE; // will never match an enabled encoder
1955 break;
1957 while ((audio_encoder = hb_audio_encoder_get_next(audio_encoder)) != NULL)
1959 if (((hb_encoder_internal_t*)audio_encoder)->gid == gid)
1961 return audio_encoder->codec;
1965 // passthru tracks are often the second audio from the same source track
1966 // if we don't have an encoder matching the passthru codec, return INVALID
1967 // dropping the track, as well as ensuring that there is at least one
1968 // audio track in the output is then up to the UIs
1969 return HB_ACODEC_INVALID;
1972 int hb_audio_encoder_get_default(int muxer)
1974 if (!(muxer & HB_MUX_MASK))
1975 goto fail;
1977 int codec = 0;
1978 const hb_encoder_t *audio_encoder = NULL;
1979 while ((audio_encoder = hb_audio_encoder_get_next(audio_encoder)) != NULL)
1981 // default encoder should not be passthru
1982 if ((audio_encoder->muxers & muxer) &&
1983 (audio_encoder->codec & HB_ACODEC_PASS_FLAG) == 0)
1985 codec = audio_encoder->codec;
1986 break;
1990 // Lame is better than our low-end AAC encoders
1991 // if the container is MKV, use the former
1992 // AAC is still used when the container is MP4 (for better compatibility)
1993 if (codec == HB_ACODEC_FFAAC && (muxer & HB_MUX_MASK_MKV) == muxer)
1995 return HB_ACODEC_LAME;
1997 else
1999 return codec;
2002 fail:
2003 return HB_ACODEC_INVALID;
2006 hb_encoder_t* hb_audio_encoder_get_from_codec(int codec)
2008 int i;
2009 for (i = 0; i < hb_audio_encoders_count; i++)
2011 if (hb_audio_encoders[i].item.codec == codec)
2013 return &hb_audio_encoders[i].item;
2017 return NULL;
2020 int hb_audio_encoder_get_from_name(const char *name)
2022 if (name == NULL || *name == '\0')
2023 goto fail;
2025 int i;
2026 for (i = 0; i < hb_audio_encoders_count; i++)
2028 if (!strcasecmp(hb_audio_encoders[i].item.name, name) ||
2029 !strcasecmp(hb_audio_encoders[i].item.short_name, name))
2031 return hb_audio_encoders[i].item.codec;
2035 fail:
2036 return HB_ACODEC_INVALID;
2039 const char* hb_audio_encoder_get_name(int encoder)
2041 if (!(encoder & HB_ACODEC_ANY))
2042 goto fail;
2044 const hb_encoder_t *audio_encoder = NULL;
2045 while ((audio_encoder = hb_audio_encoder_get_next(audio_encoder)) != NULL)
2047 if (audio_encoder->codec == encoder)
2049 return audio_encoder->name;
2053 fail:
2054 return NULL;
2057 const char* hb_audio_encoder_get_short_name(int encoder)
2059 if (!(encoder & HB_ACODEC_ANY))
2060 goto fail;
2062 const hb_encoder_t *audio_encoder = NULL;
2063 while ((audio_encoder = hb_audio_encoder_get_next(audio_encoder)) != NULL)
2065 if (audio_encoder->codec == encoder)
2067 return audio_encoder->short_name;
2071 fail:
2072 return NULL;
2075 const char* hb_audio_encoder_get_long_name(int encoder)
2077 if (!(encoder & HB_ACODEC_ANY))
2078 goto fail;
2080 const hb_encoder_t *audio_encoder = NULL;
2081 while ((audio_encoder = hb_audio_encoder_get_next(audio_encoder)) != NULL)
2083 if (audio_encoder->codec == encoder)
2085 return audio_encoder->long_name;
2089 fail:
2090 return NULL;
2093 const char* hb_audio_encoder_sanitize_name(const char *name)
2095 return hb_audio_encoder_get_name(hb_audio_encoder_get_from_name(name));
2098 const hb_encoder_t* hb_audio_encoder_get_next(const hb_encoder_t *last)
2100 if (last == NULL)
2102 return hb_audio_encoders_first_item;
2104 return ((hb_encoder_internal_t*)last)->next;
2107 void hb_autopassthru_apply_settings(hb_job_t *job)
2109 hb_audio_t *audio;
2110 int i, already_printed;
2111 for (i = already_printed = 0; i < hb_list_count(job->list_audio);)
2113 audio = hb_list_item(job->list_audio, i);
2114 if (audio->config.out.codec == HB_ACODEC_AUTO_PASS)
2116 if (!already_printed)
2117 hb_autopassthru_print_settings(job);
2118 already_printed = 1;
2119 audio->config.out.codec = hb_autopassthru_get_encoder(audio->config.in.codec,
2120 job->acodec_copy_mask,
2121 job->acodec_fallback,
2122 job->mux);
2123 if (!(audio->config.out.codec & HB_ACODEC_PASS_FLAG) &&
2124 !(audio->config.out.codec & HB_ACODEC_MASK))
2126 hb_log("Auto Passthru: passthru not possible and no valid fallback specified, dropping track %d",
2127 audio->config.out.track );
2128 hb_list_rem(job->list_audio, audio);
2129 hb_audio_close(&audio);
2130 continue;
2132 if (!(audio->config.out.codec & HB_ACODEC_PASS_FLAG))
2134 if (audio->config.out.codec == job->acodec_fallback)
2136 hb_log("Auto Passthru: passthru not possible for track %d, using fallback",
2137 audio->config.out.track);
2139 else
2141 hb_log("Auto Passthru: passthru and fallback not possible for track %d, using default encoder",
2142 audio->config.out.track);
2144 if (audio->config.out.mixdown <= 0)
2146 audio->config.out.mixdown =
2147 hb_mixdown_get_default(audio->config.out.codec,
2148 audio->config.in.channel_layout);
2150 else
2152 audio->config.out.mixdown =
2153 hb_mixdown_get_best(audio->config.out.codec,
2154 audio->config.in.channel_layout,
2155 audio->config.out.mixdown);
2157 if (audio->config.out.samplerate <= 0)
2158 audio->config.out.samplerate = audio->config.in.samplerate;
2159 audio->config.out.samplerate =
2160 hb_audio_samplerate_get_best(audio->config.out.codec,
2161 audio->config.out.samplerate,
2162 NULL);
2163 int quality_not_allowed =
2164 hb_audio_quality_get_default(audio->config.out.codec)
2165 == HB_INVALID_AUDIO_QUALITY;
2166 if (audio->config.out.bitrate > 0)
2168 // Use best bitrate
2169 audio->config.out.bitrate =
2170 hb_audio_bitrate_get_best(audio->config.out.codec,
2171 audio->config.out.bitrate,
2172 audio->config.out.samplerate,
2173 audio->config.out.mixdown);
2175 else if (quality_not_allowed ||
2176 audio->config.out.quality != HB_INVALID_AUDIO_QUALITY)
2178 // Use default bitrate
2179 audio->config.out.bitrate =
2180 hb_audio_bitrate_get_default(audio->config.out.codec,
2181 audio->config.out.samplerate,
2182 audio->config.out.mixdown);
2184 else
2186 // Use best quality
2187 audio->config.out.quality =
2188 hb_audio_quality_get_best(audio->config.out.codec,
2189 audio->config.out.quality);
2191 if (audio->config.out.compression_level < 0)
2193 audio->config.out.compression_level =
2194 hb_audio_compression_get_default(
2195 audio->config.out.codec);
2197 else
2199 audio->config.out.compression_level =
2200 hb_audio_compression_get_best(audio->config.out.codec,
2201 audio->config.out.compression_level);
2204 else
2206 const hb_encoder_t *audio_encoder = NULL;
2207 while ((audio_encoder = hb_audio_encoder_get_next(audio_encoder)) != NULL)
2209 if (audio_encoder->codec == audio->config.out.codec)
2211 hb_log("Auto Passthru: using %s for track %d",
2212 audio_encoder->name,
2213 audio->config.out.track);
2214 break;
2219 /* Adjust output track number, in case we removed one.
2220 * Output tracks sadly still need to be in sequential order.
2221 * Note: out.track starts at 1, i starts at 0 */
2222 audio->config.out.track = ++i;
2226 void hb_autopassthru_print_settings(hb_job_t *job)
2228 char *mask = NULL, *tmp;
2229 const char *fallback = NULL;
2230 const hb_encoder_t *audio_encoder = NULL;
2231 while ((audio_encoder = hb_audio_encoder_get_next(audio_encoder)) != NULL)
2233 if ((audio_encoder->codec & HB_ACODEC_PASS_FLAG) &&
2234 (audio_encoder->codec != HB_ACODEC_AUTO_PASS) &&
2235 (audio_encoder->codec & (job->acodec_copy_mask &
2236 HB_ACODEC_PASS_MASK)))
2238 if (mask != NULL)
2240 tmp = hb_strncat_dup(mask, ", ", 2);
2241 if (tmp != NULL)
2243 free(mask);
2244 mask = tmp;
2247 // passthru name without " Passthru"
2248 tmp = hb_strncat_dup(mask, audio_encoder->name,
2249 strlen(audio_encoder->name) - 9);
2250 if (tmp != NULL)
2252 free(mask);
2253 mask = tmp;
2256 else if ((audio_encoder->codec & HB_ACODEC_PASS_FLAG) == 0 &&
2257 (audio_encoder->codec == job->acodec_fallback))
2259 fallback = audio_encoder->name;
2262 if (mask == NULL)
2263 hb_log("Auto Passthru: no codecs allowed");
2264 else
2265 hb_log("Auto Passthru: allowed codecs are %s", mask);
2266 if (fallback == NULL)
2267 hb_log("Auto Passthru: no valid fallback specified");
2268 else
2269 hb_log("Auto Passthru: fallback is %s", fallback);
2272 int hb_autopassthru_get_encoder(int in_codec, int copy_mask, int fallback,
2273 int muxer)
2275 int i = 0;
2276 const hb_encoder_t *audio_encoder = NULL;
2277 int out_codec = (copy_mask & in_codec) | HB_ACODEC_PASS_FLAG;
2278 // sanitize fallback encoder and selected passthru
2279 // note: invalid fallbacks are caught in hb_autopassthru_apply_settings
2280 while ((audio_encoder = hb_audio_encoder_get_next(audio_encoder)) != NULL)
2282 if (audio_encoder->codec == out_codec)
2284 i++;
2285 if (!(audio_encoder->muxers & muxer))
2286 out_codec = 0;
2288 else if (audio_encoder->codec == fallback)
2290 i++;
2291 if (!(audio_encoder->muxers & muxer))
2292 fallback = hb_audio_encoder_get_default(muxer);
2294 if (i > 1)
2296 break;
2299 return (out_codec & HB_ACODEC_PASS_MASK) ? out_codec : fallback;
2302 hb_container_t* hb_container_get_from_format(int format)
2304 int i;
2305 for (i = 0; i < hb_containers_count; i++)
2307 if (hb_containers[i].item.format == format)
2309 return &hb_containers[i].item;
2313 return NULL;
2316 int hb_container_get_from_name(const char *name)
2318 if (name == NULL || *name == '\0')
2319 goto fail;
2321 int i;
2322 for (i = 0; i < hb_containers_count; i++)
2324 if (!strcasecmp(hb_containers[i].item.name, name) ||
2325 !strcasecmp(hb_containers[i].item.short_name, name))
2327 return hb_containers[i].item.format;
2331 fail:
2332 return HB_MUX_INVALID;
2335 int hb_container_get_from_extension(const char *extension)
2337 if (extension == NULL || *extension == '\0')
2338 goto fail;
2340 int i;
2341 for (i = 0; i < hb_containers_count; i++)
2343 if (!strcasecmp(hb_containers[i].item.default_extension, extension))
2345 return hb_containers[i].item.format;
2349 fail:
2350 return HB_MUX_INVALID;
2353 const char* hb_container_get_name(int format)
2355 if (!(format & HB_MUX_MASK))
2356 goto fail;
2358 const hb_container_t *container = NULL;
2359 while ((container = hb_container_get_next(container)) != NULL)
2361 if (container->format == format)
2363 return container->name;
2367 fail:
2368 return NULL;
2371 const char* hb_container_get_short_name(int format)
2373 if (!(format & HB_MUX_MASK))
2374 goto fail;
2376 const hb_container_t *container = NULL;
2377 while ((container = hb_container_get_next(container)) != NULL)
2379 if (container->format == format)
2381 return container->short_name;
2385 fail:
2386 return NULL;
2389 const char* hb_container_get_long_name(int format)
2391 if (!(format & HB_MUX_MASK))
2392 goto fail;
2394 const hb_container_t *container = NULL;
2395 while ((container = hb_container_get_next(container)) != NULL)
2397 if (container->format == format)
2399 return container->long_name;
2403 fail:
2404 return NULL;
2407 const char* hb_container_get_default_extension(int format)
2409 if (!(format & HB_MUX_MASK))
2410 goto fail;
2412 const hb_container_t *container = NULL;
2413 while ((container = hb_container_get_next(container)) != NULL)
2415 if (container->format == format)
2417 return container->default_extension;
2421 fail:
2422 return NULL;
2425 const char* hb_container_sanitize_name(const char *name)
2427 return hb_container_get_name(hb_container_get_from_name(name));
2430 const hb_container_t* hb_container_get_next(const hb_container_t *last)
2432 if (last == NULL)
2434 return hb_containers_first_item;
2436 return ((hb_container_internal_t*)last)->next;
2439 /**********************************************************************
2440 * hb_reduce
2441 **********************************************************************
2442 * Given a numerator (num) and a denominator (den), reduce them to an
2443 * equivalent fraction and store the result in x and y.
2444 *********************************************************************/
2445 void hb_reduce( int *x, int *y, int num, int den )
2447 // find the greatest common divisor of num & den by Euclid's algorithm
2448 int n = num, d = den;
2449 while ( d )
2451 int t = d;
2452 d = n % d;
2453 n = t;
2456 // at this point n is the gcd. if it's non-zero remove it from num
2457 // and den. Otherwise just return the original values.
2458 if ( n )
2460 *x = num / n;
2461 *y = den / n;
2463 else
2465 *x = num;
2466 *y = den;
2470 void hb_limit_rational( int *x, int *y, int num, int den, int limit )
2472 hb_reduce( &num, &den, num, den );
2473 if ( num < limit && den < limit )
2475 *x = num;
2476 *y = den;
2477 return;
2480 if ( num > den )
2482 double div = (double)limit / num;
2483 num = limit;
2484 den *= div;
2486 else
2488 double div = (double)limit / den;
2489 den = limit;
2490 num *= div;
2492 *x = num;
2493 *y = den;
2496 /**********************************************************************
2497 * hb_reduce64
2498 **********************************************************************
2499 * Given a numerator (num) and a denominator (den), reduce them to an
2500 * equivalent fraction and store the result in x and y.
2501 *********************************************************************/
2502 void hb_reduce64( int64_t *x, int64_t *y, int64_t num, int64_t den )
2504 // find the greatest common divisor of num & den by Euclid's algorithm
2505 int64_t n = num, d = den;
2506 while ( d )
2508 int64_t t = d;
2509 d = n % d;
2510 n = t;
2513 // at this point n is the gcd. if it's non-zero remove it from num
2514 // and den. Otherwise just return the original values.
2515 if ( n )
2517 num /= n;
2518 den /= n;
2521 *x = num;
2522 *y = den;
2526 void hb_limit_rational64( int64_t *x, int64_t *y, int64_t num, int64_t den, int64_t limit )
2528 hb_reduce64( &num, &den, num, den );
2529 if ( num < limit && den < limit )
2531 *x = num;
2532 *y = den;
2533 return;
2536 if ( num > den )
2538 double div = (double)limit / num;
2539 num = limit;
2540 den *= div;
2542 else
2544 double div = (double)limit / den;
2545 den = limit;
2546 num *= div;
2548 *x = num;
2549 *y = den;
2552 /**********************************************************************
2553 * hb_list implementation
2554 **********************************************************************
2555 * Basic and slow, but enough for what we need
2556 *********************************************************************/
2558 #define HB_LIST_DEFAULT_SIZE 20
2560 struct hb_list_s
2562 /* Pointers to items in the list */
2563 void ** items;
2565 /* How many (void *) allocated in 'items' */
2566 int items_alloc;
2568 /* How many valid pointers in 'items' */
2569 int items_count;
2572 /**********************************************************************
2573 * hb_list_init
2574 **********************************************************************
2575 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
2576 *********************************************************************/
2577 hb_list_t * hb_list_init()
2579 hb_list_t * l;
2581 l = calloc( sizeof( hb_list_t ), 1 );
2582 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
2583 l->items_alloc = HB_LIST_DEFAULT_SIZE;
2585 return l;
2588 /**********************************************************************
2589 * hb_list_count
2590 **********************************************************************
2591 * Returns the number of items currently in the list
2592 *********************************************************************/
2593 int hb_list_count( const hb_list_t * l )
2595 return l->items_count;
2598 /**********************************************************************
2599 * hb_list_add
2600 **********************************************************************
2601 * Adds an item at the end of the list, making it bigger if necessary.
2602 * Can safely be called with a NULL pointer to add, it will be ignored.
2603 *********************************************************************/
2604 void hb_list_add( hb_list_t * l, void * p )
2606 if( !p )
2608 return;
2611 if( l->items_count == l->items_alloc )
2613 /* We need a bigger boat */
2614 l->items_alloc += HB_LIST_DEFAULT_SIZE;
2615 l->items = realloc( l->items,
2616 l->items_alloc * sizeof( void * ) );
2619 l->items[l->items_count] = p;
2620 (l->items_count)++;
2623 /**********************************************************************
2624 * hb_list_insert
2625 **********************************************************************
2626 * Adds an item at the specifiec position in the list, making it bigger
2627 * if necessary.
2628 * Can safely be called with a NULL pointer to add, it will be ignored.
2629 *********************************************************************/
2630 void hb_list_insert( hb_list_t * l, int pos, void * p )
2632 if( !p )
2634 return;
2637 if( l->items_count == l->items_alloc )
2639 /* We need a bigger boat */
2640 l->items_alloc += HB_LIST_DEFAULT_SIZE;
2641 l->items = realloc( l->items,
2642 l->items_alloc * sizeof( void * ) );
2645 if ( l->items_count != pos )
2647 /* Shift all items after it sizeof( void * ) bytes earlier */
2648 memmove( &l->items[pos+1], &l->items[pos],
2649 ( l->items_count - pos ) * sizeof( void * ) );
2653 l->items[pos] = p;
2654 (l->items_count)++;
2657 /**********************************************************************
2658 * hb_list_rem
2659 **********************************************************************
2660 * Remove an item from the list. Bad things will happen if called
2661 * with a NULL pointer or if the item is not in the list.
2662 *********************************************************************/
2663 void hb_list_rem( hb_list_t * l, void * p )
2665 int i;
2667 /* Find the item in the list */
2668 for( i = 0; i < l->items_count; i++ )
2670 if( l->items[i] == p )
2672 /* Shift all items after it sizeof( void * ) bytes earlier */
2673 memmove( &l->items[i], &l->items[i+1],
2674 ( l->items_count - i - 1 ) * sizeof( void * ) );
2676 (l->items_count)--;
2677 break;
2682 /**********************************************************************
2683 * hb_list_item
2684 **********************************************************************
2685 * Returns item at position i, or NULL if there are not that many
2686 * items in the list
2687 *********************************************************************/
2688 void * hb_list_item( const hb_list_t * l, int i )
2690 if( l == NULL || i < 0 || i >= l->items_count )
2692 return NULL;
2695 return l->items[i];
2698 /**********************************************************************
2699 * hb_list_bytes
2700 **********************************************************************
2701 * Assuming all items are of type hb_buffer_t, returns the total
2702 * number of bytes in the list
2703 *********************************************************************/
2704 int hb_list_bytes( hb_list_t * l )
2706 hb_buffer_t * buf;
2707 int ret;
2708 int i;
2710 ret = 0;
2711 for( i = 0; i < hb_list_count( l ); i++ )
2713 buf = hb_list_item( l, i );
2714 ret += buf->size - buf->offset;
2717 return ret;
2720 /**********************************************************************
2721 * hb_list_seebytes
2722 **********************************************************************
2723 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
2724 * the list to <dst>, keeping the list unmodified.
2725 *********************************************************************/
2726 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
2728 hb_buffer_t * buf;
2729 int copied;
2730 int copying;
2731 int i;
2733 for( i = 0, copied = 0; copied < size; i++ )
2735 buf = hb_list_item( l, i );
2736 copying = MIN( buf->size - buf->offset, size - copied );
2737 memcpy( &dst[copied], &buf->data[buf->offset], copying );
2738 copied += copying;
2742 /**********************************************************************
2743 * hb_list_getbytes
2744 **********************************************************************
2745 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
2746 * the list to <dst>. What's copied is removed from the list.
2747 * The variable pointed by <pts> is set to the PTS of the buffer the
2748 * first byte has been got from.
2749 * The variable pointed by <pos> is set to the position of that byte
2750 * in that buffer.
2751 *********************************************************************/
2752 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
2753 uint64_t * pts, uint64_t * pos )
2755 hb_buffer_t * buf;
2756 int copied;
2757 int copying;
2758 uint8_t has_pts;
2760 /* So we won't have to deal with NULL pointers */
2761 uint64_t dummy1, dummy2;
2763 if( !pts ) pts = &dummy1;
2764 if( !pos ) pos = &dummy2;
2766 for( copied = 0, has_pts = 0; copied < size; )
2768 buf = hb_list_item( l, 0 );
2769 copying = MIN( buf->size - buf->offset, size - copied );
2770 memcpy( &dst[copied], &buf->data[buf->offset], copying );
2772 if( !has_pts )
2774 *pts = buf->s.start;
2775 *pos = buf->offset;
2776 has_pts = 1;
2779 buf->offset += copying;
2780 if( buf->offset >= buf->size )
2782 hb_list_rem( l, buf );
2783 hb_buffer_close( &buf );
2786 copied += copying;
2790 /**********************************************************************
2791 * hb_list_empty
2792 **********************************************************************
2793 * Assuming all items are of type hb_buffer_t, close them all and
2794 * close the list.
2795 *********************************************************************/
2796 void hb_list_empty( hb_list_t ** _l )
2798 hb_list_t * l = *_l;
2799 hb_buffer_t * b;
2801 while( ( b = hb_list_item( l, 0 ) ) )
2803 hb_list_rem( l, b );
2804 hb_buffer_close( &b );
2807 hb_list_close( _l );
2810 /**********************************************************************
2811 * hb_list_close
2812 **********************************************************************
2813 * Free memory allocated by hb_list_init. Does NOT free contents of
2814 * items still in the list.
2815 *********************************************************************/
2816 void hb_list_close( hb_list_t ** _l )
2818 hb_list_t * l = *_l;
2820 if (l == NULL)
2821 return;
2823 free( l->items );
2824 free( l );
2826 *_l = NULL;
2829 int global_verbosity_level; //Necessary for hb_deep_log
2830 /**********************************************************************
2831 * hb_valog
2832 **********************************************************************
2833 * If verbose mode is one, print message with timestamp. Messages
2834 * longer than 180 characters are stripped ;p
2835 *********************************************************************/
2836 void hb_valog( hb_debug_level_t level, const char * prefix, const char * log, va_list args)
2838 char * string;
2839 time_t _now;
2840 struct tm * now;
2841 char preamble[362];
2843 if( global_verbosity_level < level )
2845 /* Hiding message */
2846 return;
2849 /* Get the time */
2850 _now = time( NULL );
2851 now = localtime( &_now );
2852 if ( prefix && *prefix )
2854 // limit the prefix length
2855 snprintf( preamble, 361, "[%02d:%02d:%02d] %s %s\n",
2856 now->tm_hour, now->tm_min, now->tm_sec, prefix, log );
2858 else
2860 snprintf( preamble, 361, "[%02d:%02d:%02d] %s\n",
2861 now->tm_hour, now->tm_min, now->tm_sec, log );
2864 string = hb_strdup_vaprintf(preamble, args);
2866 #ifdef SYS_MINGW
2867 wchar_t *wstring; /* 360 chars + \n + \0 */
2868 int len;
2870 len = strlen(string) + 1;
2871 wstring = malloc(2 * len);
2873 // Convert internal utf8 to "console output code page".
2875 // This is just bizarre windows behavior. You would expect that
2876 // printf would automatically convert a wide character string to
2877 // the current "console output code page" when using the "%ls" format
2878 // specifier. But it doesn't... so we must do it.
2879 if (!MultiByteToWideChar(CP_UTF8, 0, string, -1, wstring, len))
2881 free(string);
2882 free(wstring);
2883 return;
2885 free(string);
2886 string = malloc(2 * len);
2887 if (!WideCharToMultiByte(GetConsoleOutputCP(), 0, wstring, -1, string, len,
2888 NULL, NULL))
2890 free(string);
2891 free(wstring);
2892 return;
2894 free(wstring);
2895 #endif
2897 /* Print it */
2898 fprintf( stderr, "%s", string );
2899 free(string);
2902 /**********************************************************************
2903 * hb_log
2904 **********************************************************************
2905 * If verbose mode is one, print message with timestamp. Messages
2906 * longer than 180 characters are stripped ;p
2907 *********************************************************************/
2908 void hb_log( char * log, ... )
2910 va_list args;
2912 va_start( args, log );
2913 hb_valog( 0, NULL, log, args );
2914 va_end( args );
2917 /**********************************************************************
2918 * hb_deep_log
2919 **********************************************************************
2920 * If verbose mode is >= level, print message with timestamp. Messages
2921 * longer than 360 characters are stripped ;p
2922 *********************************************************************/
2923 void hb_deep_log( hb_debug_level_t level, char * log, ... )
2925 va_list args;
2927 va_start( args, log );
2928 hb_valog( level, NULL, log, args );
2929 va_end( args );
2932 /**********************************************************************
2933 * hb_error
2934 **********************************************************************
2935 * Using whatever output is available display this error.
2936 *********************************************************************/
2937 void hb_error( char * log, ... )
2939 char string[181]; /* 180 chars + \0 */
2940 char rep_string[181];
2941 static char last_string[181];
2942 static int last_error_count = 0;
2943 static uint64_t last_series_error_time = 0;
2944 static hb_lock_t *mutex = 0;
2945 va_list args;
2946 uint64_t time_now;
2948 /* Convert the message to a string */
2949 va_start( args, log );
2950 vsnprintf( string, 180, log, args );
2951 va_end( args );
2953 if( !mutex )
2955 mutex = hb_lock_init();
2958 hb_lock( mutex );
2960 time_now = hb_get_date();
2962 if( strcmp( string, last_string) == 0 )
2965 * The last error and this one are the same, don't log it
2966 * just count it instead, unless it was more than one second
2967 * ago.
2969 last_error_count++;
2970 if( last_series_error_time + ( 1000 * 1 ) > time_now )
2972 hb_unlock( mutex );
2973 return;
2978 * A new error, or the same one more than 10sec since the last one
2979 * did we have any of the same counted up?
2981 if( last_error_count > 0 )
2984 * Print out the last error to ensure context for the last
2985 * repeated message.
2987 if( error_handler )
2989 error_handler( last_string );
2990 } else {
2991 hb_log( "%s", last_string );
2994 if( last_error_count > 1 )
2997 * Only print out the repeat message for more than 2 of the
2998 * same, since we just printed out two of them already.
3000 snprintf( rep_string, 180, "Last error repeated %d times",
3001 last_error_count - 1 );
3003 if( error_handler )
3005 error_handler( rep_string );
3006 } else {
3007 hb_log( "%s", rep_string );
3011 last_error_count = 0;
3014 last_series_error_time = time_now;
3016 strcpy( last_string, string );
3019 * Got the error in a single string, send it off to be dispatched.
3021 if( error_handler )
3023 error_handler( string );
3024 } else {
3025 hb_log( "%s", string );
3028 hb_unlock( mutex );
3031 void hb_register_error_handler( hb_error_handler_t * handler )
3033 error_handler = handler;
3036 static void hb_update_str( char **dst, const char *src )
3038 if ( dst )
3040 free( *dst );
3041 *dst = NULL;
3042 if ( src )
3044 *dst = strdup( src );
3049 /**********************************************************************
3050 * hb_title_init
3051 **********************************************************************
3053 *********************************************************************/
3054 hb_title_t * hb_title_init( char * path, int index )
3056 hb_title_t * t;
3058 t = calloc( sizeof( hb_title_t ), 1 );
3060 t->index = index;
3061 t->playlist = -1;
3062 t->list_audio = hb_list_init();
3063 t->list_chapter = hb_list_init();
3064 t->list_subtitle = hb_list_init();
3065 t->list_attachment = hb_list_init();
3066 t->metadata = hb_metadata_init();
3067 strncat(t->path, path, sizeof(t->path) - 1);
3068 // default to decoding mpeg2
3069 t->video_id = 0xE0;
3070 t->video_codec = WORK_DECAVCODECV;
3071 t->video_codec_param = AV_CODEC_ID_MPEG2VIDEO;
3072 t->angle_count = 1;
3073 t->geometry.par.num = 1;
3074 t->geometry.par.den = 1;
3076 return t;
3079 /**********************************************************************
3080 * hb_title_close
3081 **********************************************************************
3083 *********************************************************************/
3084 void hb_title_close( hb_title_t ** _t )
3086 hb_title_t * t = *_t;
3087 hb_audio_t * audio;
3088 hb_chapter_t * chapter;
3089 hb_subtitle_t * subtitle;
3090 hb_attachment_t * attachment;
3092 while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
3094 hb_list_rem( t->list_chapter, chapter );
3095 hb_chapter_close( &chapter );
3097 hb_list_close( &t->list_chapter );
3099 while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
3101 hb_list_rem( t->list_audio, audio );
3102 hb_audio_close( &audio );
3104 hb_list_close( &t->list_audio );
3106 while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
3108 hb_list_rem( t->list_subtitle, subtitle );
3109 hb_subtitle_close( &subtitle );
3111 hb_list_close( &t->list_subtitle );
3113 while( ( attachment = hb_list_item( t->list_attachment, 0 ) ) )
3115 hb_list_rem( t->list_attachment, attachment );
3116 hb_attachment_close( &attachment );
3118 hb_list_close( &t->list_attachment );
3120 hb_metadata_close( &t->metadata );
3122 free( t->video_codec_name );
3123 free(t->container_name);
3125 free( t );
3126 *_t = NULL;
3129 static void job_setup(hb_job_t * job, hb_title_t * title)
3131 if ( job == NULL || title == NULL )
3132 return;
3134 job->title = title;
3136 /* Set defaults settings */
3137 job->chapter_start = 1;
3138 job->chapter_end = hb_list_count( title->list_chapter );
3139 job->list_chapter = hb_chapter_list_copy( title->list_chapter );
3141 /* Autocrop by default. Gnark gnark */
3142 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
3145 hb_geometry_t resultGeo, srcGeo;
3146 hb_geometry_settings_t uiGeo;
3148 srcGeo = title->geometry;
3150 memset(&uiGeo, 0, sizeof(uiGeo));
3151 memcpy(uiGeo.crop, title->crop, 4 * sizeof( int ));
3152 uiGeo.geometry.width = srcGeo.width - uiGeo.crop[2] - uiGeo.crop[3];
3153 uiGeo.geometry.height = srcGeo.height - uiGeo.crop[0] - uiGeo.crop[1];
3154 uiGeo.mode = HB_ANAMORPHIC_NONE;
3155 uiGeo.keep = HB_KEEP_DISPLAY_ASPECT;
3157 hb_set_anamorphic_size2(&srcGeo, &uiGeo, &resultGeo);
3158 job->width = resultGeo.width;
3159 job->height = resultGeo.height;
3160 job->par = resultGeo.par;
3162 job->vcodec = HB_VCODEC_FFMPEG_MPEG4;
3163 job->vquality = -1.0;
3164 job->vbitrate = 1000;
3165 job->twopass = 0;
3166 job->pass_id = HB_PASS_ENCODE;
3167 job->vrate = title->vrate;
3169 job->mux = HB_MUX_MP4;
3171 job->list_audio = hb_list_init();
3172 job->list_subtitle = hb_list_init();
3173 job->list_filter = hb_list_init();
3175 job->list_attachment = hb_attachment_list_copy( title->list_attachment );
3176 job->metadata = hb_metadata_copy( title->metadata );
3178 #ifdef USE_QSV
3179 job->qsv.enc_info.is_init_done = 0;
3180 job->qsv.async_depth = AV_QSV_ASYNC_DEPTH_DEFAULT;
3181 job->qsv.decode = !!(title->video_decode_support &
3182 HB_DECODE_SUPPORT_QSV);
3183 #endif
3186 static void job_clean( hb_job_t * job )
3188 if (job)
3190 hb_chapter_t *chapter;
3191 hb_audio_t *audio;
3192 hb_subtitle_t *subtitle;
3193 hb_filter_object_t *filter;
3194 hb_attachment_t *attachment;
3196 free((void*)job->json);
3197 job->json = NULL;
3198 free(job->encoder_preset);
3199 job->encoder_preset = NULL;
3200 free(job->encoder_tune);
3201 job->encoder_tune = NULL;
3202 free(job->encoder_options);
3203 job->encoder_options = NULL;
3204 free(job->encoder_profile);
3205 job->encoder_profile = NULL;
3206 free(job->encoder_level);
3207 job->encoder_level = NULL;
3208 free(job->file);
3209 job->file = NULL;
3211 // clean up chapter list
3212 while( ( chapter = hb_list_item( job->list_chapter, 0 ) ) )
3214 hb_list_rem( job->list_chapter, chapter );
3215 hb_chapter_close( &chapter );
3217 hb_list_close( &job->list_chapter );
3219 // clean up audio list
3220 while( ( audio = hb_list_item( job->list_audio, 0 ) ) )
3222 hb_list_rem( job->list_audio, audio );
3223 hb_audio_close( &audio );
3225 hb_list_close( &job->list_audio );
3227 // clean up subtitle list
3228 while( ( subtitle = hb_list_item( job->list_subtitle, 0 ) ) )
3230 hb_list_rem( job->list_subtitle, subtitle );
3231 hb_subtitle_close( &subtitle );
3233 hb_list_close( &job->list_subtitle );
3235 // clean up filter list
3236 while( ( filter = hb_list_item( job->list_filter, 0 ) ) )
3238 hb_list_rem( job->list_filter, filter );
3239 hb_filter_close( &filter );
3241 hb_list_close( &job->list_filter );
3243 // clean up attachment list
3244 while( ( attachment = hb_list_item( job->list_attachment, 0 ) ) )
3246 hb_list_rem( job->list_attachment, attachment );
3247 hb_attachment_close( &attachment );
3249 hb_list_close( &job->list_attachment );
3251 // clean up metadata
3252 hb_metadata_close( &job->metadata );
3256 hb_title_t * hb_find_title_by_index( hb_handle_t *h, int title_index )
3258 hb_title_set_t *title_set = hb_get_title_set( h );
3259 int ii;
3261 for (ii = 0; ii < hb_list_count(title_set->list_title); ii++)
3263 hb_title_t *title = hb_list_item(title_set->list_title, ii);
3264 if (title_index == title->index)
3266 return title;
3269 return NULL;
3273 * Create a pristine job structure from a title
3274 * title_index is 1 based
3276 hb_job_t * hb_job_init_by_index( hb_handle_t * h, int title_index )
3278 hb_title_t * title = hb_find_title_by_index(h, title_index);
3279 if (title == NULL)
3280 return NULL;
3281 return hb_job_init(title);
3284 hb_job_t * hb_job_init( hb_title_t * title )
3286 hb_job_t * job;
3288 if ( title == NULL )
3289 return NULL;
3291 job = calloc( sizeof( hb_job_t ), 1 );
3292 job_setup(job, title);
3294 return job;
3297 /**********************************************************************
3298 * hb_job_close
3299 **********************************************************************
3301 *********************************************************************/
3302 void hb_job_close( hb_job_t ** _j )
3304 if (_j && *_j)
3306 job_clean(*_j);
3307 free( *_j );
3308 _j = NULL;
3312 void hb_job_set_encoder_preset(hb_job_t *job, const char *preset)
3314 if (job != NULL)
3316 if (preset == NULL || preset[0] == 0)
3318 preset = NULL;
3320 hb_update_str(&job->encoder_preset, preset);
3324 void hb_job_set_encoder_tune(hb_job_t *job, const char *tune)
3326 if (job != NULL)
3328 if (tune == NULL || tune[0] == 0)
3330 tune = NULL;
3332 hb_update_str(&job->encoder_tune, tune);
3336 void hb_job_set_encoder_options(hb_job_t *job, const char *options)
3338 if (job != NULL)
3340 if (options == NULL || options[0] == 0)
3342 options = NULL;
3344 hb_update_str(&job->encoder_options, options);
3348 void hb_job_set_encoder_profile(hb_job_t *job, const char *profile)
3350 if (job != NULL)
3352 if (profile == NULL || profile[0] == 0)
3354 profile = NULL;
3356 hb_update_str(&job->encoder_profile, profile);
3360 void hb_job_set_encoder_level(hb_job_t *job, const char *level)
3362 if (job != NULL)
3364 if (level == NULL || level[0] == 0)
3366 level = NULL;
3368 hb_update_str(&job->encoder_level, level);
3372 void hb_job_set_file(hb_job_t *job, const char *file)
3374 if (job != NULL)
3376 hb_update_str(&job->file, file);
3380 hb_filter_object_t * hb_filter_copy( hb_filter_object_t * filter )
3382 if( filter == NULL )
3383 return NULL;
3385 hb_filter_object_t * filter_copy = malloc( sizeof( hb_filter_object_t ) );
3386 memcpy( filter_copy, filter, sizeof( hb_filter_object_t ) );
3387 if( filter->settings )
3388 filter_copy->settings = strdup( filter->settings );
3389 return filter_copy;
3392 /**********************************************************************
3393 * hb_filter_list_copy
3394 **********************************************************************
3396 *********************************************************************/
3397 hb_list_t *hb_filter_list_copy(const hb_list_t *src)
3399 hb_list_t *list = hb_list_init();
3400 hb_filter_object_t *filter = NULL;
3401 int i;
3403 if( src )
3405 for( i = 0; i < hb_list_count(src); i++ )
3407 if( ( filter = hb_list_item( src, i ) ) )
3409 hb_list_add( list, hb_filter_copy(filter) );
3413 return list;
3417 * Gets a filter object with the given type
3418 * @param filter_id The type of filter to get.
3419 * @returns The requested filter object.
3421 hb_filter_object_t * hb_filter_init( int filter_id )
3423 hb_filter_object_t * filter;
3424 switch( filter_id )
3426 case HB_FILTER_DETELECINE:
3427 filter = &hb_filter_detelecine;
3428 break;
3430 case HB_FILTER_DECOMB:
3431 filter = &hb_filter_decomb;
3432 break;
3434 case HB_FILTER_DEINTERLACE:
3435 filter = &hb_filter_deinterlace;
3436 break;
3438 case HB_FILTER_VFR:
3439 filter = &hb_filter_vfr;
3440 break;
3442 case HB_FILTER_DEBLOCK:
3443 filter = &hb_filter_deblock;
3444 break;
3446 case HB_FILTER_DENOISE:
3447 filter = &hb_filter_denoise;
3448 break;
3450 case HB_FILTER_NLMEANS:
3451 filter = &hb_filter_nlmeans;
3452 break;
3454 case HB_FILTER_RENDER_SUB:
3455 filter = &hb_filter_render_sub;
3456 break;
3458 case HB_FILTER_CROP_SCALE:
3459 filter = &hb_filter_crop_scale;
3460 break;
3462 case HB_FILTER_ROTATE:
3463 filter = &hb_filter_rotate;
3464 break;
3466 #ifdef USE_QSV
3467 case HB_FILTER_QSV:
3468 filter = &hb_filter_qsv;
3469 break;
3471 case HB_FILTER_QSV_PRE:
3472 filter = &hb_filter_qsv_pre;
3473 break;
3475 case HB_FILTER_QSV_POST:
3476 filter = &hb_filter_qsv_post;
3477 break;
3478 #endif
3480 default:
3481 filter = NULL;
3482 break;
3484 return hb_filter_copy( filter );
3487 /**********************************************************************
3488 * hb_filter_close
3489 **********************************************************************
3491 *********************************************************************/
3492 void hb_filter_close( hb_filter_object_t ** _f )
3494 hb_filter_object_t * f = *_f;
3496 if( f->settings )
3497 free( f->settings );
3499 free( f );
3500 *_f = NULL;
3503 /**********************************************************************
3504 * hb_chapter_copy
3505 **********************************************************************
3507 *********************************************************************/
3508 hb_chapter_t *hb_chapter_copy(const hb_chapter_t *src)
3510 hb_chapter_t *chap = NULL;
3512 if ( src )
3514 chap = calloc( 1, sizeof(*chap) );
3515 memcpy( chap, src, sizeof(*chap) );
3516 if ( src->title )
3518 chap->title = strdup( src->title );
3521 return chap;
3524 /**********************************************************************
3525 * hb_chapter_list_copy
3526 **********************************************************************
3528 *********************************************************************/
3529 hb_list_t *hb_chapter_list_copy(const hb_list_t *src)
3531 hb_list_t *list = hb_list_init();
3532 hb_chapter_t *chapter = NULL;
3533 int i;
3535 if( src )
3537 for( i = 0; i < hb_list_count(src); i++ )
3539 if( ( chapter = hb_list_item( src, i ) ) )
3541 hb_list_add( list, hb_chapter_copy(chapter) );
3545 return list;
3548 /**********************************************************************
3549 * hb_chapter_close
3550 **********************************************************************
3552 *********************************************************************/
3553 void hb_chapter_close(hb_chapter_t **chap)
3555 if ( chap && *chap )
3557 free((*chap)->title);
3558 free(*chap);
3559 *chap = NULL;
3563 /**********************************************************************
3564 * hb_chapter_set_title
3565 **********************************************************************
3567 *********************************************************************/
3568 void hb_chapter_set_title( hb_chapter_t *chapter, const char *title )
3570 if ( chapter )
3572 hb_update_str( &chapter->title, title );
3576 /**********************************************************************
3577 * hb_chapter_set_title_by_index
3578 **********************************************************************
3579 * Applies information from the given job to the official job instance.
3580 * @param job Handle to hb_job_t.
3581 * @param chapter The chapter to apply the name to (1-based).
3582 * @param titel to apply.
3583 *********************************************************************/
3584 void hb_chapter_set_title_by_index( hb_job_t * job, int chapter_index, const char * title )
3586 hb_chapter_t * chapter = hb_list_item( job->list_chapter, chapter_index - 1 );
3587 hb_chapter_set_title( chapter, title );
3590 /**********************************************************************
3591 * hb_audio_copy
3592 **********************************************************************
3594 *********************************************************************/
3595 hb_audio_t *hb_audio_copy(const hb_audio_t *src)
3597 hb_audio_t *audio = NULL;
3599 if( src )
3601 audio = calloc(1, sizeof(*audio));
3602 memcpy(audio, src, sizeof(*audio));
3603 if ( src->config.out.name )
3605 audio->config.out.name = strdup(src->config.out.name);
3608 return audio;
3611 /**********************************************************************
3612 * hb_audio_list_copy
3613 **********************************************************************
3615 *********************************************************************/
3616 hb_list_t *hb_audio_list_copy(const hb_list_t *src)
3618 hb_list_t *list = hb_list_init();
3619 hb_audio_t *audio = NULL;
3620 int i;
3622 if( src )
3624 for( i = 0; i < hb_list_count(src); i++ )
3626 if( ( audio = hb_list_item( src, i ) ) )
3628 hb_list_add( list, hb_audio_copy(audio) );
3632 return list;
3635 /**********************************************************************
3636 * hb_audio_close
3637 **********************************************************************
3639 *********************************************************************/
3640 void hb_audio_close( hb_audio_t **audio )
3642 if ( audio && *audio )
3644 free((*audio)->config.out.name);
3645 free(*audio);
3646 *audio = NULL;
3650 /**********************************************************************
3651 * hb_audio_new
3652 **********************************************************************
3654 *********************************************************************/
3655 void hb_audio_config_init(hb_audio_config_t * audiocfg)
3657 /* Set read-only paramaters to invalid values */
3658 audiocfg->in.codec = 0;
3659 audiocfg->in.codec_param = 0;
3660 audiocfg->in.reg_desc = 0;
3661 audiocfg->in.stream_type = 0;
3662 audiocfg->in.substream_type = 0;
3663 audiocfg->in.version = 0;
3664 audiocfg->in.flags = 0;
3665 audiocfg->in.mode = 0;
3666 audiocfg->in.samplerate = -1;
3667 audiocfg->in.sample_bit_depth = 0;
3668 audiocfg->in.samples_per_frame = -1;
3669 audiocfg->in.bitrate = -1;
3670 audiocfg->in.matrix_encoding = AV_MATRIX_ENCODING_NONE;
3671 audiocfg->in.channel_layout = 0;
3672 audiocfg->in.channel_map = NULL;
3673 audiocfg->lang.description[0] = 0;
3674 audiocfg->lang.simple[0] = 0;
3675 audiocfg->lang.iso639_2[0] = 0;
3677 /* Initalize some sensible defaults */
3678 audiocfg->in.track = audiocfg->out.track = 0;
3679 audiocfg->out.codec = hb_audio_encoder_get_default(HB_MUX_MP4); // default container
3680 audiocfg->out.samplerate = -1;
3681 audiocfg->out.samples_per_frame = -1;
3682 audiocfg->out.bitrate = -1;
3683 audiocfg->out.quality = HB_INVALID_AUDIO_QUALITY;
3684 audiocfg->out.compression_level = -1;
3685 audiocfg->out.mixdown = HB_INVALID_AMIXDOWN;
3686 audiocfg->out.dynamic_range_compression = 0;
3687 audiocfg->out.gain = 0;
3688 audiocfg->out.normalize_mix_level = 0;
3689 audiocfg->out.dither_method = hb_audio_dither_get_default();
3690 audiocfg->out.name = NULL;
3693 /**********************************************************************
3694 * hb_audio_add
3695 **********************************************************************
3697 *********************************************************************/
3698 int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg)
3700 hb_title_t *title = job->title;
3701 hb_audio_t *audio;
3703 audio = hb_audio_copy( hb_list_item( title->list_audio, audiocfg->in.track ) );
3704 if( audio == NULL )
3706 /* We fail! */
3707 return 0;
3710 if( (audiocfg->in.bitrate != -1) && (audiocfg->in.codec != 0xDEADBEEF) )
3712 /* This most likely means the client didn't call hb_audio_config_init
3713 * so bail. */
3714 return 0;
3717 /* Set the job's "in track" to the value passed in audiocfg.
3718 * HandBrakeCLI assumes this value is preserved in the jobs
3719 * audio list, but in.track in the title's audio list is not
3720 * required to be the same. */
3721 // "track" in title->list_audio is an index into the source's tracks.
3722 // "track" in job->list_audio is an index into title->list_audio
3723 audio->config.in.track = audiocfg->in.track;
3725 /* Really shouldn't ignore the passed out track, but there is currently no
3726 * way to handle duplicates or out-of-order track numbers. */
3727 audio->config.out.track = hb_list_count(job->list_audio) + 1;
3728 audio->config.out.codec = audiocfg->out.codec;
3729 if((audiocfg->out.codec & HB_ACODEC_PASS_FLAG) &&
3730 ((audiocfg->out.codec == HB_ACODEC_AUTO_PASS) ||
3731 (audiocfg->out.codec & audio->config.in.codec & HB_ACODEC_PASS_MASK)))
3733 /* Pass-through, copy from input. */
3734 audio->config.out.samplerate = audio->config.in.samplerate;
3735 audio->config.out.bitrate = audio->config.in.bitrate;
3736 audio->config.out.mixdown = HB_AMIXDOWN_NONE;
3737 audio->config.out.dynamic_range_compression = 0;
3738 audio->config.out.gain = 0;
3739 audio->config.out.normalize_mix_level = 0;
3740 audio->config.out.compression_level = -1;
3741 audio->config.out.quality = HB_INVALID_AUDIO_QUALITY;
3742 audio->config.out.dither_method = hb_audio_dither_get_default();
3744 else
3746 /* Non pass-through, use what is given. */
3747 audio->config.out.codec &= ~HB_ACODEC_PASS_FLAG;
3748 audio->config.out.samplerate = audiocfg->out.samplerate;
3749 audio->config.out.bitrate = audiocfg->out.bitrate;
3750 audio->config.out.compression_level = audiocfg->out.compression_level;
3751 audio->config.out.quality = audiocfg->out.quality;
3752 audio->config.out.dynamic_range_compression = audiocfg->out.dynamic_range_compression;
3753 audio->config.out.mixdown = audiocfg->out.mixdown;
3754 audio->config.out.gain = audiocfg->out.gain;
3755 audio->config.out.normalize_mix_level = audiocfg->out.normalize_mix_level;
3756 audio->config.out.dither_method = audiocfg->out.dither_method;
3758 if (audiocfg->out.name && *audiocfg->out.name)
3760 audio->config.out.name = strdup(audiocfg->out.name);
3763 hb_list_add(job->list_audio, audio);
3764 return 1;
3767 hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i)
3769 hb_audio_t *audio = NULL;
3771 if( (audio = hb_list_item(list, i)) )
3772 return &(audio->config);
3774 return NULL;
3777 /**********************************************************************
3778 * hb_subtitle_copy
3779 **********************************************************************
3781 *********************************************************************/
3782 hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
3784 hb_subtitle_t *subtitle = NULL;
3786 if( src )
3788 subtitle = calloc(1, sizeof(*subtitle));
3789 memcpy(subtitle, src, sizeof(*subtitle));
3790 if ( src->extradata )
3792 subtitle->extradata = malloc( src->extradata_size );
3793 memcpy( subtitle->extradata, src->extradata, src->extradata_size );
3796 return subtitle;
3799 /**********************************************************************
3800 * hb_subtitle_list_copy
3801 **********************************************************************
3803 *********************************************************************/
3804 hb_list_t *hb_subtitle_list_copy(const hb_list_t *src)
3806 hb_list_t *list = hb_list_init();
3807 hb_subtitle_t *subtitle = NULL;
3808 int i;
3810 if( src )
3812 for( i = 0; i < hb_list_count(src); i++ )
3814 if( ( subtitle = hb_list_item( src, i ) ) )
3816 hb_list_add( list, hb_subtitle_copy(subtitle) );
3820 return list;
3823 /**********************************************************************
3824 * hb_subtitle_close
3825 **********************************************************************
3827 *********************************************************************/
3828 void hb_subtitle_close( hb_subtitle_t **sub )
3830 if ( sub && *sub )
3832 free ((*sub)->extradata);
3833 free(*sub);
3834 *sub = NULL;
3838 /**********************************************************************
3839 * hb_subtitle_add
3840 **********************************************************************
3842 *********************************************************************/
3843 int hb_subtitle_add_ssa_header(hb_subtitle_t *subtitle, const char *font,
3844 int fs, int w, int h)
3846 // Free any pre-existing extradata
3847 free(subtitle->extradata);
3849 // SRT subtitles are represented internally as SSA
3850 // Create an SSA header
3851 const char * ssa_header =
3852 "[Script Info]\r\n"
3853 "ScriptType: v4.00+\r\n"
3854 "Collisions: Normal\r\n"
3855 "PlayResX: %d\r\n"
3856 "PlayResY: %d\r\n"
3857 "Timer: 100.0\r\n"
3858 "WrapStyle: 0\r\n"
3859 "\r\n"
3860 "[V4+ Styles]\r\n"
3861 "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\r\n"
3862 "Style: Default,%s,%d,&H00FFFFFF,&H00FFFFFF,&H000F0F0F,&H000F0F0F,0,0,0,0,100,100,0,0.00,1,2,3,2,20,20,20,0\r\n";
3864 subtitle->extradata = (uint8_t*)hb_strdup_printf(ssa_header, w, h, font, fs);
3865 if (subtitle->extradata == NULL)
3867 hb_error("hb_subtitle_add_ssa_header: malloc failed");
3868 return 0;
3870 subtitle->extradata_size = strlen((char*)subtitle->extradata) + 1;
3872 return 1;
3875 int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track)
3877 hb_title_t *title = job->title;
3878 hb_subtitle_t *subtitle;
3880 subtitle = hb_subtitle_copy( hb_list_item( title->list_subtitle, track ) );
3881 if( subtitle == NULL )
3883 /* We fail! */
3884 return 0;
3887 // "track" in title->list_audio is an index into the source's tracks.
3888 // "track" in job->list_audio is an index into title->list_audio
3889 subtitle->track = track;
3890 subtitle->config = *subtitlecfg;
3891 subtitle->out_track = hb_list_count(job->list_subtitle) + 1;
3892 hb_list_add(job->list_subtitle, subtitle);
3893 return 1;
3896 int hb_srt_add( const hb_job_t * job,
3897 const hb_subtitle_config_t * subtitlecfg,
3898 const char *lang )
3900 hb_subtitle_t *subtitle;
3901 iso639_lang_t *language = NULL;
3903 subtitle = calloc( 1, sizeof( *subtitle ) );
3904 if (subtitle == NULL)
3906 hb_error("hb_srt_add: malloc failed");
3907 return 0;
3910 subtitle->id = (hb_list_count(job->list_subtitle) << 8) | 0xFF;
3911 subtitle->format = TEXTSUB;
3912 subtitle->source = SRTSUB;
3913 subtitle->codec = WORK_DECSRTSUB;
3915 language = lang_for_code2(lang);
3916 if (language == NULL)
3918 hb_log("hb_srt_add: unknown language code (%s)", lang);
3919 language = lang_for_code2("und");
3921 strcpy(subtitle->lang, language->eng_name);
3922 strcpy(subtitle->iso639_2, language->iso639_2);
3924 subtitle->config = *subtitlecfg;
3925 hb_list_add(job->list_subtitle, subtitle);
3927 return 1;
3930 int hb_subtitle_can_force( int source )
3932 return source == VOBSUB || source == PGSSUB;
3935 int hb_subtitle_can_burn( int source )
3937 return source == VOBSUB || source == PGSSUB || source == SSASUB ||
3938 source == SRTSUB || source == CC608SUB || source == UTF8SUB ||
3939 source == TX3GSUB;
3942 int hb_subtitle_can_pass( int source, int mux )
3944 switch (mux)
3946 case HB_MUX_AV_MKV:
3947 switch( source )
3949 case PGSSUB:
3950 case VOBSUB:
3951 case SSASUB:
3952 case SRTSUB:
3953 case UTF8SUB:
3954 case TX3GSUB:
3955 case CC608SUB:
3956 case CC708SUB:
3957 return 1;
3959 default:
3960 return 0;
3961 } break;
3963 case HB_MUX_AV_MP4:
3964 switch( source )
3966 case VOBSUB:
3967 case SSASUB:
3968 case SRTSUB:
3969 case UTF8SUB:
3970 case TX3GSUB:
3971 case CC608SUB:
3972 case CC708SUB:
3973 return 1;
3975 default:
3976 return 0;
3977 } break;
3979 default:
3980 // Internal error. Should never get here.
3981 hb_error("internel error. Bad mux %d\n", mux);
3982 return 0;
3986 int hb_audio_can_apply_drc(uint32_t codec, uint32_t codec_param, int encoder)
3988 if (encoder & HB_ACODEC_PASS_FLAG)
3990 // can't apply DRC to passthrough audio
3991 return 0;
3993 else if (codec & HB_ACODEC_FF_MASK)
3995 return (codec_param == AV_CODEC_ID_AC3 ||
3996 codec_param == AV_CODEC_ID_EAC3);
3998 else if (codec == HB_ACODEC_AC3)
4000 return 1;
4002 else
4004 return 0;
4008 int hb_audio_can_apply_drc2(hb_handle_t *h, int title_idx, int audio_idx, int encoder)
4010 hb_title_t *title = hb_find_title_by_index(h, title_idx);
4011 if (title == NULL)
4012 return 0;
4014 hb_audio_t *audio = hb_list_item(title->list_audio, audio_idx);
4015 if (audio == NULL)
4016 return 0;
4018 return hb_audio_can_apply_drc(audio->config.in.codec,
4019 audio->config.in.codec_param, encoder);
4022 /**********************************************************************
4023 * hb_metadata_init
4024 **********************************************************************
4026 *********************************************************************/
4027 hb_metadata_t *hb_metadata_init()
4029 hb_metadata_t *metadata = calloc( 1, sizeof(*metadata) );
4030 return metadata;
4033 /**********************************************************************
4034 * hb_metadata_copy
4035 **********************************************************************
4037 *********************************************************************/
4038 hb_metadata_t *hb_metadata_copy( const hb_metadata_t *src )
4040 hb_metadata_t *metadata = NULL;
4042 if ( src )
4044 metadata = calloc( 1, sizeof(*metadata) );
4045 if ( src->name )
4047 metadata->name = strdup(src->name);
4049 if ( src->artist )
4051 metadata->artist = strdup(src->artist);
4053 if ( src->album_artist )
4055 metadata->album_artist = strdup(src->album_artist);
4057 if ( src->composer )
4059 metadata->composer = strdup(src->composer);
4061 if ( src->release_date )
4063 metadata->release_date = strdup(src->release_date);
4065 if ( src->comment )
4067 metadata->comment = strdup(src->comment);
4069 if ( src->album )
4071 metadata->album = strdup(src->album);
4073 if ( src->genre )
4075 metadata->genre = strdup(src->genre);
4077 if ( src->description )
4079 metadata->description = strdup(src->description);
4081 if ( src->long_description )
4083 metadata->long_description = strdup(src->long_description);
4085 if ( src->list_coverart )
4087 int ii;
4088 for ( ii = 0; ii < hb_list_count( src->list_coverart ); ii++ )
4090 hb_coverart_t *art = hb_list_item( src->list_coverart, ii );
4091 hb_metadata_add_coverart(
4092 metadata, art->data, art->size, art->type );
4096 return metadata;
4099 /**********************************************************************
4100 * hb_metadata_close
4101 **********************************************************************
4103 *********************************************************************/
4104 void hb_metadata_close( hb_metadata_t **_m )
4106 if ( _m && *_m )
4108 hb_metadata_t *m = *_m;
4109 hb_coverart_t *art;
4111 free( m->name );
4112 free( m->artist );
4113 free( m->composer );
4114 free( m->release_date );
4115 free( m->comment );
4116 free( m->album );
4117 free( m->album_artist );
4118 free( m->genre );
4119 free( m->description );
4120 free( m->long_description );
4122 if ( m->list_coverart )
4124 while( ( art = hb_list_item( m->list_coverart, 0 ) ) )
4126 hb_list_rem( m->list_coverart, art );
4127 free( art->data );
4128 free( art );
4130 hb_list_close( &m->list_coverart );
4133 free( m );
4134 *_m = NULL;
4138 /**********************************************************************
4139 * hb_metadata_set_*
4140 **********************************************************************
4142 *********************************************************************/
4143 void hb_metadata_set_name( hb_metadata_t *metadata, const char *name )
4145 if ( metadata )
4147 hb_update_str( &metadata->name, name );
4151 void hb_metadata_set_artist( hb_metadata_t *metadata, const char *artist )
4153 if ( metadata )
4155 hb_update_str( &metadata->artist, artist );
4159 void hb_metadata_set_composer( hb_metadata_t *metadata, const char *composer )
4161 if ( metadata )
4163 hb_update_str( &metadata->composer, composer );
4167 void hb_metadata_set_release_date( hb_metadata_t *metadata, const char *release_date )
4169 if ( metadata )
4171 hb_update_str( &metadata->release_date, release_date );
4175 void hb_metadata_set_comment( hb_metadata_t *metadata, const char *comment )
4177 if ( metadata )
4179 hb_update_str( &metadata->comment, comment );
4183 void hb_metadata_set_genre( hb_metadata_t *metadata, const char *genre )
4185 if ( metadata )
4187 hb_update_str( &metadata->genre, genre );
4191 void hb_metadata_set_album( hb_metadata_t *metadata, const char *album )
4193 if ( metadata )
4195 hb_update_str( &metadata->album, album );
4199 void hb_metadata_set_album_artist( hb_metadata_t *metadata, const char *album_artist )
4201 if ( metadata )
4203 hb_update_str( &metadata->album_artist, album_artist );
4207 void hb_metadata_set_description( hb_metadata_t *metadata, const char *description )
4209 if ( metadata )
4211 hb_update_str( &metadata->description, description );
4215 void hb_metadata_set_long_description( hb_metadata_t *metadata, const char *long_description )
4217 if ( metadata )
4219 hb_update_str( &metadata->long_description, long_description );
4223 void hb_metadata_add_coverart( hb_metadata_t *metadata, const uint8_t *data, int size, int type )
4225 if ( metadata )
4227 if ( metadata->list_coverart == NULL )
4229 metadata->list_coverart = hb_list_init();
4231 hb_coverart_t *art = calloc( 1, sizeof(hb_coverart_t) );
4232 art->data = malloc( size );
4233 memcpy( art->data, data, size );
4234 art->size = size;
4235 art->type = type;
4236 hb_list_add( metadata->list_coverart, art );
4240 void hb_metadata_rem_coverart( hb_metadata_t *metadata, int idx )
4242 if ( metadata )
4244 hb_coverart_t *art = hb_list_item( metadata->list_coverart, idx );
4245 if ( art )
4247 hb_list_rem( metadata->list_coverart, art );
4248 free( art->data );
4249 free( art );
4254 char * hb_strdup_vaprintf( const char * fmt, va_list args )
4256 int len;
4257 int size = 256;
4258 char * str;
4259 char * tmp;
4260 va_list copy;
4262 str = malloc( size );
4263 if ( str == NULL )
4264 return NULL;
4266 while (1)
4268 // vsnprintf modifies it's va_list. Since we may need to do this
4269 // more than once, use a copy of the va_list.
4270 va_copy(copy, args);
4272 /* Try to print in the allocated space. */
4273 len = vsnprintf( str, size, fmt, copy );
4275 /* If that worked, return the string. */
4276 if ( len > -1 && len < size )
4278 return str;
4281 /* Else try again with more space. */
4282 if ( len > -1 ) /* glibc 2.1 */
4283 size = len + 1; /* precisely what is needed */
4284 else /* glibc 2.0 */
4285 size *= 2; /* twice the old size */
4286 tmp = realloc( str, size );
4287 if ( tmp == NULL )
4289 free( str );
4290 return NULL;
4292 else
4293 str = tmp;
4296 return str;
4299 char * hb_strdup_printf( const char * fmt, ... )
4301 char * str;
4302 va_list args;
4304 va_start( args, fmt );
4305 str = hb_strdup_vaprintf( fmt, args );
4306 va_end( args );
4308 return str;
4311 char * hb_strncat_dup( const char * s1, const char * s2, size_t n )
4313 size_t len;
4314 char * str;
4316 len = 0;
4317 if( s1 )
4318 len += strlen( s1 );
4319 if( s2 )
4320 len += MAX( strlen( s2 ), n );
4321 if( !len )
4322 return NULL;
4324 str = malloc( len + 1 );
4325 if( !str )
4326 return NULL;
4328 if( s1 )
4329 strcpy( str, s1 );
4330 else
4331 strcpy( str, "" );
4332 strncat( str, s2, n );
4333 return str;
4336 /**********************************************************************
4337 * hb_attachment_copy
4338 **********************************************************************
4340 *********************************************************************/
4341 hb_attachment_t *hb_attachment_copy(const hb_attachment_t *src)
4343 hb_attachment_t *attachment = NULL;
4345 if( src )
4347 attachment = calloc(1, sizeof(*attachment));
4348 memcpy(attachment, src, sizeof(*attachment));
4349 if ( src->name )
4351 attachment->name = strdup( src->name );
4353 if ( src->data )
4355 attachment->data = malloc( src->size );
4356 memcpy( attachment->data, src->data, src->size );
4359 return attachment;
4362 /**********************************************************************
4363 * hb_attachment_list_copy
4364 **********************************************************************
4366 *********************************************************************/
4367 hb_list_t *hb_attachment_list_copy(const hb_list_t *src)
4369 hb_list_t *list = hb_list_init();
4370 hb_attachment_t *attachment = NULL;
4371 int i;
4373 if( src )
4375 for( i = 0; i < hb_list_count(src); i++ )
4377 if( ( attachment = hb_list_item( src, i ) ) )
4379 hb_list_add( list, hb_attachment_copy(attachment) );
4383 return list;
4386 /**********************************************************************
4387 * hb_attachment_close
4388 **********************************************************************
4390 *********************************************************************/
4391 void hb_attachment_close( hb_attachment_t **attachment )
4393 if ( attachment && *attachment )
4395 free((*attachment)->data);
4396 free((*attachment)->name);
4397 free(*attachment);
4398 *attachment = NULL;
4402 /**********************************************************************
4403 * hb_yuv2rgb
4404 **********************************************************************
4405 * Converts a YCrCb pixel to an RGB pixel.
4407 * This conversion is lossy (due to rounding and clamping).
4409 * Algorithm:
4410 * http://en.wikipedia.org/w/index.php?title=YCbCr&oldid=361987695#Technical_details
4411 *********************************************************************/
4412 int hb_yuv2rgb(int yuv)
4414 double y, Cr, Cb;
4415 int r, g, b;
4417 y = (yuv >> 16) & 0xff;
4418 Cr = (yuv >> 8) & 0xff;
4419 Cb = (yuv ) & 0xff;
4421 r = 1.164 * (y - 16) + 1.596 * (Cr - 128);
4422 g = 1.164 * (y - 16) - 0.392 * (Cb - 128) - 0.813 * (Cr - 128);
4423 b = 1.164 * (y - 16) + 2.017 * (Cb - 128);
4425 r = (r < 0) ? 0 : r;
4426 g = (g < 0) ? 0 : g;
4427 b = (b < 0) ? 0 : b;
4429 r = (r > 255) ? 255 : r;
4430 g = (g > 255) ? 255 : g;
4431 b = (b > 255) ? 255 : b;
4433 return (r << 16) | (g << 8) | b;
4436 /**********************************************************************
4437 * hb_rgb2yuv
4438 **********************************************************************
4439 * Converts an RGB pixel to a YCrCb pixel.
4441 * This conversion is lossy (due to rounding and clamping).
4443 * Algorithm:
4444 * http://en.wikipedia.org/w/index.php?title=YCbCr&oldid=361987695#Technical_details
4445 *********************************************************************/
4446 int hb_rgb2yuv(int rgb)
4448 double r, g, b;
4449 int y, Cr, Cb;
4451 r = (rgb >> 16) & 0xff;
4452 g = (rgb >> 8) & 0xff;
4453 b = (rgb ) & 0xff;
4455 y = 16. + ( 0.257 * r) + (0.504 * g) + (0.098 * b);
4456 Cb = 128. + (-0.148 * r) - (0.291 * g) + (0.439 * b);
4457 Cr = 128. + ( 0.439 * r) - (0.368 * g) - (0.071 * b);
4459 y = (y < 0) ? 0 : y;
4460 Cb = (Cb < 0) ? 0 : Cb;
4461 Cr = (Cr < 0) ? 0 : Cr;
4463 y = (y > 255) ? 255 : y;
4464 Cb = (Cb > 255) ? 255 : Cb;
4465 Cr = (Cr > 255) ? 255 : Cr;
4467 return (y << 16) | (Cr << 8) | Cb;
4470 const char * hb_subsource_name( int source )
4472 switch (source)
4474 case VOBSUB:
4475 return "VOBSUB";
4476 case SRTSUB:
4477 return "SRT";
4478 case CC608SUB:
4479 return "CC";
4480 case CC708SUB:
4481 return "CC";
4482 case UTF8SUB:
4483 return "UTF-8";
4484 case TX3GSUB:
4485 return "TX3G";
4486 case SSASUB:
4487 return "SSA";
4488 case PGSSUB:
4489 return "PGS";
4490 default:
4491 return "Unknown";
4495 void hb_hexdump( hb_debug_level_t level, const char * label, const uint8_t * data, int len )
4497 int ii;
4498 char line[80], ascii[19], *p;
4500 ascii[18] = 0;
4501 ascii[0] = '|';
4502 ascii[17] = '|';
4503 memset(&ascii[1], '.', 16);
4504 p = line;
4505 if( label )
4506 hb_deep_log(level, "++++ %s ++++", label);
4507 else
4508 hb_deep_log(level, "++++++++++++");
4509 for( ii = 0; ii < len; ii++ )
4511 if( ( ii & 0x0f ) == 0x0f )
4513 p += sprintf( p, "%02x", data[ii] );
4514 hb_deep_log( level, " %-50s%20s", line, ascii );
4515 memset(&ascii[1], '.', 16);
4516 p = line;
4518 else if( ( ii & 0x07 ) == 0x07 )
4520 p += sprintf( p, "%02x ", data[ii] );
4522 else
4524 p += sprintf( p, "%02x ", data[ii] );
4526 if( isgraph( data[ii] ) )
4527 ascii[(ii & 0x0f) + 1] = data[ii];
4528 else
4529 ascii[(ii & 0x0f) + 1] = '.';
4531 if( p != line )
4533 hb_deep_log( level, " %-50s%20s", line, ascii );