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);
81 static gboolean gst_amr_parse_check_valid_frame (GstBaseParse * parse,
82 GstBaseParseFrame * frame, guint * framesize, gint * skipsize);
84 static GstFlowReturn gst_amr_parse_parse_frame (GstBaseParse * parse,
85 GstBaseParseFrame * frame);
87 #ifdef GST_EXT_AMRPARSER_MODIFICATION /* make full amr index table when seek */
88 static gboolean gst_amr_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event);
90 #define AMR_MAX_PULL_RANGE_BUF (5 * 1024 * 1024) /* 5 mbyte */
93 #define _do_init(bla) \
94 GST_DEBUG_CATEGORY_INIT (amrparse_debug, "amrparse", 0, \
95 "AMR-NB audio stream parser");
97 GST_BOILERPLATE_FULL (GstAmrParse, gst_amr_parse, GstBaseParse,
98 GST_TYPE_BASE_PARSE, _do_init);
101 * gst_amr_parse_base_init:
102 * @klass: #GstElementClass.
106 gst_amr_parse_base_init (gpointer klass)
108 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
110 gst_element_class_add_static_pad_template (element_class,
112 gst_element_class_add_static_pad_template (element_class, &src_template);
114 gst_element_class_set_details_simple (element_class,
115 "AMR audio stream parser", "Codec/Parser/Audio",
116 "Adaptive Multi-Rate audio parser",
117 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
122 * gst_amr_parse_class_init:
123 * @klass: GstAmrParseClass.
127 gst_amr_parse_class_init (GstAmrParseClass * klass)
129 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
131 parse_class->start = GST_DEBUG_FUNCPTR (gst_amr_parse_start);
132 parse_class->stop = GST_DEBUG_FUNCPTR (gst_amr_parse_stop);
133 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_setcaps);
134 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_getcaps);
135 parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_amr_parse_parse_frame);
136 parse_class->check_valid_frame =
137 GST_DEBUG_FUNCPTR (gst_amr_parse_check_valid_frame);
139 #ifdef GST_EXT_AMRPARSER_MODIFICATION /* make full amr index table when seek */
140 parse_class->src_event = gst_amr_parse_src_eventfunc;
146 * gst_amr_parse_init:
147 * @amrparse: #GstAmrParse
148 * @klass: #GstAmrParseClass.
152 gst_amr_parse_init (GstAmrParse * amrparse, GstAmrParseClass * klass)
155 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 62);
156 GST_DEBUG ("initialized");
162 * gst_amr_parse_set_src_caps:
163 * @amrparse: #GstAmrParse.
165 * Set source pad caps according to current knowledge about the
168 * Returns: TRUE if caps were successfully set.
171 gst_amr_parse_set_src_caps (GstAmrParse * amrparse)
173 GstCaps *src_caps = NULL;
174 gboolean res = FALSE;
176 if (amrparse->wide) {
177 GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-WB");
178 src_caps = gst_caps_new_simple ("audio/AMR-WB",
179 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 16000, NULL);
181 GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-NB");
182 /* Max. size of NB frame is 31 bytes, so we can set the min. frame
183 size to 32 (+1 for next frame header) */
184 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 32);
185 src_caps = gst_caps_new_simple ("audio/AMR",
186 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
188 gst_pad_use_fixed_caps (GST_BASE_PARSE (amrparse)->srcpad);
189 res = gst_pad_set_caps (GST_BASE_PARSE (amrparse)->srcpad, src_caps);
190 gst_caps_unref (src_caps);
196 * gst_amr_parse_sink_setcaps:
200 * Returns: TRUE on success.
203 gst_amr_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
205 GstAmrParse *amrparse;
206 GstStructure *structure;
209 amrparse = GST_AMR_PARSE (parse);
210 structure = gst_caps_get_structure (caps, 0);
211 name = gst_structure_get_name (structure);
213 GST_DEBUG_OBJECT (amrparse, "setcaps: %s", name);
215 if (!strncmp (name, "audio/x-amr-wb-sh", 17)) {
216 amrparse->block_size = block_size_wb;
218 } else if (!strncmp (name, "audio/x-amr-nb-sh", 17)) {
219 amrparse->block_size = block_size_nb;
222 GST_WARNING ("Unknown caps");
226 amrparse->need_header = FALSE;
227 gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
228 gst_amr_parse_set_src_caps (amrparse);
233 * gst_amr_parse_parse_header:
234 * @amrparse: #GstAmrParse
235 * @data: Header data to be parsed.
236 * @skipsize: Output argument where the frame size will be stored.
238 * Check if the given data contains an AMR mime header.
240 * Returns: TRUE on success.
243 gst_amr_parse_parse_header (GstAmrParse * amrparse,
244 const guint8 * data, gint * skipsize)
246 GST_DEBUG_OBJECT (amrparse, "Parsing header data");
248 if (!memcmp (data, "#!AMR-WB\n", 9)) {
249 GST_DEBUG_OBJECT (amrparse, "AMR-WB detected");
250 amrparse->block_size = block_size_wb;
251 amrparse->wide = TRUE;
252 *skipsize = amrparse->header = 9;
253 } else if (!memcmp (data, "#!AMR\n", 6)) {
254 GST_DEBUG_OBJECT (amrparse, "AMR-NB detected");
255 amrparse->block_size = block_size_nb;
256 amrparse->wide = FALSE;
257 *skipsize = amrparse->header = 6;
261 gst_amr_parse_set_src_caps (amrparse);
267 * gst_amr_parse_check_valid_frame:
268 * @parse: #GstBaseParse.
269 * @buffer: #GstBuffer.
270 * @framesize: Output variable where the found frame size is put.
271 * @skipsize: Output variable which tells how much data needs to be skipped
272 * until a frame header is found.
274 * Implementation of "check_valid_frame" vmethod in #GstBaseParse class.
276 * Returns: TRUE if the given data contains valid frame.
279 gst_amr_parse_check_valid_frame (GstBaseParse * parse,
280 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
284 gint fsize, mode, dsize;
285 GstAmrParse *amrparse;
287 amrparse = GST_AMR_PARSE (parse);
288 buffer = frame->buffer;
289 data = GST_BUFFER_DATA (buffer);
290 dsize = GST_BUFFER_SIZE (buffer);
292 GST_LOG ("buffer: %d bytes", dsize);
294 if (amrparse->need_header) {
295 if (dsize >= AMR_MIME_HEADER_SIZE &&
296 gst_amr_parse_parse_header (amrparse, data, skipsize)) {
297 amrparse->need_header = FALSE;
298 gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
300 GST_WARNING ("media doesn't look like a AMR format");
302 /* We return FALSE, so this frame won't get pushed forward. Instead,
303 the "skip" value is set, so next time we will receive a valid frame. */
307 /* Does this look like a possible frame header candidate? */
308 if ((data[0] & 0x83) == 0) {
309 /* Yep. Retrieve the frame size */
310 mode = (data[0] >> 3) & 0x0F;
311 fsize = amrparse->block_size[mode] + 1; /* +1 for the header byte */
313 /* We recognize this data as a valid frame when:
314 * - We are in sync. There is no need for extra checks then
315 * - We are in EOS. There might not be enough data to check next frame
316 * - Sync is lost, but the following data after this frame seem
317 * to contain a valid header as well (and there is enough data to
318 * perform this check)
321 gboolean found = FALSE;
323 /* in sync, no further check */
324 if (!GST_BASE_PARSE_LOST_SYNC (parse)) {
326 } else if (dsize > fsize) {
327 /* enough data, check for next sync */
328 if ((data[fsize] & 0x83) == 0)
330 } else if (GST_BASE_PARSE_DRAINING (parse)) {
331 /* not enough, but draining, so ok */
334 /* indicate we need not skip, but need more data */
336 *framesize = fsize + 1;
345 GST_LOG ("sync lost");
351 * gst_amr_parse_parse_frame:
352 * @parse: #GstBaseParse.
353 * @buffer: #GstBuffer.
355 * Implementation of "parse" vmethod in #GstBaseParse class.
357 * Returns: #GstFlowReturn defining the parsing status.
360 gst_amr_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
367 * gst_amr_parse_start:
368 * @parse: #GstBaseParse.
370 * Implementation of "start" vmethod in #GstBaseParse class.
372 * Returns: TRUE on success.
375 gst_amr_parse_start (GstBaseParse * parse)
377 GstAmrParse *amrparse;
379 amrparse = GST_AMR_PARSE (parse);
381 amrparse->need_header = TRUE;
382 amrparse->header = 0;
388 * gst_amr_parse_stop:
389 * @parse: #GstBaseParse.
391 * Implementation of "stop" vmethod in #GstBaseParse class.
393 * Returns: TRUE on success.
396 gst_amr_parse_stop (GstBaseParse * parse)
398 GstAmrParse *amrparse;
400 amrparse = GST_AMR_PARSE (parse);
402 amrparse->need_header = TRUE;
403 amrparse->header = 0;
408 gst_amr_parse_sink_getcaps (GstBaseParse * parse)
413 peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
417 /* Rename structure names */
418 peercaps = gst_caps_make_writable (peercaps);
419 n = gst_caps_get_size (peercaps);
420 for (i = 0; i < n; i++) {
421 GstStructure *s = gst_caps_get_structure (peercaps, i);
423 if (gst_structure_has_name (s, "audio/AMR"))
424 gst_structure_set_name (s, "audio/x-amr-nb-sh");
426 gst_structure_set_name (s, "audio/x-amr-wb-sh");
430 gst_caps_intersect_full (peercaps,
431 gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)),
432 GST_CAPS_INTERSECT_FIRST);
433 gst_caps_unref (peercaps);
436 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD
444 #ifdef GST_EXT_AMRPARSER_MODIFICATION /* make full amr index table when seek */
446 * gst_amr_parse_src_eventfunc:
447 * @parse: #GstBaseParse. #event
449 * before baseparse handles seek event, make full amr index table.
451 * Returns: TRUE on success.
454 gst_amr_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event)
456 gboolean handled = FALSE;
457 GstAmrParse *amrparse;
458 amrparse = GST_AMR_PARSE (parse);
460 switch (GST_EVENT_TYPE (event)) {
463 GstFlowReturn res = GST_FLOW_OK;
464 gint64 base_offset = 0, sync_offset = 0, cur = 0;
465 gint32 frame_count = 1; /* do not add first frame because it is already in index table */
466 gint64 total_file_size = 0, start_offset = 0;
467 GstClockTime current_ts = GST_CLOCK_TIME_NONE;
468 GstActivateMode pad_mode = GST_ACTIVATE_NONE;
470 #ifdef GST_EXT_BASEPARSER_MODIFICATION /* check baseparse define these fuction */
471 gst_base_parse_get_pad_mode(parse, &pad_mode);
472 if (pad_mode != GST_ACTIVATE_PULL) {
473 GST_INFO_OBJECT (amrparse, "arm parser is not pull mode. amr parser can not make index table.");
476 gst_base_parse_get_upstream_size(parse, &total_file_size);
477 gst_base_parse_get_index_last_offset(parse, &start_offset);
478 gst_base_parse_get_index_last_ts(parse, ¤t_ts);
480 GST_WARNING_OBJECT (amrparse, "baseparser does not define get private param functions. can not make index table here.");
484 GST_LOG_OBJECT (amrparse, "gst_amr_parse_src_eventfunc GST_EVENT_SEEK enter");
486 if (total_file_size == 0 || start_offset >= total_file_size) {
487 GST_ERROR("last index offset %d is larger than file size %d", start_offset, total_file_size);
491 gst_event_parse_seek (event, NULL, NULL, NULL, NULL, &cur, NULL, NULL);
492 if (cur <= current_ts) {
493 GST_INFO_OBJECT (amrparse, "seek to %"GST_TIME_FORMAT" within index table %"GST_TIME_FORMAT". do not make index table",
494 GST_TIME_ARGS(cur), GST_TIME_ARGS(current_ts));
497 GST_INFO_OBJECT (amrparse, "seek to %"GST_TIME_FORMAT" without index table %"GST_TIME_FORMAT". make index table",
498 GST_TIME_ARGS(cur), GST_TIME_ARGS(current_ts));
501 GST_INFO_OBJECT (amrparse, "make AMR Index Table. file_size = %"G_GINT64_FORMAT" last idx offset=%"G_GINT64_FORMAT
502 ", last idx ts=%"GST_TIME_FORMAT, total_file_size, start_offset, GST_TIME_ARGS(current_ts));
504 base_offset = start_offset; /* set base by start offset */
507 /* STEP 1: MAX_PULL_RANGE_BUF cycle */
508 while (total_file_size - base_offset >= AMR_MAX_PULL_RANGE_BUF) {
510 GstBuffer *buffer = NULL;
513 GST_INFO_OBJECT (amrparse, "gst_pad_pull_range %d bytes (from %"G_GINT64_FORMAT") use max size", AMR_MAX_PULL_RANGE_BUF, base_offset);
514 res = gst_pad_pull_range (parse->sinkpad, base_offset,
515 base_offset + AMR_MAX_PULL_RANGE_BUF, &buffer);
516 if (res != GST_FLOW_OK) {
517 GST_ERROR_OBJECT (amrparse, "gst_pad_pull_range failed!");
521 buf = GST_BUFFER_DATA(buffer);
523 while (offset <= AMR_MAX_PULL_RANGE_BUF) {
524 gint mode = 0, frame_size = 0;
526 if (buf != NULL && ((buf[offset] & 0x83) == 0)) {
527 mode = (buf[offset] >> 3) & 0x0F;
528 frame_size = amrparse->block_size[mode] + 1; /* +1 for the header byte */
530 if (frame_count % 50 == 0) { /* 1 sec == 50 frames. we make idx per sec */
531 // gst_base_parse_add_index_entry (parse, base_offset +offset - (amrparse->wide ? 9 : 6), current_ts, TRUE, TRUE); /* force */
532 gst_base_parse_add_index_entry (parse, base_offset +offset, current_ts, TRUE, TRUE); /* force */
533 GST_DEBUG_OBJECT (amrparse, "Adding index ts=%"GST_TIME_FORMAT" offset %"G_GINT64_FORMAT,
534 GST_TIME_ARGS(current_ts), base_offset + offset);
537 current_ts += 20 * 1000 * 1000; /* each frame is 20ms */
538 offset += frame_size;
541 GST_WARNING_OBJECT (amrparse, "we lost sync");
545 base_offset = base_offset + offset;
546 gst_buffer_unref (buffer);
547 } /* end MAX buffer cycle */
550 /* STEP 2: Remain Buffer cycle */
551 if (total_file_size - base_offset > 0) {
553 GstBuffer *buffer = NULL;
556 GST_INFO_OBJECT (amrparse, "gst_pad_pull_range %"G_GINT64_FORMAT" bytes (from %"G_GINT64_FORMAT") use remain_buf size",
557 total_file_size - base_offset, base_offset);
558 res = gst_pad_pull_range (parse->sinkpad, base_offset,
559 total_file_size, &buffer);
560 if (res != GST_FLOW_OK) {
561 GST_ERROR ("gst_pad_pull_range failed!");
565 buf = GST_BUFFER_DATA(buffer);
567 while (base_offset + offset < total_file_size) {
568 gint mode = 0, frame_size = 0;
570 if (buf != NULL && ((buf[offset] & 0x83) == 0)) {
571 mode = (buf[offset] >> 3) & 0x0F;
572 frame_size = amrparse->block_size[mode] + 1; /* +1 for the header byte */
574 if (frame_count % 50 == 0) { /* 1 sec == 50 frames. we make idx per sec */
575 gst_base_parse_add_index_entry (parse, base_offset +offset, current_ts, TRUE, TRUE); /* force */
576 GST_DEBUG_OBJECT (amrparse, "Adding index ts=%"GST_TIME_FORMAT" offset %"G_GINT64_FORMAT,
577 GST_TIME_ARGS(current_ts), base_offset + offset);
580 current_ts += 20 * 1000 * 1000; /* each frame is 20ms */
581 offset += frame_size;
584 GST_WARNING_OBJECT (amrparse, "we lost sync");
589 gst_buffer_unref (buffer);
590 } /* end remain_buf buffer cycle */
592 GST_LOG_OBJECT (amrparse, "gst_amr_parse_src_eventfunc GST_EVENT_SEEK leave");
600 /* call baseparse src_event function to handle event */
601 handled = GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);