summaryrefslogtreecommitdiff
path: root/gst-libs/gst/audio/audio-converter.c
blob: aebe0f81055d0a4948101433ae88a829be4cec31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/* GStreamer
 * Copyright (C) 2005 Wim Taymans <wim at fluendo dot com>
 *           (C) 2015 Wim Taymans <wim.taymans@gmail.com>
 *
 * audioconverter.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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

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

#include <math.h>
#include <string.h>

#include "audio-converter.h"
#include "gstaudiopack.h"

/**
 * SECTION:audioconverter
 * @short_description: Generic audio conversion
 *
 * <refsect2>
 * <para>
 * This object is used to convert audio samples from one format to another.
 * The object can perform conversion of:
 * <itemizedlist>
 *  <listitem><para>
 *    audio format with optional dithering and noise shaping
 *  </para></listitem>
 *  <listitem><para>
 *    audio samplerate
 *  </para></listitem>
 *  <listitem><para>
 *    audio channels and channel layout
 *  </para></listitem>
 * </para>
 * </refsect2>
 */

#ifndef GST_DISABLE_GST_DEBUG
#define GST_CAT_DEFAULT ensure_debug_category()
static GstDebugCategory *
ensure_debug_category (void)
{
  static gsize cat_gonce = 0;

  if (g_once_init_enter (&cat_gonce)) {
    gsize cat_done;

    cat_done = (gsize) _gst_debug_category_new ("audio-converter", 0,
        "audio-converter object");

    g_once_init_leave (&cat_gonce, cat_done);
  }

  return (GstDebugCategory *) cat_gonce;
}
#else
#define ensure_debug_category() /* NOOP */
#endif /* GST_DISABLE_GST_DEBUG */

typedef void (*AudioConvertFunc) (gpointer dst, const gpointer src, gint count);

/*                           int/int    int/float  float/int float/float
 *
 *  unpack                     S32          S32         F64       F64
 *  convert                               S32->F64
 *  channel mix                S32          F64         F64       F64
 *  convert                                           F64->S32
 *  quantize                   S32                      S32
 *  pack                       S32          F64         S32       F64
 */
struct _GstAudioConverter
{
  GstAudioInfo in;
  GstAudioInfo out;

  GstStructure *config;

  gboolean in_default;

  AudioConvertFunc convert_in;

  gboolean mix_passthrough;
  GstAudioChannelMix *mix;

  AudioConvertFunc convert_out;

  GstAudioQuantize *quant;

  gboolean out_default;

  gboolean passthrough;

  gpointer tmpbuf;
  gpointer tmpbuf2;
  gint tmpbufsize;
};

/*
static guint
get_opt_uint (GstAudioConverter * convert, const gchar * opt, guint def)
{
  guint res;
  if (!gst_structure_get_uint (convert->config, opt, &res))
    res = def;
  return res;
}
*/

static gint
get_opt_enum (GstAudioConverter * convert, const gchar * opt, GType type,
    gint def)
{
  gint res;
  if (!gst_structure_get_enum (convert->config, opt, type, &res))
    res = def;
  return res;
}

#define DEFAULT_OPT_DITHER_METHOD GST_AUDIO_DITHER_NONE
#define DEFAULT_OPT_NOISE_SHAPING_METHOD GST_AUDIO_NOISE_SHAPING_NONE
#define DEFAULT_OPT_QUANTIZATION 1

#define GET_OPT_DITHER_METHOD(c) get_opt_enum(c, \
    GST_AUDIO_CONVERTER_OPT_DITHER_METHOD, GST_TYPE_AUDIO_DITHER_METHOD, \
    DEFAULT_OPT_DITHER_METHOD)
#define GET_OPT_NOISE_SHAPING_METHOD(c) get_opt_enum(c, \
    GST_AUDIO_CONVERTER_OPT_NOISE_SHAPING_METHOD, GST_TYPE_AUDIO_NOISE_SHAPING_METHOD, \
    DEFAULT_OPT_NOISE_SHAPING_METHOD)
#define GET_OPT_QUANTIZATION(c) get_opt_uint(c, \
    GST_AUDIO_CONVERTER_OPT_QUANTIZATION, DEFAULT_OPT_QUANTIZATION)

