gstaudioconvert.c 22 KB
Newer Older
1
2
/* GStreamer
 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3
 * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
4
 * Copyright (C) 2005 Wim Taymans <wim at fluendo dot com>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * gstaudioconvert.c: Convert audio to different audio formats automatically
 *
 * 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.
 */
23
24
25
26
27
28

/**
 * SECTION:element-audioconvert
 *
 * <refsect2>
 * Audioconvert converts raw audio buffers between various possible formats.
29
 * It supports integer to float conversion, width/depth conversion,
30
31
32
33
 * signedness and endianness conversion.
 * <title>Example launch line</title>
 * <para>
 * <programlisting>
34
 * gst-launch -v -m audiotestsrc ! audioconvert ! audio/x-raw-int,channels=2,width=8,depth=8 ! level ! fakesink silent=TRUE
35
36
37
38
39
40
 * </programlisting>
 * This pipeline converts audio to 8-bit.  The level element shows that
 * the output levels still match the one for a sine wave.
 * </para>
 * <para>
 * <programlisting>
41
 * gst-launch -v -m audiotestsrc ! audioconvert ! vorbisenc ! fakesink silent=TRUE
42
43
 * </programlisting>
 * The vorbis encoder takes float audio data instead of the integer data
44
 * generated by audiotestsrc.
45
46
 * </para>
 * </refsect2>
47
48
 *
 * Last reviewed on 2006-03-02 (0.10.4)
49
 */
50

51
52
/*
 * design decisions:
53
54
 * - audioconvert converts buffers in a set of supported caps. If it supports
 *   a caps, it supports conversion from these caps to any other caps it
55
56
57
58
59
60
61
62
 *   supports. (example: if it does A=>B and A=>C, it also does B=>C)
 * - audioconvert does not save state between buffers. Every incoming buffer is
 *   converted and the converted buffer is pushed out.
 * conclusion:
 * audioconvert is not supposed to be a one-element-does-anything solution for
 * audio conversions.
 */

63
#ifdef HAVE_CONFIG_H
Thomas Vander Stichele's avatar
PTR fix    
Thomas Vander Stichele committed
64
#include "config.h"
65
#endif
66

67
#include <string.h>
68
69

#include "gstaudioconvert.h"
70
#include "gstchannelmix.h"
71
#include "plugin.h"
72

73
GST_DEBUG_CATEGORY (audio_convert_debug);
74

75
76
/*** DEFINITIONS **************************************************************/

Stefan Kost's avatar
Stefan Kost committed
77
static const GstElementDetails audio_convert_details =
j^'s avatar
j^ committed
78
GST_ELEMENT_DETAILS ("Audio converter",
79
80
81
    "Filter/Converter/Audio",
    "Convert audio to different formats",
    "Benjamin Otte <in7y118@public.uni-hamburg.de>");
Iain Holmes's avatar
Iain Holmes committed
82

83
/* type functions */
Ronald S. Bultje's avatar
Ronald S. Bultje committed
84
static void gst_audio_convert_dispose (GObject * obj);
85
86

/* gstreamer functions */
87
88
89
static gboolean gst_audio_convert_get_unit_size (GstBaseTransform * base,
    GstCaps * caps, guint * size);
static GstCaps *gst_audio_convert_transform_caps (GstBaseTransform * base,
90
    GstPadDirection direction, GstCaps * caps);
