upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.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 #define _do_init(bla) \
87     GST_DEBUG_CATEGORY_INIT (amrparse_debug, "amrparse", 0, \
88                              "AMR-NB audio stream parser");
89
90 GST_BOILERPLATE_FULL (GstAmrParse, gst_amr_parse, GstBaseParse,
91     GST_TYPE_BASE_PARSE, _do_init);
92
93
94 /**
95  * gst_amr_parse_base_init:
96  * @klass: #GstElementClass.
97  *
98  */
99 static void
100 gst_amr_parse_base_init (gpointer klass)
101 {
102   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
103
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));
108
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>");
113 }
114
115
116 /**
117  * gst_amr_parse_class_init:
118  * @klass: GstAmrParseClass.
119  *
120  */
121 static void
122 gst_amr_parse_class_init (GstAmrParseClass * klass)
123 {
124   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
125
126   parse_class->start = GST_DEBUG_FUNCPTR (gst_amr_parse_start);
127   parse_class->stop = GST_DEBUG_FUNCPTR (gst_amr_parse_stop);
128   parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_setcaps);
129   parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_amr_parse_parse_frame);
130   parse_class->check_valid_frame =
131       GST_DEBUG_FUNCPTR (gst_amr_parse_check_valid_frame);
132 }
133
134
135 /**
136  * gst_amr_parse_init:
137  * @amrparse: #GstAmrParse
138  * @klass: #GstAmrParseClass.
139  *
140  */
141 static void
142 gst_amr_parse_init (GstAmrParse * amrparse, GstAmrParseClass * klass)
143 {
144   /* init rest */
145   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 62);
146   GST_DEBUG ("initialized");
147
148 }
149
150
151 /**
152  * gst_amr_parse_set_src_caps:
153  * @amrparse: #GstAmrParse.
154  *
155  * Set source pad caps according to current knowledge about the
156  * audio stream.
157  *
158  * Returns: TRUE if caps were successfully set.
159  */
160 static gboolean
161 gst_amr_parse_set_src_caps (GstAmrParse * amrparse)
162 {
163   GstCaps *src_caps = NULL;
164   gboolean res = FALSE;
165
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);
170   } else {
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), 33);
175     src_caps = gst_caps_new_simple ("audio/AMR",
176         "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
177   }
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);
181   return res;
182 }
183
184
185 /**
186  * gst_amr_parse_sink_setcaps:
187  * @sinkpad: GstPad
188  * @caps: GstCaps
189  *
190  * Returns: TRUE on success.
191  */
192 static gboolean
193 gst_amr_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
194 {
195   GstAmrParse *amrparse;
196   GstStructure *structure;
197   const gchar *name;
198
199   amrparse = GST_AMR_PARSE (parse);
200   structure = gst_caps_get_structure (caps, 0);
201   name = gst_structure_get_name (structure);
202
203   GST_DEBUG_OBJECT (amrparse, "setcaps: %s", name);
204
205   if (!strncmp (name, "audio/x-amr-wb-sh", 17)) {
206     amrparse->block_size = block_size_wb;
207     amrparse->wide = 1;
208   } else if (!strncmp (name, "audio/x-amr-nb-sh", 17)) {
209     amrparse->block_size = block_size_nb;
210     amrparse->wide = 0;
211   } else {
212     GST_WARNING ("Unknown caps");
213     return FALSE;
214   }
215
216   amrparse->need_header = FALSE;
217   gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
218   gst_amr_parse_set_src_caps (amrparse);
219   return TRUE;
220 }
221
222 /**
223  * gst_amr_parse_parse_header:
224  * @amrparse: #GstAmrParse
225  * @data: Header data to be parsed.
226  * @skipsize: Output argument where the frame size will be stored.
227  *
228  * Check if the given data contains an AMR mime header.
229  *
230  * Returns: TRUE on success.
231  */
232 static gboolean
233 gst_amr_parse_parse_header (GstAmrParse * amrparse,
234     const guint8 * data, gint * skipsize)
235 {
236   GST_DEBUG_OBJECT (amrparse, "Parsing header data");
237
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;
248   } else
249     return FALSE;
250
251   gst_amr_parse_set_src_caps (amrparse);
252   return TRUE;
253 }
254
255
256 /**
257  * gst_amr_parse_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.
263  *
264  * Implementation of "check_valid_frame" vmethod in #GstBaseParse class.
265  *
266  * Returns: TRUE if the given data contains valid frame.
267  */
268 gboolean
269 gst_amr_parse_check_valid_frame (GstBaseParse * parse,
270     GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
271 {
272   GstBuffer *buffer;
273   const guint8 *data;
274   gint fsize, mode, dsize;
275   GstAmrParse *amrparse;
276
277   amrparse = GST_AMR_PARSE (parse);
278   buffer = frame->buffer;
279   data = GST_BUFFER_DATA (buffer);
280   dsize = GST_BUFFER_SIZE (buffer);
281
282   GST_LOG ("buffer: %d bytes", dsize);
283
284   if (amrparse->need_header) {
285     if (dsize >= AMR_MIME_HEADER_SIZE &&
286         gst_amr_parse_parse_header (amrparse, data, skipsize)) {
287       amrparse->need_header = FALSE;
288       gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
289     } else {
290       GST_WARNING ("media doesn't look like a AMR format");
291     }
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. */
294     return FALSE;
295   }
296
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 */
302
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)
309      */
310     if (fsize &&
311         (!GST_BASE_PARSE_LOST_SYNC (parse) || GST_BASE_PARSE_DRAINING (parse)
312             || (dsize >= fsize && (data[fsize] & 0x83) == 0))) {
313       *framesize = fsize;
314       return TRUE;
315     }
316   }
317
318   GST_LOG ("sync lost");
319   return FALSE;
320 }
321
322
323 /**
324  * gst_amr_parse_parse_frame:
325  * @parse: #GstBaseParse.
326  * @buffer: #GstBuffer.
327  *
328  * Implementation of "parse" vmethod in #GstBaseParse class.
329  *
330  * Returns: #GstFlowReturn defining the parsing status.
331  */
332 GstFlowReturn
333 gst_amr_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
334 {
335   return GST_FLOW_OK;
336 }
337
338
339 /**
340  * gst_amr_parse_start:
341  * @parse: #GstBaseParse.
342  *
343  * Implementation of "start" vmethod in #GstBaseParse class.
344  *
345  * Returns: TRUE on success.
346  */
347 gboolean
348 gst_amr_parse_start (GstBaseParse * parse)
349 {
350   GstAmrParse *amrparse;
351
352   amrparse = GST_AMR_PARSE (parse);
353   GST_DEBUG ("start");
354   amrparse->need_header = TRUE;
355   amrparse->header = 0;
356   return TRUE;
357 }
358
359
360 /**
361  * gst_amr_parse_stop:
362  * @parse: #GstBaseParse.
363  *
364  * Implementation of "stop" vmethod in #GstBaseParse class.
365  *
366  * Returns: TRUE on success.
367  */
368 gboolean
369 gst_amr_parse_stop (GstBaseParse * parse)
370 {
371   GstAmrParse *amrparse;
372
373   amrparse = GST_AMR_PARSE (parse);
374   GST_DEBUG ("stop");
375   amrparse->need_header = TRUE;
376   amrparse->header = 0;
377   return TRUE;
378 }