encoding-profile.c 56.6 KB
Newer Older
Edward Hervey's avatar
Edward Hervey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* GStreamer encoding profiles library
 * Copyright (C) 2009-2010 Edward Hervey <edward.hervey@collabora.co.uk>
 *           (C) 2009-2010 Nokia Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
17 18
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Edward Hervey's avatar
Edward Hervey committed
19 20 21 22
 */

/**
 * SECTION:encoding-profile
23
 * @title: GstEncodingProfile
Edward Hervey's avatar
Edward Hervey committed
24 25 26
 * @short_description: Encoding profile library
 *
 * Functions to create and handle encoding profiles.
27 28 29
 *
 * Encoding profiles describe the media types and settings one wishes to use
 * for an encoding process. The top-level profiles are commonly
30 31 32 33
 * #GstEncodingContainerProfile(s) (which contains a user-readable name and
 * description along with which container format to use). These, in turn,
 * reference one or more #GstEncodingProfile(s) which indicate which encoding
 * format should be used on each individual streams.
34 35 36 37 38 39 40 41 42 43 44 45
 *
 * #GstEncodingProfile(s) can be provided to the 'encodebin' element, which
 * will take care of selecting and setting up the required elements to produce
 * an output stream conforming to the specifications of the profile.
 *
 * Unlike other systems, the encoding profiles do not specify which #GstElement
 * to use for the various encoding and muxing steps, but instead relies on
 * specifying the format one wishes to use.
 *
 * Encoding profiles can be created at runtime by the application or loaded
 * from (and saved to) file using the #GstEncodingTarget API.
 *
46
 * # Defining a GstEncodingProfile as a string
47
 *
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 * ## Serialized encoding profile formats
 *
 * ## Using encoders and muxer element factory name:
 *
 * |[
 *   muxer_factory_name:video_encoder_factory_name:audio_encoder_factory_name
 * ]|
 *
 * For example to encode a stream into a WebM container, with an OGG audio
 * stream and a VP8 video stream, the serialized #GstEncodingProfile looks
 * like:
 *
 * |[
 *   webmmux:vp8enc:vorbisenc
 * ]|
 *
 * ## Define the encoding profile in a generic way using caps:
65 66
 *
 * |[
67
 *   muxer_source_caps:video_encoder_source_caps:audio_encoder_source_caps
68 69 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
72 73 74 75 76 77
 * like:
 *
 * |[
 *   video/webm:video/x-vp8:audio/x-vorbis
 * ]|
 *
78 79 80 81 82
 * It is possible to mix caps and element type names so you can specify a specific
 * video encoder while using caps for other encoders/muxer.
 *
 * ## Advanced encoding format serialization features:
 *
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
 * You can also set the preset name of the encoding profile using the
 * caps+preset_name syntax as in:
 *
 * |[
 *   video/webm:video/x-vp8+youtube-preset:audio/x-vorbis
 * ]|
 *
 * Moreover, you can set the `presence` property of an
 * encoding profile using the `|presence` syntax as in:
 *
 *  |[
 *   video/webm:video/x-vp8|1:audio/x-vorbis
 * ]|
 *
 * This field allows specifies the maximum number of times a
 * #GstEncodingProfile can be used inside an encodebin. If 0, it is not a
99
 * mandatory stream and can be used as many times as necessary.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
 *
 * You can also use the `restriction_caps->encoded_format_caps` syntax to
 * specify the restriction caps to be set on a #GstEncodingProfile
 *
 * It corresponds to the restriction #GstCaps to apply before the encoder that
 * will be used in the profile. The fields present in restriction caps are
 * properties of the raw stream (that is, before encoding), such as height and
 * width for video and depth and sampling rate for audio. This property does
 * not make sense for muxers. See #gst_encoding_profile_get_restriction for
 * more details.
 *
 * To force a video stream to be encoded with a Full HD resolution (using WebM
 * as the container format, VP8 as the video codec and Vorbis as the audio
 * codec), you should use:
 *
 * |[
116 117 118 119 120 121 122 123 124 125
 *   "video/webm:video/x-raw,width=1920,height=1080->video/x-vp8:audio/x-vorbis"
 * ]|
 *
 * > NOTE: Make sure to enclose into quotes to avoid '>' to be reinterpreted by
 * > the shell.
 *
 * In the case you are using encoder types, the following is also possible:
 *
 * |[
 *   "matroskamux:x264enc,width=1920,height=1080:audio/x-vorbis"
126
 * ]|
127
 *
128 129 130 131
 * ## Some serialized encoding formats examples:
 *
 * MP3 audio and H264 in MP4:
 *
Edward Hervey's avatar
Edward Hervey committed
132
 * |[
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
 *   video/quicktime,variant=iso:video/x-h264:audio/mpeg,mpegversion=1,layer=3
 * ]|
 *
 * Vorbis and theora in OGG:
 *
 * |[
 *   application/ogg:video/x-theora:audio/x-vorbis
 * ]|
 *
 * AC3 and H264 in MPEG-TS:
 *
 * |[
 *   video/mpegts:video/x-h264:audio/x-ac3
 * ]|
 *
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
 * ## Loading a profile from encoding targets
 *
 * Anywhere where you have to use a string to define a #GstEncodingProfile,
 * you can use load it from a #GstEncodingTarget using the following syntaxes:
 *
 * |[
 *   target_name[/profilename/category]
 * ]|
 *
 * or
 *
 * |[
 *   /path/to/target.gep:profilename
 * ]|
 *
163 164 165
 * # Example: Creating a profile
 *
 * |[<!-- language="c" -->
Edward Hervey's avatar
Edward Hervey committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
 * #include <gst/pbutils/encoding-profile.h>
 * ...
 * GstEncodingProfile *
 * create_ogg_theora_profile(void)
 *{
 *  GstEncodingContainerProfile *prof;
 *  GstCaps *caps;
 *
 *  caps = gst_caps_from_string("application/ogg");
 *  prof = gst_encoding_container_profile_new("Ogg audio/video",
 *     "Standard OGG/THEORA/VORBIS",
 *     caps, NULL);
 *  gst_caps_unref (caps);
 *
 *  caps = gst_caps_from_string("video/x-theora");
181
 *  gst_encoding_container_profile_add_profile(prof,
Edward Hervey's avatar
Edward Hervey committed
182 183 184 185
 *       (GstEncodingProfile*) gst_encoding_video_profile_new(caps, NULL, NULL, 0));
 *  gst_caps_unref (caps);
 *
 *  caps = gst_caps_from_string("audio/x-vorbis");
186
 *  gst_encoding_container_profile_add_profile(prof,
Edward Hervey's avatar
Edward Hervey committed
187 188 189 190 191 192 193
 *       (GstEncodingProfile*) gst_encoding_audio_profile_new(caps, NULL, NULL, 0));
 *  gst_caps_unref (caps);
 *
 *  return (GstEncodingProfile*) prof;
 *}
 *
 * ]|
194 195 196 197
 *
 * # Example: Using an encoder preset with a profile
 *
 * |[ <!-- language="c" -->
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
 * #include <gst/pbutils/encoding-profile.h>
 * ...
 * GstEncodingProfile *
 * create_ogg_theora_profile(void)
 *{
 *  GstEncodingVideoProfile *v;
 *  GstEncodingAudioProfile *a;
 *  GstEncodingContainerProfile *prof;
 *  GstCaps *caps;
 *  GstPreset *preset;
 *
 *  caps = gst_caps_from_string ("application/ogg");
 *  prof = gst_encoding_container_profile_new ("Ogg audio/video",
 *     "Standard OGG/THEORA/VORBIS",
 *     caps, NULL);
 *  gst_caps_unref (caps);
 *
 *  preset = GST_PRESET (gst_element_factory_make ("theoraenc", "theorapreset"));
 *  g_object_set (preset, "bitrate", 1000, NULL);
 *  // The preset will be saved on the filesystem,
 *  // so try to use a descriptive name
 *  gst_preset_save_preset (preset, "theora_bitrate_preset");
 *  gst_object_unref (preset);
 *
 *  caps = gst_caps_from_string ("video/x-theora");
 *  v = gst_encoding_video_profile_new (caps, "theorapreset", NULL, 0);
 *  gst_encoding_container_profile_add_profile (prof, (GstEncodingProfile*) v);
 *  gst_caps_unref (caps);
 *
 *  caps = gst_caps_from_string ("audio/x-vorbis");
 *  a = gst_encoding_audio_profile_new (caps, NULL, NULL, 0);
 *  gst_encoding_container_profile_add_profile (prof, (GstEncodingProfile*) a);
 *  gst_caps_unref (caps);
 *
 *  return (GstEncodingProfile*) prof;
 *}
 *
 * ]|
236 237 238 239
 *
 * # Example: Listing categories, targets and profiles
 *
 * |[ <!-- language="C" -->
Edward Hervey's avatar
Edward Hervey committed
240 241
 * #include <gst/pbutils/encoding-profile.h>
 * ...
242 243 244 245
 * GstEncodingProfile *prof;
 * GList *categories, *tmpc;
 * GList *targets, *tmpt;
 * ...
246
 * categories = gst_encoding_list_available_categories ();
Edward Hervey's avatar
Edward Hervey committed
247
 *
248
 * ... Show available categories to user ...
Edward Hervey's avatar
Edward Hervey committed
249
 *
250 251
 * for (tmpc = categories; tmpc; tmpc = tmpc->next) {
 *   gchar *category = (gchar *) tmpc->data;
Edward Hervey's avatar
Edward Hervey committed
252
 *
253
 *   ... and we can list all targets within that category ...
254
 *
255
 *   targets = gst_encoding_list_all_targets (category);
Edward Hervey's avatar
Edward Hervey committed
256
 *
257 258 259 260 261 262 263 264 265 266
 *   ... and show a list to our users ...
 *
 *   g_list_foreach (targets, (GFunc) gst_encoding_target_unref, NULL);
 *   g_list_free (targets);
 * }
 *
 * g_list_foreach (categories, (GFunc) g_free, NULL);
 * g_list_free (categories);
 *
 * ...
Edward Hervey's avatar
Edward Hervey committed
267
 * ]|
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
 *
 * # Encoding Target
 *
 * On top of the notion of profiles, we implement the notion of EncodingTarget.
 * Encoding Targets are basically a higher level of abstraction to define formats
 * for specific target types. Those can define several GstEncodingProfiles with
 * different names, for example one for transcoding in full HD, another one for
 * low res, etc.. which are defined in the same encoding target.
 *
 * Basically if you wan to encode a stream to send it to, say, youtube you should
 * have a Youtube encoding target defined in the "online-service" category.
 *
 * ## Encoding target serialization format
 *
 * Encoding targets are serialized in a KeyFile like files.
 *
 * |[
 * [GStreamer Encoding Target]
 * name : <name>
 * category : <category>
 * \description : <description> #translatable
 *
 * [profile-<profile1name>]
 * name : <name>
 * \description : <description> #optional
 * format : <format>
 * preset : <preset>
 *
 * [streamprofile-<id>]
 * parent : <encodingprofile.name>[,<encodingprofile.name>..]
 * \type : <type> # "audio", "video", "text"
 * format : <format>
 * preset : <preset>
 * restriction : <restriction>
 * presence : <presence>
 * pass : <pass>
 * variableframerate : <variableframerate>
 * ]|
 *
 * # Location of encoding target files
 *
 * $GST_DATADIR/gstreamer-GST_API_VERSION/encoding-profile
 * $HOME/gstreamer-GST_API_VERSION/encoding-profile
 *
 * There also is a GST_ENCODING_TARGET_PATH environment variable
 * defining a list of folder containing encoding target files.
 *
 * ## Naming convention
 *
 * |[
 *   $(target.category)/$(target.name).gep
 * ]|
 *
 * # Naming restrictions:
 *
 *  * lowercase ASCII letter for the first character
 *  * Same for all other characters + numerics + hyphens
Edward Hervey's avatar
Edward Hervey committed
325 326 327 328 329 330 331
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "encoding-profile.h"
332
#include "encoding-target.h"
Edward Hervey's avatar
Edward Hervey committed
333

334 335
#include <string.h>

Edward Hervey's avatar
Edward Hervey committed
336 337 338 339
/* GstEncodingProfile API */

