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/pbutils.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 */
72 #define ADTS_HEADERS_LENGTH 7UL /* Total byte-length of fixed and variable
73 headers prepended during raw to ADTS
76 #define AAC_FRAME_DURATION(parse) (GST_SECOND/parse->frames_per_sec)
78 static const gint loas_sample_rate_table[16] = {
79 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
80 16000, 12000, 11025, 8000, 7350, 0, 0, 0
83 static const gint loas_channels_table[16] = {
84 0, 1, 2, 3, 4, 5, 6, 8,
85 0, 0, 0, 7, 8, 0, 8, 0
88 static gboolean gst_aac_parse_start (GstBaseParse * parse);
89 static gboolean gst_aac_parse_stop (GstBaseParse * parse);
91 static gboolean gst_aac_parse_sink_setcaps (GstBaseParse * parse,
93 static GstCaps *gst_aac_parse_sink_getcaps (GstBaseParse * parse,
96 static GstFlowReturn gst_aac_parse_handle_frame (GstBaseParse * parse,
97 GstBaseParseFrame * frame, gint * skipsize);
98 static GstFlowReturn gst_aac_parse_pre_push_frame (GstBaseParse * parse,
99 GstBaseParseFrame * frame);
100 static gboolean gst_aac_parse_src_event (GstBaseParse * parse,
103 #define gst_aac_parse_parent_class parent_class
104 G_DEFINE_TYPE (GstAacParse, gst_aac_parse, GST_TYPE_BASE_PARSE);
107 * gst_aac_parse_class_init:
108 * @klass: #GstAacParseClass.
112 gst_aac_parse_class_init (GstAacParseClass * klass)
114 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
115 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
117 GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0,
118 "AAC audio stream parser");
120 gst_element_class_add_static_pad_template (element_class, &sink_template);
121 gst_element_class_add_static_pad_template (element_class, &src_template);
123 gst_element_class_set_static_metadata (element_class,
124 "AAC audio stream parser", "Codec/Parser/Audio",
125 "Advanced Audio Coding parser", "Stefan Kost <stefan.kost@nokia.com>");
127 parse_class->start = GST_DEBUG_FUNCPTR (gst_aac_parse_start);
128 parse_class->stop = GST_DEBUG_FUNCPTR (gst_aac_parse_stop);
129 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_setcaps);
130 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_getcaps);
131 parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_handle_frame);
132 parse_class->pre_push_frame =
133 GST_DEBUG_FUNCPTR (gst_aac_parse_pre_push_frame);
134 parse_class->src_event = GST_DEBUG_FUNCPTR (gst_aac_parse_src_event);
139 * gst_aac_parse_init:
140 * @aacparse: #GstAacParse.
141 * @klass: #GstAacParseClass.
145 gst_aac_parse_init (GstAacParse * aacparse)
147 GST_DEBUG ("initialized");
148 GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (aacparse));
149 GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (aacparse));
151 aacparse->last_parsed_sample_rate = 0;
152 aacparse->last_parsed_channels = 0;
157 * gst_aac_parse_set_src_caps:
158 * @aacparse: #GstAacParse.
159 * @sink_caps: (proposed) caps of sink pad
161 * Set source pad caps according to current knowledge about the
164 * Returns: TRUE if caps were successfully set.
167 gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps)
170 GstCaps *src_caps = NULL, *allowed;
171 gboolean res = FALSE;
172 const gchar *stream_format;
173 guint8 codec_data[2];
174 guint16 codec_data_data;
175 gint sample_rate_idx;
177 GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps);
179 src_caps = gst_caps_copy (sink_caps);
181 src_caps = gst_caps_new_empty_simple ("audio/mpeg");
183 gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE,
184 "mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL);
186 aacparse->output_header_type = aacparse->header_type;
187 switch (aacparse->header_type) {
188 case DSPAAC_HEADER_NONE:
189 stream_format = "raw";
191 case DSPAAC_HEADER_ADTS:
192 stream_format = "adts";
194 case DSPAAC_HEADER_ADIF:
195 stream_format = "adif";
197 case DSPAAC_HEADER_LOAS:
198 stream_format = "loas";
201 stream_format = NULL;
204 /* Generate codec data to be able to set profile/level on the caps */
206 gst_codec_utils_aac_get_index_from_sample_rate (aacparse->sample_rate);
207 if (sample_rate_idx < 0)
208 goto not_a_known_rate;
210 (aacparse->object_type << 11) |
211 (sample_rate_idx << 7) | (aacparse->channels << 3);
212 GST_WRITE_UINT16_BE (codec_data, codec_data_data);
213 gst_codec_utils_aac_caps_set_level_and_profile (src_caps, codec_data, 2);
215 s = gst_caps_get_structure (src_caps, 0);
216 if (aacparse->sample_rate > 0)
217 gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL);
218 if (aacparse->channels > 0)
219 gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL);
221 gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL);
223 allowed = gst_pad_get_allowed_caps (GST_BASE_PARSE (aacparse)->srcpad);
224 if (allowed && !gst_caps_can_intersect (src_caps, allowed)) {
225 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
226 "Caps can not intersect");
227 if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
228 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
229 "Input is ADTS, trying raw");
230 gst_caps_set_simple (src_caps, "stream-format", G_TYPE_STRING, "raw",
232 if (gst_caps_can_intersect (src_caps, allowed)) {
233 GstBuffer *codec_data_buffer;
235 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
236 "Caps can intersect, we will drop the ADTS layer");
237 aacparse->output_header_type = DSPAAC_HEADER_NONE;
239 /* The codec_data data is according to AudioSpecificConfig,
240 ISO/IEC 14496-3, 1.6.2.1 */
241 codec_data_buffer = gst_buffer_new_and_alloc (2);
242 gst_buffer_fill (codec_data_buffer, 0, codec_data, 2);
243 gst_caps_set_simple (src_caps, "codec_data", GST_TYPE_BUFFER,
244 codec_data_buffer, NULL);
246 } else if (aacparse->header_type == DSPAAC_HEADER_NONE) {
247 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
248 "Input is raw, trying ADTS");
249 gst_caps_set_simple (src_caps, "stream-format", G_TYPE_STRING, "adts",
251 if (gst_caps_can_intersect (src_caps, allowed)) {
252 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
253 "Caps can intersect, we will prepend ADTS headers");
254 aacparse->output_header_type = DSPAAC_HEADER_ADTS;
259 gst_caps_unref (allowed);
261 aacparse->last_parsed_channels = 0;
262 aacparse->last_parsed_sample_rate = 0;
264 GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps);
266 res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps);
267 gst_caps_unref (src_caps);
271 GST_ERROR_OBJECT (aacparse, "Not a known sample rate: %d",
272 aacparse->sample_rate);
273 gst_caps_unref (src_caps);
279 * gst_aac_parse_sink_setcaps:
283 * Implementation of "set_sink_caps" vmethod in #GstBaseParse class.
285 * Returns: TRUE on success.
288 gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
290 GstAacParse *aacparse;
291 GstStructure *structure;
295 aacparse = GST_AAC_PARSE (parse);
296 structure = gst_caps_get_structure (caps, 0);
297 caps_str = gst_caps_to_string (caps);
299 GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str);
302 /* This is needed at least in case of RTP
303 * Parses the codec_data information to get ObjectType,
304 * number of channels and samplerate */
305 value = gst_structure_get_value (structure, "codec_data");
307 GstBuffer *buf = gst_value_get_buffer (value);
313 gst_buffer_map (buf, &map, GST_MAP_READ);
315 sr_idx = ((map.data[0] & 0x07) << 1) | ((map.data[1] & 0x80) >> 7);
316 aacparse->object_type = (map.data[0] & 0xf8) >> 3;
317 aacparse->sample_rate =
318 gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
319 aacparse->channels = (map.data[1] & 0x78) >> 3;
320 if (aacparse->channels == 7)
321 aacparse->channels = 8;
322 else if (aacparse->channels == 11)
323 aacparse->channels = 7;
324 else if (aacparse->channels == 12 || aacparse->channels == 14)
325 aacparse->channels = 8;
326 aacparse->header_type = DSPAAC_HEADER_NONE;
327 aacparse->mpegversion = 4;
328 aacparse->frame_samples = (map.data[1] & 4) ? 960 : 1024;
329 gst_buffer_unmap (buf, &map);
331 GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d, "
332 "samples=%d", aacparse->object_type, aacparse->sample_rate,
333 aacparse->channels, aacparse->frame_samples);
335 /* arrange for metadata and get out of the way */
336 gst_aac_parse_set_src_caps (aacparse, caps);
337 if (aacparse->header_type == aacparse->output_header_type)
338 gst_base_parse_set_passthrough (parse, TRUE);
343 /* caps info overrides */
344 gst_structure_get_int (structure, "rate", &aacparse->sample_rate);
345 gst_structure_get_int (structure, "channels", &aacparse->channels);
347 const gchar *stream_format =
348 gst_structure_get_string (structure, "stream-format");
350 if (g_strcmp0 (stream_format, "raw") == 0) {
351 GST_ERROR_OBJECT (parse, "Need codec_data for raw AAC");
354 aacparse->sample_rate = 0;
355 aacparse->channels = 0;
356 aacparse->header_type = DSPAAC_HEADER_NOT_PARSED;
357 gst_base_parse_set_passthrough (parse, FALSE);
365 * gst_aac_parse_adts_get_frame_len:
366 * @data: block of data containing an ADTS header.
368 * This function calculates ADTS frame length from the given header.
370 * Returns: size of the ADTS frame.
373 gst_aac_parse_adts_get_frame_len (const guint8 * data)
375 return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5);
380 * gst_aac_parse_check_adts_frame:
381 * @aacparse: #GstAacParse.
382 * @data: Data to be checked.
383 * @avail: Amount of data passed.
384 * @framesize: If valid ADTS frame was found, this will be set to tell the
385 * found frame size in bytes.
386 * @needed_data: If frame was not found, this may be set to tell how much
387 * more data is needed in the next round to detect the frame
388 * reliably. This may happen when a frame header candidate
389 * is found but it cannot be guaranteed to be the header without
390 * peeking the following data.
392 * Check if the given data contains contains ADTS frame. The algorithm
393 * will examine ADTS frame header and calculate the frame size. Also, another
394 * consecutive ADTS frame header need to be present after the found frame.
395 * Otherwise the data is not considered as a valid ADTS frame. However, this
396 * "extra check" is omitted when EOS has been received. In this case it is
397 * enough when data[0] contains a valid ADTS header.
399 * This function may set the #needed_data to indicate that a possible frame
400 * candidate has been found, but more data (#needed_data bytes) is needed to
401 * be absolutely sure. When this situation occurs, FALSE will be returned.
403 * When a valid frame is detected, this function will use
404 * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
405 * to set the needed bytes for next frame.This way next data chunk is already
408 * Returns: TRUE if the given data contains a valid ADTS header.
411 gst_aac_parse_check_adts_frame (GstAacParse * aacparse,
412 const guint8 * data, const guint avail, gboolean drain,
413 guint * framesize, guint * needed_data)
419 /* Absolute minimum to perform the ADTS syncword,
420 layer and sampling frequency tests */
421 if (G_UNLIKELY (avail < 3)) {
426 /* Syncword and layer tests */
427 if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) {
429 /* Sampling frequency test */
430 if (G_UNLIKELY ((data[2] & 0x3C) >> 2 == 15))
433 /* This looks like an ADTS frame header but
434 we need at least 6 bytes to proceed */
435 if (G_UNLIKELY (avail < 6)) {
440 *framesize = gst_aac_parse_adts_get_frame_len (data);
442 /* If frame has CRC, it needs 2 bytes
443 for it at the end of the header */
444 crc_size = (data[1] & 0x01) ? 0 : 2;
447 if (*framesize < 7 + crc_size) {
448 *needed_data = 7 + crc_size;
452 /* In EOS mode this is enough. No need to examine the data further.
453 We also relax the check when we have sync, on the assumption that
454 if we're not looking at random data, we have a much higher chance
455 to get the correct sync, and this avoids losing two frames when
456 a single bit corruption happens. */
457 if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
461 if (*framesize + ADTS_MAX_SIZE > avail) {
462 /* We have found a possible frame header candidate, but can't be
463 sure since we don't have enough data to check the next frame */
464 GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
465 *framesize + ADTS_MAX_SIZE, avail);
466 *needed_data = *framesize + ADTS_MAX_SIZE;
467 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
468 *framesize + ADTS_MAX_SIZE);
472 if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) {
473 guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize));
475 GST_LOG ("ADTS frame found, len: %d bytes", *framesize);
476 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
477 nextlen + ADTS_MAX_SIZE);
485 gst_aac_parse_latm_get_value (GstAacParse * aacparse, GstBitReader * br,
488 guint8 bytes, i, byte;
491 if (!gst_bit_reader_get_bits_uint8 (br, &bytes, 2))
493 for (i = 0; i <= bytes; ++i) {
495 if (!gst_bit_reader_get_bits_uint8 (br, &byte, 8))
503 gst_aac_parse_get_audio_object_type (GstAacParse * aacparse, GstBitReader * br,
504 guint8 * audio_object_type)
506 if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 5))
508 if (*audio_object_type == 31) {
509 if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 6))
511 *audio_object_type += 32;
513 GST_LOG_OBJECT (aacparse, "audio object type %u", *audio_object_type);
518 gst_aac_parse_get_audio_sample_rate (GstAacParse * aacparse, GstBitReader * br,
521 guint8 sampling_frequency_index;
522 if (!gst_bit_reader_get_bits_uint8 (br, &sampling_frequency_index, 4))
524 GST_LOG_OBJECT (aacparse, "sampling_frequency_index: %u",
525 sampling_frequency_index);
526 if (sampling_frequency_index == 0xf) {
527 guint32 sampling_rate;
528 if (!gst_bit_reader_get_bits_uint32 (br, &sampling_rate, 24))
530 *sample_rate = sampling_rate;
532 *sample_rate = loas_sample_rate_table[sampling_frequency_index];
536 aacparse->last_parsed_sample_rate = *sample_rate;
540 /* See table 1.13 in ISO/IEC 14496-3 */
542 gst_aac_parse_read_loas_audio_specific_config (GstAacParse * aacparse,
543 GstBitReader * br, gint * sample_rate, gint * channels, guint32 * bits)
545 guint8 audio_object_type, channel_configuration;
547 if (!gst_aac_parse_get_audio_object_type (aacparse, br, &audio_object_type))
550 if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
553 if (!gst_bit_reader_get_bits_uint8 (br, &channel_configuration, 4))
555 GST_LOG_OBJECT (aacparse, "channel_configuration: %d", channel_configuration);
556 *channels = loas_channels_table[channel_configuration];
560 if (audio_object_type == 5) {
561 GST_LOG_OBJECT (aacparse,
562 "Audio object type 5, so rereading sampling rate...");
563 if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
567 GST_INFO_OBJECT (aacparse, "Found LOAS config: %d Hz, %d channels",
568 *sample_rate, *channels);
570 /* There's LOTS of stuff next, but we ignore it for now as we have
571 what we want (sample rate and number of channels */
572 GST_DEBUG_OBJECT (aacparse,
573 "Need more code to parse humongous LOAS data, currently ignored");
576 aacparse->last_parsed_channels = *channels;
582 gst_aac_parse_read_loas_config (GstAacParse * aacparse, const guint8 * data,
583 guint avail, gint * sample_rate, gint * channels, gint * version)
588 /* No version in the bitstream, but the spec has LOAS in the MPEG-4 section */
592 gst_bit_reader_init (&br, data, avail);
594 /* skip sync word (11 bits) and size (13 bits) */
595 if (!gst_bit_reader_skip (&br, 11 + 13))
598 /* First bit is "use last config" */
599 if (!gst_bit_reader_get_bits_uint8 (&br, &u8, 1))
602 GST_LOG_OBJECT (aacparse, "Frame uses previous config");
603 if (!aacparse->last_parsed_sample_rate || !aacparse->last_parsed_channels) {
604 GST_DEBUG_OBJECT (aacparse,
605 "No previous config to use. We'll look for more data.");
608 *sample_rate = aacparse->last_parsed_sample_rate;
609 *channels = aacparse->last_parsed_channels;
613 GST_DEBUG_OBJECT (aacparse, "Frame contains new config");
615 if (!gst_bit_reader_get_bits_uint8 (&br, &v, 1))
618 if (!gst_bit_reader_get_bits_uint8 (&br, &vA, 1))
623 GST_LOG_OBJECT (aacparse, "v %d, vA %d", v, vA);
625 guint8 same_time, subframes, num_program, prog;
628 if (!gst_aac_parse_latm_get_value (aacparse, &br, &value))
631 if (!gst_bit_reader_get_bits_uint8 (&br, &same_time, 1))
633 if (!gst_bit_reader_get_bits_uint8 (&br, &subframes, 6))
635 if (!gst_bit_reader_get_bits_uint8 (&br, &num_program, 4))
637 GST_LOG_OBJECT (aacparse, "same_time %d, subframes %d, num_program %d",
638 same_time, subframes, num_program);
640 for (prog = 0; prog <= num_program; ++prog) {
641 guint8 num_layer, layer;
642 if (!gst_bit_reader_get_bits_uint8 (&br, &num_layer, 3))
644 GST_LOG_OBJECT (aacparse, "Program %d: %d layers", prog, num_layer);
646 for (layer = 0; layer <= num_layer; ++layer) {
647 guint8 use_same_config;
648 if (prog == 0 && layer == 0) {
651 if (!gst_bit_reader_get_bits_uint8 (&br, &use_same_config, 1))
654 if (!use_same_config) {
656 if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
657 sample_rate, channels, NULL))
660 guint32 bits, asc_len;
661 if (!gst_aac_parse_latm_get_value (aacparse, &br, &asc_len))
663 if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
664 sample_rate, channels, &bits))
667 if (!gst_bit_reader_skip (&br, asc_len))
673 GST_LOG_OBJECT (aacparse, "More data ignored");
675 GST_WARNING_OBJECT (aacparse, "Spec says \"TBD\"...");
682 * gst_aac_parse_loas_get_frame_len:
683 * @data: block of data containing a LOAS header.
685 * This function calculates LOAS frame length from the given header.
687 * Returns: size of the LOAS frame.
690 gst_aac_parse_loas_get_frame_len (const guint8 * data)
692 return (((data[1] & 0x1f) << 8) | data[2]) + 3;
697 * gst_aac_parse_check_loas_frame:
698 * @aacparse: #GstAacParse.
699 * @data: Data to be checked.
700 * @avail: Amount of data passed.
701 * @framesize: If valid LOAS frame was found, this will be set to tell the
702 * found frame size in bytes.
703 * @needed_data: If frame was not found, this may be set to tell how much
704 * more data is needed in the next round to detect the frame
705 * reliably. This may happen when a frame header candidate
706 * is found but it cannot be guaranteed to be the header without
707 * peeking the following data.
709 * Check if the given data contains contains LOAS frame. The algorithm
710 * will examine LOAS frame header and calculate the frame size. Also, another
711 * consecutive LOAS frame header need to be present after the found frame.
712 * Otherwise the data is not considered as a valid LOAS frame. However, this
713 * "extra check" is omitted when EOS has been received. In this case it is
714 * enough when data[0] contains a valid LOAS header.
716 * This function may set the #needed_data to indicate that a possible frame
717 * candidate has been found, but more data (#needed_data bytes) is needed to
718 * be absolutely sure. When this situation occurs, FALSE will be returned.
720 * When a valid frame is detected, this function will use
721 * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
722 * to set the needed bytes for next frame.This way next data chunk is already
725 * LOAS can have three different formats, if I read the spec correctly. Only
726 * one of them is supported here, as the two samples I have use this one.
728 * Returns: TRUE if the given data contains a valid LOAS header.
731 gst_aac_parse_check_loas_frame (GstAacParse * aacparse,
732 const guint8 * data, const guint avail, gboolean drain,
733 guint * framesize, guint * needed_data)
738 if (G_UNLIKELY (avail < 3)) {
743 if ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) {
744 *framesize = gst_aac_parse_loas_get_frame_len (data);
745 GST_DEBUG_OBJECT (aacparse, "Found %u byte LOAS frame", *framesize);
747 /* In EOS mode this is enough. No need to examine the data further.
748 We also relax the check when we have sync, on the assumption that
749 if we're not looking at random data, we have a much higher chance
750 to get the correct sync, and this avoids losing two frames when
751 a single bit corruption happens. */
752 if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
756 if (*framesize + LOAS_MAX_SIZE > avail) {
757 /* We have found a possible frame header candidate, but can't be
758 sure since we don't have enough data to check the next frame */
759 GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
760 *framesize + LOAS_MAX_SIZE, avail);
761 *needed_data = *framesize + LOAS_MAX_SIZE;
762 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
763 *framesize + LOAS_MAX_SIZE);
767 if ((data[*framesize] == 0x56) && ((data[*framesize + 1] & 0xe0) == 0xe0)) {
768 guint nextlen = gst_aac_parse_loas_get_frame_len (data + (*framesize));
770 GST_LOG ("LOAS frame found, len: %d bytes", *framesize);
771 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
772 nextlen + LOAS_MAX_SIZE);
779 /* caller ensure sufficient data */
781 gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data,
782 gint * rate, gint * channels, gint * object, gint * version)
786 gint sr_idx = (data[2] & 0x3c) >> 2;
788 *rate = gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
791 *channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6);
797 *version = (data[1] & 0x08) ? 2 : 4;
799 *object = ((data[2] & 0xc0) >> 6) + 1;
803 * gst_aac_parse_detect_stream:
804 * @aacparse: #GstAacParse.
805 * @data: A block of data that needs to be examined for stream characteristics.
806 * @avail: Size of the given datablock.
807 * @framesize: If valid stream was found, this will be set to tell the
808 * first frame size in bytes.
809 * @skipsize: If valid stream was found, this will be set to tell the first
810 * audio frame position within the given data.
812 * Examines the given piece of data and try to detect the format of it. It
813 * checks for "ADIF" header (in the beginning of the clip) and ADTS frame
814 * header. If the stream is detected, TRUE will be returned and #framesize
815 * is set to indicate the found frame size. Additionally, #skipsize might
816 * be set to indicate the number of bytes that need to be skipped, a.k.a. the
817 * position of the frame inside given data chunk.
819 * Returns: TRUE on success.
822 gst_aac_parse_detect_stream (GstAacParse * aacparse,
823 const guint8 * data, const guint avail, gboolean drain,
824 guint * framesize, gint * skipsize)
826 gboolean found = FALSE;
827 guint need_data_adts = 0, need_data_loas;
830 GST_DEBUG_OBJECT (aacparse, "Parsing header data");
832 /* FIXME: No need to check for ADIF if we are not in the beginning of the
835 /* Can we even parse the header? */
836 if (avail < MAX (ADTS_MAX_SIZE, LOAS_MAX_SIZE)) {
837 GST_DEBUG_OBJECT (aacparse, "Not enough data to check");
841 for (i = 0; i < avail - 4; i++) {
842 if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) ||
843 ((data[i] == 0x56) && ((data[i + 1] & 0xe0) == 0xe0)) ||
844 strncmp ((char *) data + i, "ADIF", 4) == 0) {
845 GST_DEBUG_OBJECT (aacparse, "Found signature at offset %u", i);
849 /* Trick: tell the parent class that we didn't find the frame yet,
850 but make it skip 'i' amount of bytes. Next time we arrive
851 here we have full frame in the beginning of the data. */
864 if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain,
865 framesize, &need_data_adts)) {
868 GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize);
870 gst_aac_parse_parse_adts_header (aacparse, data, &rate, &channels,
871 &aacparse->object_type, &aacparse->mpegversion);
873 if (!channels || !framesize) {
874 GST_DEBUG_OBJECT (aacparse, "impossible ADTS configuration");
878 aacparse->header_type = DSPAAC_HEADER_ADTS;
879 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
880 aacparse->frame_samples, 2, 2);
882 GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d",
883 rate, channels, aacparse->object_type, aacparse->mpegversion);
885 gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
890 if (gst_aac_parse_check_loas_frame (aacparse, data, avail, drain,
891 framesize, &need_data_loas)) {
892 gint rate = 0, channels = 0;
894 GST_INFO ("LOAS, framesize: %d", *framesize);
896 aacparse->header_type = DSPAAC_HEADER_LOAS;
898 if (!gst_aac_parse_read_loas_config (aacparse, data, avail, &rate,
899 &channels, &aacparse->mpegversion)) {
900 /* This is pretty normal when skipping data at the start of
901 * random stream (MPEG-TS capture for example) */
902 GST_LOG_OBJECT (aacparse, "Error reading LOAS config");
906 if (rate && channels) {
907 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
908 aacparse->frame_samples, 2, 2);
910 /* Don't store the sample rate and channels yet -
911 * this is just format detection. */
912 GST_DEBUG ("LOAS: samplerate %d, channels %d, objtype %d, version %d",
913 rate, channels, aacparse->object_type, aacparse->mpegversion);
916 gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
921 if (need_data_adts || need_data_loas) {
922 /* This tells the parent class not to skip any data */
927 if (avail < ADIF_MAX_SIZE)
930 if (memcmp (data + i, "ADIF", 4) == 0) {
937 aacparse->header_type = DSPAAC_HEADER_ADIF;
938 aacparse->mpegversion = 4;
940 /* Skip the "ADIF" bytes */
943 /* copyright string */
945 skip_size += 9; /* skip 9 bytes */
947 bitstream_type = adif[0 + skip_size] & 0x10;
949 ((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) |
950 ((unsigned int) adif[1 + skip_size] << 11) |
951 ((unsigned int) adif[2 + skip_size] << 3) |
952 ((unsigned int) adif[3 + skip_size] & 0xe0);
955 if (bitstream_type == 0) {
957 /* Buffer fullness parsing. Currently not needed... */
961 num_elems = (adif[3 + skip_size] & 0x1e);
962 GST_INFO ("ADIF num_config_elems: %d", num_elems);
964 fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) |
965 ((unsigned int) adif[4 + skip_size] << 11) |
966 ((unsigned int) adif[5 + skip_size] << 3) |
967 ((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5);
969 GST_INFO ("ADIF buffer fullness: %d", fullness);
971 aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) |
972 ((adif[7 + skip_size] & 0x80) >> 7);
973 sr_idx = (adif[7 + skip_size] & 0x78) >> 3;
977 aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3;
978 sr_idx = ((adif[4 + skip_size] & 0x07) << 1) |
979 ((adif[5 + skip_size] & 0x80) >> 7);
982 /* FIXME: This gives totally wrong results. Duration calculation cannot
984 aacparse->sample_rate =
985 gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
987 /* baseparse is not given any fps,
988 * so it will give up on timestamps, seeking, etc */
990 /* FIXME: Can we assume this? */
991 aacparse->channels = 2;
993 GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d",
994 aacparse->bitrate, aacparse->sample_rate, aacparse->object_type);
996 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512);
998 /* arrange for metadata and get out of the way */
999 sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (aacparse));
1000 gst_aac_parse_set_src_caps (aacparse, sinkcaps);
1002 gst_caps_unref (sinkcaps);
1004 /* not syncable, not easily seekable (unless we push data from start */
1005 gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE);
1006 gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE);
1007 gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0);
1013 /* This should never happen */
1018 * gst_aac_parse_get_audio_profile_object_type
1019 * @aacparse: #GstAacParse.
1021 * Gets the MPEG-2 profile or the MPEG-4 object type value corresponding to the
1022 * mpegversion and profile of @aacparse's src pad caps, according to the
1023 * values defined by table 1.A.11 in ISO/IEC 14496-3.
1025 * Returns: the profile or object type value corresponding to @aacparse's src
1026 * pad caps, if such a value exists; otherwise G_MAXUINT8.
1029 gst_aac_parse_get_audio_profile_object_type (GstAacParse * aacparse)
1032 GstStructure *srcstruct;
1033 const gchar *profile;
1036 srccaps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse));
1037 if (G_UNLIKELY (srccaps == NULL)) {
1041 srcstruct = gst_caps_get_structure (srccaps, 0);
1042 profile = gst_structure_get_string (srcstruct, "profile");
1043 if (G_UNLIKELY (profile == NULL)) {
1044 gst_caps_unref (srccaps);
1048 if (g_strcmp0 (profile, "main") == 0) {
1050 } else if (g_strcmp0 (profile, "lc") == 0) {
1052 } else if (g_strcmp0 (profile, "ssr") == 0) {
1054 } else if (g_strcmp0 (profile, "ltp") == 0) {
1055 if (G_LIKELY (aacparse->mpegversion == 4))
1058 ret = G_MAXUINT8; /* LTP Object Type allowed only for MPEG-4 */
1063 gst_caps_unref (srccaps);
1068 * gst_aac_parse_get_audio_channel_configuration
1069 * @num_channels: number of audio channels.
1071 * Gets the Channel Configuration value, as defined by table 1.19 in ISO/IEC
1072 * 14496-3, for a given number of audio channels.
1074 * Returns: the Channel Configuration value corresponding to @num_channels, if
1075 * such a value exists; otherwise G_MAXUINT8.
1078 gst_aac_parse_get_audio_channel_configuration (gint num_channels)
1080 if (num_channels >= 1 && num_channels <= 6) /* Mono up to & including 5.1 */
1081 return (guint8) num_channels;
1082 else if (num_channels == 8) /* 7.1 */
1087 /* FIXME: Add support for configurations 11, 12 and 14 from
1088 * ISO/IEC 14496-3:2009/PDAM 4 based on the actual channel layout
1093 * gst_aac_parse_get_audio_sampling_frequency_index:
1094 * @sample_rate: audio sampling rate.
1096 * Gets the Sampling Frequency Index value, as defined by table 1.18 in ISO/IEC
1097 * 14496-3, for a given sampling rate.
1099 * Returns: the Sampling Frequency Index value corresponding to @sample_rate,
1100 * if such a value exists; otherwise G_MAXUINT8.
1103 gst_aac_parse_get_audio_sampling_frequency_index (gint sample_rate)
1105 switch (sample_rate) {
1138 * gst_aac_parse_prepend_adts_headers:
1139 * @aacparse: #GstAacParse.
1140 * @frame: raw AAC frame to which ADTS headers shall be prepended.
1142 * Prepends ADTS headers to a raw AAC audio frame.
1144 * Returns: TRUE if ADTS headers were successfully prepended; FALSE otherwise.
1147 gst_aac_parse_prepend_adts_headers (GstAacParse * aacparse,
1148 GstBaseParseFrame * frame)
1151 guint8 *adts_headers;
1154 guint8 id, profile, channel_configuration, sampling_frequency_index;
1156 id = (aacparse->mpegversion == 4) ? 0x0U : 0x1U;
1157 profile = gst_aac_parse_get_audio_profile_object_type (aacparse);
1158 if (profile == G_MAXUINT8) {
1159 GST_ERROR_OBJECT (aacparse, "Unsupported audio profile or object type");
1162 channel_configuration =
1163 gst_aac_parse_get_audio_channel_configuration (aacparse->channels);
1164 if (channel_configuration == G_MAXUINT8) {
1165 GST_ERROR_OBJECT (aacparse, "Unsupported number of channels");
1168 sampling_frequency_index =
1169 gst_aac_parse_get_audio_sampling_frequency_index (aacparse->sample_rate);
1170 if (sampling_frequency_index == G_MAXUINT8) {
1171 GST_ERROR_OBJECT (aacparse, "Unsupported sampling frequency");
1175 frame->out_buffer = gst_buffer_copy (frame->buffer);
1176 buf_size = gst_buffer_get_size (frame->out_buffer);
1177 frame_size = buf_size + ADTS_HEADERS_LENGTH;
1179 if (G_UNLIKELY (frame_size >= 0x4000)) {
1180 GST_ERROR_OBJECT (aacparse, "Frame size is too big for ADTS");
1184 adts_headers = (guint8 *) g_malloc0 (ADTS_HEADERS_LENGTH);
1186 /* Note: no error correction bits are added to the resulting ADTS frames */
1187 adts_headers[0] = 0xFFU;
1188 adts_headers[1] = 0xF0U | (id << 3) | 0x1U;
1189 adts_headers[2] = (profile << 6) | (sampling_frequency_index << 2) | 0x2U |
1190 (channel_configuration & 0x4U);
1191 adts_headers[3] = ((channel_configuration & 0x3U) << 6) | 0x30U |
1192 (guint8) (frame_size >> 11);
1193 adts_headers[4] = (guint8) ((frame_size >> 3) & 0x00FF);
1194 adts_headers[5] = (guint8) (((frame_size & 0x0007) << 5) + 0x1FU);
1195 adts_headers[6] = 0xFCU;
1197 mem = gst_memory_new_wrapped (0, adts_headers, ADTS_HEADERS_LENGTH, 0,
1198 ADTS_HEADERS_LENGTH, adts_headers, g_free);
1199 gst_buffer_prepend_memory (frame->out_buffer, mem);
1205 * gst_aac_parse_check_valid_frame:
1206 * @parse: #GstBaseParse.
1207 * @frame: #GstBaseParseFrame.
1208 * @skipsize: How much data parent class should skip in order to find the
1211 * Implementation of "handle_frame" vmethod in #GstBaseParse class.
1213 * Also determines frame overhead.
1214 * ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have
1215 * a per-frame header. LOAS has 3 bytes.
1217 * We're making a couple of simplifying assumptions:
1219 * 1. We count Program Configuration Elements rather than searching for them
1220 * in the streams to discount them - the overhead is negligible.
1222 * 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16
1223 * bits, which should still not be significant enough to warrant the
1224 * additional parsing through the headers
1226 * Returns: a #GstFlowReturn.
1228 static GstFlowReturn
1229 gst_aac_parse_handle_frame (GstBaseParse * parse,
1230 GstBaseParseFrame * frame, gint * skipsize)
1233 GstAacParse *aacparse;
1234 gboolean ret = FALSE;
1238 gint rate = 0, channels = 0;
1240 aacparse = GST_AAC_PARSE (parse);
1241 buffer = frame->buffer;
1243 gst_buffer_map (buffer, &map, GST_MAP_READ);
1246 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
1248 if (aacparse->header_type == DSPAAC_HEADER_ADIF ||
1249 aacparse->header_type == DSPAAC_HEADER_NONE) {
1250 /* There is nothing to parse */
1251 framesize = map.size;
1254 } else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) {
1256 ret = gst_aac_parse_detect_stream (aacparse, map.data, map.size,
1257 GST_BASE_PARSE_DRAINING (parse), &framesize, skipsize);
1259 } else if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1260 guint needed_data = 1024;
1262 ret = gst_aac_parse_check_adts_frame (aacparse, map.data, map.size,
1263 GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
1265 if (!ret && needed_data) {
1266 GST_DEBUG ("buffer didn't contain valid frame");
1268 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1272 } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1273 guint needed_data = 1024;
1275 ret = gst_aac_parse_check_loas_frame (aacparse, map.data,
1276 map.size, GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
1278 if (!ret && needed_data) {
1279 GST_DEBUG ("buffer didn't contain valid frame");
1281 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1286 GST_DEBUG ("buffer didn't contain valid frame");
1287 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1291 if (G_UNLIKELY (!ret))
1294 if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1296 frame->overhead = 7;
1298 gst_aac_parse_parse_adts_header (aacparse, map.data,
1299 &rate, &channels, NULL, NULL);
1301 GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels);
1303 if (G_UNLIKELY (rate != aacparse->sample_rate
1304 || channels != aacparse->channels)) {
1305 aacparse->sample_rate = rate;
1306 aacparse->channels = channels;
1308 if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1309 /* If linking fails, we need to return appropriate error */
1310 ret = GST_FLOW_NOT_LINKED;
1313 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1314 aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1316 } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1317 gboolean setcaps = FALSE;
1320 frame->overhead = 3;
1322 if (!gst_aac_parse_read_loas_config (aacparse, map.data, map.size, &rate,
1323 &channels, NULL) || !rate || !channels) {
1324 /* This is pretty normal when skipping data at the start of
1325 * random stream (MPEG-TS capture for example) */
1326 GST_DEBUG_OBJECT (aacparse, "Error reading LOAS config. Skipping.");
1327 /* Since we don't fully parse the LOAS config, we don't know for sure
1328 * how much to skip. Just skip 1 to end up to the next marker and
1329 * resume parsing from there */
1334 if (G_UNLIKELY (rate != aacparse->sample_rate
1335 || channels != aacparse->channels)) {
1336 aacparse->sample_rate = rate;
1337 aacparse->channels = channels;
1339 GST_INFO_OBJECT (aacparse, "New LOAS config: %d Hz, %d channels", rate,
1343 /* We want to set caps both at start, and when rate/channels change.
1344 Since only some LOAS frames have that info, we may receive frames
1345 before knowing about rate/channels. */
1347 || !gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse))) {
1348 if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1349 /* If linking fails, we need to return appropriate error */
1350 ret = GST_FLOW_NOT_LINKED;
1353 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1354 aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1358 if (aacparse->header_type == DSPAAC_HEADER_NONE
1359 && aacparse->output_header_type == DSPAAC_HEADER_ADTS) {
1360 if (!gst_aac_parse_prepend_adts_headers (aacparse, frame)) {
1361 GST_ERROR_OBJECT (aacparse, "Failed to prepend ADTS headers to frame");
1362 ret = GST_FLOW_ERROR;
1367 gst_buffer_unmap (buffer, &map);
1370 /* found, skip if needed */
1379 if (ret && framesize <= map.size) {
1380 return gst_base_parse_finish_frame (parse, frame, framesize);
1386 static GstFlowReturn
1387 gst_aac_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
1389 GstAacParse *aacparse = GST_AAC_PARSE (parse);
1391 if (!aacparse->sent_codec_tag) {
1392 GstTagList *taglist;
1396 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
1398 if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
1399 GST_INFO_OBJECT (parse, "Src pad is flushing");
1400 return GST_FLOW_FLUSHING;
1402 GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
1403 return GST_FLOW_NOT_NEGOTIATED;
1407 taglist = gst_tag_list_new_empty ();
1408 gst_pb_utils_add_codec_description_to_tag_list (taglist,
1409 GST_TAG_AUDIO_CODEC, caps);
1410 gst_caps_unref (caps);
1412 gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
1413 gst_tag_list_unref (taglist);
1415 /* also signals the end of first-frame processing */
1416 aacparse->sent_codec_tag = TRUE;
1419 /* As a special case, we can remove the ADTS framing and output raw AAC. */
1420 if (aacparse->header_type == DSPAAC_HEADER_ADTS
1421 && aacparse->output_header_type == DSPAAC_HEADER_NONE) {
1424 gst_buffer_map (frame->buffer, &map, GST_MAP_READ);
1425 header_size = (map.data[1] & 1) ? 7 : 9; /* optional CRC */
1426 gst_buffer_unmap (frame->buffer, &map);
1427 gst_buffer_resize (frame->buffer, header_size,
1428 gst_buffer_get_size (frame->buffer) - header_size);
1436 * gst_aac_parse_start:
1437 * @parse: #GstBaseParse.
1439 * Implementation of "start" vmethod in #GstBaseParse class.
1441 * Returns: TRUE if startup succeeded.
1444 gst_aac_parse_start (GstBaseParse * parse)
1446 GstAacParse *aacparse;
1448 aacparse = GST_AAC_PARSE (parse);
1449 GST_DEBUG ("start");
1450 aacparse->frame_samples = 1024;
1451 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), ADTS_MAX_SIZE);
1452 aacparse->sent_codec_tag = FALSE;
1453 aacparse->last_parsed_channels = 0;
1454 aacparse->last_parsed_sample_rate = 0;
1460 * gst_aac_parse_stop:
1461 * @parse: #GstBaseParse.
1463 * Implementation of "stop" vmethod in #GstBaseParse class.
1465 * Returns: TRUE is stopping succeeded.
1468 gst_aac_parse_stop (GstBaseParse * parse)
1475 remove_fields (GstCaps * caps)
1479 n = gst_caps_get_size (caps);
1480 for (i = 0; i < n; i++) {
1481 GstStructure *s = gst_caps_get_structure (caps, i);
1483 gst_structure_remove_field (s, "framed");
1488 add_conversion_fields (GstCaps * caps)
1492 n = gst_caps_get_size (caps);
1493 for (i = 0; i < n; i++) {
1494 GstStructure *s = gst_caps_get_structure (caps, i);
1496 if (gst_structure_has_field (s, "stream-format")) {
1497 const GValue *v = gst_structure_get_value (s, "stream-format");
1499 if (G_VALUE_HOLDS_STRING (v)) {
1500 const gchar *str = g_value_get_string (v);
1502 if (strcmp (str, "adts") == 0 || strcmp (str, "raw") == 0) {
1503 GValue va = G_VALUE_INIT;
1504 GValue vs = G_VALUE_INIT;
1506 g_value_init (&va, GST_TYPE_LIST);
1507 g_value_init (&vs, G_TYPE_STRING);
1508 g_value_set_string (&vs, "adts");
1509 gst_value_list_append_value (&va, &vs);
1510 g_value_set_string (&vs, "raw");
1511 gst_value_list_append_value (&va, &vs);
1512 gst_structure_set_value (s, "stream-format", &va);
1513 g_value_unset (&va);
1514 g_value_unset (&vs);
1516 } else if (GST_VALUE_HOLDS_LIST (v)) {
1517 gboolean contains_raw = FALSE;
1518 gboolean contains_adts = FALSE;
1519 guint m = gst_value_list_get_size (v), j;
1521 for (j = 0; j < m; j++) {
1522 const GValue *ve = gst_value_list_get_value (v, j);
1525 if (G_VALUE_HOLDS_STRING (ve) && (str = g_value_get_string (ve))) {
1526 if (strcmp (str, "adts") == 0)
1527 contains_adts = TRUE;
1528 else if (strcmp (str, "raw") == 0)
1529 contains_raw = TRUE;
1533 if (contains_adts || contains_raw) {
1534 GValue va = G_VALUE_INIT;
1535 GValue vs = G_VALUE_INIT;
1537 g_value_init (&va, GST_TYPE_LIST);
1538 g_value_init (&vs, G_TYPE_STRING);
1539 g_value_copy (v, &va);
1541 if (!contains_raw) {
1542 g_value_set_string (&vs, "raw");
1543 gst_value_list_append_value (&va, &vs);
1545 if (!contains_adts) {
1546 g_value_set_string (&vs, "adts");
1547 gst_value_list_append_value (&va, &vs);
1550 gst_structure_set_value (s, "stream-format", &va);
1552 g_value_unset (&vs);
1553 g_value_unset (&va);
1561 gst_aac_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter)
1563 GstCaps *peercaps, *templ;
1566 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1569 GstCaps *fcopy = gst_caps_copy (filter);
1570 /* Remove the fields we convert */
1571 remove_fields (fcopy);
1572 add_conversion_fields (fcopy);
1573 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
1574 gst_caps_unref (fcopy);
1576 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
1579 peercaps = gst_caps_make_writable (peercaps);
1580 /* Remove the fields we convert */
1581 remove_fields (peercaps);
1582 add_conversion_fields (peercaps);
1584 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1585 gst_caps_unref (peercaps);
1586 gst_caps_unref (templ);
1592 GstCaps *intersection;
1595 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1596 gst_caps_unref (res);
1604 gst_aac_parse_src_event (GstBaseParse * parse, GstEvent * event)
1606 GstAacParse *aacparse = GST_AAC_PARSE (parse);
1608 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1609 aacparse->last_parsed_channels = 0;
1610 aacparse->last_parsed_sample_rate = 0;
1613 return GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);