1 /* GStreamer Adaptive Multi-Rate parser plugin
2 * Copyright (C) 2006 Edgard Lima <edgard.lima@indt.org.br>
3 * Copyright (C) 2008 Nokia Corporation. All rights reserved.
5 * Contact: Stefan Kost <stefan.kost@nokia.com>
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.
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.
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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:element-amrparse
25 * @short_description: AMR parser
26 * @see_also: #GstAmrnbDec, #GstAmrnbEnc
28 * This is an AMR parser capable of handling both narrow-band and wideband
32 * <title>Example launch line</title>
34 * gst-launch filesrc location=abc.amr ! amrparse ! amrdec ! audioresample ! audioconvert ! alsasink
45 #include "gstamrparse.h"
48 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
51 GST_STATIC_CAPS ("audio/AMR, " "rate = (int) 8000, " "channels = (int) 1;"
52 "audio/AMR-WB, " "rate = (int) 16000, " "channels = (int) 1;")
55 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
58 GST_STATIC_CAPS ("audio/x-amr-nb-sh; audio/x-amr-wb-sh"));
60 GST_DEBUG_CATEGORY_STATIC (amrparse_debug);
61 #define GST_CAT_DEFAULT amrparse_debug
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 };
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 };
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
74 gboolean gst_amr_parse_start (GstBaseParse * parse);
75 gboolean gst_amr_parse_stop (GstBaseParse * parse);
77 static gboolean gst_amr_parse_sink_setcaps (GstBaseParse * parse,
80 gboolean gst_amr_parse_check_valid_frame (GstBaseParse * parse,
81 GstBaseParseFrame * frame, guint * framesize, gint * skipsize);
83 GstFlowReturn gst_amr_parse_parse_frame (GstBaseParse * parse,
84 GstBaseParseFrame * frame);
86 G_DEFINE_TYPE (GstAmrParse, gst_amr_parse, GST_TYPE_BASE_PARSE);
89 * gst_amr_parse_class_init:
90 * @klass: GstAmrParseClass.
94 gst_amr_parse_class_init (GstAmrParseClass * klass)
96 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
97 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
99 GST_DEBUG_CATEGORY_INIT (amrparse_debug, "amrparse", 0,
100 "AMR-NB audio stream parser");
102 gst_element_class_add_pad_template (element_class,
103 gst_static_pad_template_get (&sink_template));
104 gst_element_class_add_pad_template (element_class,
105 gst_static_pad_template_get (&src_template));
107 gst_element_class_set_details_simple (element_class,
108 "AMR audio stream parser", "Codec/Parser/Audio",
109 "Adaptive Multi-Rate audio parser",
110 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
112 parse_class->start = GST_DEBUG_FUNCPTR (gst_amr_parse_start);
113 parse_class->stop = GST_DEBUG_FUNCPTR (gst_amr_parse_stop);
114 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_setcaps);
115 parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_amr_parse_parse_frame);
116 parse_class->check_valid_frame =
117 GST_DEBUG_FUNCPTR (gst_amr_parse_check_valid_frame);
122 * gst_amr_parse_init:
123 * @amrparse: #GstAmrParse
124 * @klass: #GstAmrParseClass.
128 gst_amr_parse_init (GstAmrParse * amrparse)
131 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 62);
132 GST_DEBUG ("initialized");
138 * gst_amr_parse_set_src_caps:
139 * @amrparse: #GstAmrParse.
141 * Set source pad caps according to current knowledge about the
144 * Returns: TRUE if caps were successfully set.
147 gst_amr_parse_set_src_caps (GstAmrParse * amrparse)
149 GstCaps *src_caps = NULL;
150 gboolean res = FALSE;
152 if (amrparse->wide) {
153 GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-WB");
154 src_caps = gst_caps_new_simple ("audio/AMR-WB",
155 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 16000, NULL);
157 GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-NB");
158 /* Max. size of NB frame is 31 bytes, so we can set the min. frame
159 size to 32 (+1 for next frame header) */
160 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 32);
161 src_caps = gst_caps_new_simple ("audio/AMR",
162 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
164 gst_pad_use_fixed_caps (GST_BASE_PARSE (amrparse)->srcpad);
165 res = gst_pad_set_caps (GST_BASE_PARSE (amrparse)->srcpad, src_caps);
166 gst_caps_unref (src_caps);
172 * gst_amr_parse_sink_setcaps:
176 * Returns: TRUE on success.
179 gst_amr_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
181 GstAmrParse *amrparse;
182 GstStructure *structure;
185 amrparse = GST_AMR_PARSE (parse);
186 structure = gst_caps_get_structure (caps, 0);
187 name = gst_structure_get_name (structure);
189 GST_DEBUG_OBJECT (amrparse, "setcaps: %s", name);
191 if (!strncmp (name, "audio/x-amr-wb-sh", 17)) {
192 amrparse->block_size = block_size_wb;
194 } else if (!strncmp (name, "audio/x-amr-nb-sh", 17)) {
195 amrparse->block_size = block_size_nb;
198 GST_WARNING ("Unknown caps");
202 amrparse->need_header = FALSE;
203 gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
204 gst_amr_parse_set_src_caps (amrparse);
209 * gst_amr_parse_parse_header:
210 * @amrparse: #GstAmrParse
211 * @data: Header data to be parsed.
212 * @skipsize: Output argument where the frame size will be stored.
214 * Check if the given data contains an AMR mime header.
216 * Returns: TRUE on success.
219 gst_amr_parse_parse_header (GstAmrParse * amrparse,
220 const guint8 * data, gint * skipsize)
222 GST_DEBUG_OBJECT (amrparse, "Parsing header data");
224 if (!memcmp (data, "#!AMR-WB\n", 9)) {
225 GST_DEBUG_OBJECT (amrparse, "AMR-WB detected");
226 amrparse->block_size = block_size_wb;
227 amrparse->wide = TRUE;
228 *skipsize = amrparse->header = 9;
229 } else if (!memcmp (data, "#!AMR\n", 6)) {
230 GST_DEBUG_OBJECT (amrparse, "AMR-NB detected");
231 amrparse->block_size = block_size_nb;
232 amrparse->wide = FALSE;
233 *skipsize = amrparse->header = 6;
237 gst_amr_parse_set_src_caps (amrparse);
243 * gst_amr_parse_check_valid_frame:
244 * @parse: #GstBaseParse.
245 * @buffer: #GstBuffer.
246 * @framesize: Output variable where the found frame size is put.
247 * @skipsize: Output variable which tells how much data needs to be skipped
248 * until a frame header is found.
250 * Implementation of "check_valid_frame" vmethod in #GstBaseParse class.
252 * Returns: TRUE if the given data contains valid frame.
255 gst_amr_parse_check_valid_frame (GstBaseParse * parse,
256 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
261 gint fsize, mode, dsize;
262 GstAmrParse *amrparse;
263 gboolean ret = FALSE;
265 amrparse = GST_AMR_PARSE (parse);
266 buffer = frame->buffer;
268 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
271 GST_LOG ("buffer: %d bytes", dsize);
273 if (amrparse->need_header) {
274 if (dsize >= AMR_MIME_HEADER_SIZE &&
275 gst_amr_parse_parse_header (amrparse, data, skipsize)) {
276 amrparse->need_header = FALSE;
277 gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
279 GST_WARNING ("media doesn't look like a AMR format");
281 /* We return FALSE, so this frame won't get pushed forward. Instead,
282 the "skip" value is set, so next time we will receive a valid frame. */
286 /* Does this look like a possible frame header candidate? */
287 if ((data[0] & 0x83) == 0) {
288 /* Yep. Retrieve the frame size */
289 mode = (data[0] >> 3) & 0x0F;
290 fsize = amrparse->block_size[mode] + 1; /* +1 for the header byte */
292 /* We recognize this data as a valid frame when:
293 * - We are in sync. There is no need for extra checks then
294 * - We are in EOS. There might not be enough data to check next frame
295 * - Sync is lost, but the following data after this frame seem
296 * to contain a valid header as well (and there is enough data to
297 * perform this check)
300 gboolean found = FALSE;
302 /* in sync, no further check */
303 if (!GST_BASE_PARSE_LOST_SYNC (parse)) {
305 } else if (dsize > fsize) {
306 /* enough data, check for next sync */
307 if ((data[fsize] & 0x83) == 0)
309 } else if (GST_BASE_PARSE_DRAINING (parse)) {
310 /* not enough, but draining, so ok */
313 /* indicate we need not skip, but need more data */
315 *framesize = fsize + 1;
323 GST_LOG ("sync lost");
326 gst_buffer_unmap (buffer, data, size);
333 * gst_amr_parse_parse_frame:
334 * @parse: #GstBaseParse.
335 * @buffer: #GstBuffer.
337 * Implementation of "parse" vmethod in #GstBaseParse class.
339 * Returns: #GstFlowReturn defining the parsing status.
342 gst_amr_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
349 * gst_amr_parse_start:
350 * @parse: #GstBaseParse.
352 * Implementation of "start" vmethod in #GstBaseParse class.
354 * Returns: TRUE on success.
357 gst_amr_parse_start (GstBaseParse * parse)
359 GstAmrParse *amrparse;
361 amrparse = GST_AMR_PARSE (parse);
363 amrparse->need_header = TRUE;
364 amrparse->header = 0;
370 * gst_amr_parse_stop:
371 * @parse: #GstBaseParse.
373 * Implementation of "stop" vmethod in #GstBaseParse class.
375 * Returns: TRUE on success.
378 gst_amr_parse_stop (GstBaseParse * parse)
380 GstAmrParse *amrparse;
382 amrparse = GST_AMR_PARSE (parse);
384 amrparse->need_header = TRUE;
385 amrparse->header = 0;