Debian Codegraph @

gst/audioparsers/gstaacparse.c

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