struct _GstEncodingProfile
{
340
  GObject parent;
Edward Hervey's avatar
Edward Hervey committed
341 342 343 344 345 346

  /*< public > */
  gchar *name;
  gchar *description;
  GstCaps *format;
  gchar *preset;
347
  gchar *preset_name;
Edward Hervey's avatar
Edward Hervey committed
348 349
  guint presence;
  GstCaps *restriction;
350
  gboolean allow_dynamic_output;
351
  gboolean enabled;
Edward Hervey's avatar
Edward Hervey committed
352 353
};

354 355 356
struct _GstEncodingProfileClass
{
  GObjectClass parent_class;
357 358

  void (*copy) (GstEncodingProfile * self, GstEncodingProfile * copy);
359 360
};

361 362 363 364 365 366 367 368 369
enum
{
  FIRST_PROPERTY,
  PROP_RESTRICTION_CAPS,
  LAST_PROPERTY
};

static GParamSpec *_properties[LAST_PROPERTY];

370 371 372 373 374 375 376
static void string_to_profile_transform (const GValue * src_value,
    GValue * dest_value);
static gboolean gst_encoding_profile_deserialize_valfunc (GValue * value,
    const gchar * s);

static void gst_encoding_profile_class_init (GstEncodingProfileClass * klass);
static gpointer gst_encoding_profile_parent_class = NULL;
Edward Hervey's avatar
Edward Hervey committed
377 378

