OSDN Git Service

LinGui: first cut at anamorphic picture settings enhancements
[handbrake-jp/handbrake-jp-git.git] / gtk / src / presets.c
index f7504d6..fb4bfc2 100644 (file)
@@ -16,6 +16,7 @@
 #include <glib/gstdio.h>
 #include <string.h>
 #include <gtk/gtk.h>
+#include "hb.h"
 #include "settings.h"
 #include "callbacks.h"
 #include "audiohandler.h"
@@ -37,10 +38,12 @@ enum
 static GValue *presetsPlist = NULL;
 static GValue *internalPlist = NULL;
 static GValue *prefsPlist = NULL;
+static gboolean prefs_modified = FALSE;
 
 static const GValue* preset_dict_get_value(GValue *dict, const gchar *key);
 static void store_plist(GValue *plist, const gchar *name);
 static void store_presets(void);
+static void store_prefs(void);
 
 // This only handle limited depth
 GtkTreePath*
@@ -139,37 +142,6 @@ ghb_preset_path_string(const GValue *path)
        return str;
 }
 
-static void
-debug_show_type(GType tp)
-{
-       const gchar *str = "unknown";
-       if (tp == G_TYPE_STRING)
-       {
-               str ="string";
-       }
-       else if (tp == G_TYPE_INT)
-       {
-               str ="int";
-       }
-       else if (tp == G_TYPE_INT64)
-       {
-               str ="int64";
-       }
-       else if (tp == G_TYPE_BOOLEAN)
-       {
-               str ="bool";
-       }
-       else if (tp == ghb_array_get_type())
-       {
-               str ="array";
-       }
-       else if (tp == ghb_dict_get_type())
-       {
-               str ="dict";
-       }
-       g_message("Type: %s", str);
-}
-
 void
 dump_preset_path(const gchar *msg, const GValue *path)
 {
@@ -284,6 +256,17 @@ preset_get_name(GValue *dict)
        return g_value_get_string(preset_dict_get_value(dict, "PresetName"));
 }
 
+static gboolean
+preset_folder_is_open(GValue *dict)
+{
+       const GValue *gval;
+
+       gval = preset_dict_get_value(dict, "FolderOpen");
+       if (gval != NULL)
+               return g_value_get_boolean(gval);
+       return FALSE;
+}
+
 gboolean
 ghb_preset_folder(GValue *dict)
 {
@@ -555,7 +538,7 @@ presets_find_default2(GValue *presets, gint *len)
                {
                        if (preset_is_default(dict))
                        {
-                               indices = malloc(MAX_NESTED_PRESET * sizeof(gint));
+                               indices = g_malloc(MAX_NESTED_PRESET * sizeof(gint));
                                indices[*len] = ii;
                                (*len)++;
                                return indices;
@@ -675,6 +658,20 @@ presets_set_default(gint *indices, gint len)
        store_presets();
 }
 
+static void
+presets_set_folder_open(gboolean open, gint *indices, gint len)
+{
+       GValue *dict;
+       
+       g_debug("presets_set_folder_open ()");
+       dict = presets_get_dict(presetsPlist, indices, len);
+       if (dict)
+       {
+               ghb_dict_insert(dict, g_strdup("FolderOpen"), 
+                                               ghb_boolean_value_new(open));
+       }
+}
+
 // Used for sorting dictionaries.
 gint
 key_cmp(gconstpointer a, gconstpointer b)
@@ -861,21 +858,6 @@ preset_to_ui(signal_user_data_t *ud, GValue *dict)
        init_settings_from_dict(ud->settings, hidden, dict);
        init_ui_from_dict(ud, internal, dict);
        init_ui_from_dict(ud, hidden, dict);
-
-       if (ghb_settings_get_boolean(ud->settings, "allow_tweaks"))
-       {
-               const GValue *gval;
-               gval = preset_dict_get_value(dict, "PictureDeinterlace");
-               if (gval)
-               {
-                       ghb_ui_update(ud, "tweak_PictureDeinterlace", gval);
-               }
-               gval = preset_dict_get_value(dict, "PictureDenoise");
-               if (gval)
-               {
-                       ghb_ui_update(ud, "tweak_PictureDenoise", gval);
-               }
-       }
 }
 
 void
@@ -1177,12 +1159,14 @@ ghb_prefs_to_ui(signal_user_data_t *ud)
        }
        gval = ghb_settings_get_value(ud->settings, "default_source");
        ghb_settings_set_value (ud->settings, "source", gval);
