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 gint ghb_lookup_bframes(const gchar *options);
24 static void x264_opt_update(signal_user_data_t *ud, GtkWidget *widget);
25 static gchar* sanitize_x264opts(signal_user_data_t *ud, const gchar *options);
27 // Flag needed to prevent x264 options processing from chasing its tail
28 static gboolean ignore_options_update = FALSE;
31 x264_widget_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
33 ghb_widget_to_setting(ud->settings, widget);
34 if (!ignore_options_update)
36 ignore_options_update = TRUE;
37 x264_opt_update(ud, widget);
38 ignore_options_update = FALSE;
40 ghb_check_dependency(ud, widget);
41 ghb_clear_presets_selection(ud);
45 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 me = ghb_settings_combo_int(ud->settings, "x264_me");
63 gtk_spin_button_set_range(GTK_SPIN_BUTTON(widget), 4, 16);
68 gtk_spin_button_set_range(GTK_SPIN_BUTTON(widget), 4, 64);
73 x264_entry_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
75 g_debug("x264_entry_changed_cb ()");
76 if (!ignore_options_update)
81 textview = GTK_WIDGET(GHB_WIDGET(ud->builder, "x264Option"));
82 ghb_widget_to_setting(ud->settings, textview);
83 options = ghb_settings_get_string(ud->settings, "x264Option");
84 ignore_options_update = TRUE;
85 ghb_x264_parse_options(ud, options);
86 if (!GTK_WIDGET_HAS_FOCUS(textview))
90 sopts = sanitize_x264opts(ud, options);
91 ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
92 ghb_x264_parse_options(ud, sopts);
96 ignore_options_update = FALSE;
100 G_MODULE_EXPORT gboolean
101 x264_focus_out_cb(GtkWidget *widget, GdkEventFocus *event,
102 signal_user_data_t *ud)
104 gchar *options, *sopts;
106 ghb_widget_to_setting(ud->settings, widget);
107 options = ghb_settings_get_string(ud->settings, "x264Option");
108 sopts = sanitize_x264opts(ud, options);
109 ignore_options_update = TRUE;
110 if (sopts != NULL && strcmp(sopts, options) != 0)
112 ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
113 ghb_x264_parse_options(ud, sopts);
117 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_badapt_syns[] = {"b-adapt", "b_adapt", NULL};
144 static gchar *x264_direct_syns[] =
145 {"direct", "direct-pred", "direct_pred", NULL};
146 static gchar *x264_weightb_syns[] = {"weightb", "weight-b", "weight_b", NULL};
147 static gchar *x264_bpyramid_syns[] = {"b-pyramid", "b_pyramid", NULL};
148 static gchar *x264_me_syns[] = {"me", NULL};
149 static gchar *x264_merange_syns[] = {"merange", "me-range", "me_range", NULL};
150 static gchar *x264_subme_syns[] = {"subme", "subq", NULL};
151 static gchar *x264_aqmode_syns[] = {"aq-mode", NULL};
152 static gchar *x264_analyse_syns[] = {"analyse", "partitions", NULL};
153 static gchar *x264_8x8dct_syns[] = {"8x8dct", NULL};
154 static gchar *x264_deblock_syns[] = {"deblock", "filter", NULL};
155 static gchar *x264_trellis_syns[] = {"trellis", NULL};
156 static gchar *x264_pskip_syns[] = {"no-fast-pskip", "no_fast_pskip", NULL};
157 static gchar *x264_psy_syns[] = {"psy-rd", "psy_rd", NULL};
158 static gchar *x264_mbtree_syns[] = {"mbtree", NULL};
159 static gchar *x264_decimate_syns[] =
160 {"no-dct-decimate", "no_dct_decimate", NULL};
161 static gchar *x264_cabac_syns[] = {"cabac", NULL};
164 find_syn_match(const gchar *opt, gchar **syns)
167 for (ii = 0; syns[ii] != NULL; ii++)
169 if (strcmp(opt, syns[ii]) == 0)
175 struct x264_opt_map_s x264_opt_map[] =
177 {x264_ref_syns, "x264_refs", "3", X264_OPT_INT},
178 {x264_mixed_syns, "x264_mixed_refs", "1", X264_OPT_BOOL},
179 {x264_bframes_syns, "x264_bframes", "3", X264_OPT_INT},
180 {x264_direct_syns, "x264_direct", "spatial", X264_OPT_COMBO},
181 {x264_badapt_syns, "x264_b_adapt", "1", X264_OPT_COMBO},
182 {x264_weightb_syns, "x264_weighted_bframes", "1", X264_OPT_BOOL},
183 {x264_bpyramid_syns, "x264_bpyramid", "0", X264_OPT_BOOL},
184 {x264_me_syns, "x264_me", "hex", X264_OPT_COMBO},
185 {x264_merange_syns, "x264_merange", "16", X264_OPT_INT},
186 {x264_subme_syns, "x264_subme", "7", X264_OPT_COMBO},
187 {x264_aqmode_syns, "x264_aqmode", "1", X264_OPT_NONE},
188 {x264_analyse_syns, "x264_analyse", "some", X264_OPT_COMBO},
189 {x264_8x8dct_syns, "x264_8x8dct", "1", X264_OPT_BOOL},
190 {x264_deblock_syns, "x264_deblock_alpha", "0,0", X264_OPT_DEBLOCK},
191 {x264_deblock_syns, "x264_deblock_beta", "0,0", X264_OPT_DEBLOCK},
192 {x264_trellis_syns, "x264_trellis", "1", X264_OPT_COMBO},
193 {x264_pskip_syns, "x264_no_fast_pskip", "0", X264_OPT_BOOL},
194 {x264_decimate_syns, "x264_no_dct_decimate", "0", X264_OPT_BOOL},
195 {x264_cabac_syns, "x264_cabac", "1", X264_OPT_BOOL},
196 {x264_psy_syns, "x264_psy_rd", "1,0", X264_OPT_PSY},
197 {x264_mbtree_syns, "x264_mbtree", "1", X264_OPT_NONE},
198 {x264_psy_syns, "x264_psy_trell", "1,0", X264_OPT_PSY},
200 #define X264_OPT_MAP_SIZE (sizeof(x264_opt_map)/sizeof(struct x264_opt_map_s))
203 x264_opt_get_default(const gchar *opt)
206 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
208 if (find_syn_match(opt, x264_opt_map[jj].opt_syns) >= 0)
210 return x264_opt_map[jj].def_val;
217 x264_update_int(signal_user_data_t *ud, const gchar *name, const gchar *val)
221 if (val == NULL) return;
222 ival = g_strtod (val, NULL);
223 ghb_ui_update(ud, name, ghb_int64_value(ival));
226 static gchar *true_str[] =
235 str_is_true(const gchar *str)
238 for (ii = 0; true_str[ii]; ii++)
240 if (g_ascii_strcasecmp(str, true_str[ii]) == 0)
247 x264_update_bool(signal_user_data_t *ud, const gchar *name, const gchar *val)
250 ghb_ui_update(ud, name, ghb_boolean_value(1));
252 ghb_ui_update(ud, name, ghb_boolean_value(str_is_true(val)));
256 x264_update_combo(signal_user_data_t *ud, const gchar *name, const gchar *val)
262 gboolean foundit = FALSE;
265 if (val == NULL) return;
266 widget = GHB_WIDGET(ud->builder, name);
269 g_debug("Failed to find widget for key: %s\n", name);
272 store = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
273 if (gtk_tree_model_get_iter_first (store, &iter))
277 gtk_tree_model_get(store, &iter, 2, &shortOpt, 3, &ivalue, -1);
278 if (strcmp(shortOpt, val) == 0)
280 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), &iter);
286 } while (gtk_tree_model_iter_next (store, &iter));
290 if (gtk_tree_model_get_iter_first (store, &iter))
294 gtk_tree_model_get(store, &iter, 2, &shortOpt, 3, &ivalue, -1);
295 if (strcmp(shortOpt, "custom") == 0)
297 gtk_list_store_set(GTK_LIST_STORE(store), &iter, 4, val, -1);
298 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), &iter);
304 } while (gtk_tree_model_iter_next (store, &iter));
307 // Its possible the value hasn't changed. Since settings are only
308 // updated when the value changes, I'm initializing settings here as well.
309 ghb_widget_to_setting(ud->settings, widget);
313 x264_update_deblock(signal_user_data_t *ud, const gchar *xval)
315 gdouble avalue, bvalue;
320 if (xval == NULL) return;
321 val = g_strdup(xval);
325 gchar *pos = strchr(val, ',');
331 avalue = g_strtod (val, &end);
334 bvalue = g_strtod (bval, &end);
338 ghb_ui_update(ud, "x264_deblock_alpha", ghb_int64_value(avalue));
339 ghb_ui_update(ud, "x264_deblock_beta", ghb_int64_value(bvalue));
343 x264_parse_psy(const gchar *psy, gdouble *psy_rd, gdouble *psy_trell)
346 gchar *trell_val = NULL;
351 if (psy == NULL) return;
353 gchar *pos = strchr(val, ',');
359 *psy_rd = g_strtod (val, &end);
360 if (trell_val != NULL)
362 *psy_trell = g_strtod (trell_val, &end);
368 x264_update_psy(signal_user_data_t *ud, const gchar *xval)
370 gdouble rd_value, trell_value;
372 if (xval == NULL) return;
373 x264_parse_psy(xval, &rd_value, &trell_value);
374 ghb_ui_update(ud, "x264_psy_rd", ghb_double_value(rd_value));
375 ghb_ui_update(ud, "x264_psy_trell", ghb_double_value(trell_value));
379 ghb_x264_parse_options(signal_user_data_t *ud, const gchar *options)
381 gchar **split = g_strsplit(options, ":", -1);
382 if (split == NULL) return;
387 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
388 x264_opt_map[jj].found = FALSE;
390 for (ii = 0; split[ii] != NULL; ii++)
393 gchar *pos = strchr(split[ii], '=');
399 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
401 if (find_syn_match(split[ii], x264_opt_map[jj].opt_syns) >= 0)
403 x264_opt_map[jj].found = TRUE;
404 switch(x264_opt_map[jj].type)
407 x264_update_int(ud, x264_opt_map[jj].name, val);
410 x264_update_bool(ud, x264_opt_map[jj].name, val);
413 x264_update_combo(ud, x264_opt_map[jj].name, val);
415 case X264_OPT_DEBLOCK:
416 // dirty little hack. mark deblock_beta found as well
417 x264_opt_map[jj+1].found = TRUE;
418 x264_update_deblock(ud, val);
421 // dirty little hack. mark psy_trell found as well
422 x264_opt_map[jj+1].found = TRUE;
423 x264_update_psy(ud, val);
430 // For any options not found in the option string, set ui to
432 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
434 if (!x264_opt_map[jj].found)
436 gchar *val = strdup(x264_opt_map[jj].def_val);
437 switch(x264_opt_map[jj].type)
440 x264_update_int(ud, x264_opt_map[jj].name, val);
443 x264_update_bool(ud, x264_opt_map[jj].name, val);
446 x264_update_combo(ud, x264_opt_map[jj].name, val);
448 case X264_OPT_DEBLOCK:
449 x264_update_deblock(ud, val);
452 x264_update_psy(ud, val);
455 x264_opt_map[jj].found = TRUE;
463 get_deblock_val(signal_user_data_t *ud)
467 alpha = ghb_settings_get_string(ud->settings, "x264_deblock_alpha");
468 beta = ghb_settings_get_string(ud->settings, "x264_deblock_beta");
469 result = g_strdup_printf("%s,%s", alpha, beta);
476 get_psy_val(signal_user_data_t *ud)
480 rd = ghb_settings_get_double(ud->settings, "x264_psy_rd");
481 trell = ghb_settings_get_double(ud->settings, "x264_psy_trell");
482 result = g_strdup_printf("%g,%g", rd, trell);
487 x264_opt_update(signal_user_data_t *ud, GtkWidget *widget)
490 const gchar *name = gtk_widget_get_name(widget);
491 gchar **opt_syns = NULL;
492 const gchar *def_val = NULL;
495 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
497 if (strcmp(name, x264_opt_map[jj].name) == 0)
499 // found the options that needs updating
500 opt_syns = x264_opt_map[jj].opt_syns;
501 def_val = x264_opt_map[jj].def_val;
502 type = x264_opt_map[jj].type;
506 if (opt_syns != NULL)
508 GString *x264opts = g_string_new("");
510 gchar **split = NULL;
512 gboolean foundit = FALSE;
514 options = ghb_settings_get_string(ud->settings, "x264Option");
517 split = g_strsplit(options, ":", -1);
520 for (ii = 0; split && split[ii] != NULL; ii++)
524 gchar *pos = strchr(split[ii], '=');
530 syn = find_syn_match(split[ii], opt_syns);
532 { // Updating this option
535 if (type == X264_OPT_DEBLOCK)
536 val = get_deblock_val(ud);
537 else if (type == X264_OPT_PSY)
538 val = get_psy_val(ud);
542 gval = ghb_widget_value(widget);
543 if (G_VALUE_TYPE(gval) == G_TYPE_BOOLEAN)
545 if (ghb_value_boolean(gval))
552 val = ghb_widget_string(widget);
554 ghb_value_free(gval);
556 if (strcmp(def_val, val) != 0)
558 g_string_append_printf(x264opts, "%s=%s:", opt_syns[syn], val);
562 else if (val != NULL)
563 g_string_append_printf(x264opts, "%s=%s:", split[ii], val);
565 g_string_append_printf(x264opts, "%s:", split[ii]);
568 if (split) g_strfreev(split);
572 if (type == X264_OPT_DEBLOCK)
573 val = get_deblock_val(ud);
574 else if (type == X264_OPT_PSY)
575 val = get_psy_val(ud);
579 gval = ghb_widget_value(widget);
580 if (G_VALUE_TYPE(gval) == G_TYPE_BOOLEAN)
582 if (ghb_value_boolean(gval))
589 val = ghb_widget_string(widget);
591 ghb_value_free(gval);
593 if (strcmp(def_val, val) != 0)
595 g_string_append_printf(x264opts, "%s=%s:", opt_syns[0], val);
599 // Update the options value
600 // strip the trailing ":"
603 result = g_string_free(x264opts, FALSE);
604 len = strlen(result);
605 if (len > 0) result[len - 1] = 0;
607 sopts = sanitize_x264opts(ud, result);
608 ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
609 ghb_x264_parse_options(ud, sopts);
616 x264_find_opt(gchar **opts, gchar **opt_syns)
619 for (ii = 0; opts[ii] != NULL; ii++)
622 opt = g_strdup(opts[ii]);
623 gchar *pos = strchr(opt, '=');
628 if (find_syn_match(opt, opt_syns) >= 0)
639 x264_remove_opt(gchar **opts, gchar **opt_syns)
642 for (ii = 0; opts[ii] != NULL; ii++)
645 opt = g_strdup(opts[ii]);
646 gchar *pos = strchr(opt, '=');
651 if (find_syn_match(opt, opt_syns) >= 0)
661 x264_lookup_value(gchar **opts, gchar **opt_syns)
666 const gchar *def_val = x264_opt_get_default(opt_syns[0]);
668 pos = x264_find_opt(opts, opt_syns);
671 gchar *cpos = strchr(opts[pos], '=');
674 ret = g_strdup(cpos+1);
681 else if (def_val != NULL)
683 ret = g_strdup(def_val);
689 ghb_lookup_badapt(const gchar *options)
698 split = g_strsplit(options, ":", -1);
700 result = x264_lookup_value(split, x264_badapt_syns);
704 ret = g_strtod(result, NULL);
711 ghb_lookup_aqmode(const gchar *options)
720 split = g_strsplit(options, ":", -1);
722 result = x264_lookup_value(split, x264_aqmode_syns);
726 ret = g_strtod(result, NULL);
733 ghb_lookup_bframes(const gchar *options)
742 split = g_strsplit(options, ":", -1);
744 result = x264_lookup_value(split, x264_bframes_syns);
748 ret = g_strtod(result, NULL);
755 ghb_lookup_mbtree(const gchar *options)
757 gint ret = ghb_lookup_bframes(options) != 0;
764 split = g_strsplit(options, ":", -1);
766 result = x264_lookup_value(split, x264_mbtree_syns);
770 ret = g_strtod(result, NULL);
776 // Construct the x264 options string
777 // The result is allocated, so someone must free it at some point.
779 sanitize_x264opts(signal_user_data_t *ud, const gchar *options)
781 GString *x264opts = g_string_new("");
782 gchar **split = g_strsplit(options, ":", -1);
785 // Fix up option dependencies
786 gboolean mbtree = ghb_lookup_mbtree(options);
789 x264_remove_opt(split, x264_bpyramid_syns);
791 gint subme = ghb_settings_combo_int(ud->settings, "x264_subme");
794 x264_remove_opt(split, x264_psy_syns);
796 gint trell = ghb_settings_combo_int(ud->settings, "x264_trellis");
799 gint aqmode = ghb_lookup_aqmode(options);
800 if (trell != 2 || aqmode == 0)
802 gint pos = x264_find_opt(split, x264_subme_syns);
804 split[pos] = g_strdup_printf("subme=9");
810 gdouble psy_rd = 0., psy_trell;
812 psy = x264_find_opt(split, x264_psy_syns);
815 gchar *pos = strchr(split[psy], '=');
818 x264_parse_psy(pos+1, &psy_rd, &psy_trell);
821 split[psy] = g_strdup_printf("psy-rd=%g,0", psy_rd);
824 gint refs = ghb_settings_get_int(ud->settings, "x264_refs");
827 x264_remove_opt(split, x264_mixed_syns);
829 gint bframes = ghb_settings_get_int(ud->settings, "x264_bframes");
832 x264_remove_opt(split, x264_weightb_syns);
833 x264_remove_opt(split, x264_direct_syns);
834 x264_remove_opt(split, x264_badapt_syns);
838 x264_remove_opt(split, x264_bpyramid_syns);
840 if (!ghb_settings_get_boolean(ud->settings, "x264_cabac"))
842 x264_remove_opt(split, x264_trellis_syns);
844 // Remove entries that match the defaults
845 for (ii = 0; split[ii] != NULL; ii++)
848 gchar *opt = g_strdup(split[ii]);
849 gchar *pos = strchr(opt, '=');
859 const gchar *def_val;
860 if (find_syn_match(opt, x264_mbtree_syns) >= 0 && bframes == 0)
866 def_val = x264_opt_get_default(opt);
868 if (strcmp(val, def_val) == 0)
870 // Matches the default, so remove it
875 for (ii = 0; split[ii] != NULL; ii++)
877 if (split[ii][0] != 0)
878 g_string_append_printf(x264opts, "%s:", split[ii]);
881 // strip the trailing ":"
884 result = g_string_free(x264opts, FALSE);
885 len = strlen(result);
886 if (len > 0) result[len - 1] = 0;