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