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