multichannel.c 25.4 KB
Newer Older
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* GStreamer Multichannel-Audio helper functions
 * (c) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
19
20
/**
 * SECTION:gstmultichannel
21
 * @short_description: Support for multichannel audio elements
22
 *
23
24
 * This module contains some helper functions and a enum to work with
 * multichannel audio.
25
 */
Ronald S. Bultje's avatar
Ronald S. Bultje committed
26
27

#ifdef HAVE_CONFIG_H
28
#include "config.h"
Ronald S. Bultje's avatar
Ronald S. Bultje committed
29
30
31
32
#endif

#include "multichannel.h"

33
#define GST_AUDIO_CHANNEL_POSITIONS_FIELD_NAME "channel-positions"
Ronald S. Bultje's avatar
Ronald S. Bultje committed
34

35
36
37
38
39
40
41
42
43
44
45
46
/**
 * gst_audio_check_channel_positions:
 * @pos: An array of #GstAudioChannelPosition.
 * @channels: The number of elements in @pos.
 *
 * This functions checks if the given channel positions are valid. Channel
 * positions are valid if:
 * <itemizedlist>
 *   <listitem><para>No channel positions appears twice or all positions are %GST_AUDIO_CHANNEL_POSITION_NONE.
 *   </para></listitem>
 *   <listitem><para>Either all or none of the channel positions are %GST_AUDIO_CHANNEL_POSITION_NONE.
 *   </para></listitem>
47
 *   <listitem><para>%GST_AUDIO_CHANNEL_POSITION_FRONT_MONO and %GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT or %GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT don't appear together in the given positions.
48
49
50
51
52
53
54
 *   </para></listitem>
 * </itemizedlist>
 *
 * Since: 0.10.20
 *
 * Returns: %TRUE if the given channel positions are valid
 * and %FALSE otherwise.
Ronald S. Bultje's avatar
Ronald S. Bultje committed
55
 */
56
gboolean
Ronald S. Bultje's avatar
Ronald S. Bultje committed
57
gst_audio_check_channel_positions (const GstAudioChannelPosition * pos,
58
    guint channels)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
59
60
{
  gint i, n;
61

62
  const struct
Ronald S. Bultje's avatar
Ronald S. Bultje committed
63
  {
64
65
    const GstAudioChannelPosition pos1[2];
    const GstAudioChannelPosition pos2[1];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
66
67
68
69
70
  } conf[] = {
    /* front: mono <-> stereo */
    { {
    GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
            GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
71
    GST_AUDIO_CHANNEL_POSITION_FRONT_MONO}}, { {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
72
73
74
    GST_AUDIO_CHANNEL_POSITION_INVALID}}
  };

75
76
  g_return_val_if_fail (pos != NULL, FALSE);
  g_return_val_if_fail (channels > 0, FALSE);
77

Ronald S. Bultje's avatar
Ronald S. Bultje committed
78
79
  /* check for invalid channel positions */
  for (n = 0; n < channels; n++) {
80
81
    if (pos[n] <= GST_AUDIO_CHANNEL_POSITION_INVALID ||
        pos[n] >= GST_AUDIO_CHANNEL_POSITION_NUM) {
82
      GST_WARNING ("Channel position %d for channel %d is invalid", pos[n], n);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
83
84
85
86
      return FALSE;
    }
  }

87
88
89
90
91
92
  /* either all channel positions are NONE or all are defined,
   * but having only some channel positions NONE and others not
   * is not allowed */
  if (pos[0] == GST_AUDIO_CHANNEL_POSITION_NONE) {
    for (n = 1; n < channels; ++n) {
      if (pos[n] != GST_AUDIO_CHANNEL_POSITION_NONE) {
93
        GST_WARNING ("Either all channel positions must be defined, or all "
94
95
96
97
98
99
100
101
            "be set to NONE, having only some defined is not allowed");
        return FALSE;
      }
    }
    /* all positions are NONE, we are done here */
    return TRUE;
  }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
102
103
104
105
106
107
108
109
110
111
  /* check for multiple position occurrences */
  for (i = GST_AUDIO_CHANNEL_POSITION_INVALID + 1;
      i < GST_AUDIO_CHANNEL_POSITION_NUM; i++) {
    gint count = 0;

    for (n = 0; n < channels; n++) {
      if (pos[n] == i)
        count++;
    }

112
113
    /* NONE may not occur mixed with other channel positions */
    if (i == GST_AUDIO_CHANNEL_POSITION_NONE && count > 0) {
114
      GST_WARNING ("Either all channel positions must be defined, or all "
115
116
117
118
          "be set to NONE, having only some defined is not allowed");
      return FALSE;
    }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
119
    if (count > 1) {
120
      GST_WARNING ("Channel position %d occurred %d times, not allowed",
Ronald S. Bultje's avatar
Ronald S. Bultje committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
          i, count);
      return FALSE;
    }
  }

  /* check for position conflicts */
  for (i = 0; conf[i].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID; i++) {
    gboolean found1 = FALSE, found2 = FALSE;

    for (n = 0; n < channels; n++) {
      if (pos[n] == conf[i].pos1[0] || pos[n] == conf[i].pos1[1])
        found1 = TRUE;
      else if (pos[n] == conf[i].pos2[0])
        found2 = TRUE;
    }

    if (found1 && found2) {
138
      GST_WARNING ("Found conflicting channel positions %d/%d and %d",
Ronald S. Bultje's avatar
Ronald S. Bultje committed
139
140
141
142
143
144
145
146
          conf[i].pos1[0], conf[i].pos1[1], conf[i].pos2[0]);
      return FALSE;
    }
  }

  return TRUE;
}

