Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / audioparsers / gstaacparse.c
1 /* GStreamer AAC parser plugin
2  * Copyright (C) 2008 Nokia Corporation. All rights reserved.
3  *
4  * Contact: Stefan Kost <stefan.kost@nokia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-aacparse
24  * @short_description: AAC parser
25  * @see_also: #GstAmrParse
26  *
27  * This is an AAC parser which handles both ADIF and ADTS stream formats.
28  *
29  * As ADIF format is not framed, it is not seekable and stream duration cannot
30  * be determined either. However, ADTS format AAC clips can be seeked, and parser
31  * can also estimate playback position and clip duration.
32  *
33  * <refsect2>
34  * <title>Example launch line</title>
35  * |[
36  * gst-launch filesrc location=abc.aac ! aacparse ! faad ! audioresample ! audioconvert ! alsasink
37  * ]|
38  * </refsect2>
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <string.h>
46
47 #include "gstaacparse.h"
48
49
50 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("audio/mpeg, "
54         "framed = (boolean) true, " "mpegversion = (int) { 2, 4 }, "
55         "stream-format = (string) { raw, adts, adif };"));
56
57 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
58     GST_PAD_SINK,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) { 2, 4 };"));
61
62 GST_DEBUG_CATEGORY_STATIC (aacparse_debug);
63 #define GST_CAT_DEFAULT aacparse_debug
64
65
66 #define ADIF_MAX_SIZE 40        /* Should be enough */
67 #define ADTS_MAX_SIZE 10        /* Should be enough */
68
69
70 #define AAC_FRAME_DURATION(parse) (GST_SECOND/parse->frames_per_sec)
71
72 gboolean gst_aac_parse_start (GstBaseParse * parse);
73 gboolean gst_aac_parse_stop (GstBaseParse * parse);
74
75 static gboolean gst_aac_parse_sink_setcaps (GstBaseParse * parse,
76     GstCaps * caps);
77
78 gboolean gst_aac_parse_check_valid_frame (GstBaseParse * parse,
79     GstBaseParseFrame * frame, guint * size, gint * skipsize);
80
81 GstFlowReturn gst_aac_parse_parse_frame (GstBaseParse * parse,
82     GstBaseParseFrame * frame);
83
84 gboolean gst_aac_parse_convert (GstBaseParse * parse,
85     GstFormat src_format,
86     gint64 src_value, GstFormat dest_format, gint64 * dest_value);
87
88 gint gst_aac_parse_get_frame_overhead (GstBaseParse * parse,
89     GstBuffer * buffer);
90
91 gboolean gst_aac_parse_event (GstBaseParse * parse, GstEvent * event);
92
93 G_DEFINE_TYPE (GstAacParse, gst_aac_parse, GST_TYPE_BASE_PARSE);
94
95 static inline gint
96 gst_aac_parse_get_sample_rate_from_index (guint sr_idx)
97 {
98   static const guint aac_sample_rates[] = { 96000, 88200, 64000, 48000, 44100,
99     32000, 24000, 22050, 16000, 12000, 11025, 8000
100   };
101
102   if (sr_idx < G_N_ELEMENTS (aac_sample_rates))
103     return aac_sample_rates[sr_idx];
104   GST_WARNING ("Invalid sample rate index %u", sr_idx);
105   return 0;
106 }
107
108 /**
109  * gst_aac_parse_class_init:
110  * @klass: #GstAacParseClass.
111  *
112  */
113 static void
114 gst_aac_parse_class_init (GstAacParseClass * klass)
115 {
116   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
117   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
118
119   GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0,
120       "AAC audio stream parser");
121
122   gst_element_class_add_pad_template (element_class,
123       gst_static_pad_template_get (&sink_template));
124   gst_element_class_add_pad_template (element_class,
125       gst_static_pad_template_get (&src_template));
126
127   gst_element_class_set_details_simple (element_class,
128       "AAC audio stream parser", "Codec/Parser/Audio",
129       "Advanced Audio Coding parser", "Stefan Kost <stefan.kost@nokia.com>");
130
131   parse_class->start = GST_DEBUG_FUNCPTR (gst_aac_parse_start);
132   parse_class->stop = GST_DEBUG_FUNCPTR (gst_aac_parse_stop);
133   parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_setcaps);
134   parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_parse_frame);
135   parse_class->check_valid_frame =
136       GST_DEBUG_FUNCPTR (gst_aac_parse_check_valid_frame);
137 }
138
139
140 /**
141  * gst_aac_parse_init:
142  * @aacparse: #GstAacParse.
143  * @klass: #GstAacParseClass.
144  *
145  */
146 static void
147 gst_aac_parse_init (GstAacParse * aacparse)
148 {
149   GST_DEBUG ("initialized");
150 }
151
152
153 /**
154  * gst_aac_parse_set_src_caps:
155  * @aacparse: #GstAacParse.
156  * @sink_caps: (proposed) caps of sink pad
157  *
158  * Set source pad caps according to current knowledge about the
159  * audio stream.
160  *
161  * Returns: TRUE if caps were successfully set.
162  */
163 static gboolean
164 gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps)
165 {
166   GstStructure *s;
167   GstCaps *src_caps = NULL;
168   gboolean res = FALSE;
169   const gchar *stream_format;
170
171   GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps);
172   if (sink_caps)
173     src_caps = gst_caps_copy (sink_caps);
174   else
175     src_caps = gst_caps_new_simple ("audio/mpeg", NULL);
176
177   gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE,
178       "mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL);
179
180   switch (aacparse->header_type) {
181     case DSPAAC_HEADER_NONE:
182       stream_format = "raw";
183       break;
184     case DSPAAC_HEADER_ADTS:
185       stream_format = "adts";
186       break;
187     case DSPAAC_HEADER_ADIF:
188       stream_format = "adif";
189       break;
190     default:
191       stream_format = NULL;
192   }
193
194   s = gst_caps_get_structure (src_caps, 0);
195   if (aacparse->sample_rate > 0)
196     gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL);
197   if (aacparse->channels > 0)
198     gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL);
199   if (stream_format)
200     gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL);
201
202   GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps);
203
204   res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps);
205   gst_caps_unref (src_caps);
206   return res;
207 }
208
209
210 /**
211  * gst_aac_parse_sink_setcaps:
212  * @sinkpad: GstPad
213  * @caps: GstCaps
214  *
215  * Implementation of "set_sink_caps" vmethod in #GstBaseParse class.
216  *
217  * Returns: TRUE on success.
218  */
219 static gboolean
220 gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
221 {
222   GstAacParse *aacparse;
223   GstStructure *structure;
224   gchar *caps_str;
225   const GValue *value;
226
227   aacparse = GST_AAC_PARSE (parse);
228   structure = gst_caps_get_structure (caps, 0);
229   caps_str = gst_caps_to_string (caps);
230
231   GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str);
232   g_free (caps_str);
233
234   /* This is needed at least in case of RTP
235    * Parses the codec_data information to get ObjectType,
236    * number of channels and samplerate */
237   value = gst_structure_get_value (structure, "codec_data");
238   if (value) {
239     GstBuffer *buf = gst_value_get_buffer (value);
240
241     if (buf) {
242       guint8 *data;
243       gsize size;
244       guint sr_idx;
245
246       data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
247
248       sr_idx = ((data[0] & 0x07) << 1) | ((data[1] & 0x80) >> 7);
249       aacparse->object_type = (data[0] & 0xf8) >> 3;
250       aacparse->sample_rate = gst_aac_parse_get_sample_rate_from_index (sr_idx);
251       aacparse->channels = (data[1] & 0x78) >> 3;
252       aacparse->header_type = DSPAAC_HEADER_NONE;
253       aacparse->mpegversion = 4;
254       aacparse->frame_samples = (data[1] & 4) ? 960 : 1024;
255       gst_buffer_unmap (buf, data, size);
256
257       GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d, "
258           "samples=%d", aacparse->object_type, aacparse->sample_rate,
259           aacparse->channels, aacparse->frame_samples);
260
261       /* arrange for metadata and get out of the way */
262       gst_aac_parse_set_src_caps (aacparse, caps);
263       gst_base_parse_set_passthrough (parse, TRUE);
264     } else
265       return FALSE;
266
267     /* caps info overrides */
268     gst_structure_get_int (structure, "rate", &aacparse->sample_rate);
269     gst_structure_get_int (structure, "channels", &aacparse->channels);
270   } else {
271     gst_base_parse_set_passthrough (parse, FALSE);
272   }
273
274   return TRUE;
275 }
276
277
278 /**
279  * gst_aac_parse_adts_get_frame_len:
280  * @data: block of data containing an ADTS header.
281  *
282  * This function calculates ADTS frame length from the given header.
283  *
284  * Returns: size of the ADTS frame.
285  */
286 static inline guint
287 gst_aac_parse_adts_get_frame_len (const guint8 * data)
288 {
289   return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5);
290 }
291
292
293 /**
294  * gst_aac_parse_check_adts_frame:
295  * @aacparse: #GstAacParse.
296  * @data: Data to be checked.
297  * @avail: Amount of data passed.
298  * @framesize: If valid ADTS frame was found, this will be set to tell the
299  *             found frame size in bytes.
300  * @needed_data: If frame was not found, this may be set to tell how much
301  *               more data is needed in the next round to detect the frame
302  *               reliably. This may happen when a frame header candidate
303  *               is found but it cannot be guaranteed to be the header without
304  *               peeking the following data.
305  *
306  * Check if the given data contains contains ADTS frame. The algorithm
307  * will examine ADTS frame header and calculate the frame size. Also, another
308  * consecutive ADTS frame header need to be present after the found frame.
309  * Otherwise the data is not considered as a valid ADTS frame. However, this
310  * "extra check" is omitted when EOS has been received. In this case it is
311  * enough when data[0] contains a valid ADTS header.
312  *
313  * This function may set the #needed_data to indicate that a possible frame
314  * candidate has been found, but more data (#needed_data bytes) is needed to
315  * be absolutely sure. When this situation occurs, FALSE will be returned.
316  *
317  * When a valid frame is detected, this function will use
318  * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
319  * to set the needed bytes for next frame.This way next data chunk is already
320  * of correct size.
321  *
322  * Returns: TRUE if the given data contains a valid ADTS header.
323  */
324 static gboolean
325 gst_aac_parse_check_adts_frame (GstAacParse * aacparse,
326     const guint8 * data, const guint avail, gboolean drain,
327     guint * framesize, guint * needed_data)
328 {
329   if (G_UNLIKELY (avail < 2))
330     return FALSE;
331
332   if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) {
333     *framesize = gst_aac_parse_adts_get_frame_len (data);
334
335     /* In EOS mode this is enough. No need to examine the data further.
336        We also relax the check when we have sync, on the assumption that
337        if we're not looking at random data, we have a much higher chance
338        to get the correct sync, and this avoids losing two frames when
339        a single bit corruption happens. */
340     if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
341       return TRUE;
342     }
343
344     if (*framesize + ADTS_MAX_SIZE > avail) {
345       /* We have found a possible frame header candidate, but can't be
346          sure since we don't have enough data to check the next frame */
347       GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
348           *framesize + ADTS_MAX_SIZE, avail);
349       *needed_data = *framesize + ADTS_MAX_SIZE;
350       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
351           *framesize + ADTS_MAX_SIZE);
352       return FALSE;
353     }
354
355     if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) {
356       guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize));
357
358       GST_LOG ("ADTS frame found, len: %d bytes", *framesize);
359       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
360           nextlen + ADTS_MAX_SIZE);
361       return TRUE;
362     }
363   }
364   return FALSE;
365 }
366
367 /* caller ensure sufficient data */
368 static inline void
369 gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data,
370     gint * rate, gint * channels, gint * object, gint * version)
371 {
372
373   if (rate) {
374     gint sr_idx = (data[2] & 0x3c) >> 2;
375
376     *rate = gst_aac_parse_get_sample_rate_from_index (sr_idx);
377   }
378   if (channels)
379     *channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6);
380
381   if (version)
382     *version = (data[1] & 0x08) ? 2 : 4;
383   if (object)
384     *object = (data[2] & 0xc0) >> 6;
385 }
386
387 /**
388  * gst_aac_parse_detect_stream:
389  * @aacparse: #GstAacParse.
390  * @data: A block of data that needs to be examined for stream characteristics.
391  * @avail: Size of the given datablock.
392  * @framesize: If valid stream was found, this will be set to tell the
393  *             first frame size in bytes.
394  * @skipsize: If valid stream was found, this will be set to tell the first
395  *            audio frame position within the given data.
396  *
397  * Examines the given piece of data and try to detect the format of it. It
398  * checks for "ADIF" header (in the beginning of the clip) and ADTS frame
399  * header. If the stream is detected, TRUE will be returned and #framesize
400  * is set to indicate the found frame size. Additionally, #skipsize might
401  * be set to indicate the number of bytes that need to be skipped, a.k.a. the
402  * position of the frame inside given data chunk.
403  *
404  * Returns: TRUE on success.
405  */
406 static gboolean
407 gst_aac_parse_detect_stream (GstAacParse * aacparse,
408     const guint8 * data, const guint avail, gboolean drain,
409     guint * framesize, gint * skipsize)
410 {
411   gboolean found = FALSE;
412   guint need_data = 0;
413   guint i = 0;
414
415   GST_DEBUG_OBJECT (aacparse, "Parsing header data");
416
417   /* FIXME: No need to check for ADIF if we are not in the beginning of the
418      stream */
419
420   /* Can we even parse the header? */
421   if (avail < ADTS_MAX_SIZE)
422     return FALSE;
423
424   for (i = 0; i < avail - 4; i++) {
425     if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) ||
426         strncmp ((char *) data + i, "ADIF", 4) == 0) {
427       found = TRUE;
428
429       if (i) {
430         /* Trick: tell the parent class that we didn't find the frame yet,
431            but make it skip 'i' amount of bytes. Next time we arrive
432            here we have full frame in the beginning of the data. */
433         *skipsize = i;
434         return FALSE;
435       }
436       break;
437     }
438   }
439   if (!found) {
440     if (i)
441       *skipsize = i;
442     return FALSE;
443   }
444
445   if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain,
446           framesize, &need_data)) {
447     gint rate, channels;
448
449     GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize);
450
451     aacparse->header_type = DSPAAC_HEADER_ADTS;
452     gst_aac_parse_parse_adts_header (aacparse, data, &rate, &channels,
453         &aacparse->object_type, &aacparse->mpegversion);
454
455     gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
456         aacparse->frame_samples, 2, 2);
457
458     GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d",
459         rate, channels, aacparse->object_type, aacparse->mpegversion);
460
461     gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
462
463     return TRUE;
464   } else if (need_data) {
465     /* This tells the parent class not to skip any data */
466     *skipsize = 0;
467     return FALSE;
468   }
469
470   if (avail < ADIF_MAX_SIZE)
471     return FALSE;
472
473   if (memcmp (data + i, "ADIF", 4) == 0) {
474     const guint8 *adif;
475     int skip_size = 0;
476     int bitstream_type;
477     int sr_idx;
478     GstCaps *sinkcaps;
479
480     aacparse->header_type = DSPAAC_HEADER_ADIF;
481     aacparse->mpegversion = 4;
482
483     /* Skip the "ADIF" bytes */
484     adif = data + i + 4;
485
486     /* copyright string */
487     if (adif[0] & 0x80)
488       skip_size += 9;           /* skip 9 bytes */
489
490     bitstream_type = adif[0 + skip_size] & 0x10;
491     aacparse->bitrate =
492         ((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) |
493         ((unsigned int) adif[1 + skip_size] << 11) |
494         ((unsigned int) adif[2 + skip_size] << 3) |
495         ((unsigned int) adif[3 + skip_size] & 0xe0);
496
497     /* CBR */
498     if (bitstream_type == 0) {
499 #if 0
500       /* Buffer fullness parsing. Currently not needed... */
501       guint num_elems = 0;
502       guint fullness = 0;
503
504       num_elems = (adif[3 + skip_size] & 0x1e);
505       GST_INFO ("ADIF num_config_elems: %d", num_elems);
506
507       fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) |
508           ((unsigned int) adif[4 + skip_size] << 11) |
509           ((unsigned int) adif[5 + skip_size] << 3) |
510           ((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5);
511
512       GST_INFO ("ADIF buffer fullness: %d", fullness);
513 #endif
514       aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) |
515           ((adif[7 + skip_size] & 0x80) >> 7);
516       sr_idx = (adif[7 + skip_size] & 0x78) >> 3;
517     }
518     /* VBR */
519     else {
520       aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3;
521       sr_idx = ((adif[4 + skip_size] & 0x07) << 1) |
522           ((adif[5 + skip_size] & 0x80) >> 7);
523     }
524
525     /* FIXME: This gives totally wrong results. Duration calculation cannot
526        be based on this */
527     aacparse->sample_rate = gst_aac_parse_get_sample_rate_from_index (sr_idx);
528
529     /* baseparse is not given any fps,
530      * so it will give up on timestamps, seeking, etc */
531
532     /* FIXME: Can we assume this? */
533     aacparse->channels = 2;
534
535     GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d",
536         aacparse->bitrate, aacparse->sample_rate, aacparse->object_type);
537
538     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512);
539
540     /* arrange for metadata and get out of the way */
541     sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (aacparse));
542     gst_aac_parse_set_src_caps (aacparse, sinkcaps);
543     gst_caps_unref (sinkcaps);
544
545     /* not syncable, not easily seekable (unless we push data from start */
546     gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE);
547     gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE);
548     gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0);
549
550     *framesize = avail;
551     return TRUE;
552   }
553
554   /* This should never happen */
555   return FALSE;
556 }
557
558
559 /**
560  * gst_aac_parse_check_valid_frame:
561  * @parse: #GstBaseParse.
562  * @buffer: #GstBuffer.
563  * @framesize: If the buffer contains a valid frame, its size will be put here
564  * @skipsize: How much data parent class should skip in order to find the
565  *            frame header.
566  *
567  * Implementation of "check_valid_frame" vmethod in #GstBaseParse class.
568  *
569  * Returns: TRUE if buffer contains a valid frame.
570  */
571 gboolean
572 gst_aac_parse_check_valid_frame (GstBaseParse * parse,
573     GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
574 {
575   guint8 *data;
576   gsize size;
577   GstAacParse *aacparse;
578   gboolean ret = FALSE;
579   gboolean lost_sync;
580   GstBuffer *buffer;
581
582   aacparse = GST_AAC_PARSE (parse);
583   buffer = frame->buffer;
584
585   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
586
587   lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
588
589   if (aacparse->header_type == DSPAAC_HEADER_ADIF ||
590       aacparse->header_type == DSPAAC_HEADER_NONE) {
591     /* There is nothing to parse */
592     *framesize = size;
593     ret = TRUE;
594
595   } else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) {
596
597     ret = gst_aac_parse_detect_stream (aacparse, data, size,
598         GST_BASE_PARSE_DRAINING (parse), framesize, skipsize);
599
600   } else if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
601     guint needed_data = 1024;
602
603     ret = gst_aac_parse_check_adts_frame (aacparse, data, size,
604         GST_BASE_PARSE_DRAINING (parse), framesize, &needed_data);
605
606     if (!ret) {
607       GST_DEBUG ("buffer didn't contain valid frame");
608       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
609           needed_data);
610     }
611
612   } else {
613     GST_DEBUG ("buffer didn't contain valid frame");
614     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
615         ADTS_MAX_SIZE);
616   }
617   gst_buffer_unmap (buffer, data, size);
618
619   return ret;
620 }
621
622
623 /**
624  * gst_aac_parse_parse_frame:
625  * @parse: #GstBaseParse.
626  * @buffer: #GstBuffer.
627  *
628  * Implementation of "parse_frame" vmethod in #GstBaseParse class.
629  *
630  * Also determines frame overhead.
631  * ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have
632  * a per-frame header.
633  *
634  * We're making a couple of simplifying assumptions:
635  *
636  * 1. We count Program Configuration Elements rather than searching for them
637  *    in the streams to discount them - the overhead is negligible.
638  *
639  * 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16
640  *    bits, which should still not be significant enough to warrant the
641  *    additional parsing through the headers
642  *
643  * Returns: GST_FLOW_OK if frame was successfully parsed and can be pushed
644  *          forward. Otherwise appropriate error is returned.
645  */
646 GstFlowReturn
647 gst_aac_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
648 {
649   GstAacParse *aacparse;
650   GstBuffer *buffer;
651   GstFlowReturn ret = GST_FLOW_OK;
652   gint rate, channels;
653   guint8 *data;
654   gsize size;
655
656   aacparse = GST_AAC_PARSE (parse);
657   buffer = frame->buffer;
658
659   if (G_UNLIKELY (aacparse->header_type != DSPAAC_HEADER_ADTS))
660     return ret;
661
662   /* see above */
663   frame->overhead = 7;
664
665   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
666   gst_aac_parse_parse_adts_header (aacparse, data,
667       &rate, &channels, NULL, NULL);
668   gst_buffer_unmap (buffer, data, size);
669
670   GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels);
671
672   if (G_UNLIKELY (rate != aacparse->sample_rate
673           || channels != aacparse->channels)) {
674     GstCaps *sinkcaps;
675
676     aacparse->sample_rate = rate;
677     aacparse->channels = channels;
678
679     sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE (aacparse)->sinkpad);
680     if (!gst_aac_parse_set_src_caps (aacparse, sinkcaps)) {
681       /* If linking fails, we need to return appropriate error */
682       gst_caps_unref (sinkcaps);
683       ret = GST_FLOW_NOT_LINKED;
684     }
685     gst_caps_unref (sinkcaps);
686
687     gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
688         aacparse->sample_rate, aacparse->frame_samples, 2, 2);
689   }
690
691   return ret;
692 }
693
694
695 /**
696  * gst_aac_parse_start:
697  * @parse: #GstBaseParse.
698  *
699  * Implementation of "start" vmethod in #GstBaseParse class.
700  *
701  * Returns: TRUE if startup succeeded.
702  */
703 gboolean
704 gst_aac_parse_start (GstBaseParse * parse)
705 {
706   GstAacParse *aacparse;
707
708   aacparse = GST_AAC_PARSE (parse);
709   GST_DEBUG ("start");
710   aacparse->frame_samples = 1024;
711   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), ADTS_MAX_SIZE);
712   return TRUE;
713 }
714
715
716 /**
717  * gst_aac_parse_stop:
718  * @parse: #GstBaseParse.
719  *
720  * Implementation of "stop" vmethod in #GstBaseParse class.
721  *
722  * Returns: TRUE is stopping succeeded.
723  */
724 gboolean
725 gst_aac_parse_stop (GstBaseParse * parse)
726 {
727   GST_DEBUG ("stop");
728   return TRUE;
729 }