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