1 /* GStreamer Adaptive Multi-Rate parser plugin
2 * Copyright (C) 2006 Edgard Lima <edgard.lima@gmail.com>
3 * Copyright (C) 2008 Nokia Corporation. All rights reserved.
4 *
5 * Contact: Stefan Kost <stefan.kost@nokia.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 /**
24 * SECTION:element-amrparse
25 * @title: amrparse
26 * @short_description: AMR parser
27 * @see_also: #GstAmrnbDec, #GstAmrnbEnc
28 *
29 * This is an AMR parser capable of handling both narrow-band and wideband
30 * formats.
31 *
32 * ## Example launch line
33 * |[
34 * gst-launch-1.0 filesrc location=abc.amr ! amrparse ! amrdec ! audioresample ! audioconvert ! alsasink
35 * ]|
36 *
37 */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include <string.h>
44
45 #include "gstaudioparserselements.h"
46 #include "gstamrparse.h"
47 #include <gst/pbutils/pbutils.h>
48
49 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
50 GST_PAD_SRC,
51 GST_PAD_ALWAYS,
52 GST_STATIC_CAPS ("audio/AMR, " "rate = (int) 8000, " "channels = (int) 1;"
53 "audio/AMR-WB, " "rate = (int) 16000, " "channels = (int) 1;")
54 );
55
56 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
57 GST_PAD_SINK,
58 GST_PAD_ALWAYS,
59 GST_STATIC_CAPS ("audio/x-amr-nb-sh; audio/x-amr-wb-sh"));
60
61 GST_DEBUG_CATEGORY_STATIC (amrparse_debug);
62 #define GST_CAT_DEFAULT amrparse_debug
63
64 static const gint block_size_nb[16] =
65 { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
66
67 static const gint block_size_wb[16] =
68 { 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, -1, -1, -1, -1, 0, 0 };
69
70 /* AMR has a "hardcoded" framerate of 50fps */
71 #define AMR_FRAMES_PER_SECOND 50
72 #define AMR_FRAME_DURATION (GST_SECOND/AMR_FRAMES_PER_SECOND)
73 #define AMR_MIME_HEADER_SIZE 9
74
75 static gboolean gst_amr_parse_start (GstBaseParse * parse);
76 static gboolean gst_amr_parse_stop (GstBaseParse * parse);
77
78 static gboolean gst_amr_parse_sink_setcaps (GstBaseParse * parse,
79 GstCaps * caps);
80 static GstCaps *gst_amr_parse_sink_getcaps (GstBaseParse * parse,
81 GstCaps * filter);
82
83 static GstFlowReturn gst_amr_parse_handle_frame (GstBaseParse * parse,
84 GstBaseParseFrame * frame, gint * skipsize);
85 static GstFlowReturn gst_amr_parse_pre_push_frame (GstBaseParse * parse,
86 GstBaseParseFrame * frame);
87
88 G_DEFINE_TYPE (GstAmrParse, gst_amr_parse, GST_TYPE_BASE_PARSE);
89 GST_ELEMENT_REGISTER_DEFINE (amrparse, "amrparse",
90 GST_RANK_PRIMARY + 1, GST_TYPE_AMR_PARSE);
91
92 /**
93 * gst_amr_parse_class_init:
94 * @klass: GstAmrParseClass.
95 *
96 */
97 static void
gst_amr_parse_class_init(GstAmrParseClass * klass)98 gst_amr_parse_class_init (GstAmrParseClass * klass)
99 {
100 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
101 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
102
103 GST_DEBUG_CATEGORY_INIT (amrparse_debug, "amrparse", 0,
104 "AMR-NB audio stream parser");
105
106 gst_element_class_add_static_pad_template (element_class, &sink_template);
107 gst_element_class_add_static_pad_template (element_class, &src_template);
108
109 gst_element_class_set_static_metadata (element_class,
110 "AMR audio stream parser", "Codec/Parser/Audio",
111 "Adaptive Multi-Rate audio parser",
112 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
113
114 parse_class->start = GST_DEBUG_FUNCPTR (gst_amr_parse_start);
115 parse_class->stop = GST_DEBUG_FUNCPTR (gst_amr_parse_stop);
116 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_setcaps);
117 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_getcaps);
118 parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_amr_parse_handle_frame);
119 parse_class->pre_push_frame =
120 GST_DEBUG_FUNCPTR (gst_amr_parse_pre_push_frame);
121 }
122
123
124 /**
125 * gst_amr_parse_init:
126 * @amrparse: #GstAmrParse
127 * @klass: #GstAmrParseClass.
128 *
129 */
130 static void
gst_amr_parse_init(GstAmrParse * amrparse)131 gst_amr_parse_init (GstAmrParse * amrparse)
132 {
133 /* init rest */
134 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 62);
135 GST_DEBUG ("initialized");
136 GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (amrparse));
137 GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (amrparse));
138 }
139
140
141 /**
142 * gst_amr_parse_set_src_caps:
143 * @amrparse: #GstAmrParse.
144 *
145 * Set source pad caps according to current knowledge about the
146 * audio stream.
147 *
148 * Returns: TRUE if caps were successfully set.
149 */
150 static gboolean
gst_amr_parse_set_src_caps(GstAmrParse * amrparse)151 gst_amr_parse_set_src_caps (GstAmrParse * amrparse)
152 {
153 GstCaps *src_caps = NULL;
154 gboolean res = FALSE;
155
156 if (amrparse->wide) {
157 GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-WB");
158 src_caps = gst_caps_new_simple ("audio/AMR-WB",
159 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 16000, NULL);
160 } else {
161 GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-NB");
162 /* Max. size of NB frame is 31 bytes, so we can set the min. frame
163 size to 32 (+1 for next frame header) */
164 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 32);
165 src_caps = gst_caps_new_simple ("audio/AMR",
166 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
167 }
168 gst_pad_use_fixed_caps (GST_BASE_PARSE (amrparse)->srcpad);
169 res = gst_pad_set_caps (GST_BASE_PARSE (amrparse)->srcpad, src_caps);
170 gst_caps_unref (src_caps);
171 return res;
172 }
173
174
175 /**
176 * gst_amr_parse_sink_setcaps:
177 * @sinkpad: GstPad
178 * @caps: GstCaps
179 *
180 * Returns: TRUE on success.
181 */
182 static gboolean
gst_amr_parse_sink_setcaps(GstBaseParse * parse,GstCaps * caps)183 gst_amr_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
184 {
185 GstAmrParse *amrparse;
186 GstStructure *structure;
187 const gchar *name;
188
189 amrparse = GST_AMR_PARSE (parse);
190 structure = gst_caps_get_structure (caps, 0);
191 name = gst_structure_get_name (structure);
192
193 GST_DEBUG_OBJECT (amrparse, "setcaps: %s", name);
194
195 if (!strncmp (name, "audio/x-amr-wb-sh", 17)) {
196 amrparse->block_size = block_size_wb;
197 amrparse->wide = 1;
198 } else if (!strncmp (name, "audio/x-amr-nb-sh", 17)) {
199 amrparse->block_size = block_size_nb;
200 amrparse->wide = 0;
201 } else {
202 GST_WARNING ("Unknown caps");
203 return FALSE;
204 }
205
206 amrparse->need_header = FALSE;
207 gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
208 gst_amr_parse_set_src_caps (amrparse);
209 return TRUE;
210 }
211
212 /**
213 * gst_amr_parse_parse_header:
214 * @amrparse: #GstAmrParse
215 * @data: Header data to be parsed.
216 * @skipsize: Output argument where the frame size will be stored.
217 *
218 * Check if the given data contains an AMR mime header.
219 *
220 * Returns: TRUE on success.
221 */
222 static gboolean
gst_amr_parse_parse_header(GstAmrParse * amrparse,const guint8 * data,gint * skipsize)223 gst_amr_parse_parse_header (GstAmrParse * amrparse,
224 const guint8 * data, gint * skipsize)
225 {
226 GST_DEBUG_OBJECT (amrparse, "Parsing header data");
227
228 if (!memcmp (data, "#!AMR-WB\n", 9)) {
229 GST_DEBUG_OBJECT (amrparse, "AMR-WB detected");
230 amrparse->block_size = block_size_wb;
231 amrparse->wide = TRUE;
232 *skipsize = amrparse->header = 9;
233 } else if (!memcmp (data, "#!AMR\n", 6)) {
234 GST_DEBUG_OBJECT (amrparse, "AMR-NB detected");
235 amrparse->block_size = block_size_nb;
236 amrparse->wide = FALSE;
237 *skipsize = amrparse->header = 6;
238 } else
239 return FALSE;
240
241 gst_amr_parse_set_src_caps (amrparse);
242 return TRUE;
243 }
244
245
246 /**
247 * gst_amr_parse_check_valid_frame:
248 * @parse: #GstBaseParse.
249 * @buffer: #GstBuffer.
250 * @framesize: Output variable where the found frame size is put.
251 * @skipsize: Output variable which tells how much data needs to be skipped
252 * until a frame header is found.
253 *
254 * Implementation of "check_valid_frame" vmethod in #GstBaseParse class.
255 *
256 * Returns: TRUE if the given data contains valid frame.
257 */
258 static GstFlowReturn
gst_amr_parse_handle_frame(GstBaseParse * parse,GstBaseParseFrame * frame,gint * skipsize)259 gst_amr_parse_handle_frame (GstBaseParse * parse,
260 GstBaseParseFrame * frame, gint * skipsize)
261 {
262 GstBuffer *buffer;
263 GstMapInfo map;
264 gint fsize = 0, mode, dsize;
265 GstAmrParse *amrparse;
266 GstFlowReturn ret = GST_FLOW_OK;
267 gboolean found = FALSE;
268
269 amrparse = GST_AMR_PARSE (parse);
270 buffer = frame->buffer;
271
272 gst_buffer_map (buffer, &map, GST_MAP_READ);
273 dsize = map.size;
274
275 GST_LOG ("buffer: %d bytes", dsize);
276
277 if (amrparse->need_header) {
278 if (dsize >= AMR_MIME_HEADER_SIZE &&
279 gst_amr_parse_parse_header (amrparse, map.data, skipsize)) {
280 amrparse->need_header = FALSE;
281 gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
282 } else {
283 GST_WARNING ("media doesn't look like a AMR format");
284 }
285 /* We return FALSE, so this frame won't get pushed forward. Instead,
286 the "skip" value is set, so next time we will receive a valid frame. */
287 goto done;
288 }
289
290 *skipsize = 1;
291 /* Does this look like a possible frame header candidate? */
292 if ((map.data[0] & 0x83) == 0) {
293 /* Yep. Retrieve the frame size */
294 mode = (map.data[0] >> 3) & 0x0F;
295 fsize = amrparse->block_size[mode] + 1; /* +1 for the header byte */
296
297 /* We recognize this data as a valid frame when:
298 * - We are in sync. There is no need for extra checks then
299 * - We are in EOS. There might not be enough data to check next frame
300 * - Sync is lost, but the following data after this frame seem
301 * to contain a valid header as well (and there is enough data to
302 * perform this check)
303 */
304 if (fsize) {
305 *skipsize = 0;
306 /* in sync, no further check */
307 if (!GST_BASE_PARSE_LOST_SYNC (parse)) {
308 found = TRUE;
309 } else if (dsize > fsize) {
310 /* enough data, check for next sync */
311 if ((map.data[fsize] & 0x83) == 0)
312 found = TRUE;
313 } else if (GST_BASE_PARSE_DRAINING (parse)) {
314 /* not enough, but draining, so ok */
315 found = TRUE;
316 }
317 }
318 }
319
320 done:
321 gst_buffer_unmap (buffer, &map);
322
323 if (found && fsize <= map.size) {
324 ret = gst_base_parse_finish_frame (parse, frame, fsize);
325 }
326
327 return ret;
328 }
329
330 /**
331 * gst_amr_parse_start:
332 * @parse: #GstBaseParse.
333 *
334 * Implementation of "start" vmethod in #GstBaseParse class.
335 *
336 * Returns: TRUE on success.
337 */
338 static gboolean
gst_amr_parse_start(GstBaseParse * parse)339 gst_amr_parse_start (GstBaseParse * parse)
340 {
341 GstAmrParse *amrparse;
342
343 amrparse = GST_AMR_PARSE (parse);
344 GST_DEBUG ("start");
345 amrparse->need_header = TRUE;
346 amrparse->header = 0;
347 amrparse->sent_codec_tag = FALSE;
348 return TRUE;
349 }
350
351
352 /**
353 * gst_amr_parse_stop:
354 * @parse: #GstBaseParse.
355 *
356 * Implementation of "stop" vmethod in #GstBaseParse class.
357 *
358 * Returns: TRUE on success.
359 */
360 static gboolean
gst_amr_parse_stop(GstBaseParse * parse)361 gst_amr_parse_stop (GstBaseParse * parse)
362 {
363 GstAmrParse *amrparse;
364
365 amrparse = GST_AMR_PARSE (parse);
366 GST_DEBUG ("stop");
367 amrparse->need_header = TRUE;
368 amrparse->header = 0;
369 return TRUE;
370 }
371
372 static GstCaps *
gst_amr_parse_sink_getcaps(GstBaseParse * parse,GstCaps * filter)373 gst_amr_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter)
374 {
375 GstCaps *peercaps, *templ;
376 GstCaps *res;
377
378
379 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
380 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), filter);
381
382 if (peercaps) {
383 guint i, n;
384
385 /* Rename structure names */
386 peercaps = gst_caps_make_writable (peercaps);
387 n = gst_caps_get_size (peercaps);
388 for (i = 0; i < n; i++) {
389 GstStructure *s = gst_caps_get_structure (peercaps, i);
390
391 if (gst_structure_has_name (s, "audio/AMR"))
392 gst_structure_set_name (s, "audio/x-amr-nb-sh");
393 else
394 gst_structure_set_name (s, "audio/x-amr-wb-sh");
395 }
396
397 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
398 gst_caps_unref (peercaps);
399 res = gst_caps_make_writable (res);
400 /* Append the template caps because we still want to accept
401 * caps without any fields in the case upstream does not
402 * know anything.
403 */
404 gst_caps_append (res, templ);
405 } else {
406 res = templ;
407 }
408
409 if (filter) {
410 GstCaps *intersection;
411
412 intersection =
413 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
414 gst_caps_unref (res);
415 res = intersection;
416 }
417
418 return res;
419 }
420
421 static GstFlowReturn
gst_amr_parse_pre_push_frame(GstBaseParse * parse,GstBaseParseFrame * frame)422 gst_amr_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
423 {
424 GstAmrParse *amrparse = GST_AMR_PARSE (parse);
425
426 if (!amrparse->sent_codec_tag) {
427 GstTagList *taglist;
428 GstCaps *caps;
429
430 /* codec tag */
431 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
432 if (G_UNLIKELY (caps == NULL)) {
433 if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
434 GST_INFO_OBJECT (parse, "Src pad is flushing");
435 return GST_FLOW_FLUSHING;
436 } else {
437 GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
438 return GST_FLOW_NOT_NEGOTIATED;
439 }
440 }
441
442 taglist = gst_tag_list_new_empty ();
443 gst_pb_utils_add_codec_description_to_tag_list (taglist,
444 GST_TAG_AUDIO_CODEC, caps);
445 gst_caps_unref (caps);
446
447 gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
448 gst_tag_list_unref (taglist);
449
450 /* also signals the end of first-frame processing */
451 amrparse->sent_codec_tag = TRUE;
452 }
453
454 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
455
456 return GST_FLOW_OK;
457 }
458