3 * Copyright (C) 2020 Collabora Ltd.
4 * Author: Guillaume Desmottes <guillaume.desmottes@collabora.com>, Collabora Ltd.
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.
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.
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.
23 * SECTION:element-isacenc
25 * @short_description: iSAC audio encoder
35 #include "gstisacenc.h"
36 #include "gstisacutils.h"
38 #include <modules/audio_coding/codecs/isac/main/include/isac.h>
40 GST_DEBUG_CATEGORY_STATIC (isacenc_debug);
41 #define GST_CAT_DEFAULT isacenc_debug
43 /* Buffer size used in the simpleKenny.c test app from webrtc */
44 #define OUTPUT_BUFFER_SIZE 1200
46 #define GST_TYPE_ISACENC_OUTPUT_FRAME_LEN (gst_isacenc_output_frame_len_get_type ())
48 gst_isacenc_output_frame_len_get_type (void)
50 static GType 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)"},
59 qtype = g_enum_register_static ("GstIsacEncOutputFrameLen", values);
67 PROP_OUTPUT_FRAME_LEN,
69 PROP_MAX_PAYLOAD_SIZE,
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)
78 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
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")
87 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
90 GST_STATIC_CAPS ("audio/isac, "
91 "rate = (int) { 16000, 32000 }, " "channels = (int) 1")
96 ENCODER_MODE_WIDEBAND, /* 16 kHz */
97 ENCODER_MODE_SUPER_WIDEBAND, /* 32 kHz */
103 GstAudioEncoder parent;
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 */
112 gint output_frame_len;
114 gint max_payload_size;
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,
127 gst_isacenc_start (GstAudioEncoder * enc)
129 GstIsacEnc *self = GST_ISACENC (enc);
132 g_assert (!self->isac);
133 ret = WebRtcIsac_Create (&self->isac);
134 CHECK_ISAC_RET (ret, Create);
136 self->nb_processed_input_frames = 0;
142 gst_isacenc_stop (GstAudioEncoder * enc)
144 GstIsacEnc *self = GST_ISACENC (enc);
149 ret = WebRtcIsac_Free (self->isac);
150 CHECK_ISAC_RET (ret, Free);
158 gst_isacenc_set_format (GstAudioEncoder * enc, GstAudioInfo * info)
160 GstIsacEnc *self = GST_ISACENC (enc);
161 GstCaps *input_caps, *output_caps;
165 switch (GST_AUDIO_INFO_RATE (info)) {
167 self->mode = ENCODER_MODE_WIDEBAND;
170 self->mode = ENCODER_MODE_SUPER_WIDEBAND;
173 g_assert_not_reached ();
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);
182 GST_DEBUG_OBJECT (self, "input caps: %" GST_PTR_FORMAT, input_caps);
183 GST_DEBUG_OBJECT (self, "output caps: %" GST_PTR_FORMAT, output_caps);
185 ret = WebRtcIsac_SetEncSampRate (self->isac, GST_AUDIO_INFO_RATE (info));
186 CHECK_ISAC_RET (ret, SetEncSampleRate);
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);
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)");
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)");
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)");
211 ret = WebRtcIsac_Control (self->isac, self->bitrate, self->output_frame_len);
212 CHECK_ISAC_RET (ret, Control);
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);
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);
227 result = gst_audio_encoder_set_output_format (enc, output_caps);
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);
233 GST_DEBUG_OBJECT (self, "input frame: %d samples, %" G_GSIZE_FORMAT " bytes",
234 self->samples_per_frame, self->frame_size);
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);
240 gst_caps_unref (input_caps);
241 gst_caps_unref (output_caps);
246 gst_isacenc_handle_frame (GstAudioEncoder * enc, GstBuffer * input)
248 GstIsacEnc *self = GST_ISACENC (enc);
251 GstFlowReturn flow_ret = GST_FLOW_ERROR;
254 /* Can't drain the encoder */
258 if (!gst_buffer_map (input, &map_read, GST_MAP_READ)) {
259 GST_ELEMENT_ERROR (self, RESOURCE, READ, ("Failed to map input buffer"),
261 return GST_FLOW_ERROR;
264 GST_LOG_OBJECT (self, "Received %" G_GSIZE_FORMAT " bytes", map_read.size);
266 while (offset + self->frame_size <= map_read.size) {
268 GstMapInfo map_write;
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"),
274 gst_buffer_unref (output);
279 WebRtcIsac_Encode (self->isac,
280 (const gint16 *) (map_read.data + offset), map_write.data);
282 gst_buffer_unmap (output, &map_write);
283 self->nb_processed_input_frames++;
284 offset += self->frame_size;
288 gst_buffer_unref (output);
290 } else if (ret < 0) {
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);
299 GST_LOG_OBJECT (self, "Encoded %d input frames to %d bytes",
300 self->nb_processed_input_frames, ret);
302 gst_buffer_set_size (output, ret);
305 gst_audio_encoder_finish_frame (enc, output,
306 self->nb_processed_input_frames * self->samples_per_frame);
308 if (flow_ret != GST_FLOW_OK)
311 self->nb_processed_input_frames = 0;
315 flow_ret = GST_FLOW_OK;
317 gst_buffer_unmap (input, &map_read);
322 gst_isacenc_set_property (GObject * object, guint prop_id,
323 const GValue * value, GParamSpec * pspec)
325 GstIsacEnc *self = GST_ISACENC (object);
328 case PROP_OUTPUT_FRAME_LEN:
329 self->output_frame_len = g_value_get_enum (value);
332 self->bitrate = g_value_get_int (value);
334 case PROP_MAX_PAYLOAD_SIZE:
335 self->max_payload_size = g_value_get_int (value);
338 self->max_rate = g_value_get_int (value);
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
347 gst_isacenc_get_property (GObject * object, guint prop_id,
348 GValue * value, GParamSpec * pspec)
350 GstIsacEnc *self = GST_ISACENC (object);
353 case PROP_OUTPUT_FRAME_LEN:
354 g_value_set_enum (value, self->output_frame_len);
357 g_value_set_int (value, self->bitrate);
359 case PROP_MAX_PAYLOAD_SIZE:
360 g_value_set_int (value, self->max_payload_size);
363 g_value_set_int (value, self->max_rate);
366 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
372 gst_isacenc_class_init (GstIsacEncClass * klass)
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);
378 gobject_class->set_property = gst_isacenc_set_property;
379 gobject_class->get_property = gst_isacenc_get_property;
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);
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));
394 g_object_class_install_property (gobject_class, PROP_BITRATE,
395 g_param_spec_int ("bitrate", "Bitrate",
396 "Average Bitrate (ABR) in bits/sec",
398 GST_ISACENC_BITRATE_DEFAULT,
399 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
400 GST_PARAM_MUTABLE_READY));
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)",
407 GST_ISACENC_MAX_PAYLOAD_SIZE_DEFAULT,
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
409 GST_PARAM_MUTABLE_READY));
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)",
417 GST_ISACENC_MAX_PAYLOAD_SIZE_DEFAULT,
418 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
419 GST_PARAM_MUTABLE_READY));
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>");
426 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
427 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
431 gst_isacenc_init (GstIsacEnc * self)
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;