1 /* GStreamer AAC parser plugin
2 * Copyright (C) 2008 Nokia Corporation. All rights reserved.
4 * Contact: Stefan Kost <stefan.kost@nokia.com>
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.
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.
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.
23 * SECTION:element-aacparse
24 * @short_description: AAC parser
25 * @see_also: #GstAmrParse
27 * This is an AAC parser which handles both ADIF and ADTS stream formats.
29 * As ADIF format is not framed, it is not seekable and stream duration cannot
30 * be determined either. However, ADTS format AAC clips can be seeked, and parser
31 * can also estimate playback position and clip duration.
34 * <title>Example launch line</title>
36 * gst-launch-1.0 filesrc location=abc.aac ! aacparse ! faad ! audioresample ! audioconvert ! alsasink
47 #include <gst/base/gstbitreader.h>
48 #include <gst/pbutils/codec-utils.h>
49 #include "gstaacparse.h"
52 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
55 GST_STATIC_CAPS ("audio/mpeg, "
56 "framed = (boolean) true, " "mpegversion = (int) { 2, 4 }, "
57 "stream-format = (string) { raw, adts, adif, loas };"));
59 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
62 GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) { 2, 4 };"));
64 GST_DEBUG_CATEGORY_STATIC (aacparse_debug);
65 #define GST_CAT_DEFAULT aacparse_debug
68 #define ADIF_MAX_SIZE 40 /* Should be enough */
69 #define ADTS_MAX_SIZE 10 /* Should be enough */
70 #define LOAS_MAX_SIZE 3 /* Should be enough */
73 #define AAC_FRAME_DURATION(parse) (GST_SECOND/parse->frames_per_sec)
75 static const gint loas_sample_rate_table[32] = {
76 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
77 16000, 12000, 11025, 8000, 7350, 0, 0, 0
80 static const gint loas_channels_table[32] = {
81 0, 1, 2, 3, 4, 5, 6, 8,
82 0, 0, 0, 0, 0, 0, 0, 0
85 static gboolean gst_aac_parse_start (GstBaseParse * parse);
86 static gboolean gst_aac_parse_stop (GstBaseParse * parse);
88 static gboolean gst_aac_parse_sink_setcaps (GstBaseParse * parse,
90 static GstCaps *gst_aac_parse_sink_getcaps (GstBaseParse * parse,
93 static GstFlowReturn gst_aac_parse_handle_frame (GstBaseParse * parse,
94 GstBaseParseFrame * frame, gint * skipsize);
95 static GstFlowReturn gst_aac_parse_pre_push_frame (GstBaseParse * parse,
96 GstBaseParseFrame * frame);
98 G_DEFINE_TYPE (GstAacParse, gst_aac_parse, GST_TYPE_BASE_PARSE);
101 * gst_aac_parse_class_init:
102 * @klass: #GstAacParseClass.
106 gst_aac_parse_class_init (GstAacParseClass * klass)
108 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
109 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
111 GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0,
112 "AAC audio stream parser");
114 gst_element_class_add_pad_template (element_class,
115 gst_static_pad_template_get (&sink_template));
116 gst_element_class_add_pad_template (element_class,
117 gst_static_pad_template_get (&src_template));
119 gst_element_class_set_static_metadata (element_class,
120 "AAC audio stream parser", "Codec/Parser/Audio",
121 "Advanced Audio Coding parser", "Stefan Kost <stefan.kost@nokia.com>");
123 parse_class->start = GST_DEBUG_FUNCPTR (gst_aac_parse_start);
124 parse_class->stop = GST_DEBUG_FUNCPTR (gst_aac_parse_stop);
125 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_setcaps);
126 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_getcaps);
127 parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_handle_frame);
128 parse_class->pre_push_frame =
129 GST_DEBUG_FUNCPTR (gst_aac_parse_pre_push_frame);
134 * gst_aac_parse_init:
135 * @aacparse: #GstAacParse.
136 * @klass: #GstAacParseClass.
140 gst_aac_parse_init (GstAacParse * aacparse)
142 GST_DEBUG ("initialized");
147 * gst_aac_parse_set_src_caps:
148 * @aacparse: #GstAacParse.
149 * @sink_caps: (proposed) caps of sink pad
151 * Set source pad caps according to current knowledge about the
154 * Returns: TRUE if caps were successfully set.
157 gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps)
160 GstCaps *src_caps = NULL, *allowed;
161 gboolean res = FALSE;
162 const gchar *stream_format;
163 GstBuffer *codec_data;
164 guint16 codec_data_data;
166 GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps);
168 src_caps = gst_caps_copy (sink_caps);
170 src_caps = gst_caps_new_empty_simple ("audio/mpeg");
172 gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE,
173 "mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL);
175 aacparse->output_header_type = aacparse->header_type;
176 switch (aacparse->header_type) {
177 case DSPAAC_HEADER_NONE:
178 stream_format = "raw";
180 case DSPAAC_HEADER_ADTS:
181 stream_format = "adts";
183 case DSPAAC_HEADER_ADIF:
184 stream_format = "adif";
186 case DSPAAC_HEADER_LOAS:
187 stream_format = "loas";
190 stream_format = NULL;
193 s = gst_caps_get_structure (src_caps, 0);
194 if (aacparse->sample_rate > 0)
195 gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL);
196 if (aacparse->channels > 0)
197 gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL);
199 gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL);
201 allowed = gst_pad_get_allowed_caps (GST_BASE_PARSE (aacparse)->srcpad);
202 if (!gst_caps_can_intersect (src_caps, allowed)) {
203 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
204 "Caps can not intersect");
205 if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
206 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
207 "Input is ADTS, trying raw");
208 gst_caps_set_simple (src_caps, "stream-format", G_TYPE_STRING, "raw",
210 if (gst_caps_can_intersect (src_caps, allowed)) {
215 gst_codec_utils_aac_get_index_from_sample_rate
216 (aacparse->sample_rate);
218 goto not_a_known_rate;
220 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
221 "Caps can intersect, we will drop the ADTS layer");
222 aacparse->output_header_type = DSPAAC_HEADER_NONE;
224 /* The codec_data data is according to AudioSpecificConfig,
225 ISO/IEC 14496-3, 1.6.2.1 */
226 codec_data = gst_buffer_new_and_alloc (2);
227 gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
229 (aacparse->object_type << 11) |
230 (idx << 7) | (aacparse->channels << 3);
231 GST_WRITE_UINT16_BE (map.data, codec_data_data);
232 gst_buffer_unmap (codec_data, &map);
233 gst_caps_set_simple (src_caps, "codec_data", GST_TYPE_BUFFER,
238 gst_caps_unref (allowed);
240 GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps);
242 res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps);
243 gst_caps_unref (src_caps);
247 gst_caps_unref (allowed);
248 gst_caps_unref (src_caps);
254 * gst_aac_parse_sink_setcaps:
258 * Implementation of "set_sink_caps" vmethod in #GstBaseParse class.
260 * Returns: TRUE on success.
263 gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
265 GstAacParse *aacparse;
266 GstStructure *structure;
270 aacparse = GST_AAC_PARSE (parse);
271 structure = gst_caps_get_structure (caps, 0);
272 caps_str = gst_caps_to_string (caps);
274 GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str);
277 /* This is needed at least in case of RTP
278 * Parses the codec_data information to get ObjectType,
279 * number of channels and samplerate */
280 value = gst_structure_get_value (structure, "codec_data");
282 GstBuffer *buf = gst_value_get_buffer (value);
288 gst_buffer_map (buf, &map, GST_MAP_READ);
290 sr_idx = ((map.data[0] & 0x07) << 1) | ((map.data[1] & 0x80) >> 7);
291 aacparse->object_type = (map.data[0] & 0xf8) >> 3;
292 aacparse->sample_rate =
293 gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
294 aacparse->channels = (map.data[1] & 0x78) >> 3;
295 aacparse->header_type = DSPAAC_HEADER_NONE;
296 aacparse->mpegversion = 4;
297 aacparse->frame_samples = (map.data[1] & 4) ? 960 : 1024;
298 gst_buffer_unmap (buf, &map);
300 GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d, "
301 "samples=%d", aacparse->object_type, aacparse->sample_rate,
302 aacparse->channels, aacparse->frame_samples);
304 /* arrange for metadata and get out of the way */
305 gst_aac_parse_set_src_caps (aacparse, caps);
306 gst_base_parse_set_passthrough (parse, TRUE);
310 /* caps info overrides */
311 gst_structure_get_int (structure, "rate", &aacparse->sample_rate);
312 gst_structure_get_int (structure, "channels", &aacparse->channels);
314 aacparse->sample_rate = 0;
315 aacparse->channels = 0;
316 aacparse->header_type = DSPAAC_HEADER_NOT_PARSED;
317 gst_base_parse_set_passthrough (parse, FALSE);
325 * gst_aac_parse_adts_get_frame_len:
326 * @data: block of data containing an ADTS header.
328 * This function calculates ADTS frame length from the given header.
330 * Returns: size of the ADTS frame.
333 gst_aac_parse_adts_get_frame_len (const guint8 * data)
335 return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5);
340 * gst_aac_parse_check_adts_frame:
341 * @aacparse: #GstAacParse.
342 * @data: Data to be checked.
343 * @avail: Amount of data passed.
344 * @framesize: If valid ADTS frame was found, this will be set to tell the
345 * found frame size in bytes.
346 * @needed_data: If frame was not found, this may be set to tell how much
347 * more data is needed in the next round to detect the frame
348 * reliably. This may happen when a frame header candidate
349 * is found but it cannot be guaranteed to be the header without
350 * peeking the following data.
352 * Check if the given data contains contains ADTS frame. The algorithm
353 * will examine ADTS frame header and calculate the frame size. Also, another
354 * consecutive ADTS frame header need to be present after the found frame.
355 * Otherwise the data is not considered as a valid ADTS frame. However, this
356 * "extra check" is omitted when EOS has been received. In this case it is
357 * enough when data[0] contains a valid ADTS header.
359 * This function may set the #needed_data to indicate that a possible frame
360 * candidate has been found, but more data (#needed_data bytes) is needed to
361 * be absolutely sure. When this situation occurs, FALSE will be returned.
363 * When a valid frame is detected, this function will use
364 * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
365 * to set the needed bytes for next frame.This way next data chunk is already
368 * Returns: TRUE if the given data contains a valid ADTS header.
371 gst_aac_parse_check_adts_frame (GstAacParse * aacparse,
372 const guint8 * data, const guint avail, gboolean drain,
373 guint * framesize, guint * needed_data)
377 if (G_UNLIKELY (avail < 2))
380 if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) {
381 *framesize = gst_aac_parse_adts_get_frame_len (data);
383 /* In EOS mode this is enough. No need to examine the data further.
384 We also relax the check when we have sync, on the assumption that
385 if we're not looking at random data, we have a much higher chance
386 to get the correct sync, and this avoids losing two frames when
387 a single bit corruption happens. */
388 if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
392 if (*framesize + ADTS_MAX_SIZE > avail) {
393 /* We have found a possible frame header candidate, but can't be
394 sure since we don't have enough data to check the next frame */
395 GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
396 *framesize + ADTS_MAX_SIZE, avail);
397 *needed_data = *framesize + ADTS_MAX_SIZE;
398 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
399 *framesize + ADTS_MAX_SIZE);
403 if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) {
404 guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize));
406 GST_LOG ("ADTS frame found, len: %d bytes", *framesize);
407 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
408 nextlen + ADTS_MAX_SIZE);
416 gst_aac_parse_latm_get_value (GstAacParse * aacparse, GstBitReader * br,
419 guint8 bytes, i, byte;
422 if (!gst_bit_reader_get_bits_uint8 (br, &bytes, 2))
424 for (i = 0; i < bytes; ++i) {
426 if (!gst_bit_reader_get_bits_uint8 (br, &byte, 8))
434 gst_aac_parse_get_audio_object_type (GstAacParse * aacparse, GstBitReader * br,
435 guint8 * audio_object_type)
437 if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 5))
439 if (*audio_object_type == 31) {
440 if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 6))
442 *audio_object_type += 32;
444 GST_LOG_OBJECT (aacparse, "audio object type %u", *audio_object_type);
449 gst_aac_parse_get_audio_sample_rate (GstAacParse * aacparse, GstBitReader * br,
452 guint8 sampling_frequency_index;
453 if (!gst_bit_reader_get_bits_uint8 (br, &sampling_frequency_index, 4))
455 GST_LOG_OBJECT (aacparse, "sampling_frequency_index: %u",
456 sampling_frequency_index);
457 if (sampling_frequency_index == 0xf) {
458 guint32 sampling_rate;
459 if (!gst_bit_reader_get_bits_uint32 (br, &sampling_rate, 24))
461 *sample_rate = sampling_rate;
463 *sample_rate = loas_sample_rate_table[sampling_frequency_index];
470 /* See table 1.13 in ISO/IEC 14496-3 */
472 gst_aac_parse_read_loas_audio_specific_config (GstAacParse * aacparse,
473 GstBitReader * br, gint * sample_rate, gint * channels, guint32 * bits)
475 guint8 audio_object_type, channel_configuration;
477 if (!gst_aac_parse_get_audio_object_type (aacparse, br, &audio_object_type))
480 if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
483 if (!gst_bit_reader_get_bits_uint8 (br, &channel_configuration, 4))
485 GST_LOG_OBJECT (aacparse, "channel_configuration: %d", channel_configuration);
486 *channels = loas_channels_table[channel_configuration];
490 if (audio_object_type == 5) {
491 GST_LOG_OBJECT (aacparse,
492 "Audio object type 5, so rereading sampling rate...");
493 if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
497 GST_INFO_OBJECT (aacparse, "Found LOAS config: %d Hz, %d channels",
498 *sample_rate, *channels);
500 /* There's LOTS of stuff next, but we ignore it for now as we have
501 what we want (sample rate and number of channels */
502 GST_DEBUG_OBJECT (aacparse,
503 "Need more code to parse humongous LOAS data, currently ignored");
511 gst_aac_parse_read_loas_config (GstAacParse * aacparse, const guint8 * data,
512 guint avail, gint * sample_rate, gint * channels, gint * version)
517 /* No version in the bitstream, but the spec has LOAS in the MPEG-4 section */
521 gst_bit_reader_init (&br, data, avail);
523 /* skip sync word (11 bits) and size (13 bits) */
524 if (!gst_bit_reader_skip (&br, 11 + 13))
527 /* First bit is "use last config" */
528 if (!gst_bit_reader_get_bits_uint8 (&br, &u8, 1))
531 GST_DEBUG_OBJECT (aacparse, "Frame uses previous config");
532 if (!aacparse->sample_rate || !aacparse->channels) {
533 GST_WARNING_OBJECT (aacparse, "No previous config to use");
535 *sample_rate = aacparse->sample_rate;
536 *channels = aacparse->channels;
540 GST_DEBUG_OBJECT (aacparse, "Frame contains new config");
542 if (!gst_bit_reader_get_bits_uint8 (&br, &v, 1))
545 if (!gst_bit_reader_get_bits_uint8 (&br, &vA, 1))
550 GST_LOG_OBJECT (aacparse, "v %d, vA %d", v, vA);
552 guint8 same_time, subframes, num_program, prog;
555 if (!gst_aac_parse_latm_get_value (aacparse, &br, &value))
558 if (!gst_bit_reader_get_bits_uint8 (&br, &same_time, 1))
560 if (!gst_bit_reader_get_bits_uint8 (&br, &subframes, 6))
562 if (!gst_bit_reader_get_bits_uint8 (&br, &num_program, 4))
564 GST_LOG_OBJECT (aacparse, "same_time %d, subframes %d, num_program %d",
565 same_time, subframes, num_program);
567 for (prog = 0; prog <= num_program; ++prog) {
568 guint8 num_layer, layer;
569 if (!gst_bit_reader_get_bits_uint8 (&br, &num_layer, 3))
571 GST_LOG_OBJECT (aacparse, "Program %d: %d layers", prog, num_layer);
573 for (layer = 0; layer <= num_layer; ++layer) {
574 guint8 use_same_config;
575 if (prog == 0 && layer == 0) {
578 if (!gst_bit_reader_get_bits_uint8 (&br, &use_same_config, 1))
581 if (!use_same_config) {
583 if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
584 sample_rate, channels, NULL))
587 guint32 bits, asc_len;
588 if (!gst_aac_parse_latm_get_value (aacparse, &br, &asc_len))
590 if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
591 sample_rate, channels, &bits))
594 if (!gst_bit_reader_skip (&br, asc_len))
600 GST_LOG_OBJECT (aacparse, "More data ignored");
602 GST_WARNING_OBJECT (aacparse, "Spec says \"TBD\"...");
608 * gst_aac_parse_loas_get_frame_len:
609 * @data: block of data containing a LOAS header.
611 * This function calculates LOAS frame length from the given header.
613 * Returns: size of the LOAS frame.
616 gst_aac_parse_loas_get_frame_len (const guint8 * data)
618 return (((data[1] & 0x1f) << 8) | data[2]) + 3;
623 * gst_aac_parse_check_loas_frame:
624 * @aacparse: #GstAacParse.
625 * @data: Data to be checked.
626 * @avail: Amount of data passed.
627 * @framesize: If valid LOAS frame was found, this will be set to tell the
628 * found frame size in bytes.
629 * @needed_data: If frame was not found, this may be set to tell how much
630 * more data is needed in the next round to detect the frame
631 * reliably. This may happen when a frame header candidate
632 * is found but it cannot be guaranteed to be the header without
633 * peeking the following data.
635 * Check if the given data contains contains LOAS frame. The algorithm
636 * will examine LOAS frame header and calculate the frame size. Also, another
637 * consecutive LOAS frame header need to be present after the found frame.
638 * Otherwise the data is not considered as a valid LOAS frame. However, this
639 * "extra check" is omitted when EOS has been received. In this case it is
640 * enough when data[0] contains a valid LOAS header.
642 * This function may set the #needed_data to indicate that a possible frame
643 * candidate has been found, but more data (#needed_data bytes) is needed to
644 * be absolutely sure. When this situation occurs, FALSE will be returned.
646 * When a valid frame is detected, this function will use
647 * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
648 * to set the needed bytes for next frame.This way next data chunk is already
651 * LOAS can have three different formats, if I read the spec correctly. Only
652 * one of them is supported here, as the two samples I have use this one.
654 * Returns: TRUE if the given data contains a valid LOAS header.
657 gst_aac_parse_check_loas_frame (GstAacParse * aacparse,
658 const guint8 * data, const guint avail, gboolean drain,
659 guint * framesize, guint * needed_data)
664 if (G_UNLIKELY (avail < 3))
667 if ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) {
668 *framesize = gst_aac_parse_loas_get_frame_len (data);
669 GST_DEBUG_OBJECT (aacparse, "Found %u byte LOAS frame", *framesize);
671 /* In EOS mode this is enough. No need to examine the data further.
672 We also relax the check when we have sync, on the assumption that
673 if we're not looking at random data, we have a much higher chance
674 to get the correct sync, and this avoids losing two frames when
675 a single bit corruption happens. */
676 if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
680 if (*framesize + LOAS_MAX_SIZE > avail) {
681 /* We have found a possible frame header candidate, but can't be
682 sure since we don't have enough data to check the next frame */
683 GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
684 *framesize + LOAS_MAX_SIZE, avail);
685 *needed_data = *framesize + LOAS_MAX_SIZE;
686 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
687 *framesize + LOAS_MAX_SIZE);
691 if ((data[*framesize] == 0x56) && ((data[*framesize + 1] & 0xe0) == 0xe0)) {
692 guint nextlen = gst_aac_parse_loas_get_frame_len (data + (*framesize));
694 GST_LOG ("LOAS frame found, len: %d bytes", *framesize);
695 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
696 nextlen + LOAS_MAX_SIZE);
703 /* caller ensure sufficient data */
705 gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data,
706 gint * rate, gint * channels, gint * object, gint * version)
710 gint sr_idx = (data[2] & 0x3c) >> 2;
712 *rate = gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
715 *channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6);
718 *version = (data[1] & 0x08) ? 2 : 4;
720 *object = ((data[2] & 0xc0) >> 6) + 1;
724 * gst_aac_parse_detect_stream:
725 * @aacparse: #GstAacParse.
726 * @data: A block of data that needs to be examined for stream characteristics.
727 * @avail: Size of the given datablock.
728 * @framesize: If valid stream was found, this will be set to tell the
729 * first frame size in bytes.
730 * @skipsize: If valid stream was found, this will be set to tell the first
731 * audio frame position within the given data.
733 * Examines the given piece of data and try to detect the format of it. It
734 * checks for "ADIF" header (in the beginning of the clip) and ADTS frame
735 * header. If the stream is detected, TRUE will be returned and #framesize
736 * is set to indicate the found frame size. Additionally, #skipsize might
737 * be set to indicate the number of bytes that need to be skipped, a.k.a. the
738 * position of the frame inside given data chunk.
740 * Returns: TRUE on success.
743 gst_aac_parse_detect_stream (GstAacParse * aacparse,
744 const guint8 * data, const guint avail, gboolean drain,
745 guint * framesize, gint * skipsize)
747 gboolean found = FALSE;
748 guint need_data_adts = 0, need_data_loas;
751 GST_DEBUG_OBJECT (aacparse, "Parsing header data");
753 /* FIXME: No need to check for ADIF if we are not in the beginning of the
756 /* Can we even parse the header? */
757 if (avail < MAX (ADTS_MAX_SIZE, LOAS_MAX_SIZE)) {
758 GST_DEBUG_OBJECT (aacparse, "Not enough data to check");
762 for (i = 0; i < avail - 4; i++) {
763 if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) ||
764 ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) ||
765 strncmp ((char *) data + i, "ADIF", 4) == 0) {
766 GST_DEBUG_OBJECT (aacparse, "Found signature at offset %u", i);
770 /* Trick: tell the parent class that we didn't find the frame yet,
771 but make it skip 'i' amount of bytes. Next time we arrive
772 here we have full frame in the beginning of the data. */
785 if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain,
786 framesize, &need_data_adts)) {
789 GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize);
791 gst_aac_parse_parse_adts_header (aacparse, data, &rate, &channels,
792 &aacparse->object_type, &aacparse->mpegversion);
794 if (!channels || !framesize) {
795 GST_DEBUG_OBJECT (aacparse, "impossible ADTS configuration");
799 aacparse->header_type = DSPAAC_HEADER_ADTS;
800 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
801 aacparse->frame_samples, 2, 2);
803 GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d",
804 rate, channels, aacparse->object_type, aacparse->mpegversion);
806 gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
811 if (gst_aac_parse_check_loas_frame (aacparse, data, avail, drain,
812 framesize, &need_data_loas)) {
815 GST_INFO ("LOAS, framesize: %d", *framesize);
817 aacparse->header_type = DSPAAC_HEADER_LOAS;
819 if (!gst_aac_parse_read_loas_config (aacparse, data, avail, &rate,
820 &channels, &aacparse->mpegversion)) {
821 GST_WARNING_OBJECT (aacparse, "Error reading LOAS config");
825 if (rate && channels) {
826 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
827 aacparse->frame_samples, 2, 2);
829 GST_DEBUG ("LOAS: samplerate %d, channels %d, objtype %d, version %d",
830 rate, channels, aacparse->object_type, aacparse->mpegversion);
831 aacparse->sample_rate = rate;
832 aacparse->channels = channels;
835 gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
840 if (need_data_adts || need_data_loas) {
841 /* This tells the parent class not to skip any data */
846 if (avail < ADIF_MAX_SIZE)
849 if (memcmp (data + i, "ADIF", 4) == 0) {
856 aacparse->header_type = DSPAAC_HEADER_ADIF;
857 aacparse->mpegversion = 4;
859 /* Skip the "ADIF" bytes */
862 /* copyright string */
864 skip_size += 9; /* skip 9 bytes */
866 bitstream_type = adif[0 + skip_size] & 0x10;
868 ((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) |
869 ((unsigned int) adif[1 + skip_size] << 11) |
870 ((unsigned int) adif[2 + skip_size] << 3) |
871 ((unsigned int) adif[3 + skip_size] & 0xe0);
874 if (bitstream_type == 0) {
876 /* Buffer fullness parsing. Currently not needed... */
880 num_elems = (adif[3 + skip_size] & 0x1e);
881 GST_INFO ("ADIF num_config_elems: %d", num_elems);
883 fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) |
884 ((unsigned int) adif[4 + skip_size] << 11) |
885 ((unsigned int) adif[5 + skip_size] << 3) |
886 ((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5);
888 GST_INFO ("ADIF buffer fullness: %d", fullness);
890 aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) |
891 ((adif[7 + skip_size] & 0x80) >> 7);
892 sr_idx = (adif[7 + skip_size] & 0x78) >> 3;
896 aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3;
897 sr_idx = ((adif[4 + skip_size] & 0x07) << 1) |
898 ((adif[5 + skip_size] & 0x80) >> 7);
901 /* FIXME: This gives totally wrong results. Duration calculation cannot
903 aacparse->sample_rate =
904 gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
906 /* baseparse is not given any fps,
907 * so it will give up on timestamps, seeking, etc */
909 /* FIXME: Can we assume this? */
910 aacparse->channels = 2;
912 GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d",
913 aacparse->bitrate, aacparse->sample_rate, aacparse->object_type);
915 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512);
917 /* arrange for metadata and get out of the way */
918 sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (aacparse));
919 gst_aac_parse_set_src_caps (aacparse, sinkcaps);
921 gst_caps_unref (sinkcaps);
923 /* not syncable, not easily seekable (unless we push data from start */
924 gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE);
925 gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE);
926 gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0);
932 /* This should never happen */
938 * gst_aac_parse_check_valid_frame:
939 * @parse: #GstBaseParse.
940 * @frame: #GstBaseParseFrame.
941 * @skipsize: How much data parent class should skip in order to find the
944 * Implementation of "handle_frame" vmethod in #GstBaseParse class.
946 * Also determines frame overhead.
947 * ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have
948 * a per-frame header. LOAS has 3 bytes.
950 * We're making a couple of simplifying assumptions:
952 * 1. We count Program Configuration Elements rather than searching for them
953 * in the streams to discount them - the overhead is negligible.
955 * 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16
956 * bits, which should still not be significant enough to warrant the
957 * additional parsing through the headers
959 * Returns: a #GstFlowReturn.
962 gst_aac_parse_handle_frame (GstBaseParse * parse,
963 GstBaseParseFrame * frame, gint * skipsize)
966 GstAacParse *aacparse;
967 gboolean ret = FALSE;
973 aacparse = GST_AAC_PARSE (parse);
974 buffer = frame->buffer;
976 gst_buffer_map (buffer, &map, GST_MAP_READ);
979 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
981 if (aacparse->header_type == DSPAAC_HEADER_ADIF ||
982 aacparse->header_type == DSPAAC_HEADER_NONE) {
983 /* There is nothing to parse */
984 framesize = map.size;
987 } else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) {
989 ret = gst_aac_parse_detect_stream (aacparse, map.data, map.size,
990 GST_BASE_PARSE_DRAINING (parse), &framesize, skipsize);
992 } else if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
993 guint needed_data = 1024;
995 ret = gst_aac_parse_check_adts_frame (aacparse, map.data, map.size,
996 GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
999 GST_DEBUG ("buffer didn't contain valid frame");
1000 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1004 } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1005 guint needed_data = 1024;
1007 ret = gst_aac_parse_check_loas_frame (aacparse, map.data,
1008 map.size, GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
1011 GST_DEBUG ("buffer didn't contain valid frame");
1012 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1017 GST_DEBUG ("buffer didn't contain valid frame");
1018 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1022 if (G_UNLIKELY (!ret))
1025 if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1027 frame->overhead = 7;
1029 gst_aac_parse_parse_adts_header (aacparse, map.data,
1030 &rate, &channels, NULL, NULL);
1032 GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels);
1034 if (G_UNLIKELY (rate != aacparse->sample_rate
1035 || channels != aacparse->channels)) {
1036 aacparse->sample_rate = rate;
1037 aacparse->channels = channels;
1039 if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1040 /* If linking fails, we need to return appropriate error */
1041 ret = GST_FLOW_NOT_LINKED;
1044 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1045 aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1047 } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1048 gboolean setcaps = FALSE;
1051 frame->overhead = 3;
1053 if (!gst_aac_parse_read_loas_config (aacparse, map.data, map.size, &rate,
1055 GST_WARNING_OBJECT (aacparse, "Error reading LOAS config");
1056 } else if (G_UNLIKELY (rate != aacparse->sample_rate
1057 || channels != aacparse->channels)) {
1058 aacparse->sample_rate = rate;
1059 aacparse->channels = channels;
1061 GST_INFO_OBJECT (aacparse, "New LOAS config: %d Hz, %d channels", rate,
1065 /* We want to set caps both at start, and when rate/channels change.
1066 Since only some LOAS frames have that info, we may receive frames
1067 before knowing about rate/channels. */
1069 || !gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse))) {
1070 if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1071 /* If linking fails, we need to return appropriate error */
1072 ret = GST_FLOW_NOT_LINKED;
1075 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1076 aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1081 gst_buffer_unmap (buffer, &map);
1084 /* found, skip if needed */
1093 if (ret && framesize <= map.size) {
1094 return gst_base_parse_finish_frame (parse, frame, framesize);
1100 static GstFlowReturn
1101 gst_aac_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
1103 GstAacParse *aacparse = GST_AAC_PARSE (parse);
1105 /* As a special case, we can remove the ADTS framing and output raw AAC. */
1106 if (aacparse->header_type == DSPAAC_HEADER_ADTS
1107 && aacparse->output_header_type == DSPAAC_HEADER_NONE) {
1110 gst_buffer_map (frame->buffer, &map, GST_MAP_READ);
1111 header_size = (map.data[1] & 1) ? 7 : 9; /* optional CRC */
1112 gst_buffer_unmap (frame->buffer, &map);
1113 gst_buffer_resize (frame->buffer, header_size,
1114 gst_buffer_get_size (frame->buffer) - header_size);
1122 * gst_aac_parse_start:
1123 * @parse: #GstBaseParse.
1125 * Implementation of "start" vmethod in #GstBaseParse class.
1127 * Returns: TRUE if startup succeeded.
1130 gst_aac_parse_start (GstBaseParse * parse)
1132 GstAacParse *aacparse;
1134 aacparse = GST_AAC_PARSE (parse);
1135 GST_DEBUG ("start");
1136 aacparse->frame_samples = 1024;
1137 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), ADTS_MAX_SIZE);
1143 * gst_aac_parse_stop:
1144 * @parse: #GstBaseParse.
1146 * Implementation of "stop" vmethod in #GstBaseParse class.
1148 * Returns: TRUE is stopping succeeded.
1151 gst_aac_parse_stop (GstBaseParse * parse)
1158 gst_aac_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter)
1160 GstCaps *peercaps, *templ;
1163 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1164 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), filter);
1168 /* Remove the framed field */
1169 peercaps = gst_caps_make_writable (peercaps);
1170 n = gst_caps_get_size (peercaps);
1171 for (i = 0; i < n; i++) {
1172 GstStructure *s = gst_caps_get_structure (peercaps, i);
1174 gst_structure_remove_field (s, "framed");
1177 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1178 gst_caps_unref (peercaps);
1179 res = gst_caps_make_writable (res);
1181 /* Append the template caps because we still want to accept
1182 * caps without any fields in the case upstream does not
1185 gst_caps_append (res, templ);
1191 GstCaps *intersection;
1194 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1195 gst_caps_unref (res);