1 /* GStreamer encoding profiles library
2 * Copyright (C) 2009-2010 Edward Hervey <edward.hervey@collabora.co.uk>
3 * (C) 2009-2010 Nokia Corporation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:encoding-profile
23 * @title: GstEncodingProfile
24 * @short_description: Encoding profile library
26 * - GstEncodingProfile
27 * - GstEncodingContainerProfile
28 * - GstEncodingVideoProfile
29 * - GstEncodingAudioProfile
31 * Functions to create and handle encoding profiles.
33 * Encoding profiles describe the media types and settings one wishes to use for
34 * an encoding process. The top-level profiles are commonly
35 * #GstEncodingContainerProfile(s) (which contains a user-readable name and
36 * description along with which container format to use). These, in turn,
37 * reference one or more #GstEncodingProfile(s) which indicate which encoding
38 * format should be used on each individual streams.
40 * #GstEncodingProfile(s) can be provided to the 'encodebin' element, which will
41 * take care of selecting and setting up the required elements to produce an
42 * output stream conforming to the specifications of the profile.
44 * The encoding profiles do not necessarily specify which #GstElement to use for
45 * the various encoding and muxing steps, as they allow to specifying the format
48 * Encoding profiles can be created at runtime by the application or loaded from
49 * (and saved to) file using the #GstEncodingTarget API.
51 * ## The encoding profile serialization format
53 * Encoding profiles can be serialized to be used in the command line tools or
54 * to set it on other other #GObject-s using #gst_util_set_object_arg for
57 * The serialization format aims at being simple to understand although flexible
58 * enough to describe any possible encoding profile. There are several ways to
59 * describe the profile depending on the context but the general idea is that it
60 * is a colon separated list of EncodingProfiles descriptions, the first one
61 * needs to describe a #GstEncodingContainerProfile and the following ones
62 * describe elementary streams.
64 * ### Using encoders and muxer element factory name
67 * muxer_factory_name:video_encoder_factory_name:audio_encoder_factory_name
70 * For example to encode a stream into a WebM container, with an OGG audio
71 * stream and a VP8 video stream, the serialized #GstEncodingProfile looks like:
74 * webmmux:vp8enc:vorbisenc
77 * ### Define the encoding profile in a generic way using caps:
80 * muxer_source_caps:video_encoder_source_caps:audio_encoder_source_caps
83 * For example to encode a stream into a WebM container, with an OGG audio
84 * stream and a VP8 video stream, the serialized #GstEncodingProfile looks like:
87 * video/webm:video/x-vp8:audio/x-vorbis
90 * It is possible to mix caps and element type names so you can specify a
91 * specific video encoder while using caps for other encoders/muxer.
95 * You can also set the preset name of the encoding profile using the
96 * caps+preset_name syntax as in:
99 * video/webm:video/x-vp8+youtube-preset:audio/x-vorbis
102 * ### Setting properties on muxers or on the encoding profile itself
104 * Moreover, you can set the extra properties:
106 * * `|element-properties,property1=true` (See
107 * #gst_encoding_profile_set_element_properties)
108 * * `|presence=true` (See See #gst_encoding_profile_get_presence)
109 * * `|single-segment=true` (See #gst_encoding_profile_set_single_segment)
110 * * `|single-segment=true` (See
111 * #gst_encoding_video_profile_set_variableframerate)
116 * video/webm:video/x-vp8|presence=1|element-properties,target-bitrate=500000:audio/x-vorbis
119 * ### Enforcing properties to the stream itself (video size, number of audio channels, etc..)
121 * You can also use the `restriction_caps->encoded_format_caps` syntax to
122 * specify the restriction caps to be set on a #GstEncodingProfile
124 * It corresponds to the restriction #GstCaps to apply before the encoder that
125 * will be used in the profile (See #gst_encoding_profile_get_restriction). The
126 * fields present in restriction caps are properties of the raw stream (that is,
127 * before encoding), such as height and width for video and depth and sampling
128 * rate for audio. This property does not make sense for muxers. See
129 * #gst_encoding_profile_get_restriction for more details.
131 * To force a video stream to be encoded with a Full HD resolution (using WebM
132 * as the container format, VP8 as the video codec and Vorbis as the audio
133 * codec), you should use:
136 * "video/webm:video/x-raw,width=1920,height=1080->video/x-vp8:audio/x-vorbis"
139 * > NOTE: Make sure to enclose into quotes to avoid '>' to be reinterpreted by
142 * In the case you are specifying encoders directly, the following is also
146 * matroskamux:x264enc,width=1920,height=1080:audio/x-vorbis
149 * ## Some serialized encoding formats examples
151 * ### MP3 audio and H264 in MP4**
154 * video/quicktime,variant=iso:video/x-h264:audio/mpeg,mpegversion=1,layer=3
157 * ### Vorbis and theora in OGG
160 * application/ogg:video/x-theora:audio/x-vorbis
163 * ### AC3 and H264 in MPEG-TS
166 * video/mpegts:video/x-h264:audio/x-ac3
169 * ## Loading a profile from encoding targets
171 * Anywhere you have to use a string to define a #GstEncodingProfile, you
172 * can use load it from a #GstEncodingTarget using the following syntaxes:
175 * target_name[/profilename/category]
181 * /path/to/target.gep:profilename
186 * ### Creating a profile
189 * #include <gst/pbutils/encoding-profile.h>
191 * GstEncodingProfile *
192 * create_ogg_theora_profile(void)
194 * GstEncodingContainerProfile *prof;
197 * caps = gst_caps_from_string("application/ogg");
198 * prof = gst_encoding_container_profile_new("Ogg audio/video",
199 * "Standard OGG/THEORA/VORBIS",
201 * gst_caps_unref (caps);
203 * caps = gst_caps_from_string("video/x-theora");
204 * gst_encoding_container_profile_add_profile(prof,
205 * (GstEncodingProfile*) gst_encoding_video_profile_new(caps, NULL, NULL, 0));
206 * gst_caps_unref (caps);
208 * caps = gst_caps_from_string("audio/x-vorbis");
209 * gst_encoding_container_profile_add_profile(prof,
210 * (GstEncodingProfile*) gst_encoding_audio_profile_new(caps, NULL, NULL, 0));
211 * gst_caps_unref (caps);
213 * return (GstEncodingProfile*) prof;
218 * ### Example: Using an encoder preset with a profile
221 * #include <gst/pbutils/encoding-profile.h>
223 * GstEncodingProfile *
224 * create_ogg_theora_profile(void)
226 * GstEncodingVideoProfile *v;
227 * GstEncodingAudioProfile *a;
228 * GstEncodingContainerProfile *prof;
232 * caps = gst_caps_from_string ("application/ogg");
233 * prof = gst_encoding_container_profile_new ("Ogg audio/video",
234 * "Standard OGG/THEORA/VORBIS",
236 * gst_caps_unref (caps);
238 * preset = GST_PRESET (gst_element_factory_make ("theoraenc", "theorapreset"));
239 * g_object_set (preset, "bitrate", 1000, NULL);
240 * // The preset will be saved on the filesystem,
241 * // so try to use a descriptive name
242 * gst_preset_save_preset (preset, "theora_bitrate_preset");
243 * gst_object_unref (preset);
245 * caps = gst_caps_from_string ("video/x-theora");
246 * v = gst_encoding_video_profile_new (caps, "theora_bitrate_preset", NULL, 0);
247 * gst_encoding_container_profile_add_profile (prof, (GstEncodingProfile*) v);
248 * gst_caps_unref (caps);
250 * caps = gst_caps_from_string ("audio/x-vorbis");
251 * a = gst_encoding_audio_profile_new (caps, NULL, NULL, 0);
252 * gst_encoding_container_profile_add_profile (prof, (GstEncodingProfile*) a);
253 * gst_caps_unref (caps);
255 * return (GstEncodingProfile*) prof;
260 * ### Listing categories, targets and profiles
263 * #include <gst/pbutils/encoding-profile.h>
265 * GstEncodingProfile *prof;
266 * GList *categories, *tmpc;
267 * GList *targets, *tmpt;
269 * categories = gst_encoding_list_available_categories ();
271 * ... Show available categories to user ...
273 * for (tmpc = categories; tmpc; tmpc = tmpc->next) {
274 * gchar *category = (gchar *) tmpc->data;
276 * ... and we can list all targets within that category ...
278 * targets = gst_encoding_list_all_targets (category);
280 * ... and show a list to our users ...
282 * g_list_foreach (targets, (GFunc) gst_encoding_target_unref, NULL);
283 * g_list_free (targets);
286 * g_list_foreach (categories, (GFunc) g_free, NULL);
287 * g_list_free (categories);
297 #include "encoding-profile.h"
298 #include "encoding-target.h"
302 #ifndef GST_DISABLE_GST_DEBUG
303 #define GST_CAT_DEFAULT gst_pb_utils_encoding_profile_ensure_debug_category()
305 static GstDebugCategory *
306 gst_pb_utils_encoding_profile_ensure_debug_category (void)
308 static gsize cat_gonce = 0;
310 if (g_once_init_enter (&cat_gonce)) {
311 GstDebugCategory *cat = NULL;
313 GST_DEBUG_CATEGORY_INIT (cat, "encoding-profile", 0,
314 "GstPbUtils encoding profile");
316 g_once_init_leave (&cat_gonce, (gsize) cat);
319 return (GstDebugCategory *) cat_gonce;
321 #endif /* GST_DISABLE_GST_DEBUG */
323 /* GstEncodingProfile API */
324 #define PROFILE_LOCK(profile) (g_mutex_lock(&((GstEncodingProfile*)profile)->lock))
325 #define PROFILE_UNLOCK(profile) (g_mutex_unlock(&((GstEncodingProfile*)profile)->lock))
327 struct _GstEncodingProfile
338 gboolean allow_dynamic_output;
340 gboolean single_segment;
343 GstCaps *restriction;
344 GstStructure *element_properties;
348 struct _GstEncodingProfileClass
350 GObjectClass parent_class;
352 void (*copy) (GstEncodingProfile * self, GstEncodingProfile * copy);
358 PROP_RESTRICTION_CAPS,
359 PROP_ELEMENT_PROPERTIES,
363 static GParamSpec *_properties[LAST_PROPERTY];
365 static void string_to_profile_transform (const GValue * src_value,
366 GValue * dest_value);
367 static gboolean gst_encoding_profile_deserialize_valfunc (GValue * value,
369 static gchar *gst_encoding_profile_serialize_valfunc (GValue * value);
371 static void gst_encoding_profile_class_init (GstEncodingProfileClass * klass);
372 static gpointer gst_encoding_profile_parent_class = NULL;
375 gst_encoding_profile_class_intern_init (gpointer klass)
377 gst_encoding_profile_parent_class = g_type_class_peek_parent (klass);
378 gst_encoding_profile_class_init ((GstEncodingProfileClass *) klass);
382 gst_encoding_profile_get_type (void)
384 static gsize g_define_type_id_init = 0;
386 if (g_once_init_enter (&g_define_type_id_init)) {
387 GType g_define_type_id = g_type_register_static_simple (G_TYPE_OBJECT,
388 g_intern_static_string ("GstEncodingProfile"),
389 sizeof (GstEncodingProfileClass),
390 (GClassInitFunc) gst_encoding_profile_class_intern_init,
391 sizeof (GstEncodingProfile),
394 static GstValueTable gstvtable = {
396 (GstValueCompareFunc) NULL,
397 (GstValueSerializeFunc) gst_encoding_profile_serialize_valfunc,
398 (GstValueDeserializeFunc) gst_encoding_profile_deserialize_valfunc
401 gstvtable.type = g_define_type_id;
403 /* Register a STRING=>PROFILE GValueTransformFunc */
404 g_value_register_transform_func (G_TYPE_STRING, g_define_type_id,
405 string_to_profile_transform);
406 /* Register gst-specific GValue functions */
407 gst_value_register (&gstvtable);
409 g_once_init_leave (&g_define_type_id_init, g_define_type_id);
411 return g_define_type_id_init;
416 _encoding_profile_get_property (GObject * object, guint prop_id,
417 GValue * value, GParamSpec * pspec)
419 GstEncodingProfile *prof = (GstEncodingProfile *) object;
422 case PROP_RESTRICTION_CAPS:
423 gst_value_set_caps (value, prof->restriction);
425 case PROP_ELEMENT_PROPERTIES:
427 gst_value_set_structure (value, prof->element_properties);
428 PROFILE_UNLOCK (prof);
431 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
437 _encoding_profile_set_property (GObject * object, guint prop_id,
438 const GValue * value, GParamSpec * pspec)
440 GstEncodingProfile *prof = (GstEncodingProfile *) object;
443 case PROP_RESTRICTION_CAPS:
444 gst_encoding_profile_set_restriction (prof, gst_caps_copy
445 (gst_value_get_caps (value)));
447 case PROP_ELEMENT_PROPERTIES:
449 const GstStructure *structure = gst_value_get_structure (value);
451 gst_encoding_profile_set_element_properties (prof,
452 structure ? gst_structure_copy (structure) : NULL);
456 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
462 gst_encoding_profile_finalize (GObject * object)
464 GstEncodingProfile *prof = (GstEncodingProfile *) object;
467 gst_caps_unref (prof->format);
468 g_free (prof->preset);
469 g_free (prof->description);
470 if (prof->restriction)
471 gst_caps_unref (prof->restriction);
472 g_free (prof->preset_name);
476 gst_encoding_profile_class_init (GstEncodingProfileClass * klass)
478 GObjectClass *gobject_class = (GObjectClass *) klass;
480 gobject_class->finalize = gst_encoding_profile_finalize;
482 gobject_class->set_property = _encoding_profile_set_property;
483 gobject_class->get_property = _encoding_profile_get_property;
485 _properties[PROP_RESTRICTION_CAPS] =
486 g_param_spec_boxed ("restriction-caps", "Restriction caps",
487 "The restriction caps to use", GST_TYPE_CAPS,
488 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
491 * GstEncodingProfile:element-properties:
493 * A #GstStructure defining the properties to be set to the element
494 * the profile represents.
496 * For example for `av1enc`:
499 * element-properties,row-mt=true, end-usage=vbr
504 _properties[PROP_ELEMENT_PROPERTIES] =
505 g_param_spec_boxed ("element-properties", "Element properties",
506 "The element properties to use. "
507 "Example: {properties,boolean-prop=true,string-prop=\"hi\"}.",
509 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
511 g_object_class_install_properties (gobject_class, LAST_PROPERTY, _properties);
515 * gst_encoding_profile_get_name:
516 * @profile: a #GstEncodingProfile
518 * Returns: (nullable): the name of the profile, can be %NULL.
521 gst_encoding_profile_get_name (GstEncodingProfile * profile)
523 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);
525 return profile->name;
529 * gst_encoding_profile_get_description:
530 * @profile: a #GstEncodingProfile
532 * Returns: (nullable): the description of the profile, can be %NULL.
535 gst_encoding_profile_get_description (GstEncodingProfile * profile)
537 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);
539 return profile->description;
543 * gst_encoding_profile_get_format:
544 * @profile: a #GstEncodingProfile
546 * Returns: (transfer full): (nullable): the #GstCaps corresponding to the media format used
547 * in the profile. Unref after usage.
550 gst_encoding_profile_get_format (GstEncodingProfile * profile)
552 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);
554 return (profile->format ? gst_caps_ref (profile->format) : NULL);
558 * gst_encoding_profile_get_preset:
559 * @profile: a #GstEncodingProfile
561 * Returns: (nullable): the name of the #GstPreset to be used in the profile.
562 * This is the name that has been set when saving the preset.
565 gst_encoding_profile_get_preset (GstEncodingProfile * profile)
567 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);
569 return profile->preset;
573 * gst_encoding_profile_get_preset_name:
574 * @profile: a #GstEncodingProfile
576 * Returns: (nullable): the name of the #GstPreset factory to be used in the profile.
579 gst_encoding_profile_get_preset_name (GstEncodingProfile * profile)
581 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);
583 return profile->preset_name;
587 * gst_encoding_profile_get_presence:
588 * @profile: a #GstEncodingProfile
590 * Returns: The number of times the profile is used in its parent
591 * container profile. If 0, it is not a mandatory stream.
594 gst_encoding_profile_get_presence (GstEncodingProfile * profile)
596 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), 0);
598 return profile->presence;
602 * gst_encoding_profile_get_enabled:
603 * @profile: a #GstEncodingProfile
605 * Returns: Whether @profile is enabled or not
610 gst_encoding_profile_is_enabled (GstEncodingProfile * profile)
612 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
614 return profile->enabled;
618 * gst_encoding_profile_get_restriction:
619 * @profile: a #GstEncodingProfile
621 * Returns: (transfer full) (nullable): The restriction #GstCaps to apply before the encoder
622 * that will be used in the profile. The fields present in restriction caps are
623 * properties of the raw stream (that is before encoding), such as height and
624 * width for video and depth and sampling rate for audio. Does not apply to
625 * #GstEncodingContainerProfile (since there is no corresponding raw stream).
626 * Can be %NULL. Unref after usage.
629 gst_encoding_profile_get_restriction (GstEncodingProfile * profile)
631 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);
634 return (profile->restriction ? gst_caps_ref (profile->restriction) : NULL);
638 * gst_encoding_profile_set_name:
639 * @profile: a #GstEncodingProfile
640 * @name: (nullable): the name to set on the profile
642 * Set @name as the given name for the @profile. A copy of @name will be made
646 gst_encoding_profile_set_name (GstEncodingProfile * profile, const gchar * name)
648 g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
650 g_free (profile->name);
651 profile->name = g_strdup (name);
655 * gst_encoding_profile_set_description:
656 * @profile: a #GstEncodingProfile
657 * @description: (nullable): the description to set on the profile
659 * Set @description as the given description for the @profile. A copy of
660 * @description will be made internally.
663 gst_encoding_profile_set_description (GstEncodingProfile * profile,
664 const gchar * description)
666 g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
668 g_free (profile->description);
669 profile->description = g_strdup (description);
673 * gst_encoding_profile_set_format:
674 * @profile: a #GstEncodingProfile
675 * @format: (transfer none): the media format to use in the profile.
677 * Sets the media format used in the profile.
680 gst_encoding_profile_set_format (GstEncodingProfile * profile, GstCaps * format)
682 g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
685 gst_caps_unref (profile->format);
686 profile->format = gst_caps_ref (format);
690 * gst_encoding_profile_get_allow_dynamic_output:
691 * @profile: a #GstEncodingProfile
693 * Get whether the format that has been negotiated in at some point can be renegotiated
694 * later during the encoding.
697 gst_encoding_profile_get_allow_dynamic_output (GstEncodingProfile * profile)
699 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
701 return profile->allow_dynamic_output;
705 * gst_encoding_profile_set_allow_dynamic_output:
706 * @profile: a #GstEncodingProfile
707 * @allow_dynamic_output: Whether the format that has been negotiated first can be renegotiated
708 * during the encoding
710 * Sets whether the format that has been negotiated in at some point can be renegotiated
711 * later during the encoding.
714 gst_encoding_profile_set_allow_dynamic_output (GstEncodingProfile * profile,
715 gboolean allow_dynamic_output)
717 g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
719 profile->allow_dynamic_output = allow_dynamic_output;
723 * gst_encoding_profile_get_single_segment:
724 * @profile: a #GstEncodingProfile
726 * Returns: #TRUE if the stream represented by @profile should use a single
727 * segment before the encoder, #FALSE otherwise. This means that buffers will be retimestamped
728 * and segments will be eat so as to appear as one segment.
733 gst_encoding_profile_get_single_segment (GstEncodingProfile * profile)
735 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
737 return profile->single_segment;
741 * gst_encoding_profile_set_single_segment:
742 * @profile: a #GstEncodingProfile
743 * @single_segment: #TRUE if the stream represented by @profile should use a
744 * single segment before the encoder, #FALSE otherwise.
746 * If using a single segment, buffers will be retimestamped and segments will be
747 * eat so as to appear as one segment.
749 * > *NOTE*: Single segment is not property supported when using
750 * > #encodebin:avoid-reencoding
755 gst_encoding_profile_set_single_segment (GstEncodingProfile * profile,
756 gboolean single_segment)
758 g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
760 profile->single_segment = single_segment;
764 * gst_encoding_profile_set_preset:
765 * @profile: a #GstEncodingProfile
766 * @preset: (nullable): the element preset to use
768 * Sets the name of the #GstElement that implements the #GstPreset interface
769 * to use for the profile.
770 * This is the name that has been set when saving the preset.
773 gst_encoding_profile_set_preset (GstEncodingProfile * profile,
774 const gchar * preset)
776 g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
778 g_free (profile->preset);
779 profile->preset = g_strdup (preset);
783 * gst_encoding_profile_set_preset_name:
784 * @profile: a #GstEncodingProfile
785 * @preset_name: (nullable): The name of the preset to use in this @profile.
787 * Sets the name of the #GstPreset's factory to be used in the profile.
790 gst_encoding_profile_set_preset_name (GstEncodingProfile * profile,
791 const gchar * preset_name)
793 g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
795 g_free (profile->preset_name);
796 profile->preset_name = g_strdup (preset_name);
800 * gst_encoding_profile_set_presence:
801 * @profile: a #GstEncodingProfile
802 * @presence: the number of time the profile can be used
804 * Set the number of time the profile is used in its parent
805 * container profile. If 0, it is not a mandatory stream
808 gst_encoding_profile_set_presence (GstEncodingProfile * profile, guint presence)
810 g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
812 profile->presence = presence;
816 * gst_encoding_profile_set_enabled:
817 * @profile: a #GstEncodingProfile
818 * @enabled: %FALSE to disable @profile, %TRUE to enable it
820 * Set whether the profile should be used or not.
825 gst_encoding_profile_set_enabled (GstEncodingProfile * profile,
828 g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
830 profile->enabled = enabled;
834 * gst_encoding_profile_set_restriction:
835 * @profile: a #GstEncodingProfile
836 * @restriction: (nullable) (transfer full): the restriction to apply
838 * Set the restriction #GstCaps to apply before the encoder
839 * that will be used in the profile. See gst_encoding_profile_get_restriction()
840 * for more about restrictions. Does not apply to #GstEncodingContainerProfile.
843 gst_encoding_profile_set_restriction (GstEncodingProfile * profile,
844 GstCaps * restriction)
846 g_return_if_fail (restriction == NULL || GST_IS_CAPS (restriction));
847 g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));
849 if (profile->restriction)
850 gst_caps_unref (profile->restriction);
851 profile->restriction = restriction;
853 g_object_notify_by_pspec (G_OBJECT (profile),
854 _properties[PROP_RESTRICTION_CAPS]);
858 * gst_encoding_profile_set_element_properties:
859 * @self: a #GstEncodingProfile
860 * @element_properties: (transfer full): A #GstStructure defining the properties
861 * to be set to the element the profile represents.
863 * This allows setting the muxing/encoding element properties.
865 * **Set properties generically**
868 * [element-properties, boolean-prop=true, string-prop="hi"]
871 * **Mapping properties with well known element factories**
874 * element-properties-map, map = {
875 * [openh264enc, gop-size=32, ],
876 * [x264enc, key-int-max=32, tune=zerolatency],
883 gst_encoding_profile_set_element_properties (GstEncodingProfile * self,
884 GstStructure * element_properties)
886 g_return_if_fail (GST_IS_ENCODING_PROFILE (self));
887 g_return_if_fail (!element_properties
888 || GST_IS_STRUCTURE (element_properties));
890 #ifndef G_DISABLE_CHECKS
891 if (element_properties &&
892 (gst_structure_has_name (element_properties, "element-properties-map")
893 || gst_structure_has_name (element_properties, "properties-map")
894 || gst_structure_has_name (element_properties, "map")))
895 g_return_if_fail (gst_structure_has_field_typed (element_properties, "map",
900 if (self->element_properties)
901 gst_structure_free (self->element_properties);
902 if (element_properties)
903 self->element_properties = element_properties;
905 self->element_properties = NULL;
906 PROFILE_UNLOCK (self);
908 g_object_notify_by_pspec (G_OBJECT (self),
909 _properties[PROP_ELEMENT_PROPERTIES]);
913 * gst_encoding_profile_get_element_properties:
914 * @self: a #GstEncodingProfile
916 * Returns: (transfer full) (nullable): The properties that are going to be set on the underlying element
921 gst_encoding_profile_get_element_properties (GstEncodingProfile * self)
923 GstStructure *res = NULL;
925 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (self), NULL);
928 if (self->element_properties)
929 res = gst_structure_copy (self->element_properties);
930 PROFILE_UNLOCK (self);
935 /* Container profiles */
937 struct _GstEncodingContainerProfile
939 GstEncodingProfile parent;
941 GList *encodingprofiles;
944 struct _GstEncodingContainerProfileClass
946 GstEncodingProfileClass parent;
949 G_DEFINE_TYPE (GstEncodingContainerProfile, gst_encoding_container_profile,
950 GST_TYPE_ENCODING_PROFILE);
953 gst_encoding_container_profile_init (GstEncodingContainerProfile * prof)
955 /* Nothing to initialize */
959 gst_encoding_container_profile_finalize (GObject * object)
961 GstEncodingContainerProfile *prof = (GstEncodingContainerProfile *) object;
963 g_list_foreach (prof->encodingprofiles, (GFunc) g_object_unref, NULL);
964 g_list_free (prof->encodingprofiles);
966 G_OBJECT_CLASS (gst_encoding_container_profile_parent_class)->finalize
971 gst_encoding_container_profile_copy (GstEncodingProfile * profile,
972 GstEncodingProfile * copy_profile)
974 GstEncodingContainerProfile *self = GST_ENCODING_CONTAINER_PROFILE (profile),
975 *copy = GST_ENCODING_CONTAINER_PROFILE (copy_profile);
978 for (tmp = self->encodingprofiles; tmp; tmp = tmp->next) {
979 gst_encoding_container_profile_add_profile (copy,
980 gst_encoding_profile_copy (tmp->data));
985 gst_encoding_container_profile_class_init (GstEncodingContainerProfileClass * k)
987 GObjectClass *gobject_class = (GObjectClass *) k;
989 gobject_class->finalize = gst_encoding_container_profile_finalize;
991 ((GstEncodingProfileClass *) k)->copy = gst_encoding_container_profile_copy;
995 * gst_encoding_container_profile_get_profiles:
996 * @profile: a #GstEncodingContainerProfile
998 * Returns: (element-type GstPbutils.EncodingProfile) (transfer none):
999 * the list of contained #GstEncodingProfile.
1002 gst_encoding_container_profile_get_profiles (GstEncodingContainerProfile *
1005 g_return_val_if_fail (GST_IS_ENCODING_CONTAINER_PROFILE (profile), NULL);
1007 return profile->encodingprofiles;
1010 /* Video profiles */
1012 struct _GstEncodingVideoProfile
1014 GstEncodingProfile parent;
1017 gboolean variableframerate;
1020 struct _GstEncodingVideoProfileClass
1022 GstEncodingProfileClass parent;
1025 G_DEFINE_TYPE (GstEncodingVideoProfile, gst_encoding_video_profile,
1026 GST_TYPE_ENCODING_PROFILE);
1029 gst_encoding_video_profile_copy (GstEncodingProfile * profile,
1030 GstEncodingProfile * copy_profile)
1032 GstEncodingVideoProfile *self = GST_ENCODING_VIDEO_PROFILE (profile),
1033 *copy = GST_ENCODING_VIDEO_PROFILE (copy_profile);
1035 copy->pass = self->pass;
1036 copy->variableframerate = self->variableframerate;
1040 gst_encoding_video_profile_init (GstEncodingVideoProfile * prof)
1042 /* Nothing to initialize */
1046 gst_encoding_video_profile_class_init (GstEncodingVideoProfileClass * klass)
1048 ((GstEncodingProfileClass *) klass)->copy = gst_encoding_video_profile_copy;
1052 * gst_encoding_video_profile_get_pass:
1053 * @prof: a #GstEncodingVideoProfile
1055 * Get the pass number if this is part of a multi-pass profile.
1057 * Returns: The pass number. Starts at 1 for multi-pass. 0 if this is
1058 * not a multi-pass profile
1061 gst_encoding_video_profile_get_pass (GstEncodingVideoProfile * prof)
1063 g_return_val_if_fail (GST_IS_ENCODING_VIDEO_PROFILE (prof), 0);
1069 * gst_encoding_video_profile_get_variableframerate:
1070 * @prof: a #GstEncodingVideoProfile
1072 * > *NOTE*: Fixed framerate won't be enforced when #encodebin:avoid-reencoding
1075 * Returns: Whether non-constant video framerate is allowed for encoding.
1078 gst_encoding_video_profile_get_variableframerate (GstEncodingVideoProfile *
1081 g_return_val_if_fail (GST_IS_ENCODING_VIDEO_PROFILE (prof), FALSE);
1083 return prof->variableframerate;
1087 * gst_encoding_video_profile_set_pass:
1088 * @prof: a #GstEncodingVideoProfile
1089 * @pass: the pass number for this profile
1091 * Sets the pass number of this video profile. The first pass profile should have
1092 * this value set to 1. If this video profile isn't part of a multi-pass profile,
1093 * you may set it to 0 (the default value).
1096 gst_encoding_video_profile_set_pass (GstEncodingVideoProfile * prof, guint pass)
1098 g_return_if_fail (GST_IS_ENCODING_VIDEO_PROFILE (prof));
1104 * gst_encoding_video_profile_set_variableframerate:
1105 * @prof: a #GstEncodingVideoProfile
1106 * @variableframerate: a boolean
1108 * If set to %TRUE, then the incoming stream will be allowed to have non-constant
1109 * framerate. If set to %FALSE (default value), then the incoming stream will
1110 * be normalized by dropping/duplicating frames in order to produce a
1111 * constance framerate.
1114 gst_encoding_video_profile_set_variableframerate (GstEncodingVideoProfile *
1115 prof, gboolean variableframerate)
1117 g_return_if_fail (GST_IS_ENCODING_VIDEO_PROFILE (prof));
1119 prof->variableframerate = variableframerate;
1122 /* Audio profiles */
1124 struct _GstEncodingAudioProfile
1126 GstEncodingProfile parent;
1129 struct _GstEncodingAudioProfileClass
1131 GstEncodingProfileClass parent;
1134 G_DEFINE_TYPE (GstEncodingAudioProfile, gst_encoding_audio_profile,
1135 GST_TYPE_ENCODING_PROFILE);
1138 gst_encoding_audio_profile_init (GstEncodingAudioProfile * prof)
1140 /* Nothing to initialize */
1144 gst_encoding_audio_profile_class_init (GstEncodingAudioProfileClass * klass)
1148 static inline gboolean
1149 _gst_caps_is_equal_safe (GstCaps * a, GstCaps * b)
1153 if ((a == NULL) || (b == NULL))
1155 return gst_caps_is_equal (a, b);
1159 _compare_container_encoding_profiles (GstEncodingContainerProfile * ca,
1160 GstEncodingContainerProfile * cb)
1164 if (g_list_length (ca->encodingprofiles) !=
1165 g_list_length (cb->encodingprofiles))
1168 for (tmp = ca->encodingprofiles; tmp; tmp = tmp->next) {
1169 GstEncodingProfile *prof = (GstEncodingProfile *) tmp->data;
1170 if (!gst_encoding_container_profile_contains_profile (ca, prof))
1178 _compare_encoding_profiles (const GstEncodingProfile * a,
1179 const GstEncodingProfile * b)
1181 if ((G_TYPE_FROM_INSTANCE (a) != G_TYPE_FROM_INSTANCE (b)) ||
1182 !_gst_caps_is_equal_safe (a->format, b->format) ||
1183 (g_strcmp0 (a->preset, b->preset) != 0) ||
1184 (g_strcmp0 (a->preset_name, b->preset_name) != 0) ||
1185 (g_strcmp0 (a->name, b->name) != 0) ||
1186 (g_strcmp0 (a->description, b->description) != 0))
1189 if (GST_IS_ENCODING_CONTAINER_PROFILE (a))
1191 _compare_container_encoding_profiles (GST_ENCODING_CONTAINER_PROFILE
1192 (a), GST_ENCODING_CONTAINER_PROFILE (b));
1194 if (GST_IS_ENCODING_VIDEO_PROFILE (a)) {
1195 GstEncodingVideoProfile *va = (GstEncodingVideoProfile *) a;
1196 GstEncodingVideoProfile *vb = (GstEncodingVideoProfile *) b;
1198 if ((va->pass != vb->pass)
1199 || (va->variableframerate != vb->variableframerate))
1207 * gst_encoding_container_profile_contains_profile:
1208 * @container: a #GstEncodingContainerProfile
1209 * @profile: a #GstEncodingProfile
1211 * Checks if @container contains a #GstEncodingProfile identical to
1214 * Returns: %TRUE if @container contains a #GstEncodingProfile identical
1215 * to @profile, else %FALSE.
1218 gst_encoding_container_profile_contains_profile (GstEncodingContainerProfile *
1219 container, GstEncodingProfile * profile)
1221 g_return_val_if_fail (GST_IS_ENCODING_CONTAINER_PROFILE (container), FALSE);
1222 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
1224 return (g_list_find_custom (container->encodingprofiles, profile,
1225 (GCompareFunc) _compare_encoding_profiles) != NULL);
1229 * gst_encoding_container_profile_add_profile:
1230 * @container: the #GstEncodingContainerProfile to use
1231 * @profile: (transfer full): the #GstEncodingProfile to add.
1233 * Add a #GstEncodingProfile to the list of profiles handled by @container.
1235 * No copy of @profile will be made, if you wish to use it elsewhere after this
1236 * method you should increment its reference count.
1238 * Returns: %TRUE if the @stream was properly added, else %FALSE.
1241 gst_encoding_container_profile_add_profile (GstEncodingContainerProfile *
1242 container, GstEncodingProfile * profile)
1244 g_return_val_if_fail (GST_IS_ENCODING_CONTAINER_PROFILE (container), FALSE);
1245 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
1247 container->encodingprofiles =
1248 g_list_append (container->encodingprofiles, profile);
1253 static GstEncodingProfile *
1254 common_creation (GType objtype, GstCaps * format, const gchar * preset,
1255 const gchar * name, const gchar * description, GstCaps * restriction,
1258 GstEncodingProfile *prof;
1260 prof = (GstEncodingProfile *) g_object_new (objtype, NULL);
1263 prof->name = g_strdup (name);
1265 prof->description = g_strdup (description);
1267 prof->preset = g_strdup (preset);
1269 prof->format = gst_caps_ref (format);
1271 prof->restriction = gst_caps_ref (restriction);
1272 prof->presence = presence;
1273 prof->preset_name = NULL;
1274 prof->allow_dynamic_output = TRUE;
1275 prof->enabled = TRUE;
1281 * gst_encoding_container_profile_new:
1282 * @name: (nullable): The name of the container profile, can be %NULL
1283 * @description: (nullable): The description of the container profile,
1285 * @format: (transfer none): The format to use for this profile
1286 * @preset: (nullable): The preset to use for this profile.
1288 * Creates a new #GstEncodingContainerProfile.
1290 * Returns: The newly created #GstEncodingContainerProfile.
1292 GstEncodingContainerProfile *
1293 gst_encoding_container_profile_new (const gchar * name,
1294 const gchar * description, GstCaps * format, const gchar * preset)
1296 g_return_val_if_fail (GST_IS_CAPS (format), NULL);
1298 return (GstEncodingContainerProfile *)
1299 common_creation (GST_TYPE_ENCODING_CONTAINER_PROFILE, format, preset,
1300 name, description, NULL, 0);
1304 * gst_encoding_video_profile_new:
1305 * @format: (transfer none): the #GstCaps
1306 * @preset: (nullable): the preset(s) to use on the encoder, can be %NULL
1307 * @restriction: (nullable): the #GstCaps used to restrict the input to the encoder, can be
1308 * NULL. See gst_encoding_profile_get_restriction() for more details.
1309 * @presence: the number of time this stream must be used. 0 means any number of
1310 * times (including never)
1312 * Creates a new #GstEncodingVideoProfile
1314 * All provided allocatable arguments will be internally copied, so can be
1315 * safely freed/unreferenced after calling this method.
1317 * If you wish to control the pass number (in case of multi-pass scenarios),
1318 * please refer to the gst_encoding_video_profile_set_pass() documentation.
1320 * If you wish to use/force a constant framerate please refer to the
1321 * gst_encoding_video_profile_set_variableframerate() documentation.
1323 * Returns: the newly created #GstEncodingVideoProfile.
1325 GstEncodingVideoProfile *
1326 gst_encoding_video_profile_new (GstCaps * format, const gchar * preset,
1327 GstCaps * restriction, guint presence)
1329 return (GstEncodingVideoProfile *)
1330 common_creation (GST_TYPE_ENCODING_VIDEO_PROFILE, format, preset, NULL,
1331 NULL, restriction, presence);
1335 * gst_encoding_audio_profile_new:
1336 * @format: (transfer none): the #GstCaps
1337 * @preset: (nullable): the preset(s) to use on the encoder, can be %NULL
1338 * @restriction: (nullable): the #GstCaps used to restrict the input to the encoder, can be
1339 * NULL. See gst_encoding_profile_get_restriction() for more details.
1340 * @presence: the number of time this stream must be used. 0 means any number of
1341 * times (including never)
1343 * Creates a new #GstEncodingAudioProfile
1345 * All provided allocatable arguments will be internally copied, so can be
1346 * safely freed/unreferenced after calling this method.
1348 * Returns: the newly created #GstEncodingAudioProfile.
1350 GstEncodingAudioProfile *
1351 gst_encoding_audio_profile_new (GstCaps * format, const gchar * preset,
1352 GstCaps * restriction, guint presence)
1354 return (GstEncodingAudioProfile *)
1355 common_creation (GST_TYPE_ENCODING_AUDIO_PROFILE, format, preset, NULL,
1356 NULL, restriction, presence);
1361 * gst_encoding_profile_is_equal:
1362 * @a: a #GstEncodingProfile
1363 * @b: a #GstEncodingProfile
1365 * Checks whether the two #GstEncodingProfile are equal
1367 * Returns: %TRUE if @a and @b are equal, else %FALSE.
1370 gst_encoding_profile_is_equal (GstEncodingProfile * a, GstEncodingProfile * b)
1372 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (a), FALSE);
1373 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (b), FALSE);
1375 return (_compare_encoding_profiles (a, b) == 0);
1380 * gst_encoding_profile_get_input_caps:
1381 * @profile: a #GstEncodingProfile
1383 * Computes the full output caps that this @profile will be able to consume.
1385 * Returns: (transfer full): The full caps the given @profile can consume. Call
1386 * gst_caps_unref() when you are done with the caps.
1389 gst_encoding_profile_get_input_caps (GstEncodingProfile * profile)
1393 GstStructure *st, *outst;
1398 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);
1400 if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
1401 GstCaps *res = gst_caps_new_empty ();
1403 for (ltmp = GST_ENCODING_CONTAINER_PROFILE (profile)->encodingprofiles;
1404 ltmp; ltmp = ltmp->next) {
1405 GstEncodingProfile *sprof = (GstEncodingProfile *) ltmp->data;
1406 res = gst_caps_merge (res, gst_encoding_profile_get_input_caps (sprof));
1411 fcaps = profile->format;
1414 if ((profile->restriction == NULL) || gst_caps_is_any (profile->restriction))
1415 return gst_caps_ref (fcaps);
1417 /* Combine the format with the restriction caps */
1418 outst = gst_caps_get_structure (fcaps, 0);
1419 out_name = gst_structure_get_name_id (outst);
1420 tmp = gst_caps_new_empty ();
1421 len = gst_caps_get_size (profile->restriction);
1423 for (i = 0; i < len; i++) {
1424 st = gst_structure_copy (gst_caps_get_structure (profile->restriction, i));
1425 st->name = out_name;
1426 gst_caps_append_structure (tmp, st);
1429 out = gst_caps_intersect (tmp, fcaps);
1430 gst_caps_unref (tmp);
1436 * gst_encoding_profile_get_type_nick:
1437 * @profile: a #GstEncodingProfile
1439 * Returns: the human-readable name of the type of @profile.
1442 gst_encoding_profile_get_type_nick (GstEncodingProfile * profile)
1444 if (GST_IS_ENCODING_CONTAINER_PROFILE (profile))
1446 if (GST_IS_ENCODING_VIDEO_PROFILE (profile))
1448 if (GST_IS_ENCODING_AUDIO_PROFILE (profile))
1451 g_assert_not_reached ();
1455 extern const gchar *pb_utils_get_file_extension_from_caps (const GstCaps *
1457 gboolean pb_utils_is_tag (const GstCaps * caps);
1460 gst_encoding_profile_has_format (GstEncodingProfile * profile,
1461 const gchar * media_type)
1466 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
1468 caps = gst_encoding_profile_get_format (profile);
1469 ret = gst_structure_has_name (gst_caps_get_structure (caps, 0), media_type);
1470 gst_caps_unref (caps);
1476 gst_encoding_container_profile_has_video (GstEncodingContainerProfile * profile)
1480 g_return_val_if_fail (GST_IS_ENCODING_CONTAINER_PROFILE (profile), FALSE);
1482 for (l = profile->encodingprofiles; l != NULL; l = l->next) {
1483 if (GST_IS_ENCODING_VIDEO_PROFILE (l->data))
1485 if (GST_IS_ENCODING_CONTAINER_PROFILE (l->data) &&
1486 gst_encoding_container_profile_has_video (l->data))
1494 * gst_encoding_profile_get_file_extension:
1495 * @profile: a #GstEncodingProfile
1497 * Returns: (nullable): a suitable file extension for @profile, or NULL.
1500 gst_encoding_profile_get_file_extension (GstEncodingProfile * profile)
1502 GstEncodingContainerProfile *cprofile;
1503 const gchar *ext = NULL;
1508 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);
1510 caps = gst_encoding_profile_get_format (profile);
1511 ext = pb_utils_get_file_extension_from_caps (caps);
1513 if (!GST_IS_ENCODING_CONTAINER_PROFILE (profile))
1516 cprofile = GST_ENCODING_CONTAINER_PROFILE (profile);
1518 num_children = g_list_length (cprofile->encodingprofiles);
1520 /* if it's a tag container profile (e.g. id3mux/apemux), we need
1521 * to look at what's inside it */
1522 if (pb_utils_is_tag (caps)) {
1523 GST_DEBUG ("tag container profile");
1524 if (num_children == 1) {
1525 GstEncodingProfile *child_profile = cprofile->encodingprofiles->data;
1527 ext = gst_encoding_profile_get_file_extension (child_profile);
1529 GST_WARNING ("expected exactly one child profile with tag profile");
1534 if (num_children == 0)
1538 has_video = gst_encoding_container_profile_has_video (cprofile);
1541 if (g_strcmp0 (ext, "ogg") == 0) {
1542 /* ogg with video => .ogv */
1547 /* ogg with just speex audio => .spx */
1548 if (num_children == 1) {
1549 GstEncodingProfile *child_profile = cprofile->encodingprofiles->data;
1551 if (GST_IS_ENCODING_AUDIO_PROFILE (child_profile) &&
1552 gst_encoding_profile_has_format (child_profile, "audio/x-speex")) {
1557 /* does anyone actually use .oga for ogg audio files? */
1562 if (has_video && g_strcmp0 (ext, "mka") == 0) {
1567 /* Windows Media / ASF */
1568 if (gst_encoding_profile_has_format (profile, "video/x-ms-asf")) {
1570 guint num_wmv = 0, num_wma = 0, num_other = 0;
1572 for (l = cprofile->encodingprofiles; l != NULL; l = l->next) {
1573 if (gst_encoding_profile_has_format (l->data, "video/x-wmv"))
1575 else if (gst_encoding_profile_has_format (l->data, "audio/x-wma"))
1583 else if (num_wmv > 0)
1585 else if (num_wma > 0)
1593 GST_INFO ("caps %" GST_PTR_FORMAT ", ext: %s", caps, GST_STR_NULL (ext));
1594 gst_caps_unref (caps);
1599 * gst_encoding_profile_find:
1600 * @targetname: (transfer none): The name of the target
1601 * @profilename: (transfer none) (nullable): The name of the profile, if %NULL
1602 * provided, it will default to the encoding profile called `default`.
1603 * @category: (transfer none) (nullable): The target category. Can be %NULL
1605 * Find the #GstEncodingProfile with the specified name and category.
1607 * Returns: (transfer full) (nullable): The matching #GstEncodingProfile or %NULL.
1609 GstEncodingProfile *
1610 gst_encoding_profile_find (const gchar * targetname, const gchar * profilename,
1611 const gchar * category)
1613 GstEncodingProfile *res = NULL;
1614 GstEncodingTarget *target;
1616 g_return_val_if_fail (targetname != NULL, NULL);
1618 target = gst_encoding_target_load (targetname, category, NULL);
1621 gst_encoding_target_get_profile (target,
1622 profilename ? profilename : "default");
1623 gst_encoding_target_unref (target);
1629 static GstEncodingProfile *
1630 combo_search (const gchar * pname)
1632 GstEncodingProfile *res = NULL;
1637 split = g_strsplit (pname, "/", 3);
1638 split_length = g_strv_length (split);
1639 if (split_length > 3)
1642 res = gst_encoding_profile_find (split[0],
1643 split_length == 2 ? split[1] : NULL, split_length == 3 ? split[2] : NULL);
1653 get_profile_format_from_possible_factory_name (const gchar * factory_desc,
1654 gchar ** new_factory_name, GstCaps ** restrictions,
1655 gboolean * is_rendering_muxer)
1658 GstCaps *caps = NULL, *tmpcaps = gst_caps_from_string (factory_desc);
1659 GstStructure *tmpstruct;
1660 GstElementFactory *fact = NULL;
1662 if (is_rendering_muxer)
1663 *is_rendering_muxer = FALSE;
1664 *new_factory_name = NULL;
1665 if (gst_caps_get_size (tmpcaps) != 1)
1668 tmpstruct = gst_caps_get_structure (tmpcaps, 0);
1669 fact = gst_element_factory_find (gst_structure_get_name (tmpstruct));
1673 if (!gst_element_factory_list_is_type (fact,
1674 GST_ELEMENT_FACTORY_TYPE_ENCODER | GST_ELEMENT_FACTORY_TYPE_MUXER)) {
1675 GST_ERROR_OBJECT (fact,
1676 "is not an encoder or muxer, it can't be"
1677 " used in an encoding profile.");
1681 for (tmp = (GList *) gst_element_factory_get_static_pad_templates (fact);
1682 tmp; tmp = tmp->next) {
1683 GstStaticPadTemplate *templ = ((GstStaticPadTemplate *) tmp->data);
1685 if (templ->direction == GST_PAD_SRC) {
1686 GstCaps *tmpcaps = gst_static_caps_get (&templ->static_caps);
1688 if (gst_caps_get_size (tmpcaps) > 0)
1690 gst_caps_new_empty_simple (gst_structure_get_name
1691 (gst_caps_get_structure (tmpcaps, 0)));
1693 gst_caps_unref (tmpcaps);
1700 *new_factory_name = g_strdup (gst_structure_get_name (tmpstruct));
1702 if (gst_structure_n_fields (tmpstruct) && restrictions) {
1703 const gchar *sname =
1704 gst_structure_get_name (gst_caps_get_structure (caps, 0));
1706 if (g_str_has_prefix (sname, "audio/"))
1707 gst_structure_set_name (tmpstruct, "audio/x-raw");
1708 else if (g_str_has_prefix (sname, "video/") ||
1709 g_str_has_prefix (sname, "image/"))
1710 gst_structure_set_name (tmpstruct, "video/x-raw");
1712 *restrictions = tmpcaps;
1715 } else if (gst_element_factory_list_is_type (fact,
1716 GST_ELEMENT_FACTORY_TYPE_MUXER)) {
1717 *new_factory_name = g_strdup (gst_structure_get_name (tmpstruct));
1719 caps = gst_caps_ref (gst_caps_new_empty ());
1720 if (is_rendering_muxer)
1721 *is_rendering_muxer = TRUE;
1727 gst_object_unref (fact);
1730 gst_caps_unref (tmpcaps);
1735 static GstEncodingProfile *
1736 create_encoding_profile_from_caps (GstCaps * caps, gchar * preset_name,
1737 GstCaps * restrictioncaps, gint presence, gboolean single_segment,
1738 gchar * factory_name, GList * muxers_and_encoders, GstCaps * raw_audio_caps,
1739 GstCaps * raw_video_caps, gboolean is_rendering_muxer)
1741 GstEncodingProfile *profile = NULL;
1742 GList *factories = NULL;
1743 gboolean is_raw_audio = FALSE, is_raw_video = FALSE;
1745 if (is_rendering_muxer) {
1747 GST_ENCODING_PROFILE (gst_encoding_container_profile_new
1748 ("User profile", "User profile", caps, NULL));
1752 if (gst_caps_can_intersect (raw_audio_caps, caps)) {
1753 is_raw_audio = TRUE;
1754 } else if (gst_caps_can_intersect (raw_video_caps, caps)) {
1755 is_raw_video = TRUE;
1757 factories = gst_element_factory_list_filter (muxers_and_encoders, caps,
1758 GST_PAD_SRC, FALSE);
1761 GST_INFO ("Could not find factory for %" GST_PTR_FORMAT, caps);
1766 if (is_raw_audio || (factories
1767 && gst_element_factory_list_is_type (factories->data,
1768 GST_ELEMENT_FACTORY_TYPE_AUDIO_ENCODER)))
1770 GST_ENCODING_PROFILE (gst_encoding_audio_profile_new (caps, preset_name,
1771 restrictioncaps, presence));
1772 else if (is_raw_video || (factories
1773 && gst_element_factory_list_is_type (factories->data,
1774 GST_ELEMENT_FACTORY_TYPE_VIDEO_ENCODER)))
1776 GST_ENCODING_PROFILE (gst_encoding_video_profile_new (caps, preset_name,
1777 restrictioncaps, presence));
1778 else if (gst_element_factory_list_is_type (factories->data,
1779 GST_ELEMENT_FACTORY_TYPE_MUXER))
1781 GST_ENCODING_PROFILE (gst_encoding_container_profile_new
1782 ("User profile", "User profile", caps, NULL));
1785 gst_plugin_feature_list_free (factories);
1788 if (factory_name && profile)
1789 gst_encoding_profile_set_preset_name (profile, factory_name);
1790 gst_encoding_profile_set_single_segment (profile, single_segment);
1792 g_free (factory_name);
1798 gst_structure_validate_name (const gchar * name)
1802 g_return_val_if_fail (name != NULL, FALSE);
1804 if (G_UNLIKELY (!g_ascii_isalpha (*name)))
1807 /* FIXME: test name string more */
1809 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
1815 if (G_UNLIKELY (*s != '\0'))
1821 static GstEncodingProfile *
1822 create_encoding_stream_profile (gchar * serialized_profile,
1823 GList * muxers_and_encoders, GstCaps * raw_audio_caps,
1824 GstCaps * raw_video_caps)
1828 gboolean single_segment = FALSE;
1829 gchar *strcaps = NULL, *strpresence, **strprops_v =
1830 NULL, **restriction_format, **preset_v = NULL, *preset_name =
1831 NULL, *factory_name = NULL, *variable_framerate = NULL;
1832 GstStructure *element_properties = NULL;
1833 GstCaps *restrictioncaps = NULL;
1834 GstEncodingProfile *profile = NULL;
1836 restriction_format = g_strsplit (serialized_profile, "->", 0);
1837 if (restriction_format[1]) {
1838 restrictioncaps = gst_caps_from_string (restriction_format[0]);
1839 strcaps = g_strdup (restriction_format[1]);
1841 restrictioncaps = NULL;
1842 strcaps = g_strdup (restriction_format[0]);
1844 g_strfreev (restriction_format);
1846 preset_v = g_strsplit (strcaps, "+", 0);
1848 strpresence = preset_v[1];
1850 strcaps = g_strdup (preset_v[0]);
1852 strpresence = preset_v[0];
1855 strprops_v = g_strsplit (strpresence, "|", 0);
1856 if (strprops_v[1]) { /* We have a properties */
1860 if (preset_v[1]) { /* We have preset and properties */
1861 preset_name = g_strdup (strprops_v[0]);
1862 } else { /* We have a properties but no preset */
1864 strcaps = g_strdup (strprops_v[0]);
1867 for (propi = 1; strprops_v[propi]; propi++) {
1869 gchar *presence_str = NULL;
1870 gchar *prop = strprops_v[propi];
1871 GstStructure *tmpstruct = NULL;
1873 if (gst_structure_validate_name (prop))
1874 tmpstruct = gst_structure_new_from_string (prop);
1876 if (element_properties)
1877 gst_structure_free (element_properties);
1879 element_properties = tmpstruct;
1884 propv = g_strsplit (prop, "=", -1);
1885 if (propv[1] && propv[2]) {
1886 g_warning ("Wrong format for property: %s, only 1 `=` is expected",
1893 presence_str = propv[0];
1894 } else if (!g_strcmp0 (propv[0], "presence")) {
1895 presence_str = propv[1];
1896 } else if (!g_strcmp0 (propv[0], "variable-framerate")) {
1897 variable_framerate = g_strdup (propv[1]);
1898 } else if (!g_strcmp0 (propv[0], "single-segment")) {
1899 GValue v = G_VALUE_INIT;
1901 g_value_init (&v, G_TYPE_BOOLEAN);
1902 if (!gst_value_deserialize (&v, propv[1])) {
1903 g_warning ("Invalid value for property 'single-segment': %s",
1909 single_segment = g_value_get_boolean (&v);
1912 g_warning ("Unsupported property: %s", propv[0]);
1918 presence = g_ascii_strtoll (presence_str, &endptr, 10);
1920 if (endptr == strprops_v[1]) {
1921 g_warning ("Wrong presence %s", presence_str);
1928 } else { /* We have no presence */
1929 if (preset_v[1]) { /* Not presence but preset */
1930 preset_name = g_strdup (preset_v[1]);
1932 strcaps = g_strdup (preset_v[0]);
1933 } /* Else we have no presence nor preset */
1936 GST_DEBUG ("Creating preset with restrictions: %" GST_PTR_FORMAT
1937 ", caps: %s, preset %s, presence %d", restrictioncaps, strcaps,
1938 preset_name ? preset_name : "none", presence);
1940 caps = gst_caps_from_string (strcaps);
1942 profile = create_encoding_profile_from_caps (caps, preset_name,
1943 restrictioncaps, presence, single_segment, NULL, muxers_and_encoders,
1944 raw_audio_caps, raw_video_caps, FALSE);
1945 gst_caps_unref (caps);
1949 gboolean is_rendering_muxer;
1951 caps = get_profile_format_from_possible_factory_name (strcaps,
1952 &factory_name, restrictioncaps ? NULL : &restrictioncaps,
1953 &is_rendering_muxer);
1955 profile = create_encoding_profile_from_caps (caps, preset_name,
1956 restrictioncaps, presence, single_segment, factory_name,
1957 muxers_and_encoders, raw_audio_caps, raw_video_caps,
1958 is_rendering_muxer);
1959 gst_caps_unref (caps);
1963 if (variable_framerate) {
1964 if (GST_IS_ENCODING_VIDEO_PROFILE (profile)) {
1968 g_value_init (&v, G_TYPE_BOOLEAN);
1969 if (gst_value_deserialize (&v, variable_framerate)) {
1970 gst_encoding_video_profile_set_variableframerate
1971 (GST_ENCODING_VIDEO_PROFILE (profile), g_value_get_boolean (&v));
1973 GST_WARNING ("Invalid value for variable_framerate: %s",
1974 variable_framerate);
1980 ("Variable framerate specified on a non video encoding profile");
1984 if (profile == NULL) {
1985 GST_ERROR ("No way to create a profile for description: %s",
1986 serialized_profile);
1987 } else if (element_properties) {
1988 gst_encoding_profile_set_element_properties (profile,
1989 g_steal_pointer (&element_properties));
1994 g_free (variable_framerate);
1995 g_strfreev (strprops_v);
1996 g_strfreev (preset_v);
1997 g_free (preset_name);
1998 if (element_properties)
1999 gst_structure_free (element_properties);
2000 if (restrictioncaps)
2001 gst_caps_unref (restrictioncaps);
2006 static GstEncodingProfile *
2007 parse_encoding_profile (const gchar * value)
2009 GstEncodingProfile *res = NULL;
2010 gchar *caps_str = NULL;
2012 g_regex_split_simple ("(?<!\\\\)(?:\\\\\\\\)*:", value, 0, 0);
2014 GList *muxers_and_encoders =
2015 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_ENCODER |
2016 GST_ELEMENT_FACTORY_TYPE_MUXER,
2018 GstCaps *raw_video_caps = gst_caps_new_empty_simple ("video/x-raw");
2019 GstCaps *raw_audio_caps = gst_caps_new_empty_simple ("audio/x-raw");
2021 /* The regex returns NULL if no ":" found, handle that case. */
2022 if (strcaps_v == NULL)
2023 strcaps_v = g_strsplit (value, ":", 0);
2025 for (i = 0; strcaps_v[i] && *strcaps_v[i]; i++) {
2026 GstEncodingProfile *profile;
2027 caps_str = g_strcompress (strcaps_v[i]);
2029 create_encoding_stream_profile (caps_str, muxers_and_encoders,
2030 raw_audio_caps, raw_video_caps);
2033 GST_ERROR ("Could not create profile for caps: %s", caps_str);
2038 if (!GST_IS_ENCODING_CONTAINER_PROFILE (res)) {
2039 GST_ERROR ("The first described encoding profile was not a container"
2040 " but you are trying to add more profiles to it. This is not possible");
2044 if (!gst_encoding_container_profile_add_profile
2045 (GST_ENCODING_CONTAINER_PROFILE (res), profile)) {
2046 GST_ERROR ("Can not add profile for caps: %s", caps_str);
2053 g_clear_pointer (&caps_str, g_free);
2058 g_strfreev (strcaps_v);
2059 gst_caps_unref (raw_audio_caps);
2060 gst_caps_unref (raw_video_caps);
2061 gst_plugin_feature_list_free (muxers_and_encoders);
2066 g_clear_object (&res);
2071 static GstEncodingProfile *
2072 profile_from_string (const gchar * string)
2074 GstEncodingProfile *profile;
2075 gchar *filename_end;
2077 profile = combo_search (string);
2082 filename_end = g_strrstr (string, ".gep");
2084 GstEncodingTarget *target;
2085 gchar *profilename = NULL, *filename;
2087 if (filename_end[4] == ':')
2088 profilename = g_strdup (&filename_end[5]);
2090 if (filename_end[4] == '\0' || profilename) {
2091 filename = g_strndup (string, filename_end - string + strlen (".gep"));
2093 target = gst_encoding_target_load_from_file (filename, NULL);
2095 profile = gst_encoding_target_get_profile (target,
2096 profilename ? profilename : "default");
2097 gst_encoding_target_unref (target);
2100 g_free (profilename);
2106 profile = parse_encoding_profile (string);
2111 /* GValue transform function */
2113 string_to_profile_transform (const GValue * src_value, GValue * dest_value)
2115 const gchar *profilename;
2116 GstEncodingProfile *profile;
2118 profilename = g_value_get_string (src_value);
2120 profile = profile_from_string (profilename);
2123 g_value_take_object (dest_value, (GObject *) profile);
2127 serialize_profile (GString * res, GstEncodingProfile * profile)
2132 g_string_append_c (res, ':');
2134 if (profile->restriction) {
2135 tmp = gst_caps_to_string (profile->restriction);
2136 g_string_append_printf (res, "%s->", tmp);
2140 tmp = gst_caps_to_string (profile->format);
2141 g_string_append (res, tmp);
2143 if (profile->presence)
2144 g_string_append_printf (res, "|presence=%d", profile->presence);
2146 if (profile->single_segment)
2147 g_string_append_printf (res, "%ssingle-segment=true",
2148 profile->presence ? "" : "|");
2150 if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
2153 for (tmp = GST_ENCODING_CONTAINER_PROFILE (profile)->encodingprofiles; tmp;
2155 serialize_profile (res, tmp->data);
2160 gst_encoding_profile_serialize_valfunc (GValue * value)
2162 GString *res = g_string_new (NULL);
2163 GstEncodingProfile *profile = g_value_get_object (value);
2165 serialize_profile (res, profile);
2167 return g_string_free (res, FALSE);
2171 gst_encoding_profile_deserialize_valfunc (GValue * value, const gchar * s)
2173 GstEncodingProfile *profile;
2175 profile = profile_from_string (s);
2178 g_value_take_object (value, (GObject *) profile);
2185 static GstEncodingProfile *
2186 create_stream_profile_recurse (GstEncodingProfile * toplevel,
2187 GstDiscovererStreamInfo * sinfo)
2189 GstEncodingProfile *profile = NULL;
2193 caps = gst_discoverer_stream_info_get_caps (sinfo);
2195 /* Should unify this with copy_and_clean_caps() */
2196 caps = gst_caps_make_writable (caps);
2197 s = gst_caps_get_structure (caps, 0);
2199 gst_structure_remove_fields (s, "codec_data", "streamheader", "parsed",
2200 "colorimetry", "framed", "stream-format", "alignment", "tier", "level",
2201 "profile", "chroma-format", "bit-depth-luma", "bit-depth-chroma", NULL);
2203 GST_LOG ("Stream: %" GST_PTR_FORMAT, caps);
2204 if (GST_IS_DISCOVERER_AUDIO_INFO (sinfo)) {
2206 (GstEncodingProfile *) gst_encoding_audio_profile_new (caps, NULL,
2208 } else if (GST_IS_DISCOVERER_VIDEO_INFO (sinfo)) {
2210 (GstEncodingProfile *) gst_encoding_video_profile_new (caps, NULL,
2212 } else if (GST_IS_DISCOVERER_CONTAINER_INFO (sinfo)) {
2213 GList *streams, *stream;
2216 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
2219 if (!toplevel || !GST_IS_ENCODING_CONTAINER_PROFILE (toplevel)) {
2220 GstEncodingProfile *prev_toplevel = toplevel;
2222 toplevel = (GstEncodingProfile *)
2223 gst_encoding_container_profile_new ("auto-generated",
2224 "Automatically generated from GstDiscovererInfo", caps, NULL);
2226 gst_encoding_container_profile_add_profile
2227 (GST_ENCODING_CONTAINER_PROFILE (toplevel), prev_toplevel);
2230 for (stream = streams; stream; stream = stream->next)
2231 create_stream_profile_recurse (toplevel,
2232 (GstDiscovererStreamInfo *) stream->data);
2233 gst_discoverer_stream_info_list_free (streams);
2235 GST_FIXME ("Ignoring stream of type '%s'",
2236 g_type_name (G_OBJECT_TYPE (sinfo)));
2237 /* subtitles or other ? ignore for now */
2239 gst_caps_unref (caps);
2242 const gchar *stream_id = gst_discoverer_stream_info_get_stream_id (sinfo);
2245 const gchar *subid = strchr (stream_id, '/');
2247 gst_encoding_profile_set_name (profile, subid ? subid : stream_id);
2250 if (GST_IS_ENCODING_CONTAINER_PROFILE (toplevel))
2251 gst_encoding_container_profile_add_profile ((GstEncodingContainerProfile
2256 if (!toplevel && profile)
2259 sinfo = gst_discoverer_stream_info_get_next (sinfo);
2261 return create_stream_profile_recurse (toplevel, sinfo);
2267 * gst_encoding_profile_from_discoverer:
2268 * @info: (transfer none): The #GstDiscovererInfo to read from
2270 * Creates a #GstEncodingProfile matching the formats from the given
2271 * #GstDiscovererInfo. Streams other than audio or video (eg,
2272 * subtitles), are currently ignored.
2274 * Returns: (transfer full) (nullable): The new #GstEncodingProfile or %NULL.
2276 GstEncodingProfile *
2277 gst_encoding_profile_from_discoverer (GstDiscovererInfo * info)
2279 GstEncodingProfile *profile;
2280 GstDiscovererStreamInfo *sinfo;
2282 if (!info || gst_discoverer_info_get_result (info) != GST_DISCOVERER_OK)
2285 sinfo = gst_discoverer_info_get_stream_info (info);
2289 profile = create_stream_profile_recurse (NULL, sinfo);
2290 if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
2291 if (!gst_encoding_container_profile_get_profiles
2292 (GST_ENCODING_CONTAINER_PROFILE (profile))) {
2293 GST_ERROR ("Failed to add any streams");
2294 g_object_unref (profile);
2299 return (GstEncodingProfile *) profile;
2303 * gst_encoding_profile_copy:
2304 * @self: The #GstEncodingProfile to copy
2306 * Makes a deep copy of @self
2308 * Returns: (transfer full): The copy of @self
2312 GstEncodingProfile *
2313 gst_encoding_profile_copy (GstEncodingProfile * self)
2315 GstEncodingProfileClass *klass =
2316 (GstEncodingProfileClass *) G_OBJECT_GET_CLASS (self);
2317 GstEncodingProfile *copy =
2318 common_creation (G_OBJECT_TYPE (self), self->format, self->preset,
2319 self->name, self->description, self->restriction, self->presence);
2321 copy->enabled = self->enabled;
2322 copy->allow_dynamic_output = self->allow_dynamic_output;
2323 gst_encoding_profile_set_preset_name (copy, self->preset_name);
2324 gst_encoding_profile_set_description (copy, self->description);
2327 klass->copy (self, copy);