aacparse: put codec data on caps for loas format
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, 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-1.0 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 <gst/base/gstbitreader.h>
48 #include <gst/pbutils/pbutils.h>
49 #include "gstaacparse.h"
50
51
52 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
53     GST_PAD_SRC,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("audio/mpeg, "
56         "framed = (boolean) true, " "mpegversion = (int) { 2, 4 }, "
57         "stream-format = (string) { raw, adts, adif, loas };"));
58
59 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
60     GST_PAD_SINK,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) { 2, 4 };"));
63
64 GST_DEBUG_CATEGORY_STATIC (aacparse_debug);
65 #define GST_CAT_DEFAULT aacparse_debug
66
67
68 #define ADIF_MAX_SIZE 40        /* Should be enough */
69 #define ADTS_MAX_SIZE 10        /* Should be enough */
70 #define LOAS_MAX_SIZE 3         /* Should be enough */
71
72 #define ADTS_HEADERS_LENGTH 7UL /* Total byte-length of fixed and variable
73                                    headers prepended during raw to ADTS
74                                    conversion */
75
76 #define AAC_FRAME_DURATION(parse) (GST_SECOND/parse->frames_per_sec)
77
78 static const gint loas_sample_rate_table[32] = {
79   96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
80   16000, 12000, 11025, 8000, 7350, 0, 0, 0
81 };
82
83 static const gint loas_channels_table[32] = {
84   0, 1, 2, 3, 4, 5, 6, 8,
85   0, 0, 0, 0, 0, 0, 0, 0
86 };
87
88 static gboolean gst_aac_parse_start (GstBaseParse * parse);
89 static gboolean gst_aac_parse_stop (GstBaseParse * parse);
90
91 static gboolean gst_aac_parse_sink_setcaps (GstBaseParse * parse,
92     GstCaps * caps);
93 static GstCaps *gst_aac_parse_sink_getcaps (GstBaseParse * parse,
94     GstCaps * filter);
95
96 static GstFlowReturn gst_aac_parse_handle_frame (GstBaseParse * parse,
97     GstBaseParseFrame * frame, gint * skipsize);
98 static GstFlowReturn gst_aac_parse_pre_push_frame (GstBaseParse * parse,
99     GstBaseParseFrame * frame);
100
101 G_DEFINE_TYPE (GstAacParse, gst_aac_parse, GST_TYPE_BASE_PARSE);
102
103 /**
104  * gst_aac_parse_class_init:
105  * @klass: #GstAacParseClass.
106  *
107  */
108 static void
109 gst_aac_parse_class_init (GstAacParseClass * klass)
110 {
111   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
112   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
113
114   GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0,
115       "AAC audio stream parser");
116
117   gst_element_class_add_pad_template (element_class,
118       gst_static_pad_template_get (&sink_template));
119   gst_element_class_add_pad_template (element_class,
120       gst_static_pad_template_get (&src_template));
121
122   gst_element_class_set_static_metadata (element_class,
123       "AAC audio stream parser", "Codec/Parser/Audio",
124       "Advanced Audio Coding parser", "Stefan Kost <stefan.kost@nokia.com>");
125
126   parse_class->start = GST_DEBUG_FUNCPTR (gst_aac_parse_start);
127   parse_class->stop = GST_DEBUG_FUNCPTR (gst_aac_parse_stop);
128   parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_setcaps);
129   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_getcaps);
130   parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_handle_frame);
131   parse_class->pre_push_frame =
132       GST_DEBUG_FUNCPTR (gst_aac_parse_pre_push_frame);
133 }
134
135
136 /**
137  * gst_aac_parse_init:
138  * @aacparse: #GstAacParse.
139  * @klass: #GstAacParseClass.
140  *
141  */
142 static void
143 gst_aac_parse_init (GstAacParse * aacparse)
144 {
145   GST_DEBUG ("initialized");
146   GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (aacparse));
147 }
148
149 static GstBuffer *
150 gst_aac_parse_create_codec_data (GstAacParse * aacparse)
151 {
152   GstMapInfo map;
153   int idx;
154   GstBuffer *codec_data;
155   guint16 codec_data_data;
156
157   idx = gst_codec_utils_aac_get_index_from_sample_rate (aacparse->sample_rate);
158   if (idx < 0)
159     return NULL;
160
161   /* The codec_data data is according to AudioSpecificConfig,
162      ISO/IEC 14496-3, 1.6.2.1 */
163   codec_data = gst_buffer_new_and_alloc (2);
164   gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
165   codec_data_data =
166       (aacparse->object_type << 11) | (idx << 7) | (aacparse->channels << 3);
167   GST_WRITE_UINT16_BE (map.data, codec_data_data);
168   gst_buffer_unmap (codec_data, &map);
169
170   return codec_data;
171 }
172
173
174 /**
175  * gst_aac_parse_set_src_caps:
176  * @aacparse: #GstAacParse.
177  * @sink_caps: (proposed) caps of sink pad
178  *
179  * Set source pad caps according to current knowledge about the
180  * audio stream.
181  *
182  * Returns: TRUE if caps were successfully set.
183  */
184 static gboolean
185 gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps)
186 {
187   GstStructure *s;
188   GstCaps *src_caps = NULL, *allowed;
189   gboolean res = FALSE;
190   const gchar *stream_format;
191   GstBuffer *codec_data;
192   gboolean need_codec_data = FALSE;
193
194   GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps);
195   if (sink_caps)
196     src_caps = gst_caps_copy (sink_caps);
197   else
198     src_caps = gst_caps_new_empty_simple ("audio/mpeg");
199
200   gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE,
201       "mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL);
202
203   aacparse->output_header_type = aacparse->header_type;
204   switch (aacparse->header_type) {
205     case DSPAAC_HEADER_NONE:
206       stream_format = "raw";
207       break;
208     case DSPAAC_HEADER_ADTS:
209       stream_format = "adts";
210       break;
211     case DSPAAC_HEADER_ADIF:
212       stream_format = "adif";
213       break;
214     case DSPAAC_HEADER_LOAS:
215       stream_format = "loas";
216       need_codec_data = TRUE;
217       break;
218     default:
219       stream_format = NULL;
220   }
221
222   s = gst_caps_get_structure (src_caps, 0);
223   if (aacparse->sample_rate > 0)
224     gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL);
225   if (aacparse->channels > 0)
226     gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL);
227   if (stream_format)
228     gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL);
229
230   if (need_codec_data) {
231     codec_data = gst_aac_parse_create_codec_data (aacparse);
232     if (codec_data != NULL)
233       gst_structure_set (s, "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
234   }
235
236   allowed = gst_pad_get_allowed_caps (GST_BASE_PARSE (aacparse)->srcpad);
237   if (!gst_caps_can_intersect (src_caps, allowed)) {
238     GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
239         "Caps can not intersect");
240     if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
241       GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
242           "Input is ADTS, trying raw");
243       gst_caps_set_simple (src_caps, "stream-format", G_TYPE_STRING, "raw",
244           NULL);
245       if (gst_caps_can_intersect (src_caps, allowed)) {
246
247         GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
248             "Caps can intersect, we will drop the ADTS layer");
249         aacparse->output_header_type = DSPAAC_HEADER_NONE;
250
251         codec_data = gst_aac_parse_create_codec_data (aacparse);
252         if (codec_data == NULL)
253           goto codec_data_failed;
254         gst_caps_set_simple (src_caps, "codec_data", GST_TYPE_BUFFER,
255             codec_data, NULL);
256       }
257     } else if (aacparse->header_type == DSPAAC_HEADER_NONE) {
258       GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
259           "Input is raw, trying ADTS");
260       gst_caps_set_simple (src_caps, "stream-format", G_TYPE_STRING, "adts",
261           NULL);
262       if (gst_caps_can_intersect (src_caps, allowed)) {
263         GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
264             "Caps can intersect, we will prepend ADTS headers");
265         aacparse->output_header_type = DSPAAC_HEADER_ADTS;
266       }
267     }
268   }
269   gst_caps_unref (allowed);
270
271   GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps);
272
273   res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps);
274   gst_caps_unref (src_caps);
275   return res;
276
277 codec_data_failed:
278   gst_caps_unref (allowed);
279   gst_caps_unref (src_caps);
280   return FALSE;
281 }
282
283
284 /**
285  * gst_aac_parse_sink_setcaps:
286  * @sinkpad: GstPad
287  * @caps: GstCaps
288  *
289  * Implementation of "set_sink_caps" vmethod in #GstBaseParse class.
290  *
291  * Returns: TRUE on success.
292  */
293 static gboolean
294 gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
295 {
296   GstAacParse *aacparse;
297   GstStructure *structure;
298   gchar *caps_str;
299   const GValue *value;
300
301   aacparse = GST_AAC_PARSE (parse);
302   structure = gst_caps_get_structure (caps, 0);
303   caps_str = gst_caps_to_string (caps);
304
305   GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str);
306   g_free (caps_str);
307
308   /* This is needed at least in case of RTP
309    * Parses the codec_data information to get ObjectType,
310    * number of channels and samplerate */
311   value = gst_structure_get_value (structure, "codec_data");
312   if (value) {
313     GstBuffer *buf = gst_value_get_buffer (value);
314
315     if (buf) {
316       GstMapInfo map;
317       guint sr_idx;
318
319       gst_buffer_map (buf, &map, GST_MAP_READ);
320
321       sr_idx = ((map.data[0] & 0x07) << 1) | ((map.data[1] & 0x80) >> 7);
322       aacparse->object_type = (map.data[0] & 0xf8) >> 3;
323       aacparse->sample_rate =
324           gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
325       aacparse->channels = (map.data[1] & 0x78) >> 3;
326       aacparse->header_type = DSPAAC_HEADER_NONE;
327       aacparse->mpegversion = 4;
328       aacparse->frame_samples = (map.data[1] & 4) ? 960 : 1024;
329       gst_buffer_unmap (buf, &map);
330
331       GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d, "
332           "samples=%d", aacparse->object_type, aacparse->sample_rate,
333           aacparse->channels, aacparse->frame_samples);
334
335       /* arrange for metadata and get out of the way */
336       gst_aac_parse_set_src_caps (aacparse, caps);
337       if (aacparse->header_type == aacparse->output_header_type)
338         gst_base_parse_set_passthrough (parse, TRUE);
339     } else
340       return FALSE;
341
342     /* caps info overrides */
343     gst_structure_get_int (structure, "rate", &aacparse->sample_rate);
344     gst_structure_get_int (structure, "channels", &aacparse->channels);
345   } else {
346     aacparse->sample_rate = 0;
347     aacparse->channels = 0;
348     aacparse->header_type = DSPAAC_HEADER_NOT_PARSED;
349     gst_base_parse_set_passthrough (parse, FALSE);
350   }
351
352   return TRUE;
353 }
354
355
356 /**
357  * gst_aac_parse_adts_get_frame_len:
358  * @data: block of data containing an ADTS header.
359  *
360  * This function calculates ADTS frame length from the given header.
361  *
362  * Returns: size of the ADTS frame.
363  */
364 static inline guint
365 gst_aac_parse_adts_get_frame_len (const guint8 * data)
366 {
367   return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5);
368 }
369
370
371 /**
372  * gst_aac_parse_check_adts_frame:
373  * @aacparse: #GstAacParse.
374  * @data: Data to be checked.
375  * @avail: Amount of data passed.
376  * @framesize: If valid ADTS frame was found, this will be set to tell the
377  *             found frame size in bytes.
378  * @needed_data: If frame was not found, this may be set to tell how much
379  *               more data is needed in the next round to detect the frame
380  *               reliably. This may happen when a frame header candidate
381  *               is found but it cannot be guaranteed to be the header without
382  *               peeking the following data.
383  *
384  * Check if the given data contains contains ADTS frame. The algorithm
385  * will examine ADTS frame header and calculate the frame size. Also, another
386  * consecutive ADTS frame header need to be present after the found frame.
387  * Otherwise the data is not considered as a valid ADTS frame. However, this
388  * "extra check" is omitted when EOS has been received. In this case it is
389  * enough when data[0] contains a valid ADTS header.
390  *
391  * This function may set the #needed_data to indicate that a possible frame
392  * candidate has been found, but more data (#needed_data bytes) is needed to
393  * be absolutely sure. When this situation occurs, FALSE will be returned.
394  *
395  * When a valid frame is detected, this function will use
396  * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
397  * to set the needed bytes for next frame.This way next data chunk is already
398  * of correct size.
399  *
400  * Returns: TRUE if the given data contains a valid ADTS header.
401  */
402 static gboolean
403 gst_aac_parse_check_adts_frame (GstAacParse * aacparse,
404     const guint8 * data, const guint avail, gboolean drain,
405     guint * framesize, guint * needed_data)
406 {
407   guint crc_size;
408
409   *needed_data = 0;
410
411   /* Absolute minimum to perform the ADTS syncword,
412      layer and sampling frequency tests */
413   if (G_UNLIKELY (avail < 3))
414     return FALSE;
415
416   /* Syncword and layer tests */
417   if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) {
418
419     /* Sampling frequency test */
420     if (G_UNLIKELY ((data[2] & 0x3C) >> 2 == 15))
421       return FALSE;
422
423     /* This looks like an ADTS frame header but
424        we need at least 6 bytes to proceed */
425     if (G_UNLIKELY (avail < 6)) {
426       *needed_data = 6;
427       return FALSE;
428     }
429
430     *framesize = gst_aac_parse_adts_get_frame_len (data);
431
432     /* If frame has CRC, it needs 2 bytes
433        for it at the end of the header */
434     crc_size = (data[1] & 0x01) ? 0 : 2;
435
436     /* CRC size test */
437     if (*framesize < 7 + crc_size)
438       return FALSE;
439
440     /* In EOS mode this is enough. No need to examine the data further.
441        We also relax the check when we have sync, on the assumption that
442        if we're not looking at random data, we have a much higher chance
443        to get the correct sync, and this avoids losing two frames when
444        a single bit corruption happens. */
445     if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
446       return TRUE;
447     }
448
449     if (*framesize + ADTS_MAX_SIZE > avail) {
450       /* We have found a possible frame header candidate, but can't be
451          sure since we don't have enough data to check the next frame */
452       GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
453           *framesize + ADTS_MAX_SIZE, avail);
454       *needed_data = *framesize + ADTS_MAX_SIZE;
455       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
456           *framesize + ADTS_MAX_SIZE);
457       return FALSE;
458     }
459
460     if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) {
461       guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize));
462
463       GST_LOG ("ADTS frame found, len: %d bytes", *framesize);
464       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
465           nextlen + ADTS_MAX_SIZE);
466       return TRUE;
467     }
468   }
469   return FALSE;
470 }
471
472 static gboolean
473 gst_aac_parse_latm_get_value (GstAacParse * aacparse, GstBitReader * br,
474     guint32 * value)
475 {
476   guint8 bytes, i, byte;
477
478   *value = 0;
479   if (!gst_bit_reader_get_bits_uint8 (br, &bytes, 2))
480     return FALSE;
481   for (i = 0; i < bytes; ++i) {
482     *value <<= 8;
483     if (!gst_bit_reader_get_bits_uint8 (br, &byte, 8))
484       return FALSE;
485     *value += byte;
486   }
487   return TRUE;
488 }
489
490 static gboolean
491 gst_aac_parse_get_audio_object_type (GstAacParse * aacparse, GstBitReader * br,
492     guint8 * audio_object_type)
493 {
494   if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 5))
495     return FALSE;
496   if (*audio_object_type == 31) {
497     if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 6))
498       return FALSE;
499     *audio_object_type += 32;
500   }
501   GST_LOG_OBJECT (aacparse, "audio object type %u", *audio_object_type);
502   return TRUE;
503 }
504
505 static gboolean
506 gst_aac_parse_get_audio_sample_rate (GstAacParse * aacparse, GstBitReader * br,
507     gint * sample_rate)
508 {
509   guint8 sampling_frequency_index;
510   if (!gst_bit_reader_get_bits_uint8 (br, &sampling_frequency_index, 4))
511     return FALSE;
512   GST_LOG_OBJECT (aacparse, "sampling_frequency_index: %u",
513       sampling_frequency_index);
514   if (sampling_frequency_index == 0xf) {
515     guint32 sampling_rate;
516     if (!gst_bit_reader_get_bits_uint32 (br, &sampling_rate, 24))
517       return FALSE;
518     *sample_rate = sampling_rate;
519   } else {
520     *sample_rate = loas_sample_rate_table[sampling_frequency_index];
521     if (!*sample_rate)
522       return FALSE;
523   }
524   return TRUE;
525 }
526
527 /* See table 1.13 in ISO/IEC 14496-3 */
528 static gboolean
529 gst_aac_parse_read_loas_audio_specific_config (GstAacParse * aacparse,
530     GstBitReader * br, gint * sample_rate, gint * channels,
531     guint8 * object_type, guint32 * bits)
532 {
533   guint8 channel_configuration;
534
535   if (!gst_aac_parse_get_audio_object_type (aacparse, br, object_type))
536     return FALSE;
537
538   if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
539     return FALSE;
540
541   if (!gst_bit_reader_get_bits_uint8 (br, &channel_configuration, 4))
542     return FALSE;
543   GST_LOG_OBJECT (aacparse, "channel_configuration: %d", channel_configuration);
544   *channels = loas_channels_table[channel_configuration];
545   if (!*channels)
546     return FALSE;
547
548   if (*object_type == 5) {
549     GST_LOG_OBJECT (aacparse,
550         "Audio object type 5, so rereading sampling rate...");
551     if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
552       return FALSE;
553   }
554
555   GST_INFO_OBJECT (aacparse, "Found LOAS config: %d Hz, %d channels,"
556       " audio object type: %u", *sample_rate, *channels, *object_type);
557
558   /* There's LOTS of stuff next, but we ignore it for now as we have
559      what we want (sample rate and number of channels */
560   GST_DEBUG_OBJECT (aacparse,
561       "Need more code to parse humongous LOAS data, currently ignored");
562   if (bits)
563     *bits = 0;
564   return TRUE;
565 }
566
567
568 static gboolean
569 gst_aac_parse_read_loas_config (GstAacParse * aacparse, const guint8 * data,
570     guint avail, gint * sample_rate, gint * channels,
571     guint8 * object_type, gint * version)
572 {
573   GstBitReader br;
574   guint8 u8, v, vA;
575
576   /* No version in the bitstream, but the spec has LOAS in the MPEG-4 section */
577   if (version)
578     *version = 4;
579
580   gst_bit_reader_init (&br, data, avail);
581
582   /* skip sync word (11 bits) and size (13 bits) */
583   if (!gst_bit_reader_skip (&br, 11 + 13))
584     return FALSE;
585
586   /* First bit is "use last config" */
587   if (!gst_bit_reader_get_bits_uint8 (&br, &u8, 1))
588     return FALSE;
589   if (u8) {
590     GST_DEBUG_OBJECT (aacparse, "Frame uses previous config");
591     if (!aacparse->sample_rate || !aacparse->channels) {
592       GST_WARNING_OBJECT (aacparse, "No previous config to use");
593     }
594     *sample_rate = aacparse->sample_rate;
595     *channels = aacparse->channels;
596     return TRUE;
597   }
598
599   GST_DEBUG_OBJECT (aacparse, "Frame contains new config");
600
601   if (!gst_bit_reader_get_bits_uint8 (&br, &v, 1))
602     return FALSE;
603   if (v) {
604     if (!gst_bit_reader_get_bits_uint8 (&br, &vA, 1))
605       return FALSE;
606   } else
607     vA = 0;
608
609   GST_LOG_OBJECT (aacparse, "v %d, vA %d", v, vA);
610   if (vA == 0) {
611     guint8 same_time, subframes, num_program, prog;
612     if (v == 1) {
613       guint32 value;
614       if (!gst_aac_parse_latm_get_value (aacparse, &br, &value))
615         return FALSE;
616     }
617     if (!gst_bit_reader_get_bits_uint8 (&br, &same_time, 1))
618       return FALSE;
619     if (!gst_bit_reader_get_bits_uint8 (&br, &subframes, 6))
620       return FALSE;
621     if (!gst_bit_reader_get_bits_uint8 (&br, &num_program, 4))
622       return FALSE;
623     GST_LOG_OBJECT (aacparse, "same_time %d, subframes %d, num_program %d",
624         same_time, subframes, num_program);
625
626     for (prog = 0; prog <= num_program; ++prog) {
627       guint8 num_layer, layer;
628       if (!gst_bit_reader_get_bits_uint8 (&br, &num_layer, 3))
629         return FALSE;
630       GST_LOG_OBJECT (aacparse, "Program %d: %d layers", prog, num_layer);
631
632       for (layer = 0; layer <= num_layer; ++layer) {
633         guint8 use_same_config;
634         if (prog == 0 && layer == 0) {
635           use_same_config = 0;
636         } else {
637           if (!gst_bit_reader_get_bits_uint8 (&br, &use_same_config, 1))
638             return FALSE;
639         }
640         if (!use_same_config) {
641           if (v == 0) {
642             if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
643                     sample_rate, channels, object_type, NULL))
644               return FALSE;
645           } else {
646             guint32 bits, asc_len;
647             if (!gst_aac_parse_latm_get_value (aacparse, &br, &asc_len))
648               return FALSE;
649             if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
650                     sample_rate, channels, object_type, &bits))
651               return FALSE;
652             asc_len -= bits;
653             if (!gst_bit_reader_skip (&br, asc_len))
654               return FALSE;
655           }
656         }
657       }
658     }
659     GST_LOG_OBJECT (aacparse, "More data ignored");
660   } else {
661     GST_WARNING_OBJECT (aacparse, "Spec says \"TBD\"...");
662   }
663   return TRUE;
664 }
665
666 /**
667  * gst_aac_parse_loas_get_frame_len:
668  * @data: block of data containing a LOAS header.
669  *
670  * This function calculates LOAS frame length from the given header.
671  *
672  * Returns: size of the LOAS frame.
673  */
674 static inline guint
675 gst_aac_parse_loas_get_frame_len (const guint8 * data)
676 {
677   return (((data[1] & 0x1f) << 8) | data[2]) + 3;
678 }
679
680
681 /**
682  * gst_aac_parse_check_loas_frame:
683  * @aacparse: #GstAacParse.
684  * @data: Data to be checked.
685  * @avail: Amount of data passed.
686  * @framesize: If valid LOAS frame was found, this will be set to tell the
687  *             found frame size in bytes.
688  * @needed_data: If frame was not found, this may be set to tell how much
689  *               more data is needed in the next round to detect the frame
690  *               reliably. This may happen when a frame header candidate
691  *               is found but it cannot be guaranteed to be the header without
692  *               peeking the following data.
693  *
694  * Check if the given data contains contains LOAS frame. The algorithm
695  * will examine LOAS frame header and calculate the frame size. Also, another
696  * consecutive LOAS frame header need to be present after the found frame.
697  * Otherwise the data is not considered as a valid LOAS frame. However, this
698  * "extra check" is omitted when EOS has been received. In this case it is
699  * enough when data[0] contains a valid LOAS header.
700  *
701  * This function may set the #needed_data to indicate that a possible frame
702  * candidate has been found, but more data (#needed_data bytes) is needed to
703  * be absolutely sure. When this situation occurs, FALSE will be returned.
704  *
705  * When a valid frame is detected, this function will use
706  * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
707  * to set the needed bytes for next frame.This way next data chunk is already
708  * of correct size.
709  *
710  * LOAS can have three different formats, if I read the spec correctly. Only
711  * one of them is supported here, as the two samples I have use this one.
712  *
713  * Returns: TRUE if the given data contains a valid LOAS header.
714  */
715 static gboolean
716 gst_aac_parse_check_loas_frame (GstAacParse * aacparse,
717     const guint8 * data, const guint avail, gboolean drain,
718     guint * framesize, guint * needed_data)
719 {
720   *needed_data = 0;
721
722   /* 3 byte header */
723   if (G_UNLIKELY (avail < 3))
724     return FALSE;
725
726   if ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) {
727     *framesize = gst_aac_parse_loas_get_frame_len (data);
728     GST_DEBUG_OBJECT (aacparse, "Found %u byte LOAS frame", *framesize);
729
730     /* In EOS mode this is enough. No need to examine the data further.
731        We also relax the check when we have sync, on the assumption that
732        if we're not looking at random data, we have a much higher chance
733        to get the correct sync, and this avoids losing two frames when
734        a single bit corruption happens. */
735     if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
736       return TRUE;
737     }
738
739     if (*framesize + LOAS_MAX_SIZE > avail) {
740       /* We have found a possible frame header candidate, but can't be
741          sure since we don't have enough data to check the next frame */
742       GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
743           *framesize + LOAS_MAX_SIZE, avail);
744       *needed_data = *framesize + LOAS_MAX_SIZE;
745       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
746           *framesize + LOAS_MAX_SIZE);
747       return FALSE;
748     }
749
750     if ((data[*framesize] == 0x56) && ((data[*framesize + 1] & 0xe0) == 0xe0)) {
751       guint nextlen = gst_aac_parse_loas_get_frame_len (data + (*framesize));
752
753       GST_LOG ("LOAS frame found, len: %d bytes", *framesize);
754       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
755           nextlen + LOAS_MAX_SIZE);
756       return TRUE;
757     }
758   }
759   return FALSE;
760 }
761
762 /* caller ensure sufficient data */
763 static inline void
764 gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data,
765     gint * rate, gint * channels, gint * object, gint * version)
766 {
767
768   if (rate) {
769     gint sr_idx = (data[2] & 0x3c) >> 2;
770
771     *rate = gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
772   }
773   if (channels)
774     *channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6);
775
776   if (version)
777     *version = (data[1] & 0x08) ? 2 : 4;
778   if (object)
779     *object = ((data[2] & 0xc0) >> 6) + 1;
780 }
781
782 /**
783  * gst_aac_parse_detect_stream:
784  * @aacparse: #GstAacParse.
785  * @data: A block of data that needs to be examined for stream characteristics.
786  * @avail: Size of the given datablock.
787  * @framesize: If valid stream was found, this will be set to tell the
788  *             first frame size in bytes.
789  * @skipsize: If valid stream was found, this will be set to tell the first
790  *            audio frame position within the given data.
791  *
792  * Examines the given piece of data and try to detect the format of it. It
793  * checks for "ADIF" header (in the beginning of the clip) and ADTS frame
794  * header. If the stream is detected, TRUE will be returned and #framesize
795  * is set to indicate the found frame size. Additionally, #skipsize might
796  * be set to indicate the number of bytes that need to be skipped, a.k.a. the
797  * position of the frame inside given data chunk.
798  *
799  * Returns: TRUE on success.
800  */
801 static gboolean
802 gst_aac_parse_detect_stream (GstAacParse * aacparse,
803     const guint8 * data, const guint avail, gboolean drain,
804     guint * framesize, gint * skipsize)
805 {
806   gboolean found = FALSE;
807   guint need_data_adts = 0, need_data_loas;
808   guint i = 0;
809
810   GST_DEBUG_OBJECT (aacparse, "Parsing header data");
811
812   /* FIXME: No need to check for ADIF if we are not in the beginning of the
813      stream */
814
815   /* Can we even parse the header? */
816   if (avail < MAX (ADTS_MAX_SIZE, LOAS_MAX_SIZE)) {
817     GST_DEBUG_OBJECT (aacparse, "Not enough data to check");
818     return FALSE;
819   }
820
821   for (i = 0; i < avail - 4; i++) {
822     if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) ||
823         ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) ||
824         strncmp ((char *) data + i, "ADIF", 4) == 0) {
825       GST_DEBUG_OBJECT (aacparse, "Found signature at offset %u", i);
826       found = TRUE;
827
828       if (i) {
829         /* Trick: tell the parent class that we didn't find the frame yet,
830            but make it skip 'i' amount of bytes. Next time we arrive
831            here we have full frame in the beginning of the data. */
832         *skipsize = i;
833         return FALSE;
834       }
835       break;
836     }
837   }
838   if (!found) {
839     if (i)
840       *skipsize = i;
841     return FALSE;
842   }
843
844   if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain,
845           framesize, &need_data_adts)) {
846     gint rate, channels;
847
848     GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize);
849
850     gst_aac_parse_parse_adts_header (aacparse, data, &rate, &channels,
851         &aacparse->object_type, &aacparse->mpegversion);
852
853     if (!channels || !framesize) {
854       GST_DEBUG_OBJECT (aacparse, "impossible ADTS configuration");
855       return FALSE;
856     }
857
858     aacparse->header_type = DSPAAC_HEADER_ADTS;
859     gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
860         aacparse->frame_samples, 2, 2);
861
862     GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d",
863         rate, channels, aacparse->object_type, aacparse->mpegversion);
864
865     gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
866
867     return TRUE;
868   }
869
870   if (gst_aac_parse_check_loas_frame (aacparse, data, avail, drain,
871           framesize, &need_data_loas)) {
872     gint rate, channels;
873     guint8 object_type;
874
875     GST_INFO ("LOAS, framesize: %d", *framesize);
876
877     aacparse->header_type = DSPAAC_HEADER_LOAS;
878
879     if (!gst_aac_parse_read_loas_config (aacparse, data, avail, &rate,
880             &channels, &object_type, &aacparse->mpegversion)) {
881       GST_WARNING_OBJECT (aacparse, "Error reading LOAS config");
882       return FALSE;
883     }
884
885     aacparse->object_type = object_type;
886
887     if (rate && channels) {
888       gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
889           aacparse->frame_samples, 2, 2);
890
891       GST_DEBUG ("LOAS: samplerate %d, channels %d, objtype %d, version %d",
892           rate, channels, aacparse->object_type, aacparse->mpegversion);
893       aacparse->sample_rate = rate;
894       aacparse->channels = channels;
895     }
896
897     gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
898
899     return TRUE;
900   }
901
902   if (need_data_adts || need_data_loas) {
903     /* This tells the parent class not to skip any data */
904     *skipsize = 0;
905     return FALSE;
906   }
907
908   if (avail < ADIF_MAX_SIZE)
909     return FALSE;
910
911   if (memcmp (data + i, "ADIF", 4) == 0) {
912     const guint8 *adif;
913     int skip_size = 0;
914     int bitstream_type;
915     int sr_idx;
916     GstCaps *sinkcaps;
917
918     aacparse->header_type = DSPAAC_HEADER_ADIF;
919     aacparse->mpegversion = 4;
920
921     /* Skip the "ADIF" bytes */
922     adif = data + i + 4;
923
924     /* copyright string */
925     if (adif[0] & 0x80)
926       skip_size += 9;           /* skip 9 bytes */
927
928     bitstream_type = adif[0 + skip_size] & 0x10;
929     aacparse->bitrate =
930         ((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) |
931         ((unsigned int) adif[1 + skip_size] << 11) |
932         ((unsigned int) adif[2 + skip_size] << 3) |
933         ((unsigned int) adif[3 + skip_size] & 0xe0);
934
935     /* CBR */
936     if (bitstream_type == 0) {
937 #if 0
938       /* Buffer fullness parsing. Currently not needed... */
939       guint num_elems = 0;
940       guint fullness = 0;
941
942       num_elems = (adif[3 + skip_size] & 0x1e);
943       GST_INFO ("ADIF num_config_elems: %d", num_elems);
944
945       fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) |
946           ((unsigned int) adif[4 + skip_size] << 11) |
947           ((unsigned int) adif[5 + skip_size] << 3) |
948           ((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5);
949
950       GST_INFO ("ADIF buffer fullness: %d", fullness);
951 #endif
952       aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) |
953           ((adif[7 + skip_size] & 0x80) >> 7);
954       sr_idx = (adif[7 + skip_size] & 0x78) >> 3;
955     }
956     /* VBR */
957     else {
958       aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3;
959       sr_idx = ((adif[4 + skip_size] & 0x07) << 1) |
960           ((adif[5 + skip_size] & 0x80) >> 7);
961     }
962
963     /* FIXME: This gives totally wrong results. Duration calculation cannot
964        be based on this */
965     aacparse->sample_rate =
966         gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
967
968     /* baseparse is not given any fps,
969      * so it will give up on timestamps, seeking, etc */
970
971     /* FIXME: Can we assume this? */
972     aacparse->channels = 2;
973
974     GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d",
975         aacparse->bitrate, aacparse->sample_rate, aacparse->object_type);
976
977     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512);
978
979     /* arrange for metadata and get out of the way */
980     sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (aacparse));
981     gst_aac_parse_set_src_caps (aacparse, sinkcaps);
982     if (sinkcaps)
983       gst_caps_unref (sinkcaps);
984
985     /* not syncable, not easily seekable (unless we push data from start */
986     gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE);
987     gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE);
988     gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0);
989
990     *framesize = avail;
991     return TRUE;
992   }
993
994   /* This should never happen */
995   return FALSE;
996 }
997
998 /**
999  * gst_aac_parse_get_audio_profile_object_type
1000  * @aacparse: #GstAacParse.
1001  *
1002  * Gets the MPEG-2 profile or the MPEG-4 object type value corresponding to the
1003  * mpegversion and profile of @aacparse's src pad caps, according to the
1004  * values defined by table 1.A.11 in ISO/IEC 14496-3.
1005  *
1006  * Returns: the profile or object type value corresponding to @aacparse's src
1007  * pad caps, if such a value exists; otherwise G_MAXUINT8.
1008  */
1009 static guint8
1010 gst_aac_parse_get_audio_profile_object_type (GstAacParse * aacparse)
1011 {
1012   GstCaps *srccaps;
1013   GstStructure *srcstruct;
1014   const gchar *profile;
1015   guint8 ret;
1016
1017   srccaps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse));
1018   srcstruct = gst_caps_get_structure (srccaps, 0);
1019   profile = gst_structure_get_string (srcstruct, "profile");
1020   if (G_UNLIKELY (profile == NULL)) {
1021     gst_caps_unref (srccaps);
1022     return G_MAXUINT8;
1023   }
1024
1025   if (g_strcmp0 (profile, "main") == 0) {
1026     ret = (guint8) 0U;
1027   } else if (g_strcmp0 (profile, "lc") == 0) {
1028     ret = (guint8) 1U;
1029   } else if (g_strcmp0 (profile, "ssr") == 0) {
1030     ret = (guint8) 2U;
1031   } else if (g_strcmp0 (profile, "ltp") == 0) {
1032     if (G_LIKELY (aacparse->mpegversion == 4))
1033       ret = (guint8) 3U;
1034     else
1035       ret = G_MAXUINT8;         /* LTP Object Type allowed only for MPEG-4 */
1036   } else {
1037     ret = G_MAXUINT8;
1038   }
1039
1040   gst_caps_unref (srccaps);
1041   return ret;
1042 }
1043
1044 /**
1045  * gst_aac_parse_get_audio_channel_configuration
1046  * @num_channels: number of audio channels.
1047  *
1048  * Gets the Channel Configuration value, as defined by table 1.19 in ISO/IEC
1049  * 14496-3, for a given number of audio channels.
1050  *
1051  * Returns: the Channel Configuration value corresponding to @num_channels, if
1052  * such a value exists; otherwise G_MAXUINT8.
1053  */
1054 static guint8
1055 gst_aac_parse_get_audio_channel_configuration (gint num_channels)
1056 {
1057   if (num_channels >= 1 && num_channels <= 6)   /* Mono up to & including 5.1 */
1058     return (guint8) num_channels;
1059   else if (num_channels == 8)   /* 7.1 */
1060     return (guint8) 7U;
1061   else
1062     return G_MAXUINT8;
1063 }
1064
1065 /**
1066  * gst_aac_parse_get_audio_sampling_frequency_index:
1067  * @sample_rate: audio sampling rate.
1068  *
1069  * Gets the Sampling Frequency Index value, as defined by table 1.18 in ISO/IEC
1070  * 14496-3, for a given sampling rate.
1071  *
1072  * Returns: the Sampling Frequency Index value corresponding to @sample_rate,
1073  * if such a value exists; otherwise G_MAXUINT8.
1074  */
1075 static guint8
1076 gst_aac_parse_get_audio_sampling_frequency_index (gint sample_rate)
1077 {
1078   switch (sample_rate) {
1079     case 96000:
1080       return 0x0U;
1081     case 88200:
1082       return 0x1U;
1083     case 64000:
1084       return 0x2U;
1085     case 48000:
1086       return 0x3U;
1087     case 44100:
1088       return 0x4U;
1089     case 32000:
1090       return 0x5U;
1091     case 24000:
1092       return 0x6U;
1093     case 22050:
1094       return 0x7U;
1095     case 16000:
1096       return 0x8U;
1097     case 12000:
1098       return 0x9U;
1099     case 11025:
1100       return 0xAU;
1101     case 8000:
1102       return 0xBU;
1103     case 7350:
1104       return 0xCU;
1105     default:
1106       return G_MAXUINT8;
1107   }
1108 }
1109
1110 /**
1111  * gst_aac_parse_prepend_adts_headers:
1112  * @aacparse: #GstAacParse.
1113  * @frame: raw AAC frame to which ADTS headers shall be prepended.
1114  *
1115  * Prepends ADTS headers to a raw AAC audio frame.
1116  *
1117  * Returns: TRUE if ADTS headers were successfully prepended; FALSE otherwise.
1118  */
1119 static gboolean
1120 gst_aac_parse_prepend_adts_headers (GstAacParse * aacparse,
1121     GstBaseParseFrame * frame)
1122 {
1123   GstMemory *mem;
1124   guint8 *adts_headers;
1125   gsize buf_size;
1126   gsize frame_size;
1127   guint8 id, profile, channel_configuration, sampling_frequency_index;
1128
1129   id = (aacparse->mpegversion == 4) ? 0x0U : 0x1U;
1130   profile = gst_aac_parse_get_audio_profile_object_type (aacparse);
1131   if (profile == G_MAXUINT8) {
1132     GST_ERROR_OBJECT (aacparse, "Unsupported audio profile or object type");
1133     return FALSE;
1134   }
1135   channel_configuration =
1136       gst_aac_parse_get_audio_channel_configuration (aacparse->channels);
1137   if (channel_configuration == G_MAXUINT8) {
1138     GST_ERROR_OBJECT (aacparse, "Unsupported number of channels");
1139     return FALSE;
1140   }
1141   sampling_frequency_index =
1142       gst_aac_parse_get_audio_sampling_frequency_index (aacparse->sample_rate);
1143   if (sampling_frequency_index == G_MAXUINT8) {
1144     GST_ERROR_OBJECT (aacparse, "Unsupported sampling frequency");
1145     return FALSE;
1146   }
1147
1148   frame->out_buffer = gst_buffer_copy (frame->buffer);
1149   buf_size = gst_buffer_get_size (frame->out_buffer);
1150   frame_size = buf_size + ADTS_HEADERS_LENGTH;
1151
1152   if (G_UNLIKELY (frame_size >= 0x4000)) {
1153     GST_ERROR_OBJECT (aacparse, "Frame size is too big for ADTS");
1154     return FALSE;
1155   }
1156
1157   adts_headers = (guint8 *) g_malloc0 (ADTS_HEADERS_LENGTH);
1158
1159   /* Note: no error correction bits are added to the resulting ADTS frames */
1160   adts_headers[0] = 0xFFU;
1161   adts_headers[1] = 0xF0U | (id << 3) | 0x1U;
1162   adts_headers[2] = (profile << 6) | (sampling_frequency_index << 2) | 0x2U |
1163       (channel_configuration & 0x4U);
1164   adts_headers[3] = ((channel_configuration & 0x3U) << 6) | 0x30U |
1165       (guint8) (frame_size >> 11);
1166   adts_headers[4] = (guint8) ((frame_size >> 3) & 0x00FF);
1167   adts_headers[5] = (guint8) (((frame_size & 0x0007) << 5) + 0x1FU);
1168   adts_headers[6] = 0xFCU;
1169
1170   mem = gst_memory_new_wrapped (0, adts_headers, ADTS_HEADERS_LENGTH, 0,
1171       ADTS_HEADERS_LENGTH, NULL, NULL);
1172   gst_buffer_prepend_memory (frame->out_buffer, mem);
1173
1174   return TRUE;
1175 }
1176
1177 /**
1178  * gst_aac_parse_check_valid_frame:
1179  * @parse: #GstBaseParse.
1180  * @frame: #GstBaseParseFrame.
1181  * @skipsize: How much data parent class should skip in order to find the
1182  *            frame header.
1183  *
1184  * Implementation of "handle_frame" vmethod in #GstBaseParse class.
1185  *
1186  * Also determines frame overhead.
1187  * ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have
1188  * a per-frame header. LOAS has 3 bytes.
1189  *
1190  * We're making a couple of simplifying assumptions:
1191  *
1192  * 1. We count Program Configuration Elements rather than searching for them
1193  *    in the streams to discount them - the overhead is negligible.
1194  *
1195  * 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16
1196  *    bits, which should still not be significant enough to warrant the
1197  *    additional parsing through the headers
1198  *
1199  * Returns: a #GstFlowReturn.
1200  */
1201 static GstFlowReturn
1202 gst_aac_parse_handle_frame (GstBaseParse * parse,
1203     GstBaseParseFrame * frame, gint * skipsize)
1204 {
1205   GstMapInfo map;
1206   GstAacParse *aacparse;
1207   gboolean ret = FALSE;
1208   gboolean lost_sync;
1209   GstBuffer *buffer;
1210   guint framesize;
1211   gint rate, channels;
1212   guint8 object_type;
1213
1214   aacparse = GST_AAC_PARSE (parse);
1215   buffer = frame->buffer;
1216
1217   gst_buffer_map (buffer, &map, GST_MAP_READ);
1218
1219   *skipsize = -1;
1220   lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
1221
1222   if (aacparse->header_type == DSPAAC_HEADER_ADIF ||
1223       aacparse->header_type == DSPAAC_HEADER_NONE) {
1224     /* There is nothing to parse */
1225     framesize = map.size;
1226     ret = TRUE;
1227
1228   } else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) {
1229
1230     ret = gst_aac_parse_detect_stream (aacparse, map.data, map.size,
1231         GST_BASE_PARSE_DRAINING (parse), &framesize, skipsize);
1232
1233   } else if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1234     guint needed_data = 1024;
1235
1236     ret = gst_aac_parse_check_adts_frame (aacparse, map.data, map.size,
1237         GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
1238
1239     if (!ret) {
1240       GST_DEBUG ("buffer didn't contain valid frame");
1241       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1242           needed_data);
1243     }
1244
1245   } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1246     guint needed_data = 1024;
1247
1248     ret = gst_aac_parse_check_loas_frame (aacparse, map.data,
1249         map.size, GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
1250
1251     if (!ret) {
1252       GST_DEBUG ("buffer didn't contain valid frame");
1253       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1254           needed_data);
1255     }
1256
1257   } else {
1258     GST_DEBUG ("buffer didn't contain valid frame");
1259     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1260         ADTS_MAX_SIZE);
1261   }
1262
1263   if (G_UNLIKELY (!ret))
1264     goto exit;
1265
1266   if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1267     /* see above */
1268     frame->overhead = 7;
1269
1270     gst_aac_parse_parse_adts_header (aacparse, map.data,
1271         &rate, &channels, NULL, NULL);
1272
1273     GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels);
1274
1275     if (G_UNLIKELY (rate != aacparse->sample_rate
1276             || channels != aacparse->channels)) {
1277       aacparse->sample_rate = rate;
1278       aacparse->channels = channels;
1279
1280       if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1281         /* If linking fails, we need to return appropriate error */
1282         ret = GST_FLOW_NOT_LINKED;
1283       }
1284
1285       gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1286           aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1287     }
1288   } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1289     gboolean setcaps = FALSE;
1290
1291     /* see above */
1292     frame->overhead = 3;
1293
1294     if (!gst_aac_parse_read_loas_config (aacparse, map.data, map.size, &rate,
1295             &channels, &object_type, NULL)) {
1296       GST_WARNING_OBJECT (aacparse, "Error reading LOAS config");
1297     } else if (G_UNLIKELY (rate != aacparse->sample_rate
1298             || channels != aacparse->channels)) {
1299       aacparse->sample_rate = rate;
1300       aacparse->channels = channels;
1301       aacparse->object_type = object_type;
1302       setcaps = TRUE;
1303       GST_INFO_OBJECT (aacparse, "New LOAS config: %d Hz, %d channels, "
1304           "audio object type: %u", rate, channels, object_type);
1305     }
1306
1307     /* We want to set caps both at start, and when rate/channels change.
1308        Since only some LOAS frames have that info, we may receive frames
1309        before knowing about rate/channels. */
1310     if (setcaps
1311         || !gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse))) {
1312       if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1313         /* If linking fails, we need to return appropriate error */
1314         ret = GST_FLOW_NOT_LINKED;
1315       }
1316
1317       gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1318           aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1319     }
1320   }
1321
1322   if (aacparse->header_type == DSPAAC_HEADER_NONE
1323       && aacparse->output_header_type == DSPAAC_HEADER_ADTS) {
1324     if (!gst_aac_parse_prepend_adts_headers (aacparse, frame)) {
1325       GST_ERROR_OBJECT (aacparse, "Failed to prepend ADTS headers to frame");
1326       ret = GST_FLOW_ERROR;
1327     }
1328   }
1329
1330 exit:
1331   gst_buffer_unmap (buffer, &map);
1332
1333   if (ret) {
1334     /* found, skip if needed */
1335     if (*skipsize > 0)
1336       return GST_FLOW_OK;
1337     *skipsize = 0;
1338   } else {
1339     if (*skipsize < 0)
1340       *skipsize = 1;
1341   }
1342
1343   if (ret && framesize <= map.size) {
1344     return gst_base_parse_finish_frame (parse, frame, framesize);
1345   }
1346
1347   return GST_FLOW_OK;
1348 }
1349
1350 static GstFlowReturn
1351 gst_aac_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
1352 {
1353   GstAacParse *aacparse = GST_AAC_PARSE (parse);
1354
1355   if (!aacparse->sent_codec_tag) {
1356     GstTagList *taglist;
1357     GstCaps *caps;
1358
1359     taglist = gst_tag_list_new_empty ();
1360
1361     /* codec tag */
1362     caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
1363     gst_pb_utils_add_codec_description_to_tag_list (taglist,
1364         GST_TAG_AUDIO_CODEC, caps);
1365     gst_caps_unref (caps);
1366
1367     gst_pad_push_event (GST_BASE_PARSE_SRC_PAD (aacparse),
1368         gst_event_new_tag (taglist));
1369
1370     /* also signals the end of first-frame processing */
1371     aacparse->sent_codec_tag = TRUE;
1372   }
1373
1374   /* As a special case, we can remove the ADTS framing and output raw AAC. */
1375   if (aacparse->header_type == DSPAAC_HEADER_ADTS
1376       && aacparse->output_header_type == DSPAAC_HEADER_NONE) {
1377     guint header_size;
1378     GstMapInfo map;
1379     gst_buffer_map (frame->buffer, &map, GST_MAP_READ);
1380     header_size = (map.data[1] & 1) ? 7 : 9;    /* optional CRC */
1381     gst_buffer_unmap (frame->buffer, &map);
1382     gst_buffer_resize (frame->buffer, header_size,
1383         gst_buffer_get_size (frame->buffer) - header_size);
1384   }
1385
1386   return GST_FLOW_OK;
1387 }
1388
1389
1390 /**
1391  * gst_aac_parse_start:
1392  * @parse: #GstBaseParse.
1393  *
1394  * Implementation of "start" vmethod in #GstBaseParse class.
1395  *
1396  * Returns: TRUE if startup succeeded.
1397  */
1398 static gboolean
1399 gst_aac_parse_start (GstBaseParse * parse)
1400 {
1401   GstAacParse *aacparse;
1402
1403   aacparse = GST_AAC_PARSE (parse);
1404   GST_DEBUG ("start");
1405   aacparse->frame_samples = 1024;
1406   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), ADTS_MAX_SIZE);
1407   aacparse->sent_codec_tag = FALSE;
1408   return TRUE;
1409 }
1410
1411
1412 /**
1413  * gst_aac_parse_stop:
1414  * @parse: #GstBaseParse.
1415  *
1416  * Implementation of "stop" vmethod in #GstBaseParse class.
1417  *
1418  * Returns: TRUE is stopping succeeded.
1419  */
1420 static gboolean
1421 gst_aac_parse_stop (GstBaseParse * parse)
1422 {
1423   GST_DEBUG ("stop");
1424   return TRUE;
1425 }
1426
1427 static void
1428 remove_fields (GstCaps * caps)
1429 {
1430   guint i, n;
1431
1432   n = gst_caps_get_size (caps);
1433   for (i = 0; i < n; i++) {
1434     GstStructure *s = gst_caps_get_structure (caps, i);
1435
1436     gst_structure_remove_field (s, "framed");
1437   }
1438 }
1439
1440 static GstCaps *
1441 gst_aac_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter)
1442 {
1443   GstCaps *peercaps, *templ;
1444   GstCaps *res;
1445
1446   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1447
1448   if (filter) {
1449     GstCaps *fcopy = gst_caps_copy (filter);
1450     /* Remove the fields we convert */
1451     remove_fields (fcopy);
1452     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
1453     gst_caps_unref (fcopy);
1454   } else
1455     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
1456
1457   if (peercaps) {
1458     peercaps = gst_caps_make_writable (peercaps);
1459     /* Remove the fields we convert */
1460     remove_fields (peercaps);
1461
1462     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1463     gst_caps_unref (peercaps);
1464     gst_caps_unref (templ);
1465   } else {
1466     res = templ;
1467   }
1468
1469   if (filter) {
1470     GstCaps *intersection;
1471
1472     intersection =
1473         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1474     gst_caps_unref (res);
1475     res = intersection;
1476   }
1477
1478   return res;
1479 }