aacparse: Properly report in the CAPS query that we can convert ADTS<->RAW
[platform/upstream/gst-plugins-good.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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, 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-1.0 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 #include <gst/pbutils/pbutils.h>
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     GstCaps * filter);
81
82 static GstFlowReturn gst_amr_parse_handle_frame (GstBaseParse * parse,
83     GstBaseParseFrame * frame, gint * skipsize);
84 static GstFlowReturn gst_amr_parse_pre_push_frame (GstBaseParse * parse,
85     GstBaseParseFrame * frame);
86
87 G_DEFINE_TYPE (GstAmrParse, gst_amr_parse, GST_TYPE_BASE_PARSE);
88
89 /**
90  * gst_amr_parse_class_init:
91  * @klass: GstAmrParseClass.
92  *
93  */
94 static void
95 gst_amr_parse_class_init (GstAmrParseClass * klass)
96 {
97   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
98   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
99
100   GST_DEBUG_CATEGORY_INIT (amrparse_debug, "amrparse", 0,
101       "AMR-NB audio stream parser");
102
103   gst_element_class_add_pad_template (element_class,
104       gst_static_pad_template_get (&sink_template));
105   gst_element_class_add_pad_template (element_class,
106       gst_static_pad_template_get (&src_template));
107
108   gst_element_class_set_static_metadata (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   parse_class->start = GST_DEBUG_FUNCPTR (gst_amr_parse_start);
114   parse_class->stop = GST_DEBUG_FUNCPTR (gst_amr_parse_stop);
115   parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_setcaps);
116   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_getcaps);
117   parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_amr_parse_handle_frame);
118   parse_class->pre_push_frame =
119       GST_DEBUG_FUNCPTR (gst_amr_parse_pre_push_frame);
120 }
121
122
123 /**
124  * gst_amr_parse_init:
125  * @amrparse: #GstAmrParse
126  * @klass: #GstAmrParseClass.
127  *
128  */
129 static void
130 gst_amr_parse_init (GstAmrParse * amrparse)
131 {
132   /* init rest */
133   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 62);
134   GST_DEBUG ("initialized");
135   GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (amrparse));
136 }
137
138
139 /**
140  * gst_amr_parse_set_src_caps:
141  * @amrparse: #GstAmrParse.
142  *
143  * Set source pad caps according to current knowledge about the
144  * audio stream.
145  *
146  * Returns: TRUE if caps were successfully set.
147  */
148 static gboolean
149 gst_amr_parse_set_src_caps (GstAmrParse * amrparse)
150 {
151   GstCaps *src_caps = NULL;
152   gboolean res = FALSE;
153
154   if (amrparse->wide) {
155     GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-WB");
156     src_caps = gst_caps_new_simple ("audio/AMR-WB",
157         "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 16000, NULL);
158   } else {
159     GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-NB");
160     /* Max. size of NB frame is 31 bytes, so we can set the min. frame
161        size to 32 (+1 for next frame header) */
162     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 32);
163     src_caps = gst_caps_new_simple ("audio/AMR",
164         "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
165   }
166   gst_pad_use_fixed_caps (GST_BASE_PARSE (amrparse)->srcpad);
167   res = gst_pad_set_caps (GST_BASE_PARSE (amrparse)->srcpad, src_caps);
168   gst_caps_unref (src_caps);
169   return res;
170 }
171
172
173 /**
174  * gst_amr_parse_sink_setcaps:
175  * @sinkpad: GstPad
176  * @caps: GstCaps
177  *
178  * Returns: TRUE on success.
179  */
180 static gboolean
181 gst_amr_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
182 {
183   GstAmrParse *amrparse;
184   GstStructure *structure;
185   const gchar *name;
186
187   amrparse = GST_AMR_PARSE (parse);
188   structure = gst_caps_get_structure (caps, 0);
189   name = gst_structure_get_name (structure);
190
191   GST_DEBUG_OBJECT (amrparse, "setcaps: %s", name);
192
193   if (!strncmp (name, "audio/x-amr-wb-sh", 17)) {
194     amrparse->block_size = block_size_wb;
195     amrparse->wide = 1;
196   } else if (!strncmp (name, "audio/x-amr-nb-sh", 17)) {
197     amrparse->block_size = block_size_nb;
198     amrparse->wide = 0;
199   } else {
200     GST_WARNING ("Unknown caps");
201     return FALSE;
202   }
203
204   amrparse->need_header = FALSE;
205   gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
206   gst_amr_parse_set_src_caps (amrparse);
207   return TRUE;
208 }
209
210 /**
211  * gst_amr_parse_parse_header:
212  * @amrparse: #GstAmrParse
213  * @data: Header data to be parsed.
214  * @skipsize: Output argument where the frame size will be stored.
215  *
216  * Check if the given data contains an AMR mime header.
217  *
218  * Returns: TRUE on success.
219  */
220 static gboolean
221 gst_amr_parse_parse_header (GstAmrParse * amrparse,
222     const guint8 * data, gint * skipsize)
223 {
224   GST_DEBUG_OBJECT (amrparse, "Parsing header data");
225
226   if (!memcmp (data, "#!AMR-WB\n", 9)) {
227     GST_DEBUG_OBJECT (amrparse, "AMR-WB detected");
228     amrparse->block_size = block_size_wb;
229     amrparse->wide = TRUE;
230     *skipsize = amrparse->header = 9;
231   } else if (!memcmp (data, "#!AMR\n", 6)) {
232     GST_DEBUG_OBJECT (amrparse, "AMR-NB detected");
233     amrparse->block_size = block_size_nb;
234     amrparse->wide = FALSE;
235     *skipsize = amrparse->header = 6;
236   } else
237     return FALSE;
238
239   gst_amr_parse_set_src_caps (amrparse);
240   return TRUE;
241 }
242
243
244 /**
245  * gst_amr_parse_check_valid_frame:
246  * @parse: #GstBaseParse.
247  * @buffer: #GstBuffer.
248  * @framesize: Output variable where the found frame size is put.
249  * @skipsize: Output variable which tells how much data needs to be skipped
250  *            until a frame header is found.
251  *
252  * Implementation of "check_valid_frame" vmethod in #GstBaseParse class.
253  *
254  * Returns: TRUE if the given data contains valid frame.
255  */
256 static GstFlowReturn
257 gst_amr_parse_handle_frame (GstBaseParse * parse,
258     GstBaseParseFrame * frame, gint * skipsize)
259 {
260   GstBuffer *buffer;
261   GstMapInfo map;
262   gint fsize = 0, mode, dsize;
263   GstAmrParse *amrparse;
264   GstFlowReturn ret = GST_FLOW_OK;
265   gboolean found = FALSE;
266
267   amrparse = GST_AMR_PARSE (parse);
268   buffer = frame->buffer;
269
270   gst_buffer_map (buffer, &map, GST_MAP_READ);
271   dsize = map.size;
272
273   GST_LOG ("buffer: %d bytes", dsize);
274
275   if (amrparse->need_header) {
276     if (dsize >= AMR_MIME_HEADER_SIZE &&
277         gst_amr_parse_parse_header (amrparse, map.data, skipsize)) {
278       amrparse->need_header = FALSE;
279       gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2);
280     } else {
281       GST_WARNING ("media doesn't look like a AMR format");
282     }
283     /* We return FALSE, so this frame won't get pushed forward. Instead,
284        the "skip" value is set, so next time we will receive a valid frame. */
285     goto done;
286   }
287
288   *skipsize = 1;
289   /* Does this look like a possible frame header candidate? */
290   if ((map.data[0] & 0x83) == 0) {
291     /* Yep. Retrieve the frame size */
292     mode = (map.data[0] >> 3) & 0x0F;
293     fsize = amrparse->block_size[mode] + 1;     /* +1 for the header byte */
294
295     /* We recognize this data as a valid frame when:
296      *     - We are in sync. There is no need for extra checks then
297      *     - We are in EOS. There might not be enough data to check next frame
298      *     - Sync is lost, but the following data after this frame seem
299      *       to contain a valid header as well (and there is enough data to
300      *       perform this check)
301      */
302     if (fsize) {
303       *skipsize = 0;
304       /* in sync, no further check */
305       if (!GST_BASE_PARSE_LOST_SYNC (parse)) {
306         found = TRUE;
307       } else if (dsize > fsize) {
308         /* enough data, check for next sync */
309         if ((map.data[fsize] & 0x83) == 0)
310           found = TRUE;
311       } else if (GST_BASE_PARSE_DRAINING (parse)) {
312         /* not enough, but draining, so ok */
313         found = TRUE;
314       }
315     }
316   }
317
318 done:
319   gst_buffer_unmap (buffer, &map);
320
321   if (found && fsize <= map.size) {
322     ret = gst_base_parse_finish_frame (parse, frame, fsize);
323   }
324
325   return ret;
326 }
327
328 /**
329  * gst_amr_parse_start:
330  * @parse: #GstBaseParse.
331  *
332  * Implementation of "start" vmethod in #GstBaseParse class.
333  *
334  * Returns: TRUE on success.
335  */
336 static gboolean
337 gst_amr_parse_start (GstBaseParse * parse)
338 {
339   GstAmrParse *amrparse;
340
341   amrparse = GST_AMR_PARSE (parse);
342   GST_DEBUG ("start");
343   amrparse->need_header = TRUE;
344   amrparse->header = 0;
345   amrparse->sent_codec_tag = FALSE;
346   return TRUE;
347 }
348
349
350 /**
351  * gst_amr_parse_stop:
352  * @parse: #GstBaseParse.
353  *
354  * Implementation of "stop" vmethod in #GstBaseParse class.
355  *
356  * Returns: TRUE on success.
357  */
358 static gboolean
359 gst_amr_parse_stop (GstBaseParse * parse)
360 {
361   GstAmrParse *amrparse;
362
363   amrparse = GST_AMR_PARSE (parse);
364   GST_DEBUG ("stop");
365   amrparse->need_header = TRUE;
366   amrparse->header = 0;
367   return TRUE;
368 }
369
370 static GstCaps *
371 gst_amr_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter)
372 {
373   GstCaps *peercaps, *templ;
374   GstCaps *res;
375
376
377   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
378   peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), filter);
379
380   if (peercaps) {
381     guint i, n;
382
383     /* Rename structure names */
384     peercaps = gst_caps_make_writable (peercaps);
385     n = gst_caps_get_size (peercaps);
386     for (i = 0; i < n; i++) {
387       GstStructure *s = gst_caps_get_structure (peercaps, i);
388
389       if (gst_structure_has_name (s, "audio/AMR"))
390         gst_structure_set_name (s, "audio/x-amr-nb-sh");
391       else
392         gst_structure_set_name (s, "audio/x-amr-wb-sh");
393     }
394
395     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
396     gst_caps_unref (peercaps);
397     res = gst_caps_make_writable (res);
398     /* Append the template caps because we still want to accept
399      * caps without any fields in the case upstream does not
400      * know anything.
401      */
402     gst_caps_append (res, templ);
403   } else {
404     res = templ;
405   }
406
407   if (filter) {
408     GstCaps *intersection;
409
410     intersection =
411         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
412     gst_caps_unref (res);
413     res = intersection;
414   }
415
416   return res;
417 }
418
419 static GstFlowReturn
420 gst_amr_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
421 {
422   GstAmrParse *amrparse = GST_AMR_PARSE (parse);
423
424   if (!amrparse->sent_codec_tag) {
425     GstTagList *taglist;
426     GstCaps *caps;
427
428     taglist = gst_tag_list_new_empty ();
429
430     /* codec tag */
431     caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
432     gst_pb_utils_add_codec_description_to_tag_list (taglist,
433         GST_TAG_AUDIO_CODEC, caps);
434     gst_caps_unref (caps);
435
436     gst_pad_push_event (GST_BASE_PARSE_SRC_PAD (amrparse),
437         gst_event_new_tag (taglist));
438
439     /* also signals the end of first-frame processing */
440     amrparse->sent_codec_tag = TRUE;
441   }
442
443   return GST_FLOW_OK;
444 }