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