static void
379 380 381 382 383 384 385 386 387 388 389 390
gst_encoding_profile_class_intern_init (gpointer klass)
{
  gst_encoding_profile_parent_class = g_type_class_peek_parent (klass);
  gst_encoding_profile_class_init ((GstEncodingProfileClass *) klass);
}

GType
gst_encoding_profile_get_type (void)
{
  static volatile gsize g_define_type_id__volatile = 0;

  if (g_once_init_enter (&g_define_type_id__volatile)) {
391
    GType g_define_type_id = g_type_register_static_simple (G_TYPE_OBJECT,
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
        g_intern_static_string ("GstEncodingProfile"),
        sizeof (GstEncodingProfileClass),
        (GClassInitFunc) gst_encoding_profile_class_intern_init,
        sizeof (GstEncodingProfile),
        NULL,
        (GTypeFlags) 0);
    static GstValueTable gstvtable = {
      G_TYPE_NONE,
      (GstValueCompareFunc) NULL,
      (GstValueSerializeFunc) NULL,
      (GstValueDeserializeFunc) gst_encoding_profile_deserialize_valfunc
    };

    gstvtable.type = g_define_type_id;

    /* Register a STRING=>PROFILE GValueTransformFunc */
    g_value_register_transform_func (G_TYPE_STRING, g_define_type_id,
        string_to_profile_transform);
    /* Register gst-specific GValue functions */
    gst_value_register (&gstvtable);

    g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
  }
  return g_define_type_id__volatile;
Edward Hervey's avatar
Edward Hervey committed
416 417
}

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

static void
_encoding_profile_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstEncodingProfile *prof = (GstEncodingProfile *) object;

  switch (prop_id) {
    case PROP_RESTRICTION_CAPS:
      gst_value_set_caps (value, prof->restriction);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
_encoding_profile_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstEncodingProfile *prof = (GstEncodingProfile *) object;

  switch (prop_id) {
    case PROP_RESTRICTION_CAPS:
      gst_encoding_profile_set_restriction (prof, gst_caps_copy
          (gst_value_get_caps (value)));
445
      break;
446 447 448 449 450 451
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

Edward Hervey's avatar
Edward Hervey committed
452
static void
453
gst_encoding_profile_finalize (GObject * object)
Edward Hervey's avatar
Edward Hervey committed
454
{
455
  GstEncodingProfile *prof = (GstEncodingProfile *) object;
456
  g_free (prof->name);
Edward Hervey's avatar
Edward Hervey committed
457 458
  if (prof->format)
    gst_caps_unref (prof->format);
459 460
  g_free (prof->preset);
  g_free (prof->description);
Edward Hervey's avatar
Edward Hervey committed
461 462
  if (prof->restriction)
    gst_caps_unref (prof->restriction);
463
  g_free (prof->preset_name);
Edward Hervey's avatar
Edward Hervey committed
464 465 466
}

static void
467
gst_encoding_profile_class_init (GstEncodingProfileClass * klass)
Edward Hervey's avatar
Edward Hervey committed
468
{
469 470 471
  GObjectClass *gobject_class = (GObjectClass *) klass;

  gobject_class->finalize = gst_encoding_profile_finalize;
472 473 474 475 476 477 478 479 480 481 482 483

  gobject_class->set_property = _encoding_profile_set_property;
  gobject_class->get_property = _encoding_profile_get_property;

  _properties[PROP_RESTRICTION_CAPS] =
      g_param_spec_boxed ("restriction-caps", "Restriction caps",
      "The restriction caps to use", GST_TYPE_CAPS,
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_property (gobject_class,
      PROP_RESTRICTION_CAPS, _properties[PROP_RESTRICTION_CAPS]);

Edward Hervey's avatar
Edward Hervey committed
484 485 486 487 488 489 490 491 492 493 494
}

/**
 * gst_encoding_profile_get_name:
 * @profile: a #GstEncodingProfile
 *
 * Returns: the name of the profile, can be %NULL.
 */
const gchar *
gst_encoding_profile_get_name (GstEncodingProfile * profile)
{
495 496
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);

Edward Hervey's avatar
Edward Hervey committed
497 498 499 500 501 502 503 504 505 506 507 508
  return profile->name;
}

/**
 * gst_encoding_profile_get_description:
 * @profile: a #GstEncodingProfile
 *
 * Returns: the description of the profile, can be %NULL.
 */
const gchar *
gst_encoding_profile_get_description (GstEncodingProfile * profile)
{
509 510
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);

Edward Hervey's avatar
Edward Hervey committed
511 512 513 514 515 516 517
  return profile->description;
}

/**
 * gst_encoding_profile_get_format:
 * @profile: a #GstEncodingProfile
 *
518 519
 * Returns: (transfer full): the #GstCaps corresponding to the media format used
 * in the profile. Unref after usage.
Edward Hervey's avatar
Edward Hervey committed
520
 */
521
GstCaps *
Edward Hervey's avatar
Edward Hervey committed
522 523
gst_encoding_profile_get_format (GstEncodingProfile * profile)
{
524 525
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);

526
  return (profile->format ? gst_caps_ref (profile->format) : NULL);
Edward Hervey's avatar
Edward Hervey committed
527 528 529 530 531 532
}

/**
 * gst_encoding_profile_get_preset:
 * @profile: a #GstEncodingProfile
 *
533 534
 * Returns: the name of the #GstPreset to be used in the profile.
 * This is the name that has been set when saving the preset.
Edward Hervey's avatar
Edward Hervey committed
535 536 537 538
 */
const gchar *
gst_encoding_profile_get_preset (GstEncodingProfile * profile)
{
539 540
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);

Edward Hervey's avatar
Edward Hervey committed
541 542 543
  return profile->preset;
}

544 545 546 547
/**
 * gst_encoding_profile_get_preset_name:
 * @profile: a #GstEncodingProfile
 *
548
 * Returns: the name of the #GstPreset factory to be used in the profile.
549 550 551 552
 */
const gchar *
gst_encoding_profile_get_preset_name (GstEncodingProfile * profile)
{
553 554
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);

555 556 557
  return profile->preset_name;
}

Edward Hervey's avatar
Edward Hervey committed
558 559 560 561
/**
 * gst_encoding_profile_get_presence:
 * @profile: a #GstEncodingProfile
 *
562 563
 * Returns: The number of times the profile is used in its parent
 * container profile. If 0, it is not a mandatory stream.
Edward Hervey's avatar
Edward Hervey committed
564
 */
Rob Clark's avatar
Rob Clark committed
565
guint
Edward Hervey's avatar
Edward Hervey committed
566 567
gst_encoding_profile_get_presence (GstEncodingProfile * profile)
{
568 569
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), 0);

Edward Hervey's avatar
Edward Hervey committed
570 571 572
  return profile->presence;
}

