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;
51 static options_map_t d_container_opts[] =
53 {"MKV", "mkv", HB_MUX_MKV, 0.0, "mkv"},
54 {"MP4", "mp4", HB_MUX_MP4, 0.0, "mp4"},
55 {"M4V", "m4v", HB_MUX_MP4, 0.0, "m4v"},
56 {"AVI", "avi", HB_MUX_AVI, 0.0, "avi"},
57 {"OGM", "ogm", HB_MUX_OGM, 0.0, "ogm"},
59 combo_opts_t container_opts =
61 sizeof(d_container_opts)/sizeof(options_map_t),
65 static options_map_t d_deint_opts[] =
67 {"None", "none", 0, 0.0, ""},
68 {"Fast", "fast", 1, 0.0, "-1:-1:-1:0:1"},
69 {"Slow", "slow", 2, 0.0, "2:-1:-1:0:1"},
70 {"Slower", "slower", 3, 0.0, "0:-1:-1:0:1"},
72 combo_opts_t deint_opts =
74 sizeof(d_deint_opts)/sizeof(options_map_t),
78 static options_map_t d_denoise_opts[] =
80 {"None", "none", 0, 0.0, ""},
81 {"Weak", "weak", 1, 0.0, "2:1:2:3"},
82 {"Medium", "medium", 2, 0.0, "3:2:2:3"},
83 {"Strong", "strong", 3, 0.0, "7:7:5:5"},
85 combo_opts_t denoise_opts =
87 sizeof(d_denoise_opts)/sizeof(options_map_t),
91 static options_map_t d_vcodec_opts[] =
93 {"H.264 (x264)", "x264", HB_VCODEC_X264, 0.0, ""},
94 {"MPEG-4 (XviD)", "xvid", HB_VCODEC_XVID, 0.0, ""},
95 {"MPEG-4 (FFMPEG)", "ffmpeg", HB_VCODEC_FFMPEG, 0.0, ""},
96 {"Theora", "theora", HB_VCODEC_THEORA, 0.0, ""},
98 combo_opts_t vcodec_opts =
100 sizeof(d_vcodec_opts)/sizeof(options_map_t),
104 static options_map_t d_acodec_opts[] =
106 {"AAC (faac)", "faac", HB_ACODEC_FAAC, 0.0, "faac"},
107 {"MP3 (lame)", "lame", HB_ACODEC_LAME, 0.0, "lame"},
108 {"Vorbis", "vorbis", HB_ACODEC_VORBIS, 0.0, "vorbis"},
109 {"AC3 (pass-thru)", "ac3", HB_ACODEC_AC3, 0.0, "ac3"},
111 combo_opts_t acodec_opts =
113 sizeof(d_acodec_opts)/sizeof(options_map_t),
117 static options_map_t d_direct_opts[] =
119 {"None", "none", 0, 0.0, "none"},
120 {"Spatial", "spatial", 1, 0.0, "spatial"},
121 {"Temporal", "temporal", 2, 0.0, "temporal"},
122 {"Automatic", "auto", 3, 0.0, "auto"},
124 combo_opts_t direct_opts =
126 sizeof(d_direct_opts)/sizeof(options_map_t),
130 static options_map_t d_me_opts[] =
132 {"Diamond", "dia", 0, 0.0, "dia"},
133 {"Hexagon", "hex", 1, 0.0, "hex"},
134 {"Uneven Multi-Hexagon", "umh", 2, 0.0, "umh"},
135 {"Exhaustive", "esa", 3, 0.0, "esa"},
137 combo_opts_t me_opts =
139 sizeof(d_me_opts)/sizeof(options_map_t),
143 static options_map_t d_subme_opts[] =
145 {"1", "1", 1, 0.0, "1"},
146 {"2", "2", 2, 0.0, "2"},
147 {"3", "3", 3, 0.0, "3"},
148 {"4", "4", 4, 0.0, "4"},
149 {"5", "5", 5, 0.0, "5"},
150 {"6", "6", 6, 0.0, "6"},
151 {"7", "7", 7, 0.0, "7"},
153 combo_opts_t subme_opts =
155 sizeof(d_subme_opts)/sizeof(options_map_t),
159 static options_map_t d_analyse_opts[] =
161 {"Some", "some", 0, 0.0, "some"},
162 {"None", "none", 1, 0.0, "none"},
163 {"All", "all", 2, 0.0, "all"},
164 {"Custom", "custom", 3, 0.0, "all"},
166 combo_opts_t analyse_opts =
168 sizeof(d_analyse_opts)/sizeof(options_map_t),
172 static options_map_t d_trellis_opts[] =
174 {"Disabled", "0", 0, 0.0, "0"},
175 {"Final Macro Block", "1", 1, 0.0, "1"},
176 {"Always", "2", 2, 0.0, "2"},
178 combo_opts_t trellis_opts =
180 sizeof(d_trellis_opts)/sizeof(options_map_t),
184 typedef struct iso639_lang_t
186 char * eng_name; /* Description in English */
187 char * native_name; /* Description in native language */
188 char * iso639_1; /* ISO-639-1 (2 characters) code */
189 char * iso639_2; /* ISO-639-2/t (3 character) code */
190 char * iso639_2b; /* ISO-639-2/b code (if different from above) */
193 static const iso639_lang_t language_table[] =
195 { "Any", "", "zz", "und" },
196 { "Afar", "", "aa", "aar" },
197 { "Abkhazian", "", "ab", "abk" },
198 { "Afrikaans", "", "af", "afr" },
199 { "Akan", "", "ak", "aka" },
200 { "Albanian", "", "sq", "sqi", "alb" },
201 { "Amharic", "", "am", "amh" },
202 { "Arabic", "", "ar", "ara" },
203 { "Aragonese", "", "an", "arg" },
204 { "Armenian", "", "hy", "hye", "arm" },
205 { "Assamese", "", "as", "asm" },
206 { "Avaric", "", "av", "ava" },
207 { "Avestan", "", "ae", "ave" },
208 { "Aymara", "", "ay", "aym" },
209 { "Azerbaijani", "", "az", "aze" },
210 { "Bashkir", "", "ba", "bak" },
211 { "Bambara", "", "bm", "bam" },
212 { "Basque", "", "eu", "eus", "baq" },
213 { "Belarusian", "", "be", "bel" },
214 { "Bengali", "", "bn", "ben" },
215 { "Bihari", "", "bh", "bih" },
216 { "Bislama", "", "bi", "bis" },
217 { "Bosnian", "", "bs", "bos" },
218 { "Breton", "", "br", "bre" },
219 { "Bulgarian", "", "bg", "bul" },
220 { "Burmese", "", "my", "mya", "bur" },
221 { "Catalan", "", "ca", "cat" },
222 { "Chamorro", "", "ch", "cha" },
223 { "Chechen", "", "ce", "che" },
224 { "Chinese", "", "zh", "zho", "chi" },
225 { "Church Slavic", "", "cu", "chu" },
226 { "Chuvash", "", "cv", "chv" },
227 { "Cornish", "", "kw", "cor" },
228 { "Corsican", "", "co", "cos" },
229 { "Cree", "", "cr", "cre" },
230 { "Czech", "", "cs", "ces", "cze" },
231 { "Danish", "Dansk", "da", "dan" },
232 { "Divehi", "", "dv", "div" },
233 { "Dutch", "Nederlands", "nl", "nld", "dut" },
234 { "Dzongkha", "", "dz", "dzo" },
235 { "English", "English", "en", "eng" },
236 { "Esperanto", "", "eo", "epo" },
237 { "Estonian", "", "et", "est" },
238 { "Ewe", "", "ee", "ewe" },
239 { "Faroese", "", "fo", "fao" },
240 { "Fijian", "", "fj", "fij" },
241 { "Finnish", "Suomi", "fi", "fin" },
242 { "French", "Francais", "fr", "fra", "fre" },
243 { "Western Frisian", "", "fy", "fry" },
244 { "Fulah", "", "ff", "ful" },
245 { "Georgian", "", "ka", "kat", "geo" },
246 { "German", "Deutsch", "de", "deu", "ger" },
247 { "Gaelic (Scots)", "", "gd", "gla" },
248 { "Irish", "", "ga", "gle" },
249 { "Galician", "", "gl", "glg" },
250 { "Manx", "", "gv", "glv" },
251 { "Greek, Modern", "", "el", "ell", "gre" },
252 { "Guarani", "", "gn", "grn" },
253 { "Gujarati", "", "gu", "guj" },
254 { "Haitian", "", "ht", "hat" },
255 { "Hausa", "", "ha", "hau" },
256 { "Hebrew", "", "he", "heb" },
257 { "Herero", "", "hz", "her" },
258 { "Hindi", "", "hi", "hin" },
259 { "Hiri Motu", "", "ho", "hmo" },
260 { "Hungarian", "Magyar", "hu", "hun" },
261 { "Igbo", "", "ig", "ibo" },
262 { "Icelandic", "Islenska", "is", "isl", "ice" },
263 { "Ido", "", "io", "ido" },
264 { "Sichuan Yi", "", "ii", "iii" },
265 { "Inuktitut", "", "iu", "iku" },
266 { "Interlingue", "", "ie", "ile" },
267 { "Interlingua", "", "ia", "ina" },
268 { "Indonesian", "", "id", "ind" },
269 { "Inupiaq", "", "ik", "ipk" },
270 { "Italian", "Italiano", "it", "ita" },
271 { "Javanese", "", "jv", "jav" },
272 { "Japanese", "", "ja", "jpn" },
273 { "Kalaallisut", "", "kl", "kal" },
274 { "Kannada", "", "kn", "kan" },
275 { "Kashmiri", "", "ks", "kas" },
276 { "Kanuri", "", "kr", "kau" },
277 { "Kazakh", "", "kk", "kaz" },
278 { "Central Khmer", "", "km", "khm" },
279 { "Kikuyu", "", "ki", "kik" },
280 { "Kinyarwanda", "", "rw", "kin" },
281 { "Kirghiz", "", "ky", "kir" },
282 { "Komi", "", "kv", "kom" },
283 { "Kongo", "", "kg", "kon" },
284 { "Korean", "", "ko", "kor" },
285 { "Kuanyama", "", "kj", "kua" },
286 { "Kurdish", "", "ku", "kur" },
287 { "Lao", "", "lo", "lao" },
288 { "Latin", "", "la", "lat" },
289 { "Latvian", "", "lv", "lav" },
290 { "Limburgan", "", "li", "lim" },
291 { "Lingala", "", "ln", "lin" },
292 { "Lithuanian", "", "lt", "lit" },
293 { "Luxembourgish", "", "lb", "ltz" },
294 { "Luba-Katanga", "", "lu", "lub" },
295 { "Ganda", "", "lg", "lug" },
296 { "Macedonian", "", "mk", "mkd", "mac" },
297 { "Marshallese", "", "mh", "mah" },
298 { "Malayalam", "", "ml", "mal" },
299 { "Maori", "", "mi", "mri", "mao" },
300 { "Marathi", "", "mr", "mar" },
301 { "Malay", "", "ms", "msa", "msa" },
302 { "Malagasy", "", "mg", "mlg" },
303 { "Maltese", "", "mt", "mlt" },
304 { "Moldavian", "", "mo", "mol" },
305 { "Mongolian", "", "mn", "mon" },
306 { "Nauru", "", "na", "nau" },
307 { "Navajo", "", "nv", "nav" },
308 { "Ndebele, South", "", "nr", "nbl" },
309 { "Ndebele, North", "", "nd", "nde" },
310 { "Ndonga", "", "ng", "ndo" },
311 { "Nepali", "", "ne", "nep" },
312 { "Norwegian Nynorsk", "", "nn", "nno" },
313 { "Norwegian Bokmål", "", "nb", "nob" },
314 { "Norwegian", "Norsk", "no", "nor" },
315 { "Chichewa; Nyanja", "", "ny", "nya" },
316 { "Occitan", "", "oc", "oci" },
317 { "Ojibwa", "", "oj", "oji" },
318 { "Oriya", "", "or", "ori" },
319 { "Oromo", "", "om", "orm" },
320 { "Ossetian", "", "os", "oss" },
321 { "Panjabi", "", "pa", "pan" },
322 { "Persian", "", "fa", "fas", "per" },
323 { "Pali", "", "pi", "pli" },
324 { "Polish", "", "pl", "pol" },
325 { "Portuguese", "Portugues", "pt", "por" },
326 { "Pushto", "", "ps", "pus" },
327 { "Quechua", "", "qu", "que" },
328 { "Romansh", "", "rm", "roh" },
329 { "Romanian", "", "ro", "ron", "rum" },
330 { "Rundi", "", "rn", "run" },
331 { "Russian", "", "ru", "rus" },
332 { "Sango", "", "sg", "sag" },
333 { "Sanskrit", "", "sa", "san" },
334 { "Serbian", "", "sr", "srp", "scc" },
335 { "Croatian", "Hrvatski", "hr", "hrv", "scr" },
336 { "Sinhala", "", "si", "sin" },
337 { "Slovak", "", "sk", "slk", "slo" },
338 { "Slovenian", "", "sl", "slv" },
339 { "Northern Sami", "", "se", "sme" },
340 { "Samoan", "", "sm", "smo" },
341 { "Shona", "", "sn", "sna" },
342 { "Sindhi", "", "sd", "snd" },
343 { "Somali", "", "so", "som" },
344 { "Sotho, Southern", "", "st", "sot" },
345 { "Spanish", "Espanol", "es", "spa" },
346 { "Sardinian", "", "sc", "srd" },
347 { "Swati", "", "ss", "ssw" },
348 { "Sundanese", "", "su", "sun" },
349 { "Swahili", "", "sw", "swa" },
350 { "Swedish", "Svenska", "sv", "swe" },
351 { "Tahitian", "", "ty", "tah" },
352 { "Tamil", "", "ta", "tam" },
353 { "Tatar", "", "tt", "tat" },
354 { "Telugu", "", "te", "tel" },
355 { "Tajik", "", "tg", "tgk" },
356 { "Tagalog", "", "tl", "tgl" },
357 { "Thai", "", "th", "tha" },
358 { "Tibetan", "", "bo", "bod", "tib" },
359 { "Tigrinya", "", "ti", "tir" },
360 { "Tonga", "", "to", "ton" },
361 { "Tswana", "", "tn", "tsn" },
362 { "Tsonga", "", "ts", "tso" },
363 { "Turkmen", "", "tk", "tuk" },
364 { "Turkish", "", "tr", "tur" },
365 { "Twi", "", "tw", "twi" },
366 { "Uighur", "", "ug", "uig" },
367 { "Ukrainian", "", "uk", "ukr" },
368 { "Urdu", "", "ur", "urd" },
369 { "Uzbek", "", "uz", "uzb" },
370 { "Venda", "", "ve", "ven" },
371 { "Vietnamese", "", "vi", "vie" },
372 { "Volapük", "", "vo", "vol" },
373 { "Welsh", "", "cy", "cym", "wel" },
374 { "Walloon", "", "wa", "wln" },
375 { "Wolof", "", "wo", "wol" },
376 { "Xhosa", "", "xh", "xho" },
377 { "Yiddish", "", "yi", "yid" },
378 { "Yoruba", "", "yo", "yor" },
379 { "Zhuang", "", "za", "zha" },
380 { "Zulu", "", "zu", "zul" },
382 #define LANG_TABLE_SIZE (sizeof(language_table)/ sizeof(iso639_lang_t))
385 del_tree(const gchar *name, gboolean del_top)
389 if (g_file_test(name, G_FILE_TEST_IS_DIR))
391 GDir *gdir = g_dir_open(name, 0, NULL);
392 file = g_dir_read_name(gdir);
396 path = g_strdup_printf("%s/%s", name, file);
397 del_tree(path, TRUE);
399 file = g_dir_read_name(gdir);
417 ghb_vquality_range(signal_user_data_t *ud, gint *min, gint *max)
419 if (ghb_settings_get_bool(ud->settings, "directqp"))
421 gint vcodec = ghb_settings_get_int(ud->settings, "video_codec");
422 // Only x264 and ffmpeg currently support direct qp/crf entry
423 if (vcodec == HB_VCODEC_X264)
428 else if (vcodec == HB_VCODEC_FFMPEG)
447 ghb_lookup_acodec(const gchar *acodec)
451 for (ii = 0; ii < acodec_opts.count; ii++)
453 if (strcmp(acodec_opts.map[ii].shortOpt, acodec) == 0)
455 return acodec_opts.map[ii].ivalue;
458 return HB_ACODEC_FAAC;
462 ghb_lookup_bitrate(const gchar *bitrate)
466 for (ii = 0; ii < hb_audio_bitrates_count; ii++)
468 if (strcmp(hb_audio_bitrates[ii].string, bitrate) == 0)
470 return hb_audio_bitrates[ii].rate * 1000;
477 ghb_lookup_rate(const gchar *rate)
481 for (ii = 0; ii < hb_audio_rates_count; ii++)
483 if (strcmp(hb_audio_rates[ii].string, rate) == 0)
485 return hb_audio_rates[ii].rate;
488 // Coincidentally, the string "source" will return 0
489 // which is our flag to use "same as source"
494 ghb_lookup_mix(const gchar *mix)
498 for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
500 if (strcmp(hb_audio_mixdowns[ii].short_name, mix) == 0)
502 return hb_audio_mixdowns[ii].amixdown;
505 return HB_AMIXDOWN_DOLBYPLII;
509 ghb_lookup_drc(const gchar *drc)
512 dval = g_strtod(drc, NULL);
513 if (dval < 1.0) dval = 1.0;
514 if (dval > 4.0) dval = 4.0;
518 static setting_value_t*
519 get_acodec_value(gint val)
521 setting_value_t *value = NULL;
524 for (ii = 0; ii < acodec_opts.count; ii++)
526 if (acodec_opts.map[ii].ivalue == val)
528 value = g_malloc(sizeof(setting_value_t));
529 value->option = g_strdup(acodec_opts.map[ii].option);
530 value->shortOpt = g_strdup(acodec_opts.map[ii].shortOpt);
531 value->svalue = g_strdup(acodec_opts.map[ii].svalue);
533 value->ivalue = acodec_opts.map[ii].ivalue;
534 value->dvalue = acodec_opts.map[ii].dvalue;
541 static setting_value_t*
542 get_amix_value(gint val)
544 setting_value_t *value = NULL;
547 for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
549 if (hb_audio_mixdowns[ii].amixdown == val)
551 value = g_malloc(sizeof(setting_value_t));
552 value->option = g_strdup(hb_audio_mixdowns[ii].human_readable_name);
553 value->shortOpt = g_strdup(hb_audio_mixdowns[ii].short_name);
554 value->svalue = g_strdup(hb_audio_mixdowns[ii].internal_name);
556 value->ivalue = hb_audio_mixdowns[ii].amixdown;
557 value->dvalue = hb_audio_mixdowns[ii].amixdown;
564 // Handle for libhb. Gets set by ghb_backend_init()
565 static hb_handle_t * h = NULL;
567 extern void hb_get_tempory_directory(hb_handle_t *h, char path[512]);
570 ghb_hb_cleanup(gboolean partial)
574 hb_get_tempory_directory(h, dir);
575 del_tree(dir, !partial);
578 static hb_audio_config_t*
579 get_hb_audio(gint titleindex, gint track)
583 hb_audio_config_t *audio = NULL;
585 if (h == NULL) return NULL;
586 list = hb_get_titles( h );
587 if( !hb_list_count( list ) )
589 /* No valid title, stop right there */
592 title = hb_list_item( list, titleindex );
593 if (title == NULL) return NULL; // Bad titleindex
594 if (!hb_list_count(title->list_audio))
598 audio = (hb_audio_config_t *)hb_list_audio_config_item(title->list_audio, track);
603 search_rates(hb_rate_t *rates, gint rate, gint count)
606 for (ii = 0; ii < count; ii++)
608 if (rates[ii].rate == rate)
614 static gboolean find_combo_item_by_int(GtkTreeModel *store, gint value, GtkTreeIter *iter);
617 get_combo_box_store(GtkBuilder *builder, const gchar *name)
622 g_debug("get_combo_box_store() %s\n", name);
623 // First modify the combobox model to allow greying out of options
624 combo = GTK_COMBO_BOX(GHB_WIDGET(builder, name));
625 store = GTK_LIST_STORE(gtk_combo_box_get_model (combo));
630 grey_combo_box_item(GtkBuilder *builder, const gchar *name, gint value, gboolean grey)
635 store = get_combo_box_store(builder, name);
636 if (find_combo_item_by_int(GTK_TREE_MODEL(store), value, &iter))
638 gtk_list_store_set(store, &iter,
645 ghb_grey_combo_options(GtkBuilder *builder)
648 gint container, track, titleindex, acodec;
650 hb_audio_config_t *audio = NULL;
652 widget = GHB_WIDGET (builder, "title");
653 titleindex = ghb_widget_int(widget);
654 widget = GHB_WIDGET (builder, "audio_track");
655 track = ghb_widget_int(widget);
656 audio = get_hb_audio(titleindex, track);
657 widget = GHB_WIDGET (builder, "container");
658 container = ghb_widget_int(widget);
659 widget = GHB_WIDGET (builder, "http_optimize_mp4");
660 httpopt = ghb_widget_int(widget);
662 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_FAAC, FALSE);
663 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_LAME, FALSE);
664 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_VORBIS, FALSE);
666 gboolean allow_ac3 = TRUE;
668 !(container == HB_MUX_MP4 && httpopt) &&
669 (container != HB_MUX_OGM);
673 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_AC3, FALSE);
677 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_AC3, TRUE);
679 if (audio && audio->in.codec != HB_ACODEC_AC3)
681 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_AC3, TRUE);
683 grey_combo_box_item(builder, "video_codec", HB_VCODEC_THEORA, FALSE);
685 widget = GHB_WIDGET (builder, "audio_codec");
686 acodec = ghb_widget_int(widget);
687 if (acodec != HB_ACODEC_AC3)
689 grey_combo_box_item(builder, "audio_mix", 0, TRUE);
691 if (container == HB_MUX_MP4)
693 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_LAME, TRUE);
694 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_VORBIS, TRUE);
695 grey_combo_box_item(builder, "video_codec", HB_VCODEC_THEORA, TRUE);
697 else if (container == HB_MUX_AVI)
699 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_FAAC, TRUE);
700 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_VORBIS, TRUE);
701 grey_combo_box_item(builder, "video_codec", HB_VCODEC_THEORA, TRUE);
703 else if (container == HB_MUX_OGM)
705 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_FAAC, TRUE);
708 gboolean allow_mono = TRUE;
709 gboolean allow_stereo = TRUE;
710 gboolean allow_dolby = TRUE;
711 gboolean allow_dpl2 = TRUE;
712 gboolean allow_6ch = TRUE;
716 (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
717 (acodec != HB_ACODEC_LAME);
718 gint layout = audio->in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
720 ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
722 (layout == HB_INPUT_CH_LAYOUT_3F1R) ||
723 (layout == HB_INPUT_CH_LAYOUT_3F2R) ||
724 (layout == HB_INPUT_CH_LAYOUT_DOLBY);
725 allow_dpl2 = (layout == HB_INPUT_CH_LAYOUT_3F2R);
727 (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
728 (acodec != HB_ACODEC_LAME) &&
729 (layout == HB_INPUT_CH_LAYOUT_3F2R) &&
730 (audio->in.channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE);
732 grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_MONO, !allow_mono);
733 grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_STEREO, !allow_stereo);
734 grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_DOLBY, !allow_dolby);
735 grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_DOLBYPLII, !allow_dpl2);
736 grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_6CH, !allow_6ch);
740 ghb_get_best_mix(gint titleindex, gint track, gint acodec, gint mix)
742 hb_audio_config_t *audio = NULL;
743 gboolean allow_mono = TRUE;
744 gboolean allow_stereo = TRUE;
745 gboolean allow_dolby = TRUE;
746 gboolean allow_dpl2 = TRUE;
747 gboolean allow_6ch = TRUE;
749 if (acodec & (HB_ACODEC_AC3 | HB_ACODEC_DCA))
751 // Audio codec pass-thru. No mixdown
754 audio = get_hb_audio(titleindex, track);
758 (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
759 (acodec != HB_ACODEC_LAME);
760 gint layout = audio->in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
762 ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
764 (layout == HB_INPUT_CH_LAYOUT_3F1R) ||
765 (layout == HB_INPUT_CH_LAYOUT_3F2R) ||
766 (layout == HB_INPUT_CH_LAYOUT_DOLBY);
767 allow_dpl2 = (layout == HB_INPUT_CH_LAYOUT_3F2R);
769 (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
770 (acodec != HB_ACODEC_LAME) &&
771 (layout == HB_INPUT_CH_LAYOUT_3F2R) &&
772 (audio->in.channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE);
774 gboolean greater = FALSE;
777 // If no mix is specified, select the best available.
778 mix = HB_AMIXDOWN_6CH;
780 if (mix == HB_AMIXDOWN_6CH)
783 if (allow_6ch) return HB_AMIXDOWN_6CH;
785 if (mix == HB_AMIXDOWN_DOLBYPLII || greater)
788 if (allow_dpl2) return HB_AMIXDOWN_DOLBYPLII;
790 if (mix == HB_AMIXDOWN_DOLBY || greater)
793 if (allow_dolby) return HB_AMIXDOWN_DOLBY;
795 if (mix == HB_AMIXDOWN_STEREO || greater)
798 if (allow_stereo) return HB_AMIXDOWN_STEREO;
800 if (mix == HB_AMIXDOWN_MONO || greater)
803 if (allow_mono) return HB_AMIXDOWN_MONO;
805 if (allow_stereo) return HB_AMIXDOWN_STEREO;
806 if (allow_dolby) return HB_AMIXDOWN_DOLBY;
807 if (allow_dpl2) return HB_AMIXDOWN_DOLBYPLII;
808 if (allow_6ch) return HB_AMIXDOWN_6CH;
812 // Set up the model for the combo box
814 init_combo_box(GtkBuilder *builder, const gchar *name)
818 GtkCellRenderer *cell;
820 g_debug("init_combo_box() %s\n", name);
821 // First modify the combobox model to allow greying out of options
822 combo = GTK_COMBO_BOX(GHB_WIDGET(builder, name));
824 // 1 - String to display
825 // 2 - bool indicating whether the entry is selectable (grey or not)
826 // 3 - String that is used for presets
827 // 4 - Int value determined by backend
828 // 5 - String value determined by backend
829 store = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_BOOLEAN,
830 G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
831 gtk_combo_box_set_model(combo, GTK_TREE_MODEL(store));
833 gtk_cell_layout_clear(GTK_CELL_LAYOUT(combo));
834 cell = GTK_CELL_RENDERER(gtk_cell_renderer_text_new());
835 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), cell, TRUE);
836 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), cell,
837 "text", 0, "sensitive", 1, NULL);
841 audio_bitrate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
847 g_debug("audio_bitrate_opts_set ()\n");
848 store = get_combo_box_store(builder, name);
849 gtk_list_store_clear(store);
850 for (ii = 0; ii < count; ii++)
852 gtk_list_store_append(store, &iter);
853 gtk_list_store_set(store, &iter,
857 3, rates[ii].rate * 1000,
864 audio_bitrate_opts_clean(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
869 gboolean done = FALSE;
870 gboolean changed = FALSE;
872 g_debug("audio_bitrate_opts_clean ()\n");
873 store = get_combo_box_store(builder, name);
874 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL(store), &iter))
878 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, 3, &ivalue, -1);
879 if (search_rates(rates, ivalue/1000, count) < 0)
881 done = !gtk_list_store_remove(store, &iter);
886 done = !gtk_tree_model_iter_next (GTK_TREE_MODEL(store), &iter);
894 audio_samplerate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
900 g_debug("audio_samplerate_opts_set ()\n");
901 store = get_combo_box_store(builder, name);
902 gtk_list_store_clear(store);
903 // Add an item for "Same As Source"
904 gtk_list_store_append(store, &iter);
905 gtk_list_store_set(store, &iter,
912 for (ii = 0; ii < count; ii++)
914 gtk_list_store_append(store, &iter);
915 gtk_list_store_set(store, &iter,
926 video_rate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
932 g_debug("video_rate_opts_set ()\n");
933 store = get_combo_box_store(builder, name);
934 gtk_list_store_clear(store);
935 // Add an item for "Same As Source"
936 gtk_list_store_append(store, &iter);
937 gtk_list_store_set(store, &iter,
944 for (ii = 0; ii < count; ii++)
948 if (strcmp(rates[ii].string, "23.976") == 0)
950 desc = "(NTSC Film)";
952 else if (strcmp(rates[ii].string, "25") == 0)
954 desc = "(PAL Film/Video)";
956 else if (strcmp(rates[ii].string, "29.97") == 0)
958 desc = "(NTSC Video)";
960 option = g_strdup_printf ("%s %s", rates[ii].string, desc);
961 gtk_list_store_append(store, &iter);
962 gtk_list_store_set(store, &iter,
974 mix_opts_set(GtkBuilder *builder, const gchar *name)
980 g_debug("mix_opts_set ()\n");
981 store = get_combo_box_store(builder, name);
982 gtk_list_store_clear(store);
983 gtk_list_store_append(store, &iter);
984 gtk_list_store_set(store, &iter,
991 for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
993 gtk_list_store_append(store, &iter);
994 gtk_list_store_set(store, &iter,
995 0, hb_audio_mixdowns[ii].human_readable_name,
997 2, hb_audio_mixdowns[ii].short_name,
998 3, hb_audio_mixdowns[ii].amixdown,
999 4, hb_audio_mixdowns[ii].internal_name,
1005 language_opts_set(GtkBuilder *builder, const gchar *name)
1008 GtkListStore *store;
1011 g_debug("language_opts_set ()\n");
1012 store = get_combo_box_store(builder, name);
1013 gtk_list_store_clear(store);
1014 for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
1016 gtk_list_store_append(store, &iter);
1017 gtk_list_store_set(store, &iter,
1018 0, language_table[ii].eng_name,
1020 2, language_table[ii].iso639_2,
1022 4, language_table[ii].iso639_1,
1028 title_opts_set(GtkBuilder *builder, const gchar *name)
1031 GtkListStore *store;
1037 g_debug("title_opts_set ()\n");
1038 store = get_combo_box_store(builder, name);
1039 gtk_list_store_clear(store);
1042 list = hb_get_titles( h );
1043 count = hb_list_count( list );
1044 if (count > 100) count = 100;
1048 // No titles. Fill in a default.
1049 gtk_list_store_append(store, &iter);
1050 gtk_list_store_set(store, &iter,
1059 for (ii = 0; ii < count; ii++)
1063 title = (hb_title_t*)hb_list_item(list, ii);
1064 if (title->duration != 0)
1066 option = g_strdup_printf ("%d - %02dh%02dm%02ds",
1067 title->index, title->hours, title->minutes, title->seconds);
1071 option = g_strdup_printf ("%d - Unknown Length", title->index);
1073 gtk_list_store_append(store, &iter);
1074 gtk_list_store_set(store, &iter,
1085 find_combo_item_by_int(GtkTreeModel *store, gint value, GtkTreeIter *iter)
1088 gboolean foundit = FALSE;
1090 if (gtk_tree_model_get_iter_first (store, iter))
1094 gtk_tree_model_get(store, iter, 3, &ivalue, -1);
1095 if (value == ivalue)
1100 } while (gtk_tree_model_iter_next (store, iter));
1106 audio_rate_opts_add(GtkBuilder *builder, const gchar *name, gint rate)
1109 GtkListStore *store;
1112 g_debug("audio_rate_opts_add ()\n");
1113 store = get_combo_box_store(builder, name);
1114 if (!find_combo_item_by_int(GTK_TREE_MODEL(store), rate, &iter))
1116 str = g_strdup_printf ("%.8g", (gdouble)rate/1000.0);
1117 gtk_list_store_append(store, &iter);
1118 gtk_list_store_set(store, &iter,
1131 audio_track_opts_set(GtkBuilder *builder, const gchar *name, gint titleindex)
1134 GtkListStore *store;
1137 hb_audio_config_t * audio;
1141 g_debug("audio_track_opts_set ()\n");
1142 store = get_combo_box_store(builder, name);
1143 gtk_list_store_clear(store);
1146 list = hb_get_titles( h );
1147 title = (hb_title_t*)hb_list_item( list, titleindex );
1150 count = hb_list_count( title->list_audio );
1153 if (count > 10) count = 10;
1156 // No audio. set some default
1157 gtk_list_store_append(store, &iter);
1158 gtk_list_store_set(store, &iter,
1167 for (ii = 0; ii < count; ii++)
1169 audio = (hb_audio_config_t *) hb_list_audio_config_item( title->list_audio, ii );
1170 gtk_list_store_append(store, &iter);
1171 gtk_list_store_set(store, &iter,
1172 0, audio->lang.description,
1174 2, audio->lang.description,
1176 4, audio->lang.description,
1182 subtitle_opts_set(GtkBuilder *builder, const gchar *name, gint titleindex)
1185 GtkListStore *store;
1188 hb_subtitle_t * subtitle;
1192 g_debug("subtitle_opts_set ()\n");
1193 store = get_combo_box_store(builder, name);
1194 gtk_list_store_clear(store);
1197 list = hb_get_titles( h );
1198 title = (hb_title_t*)hb_list_item( list, titleindex );
1201 count = hb_list_count( title->list_subtitle );
1204 if (count > 10) count = 10;
1205 // Add options for "none" and "autoselect"
1206 gtk_list_store_append(store, &iter);
1207 gtk_list_store_set(store, &iter,
1214 gtk_list_store_append(store, &iter);
1215 gtk_list_store_set(store, &iter,
1222 for (ii = 0; ii < count; ii++)
1224 subtitle = (hb_subtitle_t *) hb_list_item( title->list_subtitle, ii );
1225 gtk_list_store_append(store, &iter);
1226 gtk_list_store_set(store, &iter,
1229 2, subtitle->iso639_2,
1231 4, subtitle->iso639_2,
1234 if (titleindex == -1)
1236 for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
1238 gtk_list_store_append(store, &iter);
1239 gtk_list_store_set(store, &iter,
1240 0, language_table[ii].eng_name,
1242 2, language_table[ii].iso639_2,
1244 4, language_table[ii].iso639_2,
1257 guint64 longest = 0;
1258 gint titleindex = 0;
1260 g_debug("ghb_longest_title ()\n");
1261 if (h == NULL) return 0;
1262 list = hb_get_titles( h );
1263 count = hb_list_count( list );
1264 if (count > 100) count = 100;
1265 for (ii = 0; ii < count; ii++)
1267 title = (hb_title_t*)hb_list_item(list, ii);
1268 if (title->duration > longest)
1271 longest = title->duration;
1278 ghb_find_audio_track(gint titleindex, const gchar *lang, gint index)
1282 hb_audio_config_t * audio;
1288 g_debug("find_audio_track ()\n");
1291 list = hb_get_titles( h );
1292 title = (hb_title_t*)hb_list_item( list, titleindex );
1295 count = hb_list_count( title->list_audio );
1298 if (count > 10) count = 10;
1299 for (ii = 0; ii < count; ii++)
1301 audio = (hb_audio_config_t*)hb_list_audio_config_item( title->list_audio, ii );
1302 if ((strcmp(lang, audio->lang.iso639_2) == 0) ||
1303 (strcmp(lang, "und") == 0))
1317 generic_opts_set(GtkBuilder *builder, const gchar *name, combo_opts_t *opts)
1320 GtkListStore *store;
1323 g_debug("generic_opts_set ()\n");
1324 store = get_combo_box_store(builder, name);
1325 gtk_list_store_clear(store);
1326 for (ii = 0; ii < opts->count; ii++)
1328 gtk_list_store_append(store, &iter);
1329 gtk_list_store_set(store, &iter,
1330 0, opts->map[ii].option,
1332 2, opts->map[ii].shortOpt,
1333 3, opts->map[ii].ivalue,
1334 4, opts->map[ii].svalue,
1340 ghb_update_ui_combo_box(GtkBuilder *builder, const gchar *name, gint user_data, gboolean all)
1342 GtkComboBox *combo = NULL;
1344 gint handler_id = 0;
1346 g_debug("ghb_update_ui_combo_box() %s\n", name);
1349 // Clearing a combo box causes a rash of "changed" events, even when
1350 // the active item is -1 (inactive). To control things, I'm disabling
1351 // the event till things are settled down.
1352 combo = GTK_COMBO_BOX(GHB_WIDGET(builder, name));
1353 signal_id = g_signal_lookup("changed", GTK_TYPE_COMBO_BOX);
1356 // Valid signal id found. This should always succeed.
1357 handler_id = g_signal_handler_find ((gpointer)combo, G_SIGNAL_MATCH_ID,
1358 signal_id, 0, 0, 0, 0);
1361 // This should also always succeed
1362 g_signal_handler_block ((gpointer)combo, handler_id);
1366 if (all || strcmp(name, "audio_bitrate") == 0)
1367 audio_bitrate_opts_set(builder, "audio_bitrate", hb_audio_bitrates, hb_audio_bitrates_count);
1368 if (all || strcmp(name, "audio_rate") == 0)
1369 audio_samplerate_opts_set(builder, "audio_rate", hb_audio_rates, hb_audio_rates_count);
1370 if (all || strcmp(name, "framerate") == 0)
1371 video_rate_opts_set(builder, "framerate", hb_video_rates, hb_video_rates_count);
1372 if (all || strcmp(name, "audio_mix") == 0)
1373 mix_opts_set(builder, "audio_mix");
1374 if (all || strcmp(name, "source_audio_lang") == 0)
1375 language_opts_set(builder, "source_audio_lang");
1376 if (all || strcmp(name, "subtitle_lang") == 0)
1377 subtitle_opts_set(builder, "subtitle_lang", user_data);
1378 if (all || strcmp(name, "title") == 0)
1379 title_opts_set(builder, "title");
1380 if (all || strcmp(name, "audio_track") == 0)
1381 audio_track_opts_set(builder, "audio_track", user_data);
1382 if (all || strcmp(name, "container") == 0)
1383 generic_opts_set(builder, "container", &container_opts);
1384 if (all || strcmp(name, "deinterlace") == 0)
1385 generic_opts_set(builder, "deinterlace", &deint_opts);
1386 if (all || strcmp(name, "denoise") == 0)
1387 generic_opts_set(builder, "denoise", &denoise_opts);
1388 if (all || strcmp(name, "video_codec") == 0)
1389 generic_opts_set(builder, "video_codec", &vcodec_opts);
1390 if (all || strcmp(name, "audio_codec") == 0)
1391 generic_opts_set(builder, "audio_codec", &acodec_opts);
1392 if (all || strcmp(name, "x264_direct") == 0)
1393 generic_opts_set(builder, "x264_direct", &direct_opts);
1394 if (all || strcmp(name, "x264_me") == 0)
1395 generic_opts_set(builder, "x264_me", &me_opts);
1396 if (all || strcmp(name, "x264_subme") == 0)
1397 generic_opts_set(builder, "x264_subme", &subme_opts);
1398 if (all || strcmp(name, "x264_analyse") == 0)
1399 generic_opts_set(builder, "x264_analyse", &analyse_opts);
1400 if (all || strcmp(name, "x264_trellis") == 0)
1401 generic_opts_set(builder, "x264_trellis", &trellis_opts);
1404 g_signal_handler_unblock ((gpointer)combo, handler_id);
1409 init_ui_combo_boxes(GtkBuilder *builder)
1411 init_combo_box(builder, "audio_bitrate");
1412 init_combo_box(builder, "audio_rate");
1413 init_combo_box(builder, "framerate");
1414 init_combo_box(builder, "audio_mix");
1415 init_combo_box(builder, "source_audio_lang");
1416 init_combo_box(builder, "subtitle_lang");
1417 init_combo_box(builder, "title");
1418 init_combo_box(builder, "audio_track");
1419 init_combo_box(builder, "container");
1420 init_combo_box(builder, "deinterlace");
1421 init_combo_box(builder, "denoise");
1422 init_combo_box(builder, "video_codec");
1423 init_combo_box(builder, "audio_codec");
1424 init_combo_box(builder, "x264_direct");
1425 init_combo_box(builder, "x264_me");
1426 init_combo_box(builder, "x264_subme");
1427 init_combo_box(builder, "x264_analyse");
1428 init_combo_box(builder, "x264_trellis");
1431 static const char * turbo_opts =
1432 "ref=1:subme=1:me=dia:analyse=none:trellis=0:"
1433 "no-fast-pskip=0:8x8dct=0:weightb=0";
1435 // Construct the x264 options string
1436 // The result is allocated, so someone must free it at some point.
1438 ghb_build_x264opts_string(GHashTable *settings)
1441 const gchar *opts = ghb_settings_get_string(settings, "x264_options");
1444 result = g_strdup(opts);
1448 result = g_strdup("");
1454 ghb_get_chapters(gint titleindex)
1458 hb_chapter_t * chapter;
1460 gchar **result = NULL;
1462 g_debug("ghb_get_chapters (title = %d)\n", titleindex);
1463 if (h == NULL) return NULL;
1464 list = hb_get_titles( h );
1465 title = (hb_title_t*)hb_list_item( list, titleindex );
1466 if (title == NULL) return NULL;
1467 count = hb_list_count( title->list_chapter );
1468 result = g_malloc((count+1) * sizeof(gchar*));
1469 for (ii = 0; ii < count; ii++)
1471 chapter = hb_list_item(title->list_chapter, ii);
1472 if (chapter == NULL) break;
1473 if (chapter->title == NULL || chapter->title[0] == 0)
1474 result[ii] = g_strdup_printf ("Chapter %2d", ii);
1476 result[ii] = g_strdup(chapter->title);
1483 ghb_ac3_in_audio_list(GSList *audio_list)
1488 while (link != NULL)
1490 GHashTable *asettings;
1493 asettings = (GHashTable*)link->data;
1494 acodec = ghb_settings_get_int(asettings, "audio_codec");
1495 if (acodec == HB_ACODEC_AC3)
1503 ghb_set_passthru_rate_opts(GtkBuilder *builder, gint bitrate)
1505 gboolean changed = FALSE;
1506 changed = audio_rate_opts_add(builder, "audio_bitrate", bitrate);
1511 ghb_set_default_rate_opts(GtkBuilder *builder)
1513 gboolean changed = FALSE;
1514 changed = audio_bitrate_opts_clean(builder, "audio_bitrate", hb_audio_bitrates, hb_audio_bitrates_count);
1518 static ghb_status_t hb_status;
1521 ghb_backend_init(GtkBuilder *builder, gint debug, gint update)
1524 h = hb_init( debug, update );
1525 // Set up the list model for the combos
1526 init_ui_combo_boxes(builder);
1527 // Populate all the combos
1528 ghb_update_ui_combo_box(builder, NULL, 0, TRUE);
1532 ghb_backend_scan(const gchar *path, gint titleindex)
1534 hb_scan( h, path, titleindex );
1535 hb_status.state |= GHB_STATE_SCANNING;
1536 // initialize count and cur to something that won't cause FPE
1537 // when computing progress
1538 hb_status.title_count = 1;
1539 hb_status.title_cur = 0;
1545 return hb_status.state;
1549 ghb_clear_state(gint state)
1551 hb_status.state &= ~state;
1555 ghb_set_state(gint state)
1557 hb_status.state |= state;
1561 ghb_get_status(ghb_status_t *status)
1563 memcpy(status, &hb_status, sizeof(ghb_status_t));
1570 static gint scan_complete_count = 0;
1573 if (h == NULL) return;
1574 hb_get_state( h, &s );
1575 scans = hb_get_scancount(h);
1576 if (scans > scan_complete_count)
1578 hb_status.state &= ~GHB_STATE_SCANNING;
1579 hb_status.state |= GHB_STATE_SCANDONE;
1580 scan_complete_count = hb_get_scancount(h);
1588 #define p s.param.scanning
1589 case HB_STATE_SCANNING:
1591 hb_status.state |= GHB_STATE_SCANNING;
1592 hb_status.title_count = p.title_count;
1593 hb_status.title_cur = p.title_cur;
1597 case HB_STATE_SCANDONE:
1599 hb_status.state &= ~GHB_STATE_SCANNING;
1600 hb_status.state |= GHB_STATE_SCANDONE;
1603 #define p s.param.working
1604 case HB_STATE_WORKING:
1605 hb_status.state |= GHB_STATE_WORKING;
1606 hb_status.state &= ~GHB_STATE_PAUSED;
1607 hb_status.job_cur = p.job_cur;
1608 hb_status.job_count = p.job_count;
1609 hb_status.progress = p.progress;
1610 hb_status.rate_cur = p.rate_cur;
1611 hb_status.rate_avg = p.rate_avg;
1612 hb_status.hours = p.hours;
1613 hb_status.minutes = p.minutes;
1614 hb_status.seconds = p.seconds;
1615 hb_status.unique_id = p.sequence_id & 0xFFFFFF;
1619 case HB_STATE_PAUSED:
1620 hb_status.state |= GHB_STATE_PAUSED;
1623 #define p s.param.muxing
1624 case HB_STATE_MUXING:
1626 hb_status.state |= GHB_STATE_MUXING;
1630 #define p s.param.workdone
1631 case HB_STATE_WORKDONE:
1635 hb_status.state |= GHB_STATE_WORKDONE;
1636 hb_status.state &= ~GHB_STATE_MUXING;
1637 hb_status.state &= ~GHB_STATE_PAUSED;
1638 hb_status.state &= ~GHB_STATE_WORKING;
1642 hb_status.error = GHB_ERROR_NONE;
1643 case HB_ERROR_CANCELED:
1644 hb_status.error = GHB_ERROR_CANCELED;
1646 hb_status.error = GHB_ERROR_FAIL;
1648 hb_status.error = p.error;
1649 // Delete all remaining jobs of this encode.
1650 // An encode can be composed of multiple associated jobs.
1651 // When a job is stopped, libhb removes it from the job list,
1652 // but does not remove other jobs that may be associated with it.
1653 // Associated jobs are taged in the sequence id.
1654 while (((job = hb_job(h, 0)) != NULL) && ((job->sequence_id >> 24) != 0) )
1662 ghb_get_title_info(ghb_title_info_t *tinfo, gint titleindex)
1667 if (h == NULL) return FALSE;
1668 list = hb_get_titles( h );
1669 if( !hb_list_count( list ) )
1671 /* No valid title, stop right there */
1675 title = hb_list_item( list, titleindex );
1676 if (title == NULL) return FALSE; // Bad titleindex
1677 tinfo->width = title->width;
1678 tinfo->height = title->height;
1679 memcpy(tinfo->crop, title->crop, 4 * sizeof(int));
1680 // Don't allow crop to 0
1681 if (title->crop[0] + title->crop[1] >= title->height)
1682 title->crop[0] = title->crop[1] = 0;
1683 if (title->crop[2] + title->crop[3] >= title->width)
1684 title->crop[2] = title->crop[3] = 0;
1685 tinfo->num_chapters = hb_list_count(title->list_chapter);
1686 tinfo->rate_base = title->rate_base;
1687 tinfo->rate = title->rate;
1688 hb_reduce(&(tinfo->aspect_n), &(tinfo->aspect_d),
1689 title->width * title->pixel_aspect_width,
1690 title->height * title->pixel_aspect_height);
1691 tinfo->hours = title->hours;
1692 tinfo->minutes = title->minutes;
1693 tinfo->seconds = title->seconds;
1694 tinfo->duration = title->duration;
1699 ghb_get_audio_info(ghb_audio_info_t *ainfo, gint titleindex, gint audioindex)
1701 hb_audio_config_t *audio;
1703 audio = get_hb_audio(titleindex, audioindex);
1704 if (audio == NULL) return FALSE; // Bad audioindex
1705 ainfo->codec = audio->in.codec;
1706 ainfo->bitrate = audio->in.bitrate;
1707 ainfo->samplerate = audio->in.samplerate;
1712 ghb_audio_is_passthru(gint acodec)
1714 g_debug("ghb_audio_is_passthru () \n");
1715 g_debug("acodec %d\n", acodec);
1716 return (acodec == HB_ACODEC_AC3);
1720 ghb_get_default_acodec()
1722 return HB_ACODEC_FAAC;
1726 ghb_set_scale(signal_user_data_t *ud, gint mode)
1731 GHashTable *settings = ud->settings;
1732 gboolean keep_aspect, round_dims, anamorphic;
1733 gboolean autocrop, autoscale, noscale;
1734 gint crop[4], width, height, par_width, par_height;
1735 gint crop_width, crop_height;
1736 gint aspect_n, aspect_d;
1737 gboolean keep_width = (mode == GHB_SCALE_KEEP_WIDTH);
1738 gboolean keep_height = (mode == GHB_SCALE_KEEP_HEIGHT);
1744 gint max_height = 0;
1746 g_debug("ghb_set_scale ()\n");
1748 if (h == NULL) return;
1749 list = hb_get_titles( h );
1750 if( !hb_list_count( list ) )
1752 /* No valid title, stop right there */
1755 gint titleindex = ghb_settings_get_index(settings, "title");
1756 title = hb_list_item( list, titleindex );
1757 if (title == NULL) return;
1759 if (job == NULL) return;
1761 // First configure widgets
1762 round_dims = ghb_settings_get_bool(ud->settings, "round_dimensions");
1763 anamorphic = ghb_settings_get_bool(ud->settings, "anamorphic");
1764 keep_aspect = ghb_settings_get_bool(ud->settings, "keep_aspect");
1765 autocrop = ghb_settings_get_bool(ud->settings, "autocrop");
1766 autoscale = ghb_settings_get_bool(ud->settings, "autoscale");
1767 // "Noscale" is a flag that says we prefer to crop extra to satisfy
1768 // alignment constraints rather than scaling to satisfy them.
1769 noscale = ghb_settings_get_bool(ud->settings, "noscale");
1770 // Align dimensions to either 16 or 2 pixels
1771 // The scaler crashes if the dimensions are not divisible by 2
1772 // x264 also will not accept dims that are not multiple of 2
1773 modshift = round_dims ? 4 : 1;
1774 modround = round_dims ? 8 : 1;
1778 keep_height = FALSE;
1780 if (anamorphic || keep_aspect)
1782 keep_height = FALSE;
1784 // Step needs to be at least 2 because odd widths cause scaler crash
1785 step = round_dims ? 16 : 2;
1786 widget = GHB_WIDGET (ud->builder, "scale_width");
1787 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), step, 16);
1788 widget = GHB_WIDGET (ud->builder, "scale_height");
1789 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), step, 16);
1792 ghb_title_info_t tinfo;
1794 if (ghb_get_title_info (&tinfo, titleindex))
1796 crop[0] = tinfo.crop[0];
1797 crop[1] = tinfo.crop[1];
1798 crop[2] = tinfo.crop[2];
1799 crop[3] = tinfo.crop[3];
1804 // Adjust the cropping to accomplish the desired width and height
1805 crop_width = tinfo.width - crop[2] - crop[3];
1806 crop_height = tinfo.height - crop[0] - crop[1];
1807 width = (crop_width >> modshift) << modshift;
1808 height = (crop_height >> modshift) << modshift;
1809 need1 = (crop_height - height) / 2;
1810 need2 = crop_height - height - need1;
1813 need1 = (crop_width - width) / 2;
1814 need2 = crop_width - width - need1;
1818 ghb_ui_update_int (ud, "crop_top", crop[0]);
1819 ghb_ui_update_int (ud, "crop_bottom", crop[1]);
1820 ghb_ui_update_int (ud, "crop_left", crop[2]);
1821 ghb_ui_update_int (ud, "crop_right", crop[3]);
1824 crop[0] = ghb_settings_get_int(ud->settings, "crop_top");
1825 crop[1] = ghb_settings_get_int(ud->settings, "crop_bottom");
1826 crop[2] = ghb_settings_get_int(ud->settings, "crop_left");
1827 crop[3] = ghb_settings_get_int(ud->settings, "crop_right");
1828 hb_reduce(&aspect_n, &aspect_d,
1829 title->width * title->pixel_aspect_width,
1830 title->height * title->pixel_aspect_height);
1831 crop_width = title->width - crop[2] - crop[3];
1832 crop_height = title->height - crop[0] - crop[1];
1836 height = crop_height;
1837 max_width = crop_width;
1838 max_height = crop_height;
1842 width = ghb_settings_get_int(ud->settings, "scale_width");
1843 height = ghb_settings_get_int(ud->settings, "scale_height");
1844 max_width = ghb_settings_get_int(ud->settings, "max_width");
1845 max_height = ghb_settings_get_int(ud->settings, "max_width");
1847 max_width = (max_width >> modshift) << modshift;
1848 max_height = (max_height >> modshift) << modshift;
1849 // Adjust dims according to max values
1852 max_height = crop_height;
1856 max_width = crop_width;
1858 height = MIN(height, max_height);
1859 width = MIN(width, max_width);
1860 g_debug("max_width %d, max_height %d\n", max_width, max_height);
1863 width = title->width - crop[2] - crop[3];
1865 height = title->height - crop[0] - crop[1];
1875 // The scaler crashes if the dimensions are not divisible by 2
1876 // Align mod 2. And so does something in x264_encoder_headers()
1881 job->maxHeight = max_height;
1882 job->crop[0] = crop[0]; job->crop[1] = crop[1];
1883 job->crop[2] = crop[2]; job->crop[3] = crop[3];
1884 hb_set_anamorphic_size( job, &width, &height, &par_width, &par_height );
1891 gint new_width, new_height;
1893 g_debug("kw %s kh %s\n", keep_width ? "y":"n", keep_height ? "y":"n");
1894 g_debug("w %d h %d\n", width, height);
1895 // Compute pixel aspect ration.
1896 par = (gdouble)(title->height * aspect_n) / (title->width * aspect_d);
1897 // Must scale so that par becomes 1:1
1898 // Try to keep largest dimension
1899 new_height = (crop_height * ((gdouble)width/crop_width) / par);
1900 new_width = (crop_width * ((gdouble)height/crop_height) * par);
1901 // Height and width are always multiples of 2, so do the rounding
1902 new_height = ((new_height + 1) >> 1) << 1;
1903 new_width = ((new_width + 1) >> 1) << 1;
1904 g_debug("max w %d, new w %d\n", max_width, new_width);
1905 if (max_width && (new_width > max_width))
1907 height = new_height;
1909 else if (max_height && (new_height > max_height))
1913 else if (keep_width)
1915 height = new_height;
1917 else if (keep_height)
1921 else if (width > new_width)
1923 height = new_height;
1929 g_debug("new w %d h %d\n", width, height);
1931 width = ((width + modround) >> modshift) << modshift;
1932 height = ((height + modround) >> modshift) << modshift;
1934 ghb_ui_update_int (ud, "scale_width", width);
1935 ghb_ui_update_int (ud, "scale_height", height);
1939 set_preview_job_settings(hb_job_t *job, GHashTable *settings)
1941 job->crop[0] = ghb_settings_get_int(settings, "crop_top");
1942 job->crop[1] = ghb_settings_get_int(settings, "crop_bottom");
1943 job->crop[2] = ghb_settings_get_int(settings, "crop_left");
1944 job->crop[3] = ghb_settings_get_int(settings, "crop_right");
1946 gboolean anamorphic = ghb_settings_get_bool(settings, "anamorphic");
1947 gboolean round_dimensions = ghb_settings_get_bool(settings, "round_dimensions");
1948 if (round_dimensions && anamorphic)
1951 job->pixel_ratio = 2;
1953 else if (anamorphic)
1956 job->pixel_ratio = 2;
1961 job->pixel_ratio = 0;
1963 job->width = ghb_settings_get_int(settings, "scale_width");
1964 job->height = ghb_settings_get_int(settings, "scale_height");
1965 gint deint = ghb_settings_get_int(settings, "deinterlace");
1966 gboolean decomb = ghb_settings_get_bool(settings, "decomb");
1967 job->deinterlace = (!decomb && deint == 0) ? 0 : 1;
1971 ghb_calculate_target_bitrate(GHashTable *settings, gint titleindex)
1978 if (h == NULL) return 2000;
1979 list = hb_get_titles( h );
1980 title = hb_list_item( list, titleindex );
1981 if (title == NULL) return 2000;
1983 if (job == NULL) return 2000;
1984 size = ghb_settings_get_int(settings, "video_target_size");
1985 return hb_calc_bitrate( job, size );
1989 ghb_guess_bitrate(GHashTable *settings)
1992 if (ghb_settings_get_bool(settings, "vquality_type_constant"))
1994 // This is really rough. I'm trying to err on the high
1995 // side since this is used to estimate if there is
1996 // sufficient disk space left
1997 gint vcodec = ghb_settings_get_int(settings, "video_codec");
1998 gdouble vquality = ghb_settings_get_dbl(settings, "video_quality")/100;
1999 if (vcodec == HB_VCODEC_X264 &&
2000 !ghb_settings_get_bool(settings, "linear_vquality"))
2002 vquality = 51.0 - vquality * 51.0;
2003 // Convert log curve to linear
2004 vquality = exp2((vquality-12)/6);
2005 // Don't let it go to 0
2006 if (vquality >= 31) vquality = 30;
2007 vquality = (31 - vquality) / 31;
2009 // bitrate seems to be a log relasionship to quality
2010 // with typical source material
2011 // This is a real wag
2012 bitrate = 20*1024*1024*exp10(vquality*14)/exp10(14);
2013 // Add some bits for audio
2014 bitrate += 500*1024;
2018 // Add some fudge to the bitrate to leave breathing room
2019 bitrate = ghb_settings_get_int(settings, "video_bitrate")*1024;
2020 // Add some bits for audio
2021 bitrate += 500*1024;
2027 ghb_validate_filter_string(const gchar *str, gint max_fields)
2030 gboolean in_field = FALSE;
2031 if (str == NULL || *str == 0) return TRUE;
2034 if (*str >= '0' && *str <= '9')
2039 // negative max_fields means infinate
2040 if (max_fields >= 0 && fields > max_fields) return FALSE;
2044 else if (!in_field) return FALSE;
2045 else if (*str != ':') return FALSE;
2046 else in_field = FALSE;
2053 ghb_validate_filters(signal_user_data_t *ud)
2060 tweaks = ghb_settings_get_bool(ud->settings, "allow_tweaks");
2064 str = ghb_settings_get_string(ud->settings, "tweak_detelecine");
2065 enabled = ghb_settings_get_bool(ud->settings, "detelecine");
2066 if (enabled && !ghb_validate_filter_string(str, 6))
2068 message = g_strdup_printf(
2069 "Invalid Detelecine Settings:\n\n%s\n",
2071 ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2076 str = ghb_settings_get_string(ud->settings, "tweak_decomb");
2077 enabled = ghb_settings_get_bool(ud->settings, "decomb");
2078 if (enabled && !ghb_validate_filter_string(str, 7))
2080 message = g_strdup_printf(
2081 "Invalid Decomb Settings:\n\n%s\n",
2083 ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2088 str = ghb_settings_get_string(ud->settings, "tweak_deinterlace");
2089 enabled = ghb_settings_get_bool(ud->settings, "deinterlace");
2090 if (enabled && !ghb_validate_filter_string(str, 4))
2092 message = g_strdup_printf(
2093 "Invalid Deinterlace Settings:\n\n%s\n",
2095 ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2100 str = ghb_settings_get_string(ud->settings, "tweak_deblock");
2101 enabled = ghb_settings_get_bool(ud->settings, "deblock");
2102 if (enabled && !ghb_validate_filter_string(str, 2))
2104 message = g_strdup_printf(
2105 "Invalid Deblock Settings:\n\n%s\n",
2107 ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2112 str = ghb_settings_get_string(ud->settings, "tweak_denoise");
2113 enabled = ghb_settings_get_bool(ud->settings, "denoise");
2114 if (enabled && !ghb_validate_filter_string(str, 4))
2116 message = g_strdup_printf(
2117 "Invalid Denoise Settings:\n\n%s\n",
2119 ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2128 ghb_validate_video(signal_user_data_t *ud)
2130 GHashTable *settings = ud->settings;
2134 mux = ghb_settings_get_int(settings, "container");
2135 vcodec = ghb_settings_get_int(settings, "video_codec");
2136 if ((mux == HB_MUX_MP4 || mux == HB_MUX_AVI) &&
2137 (vcodec == HB_VCODEC_THEORA))
2139 // mp4|avi/theora combination is not supported.
2140 message = g_strdup_printf(
2141 "Theora is not supported in the MP4 and AVI containers.\n\n"
2142 "You should choose a different video codec or container.\n"
2143 "If you continue, XviD will be chosen for you.");
2144 if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2150 vcodec = HB_VCODEC_XVID;
2151 ghb_ui_update_int(ud, "video_codec", vcodec);
2153 gboolean decomb = ghb_settings_get_bool(settings, "decomb");
2154 gboolean vfr = ghb_settings_get_bool(settings, "variable_frame_rate");
2157 message = g_strdup_printf(
2158 "Decomb is intended to be used in conjunction\n"
2159 "with variable frame rate.\n\n"
2160 "Would you like me to enable VFR for you?");
2161 if (ghb_message_dialog(GTK_MESSAGE_WARNING, message, "No", "Yes"))
2163 ghb_ui_update_int(ud, "variable_frame_rate", TRUE);
2171 ghb_validate_container(signal_user_data_t *ud)
2176 container = ghb_settings_get_bool(ud->settings, "container");
2177 if (container == HB_MUX_MP4)
2180 httpopt = ghb_settings_get_bool(ud->settings, "http_optimize_mp4");
2181 if (httpopt && ghb_ac3_in_audio_list(ud->audio_settings))
2183 message = g_strdup_printf(
2184 "AC3 audio in HTTP optimized MP4 is not supported.\n\n"
2185 "You should choose a different audio codec.\n"
2186 "If you continue, FAAC will be chosen for you.");
2187 if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2193 GSList *link = ud->audio_settings;
2194 while (link != NULL)
2196 GHashTable *asettings;
2197 asettings = (GHashTable*)link->data;
2198 gint acodec = ghb_settings_get_int(asettings, "audio_codec");
2199 if (acodec == HB_ACODEC_AC3)
2201 setting_value_t *value;
2202 value = get_acodec_value(HB_ACODEC_FAAC);
2203 ghb_settings_set(asettings, "audio_codec", value);
2213 ghb_validate_audio(signal_user_data_t *ud)
2217 GHashTable *settings = ud->settings;
2219 setting_value_t *value;
2221 if (h == NULL) return FALSE;
2222 list = hb_get_titles( h );
2223 if( !hb_list_count( list ) )
2225 /* No valid title, stop right there */
2226 g_message("No title found.\n");
2230 gint titleindex = ghb_settings_get_index(settings, "title");
2231 title = hb_list_item( list, titleindex );
2232 if (title == NULL) return FALSE;
2233 GSList *link = ud->audio_settings;
2234 gint mux = ghb_settings_get_int(settings, "container");
2235 while (link != NULL)
2237 GHashTable *asettings;
2238 hb_audio_config_t *taudio;
2240 asettings = (GHashTable*)link->data;
2241 gint track = ghb_settings_get_index(asettings, "audio_track");
2242 gint codec = ghb_settings_get_int(asettings, "audio_codec");
2243 taudio = (hb_audio_config_t *) hb_list_audio_config_item( title->list_audio, track );
2244 if ((taudio->in.codec != HB_ACODEC_AC3) && (codec == HB_ACODEC_AC3))
2246 // Not supported. AC3 is passthrough only, so input must be AC3
2247 message = g_strdup_printf(
2248 "The source does not support AC3 Pass-Thru.\n\n"
2249 "You should choose a different audio codec.\n"
2250 "If you continue, one will be chosen for you.");
2251 if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2257 if (mux == HB_MUX_AVI)
2259 codec = HB_ACODEC_LAME;
2263 codec = HB_ACODEC_FAAC;
2265 value = get_acodec_value(codec);
2266 ghb_settings_set(asettings, "audio_codec", value);
2268 gchar *a_unsup = NULL;
2269 gchar *mux_s = NULL;
2270 if (mux == HB_MUX_MP4)
2273 // mp4/mp3|vorbis combination is not supported.
2274 if (codec == HB_ACODEC_LAME)
2277 codec = HB_ACODEC_FAAC;
2279 if (codec == HB_ACODEC_VORBIS)
2282 codec = HB_ACODEC_FAAC;
2285 else if (mux == HB_MUX_AVI)
2288 // avi/faac|vorbis combination is not supported.
2289 if (codec == HB_ACODEC_FAAC)
2292 codec = HB_ACODEC_LAME;
2294 if (codec == HB_ACODEC_VORBIS)
2297 codec = HB_ACODEC_LAME;
2300 else if (mux == HB_MUX_OGM)
2303 // avi/faac|vorbis combination is not supported.
2304 if (codec == HB_ACODEC_FAAC)
2307 codec = HB_ACODEC_VORBIS;
2309 if (codec == HB_ACODEC_AC3)
2312 codec = HB_ACODEC_VORBIS;
2317 message = g_strdup_printf(
2318 "%s is not supported in the %s container.\n\n"
2319 "You should choose a different audio codec.\n"
2320 "If you continue, one will be chosen for you.", a_unsup, mux_s);
2321 if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2327 value = get_acodec_value(codec);
2328 ghb_settings_set(asettings, "audio_codec", value);
2330 gint mix = ghb_settings_get_int (asettings, "audio_mix");
2331 gboolean allow_mono = TRUE;
2332 gboolean allow_stereo = TRUE;
2333 gboolean allow_dolby = TRUE;
2334 gboolean allow_dpl2 = TRUE;
2335 gboolean allow_6ch = TRUE;
2337 (taudio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
2338 (codec != HB_ACODEC_LAME);
2339 gint layout = taudio->in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
2341 ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
2343 (layout == HB_INPUT_CH_LAYOUT_3F1R) ||
2344 (layout == HB_INPUT_CH_LAYOUT_3F2R) ||
2345 (layout == HB_INPUT_CH_LAYOUT_DOLBY);
2346 allow_dpl2 = (layout == HB_INPUT_CH_LAYOUT_3F2R);
2348 (taudio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
2349 (codec != HB_ACODEC_LAME) &&
2350 (layout == HB_INPUT_CH_LAYOUT_3F2R) &&
2351 (taudio->in.channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE);
2353 gchar *mix_unsup = NULL;
2354 if (mix == HB_AMIXDOWN_MONO && !allow_mono)
2358 if (mix == HB_AMIXDOWN_STEREO && !allow_stereo)
2360 mix_unsup = "stereo";
2362 if (mix == HB_AMIXDOWN_DOLBY && !allow_dolby)
2364 mix_unsup = "Dolby";
2366 if (mix == HB_AMIXDOWN_DOLBYPLII && !allow_dpl2)
2368 mix_unsup = "Dolby Pro Logic II";
2370 if (mix == HB_AMIXDOWN_6CH && !allow_6ch)
2372 mix_unsup = "6 Channel";
2376 message = g_strdup_printf(
2377 "The source audio does not support %s mixdown.\n\n"
2378 "You should choose a different mixdown.\n"
2379 "If you continue, one will be chosen for you.", mix_unsup);
2380 if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2386 mix = ghb_get_best_mix(titleindex, track, codec, mix);
2387 value = get_amix_value(mix);
2388 ghb_settings_set(asettings, "audio_mix", value);
2396 ghb_validate_vquality(GHashTable *settings)
2402 if (ghb_settings_get_bool(settings, "nocheckvquality")) return TRUE;
2403 vcodec = ghb_settings_get_int(settings, "video_codec");
2404 if (ghb_settings_get_bool(settings, "vquality_type_constant"))
2406 if (!ghb_settings_get_bool(settings, "directqp"))
2408 if (vcodec != HB_VCODEC_X264 ||
2409 ghb_settings_get_bool(settings, "linear_vquality"))
2414 else if (vcodec == HB_VCODEC_X264)
2422 if (vcodec == HB_VCODEC_X264)
2427 else if (vcodec == HB_VCODEC_FFMPEG)
2438 gint vquality = ghb_settings_get_dbl(settings, "video_quality");
2439 if (vquality < min || vquality > max)
2441 message = g_strdup_printf(
2442 "Interesting video quality choise: %d\n\n"
2443 "Typical values range from %d to %d.\n"
2444 "Are you sure you wish to use this setting?",
2445 vquality, min, max);
2446 if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message,
2447 "Cancel", "Continue"))
2459 ghb_add_job(job_settings_t *js, gint unique_id)
2464 GHashTable *settings = js->settings;
2465 static gchar *x264opts;
2467 gboolean tweaks = FALSE;
2469 g_debug("ghb_add_job()\n");
2470 if (h == NULL) return;
2471 list = hb_get_titles( h );
2472 if( !hb_list_count( list ) )
2474 /* No valid title, stop right there */
2475 g_message("No title found.\n");
2479 gint titleindex = ghb_settings_get_index(settings, "title");
2480 title = hb_list_item( list, titleindex );
2481 if (title == NULL) return;
2483 /* Set job settings */
2485 if (job == NULL) return;
2487 tweaks = ghb_settings_get_int(settings, "allow_tweaks");
2488 job->mux = ghb_settings_get_int(settings, "container");
2489 if (job->mux == HB_MUX_MP4)
2491 job->largeFileSize = ghb_settings_get_bool(settings, "large_mp4");
2492 job->mp4_optimize = ghb_settings_get_bool(settings, "http_optimize_mp4");
2496 job->largeFileSize = FALSE;
2497 job->mp4_optimize = FALSE;
2499 gint chapter_start, chapter_end;
2500 chapter_start = ghb_settings_get_int(settings, "start_chapter");
2501 chapter_end = ghb_settings_get_int(settings, "end_chapter");
2502 gint num_chapters = hb_list_count(title->list_chapter);
2503 job->chapter_start = MIN( num_chapters, chapter_start );
2504 job->chapter_end = MAX( job->chapter_start, chapter_end );
2506 job->chapter_markers = ghb_settings_get_bool(settings, "chapter_markers");
2507 if ( job->chapter_markers )
2511 for(chapter = chapter_start; chapter <= chapter_end; chapter++)
2513 hb_chapter_t * chapter_s;
2516 if (js->chapter_list == NULL || js->chapter_list[chapter-1] == 0)
2518 name = g_strdup_printf ("Chapter %2d", chapter);
2522 name = g_strdup(js->chapter_list[chapter-1]);
2524 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
2525 strncpy(chapter_s->title, name, 1023);
2526 chapter_s->title[1023] = '\0';
2530 job->crop[0] = ghb_settings_get_int(settings, "crop_top");
2531 job->crop[1] = ghb_settings_get_int(settings, "crop_bottom");
2532 job->crop[2] = ghb_settings_get_int(settings, "crop_left");
2533 job->crop[3] = ghb_settings_get_int(settings, "crop_right");
2536 gboolean decomb = ghb_settings_get_bool(settings, "decomb");
2537 gint deint = ghb_settings_get_int(settings, "deinterlace");
2539 job->deinterlace = (deint == 0) ? 0 : 1;
2541 job->deinterlace = 0;
2542 job->grayscale = ghb_settings_get_bool(settings, "grayscale");
2544 gboolean anamorphic = ghb_settings_get_bool(settings, "anamorphic");
2545 gboolean round_dimensions = ghb_settings_get_bool(settings, "round_dimensions");
2546 if (round_dimensions && anamorphic)
2548 job->pixel_ratio = 2;
2551 else if (anamorphic)
2553 // Huh! I thought I wanted to use pixel_ratio 1 for this case, but
2554 // when its 1, libhb discards the width and height and uses original
2555 // title dims - crop. Thats not what I want.
2556 // Also, x264 requires things to divisible by 2.
2557 job->pixel_ratio = 2;
2562 job->pixel_ratio = 0;
2565 job->vfr = ghb_settings_get_bool(settings, "variable_frame_rate");
2566 /* Add selected filters */
2567 job->filters = hb_list_init();
2568 if( ghb_settings_get_bool(settings, "detelecine" ) || job->vfr )
2570 hb_filter_detelecine.settings = NULL;
2574 str = ghb_settings_get_string(settings, "tweak_detelecine");
2577 hb_filter_detelecine.settings = (gchar*)str;
2580 hb_list_add( job->filters, &hb_filter_detelecine );
2584 // Use default settings
2585 hb_filter_decomb.settings = NULL;
2589 str = ghb_settings_get_string(settings, "tweak_decomb");
2592 hb_filter_decomb.settings = (gchar*)str;
2595 hb_list_add( job->filters, &hb_filter_decomb );
2597 if( job->deinterlace )
2599 hb_filter_deinterlace.settings = (gchar*)ghb_settings_get_string(settings, "deinterlace");
2603 str = ghb_settings_get_string(settings, "tweak_deinterlace");
2606 hb_filter_deinterlace.settings = (gchar*)str;
2609 hb_list_add( job->filters, &hb_filter_deinterlace );
2611 if( ghb_settings_get_bool(settings, "deblock") )
2613 hb_filter_deblock.settings = NULL;
2617 str = ghb_settings_get_string(settings, "tweak_deblock");
2620 hb_filter_deblock.settings = (gchar*)str;
2623 hb_list_add( job->filters, &hb_filter_deblock );
2625 gint denoise = ghb_settings_get_int(settings, "denoise");
2628 hb_filter_denoise.settings = (gchar*)ghb_settings_get_string(settings, "denoise");
2632 str = ghb_settings_get_string(settings, "tweak_denoise");
2635 hb_filter_denoise.settings = (gchar*)str;
2638 hb_list_add( job->filters, &hb_filter_denoise );
2640 job->width = ghb_settings_get_int(settings, "scale_width");
2641 job->height = ghb_settings_get_int(settings, "scale_height");
2643 job->vcodec = ghb_settings_get_int(settings, "video_codec");
2644 if ((job->mux == HB_MUX_MP4 || job->mux == HB_MUX_AVI) &&
2645 (job->vcodec == HB_VCODEC_THEORA))
2647 // mp4|avi/theora combination is not supported.
2648 job->vcodec = HB_VCODEC_XVID;
2650 if ((job->vcodec == HB_VCODEC_X264) && (job->mux == HB_MUX_MP4))
2652 job->ipod_atom = ghb_settings_get_bool(settings, "ipod_file");
2654 if (ghb_settings_get_bool(settings, "vquality_type_constant"))
2656 gdouble vquality = ghb_settings_get_dbl(settings, "video_quality");
2657 if (!ghb_settings_get_bool(settings, "directqp"))
2660 if (ghb_settings_get_bool(settings, "linear_vquality"))
2662 if (job->vcodec == HB_VCODEC_X264)
2664 // Adjust to same range as xvid and ffmpeg
2665 vquality = 31.0 - vquality * 31.0;
2668 // Convert linear to log curve
2669 vquality = 12 + 6 * log2(vquality);
2670 if (vquality > 51) vquality = 51;
2671 if (vquality < 1) vquality = 1;
2679 if (vquality == 0.0) vquality = 0.01;
2680 if (vquality == 1.0) vquality = 0.0;
2683 job->vquality = vquality;
2686 else if (ghb_settings_get_bool(settings, "vquality_type_bitrate"))
2688 job->vquality = -1.0;
2689 job->vbitrate = ghb_settings_get_int(settings, "video_bitrate");
2691 // AVI container does not support variable frame rate.
2692 if (job->mux == HB_MUX_AVI)
2697 gint vrate = ghb_settings_get_int(settings, "framerate");
2698 if( vrate == 0 || job->vfr )
2700 job->vrate = title->rate;
2701 job->vrate_base = title->rate_base;
2706 job->vrate = 27000000;
2707 job->vrate_base = vrate;
2710 // First remove any audios that are already in the list
2711 // This happens if you are encoding the same title a second time.
2712 gint num_audio_tracks = hb_list_count(job->list_audio);
2714 for(ii = 0; ii < num_audio_tracks; ii++)
2716 hb_audio_t *audio = (hb_audio_t*)hb_list_item(job->list_audio, 0);
2717 hb_list_rem(job->list_audio, audio);
2719 GSList *link = js->audio_settings;
2721 while (link != NULL)
2723 GHashTable *asettings;
2724 hb_audio_config_t audio;
2725 hb_audio_config_t *taudio;
2727 hb_audio_config_init(&audio);
2728 asettings = (GHashTable*)link->data;
2729 audio.in.track = ghb_settings_get_index(asettings, "audio_track");
2730 audio.out.track = count;
2731 audio.out.codec = ghb_settings_get_int(asettings, "audio_codec");
2732 taudio = (hb_audio_config_t *) hb_list_audio_config_item( title->list_audio, audio.in.track );
2733 if ((taudio->in.codec != HB_ACODEC_AC3) && (audio.out.codec == HB_ACODEC_AC3))
2735 // Not supported. AC3 is passthrough only, so input must be AC3
2736 if (job->mux == HB_MUX_AVI)
2738 audio.out.codec = HB_ACODEC_LAME;
2742 audio.out.codec = HB_ACODEC_FAAC;
2745 if ((job->mux == HB_MUX_MP4) &&
2746 ((audio.out.codec == HB_ACODEC_LAME) ||
2747 (audio.out.codec == HB_ACODEC_VORBIS)))
2749 // mp4/mp3|vorbis combination is not supported.
2750 audio.out.codec = HB_ACODEC_FAAC;
2752 if ((job->mux == HB_MUX_AVI) &&
2753 ((audio.out.codec == HB_ACODEC_FAAC) ||
2754 (audio.out.codec == HB_ACODEC_VORBIS)))
2756 // avi/faac|vorbis combination is not supported.
2757 audio.out.codec = HB_ACODEC_LAME;
2759 if ((job->mux == HB_MUX_OGM) &&
2760 ((audio.out.codec == HB_ACODEC_FAAC) ||
2761 (audio.out.codec == HB_ACODEC_AC3)))
2763 // ogm/faac|ac3 combination is not supported.
2764 audio.out.codec = HB_ACODEC_VORBIS;
2766 audio.out.dynamic_range_compression = ghb_settings_get_dbl(asettings, "audio_drc");
2767 // It would be better if this were done in libhb for us, but its not yet.
2768 if (audio.out.codec == HB_ACODEC_AC3 || audio.out.codec == HB_ACODEC_DCA)
2770 audio.out.mixdown = 0;
2774 audio.out.mixdown = ghb_settings_get_int (asettings, "audio_mix");
2775 // Make sure the mixdown is valid and pick a new one if not.
2776 audio.out.mixdown = ghb_get_best_mix(titleindex, audio.in.track, audio.out.codec,
2778 audio.out.bitrate = ghb_settings_get_int(asettings, "audio_bitrate") / 1000;
2779 gint srate = ghb_settings_get_int(asettings, "audio_rate");
2780 if (srate == 0) // 0 is same as source
2781 audio.out.samplerate = taudio->in.samplerate;
2783 audio.out.samplerate = srate;
2786 // Add it to the jobs audio list
2787 hb_audio_add( job, &audio );
2791 // I was tempted to move this up with the reset of the video quality
2792 // settings, but I suspect the settings above need to be made
2793 // first in order for hb_calc_bitrate to be accurate.
2794 if (ghb_settings_get_bool(settings, "vquality_type_target"))
2798 size = ghb_settings_get_int(settings, "video_target_size");
2799 job->vbitrate = hb_calc_bitrate( job, size );
2800 job->vquality = -1.0;
2803 job->file = ghb_settings_get_string(settings, "destination");
2804 job->crf = ghb_settings_get_bool(settings, "constant_rate_factor");
2805 // TODO: libhb holds onto a reference to the x264opts and is not
2806 // finished with it until encoding the job is done. But I can't
2807 // find a way to get at the job before it is removed in order to
2808 // free up the memory I am allocating here.
2809 // The short story is THIS LEAKS.
2810 x264opts = ghb_build_x264opts_string(settings);
2812 if( x264opts != NULL && *x264opts != '\0' )
2814 job->x264opts = x264opts;
2816 else /*avoids a bus error crash when options aren't specified*/
2818 job->x264opts = NULL;
2820 gint subtitle = ghb_settings_get_int(settings, "subtitle_lang");
2821 gboolean forced_subtitles = ghb_settings_get_bool (settings, "forced_subtitles");
2822 job->subtitle_force = forced_subtitles;
2824 job->subtitle = subtitle;
2829 // Subtitle scan. Look for subtitle matching audio language
2833 * When subtitle scan is enabled do a fast pre-scan job
2834 * which will determine which subtitles to enable, if any.
2837 job->indepth_scan = 1;
2839 x264opts_tmp = job->x264opts;
2840 job->x264opts = NULL;
2842 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
2843 *(job->select_subtitle) = NULL;
2846 * Add the pre-scan job
2848 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2850 //if (job->x264opts != NULL)
2851 // g_free(job->x264opts);
2853 job->x264opts = x264opts_tmp;
2857 job->select_subtitle = NULL;
2859 if( ghb_settings_get_bool(settings, "two_pass") &&
2860 !ghb_settings_get_bool(settings, "vquality_type_constant"))
2863 * If subtitle_scan is enabled then only turn it on
2864 * for the second pass and then off again for the
2867 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
2868 job->select_subtitle = NULL;
2870 job->indepth_scan = 0;
2871 gchar *x264opts2 = NULL;
2874 x264opts2 = g_strdup(x264opts);
2877 * If turbo options have been selected then append them
2878 * to the x264opts now (size includes one ':' and the '\0')
2880 if( ghb_settings_get_bool(settings, "turbo") )
2886 tmp_x264opts = g_strdup_printf("%s:%s", x264opts, turbo_opts);
2892 * No x264opts to modify, but apply the turbo options
2893 * anyway as they may be modifying defaults
2895 tmp_x264opts = g_strdup_printf("%s", turbo_opts);
2897 x264opts = tmp_x264opts;
2899 job->x264opts = x264opts;
2901 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2903 //if (job->x264opts != NULL)
2904 // g_free(job->x264opts);
2906 job->select_subtitle = subtitle_tmp;
2909 * On the second pass we turn off subtitle scan so that we
2910 * can actually encode using any subtitles that were auto
2911 * selected in the first pass (using the whacky select-subtitle
2912 * attribute of the job).
2914 job->indepth_scan = 0;
2915 job->x264opts = x264opts2;
2916 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2918 //if (job->x264opts != NULL)
2919 // g_free(job->x264opts);
2923 job->indepth_scan = 0;
2925 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2927 //if (job->x264opts != NULL)
2928 // g_free(job->x264opts);
2933 ghb_remove_job(gint unique_id)
2938 // Multiples passes all get the same id
2940 // Go backwards through list, so reordering doesn't screw me.
2941 ii = hb_count(h) - 1;
2942 while ((job = hb_job(h, ii--)) != NULL)
2944 if ((job->sequence_id & 0xFFFFFF) == unique_id)
2965 hb_get_state2( h, &s );
2967 if( s.state == HB_STATE_PAUSED )
2978 ghb_get_preview_image(gint titleindex, gint index, GHashTable *settings, gboolean borders)
2983 list = hb_get_titles( h );
2984 if( !hb_list_count( list ) )
2986 /* No valid title, stop right there */
2989 title = hb_list_item( list, titleindex );
2990 if (title == NULL) return NULL;
2991 if (title->job == NULL) return NULL;
2992 set_preview_job_settings(title->job, settings);
2994 // hb_get_preview can't handle sizes that are larger than the original title
2996 if (title->job->width > title->width)
2997 title->job->width = title->width;
2999 if (title->job->height > title->height)
3000 title->job->height = title->height;
3001 // And also creates artifacts if the width is not a multiple of 8
3002 //title->job->width = ((title->job->width + 4) >> 3) << 3;
3003 // And the height must be a multiple of 2
3004 //title->job->height = ((title->job->height + 1) >> 1) << 1;
3006 // Make sure we have a big enough buffer to receive the image from libhb. libhb
3007 // creates images with a one-pixel border around the original content. Hence we
3008 // add 2 pixels horizontally and vertically to the buffer size.
3009 gint srcWidth = title->width + 2;
3010 gint srcHeight= title->height + 2;
3011 gint dstWidth = title->width;
3012 gint dstHeight= title->height;
3014 gint borderLeft = 1;
3017 // |<---------- title->width ----------->|
3018 // | |<---- title->job->width ---->| |
3020 // .......................................
3021 // ....+-----------------------------+....
3022 // ....| |....<-- gray border
3025 // ....| |<------- image
3031 // ....+-----------------------------+....
3032 // .......................................
3033 dstWidth = title->job->width;
3034 dstHeight = title->job->height;
3035 borderTop = (srcHeight - dstHeight) / 2;
3036 borderLeft = (srcWidth - dstWidth) / 2;
3037 g_debug("boarders removed\n");
3040 g_debug("src %d x %d\n", srcWidth, srcHeight);
3041 g_debug("dst %d x %d\n", dstWidth, dstHeight);
3042 g_debug("job dim %d x %d\n", title->job->width, title->job->height);
3043 g_debug("title crop %d:%d:%d:%d\n",
3048 g_debug("job crop %d:%d:%d:%d\n",
3049 title->job->crop[0],
3050 title->job->crop[1],
3051 title->job->crop[2],
3052 title->job->crop[3]);
3053 static guint8 *buffer = NULL;
3054 static gint bufferSize = 0;
3057 newSize = srcWidth * srcHeight * 4;
3058 if( bufferSize < newSize )
3060 bufferSize = newSize;
3061 buffer = (guint8*) g_realloc( buffer, bufferSize );
3063 hb_get_preview( h, title, index, buffer );
3065 // Create an GdkPixbuf and copy the libhb image into it, converting it from
3066 // libhb's format something suitable. Along the way, we'll strip off the
3067 // border around libhb's image.
3069 // The image data returned by hb_get_preview is 4 bytes per pixel, BGRA format.
3070 // Alpha is ignored.
3072 GdkPixbuf *preview = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, dstWidth, dstHeight);
3073 guint8 *pixels = gdk_pixbuf_get_pixels (preview);
3075 guint32 *src = (guint32*)buffer;
3076 guint8 *dst = pixels;
3077 src += borderTop * srcWidth; // skip top rows in src to get to first row of dst
3078 src += borderLeft; // skip left pixels in src to get to first pixel of dst
3080 gint channels = gdk_pixbuf_get_n_channels (preview);
3081 gint stride = gdk_pixbuf_get_rowstride (preview);
3083 for (ii = 0; ii < dstHeight; ii++)
3086 for (jj = 0; jj < dstWidth; jj++)
3088 tmp[0] = src[0] >> 16;
3089 tmp[1] = src[0] >> 8;
3090 tmp[2] = src[0] >> 0;
3095 src += (srcWidth - dstWidth); // skip to next row in src
3097 // Got it, but hb_get_preview doesn't compensate for anamorphic, so lets
3099 gint width, height, par_width, par_height;
3100 gboolean anamorphic = ghb_settings_get_bool (settings, "anamorphic");
3103 hb_set_anamorphic_size( title->job, &width, &height, &par_width, &par_height );
3104 if (par_width > par_height)
3105 dstWidth = dstWidth * par_width / par_height;
3107 dstHeight = dstHeight * par_height / par_width;
3110 g_debug("scaled %d x %d\n", dstWidth, dstHeight);
3111 GdkPixbuf *scaled_preview;
3112 scaled_preview = gdk_pixbuf_scale_simple(preview, dstWidth, dstHeight, GDK_INTERP_HYPER);
3113 g_object_unref (preview);
3114 return scaled_preview;
3118 sanitize_volname(gchar *name)
3143 ghb_dvd_volname(const gchar *device)
3146 name = hb_dvd_name((gchar*)device);
3149 sanitize_volname(name);
3150 return g_strdup(name);