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