static gboolean
copy_config (GQuark field_id, const GValue * value, gpointer user_data)
{
  GstAudioConverter *convert = user_data;

  gst_structure_id_set_value (convert->config, field_id, value);

  return TRUE;
}

/**
 * gst_audio_converter_set_config:
 * @convert: a #GstAudioConverter
 * @config: (transfer full): a #GstStructure
 *
 * Set @config as extra configuraion for @convert.
 *
 * If the parameters in @config can not be set exactly, this function returns
 * %FALSE and will try to update as much state as possible. The new state can
 * then be retrieved and refined with gst_audio_converter_get_config().
 *
 * Look at the #GST_AUDIO_CONVERTER_OPT_* fields to check valid configuration
 * option and values.
 *
 * Returns: %TRUE when @config could be set.
 */
gboolean
gst_audio_converter_set_config (GstAudioConverter * convert,
    GstStructure * config)
{
  g_return_val_if_fail (convert != NULL, FALSE);
  g_return_val_if_fail (config != NULL, FALSE);

  gst_structure_foreach (config, copy_config, convert);
  gst_structure_free (config);

  return TRUE;
}

/**
 * gst_audio_converter_get_config:
 * @convert: a #GstAudioConverter
 *
 * Get the current configuration of @convert.
 *
 * Returns: a #GstStructure that remains valid for as long as @convert is valid
 *   or until gst_audio_converter_set_config() is called.
 */
const GstStructure *
gst_audio_converter_get_config (GstAudioConverter * convert)
{
  g_return_val_if_fail (convert != NULL, NULL);

  return convert->config;
}

/**
 * gst_audio_converter_new: (skip)
 * @in: a source #GstAudioInfo
 * @out: a destination #GstAudioInfo
 * @config: (transfer full): a #GstStructure with configuration options
 *
 * Create a new #GstAudioConverter that is able to convert between @in and @out
 * audio formats.
 *
 * @config contains extra configuration options, see #GST_VIDEO_CONVERTER_OPT_*
 * parameters for details about the options and values.
 *
 * Returns: a #GstAudioConverter or %NULL if conversion is not possible.
 */