+
        str = ghb_settings_get_string(ud->settings, "destination_dir");
+       ghb_ui_update(ud, "dest_dir", ghb_string_value(str));
 
-       gchar *path = g_strdup_printf ("%s/new_video.mp4", str);
-       ghb_ui_update(ud, "destination", ghb_string_value(path));
+       gchar *file = g_strdup_printf ("new_video.mp4");
+       ghb_ui_update(ud, "dest_file", ghb_string_value(file));
        g_free(str);
-       g_free(path);
+       g_free(file);
 
        prefs_initializing = FALSE;
 }
@@ -1213,13 +1197,36 @@ ghb_prefs_save(GValue *settings)
                        ghb_dict_insert(pref_dict, g_strdup(key), ghb_value_dup(value));
            }
        }
-    store_plist(prefsPlist, "preferences");
+       store_prefs();
+       prefs_modified = FALSE;
+}
+
+void
+ghb_pref_set(GValue *settings, const gchar *key)
+{
+       const GValue *value, *value2;
+       
+       if (prefs_initializing) return;
+       value = ghb_settings_get_value(settings, key);
+       if (value != NULL)
+       {
+               GValue *dict;
+               dict = plist_get_dict(prefsPlist, "Preferences");
+               if (dict == NULL) return;
+               value2 = ghb_dict_lookup(dict, key);
+               if (ghb_value_cmp(value, value2) != 0)
+               {
+                       ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(value));
+                       store_prefs();
+                       prefs_modified = TRUE;
+               }
+       }
 }
 
 void
 ghb_pref_save(GValue *settings, const gchar *key)
 {
-       const GValue *value;
+       const GValue *value, *value2;
        
        if (prefs_initializing) return;
        value = ghb_settings_get_value(settings, key);
@@ -1228,8 +1235,23 @@ ghb_pref_save(GValue *settings, const gchar *key)
                GValue *dict;
                dict = plist_get_dict(prefsPlist, "Preferences");
                if (dict == NULL) return;
-               ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(value));
-               store_plist(prefsPlist, "preferences");
+               value2 = ghb_dict_lookup(dict, key);
+               if (ghb_value_cmp(value, value2) != 0)
+               {
+                       ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(value));
+                       store_prefs();
+                       prefs_modified = FALSE;
+               }
+       }
+}
+
+void
+ghb_prefs_store(void)
+{
+       if (prefs_modified)
+       {
+               store_prefs();
+               prefs_modified = FALSE;
        }
 }
 
@@ -1329,7 +1351,7 @@ ghb_prefs_load(signal_user_data_t *ud)
                }
                ghb_dict_insert(dict, 
                        g_strdup("destination_dir"), ghb_value_dup(ghb_string_value(dir)));
-               store_plist(prefsPlist, "preferences");
+               store_prefs();
     }
        // Read legacy default_preset preference and update accordingly
        path = ghb_dict_lookup(dict, "default_preset");
@@ -1355,7 +1377,7 @@ ghb_prefs_load(signal_user_data_t *ud)
                        g_free(indices);
                }
                ghb_dict_remove(dict, "default_preset");
-               store_plist(prefsPlist, "preferences");
+               store_prefs();
        }
 }
 
