OSDN Git Service

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