573 574 575 576 577
/**
 * gst_encoding_profile_get_enabled:
 * @profile: a #GstEncodingProfile
 *
 * Returns: Whther @profile is enabled or not
578 579
 *
 * Since 1.6
580 581 582 583 584 585 586 587 588
 */
gboolean
gst_encoding_profile_is_enabled (GstEncodingProfile * profile)
{
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);

  return profile->enabled;
}

Edward Hervey's avatar
Edward Hervey committed
589 590 591 592
/**
 * gst_encoding_profile_get_restriction:
 * @profile: a #GstEncodingProfile
 *
593
 * Returns: (transfer full): The restriction #GstCaps to apply before the encoder
594 595 596 597
 * that will be used in the profile. The fields present in restriction caps are
 * properties of the raw stream (that is before encoding), such as height and
 * width for video and depth and sampling rate for audio. Does not apply to
 * #GstEncodingContainerProfile (since there is no corresponding raw stream).
598
 * Can be %NULL. Unref after usage.
Edward Hervey's avatar
Edward Hervey committed
599
 */
600
GstCaps *
Edward Hervey's avatar
Edward Hervey committed
601 602
gst_encoding_profile_get_restriction (GstEncodingProfile * profile)
{
603 604 605
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);


606
  return (profile->restriction ? gst_caps_ref (profile->restriction) : NULL);
Edward Hervey's avatar
Edward Hervey committed
607 608 609 610 611
}

/**
 * gst_encoding_profile_set_name:
 * @profile: a #GstEncodingProfile
612
 * @name: (allow-none): the name to set on the profile
Edward Hervey's avatar
Edward Hervey committed
613 614 615 616 617 618 619
 *
 * Set @name as the given name for the @profile. A copy of @name will be made
 * internally.
 */
void
gst_encoding_profile_set_name (GstEncodingProfile * profile, const gchar * name)
{
620 621
  g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));

622
  g_free (profile->name);
Edward Hervey's avatar
Edward Hervey committed
623 624 625 626 627 628
  profile->name = g_strdup (name);
}

/**
 * gst_encoding_profile_set_description:
 * @profile: a #GstEncodingProfile
629
 * @description: (allow-none): the description to set on the profile
Edward Hervey's avatar
Edward Hervey committed
630
 *
631 632
 * Set @description as the given description for the @profile. A copy of
 * @description will be made internally.
Edward Hervey's avatar
Edward Hervey committed
633 634 635 636 637
 */
void
gst_encoding_profile_set_description (GstEncodingProfile * profile,
    const gchar * description)
{
638 639
  g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));

640
  g_free (profile->description);
Edward Hervey's avatar
Edward Hervey committed
641 642 643 644 645 646
  profile->description = g_strdup (description);
}

/**
 * gst_encoding_profile_set_format:
 * @profile: a #GstEncodingProfile
647
 * @format: (transfer none): the media format to use in the profile.
Edward Hervey's avatar
Edward Hervey committed
648 649 650 651 652 653
 *
 * Sets the media format used in the profile.
 */
void
gst_encoding_profile_set_format (GstEncodingProfile * profile, GstCaps * format)
{
654 655
  g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));

Edward Hervey's avatar
Edward Hervey committed
656 657 658 659 660
  if (profile->format)
    gst_caps_unref (profile->format);
  profile->format = gst_caps_ref (format);
}

661 662 663 664 665 666 667 668 669 670
/**
 * gst_encoding_profile_get_allow_dynamic_output:
 * @profile: a #GstEncodingProfile
 *
 * Get whether the format that has been negotiated in at some point can be renegotiated
 * later during the encoding.
 */
gboolean
gst_encoding_profile_get_allow_dynamic_output (GstEncodingProfile * profile)
{
671 672
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
  return profile->allow_dynamic_output;
}

/**
 * gst_encoding_profile_set_allow_dynamic_output:
 * @profile: a #GstEncodingProfile
 * @allow_dynamic_output: Whether the format that has been negotiated first can be renegotiated
 * during the encoding
 *
 * Sets whether the format that has been negotiated in at some point can be renegotiated
 * later during the encoding.
 */
void
gst_encoding_profile_set_allow_dynamic_output (GstEncodingProfile * profile,
    gboolean allow_dynamic_output)
{
689 690
  g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));

691 692 693
  profile->allow_dynamic_output = allow_dynamic_output;
}

Edward Hervey's avatar
Edward Hervey committed
694 695 696
/**
 * gst_encoding_profile_set_preset:
 * @profile: a #GstEncodingProfile
697
 * @preset: (nullable): the element preset to use
Edward Hervey's avatar
Edward Hervey committed
698
 *
699 700
 * Sets the name of the #GstElement that implements the #GstPreset interface
 * to use for the profile.
701
 * This is the name that has been set when saving the preset.
Edward Hervey's avatar
Edward Hervey committed
702 703 704 705 706
 */
void
gst_encoding_profile_set_preset (GstEncodingProfile * profile,
    const gchar * preset)
{
707 708
  g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));

709
  g_free (profile->preset);
Edward Hervey's avatar
Edward Hervey committed
710 711 712
  profile->preset = g_strdup (preset);
}

713
/**
714
 * gst_encoding_profile_set_preset_name:
715
 * @profile: a #GstEncodingProfile
716
 * @preset_name: (allow-none): The name of the preset to use in this @profile.
717
 *
718
 * Sets the name of the #GstPreset's factory to be used in the profile.
719 720 721 722 723
 */
void
gst_encoding_profile_set_preset_name (GstEncodingProfile * profile,
    const gchar * preset_name)
{
724 725
  g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));

726
  g_free (profile->preset_name);
727 728 729
  profile->preset_name = g_strdup (preset_name);
}

Edward Hervey's avatar
Edward Hervey committed
730 731 732 733 734 735 736 737 738 739 740
/**
 * gst_encoding_profile_set_presence:
 * @profile: a #GstEncodingProfile
 * @presence: the number of time the profile can be used
 *
 * Set the number of time the profile is used in its parent
 * container profile. If 0, it is not a mandatory stream
 */
void
gst_encoding_profile_set_presence (GstEncodingProfile * profile, guint presence)
{
741 742
  g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));

Edward Hervey's avatar
Edward Hervey committed
743 744 745
  profile->presence = presence;
}

746 747 748 749 750 751
/**
 * gst_encoding_profile_set_enabled:
 * @profile: a #GstEncodingProfile
 * @enabled: %FALSE to disable #profile, %TRUE to enable it
 *
 * Set whether the profile should be used or not.
752 753
 *
 * Since 1.6
754 755 756 757 758 759 760 761 762 763
 */
void
gst_encoding_profile_set_enabled (GstEncodingProfile * profile,
    gboolean enabled)
{
  g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));

  profile->enabled = enabled;
}

