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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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 filesrc location=abc.aac ! aacparse ! faad ! audioresample ! audioconvert ! alsasink
47 #include <gst/base/gstbitreader.h>
48 #include "gstaacparse.h"
51 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
54 GST_STATIC_CAPS ("audio/mpeg, "
55 "framed = (boolean) true, " "mpegversion = (int) { 2, 4 }, "
56 "stream-format = (string) { raw, adts, adif, loas };"));
58 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
61 GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) { 2, 4 };"));
63 GST_DEBUG_CATEGORY_STATIC (aacparse_debug);
64 #define GST_CAT_DEFAULT aacparse_debug
67 #define ADIF_MAX_SIZE 40 /* Should be enough */
68 #define ADTS_MAX_SIZE 10 /* Should be enough */
69 #define LOAS_MAX_SIZE 3 /* Should be enough */
72 #define AAC_FRAME_DURATION(parse) (GST_SECOND/parse->frames_per_sec)
74 static const gint loas_sample_rate_table[32] = {
75 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
76 16000, 12000, 11025, 8000, 7350, 0, 0, 0
79 static const gint loas_channels_table[32] = {
80 0, 1, 2, 3, 4, 5, 6, 8,
81 0, 0, 0, 0, 0, 0, 0, 0
84 static gboolean gst_aac_parse_start (GstBaseParse * parse);
85 static gboolean gst_aac_parse_stop (GstBaseParse * parse);
87 static gboolean gst_aac_parse_sink_setcaps (GstBaseParse * parse,
89 static GstCaps *gst_aac_parse_sink_getcaps (GstBaseParse * parse,
92 static gboolean gst_aac_parse_check_valid_frame (GstBaseParse * parse,
93 GstBaseParseFrame * frame, guint * size, gint * skipsize);
95 static GstFlowReturn gst_aac_parse_parse_frame (GstBaseParse * parse,
96 GstBaseParseFrame * frame);
98 gboolean gst_aac_parse_convert (GstBaseParse * parse,
100 gint64 src_value, GstFormat dest_format, gint64 * dest_value);
102 gint gst_aac_parse_get_frame_overhead (GstBaseParse * parse,
105 gboolean gst_aac_parse_event (GstBaseParse * parse, GstEvent * event);
107 G_DEFINE_TYPE (GstAacParse, gst_aac_parse, GST_TYPE_BASE_PARSE);
110 gst_aac_parse_get_sample_rate_from_index (guint sr_idx)
112 static const guint aac_sample_rates[] = { 96000, 88200, 64000, 48000, 44100,
113 32000, 24000, 22050, 16000, 12000, 11025, 8000
116 if (sr_idx < G_N_ELEMENTS (aac_sample_rates))
117 return aac_sample_rates[sr_idx];
118 GST_WARNING ("Invalid sample rate index %u", sr_idx);
123 * gst_aac_parse_class_init:
124 * @klass: #GstAacParseClass.
128 gst_aac_parse_class_init (GstAacParseClass * klass)
130 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
131 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
133 GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0,
134 "AAC audio stream parser");
136 gst_element_class_add_pad_template (element_class,
137 gst_static_pad_template_get (&sink_template));
138 gst_element_class_add_pad_template (element_class,
139 gst_static_pad_template_get (&src_template));
141 gst_element_class_set_details_simple (element_class,
142 "AAC audio stream parser", "Codec/Parser/Audio",
143 "Advanced Audio Coding parser", "Stefan Kost <stefan.kost@nokia.com>");
145 parse_class->start = GST_DEBUG_FUNCPTR (gst_aac_parse_start);
146 parse_class->stop = GST_DEBUG_FUNCPTR (gst_aac_parse_stop);
147 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_setcaps);
148 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_getcaps);
149 parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_parse_frame);
150 parse_class->check_valid_frame =
151 GST_DEBUG_FUNCPTR (gst_aac_parse_check_valid_frame);
156 * gst_aac_parse_init:
157 * @aacparse: #GstAacParse.
158 * @klass: #GstAacParseClass.
162 gst_aac_parse_init (GstAacParse * aacparse)
164 GST_DEBUG ("initialized");
169 * gst_aac_parse_set_src_caps:
170 * @aacparse: #GstAacParse.
171 * @sink_caps: (proposed) caps of sink pad
173 * Set source pad caps according to current knowledge about the
176 * Returns: TRUE if caps were successfully set.
179 gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps)
182 GstCaps *src_caps = NULL;
183 gboolean res = FALSE;
184 const gchar *stream_format;
186 GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps);
188 src_caps = gst_caps_copy (sink_caps);
190 src_caps = gst_caps_new_empty_simple ("audio/mpeg");
192 gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE,
193 "mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL);
195 switch (aacparse->header_type) {
196 case DSPAAC_HEADER_NONE:
197 stream_format = "raw";
199 case DSPAAC_HEADER_ADTS:
200 stream_format = "adts";
202 case DSPAAC_HEADER_ADIF:
203 stream_format = "adif";
205 case DSPAAC_HEADER_LOAS:
206 stream_format = "loas";
209 stream_format = NULL;
212 s = gst_caps_get_structure (src_caps, 0);
213 if (aacparse->sample_rate > 0)
214 gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL);
215 if (aacparse->channels > 0)
216 gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL);
218 gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL);
220 GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps);
222 res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps);
223 gst_caps_unref (src_caps);
229 * gst_aac_parse_sink_setcaps:
233 * Implementation of "set_sink_caps" vmethod in #GstBaseParse class.
235 * Returns: TRUE on success.
238 gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
240 GstAacParse *aacparse;
241 GstStructure *structure;
245 aacparse = GST_AAC_PARSE (parse);
246 structure = gst_caps_get_structure (caps, 0);
247 caps_str = gst_caps_to_string (caps);
249 GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str);
252 /* This is needed at least in case of RTP
253 * Parses the codec_data information to get ObjectType,
254 * number of channels and samplerate */
255 value = gst_structure_get_value (structure, "codec_data");
257 GstBuffer *buf = gst_value_get_buffer (value);
264 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
266 sr_idx = ((data[0] & 0x07) << 1) | ((data[1] & 0x80) >> 7);
267 aacparse->object_type = (data[0] & 0xf8) >> 3;
268 aacparse->sample_rate = gst_aac_parse_get_sample_rate_from_index (sr_idx);
269 aacparse->channels = (data[1] & 0x78) >> 3;
270 aacparse->header_type = DSPAAC_HEADER_NONE;
271 aacparse->mpegversion = 4;
272 aacparse->frame_samples = (data[1] & 4) ? 960 : 1024;
273 gst_buffer_unmap (buf, data, size);
275 GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d, "
276 "samples=%d", aacparse->object_type, aacparse->sample_rate,
277 aacparse->channels, aacparse->frame_samples);
279 /* arrange for metadata and get out of the way */
280 gst_aac_parse_set_src_caps (aacparse, caps);
281 gst_base_parse_set_passthrough (parse, TRUE);
285 /* caps info overrides */
286 gst_structure_get_int (structure, "rate", &aacparse->sample_rate);
287 gst_structure_get_int (structure, "channels", &aacparse->channels);
289 gst_base_parse_set_passthrough (parse, FALSE);
297 * gst_aac_parse_adts_get_frame_len:
298 * @data: block of data containing an ADTS header.
300 * This function calculates ADTS frame length from the given header.
302 * Returns: size of the ADTS frame.
305 gst_aac_parse_adts_get_frame_len (const guint8 * data)
307 return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5);
312 * gst_aac_parse_check_adts_frame:
313 * @aacparse: #GstAacParse.
314 * @data: Data to be checked.
315 * @avail: Amount of data passed.
316 * @framesize: If valid ADTS frame was found, this will be set to tell the
317 * found frame size in bytes.
318 * @needed_data: If frame was not found, this may be set to tell how much
319 * more data is needed in the next round to detect the frame
320 * reliably. This may happen when a frame header candidate
321 * is found but it cannot be guaranteed to be the header without
322 * peeking the following data.
324 * Check if the given data contains contains ADTS frame. The algorithm
325 * will examine ADTS frame header and calculate the frame size. Also, another
326 * consecutive ADTS frame header need to be present after the found frame.
327 * Otherwise the data is not considered as a valid ADTS frame. However, this
328 * "extra check" is omitted when EOS has been received. In this case it is
329 * enough when data[0] contains a valid ADTS header.
331 * This function may set the #needed_data to indicate that a possible frame
332 * candidate has been found, but more data (#needed_data bytes) is needed to
333 * be absolutely sure. When this situation occurs, FALSE will be returned.
335 * When a valid frame is detected, this function will use
336 * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
337 * to set the needed bytes for next frame.This way next data chunk is already
340 * Returns: TRUE if the given data contains a valid ADTS header.
343 gst_aac_parse_check_adts_frame (GstAacParse * aacparse,
344 const guint8 * data, const guint avail, gboolean drain,
345 guint * framesize, guint * needed_data)
349 if (G_UNLIKELY (avail < 2))
352 if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) {
353 *framesize = gst_aac_parse_adts_get_frame_len (data);
355 /* In EOS mode this is enough. No need to examine the data further.
356 We also relax the check when we have sync, on the assumption that
357 if we're not looking at random data, we have a much higher chance
358 to get the correct sync, and this avoids losing two frames when
359 a single bit corruption happens. */
360 if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
364 if (*framesize + ADTS_MAX_SIZE > avail) {
365 /* We have found a possible frame header candidate, but can't be
366 sure since we don't have enough data to check the next frame */
367 GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
368 *framesize + ADTS_MAX_SIZE, avail);
369 *needed_data = *framesize + ADTS_MAX_SIZE;
370 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
371 *framesize + ADTS_MAX_SIZE);
375 if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) {
376 guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize));
378 GST_LOG ("ADTS frame found, len: %d bytes", *framesize);
379 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
380 nextlen + ADTS_MAX_SIZE);
388 gst_aac_parse_latm_get_value (GstAacParse * aacparse, GstBitReader * br,
391 guint8 bytes, i, byte;
394 if (!gst_bit_reader_get_bits_uint8 (br, &bytes, 2))
396 for (i = 0; i < bytes; ++i) {
398 if (!gst_bit_reader_get_bits_uint8 (br, &byte, 8))
406 gst_aac_parse_get_audio_object_type (GstAacParse * aacparse, GstBitReader * br,
407 guint8 * audio_object_type)
409 if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 5))
411 if (*audio_object_type == 31) {
412 if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 6))
414 *audio_object_type += 32;
416 GST_LOG_OBJECT (aacparse, "audio object type %u", *audio_object_type);
421 gst_aac_parse_get_audio_sample_rate (GstAacParse * aacparse, GstBitReader * br,
424 guint8 sampling_frequency_index;
425 if (!gst_bit_reader_get_bits_uint8 (br, &sampling_frequency_index, 4))
427 GST_LOG_OBJECT (aacparse, "sampling_frequency_index: %u",
428 sampling_frequency_index);
429 if (sampling_frequency_index == 0xf) {
430 guint32 sampling_rate;
431 if (!gst_bit_reader_get_bits_uint32 (br, &sampling_rate, 24))
433 *sample_rate = sampling_rate;
435 *sample_rate = loas_sample_rate_table[sampling_frequency_index];
442 /* See table 1.13 in ISO/IEC 14496-3 */
444 gst_aac_parse_read_loas_audio_specific_config (GstAacParse * aacparse,
445 GstBitReader * br, gint * sample_rate, gint * channels, guint32 * bits)
447 guint8 audio_object_type, channel_configuration;
449 if (!gst_aac_parse_get_audio_object_type (aacparse, br, &audio_object_type))
452 if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
455 if (!gst_bit_reader_get_bits_uint8 (br, &channel_configuration, 4))
457 GST_LOG_OBJECT (aacparse, "channel_configuration: %d", channel_configuration);
458 *channels = loas_channels_table[channel_configuration];
462 if (audio_object_type == 5) {
463 GST_LOG_OBJECT (aacparse,
464 "Audio object type 5, so rereading sampling rate...");
465 if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
469 GST_INFO_OBJECT (aacparse, "Found LOAS config: %d Hz, %d channels",
470 *sample_rate, *channels);
472 /* There's LOTS of stuff next, but we ignore it for now as we have
473 what we want (sample rate and number of channels */
474 GST_DEBUG_OBJECT (aacparse,
475 "Need more code to parse humongous LOAS data, currently ignored");
483 gst_aac_parse_read_loas_config (GstAacParse * aacparse, const guint8 * data,
484 guint avail, gint * sample_rate, gint * channels, gint * version)
489 /* No version in the bitstream, but the spec has LOAS in the MPEG-4 section */
493 gst_bit_reader_init (&br, data, avail);
495 /* skip sync word (11 bits) and size (13 bits) */
496 gst_bit_reader_skip (&br, 11 + 13);
498 /* First bit is "use last config" */
499 if (!gst_bit_reader_get_bits_uint8 (&br, &u8, 1))
502 GST_DEBUG_OBJECT (aacparse, "Frame uses previous config");
503 if (!aacparse->sample_rate || !aacparse->channels) {
504 GST_WARNING_OBJECT (aacparse, "No previous config to use");
506 *sample_rate = aacparse->sample_rate;
507 *channels = aacparse->channels;
511 GST_DEBUG_OBJECT (aacparse, "Frame contains new config");
513 if (!gst_bit_reader_get_bits_uint8 (&br, &v, 1))
516 if (!gst_bit_reader_get_bits_uint8 (&br, &vA, 1))
521 GST_LOG_OBJECT (aacparse, "v %d, vA %d", v, vA);
523 guint8 same_time, subframes, num_program, prog;
526 if (!gst_aac_parse_latm_get_value (aacparse, &br, &value))
529 if (!gst_bit_reader_get_bits_uint8 (&br, &same_time, 1))
531 if (!gst_bit_reader_get_bits_uint8 (&br, &subframes, 6))
533 if (!gst_bit_reader_get_bits_uint8 (&br, &num_program, 4))
535 GST_LOG_OBJECT (aacparse, "same_time %d, subframes %d, num_program %d",
536 same_time, subframes, num_program);
538 for (prog = 0; prog <= num_program; ++prog) {
539 guint8 num_layer, layer;
540 if (!gst_bit_reader_get_bits_uint8 (&br, &num_layer, 3))
542 GST_LOG_OBJECT (aacparse, "Program %d: %d layers", prog, num_layer);
544 for (layer = 0; layer <= num_layer; ++layer) {
545 guint8 use_same_config;
546 if (prog == 0 && layer == 0) {
549 if (!gst_bit_reader_get_bits_uint8 (&br, &use_same_config, 1))
552 if (!use_same_config) {
554 if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
555 sample_rate, channels, NULL))
558 guint32 bits, asc_len;
559 if (!gst_aac_parse_latm_get_value (aacparse, &br, &asc_len))
561 if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
562 sample_rate, channels, &bits))
565 gst_bit_reader_skip (&br, asc_len);
570 GST_WARNING_OBJECT (aacparse, "More data ignored");
572 GST_WARNING_OBJECT (aacparse, "Spec says \"TBD\"...");
578 * gst_aac_parse_loas_get_frame_len:
579 * @data: block of data containing a LOAS header.
581 * This function calculates LOAS frame length from the given header.
583 * Returns: size of the LOAS frame.
586 gst_aac_parse_loas_get_frame_len (const guint8 * data)
588 return (((data[1] & 0x1f) << 8) | data[2]) + 3;
593 * gst_aac_parse_check_loas_frame:
594 * @aacparse: #GstAacParse.
595 * @data: Data to be checked.
596 * @avail: Amount of data passed.
597 * @framesize: If valid LOAS frame was found, this will be set to tell the
598 * found frame size in bytes.
599 * @needed_data: If frame was not found, this may be set to tell how much
600 * more data is needed in the next round to detect the frame
601 * reliably. This may happen when a frame header candidate
602 * is found but it cannot be guaranteed to be the header without
603 * peeking the following data.
605 * Check if the given data contains contains LOAS frame. The algorithm
606 * will examine LOAS frame header and calculate the frame size. Also, another
607 * consecutive LOAS frame header need to be present after the found frame.
608 * Otherwise the data is not considered as a valid LOAS frame. However, this
609 * "extra check" is omitted when EOS has been received. In this case it is
610 * enough when data[0] contains a valid LOAS header.
612 * This function may set the #needed_data to indicate that a possible frame
613 * candidate has been found, but more data (#needed_data bytes) is needed to
614 * be absolutely sure. When this situation occurs, FALSE will be returned.
616 * When a valid frame is detected, this function will use
617 * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
618 * to set the needed bytes for next frame.This way next data chunk is already
621 * LOAS can have three different formats, if I read the spec correctly. Only
622 * one of them is supported here, as the two samples I have use this one.
624 * Returns: TRUE if the given data contains a valid LOAS header.
627 gst_aac_parse_check_loas_frame (GstAacParse * aacparse,
628 const guint8 * data, const guint avail, gboolean drain,
629 guint * framesize, guint * needed_data)
634 if (G_UNLIKELY (avail < 3))
637 if ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) {
638 *framesize = gst_aac_parse_loas_get_frame_len (data);
639 GST_DEBUG_OBJECT (aacparse, "Found %u byte LOAS frame", *framesize);
641 /* In EOS mode this is enough. No need to examine the data further.
642 We also relax the check when we have sync, on the assumption that
643 if we're not looking at random data, we have a much higher chance
644 to get the correct sync, and this avoids losing two frames when
645 a single bit corruption happens. */
646 if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
650 if (*framesize + LOAS_MAX_SIZE > avail) {
651 /* We have found a possible frame header candidate, but can't be
652 sure since we don't have enough data to check the next frame */
653 GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
654 *framesize + LOAS_MAX_SIZE, avail);
655 *needed_data = *framesize + LOAS_MAX_SIZE;
656 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
657 *framesize + LOAS_MAX_SIZE);
661 if ((data[*framesize] == 0x56) && ((data[*framesize + 1] & 0xe0) == 0xe0)) {
662 guint nextlen = gst_aac_parse_loas_get_frame_len (data + (*framesize));
664 GST_LOG ("LOAS frame found, len: %d bytes", *framesize);
665 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
666 nextlen + LOAS_MAX_SIZE);
673 /* caller ensure sufficient data */
675 gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data,
676 gint * rate, gint * channels, gint * object, gint * version)
680 gint sr_idx = (data[2] & 0x3c) >> 2;
682 *rate = gst_aac_parse_get_sample_rate_from_index (sr_idx);
685 *channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6);
688 *version = (data[1] & 0x08) ? 2 : 4;
690 *object = (data[2] & 0xc0) >> 6;
694 * gst_aac_parse_detect_stream:
695 * @aacparse: #GstAacParse.
696 * @data: A block of data that needs to be examined for stream characteristics.
697 * @avail: Size of the given datablock.
698 * @framesize: If valid stream was found, this will be set to tell the
699 * first frame size in bytes.
700 * @skipsize: If valid stream was found, this will be set to tell the first
701 * audio frame position within the given data.
703 * Examines the given piece of data and try to detect the format of it. It
704 * checks for "ADIF" header (in the beginning of the clip) and ADTS frame
705 * header. If the stream is detected, TRUE will be returned and #framesize
706 * is set to indicate the found frame size. Additionally, #skipsize might
707 * be set to indicate the number of bytes that need to be skipped, a.k.a. the
708 * position of the frame inside given data chunk.
710 * Returns: TRUE on success.
713 gst_aac_parse_detect_stream (GstAacParse * aacparse,
714 const guint8 * data, const guint avail, gboolean drain,
715 guint * framesize, gint * skipsize)
717 gboolean found = FALSE;
718 guint need_data_adts = 0, need_data_loas;
721 GST_DEBUG_OBJECT (aacparse, "Parsing header data");
723 /* FIXME: No need to check for ADIF if we are not in the beginning of the
726 /* Can we even parse the header? */
727 if (avail < MAX (ADTS_MAX_SIZE, LOAS_MAX_SIZE)) {
728 GST_DEBUG_OBJECT (aacparse, "Not enough data to check");
732 for (i = 0; i < avail - 4; i++) {
733 if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) ||
734 ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) ||
735 strncmp ((char *) data + i, "ADIF", 4) == 0) {
736 GST_DEBUG_OBJECT (aacparse, "Found ADIF signature at offset %u", i);
740 /* Trick: tell the parent class that we didn't find the frame yet,
741 but make it skip 'i' amount of bytes. Next time we arrive
742 here we have full frame in the beginning of the data. */
755 if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain,
756 framesize, &need_data_adts)) {
759 GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize);
761 aacparse->header_type = DSPAAC_HEADER_ADTS;
762 gst_aac_parse_parse_adts_header (aacparse, data, &rate, &channels,
763 &aacparse->object_type, &aacparse->mpegversion);
765 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
766 aacparse->frame_samples, 2, 2);
768 GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d",
769 rate, channels, aacparse->object_type, aacparse->mpegversion);
771 gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
776 if (gst_aac_parse_check_loas_frame (aacparse, data, avail, drain,
777 framesize, &need_data_loas)) {
780 GST_INFO ("LOAS, framesize: %d", *framesize);
782 aacparse->header_type = DSPAAC_HEADER_LOAS;
784 if (!gst_aac_parse_read_loas_config (aacparse, data, avail, &rate,
785 &channels, &aacparse->mpegversion)) {
786 GST_WARNING_OBJECT (aacparse, "Error reading LOAS config");
790 if (rate && channels) {
791 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
792 aacparse->frame_samples, 2, 2);
794 GST_DEBUG ("LOAS: samplerate %d, channels %d, objtype %d, version %d",
795 rate, channels, aacparse->object_type, aacparse->mpegversion);
796 aacparse->sample_rate = rate;
797 aacparse->channels = channels;
800 gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
805 if (need_data_adts || need_data_loas) {
806 /* This tells the parent class not to skip any data */
811 if (avail < ADIF_MAX_SIZE)
814 if (memcmp (data + i, "ADIF", 4) == 0) {
821 aacparse->header_type = DSPAAC_HEADER_ADIF;
822 aacparse->mpegversion = 4;
824 /* Skip the "ADIF" bytes */
827 /* copyright string */
829 skip_size += 9; /* skip 9 bytes */
831 bitstream_type = adif[0 + skip_size] & 0x10;
833 ((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) |
834 ((unsigned int) adif[1 + skip_size] << 11) |
835 ((unsigned int) adif[2 + skip_size] << 3) |
836 ((unsigned int) adif[3 + skip_size] & 0xe0);
839 if (bitstream_type == 0) {
841 /* Buffer fullness parsing. Currently not needed... */
845 num_elems = (adif[3 + skip_size] & 0x1e);
846 GST_INFO ("ADIF num_config_elems: %d", num_elems);
848 fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) |
849 ((unsigned int) adif[4 + skip_size] << 11) |
850 ((unsigned int) adif[5 + skip_size] << 3) |
851 ((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5);
853 GST_INFO ("ADIF buffer fullness: %d", fullness);
855 aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) |
856 ((adif[7 + skip_size] & 0x80) >> 7);
857 sr_idx = (adif[7 + skip_size] & 0x78) >> 3;
861 aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3;
862 sr_idx = ((adif[4 + skip_size] & 0x07) << 1) |
863 ((adif[5 + skip_size] & 0x80) >> 7);
866 /* FIXME: This gives totally wrong results. Duration calculation cannot
868 aacparse->sample_rate = gst_aac_parse_get_sample_rate_from_index (sr_idx);
870 /* baseparse is not given any fps,
871 * so it will give up on timestamps, seeking, etc */
873 /* FIXME: Can we assume this? */
874 aacparse->channels = 2;
876 GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d",
877 aacparse->bitrate, aacparse->sample_rate, aacparse->object_type);
879 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512);
881 /* arrange for metadata and get out of the way */
882 sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (aacparse));
883 gst_aac_parse_set_src_caps (aacparse, sinkcaps);
884 gst_caps_unref (sinkcaps);
886 /* not syncable, not easily seekable (unless we push data from start */
887 gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE);
888 gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE);
889 gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0);
895 /* This should never happen */
901 * gst_aac_parse_check_valid_frame:
902 * @parse: #GstBaseParse.
903 * @buffer: #GstBuffer.
904 * @framesize: If the buffer contains a valid frame, its size will be put here
905 * @skipsize: How much data parent class should skip in order to find the
908 * Implementation of "check_valid_frame" vmethod in #GstBaseParse class.
910 * Returns: TRUE if buffer contains a valid frame.
913 gst_aac_parse_check_valid_frame (GstBaseParse * parse,
914 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
918 GstAacParse *aacparse;
919 gboolean ret = FALSE;
923 aacparse = GST_AAC_PARSE (parse);
924 buffer = frame->buffer;
926 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
928 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
930 if (aacparse->header_type == DSPAAC_HEADER_ADIF ||
931 aacparse->header_type == DSPAAC_HEADER_NONE) {
932 /* There is nothing to parse */
936 } else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) {
938 ret = gst_aac_parse_detect_stream (aacparse, data, size,
939 GST_BASE_PARSE_DRAINING (parse), framesize, skipsize);
941 } else if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
942 guint needed_data = 1024;
944 ret = gst_aac_parse_check_adts_frame (aacparse, data, size,
945 GST_BASE_PARSE_DRAINING (parse), framesize, &needed_data);
948 GST_DEBUG ("buffer didn't contain valid frame");
949 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
953 } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
954 guint needed_data = 1024;
956 ret = gst_aac_parse_check_loas_frame (aacparse, data,
957 size, GST_BASE_PARSE_DRAINING (parse), framesize, &needed_data);
960 GST_DEBUG ("buffer didn't contain valid frame");
961 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
966 GST_DEBUG ("buffer didn't contain valid frame");
967 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
970 gst_buffer_unmap (buffer, data, size);
977 * gst_aac_parse_parse_frame:
978 * @parse: #GstBaseParse.
979 * @buffer: #GstBuffer.
981 * Implementation of "parse_frame" vmethod in #GstBaseParse class.
983 * Also determines frame overhead.
984 * ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have
985 * a per-frame header. LOAS has 3 bytes.
987 * We're making a couple of simplifying assumptions:
989 * 1. We count Program Configuration Elements rather than searching for them
990 * in the streams to discount them - the overhead is negligible.
992 * 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16
993 * bits, which should still not be significant enough to warrant the
994 * additional parsing through the headers
996 * Returns: GST_FLOW_OK if frame was successfully parsed and can be pushed
997 * forward. Otherwise appropriate error is returned.
1000 gst_aac_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
1002 GstAacParse *aacparse;
1004 GstFlowReturn ret = GST_FLOW_OK;
1005 gint rate, channels;
1009 aacparse = GST_AAC_PARSE (parse);
1010 buffer = frame->buffer;
1012 if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1014 frame->overhead = 7;
1016 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1017 gst_aac_parse_parse_adts_header (aacparse, data,
1018 &rate, &channels, NULL, NULL);
1019 gst_buffer_unmap (buffer, data, size);
1021 GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels);
1023 if (G_UNLIKELY (rate != aacparse->sample_rate
1024 || channels != aacparse->channels)) {
1027 aacparse->sample_rate = rate;
1028 aacparse->channels = channels;
1030 sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE (aacparse)->sinkpad);
1031 if (!gst_aac_parse_set_src_caps (aacparse, sinkcaps)) {
1032 /* If linking fails, we need to return appropriate error */
1033 ret = GST_FLOW_NOT_LINKED;
1035 gst_caps_unref (sinkcaps);
1037 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1038 aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1040 } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1041 gboolean setcaps = FALSE;
1044 frame->overhead = 3;
1046 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1047 if (!gst_aac_parse_read_loas_config (aacparse, data, size, &rate, &channels,
1049 GST_WARNING_OBJECT (aacparse, "Error reading LOAS config");
1050 } else if (G_UNLIKELY (rate != aacparse->sample_rate
1051 || channels != aacparse->channels)) {
1052 aacparse->sample_rate = rate;
1053 aacparse->channels = channels;
1055 GST_INFO_OBJECT (aacparse, "New LOAS config: %d Hz, %d channels", rate,
1058 gst_buffer_unmap (buffer, data, size);
1060 /* We want to set caps both at start, and when rate/channels change.
1061 Since only some LOAS frames have that info, we may receive frames
1062 before knowing about rate/channels. */
1064 || !gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse))) {
1066 gst_pad_get_current_caps (GST_BASE_PARSE (aacparse)->sinkpad);
1067 if (!gst_aac_parse_set_src_caps (aacparse, sinkcaps)) {
1068 /* If linking fails, we need to return appropriate error */
1069 ret = GST_FLOW_NOT_LINKED;
1071 gst_caps_unref (sinkcaps);
1073 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1074 aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1083 * gst_aac_parse_start:
1084 * @parse: #GstBaseParse.
1086 * Implementation of "start" vmethod in #GstBaseParse class.
1088 * Returns: TRUE if startup succeeded.
1091 gst_aac_parse_start (GstBaseParse * parse)
1093 GstAacParse *aacparse;
1095 aacparse = GST_AAC_PARSE (parse);
1096 GST_DEBUG ("start");
1097 aacparse->frame_samples = 1024;
1098 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), ADTS_MAX_SIZE);
1104 * gst_aac_parse_stop:
1105 * @parse: #GstBaseParse.
1107 * Implementation of "stop" vmethod in #GstBaseParse class.
1109 * Returns: TRUE is stopping succeeded.
1112 gst_aac_parse_stop (GstBaseParse * parse)
1119 gst_aac_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter)
1124 /* FIXME: handle filter caps */
1126 peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
1130 /* Remove the framed field */
1131 peercaps = gst_caps_make_writable (peercaps);
1132 n = gst_caps_get_size (peercaps);
1133 for (i = 0; i < n; i++) {
1134 GstStructure *s = gst_caps_get_structure (peercaps, i);
1136 gst_structure_remove_field (s, "framed");
1140 gst_caps_intersect_full (peercaps,
1141 gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)),
1142 GST_CAPS_INTERSECT_FIRST);
1143 gst_caps_unref (peercaps);
1146 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD