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 static gboolean gst_amr_parse_start (GstBaseParse * parse);
75 static gboolean gst_amr_parse_stop (GstBaseParse * parse);
77 static gboolean gst_amr_parse_sink_setcaps (GstBaseParse * parse,
79 static GstCaps *gst_amr_parse_sink_getcaps (GstBaseParse * parse,
82 static GstFlowReturn gst_amr_parse_handle_frame (GstBaseParse * parse,
83 GstBaseParseFrame * frame, gint * skipsize);
85 G_DEFINE_TYPE (GstAmrParse, gst_amr_parse, GST_TYPE_BASE_PARSE);
88 * gst_amr_parse_class_init:
89 * @klass: GstAmrParseClass.
93 gst_amr_parse_class_init (GstAmrParseClass * klass)
95 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
96 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
98 GST_DEBUG_CATEGORY_INIT (amrparse_debug, "amrparse", 0,
99 "AMR-NB audio stream parser");
101 gst_element_class_add_pad_template (element_class,
102 gst_static_pad_template_get (&sink_template));
103 gst_element_class_add_pad_template (element_class,
104 gst_static_pad_template_get (&src_template));
106 gst_element_class_set_details_simple (element_class,
107 "AMR audio stream parser", "Codec/Parser/Audio",
108 "Adaptive Multi-Rate audio parser",
109 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
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);
120 * gst_amr_parse_init:
121 * @amrparse: #GstAmrParse
122 * @klass: #GstAmrParseClass.
126 gst_amr_parse_init (GstAmrParse * amrparse)
129 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 62);
130 GST_DEBUG ("initialized");
136 * gst_amr_parse_set_src_caps:
137 * @amrparse: #GstAmrParse.
139 * Set source pad caps according to current knowledge about the
142 * Returns: TRUE if caps were successfully set.
145 gst_amr_parse_set_src_caps (GstAmrParse * amrparse)
147 GstCaps *src_caps = NULL;
148 gboolean res = FALSE;
150 if (amrparse->wide) {
151 GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-WB");
152 src_caps = gst_caps_new_simple ("audio/AMR-WB",
153 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 16000, NULL);
155 GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-NB");
156 /* Max. size of NB frame is 31 bytes, so we can set the min. frame
157 size to 32 (+1 for next frame header) */
158 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 32);
159 src_caps = gst_caps_new_simple ("audio/AMR",
160 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
162 gst_pad_use_fixed_caps (GST_BASE_PARSE (amrparse)->srcpad);
163 res = gst_pad_set_caps (GST_BASE_PARSE (amrparse)->srcpad, src_caps);
164 gst_caps_unref (src_caps);
170 * gst_amr_parse_sink_setcaps:
174 * Returns: TRUE on success.
177 gst_amr_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
179 GstAmrParse *amrparse;
180 GstStructure *structure;
183 amrparse = GST_AMR_PARSE (parse);
184 structure = gst_caps_get_structure (caps, 0);
185 name = gst_structure_get_name (structure);
187 GST_DEBUG_OBJECT (amrparse, "setcaps: %s", name);
189 if (!strncmp (name, "audio/x-amr-wb-sh", 17)) {
190 amrparse->block_size = block_size_wb;
192 } else if (!strncmp (name, "audio/x-amr-nb-sh", 17)) {
193 amrparse->block_size = block_size_nb;
196 GST_WARNING ("Unknown caps");
200 amrparse->need_header = FALSE;
201 gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
202 gst_amr_parse_set_src_caps (amrparse);
207 * gst_amr_parse_parse_header:
208 * @amrparse: #GstAmrParse
209 * @data: Header data to be parsed.
210 * @skipsize: Output argument where the frame size will be stored.
212 * Check if the given data contains an AMR mime header.
214 * Returns: TRUE on success.
217 gst_amr_parse_parse_header (GstAmrParse * amrparse,
218 const guint8 * data, gint * skipsize)
220 GST_DEBUG_OBJECT (amrparse, "Parsing header data");
222 if (!memcmp (data, "#!AMR-WB\n", 9)) {
223 GST_DEBUG_OBJECT (amrparse, "AMR-WB detected");
224 amrparse->block_size = block_size_wb;
225 amrparse->wide = TRUE;
226 *skipsize = amrparse->header = 9;
227 } else if (!memcmp (data, "#!AMR\n", 6)) {
228 GST_DEBUG_OBJECT (amrparse, "AMR-NB detected");
229 amrparse->block_size = block_size_nb;
230 amrparse->wide = FALSE;
231 *skipsize = amrparse->header = 6;
235 gst_amr_parse_set_src_caps (amrparse);
241 * gst_amr_parse_check_valid_frame:
242 * @parse: #GstBaseParse.
243 * @buffer: #GstBuffer.
244 * @framesize: Output variable where the found frame size is put.
245 * @skipsize: Output variable which tells how much data needs to be skipped
246 * until a frame header is found.
248 * Implementation of "check_valid_frame" vmethod in #GstBaseParse class.
250 * Returns: TRUE if the given data contains valid frame.
253 gst_amr_parse_handle_frame (GstBaseParse * parse,
254 GstBaseParseFrame * frame, gint * skipsize)
258 gint fsize = 0, mode, dsize;
259 GstAmrParse *amrparse;
260 GstFlowReturn ret = GST_FLOW_OK;
261 gboolean found = FALSE;
263 amrparse = GST_AMR_PARSE (parse);
264 buffer = frame->buffer;
266 gst_buffer_map (buffer, &map, GST_MAP_READ);
269 GST_LOG ("buffer: %d bytes", dsize);
271 if (amrparse->need_header) {
272 if (dsize >= AMR_MIME_HEADER_SIZE &&
273 gst_amr_parse_parse_header (amrparse, map.data, skipsize)) {
274 amrparse->need_header = FALSE;
275 gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
277 GST_WARNING ("media doesn't look like a AMR format");
279 /* We return FALSE, so this frame won't get pushed forward. Instead,
280 the "skip" value is set, so next time we will receive a valid frame. */
285 /* Does this look like a possible frame header candidate? */
286 if ((map.data[0] & 0x83) == 0) {
287 /* Yep. Retrieve the frame size */
288 mode = (map.data[0] >> 3) & 0x0F;
289 fsize = amrparse->block_size[mode] + 1; /* +1 for the header byte */
291 /* We recognize this data as a valid frame when:
292 * - We are in sync. There is no need for extra checks then
293 * - We are in EOS. There might not be enough data to check next frame
294 * - Sync is lost, but the following data after this frame seem
295 * to contain a valid header as well (and there is enough data to
296 * perform this check)
300 /* in sync, no further check */
301 if (!GST_BASE_PARSE_LOST_SYNC (parse)) {
303 } else if (dsize > fsize) {
304 /* enough data, check for next sync */
305 if ((map.data[fsize] & 0x83) == 0)
307 } else if (GST_BASE_PARSE_DRAINING (parse)) {
308 /* not enough, but draining, so ok */
315 gst_buffer_unmap (buffer, &map);
317 if (found && fsize <= map.size) {
318 ret = gst_base_parse_finish_frame (parse, frame, fsize);
325 * gst_amr_parse_start:
326 * @parse: #GstBaseParse.
328 * Implementation of "start" vmethod in #GstBaseParse class.
330 * Returns: TRUE on success.
333 gst_amr_parse_start (GstBaseParse * parse)
335 GstAmrParse *amrparse;
337 amrparse = GST_AMR_PARSE (parse);
339 amrparse->need_header = TRUE;
340 amrparse->header = 0;
346 * gst_amr_parse_stop:
347 * @parse: #GstBaseParse.
349 * Implementation of "stop" vmethod in #GstBaseParse class.
351 * Returns: TRUE on success.
354 gst_amr_parse_stop (GstBaseParse * parse)
356 GstAmrParse *amrparse;
358 amrparse = GST_AMR_PARSE (parse);
360 amrparse->need_header = TRUE;
361 amrparse->header = 0;
366 gst_amr_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter)
371 /* FIXME: handle filter caps */
373 peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
377 /* Rename structure names */
378 peercaps = gst_caps_make_writable (peercaps);
379 n = gst_caps_get_size (peercaps);
380 for (i = 0; i < n; i++) {
381 GstStructure *s = gst_caps_get_structure (peercaps, i);
383 if (gst_structure_has_name (s, "audio/AMR"))
384 gst_structure_set_name (s, "audio/x-amr-nb-sh");
386 gst_structure_set_name (s, "audio/x-amr-wb-sh");
390 gst_caps_intersect_full (peercaps,
391 gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)),
392 GST_CAPS_INTERSECT_FIRST);
393 gst_caps_unref (peercaps);
396 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD