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