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
19 #include "h264_common.h"
20 #include "h265_common.h"
22 #include "qsv_common.h"
33 /**********************************************************************
35 *********************************************************************/
36 static hb_error_handler_t
*error_handler
= NULL
;
38 /* Generic IDs for encoders, containers, etc. */
41 HB_GID_NONE
= -1, // encoders must NEVER use it
50 HB_GID_ACODEC_AAC_PASS
,
52 HB_GID_ACODEC_AC3_PASS
,
53 HB_GID_ACODEC_AUTO_PASS
,
54 HB_GID_ACODEC_DTS_PASS
,
55 HB_GID_ACODEC_DTSHD_PASS
,
57 HB_GID_ACODEC_EAC3_PASS
,
59 HB_GID_ACODEC_FLAC_PASS
,
61 HB_GID_ACODEC_MP3_PASS
,
62 HB_GID_ACODEC_TRUEHD_PASS
,
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, },
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]);
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]);
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, },
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]);
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
, },
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
)
229 if (encoder
& HB_VCODEC_QSV_MASK
)
231 return hb_qsv_video_encoder_is_enabled(encoder
);
236 // the following encoders are always enabled
238 case HB_VCODEC_THEORA
:
239 case HB_VCODEC_FFMPEG_MPEG4
:
240 case HB_VCODEC_FFMPEG_MPEG2
:
241 case HB_VCODEC_FFMPEG_VP8
:
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
, },
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
, },
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
301 case HB_ACODEC_CA_AAC
:
302 case HB_ACODEC_CA_HAAC
:
307 case HB_ACODEC_FFAAC
:
308 return avcodec_find_encoder_by_name("aac") != NULL
;
311 case HB_ACODEC_FDK_AAC
:
312 case HB_ACODEC_FDK_HAAC
:
313 return avcodec_find_encoder_by_name("libfdk_aac") != NULL
;
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
327 case HB_ACODEC_VORBIS
:
338 hb_container_t
*next
;
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
, },
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
)
370 void hb_common_global_init()
372 static int common_init_done
= 0;
373 if (common_init_done
)
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
;
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)
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
;
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)
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
;
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)
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
;
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)
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
;
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)
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
;
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
;
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
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
;
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
;
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
;
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
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
;
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
;
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
;
596 ((hb_container_internal_t
*)hb_containers_last_item
)->next
=
597 &hb_containers
[i
].item
;
599 hb_containers_last_item
= &hb_containers
[i
].item
;
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
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
;
626 common_init_done
= 1;
629 int hb_video_framerate_get_from_name(const char *name
)
631 if (name
== NULL
|| *name
== '\0')
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
;
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
)
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
;
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
)
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
)
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;
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
)
706 best_samplerate
= audio_samplerate
->rate
;
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')
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
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
);
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
)
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
;
777 const hb_rate_t
* hb_audio_samplerate_get_next(const hb_rate_t
*last
)
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
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
)
800 closest_bitrate
= audio_bitrate
->rate
;
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
,
818 hb_audio_bitrate_get_limits(codec
, samplerate
, mixdown
, &low
, &high
);
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
))
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
);
840 case HB_ACODEC_FFFLAC
:
841 case HB_ACODEC_FFFLAC24
:
846 bitrate
= (nchannels
* 128) - (32 * (nchannels
< 5));
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));
857 case HB_ACODEC_CA_HAAC
:
858 case HB_ACODEC_FDK_HAAC
:
859 bitrate
= nchannels
* 32;
863 bitrate
= nchannels
* 80;
866 // sample_rate adjustment
867 bitrate
>>= sr_shift
;
868 return hb_audio_bitrate_get_best(codec
, bitrate
, samplerate
, mixdown
);
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 * --------------------------------------------------------------------------------------
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
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)
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)
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
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
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
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.
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.
965 * supported samplerates: 8 - 48 kHz
966 * libfdk limits the bitrate to the following values:
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
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
,
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.
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
);
1006 /* samplerate, 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
;
1017 // Bitrates don't apply to "lossless" audio
1018 case HB_ACODEC_FFFLAC
:
1019 case HB_ACODEC_FFFLAC24
:
1024 *low
= 224 * nchannels
/ 5;
1028 case HB_ACODEC_FFEAC3
:
1029 *low
= 224 * nchannels
/ 5;
1030 *high
= 128 * samplerate
/ 1000;
1033 case HB_ACODEC_CA_AAC
:
1038 *low
= nchannels
* 8;
1039 *high
= nchannels
* 24;
1044 *low
= nchannels
* 12;
1045 *high
= nchannels
* 32;
1049 *low
= nchannels
* 12;
1050 *high
= nchannels
* 48;
1055 *low
= nchannels
* 16;
1056 *high
= nchannels
* 64;
1060 *low
= nchannels
* 24;
1061 *high
= nchannels
* 96;
1067 *low
= nchannels
* 32;
1068 *high
= nchannels
* (160 + (96 * (nchannels
== 1)));
1073 case HB_ACODEC_CA_HAAC
:
1074 *low
= nchannels
* (12 + (4 * (samplerate
>= 44100)));
1075 *high
= nchannels
* 40;
1078 case HB_ACODEC_FDK_AAC
:
1079 *low
= nchannels
* samplerate
* 2 / 3000;
1080 *high
= nchannels
* samplerate
* 6 / 1000;
1083 case HB_ACODEC_FDK_HAAC
:
1084 *low
= (nchannels
* (12 + (4 * (samplerate
>= 44100))));
1085 *high
= (nchannels
- (nchannels
> 2)) * (48 +
1087 (samplerate
>= 22050)));
1090 case HB_ACODEC_FFAAC
:
1091 *low
= ((nchannels
+ lfe_count
) * 32);
1092 *high
= ((nchannels
+ lfe_count
) *
1093 ((192 + (64 * ((samplerate
<< sr_shift
) >= 44100)))
1097 case HB_ACODEC_LAME
:
1098 *low
= 8 + (24 * (sr_shift
< 1));
1099 *high
= 64 + (96 * (sr_shift
< 2)) + (160 * (sr_shift
< 1));
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)));
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.
1115 *low
= hb_audio_bitrates_first_item
->rate
;
1116 *high
= hb_audio_bitrates_last_item
->rate
;
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
)
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
)
1147 if (codec
& HB_VCODEC_QSV_MASK
)
1149 return hb_qsv_video_quality_get_limits(codec
, low
, high
, granularity
,
1156 case HB_VCODEC_X264
:
1158 case HB_VCODEC_X265
:
1166 case HB_VCODEC_THEORA
:
1173 case HB_VCODEC_FFMPEG_VP8
:
1180 case HB_VCODEC_FFMPEG_MPEG2
:
1181 case HB_VCODEC_FFMPEG_MPEG4
:
1191 const char* hb_video_quality_get_name(uint32_t codec
)
1194 if (codec
& HB_VCODEC_QSV_MASK
)
1196 return hb_qsv_video_quality_get_name(codec
);
1202 case HB_VCODEC_X264
:
1204 case HB_VCODEC_X265
:
1208 case HB_VCODEC_FFMPEG_VP8
:
1216 const char* const* hb_video_encoder_get_presets(int encoder
)
1219 if (encoder
& HB_VCODEC_QSV_MASK
)
1221 return hb_qsv_preset_get_names();
1227 case HB_VCODEC_X264
:
1228 return x264_preset_names
;
1231 case HB_VCODEC_X265
:
1232 return x265_preset_names
;
1239 const char* const* hb_video_encoder_get_tunes(int encoder
)
1243 case HB_VCODEC_X264
:
1244 return x264_tune_names
;
1247 case HB_VCODEC_X265
:
1248 return x265_tune_names
;
1255 const char* const* hb_video_encoder_get_profiles(int encoder
)
1258 if (encoder
& HB_VCODEC_QSV_MASK
)
1260 return hb_qsv_profile_get_names(encoder
);
1266 case HB_VCODEC_X264
:
1267 return hb_h264_profile_names
;
1269 case HB_VCODEC_X265
:
1270 return hb_h265_profile_names
;
1277 const char* const* hb_video_encoder_get_levels(int encoder
)
1280 if (encoder
& HB_VCODEC_QSV_MASK
)
1282 return hb_qsv_level_get_names(encoder
);
1288 case HB_VCODEC_X264
:
1289 return hb_h264_level_names
;
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
)
1308 case HB_ACODEC_FFAAC
:
1315 case HB_ACODEC_FDK_HAAC
:
1316 case HB_ACODEC_FDK_AAC
:
1323 case HB_ACODEC_LAME
:
1330 case HB_ACODEC_VORBIS
:
1337 case HB_ACODEC_CA_AAC
:
1347 *low
= *high
= HB_INVALID_AUDIO_QUALITY
;
1352 float hb_audio_quality_get_best(uint32_t codec
, float quality
)
1355 float low
, high
, granularity
;
1356 hb_audio_quality_get_limits(codec
, &low
, &high
, &granularity
, &direction
);
1364 float hb_audio_quality_get_default(uint32_t codec
)
1368 case HB_ACODEC_FFAAC
:
1371 case HB_ACODEC_FDK_HAAC
:
1372 case HB_ACODEC_FDK_AAC
:
1375 case HB_ACODEC_LAME
:
1378 case HB_ACODEC_VORBIS
:
1381 case HB_ACODEC_CA_AAC
:
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
)
1401 case HB_ACODEC_FFFLAC
:
1402 case HB_ACODEC_FFFLAC24
:
1409 case HB_ACODEC_LAME
:
1424 float hb_audio_compression_get_best(uint32_t codec
, float compression
)
1427 float low
, high
, granularity
;
1428 hb_audio_compression_get_limits(codec
, &low
, &high
, &granularity
, &direction
);
1429 if( compression
> high
)
1431 if( compression
< low
)
1436 float hb_audio_compression_get_default(uint32_t codec
)
1440 case HB_ACODEC_FFFLAC
:
1441 case HB_ACODEC_FFFLAC24
:
1444 case HB_ACODEC_LAME
:
1452 int hb_audio_dither_get_default()
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)
1472 case HB_ACODEC_FFFLAC
:
1473 case HB_ACODEC_FDK_AAC
:
1474 case HB_ACODEC_FDK_HAAC
:
1482 int hb_audio_dither_get_from_name(const char *name
)
1484 if (name
== NULL
|| *name
== '\0')
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
;
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
)
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
;
1520 const hb_dither_t
* hb_audio_dither_get_next(const hb_dither_t
*last
)
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
)
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
));
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.
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
);
1584 case HB_AMIXDOWN_7POINT1
:
1585 return ((layout
& AV_CH_LAYOUT_7POINT0
) == AV_CH_LAYOUT_7POINT0
);
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
:
1625 // unknown mixdown, should never happen
1631 int hb_mixdown_get_discrete_channel_count(int amixdown
)
1635 case HB_AMIXDOWN_5_2_LFE
:
1636 case HB_AMIXDOWN_7POINT1
:
1639 case HB_AMIXDOWN_6POINT1
:
1642 case HB_AMIXDOWN_5POINT1
:
1645 case HB_AMIXDOWN_MONO
:
1646 case HB_AMIXDOWN_LEFT
:
1647 case HB_AMIXDOWN_RIGHT
:
1650 case HB_AMIXDOWN_NONE
:
1658 int hb_mixdown_get_low_freq_channel_count(int amixdown
)
1662 case HB_AMIXDOWN_5POINT1
:
1663 case HB_AMIXDOWN_6POINT1
:
1664 case HB_AMIXDOWN_7POINT1
:
1665 case HB_AMIXDOWN_5_2_LFE
:
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
)
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
;
1705 // the (E-)AC-3 encoder defaults to the best mixdown up to 5.1
1707 case HB_ACODEC_FFEAC3
:
1708 mixdown
= HB_AMIXDOWN_5POINT1
;
1711 // other encoders default to the best mixdown up to DPLII
1713 mixdown
= HB_AMIXDOWN_DOLBYPLII
;
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
)
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
;
1735 int hb_mixdown_get_from_name(const char *name
)
1737 if (name
== NULL
|| *name
== '\0')
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
;
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
)
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
;
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
)
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
;
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
)
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
))
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
;
1821 return HB_VCODEC_INVALID
;
1824 hb_encoder_t
* hb_video_encoder_get_from_codec(int codec
)
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
;
1838 int hb_video_encoder_get_from_name(const char *name
)
1840 if (name
== NULL
|| *name
== '\0')
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
;
1854 return HB_VCODEC_INVALID
;
1857 const char* hb_video_encoder_get_name(int encoder
)
1859 if (!(encoder
& HB_VCODEC_MASK
))
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
;
1875 const char* hb_video_encoder_get_short_name(int encoder
)
1877 if (!(encoder
& HB_VCODEC_MASK
))
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
;
1893 const char* hb_video_encoder_get_long_name(int encoder
)
1895 if (!(encoder
& HB_VCODEC_MASK
))
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
;
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
)
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
)
1930 const hb_encoder_t
*audio_encoder
= NULL
;
1933 case HB_ACODEC_AAC_PASS
:
1934 gid
= HB_GID_ACODEC_AAC
;
1937 case HB_ACODEC_AC3_PASS
:
1938 gid
= HB_GID_ACODEC_AC3
;
1941 case HB_ACODEC_EAC3_PASS
:
1942 gid
= HB_GID_ACODEC_EAC3
;
1945 case HB_ACODEC_FLAC_PASS
:
1946 gid
= HB_GID_ACODEC_FLAC
;
1949 case HB_ACODEC_MP3_PASS
:
1950 gid
= HB_GID_ACODEC_MP3
;
1954 gid
= HB_GID_NONE
; // will never match an enabled encoder
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
))
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
;
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
;
2003 return HB_ACODEC_INVALID
;
2006 hb_encoder_t
* hb_audio_encoder_get_from_codec(int codec
)
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
;
2020 int hb_audio_encoder_get_from_name(const char *name
)
2022 if (name
== NULL
|| *name
== '\0')
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
;
2036 return HB_ACODEC_INVALID
;
2039 const char* hb_audio_encoder_get_name(int encoder
)
2041 if (!(encoder
& HB_ACODEC_ANY
))
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
;
2057 const char* hb_audio_encoder_get_short_name(int encoder
)
2059 if (!(encoder
& HB_ACODEC_ANY
))
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
;
2075 const char* hb_audio_encoder_get_long_name(int encoder
)
2077 if (!(encoder
& HB_ACODEC_ANY
))
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
;
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
)
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
)
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
,
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
);
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
);
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
);
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
,
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)
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
);
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
);
2199 audio
->config
.out
.compression_level
=
2200 hb_audio_compression_get_best(audio
->config
.out
.codec
,
2201 audio
->config
.out
.compression_level
);
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
);
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
)))
2240 tmp
= hb_strncat_dup(mask
, ", ", 2);
2247 // passthru name without " Passthru"
2248 tmp
= hb_strncat_dup(mask
, audio_encoder
->name
,
2249 strlen(audio_encoder
->name
) - 9);
2256 else if ((audio_encoder
->codec
& HB_ACODEC_PASS_FLAG
) == 0 &&
2257 (audio_encoder
->codec
== job
->acodec_fallback
))
2259 fallback
= audio_encoder
->name
;
2263 hb_log("Auto Passthru: no codecs allowed");
2265 hb_log("Auto Passthru: allowed codecs are %s", mask
);
2266 if (fallback
== NULL
)
2267 hb_log("Auto Passthru: no valid fallback specified");
2269 hb_log("Auto Passthru: fallback is %s", fallback
);
2272 int hb_autopassthru_get_encoder(int in_codec
, int copy_mask
, int fallback
,
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
)
2285 if (!(audio_encoder
->muxers
& muxer
))
2288 else if (audio_encoder
->codec
== fallback
)
2291 if (!(audio_encoder
->muxers
& muxer
))
2292 fallback
= hb_audio_encoder_get_default(muxer
);
2299 return (out_codec
& HB_ACODEC_PASS_MASK
) ? out_codec
: fallback
;
2302 hb_container_t
* hb_container_get_from_format(int format
)
2305 for (i
= 0; i
< hb_containers_count
; i
++)
2307 if (hb_containers
[i
].item
.format
== format
)
2309 return &hb_containers
[i
].item
;
2316 int hb_container_get_from_name(const char *name
)
2318 if (name
== NULL
|| *name
== '\0')
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
;
2332 return HB_MUX_INVALID
;
2335 int hb_container_get_from_extension(const char *extension
)
2337 if (extension
== NULL
|| *extension
== '\0')
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
;
2350 return HB_MUX_INVALID
;
2353 const char* hb_container_get_name(int format
)
2355 if (!(format
& HB_MUX_MASK
))
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
;
2371 const char* hb_container_get_short_name(int format
)
2373 if (!(format
& HB_MUX_MASK
))
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
;
2389 const char* hb_container_get_long_name(int format
)
2391 if (!(format
& HB_MUX_MASK
))
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
;
2407 const char* hb_container_get_default_extension(int format
)
2409 if (!(format
& HB_MUX_MASK
))
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
;
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
)
2434 return hb_containers_first_item
;
2436 return ((hb_container_internal_t
*)last
)->next
;
2439 /**********************************************************************
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
;
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.
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
)
2482 double div
= (double)limit
/ num
;
2488 double div
= (double)limit
/ den
;
2496 /**********************************************************************
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
;
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.
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
)
2538 double div
= (double)limit
/ num
;
2544 double div
= (double)limit
/ 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
2562 /* Pointers to items in the list */
2565 /* How many (void *) allocated in 'items' */
2568 /* How many valid pointers in 'items' */
2572 /**********************************************************************
2574 **********************************************************************
2575 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
2576 *********************************************************************/
2577 hb_list_t
* hb_list_init()
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
;
2588 /**********************************************************************
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 /**********************************************************************
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
)
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
;
2623 /**********************************************************************
2625 **********************************************************************
2626 * Adds an item at the specifiec position in the list, making it bigger
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
)
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 * ) );
2657 /**********************************************************************
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
)
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 * ) );
2682 /**********************************************************************
2684 **********************************************************************
2685 * Returns item at position i, or NULL if there are not that many
2687 *********************************************************************/
2688 void * hb_list_item( const hb_list_t
* l
, int i
)
2690 if( l
== NULL
|| i
< 0 || i
>= l
->items_count
)
2698 /**********************************************************************
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
)
2711 for( i
= 0; i
< hb_list_count( l
); i
++ )
2713 buf
= hb_list_item( l
, i
);
2714 ret
+= buf
->size
- buf
->offset
;
2720 /**********************************************************************
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
)
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
);
2742 /**********************************************************************
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
2751 *********************************************************************/
2752 void hb_list_getbytes( hb_list_t
* l
, uint8_t * dst
, int size
,
2753 uint64_t * pts
, uint64_t * pos
)
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
);
2774 *pts
= buf
->s
.start
;
2779 buf
->offset
+= copying
;
2780 if( buf
->offset
>= buf
->size
)
2782 hb_list_rem( l
, buf
);
2783 hb_buffer_close( &buf
);
2790 /**********************************************************************
2792 **********************************************************************
2793 * Assuming all items are of type hb_buffer_t, close them all and
2795 *********************************************************************/
2796 void hb_list_empty( hb_list_t
** _l
)
2798 hb_list_t
* l
= *_l
;
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 /**********************************************************************
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
;
2829 int global_verbosity_level
; //Necessary for hb_deep_log
2830 /**********************************************************************
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
)
2843 if( global_verbosity_level
< level
)
2845 /* Hiding message */
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
);
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
);
2867 wchar_t *wstring
; /* 360 chars + \n + \0 */
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
))
2886 string
= malloc(2 * len
);
2887 if (!WideCharToMultiByte(GetConsoleOutputCP(), 0, wstring
, -1, string
, len
,
2898 fprintf( stderr
, "%s", string
);
2902 /**********************************************************************
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
, ... )
2912 va_start( args
, log
);
2913 hb_valog( 0, NULL
, log
, args
);
2917 /**********************************************************************
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
, ... )
2927 va_start( args
, log
);
2928 hb_valog( level
, NULL
, log
, args
);
2932 /**********************************************************************
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;
2948 /* Convert the message to a string */
2949 va_start( args
, log
);
2950 vsnprintf( string
, 180, log
, args
);
2955 mutex
= hb_lock_init();
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
2970 if( last_series_error_time
+ ( 1000 * 1 ) > time_now
)
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
2989 error_handler( last_string
);
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 );
3005 error_handler( rep_string
);
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.
3023 error_handler( string
);
3025 hb_log( "%s", string
);
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
)
3044 *dst
= strdup( src
);
3049 /**********************************************************************
3051 **********************************************************************
3053 *********************************************************************/
3054 hb_title_t
* hb_title_init( char * path
, int index
)
3058 t
= calloc( sizeof( hb_title_t
), 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
3070 t
->video_codec
= WORK_DECAVCODECV
;
3071 t
->video_codec_param
= AV_CODEC_ID_MPEG2VIDEO
;
3073 t
->geometry
.par
.num
= 1;
3074 t
->geometry
.par
.den
= 1;
3079 /**********************************************************************
3081 **********************************************************************
3083 *********************************************************************/
3084 void hb_title_close( hb_title_t
** _t
)
3086 hb_title_t
* t
= *_t
;
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
);
3129 static void job_setup(hb_job_t
* job
, hb_title_t
* title
)
3131 if ( job
== NULL
|| title
== NULL
)
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;
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
);
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
);
3186 static void job_clean( hb_job_t
* job
)
3190 hb_chapter_t
*chapter
;
3192 hb_subtitle_t
*subtitle
;
3193 hb_filter_object_t
*filter
;
3194 hb_attachment_t
*attachment
;
3196 free((void*)job
->json
);
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
;
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
);
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
)
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
);
3281 return hb_job_init(title
);
3284 hb_job_t
* hb_job_init( hb_title_t
* title
)
3288 if ( title
== NULL
)
3291 job
= calloc( sizeof( hb_job_t
), 1 );
3292 job_setup(job
, title
);
3297 /**********************************************************************
3299 **********************************************************************
3301 *********************************************************************/
3302 void hb_job_close( hb_job_t
** _j
)
3312 void hb_job_set_encoder_preset(hb_job_t
*job
, const char *preset
)
3316 if (preset
== NULL
|| preset
[0] == 0)
3320 hb_update_str(&job
->encoder_preset
, preset
);
3324 void hb_job_set_encoder_tune(hb_job_t
*job
, const char *tune
)
3328 if (tune
== NULL
|| tune
[0] == 0)
3332 hb_update_str(&job
->encoder_tune
, tune
);
3336 void hb_job_set_encoder_options(hb_job_t
*job
, const char *options
)
3340 if (options
== NULL
|| options
[0] == 0)
3344 hb_update_str(&job
->encoder_options
, options
);
3348 void hb_job_set_encoder_profile(hb_job_t
*job
, const char *profile
)
3352 if (profile
== NULL
|| profile
[0] == 0)
3356 hb_update_str(&job
->encoder_profile
, profile
);
3360 void hb_job_set_encoder_level(hb_job_t
*job
, const char *level
)
3364 if (level
== NULL
|| level
[0] == 0)
3368 hb_update_str(&job
->encoder_level
, level
);
3372 void hb_job_set_file(hb_job_t
*job
, const char *file
)
3376 hb_update_str(&job
->file
, file
);
3380 hb_filter_object_t
* hb_filter_copy( hb_filter_object_t
* filter
)
3382 if( filter
== 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
);
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
;
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
) );
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
;
3426 case HB_FILTER_DETELECINE
:
3427 filter
= &hb_filter_detelecine
;
3430 case HB_FILTER_DECOMB
:
3431 filter
= &hb_filter_decomb
;
3434 case HB_FILTER_DEINTERLACE
:
3435 filter
= &hb_filter_deinterlace
;
3439 filter
= &hb_filter_vfr
;
3442 case HB_FILTER_DEBLOCK
:
3443 filter
= &hb_filter_deblock
;
3446 case HB_FILTER_DENOISE
:
3447 filter
= &hb_filter_denoise
;
3450 case HB_FILTER_NLMEANS
:
3451 filter
= &hb_filter_nlmeans
;
3454 case HB_FILTER_RENDER_SUB
:
3455 filter
= &hb_filter_render_sub
;
3458 case HB_FILTER_CROP_SCALE
:
3459 filter
= &hb_filter_crop_scale
;
3462 case HB_FILTER_ROTATE
:
3463 filter
= &hb_filter_rotate
;
3468 filter
= &hb_filter_qsv
;
3471 case HB_FILTER_QSV_PRE
:
3472 filter
= &hb_filter_qsv_pre
;
3475 case HB_FILTER_QSV_POST
:
3476 filter
= &hb_filter_qsv_post
;
3484 return hb_filter_copy( filter
);
3487 /**********************************************************************
3489 **********************************************************************
3491 *********************************************************************/
3492 void hb_filter_close( hb_filter_object_t
** _f
)
3494 hb_filter_object_t
* f
= *_f
;
3497 free( f
->settings
);
3503 /**********************************************************************
3505 **********************************************************************
3507 *********************************************************************/
3508 hb_chapter_t
*hb_chapter_copy(const hb_chapter_t
*src
)
3510 hb_chapter_t
*chap
= NULL
;
3514 chap
= calloc( 1, sizeof(*chap
) );
3515 memcpy( chap
, src
, sizeof(*chap
) );
3518 chap
->title
= strdup( src
->title
);
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
;
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
) );
3548 /**********************************************************************
3550 **********************************************************************
3552 *********************************************************************/
3553 void hb_chapter_close(hb_chapter_t
**chap
)
3555 if ( chap
&& *chap
)
3557 free((*chap
)->title
);
3563 /**********************************************************************
3564 * hb_chapter_set_title
3565 **********************************************************************
3567 *********************************************************************/
3568 void hb_chapter_set_title( hb_chapter_t
*chapter
, const char *title
)
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 /**********************************************************************
3592 **********************************************************************
3594 *********************************************************************/
3595 hb_audio_t
*hb_audio_copy(const hb_audio_t
*src
)
3597 hb_audio_t
*audio
= NULL
;
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
);
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
;
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
) );
3635 /**********************************************************************
3637 **********************************************************************
3639 *********************************************************************/
3640 void hb_audio_close( hb_audio_t
**audio
)
3642 if ( audio
&& *audio
)
3644 free((*audio
)->config
.out
.name
);
3650 /**********************************************************************
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 /**********************************************************************
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
;
3703 audio
= hb_audio_copy( hb_list_item( title
->list_audio
, audiocfg
->in
.track
) );
3710 if( (audiocfg
->in
.bitrate
!= -1) && (audiocfg
->in
.codec
!= 0xDEADBEEF) )
3712 /* This most likely means the client didn't call hb_audio_config_init
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();
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
);
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
);
3777 /**********************************************************************
3779 **********************************************************************
3781 *********************************************************************/
3782 hb_subtitle_t
*hb_subtitle_copy(const hb_subtitle_t
*src
)
3784 hb_subtitle_t
*subtitle
= NULL
;
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
);
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
;
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
) );
3823 /**********************************************************************
3825 **********************************************************************
3827 *********************************************************************/
3828 void hb_subtitle_close( hb_subtitle_t
**sub
)
3832 free ((*sub
)->extradata
);
3838 /**********************************************************************
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
=
3853 "ScriptType: v4.00+\r\n"
3854 "Collisions: Normal\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");
3870 subtitle
->extradata_size
= strlen((char*)subtitle
->extradata
) + 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
)
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
);
3896 int hb_srt_add( const hb_job_t
* job
,
3897 const hb_subtitle_config_t
* subtitlecfg
,
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");
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
);
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
||
3942 int hb_subtitle_can_pass( int source
, int mux
)
3980 // Internal error. Should never get here.
3981 hb_error("internel error. Bad mux %d\n", mux
);
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
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
)
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
);
4014 hb_audio_t
*audio
= hb_list_item(title
->list_audio
, audio_idx
);
4018 return hb_audio_can_apply_drc(audio
->config
.in
.codec
,
4019 audio
->config
.in
.codec_param
, encoder
);
4022 /**********************************************************************
4024 **********************************************************************
4026 *********************************************************************/
4027 hb_metadata_t
*hb_metadata_init()
4029 hb_metadata_t
*metadata
= calloc( 1, sizeof(*metadata
) );
4033 /**********************************************************************
4035 **********************************************************************
4037 *********************************************************************/
4038 hb_metadata_t
*hb_metadata_copy( const hb_metadata_t
*src
)
4040 hb_metadata_t
*metadata
= NULL
;
4044 metadata
= calloc( 1, sizeof(*metadata
) );
4047 metadata
->name
= strdup(src
->name
);
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
);
4067 metadata
->comment
= strdup(src
->comment
);
4071 metadata
->album
= strdup(src
->album
);
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
)
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
);
4099 /**********************************************************************
4101 **********************************************************************
4103 *********************************************************************/
4104 void hb_metadata_close( hb_metadata_t
**_m
)
4108 hb_metadata_t
*m
= *_m
;
4113 free( m
->composer
);
4114 free( m
->release_date
);
4117 free( m
->album_artist
);
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
);
4130 hb_list_close( &m
->list_coverart
);
4138 /**********************************************************************
4140 **********************************************************************
4142 *********************************************************************/
4143 void hb_metadata_set_name( hb_metadata_t
*metadata
, const char *name
)
4147 hb_update_str( &metadata
->name
, name
);
4151 void hb_metadata_set_artist( hb_metadata_t
*metadata
, const char *artist
)
4155 hb_update_str( &metadata
->artist
, artist
);
4159 void hb_metadata_set_composer( hb_metadata_t
*metadata
, const char *composer
)
4163 hb_update_str( &metadata
->composer
, composer
);
4167 void hb_metadata_set_release_date( hb_metadata_t
*metadata
, const char *release_date
)
4171 hb_update_str( &metadata
->release_date
, release_date
);
4175 void hb_metadata_set_comment( hb_metadata_t
*metadata
, const char *comment
)
4179 hb_update_str( &metadata
->comment
, comment
);
4183 void hb_metadata_set_genre( hb_metadata_t
*metadata
, const char *genre
)
4187 hb_update_str( &metadata
->genre
, genre
);
4191 void hb_metadata_set_album( hb_metadata_t
*metadata
, const char *album
)
4195 hb_update_str( &metadata
->album
, album
);
4199 void hb_metadata_set_album_artist( hb_metadata_t
*metadata
, const char *album_artist
)
4203 hb_update_str( &metadata
->album_artist
, album_artist
);
4207 void hb_metadata_set_description( hb_metadata_t
*metadata
, const char *description
)
4211 hb_update_str( &metadata
->description
, description
);
4215 void hb_metadata_set_long_description( hb_metadata_t
*metadata
, const char *long_description
)
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
)
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
);
4236 hb_list_add( metadata
->list_coverart
, art
);
4240 void hb_metadata_rem_coverart( hb_metadata_t
*metadata
, int idx
)
4244 hb_coverart_t
*art
= hb_list_item( metadata
->list_coverart
, idx
);
4247 hb_list_rem( metadata
->list_coverart
, art
);
4254 char * hb_strdup_vaprintf( const char * fmt
, va_list args
)
4262 str
= malloc( size
);
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
)
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
);
4299 char * hb_strdup_printf( const char * fmt
, ... )
4304 va_start( args
, fmt
);
4305 str
= hb_strdup_vaprintf( fmt
, args
);
4311 char * hb_strncat_dup( const char * s1
, const char * s2
, size_t n
)
4318 len
+= strlen( s1
);
4320 len
+= MAX( strlen( s2
), n
);
4324 str
= malloc( len
+ 1 );
4332 strncat( str
, s2
, n
);
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
;
4347 attachment
= calloc(1, sizeof(*attachment
));
4348 memcpy(attachment
, src
, sizeof(*attachment
));
4351 attachment
->name
= strdup( src
->name
);
4355 attachment
->data
= malloc( src
->size
);
4356 memcpy( attachment
->data
, src
->data
, src
->size
);
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
;
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
) );
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
);
4402 /**********************************************************************
4404 **********************************************************************
4405 * Converts a YCrCb pixel to an RGB pixel.
4407 * This conversion is lossy (due to rounding and clamping).
4410 * http://en.wikipedia.org/w/index.php?title=YCbCr&oldid=361987695#Technical_details
4411 *********************************************************************/
4412 int hb_yuv2rgb(int yuv
)
4417 y
= (yuv
>> 16) & 0xff;
4418 Cr
= (yuv
>> 8) & 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 /**********************************************************************
4438 **********************************************************************
4439 * Converts an RGB pixel to a YCrCb pixel.
4441 * This conversion is lossy (due to rounding and clamping).
4444 * http://en.wikipedia.org/w/index.php?title=YCbCr&oldid=361987695#Technical_details
4445 *********************************************************************/
4446 int hb_rgb2yuv(int rgb
)
4451 r
= (rgb
>> 16) & 0xff;
4452 g
= (rgb
>> 8) & 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
)
4495 void hb_hexdump( hb_debug_level_t level
, const char * label
, const uint8_t * data
, int len
)
4498 char line
[80], ascii
[19], *p
;
4503 memset(&ascii
[1], '.', 16);
4506 hb_deep_log(level
, "++++ %s ++++", label
);
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);
4518 else if( ( ii
& 0x07 ) == 0x07 )
4520 p
+= sprintf( p
, "%02x ", data
[ii
] );
4524 p
+= sprintf( p
, "%02x ", data
[ii
] );
4526 if( isgraph( data
[ii
] ) )
4527 ascii
[(ii
& 0x0f) + 1] = data
[ii
];
4529 ascii
[(ii
& 0x0f) + 1] = '.';
4533 hb_deep_log( level
, " %-50s%20s", line
, ascii
);