43bb1a4f8759e9cb04b6f3065dc84467ef9cccd1
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-base / ext / opus / gstopusdec.c
1 /* GStreamer
2  * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
3  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4  * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5  * Copyright (C) 2011-2012 Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 /*
24  * Based on the speexdec element.
25  */
26
27 /**
28  * SECTION:element-opusdec
29  * @title: opusdec
30  * @see_also: opusenc, oggdemux
31  *
32  * This element decodes a OPUS stream to raw integer audio.
33  *
34  * ## Example pipelines
35  * |[
36  * gst-launch-1.0 -v filesrc location=opus.ogg ! oggdemux ! opusdec ! audioconvert ! audioresample ! alsasink
37  * ]|
38  * Decode an Ogg/Opus file. To create an Ogg/Opus file refer to the documentation of opusenc.
39  *
40  */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include <math.h>
47 #include <string.h>
48 #include <stdio.h>
49 #include "gstopuselements.h"
50 #include "gstopusheader.h"
51 #include "gstopuscommon.h"
52 #include "gstopusdec.h"
53 #include <gst/pbutils/pbutils.h>
54
55 GST_DEBUG_CATEGORY_STATIC (opusdec_debug);
56 #define GST_CAT_DEFAULT opusdec_debug
57
58 static GstStaticPadTemplate opus_dec_src_factory =
59 GST_STATIC_PAD_TEMPLATE ("src",
60     GST_PAD_SRC,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS ("audio/x-raw, "
63         "format = (string) " GST_AUDIO_NE (S16) ", "
64         "layout = (string) interleaved, "
65         "rate = (int) { 48000, 24000, 16000, 12000, 8000 }, "
66         "channels = (int) [ 1, 8 ] ")
67     );
68
69 static GstStaticPadTemplate opus_dec_sink_factory =
70     GST_STATIC_PAD_TEMPLATE ("sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS ("audio/x-opus, "
74         "channel-mapping-family = (int) 0; "
75         "audio/x-opus, "
76         "channel-mapping-family = (int) [1, 255], "
77         "channels = (int) [1, 255], "
78         "stream-count = (int) [1, 255], " "coupled-count = (int) [0, 255]")
79     );
80
81 G_DEFINE_TYPE (GstOpusDec, gst_opus_dec, GST_TYPE_AUDIO_DECODER);
82 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (opusdec, "opusdec",
83     GST_RANK_PRIMARY, GST_TYPE_OPUS_DEC, opus_element_init (plugin));
84
85 #define DB_TO_LINEAR(x) pow (10., (x) / 20.)
86
87 #define DEFAULT_USE_INBAND_FEC FALSE
88 #define DEFAULT_APPLY_GAIN TRUE
89 #define DEFAULT_PHASE_INVERSION FALSE
90
91 enum
92 {
93   PROP_0,
94   PROP_USE_INBAND_FEC,
95   PROP_APPLY_GAIN,
96   PROP_PHASE_INVERSION,
97   PROP_STATS,
98 };
99
100
101 static GstFlowReturn gst_opus_dec_parse_header (GstOpusDec * dec,
102     GstBuffer * buf);
103 static gboolean gst_opus_dec_start (GstAudioDecoder * dec);
104 static gboolean gst_opus_dec_stop (GstAudioDecoder * dec);
105 static GstFlowReturn gst_opus_dec_handle_frame (GstAudioDecoder * dec,
106     GstBuffer * buffer);
107 static gboolean gst_opus_dec_set_format (GstAudioDecoder * bdec,
108     GstCaps * caps);
109 static void gst_opus_dec_get_property (GObject * object, guint prop_id,
110     GValue * value, GParamSpec * pspec);
111 static void gst_opus_dec_set_property (GObject * object, guint prop_id,
112     const GValue * value, GParamSpec * pspec);
113 static GstCaps *gst_opus_dec_getcaps (GstAudioDecoder * dec, GstCaps * filter);
114
115
116 static void
117 gst_opus_dec_class_init (GstOpusDecClass * klass)
118 {
119   GObjectClass *gobject_class;
120   GstAudioDecoderClass *adclass;
121   GstElementClass *element_class;
122
123   gobject_class = (GObjectClass *) klass;
124   adclass = (GstAudioDecoderClass *) klass;
125   element_class = (GstElementClass *) klass;
126
127   gobject_class->set_property = gst_opus_dec_set_property;
128   gobject_class->get_property = gst_opus_dec_get_property;
129
130   adclass->start = GST_DEBUG_FUNCPTR (gst_opus_dec_start);
131   adclass->stop = GST_DEBUG_FUNCPTR (gst_opus_dec_stop);
132   adclass->handle_frame = GST_DEBUG_FUNCPTR (gst_opus_dec_handle_frame);
133   adclass->set_format = GST_DEBUG_FUNCPTR (gst_opus_dec_set_format);
134   adclass->getcaps = GST_DEBUG_FUNCPTR (gst_opus_dec_getcaps);
135
136   gst_element_class_add_static_pad_template (element_class,
137       &opus_dec_src_factory);
138   gst_element_class_add_static_pad_template (element_class,
139       &opus_dec_sink_factory);
140   gst_element_class_set_static_metadata (element_class, "Opus audio decoder",
141       "Codec/Decoder/Audio/Converter", "decode opus streams to audio",
142       "Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>");
143   g_object_class_install_property (gobject_class, PROP_USE_INBAND_FEC,
144       g_param_spec_boolean ("use-inband-fec", "Use in-band FEC",
145           "Use forward error correction if available (needs PLC enabled)",
146           DEFAULT_USE_INBAND_FEC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
147
148   g_object_class_install_property (gobject_class, PROP_APPLY_GAIN,
149       g_param_spec_boolean ("apply-gain", "Apply gain",
150           "Apply gain if any is specified in the header", DEFAULT_APPLY_GAIN,
151           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152
153 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
154   g_object_class_install_property (gobject_class, PROP_PHASE_INVERSION,
155       g_param_spec_boolean ("phase-inversion",
156           "Control Phase Inversion", "Set to true to enable phase inversion, "
157           "this will slightly improve stereo quality, but will have side "
158           "effects when downmixed to mono.", DEFAULT_PHASE_INVERSION,
159           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160
161 #endif
162
163   /**
164    * GstOpusDec:stats:
165    *
166    * Various decoder statistics. This property returns a GstStructure
167    * with name application/x-opusdec-stats with the following fields:
168    *
169    * * #guint64 `num-pushed`: the number of packets pushed out.
170    * * #guint64 `num-gap`: the number of gap packets received.
171    * * #guint64 `plc-num-samples`: the number of samples generated using PLC
172    * * #guint64 `plc-duration`: the total duration, in ns, of samples generated using PLC
173    * * #guint32 `bandwidth`: decoder last bandpass, in kHz, or 0 if unknown
174    * * #guint32 `sample-rate`: decoder sampling rate, or 0 if unknown
175    * * #guint32 `gain`: decoder gain adjustement, in Q8 dB units, or 0 if unknown
176    * * #guint32 `last-packet-duration`: duration, in samples, of the last packet successfully decoded or concealed, or 0 if unknown
177    * * #guint `channels`: the number of channels
178    *
179    * Since: 1.18
180    */
181   g_object_class_install_property (gobject_class, PROP_STATS,
182       g_param_spec_boxed ("stats", "Statistics",
183           "Various statistics", GST_TYPE_STRUCTURE,
184           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
185
186   GST_DEBUG_CATEGORY_INIT (opusdec_debug, "opusdec", 0,
187       "opus decoding element");
188 }
189
190 static void
191 gst_opus_dec_reset (GstOpusDec * dec)
192 {
193   dec->packetno = 0;
194   if (dec->state) {
195     opus_multistream_decoder_destroy (dec->state);
196     dec->state = NULL;
197   }
198
199   gst_buffer_replace (&dec->streamheader, NULL);
200   gst_buffer_replace (&dec->vorbiscomment, NULL);
201   gst_buffer_replace (&dec->last_buffer, NULL);
202   dec->primed = FALSE;
203
204   dec->pre_skip = 0;
205   dec->r128_gain = 0;
206   dec->sample_rate = 0;
207   dec->n_channels = 0;
208   dec->leftover_plc_duration = 0;
209   dec->last_known_buffer_duration = GST_CLOCK_TIME_NONE;
210 }
211
212 static void
213 gst_opus_dec_init (GstOpusDec * dec)
214 {
215   dec->use_inband_fec = FALSE;
216   dec->apply_gain = DEFAULT_APPLY_GAIN;
217   dec->phase_inversion = DEFAULT_PHASE_INVERSION;
218
219   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE);
220   gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
221       (dec), TRUE);
222   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec));
223
224   gst_opus_dec_reset (dec);
225 }
226
227 static gboolean
228 gst_opus_dec_start (GstAudioDecoder * dec)
229 {
230   GstOpusDec *odec = GST_OPUS_DEC (dec);
231
232   gst_opus_dec_reset (odec);
233
234   /* we know about concealment */
235   gst_audio_decoder_set_plc_aware (dec, TRUE);
236
237   if (odec->use_inband_fec) {
238     /* opusdec outputs samples directly from an input buffer, except if
239      * FEC is on, in which case it buffers one buffer in case one buffer
240      * goes missing.
241      */
242     gst_audio_decoder_set_latency (dec, 120 * GST_MSECOND, 120 * GST_MSECOND);
243   }
244
245   GST_OBJECT_LOCK (dec);
246   odec->num_pushed = 0;
247   odec->num_gap = 0;
248   odec->plc_num_samples = 0;
249   odec->plc_duration = 0;
250   GST_OBJECT_UNLOCK (dec);
251
252   return TRUE;
253 }
254
255 static gboolean
256 gst_opus_dec_stop (GstAudioDecoder * dec)
257 {
258   GstOpusDec *odec = GST_OPUS_DEC (dec);
259
260   gst_opus_dec_reset (odec);
261
262   return TRUE;
263 }
264
265 static double
266 gst_opus_dec_get_r128_gain (gint16 r128_gain)
267 {
268   return r128_gain / (double) (1 << 8);
269 }
270
271 static double
272 gst_opus_dec_get_r128_volume (gint16 r128_gain)
273 {
274   return DB_TO_LINEAR (gst_opus_dec_get_r128_gain (r128_gain));
275 }
276
277 static gboolean
278 gst_opus_dec_negotiate (GstOpusDec * dec, const GstAudioChannelPosition * pos)
279 {
280   GstCaps *caps = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (dec));
281   GstStructure *s;
282   GstAudioInfo info;
283
284   if (caps) {
285     gint rate = dec->sample_rate, channels = dec->n_channels;
286     GstCaps *constraint, *inter;
287
288     constraint = gst_caps_from_string ("audio/x-raw");
289     if (dec->n_channels <= 2) { /* including 0 */
290       gst_caps_set_simple (constraint, "channels", GST_TYPE_INT_RANGE, 1, 2,
291           NULL);
292     } else {
293       gst_caps_set_simple (constraint, "channels", G_TYPE_INT, dec->n_channels,
294           NULL);
295     }
296
297     inter = gst_caps_intersect (caps, constraint);
298     gst_caps_unref (constraint);
299
300     if (gst_caps_is_empty (inter)) {
301       GST_DEBUG_OBJECT (dec, "Empty intersection, failed to negotiate");
302       gst_caps_unref (inter);
303       gst_caps_unref (caps);
304       return FALSE;
305     }
306
307     inter = gst_caps_truncate (inter);
308     s = gst_caps_get_structure (inter, 0);
309     rate = dec->sample_rate > 0 ? dec->sample_rate : 48000;
310     gst_structure_fixate_field_nearest_int (s, "rate", dec->sample_rate);
311     gst_structure_get_int (s, "rate", &rate);
312     channels = dec->n_channels > 0 ? dec->n_channels : 2;
313     gst_structure_fixate_field_nearest_int (s, "channels", channels);
314     gst_structure_get_int (s, "channels", &channels);
315
316     gst_caps_unref (inter);
317
318     dec->sample_rate = rate;
319     dec->n_channels = channels;
320     gst_caps_unref (caps);
321   }
322
323   if (dec->n_channels == 0) {
324     GST_DEBUG_OBJECT (dec, "Using a default of 2 channels");
325     dec->n_channels = 2;
326     pos = NULL;
327   }
328
329   if (dec->sample_rate == 0) {
330     GST_DEBUG_OBJECT (dec, "Using a default of 48kHz sample rate");
331     dec->sample_rate = 48000;
332   }
333
334   GST_INFO_OBJECT (dec, "Negotiated %d channels, %d Hz", dec->n_channels,
335       dec->sample_rate);
336
337   /* pass valid order to audio info */
338   if (pos) {
339     memcpy (dec->opus_pos, pos, sizeof (pos[0]) * dec->n_channels);
340     gst_audio_channel_positions_to_valid_order (dec->opus_pos, dec->n_channels);
341   }
342
343   /* set up source format */
344   gst_audio_info_init (&info);
345   gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16,
346       dec->sample_rate, dec->n_channels, pos ? dec->opus_pos : NULL);
347   gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (dec), &info);
348
349   /* but we still need the opus order for later reordering */
350   if (pos) {
351     memcpy (dec->opus_pos, pos, sizeof (pos[0]) * dec->n_channels);
352   } else {
353     dec->opus_pos[0] = GST_AUDIO_CHANNEL_POSITION_INVALID;
354   }
355
356   dec->info = info;
357
358   return TRUE;
359 }
360
361 static GstFlowReturn
362 gst_opus_dec_parse_header (GstOpusDec * dec, GstBuffer * buf)
363 {
364   GstAudioChannelPosition pos[64];
365   const GstAudioChannelPosition *posn = NULL;
366   guint8 n_channels;
367
368   if (!gst_opus_header_is_id_header (buf)) {
369     GST_ELEMENT_ERROR (dec, STREAM, FORMAT, (NULL),
370         ("Header is not an Opus ID header"));
371     return GST_FLOW_ERROR;
372   }
373
374   if (!gst_codec_utils_opus_parse_header (buf,
375           &dec->sample_rate,
376           &n_channels,
377           &dec->channel_mapping_family,
378           &dec->n_streams,
379           &dec->n_stereo_streams,
380           dec->channel_mapping, &dec->pre_skip, &dec->r128_gain)) {
381     GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL),
382         ("Failed to parse Opus ID header"));
383     return GST_FLOW_ERROR;
384   }
385   dec->n_channels = n_channels;
386   dec->r128_gain_volume = gst_opus_dec_get_r128_volume (dec->r128_gain);
387
388   GST_INFO_OBJECT (dec,
389       "Found pre-skip of %u samples, R128 gain %d (volume %f)",
390       dec->pre_skip, dec->r128_gain, dec->r128_gain_volume);
391
392   if (dec->channel_mapping_family == 1) {
393     GST_INFO_OBJECT (dec, "Channel mapping family 1, Vorbis mapping");
394     switch (dec->n_channels) {
395       case 1:
396       case 2:
397         /* nothing */
398         break;
399       case 3:
400       case 4:
401       case 5:
402       case 6:
403       case 7:
404       case 8:
405         posn = gst_opus_channel_positions[dec->n_channels - 1];
406         break;
407       default:{
408         gint i;
409
410         GST_ELEMENT_WARNING (GST_ELEMENT (dec), STREAM, DECODE,
411             (NULL), ("Using NONE channel layout for more than 8 channels"));
412
413         for (i = 0; i < dec->n_channels; i++)
414           pos[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
415
416         posn = pos;
417       }
418     }
419   } else {
420     GST_INFO_OBJECT (dec, "Channel mapping family %d",
421         dec->channel_mapping_family);
422   }
423
424   if (!gst_opus_dec_negotiate (dec, posn))
425     return GST_FLOW_NOT_NEGOTIATED;
426
427   return GST_FLOW_OK;
428 }
429
430
431 static GstFlowReturn
432 gst_opus_dec_parse_comments (GstOpusDec * dec, GstBuffer * buf)
433 {
434   return GST_FLOW_OK;
435 }
436
437 /* adapted from ext/ogg/gstoggstream.c */
438 static gint64
439 packet_duration_opus (const unsigned char *data, size_t bytes)
440 {
441   static const guint64 durations[32] = {
442     480, 960, 1920, 2880,       /* Silk NB */
443     480, 960, 1920, 2880,       /* Silk MB */
444     480, 960, 1920, 2880,       /* Silk WB */
445     480, 960,                   /* Hybrid SWB */
446     480, 960,                   /* Hybrid FB */
447     120, 240, 480, 960,         /* CELT NB */
448     120, 240, 480, 960,         /* CELT NB */
449     120, 240, 480, 960,         /* CELT NB */
450     120, 240, 480, 960,         /* CELT NB */
451   };
452
453   gint64 duration;
454   gint64 frame_duration;
455   gint nframes = 0;
456   guint8 toc;
457
458   if (bytes < 1)
459     return 0;
460
461   /* headers */
462   if (bytes >= 8 && !memcmp (data, "Opus", 4))
463     return 0;
464
465   toc = data[0];
466
467   frame_duration = durations[toc >> 3];
468   switch (toc & 3) {
469     case 0:
470       nframes = 1;
471       break;
472     case 1:
473       nframes = 2;
474       break;
475     case 2:
476       nframes = 2;
477       break;
478     case 3:
479       if (bytes < 2) {
480         GST_WARNING ("Code 3 Opus packet has less than 2 bytes");
481         return 0;
482       }
483       nframes = data[1] & 63;
484       break;
485   }
486
487   duration = nframes * frame_duration;
488   if (duration > 5760) {
489     GST_WARNING ("Opus packet duration > 120 ms, invalid");
490     return 0;
491   }
492   GST_LOG ("Opus packet: frame size %.1f ms, %d frames, duration %.1f ms",
493       frame_duration / 48.f, nframes, duration / 48.f);
494   return duration / 48.f * 1000000;
495 }
496
497 static GstFlowReturn
498 opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buffer)
499 {
500   GstFlowReturn res = GST_FLOW_OK;
501   gsize size;
502   guint8 *data;
503   GstBuffer *outbuf, *bufd;
504   gint16 *out_data;
505   int n, err;
506   int samples;
507   unsigned int packet_size;
508   GstBuffer *buf;
509   GstMapInfo map, omap;
510   GstAudioClippingMeta *cmeta = NULL;
511
512   if (dec->state == NULL) {
513     /* If we did not get any headers, default to 2 channels */
514     if (dec->n_channels == 0) {
515       GST_INFO_OBJECT (dec, "No header, assuming single stream");
516       dec->n_channels = 2;
517       dec->sample_rate = 48000;
518       /* default stereo mapping */
519       dec->channel_mapping_family = 0;
520       dec->channel_mapping[0] = 0;
521       dec->channel_mapping[1] = 1;
522       dec->n_streams = 1;
523       dec->n_stereo_streams = 1;
524
525       if (!gst_opus_dec_negotiate (dec, NULL))
526         return GST_FLOW_NOT_NEGOTIATED;
527     }
528
529     if (dec->n_channels == 2 && dec->n_streams == 1
530         && dec->n_stereo_streams == 0) {
531       /* if we are automatically decoding 2 channels, but only have
532          a single encoded one, direct both channels to it */
533       dec->channel_mapping[1] = 0;
534     }
535
536     GST_DEBUG_OBJECT (dec, "Creating decoder with %d channels, %d Hz",
537         dec->n_channels, dec->sample_rate);
538 #ifndef GST_DISABLE_GST_DEBUG
539     gst_opus_common_log_channel_mapping_table (GST_ELEMENT (dec), opusdec_debug,
540         "Mapping table", dec->n_channels, dec->channel_mapping);
541 #endif
542
543     GST_DEBUG_OBJECT (dec, "%d streams, %d stereo", dec->n_streams,
544         dec->n_stereo_streams);
545     dec->state =
546         opus_multistream_decoder_create (dec->sample_rate, dec->n_channels,
547         dec->n_streams, dec->n_stereo_streams, dec->channel_mapping, &err);
548     if (!dec->state || err != OPUS_OK)
549       goto creation_failed;
550
551 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
552     {
553       int err;
554       err = opus_multistream_decoder_ctl (dec->state,
555           OPUS_SET_PHASE_INVERSION_DISABLED (!dec->phase_inversion));
556       if (err != OPUS_OK)
557         GST_WARNING_OBJECT (dec, "Could not configure phase inversion: %s",
558             opus_strerror (err));
559     }
560 #else
561     GST_WARNING_OBJECT (dec, "Phase inversion request is not support by this "
562         "version of the Opus Library");
563 #endif
564   }
565
566   if (buffer) {
567     GST_DEBUG_OBJECT (dec, "Received buffer of size %" G_GSIZE_FORMAT,
568         gst_buffer_get_size (buffer));
569   } else {
570     GST_DEBUG_OBJECT (dec, "Received missing buffer");
571   }
572
573   /* if using in-band FEC, we introdude one extra frame's delay as we need
574      to potentially wait for next buffer to decode a missing buffer */
575   if (dec->use_inband_fec && !dec->primed) {
576     GST_DEBUG_OBJECT (dec, "First buffer received in FEC mode, early out");
577     gst_buffer_replace (&dec->last_buffer, buffer);
578     dec->primed = TRUE;
579     goto done;
580   }
581
582   /* That's the buffer we'll be sending to the opus decoder. */
583   buf = (dec->use_inband_fec
584       && gst_buffer_get_size (dec->last_buffer) >
585       0) ? dec->last_buffer : buffer;
586
587   /* That's the buffer we get duration from */
588   bufd = dec->use_inband_fec ? dec->last_buffer : buffer;
589
590   if (buf && gst_buffer_get_size (buf) > 0) {
591     gst_buffer_map (buf, &map, GST_MAP_READ);
592     data = map.data;
593     size = map.size;
594     GST_DEBUG_OBJECT (dec, "Using buffer of size %" G_GSIZE_FORMAT, size);
595   } else {
596     /* concealment data, pass NULL as the bits parameters */
597     GST_DEBUG_OBJECT (dec, "Using NULL buffer");
598     data = NULL;
599     size = 0;
600   }
601
602   if (gst_buffer_get_size (bufd) == 0) {
603     GstClockTime const opus_plc_alignment = 2500 * GST_USECOND;
604     GstClockTime aligned_missing_duration;
605     GstClockTime missing_duration = GST_BUFFER_DURATION (bufd);
606
607     if (!GST_CLOCK_TIME_IS_VALID (missing_duration) || missing_duration == 0) {
608       if (GST_CLOCK_TIME_IS_VALID (dec->last_known_buffer_duration)) {
609         missing_duration = dec->last_known_buffer_duration;
610         GST_WARNING_OBJECT (dec,
611             "Missing duration, using last duration %" GST_TIME_FORMAT,
612             GST_TIME_ARGS (missing_duration));
613       } else {
614         GST_WARNING_OBJECT (dec,
615             "Missing buffer, but unknown duration, and no previously known duration, assuming 20 ms");
616         missing_duration = 20 * GST_MSECOND;
617       }
618     }
619
620     GST_DEBUG_OBJECT (dec,
621         "missing buffer, doing PLC duration %" GST_TIME_FORMAT
622         " plus leftover %" GST_TIME_FORMAT, GST_TIME_ARGS (missing_duration),
623         GST_TIME_ARGS (dec->leftover_plc_duration));
624
625     GST_OBJECT_LOCK (dec);
626     dec->num_gap++;
627     GST_OBJECT_UNLOCK (dec);
628
629     /* add the leftover PLC duration to that of the buffer */
630     missing_duration += dec->leftover_plc_duration;
631
632     /* align the combined buffer and leftover PLC duration to multiples
633      * of 2.5ms, rounding to nearest, and store excess duration for later */
634     aligned_missing_duration =
635         ((missing_duration +
636             opus_plc_alignment / 2) / opus_plc_alignment) * opus_plc_alignment;
637     dec->leftover_plc_duration = missing_duration - aligned_missing_duration;
638
639     /* Opus' PLC cannot operate with less than 2.5ms; skip PLC
640      * and accumulate the missing duration in the leftover_plc_duration
641      * for the next PLC attempt */
642     if (aligned_missing_duration < opus_plc_alignment) {
643       GST_DEBUG_OBJECT (dec,
644           "current duration %" GST_TIME_FORMAT
645           " of missing data not enough for PLC (minimum needed: %"
646           GST_TIME_FORMAT ") - skipping", GST_TIME_ARGS (missing_duration),
647           GST_TIME_ARGS (opus_plc_alignment));
648       goto done;
649     }
650
651     /* convert the duration (in nanoseconds) to sample count */
652     samples =
653         gst_util_uint64_scale_int (aligned_missing_duration, dec->sample_rate,
654         GST_SECOND);
655
656     GST_DEBUG_OBJECT (dec,
657         "calculated PLC frame length: %" GST_TIME_FORMAT
658         " num frame samples: %d new leftover: %" GST_TIME_FORMAT,
659         GST_TIME_ARGS (aligned_missing_duration), samples,
660         GST_TIME_ARGS (dec->leftover_plc_duration));
661
662     GST_OBJECT_LOCK (dec);
663     dec->plc_num_samples += samples;
664     dec->plc_duration += aligned_missing_duration;
665     GST_OBJECT_UNLOCK (dec);
666   } else {
667     /* use maximum size (120 ms) as the number of returned samples is
668        not constant over the stream. */
669     samples = 120 * dec->sample_rate / 1000;
670   }
671   packet_size = samples * dec->n_channels * 2;
672
673   outbuf =
674       gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (dec),
675       packet_size);
676   if (!outbuf) {
677     goto buffer_failed;
678   }
679
680   if (size > 0)
681     dec->last_known_buffer_duration = packet_duration_opus (data, size);
682
683   gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
684   out_data = (gint16 *) omap.data;
685
686   do {
687     if (dec->use_inband_fec) {
688       if (gst_buffer_get_size (dec->last_buffer) > 0) {
689         /* normal delayed decode */
690         GST_LOG_OBJECT (dec, "FEC enabled, decoding last delayed buffer");
691         n = opus_multistream_decode (dec->state, data, size, out_data, samples,
692             0);
693       } else {
694         /* FEC reconstruction decode */
695         GST_LOG_OBJECT (dec, "FEC enabled, reconstructing last buffer");
696         n = opus_multistream_decode (dec->state, data, size, out_data, samples,
697             1);
698       }
699     } else {
700       /* normal decode */
701       GST_LOG_OBJECT (dec, "FEC disabled, decoding buffer");
702       n = opus_multistream_decode (dec->state, data, size, out_data, samples,
703           0);
704     }
705     if (n == OPUS_BUFFER_TOO_SMALL) {
706       /* if too small, add 2.5 milliseconds and try again, up to the
707        * Opus max size of 120 milliseconds */
708       if (samples >= 120 * dec->sample_rate / 1000)
709         break;
710       samples += 25 * dec->sample_rate / 10000;
711       packet_size = samples * dec->n_channels * 2;
712       gst_buffer_unmap (outbuf, &omap);
713       gst_buffer_unref (outbuf);
714       outbuf =
715           gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (dec),
716           packet_size);
717       if (!outbuf) {
718         goto buffer_failed;
719       }
720       gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
721       out_data = (gint16 *) omap.data;
722     }
723   } while (n == OPUS_BUFFER_TOO_SMALL);
724   gst_buffer_unmap (outbuf, &omap);
725   if (data != NULL)
726     gst_buffer_unmap (buf, &map);
727
728   if (n < 0) {
729     GstFlowReturn ret = GST_FLOW_ERROR;
730
731     gst_buffer_unref (outbuf);
732     GST_AUDIO_DECODER_ERROR (dec, 1, STREAM, DECODE, (NULL),
733         ("Decoding error (%d): %s", n, opus_strerror (n)), ret);
734     return ret;
735   }
736   GST_DEBUG_OBJECT (dec, "decoded %d samples", n);
737   gst_buffer_set_size (outbuf, n * 2 * dec->n_channels);
738   GST_BUFFER_DURATION (outbuf) = samples * GST_SECOND / dec->sample_rate;
739   samples = n;
740
741   cmeta = gst_buffer_get_audio_clipping_meta (buf);
742
743   g_assert (!cmeta || cmeta->format == GST_FORMAT_DEFAULT);
744
745   /* Skip any samples that need skipping */
746   if (cmeta && cmeta->start) {
747     guint pre_skip = cmeta->start;
748     guint scaled_pre_skip = pre_skip * dec->sample_rate / 48000;
749     guint skip = scaled_pre_skip > n ? n : scaled_pre_skip;
750     guint scaled_skip = skip * 48000 / dec->sample_rate;
751
752     gst_buffer_resize (outbuf, skip * 2 * dec->n_channels, -1);
753
754     GST_INFO_OBJECT (dec,
755         "Skipping %u samples at the beginning (%u at 48000 Hz)",
756         skip, scaled_skip);
757   }
758
759   if (cmeta && cmeta->end) {
760     guint post_skip = cmeta->end;
761     guint scaled_post_skip = post_skip * dec->sample_rate / 48000;
762     guint skip = scaled_post_skip > n ? n : scaled_post_skip;
763     guint scaled_skip = skip * 48000 / dec->sample_rate;
764     guint outsize = gst_buffer_get_size (outbuf);
765     guint skip_bytes = skip * 2 * dec->n_channels;
766
767     if (outsize > skip_bytes)
768       outsize -= skip_bytes;
769     else
770       outsize = 0;
771
772     gst_buffer_resize (outbuf, 0, outsize);
773
774     GST_INFO_OBJECT (dec,
775         "Skipping %u samples at the end (%u at 48000 Hz)", skip, scaled_skip);
776   }
777
778   if (gst_buffer_get_size (outbuf) == 0) {
779     gst_buffer_unref (outbuf);
780     outbuf = NULL;
781   } else if (dec->opus_pos[0] != GST_AUDIO_CHANNEL_POSITION_INVALID) {
782     gst_audio_buffer_reorder_channels (outbuf, GST_AUDIO_FORMAT_S16,
783         dec->n_channels, dec->opus_pos, dec->info.position);
784   }
785
786   /* Apply gain */
787   /* Would be better off leaving this to a volume element, as this is
788      a naive conversion that does too many int/float conversions.
789      However, we don't have control over the pipeline...
790      So make it optional if the user program wants to use a volume,
791      but do it by default so the correct volume goes out by default */
792   if (dec->apply_gain && outbuf && dec->r128_gain) {
793     gsize rsize;
794     unsigned int i, nsamples;
795     double volume = dec->r128_gain_volume;
796     gint16 *samples;
797
798     gst_buffer_map (outbuf, &omap, GST_MAP_READWRITE);
799     samples = (gint16 *) omap.data;
800     rsize = omap.size;
801     GST_DEBUG_OBJECT (dec, "Applying gain: volume %f", volume);
802     nsamples = rsize / 2;
803     for (i = 0; i < nsamples; ++i) {
804       int sample = (int) (samples[i] * volume + 0.5);
805       samples[i] = sample < -32768 ? -32768 : sample > 32767 ? 32767 : sample;
806     }
807     gst_buffer_unmap (outbuf, &omap);
808   }
809
810   if (dec->use_inband_fec) {
811     gst_buffer_replace (&dec->last_buffer, buffer);
812   }
813
814   GST_OBJECT_LOCK (dec);
815   dec->num_pushed++;
816   GST_OBJECT_UNLOCK (dec);
817
818   res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
819
820   if (res != GST_FLOW_OK)
821     GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
822
823 done:
824   return res;
825
826 creation_failed:
827   GST_ELEMENT_ERROR (dec, LIBRARY, INIT, ("Failed to create Opus decoder"),
828       ("Failed to create Opus decoder (%d): %s", err, opus_strerror (err)));
829   return GST_FLOW_ERROR;
830
831 buffer_failed:
832   GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL),
833       ("Failed to create %u byte buffer", packet_size));
834   return GST_FLOW_ERROR;
835 }
836
837 static gboolean
838 gst_opus_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
839 {
840   GstOpusDec *dec = GST_OPUS_DEC (bdec);
841   gboolean ret = TRUE;
842   GstStructure *s;
843   const GValue *streamheader;
844   GstCaps *old_caps;
845
846   GST_DEBUG_OBJECT (dec, "set_format: %" GST_PTR_FORMAT, caps);
847
848   if ((old_caps = gst_pad_get_current_caps (GST_AUDIO_DECODER_SINK_PAD (bdec)))) {
849     if (gst_caps_is_equal (caps, old_caps)) {
850       gst_caps_unref (old_caps);
851       GST_DEBUG_OBJECT (dec, "caps didn't change");
852       goto done;
853     }
854
855     GST_DEBUG_OBJECT (dec, "caps have changed, resetting decoder");
856     gst_opus_dec_reset (dec);
857     gst_caps_unref (old_caps);
858   }
859
860   s = gst_caps_get_structure (caps, 0);
861   if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
862       G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
863       gst_value_array_get_size (streamheader) >= 2) {
864     const GValue *header, *vorbiscomment;
865     GstBuffer *buf;
866     GstFlowReturn res = GST_FLOW_OK;
867
868     header = gst_value_array_get_value (streamheader, 0);
869     if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
870       buf = gst_value_get_buffer (header);
871       res = gst_opus_dec_parse_header (dec, buf);
872       if (res != GST_FLOW_OK) {
873         ret = FALSE;
874         goto done;
875       }
876       gst_buffer_replace (&dec->streamheader, buf);
877     }
878
879     vorbiscomment = gst_value_array_get_value (streamheader, 1);
880     if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
881       buf = gst_value_get_buffer (vorbiscomment);
882       res = gst_opus_dec_parse_comments (dec, buf);
883       if (res != GST_FLOW_OK) {
884         ret = FALSE;
885         goto done;
886       }
887       gst_buffer_replace (&dec->vorbiscomment, buf);
888     }
889   } else {
890     const GstAudioChannelPosition *posn = NULL;
891     guint8 n_channels;
892
893     if (!gst_codec_utils_opus_parse_caps (caps, &dec->sample_rate,
894             &n_channels, &dec->channel_mapping_family,
895             &dec->n_streams, &dec->n_stereo_streams, dec->channel_mapping)) {
896       ret = FALSE;
897       goto done;
898     }
899     dec->n_channels = n_channels;
900
901     if (dec->channel_mapping_family == 1 && dec->n_channels <= 8)
902       posn = gst_opus_channel_positions[dec->n_channels - 1];
903
904     if (!gst_opus_dec_negotiate (dec, posn))
905       return FALSE;
906   }
907
908 done:
909   return ret;
910 }
911
912 static gboolean
913 memcmp_buffers (GstBuffer * buf1, GstBuffer * buf2)
914 {
915   gsize size1, size2;
916   gboolean res;
917   GstMapInfo map;
918
919   size1 = gst_buffer_get_size (buf1);
920   size2 = gst_buffer_get_size (buf2);
921
922   if (size1 != size2)
923     return FALSE;
924
925   gst_buffer_map (buf1, &map, GST_MAP_READ);
926   res = gst_buffer_memcmp (buf2, 0, map.data, map.size) == 0;
927   gst_buffer_unmap (buf1, &map);
928
929   return res;
930 }
931
932 static GstFlowReturn
933 gst_opus_dec_handle_frame (GstAudioDecoder * adec, GstBuffer * buf)
934 {
935   GstFlowReturn res;
936   GstOpusDec *dec;
937
938   /* no fancy draining */
939   if (G_UNLIKELY (!buf))
940     return GST_FLOW_OK;
941
942   dec = GST_OPUS_DEC (adec);
943   GST_LOG_OBJECT (dec,
944       "Got buffer ts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
945       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
946       GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
947
948   /* If we have the streamheader and vorbiscomment from the caps already
949    * ignore them here */
950   if (dec->streamheader && dec->vorbiscomment) {
951     if (memcmp_buffers (dec->streamheader, buf)) {
952       GST_DEBUG_OBJECT (dec, "found streamheader");
953       gst_audio_decoder_finish_frame (adec, NULL, 1);
954       res = GST_FLOW_OK;
955     } else if (memcmp_buffers (dec->vorbiscomment, buf)) {
956       GST_DEBUG_OBJECT (dec, "found vorbiscomments");
957       gst_audio_decoder_finish_frame (adec, NULL, 1);
958       res = GST_FLOW_OK;
959     } else {
960       res = opus_dec_chain_parse_data (dec, buf);
961     }
962   } else {
963     /* Otherwise fall back to packet counting and assume that the
964      * first two packets might be the headers, checking magic. */
965     switch (dec->packetno) {
966       case 0:
967         if (gst_opus_header_is_header (buf, "OpusHead", 8)) {
968           GST_DEBUG_OBJECT (dec, "found streamheader");
969           res = gst_opus_dec_parse_header (dec, buf);
970           gst_audio_decoder_finish_frame (adec, NULL, 1);
971         } else {
972           res = opus_dec_chain_parse_data (dec, buf);
973         }
974         break;
975       case 1:
976         if (gst_opus_header_is_header (buf, "OpusTags", 8)) {
977           GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
978           res = gst_opus_dec_parse_comments (dec, buf);
979           gst_audio_decoder_finish_frame (adec, NULL, 1);
980         } else {
981           res = opus_dec_chain_parse_data (dec, buf);
982         }
983         break;
984       default:
985       {
986         res = opus_dec_chain_parse_data (dec, buf);
987         break;
988       }
989     }
990   }
991
992   dec->packetno++;
993
994   return res;
995 }
996
997 /* Called with object lock hold */
998 static guint32
999 get_bandwidth (GstOpusDec * self)
1000 {
1001   gint err;
1002   gint32 bw;
1003
1004   if (!self->state)
1005     return 0;
1006
1007   err = opus_multistream_decoder_ctl (self->state, OPUS_GET_BANDWIDTH (&bw));
1008   if (err != OPUS_OK) {
1009     GST_WARNING_OBJECT (self, "Could not retrieve bandwith: %s",
1010         opus_strerror (err));
1011     return 0;
1012   }
1013
1014   switch (bw) {
1015     case OPUS_BANDWIDTH_NARROWBAND:
1016       return 4;
1017     case OPUS_BANDWIDTH_MEDIUMBAND:
1018       return 6;
1019     case OPUS_BANDWIDTH_WIDEBAND:
1020       return 8;
1021     case OPUS_BANDWIDTH_SUPERWIDEBAND:
1022       return 12;
1023     case OPUS_BANDWIDTH_FULLBAND:
1024       return 20;
1025     default:
1026       GST_WARNING_OBJECT (self, "Unknown bandwith enum: %d", bw);
1027       return 0;
1028   }
1029 }
1030
1031 /* Called with object lock hold */
1032 static guint32
1033 get_sample_rate (GstOpusDec * self)
1034 {
1035   gint err;
1036   gint32 rate;
1037
1038   if (!self->state)
1039     return 0;
1040
1041   err =
1042       opus_multistream_decoder_ctl (self->state, OPUS_GET_SAMPLE_RATE (&rate));
1043   if (err != OPUS_OK) {
1044     GST_WARNING_OBJECT (self, "Could not retrieve sample rate: %s",
1045         opus_strerror (err));
1046     return 0;
1047   }
1048
1049   return rate;
1050 }
1051
1052 /* Called with object lock hold */
1053 static guint32
1054 get_gain (GstOpusDec * self)
1055 {
1056   gint err;
1057   gint32 gain;
1058
1059   if (!self->state)
1060     return 0;
1061
1062   err = opus_multistream_decoder_ctl (self->state, OPUS_GET_GAIN (&gain));
1063   if (err != OPUS_OK) {
1064     GST_WARNING_OBJECT (self, "Could not retrieve gain: %s",
1065         opus_strerror (err));
1066     return 0;
1067   }
1068
1069   return gain;
1070 }
1071
1072 /* Called with object lock hold */
1073 static guint32
1074 get_last_packet_duration (GstOpusDec * self)
1075 {
1076   gint err;
1077   gint32 duration;
1078
1079   if (!self->state)
1080     return 0;
1081
1082   err =
1083       opus_multistream_decoder_ctl (self->state,
1084       OPUS_GET_LAST_PACKET_DURATION (&duration));
1085   if (err != OPUS_OK) {
1086     GST_WARNING_OBJECT (self, "Could not retrieve last packet duration: %s",
1087         opus_strerror (err));
1088     return 0;
1089   }
1090
1091   return duration;
1092 }
1093
1094 static GstStructure *
1095 gst_opus_dec_create_stats (GstOpusDec * self)
1096 {
1097   GstStructure *s;
1098
1099   GST_OBJECT_LOCK (self);
1100
1101   s = gst_structure_new ("application/x-opusdec-stats",
1102       "num-pushed", G_TYPE_UINT64, self->num_pushed,
1103       "num-gap", G_TYPE_UINT64, self->num_gap,
1104       "plc-num-samples", G_TYPE_UINT64, self->plc_num_samples,
1105       "plc-duration", G_TYPE_UINT64, self->plc_duration,
1106       "bandwidth", G_TYPE_UINT, get_bandwidth (self),
1107       "sample-rate", G_TYPE_UINT, get_sample_rate (self),
1108       "gain", G_TYPE_UINT, get_gain (self),
1109       "last-packet-duration", G_TYPE_UINT, get_last_packet_duration (self),
1110       "channels", G_TYPE_UINT, self->n_channels, NULL);
1111
1112   GST_OBJECT_UNLOCK (self);
1113
1114   return s;
1115 }
1116
1117 static void
1118 gst_opus_dec_get_property (GObject * object, guint prop_id, GValue * value,
1119     GParamSpec * pspec)
1120 {
1121   GstOpusDec *dec = GST_OPUS_DEC (object);
1122
1123   switch (prop_id) {
1124     case PROP_USE_INBAND_FEC:
1125       g_value_set_boolean (value, dec->use_inband_fec);
1126       break;
1127     case PROP_APPLY_GAIN:
1128       g_value_set_boolean (value, dec->apply_gain);
1129       break;
1130     case PROP_PHASE_INVERSION:
1131       g_value_set_boolean (value, dec->phase_inversion);
1132       break;
1133     case PROP_STATS:
1134       g_value_take_boxed (value, gst_opus_dec_create_stats (dec));
1135       break;
1136     default:
1137       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1138       break;
1139   }
1140 }
1141
1142 static void
1143 gst_opus_dec_set_property (GObject * object, guint prop_id,
1144     const GValue * value, GParamSpec * pspec)
1145 {
1146   GstOpusDec *dec = GST_OPUS_DEC (object);
1147
1148   switch (prop_id) {
1149     case PROP_USE_INBAND_FEC:
1150       dec->use_inband_fec = g_value_get_boolean (value);
1151       break;
1152     case PROP_APPLY_GAIN:
1153       dec->apply_gain = g_value_get_boolean (value);
1154       break;
1155     case PROP_PHASE_INVERSION:
1156       dec->phase_inversion = g_value_get_boolean (value);
1157       break;
1158     default:
1159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1160       break;
1161   }
1162 }
1163
1164 /* caps must be writable */
1165 static void
1166 gst_opus_dec_caps_extend_channels_options (GstCaps * caps)
1167 {
1168   unsigned n;
1169   int channels;
1170
1171   for (n = 0; n < gst_caps_get_size (caps); ++n) {
1172     GstStructure *s = gst_caps_get_structure (caps, n);
1173     if (gst_structure_get_int (s, "channels", &channels)) {
1174       if (channels == 1 || channels == 2) {
1175         GValue v = { 0 };
1176         g_value_init (&v, GST_TYPE_INT_RANGE);
1177         gst_value_set_int_range (&v, 1, 2);
1178         gst_structure_set_value (s, "channels", &v);
1179         g_value_unset (&v);
1180       }
1181     }
1182   }
1183 }
1184
1185 static void
1186 gst_opus_dec_value_list_append_int (GValue * list, gint i)
1187 {
1188   GValue v = { 0 };
1189
1190   g_value_init (&v, G_TYPE_INT);
1191   g_value_set_int (&v, i);
1192   gst_value_list_append_value (list, &v);
1193   g_value_unset (&v);
1194 }
1195
1196 static void
1197 gst_opus_dec_caps_extend_rate_options (GstCaps * caps)
1198 {
1199   unsigned n;
1200   GValue v = { 0 };
1201
1202   g_value_init (&v, GST_TYPE_LIST);
1203   gst_opus_dec_value_list_append_int (&v, 48000);
1204   gst_opus_dec_value_list_append_int (&v, 24000);
1205   gst_opus_dec_value_list_append_int (&v, 16000);
1206   gst_opus_dec_value_list_append_int (&v, 12000);
1207   gst_opus_dec_value_list_append_int (&v, 8000);
1208
1209   for (n = 0; n < gst_caps_get_size (caps); ++n) {
1210     GstStructure *s = gst_caps_get_structure (caps, n);
1211
1212     gst_structure_set_value (s, "rate", &v);
1213   }
1214   g_value_unset (&v);
1215 }
1216
1217 GstCaps *
1218 gst_opus_dec_getcaps (GstAudioDecoder * dec, GstCaps * filter)
1219 {
1220   GstCaps *caps, *proxy_filter = NULL, *ret;
1221
1222   if (filter) {
1223     proxy_filter = gst_caps_copy (filter);
1224     gst_opus_dec_caps_extend_channels_options (proxy_filter);
1225     gst_opus_dec_caps_extend_rate_options (proxy_filter);
1226   }
1227   caps = gst_audio_decoder_proxy_getcaps (dec, NULL, proxy_filter);
1228   if (proxy_filter)
1229     gst_caps_unref (proxy_filter);
1230   if (caps) {
1231     caps = gst_caps_make_writable (caps);
1232     gst_opus_dec_caps_extend_channels_options (caps);
1233     gst_opus_dec_caps_extend_rate_options (caps);
1234   }
1235
1236   if (filter) {
1237     ret = gst_caps_intersect (caps, filter);
1238     gst_caps_unref (caps);
1239   } else {
1240     ret = caps;
1241   }
1242   return ret;
1243 }