gstaudioresample.c 20.9 KB
Newer Older
1
/* GStreamer
2
 * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * Copyright (C) 2003,2004 David A. Schleef <ds@schleef.org>
 *
 * 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.
 */
/* Element-Checklist-Version: 5 */

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 * SECTION:element-audioresample
 *
 * <refsect2>
 * Audioresample resamples raw audio buffers to different sample rates using
 * a configurable windowing function to enhance quality.
 * <title>Example launch line</title>
 * <para>
 * <programlisting>
 * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! audio/x-raw-int, rate=8000 ! alsasink
 * </programlisting>
 * Decode an Ogg/Vorbis downsample to 8Khz and play sound through alsa. 
 * To create the Ogg/Vorbis file refer to the documentation of vorbisenc.
 * </para>
 * </refsect2>
 *
 * Last reviewed on 2006-03-02 (0.10.4)
 */

41
42
43
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
44

45
46
47
48
49
50
#include <string.h>
#include <math.h>

/*#define DEBUG_ENABLED */
#include "gstaudioresample.h"
#include <gst/audio/audio.h>
51
#include <gst/base/gstbasetransform.h>
52

53
GST_DEBUG_CATEGORY (audioresample_debug);
54
55
56
#define GST_CAT_DEFAULT audioresample_debug

/* elementfactory information */
Stefan Kost's avatar
Stefan Kost committed
57
static const GstElementDetails gst_audioresample_details =
58
59
60
61
62
GST_ELEMENT_DETAILS ("Audio scaler",
    "Filter/Converter/Audio",
    "Resample audio",
    "David Schleef <ds@schleef.org>");

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
63
#define DEFAULT_FILTERLEN       16
64

65
66
enum
{
67
68
  PROP_0,
  PROP_FILTERLEN
69
70
71
};

#define SUPPORTED_CAPS \
72
GST_STATIC_CAPS ( \
73
74
75
76
77
78
    "audio/x-raw-int, " \
      "rate = (int) [ 1, MAX ], " \
      "channels = (int) [ 1, MAX ], " \
      "endianness = (int) BYTE_ORDER, " \
      "width = (int) 16, " \
      "depth = (int) 16, " \
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
      "signed = (boolean) true;" \
    "audio/x-raw-int, " \
      "rate = (int) [ 1, MAX ], " \
      "channels = (int) [ 1, MAX ], " \
      "endianness = (int) BYTE_ORDER, " \
      "width = (int) 32, " \
      "depth = (int) 32, " \
      "signed = (boolean) true;" \
    "audio/x-raw-float, " \
      "rate = (int) [ 1, MAX ], "	\
      "channels = (int) [ 1, MAX ], " \
      "endianness = (int) BYTE_ORDER, " \
      "width = (int) 32; " \
    "audio/x-raw-float, " \
      "rate = (int) [ 1, MAX ], "	\
      "channels = (int) [ 1, MAX ], " \
      "endianness = (int) BYTE_ORDER, " \
      "width = (int) 64" \
97
)
98

99
100
101
static GstStaticPadTemplate gst_audioresample_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK, GST_PAD_ALWAYS, SUPPORTED_CAPS);
102

103
104
105
static GstStaticPadTemplate gst_audioresample_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC, GST_PAD_ALWAYS, SUPPORTED_CAPS);
106

107
108
109
110
static void gst_audioresample_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_audioresample_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);
111

112
/* vmethods */
113
114
115
116
117
118
119
120
121
122
123
124
125
126
gboolean audioresample_get_unit_size (GstBaseTransform * base,
    GstCaps * caps, guint * size);
GstCaps *audioresample_transform_caps (GstBaseTransform * base,
    GstPadDirection direction, GstCaps * caps);
gboolean audioresample_transform_size (GstBaseTransform * trans,
    GstPadDirection direction, GstCaps * incaps, guint insize,
    GstCaps * outcaps, guint * outsize);
gboolean audioresample_set_caps (GstBaseTransform * base, GstCaps * incaps,
    GstCaps * outcaps);
static GstFlowReturn audioresample_pushthrough (GstAudioresample *
    audioresample);