91
static void gst_audio_convert_fixate_caps (GstBaseTransform * base,
92
    GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
93
94
95
96
97
98
static gboolean gst_audio_convert_set_caps (GstBaseTransform * base,
    GstCaps * incaps, GstCaps * outcaps);
static GstFlowReturn gst_audio_convert_transform (GstBaseTransform * base,
    GstBuffer * inbuf, GstBuffer * outbuf);
static GstFlowReturn gst_audio_convert_transform_ip (GstBaseTransform * base,
    GstBuffer * buf);
99

100
/* AudioConvert signals and args */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
101
102
enum
{
103
104
105
  /* FILL ME */
  LAST_SIGNAL
};
106

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
107
108
enum
{
109
  ARG_0,
110
  ARG_AGGRESSIVE
111
112
};

113
114
#define DEBUG_INIT(bla) \
  GST_DEBUG_CATEGORY_INIT (audio_convert_debug, "audioconvert", 0, "audio conversion element");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
115

116
117
GST_BOILERPLATE_FULL (GstAudioConvert, gst_audio_convert, GstBaseTransform,
    GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
118

119
120
/*** GSTREAMER PROTOTYPES *****************************************************/

121
122
#define STATIC_CAPS \
GST_STATIC_CAPS ( \
123
  "audio/x-raw-float, " \
124
    "rate = (int) [ 1, MAX ], " \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
125
    "channels = (int) [ 1, 8 ], " \
126
    "endianness = (int) BYTE_ORDER, " \
Wim Taymans's avatar
Wim Taymans committed
127
    "width = (int) 32;" \
128
129
  "audio/x-raw-int, " \
    "rate = (int) [ 1, MAX ], " \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
130
    "channels = (int) [ 1, 8 ], " \
131
    "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
132
133
    "width = (int) 32, " \
    "depth = (int) [ 1, 32 ], " \
134
    "signed = (boolean) { true, false }; " \
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
135
136
137
138
139
140
  "audio/x-raw-int, "   \
    "rate = (int) [ 1, MAX ], " \
    "channels = (int) [ 1, 8 ], "       \
    "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, "        \
    "width = (int) 24, "        \
    "depth = (int) [ 1, 24 ], " "signed = (boolean) { true, false }; "  \
141
142
  "audio/x-raw-int, " \
    "rate = (int) [ 1, MAX ], " \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
143
    "channels = (int) [ 1, 8 ], " \
144
    "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
145
146
    "width = (int) 16, " \
    "depth = (int) [ 1, 16 ], " \
147
    "signed = (boolean) { true, false }; " \
148
  "audio/x-raw-int, " \
149
    "rate = (int) [ 1, MAX ], " \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
150
    "channels = (int) [ 1, 8 ], " \
151
152
153
154
    "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
    "width = (int) 8, " \
    "depth = (int) [ 1, 8 ], " \
    "signed = (boolean) { true, false } " \
155
156
)

Ronald S. Bultje's avatar
Ronald S. Bultje committed
157
158
static GstAudioChannelPosition *supported_positions;

David Schleef's avatar
David Schleef committed
159
static GstStaticPadTemplate gst_audio_convert_src_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
160
161
162
163
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    STATIC_CAPS);
164

David Schleef's avatar
David Schleef committed
165
static GstStaticPadTemplate gst_audio_convert_sink_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
166
167
168
169
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    STATIC_CAPS);
170
171
172

/*** TYPE FUNCTIONS ***********************************************************/

Iain Holmes's avatar
Iain Holmes committed
173
174
175
176
177
178
static void
gst_audio_convert_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_add_pad_template (element_class,
David Schleef's avatar
David Schleef committed
179
      gst_static_pad_template_get (&gst_audio_convert_src_template));
Iain Holmes's avatar
Iain Holmes committed
180
  gst_element_class_add_pad_template (element_class,
David Schleef's avatar
David Schleef committed
181
      gst_static_pad_template_get (&gst_audio_convert_sink_template));
Iain Holmes's avatar
Iain Holmes committed
182
183
184
  gst_element_class_set_details (element_class, &audio_convert_details);
}

185
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
186
gst_audio_convert_class_init (GstAudioConvertClass * klass)
187
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
188
189
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  gint i;
190

