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 GstFlowReturn gst_aac_parse_handle_frame (GstBaseParse * parse,
93 GstBaseParseFrame * frame, gint * skipsize);
95 G_DEFINE_TYPE (GstAacParse, gst_aac_parse, GST_TYPE_BASE_PARSE);
98 gst_aac_parse_get_sample_rate_from_index (guint sr_idx)
100 static const guint aac_sample_rates[] = { 96000, 88200, 64000, 48000, 44100,
101 32000, 24000, 22050, 16000, 12000, 11025, 8000
104 if (sr_idx < G_N_ELEMENTS (aac_sample_rates))
105 return aac_sample_rates[sr_idx];
106 GST_WARNING ("Invalid sample rate index %u", sr_idx);
111 * gst_aac_parse_class_init:
112 * @klass: #GstAacParseClass.
116 gst_aac_parse_class_init (GstAacParseClass * klass)
118 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
119 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
121 GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0,
122 "AAC audio stream parser");
124 gst_element_class_add_pad_template (element_class,
125 gst_static_pad_template_get (&sink_template));
126 gst_element_class_add_pad_template (element_class,
127 gst_static_pad_template_get (&src_template));
129 gst_element_class_set_static_metadata (element_class,
130 "AAC audio stream parser", "Codec/Parser/Audio",
131 "Advanced Audio Coding parser", "Stefan Kost <stefan.kost@nokia.com>");
133 parse_class->start = GST_DEBUG_FUNCPTR (gst_aac_parse_start);
134 parse_class->stop = GST_DEBUG_FUNCPTR (gst_aac_parse_stop);
135 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_setcaps);
136 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_getcaps);
137 parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_handle_frame);
142 * gst_aac_parse_init:
143 * @aacparse: #GstAacParse.
144 * @klass: #GstAacParseClass.
148 gst_aac_parse_init (GstAacParse * aacparse)
150 GST_DEBUG ("initialized");
155 * gst_aac_parse_set_src_caps:
156 * @aacparse: #GstAacParse.
157 * @sink_caps: (proposed) caps of sink pad
159 * Set source pad caps according to current knowledge about the
162 * Returns: TRUE if caps were successfully set.
165 gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps)
168 GstCaps *src_caps = NULL;
169 gboolean res = FALSE;
170 const gchar *stream_format;
172 GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps);
174 src_caps = gst_caps_copy (sink_caps);
176 src_caps = gst_caps_new_empty_simple ("audio/mpeg");
178 gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE,
179 "mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL);
181 switch (aacparse->header_type) {
182 case DSPAAC_HEADER_NONE:
183 stream_format = "raw";
185 case DSPAAC_HEADER_ADTS:
186 stream_format = "adts";
188 case DSPAAC_HEADER_ADIF:
189 stream_format = "adif";
191 case DSPAAC_HEADER_LOAS:
192 stream_format = "loas";
195 stream_format = NULL;
198 s = gst_caps_get_structure (src_caps, 0);
199 if (aacparse->sample_rate > 0)
200 gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL);
201 if (aacparse->channels > 0)
202 gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL);
204 gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL);
206 GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps);
208 res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps);
209 gst_caps_unref (src_caps);
215 * gst_aac_parse_sink_setcaps:
219 * Implementation of "set_sink_caps" vmethod in #GstBaseParse class.
221 * Returns: TRUE on success.
224 gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
226 GstAacParse *aacparse;
227 GstStructure *structure;
231 aacparse = GST_AAC_PARSE (parse);
232 structure = gst_caps_get_structure (caps, 0);
233 caps_str = gst_caps_to_string (caps);
235 GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str);
238 /* This is needed at least in case of RTP
239 * Parses the codec_data information to get ObjectType,
240 * number of channels and samplerate */
241 value = gst_structure_get_value (structure, "codec_data");
243 GstBuffer *buf = gst_value_get_buffer (value);
249 gst_buffer_map (buf, &map, GST_MAP_READ);
251 sr_idx = ((map.data[0] & 0x07) << 1) | ((map.data[1] & 0x80) >> 7);
252 aacparse->object_type = (map.data[0] & 0xf8) >> 3;
253 aacparse->sample_rate = gst_aac_parse_get_sample_rate_from_index (sr_idx);
254 aacparse->channels = (map.data[1] & 0x78) >> 3;
255 aacparse->header_type = DSPAAC_HEADER_NONE;
256 aacparse->mpegversion = 4;
257 aacparse->frame_samples = (map.data[1] & 4) ? 960 : 1024;
258 gst_buffer_unmap (buf, &map);
260 GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d, "
261 "samples=%d", aacparse->object_type, aacparse->sample_rate,
262 aacparse->channels, aacparse->frame_samples);
264 /* arrange for metadata and get out of the way */
265 gst_aac_parse_set_src_caps (aacparse, caps);
266 gst_base_parse_set_passthrough (parse, TRUE);
270 /* caps info overrides */
271 gst_structure_get_int (structure, "rate", &aacparse->sample_rate);
272 gst_structure_get_int (structure, "channels", &aacparse->channels);
274 gst_base_parse_set_passthrough (parse, FALSE);
282 * gst_aac_parse_adts_get_frame_len:
283 * @data: block of data containing an ADTS header.
285 * This function calculates ADTS frame length from the given header.
287 * Returns: size of the ADTS frame.
290 gst_aac_parse_adts_get_frame_len (const guint8 * data)
292 return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5);
297 * gst_aac_parse_check_adts_frame:
298 * @aacparse: #GstAacParse.
299 * @data: Data to be checked.
300 * @avail: Amount of data passed.
301 * @framesize: If valid ADTS frame was found, this will be set to tell the
302 * found frame size in bytes.
303 * @needed_data: If frame was not found, this may be set to tell how much
304 * more data is needed in the next round to detect the frame
305 * reliably. This may happen when a frame header candidate
306 * is found but it cannot be guaranteed to be the header without
307 * peeking the following data.
309 * Check if the given data contains contains ADTS frame. The algorithm
310 * will examine ADTS frame header and calculate the frame size. Also, another
311 * consecutive ADTS frame header need to be present after the found frame.
312 * Otherwise the data is not considered as a valid ADTS frame. However, this
313 * "extra check" is omitted when EOS has been received. In this case it is
314 * enough when data[0] contains a valid ADTS header.
316 * This function may set the #needed_data to indicate that a possible frame
317 * candidate has been found, but more data (#needed_data bytes) is needed to
318 * be absolutely sure. When this situation occurs, FALSE will be returned.
320 * When a valid frame is detected, this function will use
321 * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
322 * to set the needed bytes for next frame.This way next data chunk is already
325 * Returns: TRUE if the given data contains a valid ADTS header.
328 gst_aac_parse_check_adts_frame (GstAacParse * aacparse,
329 const guint8 * data, const guint avail, gboolean drain,
330 guint * framesize, guint * needed_data)
334 if (G_UNLIKELY (avail < 2))
337 if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) {
338 *framesize = gst_aac_parse_adts_get_frame_len (data);
340 /* In EOS mode this is enough. No need to examine the data further.
341 We also relax the check when we have sync, on the assumption that
342 if we're not looking at random data, we have a much higher chance
343 to get the correct sync, and this avoids losing two frames when
344 a single bit corruption happens. */
345 if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
349 if (*framesize + ADTS_MAX_SIZE > avail) {
350 /* We have found a possible frame header candidate, but can't be
351 sure since we don't have enough data to check the next frame */
352 GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
353 *framesize + ADTS_MAX_SIZE, avail);
354 *needed_data = *framesize + ADTS_MAX_SIZE;
355 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
356 *framesize + ADTS_MAX_SIZE);
360 if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) {
361 guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize));
363 GST_LOG ("ADTS frame found, len: %d bytes", *framesize);
364 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
365 nextlen + ADTS_MAX_SIZE);
373 gst_aac_parse_latm_get_value (GstAacParse * aacparse, GstBitReader * br,
376 guint8 bytes, i, byte;
379 if (!gst_bit_reader_get_bits_uint8 (br, &bytes, 2))
381 for (i = 0; i < bytes; ++i) {
383 if (!gst_bit_reader_get_bits_uint8 (br, &byte, 8))
391 gst_aac_parse_get_audio_object_type (GstAacParse * aacparse, GstBitReader * br,
392 guint8 * audio_object_type)
394 if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 5))
396 if (*audio_object_type == 31) {
397 if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 6))
399 *audio_object_type += 32;
401 GST_LOG_OBJECT (aacparse, "audio object type %u", *audio_object_type);
406 gst_aac_parse_get_audio_sample_rate (GstAacParse * aacparse, GstBitReader * br,
409 guint8 sampling_frequency_index;
410 if (!gst_bit_reader_get_bits_uint8 (br, &sampling_frequency_index, 4))
412 GST_LOG_OBJECT (aacparse, "sampling_frequency_index: %u",
413 sampling_frequency_index);
414 if (sampling_frequency_index == 0xf) {
415 guint32 sampling_rate;
416 if (!gst_bit_reader_get_bits_uint32 (br, &sampling_rate, 24))
418 *sample_rate = sampling_rate;
420 *sample_rate = loas_sample_rate_table[sampling_frequency_index];
427 /* See table 1.13 in ISO/IEC 14496-3 */
429 gst_aac_parse_read_loas_audio_specific_config (GstAacParse * aacparse,
430 GstBitReader * br, gint * sample_rate, gint * channels, guint32 * bits)
432 guint8 audio_object_type, channel_configuration;
434 if (!gst_aac_parse_get_audio_object_type (aacparse, br, &audio_object_type))
437 if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
440 if (!gst_bit_reader_get_bits_uint8 (br, &channel_configuration, 4))
442 GST_LOG_OBJECT (aacparse, "channel_configuration: %d", channel_configuration);
443 *channels = loas_channels_table[channel_configuration];
447 if (audio_object_type == 5) {
448 GST_LOG_OBJECT (aacparse,
449 "Audio object type 5, so rereading sampling rate...");
450 if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
454 GST_INFO_OBJECT (aacparse, "Found LOAS config: %d Hz, %d channels",
455 *sample_rate, *channels);
457 /* There's LOTS of stuff next, but we ignore it for now as we have
458 what we want (sample rate and number of channels */
459 GST_DEBUG_OBJECT (aacparse,
460 "Need more code to parse humongous LOAS data, currently ignored");
468 gst_aac_parse_read_loas_config (GstAacParse * aacparse, const guint8 * data,
469 guint avail, gint * sample_rate, gint * channels, gint * version)
474 /* No version in the bitstream, but the spec has LOAS in the MPEG-4 section */
478 gst_bit_reader_init (&br, data, avail);
480 /* skip sync word (11 bits) and size (13 bits) */
481 if (!gst_bit_reader_skip (&br, 11 + 13))
484 /* First bit is "use last config" */
485 if (!gst_bit_reader_get_bits_uint8 (&br, &u8, 1))
488 GST_DEBUG_OBJECT (aacparse, "Frame uses previous config");
489 if (!aacparse->sample_rate || !aacparse->channels) {
490 GST_WARNING_OBJECT (aacparse, "No previous config to use");
492 *sample_rate = aacparse->sample_rate;
493 *channels = aacparse->channels;
497 GST_DEBUG_OBJECT (aacparse, "Frame contains new config");
499 if (!gst_bit_reader_get_bits_uint8 (&br, &v, 1))
502 if (!gst_bit_reader_get_bits_uint8 (&br, &vA, 1))
507 GST_LOG_OBJECT (aacparse, "v %d, vA %d", v, vA);
509 guint8 same_time, subframes, num_program, prog;
512 if (!gst_aac_parse_latm_get_value (aacparse, &br, &value))
515 if (!gst_bit_reader_get_bits_uint8 (&br, &same_time, 1))
517 if (!gst_bit_reader_get_bits_uint8 (&br, &subframes, 6))
519 if (!gst_bit_reader_get_bits_uint8 (&br, &num_program, 4))
521 GST_LOG_OBJECT (aacparse, "same_time %d, subframes %d, num_program %d",
522 same_time, subframes, num_program);
524 for (prog = 0; prog <= num_program; ++prog) {
525 guint8 num_layer, layer;
526 if (!gst_bit_reader_get_bits_uint8 (&br, &num_layer, 3))
528 GST_LOG_OBJECT (aacparse, "Program %d: %d layers", prog, num_layer);
530 for (layer = 0; layer <= num_layer; ++layer) {
531 guint8 use_same_config;
532 if (prog == 0 && layer == 0) {
535 if (!gst_bit_reader_get_bits_uint8 (&br, &use_same_config, 1))
538 if (!use_same_config) {
540 if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
541 sample_rate, channels, NULL))
544 guint32 bits, asc_len;
545 if (!gst_aac_parse_latm_get_value (aacparse, &br, &asc_len))
547 if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
548 sample_rate, channels, &bits))
551 if (!gst_bit_reader_skip (&br, asc_len))
557 GST_WARNING_OBJECT (aacparse, "More data ignored");
559 GST_WARNING_OBJECT (aacparse, "Spec says \"TBD\"...");
565 * gst_aac_parse_loas_get_frame_len:
566 * @data: block of data containing a LOAS header.
568 * This function calculates LOAS frame length from the given header.
570 * Returns: size of the LOAS frame.
573 gst_aac_parse_loas_get_frame_len (const guint8 * data)
575 return (((data[1] & 0x1f) << 8) | data[2]) + 3;
580 * gst_aac_parse_check_loas_frame:
581 * @aacparse: #GstAacParse.
582 * @data: Data to be checked.
583 * @avail: Amount of data passed.
584 * @framesize: If valid LOAS frame was found, this will be set to tell the
585 * found frame size in bytes.
586 * @needed_data: If frame was not found, this may be set to tell how much
587 * more data is needed in the next round to detect the frame
588 * reliably. This may happen when a frame header candidate
589 * is found but it cannot be guaranteed to be the header without
590 * peeking the following data.
592 * Check if the given data contains contains LOAS frame. The algorithm
593 * will examine LOAS frame header and calculate the frame size. Also, another
594 * consecutive LOAS frame header need to be present after the found frame.
595 * Otherwise the data is not considered as a valid LOAS frame. However, this
596 * "extra check" is omitted when EOS has been received. In this case it is
597 * enough when data[0] contains a valid LOAS header.
599 * This function may set the #needed_data to indicate that a possible frame
600 * candidate has been found, but more data (#needed_data bytes) is needed to
601 * be absolutely sure. When this situation occurs, FALSE will be returned.
603 * When a valid frame is detected, this function will use
604 * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
605 * to set the needed bytes for next frame.This way next data chunk is already
608 * LOAS can have three different formats, if I read the spec correctly. Only
609 * one of them is supported here, as the two samples I have use this one.
611 * Returns: TRUE if the given data contains a valid LOAS header.
614 gst_aac_parse_check_loas_frame (GstAacParse * aacparse,
615 const guint8 * data, const guint avail, gboolean drain,
616 guint * framesize, guint * needed_data)
621 if (G_UNLIKELY (avail < 3))
624 if ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) {
625 *framesize = gst_aac_parse_loas_get_frame_len (data);
626 GST_DEBUG_OBJECT (aacparse, "Found %u byte LOAS frame", *framesize);
628 /* In EOS mode this is enough. No need to examine the data further.
629 We also relax the check when we have sync, on the assumption that
630 if we're not looking at random data, we have a much higher chance
631 to get the correct sync, and this avoids losing two frames when
632 a single bit corruption happens. */
633 if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
637 if (*framesize + LOAS_MAX_SIZE > avail) {
638 /* We have found a possible frame header candidate, but can't be
639 sure since we don't have enough data to check the next frame */
640 GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
641 *framesize + LOAS_MAX_SIZE, avail);
642 *needed_data = *framesize + LOAS_MAX_SIZE;
643 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
644 *framesize + LOAS_MAX_SIZE);
648 if ((data[*framesize] == 0x56) && ((data[*framesize + 1] & 0xe0) == 0xe0)) {
649 guint nextlen = gst_aac_parse_loas_get_frame_len (data + (*framesize));
651 GST_LOG ("LOAS frame found, len: %d bytes", *framesize);
652 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
653 nextlen + LOAS_MAX_SIZE);
660 /* caller ensure sufficient data */
662 gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data,
663 gint * rate, gint * channels, gint * object, gint * version)
667 gint sr_idx = (data[2] & 0x3c) >> 2;
669 *rate = gst_aac_parse_get_sample_rate_from_index (sr_idx);
672 *channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6);
675 *version = (data[1] & 0x08) ? 2 : 4;
677 *object = (data[2] & 0xc0) >> 6;
681 * gst_aac_parse_detect_stream:
682 * @aacparse: #GstAacParse.
683 * @data: A block of data that needs to be examined for stream characteristics.
684 * @avail: Size of the given datablock.
685 * @framesize: If valid stream was found, this will be set to tell the
686 * first frame size in bytes.
687 * @skipsize: If valid stream was found, this will be set to tell the first
688 * audio frame position within the given data.
690 * Examines the given piece of data and try to detect the format of it. It
691 * checks for "ADIF" header (in the beginning of the clip) and ADTS frame
692 * header. If the stream is detected, TRUE will be returned and #framesize
693 * is set to indicate the found frame size. Additionally, #skipsize might
694 * be set to indicate the number of bytes that need to be skipped, a.k.a. the
695 * position of the frame inside given data chunk.
697 * Returns: TRUE on success.
700 gst_aac_parse_detect_stream (GstAacParse * aacparse,
701 const guint8 * data, const guint avail, gboolean drain,
702 guint * framesize, gint * skipsize)
704 gboolean found = FALSE;
705 guint need_data_adts = 0, need_data_loas;
708 GST_DEBUG_OBJECT (aacparse, "Parsing header data");
710 /* FIXME: No need to check for ADIF if we are not in the beginning of the
713 /* Can we even parse the header? */
714 if (avail < MAX (ADTS_MAX_SIZE, LOAS_MAX_SIZE)) {
715 GST_DEBUG_OBJECT (aacparse, "Not enough data to check");
719 for (i = 0; i < avail - 4; i++) {
720 if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) ||
721 ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) ||
722 strncmp ((char *) data + i, "ADIF", 4) == 0) {
723 GST_DEBUG_OBJECT (aacparse, "Found ADIF signature at offset %u", i);
727 /* Trick: tell the parent class that we didn't find the frame yet,
728 but make it skip 'i' amount of bytes. Next time we arrive
729 here we have full frame in the beginning of the data. */
742 if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain,
743 framesize, &need_data_adts)) {
746 GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize);
748 aacparse->header_type = DSPAAC_HEADER_ADTS;
749 gst_aac_parse_parse_adts_header (aacparse, data, &rate, &channels,
750 &aacparse->object_type, &aacparse->mpegversion);
752 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
753 aacparse->frame_samples, 2, 2);
755 GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d",
756 rate, channels, aacparse->object_type, aacparse->mpegversion);
758 gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
763 if (gst_aac_parse_check_loas_frame (aacparse, data, avail, drain,
764 framesize, &need_data_loas)) {
767 GST_INFO ("LOAS, framesize: %d", *framesize);
769 aacparse->header_type = DSPAAC_HEADER_LOAS;
771 if (!gst_aac_parse_read_loas_config (aacparse, data, avail, &rate,
772 &channels, &aacparse->mpegversion)) {
773 GST_WARNING_OBJECT (aacparse, "Error reading LOAS config");
777 if (rate && channels) {
778 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
779 aacparse->frame_samples, 2, 2);
781 GST_DEBUG ("LOAS: samplerate %d, channels %d, objtype %d, version %d",
782 rate, channels, aacparse->object_type, aacparse->mpegversion);
783 aacparse->sample_rate = rate;
784 aacparse->channels = channels;
787 gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
792 if (need_data_adts || need_data_loas) {
793 /* This tells the parent class not to skip any data */
798 if (avail < ADIF_MAX_SIZE)
801 if (memcmp (data + i, "ADIF", 4) == 0) {
808 aacparse->header_type = DSPAAC_HEADER_ADIF;
809 aacparse->mpegversion = 4;
811 /* Skip the "ADIF" bytes */
814 /* copyright string */
816 skip_size += 9; /* skip 9 bytes */
818 bitstream_type = adif[0 + skip_size] & 0x10;
820 ((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) |
821 ((unsigned int) adif[1 + skip_size] << 11) |
822 ((unsigned int) adif[2 + skip_size] << 3) |
823 ((unsigned int) adif[3 + skip_size] & 0xe0);
826 if (bitstream_type == 0) {
828 /* Buffer fullness parsing. Currently not needed... */
832 num_elems = (adif[3 + skip_size] & 0x1e);
833 GST_INFO ("ADIF num_config_elems: %d", num_elems);
835 fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) |
836 ((unsigned int) adif[4 + skip_size] << 11) |
837 ((unsigned int) adif[5 + skip_size] << 3) |
838 ((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5);
840 GST_INFO ("ADIF buffer fullness: %d", fullness);
842 aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) |
843 ((adif[7 + skip_size] & 0x80) >> 7);
844 sr_idx = (adif[7 + skip_size] & 0x78) >> 3;
848 aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3;
849 sr_idx = ((adif[4 + skip_size] & 0x07) << 1) |
850 ((adif[5 + skip_size] & 0x80) >> 7);
853 /* FIXME: This gives totally wrong results. Duration calculation cannot
855 aacparse->sample_rate = gst_aac_parse_get_sample_rate_from_index (sr_idx);
857 /* baseparse is not given any fps,
858 * so it will give up on timestamps, seeking, etc */
860 /* FIXME: Can we assume this? */
861 aacparse->channels = 2;
863 GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d",
864 aacparse->bitrate, aacparse->sample_rate, aacparse->object_type);
866 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512);
868 /* arrange for metadata and get out of the way */
869 sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (aacparse));
870 gst_aac_parse_set_src_caps (aacparse, sinkcaps);
872 gst_caps_unref (sinkcaps);
874 /* not syncable, not easily seekable (unless we push data from start */
875 gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE);
876 gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE);
877 gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0);
883 /* This should never happen */
889 * gst_aac_parse_check_valid_frame:
890 * @parse: #GstBaseParse.
891 * @frame: #GstBaseParseFrame.
892 * @skipsize: How much data parent class should skip in order to find the
895 * Implementation of "handle_frame" vmethod in #GstBaseParse class.
897 * Also determines frame overhead.
898 * ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have
899 * a per-frame header. LOAS has 3 bytes.
901 * We're making a couple of simplifying assumptions:
903 * 1. We count Program Configuration Elements rather than searching for them
904 * in the streams to discount them - the overhead is negligible.
906 * 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16
907 * bits, which should still not be significant enough to warrant the
908 * additional parsing through the headers
910 * Returns: a #GstFlowReturn.
913 gst_aac_parse_handle_frame (GstBaseParse * parse,
914 GstBaseParseFrame * frame, gint * skipsize)
917 GstAacParse *aacparse;
918 gboolean ret = FALSE;
924 aacparse = GST_AAC_PARSE (parse);
925 buffer = frame->buffer;
927 gst_buffer_map (buffer, &map, GST_MAP_READ);
930 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
932 if (aacparse->header_type == DSPAAC_HEADER_ADIF ||
933 aacparse->header_type == DSPAAC_HEADER_NONE) {
934 /* There is nothing to parse */
935 framesize = map.size;
938 } else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) {
940 ret = gst_aac_parse_detect_stream (aacparse, map.data, map.size,
941 GST_BASE_PARSE_DRAINING (parse), &framesize, skipsize);
943 } else if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
944 guint needed_data = 1024;
946 ret = gst_aac_parse_check_adts_frame (aacparse, map.data, map.size,
947 GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
950 GST_DEBUG ("buffer didn't contain valid frame");
951 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
955 } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
956 guint needed_data = 1024;
958 ret = gst_aac_parse_check_loas_frame (aacparse, map.data,
959 map.size, GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
962 GST_DEBUG ("buffer didn't contain valid frame");
963 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
968 GST_DEBUG ("buffer didn't contain valid frame");
969 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
973 if (G_UNLIKELY (!ret))
976 if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
980 gst_aac_parse_parse_adts_header (aacparse, map.data,
981 &rate, &channels, NULL, NULL);
983 GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels);
985 if (G_UNLIKELY (rate != aacparse->sample_rate
986 || channels != aacparse->channels)) {
987 aacparse->sample_rate = rate;
988 aacparse->channels = channels;
990 GST_DEBUG_OBJECT (aacparse, "here");
992 if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
993 /* If linking fails, we need to return appropriate error */
994 ret = GST_FLOW_NOT_LINKED;
997 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
998 aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1000 } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1001 gboolean setcaps = FALSE;
1004 frame->overhead = 3;
1006 if (!gst_aac_parse_read_loas_config (aacparse, map.data, map.size, &rate,
1008 GST_WARNING_OBJECT (aacparse, "Error reading LOAS config");
1009 } else if (G_UNLIKELY (rate != aacparse->sample_rate
1010 || channels != aacparse->channels)) {
1011 aacparse->sample_rate = rate;
1012 aacparse->channels = channels;
1014 GST_INFO_OBJECT (aacparse, "New LOAS config: %d Hz, %d channels", rate,
1018 /* We want to set caps both at start, and when rate/channels change.
1019 Since only some LOAS frames have that info, we may receive frames
1020 before knowing about rate/channels. */
1022 || !gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse))) {
1023 if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1024 /* If linking fails, we need to return appropriate error */
1025 ret = GST_FLOW_NOT_LINKED;
1028 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1029 aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1034 gst_buffer_unmap (buffer, &map);
1037 /* found, skip if needed */
1046 if (ret && framesize <= map.size) {
1047 return gst_base_parse_finish_frame (parse, frame, framesize);
1055 * gst_aac_parse_start:
1056 * @parse: #GstBaseParse.
1058 * Implementation of "start" vmethod in #GstBaseParse class.
1060 * Returns: TRUE if startup succeeded.
1063 gst_aac_parse_start (GstBaseParse * parse)
1065 GstAacParse *aacparse;
1067 aacparse = GST_AAC_PARSE (parse);
1068 GST_DEBUG ("start");
1069 aacparse->frame_samples = 1024;
1070 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), ADTS_MAX_SIZE);
1076 * gst_aac_parse_stop:
1077 * @parse: #GstBaseParse.
1079 * Implementation of "stop" vmethod in #GstBaseParse class.
1081 * Returns: TRUE is stopping succeeded.
1084 gst_aac_parse_stop (GstBaseParse * parse)
1091 gst_aac_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter)
1093 GstCaps *peercaps, *templ;
1096 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1097 peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
1101 /* Remove the framed field */
1102 peercaps = gst_caps_make_writable (peercaps);
1103 n = gst_caps_get_size (peercaps);
1104 for (i = 0; i < n; i++) {
1105 GstStructure *s = gst_caps_get_structure (peercaps, i);
1107 gst_structure_remove_field (s, "framed");
1110 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1111 gst_caps_unref (peercaps);
1113 /* Append the template caps because we still want to accept
1114 * caps without any fields in the case upstream does not
1117 gst_caps_append (res, templ);
1123 GstCaps *intersection;
1126 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1127 gst_caps_unref (res);