aacparse: allow conversion from ADTS to raw AAC
[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
73 #define AAC_FRAME_DURATION(parse) (GST_SECOND/parse->frames_per_sec)
74
75 static const gint loas_sample_rate_table[32] = {
76   96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
77   16000, 12000, 11025, 8000, 7350, 0, 0, 0
78 };
79
80 static const gint loas_channels_table[32] = {
81   0, 1, 2, 3, 4, 5, 6, 8,
82   0, 0, 0, 0, 0, 0, 0, 0
83 };
84
85 static gboolean gst_aac_parse_start (GstBaseParse * parse);
86 static gboolean gst_aac_parse_stop (GstBaseParse * parse);
87
88 static gboolean gst_aac_parse_sink_setcaps (GstBaseParse * parse,
89     GstCaps * caps);
90 static GstCaps *gst_aac_parse_sink_getcaps (GstBaseParse * parse,
91     GstCaps * filter);
92
93 static GstFlowReturn gst_aac_parse_handle_frame (GstBaseParse * parse,
94     GstBaseParseFrame * frame, gint * skipsize);
95 static GstFlowReturn gst_aac_parse_pre_push_frame (GstBaseParse * parse,
96     GstBaseParseFrame * frame);
97
98 G_DEFINE_TYPE (GstAacParse, gst_aac_parse, GST_TYPE_BASE_PARSE);
99
100 /**
101  * gst_aac_parse_class_init:
102  * @klass: #GstAacParseClass.
103  *
104  */
105 static void
106 gst_aac_parse_class_init (GstAacParseClass * klass)
107 {
108   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
109   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
110
111   GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0,
112       "AAC audio stream parser");
113
114   gst_element_class_add_pad_template (element_class,
115       gst_static_pad_template_get (&sink_template));
116   gst_element_class_add_pad_template (element_class,
117       gst_static_pad_template_get (&src_template));
118
119   gst_element_class_set_static_metadata (element_class,
120       "AAC audio stream parser", "Codec/Parser/Audio",
121       "Advanced Audio Coding parser", "Stefan Kost <stefan.kost@nokia.com>");
122
123   parse_class->start = GST_DEBUG_FUNCPTR (gst_aac_parse_start);
124   parse_class->stop = GST_DEBUG_FUNCPTR (gst_aac_parse_stop);
125   parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_setcaps);
126   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_getcaps);
127   parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_handle_frame);
128   parse_class->pre_push_frame =
129       GST_DEBUG_FUNCPTR (gst_aac_parse_pre_push_frame);
130 }
131
132
133 /**
134  * gst_aac_parse_init:
135  * @aacparse: #GstAacParse.
136  * @klass: #GstAacParseClass.
137  *
138  */
139 static void
140 gst_aac_parse_init (GstAacParse * aacparse)
141 {
142   GST_DEBUG ("initialized");
143 }
144
145
146 /**
147  * gst_aac_parse_set_src_caps:
148  * @aacparse: #GstAacParse.
149  * @sink_caps: (proposed) caps of sink pad
150  *
151  * Set source pad caps according to current knowledge about the
152  * audio stream.
153  *
154  * Returns: TRUE if caps were successfully set.
155  */
156 static gboolean
157 gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps)
158 {
159   GstStructure *s;
160   GstCaps *src_caps = NULL, *allowed;
161   gboolean res = FALSE;
162   const gchar *stream_format;
163   GstBuffer *codec_data;
164   guint16 codec_data_data;
165
166   GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps);
167   if (sink_caps)
168     src_caps = gst_caps_copy (sink_caps);
169   else
170     src_caps = gst_caps_new_empty_simple ("audio/mpeg");
171
172   gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE,
173       "mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL);
174
175   aacparse->output_header_type = aacparse->header_type;
176   switch (aacparse->header_type) {
177     case DSPAAC_HEADER_NONE:
178       stream_format = "raw";
179       break;
180     case DSPAAC_HEADER_ADTS:
181       stream_format = "adts";
182       break;
183     case DSPAAC_HEADER_ADIF:
184       stream_format = "adif";
185       break;
186     case DSPAAC_HEADER_LOAS:
187       stream_format = "loas";
188       break;
189     default:
190       stream_format = NULL;
191   }
192
193   s = gst_caps_get_structure (src_caps, 0);
194   if (aacparse->sample_rate > 0)
195     gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL);
196   if (aacparse->channels > 0)
197     gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL);
198   if (stream_format)
199     gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL);
200
201   allowed = gst_pad_get_allowed_caps (GST_BASE_PARSE (aacparse)->srcpad);
202   if (!gst_caps_can_intersect (src_caps, allowed)) {
203     GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
204         "Caps can not intersect");
205     if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
206       GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
207           "Input is ADTS, trying raw");
208       gst_caps_set_simple (src_caps, "stream-format", G_TYPE_STRING, "raw",
209           NULL);
210       if (gst_caps_can_intersect (src_caps, allowed)) {
211         GstMapInfo map;
212         int idx;
213
214         idx =
215             gst_codec_utils_aac_get_index_from_sample_rate
216             (aacparse->sample_rate);
217         if (idx < 0)
218           goto not_a_known_rate;
219
220         GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
221             "Caps can intersect, we will drop the ADTS layer");
222         aacparse->output_header_type = DSPAAC_HEADER_NONE;
223
224         /* The codec_data data is according to AudioSpecificConfig,
225            ISO/IEC 14496-3, 1.6.2.1 */
226         codec_data = gst_buffer_new_and_alloc (2);
227         gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
228         codec_data_data =
229             (aacparse->object_type << 11) |
230             (idx << 7) | (aacparse->channels << 3);
231         GST_WRITE_UINT16_BE (map.data, codec_data_data);
232         gst_buffer_unmap (codec_data, &map);
233         gst_caps_set_simple (src_caps, "codec_data", GST_TYPE_BUFFER,
234             codec_data, NULL);
235       }
236     }
237   }
238   gst_caps_unref (allowed);
239
240   GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps);
241
242   res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps);
243   gst_caps_unref (src_caps);
244   return res;
245
246 not_a_known_rate:
247   gst_caps_unref (allowed);
248   gst_caps_unref (src_caps);
249   return FALSE;
250 }
251
252
253 /**
254  * gst_aac_parse_sink_setcaps:
255  * @sinkpad: GstPad
256  * @caps: GstCaps
257  *
258  * Implementation of "set_sink_caps" vmethod in #GstBaseParse class.
259  *
260  * Returns: TRUE on success.
261  */
262 static gboolean
263 gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
264 {
265   GstAacParse *aacparse;
266   GstStructure *structure;
267   gchar *caps_str;
268   const GValue *value;
269
270   aacparse = GST_AAC_PARSE (parse);
271   structure = gst_caps_get_structure (caps, 0);
272   caps_str = gst_caps_to_string (caps);
273
274   GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str);
275   g_free (caps_str);
276
277   /* This is needed at least in case of RTP
278    * Parses the codec_data information to get ObjectType,
279    * number of channels and samplerate */
280   value = gst_structure_get_value (structure, "codec_data");
281   if (value) {
282     GstBuffer *buf = gst_value_get_buffer (value);
283
284     if (buf) {
285       GstMapInfo map;
286       guint sr_idx;
287
288       gst_buffer_map (buf, &map, GST_MAP_READ);
289
290       sr_idx = ((map.data[0] & 0x07) << 1) | ((map.data[1] & 0x80) >> 7);
291       aacparse->object_type = (map.data[0] & 0xf8) >> 3;
292       aacparse->sample_rate =
293           gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
294       aacparse->channels = (map.data[1] & 0x78) >> 3;
295       aacparse->header_type = DSPAAC_HEADER_NONE;
296       aacparse->mpegversion = 4;
297       aacparse->frame_samples = (map.data[1] & 4) ? 960 : 1024;
298       gst_buffer_unmap (buf, &map);
299
300       GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d, "
301           "samples=%d", aacparse->object_type, aacparse->sample_rate,
302           aacparse->channels, aacparse->frame_samples);
303
304       /* arrange for metadata and get out of the way */
305       gst_aac_parse_set_src_caps (aacparse, caps);
306       gst_base_parse_set_passthrough (parse, TRUE);
307     } else
308       return FALSE;
309
310     /* caps info overrides */
311     gst_structure_get_int (structure, "rate", &aacparse->sample_rate);
312     gst_structure_get_int (structure, "channels", &aacparse->channels);
313   } else {
314     aacparse->sample_rate = 0;
315     aacparse->channels = 0;
316     aacparse->header_type = DSPAAC_HEADER_NOT_PARSED;
317     gst_base_parse_set_passthrough (parse, FALSE);
318   }
319
320   return TRUE;
321 }
322
323
324 /**
325  * gst_aac_parse_adts_get_frame_len:
326  * @data: block of data containing an ADTS header.
327  *
328  * This function calculates ADTS frame length from the given header.
329  *
330  * Returns: size of the ADTS frame.
331  */
332 static inline guint
333 gst_aac_parse_adts_get_frame_len (const guint8 * data)
334 {
335   return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5);
336 }
337
338
339 /**
340  * gst_aac_parse_check_adts_frame:
341  * @aacparse: #GstAacParse.
342  * @data: Data to be checked.
343  * @avail: Amount of data passed.
344  * @framesize: If valid ADTS frame was found, this will be set to tell the
345  *             found frame size in bytes.
346  * @needed_data: If frame was not found, this may be set to tell how much
347  *               more data is needed in the next round to detect the frame
348  *               reliably. This may happen when a frame header candidate
349  *               is found but it cannot be guaranteed to be the header without
350  *               peeking the following data.
351  *
352  * Check if the given data contains contains ADTS frame. The algorithm
353  * will examine ADTS frame header and calculate the frame size. Also, another
354  * consecutive ADTS frame header need to be present after the found frame.
355  * Otherwise the data is not considered as a valid ADTS frame. However, this
356  * "extra check" is omitted when EOS has been received. In this case it is
357  * enough when data[0] contains a valid ADTS header.
358  *
359  * This function may set the #needed_data to indicate that a possible frame
360  * candidate has been found, but more data (#needed_data bytes) is needed to
361  * be absolutely sure. When this situation occurs, FALSE will be returned.
362  *
363  * When a valid frame is detected, this function will use
364  * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
365  * to set the needed bytes for next frame.This way next data chunk is already
366  * of correct size.
367  *
368  * Returns: TRUE if the given data contains a valid ADTS header.
369  */
370 static gboolean
371 gst_aac_parse_check_adts_frame (GstAacParse * aacparse,
372     const guint8 * data, const guint avail, gboolean drain,
373     guint * framesize, guint * needed_data)
374 {
375   *needed_data = 0;
376
377   if (G_UNLIKELY (avail < 2))
378     return FALSE;
379
380   if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) {
381     *framesize = gst_aac_parse_adts_get_frame_len (data);
382
383     /* In EOS mode this is enough. No need to examine the data further.
384        We also relax the check when we have sync, on the assumption that
385        if we're not looking at random data, we have a much higher chance
386        to get the correct sync, and this avoids losing two frames when
387        a single bit corruption happens. */
388     if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
389       return TRUE;
390     }
391
392     if (*framesize + ADTS_MAX_SIZE > avail) {
393       /* We have found a possible frame header candidate, but can't be
394          sure since we don't have enough data to check the next frame */
395       GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
396           *framesize + ADTS_MAX_SIZE, avail);
397       *needed_data = *framesize + ADTS_MAX_SIZE;
398       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
399           *framesize + ADTS_MAX_SIZE);
400       return FALSE;
401     }
402
403     if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) {
404       guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize));
405
406       GST_LOG ("ADTS frame found, len: %d bytes", *framesize);
407       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
408           nextlen + ADTS_MAX_SIZE);
409       return TRUE;
410     }
411   }
412   return FALSE;
413 }
414
415 static gboolean
416 gst_aac_parse_latm_get_value (GstAacParse * aacparse, GstBitReader * br,
417     guint32 * value)
418 {
419   guint8 bytes, i, byte;
420
421   *value = 0;
422   if (!gst_bit_reader_get_bits_uint8 (br, &bytes, 2))
423     return FALSE;
424   for (i = 0; i < bytes; ++i) {
425     *value <<= 8;
426     if (!gst_bit_reader_get_bits_uint8 (br, &byte, 8))
427       return FALSE;
428     *value += byte;
429   }
430   return TRUE;
431 }
432
433 static gboolean
434 gst_aac_parse_get_audio_object_type (GstAacParse * aacparse, GstBitReader * br,
435     guint8 * audio_object_type)
436 {
437   if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 5))
438     return FALSE;
439   if (*audio_object_type == 31) {
440     if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 6))
441       return FALSE;
442     *audio_object_type += 32;
443   }
444   GST_LOG_OBJECT (aacparse, "audio object type %u", *audio_object_type);
445   return TRUE;
446 }
447
448 static gboolean
449 gst_aac_parse_get_audio_sample_rate (GstAacParse * aacparse, GstBitReader * br,
450     gint * sample_rate)
451 {
452   guint8 sampling_frequency_index;
453   if (!gst_bit_reader_get_bits_uint8 (br, &sampling_frequency_index, 4))
454     return FALSE;
455   GST_LOG_OBJECT (aacparse, "sampling_frequency_index: %u",
456       sampling_frequency_index);
457   if (sampling_frequency_index == 0xf) {
458     guint32 sampling_rate;
459     if (!gst_bit_reader_get_bits_uint32 (br, &sampling_rate, 24))
460       return FALSE;
461     *sample_rate = sampling_rate;
462   } else {
463     *sample_rate = loas_sample_rate_table[sampling_frequency_index];
464     if (!*sample_rate)
465       return FALSE;
466   }
467   return TRUE;
468 }
469
470 /* See table 1.13 in ISO/IEC 14496-3 */
471 static gboolean
472 gst_aac_parse_read_loas_audio_specific_config (GstAacParse * aacparse,
473     GstBitReader * br, gint * sample_rate, gint * channels, guint32 * bits)
474 {
475   guint8 audio_object_type, channel_configuration;
476
477   if (!gst_aac_parse_get_audio_object_type (aacparse, br, &audio_object_type))
478     return FALSE;
479
480   if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
481     return FALSE;
482
483   if (!gst_bit_reader_get_bits_uint8 (br, &channel_configuration, 4))
484     return FALSE;
485   GST_LOG_OBJECT (aacparse, "channel_configuration: %d", channel_configuration);
486   *channels = loas_channels_table[channel_configuration];
487   if (!*channels)
488     return FALSE;
489
490   if (audio_object_type == 5) {
491     GST_LOG_OBJECT (aacparse,
492         "Audio object type 5, so rereading sampling rate...");
493     if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
494       return FALSE;
495   }
496
497   GST_INFO_OBJECT (aacparse, "Found LOAS config: %d Hz, %d channels",
498       *sample_rate, *channels);
499
500   /* There's LOTS of stuff next, but we ignore it for now as we have
501      what we want (sample rate and number of channels */
502   GST_DEBUG_OBJECT (aacparse,
503       "Need more code to parse humongous LOAS data, currently ignored");
504   if (bits)
505     *bits = 0;
506   return TRUE;
507 }
508
509
510 static gboolean
511 gst_aac_parse_read_loas_config (GstAacParse * aacparse, const guint8 * data,
512     guint avail, gint * sample_rate, gint * channels, gint * version)
513 {
514   GstBitReader br;
515   guint8 u8, v, vA;
516
517   /* No version in the bitstream, but the spec has LOAS in the MPEG-4 section */
518   if (version)
519     *version = 4;
520
521   gst_bit_reader_init (&br, data, avail);
522
523   /* skip sync word (11 bits) and size (13 bits) */
524   if (!gst_bit_reader_skip (&br, 11 + 13))
525     return FALSE;
526
527   /* First bit is "use last config" */
528   if (!gst_bit_reader_get_bits_uint8 (&br, &u8, 1))
529     return FALSE;
530   if (u8) {
531     GST_DEBUG_OBJECT (aacparse, "Frame uses previous config");
532     if (!aacparse->sample_rate || !aacparse->channels) {
533       GST_WARNING_OBJECT (aacparse, "No previous config to use");
534     }
535     *sample_rate = aacparse->sample_rate;
536     *channels = aacparse->channels;
537     return TRUE;
538   }
539
540   GST_DEBUG_OBJECT (aacparse, "Frame contains new config");
541
542   if (!gst_bit_reader_get_bits_uint8 (&br, &v, 1))
543     return FALSE;
544   if (v) {
545     if (!gst_bit_reader_get_bits_uint8 (&br, &vA, 1))
546       return FALSE;
547   } else
548     vA = 0;
549
550   GST_LOG_OBJECT (aacparse, "v %d, vA %d", v, vA);
551   if (vA == 0) {
552     guint8 same_time, subframes, num_program, prog;
553     if (v == 1) {
554       guint32 value;
555       if (!gst_aac_parse_latm_get_value (aacparse, &br, &value))
556         return FALSE;
557     }
558     if (!gst_bit_reader_get_bits_uint8 (&br, &same_time, 1))
559       return FALSE;
560     if (!gst_bit_reader_get_bits_uint8 (&br, &subframes, 6))
561       return FALSE;
562     if (!gst_bit_reader_get_bits_uint8 (&br, &num_program, 4))
563       return FALSE;
564     GST_LOG_OBJECT (aacparse, "same_time %d, subframes %d, num_program %d",
565         same_time, subframes, num_program);
566
567     for (prog = 0; prog <= num_program; ++prog) {
568       guint8 num_layer, layer;
569       if (!gst_bit_reader_get_bits_uint8 (&br, &num_layer, 3))
570         return FALSE;
571       GST_LOG_OBJECT (aacparse, "Program %d: %d layers", prog, num_layer);
572
573       for (layer = 0; layer <= num_layer; ++layer) {
574         guint8 use_same_config;
575         if (prog == 0 && layer == 0) {
576           use_same_config = 0;
577         } else {
578           if (!gst_bit_reader_get_bits_uint8 (&br, &use_same_config, 1))
579             return FALSE;
580         }
581         if (!use_same_config) {
582           if (v == 0) {
583             if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
584                     sample_rate, channels, NULL))
585               return FALSE;
586           } else {
587             guint32 bits, asc_len;
588             if (!gst_aac_parse_latm_get_value (aacparse, &br, &asc_len))
589               return FALSE;
590             if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
591                     sample_rate, channels, &bits))
592               return FALSE;
593             asc_len -= bits;
594             if (!gst_bit_reader_skip (&br, asc_len))
595               return FALSE;
596           }
597         }
598       }
599     }
600     GST_LOG_OBJECT (aacparse, "More data ignored");
601   } else {
602     GST_WARNING_OBJECT (aacparse, "Spec says \"TBD\"...");
603   }
604   return TRUE;
605 }
606
607 /**
608  * gst_aac_parse_loas_get_frame_len:
609  * @data: block of data containing a LOAS header.
610  *
611  * This function calculates LOAS frame length from the given header.
612  *
613  * Returns: size of the LOAS frame.
614  */
615 static inline guint
616 gst_aac_parse_loas_get_frame_len (const guint8 * data)
617 {
618   return (((data[1] & 0x1f) << 8) | data[2]) + 3;
619 }
620
621
622 /**
623  * gst_aac_parse_check_loas_frame:
624  * @aacparse: #GstAacParse.
625  * @data: Data to be checked.
626  * @avail: Amount of data passed.
627  * @framesize: If valid LOAS frame was found, this will be set to tell the
628  *             found frame size in bytes.
629  * @needed_data: If frame was not found, this may be set to tell how much
630  *               more data is needed in the next round to detect the frame
631  *               reliably. This may happen when a frame header candidate
632  *               is found but it cannot be guaranteed to be the header without
633  *               peeking the following data.
634  *
635  * Check if the given data contains contains LOAS frame. The algorithm
636  * will examine LOAS frame header and calculate the frame size. Also, another
637  * consecutive LOAS frame header need to be present after the found frame.
638  * Otherwise the data is not considered as a valid LOAS frame. However, this
639  * "extra check" is omitted when EOS has been received. In this case it is
640  * enough when data[0] contains a valid LOAS header.
641  *
642  * This function may set the #needed_data to indicate that a possible frame
643  * candidate has been found, but more data (#needed_data bytes) is needed to
644  * be absolutely sure. When this situation occurs, FALSE will be returned.
645  *
646  * When a valid frame is detected, this function will use
647  * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
648  * to set the needed bytes for next frame.This way next data chunk is already
649  * of correct size.
650  *
651  * LOAS can have three different formats, if I read the spec correctly. Only
652  * one of them is supported here, as the two samples I have use this one.
653  *
654  * Returns: TRUE if the given data contains a valid LOAS header.
655  */
656 static gboolean
657 gst_aac_parse_check_loas_frame (GstAacParse * aacparse,
658     const guint8 * data, const guint avail, gboolean drain,
659     guint * framesize, guint * needed_data)
660 {
661   *needed_data = 0;
662
663   /* 3 byte header */
664   if (G_UNLIKELY (avail < 3))
665     return FALSE;
666
667   if ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) {
668     *framesize = gst_aac_parse_loas_get_frame_len (data);
669     GST_DEBUG_OBJECT (aacparse, "Found %u byte LOAS frame", *framesize);
670
671     /* In EOS mode this is enough. No need to examine the data further.
672        We also relax the check when we have sync, on the assumption that
673        if we're not looking at random data, we have a much higher chance
674        to get the correct sync, and this avoids losing two frames when
675        a single bit corruption happens. */
676     if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
677       return TRUE;
678     }
679
680     if (*framesize + LOAS_MAX_SIZE > avail) {
681       /* We have found a possible frame header candidate, but can't be
682          sure since we don't have enough data to check the next frame */
683       GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
684           *framesize + LOAS_MAX_SIZE, avail);
685       *needed_data = *framesize + LOAS_MAX_SIZE;
686       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
687           *framesize + LOAS_MAX_SIZE);
688       return FALSE;
689     }
690
691     if ((data[*framesize] == 0x56) && ((data[*framesize + 1] & 0xe0) == 0xe0)) {
692       guint nextlen = gst_aac_parse_loas_get_frame_len (data + (*framesize));
693
694       GST_LOG ("LOAS frame found, len: %d bytes", *framesize);
695       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
696           nextlen + LOAS_MAX_SIZE);
697       return TRUE;
698     }
699   }
700   return FALSE;
701 }
702
703 /* caller ensure sufficient data */
704 static inline void
705 gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data,
706     gint * rate, gint * channels, gint * object, gint * version)
707 {
708
709   if (rate) {
710     gint sr_idx = (data[2] & 0x3c) >> 2;
711
712     *rate = gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
713   }
714   if (channels)
715     *channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6);
716
717   if (version)
718     *version = (data[1] & 0x08) ? 2 : 4;
719   if (object)
720     *object = ((data[2] & 0xc0) >> 6) + 1;
721 }
722
723 /**
724  * gst_aac_parse_detect_stream:
725  * @aacparse: #GstAacParse.
726  * @data: A block of data that needs to be examined for stream characteristics.
727  * @avail: Size of the given datablock.
728  * @framesize: If valid stream was found, this will be set to tell the
729  *             first frame size in bytes.
730  * @skipsize: If valid stream was found, this will be set to tell the first
731  *            audio frame position within the given data.
732  *
733  * Examines the given piece of data and try to detect the format of it. It
734  * checks for "ADIF" header (in the beginning of the clip) and ADTS frame
735  * header. If the stream is detected, TRUE will be returned and #framesize
736  * is set to indicate the found frame size. Additionally, #skipsize might
737  * be set to indicate the number of bytes that need to be skipped, a.k.a. the
738  * position of the frame inside given data chunk.
739  *
740  * Returns: TRUE on success.
741  */
742 static gboolean
743 gst_aac_parse_detect_stream (GstAacParse * aacparse,
744     const guint8 * data, const guint avail, gboolean drain,
745     guint * framesize, gint * skipsize)
746 {
747   gboolean found = FALSE;
748   guint need_data_adts = 0, need_data_loas;
749   guint i = 0;
750
751   GST_DEBUG_OBJECT (aacparse, "Parsing header data");
752
753   /* FIXME: No need to check for ADIF if we are not in the beginning of the
754      stream */
755
756   /* Can we even parse the header? */
757   if (avail < MAX (ADTS_MAX_SIZE, LOAS_MAX_SIZE)) {
758     GST_DEBUG_OBJECT (aacparse, "Not enough data to check");
759     return FALSE;
760   }
761
762   for (i = 0; i < avail - 4; i++) {
763     if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) ||
764         ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) ||
765         strncmp ((char *) data + i, "ADIF", 4) == 0) {
766       GST_DEBUG_OBJECT (aacparse, "Found signature at offset %u", i);
767       found = TRUE;
768
769       if (i) {
770         /* Trick: tell the parent class that we didn't find the frame yet,
771            but make it skip 'i' amount of bytes. Next time we arrive
772            here we have full frame in the beginning of the data. */
773         *skipsize = i;
774         return FALSE;
775       }
776       break;
777     }
778   }
779   if (!found) {
780     if (i)
781       *skipsize = i;
782     return FALSE;
783   }
784
785   if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain,
786           framesize, &need_data_adts)) {
787     gint rate, channels;
788
789     GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize);
790
791     gst_aac_parse_parse_adts_header (aacparse, data, &rate, &channels,
792         &aacparse->object_type, &aacparse->mpegversion);
793
794     if (!channels || !framesize) {
795       GST_DEBUG_OBJECT (aacparse, "impossible ADTS configuration");
796       return FALSE;
797     }
798
799     aacparse->header_type = DSPAAC_HEADER_ADTS;
800     gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
801         aacparse->frame_samples, 2, 2);
802
803     GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d",
804         rate, channels, aacparse->object_type, aacparse->mpegversion);
805
806     gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
807
808     return TRUE;
809   }
810
811   if (gst_aac_parse_check_loas_frame (aacparse, data, avail, drain,
812           framesize, &need_data_loas)) {
813     gint rate, channels;
814
815     GST_INFO ("LOAS, framesize: %d", *framesize);
816
817     aacparse->header_type = DSPAAC_HEADER_LOAS;
818
819     if (!gst_aac_parse_read_loas_config (aacparse, data, avail, &rate,
820             &channels, &aacparse->mpegversion)) {
821       GST_WARNING_OBJECT (aacparse, "Error reading LOAS config");
822       return FALSE;
823     }
824
825     if (rate && channels) {
826       gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
827           aacparse->frame_samples, 2, 2);
828
829       GST_DEBUG ("LOAS: samplerate %d, channels %d, objtype %d, version %d",
830           rate, channels, aacparse->object_type, aacparse->mpegversion);
831       aacparse->sample_rate = rate;
832       aacparse->channels = channels;
833     }
834
835     gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
836
837     return TRUE;
838   }
839
840   if (need_data_adts || need_data_loas) {
841     /* This tells the parent class not to skip any data */
842     *skipsize = 0;
843     return FALSE;
844   }
845
846   if (avail < ADIF_MAX_SIZE)
847     return FALSE;
848
849   if (memcmp (data + i, "ADIF", 4) == 0) {
850     const guint8 *adif;
851     int skip_size = 0;
852     int bitstream_type;
853     int sr_idx;
854     GstCaps *sinkcaps;
855
856     aacparse->header_type = DSPAAC_HEADER_ADIF;
857     aacparse->mpegversion = 4;
858
859     /* Skip the "ADIF" bytes */
860     adif = data + i + 4;
861
862     /* copyright string */
863     if (adif[0] & 0x80)
864       skip_size += 9;           /* skip 9 bytes */
865
866     bitstream_type = adif[0 + skip_size] & 0x10;
867     aacparse->bitrate =
868         ((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) |
869         ((unsigned int) adif[1 + skip_size] << 11) |
870         ((unsigned int) adif[2 + skip_size] << 3) |
871         ((unsigned int) adif[3 + skip_size] & 0xe0);
872
873     /* CBR */
874     if (bitstream_type == 0) {
875 #if 0
876       /* Buffer fullness parsing. Currently not needed... */
877       guint num_elems = 0;
878       guint fullness = 0;
879
880       num_elems = (adif[3 + skip_size] & 0x1e);
881       GST_INFO ("ADIF num_config_elems: %d", num_elems);
882
883       fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) |
884           ((unsigned int) adif[4 + skip_size] << 11) |
885           ((unsigned int) adif[5 + skip_size] << 3) |
886           ((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5);
887
888       GST_INFO ("ADIF buffer fullness: %d", fullness);
889 #endif
890       aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) |
891           ((adif[7 + skip_size] & 0x80) >> 7);
892       sr_idx = (adif[7 + skip_size] & 0x78) >> 3;
893     }
894     /* VBR */
895     else {
896       aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3;
897       sr_idx = ((adif[4 + skip_size] & 0x07) << 1) |
898           ((adif[5 + skip_size] & 0x80) >> 7);
899     }
900
901     /* FIXME: This gives totally wrong results. Duration calculation cannot
902        be based on this */
903     aacparse->sample_rate =
904         gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
905
906     /* baseparse is not given any fps,
907      * so it will give up on timestamps, seeking, etc */
908
909     /* FIXME: Can we assume this? */
910     aacparse->channels = 2;
911
912     GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d",
913         aacparse->bitrate, aacparse->sample_rate, aacparse->object_type);
914
915     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512);
916
917     /* arrange for metadata and get out of the way */
918     sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (aacparse));
919     gst_aac_parse_set_src_caps (aacparse, sinkcaps);
920     if (sinkcaps)
921       gst_caps_unref (sinkcaps);
922
923     /* not syncable, not easily seekable (unless we push data from start */
924     gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE);
925     gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE);
926     gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0);
927
928     *framesize = avail;
929     return TRUE;
930   }
931
932   /* This should never happen */
933   return FALSE;
934 }
935
936
937 /**
938  * gst_aac_parse_check_valid_frame:
939  * @parse: #GstBaseParse.
940  * @frame: #GstBaseParseFrame.
941  * @skipsize: How much data parent class should skip in order to find the
942  *            frame header.
943  *
944  * Implementation of "handle_frame" vmethod in #GstBaseParse class.
945  *
946  * Also determines frame overhead.
947  * ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have
948  * a per-frame header. LOAS has 3 bytes.
949  *
950  * We're making a couple of simplifying assumptions:
951  *
952  * 1. We count Program Configuration Elements rather than searching for them
953  *    in the streams to discount them - the overhead is negligible.
954  *
955  * 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16
956  *    bits, which should still not be significant enough to warrant the
957  *    additional parsing through the headers
958  *
959  * Returns: a #GstFlowReturn.
960  */
961 static GstFlowReturn
962 gst_aac_parse_handle_frame (GstBaseParse * parse,
963     GstBaseParseFrame * frame, gint * skipsize)
964 {
965   GstMapInfo map;
966   GstAacParse *aacparse;
967   gboolean ret = FALSE;
968   gboolean lost_sync;
969   GstBuffer *buffer;
970   guint framesize;
971   gint rate, channels;
972
973   aacparse = GST_AAC_PARSE (parse);
974   buffer = frame->buffer;
975
976   gst_buffer_map (buffer, &map, GST_MAP_READ);
977
978   *skipsize = -1;
979   lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
980
981   if (aacparse->header_type == DSPAAC_HEADER_ADIF ||
982       aacparse->header_type == DSPAAC_HEADER_NONE) {
983     /* There is nothing to parse */
984     framesize = map.size;
985     ret = TRUE;
986
987   } else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) {
988
989     ret = gst_aac_parse_detect_stream (aacparse, map.data, map.size,
990         GST_BASE_PARSE_DRAINING (parse), &framesize, skipsize);
991
992   } else if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
993     guint needed_data = 1024;
994
995     ret = gst_aac_parse_check_adts_frame (aacparse, map.data, map.size,
996         GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
997
998     if (!ret) {
999       GST_DEBUG ("buffer didn't contain valid frame");
1000       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1001           needed_data);
1002     }
1003
1004   } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1005     guint needed_data = 1024;
1006
1007     ret = gst_aac_parse_check_loas_frame (aacparse, map.data,
1008         map.size, GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
1009
1010     if (!ret) {
1011       GST_DEBUG ("buffer didn't contain valid frame");
1012       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1013           needed_data);
1014     }
1015
1016   } else {
1017     GST_DEBUG ("buffer didn't contain valid frame");
1018     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1019         ADTS_MAX_SIZE);
1020   }
1021
1022   if (G_UNLIKELY (!ret))
1023     goto exit;
1024
1025   if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1026     /* see above */
1027     frame->overhead = 7;
1028
1029     gst_aac_parse_parse_adts_header (aacparse, map.data,
1030         &rate, &channels, NULL, NULL);
1031
1032     GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels);
1033
1034     if (G_UNLIKELY (rate != aacparse->sample_rate
1035             || channels != aacparse->channels)) {
1036       aacparse->sample_rate = rate;
1037       aacparse->channels = channels;
1038
1039       if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1040         /* If linking fails, we need to return appropriate error */
1041         ret = GST_FLOW_NOT_LINKED;
1042       }
1043
1044       gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1045           aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1046     }
1047   } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1048     gboolean setcaps = FALSE;
1049
1050     /* see above */
1051     frame->overhead = 3;
1052
1053     if (!gst_aac_parse_read_loas_config (aacparse, map.data, map.size, &rate,
1054             &channels, NULL)) {
1055       GST_WARNING_OBJECT (aacparse, "Error reading LOAS config");
1056     } else if (G_UNLIKELY (rate != aacparse->sample_rate
1057             || channels != aacparse->channels)) {
1058       aacparse->sample_rate = rate;
1059       aacparse->channels = channels;
1060       setcaps = TRUE;
1061       GST_INFO_OBJECT (aacparse, "New LOAS config: %d Hz, %d channels", rate,
1062           channels);
1063     }
1064
1065     /* We want to set caps both at start, and when rate/channels change.
1066        Since only some LOAS frames have that info, we may receive frames
1067        before knowing about rate/channels. */
1068     if (setcaps
1069         || !gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse))) {
1070       if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1071         /* If linking fails, we need to return appropriate error */
1072         ret = GST_FLOW_NOT_LINKED;
1073       }
1074
1075       gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1076           aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1077     }
1078   }
1079
1080 exit:
1081   gst_buffer_unmap (buffer, &map);
1082
1083   if (ret) {
1084     /* found, skip if needed */
1085     if (*skipsize > 0)
1086       return GST_FLOW_OK;
1087     *skipsize = 0;
1088   } else {
1089     if (*skipsize < 0)
1090       *skipsize = 1;
1091   }
1092
1093   if (ret && framesize <= map.size) {
1094     return gst_base_parse_finish_frame (parse, frame, framesize);
1095   }
1096
1097   return GST_FLOW_OK;
1098 }
1099
1100 static GstFlowReturn
1101 gst_aac_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
1102 {
1103   GstAacParse *aacparse = GST_AAC_PARSE (parse);
1104
1105   /* As a special case, we can remove the ADTS framing and output raw AAC. */
1106   if (aacparse->header_type == DSPAAC_HEADER_ADTS
1107       && aacparse->output_header_type == DSPAAC_HEADER_NONE) {
1108     guint header_size;
1109     GstMapInfo map;
1110     gst_buffer_map (frame->buffer, &map, GST_MAP_READ);
1111     header_size = (map.data[1] & 1) ? 7 : 9;    /* optional CRC */
1112     gst_buffer_unmap (frame->buffer, &map);
1113     gst_buffer_resize (frame->buffer, header_size,
1114         gst_buffer_get_size (frame->buffer) - header_size);
1115   }
1116
1117   return GST_FLOW_OK;
1118 }
1119
1120
1121 /**
1122  * gst_aac_parse_start:
1123  * @parse: #GstBaseParse.
1124  *
1125  * Implementation of "start" vmethod in #GstBaseParse class.
1126  *
1127  * Returns: TRUE if startup succeeded.
1128  */
1129 static gboolean
1130 gst_aac_parse_start (GstBaseParse * parse)
1131 {
1132   GstAacParse *aacparse;
1133
1134   aacparse = GST_AAC_PARSE (parse);
1135   GST_DEBUG ("start");
1136   aacparse->frame_samples = 1024;
1137   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), ADTS_MAX_SIZE);
1138   return TRUE;
1139 }
1140
1141
1142 /**
1143  * gst_aac_parse_stop:
1144  * @parse: #GstBaseParse.
1145  *
1146  * Implementation of "stop" vmethod in #GstBaseParse class.
1147  *
1148  * Returns: TRUE is stopping succeeded.
1149  */
1150 static gboolean
1151 gst_aac_parse_stop (GstBaseParse * parse)
1152 {
1153   GST_DEBUG ("stop");
1154   return TRUE;
1155 }
1156
1157 static GstCaps *
1158 gst_aac_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter)
1159 {
1160   GstCaps *peercaps, *templ;
1161   GstCaps *res;
1162
1163   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1164   peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), filter);
1165   if (peercaps) {
1166     guint i, n;
1167
1168     /* Remove the framed field */
1169     peercaps = gst_caps_make_writable (peercaps);
1170     n = gst_caps_get_size (peercaps);
1171     for (i = 0; i < n; i++) {
1172       GstStructure *s = gst_caps_get_structure (peercaps, i);
1173
1174       gst_structure_remove_field (s, "framed");
1175     }
1176
1177     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1178     gst_caps_unref (peercaps);
1179     res = gst_caps_make_writable (res);
1180
1181     /* Append the template caps because we still want to accept
1182      * caps without any fields in the case upstream does not
1183      * know anything.
1184      */
1185     gst_caps_append (res, templ);
1186   } else {
1187     res = templ;
1188   }
1189
1190   if (filter) {
1191     GstCaps *intersection;
1192
1193     intersection =
1194         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1195     gst_caps_unref (res);
1196     res = intersection;
1197   }
1198
1199   return res;
1200 }