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;
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 values = g_array_new (TRUE, TRUE, sizeof (GEnumValue));
113 while ((opt = av_opt_next (obj, opt))) {
114 if (opt->type == AV_OPT_TYPE_CONST && !g_strcmp0 (top_opt->unit, opt->unit)) {
117 val.value = opt->default_val.i64;
118 val.value_name = g_strdup (opt->help ? opt->help : opt->name);
119 val.value_nick = g_strdup (opt->name);
121 if (opt->default_val.i64 == top_opt->default_val.i64)
122 none_default = FALSE;
124 g_array_append_val (values, val);
131 gboolean cur_val_set = FALSE;
133 /* Sometimes ffmpeg sets a default value but no named constants with
134 * this value, we assume this means "unspecified" and add our own
139 val.value = top_opt->default_val.i64;
140 val.value_name = g_strdup ("Unspecified");
141 val.value_nick = g_strdup ("unknown");
142 g_array_append_val (values, val);
145 g_array_sort (values, (GCompareFunc) cmp_enum_value);
147 /* Dedup, easy once sorted
148 * We do this because ffmpeg can expose multiple names for the
149 * same constant, the way we expose enums makes this too confusing.
151 while (i < values->len) {
153 if (g_array_index (values, GEnumValue, i).value == cur_val) {
154 GEnumValue val = g_array_index (values, GEnumValue, i);
155 /* Don't leak the strings */
156 g_free ((gchar *) val.value_name);
157 g_free ((gchar *) val.value_nick);
158 g_array_remove_index (values, i);
160 cur_val = g_array_index (values, GEnumValue, i).value;
164 cur_val = g_array_index (values, GEnumValue, i).value;
170 res = g_enum_register_static (enum_name_strip,
171 &g_array_index (values, GEnumValue, 0));
173 gst_type_mark_as_plugin_api (res, 0);
175 g_array_free (values, FALSE);
177 g_array_free (values, TRUE);
181 g_free (lower_obj_name);
187 cmp_flags_value (GFlagsValue * val1, GFlagsValue * val2)
189 if (val1->value == val2->value)
191 return (val1->value > val2->value) ? 1 : -1;
195 register_flags (const AVClass ** obj, const AVOption * top_opt)
197 const AVOption *opt = NULL;
200 gchar *lower_obj_name = g_ascii_strdown ((*obj)->class_name, -1);
201 gchar *flags_name = g_strdup_printf ("%s-%s", lower_obj_name, top_opt->unit);
202 const gchar *flags_name_strip;
204 g_strcanon (flags_name, G_CSET_a_2_z G_CSET_DIGITS, '-');
206 /* strip leading '-'s */
207 flags_name_strip = flags_name;
208 while (flags_name_strip[0] == '-')
211 if (flags_name_strip[0] == '\0')
214 if ((res = g_type_from_name (flags_name_strip)))
217 values = g_array_new (TRUE, TRUE, sizeof (GFlagsValue));
219 while ((opt = av_opt_next (obj, opt))) {
220 if (opt->type == AV_OPT_TYPE_CONST && !g_strcmp0 (top_opt->unit, opt->unit)) {
223 /* We expose pass manually, hardcoding this isn't very nice, but
224 * I don't expect we want to do that sort of things often enough
225 * to warrant a general mechanism
227 if (!g_strcmp0 (top_opt->name, "flags")) {
228 if (opt->default_val.i64 == AV_CODEC_FLAG_QSCALE ||
229 opt->default_val.i64 == AV_CODEC_FLAG_PASS1 ||
230 opt->default_val.i64 == AV_CODEC_FLAG_PASS2) {
235 val.value = opt->default_val.i64;
236 val.value_name = g_strdup (opt->help ? opt->help : opt->name);
237 val.value_nick = g_strdup (opt->name);
239 g_array_append_val (values, val);
244 g_array_sort (values, (GCompareFunc) cmp_flags_value);
247 g_flags_register_static (flags_name_strip, &g_array_index (values,
250 gst_type_mark_as_plugin_api (res, 0);
251 g_array_free (values, FALSE);
253 g_array_free (values, TRUE);
256 g_free (lower_obj_name);
262 install_opts (GObjectClass * gobject_class, const AVClass ** obj, guint prop_id,
263 gint flags, const gchar * extra_help, GHashTable * overrides)
265 const AVOption *opt = NULL;
267 while ((opt = av_opt_next (obj, opt))) {
268 GParamSpec *pspec = NULL;
270 gdouble min = G_MINDOUBLE;
271 gdouble max = G_MAXDOUBLE;
275 if (overrides && g_hash_table_contains (overrides, opt->name)) {
277 const GstStructure *s =
278 (GstStructure *) g_hash_table_lookup (overrides, opt->name);
280 name = gst_structure_get_name (s);
281 if (gst_structure_get_boolean (s, "skip", &skip) && skip) {
288 if ((opt->flags & flags) != flags)
291 if (g_object_class_find_property (gobject_class, name))
294 if (av_opt_query_ranges (&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
295 if (r->nb_ranges == 1) {
296 min = r->range[0]->value_min;
297 max = r->range[0]->value_max;
299 av_opt_freep_ranges (&r);
302 help = g_strdup_printf ("%s%s", opt->help, extra_help);
305 case AV_OPT_TYPE_INT:
308 enum_gtype = register_enum (obj, opt);
311 pspec = g_param_spec_enum (name, name, help,
312 enum_gtype, opt->default_val.i64, G_PARAM_READWRITE);
313 g_object_class_install_property (gobject_class, prop_id++, pspec);
314 } else { /* Some options have a unit but no named constants associated */
315 pspec = g_param_spec_int (name, name, help,
316 (gint) min, (gint) max, opt->default_val.i64,
318 g_object_class_install_property (gobject_class, prop_id++, pspec);
321 pspec = g_param_spec_int (name, name, help,
322 (gint) min, (gint) max, opt->default_val.i64, G_PARAM_READWRITE);
323 g_object_class_install_property (gobject_class, prop_id++, pspec);
326 case AV_OPT_TYPE_FLAGS:
329 flags_gtype = register_flags (obj, opt);
332 pspec = g_param_spec_flags (name, name, help,
333 flags_gtype, opt->default_val.i64, G_PARAM_READWRITE);
334 g_object_class_install_property (gobject_class, prop_id++, pspec);
338 case AV_OPT_TYPE_DURATION: /* Fall through */
339 case AV_OPT_TYPE_INT64:
340 /* FIXME 2.0: Workaround for worst property related API change. We
341 * continue using a 32 bit integer for the bitrate property as
342 * otherwise too much existing code will fail at runtime.
344 * See https://gitlab.freedesktop.org/gstreamer/gst-libav/issues/41#note_142808 */
345 if (g_strcmp0 (name, "bitrate") == 0) {
346 pspec = g_param_spec_int (name, name, help,
347 (gint) MAX (min, G_MININT), (gint) MIN (max, G_MAXINT),
348 (gint) opt->default_val.i64, G_PARAM_READWRITE);
350 /* ffmpeg expresses all ranges with doubles, this is sad */
351 pspec = g_param_spec_int64 (name, name, help,
352 (min == (gdouble) INT64_MIN ? INT64_MIN : (gint64) min),
353 (max == (gdouble) INT64_MAX ? INT64_MAX : (gint64) max),
354 opt->default_val.i64, G_PARAM_READWRITE);
356 g_object_class_install_property (gobject_class, prop_id++, pspec);
358 case AV_OPT_TYPE_DOUBLE:
359 pspec = g_param_spec_double (name, name, help,
360 min, max, opt->default_val.dbl, G_PARAM_READWRITE);
361 g_object_class_install_property (gobject_class, prop_id++, pspec);
363 case AV_OPT_TYPE_FLOAT:
364 pspec = g_param_spec_float (name, name, help,
365 (gfloat) min, (gfloat) max, (gfloat) opt->default_val.dbl,
367 g_object_class_install_property (gobject_class, prop_id++, pspec);
369 case AV_OPT_TYPE_STRING:
370 pspec = g_param_spec_string (name, name, help,
371 opt->default_val.str, G_PARAM_READWRITE);
372 g_object_class_install_property (gobject_class, prop_id++, pspec);
374 case AV_OPT_TYPE_UINT64:
375 /* ffmpeg expresses all ranges with doubles, this is appalling */
376 pspec = g_param_spec_uint64 (name, name, help,
377 (guint64) (min <= (gdouble) 0 ? 0 : (guint64) min),
379 /* Biggest value before UINT64_MAX that can be represented as double */
380 (gdouble) 18446744073709550000.0 ?
381 /* The Double conversion rounds UINT64_MAX to a bigger */
382 /* value, so the following smaller limit must be used. */
383 G_GUINT64_CONSTANT (18446744073709550000) : (guint64) max),
384 opt->default_val.i64, G_PARAM_READWRITE);
385 g_object_class_install_property (gobject_class, prop_id++, pspec);
387 case AV_OPT_TYPE_BOOL:
388 pspec = g_param_spec_boolean (name, name, help,
389 opt->default_val.i64 ? TRUE : FALSE, G_PARAM_READWRITE);
390 g_object_class_install_property (gobject_class, prop_id++, pspec);
392 /* TODO: didn't find options for the video encoders with
393 * the following type, add support if needed */
394 case AV_OPT_TYPE_CHANNEL_LAYOUT:
395 case AV_OPT_TYPE_COLOR:
396 case AV_OPT_TYPE_VIDEO_RATE:
397 case AV_OPT_TYPE_SAMPLE_FMT:
398 case AV_OPT_TYPE_PIXEL_FMT:
399 case AV_OPT_TYPE_IMAGE_SIZE:
400 case AV_OPT_TYPE_DICT:
401 case AV_OPT_TYPE_BINARY:
402 case AV_OPT_TYPE_RATIONAL:
410 g_param_spec_set_qdata (pspec, avoption_quark, (gpointer) opt);
418 gst_ffmpeg_cfg_install_properties (GObjectClass * klass, AVCodec * in_plugin,
419 guint base, gint flags)
425 g_return_if_fail (base > 0);
427 ctx = avcodec_alloc_context3 (in_plugin);
429 g_warning ("could not get context");
432 install_opts ((GObjectClass *) klass, &in_plugin->priv_class, prop_id, 0,
433 " (Private codec option)", NULL);
435 install_opts ((GObjectClass *) klass, &ctx->av_class, prop_id, flags,
436 " (Generic codec option, might have no effect)", generic_overrides);
439 gst_ffmpeg_avcodec_close (ctx);
445 set_option_value (AVCodecContext * ctx, GParamSpec * pspec,
446 const GValue * value, const AVOption * opt)
450 switch (G_PARAM_SPEC_VALUE_TYPE (pspec)) {
452 res = av_opt_set_int (ctx, opt->name,
453 g_value_get_int (value), AV_OPT_SEARCH_CHILDREN);
456 res = av_opt_set_int (ctx, opt->name,
457 g_value_get_int64 (value), AV_OPT_SEARCH_CHILDREN);
460 res = av_opt_set_int (ctx, opt->name,
461 g_value_get_uint64 (value), AV_OPT_SEARCH_CHILDREN);
464 res = av_opt_set_double (ctx, opt->name,
465 g_value_get_double (value), AV_OPT_SEARCH_CHILDREN);
468 res = av_opt_set_double (ctx, opt->name,
469 g_value_get_float (value), AV_OPT_SEARCH_CHILDREN);
472 res = av_opt_set (ctx, opt->name,
473 g_value_get_string (value), AV_OPT_SEARCH_CHILDREN);
474 /* Some code in FFmpeg returns ENOMEM if the string is NULL:
475 * *dst = av_strdup(val);
476 * return *dst ? 0 : AVERROR(ENOMEM);
477 * That makes little sense, let's ignore that
479 if (!g_value_get_string (value))
483 res = av_opt_set_int (ctx, opt->name,
484 g_value_get_boolean (value), AV_OPT_SEARCH_CHILDREN);
487 if (G_IS_PARAM_SPEC_ENUM (pspec)) {
488 res = av_opt_set_int (ctx, opt->name,
489 g_value_get_enum (value), AV_OPT_SEARCH_CHILDREN);
490 } else if (G_IS_PARAM_SPEC_FLAGS (pspec)) {
491 res = av_opt_set_int (ctx, opt->name,
492 g_value_get_flags (value), AV_OPT_SEARCH_CHILDREN);
493 } else { /* oops, bit lazy we don't cover this case yet */
494 g_critical ("%s does not yet support type %s", GST_FUNCTION,
495 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
503 gst_ffmpeg_cfg_set_property (AVCodecContext * refcontext, const GValue * value,
508 opt = g_param_spec_get_qdata (pspec, avoption_quark);
513 return set_option_value (refcontext, pspec, value, opt) >= 0;
517 gst_ffmpeg_cfg_get_property (AVCodecContext * refcontext, GValue * value,
523 opt = g_param_spec_get_qdata (pspec, avoption_quark);
528 switch (G_PARAM_SPEC_VALUE_TYPE (pspec)) {
532 if ((res = av_opt_get_int (refcontext, opt->name,
533 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
534 g_value_set_int (value, val);
540 if ((res = av_opt_get_int (refcontext, opt->name,
541 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
542 g_value_set_int64 (value, val);
548 if ((res = av_opt_get_int (refcontext, opt->name,
549 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
550 g_value_set_uint64 (value, val);
556 if ((res = av_opt_get_double (refcontext, opt->name,
557 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
558 g_value_set_double (value, val);
564 if ((res = av_opt_get_double (refcontext, opt->name,
565 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
566 g_value_set_float (value, (gfloat) val);
572 if ((res = av_opt_get (refcontext, opt->name,
573 AV_OPT_SEARCH_CHILDREN | AV_OPT_ALLOW_NULL, &val) >= 0)) {
574 g_value_set_string (value, (gchar *) val);
581 if ((res = av_opt_get_int (refcontext, opt->name,
582 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
583 g_value_set_boolean (value, val ? TRUE : FALSE);
587 if (G_IS_PARAM_SPEC_ENUM (pspec)) {
590 if ((res = av_opt_get_int (refcontext, opt->name,
591 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
592 g_value_set_enum (value, val);
593 } else if (G_IS_PARAM_SPEC_FLAGS (pspec)) {
596 if ((res = av_opt_get_int (refcontext, opt->name,
597 AV_OPT_SEARCH_CHILDREN, &val) >= 0))
598 g_value_set_flags (value, val);
599 } else { /* oops, bit lazy we don't cover this case yet */
600 g_critical ("%s does not yet support type %s", GST_FUNCTION,
601 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
609 gst_ffmpeg_cfg_fill_context (GObject * object, AVCodecContext * context)
614 pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object),
617 for (i = 0; i < num_props; ++i) {
618 GParamSpec *pspec = pspecs[i];
620 GValue value = G_VALUE_INIT;
622 opt = g_param_spec_get_qdata (pspec, avoption_quark);
627 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
628 g_object_get_property (object, pspec->name, &value);
629 set_option_value (context, pspec, &value, opt);
630 g_value_unset (&value);
636 gst_ffmpeg_cfg_finalize (void)
638 GST_ERROR ("Finalizing");
639 g_assert (generic_overrides);
640 g_hash_table_unref (generic_overrides);