Edward Hervey's avatar
Edward Hervey committed
764 765 766
/**
 * gst_encoding_profile_set_restriction:
 * @profile: a #GstEncodingProfile
767
 * @restriction: (allow-none) (transfer full): the restriction to apply
Edward Hervey's avatar
Edward Hervey committed
768 769
 *
 * Set the restriction #GstCaps to apply before the encoder
770
 * that will be used in the profile. See gst_encoding_profile_get_restriction()
771
 * for more about restrictions. Does not apply to #GstEncodingContainerProfile.
Edward Hervey's avatar
Edward Hervey committed
772 773 774 775 776
 */
void
gst_encoding_profile_set_restriction (GstEncodingProfile * profile,
    GstCaps * restriction)
{
777 778 779
  g_return_if_fail (GST_IS_CAPS (restriction));
  g_return_if_fail (GST_IS_ENCODING_PROFILE (profile));

Edward Hervey's avatar
Edward Hervey committed
780 781 782
  if (profile->restriction)
    gst_caps_unref (profile->restriction);
  profile->restriction = restriction;
783 784 785

  g_object_notify_by_pspec (G_OBJECT (profile),
      _properties[PROP_RESTRICTION_CAPS]);
Edward Hervey's avatar
Edward Hervey committed
786 787 788 789 790 791 792 793 794 795 796
}

/* Container profiles */

struct _GstEncodingContainerProfile
{
  GstEncodingProfile parent;

  GList *encodingprofiles;
};

797 798 799 800 801
struct _GstEncodingContainerProfileClass
{
  GstEncodingProfileClass parent;
};

Edward Hervey's avatar
Edward Hervey committed
802 803 804 805 806 807 808 809 810 811
G_DEFINE_TYPE (GstEncodingContainerProfile, gst_encoding_container_profile,
    GST_TYPE_ENCODING_PROFILE);

static void
gst_encoding_container_profile_init (GstEncodingContainerProfile * prof)
{
  /* Nothing to initialize */
}

static void
812
gst_encoding_container_profile_finalize (GObject * object)
Edward Hervey's avatar
Edward Hervey committed
813
{
814 815
  GstEncodingContainerProfile *prof = (GstEncodingContainerProfile *) object;

Wim Taymans's avatar
Wim Taymans committed
816
  g_list_foreach (prof->encodingprofiles, (GFunc) g_object_unref, NULL);
Edward Hervey's avatar
Edward Hervey committed
817 818
  g_list_free (prof->encodingprofiles);

819 820
  G_OBJECT_CLASS (gst_encoding_container_profile_parent_class)->finalize
      ((GObject *) prof);
Edward Hervey's avatar
Edward Hervey committed
821 822
}

823 824 825 826 827 828 829 830 831 832 833 834 835 836
static void
gst_encoding_container_profile_copy (GstEncodingProfile * profile,
    GstEncodingProfile * copy_profile)
{
  GstEncodingContainerProfile *self = GST_ENCODING_CONTAINER_PROFILE (profile),
      *copy = GST_ENCODING_CONTAINER_PROFILE (copy_profile);
  GList *tmp;

  for (tmp = self->encodingprofiles; tmp; tmp = tmp->next) {
    gst_encoding_container_profile_add_profile (copy,
        gst_encoding_profile_copy (tmp->data));
  }
}

Edward Hervey's avatar
Edward Hervey committed
837
static void
838
gst_encoding_container_profile_class_init (GstEncodingContainerProfileClass * k)
Edward Hervey's avatar
Edward Hervey committed
839
{
840 841 842
  GObjectClass *gobject_class = (GObjectClass *) k;

  gobject_class->finalize = gst_encoding_container_profile_finalize;
843 844

  ((GstEncodingProfileClass *) k)->copy = gst_encoding_container_profile_copy;
Edward Hervey's avatar
Edward Hervey committed
845 846
}

847 848 849 850 851
/**
 * gst_encoding_container_profile_get_profiles:
 * @profile: a #GstEncodingContainerProfile
 *
 * Returns: (element-type GstPbutils.EncodingProfile) (transfer none):
852 853
 * the list of contained #GstEncodingProfile.
 */
Edward Hervey's avatar
Edward Hervey committed
854 855 856 857
const GList *
gst_encoding_container_profile_get_profiles (GstEncodingContainerProfile *
    profile)
{
858 859
  g_return_val_if_fail (GST_IS_ENCODING_CONTAINER_PROFILE (profile), NULL);

Edward Hervey's avatar
Edward Hervey committed
860 861 862 863 864 865 866 867 868 869 870 871 872
  return profile->encodingprofiles;
}

/* Video profiles */

struct _GstEncodingVideoProfile
{
  GstEncodingProfile parent;

  guint pass;
  gboolean variableframerate;
};

873 874 875 876 877
struct _GstEncodingVideoProfileClass
{
  GstEncodingProfileClass parent;
};

Edward Hervey's avatar
Edward Hervey committed
878 879 880
G_DEFINE_TYPE (GstEncodingVideoProfile, gst_encoding_video_profile,
    GST_TYPE_ENCODING_PROFILE);

881 882 883 884 885 886 887 888 889 890 891
static void
gst_encoding_video_profile_copy (GstEncodingProfile * profile,
    GstEncodingProfile * copy_profile)
{
  GstEncodingVideoProfile *self = GST_ENCODING_VIDEO_PROFILE (profile),
      *copy = GST_ENCODING_VIDEO_PROFILE (copy_profile);

  copy->pass = self->pass;
  copy->variableframerate = self->variableframerate;
}

Edward Hervey's avatar
Edward Hervey committed
892 893 894 895 896 897 898
static void
gst_encoding_video_profile_init (GstEncodingVideoProfile * prof)
{
  /* Nothing to initialize */
}

static void
899
gst_encoding_video_profile_class_init (GstEncodingVideoProfileClass * klass)
Edward Hervey's avatar
Edward Hervey committed
900
{
901
  ((GstEncodingProfileClass *) klass)->copy = gst_encoding_video_profile_copy;
Edward Hervey's avatar
Edward Hervey committed
902 903 904 905 906 907
}

/**
 * gst_encoding_video_profile_get_pass:
 * @prof: a #GstEncodingVideoProfile
 *
908 909 910 911
 * Get the pass number if this is part of a multi-pass profile.
 *
 * Returns: The pass number. Starts at 1 for multi-pass. 0 if this is
 * not a multi-pass profile
912
 */
Edward Hervey's avatar
Edward Hervey committed
913 914 915
guint
gst_encoding_video_profile_get_pass (GstEncodingVideoProfile * prof)
{
916 917
  g_return_val_if_fail (GST_IS_ENCODING_VIDEO_PROFILE (prof), 0);

Edward Hervey's avatar
Edward Hervey committed
918 919 920 921 922 923 924 925 926 927 928 929 930
  return prof->pass;
}

/**
 * gst_encoding_video_profile_get_variableframerate:
 * @prof: a #GstEncodingVideoProfile
 *
 * Returns: Whether non-constant video framerate is allowed for encoding.
 */
