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: 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: 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): 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: 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: 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): 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: (allow-none): 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: (allow-none): 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: (allow-none): 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: (allow-none) (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: (allow-none): The name of the container profile, can be %NULL
1283 * @description: (allow-none): The description of the container profile,
1285 * @format: (transfer none): The format to use for this profile
1286 * @preset: (allow-none): 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: (allow-none): the preset(s) to use on the encoder, can be %NULL
1307 * @restriction: (allow-none): 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: (allow-none): the preset(s) to use on the encoder, can be %NULL
1338 * @restriction: (allow-none): 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))
1453 extern const gchar *pb_utils_get_file_extension_from_caps (const GstCaps *
1455 gboolean pb_utils_is_tag (const GstCaps * caps);
1458 gst_encoding_profile_has_format (GstEncodingProfile * profile,
1459 const gchar * media_type)
1464 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
1466 caps = gst_encoding_profile_get_format (profile);
1467 ret = gst_structure_has_name (gst_caps_get_structure (caps, 0), media_type);
1468 gst_caps_unref (caps);
1474 gst_encoding_container_profile_has_video (GstEncodingContainerProfile * profile)
1478 g_return_val_if_fail (GST_IS_ENCODING_CONTAINER_PROFILE (profile), FALSE);
1480 for (l = profile->encodingprofiles; l != NULL; l = l->next) {
1481 if (GST_IS_ENCODING_VIDEO_PROFILE (l->data))
1483 if (GST_IS_ENCODING_CONTAINER_PROFILE (l->data) &&
1484 gst_encoding_container_profile_has_video (l->data))
1492 * gst_encoding_profile_get_file_extension:
1493 * @profile: a #GstEncodingProfile
1495 * Returns: a suitable file extension for @profile, or NULL.
1498 gst_encoding_profile_get_file_extension (GstEncodingProfile * profile)
1500 GstEncodingContainerProfile *cprofile;
1501 const gchar *ext = NULL;
1506 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);
1508 caps = gst_encoding_profile_get_format (profile);
1509 ext = pb_utils_get_file_extension_from_caps (caps);
1511 if (!GST_IS_ENCODING_CONTAINER_PROFILE (profile))
1514 cprofile = GST_ENCODING_CONTAINER_PROFILE (profile);
1516 num_children = g_list_length (cprofile->encodingprofiles);
1518 /* if it's a tag container profile (e.g. id3mux/apemux), we need
1519 * to look at what's inside it */
1520 if (pb_utils_is_tag (caps)) {
1521 GST_DEBUG ("tag container profile");
1522 if (num_children == 1) {
1523 GstEncodingProfile *child_profile = cprofile->encodingprofiles->data;
1525 ext = gst_encoding_profile_get_file_extension (child_profile);
1527 GST_WARNING ("expected exactly one child profile with tag profile");
1532 if (num_children == 0)
1536 has_video = gst_encoding_container_profile_has_video (cprofile);
1539 if (g_strcmp0 (ext, "ogg") == 0) {
1540 /* ogg with video => .ogv */
1545 /* ogg with just speex audio => .spx */
1546 if (num_children == 1) {
1547 GstEncodingProfile *child_profile = cprofile->encodingprofiles->data;
1549 if (GST_IS_ENCODING_AUDIO_PROFILE (child_profile) &&
1550 gst_encoding_profile_has_format (child_profile, "audio/x-speex")) {
1555 /* does anyone actually use .oga for ogg audio files? */
1560 if (has_video && g_strcmp0 (ext, "mka") == 0) {
1565 /* Windows Media / ASF */
1566 if (gst_encoding_profile_has_format (profile, "video/x-ms-asf")) {
1568 guint num_wmv = 0, num_wma = 0, num_other = 0;
1570 for (l = cprofile->encodingprofiles; l != NULL; l = l->next) {
1571 if (gst_encoding_profile_has_format (l->data, "video/x-wmv"))
1573 else if (gst_encoding_profile_has_format (l->data, "audio/x-wma"))
1581 else if (num_wmv > 0)
1583 else if (num_wma > 0)
1591 GST_INFO ("caps %" GST_PTR_FORMAT ", ext: %s", caps, GST_STR_NULL (ext));
1592 gst_caps_unref (caps);
1597 * gst_encoding_profile_find:
1598 * @targetname: (transfer none): The name of the target
1599 * @profilename: (transfer none) (allow-none): The name of the profile, if %NULL
1600 * provided, it will default to the encoding profile called `default`.
1601 * @category: (transfer none) (allow-none): The target category. Can be %NULL
1603 * Find the #GstEncodingProfile with the specified name and category.
1605 * Returns: (transfer full): The matching #GstEncodingProfile or %NULL.
1607 GstEncodingProfile *
1608 gst_encoding_profile_find (const gchar * targetname, const gchar * profilename,
1609 const gchar * category)
1611 GstEncodingProfile *res = NULL;
1612 GstEncodingTarget *target;
1614 g_return_val_if_fail (targetname != NULL, NULL);
1616 target = gst_encoding_target_load (targetname, category, NULL);
1619 gst_encoding_target_get_profile (target,
1620 profilename ? profilename : "default");
1621 gst_encoding_target_unref (target);
1627 static GstEncodingProfile *
1628 combo_search (const gchar * pname)
1630 GstEncodingProfile *res = NULL;
1635 split = g_strsplit (pname, "/", 3);
1636 split_length = g_strv_length (split);
1637 if (split_length > 3)
1640 res = gst_encoding_profile_find (split[0],
1641 split_length == 2 ? split[1] : NULL, split_length == 3 ? split[2] : NULL);
1651 get_profile_format_from_possible_factory_name (const gchar * factory_desc,
1652 gchar ** new_factory_name, GstCaps ** restrictions,
1653 gboolean * is_rendering_muxer)
1656 GstCaps *caps = NULL, *tmpcaps = gst_caps_from_string (factory_desc);
1657 GstStructure *tmpstruct;
1658 GstElementFactory *fact = NULL;
1660 if (is_rendering_muxer)
1661 *is_rendering_muxer = FALSE;
1662 *new_factory_name = NULL;
1663 if (gst_caps_get_size (tmpcaps) != 1)
1666 tmpstruct = gst_caps_get_structure (tmpcaps, 0);
1667 fact = gst_element_factory_find (gst_structure_get_name (tmpstruct));
1671 if (!gst_element_factory_list_is_type (fact,
1672 GST_ELEMENT_FACTORY_TYPE_ENCODER | GST_ELEMENT_FACTORY_TYPE_MUXER)) {
1673 GST_ERROR_OBJECT (fact,
1674 "is not an encoder or muxer, it can't be"
1675 " used in an encoding profile.");
1679 for (tmp = (GList *) gst_element_factory_get_static_pad_templates (fact);
1680 tmp; tmp = tmp->next) {
1681 GstStaticPadTemplate *templ = ((GstStaticPadTemplate *) tmp->data);
1683 if (templ->direction == GST_PAD_SRC) {
1684 GstCaps *tmpcaps = gst_static_caps_get (&templ->static_caps);
1686 if (gst_caps_get_size (tmpcaps) > 0)
1688 gst_caps_new_empty_simple (gst_structure_get_name
1689 (gst_caps_get_structure (tmpcaps, 0)));
1691 gst_caps_unref (tmpcaps);
1698 *new_factory_name = g_strdup (gst_structure_get_name (tmpstruct));
1700 if (gst_structure_n_fields (tmpstruct) && restrictions) {
1701 const gchar *sname =
1702 gst_structure_get_name (gst_caps_get_structure (caps, 0));
1704 if (g_str_has_prefix (sname, "audio/"))
1705 gst_structure_set_name (tmpstruct, "audio/x-raw");
1706 else if (g_str_has_prefix (sname, "video/") ||
1707 g_str_has_prefix (sname, "image/"))
1708 gst_structure_set_name (tmpstruct, "video/x-raw");
1710 *restrictions = tmpcaps;
1713 } else if (gst_element_factory_list_is_type (fact,
1714 GST_ELEMENT_FACTORY_TYPE_MUXER)) {
1715 *new_factory_name = g_strdup (gst_structure_get_name (tmpstruct));
1717 caps = gst_caps_ref (gst_caps_new_empty ());
1718 if (is_rendering_muxer)
1719 *is_rendering_muxer = TRUE;
1725 gst_object_unref (fact);
1728 gst_caps_unref (tmpcaps);
1733 static GstEncodingProfile *
1734 create_encoding_profile_from_caps (GstCaps * caps, gchar * preset_name,
1735 GstCaps * restrictioncaps, gint presence, gboolean single_segment,
1736 gchar * factory_name, GList * muxers_and_encoders, GstCaps * raw_audio_caps,
1737 GstCaps * raw_video_caps, gboolean is_rendering_muxer)
1739 GstEncodingProfile *profile = NULL;
1740 GList *factories = NULL;
1741 gboolean is_raw_audio = FALSE, is_raw_video = FALSE;
1743 if (is_rendering_muxer) {
1745 GST_ENCODING_PROFILE (gst_encoding_container_profile_new
1746 ("User profile", "User profile", caps, NULL));
1750 if (gst_caps_can_intersect (raw_audio_caps, caps)) {
1751 is_raw_audio = TRUE;
1752 } else if (gst_caps_can_intersect (raw_video_caps, caps)) {
1753 is_raw_video = TRUE;
1755 factories = gst_element_factory_list_filter (muxers_and_encoders, caps,
1756 GST_PAD_SRC, FALSE);
1759 GST_INFO ("Could not find factory for %" GST_PTR_FORMAT, caps);
1764 if (is_raw_audio || (factories
1765 && gst_element_factory_list_is_type (factories->data,
1766 GST_ELEMENT_FACTORY_TYPE_AUDIO_ENCODER)))
1768 GST_ENCODING_PROFILE (gst_encoding_audio_profile_new (caps, preset_name,
1769 restrictioncaps, presence));
1770 else if (is_raw_video || (factories
1771 && gst_element_factory_list_is_type (factories->data,
1772 GST_ELEMENT_FACTORY_TYPE_VIDEO_ENCODER)))
1774 GST_ENCODING_PROFILE (gst_encoding_video_profile_new (caps, preset_name,
1775 restrictioncaps, presence));
1776 else if (gst_element_factory_list_is_type (factories->data,
1777 GST_ELEMENT_FACTORY_TYPE_MUXER))
1779 GST_ENCODING_PROFILE (gst_encoding_container_profile_new
1780 ("User profile", "User profile", caps, NULL));
1783 gst_plugin_feature_list_free (factories);
1786 if (factory_name && profile)
1787 gst_encoding_profile_set_preset_name (profile, factory_name);
1788 gst_encoding_profile_set_single_segment (profile, single_segment);
1790 g_free (factory_name);
1796 gst_structure_validate_name (const gchar * name)
1800 g_return_val_if_fail (name != NULL, FALSE);
1802 if (G_UNLIKELY (!g_ascii_isalpha (*name)))
1805 /* FIXME: test name string more */
1807 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
1813 if (G_UNLIKELY (*s != '\0'))
1819 static GstEncodingProfile *
1820 create_encoding_stream_profile (gchar * serialized_profile,
1821 GList * muxers_and_encoders, GstCaps * raw_audio_caps,
1822 GstCaps * raw_video_caps)
1826 gboolean single_segment = FALSE;
1827 gchar *strcaps, *strpresence, **strprops_v, **restriction_format,
1828 **preset_v, *preset_name = NULL, *factory_name = NULL,
1829 *variable_framerate = NULL;
1830 GstStructure *element_properties = NULL;
1831 GstCaps *restrictioncaps = NULL;
1832 GstEncodingProfile *profile = NULL;
1834 restriction_format = g_strsplit (serialized_profile, "->", 0);
1835 if (restriction_format[1]) {
1836 restrictioncaps = gst_caps_from_string (restriction_format[0]);
1837 strcaps = g_strdup (restriction_format[1]);
1839 restrictioncaps = NULL;
1840 strcaps = g_strdup (restriction_format[0]);
1842 g_strfreev (restriction_format);
1844 preset_v = g_strsplit (strcaps, "+", 0);
1846 strpresence = preset_v[1];
1848 strcaps = g_strdup (preset_v[0]);
1850 strpresence = preset_v[0];
1853 strprops_v = g_strsplit (strpresence, "|", 0);
1854 if (strprops_v[1]) { /* We have a properties */
1858 if (preset_v[1]) { /* We have preset and properties */
1859 preset_name = g_strdup (strprops_v[0]);
1860 } else { /* We have a properties but no preset */
1862 strcaps = g_strdup (strprops_v[0]);
1865 for (propi = 1; strprops_v[propi]; propi++) {
1867 gchar *presence_str = NULL;
1868 gchar *prop = strprops_v[propi];
1869 GstStructure *tmpstruct = NULL;
1871 if (gst_structure_validate_name (prop))
1872 tmpstruct = gst_structure_new_from_string (prop);
1874 if (element_properties)
1875 gst_structure_free (element_properties);
1877 element_properties = tmpstruct;
1882 propv = g_strsplit (prop, "=", -1);
1883 if (propv[1] && propv[2]) {
1884 g_warning ("Wrong format for property: %s, only 1 `=` is expected",
1891 presence_str = propv[0];
1892 } else if (!g_strcmp0 (propv[0], "presence")) {
1893 presence_str = propv[1];
1894 } else if (!g_strcmp0 (propv[0], "variable-framerate")) {
1895 variable_framerate = g_strdup (propv[1]);
1896 } else if (!g_strcmp0 (propv[0], "single-segment")) {
1897 GValue v = G_VALUE_INIT;
1899 g_value_init (&v, G_TYPE_BOOLEAN);
1900 if (!gst_value_deserialize (&v, propv[1])) {
1901 g_warning ("Invalid value for property 'single-segment': %s",
1907 single_segment = g_value_get_boolean (&v);
1910 g_warning ("Unsupported property: %s", propv[0]);
1915 presence = g_ascii_strtoll (presence_str, &endptr, 10);
1917 if (endptr == strprops_v[1]) {
1918 g_warning ("Wrong presence %s", presence_str);
1923 } else { /* We have no presence */
1924 if (preset_v[1]) { /* Not presence but preset */
1925 preset_name = g_strdup (preset_v[1]);
1927 strcaps = g_strdup (preset_v[0]);
1928 } /* Else we have no presence nor preset */
1930 g_strfreev (strprops_v);
1931 g_strfreev (preset_v);
1933 GST_DEBUG ("Creating preset with restrictions: %" GST_PTR_FORMAT
1934 ", caps: %s, preset %s, presence %d", restrictioncaps, strcaps,
1935 preset_name ? preset_name : "none", presence);
1937 caps = gst_caps_from_string (strcaps);
1939 profile = create_encoding_profile_from_caps (caps, preset_name,
1940 restrictioncaps, presence, single_segment, NULL, muxers_and_encoders,
1941 raw_audio_caps, raw_video_caps, FALSE);
1942 gst_caps_unref (caps);
1946 gboolean is_rendering_muxer;
1948 caps = get_profile_format_from_possible_factory_name (strcaps,
1949 &factory_name, restrictioncaps ? NULL : &restrictioncaps,
1950 &is_rendering_muxer);
1952 profile = create_encoding_profile_from_caps (caps, preset_name,
1953 restrictioncaps, presence, single_segment, factory_name,
1954 muxers_and_encoders, raw_audio_caps, raw_video_caps,
1955 is_rendering_muxer);
1956 gst_caps_unref (caps);
1959 g_free (preset_name);
1962 if (restrictioncaps)
1963 gst_caps_unref (restrictioncaps);
1965 if (variable_framerate) {
1966 if (GST_IS_ENCODING_VIDEO_PROFILE (profile)) {
1970 g_value_init (&v, G_TYPE_BOOLEAN);
1971 if (gst_value_deserialize (&v, variable_framerate)) {
1972 gst_encoding_video_profile_set_variableframerate
1973 (GST_ENCODING_VIDEO_PROFILE (profile), g_value_get_boolean (&v));
1975 GST_WARNING ("Invalid value for variable_framerate: %s",
1976 variable_framerate);
1982 ("Variable framerate specified on a non video encoding profile");
1985 g_free (variable_framerate);
1988 if (profile == NULL) {
1989 GST_ERROR ("No way to create a profile for description: %s",
1990 serialized_profile);
1995 if (element_properties)
1996 gst_encoding_profile_set_element_properties (profile, element_properties);
2001 static GstEncodingProfile *
2002 parse_encoding_profile (const gchar * value)
2004 GstEncodingProfile *res = NULL;
2005 gchar *caps_str = NULL;
2007 g_regex_split_simple ("(?<!\\\\)(?:\\\\\\\\)*:", value, 0, 0);
2009 GList *muxers_and_encoders =
2010 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_ENCODER |
2011 GST_ELEMENT_FACTORY_TYPE_MUXER,
2013 GstCaps *raw_video_caps = gst_caps_new_empty_simple ("video/x-raw");
2014 GstCaps *raw_audio_caps = gst_caps_new_empty_simple ("audio/x-raw");
2016 /* The regex returns NULL if no ":" found, handle that case. */
2017 if (strcaps_v == NULL)
2018 strcaps_v = g_strsplit (value, ":", 0);
2020 for (i = 0; strcaps_v[i] && *strcaps_v[i]; i++) {
2021 GstEncodingProfile *profile;
2022 caps_str = g_strcompress (strcaps_v[i]);
2024 create_encoding_stream_profile (caps_str, muxers_and_encoders,
2025 raw_audio_caps, raw_video_caps);
2028 GST_ERROR ("Could not create profile for caps: %s", caps_str);
2033 if (!GST_IS_ENCODING_CONTAINER_PROFILE (res)) {
2034 GST_ERROR ("The first described encoding profile was not a container"
2035 " but you are trying to add more profiles to it. This is not possible");
2039 if (!gst_encoding_container_profile_add_profile
2040 (GST_ENCODING_CONTAINER_PROFILE (res), profile)) {
2041 GST_ERROR ("Can not add profile for caps: %s", caps_str);
2048 g_clear_pointer (&caps_str, g_free);
2053 g_strfreev (strcaps_v);
2054 gst_caps_unref (raw_audio_caps);
2055 gst_caps_unref (raw_video_caps);
2056 gst_plugin_feature_list_free (muxers_and_encoders);
2061 g_clear_object (&res);
2066 static GstEncodingProfile *
2067 profile_from_string (const gchar * string)
2069 GstEncodingProfile *profile;
2070 gchar *filename_end;
2072 profile = combo_search (string);
2077 filename_end = g_strrstr (string, ".gep");
2079 GstEncodingTarget *target;
2080 gchar *profilename = NULL, *filename;
2082 if (filename_end[4] == ':')
2083 profilename = g_strdup (&filename_end[5]);
2085 if (filename_end[4] == '\0' || profilename) {
2086 filename = g_strndup (string, filename_end - string + strlen (".gep"));
2088 target = gst_encoding_target_load_from_file (filename, NULL);
2090 profile = gst_encoding_target_get_profile (target,
2091 profilename ? profilename : "default");
2092 gst_encoding_target_unref (target);
2095 g_free (profilename);
2101 profile = parse_encoding_profile (string);
2106 /* GValue transform function */
2108 string_to_profile_transform (const GValue * src_value, GValue * dest_value)
2110 const gchar *profilename;
2111 GstEncodingProfile *profile;
2113 profilename = g_value_get_string (src_value);
2115 profile = profile_from_string (profilename);
2118 g_value_take_object (dest_value, (GObject *) profile);
2122 serialize_profile (GString * res, GstEncodingProfile * profile)
2127 g_string_append_c (res, ':');
2129 if (profile->restriction) {
2130 tmp = gst_caps_to_string (profile->restriction);
2131 g_string_append_printf (res, "%s->", tmp);
2135 tmp = gst_caps_to_string (profile->format);
2136 g_string_append (res, tmp);
2138 if (profile->presence)
2139 g_string_append_printf (res, "|presence=%d", profile->presence);
2141 if (profile->single_segment)
2142 g_string_append_printf (res, "%ssingle-segment=true",
2143 profile->presence ? "" : "|");
2145 if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
2148 for (tmp = GST_ENCODING_CONTAINER_PROFILE (profile)->encodingprofiles; tmp;
2150 serialize_profile (res, tmp->data);
2155 gst_encoding_profile_serialize_valfunc (GValue * value)
2157 GString *res = g_string_new (NULL);
2158 GstEncodingProfile *profile = g_value_get_object (value);
2160 serialize_profile (res, profile);
2162 return g_string_free (res, FALSE);
2166 gst_encoding_profile_deserialize_valfunc (GValue * value, const gchar * s)
2168 GstEncodingProfile *profile;
2170 profile = profile_from_string (s);
2173 g_value_take_object (value, (GObject *) profile);
2180 static GstEncodingProfile *
2181 create_stream_profile_recurse (GstEncodingProfile * toplevel,
2182 GstDiscovererStreamInfo * sinfo)
2184 GstEncodingProfile *profile = NULL;
2188 caps = gst_discoverer_stream_info_get_caps (sinfo);
2190 /* Should unify this with copy_and_clean_caps() */
2191 caps = gst_caps_make_writable (caps);
2192 s = gst_caps_get_structure (caps, 0);
2194 gst_structure_remove_fields (s, "codec_data", "streamheader", "parsed",
2195 "colorimetry", "framed", "stream-format", "alignment", "tier", "level",
2196 "profile", "chroma-format", "bit-depth-luma", "bit-depth-chroma", NULL);
2198 GST_LOG ("Stream: %" GST_PTR_FORMAT, caps);
2199 if (GST_IS_DISCOVERER_AUDIO_INFO (sinfo)) {
2201 (GstEncodingProfile *) gst_encoding_audio_profile_new (caps, NULL,
2203 } else if (GST_IS_DISCOVERER_VIDEO_INFO (sinfo)) {
2205 (GstEncodingProfile *) gst_encoding_video_profile_new (caps, NULL,
2207 } else if (GST_IS_DISCOVERER_CONTAINER_INFO (sinfo)) {
2208 GList *streams, *stream;
2211 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
2214 if (!toplevel || !GST_IS_ENCODING_CONTAINER_PROFILE (toplevel)) {
2215 GstEncodingProfile *prev_toplevel = toplevel;
2217 toplevel = (GstEncodingProfile *)
2218 gst_encoding_container_profile_new ("auto-generated",
2219 "Automatically generated from GstDiscovererInfo", caps, NULL);
2221 gst_encoding_container_profile_add_profile
2222 (GST_ENCODING_CONTAINER_PROFILE (toplevel), prev_toplevel);
2225 for (stream = streams; stream; stream = stream->next)
2226 create_stream_profile_recurse (toplevel,
2227 (GstDiscovererStreamInfo *) stream->data);
2228 gst_discoverer_stream_info_list_free (streams);
2230 GST_FIXME ("Ignoring stream of type '%s'",
2231 g_type_name (G_OBJECT_TYPE (sinfo)));
2232 /* subtitles or other ? ignore for now */
2234 gst_caps_unref (caps);
2237 const gchar *stream_id = gst_discoverer_stream_info_get_stream_id (sinfo);
2240 const gchar *subid = strchr (stream_id, '/');
2242 gst_encoding_profile_set_name (profile, subid ? subid : stream_id);
2245 if (GST_IS_ENCODING_CONTAINER_PROFILE (toplevel))
2246 gst_encoding_container_profile_add_profile ((GstEncodingContainerProfile
2251 if (!toplevel && profile)
2254 sinfo = gst_discoverer_stream_info_get_next (sinfo);
2256 return create_stream_profile_recurse (toplevel, sinfo);
2262 _compare_profile_names (const GstEncodingProfile * a,
2263 const GstEncodingProfile * b)
2265 return g_strcmp0 (a->name, b->name);
2269 * gst_encoding_profile_from_discoverer:
2270 * @info: (transfer none): The #GstDiscovererInfo to read from
2272 * Creates a #GstEncodingProfile matching the formats from the given
2273 * #GstDiscovererInfo. Streams other than audio or video (eg,
2274 * subtitles), are currently ignored.
2276 * Returns: (transfer full): The new #GstEncodingProfile or %NULL.
2278 GstEncodingProfile *
2279 gst_encoding_profile_from_discoverer (GstDiscovererInfo * info)
2281 GstEncodingProfile *profile;
2282 GstDiscovererStreamInfo *sinfo;
2284 if (!info || gst_discoverer_info_get_result (info) != GST_DISCOVERER_OK)
2287 sinfo = gst_discoverer_info_get_stream_info (info);
2291 profile = create_stream_profile_recurse (NULL, sinfo);
2292 if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
2293 if (!gst_encoding_container_profile_get_profiles
2294 (GST_ENCODING_CONTAINER_PROFILE (profile))) {
2295 GST_ERROR ("Failed to add any streams");
2296 g_object_unref (profile);
2299 /* Sort by stream ID */
2300 GST_ENCODING_CONTAINER_PROFILE (profile)->encodingprofiles =
2301 g_list_sort (GST_ENCODING_CONTAINER_PROFILE (profile)->encodingprofiles,
2302 (GCompareFunc) _compare_profile_names);
2306 return (GstEncodingProfile *) profile;
2310 * gst_encoding_profile_copy:
2311 * @self: The #GstEncodingProfile to copy
2313 * Makes a deep copy of @self
2315 * Returns: (transfer full): The copy of @self
2319 GstEncodingProfile *
2320 gst_encoding_profile_copy (GstEncodingProfile * self)
2322 GstEncodingProfileClass *klass =
2323 (GstEncodingProfileClass *) G_OBJECT_GET_CLASS (self);
2324 GstEncodingProfile *copy =
2325 common_creation (G_OBJECT_TYPE (self), self->format, self->preset,
2326 self->name, self->description, self->restriction, self->presence);
2328 copy->enabled = self->enabled;
2329 copy->allow_dynamic_output = self->allow_dynamic_output;
2330 gst_encoding_profile_set_preset_name (copy, self->preset_name);
2331 gst_encoding_profile_set_description (copy, self->description);
2334 klass->copy (self, copy);