tizen 2.0 init
[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 static gboolean gst_amr_parse_start (GstBaseParse * parse);
75 static gboolean gst_amr_parse_stop (GstBaseParse * parse);
76
77 static gboolean gst_amr_parse_sink_setcaps (GstBaseParse * parse,
78     GstCaps * caps);
79 static GstCaps *gst_amr_parse_sink_getcaps (GstBaseParse * parse);
80
81 static gboolean gst_amr_parse_check_valid_frame (GstBaseParse * parse,
82     GstBaseParseFrame * frame, guint * framesize, gint * skipsize);
83
84 static GstFlowReturn gst_amr_parse_parse_frame (GstBaseParse * parse,
85     GstBaseParseFrame * frame);
86
87 #define _do_init(bla) \
88     GST_DEBUG_CATEGORY_INIT (amrparse_debug, "amrparse", 0, \
89                              "AMR-NB audio stream parser");
90
91 GST_BOILERPLATE_FULL (GstAmrParse, gst_amr_parse, GstBaseParse,
92     GST_TYPE_BASE_PARSE, _do_init);
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_static_pad_template (element_class,
105       &sink_template);
106   gst_element_class_add_static_pad_template (element_class, &src_template);
107
108   gst_element_class_set_details_simple (element_class,
109       "AMR audio stream parser", "Codec/Parser/Audio",
110       "Adaptive Multi-Rate audio parser",
111       "Ronald Bultje <rbultje@ronald.bitfreak.net>");
112 }
113
114
115 /**
116  * gst_amr_parse_class_init:
117  * @klass: GstAmrParseClass.
118  *
119  */
120 static void
121 gst_amr_parse_class_init (GstAmrParseClass * klass)
122 {
123   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
124
125   parse_class->start = GST_DEBUG_FUNCPTR (gst_amr_parse_start);
126   parse_class->stop = GST_DEBUG_FUNCPTR (gst_amr_parse_stop);
127   parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_setcaps);
128   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_getcaps);
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), 32);
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 static 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       gboolean found = FALSE;
312
313       /* in sync, no further check */
314       if (!GST_BASE_PARSE_LOST_SYNC (parse)) {
315         found = TRUE;
316       } else if (dsize > fsize) {
317         /* enough data, check for next sync */
318         if ((data[fsize] & 0x83) == 0)
319           found = TRUE;
320       } else if (GST_BASE_PARSE_DRAINING (parse)) {
321         /* not enough, but draining, so ok */
322         found = TRUE;
323       } else {
324         /* indicate we need not skip, but need more data */
325         *skipsize = 0;
326         *framesize = fsize + 1;
327       }
328       if (found) {
329         *framesize = fsize;
330         return TRUE;
331       }
332     }
333   }
334
335   GST_LOG ("sync lost");
336   return FALSE;
337 }
338
339
340 /**
341  * gst_amr_parse_parse_frame:
342  * @parse: #GstBaseParse.
343  * @buffer: #GstBuffer.
344  *
345  * Implementation of "parse" vmethod in #GstBaseParse class.
346  *
347  * Returns: #GstFlowReturn defining the parsing status.
348  */
349 static GstFlowReturn
350 gst_amr_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
351 {
352   return GST_FLOW_OK;
353 }
354
355
356 /**
357  * gst_amr_parse_start:
358  * @parse: #GstBaseParse.
359  *
360  * Implementation of "start" vmethod in #GstBaseParse class.
361  *
362  * Returns: TRUE on success.
363  */
364 static gboolean
365 gst_amr_parse_start (GstBaseParse * parse)
366 {
367   GstAmrParse *amrparse;
368
369   amrparse = GST_AMR_PARSE (parse);
370   GST_DEBUG ("start");
371   amrparse->need_header = TRUE;
372   amrparse->header = 0;
373   return TRUE;
374 }
375
376
377 /**
378  * gst_amr_parse_stop:
379  * @parse: #GstBaseParse.
380  *
381  * Implementation of "stop" vmethod in #GstBaseParse class.
382  *
383  * Returns: TRUE on success.
384  */
385 static gboolean
386 gst_amr_parse_stop (GstBaseParse * parse)
387 {
388   GstAmrParse *amrparse;
389
390   amrparse = GST_AMR_PARSE (parse);
391   GST_DEBUG ("stop");
392   amrparse->need_header = TRUE;
393   amrparse->header = 0;
394   return TRUE;
395 }
396
397 static GstCaps *
398 gst_amr_parse_sink_getcaps (GstBaseParse * parse)
399 {
400   GstCaps *peercaps;
401   GstCaps *res;
402
403   peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
404   if (peercaps) {
405     guint i, n;
406
407     /* Rename structure names */
408     peercaps = gst_caps_make_writable (peercaps);
409     n = gst_caps_get_size (peercaps);
410     for (i = 0; i < n; i++) {
411       GstStructure *s = gst_caps_get_structure (peercaps, i);
412
413       if (gst_structure_has_name (s, "audio/AMR"))
414         gst_structure_set_name (s, "audio/x-amr-nb-sh");
415       else
416         gst_structure_set_name (s, "audio/x-amr-wb-sh");
417     }
418
419     res =
420         gst_caps_intersect_full (peercaps,
421         gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)),
422         GST_CAPS_INTERSECT_FIRST);
423     gst_caps_unref (peercaps);
424   } else {
425     res =
426         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD
427             (parse)));
428   }
429
430   return res;
431 }