Ronald S. Bultje's avatar
Ronald S. Bultje committed
191
192
193
194
195
196
  gobject_class->dispose = gst_audio_convert_dispose;

  supported_positions = g_new0 (GstAudioChannelPosition,
      GST_AUDIO_CHANNEL_POSITION_NUM);
  for (i = 0; i < GST_AUDIO_CHANNEL_POSITION_NUM; i++)
    supported_positions[i] = i;
197
198

  GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
199
      GST_DEBUG_FUNCPTR (gst_audio_convert_get_unit_size);
200
  GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
201
      GST_DEBUG_FUNCPTR (gst_audio_convert_transform_caps);
202
  GST_BASE_TRANSFORM_CLASS (klass)->fixate_caps =
203
      GST_DEBUG_FUNCPTR (gst_audio_convert_fixate_caps);
204
  GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
205
206
207
      GST_DEBUG_FUNCPTR (gst_audio_convert_set_caps);
  GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
      GST_DEBUG_FUNCPTR (gst_audio_convert_transform_ip);
208
  GST_BASE_TRANSFORM_CLASS (klass)->transform =
209
      GST_DEBUG_FUNCPTR (gst_audio_convert_transform);
210
211

  GST_BASE_TRANSFORM_CLASS (klass)->passthrough_on_same_caps = TRUE;
212
213
}

214
static void
215
gst_audio_convert_init (GstAudioConvert * this, GstAudioConvertClass * g_class)
216
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
217
218
219
220
221
222
223
}

static void
gst_audio_convert_dispose (GObject * obj)
{
  GstAudioConvert *this = GST_AUDIO_CONVERT (obj);

224
  audio_convert_clean_context (&this->ctx);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
225

226
  G_OBJECT_CLASS (parent_class)->dispose (obj);
227
228
229
230
}

/*** GSTREAMER FUNCTIONS ******************************************************/

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
/* convert the given GstCaps to our format */
static gboolean
gst_audio_convert_parse_caps (const GstCaps * caps, AudioConvertFmt * fmt)
{
  GstStructure *structure = gst_caps_get_structure (caps, 0);

  GST_DEBUG ("parse caps %p and %" GST_PTR_FORMAT, caps, caps);

  g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
  g_return_val_if_fail (fmt != NULL, FALSE);

  /* cleanup old */
  audio_convert_clean_fmt (fmt);

  fmt->endianness = G_BYTE_ORDER;
  fmt->is_int =
      (strcmp (gst_structure_get_name (structure), "audio/x-raw-int") == 0);

  /* parse common fields */
  if (!gst_structure_get_int (structure, "channels", &fmt->channels))
    goto no_values;
  if (!(fmt->pos = gst_audio_get_channel_positions (structure)))
    goto no_values;
  if (!gst_structure_get_int (structure, "width", &fmt->width))
    goto no_values;
  if (!gst_structure_get_int (structure, "rate", &fmt->rate))
    goto no_values;

  if (fmt->is_int) {
    /* int specific fields */
    if (!gst_structure_get_boolean (structure, "signed", &fmt->sign))
      goto no_values;
    if (!gst_structure_get_int (structure, "depth", &fmt->depth))
      goto no_values;

    /* width != 8 can have an endianness field */
    if (fmt->width != 8) {
      if (!gst_structure_get_int (structure, "endianness", &fmt->endianness))
        goto no_values;
    }
    /* depth cannot be bigger than the width */
    if (fmt->depth > fmt->width)
      goto not_allowed;
  }

  fmt->unit_size = (fmt->width * fmt->channels) / 8;

  return TRUE;

  /* ERRORS */
no_values:
  {
    GST_DEBUG ("could not get some values from structure");
    audio_convert_clean_fmt (fmt);
    return FALSE;
  }
not_allowed:
  {
    GST_DEBUG ("width > depth, not allowed - make us advertise correct fmt");
    audio_convert_clean_fmt (fmt);
    return FALSE;
  }
}

295
/* BaseTransform vmethods */
296
297
static gboolean
gst_audio_convert_get_unit_size (GstBaseTransform * base, GstCaps * caps,
298
    guint * size)
