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[32] = {
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[32] = {
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);
101 G_DEFINE_TYPE (GstAacParse, gst_aac_parse, GST_TYPE_BASE_PARSE);
104 * gst_aac_parse_class_init:
105 * @klass: #GstAacParseClass.
109 gst_aac_parse_class_init (GstAacParseClass * klass)
111 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
112 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
114 GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0,
115 "AAC audio stream parser");
117 gst_element_class_add_static_pad_template (element_class, &sink_template);
118 gst_element_class_add_static_pad_template (element_class, &src_template);
120 gst_element_class_set_static_metadata (element_class,
121 "AAC audio stream parser", "Codec/Parser/Audio",
122 "Advanced Audio Coding parser", "Stefan Kost <stefan.kost@nokia.com>");
124 parse_class->start = GST_DEBUG_FUNCPTR (gst_aac_parse_start);
125 parse_class->stop = GST_DEBUG_FUNCPTR (gst_aac_parse_stop);
126 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_setcaps);
127 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_getcaps);
128 parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_handle_frame);
129 parse_class->pre_push_frame =
130 GST_DEBUG_FUNCPTR (gst_aac_parse_pre_push_frame);
135 * gst_aac_parse_init:
136 * @aacparse: #GstAacParse.
137 * @klass: #GstAacParseClass.
141 gst_aac_parse_init (GstAacParse * aacparse)
143 GST_DEBUG ("initialized");
144 GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (aacparse));
145 GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (aacparse));
150 * gst_aac_parse_set_src_caps:
151 * @aacparse: #GstAacParse.
152 * @sink_caps: (proposed) caps of sink pad
154 * Set source pad caps according to current knowledge about the
157 * Returns: TRUE if caps were successfully set.
160 gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps)
163 GstCaps *src_caps = NULL, *allowed;
164 gboolean res = FALSE;
165 const gchar *stream_format;
166 guint8 codec_data[2];
167 guint16 codec_data_data;
168 gint sample_rate_idx;
170 GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps);
172 src_caps = gst_caps_copy (sink_caps);
174 src_caps = gst_caps_new_empty_simple ("audio/mpeg");
176 gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE,
177 "mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL);
179 aacparse->output_header_type = aacparse->header_type;
180 switch (aacparse->header_type) {
181 case DSPAAC_HEADER_NONE:
182 stream_format = "raw";
184 case DSPAAC_HEADER_ADTS:
185 stream_format = "adts";
187 case DSPAAC_HEADER_ADIF:
188 stream_format = "adif";
190 case DSPAAC_HEADER_LOAS:
191 stream_format = "loas";
194 stream_format = NULL;
197 /* Generate codec data to be able to set profile/level on the caps */
199 gst_codec_utils_aac_get_index_from_sample_rate (aacparse->sample_rate);
200 if (sample_rate_idx < 0)
201 goto not_a_known_rate;
203 (aacparse->object_type << 11) |
204 (sample_rate_idx << 7) | (aacparse->channels << 3);
205 GST_WRITE_UINT16_BE (codec_data, codec_data_data);
206 gst_codec_utils_aac_caps_set_level_and_profile (src_caps, codec_data, 2);
208 s = gst_caps_get_structure (src_caps, 0);
209 if (aacparse->sample_rate > 0)
210 gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL);
211 if (aacparse->channels > 0)
212 gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL);
214 gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL);
216 allowed = gst_pad_get_allowed_caps (GST_BASE_PARSE (aacparse)->srcpad);
217 if (allowed && !gst_caps_can_intersect (src_caps, allowed)) {
218 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
219 "Caps can not intersect");
220 if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
221 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
222 "Input is ADTS, trying raw");
223 gst_caps_set_simple (src_caps, "stream-format", G_TYPE_STRING, "raw",
225 if (gst_caps_can_intersect (src_caps, allowed)) {
226 GstBuffer *codec_data_buffer;
228 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
229 "Caps can intersect, we will drop the ADTS layer");
230 aacparse->output_header_type = DSPAAC_HEADER_NONE;
232 /* The codec_data data is according to AudioSpecificConfig,
233 ISO/IEC 14496-3, 1.6.2.1 */
234 codec_data_buffer = gst_buffer_new_and_alloc (2);
235 gst_buffer_fill (codec_data_buffer, 0, codec_data, 2);
236 gst_caps_set_simple (src_caps, "codec_data", GST_TYPE_BUFFER,
237 codec_data_buffer, NULL);
239 } else if (aacparse->header_type == DSPAAC_HEADER_NONE) {
240 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
241 "Input is raw, trying ADTS");
242 gst_caps_set_simple (src_caps, "stream-format", G_TYPE_STRING, "adts",
244 if (gst_caps_can_intersect (src_caps, allowed)) {
245 GST_DEBUG_OBJECT (GST_BASE_PARSE (aacparse)->srcpad,
246 "Caps can intersect, we will prepend ADTS headers");
247 aacparse->output_header_type = DSPAAC_HEADER_ADTS;
252 gst_caps_unref (allowed);
254 GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps);
256 res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps);
257 gst_caps_unref (src_caps);
261 GST_ERROR_OBJECT (aacparse, "Not a known sample rate: %d",
262 aacparse->sample_rate);
263 gst_caps_unref (src_caps);
269 * gst_aac_parse_sink_setcaps:
273 * Implementation of "set_sink_caps" vmethod in #GstBaseParse class.
275 * Returns: TRUE on success.
278 gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps)
280 GstAacParse *aacparse;
281 GstStructure *structure;
285 aacparse = GST_AAC_PARSE (parse);
286 structure = gst_caps_get_structure (caps, 0);
287 caps_str = gst_caps_to_string (caps);
289 GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str);
292 /* This is needed at least in case of RTP
293 * Parses the codec_data information to get ObjectType,
294 * number of channels and samplerate */
295 value = gst_structure_get_value (structure, "codec_data");
297 GstBuffer *buf = gst_value_get_buffer (value);
303 gst_buffer_map (buf, &map, GST_MAP_READ);
305 sr_idx = ((map.data[0] & 0x07) << 1) | ((map.data[1] & 0x80) >> 7);
306 aacparse->object_type = (map.data[0] & 0xf8) >> 3;
307 aacparse->sample_rate =
308 gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
309 aacparse->channels = (map.data[1] & 0x78) >> 3;
310 if (aacparse->channels == 7)
311 aacparse->channels = 8;
312 else if (aacparse->channels == 11)
313 aacparse->channels = 7;
314 else if (aacparse->channels == 12 || aacparse->channels == 14)
315 aacparse->channels = 8;
316 aacparse->header_type = DSPAAC_HEADER_NONE;
317 aacparse->mpegversion = 4;
318 aacparse->frame_samples = (map.data[1] & 4) ? 960 : 1024;
319 gst_buffer_unmap (buf, &map);
321 GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d, "
322 "samples=%d", aacparse->object_type, aacparse->sample_rate,
323 aacparse->channels, aacparse->frame_samples);
325 /* arrange for metadata and get out of the way */
326 gst_aac_parse_set_src_caps (aacparse, caps);
327 if (aacparse->header_type == aacparse->output_header_type)
328 gst_base_parse_set_passthrough (parse, TRUE);
332 /* caps info overrides */
333 gst_structure_get_int (structure, "rate", &aacparse->sample_rate);
334 gst_structure_get_int (structure, "channels", &aacparse->channels);
336 aacparse->sample_rate = 0;
337 aacparse->channels = 0;
338 aacparse->header_type = DSPAAC_HEADER_NOT_PARSED;
339 gst_base_parse_set_passthrough (parse, FALSE);
347 * gst_aac_parse_adts_get_frame_len:
348 * @data: block of data containing an ADTS header.
350 * This function calculates ADTS frame length from the given header.
352 * Returns: size of the ADTS frame.
355 gst_aac_parse_adts_get_frame_len (const guint8 * data)
357 return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5);
362 * gst_aac_parse_check_adts_frame:
363 * @aacparse: #GstAacParse.
364 * @data: Data to be checked.
365 * @avail: Amount of data passed.
366 * @framesize: If valid ADTS frame was found, this will be set to tell the
367 * found frame size in bytes.
368 * @needed_data: If frame was not found, this may be set to tell how much
369 * more data is needed in the next round to detect the frame
370 * reliably. This may happen when a frame header candidate
371 * is found but it cannot be guaranteed to be the header without
372 * peeking the following data.
374 * Check if the given data contains contains ADTS frame. The algorithm
375 * will examine ADTS frame header and calculate the frame size. Also, another
376 * consecutive ADTS frame header need to be present after the found frame.
377 * Otherwise the data is not considered as a valid ADTS frame. However, this
378 * "extra check" is omitted when EOS has been received. In this case it is
379 * enough when data[0] contains a valid ADTS header.
381 * This function may set the #needed_data to indicate that a possible frame
382 * candidate has been found, but more data (#needed_data bytes) is needed to
383 * be absolutely sure. When this situation occurs, FALSE will be returned.
385 * When a valid frame is detected, this function will use
386 * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
387 * to set the needed bytes for next frame.This way next data chunk is already
390 * Returns: TRUE if the given data contains a valid ADTS header.
393 gst_aac_parse_check_adts_frame (GstAacParse * aacparse,
394 const guint8 * data, const guint avail, gboolean drain,
395 guint * framesize, guint * needed_data)
401 /* Absolute minimum to perform the ADTS syncword,
402 layer and sampling frequency tests */
403 if (G_UNLIKELY (avail < 3)) {
408 /* Syncword and layer tests */
409 if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) {
411 /* Sampling frequency test */
412 if (G_UNLIKELY ((data[2] & 0x3C) >> 2 == 15))
415 /* This looks like an ADTS frame header but
416 we need at least 6 bytes to proceed */
417 if (G_UNLIKELY (avail < 6)) {
422 *framesize = gst_aac_parse_adts_get_frame_len (data);
424 /* If frame has CRC, it needs 2 bytes
425 for it at the end of the header */
426 crc_size = (data[1] & 0x01) ? 0 : 2;
429 if (*framesize < 7 + crc_size) {
430 *needed_data = 7 + crc_size;
434 /* In EOS mode this is enough. No need to examine the data further.
435 We also relax the check when we have sync, on the assumption that
436 if we're not looking at random data, we have a much higher chance
437 to get the correct sync, and this avoids losing two frames when
438 a single bit corruption happens. */
439 if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
443 if (*framesize + ADTS_MAX_SIZE > avail) {
444 /* We have found a possible frame header candidate, but can't be
445 sure since we don't have enough data to check the next frame */
446 GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
447 *framesize + ADTS_MAX_SIZE, avail);
448 *needed_data = *framesize + ADTS_MAX_SIZE;
449 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
450 *framesize + ADTS_MAX_SIZE);
454 if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) {
455 guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize));
457 GST_LOG ("ADTS frame found, len: %d bytes", *framesize);
458 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
459 nextlen + ADTS_MAX_SIZE);
467 gst_aac_parse_latm_get_value (GstAacParse * aacparse, GstBitReader * br,
470 guint8 bytes, i, byte;
473 if (!gst_bit_reader_get_bits_uint8 (br, &bytes, 2))
475 for (i = 0; i < bytes; ++i) {
477 if (!gst_bit_reader_get_bits_uint8 (br, &byte, 8))
485 gst_aac_parse_get_audio_object_type (GstAacParse * aacparse, GstBitReader * br,
486 guint8 * audio_object_type)
488 if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 5))
490 if (*audio_object_type == 31) {
491 if (!gst_bit_reader_get_bits_uint8 (br, audio_object_type, 6))
493 *audio_object_type += 32;
495 GST_LOG_OBJECT (aacparse, "audio object type %u", *audio_object_type);
500 gst_aac_parse_get_audio_sample_rate (GstAacParse * aacparse, GstBitReader * br,
503 guint8 sampling_frequency_index;
504 if (!gst_bit_reader_get_bits_uint8 (br, &sampling_frequency_index, 4))
506 GST_LOG_OBJECT (aacparse, "sampling_frequency_index: %u",
507 sampling_frequency_index);
508 if (sampling_frequency_index == 0xf) {
509 guint32 sampling_rate;
510 if (!gst_bit_reader_get_bits_uint32 (br, &sampling_rate, 24))
512 *sample_rate = sampling_rate;
514 *sample_rate = loas_sample_rate_table[sampling_frequency_index];
521 /* See table 1.13 in ISO/IEC 14496-3 */
523 gst_aac_parse_read_loas_audio_specific_config (GstAacParse * aacparse,
524 GstBitReader * br, gint * sample_rate, gint * channels, guint32 * bits)
526 guint8 audio_object_type, channel_configuration;
528 if (!gst_aac_parse_get_audio_object_type (aacparse, br, &audio_object_type))
531 if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
534 if (!gst_bit_reader_get_bits_uint8 (br, &channel_configuration, 4))
536 GST_LOG_OBJECT (aacparse, "channel_configuration: %d", channel_configuration);
537 *channels = loas_channels_table[channel_configuration];
541 if (audio_object_type == 5) {
542 GST_LOG_OBJECT (aacparse,
543 "Audio object type 5, so rereading sampling rate...");
544 if (!gst_aac_parse_get_audio_sample_rate (aacparse, br, sample_rate))
548 GST_INFO_OBJECT (aacparse, "Found LOAS config: %d Hz, %d channels",
549 *sample_rate, *channels);
551 /* There's LOTS of stuff next, but we ignore it for now as we have
552 what we want (sample rate and number of channels */
553 GST_DEBUG_OBJECT (aacparse,
554 "Need more code to parse humongous LOAS data, currently ignored");
562 gst_aac_parse_read_loas_config (GstAacParse * aacparse, const guint8 * data,
563 guint avail, gint * sample_rate, gint * channels, gint * version)
568 /* No version in the bitstream, but the spec has LOAS in the MPEG-4 section */
572 gst_bit_reader_init (&br, data, avail);
574 /* skip sync word (11 bits) and size (13 bits) */
575 if (!gst_bit_reader_skip (&br, 11 + 13))
578 /* First bit is "use last config" */
579 if (!gst_bit_reader_get_bits_uint8 (&br, &u8, 1))
582 GST_LOG_OBJECT (aacparse, "Frame uses previous config");
583 if (!aacparse->sample_rate || !aacparse->channels) {
584 GST_DEBUG_OBJECT (aacparse,
585 "No previous config to use. We'll look for more data.");
588 *sample_rate = aacparse->sample_rate;
589 *channels = aacparse->channels;
593 GST_DEBUG_OBJECT (aacparse, "Frame contains new config");
595 if (!gst_bit_reader_get_bits_uint8 (&br, &v, 1))
598 if (!gst_bit_reader_get_bits_uint8 (&br, &vA, 1))
603 GST_LOG_OBJECT (aacparse, "v %d, vA %d", v, vA);
605 guint8 same_time, subframes, num_program, prog;
608 if (!gst_aac_parse_latm_get_value (aacparse, &br, &value))
611 if (!gst_bit_reader_get_bits_uint8 (&br, &same_time, 1))
613 if (!gst_bit_reader_get_bits_uint8 (&br, &subframes, 6))
615 if (!gst_bit_reader_get_bits_uint8 (&br, &num_program, 4))
617 GST_LOG_OBJECT (aacparse, "same_time %d, subframes %d, num_program %d",
618 same_time, subframes, num_program);
620 for (prog = 0; prog <= num_program; ++prog) {
621 guint8 num_layer, layer;
622 if (!gst_bit_reader_get_bits_uint8 (&br, &num_layer, 3))
624 GST_LOG_OBJECT (aacparse, "Program %d: %d layers", prog, num_layer);
626 for (layer = 0; layer <= num_layer; ++layer) {
627 guint8 use_same_config;
628 if (prog == 0 && layer == 0) {
631 if (!gst_bit_reader_get_bits_uint8 (&br, &use_same_config, 1))
634 if (!use_same_config) {
636 if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
637 sample_rate, channels, NULL))
640 guint32 bits, asc_len;
641 if (!gst_aac_parse_latm_get_value (aacparse, &br, &asc_len))
643 if (!gst_aac_parse_read_loas_audio_specific_config (aacparse, &br,
644 sample_rate, channels, &bits))
647 if (!gst_bit_reader_skip (&br, asc_len))
653 GST_LOG_OBJECT (aacparse, "More data ignored");
655 GST_WARNING_OBJECT (aacparse, "Spec says \"TBD\"...");
662 * gst_aac_parse_loas_get_frame_len:
663 * @data: block of data containing a LOAS header.
665 * This function calculates LOAS frame length from the given header.
667 * Returns: size of the LOAS frame.
670 gst_aac_parse_loas_get_frame_len (const guint8 * data)
672 return (((data[1] & 0x1f) << 8) | data[2]) + 3;
677 * gst_aac_parse_check_loas_frame:
678 * @aacparse: #GstAacParse.
679 * @data: Data to be checked.
680 * @avail: Amount of data passed.
681 * @framesize: If valid LOAS frame was found, this will be set to tell the
682 * found frame size in bytes.
683 * @needed_data: If frame was not found, this may be set to tell how much
684 * more data is needed in the next round to detect the frame
685 * reliably. This may happen when a frame header candidate
686 * is found but it cannot be guaranteed to be the header without
687 * peeking the following data.
689 * Check if the given data contains contains LOAS frame. The algorithm
690 * will examine LOAS frame header and calculate the frame size. Also, another
691 * consecutive LOAS frame header need to be present after the found frame.
692 * Otherwise the data is not considered as a valid LOAS frame. However, this
693 * "extra check" is omitted when EOS has been received. In this case it is
694 * enough when data[0] contains a valid LOAS header.
696 * This function may set the #needed_data to indicate that a possible frame
697 * candidate has been found, but more data (#needed_data bytes) is needed to
698 * be absolutely sure. When this situation occurs, FALSE will be returned.
700 * When a valid frame is detected, this function will use
701 * gst_base_parse_set_min_frame_size() function from #GstBaseParse class
702 * to set the needed bytes for next frame.This way next data chunk is already
705 * LOAS can have three different formats, if I read the spec correctly. Only
706 * one of them is supported here, as the two samples I have use this one.
708 * Returns: TRUE if the given data contains a valid LOAS header.
711 gst_aac_parse_check_loas_frame (GstAacParse * aacparse,
712 const guint8 * data, const guint avail, gboolean drain,
713 guint * framesize, guint * needed_data)
718 if (G_UNLIKELY (avail < 3)) {
723 if ((data[0] == 0x56) && ((data[1] & 0xe0) == 0xe0)) {
724 *framesize = gst_aac_parse_loas_get_frame_len (data);
725 GST_DEBUG_OBJECT (aacparse, "Found %u byte LOAS frame", *framesize);
727 /* In EOS mode this is enough. No need to examine the data further.
728 We also relax the check when we have sync, on the assumption that
729 if we're not looking at random data, we have a much higher chance
730 to get the correct sync, and this avoids losing two frames when
731 a single bit corruption happens. */
732 if (drain || !GST_BASE_PARSE_LOST_SYNC (aacparse)) {
736 if (*framesize + LOAS_MAX_SIZE > avail) {
737 /* We have found a possible frame header candidate, but can't be
738 sure since we don't have enough data to check the next frame */
739 GST_DEBUG ("NEED MORE DATA: we need %d, available %d",
740 *framesize + LOAS_MAX_SIZE, avail);
741 *needed_data = *framesize + LOAS_MAX_SIZE;
742 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
743 *framesize + LOAS_MAX_SIZE);
747 if ((data[*framesize] == 0x56) && ((data[*framesize + 1] & 0xe0) == 0xe0)) {
748 guint nextlen = gst_aac_parse_loas_get_frame_len (data + (*framesize));
750 GST_LOG ("LOAS frame found, len: %d bytes", *framesize);
751 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
752 nextlen + LOAS_MAX_SIZE);
759 /* caller ensure sufficient data */
761 gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data,
762 gint * rate, gint * channels, gint * object, gint * version)
766 gint sr_idx = (data[2] & 0x3c) >> 2;
768 *rate = gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
771 *channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6);
777 *version = (data[1] & 0x08) ? 2 : 4;
779 *object = ((data[2] & 0xc0) >> 6) + 1;
783 * gst_aac_parse_detect_stream:
784 * @aacparse: #GstAacParse.
785 * @data: A block of data that needs to be examined for stream characteristics.
786 * @avail: Size of the given datablock.
787 * @framesize: If valid stream was found, this will be set to tell the
788 * first frame size in bytes.
789 * @skipsize: If valid stream was found, this will be set to tell the first
790 * audio frame position within the given data.
792 * Examines the given piece of data and try to detect the format of it. It
793 * checks for "ADIF" header (in the beginning of the clip) and ADTS frame
794 * header. If the stream is detected, TRUE will be returned and #framesize
795 * is set to indicate the found frame size. Additionally, #skipsize might
796 * be set to indicate the number of bytes that need to be skipped, a.k.a. the
797 * position of the frame inside given data chunk.
799 * Returns: TRUE on success.
802 gst_aac_parse_detect_stream (GstAacParse * aacparse,
803 const guint8 * data, const guint avail, gboolean drain,
804 guint * framesize, gint * skipsize)
806 gboolean found = FALSE;
807 guint need_data_adts = 0, need_data_loas;
810 GST_DEBUG_OBJECT (aacparse, "Parsing header data");
812 /* FIXME: No need to check for ADIF if we are not in the beginning of the
815 /* Can we even parse the header? */
816 if (avail < MAX (ADTS_MAX_SIZE, LOAS_MAX_SIZE)) {
817 GST_DEBUG_OBJECT (aacparse, "Not enough data to check");
821 for (i = 0; i < avail - 4; i++) {
822 if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) ||
823 ((data[i] == 0x56) && ((data[i + 1] & 0xe0) == 0xe0)) ||
824 strncmp ((char *) data + i, "ADIF", 4) == 0) {
825 GST_DEBUG_OBJECT (aacparse, "Found signature at offset %u", i);
829 /* Trick: tell the parent class that we didn't find the frame yet,
830 but make it skip 'i' amount of bytes. Next time we arrive
831 here we have full frame in the beginning of the data. */
844 if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain,
845 framesize, &need_data_adts)) {
848 GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize);
850 gst_aac_parse_parse_adts_header (aacparse, data, &rate, &channels,
851 &aacparse->object_type, &aacparse->mpegversion);
853 if (!channels || !framesize) {
854 GST_DEBUG_OBJECT (aacparse, "impossible ADTS configuration");
858 aacparse->header_type = DSPAAC_HEADER_ADTS;
859 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
860 aacparse->frame_samples, 2, 2);
862 GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d",
863 rate, channels, aacparse->object_type, aacparse->mpegversion);
865 gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
870 if (gst_aac_parse_check_loas_frame (aacparse, data, avail, drain,
871 framesize, &need_data_loas)) {
872 gint rate = 0, channels = 0;
874 GST_INFO ("LOAS, framesize: %d", *framesize);
876 aacparse->header_type = DSPAAC_HEADER_LOAS;
878 if (!gst_aac_parse_read_loas_config (aacparse, data, avail, &rate,
879 &channels, &aacparse->mpegversion)) {
880 /* This is pretty normal when skipping data at the start of
881 * random stream (MPEG-TS capture for example) */
882 GST_LOG_OBJECT (aacparse, "Error reading LOAS config");
886 if (rate && channels) {
887 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate,
888 aacparse->frame_samples, 2, 2);
890 /* Don't store the sample rate and channels yet -
891 * this is just format detection. */
892 GST_DEBUG ("LOAS: samplerate %d, channels %d, objtype %d, version %d",
893 rate, channels, aacparse->object_type, aacparse->mpegversion);
896 gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE);
901 if (need_data_adts || need_data_loas) {
902 /* This tells the parent class not to skip any data */
907 if (avail < ADIF_MAX_SIZE)
910 if (memcmp (data + i, "ADIF", 4) == 0) {
917 aacparse->header_type = DSPAAC_HEADER_ADIF;
918 aacparse->mpegversion = 4;
920 /* Skip the "ADIF" bytes */
923 /* copyright string */
925 skip_size += 9; /* skip 9 bytes */
927 bitstream_type = adif[0 + skip_size] & 0x10;
929 ((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) |
930 ((unsigned int) adif[1 + skip_size] << 11) |
931 ((unsigned int) adif[2 + skip_size] << 3) |
932 ((unsigned int) adif[3 + skip_size] & 0xe0);
935 if (bitstream_type == 0) {
937 /* Buffer fullness parsing. Currently not needed... */
941 num_elems = (adif[3 + skip_size] & 0x1e);
942 GST_INFO ("ADIF num_config_elems: %d", num_elems);
944 fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) |
945 ((unsigned int) adif[4 + skip_size] << 11) |
946 ((unsigned int) adif[5 + skip_size] << 3) |
947 ((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5);
949 GST_INFO ("ADIF buffer fullness: %d", fullness);
951 aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) |
952 ((adif[7 + skip_size] & 0x80) >> 7);
953 sr_idx = (adif[7 + skip_size] & 0x78) >> 3;
957 aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3;
958 sr_idx = ((adif[4 + skip_size] & 0x07) << 1) |
959 ((adif[5 + skip_size] & 0x80) >> 7);
962 /* FIXME: This gives totally wrong results. Duration calculation cannot
964 aacparse->sample_rate =
965 gst_codec_utils_aac_get_sample_rate_from_index (sr_idx);
967 /* baseparse is not given any fps,
968 * so it will give up on timestamps, seeking, etc */
970 /* FIXME: Can we assume this? */
971 aacparse->channels = 2;
973 GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d",
974 aacparse->bitrate, aacparse->sample_rate, aacparse->object_type);
976 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512);
978 /* arrange for metadata and get out of the way */
979 sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (aacparse));
980 gst_aac_parse_set_src_caps (aacparse, sinkcaps);
982 gst_caps_unref (sinkcaps);
984 /* not syncable, not easily seekable (unless we push data from start */
985 gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE);
986 gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE);
987 gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0);
993 /* This should never happen */
998 * gst_aac_parse_get_audio_profile_object_type
999 * @aacparse: #GstAacParse.
1001 * Gets the MPEG-2 profile or the MPEG-4 object type value corresponding to the
1002 * mpegversion and profile of @aacparse's src pad caps, according to the
1003 * values defined by table 1.A.11 in ISO/IEC 14496-3.
1005 * Returns: the profile or object type value corresponding to @aacparse's src
1006 * pad caps, if such a value exists; otherwise G_MAXUINT8.
1009 gst_aac_parse_get_audio_profile_object_type (GstAacParse * aacparse)
1012 GstStructure *srcstruct;
1013 const gchar *profile;
1016 srccaps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse));
1017 if (G_UNLIKELY (srccaps == NULL)) {
1021 srcstruct = gst_caps_get_structure (srccaps, 0);
1022 profile = gst_structure_get_string (srcstruct, "profile");
1023 if (G_UNLIKELY (profile == NULL)) {
1024 gst_caps_unref (srccaps);
1028 if (g_strcmp0 (profile, "main") == 0) {
1030 } else if (g_strcmp0 (profile, "lc") == 0) {
1032 } else if (g_strcmp0 (profile, "ssr") == 0) {
1034 } else if (g_strcmp0 (profile, "ltp") == 0) {
1035 if (G_LIKELY (aacparse->mpegversion == 4))
1038 ret = G_MAXUINT8; /* LTP Object Type allowed only for MPEG-4 */
1043 gst_caps_unref (srccaps);
1048 * gst_aac_parse_get_audio_channel_configuration
1049 * @num_channels: number of audio channels.
1051 * Gets the Channel Configuration value, as defined by table 1.19 in ISO/IEC
1052 * 14496-3, for a given number of audio channels.
1054 * Returns: the Channel Configuration value corresponding to @num_channels, if
1055 * such a value exists; otherwise G_MAXUINT8.
1058 gst_aac_parse_get_audio_channel_configuration (gint num_channels)
1060 if (num_channels >= 1 && num_channels <= 6) /* Mono up to & including 5.1 */
1061 return (guint8) num_channels;
1062 else if (num_channels == 8) /* 7.1 */
1067 /* FIXME: Add support for configurations 11, 12 and 14 from
1068 * ISO/IEC 14496-3:2009/PDAM 4 based on the actual channel layout
1073 * gst_aac_parse_get_audio_sampling_frequency_index:
1074 * @sample_rate: audio sampling rate.
1076 * Gets the Sampling Frequency Index value, as defined by table 1.18 in ISO/IEC
1077 * 14496-3, for a given sampling rate.
1079 * Returns: the Sampling Frequency Index value corresponding to @sample_rate,
1080 * if such a value exists; otherwise G_MAXUINT8.
1083 gst_aac_parse_get_audio_sampling_frequency_index (gint sample_rate)
1085 switch (sample_rate) {
1118 * gst_aac_parse_prepend_adts_headers:
1119 * @aacparse: #GstAacParse.
1120 * @frame: raw AAC frame to which ADTS headers shall be prepended.
1122 * Prepends ADTS headers to a raw AAC audio frame.
1124 * Returns: TRUE if ADTS headers were successfully prepended; FALSE otherwise.
1127 gst_aac_parse_prepend_adts_headers (GstAacParse * aacparse,
1128 GstBaseParseFrame * frame)
1131 guint8 *adts_headers;
1134 guint8 id, profile, channel_configuration, sampling_frequency_index;
1136 id = (aacparse->mpegversion == 4) ? 0x0U : 0x1U;
1137 profile = gst_aac_parse_get_audio_profile_object_type (aacparse);
1138 if (profile == G_MAXUINT8) {
1139 GST_ERROR_OBJECT (aacparse, "Unsupported audio profile or object type");
1142 channel_configuration =
1143 gst_aac_parse_get_audio_channel_configuration (aacparse->channels);
1144 if (channel_configuration == G_MAXUINT8) {
1145 GST_ERROR_OBJECT (aacparse, "Unsupported number of channels");
1148 sampling_frequency_index =
1149 gst_aac_parse_get_audio_sampling_frequency_index (aacparse->sample_rate);
1150 if (sampling_frequency_index == G_MAXUINT8) {
1151 GST_ERROR_OBJECT (aacparse, "Unsupported sampling frequency");
1155 frame->out_buffer = gst_buffer_copy (frame->buffer);
1156 buf_size = gst_buffer_get_size (frame->out_buffer);
1157 frame_size = buf_size + ADTS_HEADERS_LENGTH;
1159 if (G_UNLIKELY (frame_size >= 0x4000)) {
1160 GST_ERROR_OBJECT (aacparse, "Frame size is too big for ADTS");
1164 adts_headers = (guint8 *) g_malloc0 (ADTS_HEADERS_LENGTH);
1166 /* Note: no error correction bits are added to the resulting ADTS frames */
1167 adts_headers[0] = 0xFFU;
1168 adts_headers[1] = 0xF0U | (id << 3) | 0x1U;
1169 adts_headers[2] = (profile << 6) | (sampling_frequency_index << 2) | 0x2U |
1170 (channel_configuration & 0x4U);
1171 adts_headers[3] = ((channel_configuration & 0x3U) << 6) | 0x30U |
1172 (guint8) (frame_size >> 11);
1173 adts_headers[4] = (guint8) ((frame_size >> 3) & 0x00FF);
1174 adts_headers[5] = (guint8) (((frame_size & 0x0007) << 5) + 0x1FU);
1175 adts_headers[6] = 0xFCU;
1177 mem = gst_memory_new_wrapped (0, adts_headers, ADTS_HEADERS_LENGTH, 0,
1178 ADTS_HEADERS_LENGTH, adts_headers, g_free);
1179 gst_buffer_prepend_memory (frame->out_buffer, mem);
1185 * gst_aac_parse_check_valid_frame:
1186 * @parse: #GstBaseParse.
1187 * @frame: #GstBaseParseFrame.
1188 * @skipsize: How much data parent class should skip in order to find the
1191 * Implementation of "handle_frame" vmethod in #GstBaseParse class.
1193 * Also determines frame overhead.
1194 * ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have
1195 * a per-frame header. LOAS has 3 bytes.
1197 * We're making a couple of simplifying assumptions:
1199 * 1. We count Program Configuration Elements rather than searching for them
1200 * in the streams to discount them - the overhead is negligible.
1202 * 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16
1203 * bits, which should still not be significant enough to warrant the
1204 * additional parsing through the headers
1206 * Returns: a #GstFlowReturn.
1208 static GstFlowReturn
1209 gst_aac_parse_handle_frame (GstBaseParse * parse,
1210 GstBaseParseFrame * frame, gint * skipsize)
1213 GstAacParse *aacparse;
1214 gboolean ret = FALSE;
1218 gint rate = 0, channels = 0;
1220 aacparse = GST_AAC_PARSE (parse);
1221 buffer = frame->buffer;
1223 gst_buffer_map (buffer, &map, GST_MAP_READ);
1226 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
1228 if (aacparse->header_type == DSPAAC_HEADER_ADIF ||
1229 aacparse->header_type == DSPAAC_HEADER_NONE) {
1230 /* There is nothing to parse */
1231 framesize = map.size;
1234 } else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) {
1236 ret = gst_aac_parse_detect_stream (aacparse, map.data, map.size,
1237 GST_BASE_PARSE_DRAINING (parse), &framesize, skipsize);
1239 } else if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1240 guint needed_data = 1024;
1242 ret = gst_aac_parse_check_adts_frame (aacparse, map.data, map.size,
1243 GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
1245 if (!ret && needed_data) {
1246 GST_DEBUG ("buffer didn't contain valid frame");
1248 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1252 } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1253 guint needed_data = 1024;
1255 ret = gst_aac_parse_check_loas_frame (aacparse, map.data,
1256 map.size, GST_BASE_PARSE_DRAINING (parse), &framesize, &needed_data);
1258 if (!ret && needed_data) {
1259 GST_DEBUG ("buffer didn't contain valid frame");
1261 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1266 GST_DEBUG ("buffer didn't contain valid frame");
1267 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse),
1271 if (G_UNLIKELY (!ret))
1274 if (aacparse->header_type == DSPAAC_HEADER_ADTS) {
1276 frame->overhead = 7;
1278 gst_aac_parse_parse_adts_header (aacparse, map.data,
1279 &rate, &channels, NULL, NULL);
1281 GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels);
1283 if (G_UNLIKELY (rate != aacparse->sample_rate
1284 || channels != aacparse->channels)) {
1285 aacparse->sample_rate = rate;
1286 aacparse->channels = channels;
1288 if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1289 /* If linking fails, we need to return appropriate error */
1290 ret = GST_FLOW_NOT_LINKED;
1293 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1294 aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1296 } else if (aacparse->header_type == DSPAAC_HEADER_LOAS) {
1297 gboolean setcaps = FALSE;
1300 frame->overhead = 3;
1302 if (!gst_aac_parse_read_loas_config (aacparse, map.data, map.size, &rate,
1303 &channels, NULL) || !rate || !channels) {
1304 /* This is pretty normal when skipping data at the start of
1305 * random stream (MPEG-TS capture for example) */
1306 GST_DEBUG_OBJECT (aacparse, "Error reading LOAS config. Skipping.");
1307 /* Since we don't fully parse the LOAS config, we don't know for sure
1308 * how much to skip. Just skip 1 to end up to the next marker and
1309 * resume parsing from there */
1314 if (G_UNLIKELY (rate != aacparse->sample_rate
1315 || channels != aacparse->channels)) {
1316 aacparse->sample_rate = rate;
1317 aacparse->channels = channels;
1319 GST_INFO_OBJECT (aacparse, "New LOAS config: %d Hz, %d channels", rate,
1323 /* We want to set caps both at start, and when rate/channels change.
1324 Since only some LOAS frames have that info, we may receive frames
1325 before knowing about rate/channels. */
1327 || !gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (aacparse))) {
1328 if (!gst_aac_parse_set_src_caps (aacparse, NULL)) {
1329 /* If linking fails, we need to return appropriate error */
1330 ret = GST_FLOW_NOT_LINKED;
1333 gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse),
1334 aacparse->sample_rate, aacparse->frame_samples, 2, 2);
1338 if (aacparse->header_type == DSPAAC_HEADER_NONE
1339 && aacparse->output_header_type == DSPAAC_HEADER_ADTS) {
1340 if (!gst_aac_parse_prepend_adts_headers (aacparse, frame)) {
1341 GST_ERROR_OBJECT (aacparse, "Failed to prepend ADTS headers to frame");
1342 ret = GST_FLOW_ERROR;
1347 gst_buffer_unmap (buffer, &map);
1350 /* found, skip if needed */
1359 if (ret && framesize <= map.size) {
1360 return gst_base_parse_finish_frame (parse, frame, framesize);
1366 static GstFlowReturn
1367 gst_aac_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
1369 GstAacParse *aacparse = GST_AAC_PARSE (parse);
1371 if (!aacparse->sent_codec_tag) {
1372 GstTagList *taglist;
1376 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
1378 if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
1379 GST_INFO_OBJECT (parse, "Src pad is flushing");
1380 return GST_FLOW_FLUSHING;
1382 GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
1383 return GST_FLOW_NOT_NEGOTIATED;
1387 taglist = gst_tag_list_new_empty ();
1388 gst_pb_utils_add_codec_description_to_tag_list (taglist,
1389 GST_TAG_AUDIO_CODEC, caps);
1390 gst_caps_unref (caps);
1392 gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
1393 gst_tag_list_unref (taglist);
1395 /* also signals the end of first-frame processing */
1396 aacparse->sent_codec_tag = TRUE;
1399 /* As a special case, we can remove the ADTS framing and output raw AAC. */
1400 if (aacparse->header_type == DSPAAC_HEADER_ADTS
1401 && aacparse->output_header_type == DSPAAC_HEADER_NONE) {
1404 gst_buffer_map (frame->buffer, &map, GST_MAP_READ);
1405 header_size = (map.data[1] & 1) ? 7 : 9; /* optional CRC */
1406 gst_buffer_unmap (frame->buffer, &map);
1407 gst_buffer_resize (frame->buffer, header_size,
1408 gst_buffer_get_size (frame->buffer) - header_size);
1416 * gst_aac_parse_start:
1417 * @parse: #GstBaseParse.
1419 * Implementation of "start" vmethod in #GstBaseParse class.
1421 * Returns: TRUE if startup succeeded.
1424 gst_aac_parse_start (GstBaseParse * parse)
1426 GstAacParse *aacparse;
1428 aacparse = GST_AAC_PARSE (parse);
1429 GST_DEBUG ("start");
1430 aacparse->frame_samples = 1024;
1431 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), ADTS_MAX_SIZE);
1432 aacparse->sent_codec_tag = FALSE;
1438 * gst_aac_parse_stop:
1439 * @parse: #GstBaseParse.
1441 * Implementation of "stop" vmethod in #GstBaseParse class.
1443 * Returns: TRUE is stopping succeeded.
1446 gst_aac_parse_stop (GstBaseParse * parse)
1453 remove_fields (GstCaps * caps)
1457 n = gst_caps_get_size (caps);
1458 for (i = 0; i < n; i++) {
1459 GstStructure *s = gst_caps_get_structure (caps, i);
1461 gst_structure_remove_field (s, "framed");
1466 add_conversion_fields (GstCaps * caps)
1470 n = gst_caps_get_size (caps);
1471 for (i = 0; i < n; i++) {
1472 GstStructure *s = gst_caps_get_structure (caps, i);
1474 if (gst_structure_has_field (s, "stream-format")) {
1475 const GValue *v = gst_structure_get_value (s, "stream-format");
1477 if (G_VALUE_HOLDS_STRING (v)) {
1478 const gchar *str = g_value_get_string (v);
1480 if (strcmp (str, "adts") == 0 || strcmp (str, "raw") == 0) {
1481 GValue va = G_VALUE_INIT;
1482 GValue vs = G_VALUE_INIT;
1484 g_value_init (&va, GST_TYPE_LIST);
1485 g_value_init (&vs, G_TYPE_STRING);
1486 g_value_set_string (&vs, "adts");
1487 gst_value_list_append_value (&va, &vs);
1488 g_value_set_string (&vs, "raw");
1489 gst_value_list_append_value (&va, &vs);
1490 gst_structure_set_value (s, "stream-format", &va);
1491 g_value_unset (&va);
1492 g_value_unset (&vs);
1494 } else if (GST_VALUE_HOLDS_LIST (v)) {
1495 gboolean contains_raw = FALSE;
1496 gboolean contains_adts = FALSE;
1497 guint m = gst_value_list_get_size (v), j;
1499 for (j = 0; j < m; j++) {
1500 const GValue *ve = gst_value_list_get_value (v, j);
1503 if (G_VALUE_HOLDS_STRING (ve) && (str = g_value_get_string (ve))) {
1504 if (strcmp (str, "adts") == 0)
1505 contains_adts = TRUE;
1506 else if (strcmp (str, "raw") == 0)
1507 contains_raw = TRUE;
1511 if (contains_adts || contains_raw) {
1512 GValue va = G_VALUE_INIT;
1513 GValue vs = G_VALUE_INIT;
1515 g_value_init (&va, GST_TYPE_LIST);
1516 g_value_init (&vs, G_TYPE_STRING);
1517 g_value_copy (v, &va);
1519 if (!contains_raw) {
1520 g_value_set_string (&vs, "raw");
1521 gst_value_list_append_value (&va, &vs);
1523 if (!contains_adts) {
1524 g_value_set_string (&vs, "adts");
1525 gst_value_list_append_value (&va, &vs);
1528 gst_structure_set_value (s, "stream-format", &va);
1530 g_value_unset (&vs);
1531 g_value_unset (&va);
1539 gst_aac_parse_sink_getcaps (GstBaseParse * parse, GstCaps * filter)
1541 GstCaps *peercaps, *templ;
1544 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1547 GstCaps *fcopy = gst_caps_copy (filter);
1548 /* Remove the fields we convert */
1549 remove_fields (fcopy);
1550 add_conversion_fields (fcopy);
1551 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
1552 gst_caps_unref (fcopy);
1554 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
1557 peercaps = gst_caps_make_writable (peercaps);
1558 /* Remove the fields we convert */
1559 remove_fields (peercaps);
1560 add_conversion_fields (peercaps);
1562 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1563 gst_caps_unref (peercaps);
1564 gst_caps_unref (templ);
1570 GstCaps *intersection;
1573 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1574 gst_caps_unref (res);