OSDN Git Service

prevent duplicate audio list entries
[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 <gtk/gtk.h>
29 #include <glib/gstdio.h>
30 #include "hb-backend.h"
31 #include "settings.h"
32 #include "callbacks.h"
33 #include "subtitlehandler.h"
34 #include "audiohandler.h"
35 #include "x264handler.h"
36 #include "preview.h"
37 #include "values.h"
38 #include "lang.h"
39
40 typedef struct
41 {
42         const gchar *option;
43         const gchar *shortOpt;
44         gdouble ivalue;
45         const gchar *svalue;
46 } options_map_t;
47
48 typedef struct
49 {
50         gint count;
51         options_map_t *map;
52 } combo_opts_t;
53
54 static gchar **index_str = NULL;
55 static gint index_str_size = 0;
56
57 static void 
58 index_str_init(gint max_index)
59 {
60         int ii;
61
62         if (max_index+1 > index_str_size)
63         {
64                 index_str = realloc(index_str, (max_index+1) * sizeof(char*));
65                 for (ii = index_str_size; ii <= max_index; ii++)
66                 {
67                         index_str[ii] = g_strdup_printf("%d", ii);
68                 }
69                 index_str_size = max_index + 1;
70         }
71 }
72
73 static options_map_t d_point_to_point_opts[] =
74 {
75         {"Chapters:",       "chapter", 0, "0"},
76         {"Seconds:", "time",    1, "1"},
77         {"Frames:",   "frame",   2, "2"},
78 };
79 combo_opts_t point_to_point_opts =
80 {
81         sizeof(d_point_to_point_opts)/sizeof(options_map_t),
82         d_point_to_point_opts
83 };
84
85 static options_map_t d_when_complete_opts[] =
86 {
87         {"Do Nothing",            "nothing",  0, "0"},
88         {"Show Notification",     "notify",   1, "1"},
89         {"Put Computer To Sleep", "sleep",    2, "2"},
90         {"Shutdown Computer",     "shutdown", 3, "3"},
91 };
92 combo_opts_t when_complete_opts =
93 {
94         sizeof(d_when_complete_opts)/sizeof(options_map_t),
95         d_when_complete_opts
96 };
97
98 static options_map_t d_par_opts[] =
99 {
100         {"Off", "0", 0, "0"},
101         {"Strict", "1", 1, "1"},
102         {"Loose", "2", 2, "2"},
103         {"Custom", "3", 3, "3"},
104 };
105 combo_opts_t par_opts =
106 {
107         sizeof(d_par_opts)/sizeof(options_map_t),
108         d_par_opts
109 };
110
111 static options_map_t d_alignment_opts[] =
112 {
113         {"2", "2", 2, "2"},
114         {"4", "4", 4, "4"},
115         {"8", "8", 8, "8"},
116         {"16", "16", 16, "16"},
117 };
118 combo_opts_t alignment_opts =
119 {
120         sizeof(d_alignment_opts)/sizeof(options_map_t),
121         d_alignment_opts
122 };
123
124 static options_map_t d_logging_opts[] =
125 {
126         {"0", "0", 0, "0"},
127         {"1", "1", 1, "1"},
128         {"2", "2", 2, "2"},
129 };
130 combo_opts_t logging_opts =
131 {
132         sizeof(d_logging_opts)/sizeof(options_map_t),
133         d_logging_opts
134 };
135
136 static options_map_t d_log_longevity_opts[] =
137 {
138         {"Week",     "week",     7, "7"},
139         {"Month",    "month",    30, "30"},
140         {"Year",     "year",     365, "365"},
141         {"Immortal", "immortal", 366, "366"},
142 };
143 combo_opts_t log_longevity_opts =
144 {
145         sizeof(d_log_longevity_opts)/sizeof(options_map_t),
146         d_log_longevity_opts
147 };
148
149 static options_map_t d_appcast_update_opts[] =
150 {
151         {"Never", "never", 0, "never"},
152         {"Daily", "daily", 1, "daily"},
153         {"Weekly", "weekly", 2, "weekly"},
154         {"Monthly", "monthly", 3, "monthly"},
155 };
156 combo_opts_t appcast_update_opts =
157 {
158         sizeof(d_appcast_update_opts)/sizeof(options_map_t),
159         d_appcast_update_opts
160 };
161
162 static options_map_t d_vqual_granularity_opts[] =
163 {
164         {"0.2",  "0.2",  0.2,  "0.2"},
165         {"0.25", "0.25", 0.25, "0.25"},
166         {"0.5",  "0.5",  0.5,  "0.5"},
167         {"1",    "1",    1,    "1"},
168 };
169 combo_opts_t vqual_granularity_opts =
170 {
171         sizeof(d_vqual_granularity_opts)/sizeof(options_map_t),
172         d_vqual_granularity_opts
173 };
174
175 static options_map_t d_container_opts[] =
176 {
177         {"MKV", "mkv", HB_MUX_MKV, "mkv"},
178         {"MP4", "mp4", HB_MUX_MP4, "mp4"},
179 };
180 combo_opts_t container_opts =
181 {
182         sizeof(d_container_opts)/sizeof(options_map_t),
183         d_container_opts
184 };
185
186 static options_map_t d_detel_opts[] =
187 {
188         {"Off",    "off",   0, ""},
189         {"Custom", "custom", 1, ""},
190         {"Default","default",2, NULL},
191 };
192 combo_opts_t detel_opts =
193 {
194         sizeof(d_detel_opts)/sizeof(options_map_t),
195         d_detel_opts
196 };
197
198 static options_map_t d_decomb_opts[] =
199 {
200         {"Off",    "off",   0, ""},
201         {"Custom", "custom", 1, ""},
202         {"Default","default",2, NULL},
203 };
204 combo_opts_t decomb_opts =
205 {
206         sizeof(d_decomb_opts)/sizeof(options_map_t),
207         d_decomb_opts
208 };
209
210 static options_map_t d_deint_opts[] =
211 {
212         {"Off",    "off",   0, ""},
213         {"Custom", "custom", 1, ""},
214         {"Fast",   "fast",   2, "-1:-1:-1:0:1"},
215         {"Slow",   "slow",   3, "2:-1:-1:0:1"},
216         {"Slower", "slower", 4, "0:-1:-1:0:1"},
217 };
218 combo_opts_t deint_opts =
219 {
220         sizeof(d_deint_opts)/sizeof(options_map_t),
221         d_deint_opts
222 };
223
224 static options_map_t d_denoise_opts[] =
225 {
226         {"Off",    "off",   0, ""},
227         {"Custom", "custom", 1, ""},
228         {"Weak",   "weak",   2, "2:1:2:3"},
229         {"Medium", "medium", 3, "3:2:2:3"},
230         {"Strong", "strong", 4, "7:7:5:5"},
231 };
232 combo_opts_t denoise_opts =
233 {
234         sizeof(d_denoise_opts)/sizeof(options_map_t),
235         d_denoise_opts
236 };
237
238 static options_map_t d_vcodec_opts[] =
239 {
240         {"H.264 (x264)",    "x264",   HB_VCODEC_X264, ""},
241         {"MPEG-4 (FFmpeg)", "ffmpeg", HB_VCODEC_FFMPEG, ""},
242         {"VP3 (Theora)",    "theora", HB_VCODEC_THEORA, ""},
243 };
244 combo_opts_t vcodec_opts =
245 {
246         sizeof(d_vcodec_opts)/sizeof(options_map_t),
247         d_vcodec_opts
248 };
249
250 static options_map_t d_acodec_opts[] =
251 {
252         {"AAC (faac)",      "faac",   HB_ACODEC_FAAC,   "faac"},
253         {"MP3 (lame)",      "lame",   HB_ACODEC_LAME,   "lame"},
254         {"Vorbis",          "vorbis", HB_ACODEC_VORBIS, "vorbis"},
255         {"AC3 (pass-thru)", "ac3",    HB_ACODEC_AC3,    "ac3"},
256         {"DTS (pass-thru)", "dts",    HB_ACODEC_DCA,    "dts"},
257         {"Choose For Me",   "auto",   HB_ACODEC_MASK,   "auto"},
258 };
259 combo_opts_t acodec_opts =
260 {
261         sizeof(d_acodec_opts)/sizeof(options_map_t),
262         d_acodec_opts
263 };
264
265 static options_map_t d_direct_opts[] =
266 {
267         {"None",      "none",     0, "none"},
268         {"Spatial",   "spatial",  1, "spatial"},
269         {"Temporal",  "temporal", 2, "temporal"},
270         {"Automatic", "auto",     3, "auto"},
271 };
272 combo_opts_t direct_opts =
273 {
274         sizeof(d_direct_opts)/sizeof(options_map_t),
275         d_direct_opts
276 };
277
278 static options_map_t d_badapt_opts[] =
279 {
280         {"Off",             "0", 0, "0"},
281         {"Fast",            "1", 1, "1"},
282         {"Optimal",         "2", 2, "2"},
283 };
284 combo_opts_t badapt_opts =
285 {
286         sizeof(d_badapt_opts)/sizeof(options_map_t),
287         d_badapt_opts
288 };
289
290 static options_map_t d_me_opts[] =
291 {
292         {"Diamond",              "dia",  0, "dia"},
293         {"Hexagon",              "hex",  1, "hex"},
294         {"Uneven Multi-Hexagon", "umh",  2, "umh"},
295         {"Exhaustive",           "esa",  3, "esa"},
296         {"Hadamard Exhaustive",  "tesa", 4, "tesa"},
297 };
298 combo_opts_t me_opts =
299 {
300         sizeof(d_me_opts)/sizeof(options_map_t),
301         d_me_opts
302 };
303
304 static options_map_t d_subme_opts[] =
305 {
306         {"1", "1", 1, "1"},
307         {"2", "2", 2, "2"},
308         {"3", "3", 3, "3"},
309         {"4", "4", 4, "4"},
310         {"5", "5", 5, "5"},
311         {"6", "6", 6, "6"},
312         {"7", "7", 7, "7"},
313         {"8", "8", 8, "8"},
314         {"9", "9", 9, "9"},
315         {"10", "10", 10, "10"},
316 };
317 combo_opts_t subme_opts =
318 {
319         sizeof(d_subme_opts)/sizeof(options_map_t),
320         d_subme_opts
321 };
322
323 static options_map_t d_analyse_opts[] =
324 {
325         {"Some", "some", 0, "some"},
326         {"None", "none", 1, "none"},
327         {"All",  "all",  2, "all"},
328         {"Custom",  "custom",  3, "all"},
329 };
330 combo_opts_t analyse_opts =
331 {
332         sizeof(d_analyse_opts)/sizeof(options_map_t),
333         d_analyse_opts
334 };
335
336 static options_map_t d_trellis_opts[] =
337 {
338         {"Disabled",          "0", 0, "0"},
339         {"Final Macro Block", "1", 1, "1"},
340         {"Always",            "2", 2, "2"},
341 };
342 combo_opts_t trellis_opts =
343 {
344         sizeof(d_trellis_opts)/sizeof(options_map_t),
345         d_trellis_opts
346 };
347
348 combo_opts_t subtitle_opts =
349 {
350         0,
351         NULL
352 };
353
354 combo_opts_t title_opts =
355 {
356         0,
357         NULL
358 };
359
360 combo_opts_t audio_track_opts =
361 {
362         0,
363         NULL
364 };
365
366 typedef struct
367 {
368         const gchar *name;
369         combo_opts_t *opts;
370 } combo_name_map_t;
371
372 combo_name_map_t combo_name_map[] =
373 {
374         {"PtoPType", &point_to_point_opts},
375         {"WhenComplete", &when_complete_opts},
376         {"PicturePAR", &par_opts},
377         {"PictureModulus", &alignment_opts},
378         {"LoggingLevel", &logging_opts},
379         {"LogLongevity", &log_longevity_opts},
380         {"check_updates", &appcast_update_opts},
381         {"VideoQualityGranularity", &vqual_granularity_opts},
382         {"FileFormat", &container_opts},
383         {"PictureDeinterlace", &deint_opts},
384         {"PictureDecomb", &decomb_opts},
385         {"PictureDetelecine", &detel_opts},
386         {"PictureDenoise", &denoise_opts},
387         {"VideoEncoder", &vcodec_opts},
388         {"AudioEncoder", &acodec_opts},
389         {"x264_direct", &direct_opts},
390         {"x264_b_adapt", &badapt_opts},
391         {"x264_me", &me_opts},
392         {"x264_subme", &subme_opts},
393         {"x264_analyse", &analyse_opts},
394         {"x264_trellis", &trellis_opts},
395         {"SubtitleTrack", &subtitle_opts},
396         {"title", &title_opts},
397         {"AudioTrack", &audio_track_opts},
398         {NULL, NULL}
399 };
400
401 const gchar *srt_codeset_table[] =
402 {
403         "ANSI_X3.4-1968",
404         "ANSI_X3.4-1986",
405         "ANSI_X3.4",
406         "ANSI_X3.110-1983",
407         "ANSI_X3.110",
408         "ASCII",
409         "ECMA-114",
410         "ECMA-118",
411         "ECMA-128",
412         "ECMA-CYRILLIC",
413         "IEC_P27-1",
414         "ISO-8859-1",
415         "ISO-8859-2",
416         "ISO-8859-3",
417         "ISO-8859-4",
418         "ISO-8859-5",
419         "ISO-8859-6",
420         "ISO-8859-7",
421         "ISO-8859-8",
422         "ISO-8859-9",
423         "ISO-8859-9E",
424         "ISO-8859-10",
425         "ISO-8859-11",
426         "ISO-8859-13",
427         "ISO-8859-14",
428         "ISO-8859-15",
429         "ISO-8859-16",
430         "UTF-7",
431         "UTF-8",
432         "UTF-16",
433         "UTF-16LE",
434         "UTF-16BE",
435         "UTF-32",
436         "UTF-32LE",
437         "UTF-32BE",
438         NULL
439 };
440 #define SRT_TABLE_SIZE (sizeof(srt_codeset_table)/ sizeof(char*)-1)
441
442 #if 0
443 typedef struct iso639_lang_t
444 {
445     char * eng_name;        /* Description in English */
446     char * native_name;     /* Description in native language */
447     char * iso639_1;       /* ISO-639-1 (2 characters) code */
448     char * iso639_2;        /* ISO-639-2/t (3 character) code */
449     char * iso639_2b;       /* ISO-639-2/b code (if different from above) */
450 } iso639_lang_t;
451 #endif
452
453 const iso639_lang_t ghb_language_table[] =
454
455         { "Any", "", "zz", "und" },
456         { "Afar", "", "aa", "aar" },
457         { "Abkhazian", "", "ab", "abk" },
458         { "Afrikaans", "", "af", "afr" },
459         { "Akan", "", "ak", "aka" },
460         { "Albanian", "", "sq", "sqi", "alb" },
461         { "Amharic", "", "am", "amh" },
462         { "Arabic", "", "ar", "ara" },
463         { "Aragonese", "", "an", "arg" },
464         { "Armenian", "", "hy", "hye", "arm" },
465         { "Assamese", "", "as", "asm" },
466         { "Avaric", "", "av", "ava" },
467         { "Avestan", "", "ae", "ave" },
468         { "Aymara", "", "ay", "aym" },
469         { "Azerbaijani", "", "az", "aze" },
470         { "Bashkir", "", "ba", "bak" },
471         { "Bambara", "", "bm", "bam" },
472         { "Basque", "", "eu", "eus", "baq" },
473         { "Belarusian", "", "be", "bel" },
474         { "Bengali", "", "bn", "ben" },
475         { "Bihari", "", "bh", "bih" },
476         { "Bislama", "", "bi", "bis" },
477         { "Bosnian", "", "bs", "bos" },
478         { "Breton", "", "br", "bre" },
479         { "Bulgarian", "", "bg", "bul" },
480         { "Burmese", "", "my", "mya", "bur" },
481         { "Catalan", "", "ca", "cat" },
482         { "Chamorro", "", "ch", "cha" },
483         { "Chechen", "", "ce", "che" },
484         { "Chinese", "", "zh", "zho", "chi" },
485         { "Church Slavic", "", "cu", "chu" },
486         { "Chuvash", "", "cv", "chv" },
487         { "Cornish", "", "kw", "cor" },
488         { "Corsican", "", "co", "cos" },
489         { "Cree", "", "cr", "cre" },
490         { "Czech", "", "cs", "ces", "cze" },
491         { "Danish", "Dansk", "da", "dan" },
492         { "German", "Deutsch", "de", "deu", "ger" },
493         { "Divehi", "", "dv", "div" },
494         { "Dzongkha", "", "dz", "dzo" },
495         { "English", "English", "en", "eng" },
496         { "Spanish", "Espanol", "es", "spa" },
497         { "Esperanto", "", "eo", "epo" },
498         { "Estonian", "", "et", "est" },
499         { "Ewe", "", "ee", "ewe" },
500         { "Faroese", "", "fo", "fao" },
501         { "Fijian", "", "fj", "fij" },
502         { "French", "Francais", "fr", "fra", "fre" },
503         { "Western Frisian", "", "fy", "fry" },
504         { "Fulah", "", "ff", "ful" },
505         { "Georgian", "", "ka", "kat", "geo" },
506         { "Gaelic (Scots)", "", "gd", "gla" },
507         { "Irish", "", "ga", "gle" },
508         { "Galician", "", "gl", "glg" },
509         { "Manx", "", "gv", "glv" },
510         { "Greek, Modern", "", "el", "ell", "gre" },
511         { "Guarani", "", "gn", "grn" },
512         { "Gujarati", "", "gu", "guj" },
513         { "Haitian", "", "ht", "hat" },
514         { "Hausa", "", "ha", "hau" },
515         { "Hebrew", "", "he", "heb" },
516         { "Herero", "", "hz", "her" },
517         { "Hindi", "", "hi", "hin" },
518         { "Hiri Motu", "", "ho", "hmo" },
519         { "Croatian", "Hrvatski", "hr", "hrv", "scr" },
520         { "Igbo", "", "ig", "ibo" },
521         { "Ido", "", "io", "ido" },
522         { "Icelandic", "Islenska", "is", "isl", "ice" },
523         { "Sichuan Yi", "", "ii", "iii" },
524         { "Inuktitut", "", "iu", "iku" },
525         { "Interlingue", "", "ie", "ile" },
526         { "Interlingua", "", "ia", "ina" },
527         { "Indonesian", "", "id", "ind" },
528         { "Inupiaq", "", "ik", "ipk" },
529         { "Italian", "Italiano", "it", "ita" },
530         { "Javanese", "", "jv", "jav" },
531         { "Japanese", "", "ja", "jpn" },
532         { "Kalaallisut", "", "kl", "kal" },
533         { "Kannada", "", "kn", "kan" },
534         { "Kashmiri", "", "ks", "kas" },
535         { "Kanuri", "", "kr", "kau" },
536         { "Kazakh", "", "kk", "kaz" },
537         { "Central Khmer", "", "km", "khm" },
538         { "Kikuyu", "", "ki", "kik" },
539         { "Kinyarwanda", "", "rw", "kin" },
540         { "Kirghiz", "", "ky", "kir" },
541         { "Komi", "", "kv", "kom" },
542         { "Kongo", "", "kg", "kon" },
543         { "Korean", "", "ko", "kor" },
544         { "Kuanyama", "", "kj", "kua" },
545         { "Kurdish", "", "ku", "kur" },
546         { "Lao", "", "lo", "lao" },
547         { "Latin", "", "la", "lat" },
548         { "Latvian", "", "lv", "lav" },
549         { "Limburgan", "", "li", "lim" },
550         { "Lingala", "", "ln", "lin" },
551         { "Lithuanian", "", "lt", "lit" },
552         { "Luxembourgish", "", "lb", "ltz" },
553         { "Luba-Katanga", "", "lu", "lub" },
554         { "Ganda", "", "lg", "lug" },
555         { "Macedonian", "", "mk", "mkd", "mac" },
556         { "Hungarian", "Magyar", "hu", "hun" },
557         { "Marshallese", "", "mh", "mah" },
558         { "Malayalam", "", "ml", "mal" },
559         { "Maori", "", "mi", "mri", "mao" },
560         { "Marathi", "", "mr", "mar" },
561         { "Malay", "", "ms", "msa", "msa" },
562         { "Malagasy", "", "mg", "mlg" },
563         { "Maltese", "", "mt", "mlt" },
564         { "Moldavian", "", "mo", "mol" },
565         { "Mongolian", "", "mn", "mon" },
566         { "Nauru", "", "na", "nau" },
567         { "Navajo", "", "nv", "nav" },
568         { "Dutch", "Nederlands", "nl", "nld", "dut" },
569         { "Ndebele, South", "", "nr", "nbl" },
570         { "Ndebele, North", "", "nd", "nde" },
571         { "Ndonga", "", "ng", "ndo" },
572         { "Nepali", "", "ne", "nep" },
573         { "Norwegian", "Norsk", "no", "nor" },
574         { "Norwegian Nynorsk", "", "nn", "nno" },
575         { "Norwegian Bokmål", "", "nb", "nob" },
576         { "Chichewa; Nyanja", "", "ny", "nya" },
577         { "Occitan", "", "oc", "oci" },
578         { "Ojibwa", "", "oj", "oji" },
579         { "Oriya", "", "or", "ori" },
580         { "Oromo", "", "om", "orm" },
581         { "Ossetian", "", "os", "oss" },
582         { "Panjabi", "", "pa", "pan" },
583         { "Persian", "", "fa", "fas", "per" },
584         { "Pali", "", "pi", "pli" },
585         { "Polish", "", "pl", "pol" },
586         { "Portuguese", "Portugues", "pt", "por" },
587         { "Pushto", "", "ps", "pus" },
588         { "Quechua", "", "qu", "que" },
589         { "Romansh", "", "rm", "roh" },
590         { "Romanian", "", "ro", "ron", "rum" },
591         { "Rundi", "", "rn", "run" },
592         { "Russian", "", "ru", "rus" },
593         { "Sango", "", "sg", "sag" },
594         { "Sanskrit", "", "sa", "san" },
595         { "Serbian", "", "sr", "srp", "scc" },
596         { "Sinhala", "", "si", "sin" },
597         { "Slovak", "", "sk", "slk", "slo" },
598         { "Slovenian", "", "sl", "slv" },
599         { "Northern Sami", "", "se", "sme" },
600         { "Samoan", "", "sm", "smo" },
601         { "Shona", "", "sn", "sna" },
602         { "Sindhi", "", "sd", "snd" },
603         { "Somali", "", "so", "som" },
604         { "Sotho, Southern", "", "st", "sot" },
605         { "Sardinian", "", "sc", "srd" },
606         { "Swati", "", "ss", "ssw" },
607         { "Sundanese", "", "su", "sun" },
608         { "Finnish", "Suomi", "fi", "fin" },
609         { "Swahili", "", "sw", "swa" },
610         { "Swedish", "Svenska", "sv", "swe" },
611         { "Tahitian", "", "ty", "tah" },
612         { "Tamil", "", "ta", "tam" },
613         { "Tatar", "", "tt", "tat" },
614         { "Telugu", "", "te", "tel" },
615         { "Tajik", "", "tg", "tgk" },
616         { "Tagalog", "", "tl", "tgl" },
617         { "Thai", "", "th", "tha" },
618         { "Tibetan", "", "bo", "bod", "tib" },
619         { "Tigrinya", "", "ti", "tir" },
620         { "Tonga", "", "to", "ton" },
621         { "Tswana", "", "tn", "tsn" },
622         { "Tsonga", "", "ts", "tso" },
623         { "Turkmen", "", "tk", "tuk" },
624         { "Turkish", "", "tr", "tur" },
625         { "Twi", "", "tw", "twi" },
626         { "Uighur", "", "ug", "uig" },
627         { "Ukrainian", "", "uk", "ukr" },
628         { "Urdu", "", "ur", "urd" },
629         { "Uzbek", "", "uz", "uzb" },
630         { "Venda", "", "ve", "ven" },
631         { "Vietnamese", "", "vi", "vie" },
632         { "Volapük", "", "vo", "vol" },
633         { "Welsh", "", "cy", "cym", "wel" },
634         { "Walloon", "", "wa", "wln" },
635         { "Wolof", "", "wo", "wol" },
636         { "Xhosa", "", "xh", "xho" },
637         { "Yiddish", "", "yi", "yid" },
638         { "Yoruba", "", "yo", "yor" },
639         { "Zhuang", "", "za", "zha" },
640         { "Zulu", "", "zu", "zul" },
641         {NULL, NULL, NULL, NULL}
642 };
643 #define LANG_TABLE_SIZE (sizeof(ghb_language_table)/ sizeof(iso639_lang_t)-1)
644
645 static void audio_bitrate_opts_set(GtkBuilder *builder, const gchar *name);
646
647 static void
648 del_tree(const gchar *name, gboolean del_top)
649 {
650         const gchar *file;
651
652         if (g_file_test(name, G_FILE_TEST_IS_DIR))
653         {
654                 GDir *gdir = g_dir_open(name, 0, NULL);
655                 file = g_dir_read_name(gdir);
656                 while (file)
657                 {
658                         gchar *path;
659                         path = g_strdup_printf("%s/%s", name, file);
660                         del_tree(path, TRUE);
661                         g_free(path);
662                         file = g_dir_read_name(gdir);
663                 }
664                 if (del_top)
665                         g_rmdir(name);
666                 g_dir_close(gdir);
667         }
668         else
669         {
670                 g_unlink(name);
671         }
672 }
673
674 const gchar*
675 ghb_version()
676 {
677         return hb_get_version(NULL);
678 }
679
680 void
681 ghb_vquality_range(
682         signal_user_data_t *ud, 
683         gdouble *min, 
684         gdouble *max,
685         gdouble *step,
686         gdouble *page,
687         gint *digits,
688         gboolean *inverted)
689 {
690         gint vcodec = ghb_settings_combo_int(ud->settings, "VideoEncoder");
691         *page = 10;
692         *digits = 0;
693         switch (vcodec)
694         {
695                 case HB_VCODEC_X264:
696                 {
697                         *min = 0;
698                         *max = 51;
699                         *step = ghb_settings_combo_double(ud->settings, 
700                                                                                         "VideoQualityGranularity");
701                         if (*step == 0.2 || *step == 0.5)
702                                 *digits = 1;
703                         else if (*step == 0.25)
704                                 *digits = 2;
705                         *inverted = TRUE;
706                 } break;
707
708                 case HB_VCODEC_FFMPEG:
709                 {
710                         *min = 1;
711                         *max = 31;
712                         *step = 1;
713                         *inverted = TRUE;
714                 } break;
715
716                 case HB_VCODEC_THEORA:
717                 {
718                         *min = 0;
719                         *max = 63;
720                         *step = 1;
721                         *inverted = FALSE;
722                 } break;
723
724                 default:
725                 {
726                         *min = 0;
727                         *max = 100;
728                         *step = 1;
729                         *inverted = FALSE;
730                 } break;
731         }
732 }
733
734 static const gchar*
735 lookup_generic_string(combo_opts_t *opts, const GValue *gval)
736 {
737         gint ii;
738         gchar *str;
739         const gchar *result = "";
740
741         str = ghb_value_string(gval);
742         for (ii = 0; ii < opts->count; ii++)
743         {
744                 if (strcmp(opts->map[ii].shortOpt, str) == 0)
745                 {
746                         result = opts->map[ii].svalue;
747                         break;
748                 }
749         }
750         g_free(str);
751         return result;
752 }
753
754 static gint
755 lookup_generic_int(combo_opts_t *opts, const GValue *gval)
756 {
757         gint ii;
758         gchar *str;
759         gint result = -1;
760
761         str = ghb_value_string(gval);
762         for (ii = 0; ii < opts->count; ii++)
763         {
764                 if (strcmp(opts->map[ii].shortOpt, str) == 0)
765                 {
766                         result = opts->map[ii].ivalue;
767                         break;
768                 }
769         }
770         g_free(str);
771         return result;
772 }
773
774 static gdouble
775 lookup_generic_double(combo_opts_t *opts, const GValue *gval)
776 {
777         gint ii;
778         gchar *str;
779         gdouble result = -1;
780
781         str = ghb_value_string(gval);
782         for (ii = 0; ii < opts->count; ii++)
783         {
784                 if (strcmp(opts->map[ii].shortOpt, str) == 0)
785                 {
786                         result = opts->map[ii].ivalue;
787                         break;
788                 }
789         }
790         g_free(str);
791         return result;
792 }
793
794 static const gchar*
795 lookup_generic_option(combo_opts_t *opts, const GValue *gval)
796 {
797         gint ii;
798         gchar *str;
799         const gchar *result = "";
800
801         str = ghb_value_string(gval);
802         for (ii = 0; ii < opts->count; ii++)
803         {
804                 if (strcmp(opts->map[ii].shortOpt, str) == 0)
805                 {
806                         result = opts->map[ii].option;
807                         break;
808                 }
809         }
810         g_free(str);
811         return result;
812 }
813
814 static gint
815 lookup_mix_int(const GValue *mix)
816 {
817         gint ii;
818         gchar *str;
819         gint result = 0;
820
821
822         str = ghb_value_string(mix);
823         for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
824         {
825                 if (strcmp(hb_audio_mixdowns[ii].short_name, str) == 0)
826                 {
827                         result = hb_audio_mixdowns[ii].amixdown;
828                         break;
829                 }
830         }
831         g_free(str);
832         return result;
833 }
834
835 static const gchar*
836 lookup_mix_option(const GValue *mix)
837 {
838         gint ii;
839         gchar *str;
840         gchar *result = "None";
841
842
843         str = ghb_value_string(mix);
844         for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
845         {
846                 if (strcmp(hb_audio_mixdowns[ii].short_name, str) == 0)
847                 {
848                         result = hb_audio_mixdowns[ii].human_readable_name;
849                         break;
850                 }
851         }
852         g_free(str);
853         return result;
854 }
855
856 static gint
857 lookup_video_rate_int(const GValue *vrate)
858 {
859         gint ii;
860         gchar *str;
861         gint result = 0;
862
863         str = ghb_value_string(vrate);
864         for (ii = 0; ii < hb_video_rates_count; ii++)
865         {
866                 if (strcmp(hb_video_rates[ii].string, str) == 0)
867                 {
868                         result = hb_video_rates[ii].rate;
869                         break;
870                 }
871         }
872         g_free(str);
873         // Default to "same as source"
874         return result;
875 }
876
877 static const gchar*
878 lookup_video_rate_option(const GValue *vrate)
879 {
880         gint ii;
881         gchar *str;
882         const gchar *result = "Same as source";
883
884         str = ghb_value_string(vrate);
885         for (ii = 0; ii < hb_video_rates_count; ii++)
886         {
887                 if (strcmp(hb_video_rates[ii].string, str) == 0)
888                 {
889                         result = hb_video_rates[ii].string;
890                         break;
891                 }
892         }
893         g_free(str);
894         // Default to "same as source"
895         return result;
896 }
897
898 static gint
899 lookup_audio_rate_int(const GValue *rate)
900 {
901         gint ii;
902         gchar *str;
903         gint result = 0;
904
905         // Coincidentally, the string "source" will return 0
906         // which is our flag to use "same as source"
907         str = ghb_value_string(rate);
908         for (ii = 0; ii < hb_audio_rates_count; ii++)
909         {
910                 if (strcmp(hb_audio_rates[ii].string, str) == 0)
911                 {
912                         result = hb_audio_rates[ii].rate;
913                         break;
914                 }
915         }
916         g_free(str);
917         return result;
918 }
919
920 static const gchar*
921 lookup_audio_rate_option(const GValue *rate)
922 {
923         gint ii;
924         gchar *str;
925         const gchar *result = "Same as source";
926
927         // Coincidentally, the string "source" will return 0
928         // which is our flag to use "same as source"
929         str = ghb_value_string(rate);
930         for (ii = 0; ii < hb_audio_rates_count; ii++)
931         {
932                 if (strcmp(hb_audio_rates[ii].string, str) == 0)
933                 {
934                         result = hb_audio_rates[ii].string;
935                         break;
936                 }
937         }
938         g_free(str);
939         return result;
940 }
941
942 gint
943 ghb_find_closest_audio_bitrate(gint codec, gint rate)
944 {
945         gint ii;
946         gint low = 32;
947         gint high = 768;
948         gint result;
949
950         if (codec == HB_ACODEC_FAAC)
951                 high = 160;
952
953         result = high;
954         for (ii = 0; ii < hb_audio_bitrates_count; ii++)
955         {
956                 if (hb_audio_bitrates[ii].rate < low)
957                         continue;
958                 if (hb_audio_bitrates[ii].rate > high)
959                         break;
960                 if (rate <= hb_audio_bitrates[ii].rate)
961                 {
962                         result = hb_audio_bitrates[ii].rate;
963                         break;
964                 }
965         }
966         return result;
967 }
968
969 static gint
970 lookup_audio_bitrate_int(const GValue *rate)
971 {
972         gint ii;
973         gchar *str;
974         gint result = 0;
975
976         // Coincidentally, the string "source" will return 0
977         // which is our flag to use "same as source"
978         str = ghb_value_string(rate);
979         for (ii = 0; ii < hb_audio_bitrates_count; ii++)
980         {
981                 if (strcmp(hb_audio_bitrates[ii].string, str) == 0)
982                 {
983                         result = hb_audio_bitrates[ii].rate;
984                         break;
985                 }
986         }
987         g_free(str);
988         return result;
989 }
990
991 static const gchar*
992 lookup_audio_bitrate_option(const GValue *rate)
993 {
994         gint ii;
995         gchar *str;
996         const gchar *result = "Same as source";
997
998         // Coincidentally, the string "source" will return 0
999         // which is our flag to use "same as source"
1000         str = ghb_value_string(rate);
1001         for (ii = 0; ii < hb_audio_bitrates_count; ii++)
1002         {
1003                 if (strcmp(hb_audio_bitrates[ii].string, str) == 0)
1004                 {
1005                         result = hb_audio_bitrates[ii].string;
1006                         break;
1007                 }
1008         }
1009         g_free(str);
1010         return result;
1011 }
1012
1013 static gint
1014 lookup_audio_lang_int(const GValue *rate)
1015 {
1016         gint ii;
1017         gchar *str;
1018         gint result = 0;
1019
1020         // Coincidentally, the string "source" will return 0
1021         // which is our flag to use "same as source"
1022         str = ghb_value_string(rate);
1023         for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
1024         {
1025                 if (strcmp(ghb_language_table[ii].iso639_2, str) == 0)
1026                 {
1027                         result = ii;
1028                         break;
1029                 }
1030         }
1031         g_free(str);
1032         return result;
1033 }
1034
1035 static const gchar*
1036 lookup_audio_lang_option(const GValue *rate)
1037 {
1038         gint ii;
1039         gchar *str;
1040         const gchar *result = "Same as source";
1041
1042         // Coincidentally, the string "source" will return 0
1043         // which is our flag to use "same as source"
1044         str = ghb_value_string(rate);
1045         for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
1046         {
1047                 if (strcmp(ghb_language_table[ii].iso639_2, str) == 0)
1048                 {
1049                         if (ghb_language_table[ii].native_name[0] != 0)
1050                                 result = ghb_language_table[ii].native_name;
1051                         else
1052                                 result = ghb_language_table[ii].eng_name;
1053                         break;
1054                 }
1055         }
1056         g_free(str);
1057         return result;
1058 }
1059
1060 GValue*
1061 ghb_lookup_acodec_value(gint val)
1062 {
1063         GValue *value = NULL;
1064         gint ii;
1065
1066         for (ii = 0; ii < acodec_opts.count; ii++)
1067         {
1068                 if ((int)acodec_opts.map[ii].ivalue == val)
1069                 {
1070                         value = ghb_string_value_new(acodec_opts.map[ii].shortOpt);
1071                         break;
1072                 }
1073         }
1074         return value;
1075 }
1076
1077 static GValue*
1078 get_amix_value(gint val)
1079 {
1080         GValue *value = NULL;
1081         gint ii;
1082
1083         for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
1084         {
1085                 if (hb_audio_mixdowns[ii].amixdown == val)
1086                 {
1087                         value = ghb_string_value_new(hb_audio_mixdowns[ii].short_name);
1088                         break;
1089                 }
1090         }
1091         return value;
1092 }
1093
1094 // Handle for libhb.  Gets set by ghb_backend_init()
1095 static hb_handle_t * h_scan = NULL;
1096 static hb_handle_t * h_queue = NULL;
1097
1098 extern void hb_get_tempory_directory(hb_handle_t *h, char path[512]);
1099
1100 gchar*
1101 ghb_get_tmp_dir()
1102 {
1103         char dir[512];
1104
1105         hb_get_tempory_directory(h_scan, dir);
1106         return g_strdup(dir);
1107 }
1108
1109 void
1110 ghb_hb_cleanup(gboolean partial)
1111 {
1112         char dir[512];
1113
1114         hb_get_tempory_directory(h_scan, dir);
1115         del_tree(dir, !partial);
1116 }
1117
1118 gint
1119 ghb_subtitle_track_source(signal_user_data_t *ud, gint track)
1120 {
1121         gint titleindex;
1122
1123         if (track == -2)
1124                 return SRTSUB;
1125         if (track < 0)
1126                 return VOBSUB;
1127         titleindex = ghb_settings_combo_int(ud->settings, "title");
1128         if (titleindex < 0)
1129                 return VOBSUB;
1130
1131         hb_list_t  * list;
1132         hb_title_t * title;
1133         hb_subtitle_t * sub;
1134         
1135         if (h_scan == NULL) return VOBSUB;
1136         list = hb_get_titles( h_scan );
1137         if( !hb_list_count( list ) )
1138         {
1139                 /* No valid title, stop right there */
1140                 return VOBSUB;
1141         }
1142         title = hb_list_item( list, titleindex );
1143         if (title == NULL) return VOBSUB;       // Bad titleindex
1144         sub = hb_list_item( title->list_subtitle, track);
1145         if (sub != NULL)
1146                 return sub->source;
1147         else
1148                 return VOBSUB;
1149 }
1150
1151 const char*
1152 ghb_subtitle_track_source_name(signal_user_data_t *ud, gint track)
1153 {
1154         gint titleindex;
1155         const gchar * name = "Unknown";
1156
1157         if (track == -2)
1158         {
1159                 name = "SRT";
1160                 goto done;
1161         }
1162         if (track == -1)
1163         {
1164                 name = "Bitmap";
1165                 goto done;
1166         }
1167
1168         titleindex = ghb_settings_combo_int(ud->settings, "title");
1169         if (titleindex < 0)
1170                 goto done;
1171
1172         hb_list_t  * list;
1173         hb_title_t * title;
1174         hb_subtitle_t * sub;
1175         
1176         if (h_scan == NULL) 
1177                 goto done;
1178         list = hb_get_titles( h_scan );
1179         if( !hb_list_count( list ) )
1180                 goto done;
1181
1182         title = hb_list_item( list, titleindex );
1183         if (title == NULL)
1184                 goto done;
1185
1186         sub = hb_list_item( title->list_subtitle, track);
1187         if (sub != NULL)
1188         {
1189                 switch (sub->source)
1190                 {
1191                         case VOBSUB:
1192                                 name = "Bitmap";
1193                                 break;
1194                         case CC708SUB:
1195                         case CC608SUB:
1196                                 name = "Text";
1197                                 break;
1198                         case SRTSUB:
1199                                 name = "SRT";
1200                                 break;
1201                         default:
1202                                 break;
1203                 }
1204         }
1205
1206 done:
1207         return name;
1208 }
1209
1210 gchar*
1211 ghb_subtitle_track_lang(signal_user_data_t *ud, gint track)
1212 {
1213         gint titleindex;
1214
1215         titleindex = ghb_settings_combo_int(ud->settings, "title");
1216         if (titleindex < 0)
1217                 goto fail;
1218         if (track == -1)
1219                 return ghb_get_user_audio_lang(ud, titleindex, 0);
1220         if (track < 0)
1221                 goto fail;
1222
1223         hb_list_t  * list;
1224         hb_title_t * title;
1225         hb_subtitle_t * sub;
1226         
1227         if (h_scan == NULL)
1228                 goto fail;
1229
1230         list = hb_get_titles( h_scan );
1231         if( !hb_list_count( list ) )
1232         {
1233                 /* No valid title, stop right there */
1234                 goto fail;
1235         }
1236         title = hb_list_item( list, titleindex );
1237         if (title == NULL)      // Bad titleindex
1238                 goto fail;
1239         sub = hb_list_item( title->list_subtitle, track);
1240         if (sub != NULL)
1241                 return g_strdup(sub->iso639_2);
1242
1243 fail:
1244         return g_strdup("und");
1245 }
1246
1247 gint
1248 ghb_get_title_number(gint titleindex)
1249 {
1250         hb_list_t  * list;
1251         hb_title_t * title;
1252         
1253         if (h_scan == NULL) return 1;
1254         list = hb_get_titles( h_scan );
1255         if( !hb_list_count( list ) )
1256         {
1257                 /* No valid title, stop right there */
1258                 return 1;
1259         }
1260         title = hb_list_item( list, titleindex );
1261         if (title == NULL) return 1;    // Bad titleindex
1262         return title->index;
1263 }
1264
1265 static hb_audio_config_t*
1266 get_hb_audio(gint titleindex, gint track)
1267 {
1268         hb_list_t  * list;
1269         hb_title_t * title;
1270     hb_audio_config_t *audio = NULL;
1271         
1272     if (h_scan == NULL) return NULL;
1273         list = hb_get_titles( h_scan );
1274         if( !hb_list_count( list ) )
1275         {
1276                 /* No valid title, stop right there */
1277                 return NULL;
1278         }
1279     title = hb_list_item( list, titleindex );
1280         if (title == NULL) return NULL; // Bad titleindex
1281         if (!hb_list_count(title->list_audio))
1282         {
1283                 return NULL;
1284         }
1285     audio = (hb_audio_config_t *)hb_list_audio_config_item(title->list_audio, track);
1286         return audio;
1287 }
1288
1289 static gint
1290 search_rates(hb_rate_t *rates, gint rate, gint count)
1291 {
1292         gint ii;
1293         for (ii = 0; ii < count; ii++)
1294         {
1295                 if (rates[ii].rate == rate)
1296                         return ii;
1297         }
1298         return -1;
1299 }
1300
1301 static gboolean find_combo_item_by_int(GtkTreeModel *store, gint value, GtkTreeIter *iter);
1302
1303 static GtkListStore*
1304 get_combo_box_store(GtkBuilder *builder, const gchar *name)
1305 {
1306         GtkComboBox *combo;
1307         GtkListStore *store;
1308
1309         g_debug("get_combo_box_store() %s\n", name);
1310         // First modify the combobox model to allow greying out of options
1311         combo = GTK_COMBO_BOX(GHB_WIDGET(builder, name));
1312         store = GTK_LIST_STORE(gtk_combo_box_get_model (combo));
1313         return store;
1314 }
1315
1316 static void
1317 grey_combo_box_item(GtkBuilder *builder, const gchar *name, gint value, gboolean grey)
1318 {
1319         GtkListStore *store;
1320         GtkTreeIter iter;
1321         
1322         store = get_combo_box_store(builder, name);
1323         if (find_combo_item_by_int(GTK_TREE_MODEL(store), value, &iter))
1324         {
1325                 gtk_list_store_set(store, &iter, 
1326                                                    1, !grey, 
1327                                                    -1);
1328         }
1329 }
1330
1331 void
1332 ghb_grey_combo_options(GtkBuilder *builder)
1333 {
1334         GtkWidget *widget;
1335         gint container, track, titleindex, acodec;
1336     hb_audio_config_t *audio = NULL;
1337         GValue *gval;
1338         
1339         widget = GHB_WIDGET (builder, "title");
1340         gval = ghb_widget_value(widget);
1341         titleindex = ghb_lookup_combo_int("title", gval);
1342         ghb_value_free(gval);
1343         widget = GHB_WIDGET (builder, "AudioTrack");
1344         gval = ghb_widget_value(widget);
1345         track = ghb_lookup_combo_int("AudioTrack", gval);
1346         ghb_value_free(gval);
1347         audio = get_hb_audio(titleindex, track);
1348         widget = GHB_WIDGET (builder, "FileFormat");
1349         gval = ghb_widget_value(widget);
1350         container = ghb_lookup_combo_int("FileFormat", gval);
1351         ghb_value_free(gval);
1352
1353         grey_combo_box_item(builder, "x264_analyse", 3, TRUE);
1354         grey_combo_box_item(builder, "AudioEncoder", HB_ACODEC_FAAC, FALSE);
1355         grey_combo_box_item(builder, "AudioEncoder", HB_ACODEC_LAME, FALSE);
1356         grey_combo_box_item(builder, "AudioEncoder", HB_ACODEC_VORBIS, FALSE);
1357
1358         gboolean allow_dca = TRUE;
1359         allow_dca = (container != HB_MUX_MP4);
1360
1361         grey_combo_box_item(builder, "AudioEncoder", HB_ACODEC_AC3, FALSE);
1362         if (allow_dca)
1363                 grey_combo_box_item(builder, "AudioEncoder", HB_ACODEC_DCA, FALSE);
1364         else
1365                 grey_combo_box_item(builder, "AudioEncoder", HB_ACODEC_DCA, TRUE);
1366
1367         if (audio && audio->in.codec != HB_ACODEC_AC3)
1368         {
1369                 grey_combo_box_item(builder, "AudioEncoder", HB_ACODEC_AC3, TRUE);
1370         }
1371         if (audio && audio->in.codec != HB_ACODEC_DCA)
1372         {
1373                 grey_combo_box_item(builder, "AudioEncoder", HB_ACODEC_DCA, TRUE);
1374         }
1375         grey_combo_box_item(builder, "VideoEncoder", HB_VCODEC_THEORA, FALSE);
1376
1377         widget = GHB_WIDGET (builder, "AudioEncoder");
1378         gval = ghb_widget_value(widget);
1379         acodec = ghb_lookup_combo_int("AudioEncoder", gval);
1380         ghb_value_free(gval);
1381         if (acodec != HB_ACODEC_AC3)
1382         {
1383                 grey_combo_box_item(builder, "AudioMixdown", 0, TRUE);
1384         }
1385         if (container == HB_MUX_MP4)
1386         {
1387                 grey_combo_box_item(builder, "AudioEncoder", HB_ACODEC_LAME, TRUE);
1388                 grey_combo_box_item(builder, "AudioEncoder", HB_ACODEC_VORBIS, TRUE);
1389                 grey_combo_box_item(builder, "VideoEncoder", HB_VCODEC_THEORA, TRUE);
1390         }
1391
1392         gboolean allow_mono = TRUE;
1393         gboolean allow_stereo = TRUE;
1394         gboolean allow_dolby = TRUE;
1395         gboolean allow_dpl2 = TRUE;
1396         gboolean allow_6ch = TRUE;
1397         allow_mono = acodec & ~HB_ACODEC_LAME;
1398         allow_6ch = acodec & ~HB_ACODEC_LAME;
1399         if (audio)
1400         {
1401                 allow_mono = allow_mono &&
1402                         (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA));
1403                 gint layout = audio->in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
1404                 allow_stereo =
1405                         ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
1406                 allow_dolby =
1407                         (layout == HB_INPUT_CH_LAYOUT_3F1R) || 
1408                         (layout == HB_INPUT_CH_LAYOUT_3F2R) || 
1409                         (layout == HB_INPUT_CH_LAYOUT_DOLBY);
1410                 allow_dpl2 = (layout == HB_INPUT_CH_LAYOUT_3F2R);
1411                 allow_6ch = allow_6ch &&
1412                         (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
1413                         (layout == HB_INPUT_CH_LAYOUT_3F2R) && 
1414                         (audio->in.channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE);
1415         }
1416         grey_combo_box_item(builder, "AudioMixdown", HB_AMIXDOWN_MONO, !allow_mono);
1417         grey_combo_box_item(builder, "AudioMixdown", HB_AMIXDOWN_STEREO, !allow_stereo);
1418         grey_combo_box_item(builder, "AudioMixdown", HB_AMIXDOWN_DOLBY, !allow_dolby);
1419         grey_combo_box_item(builder, "AudioMixdown", HB_AMIXDOWN_DOLBYPLII, !allow_dpl2);
1420         grey_combo_box_item(builder, "AudioMixdown", HB_AMIXDOWN_6CH, !allow_6ch);
1421 }
1422
1423 gint
1424 ghb_get_best_audio_bitrate(gint acodec, gint br, gint channels)
1425 {
1426         if (acodec & HB_ACODEC_FAAC)
1427         {
1428         int maxbr = channels * 80;
1429                 if (br > maxbr)
1430                         br = maxbr;
1431         }
1432         return br;
1433 }
1434
1435 gint
1436 ghb_get_best_mix(gint titleindex, gint track, gint acodec, gint mix)
1437 {
1438     hb_audio_config_t *audio = NULL;
1439         gboolean allow_mono = TRUE;
1440         gboolean allow_stereo = TRUE;
1441         gboolean allow_dolby = TRUE;
1442         gboolean allow_dpl2 = TRUE;
1443         gboolean allow_6ch = TRUE;
1444         
1445         if (acodec & (HB_ACODEC_AC3 | HB_ACODEC_DCA))
1446         {
1447                 // Audio codec pass-thru.  No mixdown
1448                 return 0;
1449         }
1450         audio = get_hb_audio(titleindex, track);
1451         if (audio)
1452         {
1453                 allow_mono =
1454                         (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
1455                         (acodec & ~HB_ACODEC_LAME);
1456                 gint layout = audio->in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
1457                 allow_stereo =
1458                         ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
1459                 allow_dolby =
1460                         (layout == HB_INPUT_CH_LAYOUT_3F1R) || 
1461                         (layout == HB_INPUT_CH_LAYOUT_3F2R) || 
1462                         (layout == HB_INPUT_CH_LAYOUT_DOLBY);
1463                 allow_dpl2 = (layout == HB_INPUT_CH_LAYOUT_3F2R);
1464                 allow_6ch =
1465                         (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
1466                         (acodec & ~HB_ACODEC_LAME) &&
1467                         (layout == HB_INPUT_CH_LAYOUT_3F2R) && 
1468                         (audio->in.channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE);
1469         }
1470         gboolean greater = FALSE;
1471         if (mix == 0) 
1472         {
1473                 // If no mix is specified, select the best available.
1474                 mix = HB_AMIXDOWN_6CH;
1475         }
1476         if (mix == HB_AMIXDOWN_6CH)
1477         {
1478                 greater = TRUE;
1479                 if (allow_6ch) return HB_AMIXDOWN_6CH;
1480         }
1481         if (mix == HB_AMIXDOWN_DOLBYPLII || greater)
1482         {
1483                 greater = TRUE;
1484                 if (allow_dpl2) return HB_AMIXDOWN_DOLBYPLII;
1485         }
1486         if (mix == HB_AMIXDOWN_DOLBY || greater)
1487         {
1488                 greater = TRUE;
1489                 if (allow_dolby) return HB_AMIXDOWN_DOLBY;
1490         }
1491         if (mix == HB_AMIXDOWN_STEREO || greater)
1492         {
1493                 greater = TRUE;
1494                 if (allow_stereo) return HB_AMIXDOWN_STEREO;
1495         }
1496         if (mix == HB_AMIXDOWN_MONO || greater)
1497         {
1498                 greater = TRUE;
1499                 if (allow_mono) return HB_AMIXDOWN_MONO;
1500         }
1501         if (allow_stereo) return HB_AMIXDOWN_STEREO;
1502         if (allow_dolby) return HB_AMIXDOWN_DOLBY;
1503         if (allow_dpl2) return HB_AMIXDOWN_DOLBYPLII;
1504         if (allow_6ch) return HB_AMIXDOWN_6CH;
1505         return 0;
1506 }
1507
1508 // Set up the model for the combo box
1509 static void
1510 init_combo_box(GtkBuilder *builder, const gchar *name)
1511 {
1512         GtkComboBox *combo;
1513         GtkListStore *store;
1514         GtkCellRenderer *cell;
1515
1516         g_debug("init_combo_box() %s\n", name);
1517         // First modify the combobox model to allow greying out of options
1518         combo = GTK_COMBO_BOX(GHB_WIDGET(builder, name));
1519         if (combo == NULL)
1520                 return;
1521         // Store contains:
1522         // 1 - String to display
1523         // 2 - bool indicating whether the entry is selectable (grey or not)
1524         // 3 - String that is used for presets
1525         // 4 - Int value determined by backend
1526         // 5 - String value determined by backend
1527         store = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_BOOLEAN, 
1528                                                            G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_STRING);
1529         gtk_combo_box_set_model(combo, GTK_TREE_MODEL(store));
1530
1531         if (GTK_WIDGET_TYPE(combo) == GTK_TYPE_COMBO_BOX)
1532         {
1533                 gtk_cell_layout_clear(GTK_CELL_LAYOUT(combo));
1534         cell = GTK_CELL_RENDERER(gtk_cell_renderer_text_new());
1535         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), cell, TRUE);
1536         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), cell,
1537                 "markup", 0, "sensitive", 1, NULL);
1538         }
1539         else
1540         { // Combo box entry
1541                 gtk_combo_box_entry_set_text_column(GTK_COMBO_BOX_ENTRY(combo), 0);
1542         }
1543 }       
1544
1545 static void
1546 audio_samplerate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
1547 {
1548         GtkTreeIter iter;
1549         GtkListStore *store;
1550         gint ii;
1551         gchar *str;
1552         
1553         g_debug("audio_samplerate_opts_set ()\n");
1554         store = get_combo_box_store(builder, name);
1555         gtk_list_store_clear(store);
1556         // Add an item for "Same As Source"
1557         gtk_list_store_append(store, &iter);
1558         gtk_list_store_set(store, &iter, 
1559                                            0, "<small>Same as source</small>", 
1560                                            1, TRUE, 
1561                                            2, "source", 
1562                                            3, 0.0, 
1563                                            4, "source", 
1564                                            -1);
1565         for (ii = 0; ii < count; ii++)
1566         {
1567                 gtk_list_store_append(store, &iter);
1568                 str = g_strdup_printf("<small>%s</small>", rates[ii].string);
1569                 gtk_list_store_set(store, &iter, 
1570                                                    0, str,
1571                                                    1, TRUE, 
1572                                                    2, rates[ii].string, 
1573                                                    3, (gdouble)rates[ii].rate, 
1574                                                    4, rates[ii].string, 
1575                                                    -1);
1576                 g_free(str);
1577         }
1578 }
1579
1580 static void
1581 video_rate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
1582 {
1583         GtkTreeIter iter;
1584         GtkListStore *store;
1585         gint ii;
1586         
1587         g_debug("video_rate_opts_set ()\n");
1588         store = get_combo_box_store(builder, name);
1589         gtk_list_store_clear(store);
1590         // Add an item for "Same As Source"
1591         gtk_list_store_append(store, &iter);
1592         gtk_list_store_set(store, &iter, 
1593                                            0, "Same as source", 
1594                                            1, TRUE, 
1595                                            2, "source", 
1596                                            3, 0.0, 
1597                                            4, "source", 
1598                                            -1);
1599         for (ii = 0; ii < count; ii++)
1600         {
1601                 gchar *desc = "";
1602                 gchar *option;
1603                 if (strcmp(rates[ii].string, "23.976") == 0)
1604                 {
1605                         desc = "(NTSC Film)";
1606                 }
1607                 else if (strcmp(rates[ii].string, "25") == 0)
1608                 {
1609                         desc = "(PAL Film/Video)";
1610                 }
1611                 else if (strcmp(rates[ii].string, "29.97") == 0)
1612                 {
1613                         desc = "(NTSC Video)";
1614                 }
1615                 option = g_strdup_printf ("%s %s", rates[ii].string, desc);
1616                 gtk_list_store_append(store, &iter);
1617                 gtk_list_store_set(store, &iter, 
1618                                                    0, option, 
1619                                                    1, TRUE, 
1620                                                    2, rates[ii].string, 
1621                                                    3, (gdouble)rates[ii].rate, 
1622                                                    4, rates[ii].string, 
1623                                                    -1);
1624                 g_free(option);
1625         }
1626 }
1627
1628 static void
1629 mix_opts_set(GtkBuilder *builder, const gchar *name)
1630 {
1631         GtkTreeIter iter;
1632         GtkListStore *store;
1633         gint ii;
1634         gchar *str;
1635         
1636         g_debug("mix_opts_set ()\n");
1637         store = get_combo_box_store(builder, name);
1638         gtk_list_store_clear(store);
1639         gtk_list_store_append(store, &iter);
1640         gtk_list_store_set(store, &iter, 
1641                                            0, "<small>None</small>", 
1642                                            1, TRUE, 
1643                                            2, "none", 
1644                                            3, 0.0, 
1645                                            4, "none", 
1646                                            -1);
1647         for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
1648         {
1649                 gtk_list_store_append(store, &iter);
1650                 str = g_strdup_printf("<small>%s</small>",
1651                         hb_audio_mixdowns[ii].human_readable_name);
1652                 gtk_list_store_set(store, &iter, 
1653                                                    0, str,
1654                                                    1, TRUE, 
1655                                                    2, hb_audio_mixdowns[ii].short_name, 
1656                                                    3, (gdouble)hb_audio_mixdowns[ii].amixdown, 
1657                                                    4, hb_audio_mixdowns[ii].internal_name, 
1658                                                    -1);
1659                 g_free(str);
1660         }
1661 }
1662
1663 static void
1664 srt_codeset_opts_set(GtkBuilder *builder, const gchar *name)
1665 {
1666         GtkTreeIter iter;
1667         GtkListStore *store;
1668         gint ii;
1669         
1670         g_debug("srt_codeset_opts_set ()\n");
1671         store = get_combo_box_store(builder, name);
1672         gtk_list_store_clear(store);
1673         for (ii = 0; ii < SRT_TABLE_SIZE; ii++)
1674         {
1675                 gtk_list_store_append(store, &iter);
1676                 gtk_list_store_set(store, &iter, 
1677                                                    0, srt_codeset_table[ii],
1678                                                    1, TRUE, 
1679                                                    2, srt_codeset_table[ii],
1680                                                    3, (gdouble)ii, 
1681                                                    4, srt_codeset_table[ii],
1682                                                    -1);
1683         }
1684         GtkComboBoxEntry *cbe;
1685
1686         cbe = GTK_COMBO_BOX_ENTRY(GHB_WIDGET(builder, name));
1687         //gtk_combo_box_entry_set_text_column(cbe, 0);
1688 }
1689
1690 static void
1691 language_opts_set(GtkBuilder *builder, const gchar *name)
1692 {
1693         GtkTreeIter iter;
1694         GtkListStore *store;
1695         gint ii;
1696         
1697         g_debug("language_opts_set ()\n");
1698         store = get_combo_box_store(builder, name);
1699         gtk_list_store_clear(store);
1700         for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
1701         {
1702                 const gchar *lang;
1703
1704                 if (ghb_language_table[ii].native_name[0] != 0)
1705                         lang = ghb_language_table[ii].native_name;
1706                 else
1707                         lang = ghb_language_table[ii].eng_name;
1708                 
1709                 gtk_list_store_append(store, &iter);
1710                 gtk_list_store_set(store, &iter, 
1711                                                    0, lang,
1712                                                    1, TRUE, 
1713                                                    2, ghb_language_table[ii].iso639_2, 
1714                                                    3, (gdouble)ii, 
1715                                                    4, ghb_language_table[ii].iso639_1, 
1716                                                    -1);
1717         }
1718 }
1719
1720 static gchar **titles = NULL;
1721
1722 void
1723 title_opts_set(GtkBuilder *builder, const gchar *name)
1724 {
1725         GtkTreeIter iter;
1726         GtkListStore *store;
1727         hb_list_t  * list = NULL;
1728         hb_title_t * title = NULL;
1729         gint ii;
1730         gint count = 0;
1731         
1732         g_debug("title_opts_set ()\n");
1733         store = get_combo_box_store(builder, name);
1734         gtk_list_store_clear(store);
1735         if (h_scan != NULL)
1736         {
1737                 list = hb_get_titles( h_scan );
1738                 count = hb_list_count( list );
1739                 if (count > 100) count = 100;
1740         }
1741         if (titles) g_strfreev(titles);
1742         if (title_opts.map) g_free(title_opts.map);
1743         if (count > 0)
1744         {
1745                 title_opts.count = count;
1746                 title_opts.map = g_malloc(count*sizeof(options_map_t));
1747                 titles = g_malloc((count+1) * sizeof(gchar*));
1748         }
1749         else
1750         {
1751                 title_opts.count = 1;
1752                 title_opts.map = g_malloc(sizeof(options_map_t));
1753                 titles = NULL;
1754         }
1755         if( count <= 0 )
1756         {
1757                 // No titles.  Fill in a default.
1758                 gtk_list_store_append(store, &iter);
1759                 gtk_list_store_set(store, &iter, 
1760                                                    0, "No Titles", 
1761                                                    1, TRUE, 
1762                                                    2, "none", 
1763                                                    3, -1.0, 
1764                                                    4, "none", 
1765                                                    -1);
1766                 title_opts.map[0].option = "No Titles";
1767                 title_opts.map[0].shortOpt = "none";
1768                 title_opts.map[0].ivalue = -1;
1769                 title_opts.map[0].svalue = "none";
1770                 return;
1771         }
1772         for (ii = 0; ii < count; ii++)
1773         {
1774                 title = (hb_title_t*)hb_list_item(list, ii);
1775                 if (title->type == HB_STREAM_TYPE)
1776                 {
1777                         if (title->duration != 0)
1778                         {
1779                                 titles[ii]  = g_strdup_printf ("%d - %02dh%02dm%02ds - %s",
1780                                         title->index, title->hours, title->minutes, title->seconds, 
1781                                         title->name);
1782                         }
1783                         else
1784                         {
1785                                 titles[ii]  = g_strdup_printf ("%d - %s", 
1786                                                                                 title->index, title->name);
1787                         }
1788                 }
1789                 else
1790                 {
1791                         if (title->duration != 0)
1792                         {
1793                                 titles[ii]  = g_strdup_printf ("%d - %02dh%02dm%02ds",
1794                                         title->index, title->hours, title->minutes, title->seconds);
1795                         }
1796                         else
1797                         {
1798                                 titles[ii]  = g_strdup_printf ("%d - Unknown Length", 
1799                                                                                 title->index);
1800                         }
1801                 }
1802                 gtk_list_store_append(store, &iter);
1803                 gtk_list_store_set(store, &iter, 
1804                                                    0, titles[ii], 
1805                                                    1, TRUE, 
1806                                                    2, titles[ii], 
1807                                                    3, (gdouble)ii, 
1808                                                    4, titles[ii], 
1809                                                    -1);
1810                 title_opts.map[ii].option = titles[ii];
1811                 title_opts.map[ii].shortOpt = titles[ii];
1812                 title_opts.map[ii].ivalue = ii;
1813                 title_opts.map[ii].svalue = titles[ii];
1814         }
1815         titles[ii] = NULL;
1816 }
1817
1818 static gboolean
1819 find_combo_item_by_int(GtkTreeModel *store, gint value, GtkTreeIter *iter)
1820 {
1821         gdouble ivalue;
1822         gboolean foundit = FALSE;
1823         
1824         if (gtk_tree_model_get_iter_first (store, iter))
1825         {
1826                 do
1827                 {
1828                         gtk_tree_model_get(store, iter, 3, &ivalue, -1);
1829                         if (value == (int)ivalue)
1830                         {
1831                                 foundit = TRUE;
1832                                 break;
1833                         }
1834                 } while (gtk_tree_model_iter_next (store, iter));
1835         }
1836         return foundit;
1837 }
1838
1839 void
1840 audio_track_opts_set(GtkBuilder *builder, const gchar *name, gint titleindex)
1841 {
1842         GtkTreeIter iter;
1843         GtkListStore *store;
1844         hb_list_t  * list = NULL;
1845         hb_title_t * title = NULL;
1846     hb_audio_config_t * audio;
1847         gint ii;
1848         gint count = 0;
1849         gchar *str;
1850         
1851         g_debug("audio_track_opts_set ()\n");
1852         store = get_combo_box_store(builder, name);
1853         gtk_list_store_clear(store);
1854         if (h_scan != NULL)
1855         {
1856                 list = hb_get_titles( h_scan );
1857             title = (hb_title_t*)hb_list_item( list, titleindex );
1858                 if (title != NULL)
1859                 {
1860                         count = hb_list_count( title->list_audio );
1861                 }
1862         }
1863         if (count > 100) count = 100;
1864         if (audio_track_opts.map) g_free(audio_track_opts.map);
1865         if (count > 0)
1866         {
1867                 audio_track_opts.count = count;
1868                 audio_track_opts.map = g_malloc(count*sizeof(options_map_t));
1869         }
1870         else
1871         {
1872                 audio_track_opts.count = 1;
1873                 audio_track_opts.map = g_malloc(sizeof(options_map_t));
1874         }
1875         if( count <= 0 )
1876         {
1877                 // No audio. set some default
1878                 gtk_list_store_append(store, &iter);
1879                 gtk_list_store_set(store, &iter, 
1880                                                    0, "<small>No Audio</small>", 
1881                                                    1, TRUE, 
1882                                                    2, "none", 
1883                                                    3, -1.0, 
1884                                                    4, "none", 
1885                                                    -1);
1886                 audio_track_opts.map[0].option = "No Audio";
1887                 audio_track_opts.map[0].shortOpt = "none";
1888                 audio_track_opts.map[0].ivalue = -1;
1889                 audio_track_opts.map[0].svalue = "none";
1890                 return;
1891         }
1892         index_str_init(count-1);
1893         for (ii = 0; ii < count; ii++)
1894         {
1895         audio = (hb_audio_config_t *) hb_list_audio_config_item( title->list_audio, ii );
1896                 gtk_list_store_append(store, &iter);
1897                 str = g_strdup_printf("<small>%s</small>", audio->lang.description);
1898                 gtk_list_store_set(store, &iter, 
1899                                                    0, str,
1900                                                    1, TRUE, 
1901                                                    2, index_str[ii], 
1902                                                    3, (gdouble)ii, 
1903                                                    4, index_str[ii], 
1904                                                    -1);
1905                 g_free(str);
1906                 audio_track_opts.map[ii].option = audio->lang.description,
1907                 audio_track_opts.map[ii].shortOpt = index_str[ii];
1908                 audio_track_opts.map[ii].ivalue = ii;
1909                 audio_track_opts.map[ii].svalue = index_str[ii];
1910         }
1911 }
1912
1913 void
1914 subtitle_track_opts_set(GtkBuilder *builder, const gchar *name, gint titleindex)
1915 {
1916         GtkTreeIter iter;
1917         GtkListStore *store;
1918         hb_list_t  * list = NULL;
1919         hb_title_t * title = NULL;
1920         hb_subtitle_t * subtitle;
1921         gint ii, count = 0;
1922         static char ** options = NULL;
1923         
1924         g_debug("subtitle_track_opts_set ()\n");
1925         store = get_combo_box_store(builder, name);
1926         gtk_list_store_clear(store);
1927         if (h_scan != NULL)
1928         {
1929                 list = hb_get_titles( h_scan );
1930             title = (hb_title_t*)hb_list_item( list, titleindex );
1931                 if (title != NULL)
1932                 {
1933                         count = hb_list_count( title->list_subtitle );
1934                 }
1935         }
1936         if (count > 100) count = 100;
1937         if (subtitle_opts.map) g_free(subtitle_opts.map);
1938         if (count > 0)
1939         {
1940                 subtitle_opts.count = count+1;
1941                 subtitle_opts.map = g_malloc((count+1)*sizeof(options_map_t));
1942         }
1943         else
1944         {
1945                 subtitle_opts.count = LANG_TABLE_SIZE+1;
1946                 subtitle_opts.map = g_malloc((LANG_TABLE_SIZE+1)*sizeof(options_map_t));
1947         }
1948         gtk_list_store_append(store, &iter);
1949         gtk_list_store_set(store, &iter, 
1950                                            0, "Foreign Audio Search", 
1951                                            1, TRUE, 
1952                                            2, "-1", 
1953                                            3, -1.0, 
1954                                            4, "auto", 
1955                                            -1);
1956         subtitle_opts.map[0].option = "Foreign Audio Search";
1957         subtitle_opts.map[0].shortOpt = "-1";
1958         subtitle_opts.map[0].ivalue = -1;
1959         subtitle_opts.map[0].svalue = "auto";
1960         if (count > 0)
1961         {
1962                 if (options != NULL)
1963                         g_strfreev(options);
1964                 options = g_malloc((count+1)*sizeof(gchar*));
1965                 index_str_init(count-1);
1966                 for (ii = 0; ii < count; ii++)
1967                 {
1968                 subtitle = (hb_subtitle_t *)hb_list_item(title->list_subtitle, ii);
1969                         // Skip subtitles that must be burned if there is already
1970                         // a burned subtitle in the list
1971                         options[ii] = g_strdup_printf("%d - %s", ii+1, subtitle->lang);
1972                         subtitle_opts.map[ii+1].option = options[ii];
1973                         subtitle_opts.map[ii+1].shortOpt = index_str[ii];
1974                         subtitle_opts.map[ii+1].ivalue = ii;
1975                         subtitle_opts.map[ii+1].svalue = subtitle->iso639_2;
1976                         gtk_list_store_append(store, &iter);
1977                         gtk_list_store_set(store, &iter, 
1978                                                 0, options[ii], 
1979                                                 1, TRUE, 
1980                                                 2, index_str[ii], 
1981                                                 3, (gdouble)ii, 
1982                                                 4, subtitle->iso639_2, 
1983                                                 -1);
1984                 }
1985                 options[count] = NULL;
1986         }
1987         else
1988         {
1989                 index_str_init(LANG_TABLE_SIZE-1);
1990                 for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
1991                 {
1992                         const gchar *lang;
1993
1994                         if (ghb_language_table[ii].native_name[0] != 0)
1995                                 lang = ghb_language_table[ii].native_name;
1996                         else
1997                                 lang = ghb_language_table[ii].eng_name;
1998
1999                         subtitle_opts.map[ii+1].option = lang;
2000                         subtitle_opts.map[ii+1].shortOpt = index_str[ii];
2001                         subtitle_opts.map[ii+1].ivalue = ii;
2002                         subtitle_opts.map[ii+1].svalue = ghb_language_table[ii].iso639_2;
2003                         gtk_list_store_append(store, &iter);
2004                         gtk_list_store_set(store, &iter, 
2005                                         0, lang,
2006                                         1, TRUE, 
2007                                         2, index_str[ii],
2008                                         3, (gdouble)ii, 
2009                                         4, ghb_language_table[ii].iso639_2, 
2010                                         -1);
2011                 }
2012         }
2013 }
2014
2015 gint
2016 ghb_longest_title()
2017 {
2018         hb_list_t  * list;
2019         hb_title_t * title;
2020         gint ii;
2021         gint count = 0;
2022         guint64 longest = 0;
2023         gint titleindex = 0;
2024         
2025         g_debug("ghb_longest_title ()\n");
2026         if (h_scan == NULL) return 0;
2027         list = hb_get_titles( h_scan );
2028         count = hb_list_count( list );
2029         if (count > 100) count = 100;
2030         for (ii = 0; ii < count; ii++)
2031         {
2032                 title = (hb_title_t*)hb_list_item(list, ii);
2033                 if (title->duration > longest)
2034                 {
2035                         titleindex = ii;
2036                         longest = title->duration;
2037                 }
2038         }
2039         return titleindex;
2040 }
2041
2042 gchar*
2043 ghb_get_source_audio_lang(gint titleindex, gint track)
2044 {
2045         hb_list_t  * list;
2046         hb_title_t * title;
2047     hb_audio_config_t * audio;
2048         gchar *lang = NULL;
2049         
2050         g_debug("ghb_lookup_1st_audio_lang ()\n");
2051         if (h_scan == NULL) 
2052                 return NULL;
2053         list = hb_get_titles( h_scan );
2054     title = (hb_title_t*)hb_list_item( list, titleindex );
2055         if (title == NULL)
2056                 return NULL;
2057         if (hb_list_count( title->list_audio ) <= track)
2058                 return NULL;
2059
2060         audio = hb_list_audio_config_item(title->list_audio, track);
2061         if (audio == NULL)
2062                 return NULL;
2063
2064         lang = g_strdup(audio->lang.iso639_2);
2065         return lang;
2066 }
2067
2068 static gboolean*
2069 get_track_used(gint acodec, GHashTable *track_indices, gint count)
2070 {
2071         gboolean *used;
2072
2073         used = g_hash_table_lookup(track_indices, &acodec);
2074         if (used == NULL)
2075         {
2076                 gint *key;
2077
2078                 used = g_malloc0(count * sizeof(gboolean));
2079                 key = g_malloc(sizeof(gint));
2080                 *key = acodec;
2081                 g_hash_table_insert(track_indices, key, used);
2082         }
2083         return used;
2084 }
2085
2086 gint
2087 ghb_find_audio_track(
2088         gint titleindex, 
2089         const gchar *lang, 
2090         gint acodec,
2091         gint fallback_acodec,
2092         GHashTable *track_indices)
2093 {
2094         hb_list_t  * list;
2095         hb_title_t * title;
2096         hb_audio_config_t * audio;
2097         gint ii;
2098         gint count = 0;
2099         gint track = -1;
2100         gint max_chan = 0;
2101         gboolean *used;
2102         gboolean *passthru_used;
2103         gint try_acodec;
2104         gint passthru_acodec;
2105         gboolean passthru;
2106         
2107         g_debug("find_audio_track ()\n");
2108         if (h_scan == NULL) return -1;
2109         list = hb_get_titles( h_scan );
2110         title = (hb_title_t*)hb_list_item( list, titleindex );
2111         if (title != NULL)
2112         {
2113                 count = hb_list_count( title->list_audio );
2114         }
2115         if (count > 10) count = 10;
2116         // Try to find an item that matches the preferred language and
2117         // the passthru codec type
2118         passthru = (acodec & (HB_ACODEC_AC3 | HB_ACODEC_DCA)) != 0;
2119         if (passthru)
2120         {
2121                 for (ii = 0; ii < count; ii++)
2122                 {
2123                         gint channels;
2124
2125                         audio = (hb_audio_config_t*)hb_list_audio_config_item( 
2126                                                                                                         title->list_audio, ii );
2127                         passthru_acodec = (HB_ACODEC_AC3 | HB_ACODEC_DCA) & audio->in.codec;
2128                         // Is the source track use a passthru capable codec?
2129                         if (passthru_acodec == 0)
2130                                 continue;
2131                         used = get_track_used(passthru_acodec, track_indices, count);
2132                         // Has the track already been used with this codec?
2133                         if (used[ii])
2134                                 continue;
2135
2136                         channels = HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT(
2137                                                                                                         audio->in.channel_layout);
2138                         // Find a track that is not visually impaired or dirctor's
2139                         // commentary, and has the highest channel count.
2140                         if ((audio->lang.type < 2) &&
2141                                 ((strcmp(lang, audio->lang.iso639_2) == 0) ||
2142                                 (strcmp(lang, "und") == 0)))
2143                         {
2144                                 if (channels > max_chan)
2145                                 {
2146                                         track = ii;
2147                                         max_chan = channels;
2148                                 }
2149                         }
2150                 }
2151                 try_acodec = fallback_acodec;
2152         }
2153         else
2154         {
2155                 try_acodec = acodec;
2156         }
2157         if (track > -1)
2158         {
2159                 used[track] = TRUE;
2160                 return track;
2161         }
2162         // Try to find an item that matches the preferred language
2163         used = get_track_used(try_acodec, track_indices, count);
2164         for (ii = 0; ii < count; ii++)
2165         {
2166                 // Has the track already been used with this codec?
2167                 if (used[ii])
2168                         continue;
2169                 audio = (hb_audio_config_t*)hb_list_audio_config_item( 
2170                                                                                                 title->list_audio, ii );
2171                 passthru_acodec = (HB_ACODEC_AC3 | HB_ACODEC_DCA) & audio->in.codec;
2172                 if (passthru_acodec && passthru)
2173                 {
2174                         passthru_used = get_track_used(passthru_acodec, track_indices, count);
2175                         // Has the track already been used with this codec for passthru?
2176                         if (passthru_used[ii])
2177                                 continue;
2178                 }
2179                 // Find a track that is not visually impaired or dirctor's commentary
2180                 if ((audio->lang.type < 2) &&
2181                         ((strcmp(lang, audio->lang.iso639_2) == 0) ||
2182                         (strcmp(lang, "und") == 0)))
2183                 {
2184                         track = ii;
2185                         break;
2186                 }
2187         }
2188         if (track > -1)
2189         {
2190                 used[track] = TRUE;
2191                 return track;
2192         }
2193         // Try to fine an item that does not match the preferred language and
2194         // matches the passthru codec type
2195         if (passthru)
2196         {
2197                 for (ii = 0; ii < count; ii++)
2198                 {
2199                         gint channels;
2200
2201                         audio = (hb_audio_config_t*)hb_list_audio_config_item( 
2202                                                                                                         title->list_audio, ii );
2203                         passthru_acodec = (HB_ACODEC_AC3 | HB_ACODEC_DCA) & audio->in.codec;
2204                         // Is the source track use a passthru capable codec?
2205                         if (passthru_acodec == 0)
2206                                 continue;
2207                         used = get_track_used(passthru_acodec, track_indices, count);
2208                         // Has the track already been used with this codec?
2209                         if (used[ii])
2210                                 continue;
2211
2212                         channels = HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT(
2213                                                                                                         audio->in.channel_layout);
2214                         // Find a track that is not visually impaired or dirctor's
2215                         // commentary, and has the highest channel count.
2216                         if (audio->lang.type < 2)
2217                         {
2218                                 if (channels > max_chan)
2219                                 {
2220                                         track = ii;
2221                                         max_chan = channels;
2222                                 }
2223                         }
2224                 }
2225                 try_acodec = fallback_acodec;
2226         }
2227         else
2228         {
2229                 try_acodec = acodec;
2230         }
2231         if (track > -1)
2232         {
2233                 used[track] = TRUE;
2234                 return track;
2235         }
2236         // Try to fine an item that does not match the preferred language
2237         used = get_track_used(try_acodec, track_indices, count);
2238         for (ii = 0; ii < count; ii++)
2239         {
2240                 // Has the track already been used with this codec?
2241                 if (used[ii])
2242                         continue;
2243                 audio = (hb_audio_config_t*)hb_list_audio_config_item( 
2244                                                                                                         title->list_audio, ii );
2245                 passthru_acodec = (HB_ACODEC_AC3 | HB_ACODEC_DCA) & audio->in.codec;
2246                 if (passthru_acodec && passthru)
2247                 {
2248                         passthru_used = get_track_used(passthru_acodec, track_indices, count);
2249                         // Has the track already been used with this codec for passthru?
2250                         if (passthru_used[ii])
2251                                 continue;
2252                 }
2253                 // Find a track that is not visually impaired or dirctor's commentary
2254                 if (audio->lang.type < 2)
2255                 {
2256                         track = ii;
2257                         break;
2258                 }
2259         }
2260         if (track > -1)
2261         {
2262                 used[track] = TRUE;
2263                 return track;
2264         }
2265         // Last ditch, anything goes
2266         for (ii = 0; ii < count; ii++)
2267         {
2268                 audio = (hb_audio_config_t*)hb_list_audio_config_item( 
2269                                                                                                 title->list_audio, ii );
2270                 passthru_acodec = (HB_ACODEC_AC3 | HB_ACODEC_DCA) & audio->in.codec;
2271                 if (passthru_acodec && passthru)
2272                 {
2273                         passthru_used = get_track_used(passthru_acodec, track_indices, count);
2274                         // Has the track already been used with this codec for passthru?
2275                         if (passthru_used[ii])
2276                                 continue;
2277                 }
2278                 // Has the track already been used with this codec?
2279                 if (!used[ii])
2280                 {
2281                         track = ii;
2282                         break;
2283                 }
2284         }
2285         if (track > -1)
2286         {
2287                 used[track] = TRUE;
2288         }
2289         return track;
2290 }
2291
2292 gint
2293 ghb_find_pref_subtitle_track(const gchar *lang)
2294 {
2295         gint ii, count;
2296         count = subtitle_opts.count;
2297         for (ii = 0; ii < count; ii++)
2298         {
2299                 if (strcmp(lang, subtitle_opts.map[ii].svalue) == 0)
2300                 {
2301                         return subtitle_opts.map[ii].ivalue;
2302                 }
2303         }
2304         return -2;
2305 }
2306
2307 gint
2308 ghb_find_cc_track(gint titleindex)
2309 {
2310         hb_list_t  * list;
2311         hb_title_t * title;
2312         hb_subtitle_t * subtitle;
2313         gint count, ii;
2314         
2315         g_debug("ghb_find_cc_track ()\n");
2316         if (h_scan == NULL) return -2;
2317         list = hb_get_titles( h_scan );
2318         title = (hb_title_t*)hb_list_item( list, titleindex );
2319         if (title != NULL)
2320         {
2321                 count = hb_list_count( title->list_subtitle );
2322                 // Try to find an item that matches the preferred language
2323                 for (ii = 0; ii < count; ii++)
2324                 {
2325                 subtitle = (hb_subtitle_t*)hb_list_item( title->list_subtitle, ii );
2326                         if (subtitle->source == CC608SUB || subtitle->source == CC708SUB)
2327                                 return ii;
2328                 }
2329         }
2330         return -2;
2331 }
2332
2333 gint
2334 ghb_find_subtitle_track(
2335         gint          titleindex, 
2336         const gchar * lang, 
2337         gboolean      burn,
2338         gboolean      force,
2339         gint          source,
2340         GHashTable  * track_indices)
2341 {
2342         hb_list_t  * list;
2343         hb_title_t * title;
2344         hb_subtitle_t * subtitle;
2345         gint count, ii;
2346         gboolean *used;
2347         
2348         g_debug("find_subtitle_track ()\n");
2349         if (strcmp(lang, "auto") == 0)
2350                 return -1;
2351         if (h_scan == NULL) return -1;
2352         list = hb_get_titles( h_scan );
2353         title = (hb_title_t*)hb_list_item( list, titleindex );
2354         if (title != NULL)
2355         {
2356                 count = hb_list_count( title->list_subtitle );
2357                 used = g_hash_table_lookup(track_indices, lang);
2358                 if (used == NULL)
2359                 {
2360                         used = g_malloc0(count * sizeof(gboolean));
2361                         g_hash_table_insert(track_indices, g_strdup(lang), used);
2362                 }
2363                 // Try to find an item that matches the preferred language and source
2364                 for (ii = 0; ii < count; ii++)
2365                 {
2366                         if (used[ii])
2367                                 continue;
2368
2369                 subtitle = (hb_subtitle_t*)hb_list_item( title->list_subtitle, ii );
2370                         if (source == subtitle->source &&
2371                                 ((strcmp(lang, subtitle->iso639_2) == 0) ||
2372                                  (strcmp(lang, "und") == 0)))
2373                         {
2374                                 used[ii] = TRUE;
2375                                 return ii;
2376                         }
2377                 }
2378                 // Try to find an item that matches the preferred language
2379                 for (ii = 0; ii < count; ii++)
2380                 {
2381                         if (used[ii])
2382                                 continue;
2383
2384                 subtitle = (hb_subtitle_t*)hb_list_item( title->list_subtitle, ii );
2385                         if (((burn || force) && (subtitle->source == VOBSUB)) &&
2386                                 ((strcmp(lang, subtitle->iso639_2) == 0) ||
2387                                  (strcmp(lang, "und") == 0)))
2388                         {
2389                                 used[ii] = TRUE;
2390                                 return ii;
2391                         }
2392                 }
2393         }
2394         return -2;
2395 }
2396
2397 static void
2398 generic_opts_set(GtkBuilder *builder, const gchar *name, combo_opts_t *opts)
2399 {
2400         GtkTreeIter iter;
2401         GtkListStore *store;
2402         gint ii;
2403         
2404         g_debug("generic_opts_set ()\n");
2405         if (name == NULL || opts == NULL) return;
2406         store = get_combo_box_store(builder, name);
2407         gtk_list_store_clear(store);
2408         for (ii = 0; ii < opts->count; ii++)
2409         {
2410                 gtk_list_store_append(store, &iter);
2411                 gtk_list_store_set(store, &iter, 
2412                                                    0, opts->map[ii].option, 
2413                                                    1, TRUE, 
2414                                                    2, opts->map[ii].shortOpt, 
2415                                                    3, opts->map[ii].ivalue, 
2416                                                    4, opts->map[ii].svalue, 
2417                                                    -1);
2418         }
2419 }
2420
2421 static void
2422 small_opts_set(GtkBuilder *builder, const gchar *name, combo_opts_t *opts)
2423 {
2424         GtkTreeIter iter;
2425         GtkListStore *store;
2426         gint ii;
2427         gchar *str;
2428         
2429         g_debug("generic_opts_set ()\n");
2430         if (name == NULL || opts == NULL) return;
2431         store = get_combo_box_store(builder, name);
2432         gtk_list_store_clear(store);
2433         for (ii = 0; ii < opts->count; ii++)
2434         {
2435                 gtk_list_store_append(store, &iter);
2436                 str = g_strdup_printf("<small>%s</small>", opts->map[ii].option);
2437                 gtk_list_store_set(store, &iter, 
2438                                                    0, str,
2439                                                    1, TRUE, 
2440                                                    2, opts->map[ii].shortOpt, 
2441                                                    3, opts->map[ii].ivalue, 
2442                                                    4, opts->map[ii].svalue, 
2443                                                    -1);
2444                 g_free(str);
2445         }
2446 }
2447
2448 combo_opts_t*
2449 find_combo_table(const gchar *name)
2450 {
2451         gint ii;
2452
2453         for (ii = 0; combo_name_map[ii].name != NULL; ii++)
2454         {
2455                 if (strcmp(name, combo_name_map[ii].name) == 0)
2456                 {
2457                         return combo_name_map[ii].opts;
2458                 }
2459         }
2460         return NULL;
2461 }
2462
2463 gint
2464 ghb_lookup_combo_int(const gchar *name, const GValue *gval)
2465 {
2466         if (gval == NULL)
2467                 return 0;
2468         if (strcmp(name, "AudioBitrate") == 0)
2469                 return lookup_audio_bitrate_int(gval);
2470         else if (strcmp(name, "AudioSamplerate") == 0)
2471                 return lookup_audio_rate_int(gval);
2472         else if (strcmp(name, "VideoFramerate") == 0)
2473                 return lookup_video_rate_int(gval);
2474         else if (strcmp(name, "AudioMixdown") == 0)
2475                 return lookup_mix_int(gval);
2476         else if (strcmp(name, "SrtLanguage") == 0)
2477                 return lookup_audio_lang_int(gval);
2478         else if (strcmp(name, "PreferredLanguage") == 0)
2479                 return lookup_audio_lang_int(gval);
2480         else
2481         {
2482                 return lookup_generic_int(find_combo_table(name), gval);
2483         }
2484         g_warning("ghb_lookup_combo_int() couldn't find %s", name);
2485         return 0;
2486 }
2487
2488 gdouble
2489 ghb_lookup_combo_double(const gchar *name, const GValue *gval)
2490 {
2491         if (gval == NULL)
2492                 return 0;
2493         if (strcmp(name, "AudioBitrate") == 0)
2494                 return lookup_audio_bitrate_int(gval);
2495         else if (strcmp(name, "AudioSamplerate") == 0)
2496                 return lookup_audio_rate_int(gval);
2497         else if (strcmp(name, "VideoFramerate") == 0)
2498                 return lookup_video_rate_int(gval);
2499         else if (strcmp(name, "AudioMixdown") == 0)
2500                 return lookup_mix_int(gval);
2501         else if (strcmp(name, "SrtLanguage") == 0)
2502                 return lookup_audio_lang_int(gval);
2503         else if (strcmp(name, "PreferredLanguage") == 0)
2504                 return lookup_audio_lang_int(gval);
2505         else
2506         {
2507                 return lookup_generic_double(find_combo_table(name), gval);
2508         }
2509         g_warning("ghb_lookup_combo_double() couldn't find %s", name);
2510         return 0;
2511 }
2512
2513 const gchar*
2514 ghb_lookup_combo_option(const gchar *name, const GValue *gval)
2515 {
2516         if (gval == NULL)
2517                 return NULL;
2518         if (strcmp(name, "AudioBitrate") == 0)
2519                 return lookup_audio_bitrate_option(gval);
2520         else if (strcmp(name, "AudioSamplerate") == 0)
2521                 return lookup_audio_rate_option(gval);
2522         else if (strcmp(name, "VideoFramerate") == 0)
2523                 return lookup_video_rate_option(gval);
2524         else if (strcmp(name, "AudioMixdown") == 0)
2525                 return lookup_mix_option(gval);
2526         else if (strcmp(name, "SrtLanguage") == 0)
2527                 return lookup_audio_lang_option(gval);
2528         else if (strcmp(name, "PreferredLanguage") == 0)
2529                 return lookup_audio_lang_option(gval);
2530         else
2531         {
2532                 return lookup_generic_option(find_combo_table(name), gval);
2533         }
2534         g_warning("ghb_lookup_combo_int() couldn't find %s", name);
2535         return NULL;
2536 }
2537
2538 const gchar*
2539 ghb_lookup_combo_string(const gchar *name, const GValue *gval)
2540 {
2541         if (gval == NULL)
2542                 return NULL;
2543         if (strcmp(name, "AudioBitrate") == 0)
2544                 return lookup_audio_bitrate_option(gval);
2545         else if (strcmp(name, "AudioSamplerate") == 0)
2546                 return lookup_audio_rate_option(gval);
2547         else if (strcmp(name, "VideoFramerate") == 0)
2548                 return lookup_video_rate_option(gval);
2549         else if (strcmp(name, "AudioMixdown") == 0)
2550                 return lookup_mix_option(gval);
2551         else if (strcmp(name, "SrtLanguage") == 0)
2552                 return lookup_audio_lang_option(gval);
2553         else if (strcmp(name, "PreferredLanguage") == 0)
2554                 return lookup_audio_lang_option(gval);
2555         else
2556         {
2557                 return lookup_generic_string(find_combo_table(name), gval);
2558         }
2559         g_warning("ghb_lookup_combo_int() couldn't find %s", name);
2560         return NULL;
2561 }
2562
2563 void
2564 ghb_update_ui_combo_box(
2565         signal_user_data_t *ud, 
2566         const gchar *name, 
2567         gint user_data, 
2568         gboolean all)
2569 {
2570         GtkComboBox *combo = NULL;
2571         gint signal_id;
2572         gint handler_id = 0;
2573
2574         if (name != NULL)
2575         {               
2576                 g_debug("ghb_update_ui_combo_box() %s\n", name);
2577                 // Clearing a combo box causes a rash of "changed" events, even when
2578                 // the active item is -1 (inactive).  To control things, I'm disabling
2579                 // the event till things are settled down.
2580                 combo = GTK_COMBO_BOX(GHB_WIDGET(ud->builder, name));
2581                 signal_id = g_signal_lookup("changed", GTK_TYPE_COMBO_BOX);
2582                 if (signal_id > 0)
2583                 {
2584                         // Valid signal id found.  This should always succeed.
2585                         handler_id = g_signal_handler_find ((gpointer)combo, G_SIGNAL_MATCH_ID, 
2586                                                                                                 signal_id, 0, 0, 0, 0);
2587                         if (handler_id > 0)
2588                         {
2589                                 // This should also always succeed
2590                                 g_signal_handler_block ((gpointer)combo, handler_id);
2591                         }
2592                 }
2593         }       
2594         if (all)
2595         {
2596                 audio_bitrate_opts_set(ud->builder, "AudioBitrate");
2597                 audio_samplerate_opts_set(ud->builder, "AudioSamplerate", hb_audio_rates, hb_audio_rates_count);
2598                 video_rate_opts_set(ud->builder, "VideoFramerate", hb_video_rates, hb_video_rates_count);
2599                 mix_opts_set(ud->builder, "AudioMixdown");
2600                 language_opts_set(ud->builder, "SrtLanguage");
2601                 language_opts_set(ud->builder, "PreferredLanguage");
2602                 srt_codeset_opts_set(ud->builder, "SrtCodeset");
2603                 title_opts_set(ud->builder, "title");
2604                 audio_track_opts_set(ud->builder, "AudioTrack", user_data);
2605                 subtitle_track_opts_set(ud->builder, "SubtitleTrack", user_data);
2606                 generic_opts_set(ud->builder, "VideoQualityGranularity", &vqual_granularity_opts);
2607                 generic_opts_set(ud->builder, "PtoPType", &point_to_point_opts);
2608                 generic_opts_set(ud->builder, "WhenComplete", &when_complete_opts);
2609                 generic_opts_set(ud->builder, "PicturePAR", &par_opts);
2610                 generic_opts_set(ud->builder, "PictureModulus", &alignment_opts);
2611                 generic_opts_set(ud->builder, "LoggingLevel", &logging_opts);
2612                 generic_opts_set(ud->builder, "LogLongevity", &log_longevity_opts);
2613                 generic_opts_set(ud->builder, "check_updates", &appcast_update_opts);
2614                 generic_opts_set(ud->builder, "FileFormat", &container_opts);
2615                 generic_opts_set(ud->builder, "PictureDeinterlace", &deint_opts);
2616                 generic_opts_set(ud->builder, "PictureDetelecine", &detel_opts);
2617                 generic_opts_set(ud->builder, "PictureDecomb", &decomb_opts);
2618                 generic_opts_set(ud->builder, "PictureDenoise", &denoise_opts);
2619                 generic_opts_set(ud->builder, "VideoEncoder", &vcodec_opts);
2620                 small_opts_set(ud->builder, "AudioEncoder", &acodec_opts);
2621                 generic_opts_set(ud->builder, "x264_direct", &direct_opts);
2622                 generic_opts_set(ud->builder, "x264_b_adapt", &badapt_opts);
2623                 generic_opts_set(ud->builder, "x264_me", &me_opts);
2624                 generic_opts_set(ud->builder, "x264_subme", &subme_opts);
2625                 generic_opts_set(ud->builder, "x264_analyse", &analyse_opts);
2626                 generic_opts_set(ud->builder, "x264_trellis", &trellis_opts);
2627         }
2628         else
2629         {
2630                 if (strcmp(name, "AudioBitrate") == 0)
2631                         audio_bitrate_opts_set(ud->builder, "AudioBitrate");
2632                 else if (strcmp(name, "AudioSamplerate") == 0)
2633                         audio_samplerate_opts_set(ud->builder, "AudioSamplerate", hb_audio_rates, hb_audio_rates_count);
2634                 else if (strcmp(name, "VideoFramerate") == 0)
2635                         video_rate_opts_set(ud->builder, "VideoFramerate", hb_video_rates, hb_video_rates_count);
2636                 else if (strcmp(name, "AudioMixdown") == 0)
2637                         mix_opts_set(ud->builder, "AudioMixdown");
2638                 else if (strcmp(name, "SrtLanguage") == 0)
2639                         language_opts_set(ud->builder, "SrtLanguage");
2640                 else if (strcmp(name, "PreferredLanguage") == 0)
2641                         language_opts_set(ud->builder, "PreferredLanguage");
2642                 else if (strcmp(name, "SrtCodeset") == 0)
2643                         srt_codeset_opts_set(ud->builder, "SrtCodeset");
2644                 else if (strcmp(name, "title") == 0)
2645                         title_opts_set(ud->builder, "title");
2646                 else if (strcmp(name, "SubtitleTrack") == 0)
2647                         subtitle_track_opts_set(ud->builder, "SubtitleTrack", user_data);
2648                 else if (strcmp(name, "AudioTrack") == 0)
2649                         audio_track_opts_set(ud->builder, "AudioTrack", user_data);
2650                 else
2651                         generic_opts_set(ud->builder, name, find_combo_table(name));
2652         }
2653         if (handler_id > 0)
2654         {
2655                 g_signal_handler_unblock ((gpointer)combo, handler_id);
2656         }
2657 }
2658         
2659 static void
2660 init_ui_combo_boxes(GtkBuilder *builder)
2661 {
2662         gint ii;
2663
2664         init_combo_box(builder, "AudioBitrate");
2665         init_combo_box(builder, "AudioSamplerate");
2666         init_combo_box(builder, "VideoFramerate");
2667         init_combo_box(builder, "AudioMixdown");
2668         init_combo_box(builder, "SrtLanguage");
2669         init_combo_box(builder, "PreferredLanguage");
2670         init_combo_box(builder, "SrtCodeset");
2671         init_combo_box(builder, "title");
2672         init_combo_box(builder, "AudioTrack");
2673         for (ii = 0; combo_name_map[ii].name != NULL; ii++)
2674         {
2675                 init_combo_box(builder, combo_name_map[ii].name);
2676         }
2677 }
2678         
2679 static const char * turbo_opts = 
2680         "ref=1:subme=2:me=dia:analyse=none:trellis=0:"
2681         "no-fast-pskip=0:8x8dct=0";
2682
2683 // Construct the x264 options string
2684 // The result is allocated, so someone must free it at some point.
2685 gchar*
2686 ghb_build_x264opts_string(GValue *settings)
2687 {
2688         gchar *result;
2689         gchar *opts = ghb_settings_get_string(settings, "x264Option");
2690         if (opts != NULL)
2691         {
2692                 result = opts;
2693         }
2694         else
2695         {
2696                 result = g_strdup("");
2697         }
2698         return result;
2699 }
2700
2701 void
2702 ghb_part_duration(gint tt, gint sc, gint ec, gint *hh, gint *mm, gint *ss)
2703 {
2704         hb_list_t  * list;
2705         hb_title_t * title;
2706     hb_chapter_t * chapter;
2707         gint count, c;
2708         gint64 duration;
2709         
2710         *hh = *mm = *ss = 0;
2711         if (h_scan == NULL) return;
2712         list = hb_get_titles( h_scan );
2713     title = (hb_title_t*)hb_list_item( list, tt );
2714         if (title == NULL) return;
2715
2716         *hh = title->hours;
2717         *mm = title->minutes;
2718         *ss = title->seconds;
2719
2720         count = hb_list_count(title->list_chapter);
2721         if (sc > count) sc = count;
2722         if (ec > count) ec = count;
2723
2724         if (sc == 1 && ec == count)
2725                 return;
2726
2727         duration = 0;
2728         for (c = sc; c <= ec; c++)
2729         {
2730                 chapter = hb_list_item(title->list_chapter, c-1);
2731                 duration += chapter->duration;
2732         }
2733
2734         *hh =   duration / 90000 / 3600;
2735         *mm = ((duration / 90000) % 3600) / 60;
2736         *ss =  (duration / 90000) % 60;
2737 }
2738
2739 void
2740 ghb_get_chapter_duration(gint ti, gint ii, gint *hh, gint *mm, gint *ss)
2741 {
2742         hb_list_t  * list;
2743         hb_title_t * title;
2744     hb_chapter_t * chapter;
2745         gint count;
2746         
2747         g_debug("ghb_get_chapter_duration (title = %d)\n", ti);
2748         *hh = *mm = *ss = 0;
2749         if (h_scan == NULL) return;
2750         list = hb_get_titles( h_scan );
2751     title = (hb_title_t*)hb_list_item( list, ti );
2752         if (title == NULL) return;
2753         count = hb_list_count( title->list_chapter );
2754         if (ii >= count) return;
2755         chapter = hb_list_item(title->list_chapter, ii);
2756         if (chapter == NULL) return;
2757         *hh = chapter->hours;
2758         *mm = chapter->minutes;
2759         *ss = chapter->seconds;
2760 }
2761
2762 GValue*
2763 ghb_get_chapters(gint titleindex)
2764 {
2765         hb_list_t  * list;
2766         hb_title_t * title;
2767     hb_chapter_t * chapter;
2768         gint count, ii;
2769         GValue *chapters = NULL;
2770         
2771         g_debug("ghb_get_chapters (title = %d)\n", titleindex);
2772         if (h_scan == NULL) return NULL;
2773         list = hb_get_titles( h_scan );
2774     title = (hb_title_t*)hb_list_item( list, titleindex );
2775         if (title == NULL) return NULL;
2776         count = hb_list_count( title->list_chapter );
2777         chapters = ghb_array_value_new(count);
2778         for (ii = 0; ii < count; ii++)
2779         {
2780                 chapter = hb_list_item(title->list_chapter, ii);
2781                 if (chapter == NULL) break;
2782                 if (chapter->title == NULL || chapter->title[0] == 0)
2783                 {
2784                         gchar *str;
2785                         str = g_strdup_printf ("Chapter %2d", ii+1);
2786                         ghb_array_append(chapters, ghb_string_value_new(str));
2787                         g_free(str);
2788                 }
2789                 else 
2790                 {
2791                         ghb_array_append(chapters, ghb_string_value_new(chapter->title));
2792                 }
2793         }
2794         return chapters;
2795 }
2796
2797 gboolean
2798 ghb_ac3_in_audio_list(const GValue *audio_list)
2799 {
2800         gint count, ii;
2801
2802         count = ghb_array_len(audio_list);
2803         for (ii = 0; ii < count; ii++)
2804         {
2805                 GValue *asettings;
2806                 gint acodec;
2807
2808                 asettings = ghb_array_get_nth(audio_list, ii);
2809                 acodec = ghb_settings_combo_int(asettings, "AudioEncoder");
2810                 if (acodec & HB_ACODEC_AC3)
2811                         return TRUE;
2812         }
2813         return FALSE;
2814 }
2815
2816 static void
2817 audio_bitrate_opts_add(GtkBuilder *builder, const gchar *name, gint rate)
2818 {
2819         GtkTreeIter iter;
2820         GtkListStore *store;
2821         gchar *str;
2822         
2823         g_debug("audio_bitrate_opts_add ()\n");
2824
2825         if (rate < 8) return;
2826
2827         store = get_combo_box_store(builder, name);
2828         if (!find_combo_item_by_int(GTK_TREE_MODEL(store), rate, &iter))
2829         {
2830                 str = g_strdup_printf ("<small>%d</small>", rate);
2831                 gtk_list_store_append(store, &iter);
2832                 gtk_list_store_set(store, &iter, 
2833                                                    0, str, 
2834                                                    1, TRUE, 
2835                                                    2, str, 
2836                                                    3, (gdouble)rate, 
2837                                                    4, str, 
2838                                                    -1);
2839                 g_free(str);
2840         }
2841 }
2842
2843 static void
2844 audio_bitrate_opts_clean(
2845         GtkBuilder *builder, 
2846         const gchar *name, 
2847         gint first_rate, 
2848         gint last_rate)
2849 {
2850         GtkTreeIter iter;
2851         GtkListStore *store;
2852         gdouble ivalue;
2853         gboolean done = FALSE;
2854         gint ii = 0;
2855         guint last = (guint)last_rate;
2856         guint first = (guint)first_rate;
2857         
2858         g_debug("audio_bitrate_opts_clean ()\n");
2859         store = get_combo_box_store(builder, name);
2860         if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL(store), &iter))
2861         {
2862                 do
2863                 {
2864                         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, 3, &ivalue, -1);
2865                         if (search_rates(
2866                                 hb_audio_bitrates, ivalue, hb_audio_bitrates_count) < 0)
2867                         {
2868                                 done = !gtk_list_store_remove(store, &iter);
2869                         }
2870                         else if (ivalue < first || ivalue > last)
2871                         {
2872                                 ii++;
2873                                 gtk_list_store_set(store, &iter, 1, FALSE, -1);
2874                                 done = !gtk_tree_model_iter_next (GTK_TREE_MODEL(store), &iter);
2875                         }
2876                         else
2877                         {
2878                                 ii++;
2879                                 gtk_list_store_set(store, &iter, 1, TRUE, -1);
2880                                 done = !gtk_tree_model_iter_next (GTK_TREE_MODEL(store), &iter);
2881                         }
2882                 } while (!done);
2883         }
2884 }
2885
2886 static void
2887 audio_bitrate_opts_set(GtkBuilder *builder, const gchar *name)
2888 {
2889         GtkTreeIter iter;
2890         GtkListStore *store;
2891         gint ii;
2892         gchar *str;
2893         
2894         g_debug("audio_bitrate_opts_set ()\n");
2895         store = get_combo_box_store(builder, name);
2896         gtk_list_store_clear(store);
2897         for (ii = 0; ii < hb_audio_bitrates_count; ii++)
2898         {
2899                 gtk_list_store_append(store, &iter);
2900                 str = g_strdup_printf ("<small>%s</small>", 
2901                         hb_audio_bitrates[ii].string);
2902                 gtk_list_store_set(store, &iter, 
2903                                                    0, str,
2904                                                    1, TRUE, 
2905                                                    2, hb_audio_bitrates[ii].string, 
2906                                                    3, (gdouble)hb_audio_bitrates[ii].rate, 
2907                                                    4, hb_audio_bitrates[ii].string, 
2908                                                    -1);
2909                 g_free(str);
2910         }
2911 }
2912
2913 void
2914 ghb_set_passthru_bitrate_opts(GtkBuilder *builder, gint bitrate)
2915 {
2916         audio_bitrate_opts_add(builder, "AudioBitrate", bitrate);
2917 }
2918
2919 void
2920 ghb_set_default_bitrate_opts(
2921         GtkBuilder *builder, 
2922         gint first_rate, 
2923         gint last_rate)
2924 {
2925         audio_bitrate_opts_clean(builder, "AudioBitrate", first_rate, last_rate);
2926 }
2927
2928 static ghb_status_t hb_status;
2929
2930 void
2931 ghb_combo_init(signal_user_data_t *ud)
2932 {
2933         // Set up the list model for the combos
2934         init_ui_combo_boxes(ud->builder);
2935         // Populate all the combos
2936         ghb_update_ui_combo_box(ud, NULL, 0, TRUE);
2937 }
2938
2939 void
2940 ghb_backend_init(gint debug)
2941 {
2942     /* Init libhb */
2943     h_scan = hb_init( debug, 0 );
2944     h_queue = hb_init( debug, 0 );
2945 }
2946
2947 void
2948 ghb_backend_close()
2949 {
2950         hb_close(&h_queue);
2951         hb_close(&h_scan);
2952 }
2953
2954 void ghb_backend_scan_stop()
2955 {
2956     hb_scan_stop( h_scan );
2957 }
2958
2959 void
2960 ghb_backend_scan(const gchar *path, gint titleindex, gint preview_count)
2961 {
2962     hb_scan( h_scan, path, titleindex, preview_count, 1 );
2963         hb_status.scan.state |= GHB_STATE_SCANNING;
2964         // initialize count and cur to something that won't cause FPE
2965         // when computing progress
2966         hb_status.scan.title_count = 1;
2967         hb_status.scan.title_cur = 0;
2968 }
2969
2970 void
2971 ghb_backend_queue_scan(const gchar *path, gint titlenum)
2972 {
2973         g_debug("ghb_backend_queue_scan()");
2974         hb_scan( h_queue, path, titlenum, 10, 0 );
2975         hb_status.queue.state |= GHB_STATE_SCANNING;
2976 }
2977
2978 gint
2979 ghb_get_scan_state()
2980 {
2981         return hb_status.scan.state;
2982 }
2983
2984 gint
2985 ghb_get_queue_state()
2986 {
2987         return hb_status.queue.state;
2988 }
2989
2990 void
2991 ghb_clear_scan_state(gint state)
2992 {
2993         hb_status.scan.state &= ~state;
2994 }
2995
2996 void
2997 ghb_clear_queue_state(gint state)
2998 {
2999         hb_status.queue.state &= ~state;
3000 }
3001
3002 void
3003 ghb_set_scan_state(gint state)
3004 {
3005         hb_status.scan.state |= state;
3006 }
3007
3008 void
3009 ghb_set_queue_state(gint state)
3010 {
3011         hb_status.queue.state |= state;
3012 }
3013
3014 void
3015 ghb_get_status(ghb_status_t *status)
3016 {
3017         memcpy(status, &hb_status, sizeof(ghb_status_t));
3018 }
3019
3020 void 
3021 ghb_track_status()
3022 {
3023     hb_state_t s_scan;
3024     hb_state_t s_queue;
3025
3026         if (h_scan == NULL) return;
3027     hb_get_state( h_scan, &s_scan );
3028         switch( s_scan.state )
3029     {
3030 #define p s_scan.param.scanning
3031         case HB_STATE_SCANNING:
3032                 {
3033                         hb_status.scan.state |= GHB_STATE_SCANNING;
3034                         hb_status.scan.title_count = p.title_count;
3035                         hb_status.scan.title_cur = p.title_cur;
3036                 } break;
3037 #undef p
3038
3039         case HB_STATE_SCANDONE:
3040         {
3041                         hb_status.scan.state &= ~GHB_STATE_SCANNING;
3042                         hb_status.scan.state |= GHB_STATE_SCANDONE;
3043         } break;
3044
3045 #define p s_scan.param.working
3046         case HB_STATE_WORKING:
3047                         hb_status.scan.state |= GHB_STATE_WORKING;
3048                         hb_status.scan.state &= ~GHB_STATE_PAUSED;
3049                         hb_status.scan.job_cur = p.job_cur;
3050                         hb_status.scan.job_count = p.job_count;
3051                         hb_status.scan.progress = p.progress;
3052                         hb_status.scan.rate_cur = p.rate_cur;
3053                         hb_status.scan.rate_avg = p.rate_avg;
3054                         hb_status.scan.hours = p.hours;
3055                         hb_status.scan.minutes = p.minutes;
3056                         hb_status.scan.seconds = p.seconds;
3057                         hb_status.scan.unique_id = p.sequence_id & 0xFFFFFF;
3058             break;
3059 #undef p
3060
3061         case HB_STATE_PAUSED:
3062                         hb_status.scan.state |= GHB_STATE_PAUSED;
3063             break;
3064                                 
3065         case HB_STATE_MUXING:
3066         {
3067                         hb_status.scan.state |= GHB_STATE_MUXING;
3068         } break;
3069
3070 #define p s_scan.param.workdone
3071         case HB_STATE_WORKDONE:
3072                 {
3073             hb_job_t *job;
3074
3075                         hb_status.scan.state |= GHB_STATE_WORKDONE;
3076                         hb_status.scan.state &= ~GHB_STATE_MUXING;
3077                         hb_status.scan.state &= ~GHB_STATE_PAUSED;
3078                         hb_status.scan.state &= ~GHB_STATE_WORKING;
3079                         switch (p.error)
3080                         {
3081                         case HB_ERROR_NONE:
3082                                 hb_status.scan.error = GHB_ERROR_NONE;
3083                                 break;
3084                         case HB_ERROR_CANCELED:
3085                                 hb_status.scan.error = GHB_ERROR_CANCELED;
3086                                 break;
3087                         default:
3088                                 hb_status.scan.error = GHB_ERROR_FAIL;
3089                                 break;
3090                         }
3091                         // Delete all remaining jobs of this encode.
3092                         // An encode can be composed of multiple associated jobs.
3093                         // When a job is stopped, libhb removes it from the job list,
3094                         // but does not remove other jobs that may be associated with it.
3095                         // Associated jobs are taged in the sequence id.
3096             while ((job = hb_job(h_scan, 0)) != NULL) 
3097                 hb_rem( h_scan, job );
3098                 } break;
3099 #undef p
3100     }
3101     hb_get_state( h_queue, &s_queue );
3102         switch( s_queue.state )
3103     {
3104 #define p s_queue.param.scanning
3105         case HB_STATE_SCANNING:
3106                 {
3107                         hb_status.queue.state |= GHB_STATE_SCANNING;
3108                         hb_status.queue.title_count = p.title_count;
3109                         hb_status.queue.title_cur = p.title_cur;
3110                 } break;
3111 #undef p
3112
3113         case HB_STATE_SCANDONE:
3114         {
3115                         hb_status.queue.state &= ~GHB_STATE_SCANNING;
3116                         hb_status.queue.state |= GHB_STATE_SCANDONE;
3117         } break;
3118
3119 #define p s_queue.param.working
3120         case HB_STATE_WORKING:
3121                         hb_status.queue.state |= GHB_STATE_WORKING;
3122                         hb_status.queue.state &= ~GHB_STATE_PAUSED;
3123                         hb_status.queue.state &= ~GHB_STATE_SEARCHING;
3124                         hb_status.queue.job_cur = p.job_cur;
3125                         hb_status.queue.job_count = p.job_count;
3126                         hb_status.queue.progress = p.progress;
3127                         hb_status.queue.rate_cur = p.rate_cur;
3128                         hb_status.queue.rate_avg = p.rate_avg;
3129                         hb_status.queue.hours = p.hours;
3130                         hb_status.queue.minutes = p.minutes;
3131                         hb_status.queue.seconds = p.seconds;
3132                         hb_status.queue.unique_id = p.sequence_id & 0xFFFFFF;
3133             break;
3134
3135         case HB_STATE_SEARCHING:
3136                         hb_status.queue.state |= GHB_STATE_SEARCHING;
3137                         hb_status.queue.state &= ~GHB_STATE_WORKING;
3138                         hb_status.queue.state &= ~GHB_STATE_PAUSED;
3139                         hb_status.queue.job_cur = p.job_cur;
3140                         hb_status.queue.job_count = p.job_count;
3141                         hb_status.queue.progress = p.progress;
3142                         hb_status.queue.rate_cur = p.rate_cur;
3143                         hb_status.queue.rate_avg = p.rate_avg;
3144                         hb_status.queue.hours = p.hours;
3145                         hb_status.queue.minutes = p.minutes;
3146                         hb_status.queue.seconds = p.seconds;
3147                         hb_status.queue.unique_id = p.sequence_id & 0xFFFFFF;
3148             break;
3149 #undef p
3150
3151         case HB_STATE_PAUSED:
3152                         hb_status.queue.state |= GHB_STATE_PAUSED;
3153             break;
3154                                 
3155         case HB_STATE_MUXING:
3156         {
3157                         hb_status.queue.state |= GHB_STATE_MUXING;
3158         } break;
3159
3160 #define p s_queue.param.workdone
3161         case HB_STATE_WORKDONE:
3162                 {
3163             hb_job_t *job;
3164
3165                         hb_status.queue.state |= GHB_STATE_WORKDONE;
3166                         hb_status.queue.state &= ~GHB_STATE_MUXING;
3167                         hb_status.queue.state &= ~GHB_STATE_PAUSED;
3168                         hb_status.queue.state &= ~GHB_STATE_WORKING;
3169                         switch (p.error)
3170                         {
3171                         case HB_ERROR_NONE:
3172                                 hb_status.queue.error = GHB_ERROR_NONE;
3173                                 break;
3174                         case HB_ERROR_CANCELED:
3175                                 hb_status.queue.error = GHB_ERROR_CANCELED;
3176                                 break;
3177                         default:
3178                                 hb_status.queue.error = GHB_ERROR_FAIL;
3179                                 break;
3180                         }
3181                         // Delete all remaining jobs of this encode.
3182                         // An encode can be composed of multiple associated jobs.
3183                         // When a job is stopped, libhb removes it from the job list,
3184                         // but does not remove other jobs that may be associated with it.
3185                         // Associated jobs are taged in the sequence id.
3186             while ((job = hb_job(h_queue, 0)) != NULL) 
3187                 hb_rem( h_queue, job );
3188                 } break;
3189 #undef p
3190     }
3191 }
3192
3193 gboolean
3194 ghb_get_title_info(ghb_title_info_t *tinfo, gint titleindex)
3195 {
3196         hb_list_t  * list;
3197         hb_title_t * title;
3198         
3199     if (h_scan == NULL) return FALSE;
3200         list = hb_get_titles( h_scan );
3201         if( !hb_list_count( list ) )
3202         {
3203                 /* No valid title, stop right there */
3204                 return FALSE;
3205         }
3206
3207         title = hb_list_item( list, titleindex );
3208         if (title == NULL) return FALSE;        // Bad titleindex
3209         tinfo->index = titleindex;
3210         tinfo->width = title->width;
3211         tinfo->height = title->height;
3212         memcpy(tinfo->crop, title->crop, 4 * sizeof(int));
3213         // Don't allow crop to 0
3214         if (title->crop[0] + title->crop[1] >= title->height)
3215                 title->crop[0] = title->crop[1] = 0;
3216         if (title->crop[2] + title->crop[3] >= title->width)
3217                 title->crop[2] = title->crop[3] = 0;
3218         tinfo->num_chapters = hb_list_count(title->list_chapter);
3219         tinfo->rate_base = title->rate_base;
3220         tinfo->rate = title->rate;
3221         hb_reduce(&(tinfo->aspect_n), &(tinfo->aspect_d), 
3222                                 title->width * title->pixel_aspect_width, 
3223                                 title->height * title->pixel_aspect_height);
3224         tinfo->hours = title->hours;
3225         tinfo->minutes = title->minutes;
3226         tinfo->seconds = title->seconds;
3227         tinfo->duration = title->duration;
3228
3229         tinfo->angle_count = title->angle_count;
3230         tinfo->path = title->path;
3231         tinfo->name = title->name;
3232         tinfo->type = title->type;
3233         return TRUE;
3234 }
3235
3236 gboolean
3237 ghb_get_audio_info(ghb_audio_info_t *ainfo, gint titleindex, gint audioindex)
3238 {
3239     hb_audio_config_t *audio;
3240         
3241         audio = get_hb_audio(titleindex, audioindex);
3242         if (audio == NULL) return FALSE; // Bad audioindex
3243         ainfo->codec = audio->in.codec;
3244         ainfo->bitrate = audio->in.bitrate;
3245         ainfo->samplerate = audio->in.samplerate;
3246         return TRUE;
3247 }
3248
3249 gboolean
3250 ghb_audio_is_passthru(gint acodec)
3251 {
3252         g_debug("ghb_audio_is_passthru () \n");
3253         return (acodec & (HB_ACODEC_AC3 | HB_ACODEC_DCA));
3254 }
3255
3256 gint
3257 ghb_get_default_acodec()
3258 {
3259         return HB_ACODEC_FAAC;
3260 }
3261
3262 static void
3263 picture_settings_deps(signal_user_data_t *ud)
3264 {
3265         gboolean autoscale, keep_aspect, enable_keep_aspect;
3266         gboolean enable_scale_width, enable_scale_height;
3267         gboolean enable_disp_width, enable_disp_height, enable_par;
3268         gint pic_par;
3269         GtkWidget *widget;
3270
3271         pic_par = ghb_settings_combo_int(ud->settings, "PicturePAR");
3272         if (pic_par == 1)
3273         {
3274                 ghb_ui_update(ud, "autoscale", ghb_boolean_value(TRUE));
3275                 ghb_ui_update(ud, "PictureModulus", ghb_int_value(2));
3276                 ghb_ui_update(ud, "PictureLooseCrop", ghb_boolean_value(TRUE));
3277         }
3278         enable_keep_aspect = (pic_par != 1 && pic_par != 2);
3279         if (!enable_keep_aspect)
3280         {
3281                 ghb_ui_update(ud, "PictureKeepRatio", ghb_boolean_value(TRUE));
3282         }
3283         keep_aspect = ghb_settings_get_boolean(ud->settings, "PictureKeepRatio");
3284         autoscale = ghb_settings_get_boolean(ud->settings, "autoscale");
3285
3286         enable_scale_width = !autoscale && (pic_par != 1);
3287         enable_scale_height = !autoscale && (pic_par != 1);
3288         enable_disp_width = (pic_par == 3) && !keep_aspect;
3289         enable_par = (pic_par == 3) && !keep_aspect;
3290         enable_disp_height = FALSE;
3291
3292         widget = GHB_WIDGET(ud->builder, "PictureModulus");
3293         gtk_widget_set_sensitive(widget, pic_par != 1);
3294         widget = GHB_WIDGET(ud->builder, "PictureLooseCrop");
3295         gtk_widget_set_sensitive(widget, pic_par != 1);
3296         widget = GHB_WIDGET(ud->builder, "scale_width");
3297         gtk_widget_set_sensitive(widget, enable_scale_width);
3298         widget = GHB_WIDGET(ud->builder, "scale_height");
3299         gtk_widget_set_sensitive(widget, enable_scale_height);
3300         widget = GHB_WIDGET(ud->builder, "PictureDisplayWidth");
3301         gtk_widget_set_sensitive(widget, enable_disp_width);
3302         widget = GHB_WIDGET(ud->builder, "PictureDisplayHeight");
3303         gtk_widget_set_sensitive(widget, enable_disp_height);
3304         widget = GHB_WIDGET(ud->builder, "PicturePARWidth");
3305         gtk_widget_set_sensitive(widget, enable_par);
3306         widget = GHB_WIDGET(ud->builder, "PicturePARHeight");
3307         gtk_widget_set_sensitive(widget, enable_par);
3308         widget = GHB_WIDGET(ud->builder, "PictureKeepRatio");
3309         gtk_widget_set_sensitive(widget, enable_keep_aspect);
3310         widget = GHB_WIDGET(ud->builder, "autoscale");
3311         gtk_widget_set_sensitive(widget, pic_par != 1);
3312 }
3313
3314 void
3315 ghb_set_scale(signal_user_data_t *ud, gint mode)
3316 {
3317         hb_list_t  * list;
3318         hb_title_t * title;
3319         hb_job_t   * job;
3320         gboolean keep_aspect;
3321         gint pic_par;
3322         gboolean autocrop, autoscale, noscale;
3323         gint crop[4], width, height, par_width, par_height;
3324         gint crop_width, crop_height;
3325         gint aspect_n, aspect_d;
3326         gboolean keep_width = (mode & GHB_PIC_KEEP_WIDTH);
3327         gboolean keep_height = (mode & GHB_PIC_KEEP_HEIGHT);
3328         gint step;
3329         GtkWidget *widget;
3330         gint mod;
3331         gint max_width = 0;
3332         gint max_height = 0;
3333         static gboolean busy = FALSE;
3334         
3335         g_debug("ghb_set_scale ()\n");
3336         picture_settings_deps(ud);
3337         if (h_scan == NULL) return;
3338         list = hb_get_titles( h_scan );
3339         if( !hb_list_count( list ) )
3340         {
3341                 /* No valid title, stop right there */
3342                 return;
3343         }
3344         gint titleindex;
3345
3346         titleindex = ghb_settings_combo_int(ud->settings, "title");
3347     title = hb_list_item( list, titleindex );
3348         if (title == NULL) return;
3349         job   = title->job;
3350         if (job == NULL) return;
3351
3352         if (busy) return;
3353         busy = TRUE;
3354
3355         if (!ud->dont_clear_presets && (keep_width || keep_height))
3356         {
3357                 ghb_settings_set_int(ud->settings, "PictureWidth", 0);
3358                 ghb_settings_set_int(ud->settings, "PictureHeight", 0);
3359         }
3360
3361         // First configure widgets
3362         mod = ghb_settings_combo_int(ud->settings, "PictureModulus");
3363         pic_par = ghb_settings_combo_int(ud->settings, "PicturePAR");
3364         keep_aspect = ghb_settings_get_boolean(ud->settings, "PictureKeepRatio");
3365         autocrop = ghb_settings_get_boolean(ud->settings, "PictureAutoCrop");
3366         autoscale = ghb_settings_get_boolean(ud->settings, "autoscale");
3367         // "Noscale" is a flag that says we prefer to crop extra to satisfy
3368         // alignment constraints rather than scaling to satisfy them.
3369         noscale = ghb_settings_get_boolean(ud->settings, "PictureLooseCrop");
3370         // Align dimensions to either 16 or 2 pixels
3371         // The scaler crashes if the dimensions are not divisible by 2
3372         // x264 also will not accept dims that are not multiple of 2
3373         if (autoscale)
3374         {
3375                 keep_width = FALSE;
3376                 keep_height = FALSE;
3377         }
3378         // Step needs to be at least 2 because odd widths cause scaler crash
3379         step = mod;
3380         widget = GHB_WIDGET (ud->builder, "scale_width");
3381         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), step, 16);
3382         widget = GHB_WIDGET (ud->builder, "scale_height");
3383         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), step, 16);
3384         if (noscale)
3385         {
3386                 widget = GHB_WIDGET (ud->builder, "PictureTopCrop");
3387                 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), step, 16);
3388                 widget = GHB_WIDGET (ud->builder, "PictureBottomCrop");
3389                 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), step, 16);
3390                 widget = GHB_WIDGET (ud->builder, "PictureLeftCrop");
3391                 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), step, 16);
3392                 widget = GHB_WIDGET (ud->builder, "PictureRightCrop");
3393                 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), step, 16);
3394         }
3395         else
3396         {
3397                 widget = GHB_WIDGET (ud->builder, "PictureTopCrop");
3398                 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), 1, 16);
3399                 widget = GHB_WIDGET (ud->builder, "PictureBottomCrop");
3400                 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), 1, 16);
3401                 widget = GHB_WIDGET (ud->builder, "PictureLeftCrop");
3402                 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), 1, 16);
3403                 widget = GHB_WIDGET (ud->builder, "PictureRightCrop");
3404                 gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), 1, 16);
3405         }
3406         ghb_title_info_t tinfo;
3407         ghb_get_title_info (&tinfo, titleindex);
3408         if (autocrop)
3409         {
3410                 crop[0] = tinfo.crop[0];
3411                 crop[1] = tinfo.crop[1];
3412                 crop[2] = tinfo.crop[2];
3413                 crop[3] = tinfo.crop[3];
3414                 ghb_ui_update(ud, "PictureTopCrop", ghb_int64_value(crop[0]));
3415                 ghb_ui_update(ud, "PictureBottomCrop", ghb_int64_value(crop[1]));
3416                 ghb_ui_update(ud, "PictureLeftCrop", ghb_int64_value(crop[2]));
3417                 ghb_ui_update(ud, "PictureRightCrop", ghb_int64_value(crop[3]));
3418         }
3419         else
3420         {
3421                 crop[0] = ghb_settings_get_int(ud->settings, "PictureTopCrop");
3422                 crop[1] = ghb_settings_get_int(ud->settings, "PictureBottomCrop");
3423                 crop[2] = ghb_settings_get_int(ud->settings, "PictureLeftCrop");
3424                 crop[3] = ghb_settings_get_int(ud->settings, "PictureRightCrop");
3425         }
3426         if (noscale)
3427         {
3428                 gint need1, need2;
3429
3430                 // Adjust the cropping to accomplish the desired width and height
3431                 crop_width = tinfo.width - crop[2] - crop[3];
3432                 crop_height = tinfo.height - crop[0] - crop[1];
3433                 width = MOD_DOWN(crop_width, mod);
3434                 height = MOD_DOWN(crop_height, mod);
3435
3436                 need1 = (crop_height - height) / 2;
3437                 need2 = crop_height - height - need1;
3438                 crop[0] += need1;
3439                 crop[1] += need2;
3440                 need1 = (crop_width - width) / 2;
3441                 need2 = crop_width - width - need1;
3442                 crop[2] += need1;
3443                 crop[3] += need2;
3444                 ghb_ui_update(ud, "PictureTopCrop", ghb_int64_value(crop[0]));
3445                 ghb_ui_update(ud, "PictureBottomCrop", ghb_int64_value(crop[1]));
3446                 ghb_ui_update(ud, "PictureLeftCrop", ghb_int64_value(crop[2]));
3447                 ghb_ui_update(ud, "PictureRightCrop", ghb_int64_value(crop[3]));
3448         }
3449         hb_reduce(&aspect_n, &aspect_d, 
3450                                 title->width * title->pixel_aspect_width, 
3451                                 title->height * title->pixel_aspect_height);
3452         crop_width = title->width - crop[2] - crop[3];
3453         crop_height = title->height - crop[0] - crop[1];
3454         if (autoscale)
3455         {
3456                 width = crop_width;
3457                 height = crop_height;
3458         }
3459         else
3460         {
3461                 width = ghb_settings_get_int(ud->settings, "scale_width");
3462                 height = ghb_settings_get_int(ud->settings, "scale_height");
3463                 max_width = MOD_DOWN(
3464                         ghb_settings_get_int(ud->settings, "PictureWidth"), mod);
3465                 max_height = MOD_DOWN(
3466                         ghb_settings_get_int(ud->settings, "PictureHeight"), mod);
3467         }
3468         g_debug("max_width %d, max_height %d\n", max_width, max_height);
3469
3470         if (width < 16)
3471                 width = title->width - crop[2] - crop[3];
3472         if (height < 16)
3473                 height = title->height - crop[0] - crop[1];
3474
3475         width = MOD_ROUND(width, mod);
3476         height = MOD_ROUND(height, mod);
3477
3478         // Adjust dims according to max values
3479         if (max_height)
3480                 height = MIN(height, max_height);
3481         if (max_width)
3482                 width = MIN(width, max_width);
3483
3484         if (pic_par)
3485         {
3486                 job->anamorphic.mode = pic_par;
3487                 // The scaler crashes if the dimensions are not divisible by 2
3488                 // Align mod 2.  And so does something in x264_encoder_headers()
3489                 job->anamorphic.modulus = mod;
3490                 job->anamorphic.par_width = title->pixel_aspect_width;
3491                 job->anamorphic.par_height = title->pixel_aspect_height;
3492                 job->anamorphic.dar_width = 0;
3493                 job->anamorphic.dar_height = 0;
3494
3495                 if (keep_height && pic_par == 2)
3496                         width = ((double)height * crop_width / crop_height) + mod / 2;
3497                 job->width = width;
3498                 job->height = height;
3499                 job->maxWidth = max_width;
3500                 job->maxHeight = max_height;
3501                 job->crop[0] = crop[0]; job->crop[1] = crop[1];
3502                 job->crop[2] = crop[2]; job->crop[3] = crop[3];
3503                 if (job->anamorphic.mode == 3 && !keep_aspect)
3504                 {
3505                         job->anamorphic.keep_display_aspect = 0;
3506                         if (mode & GHB_PIC_KEEP_PAR)
3507                         {
3508                                 job->anamorphic.par_width = 
3509                                         ghb_settings_get_int(ud->settings, "PicturePARWidth");
3510                                 job->anamorphic.par_height = 
3511                                         ghb_settings_get_int(ud->settings, "PicturePARHeight");
3512                         }
3513                         else
3514                         {
3515                                 job->anamorphic.dar_width = 
3516                                         ghb_settings_get_int(ud->settings, 
3517                                                                                 "PictureDisplayWidth");
3518                                 job->anamorphic.dar_height = height;
3519                         }
3520                 }
3521                 else
3522                 {
3523                         job->anamorphic.keep_display_aspect = 1;
3524                 }
3525                 hb_set_anamorphic_size( job, &width, &height, 
3526                                                                 &par_width, &par_height );
3527                 if (job->anamorphic.mode == 3 && !keep_aspect && 
3528                         mode & GHB_PIC_KEEP_PAR)
3529                 {
3530                         // hb_set_anamorphic_size reduces the par, which we
3531                         // don't want in this case because the user is
3532                         // explicitely specifying it.
3533                         par_width = ghb_settings_get_int(ud->settings, 
3534                                                                                         "PicturePARWidth");
3535                         par_height = ghb_settings_get_int(ud->settings, 
3536                                                                                                 "PicturePARHeight");
3537                 }
3538         }
3539         else 
3540         {
3541                 job->anamorphic.mode = pic_par;
3542                 if (keep_aspect)
3543                 {
3544                         gdouble par;
3545                         gint new_width, new_height;
3546                         
3547                         // Compute pixel aspect ration.  
3548                         par = (gdouble)(title->height * aspect_n) / (title->width * aspect_d);
3549                         // Must scale so that par becomes 1:1
3550                         // Try to keep largest dimension
3551                         new_height = (crop_height * ((gdouble)width/crop_width) / par);
3552                         new_width = (crop_width * ((gdouble)height/crop_height) * par);
3553
3554                         if (max_width && new_width > max_width)
3555                         {
3556                                 height = new_height;
3557                         }
3558                         else if (max_height && new_height > max_height)
3559                         {
3560                                 width = new_width;
3561                         }
3562                         else if (keep_width)
3563                         {
3564                                 height = new_height;
3565                         }
3566                         else if (keep_height)
3567                         {
3568                                 width = new_width;
3569                         }
3570                         else if (width > new_width)
3571                         {
3572                                 height = new_height;
3573                         }
3574                         else
3575                         {
3576                                 width = new_width;
3577                         }
3578                         g_debug("new w %d h %d\n", width, height);
3579                 }
3580                 width = MOD_ROUND(width, mod);
3581                 height = MOD_ROUND(height, mod);
3582                 if (max_height)
3583                         height = MIN(height, max_height);
3584                 if (max_width)
3585                         width = MIN(width, max_width);
3586                 par_width = par_height = 1;
3587         }
3588         ghb_ui_update(ud, "scale_width", ghb_int64_value(width));
3589         ghb_ui_update(ud, "scale_height", ghb_int64_value(height));
3590
3591         gint disp_width, dar_width, dar_height;
3592         gchar *str;
3593
3594         disp_width = (gdouble)(width * par_width / par_height) + 0.5;
3595         hb_reduce(&dar_width, &dar_height, disp_width, height);
3596                 
3597         gint iaspect = dar_width * 9 / dar_height;
3598         if (dar_width > 2 * dar_height)
3599         {
3600                 str = g_strdup_printf("%.2f : 1", (gdouble)dar_width / dar_height);
3601         }
3602         else if (iaspect <= 16 && iaspect >= 15)
3603         {
3604                 str = g_strdup_printf("%.2f : 9", (gdouble)dar_width * 9 / dar_height);
3605         }
3606         else if (iaspect <= 12 && iaspect >= 11)
3607         {
3608                 str = g_strdup_printf("%.2f : 3", (gdouble)dar_width * 3 / dar_height);
3609         }
3610         else
3611         {
3612                 str = g_strdup_printf("%d : %d", dar_width, dar_height);
3613         }
3614         ghb_ui_update(ud, "display_aspect", ghb_string_value(str));
3615         g_free(str);
3616         ghb_ui_update(ud, "PicturePARWidth", ghb_int64_value(par_width));
3617         ghb_ui_update(ud, "PicturePARHeight", ghb_int64_value(par_height));
3618         ghb_ui_update(ud, "PictureDisplayWidth", ghb_int64_value(disp_width));
3619         ghb_ui_update(ud, "PictureDisplayHeight", ghb_int64_value(height));
3620         busy = FALSE;
3621 }
3622
3623 static void
3624 set_preview_job_settings(hb_job_t *job, GValue *settings)
3625 {
3626         job->crop[0] = ghb_settings_get_int(settings, "PictureTopCrop");
3627         job->crop[1] = ghb_settings_get_int(settings, "PictureBottomCrop");
3628         job->crop[2] = ghb_settings_get_int(settings, "PictureLeftCrop");
3629         job->crop[3] = ghb_settings_get_int(settings, "PictureRightCrop");
3630
3631         job->anamorphic.mode = ghb_settings_combo_int(settings, "PicturePAR");
3632         job->anamorphic.modulus = 
3633                 ghb_settings_combo_int(settings, "PictureModulus");
3634         job->width = ghb_settings_get_int(settings, "scale_width");
3635         job->height = ghb_settings_get_int(settings, "scale_height");
3636         if (ghb_settings_get_boolean(settings, "show_crop"))
3637         {
3638                 gdouble xscale = (gdouble)job->width / 
3639                         (gdouble)(job->title->width - job->crop[2] - job->crop[3]);
3640                 gdouble yscale = (gdouble)job->height / 
3641                         (gdouble)(job->title->height - job->crop[0] - job->crop[1]);
3642         
3643                 job->width += xscale * (job->crop[2] + job->crop[3]);
3644                 job->height += yscale * (job->crop[0] + job->crop[1]);
3645                 job->crop[0] = 0;
3646                 job->crop[1] = 0;
3647                 job->crop[2] = 0;
3648                 job->crop[3] = 0;
3649                 job->anamorphic.modulus = 2;
3650         }
3651
3652         gboolean decomb_deint = ghb_settings_get_boolean(settings, "PictureDecombDeinterlace");
3653         if (decomb_deint)
3654         {
3655                 gint decomb = ghb_settings_combo_int(settings, "PictureDecomb");
3656                 job->deinterlace = (decomb == 0) ? 0 : 1;
3657         }
3658         else
3659         {
3660                 gint deint = ghb_settings_combo_int(settings, "PictureDeinterlace");
3661                 job->deinterlace = (deint == 0) ? 0 : 1;
3662         }
3663
3664         gboolean keep_aspect;
3665         keep_aspect = ghb_settings_get_boolean(settings, "PictureKeepRatio");
3666         if (job->anamorphic.mode)
3667         {
3668                 job->anamorphic.par_width = job->title->pixel_aspect_width;
3669                 job->anamorphic.par_height = job->title->pixel_aspect_height;
3670                 job->anamorphic.dar_width = 0;
3671                 job->anamorphic.dar_height = 0;
3672
3673                 if (job->anamorphic.mode == 3 && !keep_aspect)
3674                 {
3675                         job->anamorphic.keep_display_aspect = 0;
3676                         job->anamorphic.par_width = 
3677                                 ghb_settings_get_int(settings, "PicturePARWidth");
3678                         job->anamorphic.par_height = 
3679                                 ghb_settings_get_int(settings, "PicturePARHeight");
3680                 }
3681                 else
3682                 {
3683                         job->anamorphic.keep_display_aspect = 1;
3684                 }
3685         }
3686 }
3687
3688 gint
3689 ghb_calculate_target_bitrate(GValue *settings, gint titleindex)
3690 {
3691         hb_list_t  * list;
3692         hb_title_t * title;
3693         hb_job_t   * job;
3694         gint size;
3695
3696         if (h_scan == NULL) return 1500;
3697         list = hb_get_titles( h_scan );
3698     title = hb_list_item( list, titleindex );
3699         if (title == NULL) return 1500;
3700         job   = title->job;
3701         if (job == NULL) return 1500;
3702         size = ghb_settings_get_int(settings, "VideoTargetSize");
3703         return hb_calc_bitrate( job, size );
3704 }
3705
3706 gboolean
3707 ghb_validate_filter_string(const gchar *str, gint max_fields)
3708 {
3709         gint fields = 0;
3710         gchar *end;
3711         gdouble val;
3712
3713         if (str == NULL || *str == 0) return TRUE;
3714         while (*str)
3715         {
3716                 val = g_strtod(str, &end);
3717                 if (str != end)
3718                 { // Found a numeric value
3719                         fields++;
3720                         // negative max_fields means infinate
3721                         if (max_fields >= 0 && fields > max_fields) return FALSE;
3722                         if (*end == 0)
3723                                 return TRUE;
3724                         if (*end != ':')
3725                                 return FALSE;
3726                         str = end + 1;
3727                 }
3728                 else
3729                         return FALSE;
3730         }
3731         return FALSE;
3732 }
3733
3734 gboolean
3735 ghb_validate_filters(signal_user_data_t *ud)
3736 {
3737         gchar *str;
3738         gint index;
3739         gchar *message;
3740
3741         gboolean decomb_deint = ghb_settings_get_boolean(ud->settings, "PictureDecombDeinterlace");
3742         // deinte
3743         index = ghb_settings_combo_int(ud->settings, "PictureDeinterlace");
3744         if (!decomb_deint && index == 1)
3745         {
3746                 str = ghb_settings_get_string(ud->settings, "PictureDeinterlaceCustom");
3747                 if (!ghb_validate_filter_string(str, -1))
3748                 {
3749                         message = g_strdup_printf(
3750                                                 "Invalid Deinterlace Settings:\n\n%s\n",
3751                                                 str);
3752                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
3753                         g_free(message);
3754                         g_free(str);
3755                         return FALSE;
3756                 }
3757                 g_free(str);
3758         }
3759         // detel
3760         index = ghb_settings_combo_int(ud->settings, "PictureDetelecine");
3761         if (index == 1)
3762         {
3763                 str = ghb_settings_get_string(ud->settings, "PictureDetelecineCustom");
3764                 if (!ghb_validate_filter_string(str, -1))
3765                 {
3766                         message = g_strdup_printf(
3767                                                 "Invalid Detelecine Settings:\n\n%s\n",
3768                                                 str);
3769                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
3770                         g_free(message);
3771                         g_free(str);
3772                         return FALSE;
3773                 }
3774                 g_free(str);
3775         }
3776         // decomb
3777         index = ghb_settings_combo_int(ud->settings, "PictureDecomb");
3778         if (decomb_deint && index == 1)
3779         {
3780                 str = ghb_settings_get_string(ud->settings, "PictureDecombCustom");
3781                 if (!ghb_validate_filter_string(str, -1))
3782                 {
3783                         message = g_strdup_printf(
3784                                                 "Invalid Decomb Settings:\n\n%s\n",
3785                                                 str);
3786                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
3787                         g_free(message);
3788                         g_free(str);
3789                         return FALSE;
3790                 }
3791                 g_free(str);
3792         }
3793         // denois
3794         index = ghb_settings_combo_int(ud->settings, "PictureDenoise");
3795         if (index == 1)
3796         {
3797                 str = ghb_settings_get_string(ud->settings, "PictureDenoiseCustom");
3798                 if (!ghb_validate_filter_string(str, -1))
3799                 {
3800                         message = g_strdup_printf(
3801                                                 "Invalid Denoise Settings:\n\n%s\n",
3802                                                 str);
3803                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
3804                         g_free(str);
3805                         g_free(message);
3806                         return FALSE;
3807                 }
3808                 g_free(str);
3809         }
3810         return TRUE;
3811 }
3812
3813 gboolean
3814 ghb_validate_video(signal_user_data_t *ud)
3815 {
3816         gint vcodec, mux;
3817         gchar *message;
3818
3819         mux = ghb_settings_combo_int(ud->settings, "FileFormat");
3820         vcodec = ghb_settings_combo_int(ud->settings, "VideoEncoder");
3821         if ((mux == HB_MUX_MP4) && (vcodec == HB_VCODEC_THEORA))
3822         {
3823                 // mp4/theora combination is not supported.
3824                 message = g_strdup_printf(
3825                                         "Theora is not supported in the MP4 container.\n\n"
3826                                         "You should choose a different video codec or container.\n"
3827                                         "If you continue, FFMPEG will be chosen for you.");
3828                 if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
3829                 {
3830                         g_free(message);
3831                         return FALSE;
3832                 }
3833                 g_free(message);
3834                 vcodec = HB_VCODEC_FFMPEG;
3835                 ghb_ui_update(ud, "VideoEncoder", ghb_int64_value(vcodec));
3836         }
3837         return TRUE;
3838 }
3839
3840 gboolean
3841 ghb_validate_subtitles(signal_user_data_t *ud)
3842 {
3843         hb_list_t  * list;
3844         hb_title_t * title;
3845         gchar *message;
3846
3847         if (h_scan == NULL) return FALSE;
3848         list = hb_get_titles( h_scan );
3849         if( !hb_list_count( list ) )
3850         {
3851                 /* No valid title, stop right there */
3852                 g_message("No title found.\n");
3853                 return FALSE;
3854         }
3855
3856         gint titleindex;
3857
3858         titleindex = ghb_settings_combo_int(ud->settings, "title");
3859     title = hb_list_item( list, titleindex );
3860         if (title == NULL) return FALSE;
3861         gint mux = ghb_settings_combo_int(ud->settings, "FileFormat");
3862
3863         const GValue *slist, *settings;
3864         gint count, ii, source;
3865         gboolean burned, one_burned = FALSE;
3866
3867         slist = ghb_settings_get_value(ud->settings, "subtitle_list");
3868         count = ghb_array_len(slist);
3869         for (ii = 0; ii < count; ii++)
3870         {
3871                 settings = ghb_array_get_nth(slist, ii);
3872                 source = ghb_settings_get_int(settings, "SubtitleSource");
3873                 burned = ghb_settings_get_boolean(settings, "SubtitleBurned");
3874                 if (burned && one_burned)
3875                 {
3876                         // MP4 can only handle burned vobsubs.  make sure there isn't
3877                         // already something burned in the list
3878                         message = g_strdup_printf(
3879                         "Only one subtitle may be burned into the video.\n\n"
3880                                 "You should change your subtitle selections.\n"
3881                                 "If you continue, some subtitles will be lost.");
3882                         if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
3883                         {
3884                                 g_free(message);
3885                                 return FALSE;
3886                         }
3887                         g_free(message);
3888                         break;
3889                 }
3890                 else if (burned)
3891                 {
3892                         one_burned = TRUE;
3893                 }
3894                 if (!burned && mux == HB_MUX_MP4 && source == VOBSUB)
3895                 {
3896                         // MP4 can only handle burned vobsubs.  make sure there isn't
3897                         // already something burned in the list
3898                         message = g_strdup_printf(
3899                         "Your chosen container does not support soft bitmap subtitles.\n\n"
3900                                 "You should change your subtitle selections.\n"
3901                                 "If you continue, some subtitles will be lost.");
3902                         if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, 
3903                                 "Cancel", "Continue"))
3904                         {
3905                                 g_free(message);
3906                                 return FALSE;
3907                         }
3908                         g_free(message);
3909                         break;
3910                 }
3911                 if (source == SRTSUB)
3912                 {
3913                         gchar *filename;
3914
3915                         filename = ghb_settings_get_string(settings, "SrtFile");
3916                         if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR))
3917                         {
3918                                 message = g_strdup_printf(
3919                                 "Srt file does not exist or not a regular file.\n\n"
3920                                         "You should choose a valid file.\n"
3921                                         "If you continue, this subtitle will be ignored.");
3922                                 if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, 
3923                                         "Cancel", "Continue"))
3924                                 {
3925                                         g_free(message);
3926                                         return FALSE;
3927                                 }
3928                                 g_free(message);
3929                                 break;
3930                         }
3931                 }
3932         }
3933         return TRUE;
3934 }
3935
3936 gint
3937 ghb_select_audio_codec(signal_user_data_t *ud, gint track)
3938 {
3939         hb_list_t  * list;
3940         hb_title_t * title;
3941         hb_audio_config_t *audio;
3942
3943         if (h_scan == NULL) return -1;
3944         list = hb_get_titles( h_scan );
3945         if( !hb_list_count( list ) )
3946         {
3947                 return -1;
3948         }
3949
3950         gint titleindex;
3951
3952         titleindex = ghb_settings_combo_int(ud->settings, "title");
3953     title = hb_list_item( list, titleindex );
3954         if (title == NULL) return -1;
3955
3956         gint mux = ghb_settings_combo_int(ud->settings, "FileFormat");
3957
3958         if (track < 0 || track >= hb_list_count(title->list_audio))
3959                 return -1;
3960
3961         audio = (hb_audio_config_t *) hb_list_audio_config_item(
3962                                                                         title->list_audio, track );
3963         if (mux == HB_MUX_MP4)
3964         {
3965                 if (audio->in.codec == HB_ACODEC_AC3)
3966                         return audio->in.codec;
3967                 else
3968                         return HB_ACODEC_FAAC;
3969         }
3970         else
3971         {
3972                 if (audio->in.codec == HB_ACODEC_AC3 || audio->in.codec == HB_ACODEC_DCA)
3973                         return audio->in.codec;
3974                 else
3975                         return HB_ACODEC_LAME;
3976         }
3977 }
3978
3979 const gchar*
3980 ghb_select_audio_codec_str(signal_user_data_t *ud, gint track)
3981 {
3982         gint acodec, ii;
3983
3984         acodec = ghb_select_audio_codec(ud, track);
3985         for (ii = 0; ii < acodec_opts.count; ii++)
3986         {
3987                 if (acodec_opts.map[ii].ivalue == acodec)
3988                         return acodec_opts.map[ii].option;
3989         }
3990         return "Unknown";
3991 }
3992
3993 gboolean
3994 ghb_validate_audio(signal_user_data_t *ud)
3995 {
3996         hb_list_t  * list;
3997         hb_title_t * title;
3998         gchar *message;
3999         GValue *value;
4000
4001         if (h_scan == NULL) return FALSE;
4002         list = hb_get_titles( h_scan );
4003         if( !hb_list_count( list ) )
4004         {
4005                 /* No valid title, stop right there */
4006                 g_message("No title found.\n");
4007                 return FALSE;
4008         }
4009
4010         gint titleindex;
4011
4012         titleindex = ghb_settings_combo_int(ud->settings, "title");
4013     title = hb_list_item( list, titleindex );
4014         if (title == NULL) return FALSE;
4015         gint mux = ghb_settings_combo_int(ud->settings, "FileFormat");
4016
4017         const GValue *audio_list;
4018         gint count, ii;
4019
4020         audio_list = ghb_settings_get_value(ud->settings, "audio_list");
4021         count = ghb_array_len(audio_list);
4022         for (ii = 0; ii < count; ii++)
4023         {
4024                 GValue *asettings;
4025             hb_audio_config_t *taudio;
4026
4027                 asettings = ghb_array_get_nth(audio_list, ii);
4028                 gint track = ghb_settings_combo_int(asettings, "AudioTrack");
4029                 gint codec = ghb_settings_combo_int(asettings, "AudioEncoder");
4030                 if (codec == HB_ACODEC_MASK)
4031                         continue;
4032
4033         taudio = (hb_audio_config_t *) hb_list_audio_config_item(
4034                                                                                         title->list_audio, track );
4035                 if (!(taudio->in.codec & codec) && 
4036                         (codec & (HB_ACODEC_AC3 | HB_ACODEC_DCA)))
4037                 {
4038                         // Not supported.  AC3 is passthrough only, so input must be AC3
4039                         message = g_strdup_printf(
4040                                                 "The source does not support Pass-Thru.\n\n"
4041                                                 "You should choose a different audio codec.\n"
4042                                                 "If you continue, one will be chosen for you.");
4043                         if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
4044                         {
4045                                 g_free(message);
4046                                 return FALSE;
4047                         }
4048                         g_free(message);
4049                         if (mux == HB_MUX_MKV)
4050                         {
4051                                 codec = HB_ACODEC_LAME;
4052                         }
4053                         else
4054                         {
4055                                 codec = HB_ACODEC_FAAC;
4056                         }
4057                         value = ghb_lookup_acodec_value(codec);
4058                         ghb_settings_take_value(asettings, "AudioEncoder", value);
4059                 }
4060                 gchar *a_unsup = NULL;
4061                 gchar *mux_s = NULL;
4062                 if (mux == HB_MUX_MP4)
4063                 { 
4064                         mux_s = "MP4";
4065                         // mp4/mp3|vorbis combination is not supported.
4066                         if (codec == HB_ACODEC_LAME)
4067                         {
4068                                 a_unsup = "MP3";
4069                                 codec = HB_ACODEC_FAAC;
4070                         }
4071                         if (codec == HB_ACODEC_VORBIS)
4072                         {
4073                                 a_unsup = "Vorbis";
4074                                 codec = HB_ACODEC_FAAC;
4075                         }
4076                         if (codec == HB_ACODEC_DCA)
4077                         {
4078                                 a_unsup = "DTS";
4079                                 codec = HB_ACODEC_FAAC;
4080                         }
4081                 }
4082                 if (a_unsup)
4083                 {
4084                         message = g_strdup_printf(
4085                                                 "%s is not supported in the %s container.\n\n"
4086                                                 "You should choose a different audio codec.\n"
4087                                                 "If you continue, one will be chosen for you.", a_unsup, mux_s);
4088                         if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
4089                         {
4090                                 g_free(message);
4091                                 return FALSE;
4092                         }
4093                         g_free(message);
4094                         value = ghb_lookup_acodec_value(codec);
4095                         ghb_settings_take_value(asettings, "AudioEncoder", value);
4096                 }
4097                 gint mix = ghb_settings_combo_int (asettings, "AudioMixdown");
4098                 gboolean allow_mono = TRUE;
4099                 gboolean allow_stereo = TRUE;
4100                 gboolean allow_dolby = TRUE;
4101                 gboolean allow_dpl2 = TRUE;
4102                 gboolean allow_6ch = TRUE;
4103                 allow_mono =
4104                         (taudio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
4105                         (codec & ~HB_ACODEC_LAME);
4106                 gint layout = taudio->in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
4107                 allow_stereo =
4108                         ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
4109                 allow_dolby =
4110                         (layout == HB_INPUT_CH_LAYOUT_3F1R) || 
4111                         (layout == HB_INPUT_CH_LAYOUT_3F2R) || 
4112                         (layout == HB_INPUT_CH_LAYOUT_DOLBY);
4113                 allow_dpl2 = (layout == HB_INPUT_CH_LAYOUT_3F2R);
4114                 allow_6ch =
4115                         (taudio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
4116                         (codec & ~HB_ACODEC_LAME) &&
4117                         (layout == HB_INPUT_CH_LAYOUT_3F2R) && 
4118                         (taudio->in.channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE);
4119
4120                 gchar *mix_unsup = NULL;
4121                 if (mix == HB_AMIXDOWN_MONO && !allow_mono)
4122                 {
4123                         mix_unsup = "mono";
4124                 }
4125                 if (mix == HB_AMIXDOWN_STEREO && !allow_stereo)
4126                 {
4127                         mix_unsup = "stereo";
4128                 }
4129                 if (mix == HB_AMIXDOWN_DOLBY && !allow_dolby)
4130                 {
4131                         mix_unsup = "Dolby";
4132                 }
4133                 if (mix == HB_AMIXDOWN_DOLBYPLII && !allow_dpl2)
4134                 {
4135                         mix_unsup = "Dolby Pro Logic II";
4136                 }
4137                 if (mix == HB_AMIXDOWN_6CH && !allow_6ch)
4138                 {
4139                         mix_unsup = "6 Channel";
4140                 }
4141                 if (mix_unsup)
4142                 {
4143                         message = g_strdup_printf(
4144                                                 "The source audio does not support %s mixdown.\n\n"
4145                                                 "You should choose a different mixdown.\n"
4146                                                 "If you continue, one will be chosen for you.", mix_unsup);
4147                         if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
4148                         {
4149                                 g_free(message);
4150                                 return FALSE;
4151                         }
4152                         g_free(message);
4153                         mix = ghb_get_best_mix(titleindex, track, codec, mix);
4154                         value = get_amix_value(mix);
4155                         ghb_settings_take_value(asettings, "AudioMixdown", value);
4156                 }
4157         }
4158         return TRUE;
4159 }
4160
4161 gboolean
4162 ghb_validate_vquality(GValue *settings)
4163 {
4164         gint vcodec;
4165         gchar *message;
4166         gint min, max;
4167
4168         if (ghb_settings_get_boolean(settings, "nocheckvquality")) return TRUE;
4169         vcodec = ghb_settings_combo_int(settings, "VideoEncoder");
4170         gdouble vquality;
4171         vquality = ghb_settings_get_double(settings, "VideoQualitySlider");
4172         if (ghb_settings_get_boolean(settings, "vquality_type_constant"))
4173         {
4174                 switch (vcodec)
4175                 {
4176                         case HB_VCODEC_X264:
4177                         {
4178                                 min = 16;
4179                                 max = 30;
4180                         } break;
4181
4182                         case HB_VCODEC_FFMPEG:
4183                         {
4184                                 min = 1;
4185                                 max = 8;
4186                         } break;
4187
4188                         case HB_VCODEC_THEORA:
4189                         {
4190                                 min = 0;
4191                                 max = 63;
4192                         } break;
4193
4194                         default:
4195                         {
4196                                 min = 48;
4197                                 max = 62;
4198                         } break;
4199                 }
4200                 if (vquality < min || vquality > max)
4201                 {
4202                         message = g_strdup_printf(
4203                                                 "Interesting video quality choise: %d\n\n"
4204                                                 "Typical values range from %d to %d.\n"
4205                                                 "Are you sure you wish to use this setting?",
4206                                                 (gint)vquality, min, max);
4207                         if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message, 
4208                                                                         "Cancel", "Continue"))
4209                         {
4210                                 g_free(message);
4211                                 return FALSE;
4212                         }
4213                         g_free(message);
4214                 }
4215         }
4216         return TRUE;
4217 }
4218
4219 static void
4220 add_job(hb_handle_t *h, GValue *js, gint unique_id, gint titleindex)
4221 {
4222         hb_list_t  * list;
4223         hb_title_t * title;
4224         hb_job_t   * job;
4225         static gchar *x264opts;
4226         gint sub_id = 0;
4227         gboolean tweaks = FALSE;
4228         gchar *detel_str = NULL;
4229         gchar *decomb_str = NULL;
4230         gchar *deint_str = NULL;
4231         gchar *deblock_str = NULL;
4232         gchar *denoise_str = NULL;
4233         gchar *dest_str = NULL;
4234
4235         g_debug("add_job()\n");
4236         if (h == NULL) return;
4237         list = hb_get_titles( h );
4238         if( !hb_list_count( list ) )
4239         {
4240                 /* No valid title, stop right there */
4241                 return;
4242         }
4243
4244     title = hb_list_item( list, titleindex );
4245         if (title == NULL) return;
4246
4247         /* Set job settings */
4248         job   = title->job;
4249         if (job == NULL) return;
4250
4251         job->angle = ghb_settings_get_int(js, "angle");
4252         job->start_at_preview = ghb_settings_get_int(js, "start_frame") + 1;
4253         if (job->start_at_preview)
4254         {
4255                 job->seek_points = ghb_settings_get_int(js, "preview_count");
4256                 job->pts_to_stop = ghb_settings_get_int(js, "live_duration") * 90000LL;
4257         }
4258
4259         tweaks = ghb_settings_get_boolean(js, "allow_tweaks");
4260         job->mux = ghb_settings_combo_int(js, "FileFormat");
4261         if (job->mux == HB_MUX_MP4)
4262         {
4263                 job->largeFileSize = ghb_settings_get_boolean(js, "Mp4LargeFile");
4264                 job->mp4_optimize = ghb_settings_get_boolean(js, "Mp4HttpOptimize");
4265         }
4266         else
4267         {
4268                 job->largeFileSize = FALSE;
4269                 job->mp4_optimize = FALSE;
4270         }
4271         if (!job->start_at_preview)
4272         {
4273                 gint start, end;
4274                 gint num_chapters = hb_list_count(title->list_chapter);
4275                 gint duration = title->duration / 90000;
4276                 job->chapter_start = 1;
4277                 job->chapter_end = num_chapters;
4278
4279                 if (ghb_settings_combo_int(js, "PtoPType") == 0)
4280                 {
4281                         start = ghb_settings_get_int(js, "start_point");
4282                         end = ghb_settings_get_int(js, "end_point");
4283                         job->chapter_start = MIN( num_chapters, start );
4284                         job->chapter_end   = MAX( job->chapter_start, end );
4285
4286                 }
4287                 if (ghb_settings_combo_int(js, "PtoPType") == 1)
4288                 {
4289                         job->chapter_start = 1;
4290                         job->chapter_end = num_chapters;
4291                         start = ghb_settings_get_int(js, "start_point");
4292                         end = ghb_settings_get_int(js, "end_point");
4293                         job->pts_to_start = (int64_t)MIN(duration-1, start) * 90000;
4294                         job->pts_to_stop = (int64_t)MAX(start+1, end) * 90000 - 
4295                                                                 job->pts_to_start;
4296                 }
4297                 if (ghb_settings_combo_int(js, "PtoPType") == 2)
4298                 {
4299                         job->chapter_start = 1;
4300                         job->chapter_end = num_chapters;
4301                         start = ghb_settings_get_int(js, "start_point");
4302                         end = ghb_settings_get_int(js, "end_point");
4303                         gint64 max_frames;
4304                         max_frames = (gint64)duration * title->rate / title->rate_base;
4305                         job->frame_to_start = (int64_t)MIN(max_frames-1, start-1);
4306                         job->frame_to_stop = (int64_t)MAX(start, end-1) - 
4307                                                                  job->frame_to_start;
4308                 }
4309                 job->chapter_markers = ghb_settings_get_boolean(js, "ChapterMarkers");
4310                 if (job->chapter_start == job->chapter_end)
4311                         job->chapter_markers = 0;
4312                 if ( job->chapter_markers )
4313                 {
4314                         GValue *chapters;
4315                         GValue *chapter;
4316                         gint chap;
4317                         gint count;
4318
4319                         chapters = ghb_settings_get_value(js, "chapter_list");
4320                         count = ghb_array_len(chapters);
4321                         for(chap = start; chap <= end; chap++)
4322                         {
4323                                 hb_chapter_t * chapter_s;
4324                                 gchar *name;
4325
4326                                 name = NULL;
4327                                 if (chap-1 < count)
4328                                 {
4329                                         chapter = ghb_array_get_nth(chapters, chap-1);
4330                                         name = ghb_value_string(chapter); 
4331                                 }
4332                                 if (name == NULL)
4333                                 {
4334                                         name = g_strdup_printf ("Chapter %2d", chap);
4335                                 }
4336                                 chapter_s = hb_list_item( job->title->list_chapter, chap - 1);
4337                                 strncpy(chapter_s->title, name, 1023);
4338                                 chapter_s->title[1023] = '\0';
4339                                 g_free(name);
4340                         }
4341                 }
4342         }
4343         job->crop[0] = ghb_settings_get_int(js, "PictureTopCrop");
4344         job->crop[1] = ghb_settings_get_int(js, "PictureBottomCrop");
4345         job->crop[2] = ghb_settings_get_int(js, "PictureLeftCrop");
4346         job->crop[3] = ghb_settings_get_int(js, "PictureRightCrop");
4347
4348         
4349         gboolean decomb_deint = ghb_settings_get_boolean(js, "PictureDecombDeinterlace");
4350         gint decomb = ghb_settings_combo_int(js, "PictureDecomb");
4351         gint deint = ghb_settings_combo_int(js, "PictureDeinterlace");
4352         if (!decomb_deint)
4353                 job->deinterlace = (deint != 0) ? 1 : 0;
4354         else
4355                 job->deinterlace = 0;
4356     job->grayscale   = ghb_settings_get_boolean(js, "VideoGrayScale");
4357
4358         gboolean keep_aspect;
4359         keep_aspect = ghb_settings_get_boolean(js, "PictureKeepRatio");
4360         job->anamorphic.mode = ghb_settings_combo_int(js, "PicturePAR");
4361         job->anamorphic.modulus = ghb_settings_combo_int(js, "PictureModulus");
4362         if (job->anamorphic.mode)
4363         {
4364                 job->anamorphic.par_width = title->pixel_aspect_width;
4365                 job->anamorphic.par_height = title->pixel_aspect_height;
4366                 job->anamorphic.dar_width = 0;
4367                 job->anamorphic.dar_height = 0;
4368
4369                 if (job->anamorphic.mode == 3 && !keep_aspect)
4370                 {
4371                         job->anamorphic.keep_display_aspect = 0;
4372                         job->anamorphic.par_width = 
4373                                 ghb_settings_get_int(js, "PicturePARWidth");
4374                         job->anamorphic.par_height = 
4375                                 ghb_settings_get_int(js, "PicturePARHeight");
4376                 }
4377                 else
4378                 {
4379                         job->anamorphic.keep_display_aspect = 1;
4380                 }
4381         }
4382
4383         /* Add selected filters */
4384         job->filters = hb_list_init();
4385         gint detel = ghb_settings_combo_int(js, "PictureDetelecine");
4386         if ( detel )
4387         {
4388                 if (detel != 1)
4389                 {
4390                         if (detel_opts.map[detel].svalue != NULL)
4391                                 detel_str = g_strdup(detel_opts.map[detel].svalue);
4392                 }
4393                 else
4394                         detel_str = ghb_settings_get_string(js, "PictureDetelecineCustom");
4395                 hb_filter_detelecine.settings = detel_str;
4396                 hb_list_add( job->filters, &hb_filter_detelecine );
4397         }
4398         if ( decomb_deint && decomb )
4399         {
4400                 if (decomb != 1)
4401                 {
4402                         if (decomb_opts.map[decomb].svalue != NULL)
4403                                 decomb_str = g_strdup(decomb_opts.map[decomb].svalue);
4404                 }
4405                 else
4406                         decomb_str = ghb_settings_get_string(js, "PictureDecombCustom");
4407                 hb_filter_decomb.settings = decomb_str;
4408                 hb_list_add( job->filters, &hb_filter_decomb );
4409         }
4410         if( job->deinterlace )
4411         {
4412                 if (deint != 1)
4413                 {
4414                         if (deint_opts.map[deint].svalue != NULL)
4415                                 deint_str = g_strdup(deint_opts.map[deint].svalue);
4416                 }
4417                 else
4418                         deint_str = ghb_settings_get_string(js, "PictureDeinterlaceCustom");
4419                 hb_filter_deinterlace.settings = deint_str;
4420                 hb_list_add( job->filters, &hb_filter_deinterlace );
4421         }
4422         gint deblock = ghb_settings_get_int(js, "PictureDeblock");
4423         if( deblock >= 5 )
4424         {
4425                 deblock_str = g_strdup_printf("%d", deblock);
4426                 hb_filter_deblock.settings = deblock_str;
4427                 hb_list_add( job->filters, &hb_filter_deblock );
4428         }
4429         gint denoise = ghb_settings_combo_int(js, "PictureDenoise");
4430         if( denoise )
4431         {
4432                 if (denoise != 1)
4433                 {
4434                         if (denoise_opts.map[denoise].svalue != NULL)
4435                                 denoise_str = g_strdup(denoise_opts.map[denoise].svalue);
4436                 }
4437                 else
4438                         denoise_str = ghb_settings_get_string(js, "PictureDenoiseCustom");
4439                 hb_filter_denoise.settings = denoise_str;
4440                 hb_list_add( job->filters, &hb_filter_denoise );
4441         }
4442         job->width = ghb_settings_get_int(js, "scale_width");
4443         job->height = ghb_settings_get_int(js, "scale_height");
4444
4445         job->vcodec = ghb_settings_combo_int(js, "VideoEncoder");
4446         if ((job->mux == HB_MUX_MP4 ) && (job->vcodec == HB_VCODEC_THEORA))
4447         {
4448                 // mp4/theora combination is not supported.
4449                 job->vcodec = HB_VCODEC_FFMPEG;
4450         }
4451         if ((job->vcodec == HB_VCODEC_X264) && (job->mux == HB_MUX_MP4))
4452         {
4453                 job->ipod_atom = ghb_settings_get_boolean(js, "Mp4iPodCompatible");
4454         }
4455         if (ghb_settings_get_boolean(js, "vquality_type_constant"))
4456         {
4457                 gdouble vquality;
4458                 vquality = ghb_settings_get_double(js, "VideoQualitySlider");
4459                 job->vquality =  vquality;
4460                 job->vbitrate = 0;
4461         }
4462         else if (ghb_settings_get_boolean(js, "vquality_type_bitrate"))
4463         {
4464                 job->vquality = -1.0;
4465                 job->vbitrate = ghb_settings_get_int(js, "VideoAvgBitrate");
4466         }
4467
4468         gint vrate = ghb_settings_combo_int(js, "VideoFramerate");
4469         if( vrate == 0 )
4470         {
4471                 job->vrate = title->rate;
4472                 job->vrate_base = title->rate_base;
4473                 job->cfr = 0;
4474         }
4475         else
4476         {
4477                 job->vrate = 27000000;
4478                 job->vrate_base = vrate;
4479                 job->cfr = 1;
4480         }
4481
4482         const GValue *audio_list;
4483         gint count, ii;
4484         gint tcount = 0;
4485         
4486         audio_list = ghb_settings_get_value(js, "audio_list");
4487         count = ghb_array_len(audio_list);
4488         for (ii = 0; ii < count; ii++)
4489         {
4490                 GValue *asettings;
4491             hb_audio_config_t audio;
4492             hb_audio_config_t *taudio;
4493
4494                 hb_audio_config_init(&audio);
4495                 asettings = ghb_array_get_nth(audio_list, ii);
4496                 audio.in.track = ghb_settings_get_int(asettings, "AudioTrack");
4497                 audio.out.track = tcount;
4498                 audio.out.codec = ghb_settings_combo_int(asettings, "AudioEncoder");
4499         taudio = (hb_audio_config_t *) hb_list_audio_config_item(
4500                                                                         title->list_audio, audio.in.track );
4501                 if (audio.out.codec & (HB_ACODEC_AC3 | HB_ACODEC_DCA))
4502                 {
4503                         if (!(taudio->in.codec & (HB_ACODEC_AC3 | HB_ACODEC_DCA)))
4504                         {
4505                                 // Not supported.  
4506                                 // AC3/DTS is passthrough only, so input must be AC3/DTS
4507                                 if (job->mux == HB_MUX_MKV)
4508                                 {
4509                                         audio.out.codec = HB_ACODEC_LAME;
4510                                 }
4511                                 else
4512                                 {
4513                                         audio.out.codec = HB_ACODEC_FAAC;
4514                                 }
4515                         }
4516                         else
4517                         {
4518                                 audio.out.codec &= taudio->in.codec;
4519                         }
4520                 }
4521                 if ((job->mux == HB_MUX_MP4) && 
4522                         ((audio.out.codec & HB_ACODEC_LAME) ||
4523                         (audio.out.codec & HB_ACODEC_DCA) ||
4524                         (audio.out.codec & HB_ACODEC_VORBIS)))
4525                 {
4526                         // mp4/mp3|dts|vorbis combination is not supported.
4527                         audio.out.codec = HB_ACODEC_FAAC;
4528                 }
4529         audio.out.dynamic_range_compression = 
4530                         ghb_settings_get_double(asettings, "AudioTrackDRCSlider");
4531         if (audio.out.dynamic_range_compression < 1.0)
4532                 audio.out.dynamic_range_compression = 0.0;
4533
4534                 // It would be better if this were done in libhb for us, but its not yet.
4535                 if (audio.out.codec & (HB_ACODEC_AC3 | HB_ACODEC_DCA))
4536                 {
4537                         audio.out.mixdown = 0;
4538                 }
4539                 else
4540                 {
4541                         int channels;
4542
4543                         audio.out.mixdown = ghb_settings_combo_int(asettings, "AudioMixdown");
4544                         if (audio.out.mixdown == HB_AMIXDOWN_MONO)
4545                                 channels = 1;
4546                         else if (audio.out.mixdown == HB_AMIXDOWN_6CH)
4547                                 channels = 6;
4548                         else
4549                                 channels = 2;
4550
4551                         // Make sure the mixdown is valid and pick a new one if not.
4552                         audio.out.mixdown = ghb_get_best_mix(titleindex, 
4553                                 audio.in.track, audio.out.codec, audio.out.mixdown);
4554                         audio.out.bitrate = 
4555                                 ghb_settings_combo_int(asettings, "AudioBitrate");
4556                         gint srate = ghb_settings_combo_int(asettings, "AudioSamplerate");
4557                         if (srate == 0) // 0 is same as source
4558                                 audio.out.samplerate = taudio->in.samplerate;
4559                         else
4560                                 audio.out.samplerate = srate;
4561
4562                         audio.out.bitrate = ghb_get_best_audio_bitrate(
4563                                 audio.out.codec, audio.out.bitrate, channels);
4564                 }
4565
4566                 // Add it to the jobs audio list
4567         hb_audio_add( job, &audio );
4568                 tcount++;
4569         }
4570         // I was tempted to move this up with the reset of the video quality
4571         // settings, but I suspect the settings above need to be made
4572         // first in order for hb_calc_bitrate to be accurate.
4573         if (ghb_settings_get_boolean(js, "vquality_type_target"))
4574         {
4575                 gint size;
4576                 
4577                 size = ghb_settings_get_int(js, "VideoTargetSize");
4578         job->vbitrate = hb_calc_bitrate( job, size );
4579                 job->vquality = -1.0;
4580         }
4581
4582         dest_str = ghb_settings_get_string(js, "destination");
4583         job->file = dest_str;
4584
4585         const GValue *subtitle_list;
4586         gint subtitle;
4587         gboolean force, burned, def, one_burned = FALSE;
4588         
4589         ghb_settings_set_boolean(js, "subtitle_scan", FALSE);
4590         subtitle_list = ghb_settings_get_value(js, "subtitle_list");
4591         count = ghb_array_len(subtitle_list);
4592         for (ii = 0; ii < count; ii++)
4593         {
4594                 GValue *ssettings;
4595                 gint source;
4596
4597                 ssettings = ghb_array_get_nth(subtitle_list, ii);
4598
4599                 force = ghb_settings_get_boolean(ssettings, "SubtitleForced");
4600                 burned = ghb_settings_get_boolean(ssettings, "SubtitleBurned");
4601                 def = ghb_settings_get_boolean(ssettings, "SubtitleDefaultTrack");
4602                 source = ghb_settings_get_int(ssettings, "SubtitleSource");
4603
4604                 if (source == SRTSUB)
4605                 {
4606                 hb_subtitle_config_t sub_config;
4607                         gchar *filename, *lang, *code;
4608
4609                         filename = ghb_settings_get_string(ssettings, "SrtFile");
4610                         if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR))
4611                         {
4612                                 continue;
4613                         }
4614                         sub_config.offset = ghb_settings_get_int(ssettings, "SrtOffset");
4615                         lang = ghb_settings_get_string(ssettings, "SrtLanguage");
4616                         code = ghb_settings_get_string(ssettings, "SrtCodeset");
4617                         strncpy(sub_config.src_filename, filename, 128);
4618                         strncpy(sub_config.src_codeset, code, 40);
4619                         sub_config.force = 0;
4620                         sub_config.dest = PASSTHRUSUB;
4621                         sub_config.default_track = def;
4622
4623                         hb_srt_add( job, &sub_config, lang);
4624
4625                         g_free(filename);
4626                         g_free(lang);
4627                         g_free(code);
4628                         continue;
4629                 }
4630
4631                 subtitle = ghb_settings_get_int(ssettings, "SubtitleTrack");
4632                 if (subtitle == -1)
4633                 {
4634                         if (!burned && job->mux == HB_MUX_MKV)
4635                         {
4636                                 job->select_subtitle_config.dest = PASSTHRUSUB;
4637                         }
4638                         else if (!burned && job->mux == HB_MUX_MP4)
4639                         {
4640                                 // Skip any non-burned vobsubs when output is mp4
4641                                 continue;
4642                         }
4643                         else if (burned)
4644                         {
4645                                 // Only allow one subtitle to be burned into the video
4646                                 if (one_burned)
4647                                         continue;
4648                                 job->select_subtitle_config.dest = RENDERSUB;
4649                                 one_burned = TRUE;
4650                         }
4651                         job->select_subtitle_config.force = force;
4652                         job->select_subtitle_config.default_track = def;
4653                         job->indepth_scan = 1;
4654                         ghb_settings_set_boolean(js, "subtitle_scan", TRUE);
4655                 }
4656                 else if (subtitle >= 0)
4657                 {
4658                 hb_subtitle_t * subt;
4659                 hb_subtitle_config_t sub_config;
4660
4661                 subt = (hb_subtitle_t *)hb_list_item(title->list_subtitle, subtitle);
4662                         sub_config = subt->config;
4663                         if (subt != NULL)
4664                         {
4665                                 if (!burned && job->mux == HB_MUX_MKV && 
4666                                         subt->format == PICTURESUB)
4667                                 {
4668                                         sub_config.dest = PASSTHRUSUB;
4669                                 }
4670                                 else if (!burned && job->mux == HB_MUX_MP4 && 
4671                                         subt->format == PICTURESUB)
4672                                 {
4673                                         // Skip any non-burned vobsubs when output is mp4
4674                                         continue;
4675                                 }
4676                                 else if ( burned && subt->format == PICTURESUB )
4677                                 {
4678                                         // Only allow one subtitle to be burned into the video
4679                                         if (one_burned)
4680                                                 continue;
4681                                         one_burned = TRUE;
4682                                 }
4683                                 sub_config.force = force;
4684                                 sub_config.default_track = def;
4685                         hb_subtitle_add( job, &sub_config, subtitle );
4686                         }
4687                 }
4688         }
4689
4690         // TODO: libhb holds onto a reference to the x264opts and is not
4691         // finished with it until encoding the job is done.  But I can't
4692         // find a way to get at the job before it is removed in order to
4693         // free up the memory I am allocating here.
4694         // The short story is THIS LEAKS.
4695         x264opts = ghb_build_x264opts_string(js);
4696         
4697         if( *x264opts == '\0' )
4698         {
4699                 g_free(x264opts);
4700                 x264opts = NULL;
4701         }
4702
4703         if (job->indepth_scan == 1)
4704         {
4705                 // Subtitle scan. Look for subtitle matching audio language
4706
4707                 /*
4708                  * When subtitle scan is enabled do a fast pre-scan job
4709                  * which will determine which subtitles to enable, if any.
4710                  */
4711                 job->pass = -1;
4712                 job->indepth_scan = 1;
4713                 job->x264opts = NULL;
4714
4715                 /*
4716                  * Add the pre-scan job
4717                  */
4718                 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
4719                 hb_add( h, job );
4720         }
4721
4722         if( ghb_settings_get_boolean(js, "VideoTwoPass") &&
4723                 !ghb_settings_get_boolean(js, "vquality_type_constant"))
4724         {
4725                 /*
4726                  * If subtitle_scan is enabled then only turn it on
4727                  * for the second pass and then off again for the
4728                  * second.
4729                  */
4730                 job->pass = 1;
4731                 job->indepth_scan = 0;
4732
4733                 /*
4734                  * If turbo options have been selected then append them
4735                  * to the x264opts now (size includes one ':' and the '\0')
4736                  */
4737                 if( ghb_settings_get_boolean(js, "VideoTurboTwoPass") )
4738                 {
4739                         gchar *tmp_x264opts;
4740                         gchar *extra_opts;
4741                         gint badapt;
4742
4743                         badapt = ghb_lookup_badapt(x264opts);
4744                         if (badapt == 2)
4745                         {
4746                                 extra_opts = g_strdup_printf("%s", turbo_opts);
4747                         }
4748                         else
4749                         {
4750                                 extra_opts = g_strdup_printf("%s:weightb=0", turbo_opts);
4751                         }
4752         
4753                         if ( x264opts )
4754                         {
4755                                 tmp_x264opts = g_strdup_printf("%s:%s", x264opts, extra_opts);
4756                         } 
4757                         else 
4758                         {
4759                                 /*
4760                                  * No x264opts to modify, but apply the turbo options
4761                                  * anyway as they may be modifying defaults
4762                                  */
4763                                 tmp_x264opts = g_strdup_printf("%s", extra_opts);
4764                         }
4765                         g_free(extra_opts);
4766
4767                         job->x264opts = tmp_x264opts;
4768                 }
4769                 else
4770                 {
4771                         job->x264opts = x264opts;
4772                 }
4773                 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
4774                 hb_add( h, job );
4775                 //if (job->x264opts != NULL)
4776                 //      g_free(job->x264opts);
4777
4778                 job->pass = 2;
4779                 /*
4780                  * On the second pass we turn off subtitle scan so that we
4781                  * can actually encode using any subtitles that were auto
4782                  * selected in the first pass (using the whacky select-subtitle
4783                  * attribute of the job).
4784                  */
4785                 job->indepth_scan = 0;
4786                 job->x264opts = x264opts;
4787                 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
4788                 hb_add( h, job );
4789                 //if (job->x264opts != NULL)
4790                 //      g_free(job->x264opts);
4791         }
4792         else
4793         {
4794                 job->x264opts = x264opts;
4795                 job->indepth_scan = 0;
4796                 job->pass = 0;
4797                 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
4798                 hb_add( h, job );
4799                 //if (job->x264opts != NULL)
4800                 //      g_free(job->x264opts);
4801         }
4802
4803         // clean up audio list
4804         gint num_audio_tracks = hb_list_count(job->list_audio);
4805         for(ii = 0; ii < num_audio_tracks; ii++)
4806         {
4807                 hb_audio_t *audio = (hb_audio_t*)hb_list_item(job->list_audio, 0);
4808                 hb_list_rem(job->list_audio, audio);
4809                 free(audio);
4810         }
4811
4812         // clean up subtitle list
4813         gint num_subtitle_tracks = hb_list_count(job->list_subtitle);
4814         for(ii = 0; ii < num_subtitle_tracks; ii++)
4815         {
4816                 hb_subtitle_t *subtitle = hb_list_item(job->list_subtitle, 0);
4817                 hb_list_rem(job->list_subtitle, subtitle);
4818                 free(subtitle);
4819         }
4820
4821         if (detel_str) g_free(detel_str);
4822         if (decomb_str) g_free(decomb_str);
4823         if (deint_str) g_free(deint_str);
4824         if (deblock_str) g_free(deblock_str);
4825         if (denoise_str) g_free(denoise_str);
4826         if (dest_str) g_free(dest_str);
4827 }
4828
4829 void
4830 ghb_add_job(GValue *js, gint unique_id)
4831 {
4832         // Since I'm doing a scan of the single title I want just prior 
4833         // to adding the job, there is only the one title to choose from.
4834         add_job(h_queue, js, unique_id, 0);
4835 }
4836
4837 void
4838 ghb_add_live_job(GValue *js, gint unique_id)
4839 {
4840         // Since I'm doing a scan of the single title I want just prior 
4841         // to adding the job, there is only the one title to choose from.
4842         gint titleindex = ghb_settings_combo_int(js, "title");
4843         add_job(h_scan, js, unique_id, titleindex);
4844 }
4845
4846 void
4847 ghb_remove_job(gint unique_id)
4848 {
4849     hb_job_t * job;
4850     gint ii;
4851         
4852         // Multiples passes all get the same id
4853         // remove them all.
4854         // Go backwards through list, so reordering doesn't screw me.
4855         ii = hb_count(h_queue) - 1;
4856     while ((job = hb_job(h_queue, ii--)) != NULL)
4857     {
4858         if ((job->sequence_id & 0xFFFFFF) == unique_id)
4859                         hb_rem(h_queue, job);
4860     }
4861 }
4862
4863 void
4864 ghb_start_queue()
4865 {
4866         hb_start( h_queue );
4867 }
4868
4869 void
4870 ghb_stop_queue()
4871 {
4872         hb_stop( h_queue );
4873 }
4874
4875 void
4876 ghb_start_live_encode()
4877 {
4878         hb_start( h_scan );
4879 }
4880
4881 void
4882 ghb_stop_live_encode()
4883 {
4884         hb_stop( h_scan );
4885 }
4886
4887 void
4888 ghb_pause_queue()
4889 {
4890     hb_state_t s;
4891     hb_get_state2( h_queue, &s );
4892
4893     if( s.state == HB_STATE_PAUSED )
4894     {
4895                 hb_status.queue.state &= ~GHB_STATE_PAUSED;
4896                 hb_resume( h_queue );
4897     }
4898     else
4899     {
4900                 hb_status.queue.state |= GHB_STATE_PAUSED;
4901                 hb_pause( h_queue );
4902     }
4903 }
4904
4905 static void
4906 vert_line(
4907         GdkPixbuf * pb, 
4908         guint8 r, 
4909         guint8 g, 
4910         guint8 b, 
4911         gint x, 
4912         gint y, 
4913         gint len, 
4914         gint width)
4915 {
4916         guint8 *pixels = gdk_pixbuf_get_pixels (pb);
4917         guint8 *dst;
4918         gint ii, jj;
4919         gint channels = gdk_pixbuf_get_n_channels (pb);
4920         gint stride = gdk_pixbuf_get_rowstride (pb);
4921
4922         for (jj = 0; jj < width; jj++)
4923         {
4924                 dst = pixels + y * stride + (x+jj) * channels;
4925                 for (ii = 0; ii < len; ii++)
4926                 {
4927                         dst[0] = r;
4928                         dst[1] = g;
4929                         dst[2] = b;
4930                         dst += stride;
4931                 }
4932         }
4933 }
4934
4935 static void
4936 horz_line(
4937         GdkPixbuf * pb, 
4938         guint8 r, 
4939         guint8 g, 
4940         guint8 b, 
4941         gint x, 
4942         gint y, 
4943         gint len,
4944         gint width)
4945 {
4946         guint8 *pixels = gdk_pixbuf_get_pixels (pb);
4947         guint8 *dst;
4948         gint ii, jj;
4949         gint channels = gdk_pixbuf_get_n_channels (pb);
4950         gint stride = gdk_pixbuf_get_rowstride (pb);
4951
4952         for (jj = 0; jj < width; jj++)
4953         {
4954                 dst = pixels + (y+jj) * stride + x * channels;
4955                 for (ii = 0; ii < len; ii++)
4956                 {
4957                         dst[0] = r;
4958                         dst[1] = g;
4959                         dst[2] = b;
4960                         dst += channels;
4961                 }
4962         }
4963 }
4964
4965 static void
4966 hash_pixbuf(
4967         GdkPixbuf * pb,
4968         gint        x,
4969         gint        y,
4970         gint        w,
4971         gint        h,
4972         gint        step,
4973         gint            orientation)
4974 {
4975         gint ii, jj;
4976         gint line_width = 8;
4977         struct
4978         {
4979                 guint8 r;
4980                 guint8 g;
4981                 guint8 b;
4982         } c[4] = 
4983         {{0x80, 0x80, 0x80},{0xC0, 0x80, 0x70},{0x80, 0xA0, 0x80},{0x70, 0x80, 0xA0}};
4984
4985         if (!orientation)
4986         {
4987                 // vertical lines
4988                 for (ii = x, jj = 0; ii+line_width < x+w; ii += step, jj++)
4989                 {
4990                         vert_line(pb, c[jj&3].r, c[jj&3].g, c[jj&3].b, ii, y, h, line_width);
4991                 }
4992         }
4993         else
4994         {
4995                 // horizontal lines
4996                 for (ii = y, jj = 0; ii+line_width < y+h; ii += step, jj++)
4997                 {
4998                         horz_line(pb, c[jj&3].r, c[jj&3].g, c[jj&3].b, x, ii, w, line_width);
4999                 }
5000         }
5001 }
5002
5003 GdkPixbuf*
5004 ghb_get_preview_image(
5005         gint titleindex, 
5006         gint index, 
5007         signal_user_data_t *ud,
5008         gint *out_width,
5009         gint *out_height)
5010 {
5011         GValue *settings;
5012         hb_title_t *title;
5013         hb_list_t  *list;
5014         
5015         settings = ud->settings;
5016         list = hb_get_titles( h_scan );
5017         if( !hb_list_count( list ) )
5018         {
5019                 /* No valid title, stop right there */
5020                 return NULL;
5021         }
5022     title = hb_list_item( list, titleindex );
5023         if (title == NULL) return NULL;
5024         if (title->job == NULL) return NULL;
5025         set_preview_job_settings(title->job, settings);
5026
5027         // hb_get_preview doesn't compensate for anamorphic, so lets
5028         // calculate scale factors
5029         gint width, height, par_width = 1, par_height = 1;
5030         gint pic_par = ghb_settings_combo_int(settings, "PicturePAR");
5031         if (pic_par)
5032         {
5033                 hb_set_anamorphic_size( title->job, &width, &height, 
5034                                                                 &par_width, &par_height );
5035         }
5036
5037         // Make sure we have a big enough buffer to receive the image from libhb
5038         gint dstWidth = title->job->width;
5039         gint dstHeight= title->job->height;
5040
5041         static guint8 *buffer = NULL;
5042         static gint bufferSize = 0;
5043         gint newSize;
5044
5045         newSize = dstWidth * dstHeight * 4;
5046         if( bufferSize < newSize )
5047         {
5048                 bufferSize = newSize;
5049                 buffer     = (guint8*) g_realloc( buffer, bufferSize );
5050         }
5051         hb_get_preview( h_scan, title, index, buffer );
5052
5053         // Create an GdkPixbuf and copy the libhb image into it, converting it from
5054         // libhb's format something suitable.
5055         
5056         // The image data returned by hb_get_preview is 4 bytes per pixel, 
5057         // BGRA format. Alpha is ignored.
5058
5059         GdkPixbuf *preview = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, dstWidth, dstHeight);
5060         guint8 *pixels = gdk_pixbuf_get_pixels (preview);
5061         
5062         guint32 *src = (guint32*)buffer;
5063         guint8 *dst = pixels;
5064
5065         gint ii, jj;
5066         gint channels = gdk_pixbuf_get_n_channels (preview);
5067         gint stride = gdk_pixbuf_get_rowstride (preview);
5068         guint8 *tmp;
5069
5070         for (ii = 0; ii < dstHeight; ii++)
5071         {
5072                 tmp = dst;
5073                 for (jj = 0; jj < dstWidth; jj++)
5074                 {
5075                         tmp[0] = src[0] >> 16;
5076                         tmp[1] = src[0] >> 8;
5077                         tmp[2] = src[0] >> 0;
5078                         tmp += channels;
5079                         src++;
5080                 }
5081                 dst += stride;
5082         }
5083         gint w = ghb_settings_get_int(settings, "scale_width");
5084         gint h = ghb_settings_get_int(settings, "scale_height");
5085         ghb_par_scale(ud, &w, &h, par_width, par_height);
5086
5087         gint c0, c1, c2, c3;
5088         c0 = ghb_settings_get_int(settings, "PictureTopCrop");
5089         c1 = ghb_settings_get_int(settings, "PictureBottomCrop");
5090         c2 = ghb_settings_get_int(settings, "PictureLeftCrop");
5091         c3 = ghb_settings_get_int(settings, "PictureRightCrop");
5092
5093         gdouble xscale = (gdouble)w / (gdouble)(title->width - c2 - c3);
5094         gdouble yscale = (gdouble)h / (gdouble)(title->height - c0 - c1);
5095         
5096         ghb_par_scale(ud, &dstWidth, &dstHeight, par_width, par_height);
5097         *out_width = w;
5098         *out_height = h;
5099         if (ghb_settings_get_boolean(settings, "reduce_hd_preview"))
5100         {
5101                 GdkScreen *ss;
5102                 gint s_w, s_h;
5103                 gint orig_w, orig_h;
5104                 gint factor = 80;
5105
5106                 if (ghb_settings_get_boolean(settings, "preview_fullscreen"))
5107                 {
5108                         factor = 100;
5109                 }
5110                 ss = gdk_screen_get_default();
5111                 s_w = gdk_screen_get_width(ss);
5112                 s_h = gdk_screen_get_height(ss);
5113                 orig_w = dstWidth;
5114                 orig_h = dstHeight;
5115
5116                 if (dstWidth > s_w * factor / 100)
5117                 {
5118                         dstWidth = s_w * factor / 100;
5119                         dstHeight = dstHeight * dstWidth / orig_w;
5120                 }
5121                 if (dstHeight > s_h * factor / 100)
5122                 {
5123                         dstHeight = s_h * factor / 100;
5124                         dstWidth = dstWidth * dstHeight / orig_h;
5125                 }
5126                 xscale *= (gdouble)dstWidth / orig_w;
5127                 yscale *= (gdouble)dstHeight / orig_h;
5128                 w *= (gdouble)dstWidth / orig_w;
5129                 h *= (gdouble)dstHeight / orig_h;
5130         }
5131         GdkPixbuf *scaled_preview;
5132         scaled_preview = gdk_pixbuf_scale_simple(preview, dstWidth, dstHeight, GDK_INTERP_HYPER);
5133         if (ghb_settings_get_boolean(settings, "show_crop"))
5134         {
5135                 c0 *= yscale;
5136                 c1 *= yscale;
5137                 c2 *= xscale;
5138                 c3 *= xscale;
5139                 // Top
5140                 hash_pixbuf(scaled_preview, c2, 0, w, c0, 32, 0);
5141                 // Bottom
5142                 hash_pixbuf(scaled_preview, c2, dstHeight-c1, w, c1, 32, 0);
5143                 // Left
5144                 hash_pixbuf(scaled_preview, 0, c0, c2, h, 32, 1);
5145                 // Right
5146                 hash_pixbuf(scaled_preview, dstWidth-c3, c0, c3, h, 32, 1);
5147         }
5148         g_object_unref (preview);
5149         return scaled_preview;
5150 }
5151
5152 static void
5153 sanitize_volname(gchar *name)
5154 {
5155         gchar *a, *b;
5156
5157         a = b = name;
5158         while (*b)
5159         {
5160                 switch(*b)
5161                 {
5162                 case '<':
5163                         b++;
5164                         break;
5165                 case '>':
5166                         b++;
5167                         break;
5168                 default:
5169                         *a = *b;
5170                         a++; b++;
5171                         break;
5172                 }
5173         }
5174         *a = 0;
5175 }
5176
5177 gchar*
5178 ghb_dvd_volname(const gchar *device)
5179 {
5180         gchar *name;
5181         name = hb_dvd_name((gchar*)device);
5182         if (name != NULL && name[0] != 0)
5183         {
5184                 name = g_strdup(name);
5185                 sanitize_volname(name);
5186                 return name;
5187         }
5188         return NULL;
5189 }