OSDN Git Service

6ad1cfe93884a345f8637c9353ed42225d1d2403
[handbrake-jp/handbrake-jp-git.git] / gtk / src / x264handler.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * x264handler.c
4  * Copyright (C) John Stebbins 2008 <stebbins@stebbins>
5  * 
6  * x264handler.c is free software.
7  * 
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)
11  * any later version.
12  */
13
14 #include <gtk/gtk.h>
15 #include <string.h>
16 #include "settings.h"
17 #include "values.h"
18 #include "callbacks.h"
19 #include "presets.h"
20 #include "hb-backend.h"
21 #include "x264handler.h"
22
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);
26
27 // Flag needed to prevent x264 options processing from chasing its tail
28 static gboolean ignore_options_update = FALSE;
29
30 G_MODULE_EXPORT void
31 x264_widget_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
32 {
33         ghb_widget_to_setting(ud->settings, widget);
34         if (!ignore_options_update)
35         {
36                 ignore_options_update = TRUE;
37                 x264_opt_update(ud, widget);
38                 ignore_options_update = FALSE;
39         }
40         ghb_check_dependency(ud, widget);
41         ghb_clear_presets_selection(ud);
42 }
43
44 G_MODULE_EXPORT void
45 x264_me_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
46 {
47         gint me;
48
49         ghb_widget_to_setting(ud->settings, widget);
50         if (!ignore_options_update)
51         {
52                 ignore_options_update = TRUE;
53                 x264_opt_update(ud, widget);
54                 ignore_options_update = FALSE;
55         }
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");
60         if (me < 2)
61         {       // me < umh
62                 // me_range 4 - 16
63                 gtk_spin_button_set_range(GTK_SPIN_BUTTON(widget), 4, 16);
64         }
65         else
66         {
67                 // me_range 4 - 64
68                 gtk_spin_button_set_range(GTK_SPIN_BUTTON(widget), 4, 64);
69         }
70 }
71
72 G_MODULE_EXPORT void
73 x264_entry_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
74 {
75         g_debug("x264_entry_changed_cb ()");
76         if (!ignore_options_update)
77         {
78                 GtkWidget *textview;
79                 gchar *options;
80
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))
87                 {
88                         gchar *sopts;
89
90                         sopts = sanitize_x264opts(ud, options);
91                         ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
92                         ghb_x264_parse_options(ud, sopts);
93                         g_free(sopts);
94                 }
95                 g_free(options);
96                 ignore_options_update = FALSE;
97         }
98 }
99
100 G_MODULE_EXPORT gboolean
101 x264_focus_out_cb(GtkWidget *widget, GdkEventFocus *event, 
102         signal_user_data_t *ud)
103 {
104         gchar *options, *sopts;
105
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)
111         {
112                 ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
113                 ghb_x264_parse_options(ud, sopts);
114         }
115         g_free(options);
116         g_free(sopts);
117         ignore_options_update = FALSE;
118         return FALSE;
119 }
120
121 enum
122 {
123         X264_OPT_NONE,
124         X264_OPT_DEBLOCK,
125         X264_OPT_PSY,
126         X264_OPT_INT,
127         X264_OPT_COMBO,
128         X264_OPT_BOOL,
129 };
130
131 struct x264_opt_map_s
132 {
133         gchar **opt_syns;
134         gchar *name;
135         gchar *def_val;
136         gint type;
137         gboolean found;
138 };
139
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};
162
163 static gint
164 find_syn_match(const gchar *opt, gchar **syns)
165 {
166         gint ii;
167         for (ii = 0; syns[ii] != NULL; ii++)
168         {
169                 if (strcmp(opt, syns[ii]) == 0)
170                         return ii;
171         }
172         return -1;
173 }
174
175 struct x264_opt_map_s x264_opt_map[] =
176 {
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_BOOL},
198         {x264_psy_syns, "x264_psy_trell", "1,0", X264_OPT_PSY},
199 };
200 #define X264_OPT_MAP_SIZE (sizeof(x264_opt_map)/sizeof(struct x264_opt_map_s))
201
202 static const gchar*
203 x264_opt_get_default(const gchar *opt)
204 {
205         gint jj;
206         for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
207         {
208                 if (find_syn_match(opt, x264_opt_map[jj].opt_syns) >= 0)
209                 {
210                         return x264_opt_map[jj].def_val;
211                 }
212         }
213         return "";
214 }
215
216 static void
217 x264_update_int(signal_user_data_t *ud, const gchar *name, const gchar *val)
218 {
219         gint ival;
220
221         if (val == NULL) return;
222         ival = g_strtod (val, NULL);
223         ghb_ui_update(ud, name, ghb_int64_value(ival));
224 }
225
226 static gchar *true_str[] =
227 {
228         "true",
229         "yes",
230         "1",
231         NULL
232 };
233
234 static gboolean 
235 str_is_true(const gchar *str)
236 {
237         gint ii;
238         for (ii = 0; true_str[ii]; ii++)
239         {
240                 if (g_ascii_strcasecmp(str, true_str[ii]) == 0)
241                         return TRUE;
242         }
243         return FALSE;
244 }
245
246 static void
247 x264_update_bool(signal_user_data_t *ud, const gchar *name, const gchar *val)
248 {
249         if (val == NULL)
250                 ghb_ui_update(ud, name, ghb_boolean_value(1));
251         else
252                 ghb_ui_update(ud, name, ghb_boolean_value(str_is_true(val)));
253 }
254
255 static void
256 x264_update_combo(signal_user_data_t *ud, const gchar *name, const gchar *val)
257 {
258         GtkTreeModel *store;
259         GtkTreeIter iter;
260         gchar *shortOpt;
261         gdouble ivalue;
262         gboolean foundit = FALSE;
263         GtkWidget *widget;
264
265         if (val == NULL) return;
266         widget = GHB_WIDGET(ud->builder, name);
267         if (widget == NULL)
268         {
269                 g_debug("Failed to find widget for key: %s\n", name);
270                 return;
271         }
272         store = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
273         if (gtk_tree_model_get_iter_first (store, &iter))
274         {
275                 do
276                 {
277                         gtk_tree_model_get(store, &iter, 2, &shortOpt, 3, &ivalue, -1);
278                         if (strcmp(shortOpt, val) == 0)
279                         {
280                                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), &iter);
281                                 g_free(shortOpt);
282                                 foundit = TRUE;
283                                 break;
284                         }
285                         g_free(shortOpt);
286                 } while (gtk_tree_model_iter_next (store, &iter));
287         }
288         if (!foundit)
289         {
290                 if (gtk_tree_model_get_iter_first (store, &iter))
291                 {
292                         do
293                         {
294                                 gtk_tree_model_get(store, &iter, 2, &shortOpt, 3, &ivalue, -1);
295                                 if (strcmp(shortOpt, "custom") == 0)
296                                 {
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);
299                                         g_free(shortOpt);
300                                         foundit = TRUE;
301                                         break;
302                                 }
303                                 g_free(shortOpt);
304                         } while (gtk_tree_model_iter_next (store, &iter));
305                 }
306         }
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);
310 }
311
312 static void
313 x264_update_deblock(signal_user_data_t *ud, const gchar *xval)
314 {
315         gdouble avalue, bvalue;
316         gchar *end;
317         gchar *val;
318         gchar *bval = NULL;
319
320         if (xval == NULL) return;
321         val = g_strdup(xval);
322         bvalue = avalue = 0;
323         if (val != NULL) 
324         {
325                 gchar *pos = strchr(val, ',');
326                 if (pos != NULL)
327                 {
328                         bval = pos + 1;
329                         *pos = 0;
330                 }
331                 avalue = g_strtod (val, &end);
332                 if (bval != NULL)
333                 {
334                         bvalue = g_strtod (bval, &end);
335                 }
336         }
337         g_free(val);
338         ghb_ui_update(ud, "x264_deblock_alpha", ghb_int64_value(avalue));
339         ghb_ui_update(ud, "x264_deblock_beta", ghb_int64_value(bvalue));
340 }
341
342 static void
343 x264_parse_psy(const gchar *psy, gdouble *psy_rd, gdouble *psy_trell)
344 {
345         gchar *val;
346         gchar *trell_val = NULL;
347         gchar *end;
348
349         *psy_rd = 0.;
350         *psy_trell = 0.;
351         if (psy == NULL) return;
352         val = g_strdup(psy);
353         gchar *pos = strchr(val, ',');
354         if (pos != NULL)
355         {
356                 trell_val = pos + 1;
357                 *pos = 0;
358         }
359         *psy_rd = g_strtod (val, &end);
360         if (trell_val != NULL)
361         {
362                 *psy_trell = g_strtod (trell_val, &end);
363         }
364         g_free(val);
365 }
366
367 static void
368 x264_update_psy(signal_user_data_t *ud, const gchar *xval)
369 {
370         gdouble rd_value, trell_value;
371
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));
376 }
377
378 void
379 ghb_x264_parse_options(signal_user_data_t *ud, const gchar *options)
380 {
381         gchar **split = g_strsplit(options, ":", -1);
382         if (split == NULL) return;
383
384         gint ii;
385         gint jj;
386
387         for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
388                 x264_opt_map[jj].found = FALSE;
389
390         for (ii = 0; split[ii] != NULL; ii++)
391         {
392                 gchar *val = NULL;
393                 gchar *pos = strchr(split[ii], '=');
394                 if (pos != NULL)
395                 {
396                         val = pos + 1;
397                         *pos = 0;
398                 }
399                 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
400                 {
401                         if (find_syn_match(split[ii], x264_opt_map[jj].opt_syns) >= 0)
402                         {
403                                 x264_opt_map[jj].found = TRUE;
404                                 switch(x264_opt_map[jj].type)
405                                 {
406                                 case X264_OPT_INT:
407                                         x264_update_int(ud, x264_opt_map[jj].name, val);
408                                         break;
409                                 case X264_OPT_BOOL:
410                                         x264_update_bool(ud, x264_opt_map[jj].name, val);
411                                         break;
412                                 case X264_OPT_COMBO:
413                                         x264_update_combo(ud, x264_opt_map[jj].name, val);
414                                         break;
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);
419                                         break;
420                                 case X264_OPT_PSY:
421                                         // dirty little hack.  mark psy_trell found as well
422                                         x264_opt_map[jj+1].found = TRUE;
423                                         x264_update_psy(ud, val);
424                                         break;
425                                 }
426                                 break;
427                         }
428                 }
429         }
430         // For any options not found in the option string, set ui to
431         // default values
432         for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
433         {
434                 if (!x264_opt_map[jj].found)
435                 {
436                         gchar *val;
437
438                         if (x264_opt_map[jj].opt_syns == x264_mbtree_syns)
439                         {
440                                 int bframes = ghb_lookup_bframes(options);
441                                 if (bframes > 0)
442                                         val = strdup("1");
443                                 else
444                                         val = strdup("0");
445                         }
446                         else
447                         {
448                                 val = strdup(x264_opt_map[jj].def_val);
449                         }
450                         switch(x264_opt_map[jj].type)
451                         {
452                         case X264_OPT_INT:
453                                 x264_update_int(ud, x264_opt_map[jj].name, val);
454                                 break;
455                         case X264_OPT_BOOL:
456                                 x264_update_bool(ud, x264_opt_map[jj].name, val);
457                                 break;
458                         case X264_OPT_COMBO:
459                                 x264_update_combo(ud, x264_opt_map[jj].name, val);
460                                 break;
461                         case X264_OPT_DEBLOCK:
462                                 x264_update_deblock(ud, val);
463                                 break;
464                         case X264_OPT_PSY:
465                                 x264_update_psy(ud, val);
466                                 break;
467                         }
468                         x264_opt_map[jj].found = TRUE;
469                         g_free(val);
470                 }
471         }
472         g_strfreev(split);
473 }
474
475 gchar*
476 get_deblock_val(signal_user_data_t *ud)
477 {
478         gchar *alpha, *beta;
479         gchar *result;
480         alpha = ghb_settings_get_string(ud->settings, "x264_deblock_alpha");
481         beta = ghb_settings_get_string(ud->settings, "x264_deblock_beta");
482         result = g_strdup_printf("%s,%s", alpha, beta);
483         g_free(alpha);
484         g_free(beta);
485         return result;
486 }
487
488 gchar*
489 get_psy_val(signal_user_data_t *ud)
490 {
491         gdouble rd, trell;
492         gchar *result;
493         rd = ghb_settings_get_double(ud->settings, "x264_psy_rd");
494         trell = ghb_settings_get_double(ud->settings, "x264_psy_trell");
495         result = g_strdup_printf("%g,%g", rd, trell);
496         return result;
497 }
498
499 static void
500 x264_opt_update(signal_user_data_t *ud, GtkWidget *widget)
501 {
502         gint jj;
503         const gchar *name = gtk_widget_get_name(widget);
504         gchar **opt_syns = NULL;
505         const gchar *def_val = NULL;
506         gint type;
507
508         for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
509         {
510                 if (strcmp(name, x264_opt_map[jj].name) == 0)
511                 {
512                         // found the options that needs updating
513                         opt_syns = x264_opt_map[jj].opt_syns;
514                         def_val = x264_opt_map[jj].def_val;
515                         type = x264_opt_map[jj].type;
516                         break;
517                 }
518         }
519         if (opt_syns != NULL)
520         {
521                 GString *x264opts = g_string_new("");
522                 gchar *options;
523                 gchar **split = NULL;
524                 gint ii;
525                 gboolean foundit = FALSE;
526
527                 options = ghb_settings_get_string(ud->settings, "x264Option");
528                 if (options)
529                 {
530                         split = g_strsplit(options, ":", -1);
531                         g_free(options);
532                 }
533                 for (ii = 0; split && split[ii] != NULL; ii++)
534                 {
535                         gint syn;
536                         gchar *val = NULL;
537                         gchar *pos = strchr(split[ii], '=');
538                         if (pos != NULL)
539                         {
540                                 val = pos + 1;
541                                 *pos = 0;
542                         }
543                         syn = find_syn_match(split[ii], opt_syns);
544                         if (syn >= 0)
545                         { // Updating this option
546                                 gchar *val;
547                                 foundit = TRUE;
548                                 if (type == X264_OPT_DEBLOCK)
549                                         val = get_deblock_val(ud);
550                                 else if (type == X264_OPT_PSY)
551                                         val = get_psy_val(ud);
552                                 else
553                                 {
554                                         GValue *gval;
555                                         gval = ghb_widget_value(widget);
556                                         if (G_VALUE_TYPE(gval) == G_TYPE_BOOLEAN)
557                                         {
558                                                 if (ghb_value_boolean(gval))
559                                                         val = g_strdup("1");
560                                                 else
561                                                         val = g_strdup("0");
562                                         }
563                                         else
564                                         {
565                                                 val = ghb_widget_string(widget);
566                                         }
567                                         ghb_value_free(gval);
568                                 }
569                                 if (strcmp(def_val, val) != 0)
570                                 {
571                                         g_string_append_printf(x264opts, "%s=%s:", opt_syns[syn], val);
572                                 }
573                                 g_free(val);
574                         }
575                         else if (val != NULL)
576                                 g_string_append_printf(x264opts, "%s=%s:", split[ii], val);
577                         else
578                                 g_string_append_printf(x264opts, "%s:", split[ii]);
579
580                 }
581                 if (split) g_strfreev(split);
582                 if (!foundit)
583                 {
584                         gchar *val;
585                         if (type == X264_OPT_DEBLOCK)
586                                 val = get_deblock_val(ud);
587                         else if (type == X264_OPT_PSY)
588                                 val = get_psy_val(ud);
589                         else
590                         {
591                                 GValue *gval;
592                                 gval = ghb_widget_value(widget);
593                                 if (G_VALUE_TYPE(gval) == G_TYPE_BOOLEAN)
594                                 {
595                                         if (ghb_value_boolean(gval))
596                                                 val = g_strdup("1");
597                                         else
598                                                 val = g_strdup("0");
599                                 }
600                                 else
601                                 {
602                                         val = ghb_widget_string(widget);
603                                 }
604                                 ghb_value_free(gval);
605                         }
606                         if (strcmp(def_val, val) != 0)
607                         {
608                                 g_string_append_printf(x264opts, "%s=%s:", opt_syns[0], val);
609                         }
610                         g_free(val);
611                 }
612                 // Update the options value
613                 // strip the trailing ":"
614                 gchar *result;
615                 gint len;
616                 result = g_string_free(x264opts, FALSE);
617                 len = strlen(result);
618                 if (len > 0) result[len - 1] = 0;
619                 gchar *sopts;
620                 sopts = sanitize_x264opts(ud, result);
621                 ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
622                 ghb_x264_parse_options(ud, sopts);
623                 g_free(sopts);
624                 g_free(result);
625         }
626 }
627
628 static gint
629 x264_find_opt(gchar **opts, gchar **opt_syns)
630 {
631         gint ii;
632         for (ii = 0; opts[ii] != NULL; ii++)
633         {
634                 gchar *opt;
635                 opt = g_strdup(opts[ii]);
636                 gchar *pos = strchr(opt, '=');
637                 if (pos != NULL)
638                 {
639                         *pos = 0;
640                 }
641                 if (find_syn_match(opt, opt_syns) >= 0)
642                 {
643                         g_free(opt);
644                         return ii;
645                 }
646                 g_free(opt);
647         }
648         return -1;
649 }
650
651 static void
652 x264_remove_opt(gchar **opts, gchar **opt_syns)
653 {
654         gint ii;
655         for (ii = 0; opts[ii] != NULL; ii++)
656         {
657                 gchar *opt;
658                 opt = g_strdup(opts[ii]);
659                 gchar *pos = strchr(opt, '=');
660                 if (pos != NULL)
661                 {
662                         *pos = 0;
663                 }
664                 if (find_syn_match(opt, opt_syns) >= 0)
665                 {
666                         // Mark as deleted
667                         opts[ii][0] = 0;
668                 }
669                 g_free(opt);
670         }
671 }
672
673 static gchar*
674 x264_lookup_value(gchar **opts, gchar **opt_syns)
675 {
676         gchar *ret = NULL;
677         gint pos;
678
679         const gchar *def_val = x264_opt_get_default(opt_syns[0]);
680
681         pos = x264_find_opt(opts, opt_syns);
682         if (pos >= 0)
683         {
684                 gchar *cpos = strchr(opts[pos], '=');
685                 if (cpos != NULL)
686                 {
687                         ret = g_strdup(cpos+1);
688                 }
689                 else
690                 {
691                         ret = g_strdup("");
692                 }
693         }
694         else if (def_val != NULL)
695         {
696                 ret = g_strdup(def_val);
697         }
698         return ret;
699 }
700
701 gint
702 ghb_lookup_badapt(const gchar *options)
703 {
704         gint ret = 0;
705         gchar *result;
706         gchar **split;
707         
708         if (options == NULL)
709                 options = "";
710
711         split = g_strsplit(options, ":", -1);
712
713         result = x264_lookup_value(split, x264_badapt_syns);
714         g_strfreev(split);
715         if (result != NULL)
716         {
717                 ret = g_strtod(result, NULL);
718                 g_free(result);
719         }
720         return ret;
721 }
722
723 gint
724 ghb_lookup_aqmode(const gchar *options)
725 {
726         gint ret = 0;
727         gchar *result;
728         gchar **split;
729         
730         if (options == NULL)
731                 options = "";
732
733         split = g_strsplit(options, ":", -1);
734
735         result = x264_lookup_value(split, x264_aqmode_syns);
736         g_strfreev(split);
737         if (result != NULL)
738         {
739                 ret = g_strtod(result, NULL);
740                 g_free(result);
741         }
742         return ret;
743 }
744
745 gint
746 ghb_lookup_bframes(const gchar *options)
747 {
748         gint ret = 0;
749         gchar *result;
750         gchar **split;
751         
752         if (options == NULL)
753                 options = "";
754
755         split = g_strsplit(options, ":", -1);
756
757         result = x264_lookup_value(split, x264_bframes_syns);
758         g_strfreev(split);
759         if (result != NULL)
760         {
761                 ret = g_strtod(result, NULL);
762                 g_free(result);
763         }
764         return ret;
765 }
766
767 // Construct the x264 options string
768 // The result is allocated, so someone must free it at some point.
769 static gchar*
770 sanitize_x264opts(signal_user_data_t *ud, const gchar *options)
771 {
772         GString *x264opts = g_string_new("");
773         gchar **split = g_strsplit(options, ":", -1);
774         gint ii;
775
776         // Fix up option dependencies
777         gboolean mbtree = ghb_settings_get_boolean(ud->settings, "x264_mbtree");
778         if (mbtree)
779         {
780                 x264_remove_opt(split, x264_bpyramid_syns);
781         }
782         gint subme = ghb_settings_combo_int(ud->settings, "x264_subme");
783         if (subme < 6)
784         {
785                 x264_remove_opt(split, x264_psy_syns);
786         }
787         gint trell = ghb_settings_combo_int(ud->settings, "x264_trellis");
788         if (subme == 10)
789         {
790                 gint aqmode = ghb_lookup_aqmode(options);
791                 if (trell != 2 || aqmode == 0)
792                 {
793                         gint pos = x264_find_opt(split, x264_subme_syns);
794                         g_free(split[pos]);
795                         split[pos] = g_strdup_printf("subme=9");
796                 }
797         }
798         if (trell < 1)
799         {
800                 gint psy;
801                 gdouble psy_rd = 0., psy_trell;
802
803                 psy = x264_find_opt(split, x264_psy_syns);
804                 if (psy >= 0)
805                 {
806                         gchar *pos = strchr(split[psy], '=');
807                         if (pos != NULL)
808                         {
809                                 x264_parse_psy(pos+1, &psy_rd, &psy_trell);
810                         }
811                         g_free(split[psy]);
812                         split[psy] = g_strdup_printf("psy-rd=%g,0", psy_rd);
813                 }
814         }
815         gint refs = ghb_settings_get_int(ud->settings, "x264_refs");
816         if (refs <= 1)
817         {
818                 x264_remove_opt(split, x264_mixed_syns);
819         }
820         gint bframes = ghb_settings_get_int(ud->settings, "x264_bframes");
821         if (bframes == 0)
822         {
823                 x264_remove_opt(split, x264_weightb_syns);
824                 x264_remove_opt(split, x264_direct_syns);
825                 x264_remove_opt(split, x264_badapt_syns);
826         }
827         if (bframes <= 1)
828         {
829                 x264_remove_opt(split, x264_bpyramid_syns);
830         }
831         if (!ghb_settings_get_boolean(ud->settings, "x264_cabac"))
832         {
833                 x264_remove_opt(split, x264_trellis_syns);
834         }
835         // Remove entries that match the defaults
836         for (ii = 0; split[ii] != NULL; ii++)
837         {
838                 gchar *val = NULL;
839                 gchar *opt = g_strdup(split[ii]);
840                 gchar *pos = strchr(opt, '=');
841                 if (pos != NULL)
842                 {
843                         val = pos + 1;
844                         *pos = 0;
845                 }
846                 else
847                 {
848                         val = "1";
849                 }
850                 const gchar *def_val;
851                 if (find_syn_match(opt, x264_mbtree_syns) >= 0 && bframes == 0)
852                 {
853                         def_val = "0";
854                 }
855                 else
856                 {
857                         def_val = x264_opt_get_default(opt);
858                 }
859                 if (strcmp(val, def_val) == 0)
860                 {
861                         // Matches the default, so remove it
862                         split[ii][0] = 0;
863                 }
864                 g_free(opt);
865         }
866         for (ii = 0; split[ii] != NULL; ii++)
867         {
868                 if (split[ii][0] != 0)
869                         g_string_append_printf(x264opts, "%s:", split[ii]);
870         }
871         g_strfreev(split);
872         // strip the trailing ":"
873         gchar *result;
874         gint len;
875         result = g_string_free(x264opts, FALSE);
876         len = strlen(result);
877         if (len > 0) result[len - 1] = 0;
878         return result;
879 }
880