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