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 "x264handler.h"
22 static void x264_opt_update(signal_user_data_t *ud, GtkWidget *widget);
23 static gchar* sanitize_x264opts(signal_user_data_t *ud, const gchar *options);
25 // Flag needed to prevent x264 options processing from chasing its tail
26 static gboolean ignore_options_update = FALSE;
29 x264_widget_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
31 ghb_widget_to_setting(ud->settings, widget);
32 if (!ignore_options_update)
34 ignore_options_update = TRUE;
35 x264_opt_update(ud, widget);
36 ignore_options_update = FALSE;
38 ghb_check_dependency(ud, widget);
39 ghb_clear_presets_selection(ud);
43 x264_entry_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
45 g_debug("x264_entry_changed_cb ()");
46 if (!ignore_options_update)
51 textview = GTK_WIDGET(GHB_WIDGET(ud->builder, "x264_options"));
52 ghb_widget_to_setting(ud->settings, textview);
53 options = ghb_settings_get_string(ud->settings, "x264_options");
54 ignore_options_update = TRUE;
55 ghb_x264_parse_options(ud, options);
56 if (!GTK_WIDGET_HAS_FOCUS(textview))
60 sopts = sanitize_x264opts(ud, options);
61 ghb_ui_update(ud, "x264_options", ghb_string_value(sopts));
62 ghb_x264_parse_options(ud, sopts);
66 ignore_options_update = FALSE;
71 x264_focus_out_cb(GtkWidget *widget, GdkEventFocus *event,
72 signal_user_data_t *ud)
74 gchar *options, *sopts;
76 ghb_widget_to_setting(ud->settings, widget);
77 options = ghb_settings_get_string(ud->settings, "x264_options");
78 sopts = sanitize_x264opts(ud, options);
79 ignore_options_update = TRUE;
82 ghb_ui_update(ud, "x264_options", ghb_string_value(sopts));
83 ghb_x264_parse_options(ud, sopts);
87 ignore_options_update = FALSE;
108 static gchar *x264_ref_syns[] = {"ref", "frameref", NULL};
109 static gchar *x264_mixed_syns[] = {"mixed-refs", "mixed_refs", NULL};
110 static gchar *x264_bframes_syns[] = {"bframes", NULL};
111 static gchar *x264_direct_syns[] =
112 {"direct", "direct-pred", "direct_pred", NULL};
113 static gchar *x264_weightb_syns[] = {"weightb", "weight-b", "weight_b", NULL};
114 static gchar *x264_bpyramid_syns[] = {"b-pyramid", "b_pyramid", NULL};
115 static gchar *x264_me_syns[] = {"me", NULL};
116 static gchar *x264_merange_syns[] = {"merange", "me-range", "me_range", NULL};
117 static gchar *x264_subme_syns[] = {"subme", "subq", NULL};
118 static gchar *x264_analyse_syns[] = {"analyse", "partitions", NULL};
119 static gchar *x264_8x8dct_syns[] = {"8x8dct", NULL};
120 static gchar *x264_deblock_syns[] = {"deblock", "filter", NULL};
121 static gchar *x264_trellis_syns[] = {"trellis", NULL};
122 static gchar *x264_pskip_syns[] = {"no-fast-pskip", "no_fast_pskip", NULL};
123 static gchar *x264_decimate_syns[] =
124 {"no-dct-decimate", "no_dct_decimate", NULL};
125 static gchar *x264_cabac_syns[] = {"cabac", NULL};
128 find_syn_match(const gchar *opt, gchar **syns)
131 for (ii = 0; syns[ii] != NULL; ii++)
133 if (strcmp(opt, syns[ii]) == 0)
139 struct x264_opt_map_s x264_opt_map[] =
141 {x264_ref_syns, "x264_refs", "1", X264_OPT_INT},
142 {x264_mixed_syns, "x264_mixed_refs", "0", X264_OPT_BOOL},
143 {x264_bframes_syns, "x264_bframes", "0", X264_OPT_INT},
144 {x264_direct_syns, "x264_direct", "spatial", X264_OPT_COMBO},
145 {x264_weightb_syns, "x264_weighted_bframes", "0", X264_OPT_BOOL},
146 {x264_bpyramid_syns, "x264_bpyramid", "0", X264_OPT_BOOL},
147 {x264_me_syns, "x264_me", "hex", X264_OPT_COMBO},
148 {x264_merange_syns, "x264_merange", "16", X264_OPT_INT},
149 {x264_subme_syns, "x264_subme", "6", X264_OPT_COMBO},
150 {x264_analyse_syns, "x264_analyse", "some", X264_OPT_COMBO},
151 {x264_8x8dct_syns, "x264_8x8dct", "0", X264_OPT_BOOL},
152 {x264_deblock_syns, "x264_deblock_alpha", "0,0", X264_OPT_DEBLOCK},
153 {x264_deblock_syns, "x264_deblock_beta", "0,0", X264_OPT_DEBLOCK},
154 {x264_trellis_syns, "x264_trellis", "0", X264_OPT_COMBO},
155 {x264_pskip_syns, "x264_no_fast_pskip", "0", X264_OPT_BOOL},
156 {x264_decimate_syns, "x264_no_dct_decimate", "0", X264_OPT_BOOL},
157 {x264_cabac_syns, "x264_cabac", "1", X264_OPT_BOOL},
159 #define X264_OPT_MAP_SIZE (sizeof(x264_opt_map)/sizeof(struct x264_opt_map_s))
162 x264_opt_get_default(const gchar *opt)
165 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
167 if (find_syn_match(opt, x264_opt_map[jj].opt_syns) >= 0)
169 return x264_opt_map[jj].def_val;
176 x264_update_int(signal_user_data_t *ud, const gchar *name, const gchar *val)
180 if (val == NULL) return;
181 ival = g_strtod (val, NULL);
182 ghb_ui_update(ud, name, ghb_int64_value(ival));
185 static gchar *true_str[] =
194 str_is_true(const gchar *str)
197 for (ii = 0; true_str[ii]; ii++)
199 if (g_ascii_strcasecmp(str, true_str[ii]) == 0)
206 x264_update_bool(signal_user_data_t *ud, const gchar *name, const gchar *val)
209 ghb_ui_update(ud, name, ghb_boolean_value(1));
211 ghb_ui_update(ud, name, ghb_boolean_value(str_is_true(val)));
215 x264_update_combo(signal_user_data_t *ud, const gchar *name, const gchar *val)
221 gboolean foundit = FALSE;
224 if (val == NULL) return;
225 widget = GHB_WIDGET(ud->builder, name);
228 g_debug("Failed to find widget for key: %s\n", name);
231 store = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
232 if (gtk_tree_model_get_iter_first (store, &iter))
236 gtk_tree_model_get(store, &iter, 2, &shortOpt, 3, &ivalue, -1);
237 if (strcmp(shortOpt, val) == 0)
239 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), &iter);
245 } while (gtk_tree_model_iter_next (store, &iter));
249 if (gtk_tree_model_get_iter_first (store, &iter))
253 gtk_tree_model_get(store, &iter, 2, &shortOpt, 3, &ivalue, -1);
254 if (strcmp(shortOpt, "custom") == 0)
256 gtk_list_store_set(GTK_LIST_STORE(store), &iter, 4, val, -1);
257 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), &iter);
263 } while (gtk_tree_model_iter_next (store, &iter));
266 // Its possible the value hasn't changed. Since settings are only
267 // updated when the value changes, I'm initializing settings here as well.
268 ghb_widget_to_setting(ud->settings, widget);
272 x264_update_deblock(signal_user_data_t *ud, const gchar *xval)
274 gdouble avalue, bvalue;
279 if (xval == NULL) return;
280 val = g_strdup(xval);
284 gchar *pos = strchr(val, ',');
290 avalue = g_strtod (val, &end);
293 bvalue = g_strtod (bval, &end);
297 ghb_ui_update(ud, "x264_deblock_alpha", ghb_int64_value(avalue));
298 ghb_ui_update(ud, "x264_deblock_beta", ghb_int64_value(bvalue));
302 ghb_x264_parse_options(signal_user_data_t *ud, const gchar *options)
304 gchar **split = g_strsplit(options, ":", -1);
305 if (split == NULL) return;
310 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
311 x264_opt_map[jj].found = FALSE;
313 for (ii = 0; split[ii] != NULL; ii++)
316 gchar *pos = strchr(split[ii], '=');
322 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
324 if (find_syn_match(split[ii], x264_opt_map[jj].opt_syns) >= 0)
326 x264_opt_map[jj].found = TRUE;
327 switch(x264_opt_map[jj].type)
330 x264_update_int(ud, x264_opt_map[jj].name, val);
333 x264_update_bool(ud, x264_opt_map[jj].name, val);
336 x264_update_combo(ud, x264_opt_map[jj].name, val);
338 case X264_OPT_DEBLOCK:
339 // dirty little hack. mark deblock_beta found as well
340 x264_opt_map[jj+1].found = TRUE;
341 x264_update_deblock(ud, val);
348 // For any options not found in the option string, set ui to
350 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
352 if (!x264_opt_map[jj].found)
354 gchar *val = strdup(x264_opt_map[jj].def_val);
355 switch(x264_opt_map[jj].type)
358 x264_update_int(ud, x264_opt_map[jj].name, val);
361 x264_update_bool(ud, x264_opt_map[jj].name, val);
364 x264_update_combo(ud, x264_opt_map[jj].name, val);
366 case X264_OPT_DEBLOCK:
367 x264_update_deblock(ud, val);
370 x264_opt_map[jj].found = TRUE;
378 get_deblock_val(signal_user_data_t *ud)
382 alpha = ghb_settings_get_string(ud->settings, "x264_deblock_alpha");
383 beta = ghb_settings_get_string(ud->settings, "x264_deblock_beta");
384 result = g_strdup_printf("%s,%s", alpha, beta);
391 x264_opt_update(signal_user_data_t *ud, GtkWidget *widget)
394 const gchar *name = gtk_widget_get_name(widget);
395 gchar **opt_syns = NULL;
396 const gchar *def_val = NULL;
399 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
401 if (strcmp(name, x264_opt_map[jj].name) == 0)
403 // found the options that needs updating
404 opt_syns = x264_opt_map[jj].opt_syns;
405 def_val = x264_opt_map[jj].def_val;
406 type = x264_opt_map[jj].type;
410 if (opt_syns != NULL)
412 GString *x264opts = g_string_new("");
414 gchar **split = NULL;
416 gboolean foundit = FALSE;
418 options = ghb_settings_get_string(ud->settings, "x264_options");
421 split = g_strsplit(options, ":", -1);
424 for (ii = 0; split && split[ii] != NULL; ii++)
428 gchar *pos = strchr(split[ii], '=');
434 syn = find_syn_match(split[ii], opt_syns);
436 { // Updating this option
439 if (type == X264_OPT_DEBLOCK)
440 val = get_deblock_val(ud);
444 gval = ghb_widget_value(widget);
445 if (G_VALUE_TYPE(gval) == G_TYPE_BOOLEAN)
447 if (ghb_value_boolean(gval))
454 val = ghb_widget_string(widget);
456 ghb_value_free(gval);
458 if (strcmp(def_val, val) != 0)
460 g_string_append_printf(x264opts, "%s=%s:", opt_syns[syn], val);
464 else if (val != NULL)
465 g_string_append_printf(x264opts, "%s=%s:", split[ii], val);
467 g_string_append_printf(x264opts, "%s:", split[ii]);
470 if (split) g_strfreev(split);
474 if (type == X264_OPT_DEBLOCK)
475 val = get_deblock_val(ud);
479 gval = ghb_widget_value(widget);
480 if (G_VALUE_TYPE(gval) == G_TYPE_BOOLEAN)
482 if (ghb_value_boolean(gval))
489 val = ghb_widget_string(widget);
491 ghb_value_free(gval);
493 if (strcmp(def_val, val) != 0)
495 g_string_append_printf(x264opts, "%s=%s:", opt_syns[0], val);
499 // Update the options value
500 // strip the trailing ":"
503 result = g_string_free(x264opts, FALSE);
504 len = strlen(result);
505 if (len > 0) result[len - 1] = 0;
507 sopts = sanitize_x264opts(ud, result);
508 ghb_ui_update(ud, "x264_options", ghb_string_value(sopts));
509 ghb_x264_parse_options(ud, sopts);
516 x264_remove_opt(gchar **opts, gchar **opt_syns)
519 for (ii = 0; opts[ii] != NULL; ii++)
522 opt = g_strdup(opts[ii]);
523 gchar *pos = strchr(opt, '=');
528 if (find_syn_match(opt, opt_syns) >= 0)
537 // Construct the x264 options string
538 // The result is allocated, so someone must free it at some point.
540 sanitize_x264opts(signal_user_data_t *ud, const gchar *options)
542 GString *x264opts = g_string_new("");
543 gchar **split = g_strsplit(options, ":", -1);
545 // Remove entries that match the defaults
547 for (ii = 0; split[ii] != NULL; ii++)
550 gchar *opt = g_strdup(split[ii]);
551 gchar *pos = strchr(opt, '=');
561 const gchar *def_val = x264_opt_get_default(opt);
562 if (strcmp(val, def_val) == 0)
564 // Matches the default, so remove it
569 gint refs = ghb_settings_get_int(ud->settings, "x264_refs");
572 x264_remove_opt(split, x264_mixed_syns);
574 gint bframes = ghb_settings_get_int(ud->settings, "x264_bframes");
577 x264_remove_opt(split, x264_weightb_syns);
578 x264_remove_opt(split, x264_direct_syns);
582 x264_remove_opt(split, x264_bpyramid_syns);
584 gchar *me = ghb_settings_get_string(ud->settings, "x264_me");
585 if (!(strcmp(me, "umh") == 0 || strcmp(me, "esa") == 0))
587 x264_remove_opt(split, x264_merange_syns);
590 if (!ghb_settings_get_boolean(ud->settings, "x264_cabac"))
592 x264_remove_opt(split, x264_trellis_syns);
594 for (ii = 0; split[ii] != NULL; ii++)
596 if (split[ii][0] != 0)
597 g_string_append_printf(x264opts, "%s:", split[ii]);
600 // strip the trailing ":"
603 result = g_string_free(x264opts, FALSE);
604 len = strlen(result);
605 if (len > 0) result[len - 1] = 0;