1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) John Stebbins 2008 <stebbins@stebbins>
6 * x264handler.c is free software.
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
18 #include "callbacks.h"
20 #include "hb-backend.h"
21 #include "x264handler.h"
23 static void x264_opt_update(signal_user_data_t *ud, GtkWidget *widget);
24 static gchar* sanitize_x264opts(signal_user_data_t *ud, const gchar *options);
26 // Flag needed to prevent x264 options processing from chasing its tail
27 static gboolean ignore_options_update = FALSE;
30 x264_widget_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
32 ghb_widget_to_setting(ud->settings, widget);
33 if (!ignore_options_update)
35 ignore_options_update = TRUE;
36 x264_opt_update(ud, widget);
37 ignore_options_update = FALSE;
39 ghb_check_dependency(ud, widget);
40 ghb_clear_presets_selection(ud);
44 x264_me_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
49 ghb_widget_to_setting(ud->settings, widget);
50 if (!ignore_options_update)
52 ignore_options_update = TRUE;
53 x264_opt_update(ud, widget);
54 ignore_options_update = FALSE;
56 ghb_check_dependency(ud, widget);
57 ghb_clear_presets_selection(ud);
58 widget = GHB_WIDGET(ud->builder, "x264_merange");
59 gval = ghb_settings_get_value(ud->settings, "x264_me");
60 me = ghb_lookup_combo_int("x264_me", gval);
64 gtk_spin_button_set_range(GTK_SPIN_BUTTON(widget), 4, 16);
69 gtk_spin_button_set_range(GTK_SPIN_BUTTON(widget), 4, 64);
74 x264_entry_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
76 g_debug("x264_entry_changed_cb ()");
77 if (!ignore_options_update)
82 textview = GTK_WIDGET(GHB_WIDGET(ud->builder, "x264Option"));
83 ghb_widget_to_setting(ud->settings, textview);
84 options = ghb_settings_get_string(ud->settings, "x264Option");
85 ignore_options_update = TRUE;
86 ghb_x264_parse_options(ud, options);
87 if (!GTK_WIDGET_HAS_FOCUS(textview))
91 sopts = sanitize_x264opts(ud, options);
92 ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
93 ghb_x264_parse_options(ud, sopts);
97 ignore_options_update = FALSE;
102 x264_focus_out_cb(GtkWidget *widget, GdkEventFocus *event,
103 signal_user_data_t *ud)
105 gchar *options, *sopts;
107 ghb_widget_to_setting(ud->settings, widget);
108 options = ghb_settings_get_string(ud->settings, "x264Option");
109 sopts = sanitize_x264opts(ud, options);
110 ignore_options_update = TRUE;
111 if (sopts != NULL && strcmp(sopts, options) != 0)
113 ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
114 ghb_x264_parse_options(ud, sopts);
118 ignore_options_update = FALSE;
130 struct x264_opt_map_s
139 static gchar *x264_ref_syns[] = {"ref", "frameref", NULL};
140 static gchar *x264_mixed_syns[] = {"mixed-refs", "mixed_refs", NULL};
141 static gchar *x264_bframes_syns[] = {"bframes", NULL};
142 static gchar *x264_direct_syns[] =
143 {"direct", "direct-pred", "direct_pred", NULL};
144 static gchar *x264_weightb_syns[] = {"weightb", "weight-b", "weight_b", NULL};
145 static gchar *x264_bpyramid_syns[] = {"b-pyramid", "b_pyramid", NULL};
146 static gchar *x264_me_syns[] = {"me", NULL};
147 static gchar *x264_merange_syns[] = {"merange", "me-range", "me_range", NULL};
148 static gchar *x264_subme_syns[] = {"subme", "subq", NULL};
149 static gchar *x264_analyse_syns[] = {"analyse", "partitions", NULL};
150 static gchar *x264_8x8dct_syns[] = {"8x8dct", NULL};
151 static gchar *x264_deblock_syns[] = {"deblock", "filter", NULL};
152 static gchar *x264_trellis_syns[] = {"trellis", NULL};
153 static gchar *x264_pskip_syns[] = {"no-fast-pskip", "no_fast_pskip", NULL};
154 static gchar *x264_decimate_syns[] =
155 {"no-dct-decimate", "no_dct_decimate", NULL};
156 static gchar *x264_cabac_syns[] = {"cabac", NULL};
159 find_syn_match(const gchar *opt, gchar **syns)
162 for (ii = 0; syns[ii] != NULL; ii++)
164 if (strcmp(opt, syns[ii]) == 0)
170 struct x264_opt_map_s x264_opt_map[] =
172 {x264_ref_syns, "x264_refs", "1", X264_OPT_INT},
173 {x264_mixed_syns, "x264_mixed_refs", "0", X264_OPT_BOOL},
174 {x264_bframes_syns, "x264_bframes", "0", X264_OPT_INT},
175 {x264_direct_syns, "x264_direct", "spatial", X264_OPT_COMBO},
176 {x264_weightb_syns, "x264_weighted_bframes", "0", X264_OPT_BOOL},
177 {x264_bpyramid_syns, "x264_bpyramid", "0", X264_OPT_BOOL},
178 {x264_me_syns, "x264_me", "hex", X264_OPT_COMBO},
179 {x264_merange_syns, "x264_merange", "16", X264_OPT_INT},
180 {x264_subme_syns, "x264_subme", "6", X264_OPT_COMBO},
181 {x264_analyse_syns, "x264_analyse", "some", X264_OPT_COMBO},
182 {x264_8x8dct_syns, "x264_8x8dct", "0", X264_OPT_BOOL},
183 {x264_deblock_syns, "x264_deblock_alpha", "0,0", X264_OPT_DEBLOCK},
184 {x264_deblock_syns, "x264_deblock_beta", "0,0", X264_OPT_DEBLOCK},
185 {x264_trellis_syns, "x264_trellis", "0", X264_OPT_COMBO},
186 {x264_pskip_syns, "x264_no_fast_pskip", "0", X264_OPT_BOOL},
187 {x264_decimate_syns, "x264_no_dct_decimate", "0", X264_OPT_BOOL},
188 {x264_cabac_syns, "x264_cabac", "1", X264_OPT_BOOL},
190 #define X264_OPT_MAP_SIZE (sizeof(x264_opt_map)/sizeof(struct x264_opt_map_s))
193 x264_opt_get_default(const gchar *opt)
196 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
198 if (find_syn_match(opt, x264_opt_map[jj].opt_syns) >= 0)
200 return x264_opt_map[jj].def_val;
207 x264_update_int(signal_user_data_t *ud, const gchar *name, const gchar *val)
211 if (val == NULL) return;
212 ival = g_strtod (val, NULL);
213 ghb_ui_update(ud, name, ghb_int64_value(ival));
216 static gchar *true_str[] =
225 str_is_true(const gchar *str)
228 for (ii = 0; true_str[ii]; ii++)
230 if (g_ascii_strcasecmp(str, true_str[ii]) == 0)
237 x264_update_bool(signal_user_data_t *ud, const gchar *name, const gchar *val)
240 ghb_ui_update(ud, name, ghb_boolean_value(1));
242 ghb_ui_update(ud, name, ghb_boolean_value(str_is_true(val)));
246 x264_update_combo(signal_user_data_t *ud, const gchar *name, const gchar *val)
252 gboolean foundit = FALSE;
255 if (val == NULL) return;
256 widget = GHB_WIDGET(ud->builder, name);
259 g_debug("Failed to find widget for key: %s\n", name);
262 store = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
263 if (gtk_tree_model_get_iter_first (store, &iter))
267 gtk_tree_model_get(store, &iter, 2, &shortOpt, 3, &ivalue, -1);
268 if (strcmp(shortOpt, val) == 0)
270 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), &iter);
276 } while (gtk_tree_model_iter_next (store, &iter));
280 if (gtk_tree_model_get_iter_first (store, &iter))
284 gtk_tree_model_get(store, &iter, 2, &shortOpt, 3, &ivalue, -1);
285 if (strcmp(shortOpt, "custom") == 0)
287 gtk_list_store_set(GTK_LIST_STORE(store), &iter, 4, val, -1);
288 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), &iter);
294 } while (gtk_tree_model_iter_next (store, &iter));
297 // Its possible the value hasn't changed. Since settings are only
298 // updated when the value changes, I'm initializing settings here as well.
299 ghb_widget_to_setting(ud->settings, widget);
303 x264_update_deblock(signal_user_data_t *ud, const gchar *xval)
305 gdouble avalue, bvalue;
310 if (xval == NULL) return;
311 val = g_strdup(xval);
315 gchar *pos = strchr(val, ',');
321 avalue = g_strtod (val, &end);
324 bvalue = g_strtod (bval, &end);
328 ghb_ui_update(ud, "x264_deblock_alpha", ghb_int64_value(avalue));
329 ghb_ui_update(ud, "x264_deblock_beta", ghb_int64_value(bvalue));
333 ghb_x264_parse_options(signal_user_data_t *ud, const gchar *options)
335 gchar **split = g_strsplit(options, ":", -1);
336 if (split == NULL) return;
341 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
342 x264_opt_map[jj].found = FALSE;
344 for (ii = 0; split[ii] != NULL; ii++)
347 gchar *pos = strchr(split[ii], '=');
353 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
355 if (find_syn_match(split[ii], x264_opt_map[jj].opt_syns) >= 0)
357 x264_opt_map[jj].found = TRUE;
358 switch(x264_opt_map[jj].type)
361 x264_update_int(ud, x264_opt_map[jj].name, val);
364 x264_update_bool(ud, x264_opt_map[jj].name, val);
367 x264_update_combo(ud, x264_opt_map[jj].name, val);
369 case X264_OPT_DEBLOCK:
370 // dirty little hack. mark deblock_beta found as well
371 x264_opt_map[jj+1].found = TRUE;
372 x264_update_deblock(ud, val);
379 // For any options not found in the option string, set ui to
381 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
383 if (!x264_opt_map[jj].found)
385 gchar *val = strdup(x264_opt_map[jj].def_val);
386 switch(x264_opt_map[jj].type)
389 x264_update_int(ud, x264_opt_map[jj].name, val);
392 x264_update_bool(ud, x264_opt_map[jj].name, val);
395 x264_update_combo(ud, x264_opt_map[jj].name, val);
397 case X264_OPT_DEBLOCK:
398 x264_update_deblock(ud, val);
401 x264_opt_map[jj].found = TRUE;
409 get_deblock_val(signal_user_data_t *ud)
413 alpha = ghb_settings_get_string(ud->settings, "x264_deblock_alpha");
414 beta = ghb_settings_get_string(ud->settings, "x264_deblock_beta");
415 result = g_strdup_printf("%s,%s", alpha, beta);
422 x264_opt_update(signal_user_data_t *ud, GtkWidget *widget)
425 const gchar *name = gtk_widget_get_name(widget);
426 gchar **opt_syns = NULL;
427 const gchar *def_val = NULL;
430 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
432 if (strcmp(name, x264_opt_map[jj].name) == 0)
434 // found the options that needs updating
435 opt_syns = x264_opt_map[jj].opt_syns;
436 def_val = x264_opt_map[jj].def_val;
437 type = x264_opt_map[jj].type;
441 if (opt_syns != NULL)
443 GString *x264opts = g_string_new("");
445 gchar **split = NULL;
447 gboolean foundit = FALSE;
449 options = ghb_settings_get_string(ud->settings, "x264Option");
452 split = g_strsplit(options, ":", -1);
455 for (ii = 0; split && split[ii] != NULL; ii++)
459 gchar *pos = strchr(split[ii], '=');
465 syn = find_syn_match(split[ii], opt_syns);
467 { // Updating this option
470 if (type == X264_OPT_DEBLOCK)
471 val = get_deblock_val(ud);
475 gval = ghb_widget_value(widget);
476 if (G_VALUE_TYPE(gval) == G_TYPE_BOOLEAN)
478 if (ghb_value_boolean(gval))
485 val = ghb_widget_string(widget);
487 ghb_value_free(gval);
489 if (strcmp(def_val, val) != 0)
491 g_string_append_printf(x264opts, "%s=%s:", opt_syns[syn], val);
495 else if (val != NULL)
496 g_string_append_printf(x264opts, "%s=%s:", split[ii], val);
498 g_string_append_printf(x264opts, "%s:", split[ii]);
501 if (split) g_strfreev(split);
505 if (type == X264_OPT_DEBLOCK)
506 val = get_deblock_val(ud);
510 gval = ghb_widget_value(widget);
511 if (G_VALUE_TYPE(gval) == G_TYPE_BOOLEAN)
513 if (ghb_value_boolean(gval))
520 val = ghb_widget_string(widget);
522 ghb_value_free(gval);
524 if (strcmp(def_val, val) != 0)
526 g_string_append_printf(x264opts, "%s=%s:", opt_syns[0], val);
530 // Update the options value
531 // strip the trailing ":"
534 result = g_string_free(x264opts, FALSE);
535 len = strlen(result);
536 if (len > 0) result[len - 1] = 0;
538 sopts = sanitize_x264opts(ud, result);
539 ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
540 ghb_x264_parse_options(ud, sopts);
547 x264_remove_opt(gchar **opts, gchar **opt_syns)
550 for (ii = 0; opts[ii] != NULL; ii++)
553 opt = g_strdup(opts[ii]);
554 gchar *pos = strchr(opt, '=');
559 if (find_syn_match(opt, opt_syns) >= 0)
568 // Construct the x264 options string
569 // The result is allocated, so someone must free it at some point.
571 sanitize_x264opts(signal_user_data_t *ud, const gchar *options)
573 GString *x264opts = g_string_new("");
574 gchar **split = g_strsplit(options, ":", -1);
576 // Remove entries that match the defaults
578 for (ii = 0; split[ii] != NULL; ii++)
581 gchar *opt = g_strdup(split[ii]);
582 gchar *pos = strchr(opt, '=');
592 const gchar *def_val = x264_opt_get_default(opt);
593 if (strcmp(val, def_val) == 0)
595 // Matches the default, so remove it
600 gint refs = ghb_settings_get_int(ud->settings, "x264_refs");
603 x264_remove_opt(split, x264_mixed_syns);
605 gint bframes = ghb_settings_get_int(ud->settings, "x264_bframes");
608 x264_remove_opt(split, x264_weightb_syns);
609 x264_remove_opt(split, x264_direct_syns);
613 x264_remove_opt(split, x264_bpyramid_syns);
615 if (!ghb_settings_get_boolean(ud->settings, "x264_cabac"))
617 x264_remove_opt(split, x264_trellis_syns);
619 for (ii = 0; split[ii] != NULL; ii++)
621 if (split[ii][0] != 0)
622 g_string_append_printf(x264opts, "%s:", split[ii]);
625 // strip the trailing ":"
628 result = g_string_free(x264opts, FALSE);
629 len = strlen(result);
630 if (len > 0) result[len - 1] = 0;