gboolean
gst_encoding_video_profile_get_variableframerate (GstEncodingVideoProfile *
    prof)
{
931 932
  g_return_val_if_fail (GST_IS_ENCODING_VIDEO_PROFILE (prof), FALSE);

Edward Hervey's avatar
Edward Hervey committed
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
  return prof->variableframerate;
}

/**
 * gst_encoding_video_profile_set_pass:
 * @prof: a #GstEncodingVideoProfile
 * @pass: the pass number for this profile
 *
 * Sets the pass number of this video profile. The first pass profile should have
 * this value set to 1. If this video profile isn't part of a multi-pass profile,
 * you may set it to 0 (the default value).
 */
void
gst_encoding_video_profile_set_pass (GstEncodingVideoProfile * prof, guint pass)
{
948 949
  g_return_if_fail (GST_IS_ENCODING_VIDEO_PROFILE (prof));

Edward Hervey's avatar
Edward Hervey committed
950 951 952 953 954 955 956 957
  prof->pass = pass;
}

/**
 * gst_encoding_video_profile_set_variableframerate:
 * @prof: a #GstEncodingVideoProfile
 * @variableframerate: a boolean
 *
Piotr Fusik's avatar
Piotr Fusik committed
958
 * If set to %TRUE, then the incoming stream will be allowed to have non-constant
Edward Hervey's avatar
Edward Hervey committed
959 960 961 962 963 964 965 966
 * framerate. If set to %FALSE (default value), then the incoming stream will
 * be normalized by dropping/duplicating frames in order to produce a
 * constance framerate.
 */
void
gst_encoding_video_profile_set_variableframerate (GstEncodingVideoProfile *
    prof, gboolean variableframerate)
{
967 968
  g_return_if_fail (GST_IS_ENCODING_VIDEO_PROFILE (prof));

Edward Hervey's avatar
Edward Hervey committed
969 970 971 972 973 974 975 976 977 978
  prof->variableframerate = variableframerate;
}

/* Audio profiles */

struct _GstEncodingAudioProfile
{
  GstEncodingProfile parent;
};

979 980 981 982 983
struct _GstEncodingAudioProfileClass
{
  GstEncodingProfileClass parent;
};

Edward Hervey's avatar
Edward Hervey committed
984 985 986 987 988 989 990 991 992 993
G_DEFINE_TYPE (GstEncodingAudioProfile, gst_encoding_audio_profile,
    GST_TYPE_ENCODING_PROFILE);

static void
gst_encoding_audio_profile_init (GstEncodingAudioProfile * prof)
{
  /* Nothing to initialize */
}

static void
994
gst_encoding_audio_profile_class_init (GstEncodingAudioProfileClass * klass)
Edward Hervey's avatar
Edward Hervey committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
{
}

static inline gboolean
_gst_caps_is_equal_safe (GstCaps * a, GstCaps * b)
{
  if (a == b)
    return TRUE;
  if ((a == NULL) || (b == NULL))
    return FALSE;
  return gst_caps_is_equal (a, b);
}

static gint
_compare_container_encoding_profiles (GstEncodingContainerProfile * ca,
    GstEncodingContainerProfile * cb)
{
  GList *tmp;

  if (g_list_length (ca->encodingprofiles) !=
      g_list_length (cb->encodingprofiles))
    return -1;

  for (tmp = ca->encodingprofiles; tmp; tmp = tmp->next) {
    GstEncodingProfile *prof = (GstEncodingProfile *) tmp->data;
    if (!gst_encoding_container_profile_contains_profile (ca, prof))
      return -1;
  }

  return 0;
}

static gint
_compare_encoding_profiles (const GstEncodingProfile * a,
    const GstEncodingProfile * b)
{
  if ((G_TYPE_FROM_INSTANCE (a) != G_TYPE_FROM_INSTANCE (b)) ||
      !_gst_caps_is_equal_safe (a->format, b->format) ||
      (g_strcmp0 (a->preset, b->preset) != 0) ||
1034
      (g_strcmp0 (a->preset_name, b->preset_name) != 0) ||
Edward Hervey's avatar
Edward Hervey committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
      (g_strcmp0 (a->name, b->name) != 0) ||
      (g_strcmp0 (a->description, b->description) != 0))
    return -1;

  if (GST_IS_ENCODING_CONTAINER_PROFILE (a))
    return
        _compare_container_encoding_profiles (GST_ENCODING_CONTAINER_PROFILE
        (a), GST_ENCODING_CONTAINER_PROFILE (b));

  if (GST_IS_ENCODING_VIDEO_PROFILE (a)) {
    GstEncodingVideoProfile *va = (GstEncodingVideoProfile *) a;
    GstEncodingVideoProfile *vb = (GstEncodingVideoProfile *) b;

    if ((va->pass != vb->pass)
        || (va->variableframerate != vb->variableframerate))
      return -1;
  }

  return 0;
}

/**
 * gst_encoding_container_profile_contains_profile:
 * @container: a #GstEncodingContainerProfile
 * @profile: a #GstEncodingProfile
 *
 * Checks if @container contains a #GstEncodingProfile identical to
 * @profile.
 *
 * Returns: %TRUE if @container contains a #GstEncodingProfile identical
 * to @profile, else %FALSE.
 */
gboolean
gst_encoding_container_profile_contains_profile (GstEncodingContainerProfile *
    container, GstEncodingProfile * profile)
{
  g_return_val_if_fail (GST_IS_ENCODING_CONTAINER_PROFILE (container), FALSE);
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);

  return (g_list_find_custom (container->encodingprofiles, profile,
          (GCompareFunc) _compare_encoding_profiles) != NULL);
}

/**
 * gst_encoding_container_profile_add_profile:
 * @container: the #GstEncodingContainerProfile to use
1081
 * @profile: (transfer full): the #GstEncodingProfile to add.
Edward Hervey's avatar
Edward Hervey committed
1082 1083
 *
 * Add a #GstEncodingProfile to the list of profiles handled by @container.
1084
 *
Edward Hervey's avatar
Edward Hervey committed
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
 * No copy of @profile will be made, if you wish to use it elsewhere after this
 * method you should increment its reference count.
 *
 * Returns: %TRUE if the @stream was properly added, else %FALSE.
 */
gboolean
gst_encoding_container_profile_add_profile (GstEncodingContainerProfile *
    container, GstEncodingProfile * profile)
{
  g_return_val_if_fail (GST_IS_ENCODING_CONTAINER_PROFILE (container), FALSE);
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);

  if (g_list_find_custom (container->encodingprofiles, profile,
          (GCompareFunc) _compare_encoding_profiles)) {
    GST_ERROR
        ("Encoding profile already contains an identical GstEncodingProfile");
    return FALSE;
  }

  container->encodingprofiles =
      g_list_append (container->encodingprofiles, profile);

  return TRUE;
}