GstAudioConverter *
gst_audio_converter_new (GstAudioInfo * in, GstAudioInfo * out,
    GstStructure * config)
{
  GstAudioConverter *convert;
  gint in_depth, out_depth;
  GstAudioChannelMixFlags flags;
  gboolean in_int, out_int;
  GstAudioFormat format;
  GstAudioDitherMethod dither;
  GstAudioNoiseShapingMethod ns;

  g_return_val_if_fail (in != NULL, FALSE);
  g_return_val_if_fail (out != NULL, FALSE);
  g_return_val_if_fail (in->rate == out->rate, FALSE);
  g_return_val_if_fail (in->layout == GST_AUDIO_LAYOUT_INTERLEAVED, FALSE);
  g_return_val_if_fail (in->layout == out->layout, FALSE);

  if ((GST_AUDIO_INFO_CHANNELS (in) != GST_AUDIO_INFO_CHANNELS (out)) &&
      (GST_AUDIO_INFO_IS_UNPOSITIONED (in)
          || GST_AUDIO_INFO_IS_UNPOSITIONED (out)))
    goto unpositioned;

  convert = g_slice_new0 (GstAudioConverter);

  convert->in = *in;
  convert->out = *out;

  /* default config */
  convert->config = gst_structure_new_empty ("GstAudioConverter");
  if (config)
    gst_audio_converter_set_config (convert, config);

  dither = GET_OPT_DITHER_METHOD (convert);
  ns = GET_OPT_NOISE_SHAPING_METHOD (convert);

  GST_INFO ("unitsizes: %d -> %d", in->bpf, out->bpf);

  in_depth = GST_AUDIO_FORMAT_INFO_DEPTH (in->finfo);
  out_depth = GST_AUDIO_FORMAT_INFO_DEPTH (out->finfo);

  GST_INFO ("depth in %d, out %d", in_depth, out_depth);

  in_int = GST_AUDIO_FORMAT_INFO_IS_INTEGER (in->finfo);
  out_int = GST_AUDIO_FORMAT_INFO_IS_INTEGER (out->finfo);

  flags =
      GST_AUDIO_INFO_IS_UNPOSITIONED (in) ?
      GST_AUDIO_CHANNEL_MIX_FLAGS_UNPOSITIONED_IN : 0;
  flags |=
      GST_AUDIO_INFO_IS_UNPOSITIONED (out) ?
      GST_AUDIO_CHANNEL_MIX_FLAGS_UNPOSITIONED_OUT : 0;


  /* step 1, unpack */
  format = in->finfo->unpack_format;
  convert->in_default = in->finfo->unpack_format == in->finfo->format;
  GST_INFO ("unpack format %s to %s",
      gst_audio_format_to_string (in->finfo->format),
      gst_audio_format_to_string (format));

  /* step 2, optional convert from S32 to F64 for channel mix */
  if (in_int && !out_int) {
    GST_INFO ("convert S32 to F64");
    convert->convert_in = (AudioConvertFunc) audio_orc_s32_to_double;
    format = GST_AUDIO_FORMAT_F64;
  }

  /* step 3, channel mix */
  convert->mix =
      gst_audio_channel_mix_new (flags, format, in->channels, in->position,
      out->channels, out->position);
  convert->mix_passthrough =
      gst_audio_channel_mix_is_passthrough (convert->mix);
  GST_INFO ("mix format %s, passthrough %d, in_channels %d, out_channels %d",
      gst_audio_format_to_string (format), convert->mix_passthrough,
      in->channels, out->channels);

  /* step 4, optional convert for quantize */
  if (!in_int && out_int) {
    GST_INFO ("convert F64 to S32");
    convert->convert_out = (AudioConvertFunc) audio_orc_double_to_s32;
    format = GST_AUDIO_FORMAT_S32;
  }
  /* step 5, optional quantize */
  /* Don't dither or apply noise shaping if target depth is bigger than 20 bits
   * as DA converters only can do a SNR up to 20 bits in reality.
   * Also don't dither or apply noise shaping if target depth is larger than
   * source depth. */
  if (out_depth > 20 || (in_int && out_depth >= in_depth)) {
    dither = GST_AUDIO_DITHER_NONE;
    ns = GST_AUDIO_NOISE_SHAPING_NONE;
    GST_INFO ("using no dither and noise shaping");
  } else {
    GST_INFO ("using dither %d and noise shaping %d", dither, ns);
    /* Use simple error feedback when output sample rate is smaller than
     * 32000 as the other methods might move the noise to audible ranges */
    if (ns > GST_AUDIO_NOISE_SHAPING_ERROR_FEEDBACK && out->rate < 32000)
      ns = GST_AUDIO_NOISE_SHAPING_ERROR_FEEDBACK;
  }
  /* we still want to run the quantization step when reducing bits to get
   * the rounding correct */
  if (out_int && out_depth < 32) {
    GST_INFO ("quantize to %d bits, dither %d, ns %d", out_depth, dither, ns);
    convert->quant = gst_audio_quantize_new (dither, ns, 0, format,
        out->channels, 1U << (32 - out_depth));
  }
  /* step 6, pack */
  g_assert (out->finfo->unpack_format == format);
  convert->out_default = format == out->finfo->format;
  GST_INFO ("pack format %s to %s", gst_audio_format_to_string (format),
      gst_audio_format_to_string (out->finfo->format));

  /* optimize */
  if (out->finfo->format == in->finfo->format && convert->mix_passthrough) {
    GST_INFO ("same formats and passthrough mixing -> passthrough");
    convert->passthrough = TRUE;
  }

  return convert;

  /* ERRORS */
unpositioned:
  {
    GST_WARNING ("unpositioned channels");
    return NULL;
  }
}

/**
 * gst_audio_converter_free:
 * @convert: a #GstAudioConverter
 *
 * Free a previously allocated @convert instance.
 */
void
gst_audio_converter_free (GstAudioConverter * convert)
{
  g_return_if_fail (convert != NULL);

  if (convert->quant)
    gst_audio_quantize_free (convert->quant);
  if (convert->mix)
    gst_audio_channel_mix_free (convert->mix);
  gst_audio_info_init (&convert->in);
  gst_audio_info_init (&convert->out);

  g_free (convert->tmpbuf);
  g_free (convert->tmpbuf2);
  gst_structure_free (convert->config);

  g_slice_free (GstAudioConverter, convert);
}

