1 /* iSAC encoder
2 *
3 * Copyright (C) 2020 Collabora Ltd.
4 * Author: Guillaume Desmottes <guillaume.desmottes@collabora.com>, Collabora Ltd.
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 Free
18 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA.
20 */
21
22 /**
23 * SECTION:element-isacenc
24 * @title: isacenc
25 * @short_description: iSAC audio encoder
26 *
27 * Since: 1.20
28 *
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "gstisacenc.h"
36 #include "gstisacutils.h"
37
38 #include <modules/audio_coding/codecs/isac/main/include/isac.h>
39
40 GST_DEBUG_CATEGORY_STATIC (isacenc_debug);
41 #define GST_CAT_DEFAULT isacenc_debug
42
43 /* Buffer size used in the simpleKenny.c test app from webrtc */
44 #define OUTPUT_BUFFER_SIZE 1200
45
46 #define GST_TYPE_ISACENC_OUTPUT_FRAME_LEN (gst_isacenc_output_frame_len_get_type ())
47 static GType
gst_isacenc_output_frame_len_get_type(void)48 gst_isacenc_output_frame_len_get_type (void)
49 {
50 static GType qtype = 0;
51
52 if (qtype == 0) {
53 static const GEnumValue values[] = {
54 {30, "30 ms", "30 ms"},
55 {60, "60 ms", "60 ms, only usable in wideband mode (16 kHz)"},
56 {0, NULL, NULL}
57 };
58
59 qtype = g_enum_register_static ("GstIsacEncOutputFrameLen", values);
60 }
61 return qtype;
62 }
63
64 enum
65 {
66 PROP_0,
67 PROP_OUTPUT_FRAME_LEN,
68 PROP_BITRATE,
69 PROP_MAX_PAYLOAD_SIZE,
70 PROP_MAX_RATE,
71 };
72
73 #define GST_ISACENC_OUTPUT_FRAME_LEN_DEFAULT (30)
74 #define GST_ISACENC_BITRATE_DEFAULT (32000)
75 #define GST_ISACENC_MAX_PAYLOAD_SIZE_DEFAULT (-1)
76 #define GST_ISACENC_MAX_RATE_DEFAULT (-1)
77
78 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
79 GST_PAD_SINK,
80 GST_PAD_ALWAYS,
81 GST_STATIC_CAPS ("audio/x-raw, "
82 "format = (string) " GST_AUDIO_NE (S16) ", "
83 "rate = (int) { 16000, 32000 }, "
84 "layout = (string) interleaved, " "channels = (int) 1")
85 );
86
87 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
88 GST_PAD_SRC,
89 GST_PAD_ALWAYS,
90 GST_STATIC_CAPS ("audio/isac, "
91 "rate = (int) { 16000, 32000 }, " "channels = (int) 1")
92 );
93
94 typedef enum
95 {
96 ENCODER_MODE_WIDEBAND, /* 16 kHz */
97 ENCODER_MODE_SUPER_WIDEBAND, /* 32 kHz */
98 } EncoderMode;
99
100 struct _GstIsacEnc
101 {
102 /*< private > */
103 GstAudioEncoder parent;
104
105 ISACStruct *isac;
106 EncoderMode mode;
107 gint samples_per_frame; /* number of samples in one input frame */
108 gsize frame_size; /* size, in bytes, of one input frame */
109 guint nb_processed_input_frames; /* number of input frames processed by the encoder since the last produced encoded data */
110
111 /* properties */
112 gint output_frame_len;
113 gint bitrate;
114 gint max_payload_size;
115 gint max_rate;
116 };
117
118 #define gst_isacenc_parent_class parent_class
119 G_DEFINE_TYPE_WITH_CODE (GstIsacEnc, gst_isacenc,
120 GST_TYPE_AUDIO_ENCODER,
121 GST_DEBUG_CATEGORY_INIT (isacenc_debug, "isacenc", 0,
122 "debug category for isacenc element"));
123 GST_ELEMENT_REGISTER_DEFINE (isacenc, "isacenc", GST_RANK_PRIMARY,
124 GST_TYPE_ISACENC);
125
126 static gboolean
gst_isacenc_start(GstAudioEncoder * enc)127 gst_isacenc_start (GstAudioEncoder * enc)
128 {
129 GstIsacEnc *self = GST_ISACENC (enc);
130 gint16 ret;
131
132 g_assert (!self->isac);
133 ret = WebRtcIsac_Create (&self->isac);
134 CHECK_ISAC_RET (ret, Create);
135
136 self->nb_processed_input_frames = 0;
137
138 return TRUE;
139 }
140
141 static gboolean
gst_isacenc_stop(GstAudioEncoder * enc)142 gst_isacenc_stop (GstAudioEncoder * enc)
143 {
144 GstIsacEnc *self = GST_ISACENC (enc);
145
146 if (self->isac) {
147 gint16 ret;
148
149 ret = WebRtcIsac_Free (self->isac);
150 CHECK_ISAC_RET (ret, Free);
151 self->isac = NULL;
152 }
153
154 return TRUE;
155 }
156
157 static gboolean
gst_isacenc_set_format(GstAudioEncoder * enc,GstAudioInfo * info)158 gst_isacenc_set_format (GstAudioEncoder * enc, GstAudioInfo * info)
159 {
160 GstIsacEnc *self = GST_ISACENC (enc);
161 GstCaps *input_caps, *output_caps;
162 gint16 ret;
163 gboolean result;
164
165 switch (GST_AUDIO_INFO_RATE (info)) {
166 case 16000:
167 self->mode = ENCODER_MODE_WIDEBAND;
168 break;
169 case 32000:
170 self->mode = ENCODER_MODE_SUPER_WIDEBAND;
171 break;
172 default:
173 g_assert_not_reached ();
174 return FALSE;
175 }
176
177 input_caps = gst_audio_info_to_caps (info);
178 output_caps = gst_caps_new_simple ("audio/isac",
179 "channels", G_TYPE_INT, GST_AUDIO_INFO_CHANNELS (info),
180 "rate", G_TYPE_INT, GST_AUDIO_INFO_RATE (info), NULL);
181
182 GST_DEBUG_OBJECT (self, "input caps: %" GST_PTR_FORMAT, input_caps);
183 GST_DEBUG_OBJECT (self, "output caps: %" GST_PTR_FORMAT, output_caps);
184
185 ret = WebRtcIsac_SetEncSampRate (self->isac, GST_AUDIO_INFO_RATE (info));
186 CHECK_ISAC_RET (ret, SetEncSampleRate);
187
188 /* TODO: add support for automatically adjusted bit rate and frame
189 * length (codingMode = 0). */
190 ret = WebRtcIsac_EncoderInit (self->isac, 1);
191 CHECK_ISAC_RET (ret, EncoderInit);
192
193 if (self->mode == ENCODER_MODE_SUPER_WIDEBAND && self->output_frame_len != 30) {
194 GST_ERROR_OBJECT (self,
195 "Only output-frame-len=30 is supported in super-wideband mode (32 kHz)");
196 return FALSE;
197 }
198
199 if (self->mode == ENCODER_MODE_WIDEBAND && (self->bitrate < 10000
200 || self->bitrate > 32000)) {
201 GST_ERROR_OBJECT (self,
202 "bitrate range is 10000 to 32000 bps in wideband mode (16 kHz)");
203 return FALSE;
204 } else if (self->mode == ENCODER_MODE_SUPER_WIDEBAND && (self->bitrate < 10000
205 || self->bitrate > 56000)) {
206 GST_ERROR_OBJECT (self,
207 "bitrate range is 10000 to 56000 bps in super-wideband mode (32 kHz)");
208 return FALSE;
209 }
210
211 ret = WebRtcIsac_Control (self->isac, self->bitrate, self->output_frame_len);
212 CHECK_ISAC_RET (ret, Control);
213
214 if (self->max_payload_size != GST_ISACENC_MAX_PAYLOAD_SIZE_DEFAULT) {
215 GST_DEBUG_OBJECT (self, "set max payload size to %d bytes",
216 self->max_payload_size);
217 ret = WebRtcIsac_SetMaxPayloadSize (self->isac, self->max_payload_size);
218 CHECK_ISAC_RET (ret, SetMaxPayloadSize);
219 }
220
221 if (self->max_rate != GST_ISACENC_MAX_RATE_DEFAULT) {
222 GST_DEBUG_OBJECT (self, "set max rate to %d bits/sec", self->max_rate);
223 ret = WebRtcIsac_SetMaxRate (self->isac, self->max_rate);
224 CHECK_ISAC_RET (ret, SetMaxRate);
225 }
226
227 result = gst_audio_encoder_set_output_format (enc, output_caps);
228
229 /* input size is 10ms */
230 self->samples_per_frame = GST_AUDIO_INFO_RATE (info) / 100;
231 self->frame_size = self->samples_per_frame * GST_AUDIO_INFO_BPS (info);
232
233 GST_DEBUG_OBJECT (self, "input frame: %d samples, %" G_GSIZE_FORMAT " bytes",
234 self->samples_per_frame, self->frame_size);
235
236 gst_audio_encoder_set_frame_samples_min (enc, self->samples_per_frame);
237 gst_audio_encoder_set_frame_samples_max (enc, self->samples_per_frame);
238 gst_audio_encoder_set_hard_min (enc, TRUE);
239
240 gst_caps_unref (input_caps);
241 gst_caps_unref (output_caps);
242 return result;
243 }
244
245 static GstFlowReturn
gst_isacenc_handle_frame(GstAudioEncoder * enc,GstBuffer * input)246 gst_isacenc_handle_frame (GstAudioEncoder * enc, GstBuffer * input)
247 {
248 GstIsacEnc *self = GST_ISACENC (enc);
249 GstMapInfo map_read;
250 gint16 ret;
251 GstFlowReturn flow_ret = GST_FLOW_ERROR;
252 gsize offset = 0;
253
254 /* Can't drain the encoder */
255 if (!input)
256 return GST_FLOW_OK;
257
258 if (!gst_buffer_map (input, &map_read, GST_MAP_READ)) {
259 GST_ELEMENT_ERROR (self, RESOURCE, READ, ("Failed to map input buffer"),
260 (NULL));
261 return GST_FLOW_ERROR;
262 }
263
264 GST_LOG_OBJECT (self, "Received %" G_GSIZE_FORMAT " bytes", map_read.size);
265
266 while (offset + self->frame_size <= map_read.size) {
267 GstBuffer *output;
268 GstMapInfo map_write;
269
270 output = gst_audio_encoder_allocate_output_buffer (enc, OUTPUT_BUFFER_SIZE);
271 if (!gst_buffer_map (output, &map_write, GST_MAP_WRITE)) {
272 GST_ELEMENT_ERROR (self, RESOURCE, WRITE, ("Failed to map output buffer"),
273 (NULL));
274 gst_buffer_unref (output);
275 goto out;
276 }
277
278 ret =
279 WebRtcIsac_Encode (self->isac,
280 (const gint16 *) (map_read.data + offset), map_write.data);
281
282 gst_buffer_unmap (output, &map_write);
283 self->nb_processed_input_frames++;
284 offset += self->frame_size;
285
286 if (ret == 0) {
287 /* buffering */
288 gst_buffer_unref (output);
289 continue;
290 } else if (ret < 0) {
291 /* error */
292 gint16 code = WebRtcIsac_GetErrorCode (self->isac);
293 GST_ELEMENT_ERROR (self, LIBRARY, ENCODE, ("Failed to encode frame"),
294 ("Failed to encode: %s (%d)", isac_error_code_to_str (code), code));
295 gst_buffer_unref (output);
296 goto out;
297 } else {
298 /* encoded */
299 GST_LOG_OBJECT (self, "Encoded %d input frames to %d bytes",
300 self->nb_processed_input_frames, ret);
301
302 gst_buffer_set_size (output, ret);
303
304 flow_ret =
305 gst_audio_encoder_finish_frame (enc, output,
306 self->nb_processed_input_frames * self->samples_per_frame);
307
308 if (flow_ret != GST_FLOW_OK)
309 goto out;
310
311 self->nb_processed_input_frames = 0;
312 }
313 }
314
315 flow_ret = GST_FLOW_OK;
316 out:
317 gst_buffer_unmap (input, &map_read);
318 return flow_ret;
319 }
320
321 static void
gst_isacenc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)322 gst_isacenc_set_property (GObject * object, guint prop_id,
323 const GValue * value, GParamSpec * pspec)
324 {
325 GstIsacEnc *self = GST_ISACENC (object);
326
327 switch (prop_id) {
328 case PROP_OUTPUT_FRAME_LEN:
329 self->output_frame_len = g_value_get_enum (value);
330 break;
331 case PROP_BITRATE:
332 self->bitrate = g_value_get_int (value);
333 break;
334 case PROP_MAX_PAYLOAD_SIZE:
335 self->max_payload_size = g_value_get_int (value);
336 break;
337 case PROP_MAX_RATE:
338 self->max_rate = g_value_get_int (value);
339 break;
340 default:
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 break;
343 }
344 }
345
346 static void
gst_isacenc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)347 gst_isacenc_get_property (GObject * object, guint prop_id,
348 GValue * value, GParamSpec * pspec)
349 {
350 GstIsacEnc *self = GST_ISACENC (object);
351
352 switch (prop_id) {
353 case PROP_OUTPUT_FRAME_LEN:
354 g_value_set_enum (value, self->output_frame_len);
355 break;
356 case PROP_BITRATE:
357 g_value_set_int (value, self->bitrate);
358 break;
359 case PROP_MAX_PAYLOAD_SIZE:
360 g_value_set_int (value, self->max_payload_size);
361 break;
362 case PROP_MAX_RATE:
363 g_value_set_int (value, self->max_rate);
364 break;
365 default:
366 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
367 break;
368 }
369 }
370
371 static void
gst_isacenc_class_init(GstIsacEncClass * klass)372 gst_isacenc_class_init (GstIsacEncClass * klass)
373 {
374 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
375 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
376 GstAudioEncoderClass *base_class = GST_AUDIO_ENCODER_CLASS (klass);
377
378 gobject_class->set_property = gst_isacenc_set_property;
379 gobject_class->get_property = gst_isacenc_get_property;
380
381 base_class->start = GST_DEBUG_FUNCPTR (gst_isacenc_start);
382 base_class->stop = GST_DEBUG_FUNCPTR (gst_isacenc_stop);
383 base_class->set_format = GST_DEBUG_FUNCPTR (gst_isacenc_set_format);
384 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_isacenc_handle_frame);
385
386 g_object_class_install_property (gobject_class, PROP_OUTPUT_FRAME_LEN,
387 g_param_spec_enum ("output-frame-len", "Output Frame Length",
388 "Length, in ms, of output frames",
389 GST_TYPE_ISACENC_OUTPUT_FRAME_LEN,
390 GST_ISACENC_OUTPUT_FRAME_LEN_DEFAULT,
391 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
392 GST_PARAM_MUTABLE_READY));
393
394 g_object_class_install_property (gobject_class, PROP_BITRATE,
395 g_param_spec_int ("bitrate", "Bitrate",
396 "Average Bitrate (ABR) in bits/sec",
397 10000, 56000,
398 GST_ISACENC_BITRATE_DEFAULT,
399 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
400 GST_PARAM_MUTABLE_READY));
401
402 g_object_class_install_property (gobject_class, PROP_MAX_PAYLOAD_SIZE,
403 g_param_spec_int ("max-payload-size", "Max Payload Size",
404 "Maximum payload size, in bytes. Range is 120 to 400 at 16 kHz "
405 "and 120 to 600 at 32 kHz (-1 = encoder default)",
406 -1, 600,
407 GST_ISACENC_MAX_PAYLOAD_SIZE_DEFAULT,
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
409 GST_PARAM_MUTABLE_READY));
410
411 g_object_class_install_property (gobject_class, PROP_MAX_RATE,
412 g_param_spec_int ("max-rate", "Max Rate",
413 "Maximum rate, in bits/sec, which the codec may not exceed for any "
414 "signal packet. Range is 32000 to 53400 at 16 kHz "
415 "and 32000 to 160000 at 32 kHz (-1 = encoder default)",
416 -1, 160000,
417 GST_ISACENC_MAX_PAYLOAD_SIZE_DEFAULT,
418 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
419 GST_PARAM_MUTABLE_READY));
420
421 gst_element_class_set_static_metadata (gstelement_class, "iSAC encoder",
422 "Codec/Encoder/Audio",
423 "iSAC audio encoder",
424 "Guillaume Desmottes <guillaume.desmottes@collabora.com>");
425
426 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
427 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
428 }
429
430 static void
gst_isacenc_init(GstIsacEnc * self)431 gst_isacenc_init (GstIsacEnc * self)
432 {
433 self->output_frame_len = GST_ISACENC_OUTPUT_FRAME_LEN_DEFAULT;
434 self->bitrate = GST_ISACENC_BITRATE_DEFAULT;
435 self->max_payload_size = GST_ISACENC_MAX_PAYLOAD_SIZE_DEFAULT;
436 self->max_rate = GST_ISACENC_MAX_RATE_DEFAULT;
437 }
438