1 /***************************************************************************
4 * Fri Mar 28 10:38:44 2008
5 * Copyright 2008 John Stebbins
6 * <john at stebbins dot name>
7 ****************************************************************************/
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
28 #include "hbversion.h"
30 #include <glib/gstdio.h>
31 #include "hb-backend.h"
33 #include "callbacks.h"
38 const gchar *shortOpt;
52 static options_map_t d_container_opts[] =
54 {"MKV", "mkv", HB_MUX_MKV, 0.0, "mkv"},
55 {"MP4", "mp4", HB_MUX_MP4, 0.0, "mp4"},
56 {"M4V", "m4v", HB_MUX_MP4, 0.0, "m4v"},
57 {"AVI", "avi", HB_MUX_AVI, 0.0, "avi"},
58 {"OGM", "ogm", HB_MUX_OGM, 0.0, "ogm"},
60 combo_opts_t container_opts =
62 sizeof(d_container_opts)/sizeof(options_map_t),
66 static options_map_t d_deint_opts[] =
68 {"None", "none", 0, 0.0, ""},
69 {"Fast", "fast", 1, 0.0, "-1:-1:-1:0:1"},
70 {"Slow", "slow", 2, 0.0, "2:-1:-1:0:1"},
71 {"Slower", "slower", 3, 0.0, "0:-1:-1:0:1"},
73 combo_opts_t deint_opts =
75 sizeof(d_deint_opts)/sizeof(options_map_t),
79 static options_map_t d_denoise_opts[] =
81 {"None", "none", 0, 0.0, ""},
82 {"Weak", "weak", 1, 0.0, "2:1:2:3"},
83 {"Medium", "medium", 2, 0.0, "3:2:2:3"},
84 {"Strong", "strong", 3, 0.0, "7:7:5:5"},
86 combo_opts_t denoise_opts =
88 sizeof(d_denoise_opts)/sizeof(options_map_t),
92 static options_map_t d_vcodec_opts[] =
94 {"H.264 (x264)", "x264", HB_VCODEC_X264, 0.0, ""},
95 {"MPEG-4 (XviD)", "xvid", HB_VCODEC_XVID, 0.0, ""},
96 {"MPEG-4 (FFMPEG)", "ffmpeg", HB_VCODEC_FFMPEG, 0.0, ""},
97 {"Theora", "theora", HB_VCODEC_THEORA, 0.0, ""},
99 combo_opts_t vcodec_opts =
101 sizeof(d_vcodec_opts)/sizeof(options_map_t),
105 static options_map_t d_acodec_opts[] =
107 {"AAC (faac)", "faac", HB_ACODEC_FAAC, 0.0, ""},
108 {"MP3 (lame)", "lame", HB_ACODEC_LAME, 0.0, ""},
109 {"Vorbis", "vorbis", HB_ACODEC_VORBIS, 0.0, ""},
110 {"AC3 (pass-thru)", "ac3", HB_ACODEC_AC3, 0.0, ""},
112 combo_opts_t acodec_opts =
114 sizeof(d_acodec_opts)/sizeof(options_map_t),
118 static options_map_t d_pref_acodec_opts[] =
120 {"None", "none", 0, 0.0, ""},
121 {"AAC (faac)", "faac", HB_ACODEC_FAAC, 0.0, ""},
122 {"MP3 (lame)", "lame", HB_ACODEC_LAME, 0.0, ""},
123 {"Vorbis", "vorbis", HB_ACODEC_VORBIS, 0.0, ""},
124 {"AC3 (pass-thru)", "ac3", HB_ACODEC_AC3, 0.0, ""},
126 combo_opts_t pref_acodec_opts =
128 sizeof(d_pref_acodec_opts)/sizeof(options_map_t),
132 static options_map_t d_source_acodec_opts[] =
134 {"AC3", "ac3", HB_ACODEC_AC3, 0.0, ""},
135 {"DTS", "dca", HB_ACODEC_DCA, 0.0, ""},
136 {"MP2", "mpga", HB_ACODEC_MPGA, 0.0, ""},
137 {"LPCM", "lpcm", HB_ACODEC_LPCM, 0.0, ""},
139 combo_opts_t source_acodec_opts =
141 sizeof(d_source_acodec_opts)/sizeof(options_map_t),
145 static options_map_t d_direct_opts[] =
147 {"None", "none", 0, 0.0, "none"},
148 {"Spatial", "spatial", 1, 0.0, "spatial"},
149 {"Temporal", "temporal", 2, 0.0, "temporal"},
150 {"Automatic", "auto", 3, 0.0, "auto"},
152 combo_opts_t direct_opts =
154 sizeof(d_direct_opts)/sizeof(options_map_t),
158 static options_map_t d_me_opts[] =
160 {"Diamond", "dia", 0, 0.0, "dia"},
161 {"Hexagon", "hex", 1, 0.0, "hex"},
162 {"Uneven Multi-Hexagon", "umh", 2, 0.0, "umh"},
163 {"Exhaustive", "esa", 3, 0.0, "esa"},
165 combo_opts_t me_opts =
167 sizeof(d_me_opts)/sizeof(options_map_t),
171 static options_map_t d_subme_opts[] =
173 {"1", "1", 1, 0.0, "1"},
174 {"2", "2", 2, 0.0, "2"},
175 {"3", "3", 3, 0.0, "3"},
176 {"4", "4", 4, 0.0, "4"},
177 {"5", "5", 5, 0.0, "5"},
178 {"6", "6", 6, 0.0, "6"},
179 {"7", "7", 7, 0.0, "7"},
181 combo_opts_t subme_opts =
183 sizeof(d_subme_opts)/sizeof(options_map_t),
187 static options_map_t d_analyse_opts[] =
189 {"Some", "some", 0, 0.0, "some"},
190 {"None", "none", 1, 0.0, "none"},
191 {"All", "all", 2, 0.0, "all:8x8dct=1"},
193 combo_opts_t analyse_opts =
195 sizeof(d_analyse_opts)/sizeof(options_map_t),
199 static options_map_t d_trellis_opts[] =
201 {"Disabled", "off", 0, 0.0, ""},
202 {"Final Macro Block", "fmb", 1, 0.0, ""},
203 {"Always", "always", 2, 0.0, ""},
205 combo_opts_t trellis_opts =
207 sizeof(d_trellis_opts)/sizeof(options_map_t),
211 typedef struct iso639_lang_t
213 char * eng_name; /* Description in English */
214 char * native_name; /* Description in native language */
215 char * iso639_1; /* ISO-639-1 (2 characters) code */
216 char * iso639_2; /* ISO-639-2/t (3 character) code */
217 char * iso639_2b; /* ISO-639-2/b code (if different from above) */
220 static const iso639_lang_t language_table[] =
222 { "Any", "", "zz", "und" },
223 { "Afar", "", "aa", "aar" },
224 { "Abkhazian", "", "ab", "abk" },
225 { "Afrikaans", "", "af", "afr" },
226 { "Akan", "", "ak", "aka" },
227 { "Albanian", "", "sq", "sqi", "alb" },
228 { "Amharic", "", "am", "amh" },
229 { "Arabic", "", "ar", "ara" },
230 { "Aragonese", "", "an", "arg" },
231 { "Armenian", "", "hy", "hye", "arm" },
232 { "Assamese", "", "as", "asm" },
233 { "Avaric", "", "av", "ava" },
234 { "Avestan", "", "ae", "ave" },
235 { "Aymara", "", "ay", "aym" },
236 { "Azerbaijani", "", "az", "aze" },
237 { "Bashkir", "", "ba", "bak" },
238 { "Bambara", "", "bm", "bam" },
239 { "Basque", "", "eu", "eus", "baq" },
240 { "Belarusian", "", "be", "bel" },
241 { "Bengali", "", "bn", "ben" },
242 { "Bihari", "", "bh", "bih" },
243 { "Bislama", "", "bi", "bis" },
244 { "Bosnian", "", "bs", "bos" },
245 { "Breton", "", "br", "bre" },
246 { "Bulgarian", "", "bg", "bul" },
247 { "Burmese", "", "my", "mya", "bur" },
248 { "Catalan", "", "ca", "cat" },
249 { "Chamorro", "", "ch", "cha" },
250 { "Chechen", "", "ce", "che" },
251 { "Chinese", "", "zh", "zho", "chi" },
252 { "Church Slavic", "", "cu", "chu" },
253 { "Chuvash", "", "cv", "chv" },
254 { "Cornish", "", "kw", "cor" },
255 { "Corsican", "", "co", "cos" },
256 { "Cree", "", "cr", "cre" },
257 { "Czech", "", "cs", "ces", "cze" },
258 { "Danish", "Dansk", "da", "dan" },
259 { "Divehi", "", "dv", "div" },
260 { "Dutch", "Nederlands", "nl", "nld", "dut" },
261 { "Dzongkha", "", "dz", "dzo" },
262 { "English", "English", "en", "eng" },
263 { "Esperanto", "", "eo", "epo" },
264 { "Estonian", "", "et", "est" },
265 { "Ewe", "", "ee", "ewe" },
266 { "Faroese", "", "fo", "fao" },
267 { "Fijian", "", "fj", "fij" },
268 { "Finnish", "Suomi", "fi", "fin" },
269 { "French", "Francais", "fr", "fra", "fre" },
270 { "Western Frisian", "", "fy", "fry" },
271 { "Fulah", "", "ff", "ful" },
272 { "Georgian", "", "ka", "kat", "geo" },
273 { "German", "Deutsch", "de", "deu", "ger" },
274 { "Gaelic (Scots)", "", "gd", "gla" },
275 { "Irish", "", "ga", "gle" },
276 { "Galician", "", "gl", "glg" },
277 { "Manx", "", "gv", "glv" },
278 { "Greek, Modern", "", "el", "ell", "gre" },
279 { "Guarani", "", "gn", "grn" },
280 { "Gujarati", "", "gu", "guj" },
281 { "Haitian", "", "ht", "hat" },
282 { "Hausa", "", "ha", "hau" },
283 { "Hebrew", "", "he", "heb" },
284 { "Herero", "", "hz", "her" },
285 { "Hindi", "", "hi", "hin" },
286 { "Hiri Motu", "", "ho", "hmo" },
287 { "Hungarian", "Magyar", "hu", "hun" },
288 { "Igbo", "", "ig", "ibo" },
289 { "Icelandic", "Islenska", "is", "isl", "ice" },
290 { "Ido", "", "io", "ido" },
291 { "Sichuan Yi", "", "ii", "iii" },
292 { "Inuktitut", "", "iu", "iku" },
293 { "Interlingue", "", "ie", "ile" },
294 { "Interlingua", "", "ia", "ina" },
295 { "Indonesian", "", "id", "ind" },
296 { "Inupiaq", "", "ik", "ipk" },
297 { "Italian", "Italiano", "it", "ita" },
298 { "Javanese", "", "jv", "jav" },
299 { "Japanese", "", "ja", "jpn" },
300 { "Kalaallisut", "", "kl", "kal" },
301 { "Kannada", "", "kn", "kan" },
302 { "Kashmiri", "", "ks", "kas" },
303 { "Kanuri", "", "kr", "kau" },
304 { "Kazakh", "", "kk", "kaz" },
305 { "Central Khmer", "", "km", "khm" },
306 { "Kikuyu", "", "ki", "kik" },
307 { "Kinyarwanda", "", "rw", "kin" },
308 { "Kirghiz", "", "ky", "kir" },
309 { "Komi", "", "kv", "kom" },
310 { "Kongo", "", "kg", "kon" },
311 { "Korean", "", "ko", "kor" },
312 { "Kuanyama", "", "kj", "kua" },
313 { "Kurdish", "", "ku", "kur" },
314 { "Lao", "", "lo", "lao" },
315 { "Latin", "", "la", "lat" },
316 { "Latvian", "", "lv", "lav" },
317 { "Limburgan", "", "li", "lim" },
318 { "Lingala", "", "ln", "lin" },
319 { "Lithuanian", "", "lt", "lit" },
320 { "Luxembourgish", "", "lb", "ltz" },
321 { "Luba-Katanga", "", "lu", "lub" },
322 { "Ganda", "", "lg", "lug" },
323 { "Macedonian", "", "mk", "mkd", "mac" },
324 { "Marshallese", "", "mh", "mah" },
325 { "Malayalam", "", "ml", "mal" },
326 { "Maori", "", "mi", "mri", "mao" },
327 { "Marathi", "", "mr", "mar" },
328 { "Malay", "", "ms", "msa", "msa" },
329 { "Malagasy", "", "mg", "mlg" },
330 { "Maltese", "", "mt", "mlt" },
331 { "Moldavian", "", "mo", "mol" },
332 { "Mongolian", "", "mn", "mon" },
333 { "Nauru", "", "na", "nau" },
334 { "Navajo", "", "nv", "nav" },
335 { "Ndebele, South", "", "nr", "nbl" },
336 { "Ndebele, North", "", "nd", "nde" },
337 { "Ndonga", "", "ng", "ndo" },
338 { "Nepali", "", "ne", "nep" },
339 { "Norwegian Nynorsk", "", "nn", "nno" },
340 { "Norwegian Bokmål", "", "nb", "nob" },
341 { "Norwegian", "Norsk", "no", "nor" },
342 { "Chichewa; Nyanja", "", "ny", "nya" },
343 { "Occitan", "", "oc", "oci" },
344 { "Ojibwa", "", "oj", "oji" },
345 { "Oriya", "", "or", "ori" },
346 { "Oromo", "", "om", "orm" },
347 { "Ossetian", "", "os", "oss" },
348 { "Panjabi", "", "pa", "pan" },
349 { "Persian", "", "fa", "fas", "per" },
350 { "Pali", "", "pi", "pli" },
351 { "Polish", "", "pl", "pol" },
352 { "Portuguese", "Portugues", "pt", "por" },
353 { "Pushto", "", "ps", "pus" },
354 { "Quechua", "", "qu", "que" },
355 { "Romansh", "", "rm", "roh" },
356 { "Romanian", "", "ro", "ron", "rum" },
357 { "Rundi", "", "rn", "run" },
358 { "Russian", "", "ru", "rus" },
359 { "Sango", "", "sg", "sag" },
360 { "Sanskrit", "", "sa", "san" },
361 { "Serbian", "", "sr", "srp", "scc" },
362 { "Croatian", "Hrvatski", "hr", "hrv", "scr" },
363 { "Sinhala", "", "si", "sin" },
364 { "Slovak", "", "sk", "slk", "slo" },
365 { "Slovenian", "", "sl", "slv" },
366 { "Northern Sami", "", "se", "sme" },
367 { "Samoan", "", "sm", "smo" },
368 { "Shona", "", "sn", "sna" },
369 { "Sindhi", "", "sd", "snd" },
370 { "Somali", "", "so", "som" },
371 { "Sotho, Southern", "", "st", "sot" },
372 { "Spanish", "Espanol", "es", "spa" },
373 { "Sardinian", "", "sc", "srd" },
374 { "Swati", "", "ss", "ssw" },
375 { "Sundanese", "", "su", "sun" },
376 { "Swahili", "", "sw", "swa" },
377 { "Swedish", "Svenska", "sv", "swe" },
378 { "Tahitian", "", "ty", "tah" },
379 { "Tamil", "", "ta", "tam" },
380 { "Tatar", "", "tt", "tat" },
381 { "Telugu", "", "te", "tel" },
382 { "Tajik", "", "tg", "tgk" },
383 { "Tagalog", "", "tl", "tgl" },
384 { "Thai", "", "th", "tha" },
385 { "Tibetan", "", "bo", "bod", "tib" },
386 { "Tigrinya", "", "ti", "tir" },
387 { "Tonga", "", "to", "ton" },
388 { "Tswana", "", "tn", "tsn" },
389 { "Tsonga", "", "ts", "tso" },
390 { "Turkmen", "", "tk", "tuk" },
391 { "Turkish", "", "tr", "tur" },
392 { "Twi", "", "tw", "twi" },
393 { "Uighur", "", "ug", "uig" },
394 { "Ukrainian", "", "uk", "ukr" },
395 { "Urdu", "", "ur", "urd" },
396 { "Uzbek", "", "uz", "uzb" },
397 { "Venda", "", "ve", "ven" },
398 { "Vietnamese", "", "vi", "vie" },
399 { "Volapük", "", "vo", "vol" },
400 { "Welsh", "", "cy", "cym", "wel" },
401 { "Walloon", "", "wa", "wln" },
402 { "Wolof", "", "wo", "wol" },
403 { "Xhosa", "", "xh", "xho" },
404 { "Yiddish", "", "yi", "yid" },
405 { "Yoruba", "", "yo", "yor" },
406 { "Zhuang", "", "za", "zha" },
407 { "Zulu", "", "zu", "zul" },
409 #define LANG_TABLE_SIZE (sizeof(language_table)/ sizeof(iso639_lang_t))
412 del_tree(const gchar *name, gboolean del_top)
416 if (g_file_test(name, G_FILE_TEST_IS_DIR))
418 GDir *gdir = g_dir_open(name, 0, NULL);
419 file = g_dir_read_name(gdir);
423 path = g_strdup_printf("%s/%s", name, file);
424 del_tree(path, TRUE);
426 file = g_dir_read_name(gdir);
444 ghb_vquality_range(signal_user_data_t *ud, gint *min, gint *max)
446 if (ghb_settings_get_bool(ud->settings, "directqp"))
448 gint vcodec = ghb_settings_get_int(ud->settings, "video_codec");
449 // Only x264 and ffmpeg currently support direct qp/crf entry
450 if (vcodec == HB_VCODEC_X264)
455 else if (vcodec == HB_VCODEC_FFMPEG)
473 static setting_value_t*
474 get_acodec_value(gint val)
476 setting_value_t *value = NULL;
479 for (ii = 0; ii < acodec_opts.count; ii++)
481 if (acodec_opts.map[ii].ivalue == val)
483 value = g_malloc(sizeof(setting_value_t));
484 value->option = g_strdup(acodec_opts.map[ii].option);
485 value->shortOpt = g_strdup(acodec_opts.map[ii].shortOpt);
486 value->svalue = g_strdup(acodec_opts.map[ii].svalue);
488 value->ivalue = acodec_opts.map[ii].ivalue;
489 value->dvalue = acodec_opts.map[ii].dvalue;
496 static setting_value_t*
497 get_amix_value(gint val)
499 setting_value_t *value = NULL;
502 for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
504 if (hb_audio_mixdowns[ii].amixdown == val)
506 value = g_malloc(sizeof(setting_value_t));
507 value->option = g_strdup(hb_audio_mixdowns[ii].human_readable_name);
508 value->shortOpt = g_strdup(hb_audio_mixdowns[ii].short_name);
509 value->svalue = g_strdup(hb_audio_mixdowns[ii].internal_name);
511 value->ivalue = hb_audio_mixdowns[ii].amixdown;
512 value->dvalue = hb_audio_mixdowns[ii].amixdown;
519 // Handle for libhb. Gets set by ghb_backend_init()
520 static hb_handle_t * h = NULL;
522 extern void hb_get_tempory_directory(hb_handle_t *h, char path[512]);
525 ghb_hb_cleanup(gboolean partial)
529 hb_get_tempory_directory(h, dir);
530 del_tree(dir, !partial);
533 static hb_audio_config_t*
534 get_hb_audio(gint titleindex, gint track)
538 hb_audio_config_t *audio = NULL;
540 if (h == NULL) return NULL;
541 list = hb_get_titles( h );
542 if( !hb_list_count( list ) )
544 /* No valid title, stop right there */
547 title = hb_list_item( list, titleindex );
548 if (title == NULL) return NULL; // Bad titleindex
549 if (!hb_list_count(title->list_audio))
553 audio = (hb_audio_config_t *)hb_list_audio_config_item(title->list_audio, track);
558 search_rates(hb_rate_t *rates, gint rate, gint count)
561 for (ii = 0; ii < count; ii++)
563 if (rates[ii].rate == rate)
571 ghb_get_rate_string(gint rate, gint type)
579 index = search_rates(hb_video_rates, rate, hb_video_rates_count);
580 if (index >= 0) return hb_video_rates[index].string;
582 case GHB_AUDIO_BITRATE:
585 index = search_rates(hb_audio_bitrates, rate, hb_audio_bitrates_count);
586 if (index >= 0) return hb_audio_bitrates[index].string;
588 case GHB_AUDIO_SAMPLERATE:
590 index = search_rates(hb_audio_rates, rate, hb_audio_rates_count);
591 if (index >= 0) return hb_audio_rates[index].string;
598 static gboolean find_combo_item_by_int(GtkTreeModel *store, gint value, GtkTreeIter *iter);
601 get_combo_box_store(GtkBuilder *builder, const gchar *name)
606 g_debug("get_combo_box_store() %s\n", name);
607 // First modify the combobox model to allow greying out of options
608 combo = GTK_COMBO_BOX(GHB_WIDGET(builder, name));
609 store = GTK_LIST_STORE(gtk_combo_box_get_model (combo));
614 grey_combo_box_item(GtkBuilder *builder, const gchar *name, gint value, gboolean grey)
619 store = get_combo_box_store(builder, name);
620 if (find_combo_item_by_int(GTK_TREE_MODEL(store), value, &iter))
622 gtk_list_store_set(store, &iter,
629 ghb_grey_combo_options(GtkBuilder *builder)
632 gint container, track, titleindex, acodec;
633 hb_audio_config_t *audio = NULL;
635 widget = GHB_WIDGET (builder, "title");
636 titleindex = ghb_widget_int(widget);
637 widget = GHB_WIDGET (builder, "audio_track");
638 track = ghb_widget_int(widget);
639 audio = get_hb_audio(titleindex, track);
641 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_FAAC, FALSE);
642 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_FAAC, FALSE);
643 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_FAAC, FALSE);
644 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_LAME, FALSE);
645 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_LAME, FALSE);
646 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_LAME, FALSE);
647 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_VORBIS, FALSE);
648 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_VORBIS, FALSE);
649 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_VORBIS, FALSE);
650 if (audio && audio->in.codec != HB_ACODEC_AC3)
652 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_AC3, TRUE);
656 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_AC3, FALSE);
658 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_AC3, FALSE);
659 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_AC3, FALSE);
660 grey_combo_box_item(builder, "video_codec", HB_VCODEC_THEORA, FALSE);
662 widget = GHB_WIDGET (builder, "audio_codec");
663 acodec = ghb_widget_int(widget);
664 if (acodec != HB_ACODEC_AC3)
666 grey_combo_box_item(builder, "audio_mix", 0, TRUE);
668 widget = GHB_WIDGET (builder, "container");
669 container = ghb_widget_int(widget);
670 if (container == HB_MUX_MP4)
672 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_LAME, TRUE);
673 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_LAME, TRUE);
674 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_LAME, TRUE);
675 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_VORBIS, TRUE);
676 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_VORBIS, TRUE);
677 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_VORBIS, TRUE);
678 grey_combo_box_item(builder, "video_codec", HB_VCODEC_THEORA, TRUE);
680 else if (container == HB_MUX_AVI)
682 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_FAAC, TRUE);
683 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_FAAC, TRUE);
684 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_FAAC, TRUE);
685 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_VORBIS, TRUE);
686 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_VORBIS, TRUE);
687 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_VORBIS, TRUE);
688 grey_combo_box_item(builder, "video_codec", HB_VCODEC_THEORA, TRUE);
690 else if (container == HB_MUX_OGM)
692 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_FAAC, TRUE);
693 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_FAAC, TRUE);
694 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_FAAC, TRUE);
695 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_AC3, TRUE);
696 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_AC3, TRUE);
697 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_AC3, TRUE);
700 gboolean allow_mono = TRUE;
701 gboolean allow_stereo = TRUE;
702 gboolean allow_dolby = TRUE;
703 gboolean allow_dpl2 = TRUE;
704 gboolean allow_6ch = TRUE;
708 (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
709 (acodec != HB_ACODEC_LAME);
710 gint layout = audio->in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
712 ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
714 (layout == HB_INPUT_CH_LAYOUT_3F1R) ||
715 (layout == HB_INPUT_CH_LAYOUT_3F2R) ||
716 (layout == HB_INPUT_CH_LAYOUT_DOLBY);
717 allow_dpl2 = (layout == HB_INPUT_CH_LAYOUT_3F2R);
719 (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
720 (acodec != HB_ACODEC_LAME) &&
721 (layout == HB_INPUT_CH_LAYOUT_3F2R) &&
722 (audio->in.channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE);
724 grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_MONO, !allow_mono);
725 grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_STEREO, !allow_stereo);
726 grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_DOLBY, !allow_dolby);
727 grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_DOLBYPLII, !allow_dpl2);
728 grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_6CH, !allow_6ch);
732 ghb_get_best_mix(gint titleindex, gint track, gint acodec, gint mix)
734 hb_audio_config_t *audio = NULL;
735 gboolean allow_mono = TRUE;
736 gboolean allow_stereo = TRUE;
737 gboolean allow_dolby = TRUE;
738 gboolean allow_dpl2 = TRUE;
739 gboolean allow_6ch = TRUE;
741 if (acodec & (HB_ACODEC_AC3 | HB_ACODEC_DCA))
743 // Audio codec pass-thru. No mixdown
746 audio = get_hb_audio(titleindex, track);
750 (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
751 (acodec != HB_ACODEC_LAME);
752 gint layout = audio->in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
754 ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
756 (layout == HB_INPUT_CH_LAYOUT_3F1R) ||
757 (layout == HB_INPUT_CH_LAYOUT_3F2R) ||
758 (layout == HB_INPUT_CH_LAYOUT_DOLBY);
759 allow_dpl2 = (layout == HB_INPUT_CH_LAYOUT_3F2R);
761 (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
762 (acodec != HB_ACODEC_LAME) &&
763 (layout == HB_INPUT_CH_LAYOUT_3F2R) &&
764 (audio->in.channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE);
766 gboolean greater = FALSE;
769 // If no mix is specified, select the best available.
770 mix = HB_AMIXDOWN_6CH;
772 if (mix == HB_AMIXDOWN_6CH)
775 if (allow_6ch) return HB_AMIXDOWN_6CH;
777 if (mix == HB_AMIXDOWN_DOLBYPLII || greater)
780 if (allow_dpl2) return HB_AMIXDOWN_DOLBYPLII;
782 if (mix == HB_AMIXDOWN_DOLBY || greater)
785 if (allow_dolby) return HB_AMIXDOWN_DOLBY;
787 if (mix == HB_AMIXDOWN_STEREO || greater)
790 if (allow_stereo) return HB_AMIXDOWN_STEREO;
792 if (mix == HB_AMIXDOWN_MONO || greater)
795 if (allow_mono) return HB_AMIXDOWN_MONO;
797 if (allow_stereo) return HB_AMIXDOWN_STEREO;
798 if (allow_dolby) return HB_AMIXDOWN_DOLBY;
799 if (allow_dpl2) return HB_AMIXDOWN_DOLBYPLII;
800 if (allow_6ch) return HB_AMIXDOWN_6CH;
804 // Set up the model for the combo box
806 init_combo_box(GtkBuilder *builder, const gchar *name)
810 GtkCellRenderer *cell;
812 g_debug("init_combo_box() %s\n", name);
813 // First modify the combobox model to allow greying out of options
814 combo = GTK_COMBO_BOX(GHB_WIDGET(builder, name));
816 // 1 - String to display
817 // 2 - bool indicating whether the entry is selectable (grey or not)
818 // 3 - String that is used for presets
819 // 4 - Int value determined by backend
820 // 5 - String value determined by backend
821 store = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_BOOLEAN,
822 G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
823 gtk_combo_box_set_model(combo, GTK_TREE_MODEL(store));
825 gtk_cell_layout_clear(GTK_CELL_LAYOUT(combo));
826 cell = GTK_CELL_RENDERER(gtk_cell_renderer_text_new());
827 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), cell, TRUE);
828 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), cell,
829 "text", 0, "sensitive", 1, NULL);
833 audio_bitrate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
839 g_debug("audio_bitrate_opts_set ()\n");
840 store = get_combo_box_store(builder, name);
841 gtk_list_store_clear(store);
842 for (ii = 0; ii < count; ii++)
844 gtk_list_store_append(store, &iter);
845 gtk_list_store_set(store, &iter,
849 3, rates[ii].rate * 1000,
856 audio_bitrate_opts_clean(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
861 gboolean done = FALSE;
862 gboolean changed = FALSE;
864 g_debug("audio_bitrate_opts_clean ()\n");
865 store = get_combo_box_store(builder, name);
866 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL(store), &iter))
870 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, 3, &ivalue, -1);
871 if (search_rates(rates, ivalue/1000, count) < 0)
873 done = !gtk_list_store_remove(store, &iter);
878 done = !gtk_tree_model_iter_next (GTK_TREE_MODEL(store), &iter);
886 audio_samplerate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
892 g_debug("audio_samplerate_opts_set ()\n");
893 store = get_combo_box_store(builder, name);
894 gtk_list_store_clear(store);
895 // Add an item for "Same As Source"
896 gtk_list_store_append(store, &iter);
897 gtk_list_store_set(store, &iter,
904 for (ii = 0; ii < count; ii++)
906 gtk_list_store_append(store, &iter);
907 gtk_list_store_set(store, &iter,
918 video_rate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
924 g_debug("video_rate_opts_set ()\n");
925 store = get_combo_box_store(builder, name);
926 gtk_list_store_clear(store);
927 // Add an item for "Same As Source"
928 gtk_list_store_append(store, &iter);
929 gtk_list_store_set(store, &iter,
936 for (ii = 0; ii < count; ii++)
940 if (strcmp(rates[ii].string, "23.976") == 0)
942 desc = "(NTSC Film)";
944 else if (strcmp(rates[ii].string, "25") == 0)
946 desc = "(PAL Film/Video)";
948 else if (strcmp(rates[ii].string, "29.97") == 0)
950 desc = "(NTSC Video)";
952 option = g_strdup_printf ("%s %s", rates[ii].string, desc);
953 gtk_list_store_append(store, &iter);
954 gtk_list_store_set(store, &iter,
966 mix_opts_set(GtkBuilder *builder, const gchar *name)
972 g_debug("audio_bitrate_opts_set ()\n");
973 store = get_combo_box_store(builder, name);
974 gtk_list_store_clear(store);
975 gtk_list_store_append(store, &iter);
976 gtk_list_store_set(store, &iter,
983 for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
985 gtk_list_store_append(store, &iter);
986 gtk_list_store_set(store, &iter,
987 0, hb_audio_mixdowns[ii].human_readable_name,
989 2, hb_audio_mixdowns[ii].short_name,
990 3, hb_audio_mixdowns[ii].amixdown,
991 4, hb_audio_mixdowns[ii].internal_name,
997 language_opts_set(GtkBuilder *builder, const gchar *name)
1000 GtkListStore *store;
1003 g_debug("audio_bitrate_opts_set ()\n");
1004 store = get_combo_box_store(builder, name);
1005 gtk_list_store_clear(store);
1006 for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
1008 gtk_list_store_append(store, &iter);
1009 gtk_list_store_set(store, &iter,
1010 0, language_table[ii].eng_name,
1012 2, language_table[ii].iso639_2,
1014 4, language_table[ii].iso639_1,
1020 title_opts_set(GtkBuilder *builder, const gchar *name)
1023 GtkListStore *store;
1029 g_debug("title_opts_set ()\n");
1030 store = get_combo_box_store(builder, name);
1031 gtk_list_store_clear(store);
1034 list = hb_get_titles( h );
1035 count = hb_list_count( list );
1036 if (count > 100) count = 100;
1040 // No titles. Fill in a default.
1041 gtk_list_store_append(store, &iter);
1042 gtk_list_store_set(store, &iter,
1051 for (ii = 0; ii < count; ii++)
1055 title = (hb_title_t*)hb_list_item(list, ii);
1056 option = g_strdup_printf ("%d - %02dh%02dm%02ds",
1057 title->index, title->hours, title->minutes, title->seconds);
1058 gtk_list_store_append(store, &iter);
1059 gtk_list_store_set(store, &iter,
1070 find_combo_item_by_int(GtkTreeModel *store, gint value, GtkTreeIter *iter)
1073 gboolean foundit = FALSE;
1075 if (gtk_tree_model_get_iter_first (store, iter))
1079 gtk_tree_model_get(store, iter, 3, &ivalue, -1);
1080 if (value == ivalue)
1085 } while (gtk_tree_model_iter_next (store, iter));
1091 audio_rate_opts_add(GtkBuilder *builder, const gchar *name, gint rate)
1094 GtkListStore *store;
1097 g_debug("audio_rate_opts_add ()\n");
1098 store = get_combo_box_store(builder, name);
1099 if (!find_combo_item_by_int(GTK_TREE_MODEL(store), rate, &iter))
1101 str = g_strdup_printf ("%.8g", (gdouble)rate/1000.0);
1102 gtk_list_store_append(store, &iter);
1103 gtk_list_store_set(store, &iter,
1117 audio_rate_opts_remove(GtkBuilder *builder, const gchar *name, gint rate)
1120 GtkListStore *store;
1122 g_debug("audio_rate_opts_remove ()\n");
1123 store = get_combo_box_store(builder, name);
1124 if (find_combo_item_by_int(GTK_TREE_MODEL(store), rate, &iter))
1126 gtk_list_store_remove (store, &iter);
1134 audio_track_opts_set(GtkBuilder *builder, const gchar *name, gint titleindex)
1137 GtkListStore *store;
1140 hb_audio_config_t * audio;
1144 g_debug("audio_track_opts_set ()\n");
1145 store = get_combo_box_store(builder, name);
1146 gtk_list_store_clear(store);
1149 list = hb_get_titles( h );
1150 title = (hb_title_t*)hb_list_item( list, titleindex );
1153 count = hb_list_count( title->list_audio );
1156 if (count > 10) count = 10;
1159 // No audio. set some default
1160 gtk_list_store_append(store, &iter);
1161 gtk_list_store_set(store, &iter,
1170 for (ii = 0; ii < count; ii++)
1172 audio = (hb_audio_config_t *) hb_list_audio_config_item( title->list_audio, ii );
1173 gtk_list_store_append(store, &iter);
1174 gtk_list_store_set(store, &iter,
1175 0, audio->lang.description,
1177 2, audio->lang.description,
1179 4, audio->lang.description,
1185 subtitle_opts_set(GtkBuilder *builder, const gchar *name, gint titleindex)
1188 GtkListStore *store;
1191 hb_subtitle_t * subtitle;
1195 g_debug("subtitle_opts_set ()\n");
1196 store = get_combo_box_store(builder, name);
1197 gtk_list_store_clear(store);
1200 list = hb_get_titles( h );
1201 title = (hb_title_t*)hb_list_item( list, titleindex );
1204 count = hb_list_count( title->list_subtitle );
1207 if (count > 10) count = 10;
1208 // Add options for "none" and "autoselect"
1209 gtk_list_store_append(store, &iter);
1210 gtk_list_store_set(store, &iter,
1217 gtk_list_store_append(store, &iter);
1218 gtk_list_store_set(store, &iter,
1225 for (ii = 0; ii < count; ii++)
1227 subtitle = (hb_subtitle_t *) hb_list_item( title->list_subtitle, ii );
1228 gtk_list_store_append(store, &iter);
1229 gtk_list_store_set(store, &iter,
1232 2, subtitle->iso639_2,
1234 4, subtitle->iso639_2,
1237 if (titleindex == -1)
1239 for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
1241 gtk_list_store_append(store, &iter);
1242 gtk_list_store_set(store, &iter,
1243 0, language_table[ii].eng_name,
1245 2, language_table[ii].iso639_2,
1247 4, language_table[ii].iso639_2,
1260 guint64 longest = 0;
1261 gint titleindex = 0;
1263 g_debug("ghb_longest_title ()\n");
1264 if (h == NULL) return 0;
1265 list = hb_get_titles( h );
1266 count = hb_list_count( list );
1267 if (count > 100) count = 100;
1268 for (ii = 0; ii < count; ii++)
1270 title = (hb_title_t*)hb_list_item(list, ii);
1271 if (title->duration > longest)
1274 longest = title->duration;
1281 ghb_find_audio_track(gint titleindex, const gchar *lang, gint acodec)
1285 hb_audio_config_t * audio;
1290 g_debug("find_audio_track ()\n");
1293 list = hb_get_titles( h );
1294 title = (hb_title_t*)hb_list_item( list, titleindex );
1297 count = hb_list_count( title->list_audio );
1300 if (count > 10) count = 10;
1301 for (ii = 0; ii < count; ii++)
1303 audio = (hb_audio_config_t*)hb_list_audio_config_item( title->list_audio, ii );
1304 if ((strcmp(lang, audio->lang.iso639_2) == 0) ||
1305 (strcmp(lang, "und") == 0))
1307 // Candidate track. Will use if no better match found
1309 if (audio->in.codec == acodec)
1320 generic_opts_set(GtkBuilder *builder, const gchar *name, combo_opts_t *opts)
1323 GtkListStore *store;
1326 g_debug("generic_opts_set ()\n");
1327 store = get_combo_box_store(builder, name);
1328 gtk_list_store_clear(store);
1329 for (ii = 0; ii < opts->count; ii++)
1331 gtk_list_store_append(store, &iter);
1332 gtk_list_store_set(store, &iter,
1333 0, opts->map[ii].option,
1335 2, opts->map[ii].shortOpt,
1336 3, opts->map[ii].ivalue,
1337 4, opts->map[ii].svalue,
1343 ghb_update_ui_combo_box(GtkBuilder *builder, const gchar *name, gint user_data, gboolean all)
1345 GtkComboBox *combo = NULL;
1347 gint handler_id = 0;
1349 g_debug("ghb_update_ui_combo_box() %s\n", name);
1352 // Clearing a combo box causes a rash of "changed" events, even when
1353 // the active item is -1 (inactive). To control things, I'm disabling
1354 // the event till things are settled down.
1355 combo = GTK_COMBO_BOX(GHB_WIDGET(builder, name));
1356 signal_id = g_signal_lookup("changed", GTK_TYPE_COMBO_BOX);
1359 // Valid signal id found. This should always succeed.
1360 handler_id = g_signal_handler_find ((gpointer)combo, G_SIGNAL_MATCH_ID,
1361 signal_id, 0, 0, 0, 0);
1364 // This should also always succeed
1365 g_signal_handler_block ((gpointer)combo, handler_id);
1369 if (all || strcmp(name, "audio_bitrate") == 0)
1370 audio_bitrate_opts_set(builder, "audio_bitrate", hb_audio_bitrates, hb_audio_bitrates_count);
1371 if (all || strcmp(name, "pref_audio_bitrate1") == 0)
1372 audio_bitrate_opts_set(builder, "pref_audio_bitrate1", hb_audio_bitrates, hb_audio_bitrates_count);
1373 if (all || strcmp(name, "pref_audio_bitrate2") == 0)
1374 audio_bitrate_opts_set(builder, "pref_audio_bitrate2", hb_audio_bitrates, hb_audio_bitrates_count);
1375 if (all || strcmp(name, "audio_sample_rate") == 0)
1376 audio_samplerate_opts_set(builder, "audio_sample_rate", hb_audio_rates, hb_audio_rates_count);
1377 if (all || strcmp(name, "pref_audio_rate1") == 0)
1378 audio_samplerate_opts_set(builder, "pref_audio_rate1", hb_audio_rates, hb_audio_rates_count);
1379 if (all || strcmp(name, "pref_audio_rate2") == 0)
1380 audio_samplerate_opts_set(builder, "pref_audio_rate2", hb_audio_rates, hb_audio_rates_count);
1381 if (all || strcmp(name, "framerate") == 0)
1382 video_rate_opts_set(builder, "framerate", hb_video_rates, hb_video_rates_count);
1383 if (all || strcmp(name, "audio_mix") == 0)
1384 mix_opts_set(builder, "audio_mix");
1385 if (all || strcmp(name, "pref_audio_mix1") == 0)
1386 mix_opts_set(builder, "pref_audio_mix1");
1387 if (all || strcmp(name, "pref_audio_mix2") == 0)
1388 mix_opts_set(builder, "pref_audio_mix2");
1389 if (all || strcmp(name, "pref_source_audio_lang") == 0)
1390 language_opts_set(builder, "pref_source_audio_lang");
1391 if (all || strcmp(name, "subtitle_lang") == 0)
1392 subtitle_opts_set(builder, "subtitle_lang", user_data);
1393 if (all || strcmp(name, "title") == 0)
1394 title_opts_set(builder, "title");
1395 if (all || strcmp(name, "audio_track") == 0)
1396 audio_track_opts_set(builder, "audio_track", user_data);
1397 if (all || strcmp(name, "container") == 0)
1398 generic_opts_set(builder, "container", &container_opts);
1399 if (all || strcmp(name, "deinterlace") == 0)
1400 generic_opts_set(builder, "deinterlace", &deint_opts);
1401 if (all || strcmp(name, "denoise") == 0)
1402 generic_opts_set(builder, "denoise", &denoise_opts);
1403 if (all || strcmp(name, "video_codec") == 0)
1404 generic_opts_set(builder, "video_codec", &vcodec_opts);
1405 if (all || strcmp(name, "audio_codec") == 0)
1406 generic_opts_set(builder, "audio_codec", &acodec_opts);
1407 if (all || strcmp(name, "pref_audio_codec1") == 0)
1408 generic_opts_set(builder, "pref_audio_codec1", &pref_acodec_opts);
1409 if (all || strcmp(name, "pref_audio_codec2") == 0)
1410 generic_opts_set(builder, "pref_audio_codec2", &pref_acodec_opts);
1411 if (all || strcmp(name, "pref_source_audio_codec") == 0)
1412 generic_opts_set(builder, "pref_source_audio_codec", &source_acodec_opts);
1413 if (all || strcmp(name, "x264_direct") == 0)
1414 generic_opts_set(builder, "x264_direct", &direct_opts);
1415 if (all || strcmp(name, "x264_me") == 0)
1416 generic_opts_set(builder, "x264_me", &me_opts);
1417 if (all || strcmp(name, "x264_subme") == 0)
1418 generic_opts_set(builder, "x264_subme", &subme_opts);
1419 if (all || strcmp(name, "x264_analyse") == 0)
1420 generic_opts_set(builder, "x264_analyse", &analyse_opts);
1421 if (all || strcmp(name, "x264_trellis") == 0)
1422 generic_opts_set(builder, "x264_trellis", &trellis_opts);
1425 g_signal_handler_unblock ((gpointer)combo, handler_id);
1430 init_ui_combo_boxes(GtkBuilder *builder)
1432 init_combo_box(builder, "audio_bitrate");
1433 init_combo_box(builder, "pref_audio_bitrate1");
1434 init_combo_box(builder, "pref_audio_bitrate2");
1435 init_combo_box(builder, "audio_sample_rate");
1436 init_combo_box(builder, "pref_audio_rate1");
1437 init_combo_box(builder, "pref_audio_rate2");
1438 init_combo_box(builder, "framerate");
1439 init_combo_box(builder, "audio_mix");
1440 init_combo_box(builder, "pref_audio_mix1");
1441 init_combo_box(builder, "pref_audio_mix2");
1442 init_combo_box(builder, "pref_source_audio_lang");
1443 init_combo_box(builder, "subtitle_lang");
1444 init_combo_box(builder, "title");
1445 init_combo_box(builder, "audio_track");
1446 init_combo_box(builder, "container");
1447 init_combo_box(builder, "deinterlace");
1448 init_combo_box(builder, "denoise");
1449 init_combo_box(builder, "video_codec");
1450 init_combo_box(builder, "audio_codec");
1451 init_combo_box(builder, "pref_audio_codec1");
1452 init_combo_box(builder, "pref_audio_codec2");
1453 init_combo_box(builder, "pref_source_audio_codec");
1454 init_combo_box(builder, "x264_direct");
1455 init_combo_box(builder, "x264_me");
1456 init_combo_box(builder, "x264_subme");
1457 init_combo_box(builder, "x264_analyse");
1458 init_combo_box(builder, "x264_trellis");
1461 static const char * turbo_opts =
1462 "ref=1:subme=1:me=dia:analyse=none:trellis=0:"
1463 "no-fast-pskip=0:8x8dct=0:weightb=0";
1465 // Construct the x264 options string
1466 // The result is allocated, so someone must free it at some point.
1468 ghb_build_x264opts_string(GHashTable *settings)
1470 GString *x264opts = g_string_new("");
1471 gint refs = ghb_settings_get_int(settings, "x264_refs");
1474 g_string_append_printf(x264opts, "ref=%d:", refs);
1478 if (ghb_settings_get_bool(settings, "x264_mixed_refs"))
1480 g_string_append(x264opts, "mixed-refs=1:");
1483 gint subme = ghb_settings_get_int(settings, "x264_subme");
1484 if (subme != 5) // 5 is default
1486 g_string_append_printf(x264opts, "subme=%d:", subme);
1488 gint bframes = ghb_settings_get_int(settings, "x264_bframes");
1491 g_string_append_printf(x264opts, "bframes=%d:", bframes);
1492 if (ghb_settings_get_bool(settings, "x264_weighted_bframes"))
1494 g_string_append(x264opts, "weightb=1:");
1498 if (ghb_settings_get_bool(settings, "x264_brdo"))
1500 g_string_append(x264opts, "brdo=1:");
1503 if (ghb_settings_get_bool(settings, "x264_bime"))
1505 g_string_append(x264opts, "bime=1:");
1510 if (ghb_settings_get_bool(settings, "x264_bpyramid"))
1512 g_string_append(x264opts, "b-pyramid=1:");
1515 if (ghb_settings_get_bool(settings, "x264_no_fast_pskip"))
1517 g_string_append(x264opts, "no-fast-pskip=1:");
1519 if (ghb_settings_get_bool(settings, "x264_no_dct_decimate"))
1521 g_string_append(x264opts, "no-dct-decimate=1:");
1523 if (!ghb_settings_get_bool(settings, "x264_cabac"))
1525 g_string_append(x264opts, "cabac=0:");
1529 gint trellis = ghb_settings_get_int(settings, "x264_trellis");
1530 if (trellis != 0); // != None
1532 g_string_append_printf(x264opts, "trellis=%d:", trellis);
1536 dba = ghb_settings_get_int(settings, "x264_deblock_alpha");
1537 dbb = ghb_settings_get_int(settings, "x264_deblock_beta");
1538 if (dba != 0 || dbb != 0)
1540 g_string_append_printf(x264opts, "deblock=%d,%d:", dba, dbb);
1542 const gchar *me = ghb_settings_get_string(settings, "x264_me");
1543 g_string_append_printf(x264opts, "me=%s:", me);
1544 gint analyse = ghb_settings_get_int(settings, "x264_analyse");
1545 if (analyse != 0) // != Some
1547 g_string_append_printf(x264opts, "analyse=%s:",
1548 ghb_settings_get_string(settings, "x264_analyse"));
1550 if (analyse != 1) // != none
1552 gint direct = ghb_settings_get_int(settings, "x264_direct");
1553 if (direct != 1) // !spatial
1555 g_string_append_printf(x264opts, "direct=%s:",
1556 ghb_settings_get_string(settings, "x264_direct"));
1559 g_string_append_printf(x264opts, "merange=%d:",
1560 ghb_settings_get_int(settings, "x264_merange"));
1562 const gchar *opts = ghb_settings_get_string(settings, "x264_options");
1563 if (opts != NULL && opts[0] != 0)
1565 g_string_append_printf(x264opts, "%s:", opts);
1567 // strip the trailing ":"
1570 result = g_string_free(x264opts, FALSE);
1571 len = strlen(result);
1572 if (len > 0) result[len - 1] = 0;
1577 ghb_get_chapters(gint titleindex)
1581 hb_chapter_t * chapter;
1583 gchar **result = NULL;
1585 g_debug("ghb_get_chapters (title = %d)\n", titleindex);
1586 if (h == NULL) return NULL;
1587 list = hb_get_titles( h );
1588 title = (hb_title_t*)hb_list_item( list, titleindex );
1589 if (title == NULL) return NULL;
1590 count = hb_list_count( title->list_chapter );
1591 result = g_malloc((count+1) * sizeof(gchar*));
1592 for (ii = 0; ii < count; ii++)
1594 chapter = hb_list_item(title->list_chapter, ii);
1595 if (chapter == NULL) break;
1596 if (chapter->title == NULL || chapter->title[0] == 0)
1597 result[ii] = g_strdup_printf ("Chapter %2d", ii);
1599 result[ii] = g_strdup(chapter->title);
1606 ghb_set_passthru_rate_opts(GtkBuilder *builder, gint bitrate)
1608 gboolean changed = FALSE;
1609 changed = audio_rate_opts_add(builder, "audio_bitrate", bitrate);
1614 ghb_set_default_rate_opts(GtkBuilder *builder)
1616 gboolean changed = FALSE;
1617 changed = audio_bitrate_opts_clean(builder, "audio_bitrate", hb_audio_bitrates, hb_audio_bitrates_count);
1622 ghb_backend_init(GtkBuilder *builder, gint debug, gint update)
1625 h = hb_init( debug, update );
1626 // Set up the list model for the combos
1627 init_ui_combo_boxes(builder);
1628 // Populate all the combos
1629 ghb_update_ui_combo_box(builder, NULL, 0, TRUE);
1633 ghb_backend_scan(const gchar *path, gint titleindex)
1635 hb_scan( h, path, titleindex );
1639 ghb_backend_events(signal_user_data_t *ud, gint *unique_id)
1643 GtkProgressBar *progress;
1645 if (h == NULL) return GHB_EVENT_NONE;
1646 hb_get_state( h, &s );
1647 *unique_id = s.param.working.sequence_id & 0xFFFFFF;
1648 progress = GTK_PROGRESS_BAR(GHB_WIDGET (ud->builder, "progressbar"));
1653 //fprintf( stderr, "HB Idle\n");
1656 #define p s.param.scanning
1657 case HB_STATE_SCANNING:
1659 status = g_strdup_printf ("Scanning title %d of %d...",
1660 p.title_cur, p.title_count );
1661 gtk_progress_bar_set_text (progress, status);
1663 gtk_progress_bar_set_fraction (progress, (gdouble)p.title_cur / p.title_count);
1664 /* Show what title is currently being scanned */
1668 case HB_STATE_SCANDONE:
1670 status = g_strdup_printf ("Scan done");
1671 gtk_progress_bar_set_text (progress, status);
1673 gtk_progress_bar_set_fraction (progress, 1.0);
1674 return GHB_EVENT_SCAN_DONE;
1678 #define p s.param.working
1679 case HB_STATE_WORKING:
1680 if( p.seconds > -1 )
1682 status= g_strdup_printf(
1683 "Encoding: task %d of %d, %.2f %%"
1684 " (%.2f fps, avg %.2f fps, ETA %02dh%02dm%02ds)",
1685 p.job_cur, p.job_count, 100.0 * p.progress,
1686 p.rate_cur, p.rate_avg, p.hours, p.minutes, p.seconds );
1690 status= g_strdup_printf("Encoding: task %d of %d, %.2f %%",
1691 p.job_cur, p.job_count, 100.0 * p.progress );
1693 gtk_progress_bar_set_text (progress, status);
1694 gtk_progress_bar_set_fraction (progress, p.progress);
1696 return GHB_EVENT_WORKING;
1700 case HB_STATE_PAUSED:
1701 status = g_strdup_printf ("Paused");
1702 gtk_progress_bar_set_text (progress, status);
1704 return GHB_EVENT_PAUSED;
1707 #define p s.param.muxing
1708 case HB_STATE_MUXING:
1710 gtk_progress_bar_set_text( progress, "Muxing: this may take awhile...");
1715 #define p s.param.workdone
1716 case HB_STATE_WORKDONE:
1720 // Delete all remaining jobs of this encode.
1721 // An encode can be composed of multiple associated jobs.
1722 // When a job is stopped, libhb removes it from the job list,
1723 // but does not remove other jobs that may be associated with it.
1724 // Associated jobs are taged in the sequence id.
1725 while (((job = hb_job(h, 0)) != NULL) && ((job->sequence_id >> 24) != 0) )
1731 gtk_progress_bar_set_text( progress, "Rip done!" );
1733 case HB_ERROR_CANCELED:
1734 gtk_progress_bar_set_text( progress, "Rip canceled." );
1735 return GHB_EVENT_WORK_CANCELED;
1738 gtk_progress_bar_set_text( progress, "Rip failed.");
1740 gtk_progress_bar_set_fraction (progress, 1.0);
1741 return GHB_EVENT_WORK_DONE;
1746 return GHB_EVENT_NONE;
1750 ghb_get_title_info(ghb_title_info_t *tinfo, gint titleindex)
1755 if (h == NULL) return FALSE;
1756 list = hb_get_titles( h );
1757 if( !hb_list_count( list ) )
1759 /* No valid title, stop right there */
1763 title = hb_list_item( list, titleindex );
1764 if (title == NULL) return FALSE; // Bad titleindex
1765 tinfo->width = title->width;
1766 tinfo->height = title->height;
1767 memcpy(tinfo->crop, title->crop, 4 * sizeof(int));
1768 // Don't allow crop to 0
1769 if (title->crop[0] + title->crop[1] >= title->height)
1770 title->crop[0] = title->crop[1] = 0;
1771 if (title->crop[2] + title->crop[3] >= title->width)
1772 title->crop[2] = title->crop[3] = 0;
1773 tinfo->num_chapters = hb_list_count(title->list_chapter);
1774 tinfo->rate_base = title->rate_base;
1775 tinfo->rate = title->rate;
1776 hb_reduce(&(tinfo->aspect_n), &(tinfo->aspect_d),
1777 title->width * title->pixel_aspect_width,
1778 title->height * title->pixel_aspect_height);
1779 tinfo->hours = title->hours;
1780 tinfo->minutes = title->minutes;
1781 tinfo->seconds = title->seconds;
1786 ghb_get_audio_info(ghb_audio_info_t *ainfo, gint titleindex, gint audioindex)
1788 hb_audio_config_t *audio;
1790 audio = get_hb_audio(titleindex, audioindex);
1791 if (audio == NULL) return FALSE; // Bad audioindex
1792 ainfo->codec = audio->in.codec;
1793 ainfo->bitrate = audio->in.bitrate;
1794 ainfo->samplerate = audio->in.samplerate;
1799 ghb_audio_is_passthru(gint acodec)
1801 g_debug("ghb_audio_is_passthru () \n");
1802 g_debug("acodec %d\n", acodec);
1803 return (acodec == HB_ACODEC_AC3);
1807 ghb_get_default_acodec()
1809 return HB_ACODEC_FAAC;
1813 ghb_set_scale(signal_user_data_t *ud, gint mode)
1818 GHashTable *settings = ud->settings;
1819 gboolean keep_aspect, round_dims, anamorphic;
1820 gboolean autocrop, autoscale, noscale;
1821 gint crop[4], width, height, par_width, par_height;
1822 gint crop_width, crop_height;
1823 gint aspect_n, aspect_d;
1824 gboolean keep_width = (mode == GHB_SCALE_KEEP_WIDTH);
1825 gboolean keep_height = (mode == GHB_SCALE_KEEP_HEIGHT);
1831 gint max_height = 0;
1833 g_debug("ghb_set_scale ()\n");
1835 if (h == NULL) return;
1836 list = hb_get_titles( h );
1837 if( !hb_list_count( list ) )
1839 /* No valid title, stop right there */
1842 gint titleindex = ghb_settings_get_index(settings, "title");
1843 title = hb_list_item( list, titleindex );
1844 if (title == NULL) return;
1846 if (job == NULL) return;
1848 // First configure widgets
1849 round_dims = ghb_settings_get_bool(ud->settings, "round_dimensions");
1850 anamorphic = ghb_settings_get_bool(ud->settings, "anamorphic");
1851 keep_aspect = ghb_settings_get_bool(ud->settings, "keep_aspect");
1852 autocrop = ghb_settings_get_bool(ud->settings, "autocrop");
1853 autoscale = ghb_settings_get_bool(ud->settings, "autoscale");
1854 // "Noscale" is a flag that says we prefer to crop extra to satisfy
1855 // alignment constraints rather than scaling to satisfy them.
1856 noscale = ghb_settings_get_bool(ud->settings, "noscale");
1857 // Align dimensions to either 16 or 2 pixels
1858 // The scaler crashes if the dimensions are not divisible by 2
1859 // x264 also will not accept dims that are not multiple of 2
1860 modshift = round_dims ? 4 : 1;
1861 modround = round_dims ? 8 : 1;
1865 keep_height = FALSE;
1867 if (anamorphic || keep_aspect)
1869 keep_height = FALSE;
1871 // Step needs to be at least 2 because odd widths cause scaler crash
1872 step = round_dims ? 16 : 2;
1873 widget = GHB_WIDGET (ud->builder, "scale_width");
1874 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), step, 16);
1875 widget = GHB_WIDGET (ud->builder, "scale_height");
1876 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), step, 16);
1879 ghb_title_info_t tinfo;
1881 if (ghb_get_title_info (&tinfo, titleindex))
1883 crop[0] = tinfo.crop[0];
1884 crop[1] = tinfo.crop[1];
1885 crop[2] = tinfo.crop[2];
1886 crop[3] = tinfo.crop[3];
1891 // Adjust the cropping to accomplish the desired width and height
1892 crop_width = tinfo.width - crop[2] - crop[3];
1893 crop_height = tinfo.height - crop[0] - crop[1];
1894 width = (crop_width >> modshift) << modshift;
1895 height = (crop_height >> modshift) << modshift;
1896 need1 = (crop_height - height) / 2;
1897 need2 = crop_height - height - need1;
1900 need1 = (crop_width - width) / 2;
1901 need2 = crop_width - width - need1;
1905 ghb_ui_update_int (ud, "crop_top", crop[0]);
1906 ghb_ui_update_int (ud, "crop_bottom", crop[1]);
1907 ghb_ui_update_int (ud, "crop_left", crop[2]);
1908 ghb_ui_update_int (ud, "crop_right", crop[3]);
1911 crop[0] = ghb_settings_get_int(ud->settings, "crop_top");
1912 crop[1] = ghb_settings_get_int(ud->settings, "crop_bottom");
1913 crop[2] = ghb_settings_get_int(ud->settings, "crop_left");
1914 crop[3] = ghb_settings_get_int(ud->settings, "crop_right");
1915 hb_reduce(&aspect_n, &aspect_d,
1916 title->width * title->pixel_aspect_width,
1917 title->height * title->pixel_aspect_height);
1918 crop_width = title->width - crop[2] - crop[3];
1919 crop_height = title->height - crop[0] - crop[1];
1923 height = crop_height;
1924 max_width = crop_width;
1925 max_height = crop_height;
1929 width = ghb_settings_get_int(ud->settings, "scale_width");
1930 height = ghb_settings_get_int(ud->settings, "scale_height");
1931 max_width = ghb_settings_get_int(ud->settings, "max_width");
1932 max_height = ghb_settings_get_int(ud->settings, "max_width");
1934 max_width = (max_width >> modshift) << modshift;
1935 max_height = (max_height >> modshift) << modshift;
1936 // Adjust dims according to max values
1939 max_height = crop_height;
1943 max_width = crop_width;
1945 height = MIN(height, max_height);
1946 width = MIN(width, max_width);
1947 g_debug("max_width %d, max_height %d\n", max_width, max_height);
1950 width = title->width - crop[2] - crop[3];
1952 height = title->height - crop[0] - crop[1];
1962 // The scaler crashes if the dimensions are not divisible by 2
1963 // Align mod 2. And so does something in x264_encoder_headers()
1968 job->maxHeight = max_height;
1969 job->crop[0] = crop[0]; job->crop[1] = crop[1];
1970 job->crop[2] = crop[2]; job->crop[3] = crop[3];
1971 hb_set_anamorphic_size( job, &width, &height, &par_width, &par_height );
1978 gint new_width, new_height;
1980 g_debug("kw %s kh %s\n", keep_width ? "y":"n", keep_height ? "y":"n");
1981 g_debug("w %d h %d\n", width, height);
1982 // Compute pixel aspect ration.
1983 par = (gdouble)(title->height * aspect_n) / (title->width * aspect_d);
1984 // Must scale so that par becomes 1:1
1985 // Try to keep largest dimension
1986 new_height = (crop_height * ((gdouble)width/crop_width) / par);
1987 new_width = (crop_width * ((gdouble)height/crop_height) * par);
1988 // Height and width are always multiples of 2, so do the rounding
1989 new_height = ((new_height + 1) >> 1) << 1;
1990 new_width = ((new_width + 1) >> 1) << 1;
1991 g_debug("max w %d, new w %d\n", max_width, new_width);
1992 if (max_width && (new_width > max_width))
1994 height = new_height;
1996 else if (max_height && (new_height > max_height))
2000 else if (keep_width)
2002 height = new_height;
2004 else if (keep_height)
2008 else if (width > new_width)
2010 height = new_height;
2016 g_debug("new w %d h %d\n", width, height);
2018 width = ((width + modround) >> modshift) << modshift;
2019 height = ((height + modround) >> modshift) << modshift;
2021 ghb_ui_update_int (ud, "scale_width", width);
2022 ghb_ui_update_int (ud, "scale_height", height);
2026 set_job_picture_settings(hb_job_t *job, GHashTable *settings)
2028 job->crop[0] = ghb_settings_get_int(settings, "crop_top");
2029 job->crop[1] = ghb_settings_get_int(settings, "crop_bottom");
2030 job->crop[2] = ghb_settings_get_int(settings, "crop_left");
2031 job->crop[3] = ghb_settings_get_int(settings, "crop_right");
2033 gboolean anamorphic = ghb_settings_get_bool(settings, "anamorphic");
2034 gboolean round_dimensions = ghb_settings_get_bool(settings, "round_dimensions");
2035 if (round_dimensions && anamorphic)
2038 job->pixel_ratio = 2;
2040 else if (anamorphic)
2043 job->pixel_ratio = 2;
2048 job->pixel_ratio = 0;
2050 job->width = ghb_settings_get_int(settings, "scale_width");
2051 job->height = ghb_settings_get_int(settings, "scale_height");
2052 gint deint = ghb_settings_get_int(settings, "deinterlace");
2053 job->deinterlace = (deint == 0) ? 0 : 1;
2057 ghb_calculate_target_bitrate(GHashTable *settings, gint titleindex)
2064 if (h == NULL) return 2000;
2065 list = hb_get_titles( h );
2066 title = hb_list_item( list, titleindex );
2067 if (title == NULL) return 2000;
2069 if (job == NULL) return 2000;
2070 size = ghb_settings_get_int(settings, "video_target_size");
2071 return hb_calc_bitrate( job, size );
2075 ghb_guess_bitrate(GHashTable *settings)
2078 if (ghb_settings_get_bool(settings, "vquality_type_constant"))
2080 // This is really rough. I'm trying to err on the high
2081 // side since this is used to estimate if there is
2082 // sufficient disk space left
2083 gint vcodec = ghb_settings_get_int(settings, "video_codec");
2084 gdouble vquality = ghb_settings_get_dbl(settings, "video_quality")/100;
2085 if (vcodec == HB_VCODEC_X264 &&
2086 !ghb_settings_get_bool(settings, "linear_vquality"))
2088 vquality = 51.0 - vquality * 51.0;
2089 // Convert log curve to linear
2090 vquality = exp2((vquality-12)/6);
2091 // Don't let it go to 0
2092 if (vquality >= 31) vquality = 30;
2093 vquality = (31 - vquality) / 31;
2095 // bitrate seems to be a log relasionship to quality
2096 // with typical source material
2097 // This is a real wag
2098 bitrate = 20*1024*1024*exp10(vquality*14)/exp10(14);
2099 // Add some bits for audio
2100 bitrate += 500*1024;
2104 // Add some fudge to the bitrate to leave breathing room
2105 bitrate = ghb_settings_get_int(settings, "video_bitrate")*1024;
2106 // Add some bits for audio
2107 bitrate += 500*1024;
2113 ghb_validate_video(signal_user_data_t *ud)
2115 GHashTable *settings = ud->settings;
2119 mux = ghb_settings_get_int(settings, "container");
2120 vcodec = ghb_settings_get_int(settings, "video_codec");
2121 if ((mux == HB_MUX_MP4 || mux == HB_MUX_AVI) &&
2122 (vcodec == HB_VCODEC_THEORA))
2124 // mp4|avi/theora combination is not supported.
2125 message = g_strdup_printf(
2126 "Theora is not supported in the MP4 and AVI containers.\n\n"
2127 "You should choose a different video codec or container.\n"
2128 "If you continue, XviD will be chosen for you.");
2129 if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2135 vcodec = HB_VCODEC_XVID;
2136 ghb_ui_update_int(ud, "video_codec", vcodec);
2142 ghb_validate_audio(signal_user_data_t *ud)
2146 GHashTable *settings = ud->settings;
2148 setting_value_t *value;
2150 if (h == NULL) return FALSE;
2151 list = hb_get_titles( h );
2152 if( !hb_list_count( list ) )
2154 /* No valid title, stop right there */
2155 g_message("No title found.\n");
2159 gint titleindex = ghb_settings_get_index(settings, "title");
2160 title = hb_list_item( list, titleindex );
2161 if (title == NULL) return FALSE;
2162 GSList *link = ud->audio_settings;
2163 gint mux = ghb_settings_get_int(settings, "container");
2164 while (link != NULL)
2166 GHashTable *asettings;
2167 hb_audio_config_t *taudio;
2169 asettings = (GHashTable*)link->data;
2170 gint track = ghb_settings_get_index(asettings, "audio_track");
2171 gint codec = ghb_settings_get_int(asettings, "audio_codec");
2172 taudio = (hb_audio_config_t *) hb_list_audio_config_item( title->list_audio, track );
2173 if ((taudio->in.codec != HB_ACODEC_AC3) && (codec == HB_ACODEC_AC3))
2175 // Not supported. AC3 is passthrough only, so input must be AC3
2176 message = g_strdup_printf(
2177 "The source does not support AC3 Pass-Thru.\n\n"
2178 "You should choose a different audio codec.\n"
2179 "If you continue, one will be chosen for you.");
2180 if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2186 if (mux == HB_MUX_AVI)
2188 codec = HB_ACODEC_LAME;
2192 codec = HB_ACODEC_FAAC;
2194 value = get_acodec_value(codec);
2195 ghb_settings_set(asettings, "audio_codec", value);
2197 gchar *a_unsup = NULL;
2198 gchar *mux_s = NULL;
2199 if (mux == HB_MUX_MP4)
2202 // mp4/mp3|vorbis combination is not supported.
2203 if (codec == HB_ACODEC_LAME)
2206 codec = HB_ACODEC_FAAC;
2208 if (codec == HB_ACODEC_VORBIS)
2211 codec = HB_ACODEC_FAAC;
2214 else if (mux == HB_MUX_AVI)
2217 // avi/faac|vorbis combination is not supported.
2218 if (codec == HB_ACODEC_FAAC)
2221 codec = HB_ACODEC_LAME;
2223 if (codec == HB_ACODEC_VORBIS)
2226 codec = HB_ACODEC_LAME;
2229 else if (mux == HB_MUX_OGM)
2232 // avi/faac|vorbis combination is not supported.
2233 if (codec == HB_ACODEC_FAAC)
2236 codec = HB_ACODEC_VORBIS;
2238 if (codec == HB_ACODEC_AC3)
2241 codec = HB_ACODEC_VORBIS;
2246 message = g_strdup_printf(
2247 "%s is not supported in the %s container.\n\n"
2248 "You should choose a different audio codec.\n"
2249 "If you continue, one will be chosen for you.", a_unsup, mux_s);
2250 if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2256 value = get_acodec_value(codec);
2257 ghb_settings_set(asettings, "audio_codec", value);
2259 gint mix = ghb_settings_get_int (asettings, "audio_mix");
2260 gboolean allow_mono = TRUE;
2261 gboolean allow_stereo = TRUE;
2262 gboolean allow_dolby = TRUE;
2263 gboolean allow_dpl2 = TRUE;
2264 gboolean allow_6ch = TRUE;
2266 (taudio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
2267 (codec != HB_ACODEC_LAME);
2268 gint layout = taudio->in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
2270 ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
2272 (layout == HB_INPUT_CH_LAYOUT_3F1R) ||
2273 (layout == HB_INPUT_CH_LAYOUT_3F2R) ||
2274 (layout == HB_INPUT_CH_LAYOUT_DOLBY);
2275 allow_dpl2 = (layout == HB_INPUT_CH_LAYOUT_3F2R);
2277 (taudio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
2278 (codec != HB_ACODEC_LAME) &&
2279 (layout == HB_INPUT_CH_LAYOUT_3F2R) &&
2280 (taudio->in.channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE);
2282 gchar *mix_unsup = NULL;
2283 if (mix == HB_AMIXDOWN_MONO && !allow_mono)
2287 if (mix == HB_AMIXDOWN_STEREO && !allow_stereo)
2289 mix_unsup = "stereo";
2291 if (mix == HB_AMIXDOWN_DOLBY && !allow_dolby)
2293 mix_unsup = "Dolby";
2295 if (mix == HB_AMIXDOWN_DOLBYPLII && !allow_dpl2)
2297 mix_unsup = "Dolby Pro Logic II";
2299 if (mix == HB_AMIXDOWN_6CH && !allow_6ch)
2301 mix_unsup = "6 Channel";
2305 message = g_strdup_printf(
2306 "The source audio does not support %s mixdown.\n\n"
2307 "You should choose a different mixdown.\n"
2308 "If you continue, one will be chosen for you.", mix_unsup);
2309 if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2315 mix = ghb_get_best_mix(titleindex, track, codec, mix);
2316 value = get_amix_value(mix);
2317 ghb_settings_set(asettings, "audio_mix", value);
2325 ghb_validate_vquality(GHashTable *settings)
2331 if (ghb_settings_get_bool(settings, "nocheckvquality")) return TRUE;
2332 vcodec = ghb_settings_get_int(settings, "video_codec");
2333 if (ghb_settings_get_bool(settings, "vquality_type_constant"))
2335 if (!ghb_settings_get_bool(settings, "directqp"))
2337 if (vcodec != HB_VCODEC_X264 ||
2338 ghb_settings_get_bool(settings, "linear_vquality"))
2343 else if (vcodec == HB_VCODEC_X264)
2351 if (vcodec == HB_VCODEC_X264)
2356 else if (vcodec == HB_VCODEC_FFMPEG)
2367 gint vquality = ghb_settings_get_dbl(settings, "video_quality");
2368 if (vquality < min || vquality > max)
2370 message = g_strdup_printf(
2371 "Interesting video quality choise: %d\n\n"
2372 "Typical values range from %d to %d.\n"
2373 "Are you sure you wish to use this setting?",
2374 vquality, min, max);
2375 if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message,
2376 "Cancel", "Continue"))
2388 ghb_add_job(job_settings_t *js, gint unique_id)
2393 GHashTable *settings = js->settings;
2394 static gchar *x264opts;
2397 g_debug("ghb_add_job()\n");
2398 if (h == NULL) return;
2399 list = hb_get_titles( h );
2400 if( !hb_list_count( list ) )
2402 /* No valid title, stop right there */
2403 g_message("No title found.\n");
2407 gint titleindex = ghb_settings_get_index(settings, "title");
2408 title = hb_list_item( list, titleindex );
2409 if (title == NULL) return;
2411 /* Set job settings */
2413 if (job == NULL) return;
2415 job->mux = ghb_settings_get_int(settings, "container");
2416 if (job->mux == HB_MUX_MP4)
2418 job->largeFileSize = ghb_settings_get_bool(settings, "large_mp4");
2419 job->mp4_optimize = ghb_settings_get_bool(settings, "http_optimize_mp4");
2423 job->largeFileSize = FALSE;
2424 job->mp4_optimize = FALSE;
2426 gint chapter_start, chapter_end;
2427 chapter_start = ghb_settings_get_int(settings, "start_chapter");
2428 chapter_end = ghb_settings_get_int(settings, "end_chapter");
2429 gint num_chapters = hb_list_count(title->list_chapter);
2430 job->chapter_start = MIN( num_chapters, chapter_start );
2431 job->chapter_end = MAX( job->chapter_start, chapter_end );
2433 job->chapter_markers = ghb_settings_get_bool(settings, "chapter_markers");
2434 if ( job->chapter_markers )
2438 for(chapter = chapter_start; chapter <= chapter_end; chapter++)
2440 hb_chapter_t * chapter_s;
2443 if (js->chapter_list == NULL || js->chapter_list[chapter-1] == 0)
2445 name = g_strdup_printf ("Chapter %2d", chapter);
2449 name = g_strdup(js->chapter_list[chapter-1]);
2451 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
2452 strncpy(chapter_s->title, name, 1023);
2453 chapter_s->title[1023] = '\0';
2457 job->crop[0] = ghb_settings_get_int(settings, "crop_top");
2458 job->crop[1] = ghb_settings_get_int(settings, "crop_bottom");
2459 job->crop[2] = ghb_settings_get_int(settings, "crop_left");
2460 job->crop[3] = ghb_settings_get_int(settings, "crop_right");
2463 gboolean decomb = ghb_settings_get_bool(settings, "decomb");
2464 gint deint = ghb_settings_get_int(settings, "deinterlace");
2466 job->deinterlace = (deint == 0) ? 0 : 1;
2468 job->deinterlace = 0;
2469 job->grayscale = ghb_settings_get_bool(settings, "grayscale");
2471 gboolean anamorphic = ghb_settings_get_bool(settings, "anamorphic");
2472 gboolean round_dimensions = ghb_settings_get_bool(settings, "round_dimensions");
2473 if (round_dimensions && anamorphic)
2475 job->pixel_ratio = 2;
2478 else if (anamorphic)
2480 // Huh! I thought I wanted to use pixel_ratio 1 for this case, but
2481 // when its 1, libhb discards the width and height and uses original
2482 // title dims - crop. Thats not what I want.
2483 // Also, x264 requires things to divisible by 2.
2484 job->pixel_ratio = 2;
2489 job->pixel_ratio = 0;
2492 job->vfr = ghb_settings_get_bool(settings, "variable_frame_rate");
2493 /* Add selected filters */
2494 job->filters = hb_list_init();
2495 if( ghb_settings_get_bool(settings, "detelecine" ) || job->vfr )
2497 hb_filter_detelecine.settings = NULL;
2498 hb_list_add( job->filters, &hb_filter_detelecine );
2502 // Use default settings
2503 hb_filter_decomb.settings = NULL;
2504 hb_filter_decomb.settings = "1:2:6:9:40:16:16";
2505 hb_list_add( job->filters, &hb_filter_decomb );
2507 if( job->deinterlace )
2509 hb_filter_deinterlace.settings = (gchar*)ghb_settings_get_string(settings, "deinterlace");
2510 hb_list_add( job->filters, &hb_filter_deinterlace );
2512 if( ghb_settings_get_bool(settings, "deblock") )
2514 hb_filter_deblock.settings = NULL;
2515 hb_list_add( job->filters, &hb_filter_deblock );
2517 gint denoise = ghb_settings_get_int(settings, "denoise");
2520 hb_filter_denoise.settings = (gchar*)ghb_settings_get_string(settings, "denoise");
2521 hb_list_add( job->filters, &hb_filter_denoise );
2523 job->width = ghb_settings_get_int(settings, "scale_width");
2524 job->height = ghb_settings_get_int(settings, "scale_height");
2526 job->vcodec = ghb_settings_get_int(settings, "video_codec");
2527 if ((job->mux == HB_MUX_MP4 || job->mux == HB_MUX_AVI) &&
2528 (job->vcodec == HB_VCODEC_THEORA))
2530 // mp4|avi/theora combination is not supported.
2531 job->vcodec = HB_VCODEC_XVID;
2533 if ((job->vcodec == HB_VCODEC_X264) && (job->mux == HB_MUX_MP4))
2535 job->ipod_atom = ghb_settings_get_bool(settings, "ipod_file");
2537 if (ghb_settings_get_bool(settings, "vquality_type_constant"))
2539 gdouble vquality = ghb_settings_get_dbl(settings, "video_quality");
2540 if (!ghb_settings_get_bool(settings, "directqp"))
2543 if (ghb_settings_get_bool(settings, "linear_vquality"))
2545 if (job->vcodec == HB_VCODEC_X264)
2547 // Adjust to same range as xvid and ffmpeg
2548 vquality = 31.0 - vquality * 31.0;
2551 // Convert linear to log curve
2552 vquality = 12 + 6 * log2(vquality);
2553 if (vquality > 51) vquality = 51;
2554 if (vquality < 1) vquality = 1;
2562 if (vquality == 0.0) vquality = 0.01;
2563 if (vquality == 1.0) vquality = 0.0;
2566 job->vquality = vquality;
2569 else if (ghb_settings_get_bool(settings, "vquality_type_bitrate"))
2571 job->vquality = -1.0;
2572 job->vbitrate = ghb_settings_get_int(settings, "video_bitrate");
2574 // AVI container does not support variable frame rate.
2575 if (job->mux == HB_MUX_AVI)
2580 gint vrate = ghb_settings_get_int(settings, "framerate");
2581 if( vrate == 0 || job->vfr )
2583 job->vrate = title->rate;
2584 job->vrate_base = title->rate_base;
2589 job->vrate = 27000000;
2590 job->vrate_base = vrate;
2593 // First remove any audios that are already in the list
2594 // This happens if you are encoding the same title a second time.
2595 gint num_audio_tracks = hb_list_count(job->list_audio);
2597 for(ii = 0; ii < num_audio_tracks; ii++)
2599 hb_audio_t *audio = (hb_audio_t*)hb_list_item(job->list_audio, 0);
2600 hb_list_rem(job->list_audio, audio);
2602 GSList *link = js->audio_settings;
2604 while (link != NULL)
2606 GHashTable *asettings;
2607 hb_audio_config_t audio;
2608 hb_audio_config_t *taudio;
2610 hb_audio_config_init(&audio);
2611 asettings = (GHashTable*)link->data;
2612 audio.in.track = ghb_settings_get_index(asettings, "audio_track");
2613 audio.out.track = count;
2614 audio.out.codec = ghb_settings_get_int(asettings, "audio_codec");
2615 taudio = (hb_audio_config_t *) hb_list_audio_config_item( title->list_audio, audio.in.track );
2616 if ((taudio->in.codec != HB_ACODEC_AC3) && (audio.out.codec == HB_ACODEC_AC3))
2618 // Not supported. AC3 is passthrough only, so input must be AC3
2619 if (job->mux == HB_MUX_AVI)
2621 audio.out.codec = HB_ACODEC_LAME;
2625 audio.out.codec = HB_ACODEC_FAAC;
2628 if ((job->mux == HB_MUX_MP4) &&
2629 ((audio.out.codec == HB_ACODEC_LAME) ||
2630 (audio.out.codec == HB_ACODEC_VORBIS)))
2632 // mp4/mp3|vorbis combination is not supported.
2633 audio.out.codec = HB_ACODEC_FAAC;
2635 if ((job->mux == HB_MUX_AVI) &&
2636 ((audio.out.codec == HB_ACODEC_FAAC) ||
2637 (audio.out.codec == HB_ACODEC_VORBIS)))
2639 // avi/faac|vorbis combination is not supported.
2640 audio.out.codec = HB_ACODEC_LAME;
2642 if ((job->mux == HB_MUX_OGM) &&
2643 ((audio.out.codec == HB_ACODEC_FAAC) ||
2644 (audio.out.codec == HB_ACODEC_AC3)))
2646 // ogm/faac|ac3 combination is not supported.
2647 audio.out.codec = HB_ACODEC_VORBIS;
2649 audio.out.dynamic_range_compression = ghb_settings_get_dbl(asettings, "audio_drc");
2650 // It would be better if this were done in libhb for us, but its not yet.
2651 if (audio.out.codec == HB_ACODEC_AC3 || audio.out.codec == HB_ACODEC_DCA)
2653 audio.out.mixdown = 0;
2657 audio.out.mixdown = ghb_settings_get_int (asettings, "audio_mix");
2658 // Make sure the mixdown is valid and pick a new one if not.
2659 audio.out.mixdown = ghb_get_best_mix(titleindex, audio.in.track, audio.out.codec,
2661 audio.out.bitrate = ghb_settings_get_int(asettings, "audio_bitrate") / 1000;
2662 gint srate = ghb_settings_get_int(asettings, "audio_sample_rate");
2663 if (srate == 0) // 0 is same as source
2664 audio.out.samplerate = taudio->in.samplerate;
2666 audio.out.samplerate = srate;
2669 // Add it to the jobs audio list
2670 hb_audio_add( job, &audio );
2674 // I was tempted to move this up with the reset of the video quality
2675 // settings, but I suspect the settings above need to be made
2676 // first in order for hb_calc_bitrate to be accurate.
2677 if (ghb_settings_get_bool(settings, "vquality_type_target"))
2681 size = ghb_settings_get_int(settings, "video_target_size");
2682 job->vbitrate = hb_calc_bitrate( job, size );
2683 job->vquality = -1.0;
2686 job->file = ghb_settings_get_string(settings, "destination");
2687 job->crf = ghb_settings_get_bool(settings, "constant_rate_factor");
2688 // TODO: libhb holds onto a reference to the x264opts and is not
2689 // finished with it until encoding the job is done. But I can't
2690 // find a way to get at the job before it is removed in order to
2691 // free up the memory I am allocating here.
2692 // The short story is THIS LEAKS.
2693 x264opts = ghb_build_x264opts_string(settings);
2695 if( x264opts != NULL && *x264opts != '\0' )
2697 job->x264opts = x264opts;
2699 else /*avoids a bus error crash when options aren't specified*/
2701 job->x264opts = NULL;
2703 gint subtitle = ghb_settings_get_int(settings, "subtitle_lang");
2704 gboolean forced_subtitles = ghb_settings_get_bool (settings, "forced_subtitles");
2705 job->subtitle_force = forced_subtitles;
2707 job->subtitle = subtitle;
2712 // Subtitle scan. Look for subtitle matching audio language
2716 * When subtitle scan is enabled do a fast pre-scan job
2717 * which will determine which subtitles to enable, if any.
2720 job->indepth_scan = 1;
2722 x264opts_tmp = job->x264opts;
2723 job->x264opts = NULL;
2725 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
2726 *(job->select_subtitle) = NULL;
2729 * Add the pre-scan job
2731 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2733 //if (job->x264opts != NULL)
2734 // g_free(job->x264opts);
2736 job->x264opts = x264opts_tmp;
2740 job->select_subtitle = NULL;
2742 if( ghb_settings_get_bool(settings, "two_pass") &&
2743 !ghb_settings_get_bool(settings, "vquality_type_constant"))
2746 * If subtitle_scan is enabled then only turn it on
2747 * for the second pass and then off again for the
2750 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
2751 job->select_subtitle = NULL;
2753 job->indepth_scan = 0;
2754 gchar *x264opts2 = NULL;
2757 x264opts2 = g_strdup(x264opts);
2760 * If turbo options have been selected then append them
2761 * to the x264opts now (size includes one ':' and the '\0')
2763 if( ghb_settings_get_bool(settings, "turbo") )
2769 tmp_x264opts = g_strdup_printf("%s:%s", x264opts, turbo_opts);
2775 * No x264opts to modify, but apply the turbo options
2776 * anyway as they may be modifying defaults
2778 tmp_x264opts = g_strdup_printf("%s", turbo_opts);
2780 x264opts = tmp_x264opts;
2782 job->x264opts = x264opts;
2784 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2786 //if (job->x264opts != NULL)
2787 // g_free(job->x264opts);
2789 job->select_subtitle = subtitle_tmp;
2792 * On the second pass we turn off subtitle scan so that we
2793 * can actually encode using any subtitles that were auto
2794 * selected in the first pass (using the whacky select-subtitle
2795 * attribute of the job).
2797 job->indepth_scan = 0;
2798 job->x264opts = x264opts2;
2799 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2801 //if (job->x264opts != NULL)
2802 // g_free(job->x264opts);
2806 job->indepth_scan = 0;
2808 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2810 //if (job->x264opts != NULL)
2811 // g_free(job->x264opts);
2816 ghb_remove_job(gint unique_id)
2821 // Multiples passes all get the same id
2823 // Go backwards through list, so reordering doesn't screw me.
2824 ii = hb_count(h) - 1;
2825 while ((job = hb_job(h, ii--)) != NULL)
2827 if ((job->sequence_id & 0xFFFFFF) == unique_id)
2848 hb_get_state2( h, &s );
2850 if( s.state == HB_STATE_PAUSED )
2861 ghb_get_preview_image(gint titleindex, gint index, GHashTable *settings, gboolean borders)
2866 list = hb_get_titles( h );
2867 if( !hb_list_count( list ) )
2869 /* No valid title, stop right there */
2872 title = hb_list_item( list, titleindex );
2873 if (title == NULL) return NULL;
2874 if (title->job == NULL) return NULL;
2875 set_job_picture_settings(title->job, settings);
2877 // hb_get_preview can't handle sizes that are larger than the original title
2879 if (title->job->width > title->width)
2880 title->job->width = title->width;
2882 if (title->job->height > title->height)
2883 title->job->height = title->height;
2884 // And also creates artifacts if the width is not a multiple of 8
2885 //title->job->width = ((title->job->width + 4) >> 3) << 3;
2886 // And the height must be a multiple of 2
2887 //title->job->height = ((title->job->height + 1) >> 1) << 1;
2889 // Make sure we have a big enough buffer to receive the image from libhb. libhb
2890 // creates images with a one-pixel border around the original content. Hence we
2891 // add 2 pixels horizontally and vertically to the buffer size.
2892 gint srcWidth = title->width + 2;
2893 gint srcHeight= title->height + 2;
2894 gint dstWidth = title->width;
2895 gint dstHeight= title->height;
2897 gint borderLeft = 1;
2900 // |<---------- title->width ----------->|
2901 // | |<---- title->job->width ---->| |
2903 // .......................................
2904 // ....+-----------------------------+....
2905 // ....| |....<-- gray border
2908 // ....| |<------- image
2914 // ....+-----------------------------+....
2915 // .......................................
2916 dstWidth = title->job->width;
2917 dstHeight = title->job->height;
2918 borderTop = (srcHeight - dstHeight) / 2;
2919 borderLeft = (srcWidth - dstWidth) / 2;
2920 g_debug("boarders removed\n");
2923 g_debug("src %d x %d\n", srcWidth, srcHeight);
2924 g_debug("dst %d x %d\n", dstWidth, dstHeight);
2925 g_debug("job dim %d x %d\n", title->job->width, title->job->height);
2926 g_debug("title crop %d:%d:%d:%d\n",
2931 g_debug("job crop %d:%d:%d:%d\n",
2932 title->job->crop[0],
2933 title->job->crop[1],
2934 title->job->crop[2],
2935 title->job->crop[3]);
2936 static guint8 *buffer = NULL;
2937 static gint bufferSize = 0;
2940 newSize = srcWidth * srcHeight * 4;
2941 if( bufferSize < newSize )
2943 bufferSize = newSize;
2944 buffer = (guint8*) g_realloc( buffer, bufferSize );
2946 hb_get_preview( h, title, index, buffer );
2948 // Create an GdkPixbuf and copy the libhb image into it, converting it from
2949 // libhb's format something suitable. Along the way, we'll strip off the
2950 // border around libhb's image.
2952 // The image data returned by hb_get_preview is 4 bytes per pixel, BGRA format.
2953 // Alpha is ignored.
2955 GdkPixbuf *preview = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, dstWidth, dstHeight);
2956 guint8 *pixels = gdk_pixbuf_get_pixels (preview);
2958 guint32 *src = (guint32*)buffer;
2959 guint8 *dst = pixels;
2960 src += borderTop * srcWidth; // skip top rows in src to get to first row of dst
2961 src += borderLeft; // skip left pixels in src to get to first pixel of dst
2963 gint channels = gdk_pixbuf_get_n_channels (preview);
2964 gint stride = gdk_pixbuf_get_rowstride (preview);
2966 for (ii = 0; ii < dstHeight; ii++)
2969 for (jj = 0; jj < dstWidth; jj++)
2971 tmp[0] = src[0] >> 16;
2972 tmp[1] = src[0] >> 8;
2973 tmp[2] = src[0] >> 0;
2978 src += (srcWidth - dstWidth); // skip to next row in src
2980 // Got it, but hb_get_preview doesn't compensate for anamorphic, so lets
2982 gint width, height, par_width, par_height;
2983 gboolean anamorphic = ghb_settings_get_bool (settings, "anamorphic");
2986 hb_set_anamorphic_size( title->job, &width, &height, &par_width, &par_height );
2987 if (par_width > par_height)
2988 dstWidth = dstWidth * par_width / par_height;
2990 dstHeight = dstHeight * par_height / par_width;
2993 g_debug("scaled %d x %d\n", dstWidth, dstHeight);
2994 GdkPixbuf *scaled_preview;
2995 scaled_preview = gdk_pixbuf_scale_simple(preview, dstWidth, dstHeight, GDK_INTERP_HYPER);
2996 g_object_unref (preview);
2997 return scaled_preview;
3001 sanitize_volname(gchar *name)
3026 ghb_dvd_volname(const gchar *device)
3029 name = hb_dvd_name((gchar*)device);
3032 sanitize_volname(name);
3033 return g_strdup(name);