OSDN Git Service

LinGui: Clean up temp files between scans instead of all at once when exiting.
[handbrake-jp/handbrake-jp-git.git] / gtk / src / hb-backend.c
1 /***************************************************************************
2  *            hb-backend.c
3  *
4  *  Fri Mar 28 10:38:44 2008
5  *  Copyright  2008  John Stebbins
6  *  <john at stebbins dot name>
7  ****************************************************************************/
8
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Library General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
23  */
24 #define _GNU_SOURCE
25 #include <limits.h>
26 #include <math.h>
27 #include "hb.h"
28 #include "hbversion.h"
29 #include <gtk/gtk.h>
30 #include <glib/gstdio.h>
31 #include "hb-backend.h"
32 #include "settings.h"
33 #include "callbacks.h"
34
35 typedef struct
36 {
37         const gchar *option;
38         const gchar *shortOpt;
39         gint ivalue;
40         gdouble dvalue;
41         const gchar *svalue;
42         gboolean sensitive;
43 } options_map_t;
44
45 typedef struct
46 {
47         gint count;
48         options_map_t *map;
49         gint *grey_options;
50 } combo_opts_t;
51
52 static options_map_t d_container_opts[] =
53 {
54         {"MKV", "mkv", HB_MUX_MKV, 0.0, "mkv"},
55         {"MP4", "mp4", HB_MUX_MP4, 0.0, "mp4"},
56         {"M4V", "m4v", HB_MUX_MP4, 0.0, "m4v"},
57         {"AVI", "avi", HB_MUX_AVI, 0.0, "avi"},
58         {"OGM", "ogm", HB_MUX_OGM, 0.0, "ogm"},
59 };
60 combo_opts_t container_opts =
61 {
62         sizeof(d_container_opts)/sizeof(options_map_t),
63         d_container_opts
64 };
65
66 static options_map_t d_deint_opts[] =
67 {
68         {"None",   "none",   0, 0.0, ""},
69         {"Fast",   "fast",   1, 0.0, "-1:-1:-1:0:1"},
70         {"Slow",   "slow",   2, 0.0, "2:-1:-1:0:1"},
71         {"Slower", "slower", 3, 0.0, "0:-1:-1:0:1"},
72 };
73 combo_opts_t deint_opts =
74 {
75         sizeof(d_deint_opts)/sizeof(options_map_t),
76         d_deint_opts
77 };
78
79 static options_map_t d_denoise_opts[] =
80 {
81         {"None",   "none",   0, 0.0, ""},
82         {"Weak",   "weak",   1, 0.0, "2:1:2:3"},
83         {"Medium", "medium", 2, 0.0, "3:2:2:3"},
84         {"Strong", "strong", 3, 0.0, "7:7:5:5"},
85 };
86 combo_opts_t denoise_opts =
87 {
88         sizeof(d_denoise_opts)/sizeof(options_map_t),
89         d_denoise_opts
90 };
91
92 static options_map_t d_vcodec_opts[] =
93 {
94         {"H.264 (x264)",    "x264",   HB_VCODEC_X264, 0.0, ""},
95         {"MPEG-4 (XviD)",   "xvid",   HB_VCODEC_XVID, 0.0, ""},
96         {"MPEG-4 (FFMPEG)", "ffmpeg", HB_VCODEC_FFMPEG, 0.0, ""},
97         {"Theora",          "theora", HB_VCODEC_THEORA, 0.0, ""},
98 };
99 combo_opts_t vcodec_opts =
100 {
101         sizeof(d_vcodec_opts)/sizeof(options_map_t),
102         d_vcodec_opts
103 };
104
105 static options_map_t d_acodec_opts[] =
106 {
107         {"AAC (faac)",      "faac",   HB_ACODEC_FAAC, 0.0, ""},
108         {"MP3 (lame)",      "lame",   HB_ACODEC_LAME, 0.0, ""},
109         {"Vorbis",          "vorbis", HB_ACODEC_VORBIS, 0.0, ""},
110         {"AC3 (pass-thru)", "ac3",    HB_ACODEC_AC3, 0.0, ""},
111 };
112 combo_opts_t acodec_opts =
113 {
114         sizeof(d_acodec_opts)/sizeof(options_map_t),
115         d_acodec_opts
116 };
117
118 static options_map_t d_pref_acodec_opts[] =
119 {
120         {"None",            "none",   0, 0.0, ""},
121         {"AAC (faac)",      "faac",   HB_ACODEC_FAAC, 0.0, ""},
122         {"MP3 (lame)",      "lame",   HB_ACODEC_LAME, 0.0, ""},
123         {"Vorbis",          "vorbis", HB_ACODEC_VORBIS, 0.0, ""},
124         {"AC3 (pass-thru)", "ac3",    HB_ACODEC_AC3, 0.0, ""},
125 };
126 combo_opts_t pref_acodec_opts =
127 {
128         sizeof(d_pref_acodec_opts)/sizeof(options_map_t),
129         d_pref_acodec_opts
130 };
131
132 static options_map_t d_source_acodec_opts[] =
133 {
134         {"AC3",      "ac3",   HB_ACODEC_AC3, 0.0, ""},
135         {"DTS",      "dca",   HB_ACODEC_DCA, 0.0, ""},
136         {"MP2",      "mpga",   HB_ACODEC_MPGA, 0.0, ""},
137         {"LPCM",      "lpcm",   HB_ACODEC_LPCM, 0.0, ""},
138 };
139 combo_opts_t source_acodec_opts =
140 {
141         sizeof(d_source_acodec_opts)/sizeof(options_map_t),
142         d_source_acodec_opts
143 };
144
145 static options_map_t d_direct_opts[] =
146 {
147         {"None",      "none",     0, 0.0, "none"},
148         {"Spatial",   "spatial",  1, 0.0, "spatial"},
149         {"Temporal",  "temporal", 2, 0.0, "temporal"},
150         {"Automatic", "auto",     3, 0.0, "auto"},
151 };
152 combo_opts_t direct_opts =
153 {
154         sizeof(d_direct_opts)/sizeof(options_map_t),
155         d_direct_opts
156 };
157
158 static options_map_t d_me_opts[] =
159 {
160         {"Diamond",              "dia", 0, 0.0, "dia"},
161         {"Hexagon",              "hex", 1, 0.0, "hex"},
162         {"Uneven Multi-Hexagon", "umh", 2, 0.0, "umh"},
163         {"Exhaustive",           "esa", 3, 0.0, "esa"},
164 };
165 combo_opts_t me_opts =
166 {
167         sizeof(d_me_opts)/sizeof(options_map_t),
168         d_me_opts
169 };
170
171 static options_map_t d_subme_opts[] =
172 {
173         {"1", "1", 1, 0.0, "1"},
174         {"2", "2", 2, 0.0, "2"},
175         {"3", "3", 3, 0.0, "3"},
176         {"4", "4", 4, 0.0, "4"},
177         {"5", "5", 5, 0.0, "5"},
178         {"6", "6", 6, 0.0, "6"},
179         {"7", "7", 7, 0.0, "7"},
180 };
181 combo_opts_t subme_opts =
182 {
183         sizeof(d_subme_opts)/sizeof(options_map_t),
184         d_subme_opts
185 };
186
187 static options_map_t d_analyse_opts[] =
188 {
189         {"Some", "some", 0, 0.0, "some"},
190         {"None", "none", 1, 0.0, "none"},
191         {"All",  "all",  2, 0.0, "all:8x8dct=1"},
192 };
193 combo_opts_t analyse_opts =
194 {
195         sizeof(d_analyse_opts)/sizeof(options_map_t),
196         d_analyse_opts
197 };
198
199 static options_map_t d_trellis_opts[] =
200 {
201         {"Disabled",          "off",    0, 0.0, ""},
202         {"Final Macro Block", "fmb",    1, 0.0, ""},
203         {"Always",            "always", 2, 0.0, ""},
204 };
205 combo_opts_t trellis_opts =
206 {
207         sizeof(d_trellis_opts)/sizeof(options_map_t),
208         d_trellis_opts
209 };
210
211 typedef struct iso639_lang_t
212 {
213     char * eng_name;        /* Description in English */
214     char * native_name;     /* Description in native language */
215     char * iso639_1;       /* ISO-639-1 (2 characters) code */
216     char * iso639_2;        /* ISO-639-2/t (3 character) code */
217     char * iso639_2b;       /* ISO-639-2/b code (if different from above) */
218 } iso639_lang_t;
219
220 static const iso639_lang_t language_table[] =
221
222         { "Any", "", "zz", "und" },
223         { "Afar", "", "aa", "aar" },
224         { "Abkhazian", "", "ab", "abk" },
225         { "Afrikaans", "", "af", "afr" },
226         { "Akan", "", "ak", "aka" },
227         { "Albanian", "", "sq", "sqi", "alb" },
228         { "Amharic", "", "am", "amh" },
229         { "Arabic", "", "ar", "ara" },
230         { "Aragonese", "", "an", "arg" },
231         { "Armenian", "", "hy", "hye", "arm" },
232         { "Assamese", "", "as", "asm" },
233         { "Avaric", "", "av", "ava" },
234         { "Avestan", "", "ae", "ave" },
235         { "Aymara", "", "ay", "aym" },
236         { "Azerbaijani", "", "az", "aze" },
237         { "Bashkir", "", "ba", "bak" },
238         { "Bambara", "", "bm", "bam" },
239         { "Basque", "", "eu", "eus", "baq" },
240         { "Belarusian", "", "be", "bel" },
241         { "Bengali", "", "bn", "ben" },
242         { "Bihari", "", "bh", "bih" },
243         { "Bislama", "", "bi", "bis" },
244         { "Bosnian", "", "bs", "bos" },
245         { "Breton", "", "br", "bre" },
246         { "Bulgarian", "", "bg", "bul" },
247         { "Burmese", "", "my", "mya", "bur" },
248         { "Catalan", "", "ca", "cat" },
249         { "Chamorro", "", "ch", "cha" },
250         { "Chechen", "", "ce", "che" },
251         { "Chinese", "", "zh", "zho", "chi" },
252         { "Church Slavic", "", "cu", "chu" },
253         { "Chuvash", "", "cv", "chv" },
254         { "Cornish", "", "kw", "cor" },
255         { "Corsican", "", "co", "cos" },
256         { "Cree", "", "cr", "cre" },
257         { "Czech", "", "cs", "ces", "cze" },
258         { "Danish", "Dansk", "da", "dan" },
259         { "Divehi", "", "dv", "div" },
260         { "Dutch", "Nederlands", "nl", "nld", "dut" },
261         { "Dzongkha", "", "dz", "dzo" },
262         { "English", "English", "en", "eng" },
263         { "Esperanto", "", "eo", "epo" },
264         { "Estonian", "", "et", "est" },
265         { "Ewe", "", "ee", "ewe" },
266         { "Faroese", "", "fo", "fao" },
267         { "Fijian", "", "fj", "fij" },
268         { "Finnish", "Suomi", "fi", "fin" },
269         { "French", "Francais", "fr", "fra", "fre" },
270         { "Western Frisian", "", "fy", "fry" },
271         { "Fulah", "", "ff", "ful" },
272         { "Georgian", "", "ka", "kat", "geo" },
273         { "German", "Deutsch", "de", "deu", "ger" },
274         { "Gaelic (Scots)", "", "gd", "gla" },
275         { "Irish", "", "ga", "gle" },
276         { "Galician", "", "gl", "glg" },
277         { "Manx", "", "gv", "glv" },
278         { "Greek, Modern", "", "el", "ell", "gre" },
279         { "Guarani", "", "gn", "grn" },
280         { "Gujarati", "", "gu", "guj" },
281         { "Haitian", "", "ht", "hat" },
282         { "Hausa", "", "ha", "hau" },
283         { "Hebrew", "", "he", "heb" },
284         { "Herero", "", "hz", "her" },
285         { "Hindi", "", "hi", "hin" },
286         { "Hiri Motu", "", "ho", "hmo" },
287         { "Hungarian", "Magyar", "hu", "hun" },
288         { "Igbo", "", "ig", "ibo" },
289         { "Icelandic", "Islenska", "is", "isl", "ice" },
290         { "Ido", "", "io", "ido" },
291         { "Sichuan Yi", "", "ii", "iii" },
292         { "Inuktitut", "", "iu", "iku" },
293         { "Interlingue", "", "ie", "ile" },
294         { "Interlingua", "", "ia", "ina" },
295         { "Indonesian", "", "id", "ind" },
296         { "Inupiaq", "", "ik", "ipk" },
297         { "Italian", "Italiano", "it", "ita" },
298         { "Javanese", "", "jv", "jav" },
299         { "Japanese", "", "ja", "jpn" },
300         { "Kalaallisut", "", "kl", "kal" },
301         { "Kannada", "", "kn", "kan" },
302         { "Kashmiri", "", "ks", "kas" },
303         { "Kanuri", "", "kr", "kau" },
304         { "Kazakh", "", "kk", "kaz" },
305         { "Central Khmer", "", "km", "khm" },
306         { "Kikuyu", "", "ki", "kik" },
307         { "Kinyarwanda", "", "rw", "kin" },
308         { "Kirghiz", "", "ky", "kir" },
309         { "Komi", "", "kv", "kom" },
310         { "Kongo", "", "kg", "kon" },
311         { "Korean", "", "ko", "kor" },
312         { "Kuanyama", "", "kj", "kua" },
313         { "Kurdish", "", "ku", "kur" },
314         { "Lao", "", "lo", "lao" },
315         { "Latin", "", "la", "lat" },
316         { "Latvian", "", "lv", "lav" },
317         { "Limburgan", "", "li", "lim" },
318         { "Lingala", "", "ln", "lin" },
319         { "Lithuanian", "", "lt", "lit" },
320         { "Luxembourgish", "", "lb", "ltz" },
321         { "Luba-Katanga", "", "lu", "lub" },
322         { "Ganda", "", "lg", "lug" },
323         { "Macedonian", "", "mk", "mkd", "mac" },
324         { "Marshallese", "", "mh", "mah" },
325         { "Malayalam", "", "ml", "mal" },
326         { "Maori", "", "mi", "mri", "mao" },
327         { "Marathi", "", "mr", "mar" },
328         { "Malay", "", "ms", "msa", "msa" },
329         { "Malagasy", "", "mg", "mlg" },
330         { "Maltese", "", "mt", "mlt" },
331         { "Moldavian", "", "mo", "mol" },
332         { "Mongolian", "", "mn", "mon" },
333         { "Nauru", "", "na", "nau" },
334         { "Navajo", "", "nv", "nav" },
335         { "Ndebele, South", "", "nr", "nbl" },
336         { "Ndebele, North", "", "nd", "nde" },
337         { "Ndonga", "", "ng", "ndo" },
338         { "Nepali", "", "ne", "nep" },
339         { "Norwegian Nynorsk", "", "nn", "nno" },
340         { "Norwegian Bokmål", "", "nb", "nob" },
341         { "Norwegian", "Norsk", "no", "nor" },
342         { "Chichewa; Nyanja", "", "ny", "nya" },
343         { "Occitan", "", "oc", "oci" },
344         { "Ojibwa", "", "oj", "oji" },
345         { "Oriya", "", "or", "ori" },
346         { "Oromo", "", "om", "orm" },
347         { "Ossetian", "", "os", "oss" },
348         { "Panjabi", "", "pa", "pan" },
349         { "Persian", "", "fa", "fas", "per" },
350         { "Pali", "", "pi", "pli" },
351         { "Polish", "", "pl", "pol" },
352         { "Portuguese", "Portugues", "pt", "por" },
353         { "Pushto", "", "ps", "pus" },
354         { "Quechua", "", "qu", "que" },
355         { "Romansh", "", "rm", "roh" },
356         { "Romanian", "", "ro", "ron", "rum" },
357         { "Rundi", "", "rn", "run" },
358         { "Russian", "", "ru", "rus" },
359         { "Sango", "", "sg", "sag" },
360         { "Sanskrit", "", "sa", "san" },
361         { "Serbian", "", "sr", "srp", "scc" },
362         { "Croatian", "Hrvatski", "hr", "hrv", "scr" },
363         { "Sinhala", "", "si", "sin" },
364         { "Slovak", "", "sk", "slk", "slo" },
365         { "Slovenian", "", "sl", "slv" },
366         { "Northern Sami", "", "se", "sme" },
367         { "Samoan", "", "sm", "smo" },
368         { "Shona", "", "sn", "sna" },
369         { "Sindhi", "", "sd", "snd" },
370         { "Somali", "", "so", "som" },
371         { "Sotho, Southern", "", "st", "sot" },
372         { "Spanish", "Espanol", "es", "spa" },
373         { "Sardinian", "", "sc", "srd" },
374         { "Swati", "", "ss", "ssw" },
375         { "Sundanese", "", "su", "sun" },
376         { "Swahili", "", "sw", "swa" },
377         { "Swedish", "Svenska", "sv", "swe" },
378         { "Tahitian", "", "ty", "tah" },
379         { "Tamil", "", "ta", "tam" },
380         { "Tatar", "", "tt", "tat" },
381         { "Telugu", "", "te", "tel" },
382         { "Tajik", "", "tg", "tgk" },
383         { "Tagalog", "", "tl", "tgl" },
384         { "Thai", "", "th", "tha" },
385         { "Tibetan", "", "bo", "bod", "tib" },
386         { "Tigrinya", "", "ti", "tir" },
387         { "Tonga", "", "to", "ton" },
388         { "Tswana", "", "tn", "tsn" },
389         { "Tsonga", "", "ts", "tso" },
390         { "Turkmen", "", "tk", "tuk" },
391         { "Turkish", "", "tr", "tur" },
392         { "Twi", "", "tw", "twi" },
393         { "Uighur", "", "ug", "uig" },
394         { "Ukrainian", "", "uk", "ukr" },
395         { "Urdu", "", "ur", "urd" },
396         { "Uzbek", "", "uz", "uzb" },
397         { "Venda", "", "ve", "ven" },
398         { "Vietnamese", "", "vi", "vie" },
399         { "Volapük", "", "vo", "vol" },
400         { "Welsh", "", "cy", "cym", "wel" },
401         { "Walloon", "", "wa", "wln" },
402         { "Wolof", "", "wo", "wol" },
403         { "Xhosa", "", "xh", "xho" },
404         { "Yiddish", "", "yi", "yid" },
405         { "Yoruba", "", "yo", "yor" },
406         { "Zhuang", "", "za", "zha" },
407         { "Zulu", "", "zu", "zul" },
408 };
409 #define LANG_TABLE_SIZE (sizeof(language_table)/ sizeof(iso639_lang_t))
410
411 static void
412 del_tree(const gchar *name, gboolean del_top)
413 {
414         const gchar *file;
415
416         if (g_file_test(name, G_FILE_TEST_IS_DIR))
417         {
418                 GDir *gdir = g_dir_open(name, 0, NULL);
419                 file = g_dir_read_name(gdir);
420                 while (file)
421                 {
422                         gchar *path;
423                         path = g_strdup_printf("%s/%s", name, file);
424                         del_tree(path, TRUE);
425                         g_free(path);
426                         file = g_dir_read_name(gdir);
427                 }
428                 if (del_top)
429                         g_rmdir(name);
430         }
431         else
432         {
433                 g_unlink(name);
434         }
435 }
436
437 const gchar*
438 ghb_version()
439 {
440         return HB_VERSION;
441 }
442
443 void
444 ghb_vquality_range(signal_user_data_t *ud, gint *min, gint *max)
445 {
446         if (ghb_settings_get_bool(ud->settings, "directqp"))
447         {
448                 gint vcodec = ghb_settings_get_int(ud->settings, "video_codec");
449                 // Only x264 and ffmpeg currently support direct qp/crf entry
450                 if (vcodec == HB_VCODEC_X264)
451                 {
452                         *min = 0;
453                         *max = 51;
454                 }
455                 else if (vcodec == HB_VCODEC_FFMPEG)
456                 {
457                         *min = 0;
458                         *max = 31;
459                 }
460                 else
461                 {
462                         *min = 0;
463                         *max = 100;
464                 }
465         }
466         else
467         {
468                 *min = 0;
469                 *max = 100;
470         }
471 }
472
473 static setting_value_t*
474 get_acodec_value(gint val)
475 {
476         setting_value_t *value = NULL;
477         gint ii;
478
479         for (ii = 0; ii < acodec_opts.count; ii++)
480         {
481                 if (acodec_opts.map[ii].ivalue == val)
482                 {
483                         value = g_malloc(sizeof(setting_value_t));
484                         value->option = g_strdup(acodec_opts.map[ii].option);
485                         value->shortOpt = g_strdup(acodec_opts.map[ii].shortOpt);
486                         value->svalue = g_strdup(acodec_opts.map[ii].svalue);
487                         value->index = ii;
488                         value->ivalue = acodec_opts.map[ii].ivalue;
489                         value->dvalue = acodec_opts.map[ii].dvalue;
490                         break;
491                 }
492         }
493         return value;
494 }
495
496 static setting_value_t*
497 get_amix_value(gint val)
498 {
499         setting_value_t *value = NULL;
500         gint ii;
501
502         for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
503         {
504                 if (hb_audio_mixdowns[ii].amixdown == val)
505                 {
506                         value = g_malloc(sizeof(setting_value_t));
507                         value->option = g_strdup(hb_audio_mixdowns[ii].human_readable_name);
508                         value->shortOpt = g_strdup(hb_audio_mixdowns[ii].short_name);
509                         value->svalue = g_strdup(hb_audio_mixdowns[ii].internal_name);
510                         value->index = ii;
511                         value->ivalue = hb_audio_mixdowns[ii].amixdown;
512                         value->dvalue = hb_audio_mixdowns[ii].amixdown;
513                         break;
514                 }
515         }
516         return value;
517 }
518
519 // Handle for libhb.  Gets set by ghb_backend_init()
520 static hb_handle_t * h = NULL;
521
522 extern void hb_get_tempory_directory(hb_handle_t *h, char path[512]);
523
524 void
525 ghb_hb_cleanup(gboolean partial)
526 {
527         char dir[512];
528
529         hb_get_tempory_directory(h, dir);
530         del_tree(dir, !partial);
531 }
532
533 static hb_audio_config_t*
534 get_hb_audio(gint titleindex, gint track)
535 {
536         hb_list_t  * list;
537         hb_title_t * title;
538     hb_audio_config_t *audio = NULL;
539         
540     if (h == NULL) return NULL;
541         list = hb_get_titles( h );
542         if( !hb_list_count( list ) )
543         {
544                 /* No valid title, stop right there */
545                 return NULL;
546         }
547     title = hb_list_item( list, titleindex );
548         if (title == NULL) return NULL; // Bad titleindex
549         if (!hb_list_count(title->list_audio))
550         {
551                 return NULL;
552         }
553     audio = (hb_audio_config_t *)hb_list_audio_config_item(title->list_audio, track);
554         return audio;
555 }
556
557 static gint
558 search_rates(hb_rate_t *rates, gint rate, gint count)
559 {
560         gint ii;
561         for (ii = 0; ii < count; ii++)
562         {
563                 if (rates[ii].rate == rate)
564                         return ii;
565         }
566         return -1;
567 }
568
569 #if 0
570 const gchar*
571 ghb_get_rate_string(gint rate, gint type)
572 {
573         gint index;
574         
575         switch (type)
576         {
577                 case GHB_FRAMERATE:
578                 {
579                         index = search_rates(hb_video_rates, rate, hb_video_rates_count);
580                         if (index >= 0) return hb_video_rates[index].string;
581                 } break;
582                 case GHB_AUDIO_BITRATE:
583                 {
584                         rate /= 1000;
585                         index = search_rates(hb_audio_bitrates, rate, hb_audio_bitrates_count);
586                         if (index >= 0) return hb_audio_bitrates[index].string;
587                 } break;
588                 case GHB_AUDIO_SAMPLERATE:
589                 {
590                         index = search_rates(hb_audio_rates, rate, hb_audio_rates_count);
591                         if (index >= 0) return hb_audio_rates[index].string;
592                 } break;
593         }
594         return NULL;
595 }
596 #endif
597
598 static gboolean find_combo_item_by_int(GtkTreeModel *store, gint value, GtkTreeIter *iter);
599
600 static GtkListStore*
601 get_combo_box_store(GtkBuilder *builder, const gchar *name)
602 {
603         GtkComboBox *combo;
604         GtkListStore *store;
605
606         g_debug("get_combo_box_store() %s\n", name);
607         // First modify the combobox model to allow greying out of options
608         combo = GTK_COMBO_BOX(GHB_WIDGET(builder, name));
609         store = GTK_LIST_STORE(gtk_combo_box_get_model (combo));
610         return store;
611 }
612
613 static void
614 grey_combo_box_item(GtkBuilder *builder, const gchar *name, gint value, gboolean grey)
615 {
616         GtkListStore *store;
617         GtkTreeIter iter;
618         
619         store = get_combo_box_store(builder, name);
620         if (find_combo_item_by_int(GTK_TREE_MODEL(store), value, &iter))
621         {
622                 gtk_list_store_set(store, &iter, 
623                                                    1, !grey, 
624                                                    -1);
625         }
626 }
627
628 void
629 ghb_grey_combo_options(GtkBuilder *builder)
630 {
631         GtkWidget *widget;
632         gint container, track, titleindex, acodec;
633     hb_audio_config_t *audio = NULL;
634         
635         widget = GHB_WIDGET (builder, "title");
636         titleindex = ghb_widget_int(widget);
637         widget = GHB_WIDGET (builder, "audio_track");
638         track = ghb_widget_int(widget);
639         audio = get_hb_audio(titleindex, track);
640
641         grey_combo_box_item(builder, "audio_codec", HB_ACODEC_FAAC, FALSE);
642         grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_FAAC, FALSE);
643         grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_FAAC, FALSE);
644         grey_combo_box_item(builder, "audio_codec", HB_ACODEC_LAME, FALSE);
645         grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_LAME, FALSE);
646         grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_LAME, FALSE);
647         grey_combo_box_item(builder, "audio_codec", HB_ACODEC_VORBIS, FALSE);
648         grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_VORBIS, FALSE);
649         grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_VORBIS, FALSE);
650         if (audio && audio->in.codec != HB_ACODEC_AC3)
651         {
652                 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_AC3, TRUE);
653         }
654         else
655         {
656                 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_AC3, FALSE);
657         }
658         grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_AC3, FALSE);
659         grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_AC3, FALSE);
660         grey_combo_box_item(builder, "video_codec", HB_VCODEC_THEORA, FALSE);
661
662         widget = GHB_WIDGET (builder, "audio_codec");
663         acodec = ghb_widget_int(widget);
664         if (acodec != HB_ACODEC_AC3)
665         {
666                 grey_combo_box_item(builder, "audio_mix", 0, TRUE);
667         }
668         widget = GHB_WIDGET (builder, "container");
669         container = ghb_widget_int(widget);
670         if (container == HB_MUX_MP4)
671         {
672                 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_LAME, TRUE);
673                 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_LAME, TRUE);
674                 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_LAME, TRUE);
675                 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_VORBIS, TRUE);
676                 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_VORBIS, TRUE);
677                 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_VORBIS, TRUE);
678                 grey_combo_box_item(builder, "video_codec", HB_VCODEC_THEORA, TRUE);
679         }
680         else if (container == HB_MUX_AVI)
681         {
682                 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_FAAC, TRUE);
683                 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_FAAC, TRUE);
684                 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_FAAC, TRUE);
685                 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_VORBIS, TRUE);
686                 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_VORBIS, TRUE);
687                 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_VORBIS, TRUE);
688                 grey_combo_box_item(builder, "video_codec", HB_VCODEC_THEORA, TRUE);
689         }
690         else if (container == HB_MUX_OGM)
691         {
692                 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_FAAC, TRUE);
693                 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_FAAC, TRUE);
694                 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_FAAC, TRUE);
695                 grey_combo_box_item(builder, "audio_codec", HB_ACODEC_AC3, TRUE);
696                 grey_combo_box_item(builder, "pref_audio_codec1", HB_ACODEC_AC3, TRUE);
697                 grey_combo_box_item(builder, "pref_audio_codec2", HB_ACODEC_AC3, TRUE);
698         }
699
700         gboolean allow_mono = TRUE;
701         gboolean allow_stereo = TRUE;
702         gboolean allow_dolby = TRUE;
703         gboolean allow_dpl2 = TRUE;
704         gboolean allow_6ch = TRUE;
705         if (audio)
706         {
707                 allow_mono =
708                         (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
709                         (acodec != HB_ACODEC_LAME);
710                 gint layout = audio->in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
711                 allow_stereo =
712                         ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
713                 allow_dolby =
714                         (layout == HB_INPUT_CH_LAYOUT_3F1R) || 
715                         (layout == HB_INPUT_CH_LAYOUT_3F2R) || 
716                         (layout == HB_INPUT_CH_LAYOUT_DOLBY);
717                 allow_dpl2 = (layout == HB_INPUT_CH_LAYOUT_3F2R);
718                 allow_6ch =
719                         (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
720                         (acodec != HB_ACODEC_LAME) &&
721                         (layout == HB_INPUT_CH_LAYOUT_3F2R) && 
722                         (audio->in.channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE);
723         }
724         grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_MONO, !allow_mono);
725         grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_STEREO, !allow_stereo);
726         grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_DOLBY, !allow_dolby);
727         grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_DOLBYPLII, !allow_dpl2);
728         grey_combo_box_item(builder, "audio_mix", HB_AMIXDOWN_6CH, !allow_6ch);
729 }
730
731 gint
732 ghb_get_best_mix(gint titleindex, gint track, gint acodec, gint mix)
733 {
734     hb_audio_config_t *audio = NULL;
735         gboolean allow_mono = TRUE;
736         gboolean allow_stereo = TRUE;
737         gboolean allow_dolby = TRUE;
738         gboolean allow_dpl2 = TRUE;
739         gboolean allow_6ch = TRUE;
740         
741         if (acodec & (HB_ACODEC_AC3 | HB_ACODEC_DCA))
742         {
743                 // Audio codec pass-thru.  No mixdown
744                 return 0;
745         }
746         audio = get_hb_audio(titleindex, track);
747         if (audio)
748         {
749                 allow_mono =
750                         (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
751                         (acodec != HB_ACODEC_LAME);
752                 gint layout = audio->in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
753                 allow_stereo =
754                         ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
755                 allow_dolby =
756                         (layout == HB_INPUT_CH_LAYOUT_3F1R) || 
757                         (layout == HB_INPUT_CH_LAYOUT_3F2R) || 
758                         (layout == HB_INPUT_CH_LAYOUT_DOLBY);
759                 allow_dpl2 = (layout == HB_INPUT_CH_LAYOUT_3F2R);
760                 allow_6ch =
761                         (audio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
762                         (acodec != HB_ACODEC_LAME) &&
763                         (layout == HB_INPUT_CH_LAYOUT_3F2R) && 
764                         (audio->in.channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE);
765         }
766         gboolean greater = FALSE;
767         if (mix == 0) 
768         {
769                 // If no mix is specified, select the best available.
770                 mix = HB_AMIXDOWN_6CH;
771         }
772         if (mix == HB_AMIXDOWN_6CH)
773         {
774                 greater = TRUE;
775                 if (allow_6ch) return HB_AMIXDOWN_6CH;
776         }
777         if (mix == HB_AMIXDOWN_DOLBYPLII || greater)
778         {
779                 greater = TRUE;
780                 if (allow_dpl2) return HB_AMIXDOWN_DOLBYPLII;
781         }
782         if (mix == HB_AMIXDOWN_DOLBY || greater)
783         {
784                 greater = TRUE;
785                 if (allow_dolby) return HB_AMIXDOWN_DOLBY;
786         }
787         if (mix == HB_AMIXDOWN_STEREO || greater)
788         {
789                 greater = TRUE;
790                 if (allow_stereo) return HB_AMIXDOWN_STEREO;
791         }
792         if (mix == HB_AMIXDOWN_MONO || greater)
793         {
794                 greater = TRUE;
795                 if (allow_mono) return HB_AMIXDOWN_MONO;
796         }
797         if (allow_stereo) return HB_AMIXDOWN_STEREO;
798         if (allow_dolby) return HB_AMIXDOWN_DOLBY;
799         if (allow_dpl2) return HB_AMIXDOWN_DOLBYPLII;
800         if (allow_6ch) return HB_AMIXDOWN_6CH;
801         return 0;
802 }
803
804 // Set up the model for the combo box
805 static void
806 init_combo_box(GtkBuilder *builder, const gchar *name)
807 {
808         GtkComboBox *combo;
809         GtkListStore *store;
810         GtkCellRenderer *cell;
811
812         g_debug("init_combo_box() %s\n", name);
813         // First modify the combobox model to allow greying out of options
814         combo = GTK_COMBO_BOX(GHB_WIDGET(builder, name));
815         // Store contains:
816         // 1 - String to display
817         // 2 - bool indicating whether the entry is selectable (grey or not)
818         // 3 - String that is used for presets
819         // 4 - Int value determined by backend
820         // 5 - String value determined by backend
821         store = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_BOOLEAN, 
822                                                            G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
823         gtk_combo_box_set_model(combo, GTK_TREE_MODEL(store));
824
825         gtk_cell_layout_clear(GTK_CELL_LAYOUT(combo));
826     cell = GTK_CELL_RENDERER(gtk_cell_renderer_text_new());
827     gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), cell, TRUE);
828     gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), cell,
829       "text", 0, "sensitive", 1, NULL);
830 }       
831
832 static void
833 audio_bitrate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
834 {
835         GtkTreeIter iter;
836         GtkListStore *store;
837         gint ii;
838         
839         g_debug("audio_bitrate_opts_set ()\n");
840         store = get_combo_box_store(builder, name);
841         gtk_list_store_clear(store);
842         for (ii = 0; ii < count; ii++)
843         {
844                 gtk_list_store_append(store, &iter);
845                 gtk_list_store_set(store, &iter, 
846                                                    0, rates[ii].string, 
847                                                    1, TRUE, 
848                                                    2, rates[ii].string, 
849                                                    3, rates[ii].rate * 1000, 
850                                                    4, rates[ii].string, 
851                                                    -1);
852         }
853 }
854
855 static gboolean
856 audio_bitrate_opts_clean(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
857 {
858         GtkTreeIter iter;
859         GtkListStore *store;
860         gint ivalue;
861         gboolean done = FALSE;
862         gboolean changed = FALSE;
863         
864         g_debug("audio_bitrate_opts_clean ()\n");
865         store = get_combo_box_store(builder, name);
866         if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL(store), &iter))
867         {
868                 do
869                 {
870                         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, 3, &ivalue, -1);
871                         if (search_rates(rates, ivalue/1000, count) < 0)
872                         {
873                                 done = !gtk_list_store_remove(store, &iter);
874                                 changed = TRUE;
875                         }
876                         else
877                         {
878                                 done = !gtk_tree_model_iter_next (GTK_TREE_MODEL(store), &iter);
879                         }
880                 } while (!done);
881         }
882         return changed;
883 }
884
885 static void
886 audio_samplerate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
887 {
888         GtkTreeIter iter;
889         GtkListStore *store;
890         gint ii;
891         
892         g_debug("audio_samplerate_opts_set ()\n");
893         store = get_combo_box_store(builder, name);
894         gtk_list_store_clear(store);
895         // Add an item for "Same As Source"
896         gtk_list_store_append(store, &iter);
897         gtk_list_store_set(store, &iter, 
898                                            0, "Same as source", 
899                                            1, TRUE, 
900                                            2, "source", 
901                                            3, 0, 
902                                            4, "source", 
903                                            -1);
904         for (ii = 0; ii < count; ii++)
905         {
906                 gtk_list_store_append(store, &iter);
907                 gtk_list_store_set(store, &iter, 
908                                                    0, rates[ii].string, 
909                                                    1, TRUE, 
910                                                    2, rates[ii].string, 
911                                                    3, rates[ii].rate, 
912                                                    4, rates[ii].string, 
913                                                    -1);
914         }
915 }
916
917 static void
918 video_rate_opts_set(GtkBuilder *builder, const gchar *name, hb_rate_t *rates, gint count)
919 {
920         GtkTreeIter iter;
921         GtkListStore *store;
922         gint ii;
923         
924         g_debug("video_rate_opts_set ()\n");
925         store = get_combo_box_store(builder, name);
926         gtk_list_store_clear(store);
927         // Add an item for "Same As Source"
928         gtk_list_store_append(store, &iter);
929         gtk_list_store_set(store, &iter, 
930                                            0, "Same as source", 
931                                            1, TRUE, 
932                                            2, "source", 
933                                            3, 0, 
934                                            4, "source", 
935                                            -1);
936         for (ii = 0; ii < count; ii++)
937         {
938                 gchar *desc = "";
939                 gchar *option;
940                 if (strcmp(rates[ii].string, "23.976") == 0)
941                 {
942                         desc = "(NTSC Film)";
943                 }
944                 else if (strcmp(rates[ii].string, "25") == 0)
945                 {
946                         desc = "(PAL Film/Video)";
947                 }
948                 else if (strcmp(rates[ii].string, "29.97") == 0)
949                 {
950                         desc = "(NTSC Video)";
951                 }
952                 option = g_strdup_printf ("%s %s", rates[ii].string, desc);
953                 gtk_list_store_append(store, &iter);
954                 gtk_list_store_set(store, &iter, 
955                                                    0, option, 
956                                                    1, TRUE, 
957                                                    2, rates[ii].string, 
958                                                    3, rates[ii].rate, 
959                                                    4, rates[ii].string, 
960                                                    -1);
961                 g_free(option);
962         }
963 }
964
965 static void
966 mix_opts_set(GtkBuilder *builder, const gchar *name)
967 {
968         GtkTreeIter iter;
969         GtkListStore *store;
970         gint ii;
971         
972         g_debug("audio_bitrate_opts_set ()\n");
973         store = get_combo_box_store(builder, name);
974         gtk_list_store_clear(store);
975         gtk_list_store_append(store, &iter);
976         gtk_list_store_set(store, &iter, 
977                                            0, "None", 
978                                            1, TRUE, 
979                                            2, "none", 
980                                            3, 0, 
981                                            4, "none", 
982                                            -1);
983         for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
984         {
985                 gtk_list_store_append(store, &iter);
986                 gtk_list_store_set(store, &iter, 
987                                                    0, hb_audio_mixdowns[ii].human_readable_name, 
988                                                    1, TRUE, 
989                                                    2, hb_audio_mixdowns[ii].short_name, 
990                                                    3, hb_audio_mixdowns[ii].amixdown, 
991                                                    4, hb_audio_mixdowns[ii].internal_name, 
992                                                    -1);
993         }
994 }
995
996 static void
997 language_opts_set(GtkBuilder *builder, const gchar *name)
998 {
999         GtkTreeIter iter;
1000         GtkListStore *store;
1001         gint ii;
1002         
1003         g_debug("audio_bitrate_opts_set ()\n");
1004         store = get_combo_box_store(builder, name);
1005         gtk_list_store_clear(store);
1006         for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
1007         {
1008                 gtk_list_store_append(store, &iter);
1009                 gtk_list_store_set(store, &iter, 
1010                                                    0, language_table[ii].eng_name, 
1011                                                    1, TRUE, 
1012                                                    2, language_table[ii].iso639_2, 
1013                                                    3, ii, 
1014                                                    4, language_table[ii].iso639_1, 
1015                                                    -1);
1016         }
1017 }
1018
1019 void
1020 title_opts_set(GtkBuilder *builder, const gchar *name)
1021 {
1022         GtkTreeIter iter;
1023         GtkListStore *store;
1024         hb_list_t  * list;
1025         hb_title_t * title;
1026         gint ii;
1027         gint count = 0;
1028         
1029         g_debug("title_opts_set ()\n");
1030         store = get_combo_box_store(builder, name);
1031         gtk_list_store_clear(store);
1032         if (h != NULL)
1033         {
1034                 list = hb_get_titles( h );
1035                 count = hb_list_count( list );
1036                 if (count > 100) count = 100;
1037         }
1038         if( count <= 0 )
1039         {
1040                 // No titles.  Fill in a default.
1041                 gtk_list_store_append(store, &iter);
1042                 gtk_list_store_set(store, &iter, 
1043                                                    0, "No Titles", 
1044                                                    1, TRUE, 
1045                                                    2, "none", 
1046                                                    3, -1, 
1047                                                    4, "none", 
1048                                                    -1);
1049                 return;
1050         }
1051         for (ii = 0; ii < count; ii++)
1052         {
1053                 gchar *option;
1054                 
1055                 title = (hb_title_t*)hb_list_item(list, ii);
1056                 option  = g_strdup_printf ("%d - %02dh%02dm%02ds",
1057                         title->index, title->hours, title->minutes, title->seconds);
1058                 gtk_list_store_append(store, &iter);
1059                 gtk_list_store_set(store, &iter, 
1060                                                    0, option, 
1061                                                    1, TRUE, 
1062                                                    2, option, 
1063                                                    3, ii, 
1064                                                    4, option, 
1065                                                    -1);
1066         }
1067 }
1068
1069 static gboolean
1070 find_combo_item_by_int(GtkTreeModel *store, gint value, GtkTreeIter *iter)
1071 {
1072         gint ivalue;
1073         gboolean foundit = FALSE;
1074         
1075         if (gtk_tree_model_get_iter_first (store, iter))
1076         {
1077                 do
1078                 {
1079                         gtk_tree_model_get(store, iter, 3, &ivalue, -1);
1080                         if (value == ivalue)
1081                         {
1082                                 foundit = TRUE;
1083                                 break;
1084                         }
1085                 } while (gtk_tree_model_iter_next (store, iter));
1086         }
1087         return foundit;
1088 }
1089
1090 static gboolean
1091 audio_rate_opts_add(GtkBuilder *builder, const gchar *name, gint rate)
1092 {
1093         GtkTreeIter iter;
1094         GtkListStore *store;
1095         gchar *str;
1096         
1097         g_debug("audio_rate_opts_add ()\n");
1098         store = get_combo_box_store(builder, name);
1099         if (!find_combo_item_by_int(GTK_TREE_MODEL(store), rate, &iter))
1100         {
1101                 str = g_strdup_printf ("%.8g", (gdouble)rate/1000.0);
1102                 gtk_list_store_append(store, &iter);
1103                 gtk_list_store_set(store, &iter, 
1104                                                    0, str, 
1105                                                    1, TRUE, 
1106                                                    2, str, 
1107                                                    3, rate, 
1108                                                    4, str, 
1109                                                    -1);
1110                 return TRUE;
1111         }
1112         return FALSE;
1113 }
1114
1115 #if 0
1116 static gboolean
1117 audio_rate_opts_remove(GtkBuilder *builder, const gchar *name, gint rate)
1118 {
1119         GtkTreeIter iter;
1120         GtkListStore *store;
1121         
1122         g_debug("audio_rate_opts_remove ()\n");
1123         store = get_combo_box_store(builder, name);
1124         if (find_combo_item_by_int(GTK_TREE_MODEL(store), rate, &iter))
1125         {
1126                 gtk_list_store_remove (store, &iter);
1127                 return TRUE;
1128         }
1129         return FALSE;
1130 }
1131 #endif
1132
1133 void
1134 audio_track_opts_set(GtkBuilder *builder, const gchar *name, gint titleindex)
1135 {
1136         GtkTreeIter iter;
1137         GtkListStore *store;
1138         hb_list_t  * list;
1139         hb_title_t * title;
1140     hb_audio_config_t * audio;
1141         gint ii;
1142         gint count = 0;
1143         
1144         g_debug("audio_track_opts_set ()\n");
1145         store = get_combo_box_store(builder, name);
1146         gtk_list_store_clear(store);
1147         if (h != NULL)
1148         {
1149                 list = hb_get_titles( h );
1150             title = (hb_title_t*)hb_list_item( list, titleindex );
1151                 if (title != NULL)
1152                 {
1153                         count = hb_list_count( title->list_audio );
1154                 }
1155         }
1156         if (count > 10) count = 10;
1157         if( count <= 0 )
1158         {
1159                 // No audio. set some default
1160                 gtk_list_store_append(store, &iter);
1161                 gtk_list_store_set(store, &iter, 
1162                                                    0, "No Audio", 
1163                                                    1, TRUE, 
1164                                                    2, "none", 
1165                                                    3, -1, 
1166                                                    4, "none", 
1167                                                    -1);
1168                 return;
1169         }
1170         for (ii = 0; ii < count; ii++)
1171         {
1172         audio = (hb_audio_config_t *) hb_list_audio_config_item( title->list_audio, ii );
1173                 gtk_list_store_append(store, &iter);
1174                 gtk_list_store_set(store, &iter, 
1175                                                    0, audio->lang.description, 
1176                                                    1, TRUE, 
1177                                                    2, audio->lang.description, 
1178                                                    3, ii, 
1179                                                    4, audio->lang.description, 
1180                                                    -1);
1181         }
1182 }
1183
1184 void
1185 subtitle_opts_set(GtkBuilder *builder, const gchar *name, gint titleindex)
1186 {
1187         GtkTreeIter iter;
1188         GtkListStore *store;
1189         hb_list_t  * list;
1190         hb_title_t * title;
1191     hb_subtitle_t * subtitle;
1192         gint ii;
1193         gint count = 0;
1194         
1195         g_debug("subtitle_opts_set ()\n");
1196         store = get_combo_box_store(builder, name);
1197         gtk_list_store_clear(store);
1198         if (h != NULL)
1199         {
1200                 list = hb_get_titles( h );
1201             title = (hb_title_t*)hb_list_item( list, titleindex );
1202                 if (title != NULL)
1203                 {
1204                         count = hb_list_count( title->list_subtitle );
1205                 }
1206         }
1207         if (count > 10) count = 10;
1208         // Add options for "none" and "autoselect"
1209         gtk_list_store_append(store, &iter);
1210         gtk_list_store_set(store, &iter, 
1211                                            0, "None", 
1212                                            1, TRUE, 
1213                                            2, "none", 
1214                                            3, -2, 
1215                                            4, "none", 
1216                                            -1);
1217         gtk_list_store_append(store, &iter);
1218         gtk_list_store_set(store, &iter, 
1219                                            0, "Same As Audio", 
1220                                            1, TRUE, 
1221                                            2, "auto", 
1222                                            3, -1, 
1223                                            4, "auto", 
1224                                            -1);
1225         for (ii = 0; ii < count; ii++)
1226         {
1227         subtitle = (hb_subtitle_t *) hb_list_item( title->list_subtitle, ii );
1228                 gtk_list_store_append(store, &iter);
1229                 gtk_list_store_set(store, &iter, 
1230                                                    0, subtitle->lang, 
1231                                                    1, TRUE, 
1232                                                    2, subtitle->iso639_2, 
1233                                                    3, ii, 
1234                                                    4, subtitle->iso639_2, 
1235                                                    -1);
1236         }
1237         if (titleindex == -1)
1238         {
1239                 for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
1240                 {
1241                         gtk_list_store_append(store, &iter);
1242                         gtk_list_store_set(store, &iter, 
1243                                 0, language_table[ii].eng_name, 
1244                                 1, TRUE, 
1245                                 2, language_table[ii].iso639_2, 
1246                                 3, ii, 
1247                                 4, language_table[ii].iso639_2, 
1248                                 -1);
1249                 }
1250         }
1251 }
1252
1253 gint
1254 ghb_longest_title()
1255 {
1256         hb_list_t  * list;
1257         hb_title_t * title;
1258         gint ii;
1259         gint count = 0;
1260         guint64 longest = 0;
1261         gint titleindex = 0;
1262         
1263         g_debug("ghb_longest_title ()\n");
1264         if (h == NULL) return 0;
1265         list = hb_get_titles( h );
1266         count = hb_list_count( list );
1267         if (count > 100) count = 100;
1268         for (ii = 0; ii < count; ii++)
1269         {
1270                 title = (hb_title_t*)hb_list_item(list, ii);
1271                 if (title->duration > longest)
1272                 {
1273                         titleindex = ii;
1274                         longest = title->duration;
1275                 }
1276         }
1277         return titleindex;
1278 }
1279
1280 gint
1281 ghb_find_audio_track(gint titleindex, const gchar *lang, gint acodec)
1282 {
1283         hb_list_t  * list;
1284         hb_title_t * title;
1285     hb_audio_config_t * audio;
1286         gint ii;
1287         gint count = 0;
1288         gint track = 0;
1289         
1290         g_debug("find_audio_track ()\n");
1291         if (h != NULL)
1292         {
1293                 list = hb_get_titles( h );
1294             title = (hb_title_t*)hb_list_item( list, titleindex );
1295                 if (title != NULL)
1296                 {
1297                         count = hb_list_count( title->list_audio );
1298                 }
1299         }
1300         if (count > 10) count = 10;
1301         for (ii = 0; ii < count; ii++)
1302         {
1303         audio = (hb_audio_config_t*)hb_list_audio_config_item( title->list_audio, ii );
1304                 if ((strcmp(lang, audio->lang.iso639_2) == 0) ||
1305                         (strcmp(lang, "und") == 0))
1306                 {
1307                         // Candidate track.  Will use if no better match found
1308                         track = ii;
1309                         if (audio->in.codec == acodec)
1310                         {
1311                                 // Perfect match
1312                                 return track;
1313                         }
1314                 }
1315         }
1316         return track;
1317 }
1318
1319 static void
1320 generic_opts_set(GtkBuilder *builder, const gchar *name, combo_opts_t *opts)
1321 {
1322         GtkTreeIter iter;
1323         GtkListStore *store;
1324         gint ii;
1325         
1326         g_debug("generic_opts_set ()\n");
1327         store = get_combo_box_store(builder, name);
1328         gtk_list_store_clear(store);
1329         for (ii = 0; ii < opts->count; ii++)
1330         {
1331                 gtk_list_store_append(store, &iter);
1332                 gtk_list_store_set(store, &iter, 
1333                                                    0, opts->map[ii].option, 
1334                                                    1, TRUE, 
1335                                                    2, opts->map[ii].shortOpt, 
1336                                                    3, opts->map[ii].ivalue, 
1337                                                    4, opts->map[ii].svalue, 
1338                                                    -1);
1339         }
1340 }
1341
1342 void
1343 ghb_update_ui_combo_box(GtkBuilder *builder, const gchar *name, gint user_data, gboolean all)
1344 {
1345         GtkComboBox *combo = NULL;
1346         gint signal_id;
1347         gint handler_id = 0;
1348
1349         g_debug("ghb_update_ui_combo_box() %s\n", name);
1350         if (name != NULL)
1351         {               
1352                 // Clearing a combo box causes a rash of "changed" events, even when
1353                 // the active item is -1 (inactive).  To control things, I'm disabling
1354                 // the event till things are settled down.
1355                 combo = GTK_COMBO_BOX(GHB_WIDGET(builder, name));
1356                 signal_id = g_signal_lookup("changed", GTK_TYPE_COMBO_BOX);
1357                 if (signal_id > 0)
1358                 {
1359                         // Valid signal id found.  This should always succeed.
1360                         handler_id = g_signal_handler_find ((gpointer)combo, G_SIGNAL_MATCH_ID, 
1361                                                                                                 signal_id, 0, 0, 0, 0);
1362                         if (handler_id > 0)
1363                         {
1364                                 // This should also always succeed
1365                                 g_signal_handler_block ((gpointer)combo, handler_id);
1366                         }
1367                 }
1368         }       
1369         if (all || strcmp(name, "audio_bitrate") == 0)
1370                 audio_bitrate_opts_set(builder, "audio_bitrate", hb_audio_bitrates, hb_audio_bitrates_count);
1371         if (all || strcmp(name, "pref_audio_bitrate1") == 0)
1372                 audio_bitrate_opts_set(builder, "pref_audio_bitrate1", hb_audio_bitrates, hb_audio_bitrates_count);
1373         if (all || strcmp(name, "pref_audio_bitrate2") == 0)
1374                 audio_bitrate_opts_set(builder, "pref_audio_bitrate2", hb_audio_bitrates, hb_audio_bitrates_count);
1375         if (all || strcmp(name, "audio_sample_rate") == 0)
1376                 audio_samplerate_opts_set(builder, "audio_sample_rate", hb_audio_rates, hb_audio_rates_count);
1377         if (all || strcmp(name, "pref_audio_rate1") == 0)
1378                 audio_samplerate_opts_set(builder, "pref_audio_rate1", hb_audio_rates, hb_audio_rates_count);
1379         if (all || strcmp(name, "pref_audio_rate2") == 0)
1380                 audio_samplerate_opts_set(builder, "pref_audio_rate2", hb_audio_rates, hb_audio_rates_count);
1381         if (all || strcmp(name, "framerate") == 0)
1382                 video_rate_opts_set(builder, "framerate", hb_video_rates, hb_video_rates_count);
1383         if (all || strcmp(name, "audio_mix") == 0)
1384                 mix_opts_set(builder, "audio_mix");
1385         if (all || strcmp(name, "pref_audio_mix1") == 0)
1386                 mix_opts_set(builder, "pref_audio_mix1");
1387         if (all || strcmp(name, "pref_audio_mix2") == 0)
1388                 mix_opts_set(builder, "pref_audio_mix2");
1389         if (all || strcmp(name, "pref_source_audio_lang") == 0)
1390                 language_opts_set(builder, "pref_source_audio_lang");
1391         if (all || strcmp(name, "subtitle_lang") == 0)
1392                 subtitle_opts_set(builder, "subtitle_lang", user_data);
1393         if (all || strcmp(name, "title") == 0)
1394                 title_opts_set(builder, "title");
1395         if (all || strcmp(name, "audio_track") == 0)
1396                 audio_track_opts_set(builder, "audio_track", user_data);
1397         if (all || strcmp(name, "container") == 0)
1398                 generic_opts_set(builder, "container", &container_opts);
1399         if (all || strcmp(name, "deinterlace") == 0)
1400                 generic_opts_set(builder, "deinterlace", &deint_opts);
1401         if (all || strcmp(name, "denoise") == 0)
1402                 generic_opts_set(builder, "denoise", &denoise_opts);
1403         if (all || strcmp(name, "video_codec") == 0)
1404                 generic_opts_set(builder, "video_codec", &vcodec_opts);
1405         if (all || strcmp(name, "audio_codec") == 0)
1406                 generic_opts_set(builder, "audio_codec", &acodec_opts);
1407         if (all || strcmp(name, "pref_audio_codec1") == 0)
1408                 generic_opts_set(builder, "pref_audio_codec1", &pref_acodec_opts);
1409         if (all || strcmp(name, "pref_audio_codec2") == 0)
1410                 generic_opts_set(builder, "pref_audio_codec2", &pref_acodec_opts);
1411         if (all || strcmp(name, "pref_source_audio_codec") == 0)
1412                 generic_opts_set(builder, "pref_source_audio_codec", &source_acodec_opts);
1413         if (all || strcmp(name, "x264_direct") == 0)
1414                 generic_opts_set(builder, "x264_direct", &direct_opts);
1415         if (all || strcmp(name, "x264_me") == 0)
1416                 generic_opts_set(builder, "x264_me", &me_opts);
1417         if (all || strcmp(name, "x264_subme") == 0)
1418                 generic_opts_set(builder, "x264_subme", &subme_opts);
1419         if (all || strcmp(name, "x264_analyse") == 0)
1420                 generic_opts_set(builder, "x264_analyse", &analyse_opts);
1421         if (all || strcmp(name, "x264_trellis") == 0)
1422                 generic_opts_set(builder, "x264_trellis", &trellis_opts);
1423         if (handler_id > 0)
1424         {
1425                 g_signal_handler_unblock ((gpointer)combo, handler_id);
1426         }
1427 }
1428         
1429 static void
1430 init_ui_combo_boxes(GtkBuilder *builder)
1431 {
1432         init_combo_box(builder, "audio_bitrate");
1433         init_combo_box(builder, "pref_audio_bitrate1");
1434         init_combo_box(builder, "pref_audio_bitrate2");
1435         init_combo_box(builder, "audio_sample_rate");
1436         init_combo_box(builder, "pref_audio_rate1");
1437         init_combo_box(builder, "pref_audio_rate2");
1438         init_combo_box(builder, "framerate");
1439         init_combo_box(builder, "audio_mix");
1440         init_combo_box(builder, "pref_audio_mix1");
1441         init_combo_box(builder, "pref_audio_mix2");
1442         init_combo_box(builder, "pref_source_audio_lang");
1443         init_combo_box(builder, "subtitle_lang");
1444         init_combo_box(builder, "title");
1445         init_combo_box(builder, "audio_track");
1446         init_combo_box(builder, "container");
1447         init_combo_box(builder, "deinterlace");
1448         init_combo_box(builder, "denoise");
1449         init_combo_box(builder, "video_codec");
1450         init_combo_box(builder, "audio_codec");
1451         init_combo_box(builder, "pref_audio_codec1");
1452         init_combo_box(builder, "pref_audio_codec2");
1453         init_combo_box(builder, "pref_source_audio_codec");
1454         init_combo_box(builder, "x264_direct");
1455         init_combo_box(builder, "x264_me");
1456         init_combo_box(builder, "x264_subme");
1457         init_combo_box(builder, "x264_analyse");
1458         init_combo_box(builder, "x264_trellis");
1459 }
1460         
1461 static const char * turbo_opts = 
1462         "ref=1:subme=1:me=dia:analyse=none:trellis=0:"
1463         "no-fast-pskip=0:8x8dct=0:weightb=0";
1464
1465 // Construct the x264 options string
1466 // The result is allocated, so someone must free it at some point.
1467 gchar*
1468 ghb_build_x264opts_string(GHashTable *settings)
1469 {
1470         GString *x264opts = g_string_new("");
1471         gint refs = ghb_settings_get_int(settings, "x264_refs");
1472         if (refs != 1)
1473         {
1474                 g_string_append_printf(x264opts, "ref=%d:", refs);
1475         }
1476         if (refs > 1)
1477         {
1478                 if (ghb_settings_get_bool(settings, "x264_mixed_refs"))
1479                 {
1480                         g_string_append(x264opts, "mixed-refs=1:");
1481                 }
1482         }
1483         gint subme = ghb_settings_get_int(settings, "x264_subme");
1484         if (subme != 5) // 5 is default
1485         {
1486                 g_string_append_printf(x264opts, "subme=%d:", subme);
1487         }
1488         gint bframes = ghb_settings_get_int(settings, "x264_bframes");
1489         if (bframes > 0)
1490         {
1491                 g_string_append_printf(x264opts, "bframes=%d:", bframes);
1492                 if (ghb_settings_get_bool(settings, "x264_weighted_bframes"))
1493                 {
1494                         g_string_append(x264opts, "weightb=1:");
1495                 }
1496                 if (subme >= 6)
1497                 {
1498                         if (ghb_settings_get_bool(settings, "x264_brdo"))
1499                         {
1500                                 g_string_append(x264opts, "brdo=1:");
1501                         }
1502                 }
1503                 if (ghb_settings_get_bool(settings, "x264_bime"))
1504                 {
1505                         g_string_append(x264opts, "bime=1:");
1506                 }
1507         }
1508         if (bframes > 1)
1509         {
1510                 if (ghb_settings_get_bool(settings, "x264_bpyramid"))
1511                 {
1512                         g_string_append(x264opts, "b-pyramid=1:");
1513                 }
1514         }
1515         if (ghb_settings_get_bool(settings, "x264_no_fast_pskip"))
1516         {
1517                 g_string_append(x264opts, "no-fast-pskip=1:");
1518         }
1519         if (ghb_settings_get_bool(settings, "x264_no_dct_decimate"))
1520         {
1521                 g_string_append(x264opts, "no-dct-decimate=1:");
1522         }
1523         if (!ghb_settings_get_bool(settings, "x264_cabac"))
1524         {
1525                 g_string_append(x264opts, "cabac=0:");
1526         }
1527         else
1528         {
1529                 gint trellis = ghb_settings_get_int(settings, "x264_trellis");
1530                 if (trellis != 0); // != None
1531                 {
1532                         g_string_append_printf(x264opts, "trellis=%d:", trellis);
1533                 }
1534         }
1535         gint dba, dbb;
1536         dba = ghb_settings_get_int(settings, "x264_deblock_alpha");
1537         dbb = ghb_settings_get_int(settings, "x264_deblock_beta");
1538         if (dba != 0 || dbb != 0)
1539         {
1540                 g_string_append_printf(x264opts, "deblock=%d,%d:", dba, dbb);
1541         }
1542         const gchar *me = ghb_settings_get_string(settings, "x264_me");
1543         g_string_append_printf(x264opts, "me=%s:", me);
1544         gint analyse = ghb_settings_get_int(settings, "x264_analyse");
1545         if (analyse != 0) // != Some
1546         {
1547                 g_string_append_printf(x264opts, "analyse=%s:", 
1548                                                            ghb_settings_get_string(settings, "x264_analyse"));
1549         }
1550         if (analyse != 1) // != none
1551         {
1552                 gint direct = ghb_settings_get_int(settings, "x264_direct");
1553                 if (direct != 1) // !spatial
1554                 {
1555                         g_string_append_printf(x264opts, "direct=%s:", 
1556                                                                    ghb_settings_get_string(settings, "x264_direct"));
1557                 }
1558         }
1559         g_string_append_printf(x264opts, "merange=%d:", 
1560                                                    ghb_settings_get_int(settings, "x264_merange"));
1561
1562         const gchar *opts = ghb_settings_get_string(settings, "x264_options");
1563         if (opts != NULL && opts[0] != 0)
1564         {
1565                 g_string_append_printf(x264opts, "%s:", opts);
1566         }
1567         // strip the trailing ":"
1568         gchar *result;
1569         gint len;
1570         result = g_string_free(x264opts, FALSE);
1571         len = strlen(result);
1572         if (len > 0) result[len - 1] = 0;
1573         return result;
1574 }
1575
1576 gchar **
1577 ghb_get_chapters(gint titleindex)
1578 {
1579         hb_list_t  * list;
1580         hb_title_t * title;
1581     hb_chapter_t * chapter;
1582         gint count, ii;
1583         gchar **result = NULL;
1584         
1585         g_debug("ghb_get_chapters (title = %d)\n", titleindex);
1586         if (h == NULL) return NULL;
1587         list = hb_get_titles( h );
1588     title = (hb_title_t*)hb_list_item( list, titleindex );
1589         if (title == NULL) return NULL;
1590         count = hb_list_count( title->list_chapter );
1591         result = g_malloc((count+1) * sizeof(gchar*));
1592         for (ii = 0; ii < count; ii++)
1593         {
1594                 chapter = hb_list_item(title->list_chapter, ii);
1595                 if (chapter == NULL) break;
1596                 if (chapter->title == NULL || chapter->title[0] == 0)
1597                         result[ii] = g_strdup_printf ("Chapter %2d", ii);
1598                 else 
1599                         result[ii] = g_strdup(chapter->title);
1600         }
1601         result[ii] = NULL;
1602         return result;
1603 }
1604
1605 gboolean
1606 ghb_set_passthru_rate_opts(GtkBuilder *builder, gint bitrate)
1607 {
1608         gboolean changed = FALSE;
1609         changed = audio_rate_opts_add(builder, "audio_bitrate", bitrate);
1610         return changed;
1611 }
1612
1613 gboolean
1614 ghb_set_default_rate_opts(GtkBuilder *builder)
1615 {
1616         gboolean changed = FALSE;
1617         changed = audio_bitrate_opts_clean(builder, "audio_bitrate", hb_audio_bitrates, hb_audio_bitrates_count);
1618         return changed;
1619 }
1620
1621 void
1622 ghb_backend_init(GtkBuilder *builder, gint debug, gint update)
1623 {
1624     /* Init libhb */
1625     h = hb_init( debug, update );
1626         // Set up the list model for the combos
1627         init_ui_combo_boxes(builder);
1628         // Populate all the combos
1629         ghb_update_ui_combo_box(builder, NULL, 0, TRUE);
1630 }
1631
1632 void
1633 ghb_backend_scan(const gchar *path, gint titleindex)
1634 {
1635     hb_scan( h, path, titleindex );
1636 }
1637
1638 gint 
1639 ghb_backend_events(signal_user_data_t *ud, gint *unique_id)
1640 {
1641     hb_state_t s;
1642         gchar * status;
1643         GtkProgressBar *progress;
1644         
1645         if (h == NULL) return GHB_EVENT_NONE;
1646     hb_get_state( h, &s );
1647         *unique_id = s.param.working.sequence_id & 0xFFFFFF;
1648         progress = GTK_PROGRESS_BAR(GHB_WIDGET (ud->builder, "progressbar"));
1649         switch( s.state )
1650     {
1651         case HB_STATE_IDLE:
1652             /* Nothing to do */
1653                         //fprintf( stderr, "HB Idle\n");
1654             break;
1655
1656 #define p s.param.scanning
1657         case HB_STATE_SCANNING:
1658                 {
1659                         status = g_strdup_printf ("Scanning title %d of %d...", 
1660                                                                           p.title_cur, p.title_count );
1661                         gtk_progress_bar_set_text (progress, status);
1662                         g_free(status);
1663                         gtk_progress_bar_set_fraction (progress, (gdouble)p.title_cur / p.title_count);
1664             /* Show what title is currently being scanned */
1665                 } break;
1666 #undef p
1667
1668         case HB_STATE_SCANDONE:
1669         {
1670                         status = g_strdup_printf ("Scan done"); 
1671                         gtk_progress_bar_set_text (progress, status);
1672                         g_free(status);
1673                         gtk_progress_bar_set_fraction (progress, 1.0);
1674                         return GHB_EVENT_SCAN_DONE;
1675             break;
1676         }
1677
1678 #define p s.param.working
1679         case HB_STATE_WORKING:
1680             if( p.seconds > -1 )
1681             {
1682                                 status= g_strdup_printf(
1683                                                 "Encoding: task %d of %d, %.2f %%"
1684                                                 " (%.2f fps, avg %.2f fps, ETA %02dh%02dm%02ds)",
1685                                                  p.job_cur, p.job_count, 100.0 * p.progress,
1686                                                  p.rate_cur, p.rate_avg, p.hours, p.minutes, p.seconds );
1687             }
1688                         else
1689                         {
1690                                 status= g_strdup_printf("Encoding: task %d of %d, %.2f %%",
1691                                                  p.job_cur, p.job_count, 100.0 * p.progress );
1692                         }
1693                         gtk_progress_bar_set_text (progress, status);
1694                         gtk_progress_bar_set_fraction (progress, p.progress);
1695                         g_free(status);
1696                         return GHB_EVENT_WORKING;
1697             break;
1698 #undef p
1699
1700         case HB_STATE_PAUSED:
1701                         status = g_strdup_printf ("Paused"); 
1702                         gtk_progress_bar_set_text (progress, status);
1703                         g_free(status);
1704                         return GHB_EVENT_PAUSED;
1705             break;
1706                                 
1707 #define p s.param.muxing
1708         case HB_STATE_MUXING:
1709         {
1710             gtk_progress_bar_set_text( progress, "Muxing: this may take awhile...");
1711             break;
1712         }
1713 #undef p
1714
1715 #define p s.param.workdone
1716         case HB_STATE_WORKDONE:
1717                 {
1718             hb_job_t *job;
1719
1720                         // Delete all remaining jobs of this encode.
1721                         // An encode can be composed of multiple associated jobs.
1722                         // When a job is stopped, libhb removes it from the job list,
1723                         // but does not remove other jobs that may be associated with it.
1724                         // Associated jobs are taged in the sequence id.
1725             while (((job = hb_job(h, 0)) != NULL) && ((job->sequence_id >> 24) != 0) ) 
1726                 hb_rem( h, job );
1727                                 
1728             switch( p.error )
1729             {
1730                 case HB_ERROR_NONE:
1731                     gtk_progress_bar_set_text( progress, "Rip done!" );
1732                     break;
1733                 case HB_ERROR_CANCELED:
1734                     gtk_progress_bar_set_text( progress, "Rip canceled." );
1735                                         return GHB_EVENT_WORK_CANCELED;
1736                     break;
1737                 default:
1738                     gtk_progress_bar_set_text( progress, "Rip failed.");
1739             }
1740                         gtk_progress_bar_set_fraction (progress, 1.0);
1741                         return GHB_EVENT_WORK_DONE;
1742             break;
1743                 }
1744 #undef p
1745     }
1746     return GHB_EVENT_NONE;
1747 }
1748
1749 gboolean
1750 ghb_get_title_info(ghb_title_info_t *tinfo, gint titleindex)
1751 {
1752         hb_list_t  * list;
1753         hb_title_t * title;
1754         
1755     if (h == NULL) return FALSE;
1756         list = hb_get_titles( h );
1757         if( !hb_list_count( list ) )
1758         {
1759                 /* No valid title, stop right there */
1760                 return FALSE;
1761         }
1762
1763     title = hb_list_item( list, titleindex );
1764         if (title == NULL) return FALSE;        // Bad titleindex
1765         tinfo->width = title->width;
1766         tinfo->height = title->height;
1767         memcpy(tinfo->crop, title->crop, 4 * sizeof(int));
1768         // Don't allow crop to 0
1769         if (title->crop[0] + title->crop[1] >= title->height)
1770                 title->crop[0] = title->crop[1] = 0;
1771         if (title->crop[2] + title->crop[3] >= title->width)
1772                 title->crop[2] = title->crop[3] = 0;
1773         tinfo->num_chapters = hb_list_count(title->list_chapter);
1774         tinfo->rate_base = title->rate_base;
1775         tinfo->rate = title->rate;
1776         hb_reduce(&(tinfo->aspect_n), &(tinfo->aspect_d), 
1777                                 title->width * title->pixel_aspect_width, 
1778                                 title->height * title->pixel_aspect_height);
1779         tinfo->hours = title->hours;
1780         tinfo->minutes = title->minutes;
1781         tinfo->seconds = title->seconds;
1782         return TRUE;
1783 }
1784
1785 gboolean
1786 ghb_get_audio_info(ghb_audio_info_t *ainfo, gint titleindex, gint audioindex)
1787 {
1788     hb_audio_config_t *audio;
1789         
1790         audio = get_hb_audio(titleindex, audioindex);
1791         if (audio == NULL) return FALSE; // Bad audioindex
1792         ainfo->codec = audio->in.codec;
1793         ainfo->bitrate = audio->in.bitrate;
1794         ainfo->samplerate = audio->in.samplerate;
1795         return TRUE;
1796 }
1797
1798 gboolean
1799 ghb_audio_is_passthru(gint acodec)
1800 {
1801         g_debug("ghb_audio_is_passthru () \n");
1802         g_debug("acodec %d\n", acodec);
1803         return (acodec == HB_ACODEC_AC3);
1804 }
1805
1806 gint
1807 ghb_get_default_acodec()
1808 {
1809         return HB_ACODEC_FAAC;
1810 }
1811
1812 void
1813 ghb_set_scale(signal_user_data_t *ud, gint mode)
1814 {
1815         hb_list_t  * list;
1816         hb_title_t * title;
1817         hb_job_t   * job;
1818         GHashTable *settings = ud->settings;
1819         gboolean keep_aspect, round_dims, anamorphic;
1820         gboolean autocrop, autoscale, noscale;
1821         gint crop[4], width, height, par_width, par_height;
1822         gint crop_width, crop_height;
1823         gint aspect_n, aspect_d;
1824         gboolean keep_width = (mode == GHB_SCALE_KEEP_WIDTH);
1825         gboolean keep_height = (mode == GHB_SCALE_KEEP_HEIGHT);
1826         gint step;
1827         GtkWidget *widget;
1828         gint modshift;
1829         gint modround;
1830         gint max_width = 0;
1831         gint max_height = 0;
1832         
1833         g_debug("ghb_set_scale ()\n");
1834
1835         if (h == NULL) return;
1836         list = hb_get_titles( h );
1837         if( !hb_list_count( list ) )
1838         {
1839                 /* No valid title, stop right there */
1840                 return;
1841         }
1842         gint titleindex = ghb_settings_get_index(settings, "title");
1843     title = hb_list_item( list, titleindex );
1844         if (title == NULL) return;
1845         job   = title->job;
1846         if (job == NULL) return;
1847         
1848         // First configure widgets
1849         round_dims = ghb_settings_get_bool(ud->settings, "round_dimensions");
1850         anamorphic = ghb_settings_get_bool(ud->settings, "anamorphic");
1851         keep_aspect = ghb_settings_get_bool(ud->settings, "keep_aspect");
1852         autocrop = ghb_settings_get_bool(ud->settings, "autocrop");
1853         autoscale = ghb_settings_get_bool(ud->settings, "autoscale");
1854         // "Noscale" is a flag that says we prefer to crop extra to satisfy
1855         // alignment constraints rather than scaling to satisfy them.
1856         noscale = ghb_settings_get_bool(ud->settings, "noscale");
1857         // Align dimensions to either 16 or 2 pixels
1858         // The scaler crashes if the dimensions are not divisible by 2
1859         // x264 also will not accept dims that are not multiple of 2
1860         modshift = round_dims ? 4 : 1;
1861         modround = round_dims ? 8 : 1;
1862         if (autoscale)
1863         {
1864                 keep_width = FALSE;
1865                 keep_height = FALSE;
1866         }
1867         if (anamorphic || keep_aspect)
1868         {
1869                 keep_height = FALSE;
1870         }
1871         // Step needs to be at least 2 because odd widths cause scaler crash
1872         step = round_dims ? 16 : 2;
1873         widget = GHB_WIDGET (ud->builder, "scale_width");
1874         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), step, 16);
1875         widget = GHB_WIDGET (ud->builder, "scale_height");
1876         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(widget), step, 16);
1877         if (autocrop)
1878         {
1879                 ghb_title_info_t tinfo;
1880
1881                 if (ghb_get_title_info (&tinfo, titleindex))
1882                 {
1883                         crop[0] = tinfo.crop[0];
1884                         crop[1] = tinfo.crop[1];
1885                         crop[2] = tinfo.crop[2];
1886                         crop[3] = tinfo.crop[3];
1887                         if (noscale)
1888                         {
1889                                 gint need1, need2;
1890
1891                                 // Adjust the cropping to accomplish the desired width and height
1892                                 crop_width = tinfo.width - crop[2] - crop[3];
1893                                 crop_height = tinfo.height - crop[0] - crop[1];
1894                                 width = (crop_width >> modshift) << modshift;
1895                                 height = (crop_height >> modshift) << modshift;
1896                                 need1 = (crop_height - height) / 2;
1897                                 need2 = crop_height - height - need1;
1898                                 crop[0] += need1;
1899                                 crop[1] += need2;
1900                                 need1 = (crop_width - width) / 2;
1901                                 need2 = crop_width - width - need1;
1902                                 crop[2] += need1;
1903                                 crop[3] += need2;
1904                         }
1905                         ghb_ui_update_int (ud, "crop_top", crop[0]);
1906                         ghb_ui_update_int (ud, "crop_bottom", crop[1]);
1907                         ghb_ui_update_int (ud, "crop_left", crop[2]);
1908                         ghb_ui_update_int (ud, "crop_right", crop[3]);
1909                 }
1910         }
1911         crop[0] = ghb_settings_get_int(ud->settings, "crop_top");
1912         crop[1] = ghb_settings_get_int(ud->settings, "crop_bottom");
1913         crop[2] = ghb_settings_get_int(ud->settings, "crop_left");
1914         crop[3] = ghb_settings_get_int(ud->settings, "crop_right");
1915         hb_reduce(&aspect_n, &aspect_d, 
1916                                 title->width * title->pixel_aspect_width, 
1917                                 title->height * title->pixel_aspect_height);
1918         crop_width = title->width - crop[2] - crop[3];
1919         crop_height = title->height - crop[0] - crop[1];
1920         if (autoscale)
1921         {
1922                 width = crop_width;
1923                 height = crop_height;
1924                 max_width = crop_width;
1925                 max_height = crop_height;
1926         }
1927         else
1928         {
1929                 width = ghb_settings_get_int(ud->settings, "scale_width");
1930                 height = ghb_settings_get_int(ud->settings, "scale_height");
1931                 max_width = ghb_settings_get_int(ud->settings, "max_width");
1932                 max_height = ghb_settings_get_int(ud->settings, "max_width");
1933                 // Align max dims 
1934                 max_width = (max_width >> modshift) << modshift;
1935                 max_height = (max_height >> modshift) << modshift;
1936                 // Adjust dims according to max values
1937                 if (!max_height)
1938                 {
1939                         max_height = crop_height;
1940                 }
1941                 if (!max_width)
1942                 {
1943                         max_width = crop_width;
1944                 }
1945                 height = MIN(height, max_height);
1946                 width = MIN(width, max_width);
1947                 g_debug("max_width %d, max_height %d\n", max_width, max_height);
1948         }
1949         if (width < 16)
1950                 width = title->width - crop[2] - crop[3];
1951         if (height < 16)
1952                 height = title->height - crop[0] - crop[1];
1953
1954         if (anamorphic)
1955         {
1956                 if (round_dims)
1957                 {
1958                         job->modulus = 0;
1959                 }
1960                 else
1961                 {
1962                         // The scaler crashes if the dimensions are not divisible by 2
1963                         // Align mod 2.  And so does something in x264_encoder_headers()
1964                         job->modulus = 2;
1965                 }
1966                 job->width = width;
1967                 if (max_height) 
1968                         job->maxHeight = max_height;
1969                 job->crop[0] = crop[0]; job->crop[1] = crop[1];
1970                 job->crop[2] = crop[2]; job->crop[3] = crop[3];
1971                 hb_set_anamorphic_size( job, &width, &height, &par_width, &par_height );
1972         }
1973         else 
1974         {
1975                 if (keep_aspect)
1976                 {
1977                         gdouble par;
1978                         gint new_width, new_height;
1979                         
1980                         g_debug("kw %s kh %s\n", keep_width ? "y":"n", keep_height ? "y":"n");
1981                         g_debug("w %d h %d\n", width, height);
1982                         // Compute pixel aspect ration.  
1983                         par = (gdouble)(title->height * aspect_n) / (title->width * aspect_d);
1984                         // Must scale so that par becomes 1:1
1985                         // Try to keep largest dimension
1986                         new_height = (crop_height * ((gdouble)width/crop_width) / par);
1987                         new_width = (crop_width * ((gdouble)height/crop_height) * par);
1988                         // Height and width are always multiples of 2, so do the rounding
1989                         new_height = ((new_height + 1) >> 1) << 1;
1990                         new_width = ((new_width + 1) >> 1) << 1;
1991                         g_debug("max w %d, new w %d\n", max_width, new_width);
1992                         if (max_width && (new_width > max_width))
1993                         {
1994                                 height = new_height;
1995                         }
1996                         else if (max_height && (new_height > max_height))
1997                         {
1998                                 width = new_width;
1999                         }
2000                         else if (keep_width)
2001                         {
2002                                 height = new_height;
2003                         }
2004                         else if (keep_height)
2005                         {
2006                                 width = new_width;
2007                         }
2008                         else if (width > new_width)
2009                         {
2010                                 height = new_height;
2011                         }
2012                         else
2013                         {
2014                                 width = new_width;
2015                         }
2016                         g_debug("new w %d h %d\n", width, height);
2017                 }
2018                 width = ((width + modround) >> modshift) << modshift;
2019                 height = ((height + modround) >> modshift) << modshift;
2020         }
2021         ghb_ui_update_int (ud, "scale_width", width);
2022         ghb_ui_update_int (ud, "scale_height", height);
2023 }
2024
2025 static void
2026 set_job_picture_settings(hb_job_t *job, GHashTable *settings)
2027 {
2028         job->crop[0] = ghb_settings_get_int(settings, "crop_top");
2029         job->crop[1] = ghb_settings_get_int(settings, "crop_bottom");
2030         job->crop[2] = ghb_settings_get_int(settings, "crop_left");
2031         job->crop[3] = ghb_settings_get_int(settings, "crop_right");
2032
2033         gboolean anamorphic = ghb_settings_get_bool(settings, "anamorphic");
2034         gboolean round_dimensions = ghb_settings_get_bool(settings, "round_dimensions");
2035         if (round_dimensions && anamorphic)
2036         {
2037                 job->modulus = 16;
2038                 job->pixel_ratio = 2;
2039         }
2040         else if (anamorphic)
2041         {
2042                 job->modulus = 2;
2043                 job->pixel_ratio = 2;
2044         }
2045         else
2046         {
2047                 job->modulus = 2;
2048                 job->pixel_ratio = 0;
2049         }
2050         job->width = ghb_settings_get_int(settings, "scale_width");
2051         job->height = ghb_settings_get_int(settings, "scale_height");
2052         gint deint = ghb_settings_get_int(settings, "deinterlace");
2053         job->deinterlace = (deint == 0) ? 0 : 1;
2054 }
2055
2056 gint
2057 ghb_calculate_target_bitrate(GHashTable *settings, gint titleindex)
2058 {
2059         hb_list_t  * list;
2060         hb_title_t * title;
2061         hb_job_t   * job;
2062         gint size;
2063
2064         if (h == NULL) return 2000;
2065         list = hb_get_titles( h );
2066     title = hb_list_item( list, titleindex );
2067         if (title == NULL) return 2000;
2068         job   = title->job;
2069         if (job == NULL) return 2000;
2070         size = ghb_settings_get_int(settings, "video_target_size");
2071         return hb_calc_bitrate( job, size );
2072 }
2073
2074 gint
2075 ghb_guess_bitrate(GHashTable *settings)
2076 {
2077         gint bitrate;
2078         if (ghb_settings_get_bool(settings, "vquality_type_constant"))
2079         {
2080                 // This is really rough.  I'm trying to err on the high
2081                 // side since this is used to estimate if there is 
2082                 // sufficient disk space left
2083                 gint vcodec = ghb_settings_get_int(settings, "video_codec");
2084                 gdouble vquality = ghb_settings_get_dbl(settings, "video_quality")/100;
2085                 if (vcodec == HB_VCODEC_X264 && 
2086                                 !ghb_settings_get_bool(settings, "linear_vquality"))
2087                 {
2088                         vquality = 51.0 - vquality * 51.0;
2089                         // Convert log curve to linear
2090                         vquality = exp2((vquality-12)/6);
2091                         // Don't let it go to 0
2092                         if (vquality >= 31) vquality = 30;
2093                         vquality = (31 - vquality) / 31;
2094                 }
2095                 // bitrate seems to be a log relasionship to quality
2096                 // with typical source material
2097                 // This is a real wag
2098                 bitrate = 20*1024*1024*exp10(vquality*14)/exp10(14);
2099                 // Add some bits for audio
2100                 bitrate += 500*1024;
2101         }
2102         else
2103         {
2104                 // Add some fudge to the bitrate to leave breathing room
2105                 bitrate = ghb_settings_get_int(settings, "video_bitrate")*1024;
2106                 // Add some bits for audio
2107                 bitrate += 500*1024;
2108         }
2109         return bitrate;
2110 }
2111
2112 gboolean
2113 ghb_validate_video(signal_user_data_t *ud)
2114 {
2115         GHashTable *settings = ud->settings;
2116         gint vcodec, mux;
2117         gchar *message;
2118
2119         mux = ghb_settings_get_int(settings, "container");
2120         vcodec = ghb_settings_get_int(settings, "video_codec");
2121         if ((mux == HB_MUX_MP4 || mux == HB_MUX_AVI) && 
2122                 (vcodec == HB_VCODEC_THEORA))
2123         {
2124                 // mp4|avi/theora combination is not supported.
2125                 message = g_strdup_printf(
2126                                         "Theora is not supported in the MP4 and AVI containers.\n\n"
2127                                         "You should choose a different video codec or container.\n"
2128                                         "If you continue, XviD will be chosen for you.");
2129                 if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2130                 {
2131                         g_free(message);
2132                         return FALSE;
2133                 }
2134                 g_free(message);
2135                 vcodec = HB_VCODEC_XVID;
2136                 ghb_ui_update_int(ud, "video_codec", vcodec);
2137         }
2138         return TRUE;
2139 }
2140
2141 gboolean
2142 ghb_validate_audio(signal_user_data_t *ud)
2143 {
2144         hb_list_t  * list;
2145         hb_title_t * title;
2146         GHashTable *settings = ud->settings;
2147         gchar *message;
2148         setting_value_t *value;
2149
2150         if (h == NULL) return FALSE;
2151         list = hb_get_titles( h );
2152         if( !hb_list_count( list ) )
2153         {
2154                 /* No valid title, stop right there */
2155                 g_message("No title found.\n");
2156                 return FALSE;
2157         }
2158
2159         gint titleindex = ghb_settings_get_index(settings, "title");
2160     title = hb_list_item( list, titleindex );
2161         if (title == NULL) return FALSE;
2162         GSList *link = ud->audio_settings;
2163         gint mux = ghb_settings_get_int(settings, "container");
2164         while (link != NULL)
2165         {
2166                 GHashTable *asettings;
2167             hb_audio_config_t *taudio;
2168
2169                 asettings = (GHashTable*)link->data;
2170                 gint track = ghb_settings_get_index(asettings, "audio_track");
2171                 gint codec = ghb_settings_get_int(asettings, "audio_codec");
2172         taudio = (hb_audio_config_t *) hb_list_audio_config_item( title->list_audio, track );
2173                 if ((taudio->in.codec != HB_ACODEC_AC3) && (codec == HB_ACODEC_AC3))
2174                 {
2175                         // Not supported.  AC3 is passthrough only, so input must be AC3
2176                         message = g_strdup_printf(
2177                                                 "The source does not support AC3 Pass-Thru.\n\n"
2178                                                 "You should choose a different audio codec.\n"
2179                                                 "If you continue, one will be chosen for you.");
2180                         if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2181                         {
2182                                 g_free(message);
2183                                 return FALSE;
2184                         }
2185                         g_free(message);
2186                         if (mux == HB_MUX_AVI)
2187                         {
2188                                 codec = HB_ACODEC_LAME;
2189                         }
2190                         else
2191                         {
2192                                 codec = HB_ACODEC_FAAC;
2193                         }
2194                         value = get_acodec_value(codec);
2195                         ghb_settings_set(asettings, "audio_codec", value);
2196                 }
2197                 gchar *a_unsup = NULL;
2198                 gchar *mux_s = NULL;
2199                 if (mux == HB_MUX_MP4)
2200                 { 
2201                         mux_s = "MP4";
2202                         // mp4/mp3|vorbis combination is not supported.
2203                         if (codec == HB_ACODEC_LAME)
2204                         {
2205                                 a_unsup = "MP3";
2206                                 codec = HB_ACODEC_FAAC;
2207                         }
2208                         if (codec == HB_ACODEC_VORBIS)
2209                         {
2210                                 a_unsup = "Vorbis";
2211                                 codec = HB_ACODEC_FAAC;
2212                         }
2213                 }
2214                 else if (mux == HB_MUX_AVI)
2215                 {
2216                         mux_s = "AVI";
2217                         // avi/faac|vorbis combination is not supported.
2218                         if (codec == HB_ACODEC_FAAC)
2219                         {
2220                                 a_unsup = "FAAC";
2221                                 codec = HB_ACODEC_LAME;
2222                         }
2223                         if (codec == HB_ACODEC_VORBIS)
2224                         {
2225                                 a_unsup = "Vorbis";
2226                                 codec = HB_ACODEC_LAME;
2227                         }
2228                 }
2229                 else if (mux == HB_MUX_OGM)
2230                 {
2231                         mux_s = "OGM";
2232                         // avi/faac|vorbis combination is not supported.
2233                         if (codec == HB_ACODEC_FAAC)
2234                         {
2235                                 a_unsup = "FAAC";
2236                                 codec = HB_ACODEC_VORBIS;
2237                         }
2238                         if (codec == HB_ACODEC_AC3)
2239                         {
2240                                 a_unsup = "AC-3";
2241                                 codec = HB_ACODEC_VORBIS;
2242                         }
2243                 }
2244                 if (a_unsup)
2245                 {
2246                         message = g_strdup_printf(
2247                                                 "%s is not supported in the %s container.\n\n"
2248                                                 "You should choose a different audio codec.\n"
2249                                                 "If you continue, one will be chosen for you.", a_unsup, mux_s);
2250                         if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2251                         {
2252                                 g_free(message);
2253                                 return FALSE;
2254                         }
2255                         g_free(message);
2256                         value = get_acodec_value(codec);
2257                         ghb_settings_set(asettings, "audio_codec", value);
2258                 }
2259                 gint mix = ghb_settings_get_int (asettings, "audio_mix");
2260                 gboolean allow_mono = TRUE;
2261                 gboolean allow_stereo = TRUE;
2262                 gboolean allow_dolby = TRUE;
2263                 gboolean allow_dpl2 = TRUE;
2264                 gboolean allow_6ch = TRUE;
2265                 allow_mono =
2266                         (taudio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
2267                         (codec != HB_ACODEC_LAME);
2268                 gint layout = taudio->in.channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
2269                 allow_stereo =
2270                         ((layout == HB_INPUT_CH_LAYOUT_MONO && !allow_mono) || layout >= HB_INPUT_CH_LAYOUT_STEREO);
2271                 allow_dolby =
2272                         (layout == HB_INPUT_CH_LAYOUT_3F1R) || 
2273                         (layout == HB_INPUT_CH_LAYOUT_3F2R) || 
2274                         (layout == HB_INPUT_CH_LAYOUT_DOLBY);
2275                 allow_dpl2 = (layout == HB_INPUT_CH_LAYOUT_3F2R);
2276                 allow_6ch =
2277                         (taudio->in.codec & (HB_ACODEC_AC3|HB_ACODEC_DCA)) &&
2278                         (codec != HB_ACODEC_LAME) &&
2279                         (layout == HB_INPUT_CH_LAYOUT_3F2R) && 
2280                         (taudio->in.channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE);
2281
2282                 gchar *mix_unsup = NULL;
2283                 if (mix == HB_AMIXDOWN_MONO && !allow_mono)
2284                 {
2285                         mix_unsup = "mono";
2286                 }
2287                 if (mix == HB_AMIXDOWN_STEREO && !allow_stereo)
2288                 {
2289                         mix_unsup = "stereo";
2290                 }
2291                 if (mix == HB_AMIXDOWN_DOLBY && !allow_dolby)
2292                 {
2293                         mix_unsup = "Dolby";
2294                 }
2295                 if (mix == HB_AMIXDOWN_DOLBYPLII && !allow_dpl2)
2296                 {
2297                         mix_unsup = "Dolby Pro Logic II";
2298                 }
2299                 if (mix == HB_AMIXDOWN_6CH && !allow_6ch)
2300                 {
2301                         mix_unsup = "6 Channel";
2302                 }
2303                 if (mix_unsup)
2304                 {
2305                         message = g_strdup_printf(
2306                                                 "The source audio does not support %s mixdown.\n\n"
2307                                                 "You should choose a different mixdown.\n"
2308                                                 "If you continue, one will be chosen for you.", mix_unsup);
2309                         if (!ghb_message_dialog(GTK_MESSAGE_WARNING, message, "Cancel", "Continue"))
2310                         {
2311                                 g_free(message);
2312                                 return FALSE;
2313                         }
2314                         g_free(message);
2315                         mix = ghb_get_best_mix(titleindex, track, codec, mix);
2316                         value = get_amix_value(mix);
2317                         ghb_settings_set(asettings, "audio_mix", value);
2318                 }
2319                 link = link->next;
2320         }
2321         return TRUE;
2322 }
2323
2324 gboolean
2325 ghb_validate_vquality(GHashTable *settings)
2326 {
2327         gint vcodec;
2328         gchar *message;
2329         gint min, max;
2330
2331         if (ghb_settings_get_bool(settings, "nocheckvquality")) return TRUE;
2332         vcodec = ghb_settings_get_int(settings, "video_codec");
2333         if (ghb_settings_get_bool(settings, "vquality_type_constant"))
2334         {
2335                 if (!ghb_settings_get_bool(settings, "directqp"))
2336                 {
2337                         if (vcodec != HB_VCODEC_X264 || 
2338                                 ghb_settings_get_bool(settings, "linear_vquality"))
2339                         {
2340                                 min = 68;
2341                                 max = 97;
2342                         }
2343                         else if (vcodec == HB_VCODEC_X264)
2344                         {
2345                                 min = 40;
2346                                 max = 70;
2347                         }
2348                 }
2349                 else
2350                 {
2351                         if (vcodec == HB_VCODEC_X264)
2352                         {
2353                                 min = 16;
2354                                 max = 30;
2355                         }
2356                         else if (vcodec == HB_VCODEC_FFMPEG)
2357                         {
2358                                 min = 1;
2359                                 max = 8;
2360                         }
2361                         else
2362                         {
2363                                 min = 68;
2364                                 max = 97;
2365                         }
2366                 }
2367                 gint vquality = ghb_settings_get_dbl(settings, "video_quality");
2368                 if (vquality < min || vquality > max)
2369                 {
2370                         message = g_strdup_printf(
2371                                                 "Interesting video quality choise: %d\n\n"
2372                                                 "Typical values range from %d to %d.\n"
2373                                                 "Are you sure you wish to use this setting?",
2374                                                 vquality, min, max);
2375                         if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message, 
2376                                                                         "Cancel", "Continue"))
2377                         {
2378                                 g_free(message);
2379                                 return FALSE;
2380                         }
2381                         g_free(message);
2382                 }
2383         }
2384         return TRUE;
2385 }
2386
2387 void
2388 ghb_add_job(job_settings_t *js, gint unique_id)
2389 {
2390         hb_list_t  * list;
2391         hb_title_t * title;
2392         hb_job_t   * job;
2393         GHashTable *settings = js->settings;
2394         static gchar *x264opts;
2395         gint sub_id = 0;
2396
2397         g_debug("ghb_add_job()\n");
2398         if (h == NULL) return;
2399         list = hb_get_titles( h );
2400         if( !hb_list_count( list ) )
2401         {
2402                 /* No valid title, stop right there */
2403                 g_message("No title found.\n");
2404                 return;
2405         }
2406
2407         gint titleindex = ghb_settings_get_index(settings, "title");
2408     title = hb_list_item( list, titleindex );
2409         if (title == NULL) return;
2410
2411         /* Set job settings */
2412         job   = title->job;
2413         if (job == NULL) return;
2414
2415         job->mux = ghb_settings_get_int(settings, "container");
2416         if (job->mux == HB_MUX_MP4)
2417         {
2418                 job->largeFileSize = ghb_settings_get_bool(settings, "large_mp4");
2419                 job->mp4_optimize = ghb_settings_get_bool(settings, "http_optimize_mp4");
2420         }
2421         else
2422         {
2423                 job->largeFileSize = FALSE;
2424                 job->mp4_optimize = FALSE;
2425         }
2426         gint chapter_start, chapter_end;
2427         chapter_start = ghb_settings_get_int(settings, "start_chapter");
2428         chapter_end = ghb_settings_get_int(settings, "end_chapter");
2429         gint num_chapters = hb_list_count(title->list_chapter);
2430         job->chapter_start = MIN( num_chapters, chapter_start );
2431         job->chapter_end   = MAX( job->chapter_start, chapter_end );
2432
2433         job->chapter_markers = ghb_settings_get_bool(settings, "chapter_markers");
2434         if ( job->chapter_markers )
2435         {
2436                 gint chapter;
2437                 
2438                 for(chapter = chapter_start; chapter <= chapter_end; chapter++)
2439                 {
2440                         hb_chapter_t * chapter_s;
2441                         gchar *name;
2442                         
2443                         if (js->chapter_list == NULL || js->chapter_list[chapter-1] == 0)
2444                         {
2445                                 name = g_strdup_printf ("Chapter %2d", chapter);
2446                         }
2447                         else
2448                         {
2449                                 name = g_strdup(js->chapter_list[chapter-1]);
2450                         }
2451                         chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
2452                         strncpy(chapter_s->title, name, 1023);
2453                         chapter_s->title[1023] = '\0';
2454                         g_free(name);
2455                 }
2456         }
2457         job->crop[0] = ghb_settings_get_int(settings, "crop_top");
2458         job->crop[1] = ghb_settings_get_int(settings, "crop_bottom");
2459         job->crop[2] = ghb_settings_get_int(settings, "crop_left");
2460         job->crop[3] = ghb_settings_get_int(settings, "crop_right");
2461
2462         
2463         gboolean decomb = ghb_settings_get_bool(settings, "decomb");
2464         gint deint = ghb_settings_get_int(settings, "deinterlace");
2465         if (!decomb)
2466                 job->deinterlace = (deint == 0) ? 0 : 1;
2467         else
2468                 job->deinterlace = 0;
2469     job->grayscale   = ghb_settings_get_bool(settings, "grayscale");
2470
2471         gboolean anamorphic = ghb_settings_get_bool(settings, "anamorphic");
2472         gboolean round_dimensions = ghb_settings_get_bool(settings, "round_dimensions");
2473         if (round_dimensions && anamorphic)
2474         {
2475                 job->pixel_ratio = 2;
2476                 job->modulus = 16;
2477         }
2478         else if (anamorphic)
2479         {
2480                 // Huh! I thought I wanted to use pixel_ratio 1 for this case, but
2481                 // when its 1, libhb discards the width and height and uses original
2482                 // title dims - crop.  Thats not what I want.
2483                 // Also, x264 requires things to divisible by 2.
2484                 job->pixel_ratio = 2;
2485                 job->modulus = 2;
2486         }
2487         else
2488         {
2489                 job->pixel_ratio = 0;
2490                 job->modulus = 2;
2491         }
2492         job->vfr = ghb_settings_get_bool(settings, "variable_frame_rate");
2493         /* Add selected filters */
2494         job->filters = hb_list_init();
2495         if( ghb_settings_get_bool(settings, "detelecine" ) || job->vfr )
2496         {
2497                 hb_filter_detelecine.settings = NULL;
2498                 hb_list_add( job->filters, &hb_filter_detelecine );
2499         }
2500         if( decomb )
2501         {
2502                 // Use default settings
2503                 hb_filter_decomb.settings = NULL;
2504                 hb_filter_decomb.settings = "1:2:6:9:40:16:16";
2505                 hb_list_add( job->filters, &hb_filter_decomb );
2506         }
2507         if( job->deinterlace )
2508         {
2509                 hb_filter_deinterlace.settings = (gchar*)ghb_settings_get_string(settings, "deinterlace");
2510                 hb_list_add( job->filters, &hb_filter_deinterlace );
2511         }
2512         if( ghb_settings_get_bool(settings, "deblock") )
2513         {
2514                 hb_filter_deblock.settings = NULL;
2515                 hb_list_add( job->filters, &hb_filter_deblock );
2516         }
2517         gint denoise = ghb_settings_get_int(settings, "denoise");
2518         if( denoise != 0 )
2519         {
2520                 hb_filter_denoise.settings = (gchar*)ghb_settings_get_string(settings, "denoise");
2521                 hb_list_add( job->filters, &hb_filter_denoise );
2522         }
2523         job->width = ghb_settings_get_int(settings, "scale_width");
2524         job->height = ghb_settings_get_int(settings, "scale_height");
2525
2526         job->vcodec = ghb_settings_get_int(settings, "video_codec");
2527         if ((job->mux == HB_MUX_MP4 || job->mux == HB_MUX_AVI) && 
2528                 (job->vcodec == HB_VCODEC_THEORA))
2529         {
2530                 // mp4|avi/theora combination is not supported.
2531                 job->vcodec = HB_VCODEC_XVID;
2532         }
2533         if ((job->vcodec == HB_VCODEC_X264) && (job->mux == HB_MUX_MP4))
2534         {
2535                 job->ipod_atom = ghb_settings_get_bool(settings, "ipod_file");
2536         }
2537         if (ghb_settings_get_bool(settings, "vquality_type_constant"))
2538         {
2539                 gdouble vquality = ghb_settings_get_dbl(settings, "video_quality");
2540                 if (!ghb_settings_get_bool(settings, "directqp"))
2541                 {
2542                         vquality /= 100.0;
2543                         if (ghb_settings_get_bool(settings, "linear_vquality"))
2544                         {
2545                                 if (job->vcodec == HB_VCODEC_X264)
2546                                 {
2547                                         // Adjust to same range as xvid and ffmpeg
2548                                         vquality = 31.0 - vquality * 31.0;
2549                                         if (vquality > 0)
2550                                         {
2551                                                 // Convert linear to log curve
2552                                                 vquality = 12 + 6 * log2(vquality);
2553                                                 if (vquality > 51) vquality = 51;
2554                                                 if (vquality < 1) vquality = 1;
2555                                         }
2556                                         else
2557                                                 vquality = 0;
2558                                 }
2559                         }
2560                         else
2561                         {
2562                                 if (vquality == 0.0) vquality = 0.01;
2563                                 if (vquality == 1.0) vquality = 0.0;
2564                         }
2565                 }
2566                 job->vquality =  vquality;
2567                 job->vbitrate = 0;
2568         }
2569         else if (ghb_settings_get_bool(settings, "vquality_type_bitrate"))
2570         {
2571                 job->vquality = -1.0;
2572                 job->vbitrate = ghb_settings_get_int(settings, "video_bitrate");
2573         }
2574         // AVI container does not support variable frame rate.
2575         if (job->mux == HB_MUX_AVI)
2576         {
2577                 job->vfr = FALSE;
2578         }
2579
2580         gint vrate = ghb_settings_get_int(settings, "framerate");
2581         if( vrate == 0 || job->vfr )
2582         {
2583                 job->vrate = title->rate;
2584                 job->vrate_base = title->rate_base;
2585                 job->cfr = 0;
2586         }
2587         else
2588         {
2589                 job->vrate = 27000000;
2590                 job->vrate_base = vrate;
2591                 job->cfr = 1;
2592         }
2593         // First remove any audios that are already in the list
2594         // This happens if you are encoding the same title a second time.
2595         gint num_audio_tracks = hb_list_count(job->list_audio);
2596         gint ii;
2597     for(ii = 0; ii < num_audio_tracks; ii++)
2598     {
2599         hb_audio_t *audio = (hb_audio_t*)hb_list_item(job->list_audio, 0);
2600         hb_list_rem(job->list_audio, audio);
2601     }
2602         GSList *link = js->audio_settings;
2603         gint count = 0;
2604         while (link != NULL)
2605         {
2606                 GHashTable *asettings;
2607             hb_audio_config_t audio;
2608             hb_audio_config_t *taudio;
2609
2610                 hb_audio_config_init(&audio);
2611                 asettings = (GHashTable*)link->data;
2612                 audio.in.track = ghb_settings_get_index(asettings, "audio_track");
2613                 audio.out.track = count;
2614                 audio.out.codec = ghb_settings_get_int(asettings, "audio_codec");
2615         taudio = (hb_audio_config_t *) hb_list_audio_config_item( title->list_audio, audio.in.track );
2616                 if ((taudio->in.codec != HB_ACODEC_AC3) && (audio.out.codec == HB_ACODEC_AC3))
2617                 {
2618                         // Not supported.  AC3 is passthrough only, so input must be AC3
2619                         if (job->mux == HB_MUX_AVI)
2620                         {
2621                                 audio.out.codec = HB_ACODEC_LAME;
2622                         }
2623                         else
2624                         {
2625                                 audio.out.codec = HB_ACODEC_FAAC;
2626                         }
2627                 }
2628                 if ((job->mux == HB_MUX_MP4) && 
2629                         ((audio.out.codec == HB_ACODEC_LAME) ||
2630                         (audio.out.codec == HB_ACODEC_VORBIS)))
2631                 {
2632                         // mp4/mp3|vorbis combination is not supported.
2633                         audio.out.codec = HB_ACODEC_FAAC;
2634                 }
2635                 if ((job->mux == HB_MUX_AVI) && 
2636                         ((audio.out.codec == HB_ACODEC_FAAC) ||
2637                         (audio.out.codec == HB_ACODEC_VORBIS)))
2638                 {
2639                         // avi/faac|vorbis combination is not supported.
2640                         audio.out.codec = HB_ACODEC_LAME;
2641                 }
2642                 if ((job->mux == HB_MUX_OGM) && 
2643                         ((audio.out.codec == HB_ACODEC_FAAC) ||
2644                         (audio.out.codec == HB_ACODEC_AC3)))
2645                 {
2646                         // ogm/faac|ac3 combination is not supported.
2647                         audio.out.codec = HB_ACODEC_VORBIS;
2648                 }
2649         audio.out.dynamic_range_compression = ghb_settings_get_dbl(asettings, "audio_drc");
2650                 // It would be better if this were done in libhb for us, but its not yet.
2651                 if (audio.out.codec == HB_ACODEC_AC3 || audio.out.codec == HB_ACODEC_DCA)
2652                 {
2653                         audio.out.mixdown = 0;
2654                 }
2655                 else
2656                 {
2657                         audio.out.mixdown = ghb_settings_get_int (asettings, "audio_mix");
2658                         // Make sure the mixdown is valid and pick a new one if not.
2659                         audio.out.mixdown = ghb_get_best_mix(titleindex, audio.in.track, audio.out.codec, 
2660                                                                                                 audio.out.mixdown);
2661                         audio.out.bitrate = ghb_settings_get_int(asettings, "audio_bitrate") / 1000;
2662                         gint srate = ghb_settings_get_int(asettings, "audio_sample_rate");
2663                         if (srate == 0) // 0 is same as source
2664                                 audio.out.samplerate = taudio->in.samplerate;
2665                         else
2666                                 audio.out.samplerate = srate;
2667                 }
2668
2669                 // Add it to the jobs audio list
2670         hb_audio_add( job, &audio );
2671                 count++;
2672                 link = link->next;
2673         }
2674         // I was tempted to move this up with the reset of the video quality
2675         // settings, but I suspect the settings above need to be made
2676         // first in order for hb_calc_bitrate to be accurate.
2677         if (ghb_settings_get_bool(settings, "vquality_type_target"))
2678         {
2679                 gint size;
2680                 
2681                 size = ghb_settings_get_int(settings, "video_target_size");
2682         job->vbitrate = hb_calc_bitrate( job, size );
2683                 job->vquality = -1.0;
2684         }
2685
2686         job->file = ghb_settings_get_string(settings, "destination");
2687         job->crf = ghb_settings_get_bool(settings, "constant_rate_factor");
2688         // TODO: libhb holds onto a reference to the x264opts and is not
2689         // finished with it until encoding the job is done.  But I can't
2690         // find a way to get at the job before it is removed in order to
2691         // free up the memory I am allocating here.
2692         // The short story is THIS LEAKS.
2693         x264opts = ghb_build_x264opts_string(settings);
2694         
2695         if( x264opts != NULL && *x264opts != '\0' )
2696         {
2697                 job->x264opts = x264opts;
2698         }
2699         else /*avoids a bus error crash when options aren't specified*/
2700         {
2701                 job->x264opts =  NULL;
2702         }
2703         gint subtitle = ghb_settings_get_int(settings, "subtitle_lang");
2704         gboolean forced_subtitles = ghb_settings_get_bool (settings, "forced_subtitles");
2705         job->subtitle_force = forced_subtitles;
2706         if (subtitle >= 0)
2707                 job->subtitle = subtitle;
2708         else
2709                 job->subtitle = -1;
2710         if (subtitle == -1)
2711         {
2712                 // Subtitle scan. Look for subtitle matching audio language
2713                 char *x264opts_tmp;
2714
2715                 /*
2716                  * When subtitle scan is enabled do a fast pre-scan job
2717                  * which will determine which subtitles to enable, if any.
2718                  */
2719                 job->pass = -1;
2720                 job->indepth_scan = 1;
2721
2722                 x264opts_tmp = job->x264opts;
2723                 job->x264opts = NULL;
2724
2725                 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
2726                 *(job->select_subtitle) = NULL;
2727
2728                 /*
2729                  * Add the pre-scan job
2730                  */
2731                 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2732                 hb_add( h, job );
2733                 //if (job->x264opts != NULL)
2734                 //      g_free(job->x264opts);
2735
2736                 job->x264opts = x264opts_tmp;
2737         }
2738         else
2739         {
2740                 job->select_subtitle = NULL;
2741         }
2742         if( ghb_settings_get_bool(settings, "two_pass") &&
2743                 !ghb_settings_get_bool(settings, "vquality_type_constant"))
2744         {
2745                 /*
2746                  * If subtitle_scan is enabled then only turn it on
2747                  * for the second pass and then off again for the
2748                  * second.
2749                  */
2750                 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
2751                 job->select_subtitle = NULL;
2752                 job->pass = 1;
2753                 job->indepth_scan = 0;
2754                 gchar *x264opts2 = NULL;
2755                 if (x264opts)
2756                 {
2757                         x264opts2 = g_strdup(x264opts);
2758                 }
2759                 /*
2760                  * If turbo options have been selected then append them
2761                  * to the x264opts now (size includes one ':' and the '\0')
2762                  */
2763                 if( ghb_settings_get_bool(settings, "turbo") )
2764                 {
2765                         char *tmp_x264opts;
2766
2767                         if ( x264opts )
2768                         {
2769                                 tmp_x264opts = g_strdup_printf("%s:%s", x264opts, turbo_opts);
2770                                 g_free(x264opts);
2771                         } 
2772                         else 
2773                         {
2774                                 /*
2775                                  * No x264opts to modify, but apply the turbo options
2776                                  * anyway as they may be modifying defaults
2777                                  */
2778                                 tmp_x264opts = g_strdup_printf("%s", turbo_opts);
2779                         }
2780                         x264opts = tmp_x264opts;
2781
2782                         job->x264opts = x264opts;
2783                 }
2784                 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2785                 hb_add( h, job );
2786                 //if (job->x264opts != NULL)
2787                 //      g_free(job->x264opts);
2788
2789                 job->select_subtitle = subtitle_tmp;
2790                 job->pass = 2;
2791                 /*
2792                  * On the second pass we turn off subtitle scan so that we
2793                  * can actually encode using any subtitles that were auto
2794                  * selected in the first pass (using the whacky select-subtitle
2795                  * attribute of the job).
2796                  */
2797                 job->indepth_scan = 0;
2798                 job->x264opts = x264opts2;
2799                 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2800                 hb_add( h, job );
2801                 //if (job->x264opts != NULL)
2802                 //      g_free(job->x264opts);
2803         }
2804         else
2805         {
2806                 job->indepth_scan = 0;
2807                 job->pass = 0;
2808                 job->sequence_id = (unique_id & 0xFFFFFF) | (sub_id++ << 24);
2809                 hb_add( h, job );
2810                 //if (job->x264opts != NULL)
2811                 //      g_free(job->x264opts);
2812         }
2813 }
2814
2815 void
2816 ghb_remove_job(gint unique_id)
2817 {
2818     hb_job_t * job;
2819     gint ii;
2820         
2821         // Multiples passes all get the same id
2822         // remove them all.
2823         // Go backwards through list, so reordering doesn't screw me.
2824         ii = hb_count(h) - 1;
2825     while ((job = hb_job(h, ii--)) != NULL)
2826     {
2827         if ((job->sequence_id & 0xFFFFFF) == unique_id)
2828                         hb_rem(h, job);
2829     }
2830 }
2831
2832 void
2833 ghb_start_queue()
2834 {
2835         hb_start( h );
2836 }
2837
2838 void
2839 ghb_stop_queue()
2840 {
2841         hb_stop( h );
2842 }
2843
2844 void
2845 ghb_pause_queue()
2846 {
2847     hb_state_t s;
2848     hb_get_state2( h, &s );
2849
2850     if( s.state == HB_STATE_PAUSED )
2851     {
2852         hb_resume( h );
2853     }
2854     else
2855     {
2856         hb_pause( h );
2857     }
2858 }
2859
2860 GdkPixbuf*
2861 ghb_get_preview_image(gint titleindex, gint index, GHashTable *settings, gboolean borders)
2862 {
2863         hb_title_t *title;
2864         hb_list_t  *list;
2865         
2866         list = hb_get_titles( h );
2867         if( !hb_list_count( list ) )
2868         {
2869                 /* No valid title, stop right there */
2870                 return NULL;
2871         }
2872     title = hb_list_item( list, titleindex );
2873         if (title == NULL) return NULL;
2874         if (title->job == NULL) return NULL;
2875         set_job_picture_settings(title->job, settings);
2876
2877         // hb_get_preview can't handle sizes that are larger than the original title
2878         // dimensions
2879         if (title->job->width > title->width)
2880                 title->job->width = title->width;
2881         
2882         if (title->job->height > title->height)
2883                 title->job->height = title->height;
2884         // And also creates artifacts if the width is not a multiple of 8
2885         //title->job->width = ((title->job->width + 4) >> 3) << 3;
2886         // And the height must be a multiple of 2
2887         //title->job->height = ((title->job->height + 1) >> 1) << 1;
2888         
2889         // Make sure we have a big enough buffer to receive the image from libhb. libhb
2890         // creates images with a one-pixel border around the original content. Hence we
2891         // add 2 pixels horizontally and vertically to the buffer size.
2892         gint srcWidth = title->width + 2;
2893         gint srcHeight= title->height + 2;
2894         gint dstWidth = title->width;
2895         gint dstHeight= title->height;
2896         gint borderTop = 1;
2897         gint borderLeft = 1;
2898     if (borders)
2899     {
2900         //     |<---------- title->width ----------->|
2901         //     |   |<---- title->job->width ---->|   |
2902         //     |   |                             |   |
2903         //     .......................................
2904         //     ....+-----------------------------+....
2905         //     ....|                             |....<-- gray border
2906         //     ....|                             |....
2907         //     ....|                             |....
2908         //     ....|                             |<------- image
2909         //     ....|                             |....
2910         //     ....|                             |....
2911         //     ....|                             |....
2912         //     ....|                             |....
2913         //     ....|                             |....
2914         //     ....+-----------------------------+....
2915         //     .......................................
2916                 dstWidth = title->job->width;
2917         dstHeight = title->job->height;
2918                 borderTop = (srcHeight - dstHeight) / 2;
2919                 borderLeft = (srcWidth - dstWidth) / 2;
2920                 g_debug("boarders removed\n");
2921         }
2922
2923         g_debug("src %d x %d\n", srcWidth, srcHeight);
2924         g_debug("dst %d x %d\n", dstWidth, dstHeight);
2925         g_debug("job dim %d x %d\n", title->job->width, title->job->height);
2926         g_debug("title crop %d:%d:%d:%d\n", 
2927                         title->crop[0],
2928                         title->crop[1],
2929                         title->crop[2],
2930                         title->crop[3]);
2931         g_debug("job crop %d:%d:%d:%d\n", 
2932                         title->job->crop[0],
2933                         title->job->crop[1],
2934                         title->job->crop[2],
2935                         title->job->crop[3]);
2936         static guint8 *buffer = NULL;
2937         static gint bufferSize = 0;
2938
2939         gint newSize;
2940         newSize = srcWidth * srcHeight * 4;
2941         if( bufferSize < newSize )
2942         {
2943                 bufferSize = newSize;
2944                 buffer     = (guint8*) g_realloc( buffer, bufferSize );
2945         }
2946         hb_get_preview( h, title, index, buffer );
2947
2948         // Create an GdkPixbuf and copy the libhb image into it, converting it from
2949         // libhb's format something suitable. Along the way, we'll strip off the
2950         // border around libhb's image.
2951         
2952         // The image data returned by hb_get_preview is 4 bytes per pixel, BGRA format.
2953         // Alpha is ignored.
2954
2955         GdkPixbuf *preview = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, dstWidth, dstHeight);
2956         guint8 *pixels = gdk_pixbuf_get_pixels (preview);
2957         
2958         guint32 *src = (guint32*)buffer;
2959         guint8 *dst = pixels;
2960         src += borderTop * srcWidth;    // skip top rows in src to get to first row of dst
2961         src += borderLeft;              // skip left pixels in src to get to first pixel of dst
2962         gint ii, jj;
2963         gint channels = gdk_pixbuf_get_n_channels (preview);
2964         gint stride = gdk_pixbuf_get_rowstride (preview);
2965         guint8 *tmp;
2966         for (ii = 0; ii < dstHeight; ii++)
2967         {
2968                 tmp = dst;
2969                 for (jj = 0; jj < dstWidth; jj++)
2970                 {
2971                         tmp[0] = src[0] >> 16;
2972                         tmp[1] = src[0] >> 8;
2973                         tmp[2] = src[0] >> 0;
2974                         tmp += channels;
2975                         src++;
2976                 }
2977                 dst += stride;
2978                 src += (srcWidth - dstWidth);   // skip to next row in src
2979         }
2980         // Got it, but hb_get_preview doesn't compensate for anamorphic, so lets
2981         // scale
2982         gint width, height, par_width, par_height;
2983         gboolean anamorphic = ghb_settings_get_bool (settings, "anamorphic");
2984         if (anamorphic)
2985         {
2986                 hb_set_anamorphic_size( title->job, &width, &height, &par_width, &par_height );
2987                 if (par_width > par_height)
2988                         dstWidth = dstWidth * par_width / par_height;
2989                 else
2990                         dstHeight = dstHeight * par_height / par_width;
2991         }
2992         
2993         g_debug("scaled %d x %d\n", dstWidth, dstHeight);
2994         GdkPixbuf *scaled_preview;
2995         scaled_preview = gdk_pixbuf_scale_simple(preview, dstWidth, dstHeight, GDK_INTERP_HYPER);
2996         g_object_unref (preview);
2997         return scaled_preview;
2998 }
2999
3000 static void
3001 sanitize_volname(gchar *name)
3002 {
3003         gchar *a, *b;
3004
3005         a = b = name;
3006         while (*b)
3007         {
3008                 switch(*b)
3009                 {
3010                 case '<':
3011                         b++;
3012                         break;
3013                 case '>':
3014                         b++;
3015                         break;
3016                 default:
3017                         *a = *b;
3018                         a++; b++;
3019                         break;
3020                 }
3021         }
3022         *a = 0;
3023 }
3024
3025 gchar*
3026 ghb_dvd_volname(const gchar *device)
3027 {
3028         gchar *name;
3029         name = hb_dvd_name((gchar*)device);
3030         if (name != NULL)
3031         {
3032                 sanitize_volname(name);
3033                 return g_strdup(name);
3034         }
3035         return name;
3036 }