static GstEncodingProfile *
common_creation (GType objtype, GstCaps * format, const gchar * preset,
    const gchar * name, const gchar * description, GstCaps * restriction,
    guint presence)
{
  GstEncodingProfile *prof;

1117
  prof = (GstEncodingProfile *) g_object_new (objtype, NULL);
Edward Hervey's avatar
Edward Hervey committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

  if (name)
    prof->name = g_strdup (name);
  if (description)
    prof->description = g_strdup (description);
  if (preset)
    prof->preset = g_strdup (preset);
  if (format)
    prof->format = gst_caps_ref (format);
  if (restriction)
    prof->restriction = gst_caps_ref (restriction);
  prof->presence = presence;
1130
  prof->preset_name = NULL;
1131
  prof->allow_dynamic_output = TRUE;
1132
  prof->enabled = TRUE;
Edward Hervey's avatar
Edward Hervey committed
1133 1134 1135 1136 1137 1138

  return prof;
}

/**
 * gst_encoding_container_profile_new:
1139 1140 1141
 * @name: (allow-none): The name of the container profile, can be %NULL
 * @description: (allow-none): The description of the container profile,
 *     can be %NULL
1142
 * @format: (transfer none): The format to use for this profile
1143
 * @preset: (allow-none): The preset to use for this profile.
Edward Hervey's avatar
Edward Hervey committed
1144 1145 1146 1147 1148 1149 1150 1151 1152
 *
 * Creates a new #GstEncodingContainerProfile.
 *
 * Returns: The newly created #GstEncodingContainerProfile.
 */
GstEncodingContainerProfile *
gst_encoding_container_profile_new (const gchar * name,
    const gchar * description, GstCaps * format, const gchar * preset)
{
1153 1154
  g_return_val_if_fail (GST_IS_CAPS (format), NULL);

Edward Hervey's avatar
Edward Hervey committed
1155 1156 1157 1158 1159 1160 1161
  return (GstEncodingContainerProfile *)
      common_creation (GST_TYPE_ENCODING_CONTAINER_PROFILE, format, preset,
      name, description, NULL, 0);
}

/**
 * gst_encoding_video_profile_new:
1162
 * @format: (transfer none): the #GstCaps
1163
 * @preset: (allow-none): the preset(s) to use on the encoder, can be %NULL
1164
 * @restriction: (allow-none): the #GstCaps used to restrict the input to the encoder, can be
1165
 * NULL. See gst_encoding_profile_get_restriction() for more details.
Edward Hervey's avatar
Edward Hervey committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
 * @presence: the number of time this stream must be used. 0 means any number of
 *  times (including never)
 *
 * Creates a new #GstEncodingVideoProfile
 *
 * All provided allocatable arguments will be internally copied, so can be
 * safely freed/unreferenced after calling this method.
 *
 * If you wish to control the pass number (in case of multi-pass scenarios),
 * please refer to the gst_encoding_video_profile_set_pass() documentation.
 *
 * If you wish to use/force a constant framerate please refer to the
 * gst_encoding_video_profile_set_variableframerate() documentation.
 *
 * Returns: the newly created #GstEncodingVideoProfile.
 */
GstEncodingVideoProfile *
gst_encoding_video_profile_new (GstCaps * format, const gchar * preset,
    GstCaps * restriction, guint presence)
{
  return (GstEncodingVideoProfile *)
      common_creation (GST_TYPE_ENCODING_VIDEO_PROFILE, format, preset, NULL,
      NULL, restriction, presence);
}

/**
 * gst_encoding_audio_profile_new:
1193
 * @format: (transfer none): the #GstCaps
1194
 * @preset: (allow-none): the preset(s) to use on the encoder, can be %NULL
1195
 * @restriction: (allow-none): the #GstCaps used to restrict the input to the encoder, can be
1196
 * NULL. See gst_encoding_profile_get_restriction() for more details.
Edward Hervey's avatar
Edward Hervey committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
 * @presence: the number of time this stream must be used. 0 means any number of
 *  times (including never)
 *
 * Creates a new #GstEncodingAudioProfile
 *
 * All provided allocatable arguments will be internally copied, so can be
 * safely freed/unreferenced after calling this method.
 *
 * Returns: the newly created #GstEncodingAudioProfile.
 */
GstEncodingAudioProfile *
gst_encoding_audio_profile_new (GstCaps * format, const gchar * preset,
    GstCaps * restriction, guint presence)
{
  return (GstEncodingAudioProfile *)
      common_creation (GST_TYPE_ENCODING_AUDIO_PROFILE, format, preset, NULL,
      NULL, restriction, presence);
}


/**
 * gst_encoding_profile_is_equal:
 * @a: a #GstEncodingProfile
 * @b: a #GstEncodingProfile
 *
 * Checks whether the two #GstEncodingProfile are equal
 *
 * Returns: %TRUE if @a and @b are equal, else %FALSE.
 */
gboolean
gst_encoding_profile_is_equal (GstEncodingProfile * a, GstEncodingProfile * b)
{
1229 1230 1231
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (a), FALSE);
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (b), FALSE);

Edward Hervey's avatar
Edward Hervey committed
1232 1233 1234 1235 1236
  return (_compare_encoding_profiles (a, b) == 0);
}


/**
1237
 * gst_encoding_profile_get_input_caps:
Edward Hervey's avatar
Edward Hervey committed
1238 1239 1240 1241
 * @profile: a #GstEncodingProfile
 *
 * Computes the full output caps that this @profile will be able to consume.
 *
1242 1243
 * Returns: (transfer full): The full caps the given @profile can consume. Call
 * gst_caps_unref() when you are done with the caps.
Edward Hervey's avatar
Edward Hervey committed
1244 1245
 */
GstCaps *
1246
gst_encoding_profile_get_input_caps (GstEncodingProfile * profile)
Edward Hervey's avatar
Edward Hervey committed
1247 1248 1249 1250 1251 1252
{
  GstCaps *out, *tmp;
  GList *ltmp;
  GstStructure *st, *outst;
  GQuark out_name;
  guint i, len;
1253
  GstCaps *fcaps;
Edward Hervey's avatar
Edward Hervey committed
1254

1255 1256
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);

Edward Hervey's avatar
Edward Hervey committed
1257 1258 1259 1260 1261 1262
  if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
    GstCaps *res = gst_caps_new_empty ();

    for (ltmp = GST_ENCODING_CONTAINER_PROFILE (profile)->encodingprofiles;
        ltmp; ltmp = ltmp->next) {
      GstEncodingProfile *sprof = (GstEncodingProfile *) ltmp->data;
Wim Taymans's avatar
Wim Taymans committed
1263
      res = gst_caps_merge (res, gst_encoding_profile_get_input_caps (sprof));
Edward Hervey's avatar
Edward Hervey committed
1264 1265 1266 1267 1268 1269 1270 1271
    }
    return res;
  }

  fcaps = profile->format;

  /* fast-path */
  if ((profile->restriction == NULL) || gst_caps_is_any (profile->restriction))