@@ -1467,6 +1489,20 @@ ghb_presets_list_init(
                if (folder)
                {
                        ghb_presets_list_init(ud, more_indices, len+1);
+                       if (preset_folder_is_open(dict))
+                       {
+                               GtkTreePath *path;
+
+                               if (piter != NULL)
+                               {
+                                       path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), piter);
+                                       gtk_tree_view_expand_row(treeview, path, FALSE);
+                                       gtk_tree_path_free(path);
+                               }
+                               path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
+                               gtk_tree_view_expand_row(treeview, path, FALSE);
+                               gtk_tree_path_free(path);
+                       }
                }
        }
        g_free(more_indices);
@@ -1655,7 +1691,7 @@ typedef struct
 static value_map_t vcodec_xlat[] =
 {
        {"MPEG-4 (FFmpeg)", "ffmpeg"},
-       {"MPEG-4 (XviD)", "xvid"},
+       {"MPEG-4 (XviD)", "ffmpeg"},
        {"H.264 (x264)", "x264"},
        {"VP3 (Theora)", "theora"},
        {NULL,NULL}
@@ -1675,8 +1711,8 @@ value_map_t container_xlat[] =
        {"MP4 file", "mp4"},
        {"M4V file", "m4v"},
        {"MKV file", "mkv"},
-       {"AVI file", "avi"},
-       {"OGM file", "ogm"},
+       {"AVI file", "mkv"},
+       {"OGM file", "mkv"},
        {NULL, NULL}
 };
 
@@ -1719,18 +1755,36 @@ value_map_t mix_xlat[] =
 value_map_t deint_xlat[] =
 {
        {"0", "none"},
-       {"1", "fast"},
-       {"2", "slow"},
-       {"3", "slower"},
+       {"1", "custom"},
+       {"2", "fast"},
+       {"3", "slow"},
+       {"4", "slower"},
        {NULL, NULL}
 };
 
 value_map_t denoise_xlat[] =
 {
        {"0", "none"},
-       {"1", "weak"},
-       {"2", "medium"},
-       {"3", "strong"},
+       {"1", "custom"},
+       {"2", "weak"},
+       {"3", "medium"},
+       {"4", "strong"},
+       {NULL, NULL}
+};
+
+value_map_t detel_xlat[] =
+{
+       {"0", "none"},
+       {"1", "custom"},
+       {"2", "default"},
+       {NULL, NULL}
+};
+
+value_map_t decomb_xlat[] =
+{
+       {"0", "none"},
+       {"1", "custom"},
+       {"2", "default"},
        {NULL, NULL}
 };
 
@@ -1931,20 +1985,26 @@ export_value_xlat(GValue *dict)
        gval = export_value_xlat2(framerate_xlat, lin_val, G_TYPE_STRING);
        if (gval)
                ghb_dict_insert(dict, g_strdup(key), gval);
+       key = "PictureDetelecine";
+       lin_val = ghb_dict_lookup(dict, key);
+       gval = export_value_xlat2(detel_xlat, lin_val, G_TYPE_INT);
+       if (gval)
+               ghb_dict_insert(dict, g_strdup(key), gval);
+       key = "PictureDecomb";
+       lin_val = ghb_dict_lookup(dict, key);
+       gval = export_value_xlat2(decomb_xlat, lin_val, G_TYPE_INT);
+       if (gval)
+               ghb_dict_insert(dict, g_strdup(key), gval);
        key = "PictureDeinterlace";
        lin_val = ghb_dict_lookup(dict, key);
        gval = export_value_xlat2(deint_xlat, lin_val, G_TYPE_INT);
        if (gval)
                ghb_dict_insert(dict, g_strdup(key), gval);
-       else
-               ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(lin_val));
        key = "PictureDenoise";
        lin_val = ghb_dict_lookup(dict, key);
        gval = export_value_xlat2(denoise_xlat, lin_val, G_TYPE_INT);
        if (gval)
                ghb_dict_insert(dict, g_strdup(key), gval);
-       else
-               ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(lin_val));
        key = "Subtitles";
        lin_val = ghb_dict_lookup(dict, key);
        gval = export_subtitle_xlat2(lin_val);
@@ -2053,6 +2113,16 @@ import_value_xlat(GValue *dict)
        gval = import_value_xlat2(defaults, framerate_xlat, key, mac_val);
        if (gval)
                ghb_dict_insert(dict, g_strdup(key), gval);
