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 (gst_amrparse_debug);
61 #define GST_CAT_DEFAULT gst_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, 5, 0, 0, 0, 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_amrparse_start (GstBaseParse * parse);
75 gboolean gst_amrparse_stop (GstBaseParse * parse);
77 static gboolean gst_amrparse_sink_setcaps (GstBaseParse * parse,
80 gboolean gst_amrparse_check_valid_frame (GstBaseParse * parse,
81 GstBaseParseFrame * frame, guint * framesize, gint * skipsize);
83 GstFlowReturn gst_amrparse_parse_frame (GstBaseParse * parse,
84 GstBaseParseFrame * frame);
86 #define _do_init(bla) \
87 GST_DEBUG_CATEGORY_INIT (gst_amrparse_debug, "amrparse", 0, \
88 "AMR-NB audio stream parser");
90 GST_BOILERPLATE_FULL (GstAmrParse, gst_amrparse, GstBaseParse,
91 GST_TYPE_BASE_PARSE, _do_init);
95 * gst_amrparse_base_init:
96 * @klass: #GstElementClass.
100 gst_amrparse_base_init (gpointer klass)
102 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
104 gst_element_class_add_pad_template (element_class,
105 gst_static_pad_template_get (&sink_template));
106 gst_element_class_add_pad_template (element_class,
107 gst_static_pad_template_get (&src_template));
109 gst_element_class_set_details_simple (element_class,
110 "AMR audio stream parser", "Codec/Parser/Audio",
111 "Adaptive Multi-Rate audio parser",
112 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
117 * gst_amrparse_class_init:
118 * @klass: GstAmrParseClass.
122 gst_amrparse_class_init (GstAmrParseClass * klass)
124 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
126 parse_class->start = GST_DEBUG_FUNCPTR (gst_amrparse_start);
127 parse_class->stop = GST_DEBUG_FUNCPTR (gst_amrparse_stop);
128 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_amrparse_sink_setcaps);
129 parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_amrparse_parse_frame);
130 parse_class->check_valid_frame =
131 GST_DEBUG_FUNCPTR (gst_amrparse_check_valid_frame);
137 * @amrparse: #GstAmrParse
138 * @klass: #GstAmrParseClass.
142 gst_amrparse_init (GstAmrParse * amrparse, GstAmrParseClass * klass)
145 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 62);
146 GST_DEBUG ("initialized");
152 * gst_amrparse_set_src_caps:
153 * @amrparse: #GstAmrParse.
155 * Set source pad caps according to current knowledge about the
158 * Returns: TRUE if caps were successfully set.
161 gst_amrparse_set_src_caps (GstAmrParse * amrparse)
163 GstCaps *src_caps = NULL;
164 gboolean res = FALSE;
166 if (amrparse->wide) {
167 GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-WB");
168 src_caps = gst_caps_new_simple ("audio/AMR-WB",
169 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 16000, NULL);
171 GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-NB");
172 /* Max. size of NB frame is 31 bytes, so we can set the min. frame
173 size to 32 (+1 for next frame header) */
174 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 32);
175 src_caps = gst_caps_new_simple ("audio/AMR",
176 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
178 gst_pad_use_fixed_caps (GST_BASE_PARSE (amrparse)->srcpad);
179 res = gst_pad_set_caps (GST_BASE_PARSE (amrparse)->srcpad, src_caps);
180 gst_caps_unref (src_caps);
186 * gst_amrparse_sink_setcaps:
190 * Returns: TRUE on success.
193 gst_amrparse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
195 GstAmrParse *amrparse;
196 GstStructure *structure;
199 amrparse = GST_AMRPARSE (parse);
200 structure = gst_caps_get_structure (caps, 0);
201 name = gst_structure_get_name (structure);
203 GST_DEBUG_OBJECT (amrparse, "setcaps: %s", name);
205 if (!strncmp (name, "audio/x-amr-wb-sh", 17)) {
206 amrparse->block_size = block_size_wb;
208 } else if (!strncmp (name, "audio/x-amr-nb-sh", 17)) {
209 amrparse->block_size = block_size_nb;
212 GST_WARNING ("Unknown caps");
216 amrparse->need_header = FALSE;
217 gst_base_parse_set_frame_props (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
218 gst_amrparse_set_src_caps (amrparse);
223 * gst_amrparse_parse_header:
224 * @amrparse: #GstAmrParse
225 * @data: Header data to be parsed.
226 * @skipsize: Output argument where the frame size will be stored.
228 * Check if the given data contains an AMR mime header.
230 * Returns: TRUE on success.
233 gst_amrparse_parse_header (GstAmrParse * amrparse,
234 const guint8 * data, gint * skipsize)
236 GST_DEBUG_OBJECT (amrparse, "Parsing header data");
238 if (!memcmp (data, "#!AMR-WB\n", 9)) {
239 GST_DEBUG_OBJECT (amrparse, "AMR-WB detected");
240 amrparse->block_size = block_size_wb;
241 amrparse->wide = TRUE;
242 *skipsize = amrparse->header = 9;
243 } else if (!memcmp (data, "#!AMR\n", 6)) {
244 GST_DEBUG_OBJECT (amrparse, "AMR-NB detected");
245 amrparse->block_size = block_size_nb;
246 amrparse->wide = FALSE;
247 *skipsize = amrparse->header = 6;
251 gst_amrparse_set_src_caps (amrparse);
257 * gst_amrparse_check_valid_frame:
258 * @parse: #GstBaseParse.
259 * @buffer: #GstBuffer.
260 * @framesize: Output variable where the found frame size is put.
261 * @skipsize: Output variable which tells how much data needs to be skipped
262 * until a frame header is found.
264 * Implementation of "check_valid_frame" vmethod in #GstBaseParse class.
266 * Returns: TRUE if the given data contains valid frame.
269 gst_amrparse_check_valid_frame (GstBaseParse * parse,
270 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
274 gint fsize, mode, dsize;
275 GstAmrParse *amrparse;
277 amrparse = GST_AMRPARSE (parse);
278 buffer = frame->buffer;
279 data = GST_BUFFER_DATA (buffer);
280 dsize = GST_BUFFER_SIZE (buffer);
282 GST_LOG ("buffer: %d bytes", dsize);
284 if (amrparse->need_header) {
285 if (dsize >= AMR_MIME_HEADER_SIZE &&
286 gst_amrparse_parse_header (amrparse, data, skipsize)) {
287 amrparse->need_header = FALSE;
288 gst_base_parse_set_frame_props (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
290 GST_WARNING ("media doesn't look like a AMR format");
292 /* We return FALSE, so this frame won't get pushed forward. Instead,
293 the "skip" value is set, so next time we will receive a valid frame. */
297 /* Does this look like a possible frame header candidate? */
298 if ((data[0] & 0x83) == 0) {
299 /* Yep. Retrieve the frame size */
300 mode = (data[0] >> 3) & 0x0F;
301 fsize = amrparse->block_size[mode] + 1; /* +1 for the header byte */
303 /* We recognize this data as a valid frame when:
304 * - We are in sync. There is no need for extra checks then
305 * - We are in EOS. There might not be enough data to check next frame
306 * - Sync is lost, but the following data after this frame seem
307 * to contain a valid header as well (and there is enough data to
308 * perform this check)
310 if (GST_BASE_PARSE_FRAME_SYNC (frame) || GST_BASE_PARSE_FRAME_DRAIN (frame)
311 || (dsize >= fsize && (data[fsize] & 0x83) == 0)) {
317 GST_LOG ("sync lost");
323 * gst_amrparse_parse_frame:
324 * @parse: #GstBaseParse.
325 * @buffer: #GstBuffer.
327 * Implementation of "parse" vmethod in #GstBaseParse class.
329 * Returns: #GstFlowReturn defining the parsing status.
332 gst_amrparse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
339 * gst_amrparse_start:
340 * @parse: #GstBaseParse.
342 * Implementation of "start" vmethod in #GstBaseParse class.
344 * Returns: TRUE on success.
347 gst_amrparse_start (GstBaseParse * parse)
349 GstAmrParse *amrparse;
351 amrparse = GST_AMRPARSE (parse);
353 amrparse->need_header = TRUE;
354 amrparse->header = 0;
361 * @parse: #GstBaseParse.
363 * Implementation of "stop" vmethod in #GstBaseParse class.
365 * Returns: TRUE on success.
368 gst_amrparse_stop (GstBaseParse * parse)
370 GstAmrParse *amrparse;
372 amrparse = GST_AMRPARSE (parse);
374 amrparse->need_header = TRUE;
375 amrparse->header = 0;