• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @title: aacparse
25  * @short_description: AAC parser
26  * @see_also: #GstAmrParse
27  *
28  * This is an AAC parser which handles both ADIF and ADTS stream formats.
29  *
30  * As ADIF format is not framed, it is not seekable and stream duration cannot
31  * be determined either. However, ADTS format AAC clips can be seeked, and parser
32  * can also estimate playback position and clip duration.
33  *
34  * ## Example launch line
35  * |[
36  * gst-launch-1.0 filesrc location=abc.aac ! aacparse ! faad ! audioresample ! audioconvert ! alsasink
37  * ]|
38  *
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 "gstaudioparserselements.h"
50 #include "gstaacparse.h"
51 
52 
53 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
54     GST_PAD_SRC,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("audio/mpeg, "
57         "framed = (boolean) true, " "mpegversion = (int) { 2, 4 }, "
58         "stream-format = (string) { raw, adts, adif, loas };"));
59 
60 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
61     GST_PAD_SINK,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) { 2, 4 };"));
64 
65 GST_DEBUG_CATEGORY_STATIC (aacparse_debug);
66 #define GST_CAT_DEFAULT aacparse_debug
67 
68 
69 #define ADIF_MAX_SIZE 40        /* Should be enough */
70 #define ADTS_MAX_SIZE 10        /* Should be enough */
71 #define LOAS_MAX_SIZE 3         /* Should be enough */
72 #define RAW_MAX_SIZE  1         /* Correct framing is required */
73 
74 #define ADTS_HEADERS_LENGTH 7UL /* Total byte-length of fixed and variable
75                                    headers prepended during raw to ADTS
76                                    conversion */
77 
78 #define AAC_FRAME_DURATION(parse) (GST_SECOND/parse->frames_per_sec)
79 
80 static const gint loas_sample_rate_table[16] = {
81   96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
82   16000, 12000, 11025, 8000, 7350, 0, 0, 0
83 };
84 
85 static const gint loas_channels_table[16] = {
86   0, 1, 2, 3, 4, 5, 6, 8,
87   0, 0, 0, 7, 8, 0, 8, 0
88 };
89 
90 static gboolean gst_aac_parse_start (GstBaseParse * parse);
91 static gboolean gst_aac_parse_stop (GstBaseParse * parse);
92 
93 static gboolean gst_aac_parse_sink_setcaps (GstBaseParse * parse,
94     GstCaps * caps);
95 static GstCaps *gst_aac_parse_sink_getcaps (GstBaseParse * parse,
96     GstCaps * filter);
97 
98 static GstFlowReturn gst_aac_parse_handle_frame (GstBaseParse * parse,
99     GstBaseParseFrame * frame, gint * skipsize);
100 static GstFlowReturn gst_aac_parse_pre_push_frame (GstBaseParse * parse,
101     GstBaseParseFrame * frame);
102 static gboolean gst_aac_parse_src_event (GstBaseParse * parse,
103     GstEvent * event);
104 
105 static gboolean gst_aac_parse_read_audio_specific_config (GstAacParse *
106     aacparse, GstBitReader * br, gint * object_type, gint * sample_rate,
107     gint * channels, gint * frame_samples);
108 
109 
110 #define gst_aac_parse_parent_class parent_class
111 G_DEFINE_TYPE (GstAacParse, gst_aac_parse, GST_TYPE_BASE_PARSE);
112 #ifdef OHOS_EXT_FUNC
113 /* ohos.ext.func.0026:
114  * The avmuxer need aacparse, so aacparse needs to be registered, and to avoid conflicts with existing code,
115  * aacparse's priority has been reduced to GST_RANK_NONE
116  */
117 GST_ELEMENT_REGISTER_DEFINE (aacparse, "aacparse",
118     GST_RANK_NONE, GST_TYPE_AAC_PARSE);
119 #else
120 GST_ELEMENT_REGISTER_DEFINE (aacparse, "aacparse",
121     GST_RANK_PRIMARY + 1, GST_TYPE_AAC_PARSE);
122 #endif
123 
124 /**
125  * gst_aac_parse_class_init:
126  * @klass: #GstAacParseClass.
127  *
128  */
129 static void
gst_aac_parse_class_init(GstAacParseClass * klass)130 gst_aac_parse_class_init (GstAacParseClass * klass)
131 {
132   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
133   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
134 
135   GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0,
136       "AAC audio stream parser");
137 
138   gst_element_class_add_static_pad_template (element_class, &sink_template);
139   gst_element_class_add_static_pad_template (element_class, &src_template);
140 
141   gst_element_class_set_static_metadata (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->handle_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_handle_frame);
150   parse_class->pre_push_frame =
151       GST_DEBUG_FUNCPTR (gst_aac_parse_pre_push_frame);
152   parse_class->src_event = GST_DEBUG_FUNCPTR (gst_aac_parse_src_event);
153 }
154 
155 
156 /**
157  * gst_aac_parse_init:
158  * @aacparse: #GstAacParse.
159  * @klass: #GstAacParseClass.
160  *
161  */
162 static void
gst_aac_parse_init(GstAacParse * aacparse)163 gst_aac_parse_init (GstAacParse * aacparse)
164 {
165   GST_DEBUG ("initialized");
166   GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (aacparse));
167   GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (aacparse));
168 
169   aacparse->last_parsed_sample_rate = 0;
170   aacparse->last_parsed_channels = 0;
171 }
172 
173 
174 /**
175  * gst_aac_parse_set_src_caps:
176  * @aacparse: #GstAacParse.
177  * @sink_caps: (proposed) caps of sink pad
178  *
179  * Set source pad caps according to current knowledge about the
180  * audio stream.
181  *
182  * Returns: TRUE if caps were successfully set.
183  */
184 static gboolean
gst_aac_parse_set_src_caps(GstAacParse * aacparse,GstCaps * sink_caps)185 gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps)
186 {
187   GstStructure *s;
188   GstCaps *src_caps = NULL, *peercaps;
189   gboolean res = FALSE;
190   const gchar *stream_format;
191   guint8 codec_data[2];
192   guint16 codec_data_data;
193   gint sample_rate_idx;
194 
195   GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps);
196   if (sink_caps)
197     src_caps = gst_caps_copy (sink_caps);
198   else
199     src_caps = gst_caps_new_empty_simple ("audio/mpeg");
200 
201   gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE,
202       "mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL);
203 
204   aacparse->output_header_type = aacparse->header_type;
205   switch (aacparse->header_type) {
206     case DSPAAC_HEADER_NONE:
207       stream_format = "raw";
208       break;
209     case DSPAAC_HEADER_ADTS:
210       stream_format = "adts";
211       break;
212     case DSPAAC_HEADER_ADIF:
213       stream_format = "adif";
214       break;
215     case DSPAAC_HEADER_LOAS:
216       stream_format = "loas";
217       break;
218     default:
219       stream_format = NULL;
220   }
221 
222   /* Generate codec data to be able to set profile/level on the caps.
223    * The codec_data data is according to AudioSpecificConfig,
224    * ISO/IEC 14496-3, 1.6.2.1 */
225   sample_rate_idx =
226       gst_codec_utils_aac_get_index_from_sample_rate (aacparse->sample_rate);
227   if (sample_rate_idx < 0)
228     goto not_a_known_rate;
229   codec_data_data =
230       (aacparse->object_type << 11) |
231       (sample_rate_idx << 7) | (aacparse->channels << 3);
232   GST_WRITE_UINT16_BE (codec_data, codec_data_data);
233   gst_codec_utils_aac_caps_set_level_and_profile (src_caps, codec_data, 2);
234 
235   s = gst_caps_get_structure (src_caps, 0);
236   if (aacparse->sample_rate > 0)
237     gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL);
238   if (aacparse->channels > 0)
239     gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL);
240   if (stream_format)
241     gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL);
242 
243   peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (aacparse), NULL);
244   if (peercaps && !gst_caps_can_intersect (src_caps, peercaps)) {
245     GstCaps *convcaps = gst_caps_copy (src_caps);
246     GstStructure *cs = gst_caps_get_structure (convcaps, 0);
247 
248     GST_DEBUG_OBJECT (aacparse, "Caps do not intersect: parsed %" GST_PTR_FORMAT
249         " and peer %" GST_PTR_FORMAT, src_caps, peercaps);
250 
251     if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
252       GstBuffer *codec_data_buffer = gst_buffer_new_and_alloc (2);
253 
254       gst_buffer_fill (codec_data_buffer, 0, codec_data, 2);
255       gst_structure_set (cs, "stream-format", G_TYPE_STRING, "raw",
256           "codec_data", GST_TYPE_BUFFER, codec_data_buffer, NULL);
257 
258       if (gst_caps_can_intersect (convcaps, peercaps)) {
259         GST_DEBUG_OBJECT (aacparse, "Converting from ADTS to raw");
260         aacparse->output_header_type = DSPAAC_HEADER_NONE;
261         gst_caps_replace (&src_caps, convcaps);
262       }
263 
264       gst_buffer_unref (codec_data_buffer);
265     } else if (aacparse->header_type == DSPAAC_HEADER_NONE) {
266       gst_structure_set (cs, "stream-format", G_TYPE_STRING, "adts", NULL);
267       gst_structure_remove_field (cs, "codec_data");
268 
269       if (gst_caps_can_intersect (convcaps, peercaps)) {
270         GST_DEBUG_OBJECT (aacparse, "Converting from raw to ADTS");
271         aacparse->output_header_type = DSPAAC_HEADER_ADTS;
272         gst_caps_replace (&src_caps, convcaps);
273       }
274     }
275 
276     gst_caps_unref (convcaps);
277   }
278   if (peercaps)
279     gst_caps_unref (peercaps);
280 
281   aacparse->last_parsed_channels = 0;
282   aacparse->last_parsed_sample_rate = 0;
283 
284   GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps);
285 
286   res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps);
287   gst_caps_unref (src_caps);
288   return res;
289 
290 not_a_known_rate:
291   GST_ERROR_OBJECT (aacparse, "Not a known sample rate: %d",
292       aacparse->sample_rate);
293   gst_caps_unref (src_caps);
294   return FALSE;
295 }
296 
297 
298 /**
299  * gst_aac_parse_sink_setcaps:
300  * @sinkpad: GstPad
301  * @caps: GstCaps
302  *
303  * Implementation of "set_sink_caps" vmethod in #GstBaseParse class.
304  *
305  * Returns: TRUE on success.
306  */
307 static gboolean
gst_aac_parse_sink_setcaps(GstBaseParse * parse,GstCaps * caps)308 gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
309 {
310   GstAacParse *aacparse;
311   GstStructure *structure;
312   gchar *caps_str;
313   const GValue *value;
314 
315   aacparse = GST_AAC_PARSE (parse);
316   structure = gst_caps_get_structure (caps, 0);
317   caps_str = gst_caps_to_string (caps);
318 
319   GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str);
320   g_free (caps_str);
321 
322   /* This is needed at least in case of RTP
323    * Parses the codec_data information to get ObjectType,
324    * number of channels and samplerate */
325   value = gst_structure_get_value (structure, "codec_data");
326   if (value) {
327     GstBuffer *buf = gst_value_get_buffer (value);
328 
329     if (buf && gst_buffer_get_size (buf) >= 2) {
330       GstMapInfo map;
331       GstBitReader br;
332 
333       if (!gst_buffer_map (buf, &map, GST_MAP_READ))
334         return FALSE;
335       gst_bit_reader_init (&br, map.data, map.size);
336       gst_aac_parse_read_audio_specific_config (aacparse, &br,
337           &aacparse->object_type, &aacparse->sample_rate, &aacparse->channels,
338           &aacparse->frame_samples);
339 
340       aacparse->header_type = DSPAAC_HEADER_NONE;
341       aacparse->mpegversion = 4;
342       gst_buffer_unmap (buf, &map);
343 
344       GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d, "
345           "samples=%d", aacparse->object_type, aacparse->sample_rate,
346           aacparse->channels, aacparse->frame_samples);
347 
348       /* arrange for metadata and get out of the way */
349       gst_aac_parse_set_src_caps (aacparse, caps);
350       if (aacparse->header_type == aacparse->output_header_type)
351         gst_base_parse_set_passthrough (parse, TRUE);
352 
353       /* input is already correctly framed */
354       gst_base_parse_set_min_frame_size (parse, RAW_MAX_SIZE);
355     } else {
356       return FALSE;
357     }
358 
359     /* caps info overrides */
360     gst_structure_get_int (structure, "rate", &aacparse->sample_rate);
361     gst_structure_get_int (structure, "channels", &aacparse->channels);
362   } else {
363     const gchar *stream_format =
364         gst_structure_get_string (structure, "stream-format");
365 
366     if (g_strcmp0 (stream_format, "raw") == 0) {
367       GST_ERROR_OBJECT (parse, "Need codec_data for raw AAC");
368       return FALSE;
369     } else {
370       aacparse->sample_rate = 0;
371       aacparse->channels = 0;
372       aacparse->header_type = DSPAAC_HEADER_NOT_PARSED;
373       gst_base_parse_set_passthrough (parse, FALSE);
374     }
375   }
376   return TRUE;
377 }
378 
379 
380 /**
381  * gst_aac_parse_adts_get_frame_len:
382  * @data: block of data containing an ADTS header.
383  *
384  * This function calculates ADTS frame length from the given header.
385  *
386  * Returns: size of the ADTS frame.
387  */
388 static inline guint
gst_aac_parse_adts_get_frame_len(const guint8 * data)389 gst_aac_parse_adts_get_frame_len (const guint8 * data)
390 {
391   return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5);
392 }
393 
394 
395 /**
396  * gst_aac_parse_check_adts_frame:
397  * @aacparse: #GstAacParse.
398  * @data: Data to be checked.
399  * @avail: Amount of data passed.
400  * @framesize: If valid ADTS frame was found, this will be set to tell the
401  *             found frame size in bytes.
402  * @needed_data: If frame was not found, this may be set to tell how much
403  *               more data is needed in the next round to detect the frame
404  *               reliably. This may happen when a frame header candidate
405  *               is found but it cannot be guaranteed to be the header without
406  *               peeking the following data.
407  *
408  * Check if the given data contains contains ADTS frame. The algorithm
409  * will examine ADTS frame header and calculate the frame size. Also, another
410  * consecutive ADTS frame header need to be present after the found frame.
411  * Otherwise the data is not considered as a valid ADTS frame. However, this
412  * "extra check" is omitted when EOS has been received. In this case it is
413  * enough when data[0] contains a valid ADTS header.
414  *
415  * This function may set the #needed_data to indicate that a possible frame
416  * candidate has been found, but more data (#needed_data bytes) is needed to
417  * be absolutely sure. When this situation occurs, FALSE will be returned.
418  *
419  * When a valid frame is detected, this function will use
420  * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
421  * to set the needed bytes for next frame.This way next data chunk is already
422  * of correct size.
423  *
424  * Returns: TRUE if the given data contains a valid ADTS header.
425  */
426 static gboolean
gst_aac_parse_check_adts_frame(GstAacParse * aacparse,const guint8 * data,const guint avail,gboolean drain,guint * framesize,guint * needed_data)427 gst_aac_parse_check_adts_frame (GstAacParse * aacparse,
428     const guint8 * data, const guint avail, gboolean drain,
429     guint * framesize, guint * needed_data)
430 {
431   guint crc_size;
432 
433   *needed_data = 0;
434 
435   /* Absolute minimum to perform the ADTS syncword,
436      layer and sampling frequency tests */
437   if (G_UNLIKELY (avail < 3)) {
438     *needed_data = 3;
439     return FALSE;
440   }
441 
442   /* Syncword and layer tests */
443   if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) {
444 
445     /* Sampling frequency test */
446     if (G_UNLIKELY ((data[2] & 0x3C) >> 2 == 15))
447       return FALSE;
448 
449     /* This looks like an ADTS frame header but
450        we need at least 6 bytes to proceed */
451     if (G_UNLIKELY (avail < 6)) {
452       *needed_data = 6;
453       return FALSE;
454     }
455 
456     *framesize = gst_aac_parse_adts_get_frame_len (data);
457 
458     /* If frame has CRC, it needs 2 bytes
459        for it at the end of the header */
460     crc_size = (data[1] & 0x01) ? 0 : 2;
461 
462     /* CRC size test */
463     if (*framesize < 7 + crc_size) {
464       *needed_data = 7 + crc_size;
465       return FALSE;
466     }
467 
468     /* In EOS mode this is enough. No need to examine the data further.
469        We also relax the check when we have sync, on the assumption that
470        if we're not looking at random data, we have a much higher chance
471        to get the correct sync, and this avoids losing two frames when
472        a single bit corruption happens. */
473     if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
474       return TRUE;
475     }
476 
477     if (*framesize + ADTS_MAX_SIZE > avail) {
478       /* We have found a possible frame header candidate, but can't be
479          sure since we don't have enough data to check the next frame */
480       GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
481           *framesize + ADTS_MAX_SIZE, avail);
482       *needed_data = *framesize + ADTS_MAX_SIZE;
483       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
484           *framesize + ADTS_MAX_SIZE);
485       return FALSE;
486     }
487 
488     if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) {
489       guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize));
490 
491       GST_LOG ("ADTS frame found, len: %d bytes", *framesize);
492       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
493           nextlen + ADTS_MAX_SIZE);
494       return TRUE;
495     }
496   }
497   return FALSE;
498 }
499 
500 static gboolean
gst_aac_parse_latm_get_value(GstAacParse * aacparse,GstBitReader * br,guint32 * value)501 gst_aac_parse_latm_get_value (GstAacParse * aacparse, GstBitReader * br,
502     guint32 * value)
503 {
504   guint8 bytes, i, byte;
505 
506   *value = 0;
507   if (!gst_bit_reader_get_bits_uint8 (br, &bytes, 2))
508     return FALSE;
509   for (i = 0; i <= bytes; ++i) {
510     *value <<= 8;
511     if (!gst_bit_reader_get_bits_uint8 (br, &byte, 8))
512       return FALSE;
513     *value += byte;
514   }
515   return TRUE;
516 }
517 
518 static gboolean
gst_aac_parse_get_audio_object_type(GstAacParse * aacparse,GstBitReader * br,guint8 * audio_object_type)519 gst_aac_parse_get_audio_object_type (GstAacParse * aacparse, GstBitReader * br,
520     guint8 * audio_object_type)
521 {
522   if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 5))
523     return FALSE;
524   if (*audio_object_type == 31) {
525     if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 6))
526       return FALSE;
527     *audio_object_type += 32;
528   }
529   GST_LOG_OBJECT (aacparse, "audio object type %u", *audio_object_type);
530   return TRUE;
531 }
532 
533 static gboolean
gst_aac_parse_get_audio_sample_rate(GstAacParse * aacparse,GstBitReader * br,gint * sample_rate)534 gst_aac_parse_get_audio_sample_rate (GstAacParse * aacparse, GstBitReader * br,
535     gint * sample_rate)
536 {
537   guint8 sampling_frequency_index;
538   if (!gst_bit_reader_get_bits_uint8 (br, &sampling_frequency_index, 4))
539     return FALSE;
540   GST_LOG_OBJECT (aacparse, "sampling_frequency_index: %u",
541       sampling_frequency_index);
542   if (sampling_frequency_index == 0xf) {
543     guint32 sampling_rate;
544     if (!gst_bit_reader_get_bits_uint32 (br, &sampling_rate, 24))
545       return FALSE;
546     *sample_rate = sampling_rate;
547   } else {
548     *sample_rate = loas_sample_rate_table[sampling_frequency_index];
549     if (!*sample_rate)
550       return FALSE;
551   }
552   aacparse->last_parsed_sample_rate = *sample_rate;
553   return TRUE;
554 }
555 
556 /* See table 1.13 in ISO/IEC 14496-3 */
557 static gboolean
gst_aac_parse_read_audio_specific_config(GstAacParse * aacparse,GstBitReader * br,gint * object_type,gint * sample_rate,gint * channels,gint * frame_samples)558 gst_aac_parse_read_audio_specific_config (GstAacParse * aacparse,
559     GstBitReader * br, gint * object_type, gint * sample_rate, gint * channels,
560     gint * frame_samples)
561 {
562   guint8 audio_object_type;
563   guint8 G_GNUC_UNUSED extension_audio_object_type;
564   guint8 channel_configuration, extension_channel_configuration;
565   gboolean G_GNUC_UNUSED sbr = FALSE, ps = FALSE;
566 
567   if (!gst_aac_parse_get_audio_object_type (aacparse, br, &audio_object_type))
568     return FALSE;
569   if (object_type)
570     *object_type = audio_object_type;
571 
572   if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
573     return FALSE;
574 
575   if (!gst_bit_reader_get_bits_uint8 (br, &channel_configuration, 4))
576     return FALSE;
577   *channels = loas_channels_table[channel_configuration];
578   GST_LOG_OBJECT (aacparse, "channel_configuration: %d", channel_configuration);
579   if (!*channels)
580     return FALSE;
581 
582   if (audio_object_type == 5 || audio_object_type == 29) {
583     extension_audio_object_type = 5;
584     sbr = TRUE;
585     if (audio_object_type == 29) {
586       ps = TRUE;
587       /* Parametric stereo. If we have a one-channel configuration, we can
588        * override it to stereo */
589       if (*channels == 1)
590         *channels = 2;
591     }
592 
593     GST_LOG_OBJECT (aacparse,
594         "Audio object type 5 or 29, so rereading sampling rate (was %d)...",
595         *sample_rate);
596     if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
597       return FALSE;
598 
599     if (!gst_aac_parse_get_audio_object_type (aacparse, br, &audio_object_type))
600       return FALSE;
601 
602     if (audio_object_type == 22) {
603       /* extension channel configuration */
604       if (!gst_bit_reader_get_bits_uint8 (br, &extension_channel_configuration,
605               4))
606         return FALSE;
607       GST_LOG_OBJECT (aacparse, "extension channel_configuration: %d",
608           extension_channel_configuration);
609       *channels = loas_channels_table[extension_channel_configuration];
610       if (!*channels)
611         return FALSE;
612     }
613   } else {
614     extension_audio_object_type = 0;
615   }
616 
617   GST_INFO_OBJECT (aacparse, "Parsed AudioSpecificConfig: %d Hz, %d channels",
618       *sample_rate, *channels);
619 
620   if (frame_samples && audio_object_type == 23) {
621     guint8 frame_flag;
622     /* Read the Decoder Configuration (GASpecificConfig) if present */
623     /* We only care about the first bit to know what the number of samples
624      * in a frame is */
625     if (!gst_bit_reader_get_bits_uint8 (br, &frame_flag, 1))
626       return FALSE;
627     *frame_samples = frame_flag ? 960 : 1024;
628   }
629 
630   /* There's LOTS of stuff next, but we ignore it for now as we have
631      what we want (sample rate and number of channels */
632   GST_DEBUG_OBJECT (aacparse,
633       "Need more code to parse humongous LOAS data, currently ignored");
634   aacparse->last_parsed_channels = *channels;
635   return TRUE;
636 }
637 
638 
639 static gboolean
gst_aac_parse_read_loas_config(GstAacParse * aacparse,const guint8 * data,guint avail,gint * sample_rate,gint * channels,gint * version)640 gst_aac_parse_read_loas_config (GstAacParse * aacparse, const guint8 * data,
641     guint avail, gint * sample_rate, gint * channels, gint * version)
642 {
643   GstBitReader br;
644   guint8 u8, v, vA;
645 
646   /* No version in the bitstream, but the spec has LOAS in the MPEG-4 section */
647   if (version)
648     *version = 4;
649 
650   gst_bit_reader_init (&br, data, avail);
651 
652   /* skip sync word (11 bits) and size (13 bits) */
653   if (!gst_bit_reader_skip (&br, 11 + 13))
654     return FALSE;
655 
656   /* First bit is "use last config" */
657   if (!gst_bit_reader_get_bits_uint8 (&br, &u8, 1))
658     return FALSE;
659   if (u8) {
660     GST_LOG_OBJECT (aacparse, "Frame uses previous config");
661     if (!aacparse->last_parsed_sample_rate || !aacparse->last_parsed_channels) {
662       GST_DEBUG_OBJECT (aacparse,
663           "No previous config to use. We'll look for more data.");
664       return FALSE;
665     }
666     *sample_rate = aacparse->last_parsed_sample_rate;
667     *channels = aacparse->last_parsed_channels;
668     return TRUE;
669   }
670 
671   GST_DEBUG_OBJECT (aacparse, "Frame contains new config");
672 
673   /* audioMuxVersion */
674   if (!gst_bit_reader_get_bits_uint8 (&br, &v, 1))
675     return FALSE;
676   if (v) {
677     /* audioMuxVersionA */
678     if (!gst_bit_reader_get_bits_uint8 (&br, &vA, 1))
679       return FALSE;
680   } else
681     vA = 0;
682 
683   GST_LOG_OBJECT (aacparse, "v %d, vA %d", v, vA);
684   if (vA == 0) {
685     guint8 same_time, subframes, num_program, prog;
686     if (v == 1) {
687       guint32 value;
688       /* taraBufferFullness */
689       if (!gst_aac_parse_latm_get_value (aacparse, &br, &value))
690         return FALSE;
691     }
692     if (!gst_bit_reader_get_bits_uint8 (&br, &same_time, 1))
693       return FALSE;
694     if (!gst_bit_reader_get_bits_uint8 (&br, &subframes, 6))
695       return FALSE;
696     if (!gst_bit_reader_get_bits_uint8 (&br, &num_program, 4))
697       return FALSE;
698     GST_LOG_OBJECT (aacparse, "same_time %d, subframes %d, num_program %d",
699         same_time, subframes, num_program);
700 
701     for (prog = 0; prog <= num_program; ++prog) {
702       guint8 num_layer, layer;
703       if (!gst_bit_reader_get_bits_uint8 (&br, &num_layer, 3))
704         return FALSE;
705       GST_LOG_OBJECT (aacparse, "Program %d: %d layers", prog, num_layer);
706 
707       for (layer = 0; layer <= num_layer; ++layer) {
708         guint8 use_same_config;
709         if (prog == 0 && layer == 0) {
710           use_same_config = 0;
711         } else {
712           if (!gst_bit_reader_get_bits_uint8 (&br, &use_same_config, 1))
713             return FALSE;
714         }
715         if (!use_same_config) {
716           if (v == 0) {
717             if (!gst_aac_parse_read_audio_specific_config (aacparse, &br, NULL,
718                     sample_rate, channels, NULL))
719               return FALSE;
720           } else {
721             guint32 asc_len;
722             if (!gst_aac_parse_latm_get_value (aacparse, &br, &asc_len))
723               return FALSE;
724             if (!gst_aac_parse_read_audio_specific_config (aacparse, &br, NULL,
725                     sample_rate, channels, NULL))
726               return FALSE;
727             if (!gst_bit_reader_skip (&br, asc_len))
728               return FALSE;
729           }
730         }
731       }
732     }
733     GST_LOG_OBJECT (aacparse, "More data ignored");
734   } else {
735     GST_WARNING_OBJECT (aacparse, "Spec says \"TBD\"...");
736     return FALSE;
737   }
738   return TRUE;
739 }
740 
741 /**
742  * gst_aac_parse_loas_get_frame_len:
743  * @data: block of data containing a LOAS header.
744  *
745  * This function calculates LOAS frame length from the given header.
746  *
747  * Returns: size of the LOAS frame.
748  */
749 static inline guint
gst_aac_parse_loas_get_frame_len(const guint8 * data)750 gst_aac_parse_loas_get_frame_len (const guint8 * data)
751 {
752   return (((data[1] & 0x1f) << 8) | data[2]) + 3;
753 }
754 
755 
756 /**
757  * gst_aac_parse_check_loas_frame:
758  * @aacparse: #GstAacParse.
759  * @data: Data to be checked.
760  * @avail: Amount of data passed.
761  * @framesize: If valid LOAS frame was found, this will be set to tell the
762  *             found frame size in bytes.
763  * @needed_data: If frame was not found, this may be set to tell how much
764  *               more data is needed in the next round to detect the frame
765  *               reliably. This may happen when a frame header candidate
766  *               is found but it cannot be guaranteed to be the header without
767  *               peeking the following data.
768  *
769  * Check if the given data contains contains LOAS frame. The algorithm
770  * will examine LOAS frame header and calculate the frame size. Also, another
771  * consecutive LOAS frame header need to be present after the found frame.
772  * Otherwise the data is not considered as a valid LOAS frame. However, this
773  * "extra check" is omitted when EOS has been received. In this case it is
774  * enough when data[0] contains a valid LOAS header.
775  *
776  * This function may set the #needed_data to indicate that a possible frame
777  * candidate has been found, but more data (#needed_data bytes) is needed to
778  * be absolutely sure. When this situation occurs, FALSE will be returned.
779  *
780  * When a valid frame is detected, this function will use
781  * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
782  * to set the needed bytes for next frame.This way next data chunk is already
783  * of correct size.
784  *
785  * LOAS can have three different formats, if I read the spec correctly. Only
786  * one of them is supported here, as the two samples I have use this one.
787  *
788  * Returns: TRUE if the given data contains a valid LOAS header.
789  */
790 static gboolean
gst_aac_parse_check_loas_frame(GstAacParse * aacparse,const guint8 * data,const guint avail,gboolean drain,guint * framesize,guint * needed_data)791 gst_aac_parse_check_loas_frame (GstAacParse * aacparse,
792     const guint8 * data, const guint avail, gboolean drain,
793     guint * framesize, guint * needed_data)
794 {
795   *needed_data = 0;
796 
797   /* 3 byte header */
798   if (G_UNLIKELY (avail < 3)) {
799     *needed_data = 3;
800     return FALSE;
801   }
802 
803   if ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) {
804     *framesize = gst_aac_parse_loas_get_frame_len (data);
805     GST_DEBUG_OBJECT (aacparse, "Found possible %u byte LOAS frame",
806         *framesize);
807 
808     /* In EOS mode this is enough. No need to examine the data further.
809        We also relax the check when we have sync, on the assumption that
810        if we're not looking at random data, we have a much higher chance
811        to get the correct sync, and this avoids losing two frames when
812        a single bit corruption happens. */
813     if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
814       return TRUE;
815     }
816 
817     if (*framesize + LOAS_MAX_SIZE > avail) {
818       /* We have found a possible frame header candidate, but can't be
819          sure since we don't have enough data to check the next frame */
820       GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
821           *framesize + LOAS_MAX_SIZE, avail);
822       *needed_data = *framesize + LOAS_MAX_SIZE;
823       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
824           *framesize + LOAS_MAX_SIZE);
825       return FALSE;
826     }
827 
828     if ((data[*framesize] == 0x56) && ((data[*framesize + 1] & 0xe0) == 0xe0)) {
829       guint nextlen = gst_aac_parse_loas_get_frame_len (data + (*framesize));
830 
831       GST_LOG ("LOAS frame found, len: %d bytes", *framesize);
832       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
833           nextlen + LOAS_MAX_SIZE);
834       return TRUE;
835     } else {
836       GST_DEBUG_OBJECT (aacparse, "That was a false positive");
837     }
838   }
839   return FALSE;
840 }
841 
842 /* caller ensure sufficient data */
843 static inline void
gst_aac_parse_parse_adts_header(GstAacParse * aacparse,const guint8 * data,gint * rate,gint * channels,gint * object,gint * version)844 gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data,
845     gint * rate, gint * channels, gint * object, gint * version)
846 {
847 
848   if (rate) {
849     gint sr_idx = (data[2] & 0x3c) >> 2;
850 
851     *rate = gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
852   }
853   if (channels) {
854     *channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6);
855     if (*channels == 7)
856       *channels = 8;
857   }
858 
859   if (version)
860     *version = (data[1] & 0x08) ? 2 : 4;
861   if (object)
862     *object = ((data[2] & 0xc0) >> 6) + 1;
863 }
864 
865 /**
866  * gst_aac_parse_detect_stream:
867  * @aacparse: #GstAacParse.
868  * @data: A block of data that needs to be examined for stream characteristics.
869  * @avail: Size of the given datablock.
870  * @framesize: If valid stream was found, this will be set to tell the
871  *             first frame size in bytes.
872  * @skipsize: If valid stream was found, this will be set to tell the first
873  *            audio frame position within the given data.
874  *
875  * Examines the given piece of data and try to detect the format of it. It
876  * checks for "ADIF" header (in the beginning of the clip) and ADTS frame
877  * header. If the stream is detected, TRUE will be returned and #framesize
878  * is set to indicate the found frame size. Additionally, #skipsize might
879  * be set to indicate the number of bytes that need to be skipped, a.k.a. the
880  * position of the frame inside given data chunk.
881  *
882  * Returns: TRUE on success.
883  */
884 static gboolean
gst_aac_parse_detect_stream(GstAacParse * aacparse,const guint8 * data,const guint avail,gboolean drain,guint * framesize,gint * skipsize)885 gst_aac_parse_detect_stream (GstAacParse * aacparse,
886     const guint8 * data, const guint avail, gboolean drain,
887     guint * framesize, gint * skipsize)
888 {
889   gboolean found = FALSE;
890   guint need_data_adts = 0, need_data_loas;
891   guint i = 0;
892 
893   GST_DEBUG_OBJECT (aacparse, "Parsing header data");
894 
895   /* FIXME: No need to check for ADIF if we are not in the beginning of the
896      stream */
897 
898   /* Can we even parse the header? */
899   if (avail < MAX (ADTS_MAX_SIZE, LOAS_MAX_SIZE)) {
900     GST_DEBUG_OBJECT (aacparse, "Not enough data to check");
901     return FALSE;
902   }
903 
904   for (i = 0; i < avail - 4; i++) {
905     if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) ||
906         ((data[i] == 0x56) && ((data[i + 1] & 0xe0) == 0xe0)) ||
907         strncmp ((char *) data + i, "ADIF", 4) == 0) {
908       GST_DEBUG_OBJECT (aacparse, "Found signature at offset %u", i);
909       found = TRUE;
910 
911       if (i) {
912         /* Trick: tell the parent class that we didn't find the frame yet,
913            but make it skip 'i' amount of bytes. Next time we arrive
914            here we have full frame in the beginning of the data. */
915         *skipsize = i;
916         return FALSE;
917       }
918       break;
919     }
920   }
921   if (!found) {
922     if (i)
923       *skipsize = i;
924     return FALSE;
925   }
926 
927   if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain,
928           framesize, &need_data_adts)) {
929     gint rate, channels;
930 
931     GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize);
932 
933     gst_aac_parse_parse_adts_header (aacparse, data, &rate, &channels,
934         &aacparse->object_type, &aacparse->mpegversion);
935 
936     if (!channels || !framesize) {
937       GST_DEBUG_OBJECT (aacparse, "impossible ADTS configuration");
938       return FALSE;
939     }
940 
941     aacparse->header_type = DSPAAC_HEADER_ADTS;
942     gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
943         aacparse->frame_samples, 2, 2);
944 
945     GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d",
946         rate, channels, aacparse->object_type, aacparse->mpegversion);
947 
948     gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
949 
950     return TRUE;
951   }
952 
953   if (gst_aac_parse_check_loas_frame (aacparse, data, avail, drain,
954           framesize, &need_data_loas)) {
955     gint rate = 0, channels = 0;
956 
957     GST_INFO ("LOAS, framesize: %d", *framesize);
958 
959     aacparse->header_type = DSPAAC_HEADER_LOAS;
960 
961     if (!gst_aac_parse_read_loas_config (aacparse, data, avail, &rate,
962             &channels, &aacparse->mpegversion)) {
963       /* This is pretty normal when skipping data at the start of
964        * random stream (MPEG-TS capture for example) */
965       GST_LOG_OBJECT (aacparse, "Error reading LOAS config");
966       return FALSE;
967     }
968 
969     if (rate && channels) {
970       gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
971           aacparse->frame_samples, 2, 2);
972 
973       /* Don't store the sample rate and channels yet -
974        * this is just format detection. */
975       GST_DEBUG ("LOAS: samplerate %d, channels %d, objtype %d, version %d",
976           rate, channels, aacparse->object_type, aacparse->mpegversion);
977     }
978 
979     gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
980 
981     return TRUE;
982   }
983 
984   if (need_data_adts || need_data_loas) {
985     /* This tells the parent class not to skip any data */
986     *skipsize = 0;
987     return FALSE;
988   }
989 
990   if (avail < ADIF_MAX_SIZE)
991     return FALSE;
992 
993   if (memcmp (data + i, "ADIF", 4) == 0) {
994     const guint8 *adif;
995     int skip_size = 0;
996     int bitstream_type;
997     int sr_idx;
998     GstCaps *sinkcaps;
999 
1000     aacparse->header_type = DSPAAC_HEADER_ADIF;
1001     aacparse->mpegversion = 4;
1002 
1003     /* Skip the "ADIF" bytes */
1004     adif = data + i + 4;
1005 
1006     /* copyright string */
1007     if (adif[0] & 0x80)
1008       skip_size += 9;           /* skip 9 bytes */
1009 
1010     bitstream_type = adif[0 + skip_size] & 0x10;
1011     aacparse->bitrate =
1012         ((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) |
1013         ((unsigned int) adif[1 + skip_size] << 11) |
1014         ((unsigned int) adif[2 + skip_size] << 3) |
1015         ((unsigned int) adif[3 + skip_size] & 0xe0);
1016 
1017     /* CBR */
1018     if (bitstream_type == 0) {
1019 #if 0
1020       /* Buffer fullness parsing. Currently not needed... */
1021       guint num_elems = 0;
1022       guint fullness = 0;
1023 
1024       num_elems = (adif[3 + skip_size] & 0x1e);
1025       GST_INFO ("ADIF num_config_elems: %d", num_elems);
1026 
1027       fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) |
1028           ((unsigned int) adif[4 + skip_size] << 11) |
1029           ((unsigned int) adif[5 + skip_size] << 3) |
1030           ((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5);
1031 
1032       GST_INFO ("ADIF buffer fullness: %d", fullness);
1033 #endif
1034       aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) |
1035           ((adif[7 + skip_size] & 0x80) >> 7);
1036       sr_idx = (adif[7 + skip_size] & 0x78) >> 3;
1037     }
1038     /* VBR */
1039     else {
1040       aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3;
1041       sr_idx = ((adif[4 + skip_size] & 0x07) << 1) |
1042           ((adif[5 + skip_size] & 0x80) >> 7);
1043     }
1044 
1045     /* FIXME: This gives totally wrong results. Duration calculation cannot
1046        be based on this */
1047     aacparse->sample_rate =
1048         gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
1049 
1050     /* baseparse is not given any fps,
1051      * so it will give up on timestamps, seeking, etc */
1052 
1053     /* FIXME: Can we assume this? */
1054     aacparse->channels = 2;
1055 
1056     GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d",
1057         aacparse->bitrate, aacparse->sample_rate, aacparse->object_type);
1058 
1059     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512);
1060 
1061     /* arrange for metadata and get out of the way */
1062     sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (aacparse));
1063     gst_aac_parse_set_src_caps (aacparse, sinkcaps);
1064     if (sinkcaps)
1065       gst_caps_unref (sinkcaps);
1066 
1067     /* not syncable, not easily seekable (unless we push data from start */
1068     gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE);
1069     gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE);
1070     gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0);
1071 
1072     *framesize = avail;
1073     return TRUE;
1074   }
1075 
1076   /* This should never happen */
1077   return FALSE;
1078 }
1079 
1080 /**
1081  * gst_aac_parse_get_audio_profile_object_type
1082  * @aacparse: #GstAacParse.
1083  *
1084  * Gets the MPEG-2 profile or the MPEG-4 object type value corresponding to the
1085  * mpegversion and profile of @aacparse's src pad caps, according to the
1086  * values defined by table 1.A.11 in ISO/IEC 14496-3.
1087  *
1088  * Returns: the profile or object type value corresponding to @aacparse's src
1089  * pad caps, if such a value exists; otherwise G_MAXUINT8.
1090  */
1091 static guint8
gst_aac_parse_get_audio_profile_object_type(GstAacParse * aacparse)1092 gst_aac_parse_get_audio_profile_object_type (GstAacParse * aacparse)
1093 {
1094   GstCaps *srccaps;
1095   GstStructure *srcstruct;
1096   const gchar *profile;
1097   guint8 ret;
1098 
1099   srccaps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse));
1100   if (G_UNLIKELY (srccaps == NULL)) {
1101     return G_MAXUINT8;
1102   }
1103 
1104   srcstruct = gst_caps_get_structure (srccaps, 0);
1105   profile = gst_structure_get_string (srcstruct, "profile");
1106   if (G_UNLIKELY (profile == NULL)) {
1107     gst_caps_unref (srccaps);
1108     return G_MAXUINT8;
1109   }
1110 
1111   if (g_strcmp0 (profile, "main") == 0) {
1112     ret = (guint8) 0U;
1113   } else if (g_strcmp0 (profile, "lc") == 0) {
1114     ret = (guint8) 1U;
1115   } else if (g_strcmp0 (profile, "ssr") == 0) {
1116     ret = (guint8) 2U;
1117   } else if (g_strcmp0 (profile, "ltp") == 0) {
1118     if (G_LIKELY (aacparse->mpegversion == 4))
1119       ret = (guint8) 3U;
1120     else
1121       ret = G_MAXUINT8;         /* LTP Object Type allowed only for MPEG-4 */
1122   } else {
1123     ret = G_MAXUINT8;
1124   }
1125 
1126   gst_caps_unref (srccaps);
1127   return ret;
1128 }
1129 
1130 /**
1131  * gst_aac_parse_get_audio_channel_configuration
1132  * @num_channels: number of audio channels.
1133  *
1134  * Gets the Channel Configuration value, as defined by table 1.19 in ISO/IEC
1135  * 14496-3, for a given number of audio channels.
1136  *
1137  * Returns: the Channel Configuration value corresponding to @num_channels, if
1138  * such a value exists; otherwise G_MAXUINT8.
1139  */
1140 static guint8
gst_aac_parse_get_audio_channel_configuration(gint num_channels)1141 gst_aac_parse_get_audio_channel_configuration (gint num_channels)
1142 {
1143   if (num_channels >= 1 && num_channels <= 6)   /* Mono up to & including 5.1 */
1144     return (guint8) num_channels;
1145   else if (num_channels == 8)   /* 7.1 */
1146     return (guint8) 7U;
1147   else
1148     return G_MAXUINT8;
1149 
1150   /* FIXME: Add support for configurations 11, 12 and 14 from
1151    * ISO/IEC 14496-3:2009/PDAM 4 based on the actual channel layout
1152    */
1153 }
1154 
1155 /**
1156  * gst_aac_parse_get_audio_sampling_frequency_index:
1157  * @sample_rate: audio sampling rate.
1158  *
1159  * Gets the Sampling Frequency Index value, as defined by table 1.18 in ISO/IEC
1160  * 14496-3, for a given sampling rate.
1161  *
1162  * Returns: the Sampling Frequency Index value corresponding to @sample_rate,
1163  * if such a value exists; otherwise G_MAXUINT8.
1164  */
1165 static guint8
gst_aac_parse_get_audio_sampling_frequency_index(gint sample_rate)1166 gst_aac_parse_get_audio_sampling_frequency_index (gint sample_rate)
1167 {
1168   switch (sample_rate) {
1169     case 96000:
1170       return 0x0U;
1171     case 88200:
1172       return 0x1U;
1173     case 64000:
1174       return 0x2U;
1175     case 48000:
1176       return 0x3U;
1177     case 44100:
1178       return 0x4U;
1179     case 32000:
1180       return 0x5U;
1181     case 24000:
1182       return 0x6U;
1183     case 22050:
1184       return 0x7U;
1185     case 16000:
1186       return 0x8U;
1187     case 12000:
1188       return 0x9U;
1189     case 11025:
1190       return 0xAU;
1191     case 8000:
1192       return 0xBU;
1193     case 7350:
1194       return 0xCU;
1195     default:
1196       return G_MAXUINT8;
1197   }
1198 }
1199 
1200 /**
1201  * gst_aac_parse_prepend_adts_headers:
1202  * @aacparse: #GstAacParse.
1203  * @frame: raw AAC frame to which ADTS headers shall be prepended.
1204  *
1205  * Prepends ADTS headers to a raw AAC audio frame.
1206  *
1207  * Returns: TRUE if ADTS headers were successfully prepended; FALSE otherwise.
1208  */
1209 static gboolean
gst_aac_parse_prepend_adts_headers(GstAacParse * aacparse,GstBaseParseFrame * frame)1210 gst_aac_parse_prepend_adts_headers (GstAacParse * aacparse,
1211     GstBaseParseFrame * frame)
1212 {
1213   GstMemory *mem;
1214   guint8 *adts_headers;
1215   gsize buf_size;
1216   gsize frame_size;
1217   guint8 id, profile, channel_configuration, sampling_frequency_index;
1218 
1219   id = (aacparse->mpegversion == 4) ? 0x0U : 0x1U;
1220   profile = gst_aac_parse_get_audio_profile_object_type (aacparse);
1221   if (profile == G_MAXUINT8) {
1222     GST_ERROR_OBJECT (aacparse, "Unsupported audio profile or object type");
1223     return FALSE;
1224   }
1225   channel_configuration =
1226       gst_aac_parse_get_audio_channel_configuration (aacparse->channels);
1227   if (channel_configuration == G_MAXUINT8) {
1228     GST_ERROR_OBJECT (aacparse, "Unsupported number of channels");
1229     return FALSE;
1230   }
1231   sampling_frequency_index =
1232       gst_aac_parse_get_audio_sampling_frequency_index (aacparse->sample_rate);
1233   if (sampling_frequency_index == G_MAXUINT8) {
1234     GST_ERROR_OBJECT (aacparse, "Unsupported sampling frequency");
1235     return FALSE;
1236   }
1237 
1238   frame->out_buffer = gst_buffer_copy (frame->buffer);
1239   buf_size = gst_buffer_get_size (frame->out_buffer);
1240   frame_size = buf_size + ADTS_HEADERS_LENGTH;
1241 
1242   if (G_UNLIKELY (frame_size >= 0x4000)) {
1243     GST_ERROR_OBJECT (aacparse, "Frame size is too big for ADTS");
1244     return FALSE;
1245   }
1246 
1247   adts_headers = (guint8 *) g_malloc0 (ADTS_HEADERS_LENGTH);
1248 
1249   /* Note: no error correction bits are added to the resulting ADTS frames */
1250   adts_headers[0] = 0xFFU;
1251   adts_headers[1] = 0xF0U | (id << 3) | 0x1U;
1252   adts_headers[2] = (profile << 6) | (sampling_frequency_index << 2) | 0x2U |
1253       ((channel_configuration & 0x4U) >> 2);
1254   adts_headers[3] = ((channel_configuration & 0x3U) << 6) | 0x30U |
1255       (guint8) (frame_size >> 11);
1256   adts_headers[4] = (guint8) ((frame_size >> 3) & 0x00FF);
1257   adts_headers[5] = (guint8) (((frame_size & 0x0007) << 5) + 0x1FU);
1258   adts_headers[6] = 0xFCU;
1259 
1260   mem = gst_memory_new_wrapped (0, adts_headers, ADTS_HEADERS_LENGTH, 0,
1261       ADTS_HEADERS_LENGTH, adts_headers, g_free);
1262   gst_buffer_prepend_memory (frame->out_buffer, mem);
1263 
1264   return TRUE;
1265 }
1266 
1267 /**
1268  * gst_aac_parse_check_valid_frame:
1269  * @parse: #GstBaseParse.
1270  * @frame: #GstBaseParseFrame.
1271  * @skipsize: How much data parent class should skip in order to find the
1272  *            frame header.
1273  *
1274  * Implementation of "handle_frame" vmethod in #GstBaseParse class.
1275  *
1276  * Also determines frame overhead.
1277  * ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have
1278  * a per-frame header. LOAS has 3 bytes.
1279  *
1280  * We're making a couple of simplifying assumptions:
1281  *
1282  * 1. We count Program Configuration Elements rather than searching for them
1283  *    in the streams to discount them - the overhead is negligible.
1284  *
1285  * 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16
1286  *    bits, which should still not be significant enough to warrant the
1287  *    additional parsing through the headers
1288  *
1289  * Returns: a #GstFlowReturn.
1290  */
1291 static GstFlowReturn
gst_aac_parse_handle_frame(GstBaseParse * parse,GstBaseParseFrame * frame,gint * skipsize)1292 gst_aac_parse_handle_frame (GstBaseParse * parse,
1293     GstBaseParseFrame * frame, gint * skipsize)
1294 {
1295   GstMapInfo map;
1296   GstAacParse *aacparse;
1297   gboolean ret = FALSE;
1298   gboolean lost_sync;
1299   GstBuffer *buffer;
1300   guint framesize;
1301   gint rate = 0, channels = 0;
1302 
1303   aacparse = GST_AAC_PARSE (parse);
1304   buffer = frame->buffer;
1305 
1306   gst_buffer_map (buffer, &map, GST_MAP_READ);
1307 
1308   *skipsize = -1;
1309   lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
1310 
1311   if (aacparse->header_type == DSPAAC_HEADER_ADIF ||
1312       aacparse->header_type == DSPAAC_HEADER_NONE) {
1313     /* There is nothing to parse */
1314     framesize = map.size;
1315     ret = TRUE;
1316 
1317   } else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) {
1318 
1319     ret = gst_aac_parse_detect_stream (aacparse, map.data, map.size,
1320         GST_BASE_PARSE_DRAINING (parse), &framesize, skipsize);
1321 
1322   } else if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1323     guint needed_data = 1024;
1324 
1325     ret = gst_aac_parse_check_adts_frame (aacparse, map.data, map.size,
1326         GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
1327 
1328     if (!ret && needed_data) {
1329       GST_DEBUG ("buffer didn't contain valid frame");
1330       *skipsize = 0;
1331       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1332           needed_data);
1333     }
1334 
1335   } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1336     guint needed_data = 1024;
1337 
1338     ret = gst_aac_parse_check_loas_frame (aacparse, map.data,
1339         map.size, GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
1340 
1341     if (!ret && needed_data) {
1342       GST_DEBUG ("buffer didn't contain valid frame");
1343       *skipsize = 0;
1344       gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1345           needed_data);
1346     }
1347 
1348   } else {
1349     GST_DEBUG ("buffer didn't contain valid frame");
1350     gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1351         ADTS_MAX_SIZE);
1352   }
1353 
1354   if (G_UNLIKELY (!ret))
1355     goto exit;
1356 
1357   if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1358     /* see above */
1359     frame->overhead = 7;
1360 
1361     gst_aac_parse_parse_adts_header (aacparse, map.data,
1362         &rate, &channels, NULL, NULL);
1363 
1364     GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels);
1365 
1366     if (G_UNLIKELY (rate != aacparse->sample_rate
1367             || channels != aacparse->channels)) {
1368       aacparse->sample_rate = rate;
1369       aacparse->channels = channels;
1370 
1371       if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1372         /* If linking fails, we need to return appropriate error */
1373         ret = GST_FLOW_NOT_LINKED;
1374       }
1375 
1376       gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1377           aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1378     }
1379   } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1380     gboolean setcaps = FALSE;
1381 
1382     /* see above */
1383     frame->overhead = 3;
1384 
1385     if (!gst_aac_parse_read_loas_config (aacparse, map.data, map.size, &rate,
1386             &channels, NULL) || !rate || !channels) {
1387       /* This is pretty normal when skipping data at the start of
1388        * random stream (MPEG-TS capture for example) */
1389       GST_DEBUG_OBJECT (aacparse, "Error reading LOAS config. Skipping.");
1390       /* Since we don't fully parse the LOAS config, we don't know for sure
1391        * how much to skip. Just skip 1 to end up to the next marker and
1392        * resume parsing from there */
1393       *skipsize = 1;
1394       goto exit;
1395     }
1396 
1397     if (G_UNLIKELY (rate != aacparse->sample_rate
1398             || channels != aacparse->channels)) {
1399       aacparse->sample_rate = rate;
1400       aacparse->channels = channels;
1401       setcaps = TRUE;
1402       GST_INFO_OBJECT (aacparse, "New LOAS config: %d Hz, %d channels", rate,
1403           channels);
1404     }
1405 
1406     /* We want to set caps both at start, and when rate/channels change.
1407        Since only some LOAS frames have that info, we may receive frames
1408        before knowing about rate/channels. */
1409     if (setcaps
1410         || !gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse))) {
1411       if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1412         /* If linking fails, we need to return appropriate error */
1413         ret = GST_FLOW_NOT_LINKED;
1414       }
1415 
1416       gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1417           aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1418     }
1419   }
1420 
1421   if (aacparse->header_type == DSPAAC_HEADER_NONE
1422       && aacparse->output_header_type == DSPAAC_HEADER_ADTS) {
1423     if (!gst_aac_parse_prepend_adts_headers (aacparse, frame)) {
1424       GST_ERROR_OBJECT (aacparse, "Failed to prepend ADTS headers to frame");
1425       ret = GST_FLOW_ERROR;
1426     }
1427   }
1428 
1429 exit:
1430   gst_buffer_unmap (buffer, &map);
1431 
1432   if (ret) {
1433     /* found, skip if needed */
1434     if (*skipsize > 0)
1435       return GST_FLOW_OK;
1436     *skipsize = 0;
1437   } else {
1438     if (*skipsize < 0)
1439       *skipsize = 1;
1440   }
1441 
1442   if (ret && framesize <= map.size) {
1443     return gst_base_parse_finish_frame (parse, frame, framesize);
1444   }
1445 
1446   return GST_FLOW_OK;
1447 }
1448 
1449 static GstFlowReturn
gst_aac_parse_pre_push_frame(GstBaseParse * parse,GstBaseParseFrame * frame)1450 gst_aac_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
1451 {
1452   GstAacParse *aacparse = GST_AAC_PARSE (parse);
1453 
1454   if (!aacparse->sent_codec_tag) {
1455     GstTagList *taglist;
1456     GstCaps *caps;
1457 
1458     /* codec tag */
1459     caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
1460     if (caps == NULL) {
1461       if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
1462         GST_INFO_OBJECT (parse, "Src pad is flushing");
1463         return GST_FLOW_FLUSHING;
1464       } else {
1465         GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
1466         return GST_FLOW_NOT_NEGOTIATED;
1467       }
1468     }
1469 
1470     taglist = gst_tag_list_new_empty ();
1471     gst_pb_utils_add_codec_description_to_tag_list (taglist,
1472         GST_TAG_AUDIO_CODEC, caps);
1473     gst_caps_unref (caps);
1474 
1475     gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
1476     gst_tag_list_unref (taglist);
1477 
1478     /* also signals the end of first-frame processing */
1479     aacparse->sent_codec_tag = TRUE;
1480   }
1481 
1482   /* As a special case, we can remove the ADTS framing and output raw AAC. */
1483   if (aacparse->header_type == DSPAAC_HEADER_ADTS
1484       && aacparse->output_header_type == DSPAAC_HEADER_NONE) {
1485     guint header_size;
1486     GstMapInfo map;
1487     frame->out_buffer = gst_buffer_make_writable (frame->buffer);
1488     frame->buffer = NULL;
1489     gst_buffer_map (frame->out_buffer, &map, GST_MAP_READ);
1490     header_size = (map.data[1] & 1) ? 7 : 9;    /* optional CRC */
1491     gst_buffer_unmap (frame->out_buffer, &map);
1492     gst_buffer_resize (frame->out_buffer, header_size,
1493         gst_buffer_get_size (frame->out_buffer) - header_size);
1494   }
1495 
1496   frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
1497 
1498   return GST_FLOW_OK;
1499 }
1500 
1501 
1502 /**
1503  * gst_aac_parse_start:
1504  * @parse: #GstBaseParse.
1505  *
1506  * Implementation of "start" vmethod in #GstBaseParse class.
1507  *
1508  * Returns: TRUE if startup succeeded.
1509  */
1510 static gboolean
gst_aac_parse_start(GstBaseParse * parse)1511 gst_aac_parse_start (GstBaseParse * parse)
1512 {
1513   GstAacParse *aacparse;
1514 
1515   aacparse = GST_AAC_PARSE (parse);
1516   GST_DEBUG ("start");
1517   aacparse->frame_samples = 1024;
1518   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), ADTS_MAX_SIZE);
1519   aacparse->sent_codec_tag = FALSE;
1520   aacparse->last_parsed_channels = 0;
1521   aacparse->last_parsed_sample_rate = 0;
1522   aacparse->object_type = 0;
1523   aacparse->bitrate = 0;
1524   aacparse->header_type = DSPAAC_HEADER_NOT_PARSED;
1525   aacparse->output_header_type = DSPAAC_HEADER_NOT_PARSED;
1526   aacparse->channels = 0;
1527   aacparse->sample_rate = 0;
1528   return TRUE;
1529 }
1530 
1531 
1532 /**
1533  * gst_aac_parse_stop:
1534  * @parse: #GstBaseParse.
1535  *
1536  * Implementation of "stop" vmethod in #GstBaseParse class.
1537  *
1538  * Returns: TRUE is stopping succeeded.
1539  */
1540 static gboolean
gst_aac_parse_stop(GstBaseParse * parse)1541 gst_aac_parse_stop (GstBaseParse * parse)
1542 {
1543   GST_DEBUG ("stop");
1544   return TRUE;
1545 }
1546 
1547 static void
remove_fields(GstCaps * caps)1548 remove_fields (GstCaps * caps)
1549 {
1550   guint i, n;
1551 
1552   n = gst_caps_get_size (caps);
1553   for (i = 0; i < n; i++) {
1554     GstStructure *s = gst_caps_get_structure (caps, i);
1555 
1556     gst_structure_remove_field (s, "framed");
1557   }
1558 }
1559 
1560 static void
add_conversion_fields(GstCaps * caps)1561 add_conversion_fields (GstCaps * caps)
1562 {
1563   guint i, n;
1564 
1565   n = gst_caps_get_size (caps);
1566   for (i = 0; i < n; i++) {
1567     GstStructure *s = gst_caps_get_structure (caps, i);
1568 
1569     if (gst_structure_has_field (s, "stream-format")) {
1570       const GValue *v = gst_structure_get_value (s, "stream-format");
1571 
1572       if (G_VALUE_HOLDS_STRING (v)) {
1573         const gchar *str = g_value_get_string (v);
1574 
1575         if (strcmp (str, "adts") == 0 || strcmp (str, "raw") == 0) {
1576           GValue va = G_VALUE_INIT;
1577           GValue vs = G_VALUE_INIT;
1578 
1579           g_value_init (&va, GST_TYPE_LIST);
1580           g_value_init (&vs, G_TYPE_STRING);
1581           g_value_set_string (&vs, "adts");
1582           gst_value_list_append_value (&va, &vs);
1583           g_value_set_string (&vs, "raw");
1584           gst_value_list_append_value (&va, &vs);
1585           gst_structure_set_value (s, "stream-format", &va);
1586           g_value_unset (&va);
1587           g_value_unset (&vs);
1588         }
1589       } else if (GST_VALUE_HOLDS_LIST (v)) {
1590         gboolean contains_raw = FALSE;
1591         gboolean contains_adts = FALSE;
1592         guint m = gst_value_list_get_size (v), j;
1593 
1594         for (j = 0; j < m; j++) {
1595           const GValue *ve = gst_value_list_get_value (v, j);
1596           const gchar *str;
1597 
1598           if (G_VALUE_HOLDS_STRING (ve) && (str = g_value_get_string (ve))) {
1599             if (strcmp (str, "adts") == 0)
1600               contains_adts = TRUE;
1601             else if (strcmp (str, "raw") == 0)
1602               contains_raw = TRUE;
1603           }
1604         }
1605 
1606         if (contains_adts || contains_raw) {
1607           GValue va = G_VALUE_INIT;
1608           GValue vs = G_VALUE_INIT;
1609 
1610           g_value_init (&va, GST_TYPE_LIST);
1611           g_value_init (&vs, G_TYPE_STRING);
1612           g_value_copy (v, &va);
1613 
1614           if (!contains_raw) {
1615             g_value_set_string (&vs, "raw");
1616             gst_value_list_append_value (&va, &vs);
1617           }
1618           if (!contains_adts) {
1619             g_value_set_string (&vs, "adts");
1620             gst_value_list_append_value (&va, &vs);
1621           }
1622 
1623           gst_structure_set_value (s, "stream-format", &va);
1624 
1625           g_value_unset (&vs);
1626           g_value_unset (&va);
1627         }
1628       }
1629     }
1630   }
1631 }
1632 
1633 static GstCaps *
gst_aac_parse_sink_getcaps(GstBaseParse * parse,GstCaps * filter)1634 gst_aac_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter)
1635 {
1636   GstCaps *peercaps, *templ;
1637   GstCaps *res;
1638 
1639   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1640 
1641   if (filter) {
1642     GstCaps *fcopy = gst_caps_copy (filter);
1643     /* Remove the fields we convert */
1644     remove_fields (fcopy);
1645     add_conversion_fields (fcopy);
1646     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
1647     gst_caps_unref (fcopy);
1648   } else
1649     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
1650 
1651   if (peercaps) {
1652     peercaps = gst_caps_make_writable (peercaps);
1653     /* Remove the fields we convert */
1654     remove_fields (peercaps);
1655     add_conversion_fields (peercaps);
1656 
1657     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1658     gst_caps_unref (peercaps);
1659     gst_caps_unref (templ);
1660   } else {
1661     res = templ;
1662   }
1663 
1664   if (filter) {
1665     GstCaps *intersection;
1666 
1667     intersection =
1668         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1669     gst_caps_unref (res);
1670     res = intersection;
1671   }
1672 
1673   return res;
1674 }
1675 
1676 static gboolean
gst_aac_parse_src_event(GstBaseParse * parse,GstEvent * event)1677 gst_aac_parse_src_event (GstBaseParse * parse, GstEvent * event)
1678 {
1679   GstAacParse *aacparse = GST_AAC_PARSE (parse);
1680 
1681   if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1682     aacparse->last_parsed_channels = 0;
1683     aacparse->last_parsed_sample_rate = 0;
1684   }
1685 
1686   return GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
1687 }
1688