+       key = "PictureDetelecine";
+       mac_val = ghb_dict_lookup(dict, key);
+       gval = import_value_xlat2(defaults, detel_xlat, key, mac_val);
+       if (gval)
+               ghb_dict_insert(dict, g_strdup(key), gval);
+       key = "PictureDecomb";
+       mac_val = ghb_dict_lookup(dict, key);
+       gval = import_value_xlat2(defaults, decomb_xlat, key, mac_val);
+       if (gval)
+               ghb_dict_insert(dict, g_strdup(key), gval);
        key = "PictureDeinterlace";
        mac_val = ghb_dict_lookup(dict, key);
        gval = import_value_xlat2(defaults, deint_xlat, key, mac_val);
@@ -2133,32 +2203,26 @@ import_xlat_preset(GValue *dict)
        {
        case 0:
        {
-               ghb_dict_insert(dict, g_strdup("anamorphic"), 
-                                               ghb_boolean_value_new(FALSE));
-               if (ghb_dict_lookup(dict, "ModDimensions") == NULL)
-                       ghb_dict_insert(dict, g_strdup("ModDimensions"), 
-                                                       ghb_boolean_value_new(TRUE));
+               if (ghb_dict_lookup(dict, "PictureAlignment") == NULL)
+                       ghb_dict_insert(dict, g_strdup("PictureAlignment"), 
+                                                       ghb_int_value_new(16));
        } break;
        case 1:
        {
-               ghb_dict_insert(dict, g_strdup("anamorphic"), 
-                                               ghb_boolean_value_new(TRUE));
-               ghb_dict_insert(dict, g_strdup("ModDimensions"), 
-                                               ghb_boolean_value_new(FALSE));
+               ghb_dict_insert(dict, g_strdup("PictureAlignment"), 
+                                               ghb_int_value_new(1));
        } break;
        case 2:
        {
-               ghb_dict_insert(dict, g_strdup("anamorphic"), 
-                                               ghb_boolean_value_new(TRUE));
-               ghb_dict_insert(dict, g_strdup("ModDimensions"), 
-                                               ghb_boolean_value_new(TRUE));
+               if (ghb_dict_lookup(dict, "PictureAlignment") == NULL)
+                       ghb_dict_insert(dict, g_strdup("PictureAlignment"), 
+                                                       ghb_int_value_new(16));
        } break;
        default:
        {
-               ghb_dict_insert(dict, g_strdup("anamorphic"), 
-                                               ghb_boolean_value_new(TRUE));
-               ghb_dict_insert(dict, g_strdup("ModDimensions"), 
-                                               ghb_boolean_value_new(TRUE));
+               if (ghb_dict_lookup(dict, "PictureAlignment") == NULL)
+                       ghb_dict_insert(dict, g_strdup("PictureAlignment"), 
+                                                       ghb_int_value_new(16));
        } break;
        }
        // VideoQualityType/0/1/2 - vquality_type_/target/bitrate/constant
@@ -2202,6 +2266,43 @@ import_xlat_preset(GValue *dict)
        } break;
        }
        import_value_xlat(dict);
+
+       gdouble vquality;
+       const GValue *gval;
+
+       vquality = ghb_value_double(preset_dict_get_value(dict, "VideoQualitySlider"));
+       if (vquality < 1.0)
+       {
+               gint vcodec;
+
+               gval = preset_dict_get_value(dict, "VideoEncoder");
+               vcodec = ghb_lookup_combo_int("VideoEncoder", gval);
+               switch (vcodec)
+               {
+                       case HB_VCODEC_X264:
+                       {
+                               vquality = 51. - vquality * 51.;
+                       } break;
+
+                       case HB_VCODEC_XVID:
+                       case HB_VCODEC_FFMPEG:
+                       {
+                               vquality = 31. - vquality * 30.;
+                       } break;
+
+                       case HB_VCODEC_THEORA:
+                       {
+                               vquality = vquality * 63.;
+                       } break;
+
+                       default:
+                       {
+                               vquality = 0.;
+                       } break;
+               }
+               ghb_dict_insert(dict, g_strdup("VideoQualitySlider"), 
+                                               ghb_double_value_new(vquality));
+       }
 }
 
 static void