147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* FIXME: these default positions may or may not be correct. In any
 * case, they are mostly just a fallback for buggy plugins, so it
 * should not really matter too much */
#define NUM_DEF_CHANS  8
static const GstAudioChannelPosition
    default_positions[NUM_DEF_CHANS][NUM_DEF_CHANS] = {
  /* 1 channel */
  {
        GST_AUDIO_CHANNEL_POSITION_FRONT_MONO,
      },
  /* 2 channels */
  {
        GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
        GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
      },
  /* 3 channels (2.1) */
  {
        GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
        GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
        GST_AUDIO_CHANNEL_POSITION_LFE, /* or FRONT_CENTER for 3.0? */
      },
  /* 4 channels (4.0 or 3.1?) */
  {
        GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
        GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
        GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
        GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
      },
  /* 5 channels */
  {
        GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
        GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
        GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
        GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
        GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
      },
  /* 6 channels */
  {
        GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
        GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
        GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
        GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
        GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
        GST_AUDIO_CHANNEL_POSITION_LFE,
      },
  /* 7 channels */
  {
        GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
        GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
        GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
        GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
        GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
        GST_AUDIO_CHANNEL_POSITION_LFE,
        GST_AUDIO_CHANNEL_POSITION_REAR_CENTER,
      },
  /* 8 channels */
  {
        GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
        GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
        GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
        GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
        GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
        GST_AUDIO_CHANNEL_POSITION_LFE,
        GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
        GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
      }
};

Ronald S. Bultje's avatar
Ronald S. Bultje committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
 * gst_audio_get_channel_positions:
 * @str: A #GstStructure to retrieve channel positions from.
 *
 * Retrieves a number of (fixed!) audio channel positions from
 * the provided #GstStructure and returns it as a newly allocated
 * array. The caller should g_free () this array. The caller
 * should also check that the members in this #GstStructure are
 * indeed "fixed" before calling this function.
 *
 * Returns: a newly allocated array containing the channel
 * positions as provided in the given #GstStructure. Returns
 * NULL on error.
 */

