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