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