GstAudioChannelPosition *
gst_audio_get_channel_positions (GstStructure * str)
{
  GstAudioChannelPosition *pos;
234

Ronald S. Bultje's avatar
Ronald S. Bultje committed
235
  gint channels, n;
236

Ronald S. Bultje's avatar
Ronald S. Bultje committed
237
  const GValue *pos_val_arr, *pos_val_entry;
238

Ronald S. Bultje's avatar
Ronald S. Bultje committed
239
  gboolean res;
240

Ronald S. Bultje's avatar
Ronald S. Bultje committed
241
242
243
244
245
246
247
248
  GType t;

  /* get number of channels, general type checkups */
  g_return_val_if_fail (str != NULL, NULL);
  res = gst_structure_get_int (str, "channels", &channels);
  g_return_val_if_fail (res, NULL);
  g_return_val_if_fail (channels > 0, NULL);
  pos_val_arr = gst_structure_get_value (str,
249
      GST_AUDIO_CHANNEL_POSITIONS_FIELD_NAME);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
250
251

  /* The following checks are here to retain compatibility for plugins not
252
   * implementing this field. They expect that channels=1 implies mono
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
   * and channels=2 implies stereo, so we follow that. */
  if (pos_val_arr == NULL) {
    /* channel layouts for 1 and 2 channels are implicit, don't warn */
    if (channels > 2) {
      g_warning ("Failed to retrieve channel layout from caps. This usually "
          "means there is a GStreamer element that does not implement "
          "multichannel audio correctly. Please file a bug.");
    }

    /* just return some default channel layout if we have one */
    if (channels >= 1 && channels <= NUM_DEF_CHANS) {
      const GstAudioChannelPosition *p;

      p = default_positions[channels - 1];
      return g_memdup (p, channels * sizeof (GstAudioChannelPosition));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
268
    }
269
270

    return NULL;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
271
  }
272

273
  g_return_val_if_fail (gst_value_array_get_size (pos_val_arr) == channels,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
274
275
      NULL);
  for (n = 0; n < channels; n++) {
276
    t = G_VALUE_TYPE (gst_value_array_get_value (pos_val_arr, n));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
277
278
279
280
281
282
    g_return_val_if_fail (t == GST_TYPE_AUDIO_CHANNEL_POSITION, NULL);
  }

  /* ... and fill array */
  pos = g_new (GstAudioChannelPosition, channels);
  for (n = 0; n < channels; n++) {
283
    pos_val_entry = gst_value_array_get_value (pos_val_arr, n);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
284
285
286
287
288
289
290
291
292
293
294
295
296
    pos[n] = g_value_get_enum (pos_val_entry);
  }

  if (!gst_audio_check_channel_positions (pos, channels)) {
    g_free (pos);
    return NULL;
  }

  return pos;
}

/**
 * gst_audio_set_channel_positions:
297
 * @str: A #GstStructure to set channel positions on.
Ronald S. Bultje's avatar
Ronald S. Bultje committed
298
299
 * @pos: an array of channel positions. The number of members
 *       in this array should be equal to the (fixed!) number
300
 *       of the "channels" field in the given #GstStructure.
Ronald S. Bultje's avatar
Ronald S. Bultje committed
301
 *
302
 * Adds a "channel-positions" field to the given #GstStructure,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
303
304
305
306
307
308
309
310
311
312
313
 * which will represent the channel positions as given in the
 * provided #GstAudioChannelPosition array.
 */

void
gst_audio_set_channel_positions (GstStructure * str,
    const GstAudioChannelPosition * pos)
{
  GValue pos_val_arr = { 0 }, pos_val_entry = {
  0};
  gint channels, n;
314

Ronald S. Bultje's avatar
Ronald S. Bultje committed
315
316
317
318
319
320
321
322
323
324
325
326
327
  gboolean res;

  /* get number of channels, checkups */
  g_return_if_fail (str != NULL);
  g_return_if_fail (pos != NULL);
  res = gst_structure_get_int (str, "channels", &channels);
  g_return_if_fail (res);
  g_return_if_fail (channels > 0);
  if (!gst_audio_check_channel_positions (pos, channels))
    return;

  /* build gvaluearray from positions */
  g_value_init (&pos_val_entry, GST_TYPE_AUDIO_CHANNEL_POSITION);
328
  g_value_init (&pos_val_arr, GST_TYPE_ARRAY);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
329
330
  for (n = 0; n < channels; n++) {
    g_value_set_enum (&pos_val_entry, pos[n]);
331
    gst_value_array_append_value (&pos_val_arr, &pos_val_entry);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
332
333
334
335
336
  }
  g_value_unset (&pos_val_entry);

  /* add to structure */
  gst_structure_set_value (str,
337
      GST_AUDIO_CHANNEL_POSITIONS_FIELD_NAME, &pos_val_arr);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
  g_value_unset (&pos_val_arr);
}

/**
 * gst_audio_set_structure_channel_positions_list:
 * @str: #GstStructure to set the list of channel positions
 *       on.
 * @pos: the array containing one or more possible audio
 *       channel positions that we should add in each value
 *       of the array in the given structure.
 * @num_positions: the number of values in pos.
 *
 * Sets a (possibly non-fixed) list of possible audio channel
 * positions (given in pos) on the given structure. The
 * structure, after this function has been called, will contain
353
354
355
 * a "channel-positions" field with an array of the size of
 * the "channels" field value in the given structure (note
 * that this means that the channels field in the provided
Ronald S. Bultje's avatar
Ronald S. Bultje committed
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
 * structure should be fixed!). Each value in the array will
 * contain each of the values given in the pos array.
 */

