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