299
{
300
  AudioConvertFmt fmt = { 0 };
301

302
  g_assert (size);
303

304
  if (!gst_audio_convert_parse_caps (caps, &fmt))
305
    goto parse_error;
306

307
  *size = fmt.unit_size;
308

309
  audio_convert_clean_fmt (&fmt);
310

311
  return TRUE;
312
313
314
315
316

parse_error:
  {
    return FALSE;
  }
317
}
318

319
320
321
322
323
324
325
326
/* Modify the structure so that things that must always have a single
 * value (for float), or can always be losslessly converted (for int), have
 * appropriate values.
 */
static GstStructure *
make_lossless_changes (GstStructure * s, gboolean isfloat)
{
  if (isfloat) {
327
328
    /* float doesn't have a depth or signedness field and only supports a
     * width of 32 and native endianness */
329
    gst_structure_remove_field (s, "depth");
330
    gst_structure_remove_field (s, "signed");
331
332
333
334
335
336
337
    gst_structure_set (s, "width", G_TYPE_INT, 32, NULL);
    gst_structure_set (s, "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
  } else {
    /* int supports either endian, and signed or unsigned. GValues are a pain */
    GValue list = { 0 };
    GValue val = { 0 };
    int i;
338
339
    const gint endian[] = { G_LITTLE_ENDIAN, G_BIG_ENDIAN };
    const gboolean booleans[] = { TRUE, FALSE };
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417

    g_value_init (&list, GST_TYPE_LIST);
    g_value_init (&val, G_TYPE_INT);
    for (i = 0; i < 2; i++) {
      g_value_set_int (&val, endian[i]);
      gst_value_list_append_value (&list, &val);
    }
    gst_structure_set_value (s, "endianness", &list);
    g_value_unset (&val);
    g_value_unset (&list);

    g_value_init (&list, GST_TYPE_LIST);
    g_value_init (&val, G_TYPE_BOOLEAN);
    for (i = 0; i < 2; i++) {
      g_value_set_boolean (&val, booleans[i]);
      gst_value_list_append_value (&list, &val);
    }
    gst_structure_set_value (s, "signed", &list);
    g_value_unset (&val);
    g_value_unset (&list);
  }

  return s;
}

/* Little utility function to create a related structure for float/int */
static void
append_with_other_format (GstCaps * caps, GstStructure * s, gboolean isfloat)
{
  GstStructure *s2;

  if (isfloat) {
    s2 = gst_structure_copy (s);
    gst_structure_set_name (s2, "audio/x-raw-int");
    s = make_lossless_changes (s2, FALSE);
    gst_caps_append_structure (caps, s2);
  } else {
    s2 = gst_structure_copy (s);
    gst_structure_set_name (s2, "audio/x-raw-float");
    s = make_lossless_changes (s2, TRUE);
    gst_caps_append_structure (caps, s2);
  }
}

/* Set widths (a list); multiples of 8 between min and max */
static void
set_structure_widths (GstStructure * s, int min, int max)
{
  GValue list = { 0 };
  GValue val = { 0 };
  int width;

  if (min == max) {
    gst_structure_set (s, "width", G_TYPE_INT, min, NULL);
    return;
  }

  g_value_init (&list, GST_TYPE_LIST);
  g_value_init (&val, G_TYPE_INT);
  for (width = min; width <= max; width += 8) {
    g_value_set_int (&val, width);
    gst_value_list_append_value (&list, &val);
  }
  gst_structure_set_value (s, "width", &list);
  g_value_unset (&val);
  g_value_unset (&list);
}

/* Audioconvert can perform all conversions on audio except for resampling. 
 * However, there are some conversions we _prefer_ not to do. For example, it's
 * better to convert format (float<->int, endianness, etc) than the number of
 * channels, as the latter conversion is not lossless.
 *
 * So, we return, in order (assuming input caps have only one structure; 
 * is this right?):
*  - input caps with a different format (lossless conversions).
 *  - input caps with a different format (slightly lossy conversions).
 *  - input caps with a different number of channels (very lossy!)
418
 */
419
420
static GstCaps *
gst_audio_convert_transform_caps (GstBaseTransform * base,
421
422
    GstPadDirection direction, GstCaps * caps)
{
423
  GstCaps *ret;
424
425
426
  GstStructure *s, *structure;
  gboolean isfloat;
  gint width, depth, channels;
427
  const gchar *fields_used[] = {
428
    "width", "depth", "rate", "channels", "endianness", "signed"
429
  };
430
  const gchar *structure_name;
431
  int i;
432
433
434

  g_return_val_if_fail (GST_CAPS_IS_SIMPLE (caps), NULL);

435
  structure = gst_caps_get_structure (caps, 0);
436
  structure_name = gst_structure_get_name (structure);
437

438
  isfloat = strcmp (structure_name, "audio/x-raw-float") == 0;
439

440
441
  /* We operate on a version of the original structure with any additional
   * fields absent */
442
  s = gst_structure_empty_new (structure_name);
443
444
445
446
447
  for (i = 0; i < sizeof (fields_used) / sizeof (*fields_used); i++) {
    if (gst_structure_has_field (structure, fields_used[i]))
      gst_structure_set_value (s, fields_used[i],
          gst_structure_get_value (structure, fields_used[i]));
  }
448

449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
  if (!isfloat) {
    /* Commonly, depth is left out: set it equal to width if we have a fixed
     * width, if so */
    if (!gst_structure_has_field (s, "depth") &&
        gst_structure_get_int (s, "width", &width))
      gst_structure_set (s, "depth", G_TYPE_INT, width, NULL);
  }

  ret = gst_caps_new_empty ();

  /* All lossless conversions */
  s = make_lossless_changes (s, isfloat);
  gst_caps_append_structure (ret, s);

  /* Same, plus a float<->int conversion */
  append_with_other_format (ret, s, isfloat);
465
466
  GST_DEBUG_OBJECT (base, "  step1: (%d) %" GST_PTR_FORMAT,
      gst_caps_get_size (ret), ret);
467
468
469
470
471
472
473
474
475
476
477
478
479
480

  /* We don't mind increasing width/depth/channels, but reducing them is 
   * Very Bad. Only available if width, depth, channels are already fixed. */
  s = gst_structure_copy (s);
  if (!isfloat) {
    if (gst_structure_get_int (structure, "width", &width))
      set_structure_widths (s, width, 32);
    if (gst_structure_get_int (structure, "depth", &depth)) {
      if (depth == 32)
        gst_structure_set (s, "depth", G_TYPE_INT, 32, NULL);
      else
        gst_structure_set (s, "depth", GST_TYPE_INT_RANGE, depth, 32, NULL);
    }
  }
481

482
483
484
485
486
  if (gst_structure_get_int (structure, "channels", &channels)) {
    if (channels == 8)
      gst_structure_set (s, "channels", G_TYPE_INT, 8, NULL);
    else
      gst_structure_set (s, "channels", GST_TYPE_INT_RANGE, channels, 8, NULL);
487
  }
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
  gst_caps_append_structure (ret, s);

  /* Same, plus a float<->int conversion */
  append_with_other_format (ret, s, isfloat);

  /* We'll reduce depth if we must... only for integer, since we can't do this
   * for float. We reduce as low as 16 bits; reducing to less than this is
   * even worse than dropping channels. We only do this if we haven't already
   * done the equivalent above. */
  if (!gst_structure_get_int (structure, "width", &width) || width > 16) {
    if (isfloat) {
      /* These are invalid widths/depths for float, but we don't actually use
       * them - we just pass it to append_with_other_format, which makes them
       * valid
       */
      GstStructure *s2 = gst_structure_copy (s);

      set_structure_widths (s2, 16, 32);
      gst_structure_set (s2, "depth", GST_TYPE_INT_RANGE, 16, 32, NULL);
      append_with_other_format (ret, s2, TRUE);
      gst_structure_free (s2);
    } else {
      s = gst_structure_copy (s);
      set_structure_widths (s, 16, 32);
      gst_structure_set (s, "depth", GST_TYPE_INT_RANGE, 16, 32, NULL);
      gst_caps_append_structure (ret, s);
    }
  }

  /* Channel conversions to fewer channels is only done if needed - generally
   * it's very bad to drop channels entirely.
   */
  s = gst_structure_copy (s);
  gst_structure_set (s, "channels", GST_TYPE_INT_RANGE, 1, 8, NULL);
  gst_caps_append_structure (ret, s);

  /* Same, plus a float<->int conversion */
  append_with_other_format (ret, s, isfloat);

  /* And, finally, for integer only, we allow conversion to any width/depth we
   * support: this should be equivalent to our (non-float) template caps. (the
   * floating point case should be being handled just above) */
  s = gst_structure_copy (s);
  set_structure_widths (s, 8, 32);
  gst_structure_set (s, "depth", GST_TYPE_INT_RANGE, 1, 32, NULL);

  if (isfloat) {
    append_with_other_format (ret, s, TRUE);
    gst_structure_free (s);
  } else
    gst_caps_append_structure (ret, s);

540
  return ret;
541
}
542

543
544
545
/* try to keep as many of the structure members the same by fixating the
 * possible ranges; this way we convert the least amount of things as possible
 */
546
547
static void
gst_audio_convert_fixate_caps (GstBaseTransform * base,
548
    GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
549
{
550
  GstStructure *ins, *outs;
551
  gint rate, endianness, depth, width, channels;
552
553
554
555
556
557
558
559
560
561
  gboolean signedness;

  g_return_if_fail (gst_caps_is_fixed (caps));

  GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
      " based on caps %" GST_PTR_FORMAT, othercaps, caps);

  ins = gst_caps_get_structure (caps, 0);
  outs = gst_caps_get_structure (othercaps, 0);

562
563
  if (gst_structure_get_int (ins, "channels", &channels)) {
    if (gst_structure_has_field (outs, "channels")) {
564
      gst_structure_fixate_field_nearest_int (outs, "channels", channels);
565
566
    }
  }
567
568
  if (gst_structure_get_int (ins, "rate", &rate)) {
    if (gst_structure_has_field (outs, "rate")) {
569
      gst_structure_fixate_field_nearest_int (outs, "rate", rate);
570
571
572
573
    }
  }
  if (gst_structure_get_int (ins, "endianness", &endianness)) {
    if (gst_structure_has_field (outs, "endianness")) {
574
      gst_structure_fixate_field_nearest_int (outs, "endianness", endianness);
575
576
    }
  }
577
578
  if (gst_structure_get_int (ins, "width", &width)) {
    if (gst_structure_has_field (outs, "width")) {
579
      gst_structure_fixate_field_nearest_int (outs, "width", width);
580
581
582
583
584
    }
  } else {
    /* this is not allowed */
  }

585
586
  if (gst_structure_get_int (ins, "depth", &depth)) {
    if (gst_structure_has_field (outs, "depth")) {
587
      gst_structure_fixate_field_nearest_int (outs, "depth", depth);
588
    }
589
590
591
  } else {
    /* set depth as width */
    if (gst_structure_has_field (outs, "depth")) {
592
      gst_structure_fixate_field_nearest_int (outs, "depth", width);
593
    }
594
  }
595

596
597
  if (gst_structure_get_boolean (ins, "signed", &signedness)) {
    if (gst_structure_has_field (outs, "signed")) {
598
      gst_structure_fixate_field_boolean (outs, "signed", signedness);
599
    }
600
  }
601

602
  GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
603
604
}

605
606
static gboolean
gst_audio_convert_set_caps (GstBaseTransform * base, GstCaps * incaps,
607
    GstCaps * outcaps)
608
{
609
610
  AudioConvertFmt in_ac_caps = { 0 };
  AudioConvertFmt out_ac_caps = { 0 };
611
  GstAudioConvert *this = GST_AUDIO_CONVERT (base);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
612

613
614
615
616
617
618
619
620
  GST_DEBUG_OBJECT (base, "incaps %" GST_PTR_FORMAT ", outcaps %"
      GST_PTR_FORMAT, incaps, outcaps);

  if (!gst_audio_convert_parse_caps (incaps, &in_ac_caps))
    return FALSE;
  if (!gst_audio_convert_parse_caps (outcaps, &out_ac_caps))
    return FALSE;

621
622
  if (!audio_convert_prepare_context (&this->ctx, &in_ac_caps, &out_ac_caps))
    goto no_converter;
623
624

  return TRUE;
625

626
627
no_converter:
  {
628
629
    return FALSE;
  }
630
}
631

632
633
static GstFlowReturn
gst_audio_convert_transform_ip (GstBaseTransform * base, GstBuffer * buf)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
634
{
635
636
  /* nothing to do here */
  return GST_FLOW_OK;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
637
}
638

639
640
641
static GstFlowReturn
gst_audio_convert_transform (GstBaseTransform * base, GstBuffer * inbuf,
    GstBuffer * outbuf)
642
{
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
  GstAudioConvert *this = GST_AUDIO_CONVERT (base);
  gboolean res;
  gint insize, outsize;
  gint samples;
  gpointer src, dst;

  /* get amount of samples to convert. */
  samples = GST_BUFFER_SIZE (inbuf) / this->ctx.in.unit_size;

  /* get in/output sizes, to see if the buffers we got are of correct
   * sizes */
  if (!(res = audio_convert_get_sizes (&this->ctx, samples, &insize, &outsize)))
    goto error;

  /* check in and outsize */
  if (GST_BUFFER_SIZE (inbuf) < insize)
    goto wrong_size;
  if (GST_BUFFER_SIZE (outbuf) < outsize)
    goto wrong_size;

  /* get src and dst data */
  src = GST_BUFFER_DATA (inbuf);
  dst = GST_BUFFER_DATA (outbuf);

  /* and convert the samples */
  if (!(res = audio_convert_convert (&this->ctx, src, dst,
              samples, gst_buffer_is_writable (inbuf))))
670
671
672
    goto convert_error;

  GST_BUFFER_SIZE (outbuf) = outsize;
673

674
  return GST_FLOW_OK;
675

676
677
678
  /* ERRORS */
error:
  {
679
680
681
    GST_ELEMENT_ERROR (this, STREAM, NOT_IMPLEMENTED,
        ("cannot get input/output sizes for %d samples", samples),
        ("cannot get input/output sizes for %d samples", samples));
682
683
684
685
    return GST_FLOW_ERROR;
  }
wrong_size:
  {
686
687
688
689
690
691
692
693
694
695
696
697
    GST_ELEMENT_ERROR (this, STREAM, NOT_IMPLEMENTED,
        ("input/output buffers are of wrong size in: %d < %d or out: %d < %d",
            GST_BUFFER_SIZE (inbuf), insize, GST_BUFFER_SIZE (outbuf), outsize),
        ("input/output buffers are of wrong size in: %d < %d or out: %d < %d",
            GST_BUFFER_SIZE (inbuf), insize, GST_BUFFER_SIZE (outbuf),
            outsize));
    return GST_FLOW_ERROR;
  }
convert_error:
  {
    GST_ELEMENT_ERROR (this, STREAM, NOT_IMPLEMENTED,
        ("error while converting"), ("error while converting"));
698
    return GST_FLOW_ERROR;
699
700
  }
}