static GstFlowReturn audioresample_transform (GstBaseTransform * base,
    GstBuffer * inbuf, GstBuffer * outbuf);
static gboolean audioresample_event (GstBaseTransform * base, GstEvent * event);
127
128
static gboolean audioresample_start (GstBaseTransform * base);
static gboolean audioresample_stop (GstBaseTransform * base);
129

130
131
#define DEBUG_INIT(bla) \
  GST_DEBUG_CATEGORY_INIT (audioresample_debug, "audioresample", 0, "audio resampling element");
132

133
134
GST_BOILERPLATE_FULL (GstAudioresample, gst_audioresample, GstBaseTransform,
    GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
135

136
137
138
139
static void
gst_audioresample_base_init (gpointer g_class)
{
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
140

141
142
143
144
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&gst_audioresample_src_template));
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&gst_audioresample_sink_template));
145

146
147
  gst_element_class_set_details (gstelement_class, &gst_audioresample_details);
}
148

149
150
static void
gst_audioresample_class_init (GstAudioresampleClass * klass)
151
152
153
154
155
156
157
158
{
  GObjectClass *gobject_class;

  gobject_class = (GObjectClass *) klass;

  gobject_class->set_property = gst_audioresample_set_property;
  gobject_class->get_property = gst_audioresample_get_property;

159
  g_object_class_install_property (gobject_class, PROP_FILTERLEN,
160
      g_param_spec_int ("filter_length", "filter_length", "filter_length",
161
162
          0, G_MAXINT, DEFAULT_FILTERLEN,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
163

164
165
166
167
  GST_BASE_TRANSFORM_CLASS (klass)->start =
      GST_DEBUG_FUNCPTR (audioresample_start);
  GST_BASE_TRANSFORM_CLASS (klass)->stop =
      GST_DEBUG_FUNCPTR (audioresample_stop);
168
169
170
171
172
173
174
175
176
177
  GST_BASE_TRANSFORM_CLASS (klass)->transform_size =
      GST_DEBUG_FUNCPTR (audioresample_transform_size);
  GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
      GST_DEBUG_FUNCPTR (audioresample_get_unit_size);
  GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
      GST_DEBUG_FUNCPTR (audioresample_transform_caps);
  GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
      GST_DEBUG_FUNCPTR (audioresample_set_caps);
  GST_BASE_TRANSFORM_CLASS (klass)->transform =
      GST_DEBUG_FUNCPTR (audioresample_transform);
178
179
  GST_BASE_TRANSFORM_CLASS (klass)->event =
      GST_DEBUG_FUNCPTR (audioresample_event);
180
181

  GST_BASE_TRANSFORM_CLASS (klass)->passthrough_on_same_caps = TRUE;
182
183
}

184
static void
185
gst_audioresample_init (GstAudioresample * audioresample,
186
    GstAudioresampleClass * klass)
187
{
188
189
190
191
192
  GstBaseTransform *trans;

  trans = GST_BASE_TRANSFORM (audioresample);

  /* buffer alloc passthrough is too impossible. FIXME, it
193
   * is trivial in the passthrough case. */
194
  gst_pad_set_bufferalloc_function (trans->sinkpad, NULL);
195

196
197
198
199
200
201
202
203
204
205
  audioresample->filter_length = DEFAULT_FILTERLEN;
}

/* vmethods */
static gboolean
audioresample_start (GstBaseTransform * base)
{
  GstAudioresample *audioresample = GST_AUDIORESAMPLE (base);

  audioresample->resample = resample_new ();
206
207
208
  audioresample->ts_offset = -1;
  audioresample->offset = -1;
  audioresample->next_ts = -1;
209

210
211
212
213
  resample_set_filter_length (audioresample->resample,
      audioresample->filter_length);

  return TRUE;
214
215
}

216
217
static gboolean
audioresample_stop (GstBaseTransform * base)
218
{
219
  GstAudioresample *audioresample = GST_AUDIORESAMPLE (base);
220

221
222
223
  if (audioresample->resample) {
    resample_free (audioresample->resample);
    audioresample->resample = NULL;
224
  }
225

226
227
228
  gst_caps_replace (&audioresample->sinkcaps, NULL);
  gst_caps_replace (&audioresample->srccaps, NULL);

229
  return TRUE;
230
231
}

232
gboolean
233
234
235
audioresample_get_unit_size (GstBaseTransform * base, GstCaps * caps,
    guint * size)
{
236
237
238
  gint width, channels;
  GstStructure *structure;
  gboolean ret;
239

240
  g_assert (size);
241

242
243
244
245
246
  /* this works for both float and int */
  structure = gst_caps_get_structure (caps, 0);
  ret = gst_structure_get_int (structure, "width", &width);
  ret &= gst_structure_get_int (structure, "channels", &channels);
  g_return_val_if_fail (ret, FALSE);
247

248
  *size = width * channels / 8;
249

250
  return TRUE;
251
252
}

253
254
GstCaps *
audioresample_transform_caps (GstBaseTransform * base,
255
    GstPadDirection direction, GstCaps * caps)
256
{
257
  GstCaps *res;
258
259
  GstStructure *structure;

260
261
262
263
264
  /* transform caps gives one single caps so we can just replace
   * the rate property with our range. */
  res = gst_caps_copy (caps);
  structure = gst_caps_get_structure (res, 0);
  gst_structure_set (structure, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
265

266
  return res;
267
268
}

269
static gboolean
270
resample_set_state_from_caps (ResampleState * state, GstCaps * incaps,
271
    GstCaps * outcaps, gint * channels, gint * inrate, gint * outrate)
272
273
274
{
  GstStructure *structure;
  gboolean ret;
275
276
  gint myinrate, myoutrate;
  int mychannels;
277
278
  gint width, depth;
  ResampleFormat format;
279

280
281
  GST_DEBUG ("incaps %" GST_PTR_FORMAT ", outcaps %"
      GST_PTR_FORMAT, incaps, outcaps);
282

283
  structure = gst_caps_get_structure (incaps, 0);
284

285
286
287
288
289
290
  /* get width */
  ret = gst_structure_get_int (structure, "width", &width);
  if (!ret)
    goto no_width;

  /* figure out the format */
291
  if (g_str_equal (gst_structure_get_name (structure), "audio/x-raw-float")) {
292
293
294
295
296
297
    if (width == 32)
      format = RESAMPLE_FORMAT_F32;
    else if (width == 64)
      format = RESAMPLE_FORMAT_F64;
    else
      goto wrong_depth;
298
  } else {
299
300
301
302
303
304
305
306
307
308
309
    /* for int, depth and width must be the same */
    ret = gst_structure_get_int (structure, "depth", &depth);
    if (!ret || width != depth)
      goto not_equal;

    if (width == 16)
      format = RESAMPLE_FORMAT_S16;
    else if (width == 32)
      format = RESAMPLE_FORMAT_S32;
    else
      goto wrong_depth;
310
  }
311
312
  ret = gst_structure_get_int (structure, "rate", &myinrate);
  ret &= gst_structure_get_int (structure, "channels", &mychannels);
313
314
  if (!ret)
    goto no_in_rate_channels;
315

316
317
  structure = gst_caps_get_structure (outcaps, 0);
  ret = gst_structure_get_int (structure, "rate", &myoutrate);
318
319
  if (!ret)
    goto no_out_rate;
320

321
322
323
324
325
326
  if (channels)
    *channels = mychannels;
  if (inrate)
    *inrate = myinrate;
  if (outrate)
    *outrate = myoutrate;
327

328
  resample_set_format (state, format);
329
330
331
  resample_set_n_channels (state, mychannels);
  resample_set_input_rate (state, myinrate);
  resample_set_output_rate (state, myoutrate);
332

333
  return TRUE;
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

  /* ERRORS */
no_width:
  {
    GST_DEBUG ("failed to get width from caps");
    return FALSE;
  }
not_equal:
  {
    GST_DEBUG ("width %d and depth %d must be the same", width, depth);
    return FALSE;
  }
wrong_depth:
  {
    GST_DEBUG ("unknown depth %d found", depth);
    return FALSE;
  }
no_in_rate_channels:
  {
    GST_DEBUG ("could not get input rate and channels");
    return FALSE;
  }
no_out_rate:
  {
    GST_DEBUG ("could not get output rate");
    return FALSE;
  }
361
362
}

363
gboolean
364
audioresample_transform_size (GstBaseTransform * base,
365
    GstPadDirection direction, GstCaps * caps, guint size, GstCaps * othercaps,
366
367
    guint * othersize)
{
368
369
370
371
372
373
  GstAudioresample *audioresample = GST_AUDIORESAMPLE (base);
  ResampleState *state;
  GstCaps *srccaps, *sinkcaps;
  gboolean use_internal = FALSE;        /* whether we use the internal state */
  gboolean ret = TRUE;

374
375
  GST_DEBUG_OBJECT (base, "asked to transform size %d in direction %s",
      size, direction == GST_PAD_SINK ? "SINK" : "SRC");
376
377
378
379
380
381
382
  if (direction == GST_PAD_SINK) {
    sinkcaps = caps;
    srccaps = othercaps;
  } else {
    sinkcaps = othercaps;
    srccaps = caps;
  }
383

384
385
386
387
388
389
390
  /* if the caps are the ones that _set_caps got called with; we can use
   * our own state; otherwise we'll have to create a state */
  if (gst_caps_is_equal (sinkcaps, audioresample->sinkcaps) &&
      gst_caps_is_equal (srccaps, audioresample->srccaps)) {
    use_internal = TRUE;
    state = audioresample->resample;
  } else {
391
392
    GST_DEBUG_OBJECT (audioresample,
        "caps are not the set caps, creating state");
393
    state = resample_new ();
394
    resample_set_filter_length (state, audioresample->filter_length);
395
396
    resample_set_state_from_caps (state, sinkcaps, srccaps, NULL, NULL, NULL);
  }
397

398
399
400
401
  if (direction == GST_PAD_SINK) {
    /* asked to convert size of an incoming buffer */
    *othersize = resample_get_output_size_for_input (state, size);
  } else {
402
403
    /* asked to convert size of an outgoing buffer */
    *othersize = resample_get_input_size_for_output (state, size);
404
  }
405
406
  g_assert (*othersize % state->sample_size == 0);

407
408
409
  /* we make room for one extra sample, given that the resampling filter
   * can output an extra one for non-integral i_rate/o_rate */
  GST_DEBUG_OBJECT (base, "transformed size %d to %d", size, *othersize);
410

411
412
413
414
415
  if (!use_internal) {
    resample_free (state);
  }

  return ret;
416
417
}

418
gboolean
419
420
421
audioresample_set_caps (GstBaseTransform * base, GstCaps * incaps,
    GstCaps * outcaps)
{
422
423
424
425
  gboolean ret;
  gint inrate, outrate;
  int channels;
  GstAudioresample *audioresample = GST_AUDIORESAMPLE (base);
426

427
428
  GST_DEBUG_OBJECT (base, "incaps %" GST_PTR_FORMAT ", outcaps %"
      GST_PTR_FORMAT, incaps, outcaps);
429

430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
  ret = resample_set_state_from_caps (audioresample->resample, incaps, outcaps,
      &channels, &inrate, &outrate);

  g_return_val_if_fail (ret, FALSE);

  audioresample->channels = channels;
  GST_DEBUG_OBJECT (audioresample, "set channels to %d", channels);
  audioresample->i_rate = inrate;
  GST_DEBUG_OBJECT (audioresample, "set i_rate to %d", inrate);
  audioresample->o_rate = outrate;
  GST_DEBUG_OBJECT (audioresample, "set o_rate to %d", outrate);

  /* save caps so we can short-circuit in the size_transform if the caps
   * are the same */
  gst_caps_replace (&audioresample->sinkcaps, incaps);
  gst_caps_replace (&audioresample->srccaps, outcaps);

  return TRUE;
448
449
}

450
451
static gboolean
audioresample_event (GstBaseTransform * base, GstEvent * event)
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
{
  GstAudioresample *audioresample;

  audioresample = GST_AUDIORESAMPLE (base);

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_FLUSH_START:
      break;
    case GST_EVENT_FLUSH_STOP:
      resample_input_flush (audioresample->resample);
      audioresample->ts_offset = -1;
      audioresample->next_ts = -1;
      audioresample->offset = -1;
      break;
    case GST_EVENT_NEWSEGMENT:
      resample_input_pushthrough (audioresample->resample);
      audioresample_pushthrough (audioresample);
      audioresample->ts_offset = -1;
      audioresample->next_ts = -1;
      audioresample->offset = -1;
      break;
    case GST_EVENT_EOS:
      resample_input_eos (audioresample->resample);
      audioresample_pushthrough (audioresample);
      break;
    default:
      break;
  }
  parent_class->event (base, event);

  return TRUE;
}

485
static GstFlowReturn
486
audioresample_do_output (GstAudioresample * audioresample, GstBuffer * outbuf)
487
488
{
  int outsize;
489
  int outsamples;
490
  ResampleState *r;
491
492
493
494

  r = audioresample->resample;

  outsize = resample_get_output_size (r);
495
496
497
498
499
  GST_DEBUG_OBJECT (audioresample, "audioresample can give me %d bytes",
      outsize);

  /* protect against mem corruption */
  if (outsize > GST_BUFFER_SIZE (outbuf)) {
500
501
502
503
    GST_WARNING_OBJECT (audioresample,
        "overriding audioresample's outsize %d with outbuffer's size %d",
        outsize, GST_BUFFER_SIZE (outbuf));
    outsize = GST_BUFFER_SIZE (outbuf);
504
  }
505
506
507
508
509
510
  /* catch possibly wrong size differences */
  if (GST_BUFFER_SIZE (outbuf) - outsize > r->sample_size) {
    GST_WARNING_OBJECT (audioresample,
        "audioresample's outsize %d too far from outbuffer's size %d",
        outsize, GST_BUFFER_SIZE (outbuf));
  }
511
512

  outsize = resample_get_output_data (r, GST_BUFFER_DATA (outbuf), outsize);
513
514
515
516
  outsamples = outsize / r->sample_size;
  GST_LOG_OBJECT (audioresample, "resample gave me %d bytes or %d samples",
      outsize, outsamples);

517
  GST_BUFFER_OFFSET (outbuf) = audioresample->offset;
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
  GST_BUFFER_TIMESTAMP (outbuf) = audioresample->next_ts;

  if (audioresample->ts_offset != -1) {
    audioresample->offset += outsamples;
    audioresample->ts_offset += outsamples;
    audioresample->next_ts =
        gst_util_uint64_scale_int (audioresample->ts_offset, GST_SECOND,
        audioresample->o_rate);
    GST_BUFFER_OFFSET_END (outbuf) = audioresample->offset;

    /* we calculate DURATION as the difference between "next" timestamp
     * and current timestamp so we ensure a contiguous stream, instead of
     * having rounding errors. */
    GST_BUFFER_DURATION (outbuf) = audioresample->next_ts -
        GST_BUFFER_TIMESTAMP (outbuf);
  } else {
    /* no valid offset know, we can still sortof calculate the duration though */
    GST_BUFFER_DURATION (outbuf) =
        gst_util_uint64_scale_int (outsamples, GST_SECOND,
        audioresample->o_rate);
  }
539

540
541
542
543
544
545
546
547
548
549
550
551
552
  /* check for possible mem corruption */
  if (outsize > GST_BUFFER_SIZE (outbuf)) {
    /* this is an error that when it happens, would need fixing in the
     * resample library; we told
     * it we wanted only GST_BUFFER_SIZE (outbuf), and it gave us more ! */
    GST_WARNING_OBJECT (audioresample,
        "audioresample, you memory corrupting bastard. "
        "you gave me outsize %d while my buffer was size %d",
        outsize, GST_BUFFER_SIZE (outbuf));
    return GST_FLOW_ERROR;
  }
  /* catch possibly wrong size differences */
  if (GST_BUFFER_SIZE (outbuf) - outsize > r->sample_size) {
553
    GST_WARNING_OBJECT (audioresample,
554
        "audioresample's written outsize %d too far from outbuffer's size %d",
555
556
        outsize, GST_BUFFER_SIZE (outbuf));
  }
557
  GST_BUFFER_SIZE (outbuf) = outsize;
558
559

  return GST_FLOW_OK;
560
561
}

562
static GstFlowReturn
563
audioresample_transform (GstBaseTransform * base, GstBuffer * inbuf,
564
565
566
567
    GstBuffer * outbuf)
{
  GstAudioresample *audioresample;
  ResampleState *r;
568
  guchar *data, *datacopy;
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
  gulong size;
  GstClockTime timestamp;

  audioresample = GST_AUDIORESAMPLE (base);
  r = audioresample->resample;

  data = GST_BUFFER_DATA (inbuf);
  size = GST_BUFFER_SIZE (inbuf);
  timestamp = GST_BUFFER_TIMESTAMP (inbuf);

  GST_DEBUG_OBJECT (audioresample, "got buffer of %ld bytes", size);

  if (audioresample->ts_offset == -1) {
    /* if we don't know the initial offset yet, calculate it based on the 
     * input timestamp. */
    if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
      GstClockTime stime;

      /* offset used to calculate the timestamps. We use the sample offset for this
       * to make it more accurate. We want the first buffer to have the same timestamp
       * as the incomming timestamp. */
      audioresample->next_ts = timestamp;
      audioresample->ts_offset =
          gst_util_uint64_scale_int (timestamp, r->o_rate, GST_SECOND);
      /* offset used to set as the buffer offset, this offset is always relative
       * to the stream time, note that timestamp is not... */
      stime = (timestamp - base->segment.start) + base->segment.time;
      audioresample->offset =
          gst_util_uint64_scale_int (stime, r->o_rate, GST_SECOND);
    }
  }

  /* need to memdup, resample takes ownership. */
602
603
  datacopy = g_memdup (data, size);
  resample_add_input_data (r, datacopy, size, g_free, datacopy);
604
605
606
607
608
609

  return audioresample_do_output (audioresample, outbuf);
}

/* push remaining data in the buffers out */
static GstFlowReturn
610
audioresample_pushthrough (GstAudioresample * audioresample)
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
{
  int outsize;
  ResampleState *r;
  GstBuffer *outbuf;
  GstFlowReturn res = GST_FLOW_OK;
  GstBaseTransform *trans;

  r = audioresample->resample;

  outsize = resample_get_output_size (r);
  if (outsize == 0)
    goto done;

  outbuf = gst_buffer_new_and_alloc (outsize);

  res = audioresample_do_output (audioresample, outbuf);
  if (res != GST_FLOW_OK)
    goto done;

  trans = GST_BASE_TRANSFORM (audioresample);

  res = gst_pad_push (trans->srcpad, outbuf);

done:
  return res;
}


639
static void
640
gst_audioresample_set_property (GObject * object, guint prop_id,
641
642
    const GValue * value, GParamSpec * pspec)
{
643
  GstAudioresample *audioresample;
644

645
  audioresample = GST_AUDIORESAMPLE (object);
646
647

  switch (prop_id) {
648
    case PROP_FILTERLEN:
649
      audioresample->filter_length = g_value_get_int (value);
650
      GST_DEBUG_OBJECT (GST_ELEMENT (audioresample), "new filter length %d",
651
          audioresample->filter_length);
652
653
654
655
      if (audioresample->resample) {
        resample_set_filter_length (audioresample->resample,
            audioresample->filter_length);
      }
656
      break;
657
658
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
659
660
661
662
663
      break;
  }
}

static void
664
gst_audioresample_get_property (GObject * object, guint prop_id,
665
666
    GValue * value, GParamSpec * pspec)
{
667
  GstAudioresample *audioresample;
668
669
670
671

  audioresample = GST_AUDIORESAMPLE (object);

  switch (prop_id) {
672
    case PROP_FILTERLEN:
673
674
675
676
677
678
679
680
681
      g_value_set_int (value, audioresample->filter_length);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}


682
683
static gboolean
plugin_init (GstPlugin * plugin)
684
685
686
687
688
689
690
691
692
693
694
695
696
697
{
  resample_init ();

  if (!gst_element_register (plugin, "audioresample", GST_RANK_PRIMARY,
          GST_TYPE_AUDIORESAMPLE)) {
    return FALSE;
  }

  return TRUE;
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "audioresample",
698
699
    "Resamples audio", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
    GST_PACKAGE_ORIGIN);