void
gst_audio_set_structure_channel_positions_list (GstStructure * str,
    const GstAudioChannelPosition * pos, gint num_positions)
{
  gint channels, n, c;
  GValue pos_val_arr = { 0 }, pos_val_list = {
  0}, pos_val_entry = {
  0};
  gboolean res;

  /* get number of channels, general type checkups */
  g_return_if_fail (str != NULL);
  g_return_if_fail (num_positions > 0);
  g_return_if_fail (pos != NULL);
  res = gst_structure_get_int (str, "channels", &channels);
  g_return_if_fail (res);
  g_return_if_fail (channels > 0);

  /* create the array of lists */
379
  g_value_init (&pos_val_arr, GST_TYPE_ARRAY);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
380
381
382
383
384
385
386
  g_value_init (&pos_val_entry, GST_TYPE_AUDIO_CHANNEL_POSITION);
  for (n = 0; n < channels; n++) {
    g_value_init (&pos_val_list, GST_TYPE_LIST);
    for (c = 0; c < num_positions; c++) {
      g_value_set_enum (&pos_val_entry, pos[c]);
      gst_value_list_append_value (&pos_val_list, &pos_val_entry);
    }
387
    gst_value_array_append_value (&pos_val_arr, &pos_val_list);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
388
389
390
    g_value_unset (&pos_val_list);
  }
  g_value_unset (&pos_val_entry);
391
  gst_structure_set_value (str, GST_AUDIO_CHANNEL_POSITIONS_FIELD_NAME,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
      &pos_val_arr);
  g_value_unset (&pos_val_arr);
}

/*
 * Helper function for below. The structure will be conserved,
 * but might be cut down. Any additional structures that were
 * created will be stored in the returned caps.
 */

static GstCaps *
add_list_to_struct (GstStructure * str,
    const GstAudioChannelPosition * pos, gint num_positions)
{
  GstCaps *caps = gst_caps_new_empty ();
407

Ronald S. Bultje's avatar
Ronald S. Bultje committed
408
409
410
411
412
413
414
  const GValue *chan_val;

  chan_val = gst_structure_get_value (str, "channels");
  if (G_VALUE_TYPE (chan_val) == G_TYPE_INT) {
    gst_audio_set_structure_channel_positions_list (str, pos, num_positions);
  } else if (G_VALUE_TYPE (chan_val) == GST_TYPE_LIST) {
    gint size;
415

Ronald S. Bultje's avatar
Ronald S. Bultje 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
    const GValue *sub_val;

    size = gst_value_list_get_size (chan_val);
    sub_val = gst_value_list_get_value (chan_val, 0);
    gst_structure_set_value (str, "channels", sub_val);
    gst_caps_append (caps, add_list_to_struct (str, pos, num_positions));
    while (--size > 0) {
      str = gst_structure_copy (str);
      sub_val = gst_value_list_get_value (chan_val, size);
      gst_structure_set_value (str, "channels", sub_val);
      gst_caps_append (caps, add_list_to_struct (str, pos, num_positions));
      gst_caps_append_structure (caps, str);
    }
  } else if (G_VALUE_TYPE (chan_val) == GST_TYPE_INT_RANGE) {
    gint min, max;

    min = gst_value_get_int_range_min (chan_val);
    max = gst_value_get_int_range_max (chan_val);

    gst_structure_set (str, "channels", G_TYPE_INT, min, NULL);
    gst_audio_set_structure_channel_positions_list (str, pos, num_positions);
    for (++min; min < max; min++) {
      str = gst_structure_copy (str);
      gst_structure_set (str, "channels", G_TYPE_INT, min, NULL);
      gst_audio_set_structure_channel_positions_list (str, pos, num_positions);
      gst_caps_append_structure (caps, str);
    }
  } else {
444
    g_warning ("Unexpected value type '%s' for channels field",
445
        GST_STR_NULL (g_type_name (G_VALUE_TYPE (chan_val))));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
  }

  return caps;
}

