OSDN Git Service

LinGui: add a hidden feature to allow tweaking some settings. currently
[handbrake-jp/handbrake-jp-git.git] / gtk / src / hb-backend.c
1 /***************************************************************************
2  *            hb-backend.c
3  *
4  *  Fri Mar 28 10:38:44 2008
5  *  Copyright  2008  John Stebbins
6  *  <john at stebbins dot name>
7  ****************************************************************************/
8
9 /*
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.
14  * 
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.
19  * 
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
23  */
24 #define _GNU_SOURCE
25 #include <limits.h>
26 #include <math.h>
27 #include "hb.h"
28 #include "hbversion.h"
29 #include <gtk/gtk.h>
30 #include <glib/gstdio.h>
31 #include "hb-backend.h"
32 #include "settings.h"
33 #include "callbacks.h"
34
35 typedef struct
36 {
37         const gchar *option;
38         const gchar *shortOpt;
39         gint ivalue;
40         gdouble dvalue;
41         const gchar *svalue;
42 } options_map_t;
43
44 typedef struct
45 {
46         gint count;
47         options_map_t *map;
48         gint *grey_options;
49 } combo_opts_t;
50
51 static options_map_t d_container_opts[] =
52 {
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"},
58 };
59 combo_opts_t container_opts =
60 {
61         sizeof(d_container_opts)/sizeof(options_map_t),
62         d_container_opts
63 };
64
65 static options_map_t d_deint_opts[] =
66 {
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"},
71 };
72 combo_opts_t deint_opts =
73 {
74         sizeof(d_deint_opts)/sizeof(options_map_t),
75         d_deint_opts
76 };
77
78 static options_map_t d_denoise_opts[] =
79 {
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"},
84 };
85 combo_opts_t denoise_opts =
86 {
87         sizeof(d_denoise_opts)/sizeof(options_map_t),
88         d_denoise_opts
89 };
90
91 static options_map_t d_vcodec_opts[] =
92 {
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, ""},
97 };
98 combo_opts_t vcodec_opts =
99 {
100         sizeof(d_vcodec_opts)/sizeof(options_map_t),
101         d_vcodec_opts
102 };
103
104 static options_map_t d_acodec_opts[] =
105 {
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"},
110 };
111 combo_opts_t acodec_opts =
112 {
113         sizeof(d_acodec_opts)/sizeof(options_map_t),
114         d_acodec_opts
115 };
116
117 static options_map_t d_direct_opts[] =
118 {
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"},
123 };
124 combo_opts_t direct_opts =
125 {
126         sizeof(d_direct_opts)/sizeof(options_map_t),
127         d_direct_opts
128 };
129
130 static options_map_t d_me_opts[] =
131 {
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"},
136 };
137 combo_opts_t me_opts =
138 {
139         sizeof(d_me_opts)/sizeof(options_map_t),
140         d_me_opts
141 };
142
143 static options_map_t d_subme_opts[] =
144 {
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"},
152 };
153 combo_opts_t subme_opts =
154 {
155         sizeof(d_subme_opts)/sizeof(options_map_t),
156         d_subme_opts
157 };
158
159 static options_map_t d_analyse_opts[] =
160 {
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"},
165 };
166 combo_opts_t analyse_opts =
167 {
168         sizeof(d_analyse_opts)/sizeof(options_map_t),
169         d_analyse_opts
170 };
171
172 static options_map_t d_trellis_opts[] =
173 {
174         {"Disabled",          "0",    0, 0.0, "0"},
175         {"Final Macro Block", "1",    1, 0.0, "1"},
176         {"Always",            "2", 2, 0.0, "2"},
177 };
178 combo_opts_t trellis_opts =
179 {
180         sizeof(d_trellis_opts)/sizeof(options_map_t),
181         d_trellis_opts
182 };
183
184 typedef struct iso639_lang_t
185 {
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) */
191 } iso639_lang_t;
192
193 static const iso639_lang_t language_table[] =
194
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" },
381 };
382 #define LANG_TABLE_SIZE (sizeof(language_table)/ sizeof(iso639_lang_t))
383
384 static void
385 del_tree(const gchar *name, gboolean del_top)
386 {
387         const gchar *file;
388
389         if (g_file_test(name, G_FILE_TEST_IS_DIR))
390         {
391                 GDir *gdir = g_dir_open(name, 0, NULL);
392                 file = g_dir_read_name(gdir);
393                 while (file)
394                 {
395                         gchar *path;
396                         path = g_strdup_printf("%s/%s", name, file);
397                         del_tree(path, TRUE);
398                         g_free(path);
399                         file = g_dir_read_name(gdir);
400                 }
401                 if (del_top)
402                         g_rmdir(name);
403         }
404         else
405         {
406                 g_unlink(name);
407         }
408 }
409
410 const gchar*
411 ghb_version()
412 {
413         return HB_VERSION;
414 }
415
416 void
417 ghb_vquality_range(signal_user_data_t *ud, gint *min, gint *max)
418 {
419         if (ghb_settings_get_bool(ud->settings, "directqp"))
420         {
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)
424                 {
425                         *min = 0;
426                         *max = 51;
427                 }
428                 else if (vcodec == HB_VCODEC_FFMPEG)
429                 {
430                         *min = 0;
431                         *max = 31;
432                 }
433                 else
434                 {
435                         *min = 0;
436                         *max = 100;
437                 }
438         }
439         else
440         {
441                 *min = 0;
442                 *max = 100;
443         }
444 }
445
446 gint
447 ghb_lookup_acodec(const gchar *acodec)
448 {
449         gint ii;
450
451         for (ii = 0; ii < acodec_opts.count; ii++)
452         {
453                 if (strcmp(acodec_opts.map[ii].shortOpt, acodec) == 0)
454                 {
455                         return acodec_opts.map[ii].ivalue;
456                 }
457         }
458         return HB_ACODEC_FAAC;
459 }
460
461 gint
462 ghb_lookup_bitrate(const gchar *bitrate)
463 {
464         gint ii;
465
466         for (ii = 0; ii < hb_audio_bitrates_count; ii++)
467         {
468                 if (strcmp(hb_audio_bitrates[ii].string, bitrate) == 0)
469                 {
470                         return hb_audio_bitrates[ii].rate * 1000;
471                 }
472         }
473         return 160 * 1000;
474 }
475
476 gint
477 ghb_lookup_rate(const gchar *rate)
478 {
479         gint ii;
480
481         for (ii = 0; ii < hb_audio_rates_count; ii++)
482         {
483                 if (strcmp(hb_audio_rates[ii].string, rate) == 0)
484                 {
485                         return hb_audio_rates[ii].rate;
486                 }
487         }
488         // Coincidentally, the string "source" will return 0
489         // which is our flag to use "same as source"
490         return 0;
491 }
492
493 gint
494 ghb_lookup_mix(const gchar *mix)
495 {
496         gint ii;
497
498         for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
499         {
500                 if (strcmp(hb_audio_mixdowns[ii].short_name, mix) == 0)
501                 {
502                         return hb_audio_mixdowns[ii].amixdown;
503                 }
504         }
505         return HB_AMIXDOWN_DOLBYPLII;
506 }
507
508 gdouble
509 ghb_lookup_drc(const gchar *drc)
510 {
511         gdouble dval;
512         dval = g_strtod(drc, NULL);
513         if (dval < 1.0) dval = 1.0;
514         if (dval > 4.0) dval = 4.0;
515         return dval;
516 }
517
518 static setting_value_t*
519 get_acodec_value(gint val)
520 {
521         setting_value_t *value = NULL;
522         gint ii;
523
524         for (ii = 0; ii < acodec_opts.count; ii++)
525         {
526                 if (acodec_opts.map[ii].ivalue == val)
527                 {
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);
532                         value->index = ii;
533                         value->ivalue = acodec_opts.map[ii].ivalue;
534                         value->dvalue = acodec_opts.map[ii].dvalue;
535                         break;
536                 }
537         }
538         return value;
539 }
540
541 static setting_value_t*
542 get_amix_value(gint val)
543 {
544         setting_value_t *value = NULL;
545         gint ii;
546
547         for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
548         {
549                 if (hb_audio_mixdowns[ii].amixdown == val)
550                 {
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);
555                         value->index = ii;
556                         value->ivalue = hb_audio_mixdowns[ii].amixdown;
557                         value->dvalue = hb_audio_mixdowns[ii].amixdown;
558                         break;
559                 }
560         }
561         return value;
562 }
563
564 // Handle for libhb.  Gets set by ghb_backend_init()
565 static hb_handle_t * h = NULL;
566
567 extern void hb_get_tempory_directory(hb_handle_t *h, char path[512]);
568
569 void
570 ghb_hb_cleanup(gboolean partial)
571 {
572         char dir[512];
573
574         hb_get_tempory_directory(h, dir);
575         del_tree(dir, !partial);
576 }
577
578 static hb_audio_config_t*
579 get_hb_audio(gint titleindex, gint track)
580 {
581         hb_list_t  * list;
582         hb_title_t * title;
583     hb_audio_config_t *audio = NULL;
584         
585     if (h == NULL) return NULL;
586         list = hb_get_titles( h );
587         if( !hb_list_count( list ) )
588         {
589                 /* No valid title, stop right there */
590                 return NULL;
591         }
592     title = hb_list_item( list, titleindex );
593         if (title == NULL) return NULL; // Bad titleindex
594         if (!hb_list_count(title->list_audio))
595         {
596                 return NULL;
597         }
598     audio = (hb_audio_config_t *)hb_list_audio_config_item(title->list_audio, track);
599         return audio;
600 }
601
602 static gint
603 search_rates(hb_rate_t *rates, gint rate, gint count)
604 {
605         gint ii;
606         for (ii = 0; ii < count; ii++)
607         {
608                 if (rates[ii].rate == rate)
609                         return ii;
610         }
611         return -1;
612 }
613
614 static gboolean find_combo_item_by_int(GtkTreeModel *store, gint value, GtkTreeIter *iter);
615
616 static GtkListStore*
617 get_combo_box_store(GtkBuilder *builder, const gchar *name)
618 {
619         GtkComboBox *combo;
620         GtkListStore *store;
621
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));
626         return store;
627 }
628
629 static void
630 grey_combo_box_item(GtkBuilder *builder, const gchar *name, gint value, gboolean grey)
631 {
632         GtkListStore *store;
633         GtkTreeIter iter;
634         
635         store = get_combo_box_store(builder, name);
636         if (find_combo_item_by_int(GTK_TREE_MODEL(store), value, &iter))
637         {
638                 gtk_list_store_set(store, &iter, 
639                                                    1, !grey, 
640                                                    -1);
641         }
642 }
643
644 void
645 ghb_grey_combo_options(GtkBuilder *builder)
646 {
647         GtkWidget *widget;
648         gint container, track, titleindex, acodec;
649         gboolean httpopt;
650     hb_audio_config_t *audio = NULL;
651         
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);
661
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);
665
666         gboolean allow_ac3 = TRUE;
667         allow_ac3 = 
668                 !(container == HB_MUX_MP4 && httpopt) &&
669                 (container != HB_MUX_OGM);
670
671         if (allow_ac3)
672         {
673                 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_AC3, FALSE);
674         }
675         else
676         {
677                 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_AC3, TRUE);
678         }
679         if (audio && audio->in.codec != HB_ACODEC_AC3)
680         {
681                 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_AC3, TRUE);
682         }
683         grey_combo_box_item(builder, "video_codec", HB_VCODEC_THEORA, FALSE);
684
685         widget = GHB_WIDGET (builder, "audio_codec");
686         acodec = ghb_widget_int(widget);
687         if (acodec != HB_ACODEC_AC3)
688         {
689                 grey_combo_box_item(builder, "audio_mix", 0, TRUE);
690         }
691         if (container == HB_MUX_MP4)
692         {
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);
696         }
697         else if (container == HB_MUX_AVI)
698         {
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);
702         }
703         else if (container == HB_MUX_OGM)
704         {
705                 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_FAAC, TRUE);
706         }
707
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;
713         if (audio)
714         {
715                 allow_mono =
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;
719                 allow_stereo =
720                         ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
721                 allow_dolby =
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);
726                 allow_6ch =
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);
731         }
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);
737 }
738
739 gint
740 ghb_get_best_mix(gint titleindex, gint track, gint acodec, gint mix)
741 {
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;
748         
749         if (acodec & (HB_ACODEC_AC3 | HB_ACODEC_DCA))
750         {
751                 // Audio codec pass-thru.  No mixdown
752                 return 0;
753         }
754         audio = get_hb_audio(titleindex, track);
755         if (audio)
756         {
757                 allow_mono =
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;
761                 allow_stereo =
762                         ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
763                 allow_dolby =
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);
768                 allow_6ch =
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);
773         }
774         gboolean greater = FALSE;
775         if (mix == 0) 
776         {
777                 // If no mix is specified, select the best available.
778                 mix = HB_AMIXDOWN_6CH;
779         }
780         if (mix == HB_AMIXDOWN_6CH)
781         {
782                 greater = TRUE;
783                 if (allow_6ch) return HB_AMIXDOWN_6CH;
784         }
785         if (mix == HB_AMIXDOWN_DOLBYPLII || greater)
786         {
787                 greater = TRUE;
788                 if (allow_dpl2) return HB_AMIXDOWN_DOLBYPLII;
789         }
790         if (mix == HB_AMIXDOWN_DOLBY || greater)
791         {
792                 greater = TRUE;
793                 if (allow_dolby) return HB_AMIXDOWN_DOLBY;
794         }
795         if (mix == HB_AMIXDOWN_STEREO || greater)
796         {
797                 greater = TRUE;
798                 if (allow_stereo) return HB_AMIXDOWN_STEREO;
799         }
800         if (mix == HB_AMIXDOWN_MONO || greater)
801         {
802                 greater = TRUE;
803                 if (allow_mono) return HB_AMIXDOWN_MONO;
804         }
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;
809         return 0;
810 }
811
812 // Set up the model for the combo box
813 static void
814 init_combo_box(GtkBuilder *builder, const gchar *name)
815 {
816         GtkComboBox *combo;
817         GtkListStore *store;
818         GtkCellRenderer *cell;
819
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));
823         // Store contains:
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));
832
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);
838 }       
839
840 static void
841 audio_bitrate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
842 {
843         GtkTreeIter iter;
844         GtkListStore *store;
845         gint ii;
846         
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++)
851         {
852                 gtk_list_store_append(store, &iter);
853                 gtk_list_store_set(store, &iter, 
854                                                    0, rates[ii].string, 
855                                                    1, TRUE, 
856                                                    2, rates[ii].string, 
857                                                    3, rates[ii].rate * 1000, 
858                                                    4, rates[ii].string, 
859                                                    -1);
860         }
861 }
862
863 static gboolean
864 audio_bitrate_opts_clean(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
865 {
866         GtkTreeIter iter;
867         GtkListStore *store;
868         gint ivalue;
869         gboolean done = FALSE;
870         gboolean changed = FALSE;
871         
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))
875         {
876                 do
877                 {
878                         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, 3, &ivalue, -1);
879                         if (search_rates(rates, ivalue/1000, count) < 0)
880                         {
881                                 done = !gtk_list_store_remove(store, &iter);
882                                 changed = TRUE;
883                         }
884                         else
885                         {
886                                 done = !gtk_tree_model_iter_next (GTK_TREE_MODEL(store), &iter);
887                         }
888                 } while (!done);
889         }
890         return changed;
891 }
892
893 static void
894 audio_samplerate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
895 {
896         GtkTreeIter iter;
897         GtkListStore *store;
898         gint ii;
899         
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, 
906                                            0, "Same as source", 
907                                            1, TRUE, 
908                                            2, "source", 
909                                            3, 0, 
910                                            4, "source", 
911                                            -1);
912         for (ii = 0; ii < count; ii++)
913         {
914                 gtk_list_store_append(store, &iter);
915                 gtk_list_store_set(store, &iter, 
916                                                    0, rates[ii].string, 
917                                                    1, TRUE, 
918                                                    2, rates[ii].string, 
919                                                    3, rates[ii].rate, 
920                                                    4, rates[ii].string, 
921                                                    -1);
922         }
923 }
924
925 static void
926 video_rate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
927 {
928         GtkTreeIter iter;
929         GtkListStore *store;
930         gint ii;
931         
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, 
938                                            0, "Same as source", 
939                                            1, TRUE, 
940                                            2, "source", 
941                                            3, 0, 
942                                            4, "source", 
943                                            -1);
944         for (ii = 0; ii < count; ii++)
945         {
946                 gchar *desc = "";
947                 gchar *option;
948                 if (strcmp(rates[ii].string, "23.976") == 0)
949                 {
950                         desc = "(NTSC Film)";
951                 }
952                 else if (strcmp(rates[ii].string, "25") == 0)
953                 {
954                         desc = "(PAL Film/Video)";
955                 }
956                 else if (strcmp(rates[ii].string, "29.97") == 0)
957                 {
958                         desc = "(NTSC Video)";
959                 }
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, 
963                                                    0, option, 
964                                                    1, TRUE, 
965                                                    2, rates[ii].string, 
966                                                    3, rates[ii].rate, 
967                                                    4, rates[ii].string, 
968                                                    -1);
969                 g_free(option);
970         }
971 }
972
973 static void
974 mix_opts_set(GtkBuilder *builder, const gchar *name)
975 {
976         GtkTreeIter iter;
977         GtkListStore *store;
978         gint ii;
979         
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, 
985                                            0, "None", 
986                                            1, TRUE, 
987                                            2, "none", 
988                                            3, 0, 
989                                            4, "none", 
990                                            -1);
991         for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
992         {
993                 gtk_list_store_append(store, &iter);
994                 gtk_list_store_set(store, &iter, 
995                                                    0, hb_audio_mixdowns[ii].human_readable_name, 
996                                                    1, TRUE, 
997                                                    2, hb_audio_mixdowns[ii].short_name, 
998                                                    3, hb_audio_mixdowns[ii].amixdown, 
999                                                    4, hb_audio_mixdowns[ii].internal_name, 
1000                                                    -1);
1001         }
1002 }
1003
1004 static void
1005 language_opts_set(GtkBuilder *builder, const gchar *name)
1006 {
1007         GtkTreeIter iter;
1008         GtkListStore *store;
1009         gint ii;
1010         
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++)
1015         {
1016                 gtk_list_store_append(store, &iter);
1017                 gtk_list_store_set(store, &iter, 
1018                                                    0, language_table[ii].eng_name, 
1019                                                    1, TRUE, 
1020                                                    2, language_table[ii].iso639_2, 
1021                                                    3, ii, 
1022                                                    4, language_table[ii].iso639_1, 
1023                                                    -1);
1024         }
1025 }
1026
1027 void
1028 title_opts_set(GtkBuilder *builder, const gchar *name)
1029 {
1030         GtkTreeIter iter;
1031         GtkListStore *store;
1032         hb_list_t  * list;
1033         hb_title_t * title;
1034         gint ii;
1035         gint count = 0;
1036         
1037         g_debug("title_opts_set ()\n");
1038         store = get_combo_box_store(builder, name);
1039         gtk_list_store_clear(store);
1040         if (h != NULL)
1041         {
1042                 list = hb_get_titles( h );
1043                 count = hb_list_count( list );
1044                 if (count > 100) count = 100;
1045         }
1046         if( count <= 0 )
1047         {
1048                 // No titles.  Fill in a default.
1049                 gtk_list_store_append(store, &iter);
1050                 gtk_list_store_set(store, &iter, 
1051                                                    0, "No Titles", 
1052                                                    1, TRUE, 
1053                                                    2, "none", 
1054                                                    3, -1, 
1055                                                    4, "none", 
1056                                                    -1);
1057                 return;
1058         }
1059         for (ii = 0; ii < count; ii++)
1060         {
1061                 gchar *option;
1062                 
1063                 title = (hb_title_t*)hb_list_item(list, ii);
1064                 if (title->duration != 0)
1065                 {
1066                         option  = g_strdup_printf ("%d - %02dh%02dm%02ds",
1067                                 title->index, title->hours, title->minutes, title->seconds);
1068                 }
1069                 else
1070                 {
1071                         option  = g_strdup_printf ("%d - Unknown Length", title->index);
1072                 }
1073                 gtk_list_store_append(store, &iter);
1074                 gtk_list_store_set(store, &iter, 
1075                                                    0, option, 
1076                                                    1, TRUE, 
1077                                                    2, option, 
1078                                                    3, ii, 
1079                                                    4, option, 
1080                                                    -1);
1081         }
1082 }
1083
1084 static gboolean
1085 find_combo_item_by_int(GtkTreeModel *store, gint value, GtkTreeIter *iter)
1086 {
1087         gint ivalue;
1088         gboolean foundit = FALSE;
1089         
1090         if (gtk_tree_model_get_iter_first (store, iter))
1091         {
1092                 do
1093                 {
1094                         gtk_tree_model_get(store, iter, 3, &ivalue, -1);
1095                         if (value == ivalue)
1096                         {
1097                                 foundit = TRUE;
1098                                 break;
1099                         }
1100                 } while (gtk_tree_model_iter_next (store, iter));
1101         }
1102         return foundit;
1103 }
1104
1105 static gboolean
1106 audio_rate_opts_add(GtkBuilder *builder, const gchar *name, gint rate)
1107 {
1108         GtkTreeIter iter;
1109         GtkListStore *store;
1110         gchar *str;
1111         
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))
1115         {
1116                 str = g_strdup_printf ("%.8g", (gdouble)rate/1000.0);
1117                 gtk_list_store_append(store, &iter);
1118                 gtk_list_store_set(store, &iter, 
1119                                                    0, str, 
1120                                                    1, TRUE, 
1121                                                    2, str, 
1122                                                    3, rate, 
1123                                                    4, str, 
1124                                                    -1);
1125                 return TRUE;
1126         }
1127         return FALSE;
1128 }
1129
1130 void
1131 audio_track_opts_set(GtkBuilder *builder, const gchar *name, gint titleindex)
1132 {
1133         GtkTreeIter iter;
1134         GtkListStore *store;
1135         hb_list_t  * list;
1136         hb_title_t * title;
1137     hb_audio_config_t * audio;
1138         gint ii;
1139         gint count = 0;
1140         
1141         g_debug("audio_track_opts_set ()\n");
1142         store = get_combo_box_store(builder, name);
1143         gtk_list_store_clear(store);
1144         if (h != NULL)
1145         {
1146                 list = hb_get_titles( h );
1147             title = (hb_title_t*)hb_list_item( list, titleindex );
1148                 if (title != NULL)
1149                 {
1150                         count = hb_list_count( title->list_audio );
1151                 }
1152         }
1153         if (count > 10) count = 10;
1154         if( count <= 0 )
1155         {
1156                 // No audio. set some default
1157                 gtk_list_store_append(store, &iter);
1158                 gtk_list_store_set(store, &iter, 
1159                                                    0, "No Audio", 
1160                                                    1, TRUE, 
1161                                                    2, "none", 
1162                                                    3, -1, 
1163                                                    4, "none", 
1164                                                    -1);
1165                 return;
1166         }
1167         for (ii = 0; ii < count; ii++)
1168         {
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, 
1173                                                    1, TRUE, 
1174                                                    2, audio->lang.description, 
1175                                                    3, ii, 
1176                                                    4, audio->lang.description, 
1177                                                    -1);
1178         }
1179 }
1180
1181 void
1182 subtitle_opts_set(GtkBuilder *builder, const gchar *name, gint titleindex)
1183 {
1184         GtkTreeIter iter;
1185         GtkListStore *store;
1186         hb_list_t  * list;
1187         hb_title_t * title;
1188     hb_subtitle_t * subtitle;
1189         gint ii;
1190         gint count = 0;
1191         
1192         g_debug("subtitle_opts_set ()\n");
1193         store = get_combo_box_store(builder, name);
1194         gtk_list_store_clear(store);
1195         if (h != NULL)
1196         {
1197                 list = hb_get_titles( h );
1198             title = (hb_title_t*)hb_list_item( list, titleindex );
1199                 if (title != NULL)
1200                 {
1201                         count = hb_list_count( title->list_subtitle );
1202                 }
1203         }
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, 
1208                                            0, "None", 
1209                                            1, TRUE, 
1210                                            2, "none", 
1211                                            3, -2, 
1212                                            4, "none", 
1213                                            -1);
1214         gtk_list_store_append(store, &iter);
1215         gtk_list_store_set(store, &iter, 
1216                                            0, "Same As Audio", 
1217                                            1, TRUE, 
1218                                            2, "auto", 
1219                                            3, -1, 
1220                                            4, "auto", 
1221                                            -1);
1222         for (ii = 0; ii < count; ii++)
1223         {
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, 
1227                                                    0, subtitle->lang, 
1228                                                    1, TRUE, 
1229                                                    2, subtitle->iso639_2, 
1230                                                    3, ii, 
1231                                                    4, subtitle->iso639_2, 
1232                                                    -1);
1233         }
1234         if (titleindex == -1)
1235         {
1236                 for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
1237                 {
1238                         gtk_list_store_append(store, &iter);
1239                         gtk_list_store_set(store, &iter, 
1240                                 0, language_table[ii].eng_name, 
1241                                 1, TRUE, 
1242                                 2, language_table[ii].iso639_2, 
1243                                 3, ii, 
1244                                 4, language_table[ii].iso639_2, 
1245                                 -1);
1246                 }
1247         }
1248 }
1249
1250 gint
1251 ghb_longest_title()
1252 {
1253         hb_list_t  * list;
1254         hb_title_t * title;
1255         gint ii;
1256         gint count = 0;
1257         guint64 longest = 0;
1258         gint titleindex = 0;
1259         
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++)
1266         {
1267                 title = (hb_title_t*)hb_list_item(list, ii);
1268                 if (title->duration > longest)
1269                 {
1270                         titleindex = ii;
1271                         longest = title->duration;
1272                 }
1273         }
1274         return titleindex;
1275 }
1276
1277 gint
1278 ghb_find_audio_track(gint titleindex, const gchar *lang, gint index)
1279 {
1280         hb_list_t  * list;
1281         hb_title_t * title;
1282     hb_audio_config_t * audio;
1283         gint ii;
1284         gint count = 0;
1285         gint track = -1;
1286         gint match = 0;
1287         
1288         g_debug("find_audio_track ()\n");
1289         if (h != NULL)
1290         {
1291                 list = hb_get_titles( h );
1292             title = (hb_title_t*)hb_list_item( list, titleindex );
1293                 if (title != NULL)
1294                 {
1295                         count = hb_list_count( title->list_audio );
1296                 }
1297         }
1298         if (count > 10) count = 10;
1299         for (ii = 0; ii < count; ii++)
1300         {
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))
1304                 {
1305                         if (index == match)
1306                         {
1307                                 track = ii;
1308                                 break;
1309                         }
1310                         match++;
1311                 }
1312         }
1313         return track;
1314 }
1315
1316 static void
1317 generic_opts_set(GtkBuilder *builder, const gchar *name, combo_opts_t *opts)
1318 {
1319         GtkTreeIter iter;
1320         GtkListStore *store;
1321         gint ii;
1322         
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++)
1327         {
1328                 gtk_list_store_append(store, &iter);
1329                 gtk_list_store_set(store, &iter, 
1330                                                    0, opts->map[ii].option, 
1331                                                    1, TRUE, 
1332                                                    2, opts->map[ii].shortOpt, 
1333                                                    3, opts->map[ii].ivalue, 
1334                                                    4, opts->map[ii].svalue, 
1335                                                    -1);
1336         }
1337 }
1338
1339 void
1340 ghb_update_ui_combo_box(GtkBuilder *builder, const gchar *name, gint user_data, gboolean all)
1341 {
1342         GtkComboBox *combo = NULL;
1343         gint signal_id;
1344         gint handler_id = 0;
1345
1346         g_debug("ghb_update_ui_combo_box() %s\n", name);
1347         if (name != NULL)
1348         {               
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);
1354                 if (signal_id > 0)
1355                 {
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);
1359                         if (handler_id > 0)
1360                         {
1361                                 // This should also always succeed
1362                                 g_signal_handler_block ((gpointer)combo, handler_id);
1363                         }
1364                 }
1365         }       
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);
1402         if (handler_id > 0)
1403         {
1404                 g_signal_handler_unblock ((gpointer)combo, handler_id);
1405         }
1406 }
1407         
1408 static void
1409 init_ui_combo_boxes(GtkBuilder *builder)
1410 {
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");
1429 }
1430         
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";
1434
1435 // Construct the x264 options string
1436 // The result is allocated, so someone must free it at some point.
1437 gchar*
1438 ghb_build_x264opts_string(GHashTable *settings)
1439 {
1440         gchar *result;
1441         const gchar *opts = ghb_settings_get_string(settings, "x264_options");
1442         if (opts != NULL)
1443         {
1444                 result = g_strdup(opts);
1445         }
1446         else
1447         {
1448                 result = g_strdup("");
1449         }
1450         return result;
1451 }
1452
1453 gchar **
1454 ghb_get_chapters(gint titleindex)
1455 {
1456         hb_list_t  * list;
1457         hb_title_t * title;
1458     hb_chapter_t * chapter;
1459         gint count, ii;
1460         gchar **result = NULL;
1461         
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++)
1470         {
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);
1475                 else 
1476                         result[ii] = g_strdup(chapter->title);
1477         }
1478         result[ii] = NULL;
1479         return result;
1480 }
1481
1482 gboolean
1483 ghb_ac3_in_audio_list(GSList *audio_list)
1484 {
1485         GSList *link;
1486
1487         link = audio_list;
1488         while (link != NULL)
1489         {
1490                 GHashTable *asettings;
1491                 gint acodec;
1492
1493                 asettings = (GHashTable*)link->data;
1494                 acodec = ghb_settings_get_int(asettings, "audio_codec");
1495                 if (acodec == HB_ACODEC_AC3)
1496                         return TRUE;
1497                 link = link->next;
1498         }
1499         return FALSE;
1500 }
1501
1502 gboolean
1503 ghb_set_passthru_rate_opts(GtkBuilder *builder, gint bitrate)
1504 {
1505         gboolean changed = FALSE;
1506         changed = audio_rate_opts_add(builder, "audio_bitrate", bitrate);
1507         return changed;
1508 }
1509
1510 gboolean
1511 ghb_set_default_rate_opts(GtkBuilder *builder)
1512 {
1513         gboolean changed = FALSE;
1514         changed = audio_bitrate_opts_clean(builder, "audio_bitrate", hb_audio_bitrates, hb_audio_bitrates_count);
1515         return changed;
1516 }
1517
1518 static ghb_status_t hb_status;
1519
1520 void
1521 ghb_backend_init(GtkBuilder *builder, gint debug, gint update)
1522 {
1523     /* Init libhb */
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);
1529 }
1530
1531 void
1532 ghb_backend_scan(const gchar *path, gint titleindex)
1533 {
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;
1540 }
1541
1542 gint
1543 ghb_get_state()
1544 {
1545         return hb_status.state;
1546 }
1547
1548 void
1549 ghb_clear_state(gint state)
1550 {
1551         hb_status.state &= ~state;
1552 }
1553
1554 void
1555 ghb_set_state(gint state)
1556 {
1557         hb_status.state |= state;
1558 }
1559
1560 void
1561 ghb_get_status(ghb_status_t *status)
1562 {
1563         memcpy(status, &hb_status, sizeof(ghb_status_t));
1564 }
1565
1566 void 
1567 ghb_track_status()
1568 {
1569     hb_state_t s;
1570         static gint scan_complete_count = 0;
1571         gint scans;
1572
1573         if (h == NULL) return;
1574     hb_get_state( h, &s );
1575         scans = hb_get_scancount(h);
1576         if (scans > scan_complete_count)
1577         {
1578                 hb_status.state &= ~GHB_STATE_SCANNING;
1579                 hb_status.state |= GHB_STATE_SCANDONE;
1580                 scan_complete_count = hb_get_scancount(h);
1581         }
1582         switch( s.state )
1583     {
1584         case HB_STATE_IDLE:
1585             /* Nothing to do */
1586             break;
1587
1588 #define p s.param.scanning
1589         case HB_STATE_SCANNING:
1590                 {
1591                         hb_status.state |= GHB_STATE_SCANNING;
1592                         hb_status.title_count = p.title_count;
1593                         hb_status.title_cur = p.title_cur;
1594                 } break;
1595 #undef p
1596
1597         case HB_STATE_SCANDONE:
1598         {
1599                         hb_status.state &= ~GHB_STATE_SCANNING;
1600                         hb_status.state |= GHB_STATE_SCANDONE;
1601         } break;
1602
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;
1616             break;
1617 #undef p
1618
1619         case HB_STATE_PAUSED:
1620                         hb_status.state |= GHB_STATE_PAUSED;
1621             break;
1622                                 
1623 #define p s.param.muxing
1624         case HB_STATE_MUXING:
1625         {
1626                         hb_status.state |= GHB_STATE_MUXING;
1627         } break;
1628 #undef p
1629
1630 #define p s.param.workdone
1631         case HB_STATE_WORKDONE:
1632                 {
1633             hb_job_t *job;
1634
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;
1639                         switch (p.error)
1640                         {
1641                         case HB_ERROR_NONE:
1642                                 hb_status.error = GHB_ERROR_NONE;
1643                         case HB_ERROR_CANCELED:
1644                                 hb_status.error = GHB_ERROR_CANCELED;
1645                         default:
1646                                 hb_status.error = GHB_ERROR_FAIL;
1647                         }
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) ) 
1655                 hb_rem( h, job );
1656                 } break;
1657 #undef p
1658     }
1659 }
1660
1661 gboolean
1662 ghb_get_title_info(ghb_title_info_t *tinfo, gint titleindex)
1663 {
1664         hb_list_t  * list;
1665         hb_title_t * title;
1666         
1667     if (h == NULL) return FALSE;
1668         list = hb_get_titles( h );
1669         if( !hb_list_count( list ) )
1670         {
1671                 /* No valid title, stop right there */
1672                 return FALSE;
1673         }
1674
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;
1695         return TRUE;
1696 }
1697
1698 gboolean
1699 ghb_get_audio_info(ghb_audio_info_t *ainfo, gint titleindex, gint audioindex)
1700 {
1701     hb_audio_config_t *audio;
1702         
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;
1708         return TRUE;
1709 }
1710
1711 gboolean
1712 ghb_audio_is_passthru(gint acodec)
1713 {
1714         g_debug("ghb_audio_is_passthru () \n");
1715         g_debug("acodec %d\n", acodec);
1716         return (acodec == HB_ACODEC_AC3);
1717 }
1718
1719 gint
1720 ghb_get_default_acodec()
1721 {
1722         return HB_ACODEC_FAAC;
1723 }
1724
1725 void
1726 ghb_set_scale(signal_user_data_t *ud, gint mode)
1727 {
1728         hb_list_t  * list;
1729         hb_title_t * title;
1730         hb_job_t   * job;
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);
1739         gint step;
1740         GtkWidget *widget;
1741         gint modshift;
1742         gint modround;
1743         gint max_width = 0;
1744         gint max_height = 0;
1745         
1746         g_debug("ghb_set_scale ()\n");
1747
1748         if (h == NULL) return;
1749         list = hb_get_titles( h );
1750         if( !hb_list_count( list ) )
1751         {
1752                 /* No valid title, stop right there */
1753                 return;
1754         }
1755         gint titleindex = ghb_settings_get_index(settings, "title");
1756     title = hb_list_item( list, titleindex );
1757         if (title == NULL) return;
1758         job   = title->job;
1759         if (job == NULL) return;
1760         
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;
1775         if (autoscale)
1776         {
1777                 keep_width = FALSE;
1778                 keep_height = FALSE;
1779         }
1780         if (anamorphic || keep_aspect)
1781         {
1782                 keep_height = FALSE;
1783         }
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);
1790         if (autocrop)
1791         {
1792                 ghb_title_info_t tinfo;
1793
1794                 if (ghb_get_title_info (&tinfo, titleindex))
1795                 {
1796                         crop[0] = tinfo.crop[0];
1797                         crop[1] = tinfo.crop[1];
1798                         crop[2] = tinfo.crop[2];
1799                         crop[3] = tinfo.crop[3];
1800                         if (noscale)
1801                         {
1802                                 gint need1, need2;
1803
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;
1811                                 crop[0] += need1;
1812                                 crop[1] += need2;
1813                                 need1 = (crop_width - width) / 2;
1814                                 need2 = crop_width - width - need1;
1815                                 crop[2] += need1;
1816                                 crop[3] += need2;
1817                         }
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]);
1822                 }
1823         }
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];
1833         if (autoscale)
1834         {
1835                 width = crop_width;
1836                 height = crop_height;
1837                 max_width = crop_width;
1838                 max_height = crop_height;
1839         }
1840         else
1841         {
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");
1846                 // Align max dims 
1847                 max_width = (max_width >> modshift) << modshift;
1848                 max_height = (max_height >> modshift) << modshift;
1849                 // Adjust dims according to max values
1850                 if (!max_height)
1851                 {
1852                         max_height = crop_height;
1853                 }
1854                 if (!max_width)
1855                 {
1856                         max_width = crop_width;
1857                 }
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);
1861         }
1862         if (width < 16)
1863                 width = title->width - crop[2] - crop[3];
1864         if (height < 16)
1865                 height = title->height - crop[0] - crop[1];
1866
1867         if (anamorphic)
1868         {
1869                 if (round_dims)
1870                 {
1871                         job->modulus = 0;
1872                 }
1873                 else
1874                 {
1875                         // The scaler crashes if the dimensions are not divisible by 2
1876                         // Align mod 2.  And so does something in x264_encoder_headers()
1877                         job->modulus = 2;
1878                 }
1879                 job->width = width;
1880                 if (max_height) 
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 );
1885         }
1886         else 
1887         {
1888                 if (keep_aspect)
1889                 {
1890                         gdouble par;
1891                         gint new_width, new_height;
1892                         
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))
1906                         {
1907                                 height = new_height;
1908                         }
1909                         else if (max_height && (new_height > max_height))
1910                         {
1911                                 width = new_width;
1912                         }
1913                         else if (keep_width)
1914                         {
1915                                 height = new_height;
1916                         }
1917                         else if (keep_height)
1918                         {
1919                                 width = new_width;
1920                         }
1921                         else if (width > new_width)
1922                         {
1923                                 height = new_height;
1924                         }
1925                         else
1926                         {
1927                                 width = new_width;
1928                         }
1929                         g_debug("new w %d h %d\n", width, height);
1930                 }
1931                 width = ((width + modround) >> modshift) << modshift;
1932                 height = ((height + modround) >> modshift) << modshift;
1933         }
1934         ghb_ui_update_int (ud, "scale_width", width);
1935         ghb_ui_update_int (ud, "scale_height", height);
1936 }
1937
1938 static void
1939 set_preview_job_settings(hb_job_t *job, GHashTable *settings)
1940 {
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");
1945
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)
1949         {
1950                 job->modulus = 16;
1951                 job->pixel_ratio = 2;
1952         }
1953         else if (anamorphic)
1954         {
1955                 job->modulus = 2;
1956                 job->pixel_ratio = 2;
1957         }
1958         else
1959         {
1960                 job->modulus = 2;
1961                 job->pixel_ratio = 0;
1962         }
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;
1968 }
1969
1970 gint
1971 ghb_calculate_target_bitrate(GHashTable *settings, gint titleindex)
1972 {
1973         hb_list_t  * list;
1974         hb_title_t * title;
1975         hb_job_t   * job;
1976         gint size;
1977
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;
1982         job   = title->job;
1983         if (job == NULL) return 2000;
1984         size = ghb_settings_get_int(settings, "video_target_size");
1985         return hb_calc_bitrate( job, size );
1986 }
1987
1988 gint
1989 ghb_guess_bitrate(GHashTable *settings)
1990 {
1991         gint bitrate;
1992         if (ghb_settings_get_bool(settings, "vquality_type_constant"))
1993         {
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"))
2001                 {
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;
2008                 }
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;
2015         }
2016         else
2017         {
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;
2022         }
2023         return bitrate;
2024 }
2025
2026 gboolean
2027 ghb_validate_filter_string(const gchar *str, gint max_fields)
2028 {
2029         gint fields = 0;
2030         gboolean in_field = FALSE;
2031         if (str == NULL || *str == 0) return TRUE;
2032         while (*str)
2033         {
2034                 if (*str >= '0' && *str <= '9')
2035                 {
2036                         if (!in_field)
2037                         {
2038                                 fields++;
2039                                 // negative max_fields means infinate
2040                                 if (max_fields >= 0 && fields > max_fields) return FALSE;
2041                                 in_field = TRUE;
2042                         }
2043                 }
2044                 else if (!in_field) return FALSE;
2045                 else if (*str != ':') return FALSE;
2046                 else in_field = FALSE;
2047                 str++;
2048         }
2049         return TRUE;
2050 }
2051
2052 gboolean
2053 ghb_validate_filters(signal_user_data_t *ud)
2054 {
2055         gboolean tweaks;
2056         const gchar *str;
2057         gchar *message;
2058         gboolean enabled;
2059
2060         tweaks = ghb_settings_get_bool(ud->settings, "allow_tweaks");
2061         if (tweaks)
2062         {
2063                 // detele 6
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))
2067                 {
2068                         message = g_strdup_printf(
2069                                                 "Invalid Detelecine Settings:\n\n%s\n",
2070                                                 str);
2071                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2072                         g_free(message);
2073                         return FALSE;
2074                 }
2075                 // decomb 7
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))
2079                 {
2080                         message = g_strdup_printf(
2081                                                 "Invalid Decomb Settings:\n\n%s\n",
2082                                                 str);
2083                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2084                         g_free(message);
2085                         return FALSE;
2086                 }
2087                 // deinte 4
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))
2091                 {
2092                         message = g_strdup_printf(
2093                                                 "Invalid Deinterlace Settings:\n\n%s\n",
2094                                                 str);
2095                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2096                         g_free(message);
2097                         return FALSE;
2098                 }
2099                 // debloc 2
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))
2103                 {
2104                         message = g_strdup_printf(
2105                                                 "Invalid Deblock Settings:\n\n%s\n",
2106                                                 str);
2107                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2108                         g_free(message);
2109                         return FALSE;
2110                 }
2111                 // denois 4
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))
2115                 {
2116                         message = g_strdup_printf(
2117                                                 "Invalid Denoise Settings:\n\n%s\n",
2118                                                 str);
2119                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2120                         g_free(message);
2121                         return FALSE;
2122                 }
2123         }
2124         return TRUE;
2125 }
2126
2127 gboolean
2128 ghb_validate_video(signal_user_data_t *ud)
2129 {
2130         GHashTable *settings = ud->settings;
2131         gint vcodec, mux;
2132         gchar *message;
2133
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))
2138         {
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"))
2145                 {
2146                         g_free(message);
2147                         return FALSE;
2148                 }
2149                 g_free(message);
2150                 vcodec = HB_VCODEC_XVID;
2151                 ghb_ui_update_int(ud, "video_codec", vcodec);
2152         }
2153         gboolean decomb = ghb_settings_get_bool(settings, "decomb");
2154         gboolean vfr = ghb_settings_get_bool(settings, "variable_frame_rate");
2155         if (decomb && !vfr)
2156         {
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"))
2162                 {
2163                         ghb_ui_update_int(ud, "variable_frame_rate", TRUE);
2164                 }
2165                 g_free(message);
2166         }
2167         return TRUE;
2168 }
2169
2170 gboolean
2171 ghb_validate_container(signal_user_data_t *ud)
2172 {
2173         gint container;
2174         gchar *message;
2175
2176         container = ghb_settings_get_bool(ud->settings, "container");
2177         if (container == HB_MUX_MP4)
2178         {
2179                 gboolean httpopt;
2180                 httpopt = ghb_settings_get_bool(ud->settings, "http_optimize_mp4");
2181                 if (httpopt && ghb_ac3_in_audio_list(ud->audio_settings))
2182                 {
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"))
2188                         {
2189                                 g_free(message);
2190                                 return FALSE;
2191                         }
2192                         g_free(message);
2193                         GSList *link = ud->audio_settings;
2194                         while (link != NULL)
2195                         {
2196                                 GHashTable *asettings;
2197                                 asettings = (GHashTable*)link->data;
2198                                 gint acodec = ghb_settings_get_int(asettings, "audio_codec");
2199                                 if (acodec == HB_ACODEC_AC3)
2200                                 {
2201                                         setting_value_t *value;
2202                                         value = get_acodec_value(HB_ACODEC_FAAC);
2203                                         ghb_settings_set(asettings, "audio_codec", value);
2204                                 }
2205                                 link = link->next;
2206                         }
2207                 }
2208         }
2209         return TRUE;
2210 }
2211
2212 gboolean
2213 ghb_validate_audio(signal_user_data_t *ud)
2214 {
2215         hb_list_t  * list;
2216         hb_title_t * title;
2217         GHashTable *settings = ud->settings;
2218         gchar *message;
2219         setting_value_t *value;
2220
2221         if (h == NULL) return FALSE;
2222         list = hb_get_titles( h );
2223         if( !hb_list_count( list ) )
2224         {
2225                 /* No valid title, stop right there */
2226                 g_message("No title found.\n");
2227                 return FALSE;
2228         }
2229
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)
2236         {
2237                 GHashTable *asettings;
2238             hb_audio_config_t *taudio;
2239
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))
2245                 {
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"))
2252                         {
2253                                 g_free(message);
2254                                 return FALSE;
2255                         }
2256                         g_free(message);
2257                         if (mux == HB_MUX_AVI)
2258                         {
2259                                 codec = HB_ACODEC_LAME;
2260                         }
2261                         else
2262                         {
2263                                 codec = HB_ACODEC_FAAC;
2264                         }
2265                         value = get_acodec_value(codec);
2266                         ghb_settings_set(asettings, "audio_codec", value);
2267                 }
2268                 gchar *a_unsup = NULL;
2269                 gchar *mux_s = NULL;
2270                 if (mux == HB_MUX_MP4)
2271                 { 
2272                         mux_s = "MP4";
2273                         // mp4/mp3|vorbis combination is not supported.
2274                         if (codec == HB_ACODEC_LAME)
2275                         {
2276                                 a_unsup = "MP3";
2277                                 codec = HB_ACODEC_FAAC;
2278                         }
2279                         if (codec == HB_ACODEC_VORBIS)
2280                         {
2281                                 a_unsup = "Vorbis";
2282                                 codec = HB_ACODEC_FAAC;
2283                         }
2284                 }
2285                 else if (mux == HB_MUX_AVI)
2286                 {
2287                         mux_s = "AVI";
2288                         // avi/faac|vorbis combination is not supported.
2289                         if (codec == HB_ACODEC_FAAC)
2290                         {
2291                                 a_unsup = "FAAC";
2292                                 codec = HB_ACODEC_LAME;
2293                         }
2294                         if (codec == HB_ACODEC_VORBIS)
2295                         {
2296                                 a_unsup = "Vorbis";
2297                                 codec = HB_ACODEC_LAME;
2298                         }
2299                 }
2300                 else if (mux == HB_MUX_OGM)
2301                 {
2302                         mux_s = "OGM";
2303                         // avi/faac|vorbis combination is not supported.
2304                         if (codec == HB_ACODEC_FAAC)
2305                         {
2306                                 a_unsup = "FAAC";
2307                                 codec = HB_ACODEC_VORBIS;
2308                         }
2309                         if (codec == HB_ACODEC_AC3)
2310                         {
2311                                 a_unsup = "AC-3";
2312                                 codec = HB_ACODEC_VORBIS;
2313                         }
2314                 }
2315                 if (a_unsup)
2316                 {
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"))
2322                         {
2323                                 g_free(message);
2324                                 return FALSE;
2325                         }
2326                         g_free(message);
2327                         value = get_acodec_value(codec);
2328                         ghb_settings_set(asettings, "audio_codec", value);
2329                 }
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;
2336                 allow_mono =
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;
2340                 allow_stereo =
2341                         ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
2342                 allow_dolby =
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);
2347                 allow_6ch =
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);
2352
2353                 gchar *mix_unsup = NULL;
2354                 if (mix == HB_AMIXDOWN_MONO && !allow_mono)
2355                 {
2356                         mix_unsup = "mono";
2357                 }
2358                 if (mix == HB_AMIXDOWN_STEREO && !allow_stereo)
2359                 {
2360                         mix_unsup = "stereo";
2361                 }
2362                 if (mix == HB_AMIXDOWN_DOLBY && !allow_dolby)
2363                 {
2364                         mix_unsup = "Dolby";
2365                 }
2366                 if (mix == HB_AMIXDOWN_DOLBYPLII && !allow_dpl2)
2367                 {
2368                         mix_unsup = "Dolby Pro Logic II";
2369                 }
2370                 if (mix == HB_AMIXDOWN_6CH && !allow_6ch)
2371                 {
2372                         mix_unsup = "6 Channel";
2373                 }
2374                 if (mix_unsup)
2375                 {
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"))
2381                         {
2382                                 g_free(message);
2383                                 return FALSE;
2384                         }
2385                         g_free(message);
2386                         mix = ghb_get_best_mix(titleindex, track, codec, mix);
2387                         value = get_amix_value(mix);
2388                         ghb_settings_set(asettings, "audio_mix", value);
2389                 }
2390                 link = link->next;
2391         }
2392         return TRUE;
2393 }
2394
2395 gboolean
2396 ghb_validate_vquality(GHashTable *settings)
2397 {
2398         gint vcodec;
2399         gchar *message;
2400         gint min, max;
2401
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"))
2405         {
2406                 if (!ghb_settings_get_bool(settings, "directqp"))
2407                 {
2408                         if (vcodec != HB_VCODEC_X264 || 
2409                                 ghb_settings_get_bool(settings, "linear_vquality"))
2410                         {
2411                                 min = 68;
2412                                 max = 97;
2413                         }
2414                         else if (vcodec == HB_VCODEC_X264)
2415                         {
2416                                 min = 40;
2417                                 max = 70;
2418                         }
2419                 }
2420                 else
2421                 {
2422                         if (vcodec == HB_VCODEC_X264)
2423                         {
2424                                 min = 16;
2425                                 max = 30;
2426                         }
2427                         else if (vcodec == HB_VCODEC_FFMPEG)
2428                         {
2429                                 min = 1;
2430                                 max = 8;
2431                         }
2432                         else
2433                         {
2434                                 min = 68;
2435                                 max = 97;
2436                         }
2437                 }
2438                 gint vquality = ghb_settings_get_dbl(settings, "video_quality");
2439                 if (vquality < min || vquality > max)
2440                 {
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"))
2448                         {
2449                                 g_free(message);
2450                                 return FALSE;
2451                         }
2452                         g_free(message);
2453                 }
2454         }
2455         return TRUE;
2456 }
2457
2458 void
2459 ghb_add_job(job_settings_t *js, gint unique_id)
2460 {
2461         hb_list_t  * list;
2462         hb_title_t * title;
2463         hb_job_t   * job;
2464         GHashTable *settings = js->settings;
2465         static gchar *x264opts;
2466         gint sub_id = 0;
2467         gboolean tweaks = FALSE;
2468
2469         g_debug("ghb_add_job()\n");
2470         if (h == NULL) return;
2471         list = hb_get_titles( h );
2472         if( !hb_list_count( list ) )
2473         {
2474                 /* No valid title, stop right there */
2475                 g_message("No title found.\n");
2476                 return;
2477         }
2478
2479         gint titleindex = ghb_settings_get_index(settings, "title");
2480     title = hb_list_item( list, titleindex );
2481         if (title == NULL) return;
2482
2483         /* Set job settings */
2484         job   = title->job;
2485         if (job == NULL) return;
2486
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)
2490         {
2491                 job->largeFileSize = ghb_settings_get_bool(settings, "large_mp4");
2492                 job->mp4_optimize = ghb_settings_get_bool(settings, "http_optimize_mp4");
2493         }
2494         else
2495         {
2496                 job->largeFileSize = FALSE;
2497                 job->mp4_optimize = FALSE;
2498         }
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 );
2505
2506         job->chapter_markers = ghb_settings_get_bool(settings, "chapter_markers");
2507         if ( job->chapter_markers )
2508         {
2509                 gint chapter;
2510                 
2511                 for(chapter = chapter_start; chapter <= chapter_end; chapter++)
2512                 {
2513                         hb_chapter_t * chapter_s;
2514                         gchar *name;
2515                         
2516                         if (js->chapter_list == NULL || js->chapter_list[chapter-1] == 0)
2517                         {
2518                                 name = g_strdup_printf ("Chapter %2d", chapter);
2519                         }
2520                         else
2521                         {
2522                                 name = g_strdup(js->chapter_list[chapter-1]);
2523                         }
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';
2527                         g_free(name);
2528                 }
2529         }
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");
2534
2535         
2536         gboolean decomb = ghb_settings_get_bool(settings, "decomb");
2537         gint deint = ghb_settings_get_int(settings, "deinterlace");
2538         if (!decomb)
2539                 job->deinterlace = (deint == 0) ? 0 : 1;
2540         else
2541                 job->deinterlace = 0;
2542     job->grayscale   = ghb_settings_get_bool(settings, "grayscale");
2543
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)
2547         {
2548                 job->pixel_ratio = 2;
2549                 job->modulus = 16;
2550         }
2551         else if (anamorphic)
2552         {
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;
2558                 job->modulus = 2;
2559         }
2560         else
2561         {
2562                 job->pixel_ratio = 0;
2563                 job->modulus = 2;
2564         }
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 )
2569         {
2570                 hb_filter_detelecine.settings = NULL;
2571                 if (tweaks)
2572                 {
2573                         const gchar *str;
2574                         str = ghb_settings_get_string(settings, "tweak_detelecine");
2575                         if (str && str[0])
2576                         {
2577                                 hb_filter_detelecine.settings = (gchar*)str;
2578                         }
2579                 }
2580                 hb_list_add( job->filters, &hb_filter_detelecine );
2581         }
2582         if( decomb )
2583         {
2584                 // Use default settings
2585                 hb_filter_decomb.settings = NULL;
2586                 if (tweaks)
2587                 {
2588                         const gchar *str;
2589                         str = ghb_settings_get_string(settings, "tweak_decomb");
2590                         if (str && str[0])
2591                         {
2592                                 hb_filter_decomb.settings = (gchar*)str;
2593                         }
2594                 }
2595                 hb_list_add( job->filters, &hb_filter_decomb );
2596         }
2597         if( job->deinterlace )
2598         {
2599                 hb_filter_deinterlace.settings = (gchar*)ghb_settings_get_string(settings, "deinterlace");
2600                 if (tweaks)
2601                 {
2602                         const gchar *str;
2603                         str = ghb_settings_get_string(settings, "tweak_deinterlace");
2604                         if (str && str[0])
2605                         {
2606                                 hb_filter_deinterlace.settings = (gchar*)str;
2607                         }
2608                 }
2609                 hb_list_add( job->filters, &hb_filter_deinterlace );
2610         }
2611         if( ghb_settings_get_bool(settings, "deblock") )
2612         {
2613                 hb_filter_deblock.settings = NULL;
2614                 if (tweaks)
2615                 {
2616                         const gchar *str;
2617                         str = ghb_settings_get_string(settings, "tweak_deblock");
2618                         if (str && str[0])
2619                         {
2620                                 hb_filter_deblock.settings = (gchar*)str;
2621                         }
2622                 }
2623                 hb_list_add( job->filters, &hb_filter_deblock );
2624         }
2625         gint denoise = ghb_settings_get_int(settings, "denoise");
2626         if( denoise != 0 )
2627         {
2628                 hb_filter_denoise.settings = (gchar*)ghb_settings_get_string(settings, "denoise");
2629                 if (tweaks)
2630                 {
2631                         const gchar *str;
2632                         str = ghb_settings_get_string(settings, "tweak_denoise");
2633                         if (str && str[0])
2634                         {
2635                                 hb_filter_denoise.settings = (gchar*)str;
2636                         }
2637                 }
2638                 hb_list_add( job->filters, &hb_filter_denoise );
2639         }
2640         job->width = ghb_settings_get_int(settings, "scale_width");
2641         job->height = ghb_settings_get_int(settings, "scale_height");
2642
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))
2646         {
2647                 // mp4|avi/theora combination is not supported.
2648                 job->vcodec = HB_VCODEC_XVID;
2649         }
2650         if ((job->vcodec == HB_VCODEC_X264) && (job->mux == HB_MUX_MP4))
2651         {
2652                 job->ipod_atom = ghb_settings_get_bool(settings, "ipod_file");
2653         }
2654         if (ghb_settings_get_bool(settings, "vquality_type_constant"))
2655         {
2656                 gdouble vquality = ghb_settings_get_dbl(settings, "video_quality");
2657                 if (!ghb_settings_get_bool(settings, "directqp"))
2658                 {
2659                         vquality /= 100.0;
2660                         if (ghb_settings_get_bool(settings, "linear_vquality"))
2661                         {
2662                                 if (job->vcodec == HB_VCODEC_X264)
2663                                 {
2664                                         // Adjust to same range as xvid and ffmpeg
2665                                         vquality = 31.0 - vquality * 31.0;
2666                                         if (vquality > 0)
2667                                         {
2668                                                 // Convert linear to log curve
2669                                                 vquality = 12 + 6 * log2(vquality);
2670                                                 if (vquality > 51) vquality = 51;
2671                                                 if (vquality < 1) vquality = 1;
2672                                         }
2673                                         else
2674                                                 vquality = 0;
2675                                 }
2676                         }
2677                         else
2678                         {
2679                                 if (vquality == 0.0) vquality = 0.01;
2680                                 if (vquality == 1.0) vquality = 0.0;
2681                         }
2682                 }
2683                 job->vquality =  vquality;
2684                 job->vbitrate = 0;
2685         }
2686         else if (ghb_settings_get_bool(settings, "vquality_type_bitrate"))
2687         {
2688                 job->vquality = -1.0;
2689                 job->vbitrate = ghb_settings_get_int(settings, "video_bitrate");
2690         }
2691         // AVI container does not support variable frame rate.
2692         if (job->mux == HB_MUX_AVI)
2693         {
2694                 job->vfr = FALSE;
2695         }
2696
2697         gint vrate = ghb_settings_get_int(settings, "framerate");
2698         if( vrate == 0 || job->vfr )
2699         {
2700                 job->vrate = title->rate;
2701                 job->vrate_base = title->rate_base;
2702                 job->cfr = 0;
2703         }
2704         else
2705         {
2706                 job->vrate = 27000000;
2707                 job->vrate_base = vrate;
2708                 job->cfr = 1;
2709         }
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);
2713         gint ii;
2714     for(ii = 0; ii < num_audio_tracks; ii++)
2715     {
2716         hb_audio_t *audio = (hb_audio_t*)hb_list_item(job->list_audio, 0);
2717         hb_list_rem(job->list_audio, audio);
2718     }
2719         GSList *link = js->audio_settings;
2720         gint count = 0;
2721         while (link != NULL)
2722         {
2723                 GHashTable *asettings;
2724             hb_audio_config_t audio;
2725             hb_audio_config_t *taudio;
2726
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))
2734                 {
2735                         // Not supported.  AC3 is passthrough only, so input must be AC3
2736                         if (job->mux == HB_MUX_AVI)
2737                         {
2738                                 audio.out.codec = HB_ACODEC_LAME;
2739                         }
2740                         else
2741                         {
2742                                 audio.out.codec = HB_ACODEC_FAAC;
2743                         }
2744                 }
2745                 if ((job->mux == HB_MUX_MP4) && 
2746                         ((audio.out.codec == HB_ACODEC_LAME) ||
2747                         (audio.out.codec == HB_ACODEC_VORBIS)))
2748                 {
2749                         // mp4/mp3|vorbis combination is not supported.
2750                         audio.out.codec = HB_ACODEC_FAAC;
2751                 }
2752                 if ((job->mux == HB_MUX_AVI) && 
2753                         ((audio.out.codec == HB_ACODEC_FAAC) ||
2754                         (audio.out.codec == HB_ACODEC_VORBIS)))
2755                 {
2756                         // avi/faac|vorbis combination is not supported.
2757                         audio.out.codec = HB_ACODEC_LAME;
2758                 }
2759                 if ((job->mux == HB_MUX_OGM) && 
2760                         ((audio.out.codec == HB_ACODEC_FAAC) ||
2761                         (audio.out.codec == HB_ACODEC_AC3)))
2762                 {
2763                         // ogm/faac|ac3 combination is not supported.
2764                         audio.out.codec = HB_ACODEC_VORBIS;
2765                 }
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)
2769                 {
2770                         audio.out.mixdown = 0;
2771                 }
2772                 else
2773                 {
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, 
2777                                                                                                 audio.out.mixdown);
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;
2782                         else
2783                                 audio.out.samplerate = srate;
2784                 }
2785
2786                 // Add it to the jobs audio list
2787         hb_audio_add( job, &audio );
2788                 count++;
2789                 link = link->next;
2790         }
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"))
2795         {
2796                 gint size;
2797                 
2798                 size = ghb_settings_get_int(settings, "video_target_size");
2799         job->vbitrate = hb_calc_bitrate( job, size );
2800                 job->vquality = -1.0;
2801         }
2802
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);
2811         
2812         if( x264opts != NULL && *x264opts != '\0' )
2813         {
2814                 job->x264opts = x264opts;
2815         }
2816         else /*avoids a bus error crash when options aren't specified*/
2817         {
2818                 job->x264opts =  NULL;
2819         }
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;
2823         if (subtitle >= 0)
2824                 job->subtitle = subtitle;
2825         else
2826                 job->subtitle = -1;
2827         if (subtitle == -1)
2828         {
2829                 // Subtitle scan. Look for subtitle matching audio language
2830                 char *x264opts_tmp;
2831
2832                 /*
2833                  * When subtitle scan is enabled do a fast pre-scan job
2834                  * which will determine which subtitles to enable, if any.
2835                  */
2836                 job->pass = -1;
2837                 job->indepth_scan = 1;
2838
2839                 x264opts_tmp = job->x264opts;
2840                 job->x264opts = NULL;
2841
2842                 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
2843                 *(job->select_subtitle) = NULL;
2844
2845                 /*
2846                  * Add the pre-scan job
2847                  */
2848                 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2849                 hb_add( h, job );
2850                 //if (job->x264opts != NULL)
2851                 //      g_free(job->x264opts);
2852
2853                 job->x264opts = x264opts_tmp;
2854         }
2855         else
2856         {
2857                 job->select_subtitle = NULL;
2858         }
2859         if( ghb_settings_get_bool(settings, "two_pass") &&
2860                 !ghb_settings_get_bool(settings, "vquality_type_constant"))
2861         {
2862                 /*
2863                  * If subtitle_scan is enabled then only turn it on
2864                  * for the second pass and then off again for the
2865                  * second.
2866                  */
2867                 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
2868                 job->select_subtitle = NULL;
2869                 job->pass = 1;
2870                 job->indepth_scan = 0;
2871                 gchar *x264opts2 = NULL;
2872                 if (x264opts)
2873                 {
2874                         x264opts2 = g_strdup(x264opts);
2875                 }
2876                 /*
2877                  * If turbo options have been selected then append them
2878                  * to the x264opts now (size includes one ':' and the '\0')
2879                  */
2880                 if( ghb_settings_get_bool(settings, "turbo") )
2881                 {
2882                         char *tmp_x264opts;
2883
2884                         if ( x264opts )
2885                         {
2886                                 tmp_x264opts = g_strdup_printf("%s:%s", x264opts, turbo_opts);
2887                                 g_free(x264opts);
2888                         } 
2889                         else 
2890                         {
2891                                 /*
2892                                  * No x264opts to modify, but apply the turbo options
2893                                  * anyway as they may be modifying defaults
2894                                  */
2895                                 tmp_x264opts = g_strdup_printf("%s", turbo_opts);
2896                         }
2897                         x264opts = tmp_x264opts;
2898
2899                         job->x264opts = x264opts;
2900                 }
2901                 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2902                 hb_add( h, job );
2903                 //if (job->x264opts != NULL)
2904                 //      g_free(job->x264opts);
2905
2906                 job->select_subtitle = subtitle_tmp;
2907                 job->pass = 2;
2908                 /*
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).
2913                  */
2914                 job->indepth_scan = 0;
2915                 job->x264opts = x264opts2;
2916                 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2917                 hb_add( h, job );
2918                 //if (job->x264opts != NULL)
2919                 //      g_free(job->x264opts);
2920         }
2921         else
2922         {
2923                 job->indepth_scan = 0;
2924                 job->pass = 0;
2925                 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2926                 hb_add( h, job );
2927                 //if (job->x264opts != NULL)
2928                 //      g_free(job->x264opts);
2929         }
2930 }
2931
2932 void
2933 ghb_remove_job(gint unique_id)
2934 {
2935     hb_job_t * job;
2936     gint ii;
2937         
2938         // Multiples passes all get the same id
2939         // remove them all.
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)
2943     {
2944         if ((job->sequence_id & 0xFFFFFF) == unique_id)
2945                         hb_rem(h, job);
2946     }
2947 }
2948
2949 void
2950 ghb_start_queue()
2951 {
2952         hb_start( h );
2953 }
2954
2955 void
2956 ghb_stop_queue()
2957 {
2958         hb_stop( h );
2959 }
2960
2961 void
2962 ghb_pause_queue()
2963 {
2964     hb_state_t s;
2965     hb_get_state2( h, &s );
2966
2967     if( s.state == HB_STATE_PAUSED )
2968     {
2969         hb_resume( h );
2970     }
2971     else
2972     {
2973         hb_pause( h );
2974     }
2975 }
2976
2977 GdkPixbuf*
2978 ghb_get_preview_image(gint titleindex, gint index, GHashTable *settings, gboolean borders)
2979 {
2980         hb_title_t *title;
2981         hb_list_t  *list;
2982         
2983         list = hb_get_titles( h );
2984         if( !hb_list_count( list ) )
2985         {
2986                 /* No valid title, stop right there */
2987                 return NULL;
2988         }
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);
2993
2994         // hb_get_preview can't handle sizes that are larger than the original title
2995         // dimensions
2996         if (title->job->width > title->width)
2997                 title->job->width = title->width;
2998         
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;
3005         
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;
3013         gint borderTop = 1;
3014         gint borderLeft = 1;
3015     if (borders)
3016     {
3017         //     |<---------- title->width ----------->|
3018         //     |   |<---- title->job->width ---->|   |
3019         //     |   |                             |   |
3020         //     .......................................
3021         //     ....+-----------------------------+....
3022         //     ....|                             |....<-- gray border
3023         //     ....|                             |....
3024         //     ....|                             |....
3025         //     ....|                             |<------- image
3026         //     ....|                             |....
3027         //     ....|                             |....
3028         //     ....|                             |....
3029         //     ....|                             |....
3030         //     ....|                             |....
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");
3038         }
3039
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", 
3044                         title->crop[0],
3045                         title->crop[1],
3046                         title->crop[2],
3047                         title->crop[3]);
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;
3055
3056         gint newSize;
3057         newSize = srcWidth * srcHeight * 4;
3058         if( bufferSize < newSize )
3059         {
3060                 bufferSize = newSize;
3061                 buffer     = (guint8*) g_realloc( buffer, bufferSize );
3062         }
3063         hb_get_preview( h, title, index, buffer );
3064
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.
3068         
3069         // The image data returned by hb_get_preview is 4 bytes per pixel, BGRA format.
3070         // Alpha is ignored.
3071
3072         GdkPixbuf *preview = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, dstWidth, dstHeight);
3073         guint8 *pixels = gdk_pixbuf_get_pixels (preview);
3074         
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
3079         gint ii, jj;
3080         gint channels = gdk_pixbuf_get_n_channels (preview);
3081         gint stride = gdk_pixbuf_get_rowstride (preview);
3082         guint8 *tmp;
3083         for (ii = 0; ii < dstHeight; ii++)
3084         {
3085                 tmp = dst;
3086                 for (jj = 0; jj < dstWidth; jj++)
3087                 {
3088                         tmp[0] = src[0] >> 16;
3089                         tmp[1] = src[0] >> 8;
3090                         tmp[2] = src[0] >> 0;
3091                         tmp += channels;
3092                         src++;
3093                 }
3094                 dst += stride;
3095                 src += (srcWidth - dstWidth);   // skip to next row in src
3096         }
3097         // Got it, but hb_get_preview doesn't compensate for anamorphic, so lets
3098         // scale
3099         gint width, height, par_width, par_height;
3100         gboolean anamorphic = ghb_settings_get_bool (settings, "anamorphic");
3101         if (anamorphic)
3102         {
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;
3106                 else
3107                         dstHeight = dstHeight * par_height / par_width;
3108         }
3109         
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;
3115 }
3116
3117 static void
3118 sanitize_volname(gchar *name)
3119 {
3120         gchar *a, *b;
3121
3122         a = b = name;
3123         while (*b)
3124         {
3125                 switch(*b)
3126                 {
3127                 case '<':
3128                         b++;
3129                         break;
3130                 case '>':
3131                         b++;
3132                         break;
3133                 default:
3134                         *a = *b;
3135                         a++; b++;
3136                         break;
3137                 }
3138         }
3139         *a = 0;
3140 }
3141
3142 gchar*
3143 ghb_dvd_volname(const gchar *device)
3144 {
3145         gchar *name;
3146         name = hb_dvd_name((gchar*)device);
3147         if (name != NULL)
3148         {
3149                 sanitize_volname(name);
3150                 return g_strdup(name);
3151         }
3152         return name;
3153 }