+ g_free(str);
+ return result;
+}
+
+static gint
+lookup_audio_bitrate_int(const GValue *rate)
+{
+ gint ii;
+ gchar *str;
+ gint result = 0;
+
+ // Coincidentally, the string "source" will return 0
+ // which is our flag to use "same as source"
+ str = ghb_value_string(rate);
+ for (ii = 0; ii < hb_audio_bitrates_count; ii++)
+ {
+ if (strcmp(hb_audio_bitrates[ii].string, str) == 0)
+ {
+ result = hb_audio_bitrates[ii].rate;
+ break;
+ }
+ }
+ g_free(str);
+ return result;
+}
+
+static const gchar*
+lookup_audio_bitrate_option(const GValue *rate)
+{
+ gint ii;
+ gchar *str;
+ const gchar *result = "Same as source";
+
+ // Coincidentally, the string "source" will return 0
+ // which is our flag to use "same as source"
+ str = ghb_value_string(rate);
+ for (ii = 0; ii < hb_audio_bitrates_count; ii++)
+ {
+ if (strcmp(hb_audio_bitrates[ii].string, str) == 0)
+ {
+ result = hb_audio_bitrates[ii].string;
+ break;
+ }
+ }
+ g_free(str);
+ return result;
+}
+
+static gint
+lookup_audio_lang_int(const GValue *rate)
+{
+ gint ii;
+ gchar *str;
+ gint result = 0;
+
+ // Coincidentally, the string "source" will return 0
+ // which is our flag to use "same as source"
+ str = ghb_value_string(rate);
+ for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
+ {
+ if (strcmp(ghb_language_table[ii].iso639_2, str) == 0)
+ {
+ result = ii;
+ break;
+ }
+ }
+ g_free(str);
+ return result;
+}
+
+static const gchar*
+lookup_audio_lang_option(const GValue *rate)
+{
+ gint ii;
+ gchar *str;
+ const gchar *result = "Same as source";
+
+ // Coincidentally, the string "source" will return 0
+ // which is our flag to use "same as source"
+ str = ghb_value_string(rate);
+ for (ii = 0; ii < LANG_TABLE_SIZE; ii++)
+ {
+ if (strcmp(ghb_language_table[ii].iso639_2, str) == 0)
+ {
+ if (ghb_language_table[ii].native_name[0] != 0)
+ result = ghb_language_table[ii].native_name;
+ else
+ result = ghb_language_table[ii].eng_name;
+ break;
+ }
+ }
+ g_free(str);
+ return result;
+}
+
+static GValue*
+get_acodec_value(gint val)
+{
+ GValue *value = NULL;
+ gint ii;
+
+ for (ii = 0; ii < acodec_opts.count; ii++)
+ {
+ if ((int)acodec_opts.map[ii].ivalue == val)
+ {
+ value = ghb_string_value_new(acodec_opts.map[ii].shortOpt);
+ break;
+ }
+ }
+ return value;
+}
+
+static GValue*
+get_amix_value(gint val)
+{
+ GValue *value = NULL;
+ gint ii;
+
+ for (ii = 0; ii < hb_audio_mixdowns_count; ii++)
+ {
+ if (hb_audio_mixdowns[ii].amixdown == val)
+ {
+ value = ghb_string_value_new(hb_audio_mixdowns[ii].short_name);
+ break;
+ }
+ }
+ return value;
+}
+
+// Handle for libhb. Gets set by ghb_backend_init()
+static hb_handle_t * h_scan = NULL;
+static hb_handle_t * h_queue = NULL;
+
+extern void hb_get_tempory_directory(hb_handle_t *h, char path[512]);
+
+gchar*
+ghb_get_tmp_dir()
+{
+ char dir[512];
+
+ hb_get_tempory_directory(h_scan, dir);
+ return g_strdup(dir);
+}
+
+void
+ghb_hb_cleanup(gboolean partial)
+{
+ char dir[512];
+
+ hb_get_tempory_directory(h_scan, dir);
+ del_tree(dir, !partial);
+}
+
+gint
+ghb_subtitle_track_source(signal_user_data_t *ud, gint track)
+{
+ gint titleindex;
+
+ if (track == -2)
+ return SRTSUB;
+ if (track < 0)
+ return VOBSUB;
+ titleindex = ghb_settings_combo_int(ud->settings, "title");
+ if (titleindex < 0)
+ return VOBSUB;
+
+ hb_list_t * list;
+ hb_title_t * title;
+ hb_subtitle_t * sub;
+
+ if (h_scan == NULL) return VOBSUB;
+ list = hb_get_titles( h_scan );
+ if( !hb_list_count( list ) )
+ {
+ /* No valid title, stop right there */
+ return VOBSUB;
+ }
+ title = hb_list_item( list, titleindex );
+ if (title == NULL) return VOBSUB; // Bad titleindex
+ sub = hb_list_item( title->list_subtitle, track);
+ if (sub != NULL)
+ return sub->source;
+ else
+ return VOBSUB;
+}
+
+const char*
+ghb_subtitle_track_source_name(signal_user_data_t *ud, gint track)
+{
+ gint titleindex;
+ const gchar * name = "Unknown";
+
+ if (track == -2)
+ {
+ name = "SRT";
+ goto done;
+ }
+ if (track == -1)
+ {
+ name = "Bitmap";
+ goto done;
+ }
+
+ titleindex = ghb_settings_combo_int(ud->settings, "title");
+ if (titleindex < 0)
+ goto done;
+
+ hb_list_t * list;
+ hb_title_t * title;
+ hb_subtitle_t * sub;
+
+ if (h_scan == NULL)
+ goto done;
+ list = hb_get_titles( h_scan );
+ if( !hb_list_count( list ) )
+ goto done;
+
+ title = hb_list_item( list, titleindex );
+ if (title == NULL)
+ goto done;
+
+ sub = hb_list_item( title->list_subtitle, track);
+ if (sub != NULL)
+ {
+ switch (sub->source)
+ {
+ case VOBSUB:
+ name = "Bitmap";
+ break;
+ case CC708SUB:
+ case CC608SUB:
+ name = "Text";
+ break;
+ case SRTSUB:
+ name = "SRT";
+ break;
+ default:
+ break;
+ }
+ }
+
+done:
+ return name;
+}
+
+gchar*
+ghb_subtitle_track_lang(signal_user_data_t *ud, gint track)
+{
+ gint titleindex;
+
+ titleindex = ghb_settings_combo_int(ud->settings, "title");
+ if (titleindex < 0)
+ goto fail;
+ if (track == -1)
+ return ghb_get_user_audio_lang(ud, titleindex, 0);
+ if (track < 0)
+ goto fail;
+
+ hb_list_t * list;
+ hb_title_t * title;
+ hb_subtitle_t * sub;
+
+ if (h_scan == NULL)
+ goto fail;
+
+ list = hb_get_titles( h_scan );
+ if( !hb_list_count( list ) )
+ {
+ /* No valid title, stop right there */
+ goto fail;
+ }
+ title = hb_list_item( list, titleindex );
+ if (title == NULL) // Bad titleindex
+ goto fail;
+ sub = hb_list_item( title->list_subtitle, track);
+ if (sub != NULL)
+ return g_strdup(sub->iso639_2);
+
+fail:
+ return g_strdup("und");
+}
+
+gint
+ghb_get_title_number(gint titleindex)
+{
+ hb_list_t * list;
+ hb_title_t * title;
+
+ if (h_scan == NULL) return 1;
+ list = hb_get_titles( h_scan );
+ if( !hb_list_count( list ) )
+ {
+ /* No valid title, stop right there */
+ return 1;
+ }
+ title = hb_list_item( list, titleindex );
+ if (title == NULL) return 1; // Bad titleindex
+ return title->index;
+}
+
+static hb_audio_config_t*
+get_hb_audio(gint titleindex, gint track)
+{
+ hb_list_t * list;
+ hb_title_t * title;
+ hb_audio_config_t *audio = NULL;
+
+ if (h_scan == NULL) return NULL;
+ list = hb_get_titles( h_scan );
+ if( !hb_list_count( list ) )
+ {
+ /* No valid title, stop right there */
+ return NULL;
+ }
+ title = hb_list_item( list, titleindex );
+ if (title == NULL) return NULL; // Bad titleindex
+ if (!hb_list_count(title->list_audio))