/**
 * gst_audio_set_caps_channel_positions_list:
 * @caps: #GstCaps to set the list of channel positions on.
 * @pos: the array containing one or more possible audio
 *       channel positions that we should add in each value
 *       of the array in the given structure.
 * @num_positions: the number of values in pos.
 *
 * Sets a (possibly non-fixed) list of possible audio channel
 * positions (given in pos) on the given caps. Each of the
 * structures of the caps, after this function has been called,
462
 * will contain a "channel-positions" field with an array.
Ronald S. Bultje's avatar
Ronald S. Bultje committed
463
464
465
 * Each value in the array will contain each of the values given
 * in the pos array. Note that the size of the caps might be
 * increased by this, since each structure with a "channel-
466
 * positions" field needs to have a fixed "channels" field.
Ronald S. Bultje's avatar
Ronald S. Bultje committed
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
 * The input caps is not required to have this.
 */

void
gst_audio_set_caps_channel_positions_list (GstCaps * caps,
    const GstAudioChannelPosition * pos, gint num_positions)
{
  gint size, n;

  /* get number of channels, general type checkups */
  g_return_if_fail (caps != NULL);
  g_return_if_fail (num_positions > 0);
  g_return_if_fail (pos != NULL);

  size = gst_caps_get_size (caps);
  for (n = 0; n < size; n++) {
    gst_caps_append (caps, add_list_to_struct (gst_caps_get_structure (caps,
                n), pos, num_positions));
  }
}

/**
 * gst_audio_fixate_channel_positions:
 * @str: a #GstStructure containing a (possibly unfixed)
491
 *       "channel-positions" field.
Ronald S. Bultje's avatar
Ronald S. Bultje committed
492
493
 *
 * Custom fixate function. Elements that implement some sort of
494
 * channel conversion algorithm should use this function for
Ronald S. Bultje's avatar
Ronald S. Bultje committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
 * fixating on GstAudioChannelPosition properties. It will take
 * care of equal channel positioning (left/right). Caller g_free()s
 * the return value. The input properties may be (and are supposed
 * to be) unfixed.
 * Note that this function is mostly a hack because we currently
 * have no way to add default fixation functions for new GTypes.
 *
 * Returns: fixed values that the caller could use as a fixed
 * set of #GstAudioChannelPosition values.
 */

