1 /* GStreamer AC3 parser
2 * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
3 * Copyright (C) 2009 Mark Nauwelaerts <mnauw users sf net>
4 * Copyright (C) 2009 Nokia Corporation. All rights reserved.
5 * Contact: Stefan Kost <stefan.kost@nokia.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 * SECTION:element-ac3parse
24 * @short_description: AC3 parser
25 * @see_also: #GstAmrParse, #GstAACParse
27 * This is an AC3 parser.
30 * <title>Example launch line</title>
32 * gst-launch-1.0 filesrc location=abc.ac3 ! ac3parse ! a52dec ! audioresample ! audioconvert ! autoaudiosink
38 * - audio/ac3 to audio/x-private1-ac3 is not implemented (done in the muxer)
39 * - should accept framed and unframed input (needs decodebin fixes first)
48 #include "gstac3parse.h"
49 #include <gst/base/base.h>
50 #include <gst/pbutils/pbutils.h>
52 GST_DEBUG_CATEGORY_STATIC (ac3_parse_debug);
53 #define GST_CAT_DEFAULT ac3_parse_debug
57 const guint bit_rate; /* nominal bit rate */
58 const guint frame_size[3]; /* frame size for 32kHz, 44kHz, and 48kHz */
59 } frmsizcod_table[38] = {
126 1024, 1114, 1536}}, {
128 1024, 1115, 1536}}, {
130 1152, 1253, 1728}}, {
132 1152, 1254, 1728}}, {
134 1280, 1393, 1920}}, {
139 static const guint fscod_rates[4] = { 48000, 44100, 32000, 0 };
140 static const guint acmod_chans[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
141 static const guint numblks[4] = { 1, 2, 3, 6 };
143 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
146 GST_STATIC_CAPS ("audio/x-ac3, framed = (boolean) true, "
147 " channels = (int) [ 1, 6 ], rate = (int) [ 8000, 48000 ], "
148 " alignment = (string) { iec61937, frame}; "
149 "audio/x-eac3, framed = (boolean) true, "
150 " channels = (int) [ 1, 6 ], rate = (int) [ 8000, 48000 ], "
151 " alignment = (string) { iec61937, frame}; "));
153 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
156 GST_STATIC_CAPS ("audio/x-ac3; " "audio/x-eac3; " "audio/ac3; "
157 "audio/x-private1-ac3"));
159 static void gst_ac3_parse_finalize (GObject * object);
161 static gboolean gst_ac3_parse_start (GstBaseParse * parse);
162 static gboolean gst_ac3_parse_stop (GstBaseParse * parse);
163 static GstFlowReturn gst_ac3_parse_handle_frame (GstBaseParse * parse,
164 GstBaseParseFrame * frame, gint * skipsize);
165 static GstFlowReturn gst_ac3_parse_pre_push_frame (GstBaseParse * parse,
166 GstBaseParseFrame * frame);
167 static gboolean gst_ac3_parse_src_event (GstBaseParse * parse,
169 static GstCaps *gst_ac3_parse_get_sink_caps (GstBaseParse * parse,
171 static gboolean gst_ac3_parse_set_sink_caps (GstBaseParse * parse,
174 #define gst_ac3_parse_parent_class parent_class
175 G_DEFINE_TYPE (GstAc3Parse, gst_ac3_parse, GST_TYPE_BASE_PARSE);
178 gst_ac3_parse_class_init (GstAc3ParseClass * klass)
180 GObjectClass *object_class = G_OBJECT_CLASS (klass);
181 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
182 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
184 GST_DEBUG_CATEGORY_INIT (ac3_parse_debug, "ac3parse", 0,
185 "AC3 audio stream parser");
187 object_class->finalize = gst_ac3_parse_finalize;
189 gst_element_class_add_pad_template (element_class,
190 gst_static_pad_template_get (&sink_template));
191 gst_element_class_add_pad_template (element_class,
192 gst_static_pad_template_get (&src_template));
194 gst_element_class_set_static_metadata (element_class,
195 "AC3 audio stream parser", "Codec/Parser/Converter/Audio",
196 "AC3 parser", "Tim-Philipp Müller <tim centricular net>");
198 parse_class->start = GST_DEBUG_FUNCPTR (gst_ac3_parse_start);
199 parse_class->stop = GST_DEBUG_FUNCPTR (gst_ac3_parse_stop);
200 parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_ac3_parse_handle_frame);
201 parse_class->pre_push_frame =
202 GST_DEBUG_FUNCPTR (gst_ac3_parse_pre_push_frame);
203 parse_class->src_event = GST_DEBUG_FUNCPTR (gst_ac3_parse_src_event);
204 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_ac3_parse_get_sink_caps);
205 parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_ac3_parse_set_sink_caps);
209 gst_ac3_parse_reset (GstAc3Parse * ac3parse)
211 ac3parse->channels = -1;
212 ac3parse->sample_rate = -1;
213 ac3parse->blocks = -1;
214 ac3parse->eac = FALSE;
215 ac3parse->sent_codec_tag = FALSE;
216 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_NONE);
220 gst_ac3_parse_init (GstAc3Parse * ac3parse)
222 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (ac3parse), 6);
223 gst_ac3_parse_reset (ac3parse);
224 ac3parse->baseparse_chainfunc =
225 GST_BASE_PARSE_SINK_PAD (GST_BASE_PARSE (ac3parse))->chainfunc;
226 GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (ac3parse));
230 gst_ac3_parse_finalize (GObject * object)
232 G_OBJECT_CLASS (parent_class)->finalize (object);
236 gst_ac3_parse_start (GstBaseParse * parse)
238 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
240 GST_DEBUG_OBJECT (parse, "starting");
242 gst_ac3_parse_reset (ac3parse);
248 gst_ac3_parse_stop (GstBaseParse * parse)
250 GST_DEBUG_OBJECT (parse, "stopping");
256 gst_ac3_parse_set_alignment (GstAc3Parse * ac3parse, gboolean eac)
260 const gchar *str = NULL;
266 caps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (ac3parse));
271 for (i = 0; i < gst_caps_get_size (caps); i++) {
272 st = gst_caps_get_structure (caps, i);
274 if (!g_str_equal (gst_structure_get_name (st), "audio/x-eac3"))
277 if ((str = gst_structure_get_string (st, "alignment"))) {
278 if (g_str_equal (str, "iec61937")) {
279 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
280 GST_DEBUG_OBJECT (ac3parse, "picked iec61937 alignment");
281 } else if (g_str_equal (str, "frame") == 0) {
282 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
283 GST_DEBUG_OBJECT (ac3parse, "picked frame alignment");
285 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
286 GST_WARNING_OBJECT (ac3parse, "unknown alignment: %s", str);
293 gst_caps_unref (caps);
297 if (ac3parse->align == GST_AC3_PARSE_ALIGN_NONE) {
298 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
299 GST_DEBUG_OBJECT (ac3parse, "picked syncframe alignment");
304 gst_ac3_parse_frame_header_ac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
305 gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
310 guint8 fscod, frmsizcod, bsid, acmod, lfe_on, rate_scale;
311 gboolean ret = FALSE;
313 GST_LOG_OBJECT (ac3parse, "parsing ac3");
315 gst_buffer_map (buf, &map, GST_MAP_READ);
316 gst_bit_reader_init (&bits, map.data, map.size);
317 gst_bit_reader_skip_unchecked (&bits, skip * 8);
319 gst_bit_reader_skip_unchecked (&bits, 16 + 16);
320 fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);
321 frmsizcod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 6);
323 if (G_UNLIKELY (fscod == 3 || frmsizcod >= G_N_ELEMENTS (frmsizcod_table))) {
324 GST_DEBUG_OBJECT (ac3parse, "bad fscod=%d frmsizcod=%d", fscod, frmsizcod);
328 bsid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 5);
329 gst_bit_reader_skip_unchecked (&bits, 3); /* bsmod */
330 acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);
332 /* spec not quite clear here: decoder should decode if less than 8,
333 * but seemingly only defines 6 and 8 cases */
334 /* Files with 9 and 10 happen, and seem to comply with the <= 8
335 format, so let them through. The spec says nothing about 9 and 10 */
337 GST_DEBUG_OBJECT (ac3parse, "unexpected bsid=%d", bsid);
339 } else if (bsid != 8 && bsid != 6) {
340 GST_DEBUG_OBJECT (ac3parse, "undefined bsid=%d", bsid);
343 if ((acmod & 0x1) && (acmod != 0x1)) /* 3 front channels */
344 gst_bit_reader_skip_unchecked (&bits, 2);
345 if ((acmod & 0x4)) /* if a surround channel exists */
346 gst_bit_reader_skip_unchecked (&bits, 2);
347 if (acmod == 0x2) /* if in 2/0 mode */
348 gst_bit_reader_skip_unchecked (&bits, 2);
350 lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);
352 /* 6/8->0, 9->1, 10->2,
353 see http://matroska.org/technical/specs/codecid/index.html */
354 rate_scale = (CLAMP (bsid, 8, 10) - 8);
357 *frame_size = frmsizcod_table[frmsizcod].frame_size[fscod] * 2;
359 *rate = fscod_rates[fscod] >> rate_scale;
361 *chans = acmod_chans[acmod] + lfe_on;
370 gst_buffer_unmap (buf, &map);
376 gst_ac3_parse_frame_header_eac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
377 gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
382 guint16 frmsiz, sample_rate, blocks;
383 guint8 strmtyp, fscod, fscod2, acmod, lfe_on, strmid, numblkscod;
384 gboolean ret = FALSE;
386 GST_LOG_OBJECT (ac3parse, "parsing e-ac3");
388 gst_buffer_map (buf, &map, GST_MAP_READ);
389 gst_bit_reader_init (&bits, map.data, map.size);
390 gst_bit_reader_skip_unchecked (&bits, skip * 8);
392 gst_bit_reader_skip_unchecked (&bits, 16);
393 strmtyp = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* strmtyp */
394 if (G_UNLIKELY (strmtyp == 3)) {
395 GST_DEBUG_OBJECT (ac3parse, "bad strmtyp %d", strmtyp);
399 strmid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* substreamid */
400 frmsiz = gst_bit_reader_get_bits_uint16_unchecked (&bits, 11); /* frmsiz */
401 fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod */
403 fscod2 = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod2 */
404 if (G_UNLIKELY (fscod2 == 3)) {
405 GST_DEBUG_OBJECT (ac3parse, "invalid fscod2");
408 sample_rate = fscod_rates[fscod2] / 2;
411 numblkscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* numblkscod */
412 sample_rate = fscod_rates[fscod];
413 blocks = numblks[numblkscod];
416 acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* acmod */
417 lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1); /* lfeon */
419 gst_bit_reader_skip_unchecked (&bits, 5); /* bsid */
422 *frame_size = (frmsiz + 1) * 2;
426 *chans = acmod_chans[acmod] + lfe_on;
430 *sid = (strmtyp & 0x1) << 3 | strmid;
435 gst_buffer_unmap (buf, &map);
441 gst_ac3_parse_frame_header (GstAc3Parse * parse, GstBuffer * buf, gint skip,
442 guint * framesize, guint * rate, guint * chans, guint * blocks,
443 guint * sid, gboolean * eac)
449 gboolean ret = FALSE;
451 gst_buffer_map (buf, &map, GST_MAP_READ);
452 gst_bit_reader_init (&bits, map.data, map.size);
454 GST_MEMDUMP_OBJECT (parse, "AC3 frame sync", map.data, MIN (map.size, 16));
456 gst_bit_reader_skip_unchecked (&bits, skip * 8);
458 sync = gst_bit_reader_get_bits_uint16_unchecked (&bits, 16);
459 gst_bit_reader_skip_unchecked (&bits, 16 + 8);
460 bsid = gst_bit_reader_peek_bits_uint8_unchecked (&bits, 5);
462 if (G_UNLIKELY (sync != 0x0b77))
465 GST_LOG_OBJECT (parse, "bsid = %d", bsid);
470 ret = gst_ac3_parse_frame_header_ac3 (parse, buf, skip, framesize, rate,
473 } else if (bsid <= 16) {
476 ret = gst_ac3_parse_frame_header_eac3 (parse, buf, skip, framesize, rate,
480 GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
484 GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
487 gst_buffer_unmap (buf, &map);
493 gst_ac3_parse_handle_frame (GstBaseParse * parse,
494 GstBaseParseFrame * frame, gint * skipsize)
496 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
497 GstBuffer *buf = frame->buffer;
498 GstByteReader reader;
500 gboolean lost_sync, draining, eac, more = FALSE;
501 guint frmsiz, blocks, sid;
503 gboolean update_rate = FALSE;
505 gint have_blocks = 0;
507 gboolean ret = FALSE;
508 GstFlowReturn res = GST_FLOW_OK;
510 gst_buffer_map (buf, &map, GST_MAP_READ);
512 if (G_UNLIKELY (map.size < 6)) {
517 gst_byte_reader_init (&reader, map.data, map.size);
518 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000, 0x0b770000,
521 GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
523 /* didn't find anything that looks like a sync word, skip */
525 *skipsize = map.size - 3;
529 /* possible frame header, but not at offset 0? skip bytes before sync */
535 /* make sure the values in the frame header look sane */
536 if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &frmsiz, &rate, &chans,
537 &blocks, &sid, &eac)) {
542 GST_LOG_OBJECT (parse, "size: %u, blocks: %u, rate: %u, chans: %u", frmsiz,
543 blocks, rate, chans);
547 if (G_UNLIKELY (g_atomic_int_get (&ac3parse->align) ==
548 GST_AC3_PARSE_ALIGN_NONE))
549 gst_ac3_parse_set_alignment (ac3parse, eac);
551 GST_LOG_OBJECT (parse, "got frame");
553 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
554 draining = GST_BASE_PARSE_DRAINING (parse);
556 if (g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937) {
557 /* We need 6 audio blocks from each substream, so we keep going forwards
560 g_assert (blocks > 0);
561 GST_LOG_OBJECT (ac3parse, "Need %d frames before pushing", 6 / blocks);
564 /* We need the first substream to be the one with id 0 */
565 GST_LOG_OBJECT (ac3parse, "Skipping till we find sid 0");
572 /* Loop till we have 6 blocks per substream */
573 for (have_blocks = 0; !more && have_blocks < 6; have_blocks += blocks) {
574 /* Loop till we get one frame from each substream */
578 if (!gst_byte_reader_skip (&reader, frmsiz)
579 || map.size < (framesize + 6)) {
584 if (!gst_ac3_parse_frame_header (ac3parse, buf, framesize, &frmsiz,
585 NULL, NULL, NULL, &sid, &eac)) {
592 /* We're now at the next frame, so no need to skip if resyncing */
596 if (lost_sync && !draining) {
599 GST_DEBUG_OBJECT (ac3parse, "resyncing; checking next frame syncword");
601 if (more || !gst_byte_reader_skip (&reader, frmsiz) ||
602 !gst_byte_reader_get_uint16_be (&reader, &word)) {
603 GST_DEBUG_OBJECT (ac3parse, "... but not sufficient data");
604 gst_base_parse_set_min_frame_size (parse, framesize + 6);
608 if (word != 0x0b77) {
609 GST_DEBUG_OBJECT (ac3parse, "0x%x not OK", word);
613 /* ok, got sync now, let's assume constant frame size */
614 gst_base_parse_set_min_frame_size (parse, framesize);
619 /* expect to have found a frame here */
620 g_assert (framesize);
623 /* arrange for metadata setup */
624 if (G_UNLIKELY (sid)) {
625 /* dependent frame, no need to (ac)count for or consider further */
626 GST_LOG_OBJECT (parse, "sid: %d", sid);
627 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME;
628 /* TODO maybe also mark as DELTA_UNIT,
629 * if that does not surprise baseparse elsewhere */
630 /* occupies same time space as previous base frame */
631 if (G_LIKELY (GST_BUFFER_TIMESTAMP (buf) >= GST_BUFFER_DURATION (buf)))
632 GST_BUFFER_TIMESTAMP (buf) -= GST_BUFFER_DURATION (buf);
633 /* only shortcut if we already arranged for caps */
634 if (G_LIKELY (ac3parse->sample_rate > 0))
638 if (G_UNLIKELY (ac3parse->sample_rate != rate || ac3parse->channels != chans
639 || ac3parse->eac != eac)) {
640 GstCaps *caps = gst_caps_new_simple (eac ? "audio/x-eac3" : "audio/x-ac3",
641 "framed", G_TYPE_BOOLEAN, TRUE, "rate", G_TYPE_INT, rate,
642 "channels", G_TYPE_INT, chans, NULL);
643 gst_caps_set_simple (caps, "alignment", G_TYPE_STRING,
644 g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937 ?
645 "iec61937" : "frame", NULL);
646 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
647 gst_caps_unref (caps);
649 ac3parse->sample_rate = rate;
650 ac3parse->channels = chans;
656 if (G_UNLIKELY (ac3parse->blocks != blocks)) {
657 ac3parse->blocks = blocks;
662 if (G_UNLIKELY (update_rate))
663 gst_base_parse_set_frame_rate (parse, rate, 256 * blocks, 2, 2);
666 gst_buffer_unmap (buf, &map);
668 if (ret && framesize <= map.size) {
669 res = gst_base_parse_finish_frame (parse, frame, framesize);
677 * MPEG-PS private1 streams add a 2 bytes "Audio Substream Headers" for each
678 * buffer (not each frame) with the offset of the next frame's start.
681 * -------------------------------------------
682 * |firstAccUnit|AC3SyncWord|xxxxxxxxxxxxxxxxx
683 * -------------------------------------------
685 * -------------------------------------------
686 * |firstAccUnit|xxxxxx|AC3SyncWord|xxxxxxxxxx
687 * -------------------------------------------
689 * These 2 bytes can be dropped safely as they do not include any timing
690 * information, only the offset to the start of the next frame.
692 * From http://stnsoft.com/DVD/ass-hdr.html:
693 * "FirstAccUnit offset to frame which corresponds to PTS value offset 0 is the
694 * last byte of FirstAccUnit, ie add the offset of byte 2 to get the AU's offset
695 * The value 0000 indicates there is no first access unit"
699 gst_ac3_parse_chain_priv (GstPad * pad, GstObject * parent, GstBuffer * buf)
701 GstAc3Parse *ac3parse = GST_AC3_PARSE (parent);
710 size = gst_buffer_get_size (buf);
712 goto not_enough_data;
714 gst_buffer_extract (buf, 0, data, 2);
715 first_access = (data[0] << 8) | data[1];
717 /* Skip the first_access header */
720 if (first_access > 1) {
721 /* Length of data before first_access */
722 len = first_access - 1;
724 if (len <= 0 || offset + len > size)
725 goto bad_first_access_parameter;
727 subbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset, len);
728 GST_BUFFER_DTS (subbuf) = GST_CLOCK_TIME_NONE;
729 GST_BUFFER_PTS (subbuf) = GST_CLOCK_TIME_NONE;
730 ret = ac3parse->baseparse_chainfunc (pad, parent, subbuf);
731 if (ret != GST_FLOW_OK) {
732 gst_buffer_unref (buf);
740 subbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset, len);
741 GST_BUFFER_PTS (subbuf) = GST_BUFFER_PTS (buf);
742 GST_BUFFER_DTS (subbuf) = GST_BUFFER_DTS (buf);
744 ret = ac3parse->baseparse_chainfunc (pad, parent, subbuf);
746 gst_buffer_unref (buf);
748 /* first_access = 0 or 1, so if there's a timestamp it applies to the first byte */
750 gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset,
752 GST_BUFFER_PTS (subbuf) = GST_BUFFER_PTS (buf);
753 GST_BUFFER_DTS (subbuf) = GST_BUFFER_DTS (buf);
754 gst_buffer_unref (buf);
755 ret = ac3parse->baseparse_chainfunc (pad, parent, subbuf);
764 GST_ELEMENT_ERROR (GST_ELEMENT (ac3parse), STREAM, FORMAT, (NULL),
765 ("Insufficient data in buffer. Can't determine first_acess"));
766 gst_buffer_unref (buf);
767 return GST_FLOW_ERROR;
769 bad_first_access_parameter:
771 GST_ELEMENT_ERROR (GST_ELEMENT (ac3parse), STREAM, FORMAT, (NULL),
772 ("Bad first_access parameter (%d) in buffer", first_access));
773 gst_buffer_unref (buf);
774 return GST_FLOW_ERROR;
779 gst_ac3_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
781 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
783 if (!ac3parse->sent_codec_tag) {
787 taglist = gst_tag_list_new_empty ();
790 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
791 gst_pb_utils_add_codec_description_to_tag_list (taglist,
792 GST_TAG_AUDIO_CODEC, caps);
793 gst_caps_unref (caps);
795 gst_pad_push_event (GST_BASE_PARSE_SRC_PAD (ac3parse),
796 gst_event_new_tag (taglist));
798 /* also signals the end of first-frame processing */
799 ac3parse->sent_codec_tag = TRUE;
806 gst_ac3_parse_src_event (GstBaseParse * parse, GstEvent * event)
808 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
810 if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) &&
811 gst_event_has_name (event, "ac3parse-set-alignment")) {
812 const GstStructure *st = gst_event_get_structure (event);
813 const gchar *align = gst_structure_get_string (st, "alignment");
815 if (g_str_equal (align, "iec61937")) {
816 GST_DEBUG_OBJECT (ac3parse, "Switching to iec61937 alignment");
817 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
818 } else if (g_str_equal (align, "frame")) {
819 GST_DEBUG_OBJECT (ac3parse, "Switching to frame alignment");
820 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
822 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
823 GST_WARNING_OBJECT (ac3parse, "Got unknown alignment request (%s) "
824 "reverting to frame alignment.",
825 gst_structure_get_string (st, "alignment"));
828 gst_event_unref (event);
832 return GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
836 remove_fields (GstCaps * caps)
840 n = gst_caps_get_size (caps);
841 for (i = 0; i < n; i++) {
842 GstStructure *s = gst_caps_get_structure (caps, i);
844 gst_structure_remove_field (s, "framed");
845 gst_structure_remove_field (s, "alignment");
850 extend_caps (GstCaps * caps, gboolean add_private)
853 GstCaps *ncaps = gst_caps_new_empty ();
855 n = gst_caps_get_size (caps);
856 for (i = 0; i < n; i++) {
857 GstStructure *s = gst_caps_get_structure (caps, i);
859 if (add_private && !gst_structure_has_name (s, "audio/x-private1-ac3")) {
860 GstStructure *ns = gst_structure_copy (s);
861 gst_structure_set_name (ns, "audio/x-private1-ac3");
862 gst_caps_append_structure (ncaps, ns);
863 } else if (!add_private &&
864 gst_structure_has_name (s, "audio/x-private1-ac3")) {
865 GstStructure *ns = gst_structure_copy (s);
866 gst_structure_set_name (ns, "audio/x-ac3");
867 gst_caps_append_structure (ncaps, ns);
868 ns = gst_structure_copy (s);
869 gst_structure_set_name (ns, "audio/x-eac3");
870 gst_caps_append_structure (ncaps, ns);
871 } else if (!add_private) {
872 gst_caps_append_structure (ncaps, gst_structure_copy (s));
877 gst_caps_append (caps, ncaps);
879 gst_caps_unref (caps);
887 gst_ac3_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
889 GstCaps *peercaps, *templ;
892 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
894 GstCaps *fcopy = gst_caps_copy (filter);
895 /* Remove the fields we convert */
896 remove_fields (fcopy);
897 /* we do not ask downstream to handle x-private1-ac3 */
898 fcopy = extend_caps (fcopy, FALSE);
899 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
900 gst_caps_unref (fcopy);
902 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
905 /* Remove the framed and alignment field. We can convert
906 * between different alignments. */
907 peercaps = gst_caps_make_writable (peercaps);
908 remove_fields (peercaps);
909 /* also allow for x-private1-ac3 input */
910 peercaps = extend_caps (peercaps, TRUE);
912 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
913 gst_caps_unref (peercaps);
914 gst_caps_unref (templ);
920 GstCaps *intersection;
923 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
924 gst_caps_unref (res);
932 gst_ac3_parse_set_sink_caps (GstBaseParse * parse, GstCaps * caps)
935 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
937 s = gst_caps_get_structure (caps, 0);
938 if (gst_structure_has_name (s, "audio/x-private1-ac3")) {
939 gst_pad_set_chain_function (parse->sinkpad, gst_ac3_parse_chain_priv);
941 gst_pad_set_chain_function (parse->sinkpad, ac3parse->baseparse_chainfunc);