/**
 * gst_audio_converter_samples:
 * @convert: a #GstAudioConverter
 * @flags: extra #GstAudioConverterFlags
 * @in: input samples
 * @in_samples: number of input samples
 * @out: output samples
 * @out_samples: number of output samples
 * @in_consumed: number of input samples consumed
 * @out_produced: number of output samples produced
 *
 * Perform the conversion with @in_samples in @in to @out_samples in @out
 * using @convert.
 *
 * The actual number of samples used from @in is returned in @in_consumed and
 * can be less than @in_samples. The actual number of samples produced is
 * returned in @out_produced and can be less than @out_samples.
 *
 * Returns: %TRUE is the conversion could be performed.
 */
gboolean
gst_audio_converter_samples (GstAudioConverter * convert,
    GstAudioConverterFlags flags, gpointer in, gsize in_samples,
    gpointer out, gsize out_samples, gsize * in_consumed, gsize * out_produced)
{
  guint size;
  gpointer outbuf, tmpbuf, tmpbuf2;

  g_return_val_if_fail (convert != NULL, FALSE);
  g_return_val_if_fail (in != NULL, FALSE);
  g_return_val_if_fail (out != NULL, FALSE);
  g_return_val_if_fail (in_consumed != NULL, FALSE);
  g_return_val_if_fail (out_produced != NULL, FALSE);

  in_samples = MIN (in_samples, out_samples);

  if (in_samples == 0) {
    *in_consumed = 0;
    *out_produced = 0;
    return TRUE;
  }

  if (convert->passthrough) {
    memcpy (out, in, in_samples * convert->in.bpf);
    *out_produced = in_samples;
    *in_consumed = in_samples;
    return TRUE;
  }

  size =
      sizeof (gdouble) * in_samples * MAX (convert->in.channels,
      convert->out.channels);

  if (size > convert->tmpbufsize) {
    convert->tmpbuf = g_realloc (convert->tmpbuf, size);
    convert->tmpbuf2 = g_realloc (convert->tmpbuf2, size);
    convert->tmpbufsize = size;
  }
  tmpbuf = convert->tmpbuf;
  tmpbuf2 = convert->tmpbuf2;

  /* 1. unpack */
  if (!convert->in_default) {
    if (!convert->convert_in && convert->mix_passthrough
        && !convert->convert_out && !convert->quant && convert->out_default)
      outbuf = out;
    else
      outbuf = tmpbuf;

    convert->in.finfo->unpack_func (convert->in.finfo,
        GST_AUDIO_PACK_FLAG_TRUNCATE_RANGE, outbuf, in,
        in_samples * convert->in.channels);
    in = outbuf;
  }

  /* 2. optionally convert for mixing */
  if (convert->convert_in) {
    if (convert->mix_passthrough && !convert->convert_out && !convert->quant
        && convert->out_default)
      outbuf = out;
    else if (in == tmpbuf)
      outbuf = tmpbuf2;
    else
      outbuf = tmpbuf;

    convert->convert_in (outbuf, in, in_samples * convert->in.channels);
    in = outbuf;
  }

  /* step 3, channel mix if not passthrough */
  if (!convert->mix_passthrough) {
    if (!convert->convert_out && !convert->quant && convert->out_default)
      outbuf = out;
    else
      outbuf = tmpbuf;

    gst_audio_channel_mix_samples (convert->mix, in, outbuf, in_samples);
    in = outbuf;
  }
  /* step 4, optional convert F64 -> S32 for quantize */
  if (convert->convert_out) {
    if (!convert->quant && convert->out_default)
      outbuf = out;
    else
      outbuf = tmpbuf;

    convert->convert_out (outbuf, in, in_samples * convert->out.channels);
    in = outbuf;
  }

  /* step 5, optional quantize */
  if (convert->quant) {
    if (convert->out_default)
      outbuf = out;
    else
      outbuf = tmpbuf;

    gst_audio_quantize_samples (convert->quant, outbuf, in, in_samples);
    in = outbuf;
  }

  /* step 6, pack */
  if (!convert->out_default) {
    convert->out.finfo->pack_func (convert->out.finfo, 0, in, out,
        in_samples * convert->out.channels);
  }
  *out_produced = in_samples;
  *in_consumed = in_samples;

  return TRUE;
}