audioencoders: chain up to parent event handler
[platform/upstream/gstreamer.git] / ext / opus / gstopusenc.c
1 /* GStreamer Opus Encoder
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2008> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  * Copyright (C) <2011> Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /*
23  * Based on the speexenc element
24  */
25
26 /**
27  * SECTION:element-opusenc
28  * @see_also: opusdec, oggmux
29  *
30  * This element encodes raw audio to OPUS.
31  *
32  * <refsect2>
33  * <title>Example pipelines</title>
34  * |[
35  * gst-launch -v audiotestsrc wave=sine num-buffers=100 ! audioconvert ! opusenc ! oggmux ! filesink location=sine.ogg
36  * ]| Encode a test sine signal to Ogg/OPUS.
37  * </refsect2>
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <math.h>
47 #include <opus/opus.h>
48
49 #include <gst/gsttagsetter.h>
50 #include <gst/audio/audio.h>
51 #include <gst/glib-compat-private.h>
52 #include "gstopusheader.h"
53 #include "gstopuscommon.h"
54 #include "gstopusenc.h"
55
56 GST_DEBUG_CATEGORY_STATIC (opusenc_debug);
57 #define GST_CAT_DEFAULT opusenc_debug
58
59 /* Some arbitrary bounds beyond which it really doesn't make sense.
60    The spec mentions 6 kb/s to 510 kb/s, so 4000 and 650000 ought to be
61    safe as property bounds. */
62 #define LOWEST_BITRATE 4000
63 #define HIGHEST_BITRATE 650000
64
65 #define GST_OPUS_ENC_TYPE_BANDWIDTH (gst_opus_enc_bandwidth_get_type())
66 static GType
67 gst_opus_enc_bandwidth_get_type (void)
68 {
69   static const GEnumValue values[] = {
70     {OPUS_BANDWIDTH_NARROWBAND, "Narrow band", "narrowband"},
71     {OPUS_BANDWIDTH_MEDIUMBAND, "Medium band", "mediumband"},
72     {OPUS_BANDWIDTH_WIDEBAND, "Wide band", "wideband"},
73     {OPUS_BANDWIDTH_SUPERWIDEBAND, "Super wide band", "superwideband"},
74     {OPUS_BANDWIDTH_FULLBAND, "Full band", "fullband"},
75     {OPUS_AUTO, "Auto", "auto"},
76     {0, NULL, NULL}
77   };
78   static volatile GType id = 0;
79
80   if (g_once_init_enter ((gsize *) & id)) {
81     GType _id;
82
83     _id = g_enum_register_static ("GstOpusEncBandwidth", values);
84
85     g_once_init_leave ((gsize *) & id, _id);
86   }
87
88   return id;
89 }
90
91 #define GST_OPUS_ENC_TYPE_FRAME_SIZE (gst_opus_enc_frame_size_get_type())
92 static GType
93 gst_opus_enc_frame_size_get_type (void)
94 {
95   static const GEnumValue values[] = {
96     {2, "2.5", "2.5"},
97     {5, "5", "5"},
98     {10, "10", "10"},
99     {20, "20", "20"},
100     {40, "40", "40"},
101     {60, "60", "60"},
102     {0, NULL, NULL}
103   };
104   static volatile GType id = 0;
105
106   if (g_once_init_enter ((gsize *) & id)) {
107     GType _id;
108
109     _id = g_enum_register_static ("GstOpusEncFrameSize", values);
110
111     g_once_init_leave ((gsize *) & id, _id);
112   }
113
114   return id;
115 }
116
117 #define FORMAT_STR GST_AUDIO_NE(S16)
118 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
119     GST_PAD_SINK,
120     GST_PAD_ALWAYS,
121     GST_STATIC_CAPS ("audio/x-raw, "
122         "format = (string) " FORMAT_STR ", "
123         "rate = (int) { 8000, 12000, 16000, 24000, 48000 }, "
124         "channels = (int) [ 1, 2 ] ")
125     );
126
127 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
128     GST_PAD_SRC,
129     GST_PAD_ALWAYS,
130     GST_STATIC_CAPS ("audio/x-opus")
131     );
132
133 #define DEFAULT_AUDIO           TRUE
134 #define DEFAULT_BITRATE         64000
135 #define DEFAULT_BANDWIDTH       OPUS_BANDWIDTH_FULLBAND
136 #define DEFAULT_FRAMESIZE       20
137 #define DEFAULT_CBR             TRUE
138 #define DEFAULT_CONSTRAINED_VBR TRUE
139 #define DEFAULT_COMPLEXITY      10
140 #define DEFAULT_INBAND_FEC      FALSE
141 #define DEFAULT_DTX             FALSE
142 #define DEFAULT_PACKET_LOSS_PERCENT 0
143 #define DEFAULT_MAX_PAYLOAD_SIZE 1024
144
145 enum
146 {
147   PROP_0,
148   PROP_AUDIO,
149   PROP_BITRATE,
150   PROP_BANDWIDTH,
151   PROP_FRAME_SIZE,
152   PROP_CBR,
153   PROP_CONSTRAINED_VBR,
154   PROP_COMPLEXITY,
155   PROP_INBAND_FEC,
156   PROP_DTX,
157   PROP_PACKET_LOSS_PERCENT,
158   PROP_MAX_PAYLOAD_SIZE
159 };
160
161 static void gst_opus_enc_finalize (GObject * object);
162
163 static gboolean gst_opus_enc_sink_event (GstAudioEncoder * benc,
164     GstEvent * event);
165 static GstCaps *gst_opus_enc_sink_getcaps (GstAudioEncoder * benc,
166     GstCaps * filter);
167 static gboolean gst_opus_enc_setup (GstOpusEnc * enc);
168
169 static void gst_opus_enc_get_property (GObject * object, guint prop_id,
170     GValue * value, GParamSpec * pspec);
171 static void gst_opus_enc_set_property (GObject * object, guint prop_id,
172     const GValue * value, GParamSpec * pspec);
173
174 static gboolean gst_opus_enc_start (GstAudioEncoder * benc);
175 static gboolean gst_opus_enc_stop (GstAudioEncoder * benc);
176 static gboolean gst_opus_enc_set_format (GstAudioEncoder * benc,
177     GstAudioInfo * info);
178 static GstFlowReturn gst_opus_enc_handle_frame (GstAudioEncoder * benc,
179     GstBuffer * buf);
180 static gint64 gst_opus_enc_get_latency (GstOpusEnc * enc);
181
182 static GstFlowReturn gst_opus_enc_encode (GstOpusEnc * enc, GstBuffer * buffer);
183
184 #define gst_opus_enc_parent_class parent_class
185 G_DEFINE_TYPE_WITH_CODE (GstOpusEnc, gst_opus_enc, GST_TYPE_AUDIO_ENCODER,
186     G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL);
187     G_IMPLEMENT_INTERFACE (GST_TYPE_PRESET, NULL));
188
189 static void
190 gst_opus_enc_class_init (GstOpusEncClass * klass)
191 {
192   GObjectClass *gobject_class;
193   GstAudioEncoderClass *base_class;
194   GstElementClass *gstelement_class;
195
196   gobject_class = (GObjectClass *) klass;
197   base_class = (GstAudioEncoderClass *) klass;
198   gstelement_class = (GstElementClass *) klass;
199
200   gobject_class->set_property = gst_opus_enc_set_property;
201   gobject_class->get_property = gst_opus_enc_get_property;
202
203   gst_element_class_add_pad_template (gstelement_class,
204       gst_static_pad_template_get (&src_factory));
205   gst_element_class_add_pad_template (gstelement_class,
206       gst_static_pad_template_get (&sink_factory));
207   gst_element_class_set_details_simple (gstelement_class, "Opus audio encoder",
208       "Codec/Encoder/Audio",
209       "Encodes audio in Opus format",
210       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
211
212   base_class->start = GST_DEBUG_FUNCPTR (gst_opus_enc_start);
213   base_class->stop = GST_DEBUG_FUNCPTR (gst_opus_enc_stop);
214   base_class->set_format = GST_DEBUG_FUNCPTR (gst_opus_enc_set_format);
215   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_opus_enc_handle_frame);
216   base_class->event = GST_DEBUG_FUNCPTR (gst_opus_enc_sink_event);
217   base_class->getcaps = GST_DEBUG_FUNCPTR (gst_opus_enc_sink_getcaps);
218
219   g_object_class_install_property (gobject_class, PROP_AUDIO,
220       g_param_spec_boolean ("audio", "Audio or voice",
221           "Audio or voice", DEFAULT_AUDIO,
222           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BITRATE,
224       g_param_spec_int ("bitrate", "Encoding Bit-rate",
225           "Specify an encoding bit-rate (in bps).",
226           LOWEST_BITRATE, HIGHEST_BITRATE, DEFAULT_BITRATE,
227           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
228           GST_PARAM_MUTABLE_PLAYING));
229   g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
230       g_param_spec_enum ("bandwidth", "Band Width", "Audio Band Width",
231           GST_OPUS_ENC_TYPE_BANDWIDTH, DEFAULT_BANDWIDTH,
232           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
233           GST_PARAM_MUTABLE_PLAYING));
234   g_object_class_install_property (gobject_class, PROP_FRAME_SIZE,
235       g_param_spec_enum ("frame-size", "Frame Size",
236           "The duration of an audio frame, in ms", GST_OPUS_ENC_TYPE_FRAME_SIZE,
237           DEFAULT_FRAMESIZE,
238           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
239           GST_PARAM_MUTABLE_PLAYING));
240   g_object_class_install_property (gobject_class, PROP_CBR,
241       g_param_spec_boolean ("cbr", "Constant bit rate", "Constant bit rate",
242           DEFAULT_CBR,
243           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
244           GST_PARAM_MUTABLE_PLAYING));
245   g_object_class_install_property (gobject_class, PROP_CONSTRAINED_VBR,
246       g_param_spec_boolean ("constrained-vbr", "Constrained VBR",
247           "Constrained VBR", DEFAULT_CONSTRAINED_VBR,
248           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
249           GST_PARAM_MUTABLE_PLAYING));
250   g_object_class_install_property (gobject_class, PROP_COMPLEXITY,
251       g_param_spec_int ("complexity", "Complexity", "Complexity", 0, 10,
252           DEFAULT_COMPLEXITY,
253           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
254           GST_PARAM_MUTABLE_PLAYING));
255   g_object_class_install_property (gobject_class, PROP_INBAND_FEC,
256       g_param_spec_boolean ("inband-fec", "In-band FEC",
257           "Enable forward error correction", DEFAULT_INBAND_FEC,
258           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
259           GST_PARAM_MUTABLE_PLAYING));
260   g_object_class_install_property (gobject_class, PROP_DTX,
261       g_param_spec_boolean ("dtx", "DTX", "DTX", DEFAULT_DTX,
262           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
263           GST_PARAM_MUTABLE_PLAYING));
264   g_object_class_install_property (G_OBJECT_CLASS (klass),
265       PROP_PACKET_LOSS_PERCENT, g_param_spec_int ("packet-loss-percentage",
266           "Loss percentage", "Packet loss percentage", 0, 100,
267           DEFAULT_PACKET_LOSS_PERCENT,
268           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
269           GST_PARAM_MUTABLE_PLAYING));
270   g_object_class_install_property (G_OBJECT_CLASS (klass),
271       PROP_MAX_PAYLOAD_SIZE, g_param_spec_uint ("max-payload-size",
272           "Max payload size", "Maximum payload size in bytes", 2, 1275,
273           DEFAULT_MAX_PAYLOAD_SIZE,
274           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
275           GST_PARAM_MUTABLE_PLAYING));
276
277   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_opus_enc_finalize);
278
279   GST_DEBUG_CATEGORY_INIT (opusenc_debug, "opusenc", 0, "Opus encoder");
280 }
281
282 static void
283 gst_opus_enc_finalize (GObject * object)
284 {
285   GstOpusEnc *enc;
286
287   enc = GST_OPUS_ENC (object);
288
289   g_mutex_free (enc->property_lock);
290
291   G_OBJECT_CLASS (parent_class)->finalize (object);
292 }
293
294 static void
295 gst_opus_enc_init (GstOpusEnc * enc)
296 {
297   GstAudioEncoder *benc = GST_AUDIO_ENCODER (enc);
298
299   GST_DEBUG_OBJECT (enc, "init");
300
301   enc->property_lock = g_mutex_new ();
302
303   enc->n_channels = -1;
304   enc->sample_rate = -1;
305   enc->frame_samples = 0;
306
307   enc->bitrate = DEFAULT_BITRATE;
308   enc->bandwidth = DEFAULT_BANDWIDTH;
309   enc->frame_size = DEFAULT_FRAMESIZE;
310   enc->cbr = DEFAULT_CBR;
311   enc->constrained_vbr = DEFAULT_CONSTRAINED_VBR;
312   enc->complexity = DEFAULT_COMPLEXITY;
313   enc->inband_fec = DEFAULT_INBAND_FEC;
314   enc->dtx = DEFAULT_DTX;
315   enc->packet_loss_percentage = DEFAULT_PACKET_LOSS_PERCENT;
316   enc->max_payload_size = DEFAULT_MAX_PAYLOAD_SIZE;
317
318   /* arrange granulepos marking (and required perfect ts) */
319   gst_audio_encoder_set_mark_granule (benc, TRUE);
320   gst_audio_encoder_set_perfect_timestamp (benc, TRUE);
321 }
322
323 static gboolean
324 gst_opus_enc_start (GstAudioEncoder * benc)
325 {
326   GstOpusEnc *enc = GST_OPUS_ENC (benc);
327
328   GST_DEBUG_OBJECT (enc, "start");
329   enc->tags = gst_tag_list_new_empty ();
330   enc->header_sent = FALSE;
331
332   return TRUE;
333 }
334
335 static gboolean
336 gst_opus_enc_stop (GstAudioEncoder * benc)
337 {
338   GstOpusEnc *enc = GST_OPUS_ENC (benc);
339
340   GST_DEBUG_OBJECT (enc, "stop");
341   enc->header_sent = FALSE;
342   if (enc->state) {
343     opus_multistream_encoder_destroy (enc->state);
344     enc->state = NULL;
345   }
346   gst_tag_list_free (enc->tags);
347   enc->tags = NULL;
348   g_slist_foreach (enc->headers, (GFunc) gst_buffer_unref, NULL);
349   g_slist_free (enc->headers);
350   enc->headers = NULL;
351   gst_tag_setter_reset_tags (GST_TAG_SETTER (enc));
352
353   return TRUE;
354 }
355
356 static gint64
357 gst_opus_enc_get_latency (GstOpusEnc * enc)
358 {
359   gint64 latency = gst_util_uint64_scale (enc->frame_samples, GST_SECOND,
360       enc->sample_rate);
361   GST_DEBUG_OBJECT (enc, "Latency: %" GST_TIME_FORMAT, GST_TIME_ARGS (latency));
362   return latency;
363 }
364
365 static void
366 gst_opus_enc_setup_base_class (GstOpusEnc * enc, GstAudioEncoder * benc)
367 {
368   gst_audio_encoder_set_latency (benc,
369       gst_opus_enc_get_latency (enc), gst_opus_enc_get_latency (enc));
370   gst_audio_encoder_set_frame_samples_min (benc,
371       enc->frame_samples * enc->n_channels * 2);
372   gst_audio_encoder_set_frame_samples_max (benc,
373       enc->frame_samples * enc->n_channels * 2);
374   gst_audio_encoder_set_frame_max (benc, 0);
375 }
376
377 static gint
378 gst_opus_enc_get_frame_samples (GstOpusEnc * enc)
379 {
380   gint frame_samples = 0;
381   switch (enc->frame_size) {
382     case 2:
383       frame_samples = enc->sample_rate / 400;
384       break;
385     case 5:
386       frame_samples = enc->sample_rate / 200;
387       break;
388     case 10:
389       frame_samples = enc->sample_rate / 100;
390       break;
391     case 20:
392       frame_samples = enc->sample_rate / 50;
393       break;
394     case 40:
395       frame_samples = enc->sample_rate / 25;
396       break;
397     case 60:
398       frame_samples = 3 * enc->sample_rate / 50;
399       break;
400     default:
401       GST_WARNING_OBJECT (enc, "Unsupported frame size: %d", enc->frame_size);
402       frame_samples = 0;
403       break;
404   }
405   return frame_samples;
406 }
407
408 static void
409 gst_opus_enc_setup_trivial_mapping (GstOpusEnc * enc, guint8 mapping[256])
410 {
411   int n;
412
413   for (n = 0; n < 255; ++n)
414     mapping[n] = n;
415 }
416
417 static int
418 gst_opus_enc_find_channel_position (GstOpusEnc * enc, const GstAudioInfo * info,
419     GstAudioChannelPosition position)
420 {
421   int n;
422   for (n = 0; n < enc->n_channels; ++n) {
423     if (GST_AUDIO_INFO_POSITION (info, n) == position) {
424       return n;
425     }
426   }
427   return -1;
428 }
429
430 static int
431 gst_opus_enc_find_channel_position_in_vorbis_order (GstOpusEnc * enc,
432     GstAudioChannelPosition position)
433 {
434   int c;
435
436   for (c = 0; c < enc->n_channels; ++c) {
437     if (gst_opus_channel_positions[enc->n_channels - 1][c] == position) {
438       GST_INFO_OBJECT (enc,
439           "Channel position %s maps to index %d in Vorbis order",
440           gst_opus_channel_names[position], c);
441       return c;
442     }
443   }
444   GST_WARNING_OBJECT (enc,
445       "Channel position %s is not representable in Vorbis order",
446       gst_opus_channel_names[position]);
447   return -1;
448 }
449
450 static void
451 gst_opus_enc_setup_channel_mappings (GstOpusEnc * enc,
452     const GstAudioInfo * info)
453 {
454 #define MAPS(idx,pos) (GST_AUDIO_INFO_POSITION (info, (idx)) == GST_AUDIO_CHANNEL_POSITION_##pos)
455
456   int n;
457
458   GST_DEBUG_OBJECT (enc, "Setting up channel mapping for %d channels",
459       enc->n_channels);
460
461   /* Start by setting up a default trivial mapping */
462   enc->n_stereo_streams = 0;
463   gst_opus_enc_setup_trivial_mapping (enc, enc->encoding_channel_mapping);
464   gst_opus_enc_setup_trivial_mapping (enc, enc->decoding_channel_mapping);
465
466   /* For one channel, use the basic RTP mapping */
467   if (enc->n_channels == 1) {
468     GST_INFO_OBJECT (enc, "Mono, trivial RTP mapping");
469     enc->channel_mapping_family = 0;
470     /* implicit mapping for family 0 */
471     return;
472   }
473
474   /* For two channels, use the basic RTP mapping if the channels are
475      mapped as left/right. */
476   if (enc->n_channels == 2) {
477     if (MAPS (0, FRONT_LEFT) && MAPS (1, FRONT_RIGHT)) {
478       GST_INFO_OBJECT (enc, "Stereo, canonical mapping");
479       enc->channel_mapping_family = 0;
480       enc->n_stereo_streams = 1;
481       /* The channel mapping is implicit for family 0, that's why we do not
482          attempt to create one for right/left - this will be mapped to the
483          Vorbis mapping below. */
484       return;
485     } else {
486       GST_DEBUG_OBJECT (enc, "Stereo, but not canonical mapping, continuing");
487     }
488   }
489
490   /* For channels between 1 and 8, we use the Vorbis mapping if we can
491      find a permutation that matches it. Mono will have been taken care
492      of earlier, but this code also handles it. Same for left/right stereo.
493      There are two mappings. One maps the input channels to an ordering
494      which has the natural pairs first so they can benefit from the Opus
495      stereo channel coupling, and the other maps this ordering to the
496      Vorbis ordering. */
497   if (enc->n_channels >= 1 && enc->n_channels <= 8) {
498     int c0, c1, c0v, c1v;
499     int mapped;
500     gboolean positions_done[256];
501     static const GstAudioChannelPosition pairs[][2] = {
502       {GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
503           GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT},
504       {GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
505           GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT},
506       {GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
507           GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER},
508       {GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
509           GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER},
510       {GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
511           GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT},
512     };
513     size_t pair;
514
515     GST_DEBUG_OBJECT (enc,
516         "In range for the Vorbis mapping, building channel mapping tables");
517
518     enc->n_stereo_streams = 0;
519     mapped = 0;
520     for (n = 0; n < 256; ++n)
521       positions_done[n] = FALSE;
522
523     /* First, find any natural pairs, and move them to the front */
524     for (pair = 0; pair < G_N_ELEMENTS (pairs); ++pair) {
525       GstAudioChannelPosition p0 = pairs[pair][0];
526       GstAudioChannelPosition p1 = pairs[pair][1];
527       c0 = gst_opus_enc_find_channel_position (enc, info, p0);
528       c1 = gst_opus_enc_find_channel_position (enc, info, p1);
529       if (c0 >= 0 && c1 >= 0) {
530         /* We found a natural pair */
531         GST_DEBUG_OBJECT (enc, "Natural pair '%s/%s' found at %d %d",
532             gst_opus_channel_names[p0], gst_opus_channel_names[p1], c0, c1);
533         /* Find where they map in Vorbis order */
534         c0v = gst_opus_enc_find_channel_position_in_vorbis_order (enc, p0);
535         c1v = gst_opus_enc_find_channel_position_in_vorbis_order (enc, p1);
536         if (c0v < 0 || c1v < 0) {
537           GST_WARNING_OBJECT (enc,
538               "Cannot map channel positions to Vorbis order, using unknown mapping");
539           enc->channel_mapping_family = 255;
540           enc->n_stereo_streams = 0;
541           return;
542         }
543
544         enc->encoding_channel_mapping[mapped] = c0;
545         enc->encoding_channel_mapping[mapped + 1] = c1;
546         enc->decoding_channel_mapping[c0v] = mapped;
547         enc->decoding_channel_mapping[c1v] = mapped + 1;
548         enc->n_stereo_streams++;
549         mapped += 2;
550         positions_done[p0] = positions_done[p1] = TRUE;
551       }
552     }
553
554     /* Now add all other input channels as mono streams */
555     for (n = 0; n < enc->n_channels; ++n) {
556       GstAudioChannelPosition position = GST_AUDIO_INFO_POSITION (info, n);
557
558       /* if we already mapped it while searching for pairs, nothing else
559          needs to be done */
560       if (!positions_done[position]) {
561         int cv;
562         GST_DEBUG_OBJECT (enc, "Channel position %s is not mapped yet, adding",
563             gst_opus_channel_names[position]);
564         cv = gst_opus_enc_find_channel_position_in_vorbis_order (enc, position);
565         if (cv < 0) {
566           GST_WARNING_OBJECT (enc,
567               "Cannot map channel positions to Vorbis order, using unknown mapping");
568           enc->channel_mapping_family = 255;
569           enc->n_stereo_streams = 0;
570           return;
571         }
572         enc->encoding_channel_mapping[mapped] = n;
573         enc->decoding_channel_mapping[cv] = mapped;
574         mapped++;
575       }
576     }
577
578 #ifndef GST_DISABLE_DEBUG
579     GST_INFO_OBJECT (enc,
580         "Mapping tables built: %d channels, %d stereo streams", enc->n_channels,
581         enc->n_stereo_streams);
582     gst_opus_common_log_channel_mapping_table (GST_ELEMENT (enc), opusenc_debug,
583         "Encoding mapping table", enc->n_channels,
584         enc->encoding_channel_mapping);
585     gst_opus_common_log_channel_mapping_table (GST_ELEMENT (enc), opusenc_debug,
586         "Decoding mapping table", enc->n_channels,
587         enc->decoding_channel_mapping);
588 #endif
589
590     enc->channel_mapping_family = 1;
591     return;
592   }
593
594   /* More than 8 channels, if future mappings are added for those */
595
596   /* For other cases, we use undefined, with the default trivial mapping
597      and all mono streams */
598   GST_WARNING_OBJECT (enc, "Unknown mapping");
599   enc->channel_mapping_family = 255;
600   enc->n_stereo_streams = 0;
601
602 #undef MAPS
603 }
604
605 static gboolean
606 gst_opus_enc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
607 {
608   GstOpusEnc *enc;
609
610   enc = GST_OPUS_ENC (benc);
611
612   g_mutex_lock (enc->property_lock);
613
614   enc->n_channels = GST_AUDIO_INFO_CHANNELS (info);
615   enc->sample_rate = GST_AUDIO_INFO_RATE (info);
616   gst_opus_enc_setup_channel_mappings (enc, info);
617   GST_DEBUG_OBJECT (benc, "Setup with %d channels, %d Hz", enc->n_channels,
618       enc->sample_rate);
619
620   /* handle reconfigure */
621   if (enc->state) {
622     opus_multistream_encoder_destroy (enc->state);
623     enc->state = NULL;
624   }
625   if (!gst_opus_enc_setup (enc))
626     return FALSE;
627
628   enc->frame_samples = gst_opus_enc_get_frame_samples (enc);
629
630   /* feedback to base class */
631   gst_opus_enc_setup_base_class (enc, benc);
632
633   g_mutex_unlock (enc->property_lock);
634
635   return TRUE;
636 }
637
638 static gboolean
639 gst_opus_enc_setup (GstOpusEnc * enc)
640 {
641   int error = OPUS_OK;
642
643 #ifndef GST_DISABLE_DEBUG
644   GST_DEBUG_OBJECT (enc,
645       "setup: %d Hz, %d channels, %d stereo streams, family %d",
646       enc->sample_rate, enc->n_channels, enc->n_stereo_streams,
647       enc->channel_mapping_family);
648   GST_INFO_OBJECT (enc, "Mapping tables built: %d channels, %d stereo streams",
649       enc->n_channels, enc->n_stereo_streams);
650   gst_opus_common_log_channel_mapping_table (GST_ELEMENT (enc), opusenc_debug,
651       "Encoding mapping table", enc->n_channels, enc->encoding_channel_mapping);
652   gst_opus_common_log_channel_mapping_table (GST_ELEMENT (enc), opusenc_debug,
653       "Decoding mapping table", enc->n_channels, enc->decoding_channel_mapping);
654 #endif
655
656   enc->state = opus_multistream_encoder_create (enc->sample_rate,
657       enc->n_channels, enc->n_channels - enc->n_stereo_streams,
658       enc->n_stereo_streams, enc->encoding_channel_mapping,
659       enc->audio_or_voip ? OPUS_APPLICATION_AUDIO : OPUS_APPLICATION_VOIP,
660       &error);
661   if (!enc->state || error != OPUS_OK)
662     goto encoder_creation_failed;
663
664   opus_multistream_encoder_ctl (enc->state, OPUS_SET_BITRATE (enc->bitrate), 0);
665   opus_multistream_encoder_ctl (enc->state, OPUS_SET_BANDWIDTH (enc->bandwidth),
666       0);
667   opus_multistream_encoder_ctl (enc->state, OPUS_SET_VBR (!enc->cbr), 0);
668   opus_multistream_encoder_ctl (enc->state,
669       OPUS_SET_VBR_CONSTRAINT (enc->constrained_vbr), 0);
670   opus_multistream_encoder_ctl (enc->state,
671       OPUS_SET_COMPLEXITY (enc->complexity), 0);
672   opus_multistream_encoder_ctl (enc->state,
673       OPUS_SET_INBAND_FEC (enc->inband_fec), 0);
674   opus_multistream_encoder_ctl (enc->state, OPUS_SET_DTX (enc->dtx), 0);
675   opus_multistream_encoder_ctl (enc->state,
676       OPUS_SET_PACKET_LOSS_PERC (enc->packet_loss_percentage), 0);
677
678   GST_LOG_OBJECT (enc, "we have frame size %d", enc->frame_size);
679
680   return TRUE;
681
682 encoder_creation_failed:
683   GST_ERROR_OBJECT (enc, "Encoder creation failed");
684   return FALSE;
685 }
686
687 static gboolean
688 gst_opus_enc_sink_event (GstAudioEncoder * benc, GstEvent * event)
689 {
690   GstOpusEnc *enc;
691
692   enc = GST_OPUS_ENC (benc);
693
694   GST_DEBUG_OBJECT (enc, "sink event: %s", GST_EVENT_TYPE_NAME (event));
695   switch (GST_EVENT_TYPE (event)) {
696     case GST_EVENT_TAG:
697     {
698       GstTagList *list;
699       GstTagSetter *setter = GST_TAG_SETTER (enc);
700       const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
701
702       gst_event_parse_tag (event, &list);
703       gst_tag_setter_merge_tags (setter, list, mode);
704       break;
705     }
706
707     default:
708       break;
709   }
710
711   return GST_AUDIO_ENCODER_CLASS (parent_class)->event (benc, event);
712 }
713
714 static GstCaps *
715 gst_opus_enc_sink_getcaps (GstAudioEncoder * benc, GstCaps * filter)
716 {
717   GstOpusEnc *enc;
718   GstCaps *caps;
719   GstCaps *peercaps = NULL;
720   GstCaps *intersect = NULL;
721   guint i;
722   gboolean allow_multistream;
723
724   enc = GST_OPUS_ENC (benc);
725
726   GST_DEBUG_OBJECT (enc, "sink getcaps");
727
728   peercaps = gst_pad_peer_query_caps (GST_AUDIO_ENCODER_SRC_PAD (benc), filter);
729   if (!peercaps) {
730     GST_DEBUG_OBJECT (benc, "No peercaps, returning template sink caps");
731     return
732         gst_caps_copy (gst_pad_get_pad_template_caps
733         (GST_AUDIO_ENCODER_SINK_PAD (benc)));
734   }
735
736   intersect = gst_caps_intersect (peercaps,
737       gst_pad_get_pad_template_caps (GST_AUDIO_ENCODER_SRC_PAD (benc)));
738   gst_caps_unref (peercaps);
739
740   if (gst_caps_is_empty (intersect))
741     return intersect;
742
743   allow_multistream = FALSE;
744   for (i = 0; i < gst_caps_get_size (intersect); i++) {
745     GstStructure *s = gst_caps_get_structure (intersect, i);
746     gboolean multistream;
747     if (gst_structure_get_boolean (s, "multistream", &multistream)) {
748       if (multistream) {
749         allow_multistream = TRUE;
750       }
751     } else {
752       allow_multistream = TRUE;
753     }
754   }
755
756   gst_caps_unref (intersect);
757
758   caps =
759       gst_caps_copy (gst_pad_get_pad_template_caps (GST_AUDIO_ENCODER_SINK_PAD
760           (benc)));
761   if (!allow_multistream) {
762     GValue range = { 0 };
763     g_value_init (&range, GST_TYPE_INT_RANGE);
764     gst_value_set_int_range (&range, 1, 2);
765     for (i = 0; i < gst_caps_get_size (caps); i++) {
766       GstStructure *s = gst_caps_get_structure (caps, i);
767       gst_structure_set_value (s, "channels", &range);
768     }
769     g_value_unset (&range);
770   }
771
772   if (filter) {
773     GstCaps *tmp = gst_caps_intersect_full (caps, filter,
774         GST_CAPS_INTERSECT_FIRST);
775     gst_caps_unref (caps);
776     caps = tmp;
777   }
778
779   GST_DEBUG_OBJECT (enc, "Returning caps: %" GST_PTR_FORMAT, caps);
780   return caps;
781 }
782
783 static GstFlowReturn
784 gst_opus_enc_encode (GstOpusEnc * enc, GstBuffer * buf)
785 {
786   guint8 *bdata = NULL, *data, *mdata = NULL;
787   gsize bsize, size;
788   gsize bytes = enc->frame_samples * enc->n_channels * 2;
789   gint ret = GST_FLOW_OK;
790
791   g_mutex_lock (enc->property_lock);
792
793   if (G_LIKELY (buf)) {
794     bdata = gst_buffer_map (buf, &bsize, NULL, GST_MAP_READ);
795
796     if (G_UNLIKELY (bsize % bytes)) {
797       GST_DEBUG_OBJECT (enc, "draining; adding silence samples");
798
799       size = ((bsize / bytes) + 1) * bytes;
800       mdata = g_malloc0 (size);
801       memcpy (mdata, bdata, bsize);
802       gst_buffer_unmap (buf, bdata, bsize);
803       bdata = NULL;
804       data = mdata;
805     } else {
806       data = bdata;
807       size = bsize;
808     }
809   } else {
810     GST_DEBUG_OBJECT (enc, "nothing to drain");
811     goto done;
812   }
813
814   while (size) {
815     gint encoded_size;
816     unsigned char *out_data;
817     gsize out_size;
818     GstBuffer *outbuf;
819
820     outbuf = gst_buffer_new_and_alloc (enc->max_payload_size * enc->n_channels);
821     if (!outbuf)
822       goto done;
823
824     GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)",
825         enc->frame_samples, (int) bytes);
826
827     out_data = gst_buffer_map (outbuf, &out_size, NULL, GST_MAP_WRITE);
828     encoded_size =
829         opus_multistream_encode (enc->state, (const gint16 *) data,
830         enc->frame_samples, out_data, enc->max_payload_size * enc->n_channels);
831     gst_buffer_unmap (outbuf, out_data, out_size);
832
833     if (encoded_size < 0) {
834       GST_ERROR_OBJECT (enc, "Encoding failed: %d", encoded_size);
835       ret = GST_FLOW_ERROR;
836       goto done;
837     } else if (encoded_size > enc->max_payload_size) {
838       GST_WARNING_OBJECT (enc,
839           "Encoded size %d is higher than max payload size (%d bytes)",
840           out_size, enc->max_payload_size);
841       ret = GST_FLOW_ERROR;
842       goto done;
843     }
844
845     GST_DEBUG_OBJECT (enc, "Output packet is %u bytes", encoded_size);
846     gst_buffer_set_size (outbuf, encoded_size);
847
848     ret =
849         gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (enc), outbuf,
850         enc->frame_samples);
851
852     if ((GST_FLOW_OK != ret) && (GST_FLOW_NOT_LINKED != ret))
853       goto done;
854
855     data += bytes;
856     size -= bytes;
857   }
858
859 done:
860
861   if (bdata)
862     gst_buffer_unmap (buf, bdata, bsize);
863   g_mutex_unlock (enc->property_lock);
864
865   if (mdata)
866     g_free (mdata);
867
868   return ret;
869 }
870
871 static GstFlowReturn
872 gst_opus_enc_handle_frame (GstAudioEncoder * benc, GstBuffer * buf)
873 {
874   GstOpusEnc *enc;
875   GstFlowReturn ret = GST_FLOW_OK;
876
877   enc = GST_OPUS_ENC (benc);
878   GST_DEBUG_OBJECT (enc, "handle_frame");
879
880   if (!enc->header_sent) {
881     GstCaps *caps;
882
883     g_slist_foreach (enc->headers, (GFunc) gst_buffer_unref, NULL);
884     g_slist_free (enc->headers);
885     enc->headers = NULL;
886
887     gst_opus_header_create_caps (&caps, &enc->headers, enc->n_channels,
888         enc->n_stereo_streams, enc->sample_rate, enc->channel_mapping_family,
889         enc->decoding_channel_mapping,
890         gst_tag_setter_get_tag_list (GST_TAG_SETTER (enc)));
891
892
893     /* negotiate with these caps */
894     GST_DEBUG_OBJECT (enc, "here are the caps: %" GST_PTR_FORMAT, caps);
895
896     gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (enc), caps);
897     gst_caps_unref (caps);
898
899     enc->header_sent = TRUE;
900   }
901
902   GST_DEBUG_OBJECT (enc, "received buffer %p of %u bytes", buf,
903       buf ? gst_buffer_get_size (buf) : 0);
904
905   ret = gst_opus_enc_encode (enc, buf);
906
907   return ret;
908 }
909
910 static void
911 gst_opus_enc_get_property (GObject * object, guint prop_id, GValue * value,
912     GParamSpec * pspec)
913 {
914   GstOpusEnc *enc;
915
916   enc = GST_OPUS_ENC (object);
917
918   g_mutex_lock (enc->property_lock);
919
920   switch (prop_id) {
921     case PROP_AUDIO:
922       g_value_set_boolean (value, enc->audio_or_voip);
923       break;
924     case PROP_BITRATE:
925       g_value_set_int (value, enc->bitrate);
926       break;
927     case PROP_BANDWIDTH:
928       g_value_set_enum (value, enc->bandwidth);
929       break;
930     case PROP_FRAME_SIZE:
931       g_value_set_enum (value, enc->frame_size);
932       break;
933     case PROP_CBR:
934       g_value_set_boolean (value, enc->cbr);
935       break;
936     case PROP_CONSTRAINED_VBR:
937       g_value_set_boolean (value, enc->constrained_vbr);
938       break;
939     case PROP_COMPLEXITY:
940       g_value_set_int (value, enc->complexity);
941       break;
942     case PROP_INBAND_FEC:
943       g_value_set_boolean (value, enc->inband_fec);
944       break;
945     case PROP_DTX:
946       g_value_set_boolean (value, enc->dtx);
947       break;
948     case PROP_PACKET_LOSS_PERCENT:
949       g_value_set_int (value, enc->packet_loss_percentage);
950       break;
951     case PROP_MAX_PAYLOAD_SIZE:
952       g_value_set_uint (value, enc->max_payload_size);
953       break;
954     default:
955       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
956       break;
957   }
958
959   g_mutex_unlock (enc->property_lock);
960 }
961
962 static void
963 gst_opus_enc_set_property (GObject * object, guint prop_id,
964     const GValue * value, GParamSpec * pspec)
965 {
966   GstOpusEnc *enc;
967
968   enc = GST_OPUS_ENC (object);
969
970 #define GST_OPUS_UPDATE_PROPERTY(prop,type,ctl) do { \
971   g_mutex_lock (enc->property_lock); \
972   enc->prop = g_value_get_##type (value); \
973   if (enc->state) { \
974     opus_multistream_encoder_ctl (enc->state, OPUS_SET_##ctl (enc->prop)); \
975   } \
976   g_mutex_unlock (enc->property_lock); \
977 } while(0)
978
979   switch (prop_id) {
980     case PROP_AUDIO:
981       enc->audio_or_voip = g_value_get_boolean (value);
982       break;
983     case PROP_BITRATE:
984       GST_OPUS_UPDATE_PROPERTY (bitrate, int, BITRATE);
985       break;
986     case PROP_BANDWIDTH:
987       GST_OPUS_UPDATE_PROPERTY (bandwidth, enum, BANDWIDTH);
988       break;
989     case PROP_FRAME_SIZE:
990       g_mutex_lock (enc->property_lock);
991       enc->frame_size = g_value_get_enum (value);
992       enc->frame_samples = gst_opus_enc_get_frame_samples (enc);
993       gst_opus_enc_setup_base_class (enc, GST_AUDIO_ENCODER (enc));
994       g_mutex_unlock (enc->property_lock);
995       break;
996     case PROP_CBR:
997       /* this one has an opposite meaning to the opus ctl... */
998       g_mutex_lock (enc->property_lock);
999       enc->cbr = g_value_get_boolean (value);
1000       opus_multistream_encoder_ctl (enc->state, OPUS_SET_VBR (!enc->cbr));
1001       g_mutex_unlock (enc->property_lock);
1002       break;
1003     case PROP_CONSTRAINED_VBR:
1004       GST_OPUS_UPDATE_PROPERTY (constrained_vbr, boolean, VBR_CONSTRAINT);
1005       break;
1006     case PROP_COMPLEXITY:
1007       GST_OPUS_UPDATE_PROPERTY (complexity, int, COMPLEXITY);
1008       break;
1009     case PROP_INBAND_FEC:
1010       GST_OPUS_UPDATE_PROPERTY (inband_fec, boolean, INBAND_FEC);
1011       break;
1012     case PROP_DTX:
1013       GST_OPUS_UPDATE_PROPERTY (dtx, boolean, DTX);
1014       break;
1015     case PROP_PACKET_LOSS_PERCENT:
1016       GST_OPUS_UPDATE_PROPERTY (packet_loss_percentage, int, PACKET_LOSS_PERC);
1017       break;
1018     case PROP_MAX_PAYLOAD_SIZE:
1019       g_mutex_lock (enc->property_lock);
1020       enc->max_payload_size = g_value_get_uint (value);
1021       g_mutex_unlock (enc->property_lock);
1022       break;
1023     default:
1024       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1025       break;
1026   }
1027
1028 #undef GST_OPUS_UPDATE_PROPERTY
1029
1030 }