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