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