OSDN Git Service

LinGui: change x264 turbo mode from subme=1 to subme=2
[handbrake-jp/handbrake-jp-git.git] / gtk / src / x264handler.c
index 20ec56e..a466412 100644 (file)
 #include "hb-backend.h"
 #include "x264handler.h"
 
+gint ghb_lookup_bframes(const gchar *options);
 static void x264_opt_update(signal_user_data_t *ud, GtkWidget *widget);
 static gchar* sanitize_x264opts(signal_user_data_t *ud, const gchar *options);
 
 // Flag needed to prevent x264 options processing from chasing its tail
 static gboolean ignore_options_update = FALSE;
 
-void
+G_MODULE_EXPORT void
 x264_widget_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
 {
        ghb_widget_to_setting(ud->settings, widget);
@@ -36,11 +37,11 @@ x264_widget_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
                x264_opt_update(ud, widget);
                ignore_options_update = FALSE;
        }
-       ghb_check_dependency(ud, widget);
+       ghb_check_dependency(ud, widget, NULL);
        ghb_clear_presets_selection(ud);
 }
 
-void
+G_MODULE_EXPORT void
 x264_me_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
 {
        gint me;
@@ -52,7 +53,7 @@ x264_me_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
                x264_opt_update(ud, widget);
                ignore_options_update = FALSE;
        }
-       ghb_check_dependency(ud, widget);
+       ghb_check_dependency(ud, widget, NULL);
        ghb_clear_presets_selection(ud);
        widget = GHB_WIDGET(ud->builder, "x264_merange");
        me = ghb_settings_combo_int(ud->settings, "x264_me");
@@ -68,7 +69,7 @@ x264_me_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
        }
 }
 
-void
+G_MODULE_EXPORT void
 x264_entry_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
 {
        g_debug("x264_entry_changed_cb ()");
@@ -96,7 +97,7 @@ x264_entry_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
        }
 }
 