GstAudioChannelPosition *
gst_audio_fixate_channel_positions (GstStructure * str)
{
  GstAudioChannelPosition *pos;
510

Ronald S. Bultje's avatar
Ronald S. Bultje committed
511
  gint channels, n, num_unfixed = 0, i, c;
512

Ronald S. Bultje's avatar
Ronald S. Bultje committed
513
  const GValue *pos_val_arr, *pos_val_entry, *pos_val;
514

Ronald S. Bultje's avatar
Ronald S. Bultje committed
515
  gboolean res, is_stereo = TRUE;
516

Ronald S. Bultje's avatar
Ronald S. Bultje committed
517
518
519
520
521
522
523
524
  GType t;

  /*
   * We're going to do this cluelessly. We'll make an array of values that
   * conflict with each other and, for each iteration in this array, pick
   * either one until all unknown values are filled. This might not work in
   * corner cases but should work OK for the general case.
   */
525
  const struct
Ronald S. Bultje's avatar
Ronald S. Bultje committed
526
  {
527
528
    const GstAudioChannelPosition pos1[2];
    const GstAudioChannelPosition pos2[1];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
529
530
  } conf[] = {
    /* front: mono <-> stereo */
531
532
533
    {
      {
      GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
534
            GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
535
    GST_AUDIO_CHANNEL_POSITION_FRONT_MONO}}, { {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
536
537
    GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
            GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, {
538
539
540
    GST_AUDIO_CHANNEL_POSITION_INVALID}}, { {
    GST_AUDIO_CHANNEL_POSITION_INVALID, GST_AUDIO_CHANNEL_POSITION_INVALID}, {
    GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}}, { {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
541
542
    GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
            GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
543
544
    GST_AUDIO_CHANNEL_POSITION_INVALID}}, { {
    GST_AUDIO_CHANNEL_POSITION_INVALID, GST_AUDIO_CHANNEL_POSITION_INVALID}, {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
    GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}}, { {
    GST_AUDIO_CHANNEL_POSITION_INVALID, GST_AUDIO_CHANNEL_POSITION_INVALID}, {
    GST_AUDIO_CHANNEL_POSITION_LFE}}, { {
    GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
            GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT}, {
    GST_AUDIO_CHANNEL_POSITION_INVALID}}, { {
    GST_AUDIO_CHANNEL_POSITION_INVALID, GST_AUDIO_CHANNEL_POSITION_INVALID}, {
    GST_AUDIO_CHANNEL_POSITION_INVALID}}
  };
  struct
  {
    gint num_opt[3];
    guint num_opts[3];
    gboolean is_fixed[3];
    gint choice;                /* -1 is none, 0 is the two, 1 is the one */
  } opt;

  /* get number of channels, general type checkups */
  g_return_val_if_fail (str != NULL, NULL);
  res = gst_structure_get_int (str, "channels", &channels);
  g_return_val_if_fail (res, NULL);
  g_return_val_if_fail (channels > 0, NULL);

  /* 0.8.x mono/stereo checks */
  pos_val_arr = gst_structure_get_value (str,
570
      GST_AUDIO_CHANNEL_POSITIONS_FIELD_NAME);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
571
572
573
574
575
576
577
578
579
580
581
  if (!pos_val_arr && (channels == 1 || channels == 2)) {
    pos = g_new (GstAudioChannelPosition, channels);
    if (channels == 1) {
      pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO;
    } else {
      pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
      pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
    }
    return pos;
  }
  g_return_val_if_fail (pos_val_arr != NULL, NULL);
582
  g_return_val_if_fail (gst_value_array_get_size (pos_val_arr) == channels,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
583
584
      NULL);
  for (n = 0; n < channels; n++) {
585
    t = G_VALUE_TYPE (gst_value_array_get_value (pos_val_arr, n));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
    g_return_val_if_fail (t == GST_TYPE_LIST ||
        t == GST_TYPE_AUDIO_CHANNEL_POSITION, NULL);
  }

  /* all unknown, to start with */
  pos = g_new (GstAudioChannelPosition, channels);
  for (n = 0; n < channels; n++)
    pos[n] = GST_AUDIO_CHANNEL_POSITION_INVALID;
  num_unfixed = channels;

  /* Iterate the array of conflicting values */
  for (i = 0; conf[i].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID ||
      conf[i].pos2[0] != GST_AUDIO_CHANNEL_POSITION_INVALID; i++) {
    /* front/center only important if not mono (obviously) */
    if (conf[i].pos1[0] == GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER &&
        !is_stereo)
      continue;

    /* init values */
    for (n = 0; n < 3; n++) {
      opt.num_opt[n] = -1;
      opt.num_opts[n] = -1;
      opt.is_fixed[n] = FALSE;
    }

    /* Now, we'll see for each channel if it allows for any of the values in
     * the set of conflicting audio channel positions and keep scores. */
    for (n = 0; n < channels; n++) {
      /* if the channel is already taken, don't bother */
      if (pos[n] != GST_AUDIO_CHANNEL_POSITION_INVALID)
        continue;

618
      pos_val_entry = gst_value_array_get_value (pos_val_arr, n);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
619
620
621
622
623
624
625
626
627
628
629
630
631
632
      t = G_VALUE_TYPE (pos_val_entry);
      if (t == GST_TYPE_LIST) {
        /* This algorhythm is suboptimal. */
        for (c = 0; c < gst_value_list_get_size (pos_val_entry); c++) {
          pos_val = gst_value_list_get_value (pos_val_entry, c);
          if (g_value_get_enum (pos_val) == conf[i].pos1[0] &&
              opt.num_opts[0] > gst_value_list_get_size (pos_val_entry) &&
              !opt.is_fixed[0]) {
            /* Now test if the old position of num_opt[0] also allows for
             * the other channel (which was skipped previously). If so,
             * keep score. */
            if (opt.num_opt[0] != -1) {
              gint c1;

633
              pos_val_entry = gst_value_array_get_value (pos_val_arr,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
634
635
636
637
638
639
640
641
642
643
644
645
                  opt.num_opt[0]);
              if (G_VALUE_TYPE (pos_val_entry) == GST_TYPE_LIST) {
                for (c1 = 0; c1 < gst_value_list_get_size (pos_val_entry); c1++) {
                  pos_val = gst_value_list_get_value (pos_val_entry, c1);
                  if (g_value_get_enum (pos_val) == conf[i].pos1[1] &&
                      opt.num_opts[1] > opt.num_opts[0] && !opt.is_fixed[1]) {
                    opt.num_opts[1] = opt.num_opts[0];
                    opt.num_opt[1] = opt.num_opt[0];
                  }
                }
                pos_val = gst_value_list_get_value (pos_val_entry, c);
              }
646
              pos_val_entry = gst_value_array_get_value (pos_val_arr, n);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
            }

            /* and save values */
            opt.num_opts[0] = gst_value_list_get_size (pos_val_entry);
            opt.num_opt[0] = n;
          } else if (g_value_get_enum (pos_val) == conf[i].pos1[1] &&
              opt.num_opts[1] > gst_value_list_get_size (pos_val_entry) &&
              !opt.is_fixed[1] && n != opt.num_opt[0]) {
            opt.num_opts[1] = gst_value_list_get_size (pos_val_entry);
            opt.num_opt[1] = n;
          }

          /* 2 goes separately, because 0/1 vs. 2 are separate */
          if (g_value_get_enum (pos_val) == conf[i].pos2[0] &&
              opt.num_opts[2] > gst_value_list_get_size (pos_val_entry) &&
              !opt.is_fixed[2]) {
            opt.num_opts[2] = gst_value_list_get_size (pos_val_entry);
            opt.num_opt[2] = n;
          }
        }
      } else {
        if (g_value_get_enum (pos_val_entry) == conf[i].pos1[0]) {
          opt.num_opt[0] = n;
          opt.is_fixed[0] = TRUE;
        } else if (g_value_get_enum (pos_val_entry) == conf[i].pos1[1]) {
          opt.num_opt[1] = n;
          opt.is_fixed[1] = TRUE;
        } else if (g_value_get_enum (pos_val_entry) == conf[i].pos2[0]) {
          opt.num_opt[2] = n;
          opt.is_fixed[2] = TRUE;
        }
      }
    }

    /* check our results and choose either one */
    if ((opt.is_fixed[0] || opt.is_fixed[1]) && opt.is_fixed[2]) {
      g_warning ("Pre-fixated on both %d/%d and %d - conflict!",
          conf[i].pos1[0], conf[i].pos1[1], conf[i].pos2[0]);
      g_free (pos);
      return NULL;
    } else if ((opt.is_fixed[0] && opt.num_opt[1] == -1) ||
        (opt.is_fixed[1] && opt.num_opt[0] == -1)) {
      g_warning ("Pre-fixated one side, but other side n/a of %d/%d",
          conf[i].pos1[0], conf[i].pos1[1]);
      g_free (pos);
      return NULL;
    } else if (opt.is_fixed[0] || opt.is_fixed[1]) {
      opt.choice = 0;
    } else if (opt.is_fixed[2]) {
      opt.choice = 1;
    } else if (opt.num_opt[0] != -1 && opt.num_opt[1] != -1) {
      opt.choice = 0;
    } else if (opt.num_opt[2] != -1) {
      opt.choice = 1;
    } else {
      opt.choice = -1;
    }

    /* stereo? Note that we keep is_stereo to TRUE if we didn't decide on
     * any arrangement. The mono/stereo channels might be handled elsewhere
     * which is clearly outside the scope of this element, so we cannot
     * know and expect the application to handle that then. */
    if (conf[i].pos2[0] == GST_AUDIO_CHANNEL_POSITION_FRONT_MONO &&
        opt.choice == 1) {
      is_stereo = FALSE;
    }

    /* now actually decide what we'll do and fixate on that */
    if (opt.choice == 0) {
      g_assert (conf[i].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID &&
          conf[i].pos1[1] != GST_AUDIO_CHANNEL_POSITION_INVALID);
      pos[opt.num_opt[0]] = conf[i].pos1[0];
      pos[opt.num_opt[1]] = conf[i].pos1[1];
      num_unfixed -= 2;
    } else if (opt.choice == 1) {
      g_assert (conf[i].pos2[0] != GST_AUDIO_CHANNEL_POSITION_INVALID);
      pos[opt.num_opt[2]] = conf[i].pos2[0];
      num_unfixed--;
    }
  }

  /* safety check */
  if (num_unfixed > 0) {
    g_warning ("%d unfixed channel positions left after fixation!",
        num_unfixed);
    g_free (pos);
    return NULL;
  }

  if (!gst_audio_check_channel_positions (pos, channels)) {
    g_free (pos);
    return NULL;
  }

  return pos;
}