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