-gboolean
+G_MODULE_EXPORT gboolean
 x264_focus_out_cb(GtkWidget *widget, GdkEventFocus *event, 
        signal_user_data_t *ud)
 {
@@ -119,6 +120,9 @@ x264_focus_out_cb(GtkWidget *widget, GdkEventFocus *event,
 
 enum
 {
+       X264_OPT_NONE,
+       X264_OPT_BOOL_NONE,
+       X264_OPT_INT_NONE,
        X264_OPT_DEBLOCK,
        X264_OPT_PSY,
        X264_OPT_INT,
@@ -146,12 +150,14 @@ static gchar *x264_bpyramid_syns[] = {"b-pyramid", "b_pyramid", NULL};
 static gchar *x264_me_syns[] = {"me", NULL};
 static gchar *x264_merange_syns[] = {"merange", "me-range", "me_range", NULL};
 static gchar *x264_subme_syns[] = {"subme", "subq", NULL};
+static gchar *x264_aqmode_syns[] = {"aq-mode", NULL};
 static gchar *x264_analyse_syns[] = {"analyse", "partitions", NULL};
 static gchar *x264_8x8dct_syns[] = {"8x8dct", NULL};
 static gchar *x264_deblock_syns[] = {"deblock", "filter", NULL};
 static gchar *x264_trellis_syns[] = {"trellis", NULL};
 static gchar *x264_pskip_syns[] = {"no-fast-pskip", "no_fast_pskip", NULL};
 static gchar *x264_psy_syns[] = {"psy-rd", "psy_rd", NULL};
+static gchar *x264_mbtree_syns[] = {"mbtree", NULL};
 static gchar *x264_decimate_syns[] = 
        {"no-dct-decimate", "no_dct_decimate", NULL};
 static gchar *x264_cabac_syns[] = {"cabac", NULL};
@@ -170,26 +176,28 @@ find_syn_match(const gchar *opt, gchar **syns)
 
 struct x264_opt_map_s x264_opt_map[] =
 {
-       {x264_ref_syns, "x264_refs", "1", X264_OPT_INT},
-       {x264_mixed_syns, "x264_mixed_refs", "0", X264_OPT_BOOL},
-       {x264_bframes_syns, "x264_bframes", "0", X264_OPT_INT},
+       {x264_ref_syns, "x264_refs", "3", X264_OPT_INT},
+       {x264_mixed_syns, "x264_mixed_refs", "1", X264_OPT_BOOL},
+       {x264_bframes_syns, "x264_bframes", "3", X264_OPT_INT},
        {x264_direct_syns, "x264_direct", "spatial", X264_OPT_COMBO},
        {x264_badapt_syns, "x264_b_adapt", "1", X264_OPT_COMBO},
-       {x264_weightb_syns, "x264_weighted_bframes", "0", X264_OPT_BOOL},
+       {x264_weightb_syns, "x264_weighted_bframes", "1", X264_OPT_BOOL},
        {x264_bpyramid_syns, "x264_bpyramid", "0", X264_OPT_BOOL},
        {x264_me_syns, "x264_me", "hex", X264_OPT_COMBO},
        {x264_merange_syns, "x264_merange", "16", X264_OPT_INT},
-       {x264_subme_syns, "x264_subme", "6", X264_OPT_COMBO},
+       {x264_subme_syns, "x264_subme", "7", X264_OPT_COMBO},
+       {x264_aqmode_syns, "x264_aqmode", "1", X264_OPT_INT_NONE},
        {x264_analyse_syns, "x264_analyse", "some", X264_OPT_COMBO},
-       {x264_8x8dct_syns, "x264_8x8dct", "0", X264_OPT_BOOL},
+       {x264_8x8dct_syns, "x264_8x8dct", "1", X264_OPT_BOOL},
        {x264_deblock_syns, "x264_deblock_alpha", "0,0", X264_OPT_DEBLOCK},
        {x264_deblock_syns, "x264_deblock_beta", "0,0", X264_OPT_DEBLOCK},
-       {x264_trellis_syns, "x264_trellis", "0", X264_OPT_COMBO},
+       {x264_trellis_syns, "x264_trellis", "1", X264_OPT_COMBO},
        {x264_pskip_syns, "x264_no_fast_pskip", "0", X264_OPT_BOOL},
        {x264_decimate_syns, "x264_no_dct_decimate", "0", X264_OPT_BOOL},
        {x264_cabac_syns, "x264_cabac", "1", X264_OPT_BOOL},
        {x264_psy_syns, "x264_psy_rd", "1,0", X264_OPT_PSY},
        {x264_psy_syns, "x264_psy_trell", "1,0", X264_OPT_PSY},
+       {x264_mbtree_syns, "x264_mbtree", "1", X264_OPT_BOOL_NONE},
 };
 #define X264_OPT_MAP_SIZE (sizeof(x264_opt_map)/sizeof(struct x264_opt_map_s))
 
@@ -217,6 +225,17 @@ x264_update_int(signal_user_data_t *ud, const gchar *name, const gchar *val)
        ghb_ui_update(ud, name, ghb_int64_value(ival));
 }
 
+static void
+x264_update_int_setting(signal_user_data_t *ud, const gchar *name, const gchar *val)
+{
+       gint ival;
+
+       if (val == NULL) return;
+       ival = g_strtod (val, NULL);
+       ghb_settings_set_value(ud->settings, name, ghb_int64_value(ival));
+       ghb_check_dependency(ud, NULL, name);
+}
+
 static gchar *true_str[] =
 {
        "true",
@@ -247,6 +266,17 @@ x264_update_bool(signal_user_data_t *ud, const gchar *name, const gchar *val)
 }
 
 static void
+x264_update_bool_setting(signal_user_data_t *ud, const gchar *name, const gchar *val)
+{
+       if (val == NULL)
+               ghb_settings_set_value(ud->settings, name, ghb_boolean_value(1));
+       else
+               ghb_settings_set_value(ud->settings, name, ghb_boolean_value(str_is_true(val)));
+
+       ghb_check_dependency(ud, NULL, name);
+}
+
+static void
 x264_update_combo(signal_user_data_t *ud, const gchar *name, const gchar *val)
 {
        GtkTreeModel *store;
@@ -416,6 +446,12 @@ ghb_x264_parse_options(signal_user_data_t *ud, const gchar *options)
                                        x264_opt_map[jj+1].found = TRUE;
                                        x264_update_psy(ud, val);
                                        break;
+                               case X264_OPT_BOOL_NONE:
+                                       x264_update_bool_setting(ud, x264_opt_map[jj].name, val);
+                                       break;
+                               case X264_OPT_INT_NONE:
+                                       x264_update_int_setting(ud, x264_opt_map[jj].name, val);
+                                       break;
                                }
                                break;
                        }
@@ -445,6 +481,12 @@ ghb_x264_parse_options(signal_user_data_t *ud, const gchar *options)
                        case X264_OPT_PSY:
                                x264_update_psy(ud, val);
                                break;
+                       case X264_OPT_BOOL_NONE:
+                               x264_update_bool_setting(ud, x264_opt_map[jj].name, val);
+                               break;
+                       case X264_OPT_INT_NONE:
+                               x264_update_int_setting(ud, x264_opt_map[jj].name, val);
+                               break;
                        }
                        x264_opt_map[jj].found = TRUE;
                        g_free(val);
