5 * Copyright (C) <2006> Mark Nauwelaerts <manauw@skynet.be>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
29 #include "gstavvidenc.h"
33 #include <libavutil/opt.h>
35 static GQuark avoption_quark;
36 static GHashTable *generic_overrides = NULL;
39 make_generic_overrides (void)
41 g_assert (!generic_overrides);
42 generic_overrides = g_hash_table_new_full (g_str_hash, g_str_equal,
43 g_free, (GDestroyNotify) gst_structure_free);
45 g_hash_table_insert (generic_overrides, g_strdup ("b"),
46 gst_structure_new_empty ("bitrate"));
47 g_hash_table_insert (generic_overrides, g_strdup ("ab"),
48 gst_structure_new_empty ("bitrate"));
49 g_hash_table_insert (generic_overrides, g_strdup ("g"),
50 gst_structure_new_empty ("gop-size"));
51 g_hash_table_insert (generic_overrides, g_strdup ("bt"),
52 gst_structure_new_empty ("bitrate-tolerance"));
53 g_hash_table_insert (generic_overrides, g_strdup ("bf"),
54 gst_structure_new_empty ("max-bframes"));
56 /* Those are exposed through caps */
57 g_hash_table_insert (generic_overrides, g_strdup ("profile"),
58 gst_structure_new ("profile", "skip", G_TYPE_BOOLEAN, TRUE, NULL));
59 g_hash_table_insert (generic_overrides, g_strdup ("level"),
60 gst_structure_new ("level", "skip", G_TYPE_BOOLEAN, TRUE, NULL));
61 g_hash_table_insert (generic_overrides, g_strdup ("color_primaries"),
62 gst_structure_new ("color_primaries", "skip", G_TYPE_BOOLEAN, TRUE,
64 g_hash_table_insert (generic_overrides, g_strdup ("color_trc"),
65 gst_structure_new ("color_trc", "skip", G_TYPE_BOOLEAN, TRUE, NULL));
66 g_hash_table_insert (generic_overrides, g_strdup ("colorspace"),
67 gst_structure_new ("colorspace", "skip", G_TYPE_BOOLEAN, TRUE, NULL));
68 g_hash_table_insert (generic_overrides, g_strdup ("color_range"),
69 gst_structure_new ("color_range", "skip", G_TYPE_BOOLEAN, TRUE, NULL));
73 gst_ffmpeg_cfg_init (void)
75 avoption_quark = g_quark_from_static_string ("ffmpeg-cfg-param-spec-data");
76 make_generic_overrides ();
80 cmp_enum_value (GEnumValue * val1, GEnumValue * val2)
82 if (val1->value == val2->value)
84 return (val1->value > val2->value) ? 1 : -1;
88 register_enum (const AVClass ** obj, const AVOption * top_opt)
90 const AVOption *opt = NULL;
92 GArray *values = g_array_new (TRUE, TRUE, sizeof (GEnumValue));
93 gchar *lower_obj_name = g_ascii_strdown ((*obj)->class_name, -1);
94 gchar *enum_name = g_strdup_printf ("%s-%s", lower_obj_name, top_opt->unit);
95 gboolean none_default = TRUE;
96 const gchar *enum_name_strip;
98 g_strcanon (enum_name, G_CSET_a_2_z G_CSET_DIGITS, '-');
100 /* strip leading '-'s */
101 enum_name_strip = enum_name;
102 while (enum_name_strip[0] == '-')
105 if (enum_name_strip[0] == '\0')
108 if ((res = g_type_from_name (enum_name_strip)))
111 while ((opt = av_opt_next (obj, opt))) {
112 if (opt->type == AV_OPT_TYPE_CONST && !g_strcmp0 (top_opt->unit, opt->unit)) {
115 val.value = opt->default_val.i64;
116 val.value_name = g_strdup (opt->help ? opt->help : opt->name);
117 val.value_nick = g_strdup (opt->name);
119 if (opt->default_val.i64 == top_opt->default_val.i64)
120 none_default = FALSE;
122 g_array_append_val (values, val);
129 gboolean cur_val_set = FALSE;
131 /* Sometimes ffmpeg sets a default value but no named constants with
132 * this value, we assume this means "unspecified" and add our own
137 val.value = top_opt->default_val.i64;
138 val.value_name = g_strdup ("Unspecified");
139 val.value_nick = g_strdup ("unknown");
140 g_array_append_val (values, val);
143 g_array_sort (values, (GCompareFunc) cmp_enum_value);
145 /* Dedup, easy once sorted
146 * We do this because ffmpeg can expose multiple names for the
147 * same constant, the way we expose enums makes this too confusing.
149 while (i < values->len) {
151 if (g_array_index (values, GEnumValue, i).value == cur_val) {
152 GEnumValue val = g_array_index (values, GEnumValue, i);
153 /* Don't leak the strings */
154 g_free ((gchar *) val.value_name);
155 g_free ((gchar *) val.value_nick);
156 g_array_remove_index (values, i);
158 cur_val = g_array_index (values, GEnumValue, i).value;
162 cur_val = g_array_index (values, GEnumValue, i).value;
168 res = g_enum_register_static (enum_name_strip,
169 &g_array_index (values, GEnumValue, 0));
171 gst_type_mark_as_plugin_api (res, 0);
175 g_free (lower_obj_name);
181 cmp_flags_value (GFlagsValue * val1, GFlagsValue * val2)
183 if (val1->value == val2->value)
185 return (val1->value > val2->value) ? 1 : -1;
189 register_flags (const AVClass ** obj, const AVOption * top_opt)
191 const AVOption *opt = NULL;
193 GArray *values = g_array_new (TRUE, TRUE, sizeof (GEnumValue));
194 gchar *lower_obj_name = g_ascii_strdown ((*obj)->class_name, -1);
195 gchar *flags_name = g_strdup_printf ("%s-%s", lower_obj_name, top_opt->unit);
196 const gchar *flags_name_strip;
198 g_strcanon (flags_name, G_CSET_a_2_z G_CSET_DIGITS, '-');
200 /* strip leading '-'s */
201 flags_name_strip = flags_name;
202 while (flags_name_strip[0] == '-')
205 if (flags_name_strip[0] == '\0')
208 if ((res = g_type_from_name (flags_name_strip)))
211 while ((opt = av_opt_next (obj, opt))) {
212 if (opt->type == AV_OPT_TYPE_CONST && !g_strcmp0 (top_opt->unit, opt->unit)) {
215 /* We expose pass manually, hardcoding this isn't very nice, but
216 * I don't expect we want to do that sort of things often enough
217 * to warrant a general mechanism
219 if (!g_strcmp0 (top_opt->name, "flags")) {
220 if (opt->default_val.i64 == AV_CODEC_FLAG_QSCALE ||
221 opt->default_val.i64 == AV_CODEC_FLAG_PASS1 ||
222 opt->default_val.i64 == AV_CODEC_FLAG_PASS2) {
227 val.value = opt->default_val.i64;
228 val.value_name = g_strdup (opt->help ? opt->help : opt->name);
229 val.value_nick = g_strdup (opt->name);
231 g_array_append_val (values, val);
236 g_array_sort (values, (GCompareFunc) cmp_flags_value);
239 g_flags_register_static (flags_name_strip, &g_array_index (values,
242 gst_type_mark_as_plugin_api (res, 0);
246 g_free (lower_obj_name);
252 install_opts (GObjectClass * gobject_class, const AVClass ** obj, guint prop_id,
253 gint flags, const gchar * extra_help, GHashTable * overrides)
255 const AVOption *opt = NULL;
257 while ((opt = av_opt_next (obj, opt))) {
258 GParamSpec *pspec = NULL;
260 gdouble min = G_MINDOUBLE;
261 gdouble max = G_MAXDOUBLE;
265 if (overrides && g_hash_table_contains (overrides, opt->name)) {
267 const GstStructure *s =
268 (GstStructure *) g_hash_table_lookup (overrides, opt->name);
270 name = gst_structure_get_name (s);
271 if (gst_structure_get_boolean (s, "skip", &skip) && skip) {
278 if ((opt->flags & flags) != flags)
281 if (g_object_class_find_property (gobject_class, name))
284 if (av_opt_query_ranges (&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
285 if (r->nb_ranges == 1) {
286 min = r->range[0]->value_min;
287 max = r->range[0]->value_max;
289 av_opt_freep_ranges (&r);
292 help = g_strdup_printf ("%s%s", opt->help, extra_help);
295 case AV_OPT_TYPE_INT:
298 enum_gtype = register_enum (obj, opt);
301 pspec = g_param_spec_enum (name, name, help,
302 enum_gtype, opt->default_val.i64, G_PARAM_READWRITE);
303 g_object_class_install_property (gobject_class, prop_id++, pspec);
304 } else { /* Some options have a unit but no named constants associated */
305 pspec = g_param_spec_int (name, name, help,
306 (gint) min, (gint) max, opt->default_val.i64,
308 g_object_class_install_property (gobject_class, prop_id++, pspec);
311 pspec = g_param_spec_int (name, name, help,
312 (gint) min, (gint) max, opt->default_val.i64, G_PARAM_READWRITE);
313 g_object_class_install_property (gobject_class, prop_id++, pspec);
316 case AV_OPT_TYPE_FLAGS:
319 flags_gtype = register_flags (obj, opt);
322 pspec = g_param_spec_flags (name, name, help,
323 flags_gtype, opt->default_val.i64, G_PARAM_READWRITE);
324 g_object_class_install_property (gobject_class, prop_id++, pspec);
328 case AV_OPT_TYPE_DURATION: /* Fall through */
329 case AV_OPT_TYPE_INT64:
330 /* FIXME 2.0: Workaround for worst property related API change. We
331 * continue using a 32 bit integer for the bitrate property as
332 * otherwise too much existing code will fail at runtime.
334 * See https://gitlab.freedesktop.org/gstreamer/gst-libav/issues/41#note_142808 */
335 if (g_strcmp0 (name, "bitrate") == 0) {
336 pspec = g_param_spec_int (name, name, help,
337 (gint) MAX (min, G_MININT), (gint) MIN (max, G_MAXINT),
338 (gint) opt->default_val.i64, G_PARAM_READWRITE);
340 /* ffmpeg expresses all ranges with doubles, this is sad */
341 pspec = g_param_spec_int64 (name, name, help,
342 (min == (gdouble) INT64_MIN ? INT64_MIN : (gint64) min),
343 (max == (gdouble) INT64_MAX ? INT64_MAX : (gint64) max),
344 opt->default_val.i64, G_PARAM_READWRITE);
346 g_object_class_install_property (gobject_class, prop_id++, pspec);
348 case AV_OPT_TYPE_DOUBLE:
349 pspec = g_param_spec_double (name, name, help,
350 min, max, opt->default_val.dbl, G_PARAM_READWRITE);
351 g_object_class_install_property (gobject_class, prop_id++, pspec);
353 case AV_OPT_TYPE_FLOAT:
354 pspec = g_param_spec_float (name, name, help,
355 (gfloat) min, (gfloat) max, (gfloat) opt->default_val.dbl,
357 g_object_class_install_property (gobject_class, prop_id++, pspec);
359 case AV_OPT_TYPE_STRING:
360 pspec = g_param_spec_string (name, name, help,
361 opt->default_val.str, G_PARAM_READWRITE);
362 g_object_class_install_property (gobject_class, prop_id++, pspec);
364 case AV_OPT_TYPE_UINT64:
365 /* ffmpeg expresses all ranges with doubles, this is appalling */
366 pspec = g_param_spec_uint64 (name, name, help,
367 (guint64) (min <= (gdouble) 0 ? 0 : (guint64) min),
369 /* Biggest value before UINT64_MAX that can be represented as double */
370 (gdouble) 18446744073709550000.0 ?
371 /* The Double conversion rounds UINT64_MAX to a bigger */
372 /* value, so the following smaller limit must be used. */
373 G_GUINT64_CONSTANT (18446744073709550000) : (guint64) max),
374 opt->default_val.i64, G_PARAM_READWRITE);
375 g_object_class_install_property (gobject_class, prop_id++, pspec);
377 case AV_OPT_TYPE_BOOL:
378 pspec = g_param_spec_boolean (name, name, help,
379 opt->default_val.i64 ? TRUE : FALSE, G_PARAM_READWRITE);
380 g_object_class_install_property (gobject_class, prop_id++, pspec);
382 /* TODO: didn't find options for the video encoders with
383 * the following type, add support if needed */
384 case AV_OPT_TYPE_CHANNEL_LAYOUT:
385 case AV_OPT_TYPE_COLOR:
386 case AV_OPT_TYPE_VIDEO_RATE:
387 case AV_OPT_TYPE_SAMPLE_FMT:
388 case AV_OPT_TYPE_PIXEL_FMT:
389 case AV_OPT_TYPE_IMAGE_SIZE:
390 case AV_OPT_TYPE_DICT:
391 case AV_OPT_TYPE_BINARY:
392 case AV_OPT_TYPE_RATIONAL:
400 g_param_spec_set_qdata (pspec, avoption_quark, (gpointer) opt);
408 gst_ffmpeg_cfg_install_properties (GObjectClass * klass, AVCodec * in_plugin,
409 guint base, gint flags)
415 g_return_if_fail (base > 0);
417 ctx = avcodec_alloc_context3 (in_plugin);
419 g_warning ("could not get context");
422 install_opts ((GObjectClass *) klass, &in_plugin->priv_class, prop_id, 0,
423 " (Private codec option)", NULL);
425 install_opts ((GObjectClass *) klass, &ctx->av_class, prop_id, flags,
426 " (Generic codec option, might have no effect)", generic_overrides);
429 gst_ffmpeg_avcodec_close (ctx);
435 set_option_value (AVCodecContext * ctx, GParamSpec * pspec,
436 const GValue * value, const AVOption * opt)
440 switch (G_PARAM_SPEC_VALUE_TYPE (pspec)) {
442 res = av_opt_set_int (ctx, opt->name,
443 g_value_get_int (value), AV_OPT_SEARCH_CHILDREN);
446 res = av_opt_set_int (ctx, opt->name,
447 g_value_get_int64 (value), AV_OPT_SEARCH_CHILDREN);
450 res = av_opt_set_int (ctx, opt->name,
451 g_value_get_uint64 (value), AV_OPT_SEARCH_CHILDREN);
454 res = av_opt_set_double (ctx, opt->name,
455 g_value_get_double (value), AV_OPT_SEARCH_CHILDREN);
458 res = av_opt_set_double (ctx, opt->name,
459 g_value_get_float (value), AV_OPT_SEARCH_CHILDREN);
462 res = av_opt_set (ctx, opt->name,
463 g_value_get_string (value), AV_OPT_SEARCH_CHILDREN);
464 /* Some code in FFmpeg returns ENOMEM if the string is NULL:
465 * *dst = av_strdup(val);
466 * return *dst ? 0 : AVERROR(ENOMEM);
467 * That makes little sense, let's ignore that
469 if (!g_value_get_string (value))
473 res = av_opt_set_int (ctx, opt->name,
474 g_value_get_boolean (value), AV_OPT_SEARCH_CHILDREN);
477 if (G_IS_PARAM_SPEC_ENUM (pspec)) {
478 res = av_opt_set_int (ctx, opt->name,
479 g_value_get_enum (value), AV_OPT_SEARCH_CHILDREN);
480 } else if (G_IS_PARAM_SPEC_FLAGS (pspec)) {
481 res = av_opt_set_int (ctx, opt->name,
482 g_value_get_flags (value), AV_OPT_SEARCH_CHILDREN);
483 } else { /* oops, bit lazy we don't cover this case yet */
484 g_critical ("%s does not yet support type %s", GST_FUNCTION,
485 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
493 gst_ffmpeg_cfg_set_property (AVCodecContext * refcontext, const GValue * value,
498 opt = g_param_spec_get_qdata (pspec, avoption_quark);
503 return set_option_value (refcontext, pspec, value, opt) >= 0;
507 gst_ffmpeg_cfg_get_property (AVCodecContext * refcontext, GValue * value,
513 opt = g_param_spec_get_qdata (pspec, avoption_quark);
518 switch (G_PARAM_SPEC_VALUE_TYPE (pspec)) {
522 if ((res = av_opt_get_int (refcontext, opt->name,
523 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
524 g_value_set_int (value, val);
530 if ((res = av_opt_get_int (refcontext, opt->name,
531 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
532 g_value_set_int64 (value, val);
538 if ((res = av_opt_get_int (refcontext, opt->name,
539 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
540 g_value_set_uint64 (value, val);
546 if ((res = av_opt_get_double (refcontext, opt->name,
547 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
548 g_value_set_double (value, val);
554 if ((res = av_opt_get_double (refcontext, opt->name,
555 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
556 g_value_set_float (value, (gfloat) val);
562 if ((res = av_opt_get (refcontext, opt->name,
563 AV_OPT_SEARCH_CHILDREN | AV_OPT_ALLOW_NULL, &val) >= 0)) {
564 g_value_set_string (value, (gchar *) val);
571 if ((res = av_opt_get_int (refcontext, opt->name,
572 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
573 g_value_set_boolean (value, val ? TRUE : FALSE);
577 if (G_IS_PARAM_SPEC_ENUM (pspec)) {
580 if ((res = av_opt_get_int (refcontext, opt->name,
581 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
582 g_value_set_enum (value, val);
583 } else if (G_IS_PARAM_SPEC_FLAGS (pspec)) {
586 if ((res = av_opt_get_int (refcontext, opt->name,
587 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
588 g_value_set_flags (value, val);
589 } else { /* oops, bit lazy we don't cover this case yet */
590 g_critical ("%s does not yet support type %s", GST_FUNCTION,
591 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
599 gst_ffmpeg_cfg_fill_context (GObject * object, AVCodecContext * context)
604 pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object),
607 for (i = 0; i < num_props; ++i) {
608 GParamSpec *pspec = pspecs[i];
610 GValue value = G_VALUE_INIT;
612 opt = g_param_spec_get_qdata (pspec, avoption_quark);
617 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
618 g_object_get_property (object, pspec->name, &value);
619 set_option_value (context, pspec, &value, opt);
620 g_value_unset (&value);
626 gst_ffmpeg_cfg_finalize (void)
628 GST_ERROR ("Finalizing");
629 g_assert (generic_overrides);
630 g_hash_table_unref (generic_overrides);