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