@@ -2235,12 +2336,10 @@ import_xlat_presets(GValue *presets)
 static void
 export_xlat_preset(GValue *dict)
 {
-       gboolean ana, round, autoscale, target, br, constant;
+       gboolean autoscale, target, br, constant;
 
        g_debug("export_xlat_prest ()");
        autoscale = ghb_value_boolean(preset_dict_get_value(dict, "autoscale"));
-       ana = ghb_value_boolean(preset_dict_get_value(dict, "anamorphic"));
-       round = ghb_value_boolean(preset_dict_get_value(dict, "ModDimensions"));
        target = ghb_value_boolean(
                                preset_dict_get_value(dict, "vquality_type_target"));
        br = ghb_value_boolean(
@@ -2255,20 +2354,6 @@ export_xlat_preset(GValue *dict)
                ghb_dict_insert(dict, g_strdup("UsesPictureSettings"), 
                                                ghb_int_value_new(1));
 
-       if (ana)
-       {
-               if (round)
-                       ghb_dict_insert(dict, g_strdup("PicturePAR"), 
-                                               ghb_int_value_new(2));
-               else
-                       ghb_dict_insert(dict, g_strdup("PicturePAR"), 
-                                               ghb_int_value_new(1));
-       }
-       else
-       {
-               ghb_dict_insert(dict, g_strdup("PicturePAR"), 
-                                               ghb_int_value_new(0));
-       }
        // VideoQualityType/0/1/2 - vquality_type_/target/bitrate/constant
        if (target)
        {
@@ -2285,8 +2370,8 @@ export_xlat_preset(GValue *dict)
                ghb_dict_insert(dict, g_strdup("VideoQualityType"), 
                                                ghb_int_value_new(2));
        }
+       ghb_dict_remove(dict, "UsesMaxPictureSettings");
        ghb_dict_remove(dict, "autoscale");
-       ghb_dict_remove(dict, "anamorphic");
        ghb_dict_remove(dict, "vquality_type_target");
        ghb_dict_remove(dict, "vquality_type_bitrate");
        ghb_dict_remove(dict, "vquality_type_constant");
@@ -2320,6 +2405,16 @@ export_xlat_presets(GValue *presets)
        }
 }
 
+static guint prefs_timeout_id = 0;
+
+static gboolean
+delayed_store_prefs(gpointer data)
+{
+       store_plist(prefsPlist, "preferences");
+       prefs_timeout_id = 0;
+       return FALSE;
+}
+
 static void
 store_presets()
 {
@@ -2331,6 +2426,22 @@ store_presets()
        ghb_value_free(export);
 }
 
+static void
+store_prefs(void)
+{
+       if (prefs_timeout_id != 0)
+       {
+               GMainContext *mc;
+               GSource *source;
+
+               mc = g_main_context_default();
+               source = g_main_context_find_source_by_id(mc, prefs_timeout_id);
+               if (source != NULL)
+                       g_source_destroy(source);
+       }
+       prefs_timeout_id = g_timeout_add_seconds(1, (GSourceFunc)delayed_store_prefs, NULL);
+}
+
 void
 ghb_presets_reload(signal_user_data_t *ud)
 {
@@ -2461,22 +2572,6 @@ settings_save(signal_user_data_t *ud, const GValue *path)
                }
        }
        current_preset = dict;
