1 /* iSAC decoder
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-isacdec
24 * @title: isacdec
25 * @short_description: iSAC audio decoder
26 *
27 * Since: 1.20
28 *
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "gstisacdec.h"
36 #include "gstisacutils.h"
37
38 #include <modules/audio_coding/codecs/isac/main/include/isac.h>
39
40 GST_DEBUG_CATEGORY_STATIC (isacdec_debug);
41 #define GST_CAT_DEFAULT isacdec_debug
42
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)
46
47 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
48 GST_PAD_SINK,
49 GST_PAD_ALWAYS,
50 GST_STATIC_CAPS ("audio/isac, "
51 "rate = (int) { 16000, 32000 }, " "channels = (int) 1")
52 );
53
54 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
55 GST_PAD_SRC,
56 GST_PAD_ALWAYS,
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")
61 );
62
63 struct _GstIsacDec
64 {
65 /*< private > */
66 GstAudioDecoder parent;
67
68 ISACStruct *isac;
69
70 /* properties */
71 };
72
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,
79 GST_TYPE_ISACDEC);
80
81 static gboolean
gst_isacdec_start(GstAudioDecoder * dec)82 gst_isacdec_start (GstAudioDecoder * dec)
83 {
84 GstIsacDec *self = GST_ISACDEC (dec);
85 gint16 ret;
86
87 g_assert (!self->isac);
88 ret = WebRtcIsac_Create (&self->isac);
89 CHECK_ISAC_RET (ret, Create);
90
91 return TRUE;
92 }
93
94 static gboolean
gst_isacdec_stop(GstAudioDecoder * dec)95 gst_isacdec_stop (GstAudioDecoder * dec)
96 {
97 GstIsacDec *self = GST_ISACDEC (dec);
98
99 if (self->isac) {
100 gint16 ret;
101
102 ret = WebRtcIsac_Free (self->isac);
103 CHECK_ISAC_RET (ret, Free);
104 self->isac = NULL;
105 }
106
107 return TRUE;
108 }
109
110 static gboolean
gst_isacdec_set_format(GstAudioDecoder * dec,GstCaps * input_caps)111 gst_isacdec_set_format (GstAudioDecoder * dec, GstCaps * input_caps)
112 {
113 GstIsacDec *self = GST_ISACDEC (dec);
114 GstAudioInfo output_format;
115 gint16 ret;
116 gboolean result;
117 GstStructure *s;
118 gint rate, channels;
119 GstCaps *output_caps;
120
121 GST_DEBUG_OBJECT (self, "input caps: %" GST_PTR_FORMAT, input_caps);
122
123 s = gst_caps_get_structure (input_caps, 0);
124 if (!s)
125 return FALSE;
126
127 if (!gst_structure_get_int (s, "rate", &rate)) {
128 GST_ERROR_OBJECT (self, "'rate' missing in input caps: %" GST_PTR_FORMAT,
129 input_caps);
130 return FALSE;
131 }
132
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);
136 return FALSE;
137 }
138
139 gst_audio_info_set_format (&output_format, GST_AUDIO_FORMAT_S16LE, rate,
140 channels, NULL);
141
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);
145
146 ret = WebRtcIsac_SetDecSampRate (self->isac, rate);
147 CHECK_ISAC_RET (ret, SetDecSampleRate);
148
149 WebRtcIsac_DecoderInit (self->isac);
150
151 result = gst_audio_decoder_set_output_format (dec, &output_format);
152
153 gst_audio_decoder_set_plc_aware (dec, TRUE);
154
155 return result;
156 }
157
158 static GstFlowReturn
gst_isacdec_plc(GstIsacDec * self,GstClockTime duration)159 gst_isacdec_plc (GstIsacDec * self, GstClockTime duration)
160 {
161 GstAudioDecoder *dec = GST_AUDIO_DECODER (self);
162 guint nb_plc_frames;
163 GstBuffer *output;
164 GstMapInfo map_write;
165 size_t ret;
166
167 /* Decoder produces 30 ms PLC frames */
168 nb_plc_frames = duration / (30 * GST_MSECOND);
169
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);
173
174 output =
175 gst_audio_decoder_allocate_output_buffer (dec,
176 nb_plc_frames * MAX_OUTPUT_SIZE);
177
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;
182 }
183
184 ret =
185 WebRtcIsac_DecodePlc (self->isac, (gint16 *) map_write.data,
186 nb_plc_frames);
187
188 gst_buffer_unmap (output, &map_write);
189
190 if (ret < 0) {
191 /* error */
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);
200 return GST_FLOW_OK;
201 }
202
203 gst_buffer_set_size (output, ret * SAMPLE_SIZE);
204
205 GST_LOG_OBJECT (self, "Produced %" G_GSIZE_FORMAT " PLC samples", ret);
206
207 return gst_audio_decoder_finish_frame (dec, output, 1);
208 }
209
210 static GstFlowReturn
gst_isacdec_handle_frame(GstAudioDecoder * dec,GstBuffer * input)211 gst_isacdec_handle_frame (GstAudioDecoder * dec, GstBuffer * input)
212 {
213 GstIsacDec *self = GST_ISACDEC (dec);
214 GstMapInfo map_read, map_write;
215 GstBuffer *output;
216 gint16 ret, speech_type[1];
217 gsize input_size;
218
219 /* Can't drain the decoder */
220 if (!input)
221 return GST_FLOW_OK;
222
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));
226 }
227
228 if (!gst_buffer_map (input, &map_read, GST_MAP_READ)) {
229 GST_ELEMENT_ERROR (self, RESOURCE, READ, ("Failed to map input buffer"),
230 (NULL));
231 return GST_FLOW_ERROR;
232 }
233
234 input_size = map_read.size;
235
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"),
239 (NULL));
240 gst_buffer_unref (output);
241 gst_buffer_unmap (input, &map_read);
242 return GST_FLOW_ERROR;
243 }
244
245 ret = WebRtcIsac_Decode (self->isac, map_read.data, map_read.size,
246 (gint16 *) map_write.data, speech_type);
247
248 gst_buffer_unmap (input, &map_read);
249 gst_buffer_unmap (output, &map_write);
250
251 if (ret < 0) {
252 /* error */
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 */
258 return GST_FLOW_OK;
259 } else if (ret == 0) {
260 GST_DEBUG_OBJECT (self, "Decoder didn't produce any frame");
261 gst_buffer_unref (output);
262 output = NULL;
263 } else {
264 gst_buffer_set_size (output, ret * SAMPLE_SIZE);
265 }
266
267 GST_LOG_OBJECT (self, "Decoded %d samples from %" G_GSIZE_FORMAT " bytes",
268 ret, input_size);
269
270 return gst_audio_decoder_finish_frame (dec, output, 1);
271 }
272
273 static void
gst_isacdec_class_init(GstIsacDecClass * klass)274 gst_isacdec_class_init (GstIsacDecClass * klass)
275 {
276 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
277 GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
278
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);
283
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>");
288
289 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
290 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
291 }
292
293 static void
gst_isacdec_init(GstIsacDec * self)294 gst_isacdec_init (GstIsacDec * self)
295 {
296 self->isac = NULL;
297 }
298