@@ -651,6 +693,122 @@ x264_remove_opt(gchar **opts, gchar **opt_syns)
        }
 }
 
+static gchar*
+x264_lookup_value(gchar **opts, gchar **opt_syns)
+{
+       gchar *ret = NULL;
+       gint pos;
+
+       const gchar *def_val = x264_opt_get_default(opt_syns[0]);
+
+       pos = x264_find_opt(opts, opt_syns);
+       if (pos >= 0)
+       {
+               gchar *cpos = strchr(opts[pos], '=');
+               if (cpos != NULL)
+               {
+                       ret = g_strdup(cpos+1);
+               }
+               else
+               {
+                       ret = g_strdup("");
+               }
+       }
+       else if (def_val != NULL)
+       {
+               ret = g_strdup(def_val);
+       }
+       return ret;
+}
+
+gint
+ghb_lookup_badapt(const gchar *options)
+{
+       gint ret = 0;
+       gchar *result;
+       gchar **split;
+       
+       if (options == NULL)
+               options = "";
+
+       split = g_strsplit(options, ":", -1);
+
+       result = x264_lookup_value(split, x264_badapt_syns);
+       g_strfreev(split);
+       if (result != NULL)
+       {
+               ret = g_strtod(result, NULL);
+               g_free(result);
+       }
+       return ret;
+}
+
+gint
+ghb_lookup_aqmode(const gchar *options)
+{
+       gint ret = 0;
+       gchar *result;
+       gchar **split;
+       
+       if (options == NULL)
+               options = "";
+
+       split = g_strsplit(options, ":", -1);
+
+       result = x264_lookup_value(split, x264_aqmode_syns);
+       g_strfreev(split);
+       if (result != NULL)
+       {
+               ret = g_strtod(result, NULL);
+               g_free(result);
+       }
+       return ret;
+}
+
+gint
+ghb_lookup_bframes(const gchar *options)
+{
+       gint ret = 0;
+       gchar *result;
+       gchar **split;
+       
+       if (options == NULL)
+               options = "";
+
+       split = g_strsplit(options, ":", -1);
+
+       result = x264_lookup_value(split, x264_bframes_syns);
+       g_strfreev(split);
+       if (result != NULL)
+       {
+               ret = g_strtod(result, NULL);
+               g_free(result);
+       }
+       return ret;
+}
+
+gint
+ghb_lookup_mbtree(const gchar *options)
+{
+       gint ret = ghb_lookup_bframes(options) != 0;
+       gchar *result;
+       gchar **split;
+       
+       if (options == NULL)
+               options = "";
+
+       split = g_strsplit(options, ":", -1);
+
+       result = x264_lookup_value(split, x264_mbtree_syns);
+       g_strfreev(split);
+       if (result != NULL)
+       {
+               ret = g_strtod(result, NULL);
+               g_free(result);
+       }
+       return ret;
+}
+
 // Construct the x264 options string
 // The result is allocated, so someone must free it at some point.
 static gchar*
@@ -661,12 +819,27 @@ sanitize_x264opts(signal_user_data_t *ud, const gchar *options)
        gint ii;
 
        // Fix up option dependencies
+       gboolean mbtree = ghb_lookup_mbtree(options);
+       if (mbtree)
+       {
+               x264_remove_opt(split, x264_bpyramid_syns);
+       }
        gint subme = ghb_settings_combo_int(ud->settings, "x264_subme");
        if (subme < 6)
        {
                x264_remove_opt(split, x264_psy_syns);
        }
        gint trell = ghb_settings_combo_int(ud->settings, "x264_trellis");
+       if (subme == 10)
+       {
+               gint aqmode = ghb_lookup_aqmode(options);
+               if (trell != 2 || aqmode == 0)
+               {
+                       gint pos = x264_find_opt(split, x264_subme_syns);
+                       g_free(split[pos]);
+                       split[pos] = g_strdup_printf("subme=9");
+               }
+       }
        if (trell < 1)
        {
                gint psy;
@@ -719,7 +892,15 @@ sanitize_x264opts(signal_user_data_t *ud, const gchar *options)
                {
                        val = "1";
                }
-               const gchar *def_val = x264_opt_get_default(opt);
+               const gchar *def_val;
+               if (find_syn_match(opt, x264_mbtree_syns) >= 0 && bframes == 0)
+               {
+                       def_val = "0";
+               }
+               else
+               {
+                       def_val = x264_opt_get_default(opt);
+               }
                if (strcmp(val, def_val) == 0)
                {
                        // Matches the default, so remove it