1272
    return gst_caps_ref (fcaps);
Edward Hervey's avatar
Edward Hervey committed
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308

  /* Combine the format with the restriction caps */
  outst = gst_caps_get_structure (fcaps, 0);
  out_name = gst_structure_get_name_id (outst);
  tmp = gst_caps_new_empty ();
  len = gst_caps_get_size (profile->restriction);

  for (i = 0; i < len; i++) {
    st = gst_structure_copy (gst_caps_get_structure (profile->restriction, i));
    st->name = out_name;
    gst_caps_append_structure (tmp, st);
  }

  out = gst_caps_intersect (tmp, fcaps);
  gst_caps_unref (tmp);

  return out;
}

/**
 * gst_encoding_profile_get_type_nick:
 * @profile: a #GstEncodingProfile
 *
 * Returns: the human-readable name of the type of @profile.
 */
const gchar *
gst_encoding_profile_get_type_nick (GstEncodingProfile * profile)
{
  if (GST_IS_ENCODING_CONTAINER_PROFILE (profile))
    return "container";
  if (GST_IS_ENCODING_VIDEO_PROFILE (profile))
    return "video";
  if (GST_IS_ENCODING_AUDIO_PROFILE (profile))
    return "audio";
  return NULL;
}
1309

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
extern const gchar *pb_utils_get_file_extension_from_caps (const GstCaps *
    caps);
gboolean pb_utils_is_tag (const GstCaps * caps);

static gboolean
gst_encoding_profile_has_format (GstEncodingProfile * profile,
    const gchar * media_type)
{
  GstCaps *caps;
  gboolean ret;

1321 1322
  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);

1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
  caps = gst_encoding_profile_get_format (profile);
  ret = gst_structure_has_name (gst_caps_get_structure (caps, 0), media_type);
  gst_caps_unref (caps);

  return ret;
}

static gboolean
gst_encoding_container_profile_has_video (GstEncodingContainerProfile * profile)
{
  const GList *l;

1335 1336
  g_return_val_if_fail (GST_IS_ENCODING_CONTAINER_PROFILE (profile), FALSE);

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
  for (l = profile->encodingprofiles; l != NULL; l = l->next) {
    if (GST_IS_ENCODING_VIDEO_PROFILE (l->data))
      return TRUE;
    if (GST_IS_ENCODING_CONTAINER_PROFILE (l->data) &&
        gst_encoding_container_profile_has_video (l->data))
      return TRUE;
  }

  return FALSE;
}

/**
 * gst_encoding_profile_get_file_extension:
 * @profile: a #GstEncodingProfile
 *
 * Returns: a suitable file extension for @profile, or NULL.
 */
const gchar *
gst_encoding_profile_get_file_extension (GstEncodingProfile * profile)
{
  GstEncodingContainerProfile *cprofile;
  const gchar *ext = NULL;
  gboolean has_video;
  GstCaps *caps;
  guint num_children;

  g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), NULL);

  caps = gst_encoding_profile_get_format (profile);
  ext = pb_utils_get_file_extension_from_caps (caps);

  if (!GST_IS_ENCODING_CONTAINER_PROFILE (profile))
    goto done;

  cprofile = GST_ENCODING_CONTAINER_PROFILE (profile);

  num_children = g_list_length (cprofile->encodingprofiles);

  /* if it's a tag container profile (e.g. id3mux/apemux), we need
   * to look at what's inside it */
  if (pb_utils_is_tag (caps)) {
    GST_DEBUG ("tag container profile");
    if (num_children == 1) {
      GstEncodingProfile *child_profile = cprofile->encodingprofiles->data;

      ext = gst_encoding_profile_get_file_extension (child_profile);
    } else {
      GST_WARNING ("expected exactly one child profile with tag profile");
    }
    goto done;
  }

1389 1390 1391
  if (num_children == 0)
    goto done;

1392 1393 1394
  /* special cases */
  has_video = gst_encoding_container_profile_has_video (cprofile);

1395
  /* Ogg */
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
  if (strcmp (ext, "ogg") == 0) {
    /* ogg with video => .ogv */
    if (has_video) {
      ext = "ogv";
      goto done;
    }
    /* ogg with just speex audio => .spx */
    if (num_children == 1) {
      GstEncodingProfile *child_profile = cprofile->encodingprofiles->data;

      if (GST_IS_ENCODING_AUDIO_PROFILE (child_profile) &&
          gst_encoding_profile_has_format (child_profile, "audio/x-speex")) {
        ext = "spx";
        goto done;
      }
    }
    /* does anyone actually use .oga for ogg audio files? */
    goto done;
  }

1416 1417
  /* Matroska */
  if (has_video && strcmp (ext, "mka") == 0) {
1418
    ext = "mkv";
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
    goto done;
  }

  /* Windows Media / ASF */
  if (gst_encoding_profile_has_format (profile, "video/x-ms-asf")) {
    const GList *l;
    guint num_wmv = 0, num_wma = 0, num_other = 0;

    for (l = cprofile->encodingprofiles; l != NULL; l = l->next) {
      if (gst_encoding_profile_has_format (l->data, "video/x-wmv"))
        ++num_wmv;
      else if (gst_encoding_profile_has_format (l->data, "audio/x-wma"))
        ++num_wma;
      else
        ++num_other;
    }

    if (num_other > 0)
      ext = "asf";
    else if (num_wmv > 0)
      ext = "wmv";
    else if (num_wma > 0)
      ext = "wma";

    goto done;
  }
1445 1446 1447 1448 1449 1450 1451 1452

done:

  GST_INFO ("caps %" GST_PTR_FORMAT ", ext: %s", caps, GST_STR_NULL (ext));
  gst_caps_unref (caps);
  return ext;
}

1453 1454 1455
/**
 * gst_encoding_profile_find:
 * @targetname: (transfer none): The name of the target
1456
 * @profilename: (transfer none) (allow-none): The name of the profile, if %NULL
1457
 * provided, it will default to the encoding profile called `default`.
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
 * @category: (transfer none) (allow-none): The target category. Can be %NULL
 *
 * Find the #GstEncodingProfile with the specified name and category.
 *
 * Returns: (transfer full): The matching #GstEncodingProfile or %NULL.
 */
GstEncodingProfile *
gst_encoding_profile_find (const gchar * targetname, const gchar * profilename,
    const gchar * category)
{
  GstEncodingProfile *res = NULL;
  GstEncodingTarget *target;

  g_return_val_if_fail (targetname != NULL, NULL);

  target = gst_encoding_target_load (targetname, category, NULL);
  if (target) {
1475 1476 1477
    res =
        gst_encoding_target_get_profile (target,
        profilename ? profilename : "default");
1478 1479 1480 1481 1482 1483 1484 1485 1486