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;
131 struct x264_opt_map_s
140 static gchar *x264_ref_syns[] = {"ref", "frameref", NULL};
141 static gchar *x264_mixed_syns[] = {"mixed-refs", "mixed_refs", NULL};
142 static gchar *x264_bframes_syns[] = {"bframes", NULL};
143 static gchar *x264_direct_syns[] =
144 {"direct", "direct-pred", "direct_pred", NULL};
145 static gchar *x264_weightb_syns[] = {"weightb", "weight-b", "weight_b", NULL};
146 static gchar *x264_bpyramid_syns[] = {"b-pyramid", "b_pyramid", NULL};
147 static gchar *x264_me_syns[] = {"me", NULL};
148 static gchar *x264_merange_syns[] = {"merange", "me-range", "me_range", NULL};
149 static gchar *x264_subme_syns[] = {"subme", "subq", NULL};
150 static gchar *x264_analyse_syns[] = {"analyse", "partitions", NULL};
151 static gchar *x264_8x8dct_syns[] = {"8x8dct", NULL};
152 static gchar *x264_deblock_syns[] = {"deblock", "filter", NULL};
153 static gchar *x264_trellis_syns[] = {"trellis", NULL};
154 static gchar *x264_pskip_syns[] = {"no-fast-pskip", "no_fast_pskip", NULL};
155 static gchar *x264_psy_syns[] = {"psy-rd", NULL};
156 static gchar *x264_decimate_syns[] =
157 {"no-dct-decimate", "no_dct_decimate", NULL};
158 static gchar *x264_cabac_syns[] = {"cabac", NULL};
161 find_syn_match(const gchar *opt, gchar **syns)
164 for (ii = 0; syns[ii] != NULL; ii++)
166 if (strcmp(opt, syns[ii]) == 0)
172 struct x264_opt_map_s x264_opt_map[] =
174 {x264_ref_syns, "x264_refs", "1", X264_OPT_INT},
175 {x264_mixed_syns, "x264_mixed_refs", "0", X264_OPT_BOOL},
176 {x264_bframes_syns, "x264_bframes", "0", X264_OPT_INT},
177 {x264_direct_syns, "x264_direct", "spatial", X264_OPT_COMBO},
178 {x264_weightb_syns, "x264_weighted_bframes", "0", X264_OPT_BOOL},
179 {x264_bpyramid_syns, "x264_bpyramid", "0", X264_OPT_BOOL},
180 {x264_me_syns, "x264_me", "hex", X264_OPT_COMBO},
181 {x264_merange_syns, "x264_merange", "16", X264_OPT_INT},
182 {x264_subme_syns, "x264_subme", "6", X264_OPT_COMBO},
183 {x264_analyse_syns, "x264_analyse", "some", X264_OPT_COMBO},
184 {x264_8x8dct_syns, "x264_8x8dct", "0", X264_OPT_BOOL},
185 {x264_deblock_syns, "x264_deblock_alpha", "0,0", X264_OPT_DEBLOCK},
186 {x264_deblock_syns, "x264_deblock_beta", "0,0", X264_OPT_DEBLOCK},
187 {x264_trellis_syns, "x264_trellis", "0", X264_OPT_COMBO},
188 {x264_pskip_syns, "x264_no_fast_pskip", "0", X264_OPT_BOOL},
189 {x264_decimate_syns, "x264_no_dct_decimate", "0", X264_OPT_BOOL},
190 {x264_cabac_syns, "x264_cabac", "1", X264_OPT_BOOL},
191 {x264_psy_syns, "x264_psy_rd", "1,0", X264_OPT_PSY},
192 {x264_psy_syns, "x264_psy_trell", "1,0", X264_OPT_PSY},
194 #define X264_OPT_MAP_SIZE (sizeof(x264_opt_map)/sizeof(struct x264_opt_map_s))
197 x264_opt_get_default(const gchar *opt)
200 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
202 if (find_syn_match(opt, x264_opt_map[jj].opt_syns) >= 0)
204 return x264_opt_map[jj].def_val;
211 x264_update_int(signal_user_data_t *ud, const gchar *name, const gchar *val)
215 if (val == NULL) return;
216 ival = g_strtod (val, NULL);
217 ghb_ui_update(ud, name, ghb_int64_value(ival));
220 static gchar *true_str[] =
229 str_is_true(const gchar *str)
232 for (ii = 0; true_str[ii]; ii++)
234 if (g_ascii_strcasecmp(str, true_str[ii]) == 0)
241 x264_update_bool(signal_user_data_t *ud, const gchar *name, const gchar *val)
244 ghb_ui_update(ud, name, ghb_boolean_value(1));
246 ghb_ui_update(ud, name, ghb_boolean_value(str_is_true(val)));
250 x264_update_combo(signal_user_data_t *ud, const gchar *name, const gchar *val)
256 gboolean foundit = FALSE;
259 if (val == NULL) return;
260 widget = GHB_WIDGET(ud->builder, name);
263 g_debug("Failed to find widget for key: %s\n", name);
266 store = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
267 if (gtk_tree_model_get_iter_first (store, &iter))
271 gtk_tree_model_get(store, &iter, 2, &shortOpt, 3, &ivalue, -1);
272 if (strcmp(shortOpt, val) == 0)
274 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), &iter);
280 } while (gtk_tree_model_iter_next (store, &iter));
284 if (gtk_tree_model_get_iter_first (store, &iter))
288 gtk_tree_model_get(store, &iter, 2, &shortOpt, 3, &ivalue, -1);
289 if (strcmp(shortOpt, "custom") == 0)
291 gtk_list_store_set(GTK_LIST_STORE(store), &iter, 4, val, -1);
292 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), &iter);
298 } while (gtk_tree_model_iter_next (store, &iter));
301 // Its possible the value hasn't changed. Since settings are only
302 // updated when the value changes, I'm initializing settings here as well.
303 ghb_widget_to_setting(ud->settings, widget);
307 x264_update_deblock(signal_user_data_t *ud, const gchar *xval)
309 gdouble avalue, bvalue;
314 if (xval == NULL) return;
315 val = g_strdup(xval);
319 gchar *pos = strchr(val, ',');
325 avalue = g_strtod (val, &end);
328 bvalue = g_strtod (bval, &end);
332 ghb_ui_update(ud, "x264_deblock_alpha", ghb_int64_value(avalue));
333 ghb_ui_update(ud, "x264_deblock_beta", ghb_int64_value(bvalue));
337 x264_update_psy(signal_user_data_t *ud, const gchar *xval)
339 gdouble rd_value, trell_value;
342 gchar *trell_val = NULL;
344 if (xval == NULL) return;
345 val = g_strdup(xval);
346 rd_value = trell_value = 0;
349 gchar *pos = strchr(val, ',');
355 rd_value = g_strtod (val, &end);
356 if (trell_val != NULL)
358 trell_value = g_strtod (trell_val, &end);
362 ghb_ui_update(ud, "x264_psy_rd", ghb_double_value(rd_value));
363 ghb_ui_update(ud, "x264_psy_trell", ghb_double_value(trell_value));
367 ghb_x264_parse_options(signal_user_data_t *ud, const gchar *options)
369 gchar **split = g_strsplit(options, ":", -1);
370 if (split == NULL) return;
375 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
376 x264_opt_map[jj].found = FALSE;
378 for (ii = 0; split[ii] != NULL; ii++)
381 gchar *pos = strchr(split[ii], '=');
387 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
389 if (find_syn_match(split[ii], x264_opt_map[jj].opt_syns) >= 0)
391 x264_opt_map[jj].found = TRUE;
392 switch(x264_opt_map[jj].type)
395 x264_update_int(ud, x264_opt_map[jj].name, val);
398 x264_update_bool(ud, x264_opt_map[jj].name, val);
401 x264_update_combo(ud, x264_opt_map[jj].name, val);
403 case X264_OPT_DEBLOCK:
404 // dirty little hack. mark deblock_beta found as well
405 x264_opt_map[jj+1].found = TRUE;
406 x264_update_deblock(ud, val);
409 // dirty little hack. mark psy_trell found as well
410 x264_opt_map[jj+1].found = TRUE;
411 x264_update_psy(ud, val);
418 // For any options not found in the option string, set ui to
420 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
422 if (!x264_opt_map[jj].found)
424 gchar *val = strdup(x264_opt_map[jj].def_val);
425 switch(x264_opt_map[jj].type)
428 x264_update_int(ud, x264_opt_map[jj].name, val);
431 x264_update_bool(ud, x264_opt_map[jj].name, val);
434 x264_update_combo(ud, x264_opt_map[jj].name, val);
436 case X264_OPT_DEBLOCK:
437 x264_update_deblock(ud, val);
440 x264_update_psy(ud, val);
443 x264_opt_map[jj].found = TRUE;
451 get_deblock_val(signal_user_data_t *ud)
455 alpha = ghb_settings_get_string(ud->settings, "x264_deblock_alpha");
456 beta = ghb_settings_get_string(ud->settings, "x264_deblock_beta");
457 result = g_strdup_printf("%s,%s", alpha, beta);
464 get_psy_val(signal_user_data_t *ud)
468 rd = ghb_settings_get_double(ud->settings, "x264_psy_rd");
469 trell = ghb_settings_get_double(ud->settings, "x264_psy_trell");
470 result = g_strdup_printf("%g,%g", rd, trell);
475 x264_opt_update(signal_user_data_t *ud, GtkWidget *widget)
478 const gchar *name = gtk_widget_get_name(widget);
479 gchar **opt_syns = NULL;
480 const gchar *def_val = NULL;
483 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
485 if (strcmp(name, x264_opt_map[jj].name) == 0)
487 // found the options that needs updating
488 opt_syns = x264_opt_map[jj].opt_syns;
489 def_val = x264_opt_map[jj].def_val;
490 type = x264_opt_map[jj].type;
494 if (opt_syns != NULL)
496 GString *x264opts = g_string_new("");
498 gchar **split = NULL;
500 gboolean foundit = FALSE;
502 options = ghb_settings_get_string(ud->settings, "x264Option");
505 split = g_strsplit(options, ":", -1);
508 for (ii = 0; split && split[ii] != NULL; ii++)
512 gchar *pos = strchr(split[ii], '=');
518 syn = find_syn_match(split[ii], opt_syns);
520 { // Updating this option
523 if (type == X264_OPT_DEBLOCK)
524 val = get_deblock_val(ud);
525 else if (type == X264_OPT_PSY)
526 val = get_psy_val(ud);
530 gval = ghb_widget_value(widget);
531 if (G_VALUE_TYPE(gval) == G_TYPE_BOOLEAN)
533 if (ghb_value_boolean(gval))
540 val = ghb_widget_string(widget);
542 ghb_value_free(gval);
544 if (strcmp(def_val, val) != 0)
546 g_string_append_printf(x264opts, "%s=%s:", opt_syns[syn], val);
550 else if (val != NULL)
551 g_string_append_printf(x264opts, "%s=%s:", split[ii], val);
553 g_string_append_printf(x264opts, "%s:", split[ii]);
556 if (split) g_strfreev(split);
560 if (type == X264_OPT_DEBLOCK)
561 val = get_deblock_val(ud);
562 else if (type == X264_OPT_PSY)
563 val = get_psy_val(ud);
567 gval = ghb_widget_value(widget);
568 if (G_VALUE_TYPE(gval) == G_TYPE_BOOLEAN)
570 if (ghb_value_boolean(gval))
577 val = ghb_widget_string(widget);
579 ghb_value_free(gval);
581 if (strcmp(def_val, val) != 0)
583 g_string_append_printf(x264opts, "%s=%s:", opt_syns[0], val);
587 // Update the options value
588 // strip the trailing ":"
591 result = g_string_free(x264opts, FALSE);
592 len = strlen(result);
593 if (len > 0) result[len - 1] = 0;
595 sopts = sanitize_x264opts(ud, result);
596 ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
597 ghb_x264_parse_options(ud, sopts);
604 x264_remove_opt(gchar **opts, gchar **opt_syns)
607 for (ii = 0; opts[ii] != NULL; ii++)
610 opt = g_strdup(opts[ii]);
611 gchar *pos = strchr(opt, '=');
616 if (find_syn_match(opt, opt_syns) >= 0)
625 // Construct the x264 options string
626 // The result is allocated, so someone must free it at some point.
628 sanitize_x264opts(signal_user_data_t *ud, const gchar *options)
630 GString *x264opts = g_string_new("");
631 gchar **split = g_strsplit(options, ":", -1);
633 // Remove entries that match the defaults
635 for (ii = 0; split[ii] != NULL; ii++)
638 gchar *opt = g_strdup(split[ii]);
639 gchar *pos = strchr(opt, '=');
649 const gchar *def_val = x264_opt_get_default(opt);
650 if (strcmp(val, def_val) == 0)
652 // Matches the default, so remove it
657 gint refs = ghb_settings_get_int(ud->settings, "x264_refs");
660 x264_remove_opt(split, x264_mixed_syns);
662 gint bframes = ghb_settings_get_int(ud->settings, "x264_bframes");
665 x264_remove_opt(split, x264_weightb_syns);
666 x264_remove_opt(split, x264_direct_syns);
670 x264_remove_opt(split, x264_bpyramid_syns);
672 if (!ghb_settings_get_boolean(ud->settings, "x264_cabac"))
674 x264_remove_opt(split, x264_trellis_syns);
676 for (ii = 0; split[ii] != NULL; ii++)
678 if (split[ii][0] != 0)
679 g_string_append_printf(x264opts, "%s:", split[ii]);
682 // strip the trailing ":"
685 result = g_string_free(x264opts, FALSE);
686 len = strlen(result);
687 if (len > 0) result[len - 1] = 0;