Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / audioparsers / gstamrparse.c
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.
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, 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 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
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 gboolean gst_amr_parse_start (GstBaseParse * parse);
75 gboolean gst_amr_parse_stop (GstBaseParse * parse);
76
77 static gboolean gst_amr_parse_sink_setcaps (GstBaseParse * parse,
78     GstCaps * caps);
79
80 gboolean gst_amr_parse_check_valid_frame (GstBaseParse * parse,
81     GstBaseParseFrame * frame, guint * framesize, gint * skipsize);
82
83 GstFlowReturn gst_amr_parse_parse_frame (GstBaseParse * parse,
84     GstBaseParseFrame * frame);
85
86 G_DEFINE_TYPE (GstAmrParse, gst_amr_parse, GST_TYPE_BASE_PARSE);
87
88 /**
89  * gst_amr_parse_class_init:
90  * @klass: GstAmrParseClass.
91  *
92  */
93 static void
94 gst_amr_parse_class_init (GstAmrParseClass * klass)
95 {
96   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
97   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
98
99   GST_DEBUG_CATEGORY_INIT (amrparse_debug, "amrparse", 0,
100       "AMR-NB audio stream parser");
101
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));
106
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>");
111
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);
118 }
119
120
121 /**
122  * gst_amr_parse_init:
123  * @amrparse: #GstAmrParse
124  * @klass: #GstAmrParseClass.
125  *
126  */
127 static void
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
134 }
135
136
137 /**
138  * gst_amr_parse_set_src_caps:
139  * @amrparse: #GstAmrParse.
140  *
141  * Set source pad caps according to current knowledge about the
142  * audio stream.
143  *
144  * Returns: TRUE if caps were successfully set.
145  */
146 static gboolean
147 gst_amr_parse_set_src_caps (GstAmrParse * amrparse)
148 {
149   GstCaps *src_caps = NULL;
150   gboolean res = FALSE;
151
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);
156   } else {
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);
163   }
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);
167   return res;
168 }
169
170
171 /**
172  * gst_amr_parse_sink_setcaps:
173  * @sinkpad: GstPad
174  * @caps: GstCaps
175  *
176  * Returns: TRUE on success.
177  */
178 static gboolean
179 gst_amr_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
180 {
181   GstAmrParse *amrparse;
182   GstStructure *structure;
183   const gchar *name;
184
185   amrparse = GST_AMR_PARSE (parse);
186   structure = gst_caps_get_structure (caps, 0);
187   name = gst_structure_get_name (structure);
188
189   GST_DEBUG_OBJECT (amrparse, "setcaps: %s", name);
190
191   if (!strncmp (name, "audio/x-amr-wb-sh", 17)) {
192     amrparse->block_size = block_size_wb;
193     amrparse->wide = 1;
194   } else if (!strncmp (name, "audio/x-amr-nb-sh", 17)) {
195     amrparse->block_size = block_size_nb;
196     amrparse->wide = 0;
197   } else {
198     GST_WARNING ("Unknown caps");
199     return FALSE;
200   }
201
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);
205   return TRUE;
206 }
207
208 /**
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.
213  *
214  * Check if the given data contains an AMR mime header.
215  *
216  * Returns: TRUE on success.
217  */
218 static gboolean
219 gst_amr_parse_parse_header (GstAmrParse * amrparse,
220     const guint8 * data, gint * skipsize)
221 {
222   GST_DEBUG_OBJECT (amrparse, "Parsing header data");
223
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;
234   } else
235     return FALSE;
236
237   gst_amr_parse_set_src_caps (amrparse);
238   return TRUE;
239 }
240
241
242 /**
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.
249  *
250  * Implementation of "check_valid_frame" vmethod in #GstBaseParse class.
251  *
252  * Returns: TRUE if the given data contains valid frame.
253  */
254 gboolean
255 gst_amr_parse_check_valid_frame (GstBaseParse * parse,
256     GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
257 {
258   GstBuffer *buffer;
259   guint8 *data;
260   gsize size;
261   gint fsize, mode, dsize;
262   GstAmrParse *amrparse;
263   gboolean ret = FALSE;
264
265   amrparse = GST_AMR_PARSE (parse);
266   buffer = frame->buffer;
267
268   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
269   dsize = size;
270
271   GST_LOG ("buffer: %d bytes", dsize);
272
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);
278     } else {
279       GST_WARNING ("media doesn't look like a AMR format");
280     }
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. */
283     goto done;
284   }
285
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 */
291
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)
298      */
299     if (fsize &&
300         (!GST_BASE_PARSE_LOST_SYNC (parse) || GST_BASE_PARSE_DRAINING (parse)
301             || (dsize > fsize && (data[fsize] & 0x83) == 0))) {
302       *framesize = fsize;
303       ret = TRUE;
304       goto done;
305     }
306   }
307   GST_LOG ("sync lost");
308
309 done:
310   gst_buffer_unmap (buffer, data, size);
311
312   return ret;
313 }
314
315
316 /**
317  * gst_amr_parse_parse_frame:
318  * @parse: #GstBaseParse.
319  * @buffer: #GstBuffer.
320  *
321  * Implementation of "parse" vmethod in #GstBaseParse class.
322  *
323  * Returns: #GstFlowReturn defining the parsing status.
324  */
325 GstFlowReturn
326 gst_amr_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
327 {
328   return GST_FLOW_OK;
329 }
330
331
332 /**
333  * gst_amr_parse_start:
334  * @parse: #GstBaseParse.
335  *
336  * Implementation of "start" vmethod in #GstBaseParse class.
337  *
338  * Returns: TRUE on success.
339  */
340 gboolean
341 gst_amr_parse_start (GstBaseParse * parse)
342 {
343   GstAmrParse *amrparse;
344
345   amrparse = GST_AMR_PARSE (parse);
346   GST_DEBUG ("start");
347   amrparse->need_header = TRUE;
348   amrparse->header = 0;
349   return TRUE;
350 }
351
352
353 /**
354  * gst_amr_parse_stop:
355  * @parse: #GstBaseParse.
356  *
357  * Implementation of "stop" vmethod in #GstBaseParse class.
358  *
359  * Returns: TRUE on success.
360  */
361 gboolean
362 gst_amr_parse_stop (GstBaseParse * parse)
363 {
364   GstAmrParse *amrparse;
365
366   amrparse = GST_AMR_PARSE (parse);
367   GST_DEBUG ("stop");
368   amrparse->need_header = TRUE;
369   amrparse->header = 0;
370   return TRUE;
371 }