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