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-isacdec
25 * @short_description: iSAC audio decoder
35 #include "gstisacdec.h"
36 #include "gstisacutils.h"
38 #include <modules/audio_coding/codecs/isac/main/include/isac.h>
40 GST_DEBUG_CATEGORY_STATIC (isacdec_debug);
41 #define GST_CAT_DEFAULT isacdec_debug
43 #define SAMPLE_SIZE 2 /* 16-bits samples */
44 #define MAX_OUTPUT_SAMPLES 960 /* decoder produces max 960 samples */
45 #define MAX_OUTPUT_SIZE (SAMPLE_SIZE * MAX_OUTPUT_SAMPLES)
47 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
50 GST_STATIC_CAPS ("audio/isac, "
51 "rate = (int) { 16000, 32000 }, " "channels = (int) 1")
54 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
57 GST_STATIC_CAPS ("audio/x-raw, "
58 "format = (string) " GST_AUDIO_NE (S16) ", "
59 "rate = (int) { 16000, 32000 }, "
60 "layout = (string) interleaved, " "channels = (int) 1")
66 GstAudioDecoder parent;
73 #define gst_isacdec_parent_class parent_class
74 G_DEFINE_TYPE_WITH_CODE (GstIsacDec, gst_isacdec,
75 GST_TYPE_AUDIO_DECODER,
76 GST_DEBUG_CATEGORY_INIT (isacdec_debug, "isacdec", 0,
77 "debug category for isacdec element"));
78 GST_ELEMENT_REGISTER_DEFINE (isacdec, "isacdec", GST_RANK_PRIMARY,
82 gst_isacdec_start (GstAudioDecoder * dec)
84 GstIsacDec *self = GST_ISACDEC (dec);
87 g_assert (!self->isac);
88 ret = WebRtcIsac_Create (&self->isac);
89 CHECK_ISAC_RET (ret, Create);
95 gst_isacdec_stop (GstAudioDecoder * dec)
97 GstIsacDec *self = GST_ISACDEC (dec);
102 ret = WebRtcIsac_Free (self->isac);
103 CHECK_ISAC_RET (ret, Free);
111 gst_isacdec_set_format (GstAudioDecoder * dec, GstCaps * input_caps)
113 GstIsacDec *self = GST_ISACDEC (dec);
114 GstAudioInfo output_format;
119 GstCaps *output_caps;
121 GST_DEBUG_OBJECT (self, "input caps: %" GST_PTR_FORMAT, input_caps);
123 s = gst_caps_get_structure (input_caps, 0);
127 if (!gst_structure_get_int (s, "rate", &rate)) {
128 GST_ERROR_OBJECT (self, "'rate' missing in input caps: %" GST_PTR_FORMAT,
133 if (!gst_structure_get_int (s, "channels", &channels)) {
134 GST_ERROR_OBJECT (self,
135 "'channels' missing in input caps: %" GST_PTR_FORMAT, input_caps);
139 gst_audio_info_set_format (&output_format, GST_AUDIO_FORMAT_S16LE, rate,
142 output_caps = gst_audio_info_to_caps (&output_format);
143 GST_DEBUG_OBJECT (self, "output caps: %" GST_PTR_FORMAT, output_caps);
144 gst_caps_unref (output_caps);
146 ret = WebRtcIsac_SetDecSampRate (self->isac, rate);
147 CHECK_ISAC_RET (ret, SetDecSampleRate);
149 WebRtcIsac_DecoderInit (self->isac);
151 result = gst_audio_decoder_set_output_format (dec, &output_format);
153 gst_audio_decoder_set_plc_aware (dec, TRUE);
159 gst_isacdec_plc (GstIsacDec * self, GstClockTime duration)
161 GstAudioDecoder *dec = GST_AUDIO_DECODER (self);
164 GstMapInfo map_write;
167 /* Decoder produces 30 ms PLC frames */
168 nb_plc_frames = duration / (30 * GST_MSECOND);
170 GST_DEBUG_OBJECT (self,
171 "GAP of %" GST_TIME_FORMAT " detected, request PLC for %d frames",
172 GST_TIME_ARGS (duration), nb_plc_frames);
175 gst_audio_decoder_allocate_output_buffer (dec,
176 nb_plc_frames * MAX_OUTPUT_SIZE);
178 if (!gst_buffer_map (output, &map_write, GST_MAP_WRITE)) {
179 GST_ERROR_OBJECT (self, "Failed to map output buffer");
180 gst_buffer_unref (output);
181 return GST_FLOW_ERROR;
185 WebRtcIsac_DecodePlc (self->isac, (gint16 *) map_write.data,
188 gst_buffer_unmap (output, &map_write);
192 gint16 code = WebRtcIsac_GetErrorCode (self->isac);
193 GST_WARNING_OBJECT (self, "Failed to produce PLC: %s (%d)",
194 isac_error_code_to_str (code), code);
195 gst_buffer_unref (output);
196 return GST_FLOW_ERROR;
197 } else if (ret == 0) {
198 GST_DEBUG_OBJECT (self, "Decoder didn't produce any PLC frame");
199 gst_buffer_unref (output);
203 gst_buffer_set_size (output, ret * SAMPLE_SIZE);
205 GST_LOG_OBJECT (self, "Produced %" G_GSIZE_FORMAT " PLC samples", ret);
207 return gst_audio_decoder_finish_frame (dec, output, 1);
211 gst_isacdec_handle_frame (GstAudioDecoder * dec, GstBuffer * input)
213 GstIsacDec *self = GST_ISACDEC (dec);
214 GstMapInfo map_read, map_write;
216 gint16 ret, speech_type[1];
219 /* Can't drain the decoder */
223 if (!gst_buffer_get_size (input)) {
224 /* Base class detected a gap in the stream, try to do PLC */
225 return gst_isacdec_plc (self, GST_BUFFER_DURATION (input));
228 if (!gst_buffer_map (input, &map_read, GST_MAP_READ)) {
229 GST_ELEMENT_ERROR (self, RESOURCE, READ, ("Failed to map input buffer"),
231 return GST_FLOW_ERROR;
234 input_size = map_read.size;
236 output = gst_audio_decoder_allocate_output_buffer (dec, MAX_OUTPUT_SIZE);
237 if (!gst_buffer_map (output, &map_write, GST_MAP_WRITE)) {
238 GST_ELEMENT_ERROR (self, RESOURCE, WRITE, ("Failed to map output buffer"),
240 gst_buffer_unref (output);
241 gst_buffer_unmap (input, &map_read);
242 return GST_FLOW_ERROR;
245 ret = WebRtcIsac_Decode (self->isac, map_read.data, map_read.size,
246 (gint16 *) map_write.data, speech_type);
248 gst_buffer_unmap (input, &map_read);
249 gst_buffer_unmap (output, &map_write);
253 gint16 code = WebRtcIsac_GetErrorCode (self->isac);
254 GST_WARNING_OBJECT (self, "Failed to decode: %s (%d)",
255 isac_error_code_to_str (code), code);
256 gst_buffer_unref (output);
257 /* Give a chance to decode next frames */
259 } else if (ret == 0) {
260 GST_DEBUG_OBJECT (self, "Decoder didn't produce any frame");
261 gst_buffer_unref (output);
264 gst_buffer_set_size (output, ret * SAMPLE_SIZE);
267 GST_LOG_OBJECT (self, "Decoded %d samples from %" G_GSIZE_FORMAT " bytes",
270 return gst_audio_decoder_finish_frame (dec, output, 1);
274 gst_isacdec_class_init (GstIsacDecClass * klass)
276 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
277 GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
279 base_class->start = GST_DEBUG_FUNCPTR (gst_isacdec_start);
280 base_class->stop = GST_DEBUG_FUNCPTR (gst_isacdec_stop);
281 base_class->set_format = GST_DEBUG_FUNCPTR (gst_isacdec_set_format);
282 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_isacdec_handle_frame);
284 gst_element_class_set_static_metadata (gstelement_class, "iSAC decoder",
285 "Codec/Decoder/Audio",
286 "iSAC audio decoder",
287 "Guillaume Desmottes <guillaume.desmottes@collabora.com>");
289 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
290 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
294 gst_isacdec_init (GstIsacDec * self)