e1f882d968e76c1dbd3f89a8fe3cb292e2bb8f0e
[platform/upstream/gst-plugins-good.git] / gst / audioparsers / gstaacparse.c
1 /* GStreamer AAC parser plugin
2  * Copyright (C) 2008 Nokia Corporation. All rights reserved.
3  *
4  * Contact: Stefan Kost <stefan.kost@nokia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:element-aacparse
24  * @short_description: AAC parser
25  * @see_also: #GstAmrParse
26  *
27  * This is an AAC parser which handles both ADIF and ADTS stream formats.
28  *
29  * As ADIF format is not framed, it is not seekable and stream duration cannot
30  * be determined either. However, ADTS format AAC clips can be seeked, and parser
31  * can also estimate playback position and clip duration.
32  *
33  * <refsect2>
34  * <title>Example launch line</title>
35  * |[
36  * gst-launch-1.0 filesrc location=abc.aac ! aacparse ! faad ! audioresample ! audioconvert ! alsasink
37  * ]|
38  * </refsect2>
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <string.h>
46
47 #include <gst/base/gstbitreader.h>
48 #include <gst/pbutils/pbutils.h>
49 #include "gstaacparse.h"
50
51
52 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
53     GST_PAD_SRC,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("audio/mpeg, "
56         "framed = (boolean) true, " "mpegversion = (int) { 2, 4 }, "
57         "stream-format = (string) { raw, adts, adif, loas };"));
58
59 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
60     GST_PAD_SINK,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) { 2, 4 };"));
63
64 GST_DEBUG_CATEGORY_STATIC (aacparse_debug);
65 #define GST_CAT_DEFAULT aacparse_debug
66
67
68 #define ADIF_MAX_SIZE 40        /* Should be enough */
69 #define ADTS_MAX_SIZE 10        /* Should be enough */
70 #define LOAS_MAX_SIZE 3         /* Should be enough */
71
72 #define ADTS_HEADERS_LENGTH 7UL /* Total byte-length of fixed and variable
73                                    headers prepended during raw to ADTS
74                                    conversion */
75
76 #ifdef TIZEN_FEATURE_AACPARSE_MODIFICATION      /* to get more accurate duration */
77 #define AAC_MAX_ESTIMATE_DURATION_BUF (1024 * 1024)     /* use first 1 Mbyte */
78 #define AAC_SAMPLE_PER_FRAME 1024
79
80 #define AAC_MAX_PULL_RANGE_BUF  (1 * 1024 * 1024)       /* 1 MByte */
81 #define AAC_LARGE_FILE_SIZE     (2 * 1024 * 1024)       /* 2 MByte */
82 #define gst_aac_parse_parent_class parent_class
83 #endif
84
85 #define AAC_FRAME_DURATION(parse) (GST_SECOND/parse->frames_per_sec)
86
87 static const gint loas_sample_rate_table[16] = {
88   96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
89   16000, 12000, 11025, 8000, 7350, 0, 0, 0
90 };
91
92 static const gint loas_channels_table[16] = {
93   0, 1, 2, 3, 4, 5, 6, 8,
94   0, 0, 0, 7, 8, 0, 8, 0
95 };
96
97 static gboolean gst_aac_parse_start (GstBaseParse * parse);
98 static gboolean gst_aac_parse_stop (GstBaseParse * parse);
99
100 static gboolean gst_aac_parse_sink_setcaps (GstBaseParse * parse,
101     GstCaps * caps);
102 static GstCaps *gst_aac_parse_sink_getcaps (GstBaseParse * parse,
103     GstCaps * filter);
104
105 static GstFlowReturn gst_aac_parse_handle_frame (GstBaseParse * parse,
106     GstBaseParseFrame * frame, gint * skipsize);
107 static GstFlowReturn gst_aac_parse_pre_push_frame (GstBaseParse * parse,
108     GstBaseParseFrame * frame);
109 static gboolean gst_aac_parse_src_event (GstBaseParse * parse,
110     GstEvent * event);
111
112 static gboolean gst_aac_parse_read_audio_specific_config (GstAacParse *
113     aacparse, GstBitReader * br, gint * object_type, gint * sample_rate,
114     gint * channels, gint * frame_samples);
115
116 #ifdef TIZEN_FEATURE_AACPARSE_MODIFICATION
117 static guint gst_aac_parse_adts_get_fast_frame_len (const guint8 * data);
118 static gboolean gst_aac_parse_src_eventfunc (GstBaseParse * parse,
119     GstEvent * event);
120 /* make full aac(adts) index table when seek */
121 static gboolean gst_aac_parse_adts_src_eventfunc (GstBaseParse * parse,
122     GstEvent * event);
123 int get_aac_parse_get_adts_frame_length (const unsigned char *data,
124     gint64 offset);
125 static gboolean gst_aac_parse_estimate_duration (GstBaseParse * parse);
126 #endif
127
128 #define gst_aac_parse_parent_class parent_class
129 G_DEFINE_TYPE (GstAacParse, gst_aac_parse, GST_TYPE_BASE_PARSE);
130
131 #ifdef TIZEN_FEATURE_AACPARSE_MODIFICATION
132 static inline gint
133 gst_aac_parse_get_sample_rate_from_index (guint sr_idx)
134 {
135   static const guint aac_sample_rates[] = { 96000, 88200, 64000, 48000, 44100,
136     32000, 24000, 22050, 16000, 12000, 11025, 8000
137   };
138
139   if (sr_idx < G_N_ELEMENTS (aac_sample_rates))
140     return aac_sample_rates[sr_idx];
141   GST_WARNING ("Invalid sample rate index %u", sr_idx);
142   return 0;
143 }
144 #endif
145 /**
146  * gst_aac_parse_class_init:
147  * @klass: #GstAacParseClass.
148  *
149  */
150 static void
151 gst_aac_parse_class_init (GstAacParseClass * klass)
152 {
153   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
154   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
155
156   GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0,
157       "AAC audio stream parser");
158
159   gst_element_class_add_static_pad_template (element_class, &sink_template);
160   gst_element_class_add_static_pad_template (element_class, &src_template);
161
162   gst_element_class_set_static_metadata (element_class,
163       "AAC audio stream parser", "Codec/Parser/Audio",
164       "Advanced Audio Coding parser", "Stefan Kost <stefan.kost@nokia.com>");
165
166   parse_class->start = GST_DEBUG_FUNCPTR (gst_aac_parse_start);
167   parse_class->stop = GST_DEBUG_FUNCPTR (gst_aac_parse_stop);
168   parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_setcaps);
169   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_getcaps);
170   parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_handle_frame);
171   parse_class->pre_push_frame =
172       GST_DEBUG_FUNCPTR (gst_aac_parse_pre_push_frame);
173   parse_class->src_event = GST_DEBUG_FUNCPTR (gst_aac_parse_src_event);
174 }
175
176
177 /**
178  * gst_aac_parse_init:
179  * @aacparse: #GstAacParse.
180  * @klass: #GstAacParseClass.
181  *
182  */
183 static void
184 gst_aac_parse_init (GstAacParse * aacparse)
185 {
186   GST_DEBUG ("initialized");
187   GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (aacparse));
188   GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (aacparse));
189
190   aacparse->last_parsed_sample_rate = 0;
191   aacparse->last_parsed_channels = 0;
192 #ifdef TIZEN_FEATURE_AACPARSE_MODIFICATION
193   /* to get more correct duration */
194   aacparse->first_frame = TRUE;
195 #endif
196 }
197
198
199 /**
200  * gst_aac_parse_set_src_caps:
201  * @aacparse: #GstAacParse.
202  * @sink_caps: (proposed) caps of sink pad
203  *
204  * Set source pad caps according to current knowledge about the
205  * audio stream.
206  *
207  * Returns: TRUE if caps were successfully set.
208  */
209 static gboolean
210 gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps)
211 {
212   GstStructure *s;
213   GstCaps *src_caps = NULL, *allowed;
214   gboolean res = FALSE;
215   const gchar *stream_format;
216   guint8 codec_data[2];
217   guint16 codec_data_data;
218   gint sample_rate_idx;
219
220   GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps);
221   if (sink_caps)
222     src_caps = gst_caps_copy (sink_caps);
223   else
224     src_caps = gst_caps_new_empty_simple ("audio/mpeg");
225
226   gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE,
227       "mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL);
228
229   aacparse->output_header_type = aacparse->header_type;
230   switch (aacparse->header_type) {
231     case DSPAAC_HEADER_NONE:
232       stream_format = "raw";
233       break;
234     case DSPAAC_HEADER_ADTS:
235       stream_format = "adts";
236       break;
237     case DSPAAC_HEADER_ADIF:
238       stream_format = "adif";
239       break;
240     case DSPAAC_HEADER_LOAS:
241       stream_format = "loas";
242       break;
243     default:
244       stream_format = NULL;
245   }
246
247   /* Generate codec data to be able to set profile/level on the caps */
248   sample_rate_idx =
249       gst_codec_utils_aac_get_index_from_sample_rate (aacparse->sample_rate);
250   if (sample_rate_idx < 0)
251     goto not_a_known_rate;
252   codec_data_data =
253       (aacparse->object_type << 11) |
254       (sample_rate_idx << 7) | (aacparse->channels << 3);
255   GST_WRITE_UINT16_BE (codec_data, codec_data_data);
256   gst_codec_utils_aac_caps_set_level_and_profile (src_caps, codec_data, 2);
257
258   s = gst_caps_get_structure (src_caps, 0);
259   if (aacparse->sample_rate > 0)
260     gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL);
261   if (aacparse->channels > 0)
262     gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL);
263   if (stream_format)
264     gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL);
265
266   allowed = gst_pad_get_allowed_caps (GST_BASE_PARSE (aacparse)->srcpad);
267   if (allowed && !gst_caps_can_intersect (src_caps, allowed)) {
268     GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
269         "Caps can not intersect");
270     if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
271       GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
272           "Input is ADTS, trying raw");
273       gst_caps_set_simple (src_caps, "stream-format", G_TYPE_STRING, "raw",
274           NULL);
275       if (gst_caps_can_intersect (src_caps, allowed)) {
276         GstBuffer *codec_data_buffer;
277
278         GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
279             "Caps can intersect, we will drop the ADTS layer");
280         aacparse->output_header_type = DSPAAC_HEADER_NONE;
281
282         /* The codec_data data is according to AudioSpecificConfig,
283            ISO/IEC 14496-3, 1.6.2.1 */
284         codec_data_buffer = gst_buffer_new_and_alloc (2);
285         gst_buffer_fill (codec_data_buffer, 0, codec_data, 2);
286         gst_caps_set_simple (src_caps, "codec_data", GST_TYPE_BUFFER,
287             codec_data_buffer, NULL);
288       }
289     } else if (aacparse->header_type == DSPAAC_HEADER_NONE) {
290       GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
291           "Input is raw, trying ADTS");
292       gst_caps_set_simple (src_caps, "stream-format", G_TYPE_STRING, "adts",
293           NULL);
294       if (gst_caps_can_intersect (src_caps, allowed)) {
295         GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
296             "Caps can intersect, we will prepend ADTS headers");
297         aacparse->output_header_type = DSPAAC_HEADER_ADTS;
298       }
299     }
300   }
301   if (allowed)
302     gst_caps_unref (allowed);
303
304   aacparse->last_parsed_channels = 0;
305   aacparse->last_parsed_sample_rate = 0;
306
307   GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps);
308
309   res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps);
310   gst_caps_unref (src_caps);
311   return res;
312
313 not_a_known_rate:
314   GST_ERROR_OBJECT (aacparse, "Not a known sample rate: %d",
315       aacparse->sample_rate);
316   gst_caps_unref (src_caps);
317   return FALSE;
318 }
319
320
321 /**
322  * gst_aac_parse_sink_setcaps:
323  * @sinkpad: GstPad
324  * @caps: GstCaps
325  *
326  * Implementation of "set_sink_caps" vmethod in #GstBaseParse class.
327  *
328  * Returns: TRUE on success.
329  */
330 static gboolean
331 gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
332 {
333   GstAacParse *aacparse;
334   GstStructure *structure;
335   gchar *caps_str;
336   const GValue *value;
337
338   aacparse = GST_AAC_PARSE (parse);
339   structure = gst_caps_get_structure (caps, 0);
340   caps_str = gst_caps_to_string (caps);
341
342   GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str);
343   g_free (caps_str);
344
345   /* This is needed at least in case of RTP
346    * Parses the codec_data information to get ObjectType,
347    * number of channels and samplerate */
348   value = gst_structure_get_value (structure, "codec_data");
349   if (value) {
350     GstBuffer *buf = gst_value_get_buffer (value);
351
352     if (buf && gst_buffer_get_size (buf) >= 2) {
353       GstMapInfo map;
354       GstBitReader br;
355
356       if (!gst_buffer_map (buf, &map, GST_MAP_READ))
357         return FALSE;
358       gst_bit_reader_init (&br, map.data, map.size);
359       gst_aac_parse_read_audio_specific_config (aacparse, &br,
360           &aacparse->object_type, &aacparse->sample_rate, &aacparse->channels,
361           &aacparse->frame_samples);
362
363       aacparse->header_type = DSPAAC_HEADER_NONE;
364       aacparse->mpegversion = 4;
365       gst_buffer_unmap (buf, &map);
366
367       GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d, "
368           "samples=%d", aacparse->object_type, aacparse->sample_rate,
369           aacparse->channels, aacparse->frame_samples);
370
371       /* arrange for metadata and get out of the way */
372       gst_aac_parse_set_src_caps (aacparse, caps);
373       if (aacparse->header_type == aacparse->output_header_type)
374         gst_base_parse_set_passthrough (parse, TRUE);
375     } else {
376       return FALSE;
377     }
378
379     /* caps info overrides */
380     gst_structure_get_int (structure, "rate", &aacparse->sample_rate);
381     gst_structure_get_int (structure, "channels", &aacparse->channels);
382   } else {
383     const gchar *stream_format =
384         gst_structure_get_string (structure, "stream-format");
385
386     if (g_strcmp0 (stream_format, "raw") == 0) {
387       GST_ERROR_OBJECT (parse, "Need codec_data for raw AAC");
388       return FALSE;
389     } else {
390       aacparse->sample_rate = 0;
391       aacparse->channels = 0;
392       aacparse->header_type = DSPAAC_HEADER_NOT_PARSED;
393       gst_base_parse_set_passthrough (parse, FALSE);
394     }
395   }
396   return TRUE;
397 }
398
399
400 /**
401  * gst_aac_parse_adts_get_frame_len:
402  * @data: block of data containing an ADTS header.
403  *
404  * This function calculates ADTS frame length from the given header.
405  *
406  * Returns: size of the ADTS frame.
407  */
408 static inline guint
409 gst_aac_parse_adts_get_frame_len (const guint8 * data)
410 {
411   return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5);
412 }
413
414
415 /**
416  * gst_aac_parse_check_adts_frame:
417  * @aacparse: #GstAacParse.
418  * @data: Data to be checked.
419  * @avail: Amount of data passed.
420  * @framesize: If valid ADTS frame was found, this will be set to tell the
421  *             found frame size in bytes.
422  * @needed_data: If frame was not found, this may be set to tell how much
423  *               more data is needed in the next round to detect the frame
424  *               reliably. This may happen when a frame header candidate
425  *               is found but it cannot be guaranteed to be the header without
426  *               peeking the following data.
427  *
428  * Check if the given data contains contains ADTS frame. The algorithm
429  * will examine ADTS frame header and calculate the frame size. Also, another
430  * consecutive ADTS frame header need to be present after the found frame.
431  * Otherwise the data is not considered as a valid ADTS frame. However, this
432  * "extra check" is omitted when EOS has been received. In this case it is
433  * enough when data[0] contains a valid ADTS header.
434  *
435  * This function may set the #needed_data to indicate that a possible frame
436  * candidate has been found, but more data (#needed_data bytes) is needed to
437  * be absolutely sure. When this situation occurs, FALSE will be returned.
438  *
439  * When a valid frame is detected, this function will use
440  * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
441  * to set the needed bytes for next frame.This way next data chunk is already
442  * of correct size.
443  *
444  * Returns: TRUE if the given data contains a valid ADTS header.
445  */
446 static gboolean
447 gst_aac_parse_check_adts_frame (GstAacParse * aacparse,
448     const guint8 * data, const guint avail, gboolean drain,
449     guint * framesize, guint * needed_data)
450 {
451   guint crc_size;
452
453   *needed_data = 0;
454
455   /* Absolute minimum to perform the ADTS syncword,
456      layer and sampling frequency tests */
457   if (G_UNLIKELY (avail < 3)) {
458     *needed_data = 3;
459     return FALSE;
460   }
461
462   /* Syncword and layer tests */
463   if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) {
464
465     /* Sampling frequency test */
466     if (G_UNLIKELY ((data[2] & 0x3C) >> 2 == 15))
467       return FALSE;
468
469     /* This looks like an ADTS frame header but
470        we need at least 6 bytes to proceed */
471     if (G_UNLIKELY (avail < 6)) {
472       *needed_data = 6;
473       return FALSE;
474     }
475
476     *framesize = gst_aac_parse_adts_get_frame_len (data);
477
478     /* If frame has CRC, it needs 2 bytes
479        for it at the end of the header */
480     crc_size = (data[1] & 0x01) ? 0 : 2;
481
482     /* CRC size test */
483     if (*framesize < 7 + crc_size) {
484       *needed_data = 7 + crc_size;
485       return FALSE;
486     }
487
488     /* In EOS mode this is enough. No need to examine the data further.
489        We also relax the check when we have sync, on the assumption that
490        if we're not looking at random data, we have a much higher chance
491        to get the correct sync, and this avoids losing two frames when
492        a single bit corruption happens. */
493     if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
494       return TRUE;
495     }
496
497     if (*framesize + ADTS_MAX_SIZE > avail) {
498       /* We have found a possible frame header candidate, but can't be
499          sure since we don't have enough data to check the next frame */
500       GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
501           *framesize + ADTS_MAX_SIZE, avail);
502       *needed_data = *framesize + ADTS_MAX_SIZE;
503       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
504           *framesize + ADTS_MAX_SIZE);
505       return FALSE;
506     }
507
508     if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) {
509       guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize));
510
511       GST_LOG ("ADTS frame found, len: %d bytes", *framesize);
512       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
513           nextlen + ADTS_MAX_SIZE);
514       return TRUE;
515     }
516   }
517   return FALSE;
518 }
519
520 static gboolean
521 gst_aac_parse_latm_get_value (GstAacParse * aacparse, GstBitReader * br,
522     guint32 * value)
523 {
524   guint8 bytes, i, byte;
525
526   *value = 0;
527   if (!gst_bit_reader_get_bits_uint8 (br, &bytes, 2))
528     return FALSE;
529   for (i = 0; i <= bytes; ++i) {
530     *value <<= 8;
531     if (!gst_bit_reader_get_bits_uint8 (br, &byte, 8))
532       return FALSE;
533     *value += byte;
534   }
535   return TRUE;
536 }
537
538 static gboolean
539 gst_aac_parse_get_audio_object_type (GstAacParse * aacparse, GstBitReader * br,
540     guint8 * audio_object_type)
541 {
542   if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 5))
543     return FALSE;
544   if (*audio_object_type == 31) {
545     if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 6))
546       return FALSE;
547     *audio_object_type += 32;
548   }
549   GST_LOG_OBJECT (aacparse, "audio object type %u", *audio_object_type);
550   return TRUE;
551 }
552
553 static gboolean
554 gst_aac_parse_get_audio_sample_rate (GstAacParse * aacparse, GstBitReader * br,
555     gint * sample_rate)
556 {
557   guint8 sampling_frequency_index;
558   if (!gst_bit_reader_get_bits_uint8 (br, &sampling_frequency_index, 4))
559     return FALSE;
560   GST_LOG_OBJECT (aacparse, "sampling_frequency_index: %u",
561       sampling_frequency_index);
562   if (sampling_frequency_index == 0xf) {
563     guint32 sampling_rate;
564     if (!gst_bit_reader_get_bits_uint32 (br, &sampling_rate, 24))
565       return FALSE;
566     *sample_rate = sampling_rate;
567   } else {
568     *sample_rate = loas_sample_rate_table[sampling_frequency_index];
569     if (!*sample_rate)
570       return FALSE;
571   }
572   aacparse->last_parsed_sample_rate = *sample_rate;
573   return TRUE;
574 }
575
576 /* See table 1.13 in ISO/IEC 14496-3 */
577 static gboolean
578 gst_aac_parse_read_audio_specific_config (GstAacParse * aacparse,
579     GstBitReader * br, gint * object_type, gint * sample_rate, gint * channels,
580     gint * frame_samples)
581 {
582   guint8 audio_object_type;
583   guint8 G_GNUC_UNUSED extension_audio_object_type;
584   guint8 channel_configuration, extension_channel_configuration;
585   gboolean G_GNUC_UNUSED sbr = FALSE, ps = FALSE;
586
587   if (!gst_aac_parse_get_audio_object_type (aacparse, br, &audio_object_type))
588     return FALSE;
589   if (object_type)
590     *object_type = audio_object_type;
591
592   if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
593     return FALSE;
594
595   if (!gst_bit_reader_get_bits_uint8 (br, &channel_configuration, 4))
596     return FALSE;
597   *channels = loas_channels_table[channel_configuration];
598   GST_LOG_OBJECT (aacparse, "channel_configuration: %d", channel_configuration);
599   if (!*channels)
600     return FALSE;
601
602   if (audio_object_type == 5 || audio_object_type == 29) {
603     extension_audio_object_type = 5;
604     sbr = TRUE;
605     if (audio_object_type == 29) {
606       ps = TRUE;
607       /* Parametric stereo. If we have a one-channel configuration, we can
608        * override it to stereo */
609       if (*channels == 1)
610         *channels = 2;
611     }
612
613     GST_LOG_OBJECT (aacparse,
614         "Audio object type 5 or 29, so rereading sampling rate (was %d)...",
615         *sample_rate);
616     if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
617       return FALSE;
618
619     if (!gst_aac_parse_get_audio_object_type (aacparse, br, &audio_object_type))
620       return FALSE;
621
622     if (audio_object_type == 22) {
623       /* extension channel configuration */
624       if (!gst_bit_reader_get_bits_uint8 (br, &extension_channel_configuration,
625               4))
626         return FALSE;
627       GST_LOG_OBJECT (aacparse, "extension channel_configuration: %d",
628           extension_channel_configuration);
629       *channels = loas_channels_table[extension_channel_configuration];
630       if (!*channels)
631         return FALSE;
632     }
633   } else {
634     extension_audio_object_type = 0;
635   }
636
637   GST_INFO_OBJECT (aacparse, "Parsed AudioSpecificConfig: %d Hz, %d channels",
638       *sample_rate, *channels);
639
640   if (frame_samples && audio_object_type == 23) {
641     guint8 frame_flag;
642     /* Read the Decoder Configuration (GASpecificConfig) if present */
643     /* We only care about the first bit to know what the number of samples
644      * in a frame is */
645     if (!gst_bit_reader_get_bits_uint8 (br, &frame_flag, 1))
646       return FALSE;
647     *frame_samples = frame_flag ? 960 : 1024;
648   }
649
650   /* There's LOTS of stuff next, but we ignore it for now as we have
651      what we want (sample rate and number of channels */
652   GST_DEBUG_OBJECT (aacparse,
653       "Need more code to parse humongous LOAS data, currently ignored");
654   aacparse->last_parsed_channels = *channels;
655   return TRUE;
656 }
657
658
659 static gboolean
660 gst_aac_parse_read_loas_config (GstAacParse * aacparse, const guint8 * data,
661     guint avail, gint * sample_rate, gint * channels, gint * version)
662 {
663   GstBitReader br;
664   guint8 u8, v, vA;
665
666   /* No version in the bitstream, but the spec has LOAS in the MPEG-4 section */
667   if (version)
668     *version = 4;
669
670   gst_bit_reader_init (&br, data, avail);
671
672   /* skip sync word (11 bits) and size (13 bits) */
673   if (!gst_bit_reader_skip (&br, 11 + 13))
674     return FALSE;
675
676   /* First bit is "use last config" */
677   if (!gst_bit_reader_get_bits_uint8 (&br, &u8, 1))
678     return FALSE;
679   if (u8) {
680     GST_LOG_OBJECT (aacparse, "Frame uses previous config");
681     if (!aacparse->last_parsed_sample_rate || !aacparse->last_parsed_channels) {
682       GST_DEBUG_OBJECT (aacparse,
683           "No previous config to use. We'll look for more data.");
684       return FALSE;
685     }
686     *sample_rate = aacparse->last_parsed_sample_rate;
687     *channels = aacparse->last_parsed_channels;
688     return TRUE;
689   }
690
691   GST_DEBUG_OBJECT (aacparse, "Frame contains new config");
692
693   /* audioMuxVersion */
694   if (!gst_bit_reader_get_bits_uint8 (&br, &v, 1))
695     return FALSE;
696   if (v) {
697     /* audioMuxVersionA */
698     if (!gst_bit_reader_get_bits_uint8 (&br, &vA, 1))
699       return FALSE;
700   } else
701     vA = 0;
702
703   GST_LOG_OBJECT (aacparse, "v %d, vA %d", v, vA);
704   if (vA == 0) {
705     guint8 same_time, subframes, num_program, prog;
706     if (v == 1) {
707       guint32 value;
708       /* taraBufferFullness */
709       if (!gst_aac_parse_latm_get_value (aacparse, &br, &value))
710         return FALSE;
711     }
712     if (!gst_bit_reader_get_bits_uint8 (&br, &same_time, 1))
713       return FALSE;
714     if (!gst_bit_reader_get_bits_uint8 (&br, &subframes, 6))
715       return FALSE;
716     if (!gst_bit_reader_get_bits_uint8 (&br, &num_program, 4))
717       return FALSE;
718     GST_LOG_OBJECT (aacparse, "same_time %d, subframes %d, num_program %d",
719         same_time, subframes, num_program);
720
721     for (prog = 0; prog <= num_program; ++prog) {
722       guint8 num_layer, layer;
723       if (!gst_bit_reader_get_bits_uint8 (&br, &num_layer, 3))
724         return FALSE;
725       GST_LOG_OBJECT (aacparse, "Program %d: %d layers", prog, num_layer);
726
727       for (layer = 0; layer <= num_layer; ++layer) {
728         guint8 use_same_config;
729         if (prog == 0 && layer == 0) {
730           use_same_config = 0;
731         } else {
732           if (!gst_bit_reader_get_bits_uint8 (&br, &use_same_config, 1))
733             return FALSE;
734         }
735         if (!use_same_config) {
736           if (v == 0) {
737             if (!gst_aac_parse_read_audio_specific_config (aacparse, &br, NULL,
738                     sample_rate, channels, NULL))
739               return FALSE;
740           } else {
741             guint32 asc_len;
742             if (!gst_aac_parse_latm_get_value (aacparse, &br, &asc_len))
743               return FALSE;
744             if (!gst_aac_parse_read_audio_specific_config (aacparse, &br, NULL,
745                     sample_rate, channels, NULL))
746               return FALSE;
747             if (!gst_bit_reader_skip (&br, asc_len))
748               return FALSE;
749           }
750         }
751       }
752     }
753     GST_LOG_OBJECT (aacparse, "More data ignored");
754   } else {
755     GST_WARNING_OBJECT (aacparse, "Spec says \"TBD\"...");
756     return FALSE;
757   }
758   return TRUE;
759 }
760
761 /**
762  * gst_aac_parse_loas_get_frame_len:
763  * @data: block of data containing a LOAS header.
764  *
765  * This function calculates LOAS frame length from the given header.
766  *
767  * Returns: size of the LOAS frame.
768  */
769 static inline guint
770 gst_aac_parse_loas_get_frame_len (const guint8 * data)
771 {
772   return (((data[1] & 0x1f) << 8) | data[2]) + 3;
773 }
774
775
776 /**
777  * gst_aac_parse_check_loas_frame:
778  * @aacparse: #GstAacParse.
779  * @data: Data to be checked.
780  * @avail: Amount of data passed.
781  * @framesize: If valid LOAS frame was found, this will be set to tell the
782  *             found frame size in bytes.
783  * @needed_data: If frame was not found, this may be set to tell how much
784  *               more data is needed in the next round to detect the frame
785  *               reliably. This may happen when a frame header candidate
786  *               is found but it cannot be guaranteed to be the header without
787  *               peeking the following data.
788  *
789  * Check if the given data contains contains LOAS frame. The algorithm
790  * will examine LOAS frame header and calculate the frame size. Also, another
791  * consecutive LOAS frame header need to be present after the found frame.
792  * Otherwise the data is not considered as a valid LOAS frame. However, this
793  * "extra check" is omitted when EOS has been received. In this case it is
794  * enough when data[0] contains a valid LOAS header.
795  *
796  * This function may set the #needed_data to indicate that a possible frame
797  * candidate has been found, but more data (#needed_data bytes) is needed to
798  * be absolutely sure. When this situation occurs, FALSE will be returned.
799  *
800  * When a valid frame is detected, this function will use
801  * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
802  * to set the needed bytes for next frame.This way next data chunk is already
803  * of correct size.
804  *
805  * LOAS can have three different formats, if I read the spec correctly. Only
806  * one of them is supported here, as the two samples I have use this one.
807  *
808  * Returns: TRUE if the given data contains a valid LOAS header.
809  */
810 static gboolean
811 gst_aac_parse_check_loas_frame (GstAacParse * aacparse,
812     const guint8 * data, const guint avail, gboolean drain,
813     guint * framesize, guint * needed_data)
814 {
815   *needed_data = 0;
816
817   /* 3 byte header */
818   if (G_UNLIKELY (avail < 3)) {
819     *needed_data = 3;
820     return FALSE;
821   }
822
823   if ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) {
824     *framesize = gst_aac_parse_loas_get_frame_len (data);
825     GST_DEBUG_OBJECT (aacparse, "Found possible %u byte LOAS frame",
826         *framesize);
827
828     /* In EOS mode this is enough. No need to examine the data further.
829        We also relax the check when we have sync, on the assumption that
830        if we're not looking at random data, we have a much higher chance
831        to get the correct sync, and this avoids losing two frames when
832        a single bit corruption happens. */
833     if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
834       return TRUE;
835     }
836
837     if (*framesize + LOAS_MAX_SIZE > avail) {
838       /* We have found a possible frame header candidate, but can't be
839          sure since we don't have enough data to check the next frame */
840       GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
841           *framesize + LOAS_MAX_SIZE, avail);
842       *needed_data = *framesize + LOAS_MAX_SIZE;
843       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
844           *framesize + LOAS_MAX_SIZE);
845       return FALSE;
846     }
847
848     if ((data[*framesize] == 0x56) && ((data[*framesize + 1] & 0xe0) == 0xe0)) {
849       guint nextlen = gst_aac_parse_loas_get_frame_len (data + (*framesize));
850
851       GST_LOG ("LOAS frame found, len: %d bytes", *framesize);
852       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
853           nextlen + LOAS_MAX_SIZE);
854       return TRUE;
855     } else {
856       GST_DEBUG_OBJECT (aacparse, "That was a false positive");
857     }
858   }
859   return FALSE;
860 }
861
862 /* caller ensure sufficient data */
863 static inline void
864 gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data,
865     gint * rate, gint * channels, gint * object, gint * version)
866 {
867
868   if (rate) {
869     gint sr_idx = (data[2] & 0x3c) >> 2;
870
871     *rate = gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
872   }
873   if (channels) {
874     *channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6);
875     if (*channels == 7)
876       *channels = 8;
877   }
878
879   if (version)
880     *version = (data[1] & 0x08) ? 2 : 4;
881   if (object)
882     *object = ((data[2] & 0xc0) >> 6) + 1;
883 }
884
885 /**
886  * gst_aac_parse_detect_stream:
887  * @aacparse: #GstAacParse.
888  * @data: A block of data that needs to be examined for stream characteristics.
889  * @avail: Size of the given datablock.
890  * @framesize: If valid stream was found, this will be set to tell the
891  *             first frame size in bytes.
892  * @skipsize: If valid stream was found, this will be set to tell the first
893  *            audio frame position within the given data.
894  *
895  * Examines the given piece of data and try to detect the format of it. It
896  * checks for "ADIF" header (in the beginning of the clip) and ADTS frame
897  * header. If the stream is detected, TRUE will be returned and #framesize
898  * is set to indicate the found frame size. Additionally, #skipsize might
899  * be set to indicate the number of bytes that need to be skipped, a.k.a. the
900  * position of the frame inside given data chunk.
901  *
902  * Returns: TRUE on success.
903  */
904 static gboolean
905 gst_aac_parse_detect_stream (GstAacParse * aacparse,
906     const guint8 * data, const guint avail, gboolean drain,
907     guint * framesize, gint * skipsize)
908 {
909   gboolean found = FALSE;
910   guint need_data_adts = 0, need_data_loas;
911   guint i = 0;
912
913   GST_DEBUG_OBJECT (aacparse, "Parsing header data");
914
915   /* FIXME: No need to check for ADIF if we are not in the beginning of the
916      stream */
917
918   /* Can we even parse the header? */
919   if (avail < MAX (ADTS_MAX_SIZE, LOAS_MAX_SIZE)) {
920     GST_DEBUG_OBJECT (aacparse, "Not enough data to check");
921     return FALSE;
922   }
923
924   for (i = 0; i < avail - 4; i++) {
925     if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) ||
926         ((data[i] == 0x56) && ((data[i + 1] & 0xe0) == 0xe0)) ||
927         strncmp ((char *) data + i, "ADIF", 4) == 0) {
928       GST_DEBUG_OBJECT (aacparse, "Found signature at offset %u", i);
929       found = TRUE;
930
931       if (i) {
932         /* Trick: tell the parent class that we didn't find the frame yet,
933            but make it skip 'i' amount of bytes. Next time we arrive
934            here we have full frame in the beginning of the data. */
935         *skipsize = i;
936         return FALSE;
937       }
938       break;
939     }
940   }
941   if (!found) {
942     if (i)
943       *skipsize = i;
944     return FALSE;
945   }
946
947   if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain,
948           framesize, &need_data_adts)) {
949     gint rate, channels;
950
951     GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize);
952
953     gst_aac_parse_parse_adts_header (aacparse, data, &rate, &channels,
954         &aacparse->object_type, &aacparse->mpegversion);
955
956     if (!channels || !framesize) {
957       GST_DEBUG_OBJECT (aacparse, "impossible ADTS configuration");
958       return FALSE;
959     }
960
961     aacparse->header_type = DSPAAC_HEADER_ADTS;
962     gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
963         aacparse->frame_samples, 2, 2);
964
965     GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d",
966         rate, channels, aacparse->object_type, aacparse->mpegversion);
967
968     gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
969
970     return TRUE;
971   }
972
973   if (gst_aac_parse_check_loas_frame (aacparse, data, avail, drain,
974           framesize, &need_data_loas)) {
975     gint rate = 0, channels = 0;
976
977     GST_INFO ("LOAS, framesize: %d", *framesize);
978
979     aacparse->header_type = DSPAAC_HEADER_LOAS;
980
981     if (!gst_aac_parse_read_loas_config (aacparse, data, avail, &rate,
982             &channels, &aacparse->mpegversion)) {
983       /* This is pretty normal when skipping data at the start of
984        * random stream (MPEG-TS capture for example) */
985       GST_LOG_OBJECT (aacparse, "Error reading LOAS config");
986       return FALSE;
987     }
988
989     if (rate && channels) {
990       gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
991           aacparse->frame_samples, 2, 2);
992
993       /* Don't store the sample rate and channels yet -
994        * this is just format detection. */
995       GST_DEBUG ("LOAS: samplerate %d, channels %d, objtype %d, version %d",
996           rate, channels, aacparse->object_type, aacparse->mpegversion);
997     }
998
999     gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
1000
1001     return TRUE;
1002   }
1003
1004   if (need_data_adts || need_data_loas) {
1005     /* This tells the parent class not to skip any data */
1006     *skipsize = 0;
1007     return FALSE;
1008   }
1009
1010   if (avail < ADIF_MAX_SIZE)
1011     return FALSE;
1012
1013   if (memcmp (data + i, "ADIF", 4) == 0) {
1014     const guint8 *adif;
1015     int skip_size = 0;
1016     int bitstream_type;
1017     int sr_idx;
1018     GstCaps *sinkcaps;
1019
1020     aacparse->header_type = DSPAAC_HEADER_ADIF;
1021     aacparse->mpegversion = 4;
1022
1023     /* Skip the "ADIF" bytes */
1024     adif = data + i + 4;
1025
1026     /* copyright string */
1027     if (adif[0] & 0x80)
1028       skip_size += 9;           /* skip 9 bytes */
1029
1030     bitstream_type = adif[0 + skip_size] & 0x10;
1031     aacparse->bitrate =
1032         ((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) |
1033         ((unsigned int) adif[1 + skip_size] << 11) |
1034         ((unsigned int) adif[2 + skip_size] << 3) |
1035         ((unsigned int) adif[3 + skip_size] & 0xe0);
1036
1037     /* CBR */
1038     if (bitstream_type == 0) {
1039 #if 0
1040       /* Buffer fullness parsing. Currently not needed... */
1041       guint num_elems = 0;
1042       guint fullness = 0;
1043
1044       num_elems = (adif[3 + skip_size] & 0x1e);
1045       GST_INFO ("ADIF num_config_elems: %d", num_elems);
1046
1047       fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) |
1048           ((unsigned int) adif[4 + skip_size] << 11) |
1049           ((unsigned int) adif[5 + skip_size] << 3) |
1050           ((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5);
1051
1052       GST_INFO ("ADIF buffer fullness: %d", fullness);
1053 #endif
1054       aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) |
1055           ((adif[7 + skip_size] & 0x80) >> 7);
1056       sr_idx = (adif[7 + skip_size] & 0x78) >> 3;
1057     }
1058     /* VBR */
1059     else {
1060       aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3;
1061       sr_idx = ((adif[4 + skip_size] & 0x07) << 1) |
1062           ((adif[5 + skip_size] & 0x80) >> 7);
1063     }
1064
1065     /* FIXME: This gives totally wrong results. Duration calculation cannot
1066        be based on this */
1067     aacparse->sample_rate =
1068         gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
1069
1070     /* baseparse is not given any fps,
1071      * so it will give up on timestamps, seeking, etc */
1072
1073     /* FIXME: Can we assume this? */
1074     aacparse->channels = 2;
1075
1076     GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d",
1077         aacparse->bitrate, aacparse->sample_rate, aacparse->object_type);
1078
1079     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512);
1080
1081     /* arrange for metadata and get out of the way */
1082     sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (aacparse));
1083     gst_aac_parse_set_src_caps (aacparse, sinkcaps);
1084     if (sinkcaps)
1085       gst_caps_unref (sinkcaps);
1086
1087     /* not syncable, not easily seekable (unless we push data from start */
1088     gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE);
1089     gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE);
1090     gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0);
1091
1092     *framesize = avail;
1093     return TRUE;
1094   }
1095
1096   /* This should never happen */
1097   return FALSE;
1098 }
1099
1100 /**
1101  * gst_aac_parse_get_audio_profile_object_type
1102  * @aacparse: #GstAacParse.
1103  *
1104  * Gets the MPEG-2 profile or the MPEG-4 object type value corresponding to the
1105  * mpegversion and profile of @aacparse's src pad caps, according to the
1106  * values defined by table 1.A.11 in ISO/IEC 14496-3.
1107  *
1108  * Returns: the profile or object type value corresponding to @aacparse's src
1109  * pad caps, if such a value exists; otherwise G_MAXUINT8.
1110  */
1111 static guint8
1112 gst_aac_parse_get_audio_profile_object_type (GstAacParse * aacparse)
1113 {
1114   GstCaps *srccaps;
1115   GstStructure *srcstruct;
1116   const gchar *profile;
1117   guint8 ret;
1118
1119   srccaps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse));
1120   if (G_UNLIKELY (srccaps == NULL)) {
1121     return G_MAXUINT8;
1122   }
1123
1124   srcstruct = gst_caps_get_structure (srccaps, 0);
1125   profile = gst_structure_get_string (srcstruct, "profile");
1126   if (G_UNLIKELY (profile == NULL)) {
1127     gst_caps_unref (srccaps);
1128     return G_MAXUINT8;
1129   }
1130
1131   if (g_strcmp0 (profile, "main") == 0) {
1132     ret = (guint8) 0U;
1133   } else if (g_strcmp0 (profile, "lc") == 0) {
1134     ret = (guint8) 1U;
1135   } else if (g_strcmp0 (profile, "ssr") == 0) {
1136     ret = (guint8) 2U;
1137   } else if (g_strcmp0 (profile, "ltp") == 0) {
1138     if (G_LIKELY (aacparse->mpegversion == 4))
1139       ret = (guint8) 3U;
1140     else
1141       ret = G_MAXUINT8;         /* LTP Object Type allowed only for MPEG-4 */
1142   } else {
1143     ret = G_MAXUINT8;
1144   }
1145
1146   gst_caps_unref (srccaps);
1147   return ret;
1148 }
1149
1150 /**
1151  * gst_aac_parse_get_audio_channel_configuration
1152  * @num_channels: number of audio channels.
1153  *
1154  * Gets the Channel Configuration value, as defined by table 1.19 in ISO/IEC
1155  * 14496-3, for a given number of audio channels.
1156  *
1157  * Returns: the Channel Configuration value corresponding to @num_channels, if
1158  * such a value exists; otherwise G_MAXUINT8.
1159  */
1160 static guint8
1161 gst_aac_parse_get_audio_channel_configuration (gint num_channels)
1162 {
1163   if (num_channels >= 1 && num_channels <= 6)   /* Mono up to & including 5.1 */
1164     return (guint8) num_channels;
1165   else if (num_channels == 8)   /* 7.1 */
1166     return (guint8) 7U;
1167   else
1168     return G_MAXUINT8;
1169
1170   /* FIXME: Add support for configurations 11, 12 and 14 from
1171    * ISO/IEC 14496-3:2009/PDAM 4 based on the actual channel layout
1172    */
1173 }
1174
1175 /**
1176  * gst_aac_parse_get_audio_sampling_frequency_index:
1177  * @sample_rate: audio sampling rate.
1178  *
1179  * Gets the Sampling Frequency Index value, as defined by table 1.18 in ISO/IEC
1180  * 14496-3, for a given sampling rate.
1181  *
1182  * Returns: the Sampling Frequency Index value corresponding to @sample_rate,
1183  * if such a value exists; otherwise G_MAXUINT8.
1184  */
1185 static guint8
1186 gst_aac_parse_get_audio_sampling_frequency_index (gint sample_rate)
1187 {
1188   switch (sample_rate) {
1189     case 96000:
1190       return 0x0U;
1191     case 88200:
1192       return 0x1U;
1193     case 64000:
1194       return 0x2U;
1195     case 48000:
1196       return 0x3U;
1197     case 44100:
1198       return 0x4U;
1199     case 32000:
1200       return 0x5U;
1201     case 24000:
1202       return 0x6U;
1203     case 22050:
1204       return 0x7U;
1205     case 16000:
1206       return 0x8U;
1207     case 12000:
1208       return 0x9U;
1209     case 11025:
1210       return 0xAU;
1211     case 8000:
1212       return 0xBU;
1213     case 7350:
1214       return 0xCU;
1215     default:
1216       return G_MAXUINT8;
1217   }
1218 }
1219
1220 /**
1221  * gst_aac_parse_prepend_adts_headers:
1222  * @aacparse: #GstAacParse.
1223  * @frame: raw AAC frame to which ADTS headers shall be prepended.
1224  *
1225  * Prepends ADTS headers to a raw AAC audio frame.
1226  *
1227  * Returns: TRUE if ADTS headers were successfully prepended; FALSE otherwise.
1228  */
1229 static gboolean
1230 gst_aac_parse_prepend_adts_headers (GstAacParse * aacparse,
1231     GstBaseParseFrame * frame)
1232 {
1233   GstMemory *mem;
1234   guint8 *adts_headers;
1235   gsize buf_size;
1236   gsize frame_size;
1237   guint8 id, profile, channel_configuration, sampling_frequency_index;
1238
1239   id = (aacparse->mpegversion == 4) ? 0x0U : 0x1U;
1240   profile = gst_aac_parse_get_audio_profile_object_type (aacparse);
1241   if (profile == G_MAXUINT8) {
1242     GST_ERROR_OBJECT (aacparse, "Unsupported audio profile or object type");
1243     return FALSE;
1244   }
1245   channel_configuration =
1246       gst_aac_parse_get_audio_channel_configuration (aacparse->channels);
1247   if (channel_configuration == G_MAXUINT8) {
1248     GST_ERROR_OBJECT (aacparse, "Unsupported number of channels");
1249     return FALSE;
1250   }
1251   sampling_frequency_index =
1252       gst_aac_parse_get_audio_sampling_frequency_index (aacparse->sample_rate);
1253   if (sampling_frequency_index == G_MAXUINT8) {
1254     GST_ERROR_OBJECT (aacparse, "Unsupported sampling frequency");
1255     return FALSE;
1256   }
1257
1258   frame->out_buffer = gst_buffer_copy (frame->buffer);
1259   buf_size = gst_buffer_get_size (frame->out_buffer);
1260   frame_size = buf_size + ADTS_HEADERS_LENGTH;
1261
1262   if (G_UNLIKELY (frame_size >= 0x4000)) {
1263     GST_ERROR_OBJECT (aacparse, "Frame size is too big for ADTS");
1264     return FALSE;
1265   }
1266
1267   adts_headers = (guint8 *) g_malloc0 (ADTS_HEADERS_LENGTH);
1268
1269   /* Note: no error correction bits are added to the resulting ADTS frames */
1270   adts_headers[0] = 0xFFU;
1271   adts_headers[1] = 0xF0U | (id << 3) | 0x1U;
1272   adts_headers[2] = (profile << 6) | (sampling_frequency_index << 2) | 0x2U |
1273       (channel_configuration & 0x4U);
1274   adts_headers[3] = ((channel_configuration & 0x3U) << 6) | 0x30U |
1275       (guint8) (frame_size >> 11);
1276   adts_headers[4] = (guint8) ((frame_size >> 3) & 0x00FF);
1277   adts_headers[5] = (guint8) (((frame_size & 0x0007) << 5) + 0x1FU);
1278   adts_headers[6] = 0xFCU;
1279
1280   mem = gst_memory_new_wrapped (0, adts_headers, ADTS_HEADERS_LENGTH, 0,
1281       ADTS_HEADERS_LENGTH, adts_headers, g_free);
1282   gst_buffer_prepend_memory (frame->out_buffer, mem);
1283
1284   return TRUE;
1285 }
1286
1287 /**
1288  * gst_aac_parse_check_valid_frame:
1289  * @parse: #GstBaseParse.
1290  * @frame: #GstBaseParseFrame.
1291  * @skipsize: How much data parent class should skip in order to find the
1292  *            frame header.
1293  *
1294  * Implementation of "handle_frame" vmethod in #GstBaseParse class.
1295  *
1296  * Also determines frame overhead.
1297  * ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have
1298  * a per-frame header. LOAS has 3 bytes.
1299  *
1300  * We're making a couple of simplifying assumptions:
1301  *
1302  * 1. We count Program Configuration Elements rather than searching for them
1303  *    in the streams to discount them - the overhead is negligible.
1304  *
1305  * 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16
1306  *    bits, which should still not be significant enough to warrant the
1307  *    additional parsing through the headers
1308  *
1309  * Returns: a #GstFlowReturn.
1310  */
1311 static GstFlowReturn
1312 gst_aac_parse_handle_frame (GstBaseParse * parse,
1313     GstBaseParseFrame * frame, gint * skipsize)
1314 {
1315   GstMapInfo map;
1316   GstAacParse *aacparse;
1317   gboolean ret = FALSE;
1318   gboolean lost_sync;
1319   GstBuffer *buffer;
1320   guint framesize;
1321   gint rate = 0, channels = 0;
1322
1323   aacparse = GST_AAC_PARSE (parse);
1324   buffer = frame->buffer;
1325
1326   gst_buffer_map (buffer, &map, GST_MAP_READ);
1327
1328   *skipsize = -1;
1329   lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
1330
1331   if (aacparse->header_type == DSPAAC_HEADER_ADIF ||
1332       aacparse->header_type == DSPAAC_HEADER_NONE) {
1333     /* There is nothing to parse */
1334     framesize = map.size;
1335     ret = TRUE;
1336
1337   } else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) {
1338
1339     ret = gst_aac_parse_detect_stream (aacparse, map.data, map.size,
1340         GST_BASE_PARSE_DRAINING (parse), &framesize, skipsize);
1341
1342   } else if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1343     guint needed_data = 1024;
1344
1345     ret = gst_aac_parse_check_adts_frame (aacparse, map.data, map.size,
1346         GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
1347
1348     if (!ret && needed_data) {
1349       GST_DEBUG ("buffer didn't contain valid frame");
1350       *skipsize = 0;
1351       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1352           needed_data);
1353     }
1354
1355   } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1356     guint needed_data = 1024;
1357
1358     ret = gst_aac_parse_check_loas_frame (aacparse, map.data,
1359         map.size, GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
1360
1361     if (!ret && needed_data) {
1362       GST_DEBUG ("buffer didn't contain valid frame");
1363       *skipsize = 0;
1364       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1365           needed_data);
1366     }
1367
1368   } else {
1369     GST_DEBUG ("buffer didn't contain valid frame");
1370     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1371         ADTS_MAX_SIZE);
1372   }
1373
1374   if (G_UNLIKELY (!ret))
1375     goto exit;
1376
1377   if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1378     /* see above */
1379     frame->overhead = 7;
1380
1381     gst_aac_parse_parse_adts_header (aacparse, map.data,
1382         &rate, &channels, NULL, NULL);
1383
1384     GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels);
1385
1386     if (G_UNLIKELY (rate != aacparse->sample_rate
1387             || channels != aacparse->channels)) {
1388       aacparse->sample_rate = rate;
1389       aacparse->channels = channels;
1390
1391       if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1392         /* If linking fails, we need to return appropriate error */
1393         ret = GST_FLOW_NOT_LINKED;
1394       }
1395
1396       gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1397           aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1398     }
1399 #ifdef TIZEN_FEATURE_AACPARSE_MODIFICATION
1400     if (aacparse->first_frame == TRUE) {
1401       gboolean ret = FALSE;
1402       aacparse->first_frame = FALSE;
1403
1404       ret = gst_aac_parse_estimate_duration (parse);
1405       if (!ret) {
1406         GST_WARNING_OBJECT (aacparse, "can not estimate total duration");
1407         ret = GST_FLOW_NOT_SUPPORTED;
1408       }
1409     }
1410 #endif
1411   } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1412     gboolean setcaps = FALSE;
1413
1414     /* see above */
1415     frame->overhead = 3;
1416
1417     if (!gst_aac_parse_read_loas_config (aacparse, map.data, map.size, &rate,
1418             &channels, NULL) || !rate || !channels) {
1419       /* This is pretty normal when skipping data at the start of
1420        * random stream (MPEG-TS capture for example) */
1421       GST_DEBUG_OBJECT (aacparse, "Error reading LOAS config. Skipping.");
1422       /* Since we don't fully parse the LOAS config, we don't know for sure
1423        * how much to skip. Just skip 1 to end up to the next marker and
1424        * resume parsing from there */
1425       *skipsize = 1;
1426       goto exit;
1427     }
1428
1429     if (G_UNLIKELY (rate != aacparse->sample_rate
1430             || channels != aacparse->channels)) {
1431       aacparse->sample_rate = rate;
1432       aacparse->channels = channels;
1433       setcaps = TRUE;
1434       GST_INFO_OBJECT (aacparse, "New LOAS config: %d Hz, %d channels", rate,
1435           channels);
1436     }
1437
1438     /* We want to set caps both at start, and when rate/channels change.
1439        Since only some LOAS frames have that info, we may receive frames
1440        before knowing about rate/channels. */
1441     if (setcaps
1442         || !gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse))) {
1443       if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1444         /* If linking fails, we need to return appropriate error */
1445         ret = GST_FLOW_NOT_LINKED;
1446       }
1447
1448       gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1449           aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1450     }
1451   }
1452 #ifdef TIZEN_FEATURE_AACPARSE_MODIFICATION
1453   else if (aacparse->header_type == DSPAAC_HEADER_ADIF) {
1454     /* to get more correct duration */
1455     float estimated_duration = 0;
1456     gint64 total_file_size;
1457     gst_base_parse_get_upstream_size (parse, &total_file_size);
1458     estimated_duration =
1459         ((total_file_size * 8) / (float) (aacparse->bitrate * 1000)) *
1460         GST_SECOND;
1461     gst_base_parse_set_duration (parse, GST_FORMAT_TIME,
1462         estimated_duration * 1000, 0);
1463   }
1464 #endif
1465
1466   if (aacparse->header_type == DSPAAC_HEADER_NONE
1467       && aacparse->output_header_type == DSPAAC_HEADER_ADTS) {
1468     if (!gst_aac_parse_prepend_adts_headers (aacparse, frame)) {
1469       GST_ERROR_OBJECT (aacparse, "Failed to prepend ADTS headers to frame");
1470       ret = GST_FLOW_ERROR;
1471     }
1472   }
1473
1474 exit:
1475   gst_buffer_unmap (buffer, &map);
1476
1477   if (ret) {
1478     /* found, skip if needed */
1479     if (*skipsize > 0)
1480       return GST_FLOW_OK;
1481     *skipsize = 0;
1482   } else {
1483     if (*skipsize < 0)
1484       *skipsize = 1;
1485   }
1486
1487   if (ret && framesize <= map.size) {
1488     return gst_base_parse_finish_frame (parse, frame, framesize);
1489   }
1490
1491   return GST_FLOW_OK;
1492 }
1493
1494 static GstFlowReturn
1495 gst_aac_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
1496 {
1497   GstAacParse *aacparse = GST_AAC_PARSE (parse);
1498
1499   if (!aacparse->sent_codec_tag) {
1500     GstTagList *taglist;
1501     GstCaps *caps;
1502
1503     /* codec tag */
1504     caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
1505     if (caps == NULL) {
1506       if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
1507         GST_INFO_OBJECT (parse, "Src pad is flushing");
1508         return GST_FLOW_FLUSHING;
1509       } else {
1510         GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
1511         return GST_FLOW_NOT_NEGOTIATED;
1512       }
1513     }
1514
1515     taglist = gst_tag_list_new_empty ();
1516     gst_pb_utils_add_codec_description_to_tag_list (taglist,
1517         GST_TAG_AUDIO_CODEC, caps);
1518     gst_caps_unref (caps);
1519
1520     gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
1521     gst_tag_list_unref (taglist);
1522
1523     /* also signals the end of first-frame processing */
1524     aacparse->sent_codec_tag = TRUE;
1525   }
1526
1527   /* As a special case, we can remove the ADTS framing and output raw AAC. */
1528   if (aacparse->header_type == DSPAAC_HEADER_ADTS
1529       && aacparse->output_header_type == DSPAAC_HEADER_NONE) {
1530     guint header_size;
1531     GstMapInfo map;
1532     frame->out_buffer = gst_buffer_make_writable (frame->buffer);
1533     frame->buffer = NULL;
1534     gst_buffer_map (frame->out_buffer, &map, GST_MAP_READ);
1535     header_size = (map.data[1] & 1) ? 7 : 9;    /* optional CRC */
1536     gst_buffer_unmap (frame->out_buffer, &map);
1537     gst_buffer_resize (frame->out_buffer, header_size,
1538         gst_buffer_get_size (frame->out_buffer) - header_size);
1539   }
1540
1541   return GST_FLOW_OK;
1542 }
1543
1544
1545 /**
1546  * gst_aac_parse_start:
1547  * @parse: #GstBaseParse.
1548  *
1549  * Implementation of "start" vmethod in #GstBaseParse class.
1550  *
1551  * Returns: TRUE if startup succeeded.
1552  */
1553 static gboolean
1554 gst_aac_parse_start (GstBaseParse * parse)
1555 {
1556   GstAacParse *aacparse;
1557
1558   aacparse = GST_AAC_PARSE (parse);
1559   GST_DEBUG ("start");
1560   aacparse->frame_samples = 1024;
1561   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), ADTS_MAX_SIZE);
1562   aacparse->sent_codec_tag = FALSE;
1563   aacparse->last_parsed_channels = 0;
1564   aacparse->last_parsed_sample_rate = 0;
1565   aacparse->object_type = 0;
1566   aacparse->bitrate = 0;
1567   aacparse->header_type = DSPAAC_HEADER_NOT_PARSED;
1568   aacparse->output_header_type = DSPAAC_HEADER_NOT_PARSED;
1569   aacparse->channels = 0;
1570   aacparse->sample_rate = 0;
1571   return TRUE;
1572 }
1573
1574
1575 /**
1576  * gst_aac_parse_stop:
1577  * @parse: #GstBaseParse.
1578  *
1579  * Implementation of "stop" vmethod in #GstBaseParse class.
1580  *
1581  * Returns: TRUE is stopping succeeded.
1582  */
1583 static gboolean
1584 gst_aac_parse_stop (GstBaseParse * parse)
1585 {
1586   GST_DEBUG ("stop");
1587   return TRUE;
1588 }
1589
1590 static void
1591 remove_fields (GstCaps * caps)
1592 {
1593   guint i, n;
1594
1595   n = gst_caps_get_size (caps);
1596   for (i = 0; i < n; i++) {
1597     GstStructure *s = gst_caps_get_structure (caps, i);
1598
1599     gst_structure_remove_field (s, "framed");
1600   }
1601 }
1602
1603 static void
1604 add_conversion_fields (GstCaps * caps)
1605 {
1606   guint i, n;
1607
1608   n = gst_caps_get_size (caps);
1609   for (i = 0; i < n; i++) {
1610     GstStructure *s = gst_caps_get_structure (caps, i);
1611
1612     if (gst_structure_has_field (s, "stream-format")) {
1613       const GValue *v = gst_structure_get_value (s, "stream-format");
1614
1615       if (G_VALUE_HOLDS_STRING (v)) {
1616         const gchar *str = g_value_get_string (v);
1617
1618         if (strcmp (str, "adts") == 0 || strcmp (str, "raw") == 0) {
1619           GValue va = G_VALUE_INIT;
1620           GValue vs = G_VALUE_INIT;
1621
1622           g_value_init (&va, GST_TYPE_LIST);
1623           g_value_init (&vs, G_TYPE_STRING);
1624           g_value_set_string (&vs, "adts");
1625           gst_value_list_append_value (&va, &vs);
1626           g_value_set_string (&vs, "raw");
1627           gst_value_list_append_value (&va, &vs);
1628           gst_structure_set_value (s, "stream-format", &va);
1629           g_value_unset (&va);
1630           g_value_unset (&vs);
1631         }
1632       } else if (GST_VALUE_HOLDS_LIST (v)) {
1633         gboolean contains_raw = FALSE;
1634         gboolean contains_adts = FALSE;
1635         guint m = gst_value_list_get_size (v), j;
1636
1637         for (j = 0; j < m; j++) {
1638           const GValue *ve = gst_value_list_get_value (v, j);
1639           const gchar *str;
1640
1641           if (G_VALUE_HOLDS_STRING (ve) && (str = g_value_get_string (ve))) {
1642             if (strcmp (str, "adts") == 0)
1643               contains_adts = TRUE;
1644             else if (strcmp (str, "raw") == 0)
1645               contains_raw = TRUE;
1646           }
1647         }
1648
1649         if (contains_adts || contains_raw) {
1650           GValue va = G_VALUE_INIT;
1651           GValue vs = G_VALUE_INIT;
1652
1653           g_value_init (&va, GST_TYPE_LIST);
1654           g_value_init (&vs, G_TYPE_STRING);
1655           g_value_copy (v, &va);
1656
1657           if (!contains_raw) {
1658             g_value_set_string (&vs, "raw");
1659             gst_value_list_append_value (&va, &vs);
1660           }
1661           if (!contains_adts) {
1662             g_value_set_string (&vs, "adts");
1663             gst_value_list_append_value (&va, &vs);
1664           }
1665
1666           gst_structure_set_value (s, "stream-format", &va);
1667
1668           g_value_unset (&vs);
1669           g_value_unset (&va);
1670         }
1671       }
1672     }
1673   }
1674 }
1675
1676 static GstCaps *
1677 gst_aac_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter)
1678 {
1679   GstCaps *peercaps, *templ;
1680   GstCaps *res;
1681
1682   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1683
1684   if (filter) {
1685     GstCaps *fcopy = gst_caps_copy (filter);
1686     /* Remove the fields we convert */
1687     remove_fields (fcopy);
1688     add_conversion_fields (fcopy);
1689     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
1690     gst_caps_unref (fcopy);
1691   } else
1692     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
1693
1694   if (peercaps) {
1695     peercaps = gst_caps_make_writable (peercaps);
1696     /* Remove the fields we convert */
1697     remove_fields (peercaps);
1698     add_conversion_fields (peercaps);
1699
1700     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1701     gst_caps_unref (peercaps);
1702     gst_caps_unref (templ);
1703   } else {
1704     res = templ;
1705   }
1706
1707   if (filter) {
1708     GstCaps *intersection;
1709
1710     intersection =
1711         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1712     gst_caps_unref (res);
1713     res = intersection;
1714   }
1715
1716   return res;
1717 }
1718
1719 static gboolean
1720 gst_aac_parse_src_event (GstBaseParse * parse, GstEvent * event)
1721 {
1722   GstAacParse *aacparse = GST_AAC_PARSE (parse);
1723
1724   if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1725     aacparse->last_parsed_channels = 0;
1726     aacparse->last_parsed_sample_rate = 0;
1727   }
1728 #ifdef TIZEN_FEATURE_AACPARSE_MODIFICATION
1729   GST_DEBUG ("Entering gst_aac_parse_src_eventfunc header type = %d",
1730       aacparse->header_type);
1731   if (aacparse->header_type == DSPAAC_HEADER_ADTS)
1732     return gst_aac_parse_adts_src_eventfunc (parse, event);
1733
1734 aac_seek_null_exit:
1735 #endif
1736   return GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
1737
1738 }
1739
1740 #ifdef TIZEN_FEATURE_AACPARSE_MODIFICATION
1741 /**
1742  * get_aac_parse_get_adts_framelength:
1743  * @data: #GstBufferData.
1744  * @offset: #GstBufferData offset
1745  *
1746  * Implementation to get adts framelength by using first some frame.
1747  *
1748  * Returns: frame size
1749  */
1750 int
1751 get_aac_parse_get_adts_frame_length (const unsigned char *data, gint64 offset)
1752 {
1753   const gint adts_header_length_no_crc = 7;
1754   const gint adts_header_length_with_crc = 9;
1755   gint frame_size = 0;
1756   gint protection_absent;
1757   gint head_size;
1758
1759   /* check of syncword */
1760   if ((data[offset + 0] != 0xff) || ((data[offset + 1] & 0xf6) != 0xf0)) {
1761     GST_ERROR ("check sync word is fail\n");
1762     return -1;
1763   }
1764
1765   /* check of protection absent */
1766   protection_absent = (data[offset + 1] & 0x01);
1767
1768   /*check of frame length */
1769   frame_size =
1770       (data[offset + 3] & 0x3) << 11 | data[offset + 4] << 3 | data[offset +
1771       5] >> 5;
1772
1773   /* check of header size */
1774   /* protectionAbsent is 0 if there is CRC */
1775   head_size =
1776       protection_absent ? adts_header_length_no_crc :
1777       adts_header_length_with_crc;
1778   if (head_size > frame_size) {
1779     GST_ERROR ("return frame length as 0 (frameSize %u < headSize %u)",
1780         frame_size, head_size);
1781     return 0;
1782   }
1783
1784   return frame_size;
1785 }
1786
1787 /**
1788  * gst_aac_parse_estimate_duration:
1789  * @parse: #GstBaseParse.
1790  *
1791  * Implementation to get estimated total duration by using first some frame.
1792  *
1793  * Returns: TRUE if we can get estimated total duraion
1794  */
1795 static gboolean
1796 gst_aac_parse_estimate_duration (GstBaseParse * parse)
1797 {
1798   gboolean ret = FALSE;
1799   GstFlowReturn res = GST_FLOW_OK;
1800   gint64 pull_size = 0, file_size = 0, offset = 0, num_frames = 0, duration = 0;
1801   guint sample_rate_index = 0, sample_rate = 0, channel = 0;
1802   guint frame_size = 0, frame_duration_us = 0, estimated_bitrate = 0;
1803   guint lost_sync_count = 0;
1804   GstClockTime estimated_duration = GST_CLOCK_TIME_NONE;
1805   GstBuffer *buffer = NULL;
1806   guint8 *buf = NULL;
1807   gint i = 0;
1808   GstPadMode pad_mode = GST_PAD_MODE_NONE;
1809   GstAacParse *aacparse;
1810   gint64 buffer_size = 0;
1811   GstMapInfo map;
1812
1813   aacparse = GST_AAC_PARSE (parse);
1814   GST_LOG_OBJECT (aacparse, "gst_aac_parse_estimate_duration enter");
1815
1816   /* check baseparse define these fuction */
1817   gst_base_parse_get_pad_mode (parse, &pad_mode);
1818   if (pad_mode != GST_PAD_MODE_PULL) {
1819     GST_INFO_OBJECT (aacparse,
1820         "aac parser is not pull mode. can not estimate duration");
1821     return FALSE;
1822   }
1823
1824   gst_base_parse_get_upstream_size (parse, &file_size);
1825
1826   if (file_size < ADIF_MAX_SIZE) {
1827     GST_ERROR_OBJECT (aacparse, "file size is too short");
1828     return FALSE;
1829   }
1830
1831   pull_size = MIN (file_size, AAC_MAX_ESTIMATE_DURATION_BUF);
1832
1833   res = gst_pad_pull_range (parse->sinkpad, 0, pull_size, &buffer);
1834   if (res != GST_FLOW_OK) {
1835     GST_ERROR_OBJECT (aacparse, "gst_pad_pull_range failed!");
1836     return FALSE;
1837   }
1838
1839   gst_buffer_map (buffer, &map, GST_MAP_READ);
1840   buf = map.data;
1841   buffer_size = map.size;
1842   if (buffer_size != pull_size) {
1843     GST_ERROR_OBJECT (aacparse,
1844         "We got different buffer_size(%" G_GINT64_FORMAT ") with pull_size(%"
1845         G_GINT64_FORMAT ").", buffer_size, pull_size);
1846   }
1847
1848   /* MODIFICATION : add defence codes for real buffer_size is different with pull_size */
1849   for (i = 0; i < buffer_size; i++) {
1850     if ((buf[i] == 0xff) && ((buf[i + 1] & 0xf6) == 0xf0)) {    /* aac sync word */
1851       //guint profile = (buf[i+2] >> 6) & 0x3;
1852       sample_rate_index = (buf[i + 2] >> 2) & 0xf;
1853       sample_rate =
1854           gst_aac_parse_get_sample_rate_from_index (sample_rate_index);
1855       if (sample_rate == 0) {
1856         GST_WARNING_OBJECT (aacparse, "Invalid sample rate index (0)");
1857         goto EXIT;
1858       }
1859       channel = (buf[i + 2] & 0x1) << 2 | (buf[i + 3] >> 6);
1860
1861       GST_INFO_OBJECT (aacparse, "found sync. aac fs=%d, ch=%d", sample_rate,
1862           channel);
1863
1864       /* count number of frames */
1865       /* MODIFICATION : add defence codes for real buffer_size is different with pull_size */
1866       //while (offset < pull_size) {
1867       while (offset < buffer_size) {
1868         frame_size = get_aac_parse_get_adts_frame_length (buf, i + offset);
1869         if (frame_size == 0) {
1870           GST_ERROR_OBJECT (aacparse,
1871               "framesize error at offset %" G_GINT64_FORMAT, offset);
1872           break;
1873         } else if (frame_size == -1) {
1874           offset++;
1875           lost_sync_count++;    //  lost sync count limmitation 2K Bytes
1876           if (lost_sync_count > (1024 * 2)) {
1877             GST_WARNING_OBJECT (aacparse,
1878                 "lost_sync_count is larger than 2048");
1879             goto EXIT;
1880           }
1881         } else {
1882           offset += frame_size;
1883           num_frames++;
1884           lost_sync_count = 0;
1885         }
1886       }                         /* while */
1887
1888       /* if we can got full file, we can calculate the accurate duration */
1889       /* MODIFICATION : add defence codes for real buffer_size is different with pull_size */
1890       //if (pull_size == file_size) {
1891       if (buffer_size == file_size) {
1892         gfloat duration_for_one_frame = 0;
1893         GstClockTime calculated_duration = GST_CLOCK_TIME_NONE;
1894
1895         GST_INFO_OBJECT (aacparse,
1896             "we got total file (%" G_GINT64_FORMAT
1897             " bytes). do not estimate but make Accurate total duration.",
1898             pull_size);
1899
1900         duration_for_one_frame =
1901             (gfloat) AAC_SAMPLE_PER_FRAME / (gfloat) sample_rate;
1902         calculated_duration =
1903             num_frames * duration_for_one_frame * 1000 * 1000 * 1000;
1904
1905         GST_INFO_OBJECT (aacparse, "duration_for_one_frame %f ms",
1906             duration_for_one_frame);
1907         GST_INFO_OBJECT (aacparse, "calculated duration = %" GST_TIME_FORMAT,
1908             GST_TIME_ARGS (calculated_duration));
1909         /* 0 means disable estimate */
1910         gst_base_parse_set_duration (parse, GST_FORMAT_TIME,
1911             calculated_duration, 0);
1912
1913       } else {
1914         GST_INFO_OBJECT (aacparse,
1915             "we got %" G_GUINT64_FORMAT " bytes in total file (%"
1916             G_GINT64_FORMAT "). can not make accurate duration but Estimate.",
1917             pull_size, file_size);
1918         frame_duration_us =
1919             (1024 * 1000000ll + (sample_rate - 1)) / sample_rate;
1920         duration = num_frames * frame_duration_us;
1921
1922         if (duration == 0) {
1923           GST_WARNING_OBJECT (aacparse, "Invalid duration");
1924           goto EXIT;
1925         }
1926         estimated_bitrate =
1927             (gint) ((gfloat) (offset * 8) / (gfloat) (duration / 1000));
1928
1929         if (estimated_bitrate == 0) {
1930           GST_WARNING_OBJECT (aacparse, "Invalid estimated_bitrate");
1931           goto EXIT;
1932         }
1933         estimated_duration =
1934             (GstClockTime) ((file_size * 8) / (estimated_bitrate * 1000)) *
1935             GST_SECOND;
1936
1937         GST_INFO_OBJECT (aacparse, "number of frame = %" G_GINT64_FORMAT,
1938             num_frames);
1939         GST_INFO_OBJECT (aacparse, "duration = %" G_GINT64_FORMAT,
1940             duration / 1000000);
1941         GST_INFO_OBJECT (aacparse, "byte = %" G_GINT64_FORMAT, offset);
1942         GST_INFO_OBJECT (aacparse, "estimated bitrate = %d bps",
1943             estimated_bitrate);
1944         GST_INFO_OBJECT (aacparse, "estimated duration = %" GST_TIME_FORMAT,
1945             GST_TIME_ARGS (estimated_duration));
1946
1947         gst_base_parse_set_average_bitrate (parse, estimated_bitrate * 1000);
1948         /* set update_interval as duration(sec)/2 */
1949         gst_base_parse_set_duration (parse, GST_FORMAT_TIME, estimated_duration,
1950             (gint) (duration / 2));
1951       }
1952
1953       break;
1954     }
1955   }
1956   ret = TRUE;
1957
1958 EXIT:
1959   gst_buffer_unmap (buffer, &map);
1960   gst_buffer_unref (buffer);
1961   return ret;
1962 }
1963
1964
1965 /* perform seek in push based mode:
1966    find BYTE position to move to based on time and delegate to upstream
1967 */
1968 static gboolean
1969 gst_aac_audio_parse_do_push_seek (GstBaseParse * parse,
1970     GstPad * pad, GstEvent * event)
1971 {
1972   GstAacParse *aacparse = GST_AAC_PARSE (parse);
1973   gdouble rate;
1974   GstFormat format;
1975   GstSeekFlags flags;
1976   GstSeekType cur_type, stop_type;
1977   gint64 cur, stop;
1978   gboolean res;
1979   gint64 byte_cur;
1980   gint64 esimate_byte;
1981   gint32 frame_dur;
1982   gint64 upstream_total_bytes = 0;
1983   GstFormat fmt = GST_FORMAT_BYTES;
1984
1985   GST_INFO_OBJECT (parse, "doing aac push-based seek");
1986
1987   gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
1988       &stop_type, &stop);
1989
1990   /* FIXME, always play to the end */
1991   stop = -1;
1992
1993   /* only forward streaming and seeking is possible */
1994   if (rate <= 0)
1995     goto unsupported_seek;
1996
1997   if (cur == 0) {
1998     /* handle rewind only */
1999     cur_type = GST_SEEK_TYPE_SET;
2000     byte_cur = 0;
2001     stop_type = GST_SEEK_TYPE_NONE;
2002     stop = -1;
2003     flags |= GST_SEEK_FLAG_FLUSH;
2004   } else {
2005     /* handle normal seek */
2006     cur_type = GST_SEEK_TYPE_SET;
2007     stop_type = GST_SEEK_TYPE_NONE;
2008     stop = -1;
2009     flags |= GST_SEEK_FLAG_FLUSH;
2010
2011     esimate_byte = (cur / (1000 * 1000)) * aacparse->frame_byte;
2012     if (aacparse->sample_rate > 0)
2013       frame_dur = (aacparse->spf * 1000) / aacparse->sample_rate;
2014     else
2015       goto unsupported_seek;
2016     if (frame_dur > 0)
2017       byte_cur = esimate_byte / (frame_dur);
2018     else
2019       goto unsupported_seek;
2020
2021     GST_INFO_OBJECT (parse, "frame_byte(%d) spf(%d)  rate (%d) ",
2022         aacparse->frame_byte, aacparse->spf, aacparse->sample_rate);
2023     GST_INFO_OBJECT (parse,
2024         "seek cur (%" G_GINT64_FORMAT ") = (%" GST_TIME_FORMAT ") ", cur,
2025         GST_TIME_ARGS (cur));
2026     GST_INFO_OBJECT (parse,
2027         "esimate_byte(%" G_GINT64_FORMAT ")  esimate_byte (%d)", esimate_byte,
2028         frame_dur);
2029   }
2030
2031   /* obtain real upstream total bytes */
2032   if (!gst_pad_peer_query_duration (parse->sinkpad, fmt, &upstream_total_bytes))
2033     upstream_total_bytes = 0;
2034   GST_INFO_OBJECT (aacparse,
2035       "gst_pad_query_peer_duration -upstream_total_bytes (%" G_GUINT64_FORMAT
2036       ")", upstream_total_bytes);
2037   aacparse->file_size = upstream_total_bytes;
2038
2039   if ((byte_cur == -1) || (byte_cur > aacparse->file_size)) {
2040     GST_INFO_OBJECT (parse,
2041         "[WEB-ERROR] seek cur (%" G_GINT64_FORMAT ") > file_size (%"
2042         G_GINT64_FORMAT ") ", cur, aacparse->file_size);
2043     goto abort_seek;
2044   }
2045
2046   GST_INFO_OBJECT (parse,
2047       "Pushing BYTE seek rate %g, " "start %" G_GINT64_FORMAT ", stop %"
2048       G_GINT64_FORMAT, rate, byte_cur, stop);
2049
2050   if (!(flags & GST_SEEK_FLAG_KEY_UNIT)) {
2051     GST_INFO_OBJECT (parse,
2052         "Requested seek time: %" GST_TIME_FORMAT ", calculated seek offset: %"
2053         G_GUINT64_FORMAT, GST_TIME_ARGS (cur), byte_cur);
2054   }
2055
2056   /* BYTE seek event */
2057   event =
2058       gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, byte_cur,
2059       stop_type, stop);
2060   res = gst_pad_push_event (parse->sinkpad, event);
2061
2062   return res;
2063
2064   /* ERRORS */
2065
2066 abort_seek:
2067   {
2068     GST_DEBUG_OBJECT (parse,
2069         "could not determine byte position to seek to, " "seek aborted.");
2070     return FALSE;
2071   }
2072
2073 unsupported_seek:
2074   {
2075     GST_DEBUG_OBJECT (parse, "unsupported seek, seek aborted.");
2076     return FALSE;
2077   }
2078 }
2079
2080
2081 static guint
2082 gst_aac_parse_adts_get_fast_frame_len (const guint8 * data)
2083 {
2084   int length;
2085   if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) {
2086     length =
2087         ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5);
2088   } else {
2089     length = 0;
2090   }
2091   return length;
2092 }
2093
2094 /**
2095  * gst_aac_parse_adts_src_eventfunc:
2096  * @parse: #GstBaseParse. #event
2097  *
2098  * before baseparse handles seek event, make full amr index table.
2099  *
2100  * Returns: TRUE on success.
2101  */
2102 static gboolean
2103 gst_aac_parse_adts_src_eventfunc (GstBaseParse * parse, GstEvent * event)
2104 {
2105   gboolean handled = FALSE;
2106   GstAacParse *aacparse = GST_AAC_PARSE (parse);
2107
2108   switch (GST_EVENT_TYPE (event)) {
2109     case GST_EVENT_SEEK:
2110     {
2111       GstFlowReturn res = GST_FLOW_OK;
2112       gint64 base_offset = 0, cur = 0;
2113       gint32 frame_count = 1;   /* do not add first frame because it is already in index table */
2114       gint64 second_count = 0;  /* initial 1 second */
2115       gint64 total_file_size = 0, start_offset = 0;
2116       GstClockTime current_ts = GST_CLOCK_TIME_NONE;
2117       GstPadMode pad_mode = GST_PAD_MODE_NONE;
2118
2119       /* check baseparse define these fuction */
2120       gst_base_parse_get_pad_mode (parse, &pad_mode);
2121       if (pad_mode != GST_PAD_MODE_PULL) {
2122         gboolean ret = FALSE;
2123         GstPad *srcpad = parse->srcpad;
2124         GST_INFO_OBJECT (aacparse, "aac parser is PUSH MODE.");
2125         /* check NULL */
2126         ret = gst_aac_audio_parse_do_push_seek (parse, srcpad, event);
2127         gst_object_unref (srcpad);
2128         return ret;
2129       }
2130       gst_base_parse_get_upstream_size (parse, &total_file_size);
2131       gst_base_parse_get_index_last_offset (parse, &start_offset);
2132       gst_base_parse_get_index_last_ts (parse, &current_ts);
2133
2134       if (total_file_size > AAC_LARGE_FILE_SIZE) {
2135         gst_base_parse_set_seek_mode (parse, 0);
2136         GST_INFO_OBJECT (aacparse, "larger than big size (2MB).");
2137         goto aac_seek_null_exit;
2138       }
2139
2140       GST_DEBUG ("gst_aac_parse_adts_src_eventfunc GST_EVENT_SEEK enter");
2141
2142       if (total_file_size == 0 || start_offset >= total_file_size) {
2143         GST_ERROR ("last index offset %" G_GINT64_FORMAT
2144             " is larger than file size %" G_GINT64_FORMAT, start_offset,
2145             total_file_size);
2146         break;
2147       }
2148
2149       gst_event_parse_seek (event, NULL, NULL, NULL, NULL, &cur, NULL, NULL);
2150       if (cur <= current_ts) {
2151         GST_INFO ("seek to %" GST_TIME_FORMAT " within index table %"
2152             GST_TIME_FORMAT ". do not make index table", GST_TIME_ARGS (cur),
2153             GST_TIME_ARGS (current_ts));
2154         break;
2155       } else {
2156         GST_INFO ("seek to %" GST_TIME_FORMAT " without index table %"
2157             GST_TIME_FORMAT ". make index table", GST_TIME_ARGS (cur),
2158             GST_TIME_ARGS (current_ts));
2159       }
2160
2161       GST_INFO ("make AAC(ADTS) Index Table. file_size  = %" G_GINT64_FORMAT
2162           " last idx offset=%" G_GINT64_FORMAT ", last idx ts=%"
2163           GST_TIME_FORMAT, total_file_size, start_offset,
2164           GST_TIME_ARGS (current_ts));
2165
2166       base_offset = start_offset;       /* set base by start offset */
2167       second_count = current_ts + GST_SECOND;   /* 1sec */
2168
2169       /************************************/
2170       /* STEP 0: Setting parse information */
2171       /************************************/
2172       aacparse->spf = aacparse->frame_samples;
2173       aacparse->frame_duration = (aacparse->spf * 1000 * 100) / aacparse->sample_rate;  /* duration per frame (msec) */
2174       aacparse->frame_per_sec = (aacparse->sample_rate) / aacparse->spf;        /* frames per second (ea) */
2175
2176       /************************************/
2177       /* STEP 1: MAX_PULL_RANGE_BUF cycle */
2178       /************************************/
2179       while (total_file_size - base_offset >= AAC_MAX_PULL_RANGE_BUF) {
2180         gint64 offset = 0;
2181         GstBuffer *buffer = NULL;
2182         guint8 *buf = NULL;
2183         GstMapInfo map;
2184         GST_INFO ("gst_pad_pull_range %d bytes (from %" G_GINT64_FORMAT
2185             ") use max size", AAC_MAX_PULL_RANGE_BUF, base_offset);
2186         res =
2187             gst_pad_pull_range (parse->sinkpad, base_offset,
2188             base_offset + AAC_MAX_PULL_RANGE_BUF, &buffer);
2189         if (res != GST_FLOW_OK) {
2190           GST_ERROR ("gst_pad_pull_range failed!");
2191           break;
2192         }
2193
2194         gst_buffer_map (buffer, &map, GST_MAP_READ);
2195         buf = map.data;
2196         if (buf == NULL) {
2197           gst_buffer_unmap (buffer, &map);
2198           GST_WARNING ("buffer is NULL in make aac seek table's STEP1");
2199           gst_buffer_unref (buffer);
2200           goto aac_seek_null_exit;
2201         }
2202
2203         while (offset <= AAC_MAX_PULL_RANGE_BUF) {
2204           gint frame_size = 0;
2205
2206           /* make sure the values in the frame header look sane */
2207           frame_size = gst_aac_parse_adts_get_fast_frame_len (buf);
2208
2209           if ((frame_size > 0)
2210               && (frame_size < (AAC_MAX_PULL_RANGE_BUF - offset))) {
2211             if (current_ts > second_count) {    /* 1 sec == xx frames. we make idx per sec */
2212               gst_base_parse_add_index_entry (parse, base_offset + offset, current_ts, TRUE, TRUE);     /* force */
2213               GST_DEBUG ("Adding  index ts=%" GST_TIME_FORMAT " offset %"
2214                   G_GINT64_FORMAT, GST_TIME_ARGS (current_ts),
2215                   base_offset + offset);
2216               second_count += GST_SECOND;       /* 1sec */
2217             }
2218
2219             current_ts += (aacparse->frame_duration * GST_MSECOND) / 100;       /* each frame is (frame_duration) ms */
2220             offset += frame_size;
2221             buf += frame_size;
2222             frame_count++;
2223           } else if (frame_size >= (AAC_MAX_PULL_RANGE_BUF - offset)) {
2224             GST_DEBUG ("we need refill buffer");
2225             break;
2226           } else {
2227             GST_WARNING ("we lost sync");
2228             buf++;
2229             offset++;
2230           }
2231         }                       /* while */
2232
2233         base_offset = base_offset + offset;
2234
2235         gst_buffer_unmap (buffer, &map);
2236         gst_buffer_unref (buffer);
2237       }                         /* end MAX buffer cycle */
2238
2239       /*******************************/
2240       /* STEP 2: Remain Buffer cycle */
2241       /*******************************/
2242       if (total_file_size - base_offset > 0) {
2243         gint64 offset = 0;
2244         GstBuffer *buffer = NULL;
2245         guint8 *buf = NULL;
2246         GstMapInfo map;
2247
2248         GST_INFO ("gst_pad_pull_range %" G_GINT64_FORMAT " bytes (from %"
2249             G_GINT64_FORMAT ") use remain_buf size",
2250             total_file_size - base_offset, base_offset);
2251         res =
2252             gst_pad_pull_range (parse->sinkpad, base_offset, total_file_size,
2253             &buffer);
2254         if (res != GST_FLOW_OK) {
2255           GST_ERROR ("gst_pad_pull_range failed!");
2256           break;
2257         }
2258
2259         gst_buffer_map (buffer, &map, GST_MAP_READ);
2260         buf = map.data;
2261         if (buf == NULL) {
2262           gst_buffer_unmap (buffer, &map);
2263           GST_WARNING ("buffer is NULL in make aac seek table's STEP2");
2264           gst_buffer_unref (buffer);
2265           goto aac_seek_null_exit;
2266         }
2267
2268         while (base_offset + offset < total_file_size) {
2269           gint frame_size = 0;
2270
2271           /* make sure the values in the frame header look sane */
2272           frame_size = gst_aac_parse_adts_get_fast_frame_len (buf);
2273
2274           if ((frame_size > 0)
2275               && (frame_size <= (total_file_size - (base_offset + offset)))) {
2276             if (current_ts > second_count) {    /* 1 sec == xx frames. we make idx per sec */
2277               gst_base_parse_add_index_entry (parse, base_offset + offset, current_ts, TRUE, TRUE);     /* force */
2278               GST_DEBUG ("Adding  index ts=%" GST_TIME_FORMAT " offset %"
2279                   G_GINT64_FORMAT, GST_TIME_ARGS (current_ts),
2280                   base_offset + offset);
2281               second_count += GST_SECOND;       /* 1sec */
2282             }
2283
2284             current_ts += (aacparse->frame_duration * GST_MSECOND) / 100;       /* each frame is (frame_duration) ms */
2285             offset += frame_size;
2286             buf += frame_size;
2287             frame_count++;
2288           } else if (frame_size == 0) {
2289             GST_DEBUG ("Frame size is 0 so, Decoding end..");
2290             break;
2291           } else {
2292             GST_WARNING ("we lost sync");
2293             buf++;
2294             offset++;
2295           }
2296         }                       /* while */
2297
2298         gst_buffer_unmap (buffer, &map);
2299         gst_buffer_unref (buffer);
2300       }
2301       /* end remain_buf buffer cycle */
2302       GST_DEBUG ("gst_aac_parse_adts_src_eventfunc GST_EVENT_SEEK leave");
2303     }
2304       break;
2305
2306     default:
2307       break;
2308   }
2309
2310 aac_seek_null_exit:
2311
2312   /* call baseparse src_event function to handle event */
2313   handled = GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
2314   return handled;
2315 }
2316 #endif //end of #ifdef TIZEN_FEATURE_AACPARSE_MODIFICATION