-       if (ghb_settings_get_boolean(ud->settings, "allow_tweaks"))
-       {
-               gchar *str;
-               str = ghb_settings_get_string(ud->settings, "tweak_PictureDeinterlace");
-               if (str)
-               {
-                       ghb_settings_set_string(ud->settings, "PictureDeinterlace", str);
-                       g_free(str);
-               }
-               str = ghb_settings_get_string(ud->settings, "tweak_PictureDenoise");
-               if (str)
-               {
-                       ghb_settings_set_string(ud->settings, "PictureDenoise", str);
-                       g_free(str);
-               }
-       }
        autoscale = ghb_settings_get_boolean(ud->settings, "autoscale");
        ghb_settings_set_int64(ud->settings, "Type", PRESETS_CUSTOM);
 
@@ -3117,6 +3212,52 @@ presets_drag_cb(
        }
 }
 
+void
+presets_row_expanded_cb(
+       GtkTreeView *treeview, 
+       GtkTreeIter *iter, 
+       GtkTreePath *path, 
+       signal_user_data_t *ud)
+{
+       gint *indices, len;
+       gboolean expanded, folder;
+
+       expanded = gtk_tree_view_row_expanded(treeview, path);
+       indices = gtk_tree_path_get_indices(path);
+       len = gtk_tree_path_get_depth(path);
+       folder = ghb_presets_get_folder(presetsPlist, indices, len);
+       if (folder)
+       {
+               presets_set_folder_open(expanded, indices, len);
+       }
+
+       // Collapsing parent folder collapses all children
+       if (!expanded)
+       {
+               GValue *presets = NULL;
+               GValue *dict;
+               gint *more_indices, count, ii;
+
+               more_indices = g_malloc((len+1)*sizeof(gint));
+               memcpy(more_indices, indices, len*sizeof(gint));
+
+               presets = presets_get_folder(presetsPlist, indices, len);
+               count = ghb_array_len(presets);
+               for (ii = 0; ii < count; ii++)
+               {
+                       dict = ghb_array_get_nth(presets, ii);
+                       folder = ghb_preset_folder(dict);
+                       if (folder)
+                       {
+                               more_indices[len] = ii;
+                               presets_set_folder_open(expanded, more_indices, len+1);
+                       }
+               }
+               g_free(more_indices);
+       }
+       store_presets();
+}
+
 static void
 preset_update_title_deps(signal_user_data_t *ud, ghb_title_info_t *tinfo)
 {
@@ -3125,10 +3266,11 @@ preset_update_title_deps(signal_user_data_t *ud, ghb_title_info_t *tinfo)
        ghb_ui_update(ud, "scale_width", 
                        ghb_int64_value(tinfo->width - tinfo->crop[2] - tinfo->crop[3]));
        // If anamorphic or keep_aspect, the hight will be automatically calculated
-       gboolean keep_aspect, anamorphic;
+       gboolean keep_aspect;
+       gint pic_par;
        keep_aspect = ghb_settings_get_boolean(ud->settings, "PictureKeepRatio");
-       anamorphic = ghb_settings_get_boolean(ud->settings, "anamorphic");
-       if (!(keep_aspect || anamorphic))
+       pic_par = ghb_settings_combo_int(ud->settings, "PicturePAR");
+       if (!(keep_aspect || pic_par) || pic_par == 3)
        {
                ghb_ui_update(ud, "scale_height", 
                        ghb_int64_value(tinfo->height - tinfo->crop[0] - tinfo->crop[1]));
@@ -3209,10 +3351,14 @@ presets_list_selection_changed_cb(GtkTreeSelection *selection, signal_user_data_
 
                        gdouble vqmin, vqmax, step, page;
                        gint digits;
-                       ghb_vquality_range(ud, &vqmin, &vqmax, &step, &page, &digits);
+                       gboolean inverted;
+
+                       ghb_vquality_range(ud, &vqmin, &vqmax, &step, 
+                                                               &page, &digits, &inverted);
                        gtk_range_set_range (GTK_RANGE(qp), vqmin, vqmax);
                        gtk_range_set_increments (GTK_RANGE(qp), step, page);
                        gtk_scale_set_digits(GTK_SCALE(qp), digits);
+                       gtk_range_set_inverted (GTK_RANGE(qp), inverted);
 
                        gchar *text;
                        gint crop[4];