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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, 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 filesrc location=abc.ac3 ! ac3parse ! a52dec ! audioresample ! audioconvert ! autoaudiosink
38 * - add support for audio/x-private1-ac3 as well
39 * - should accept framed and unframed input (needs decodebin fixes first)
48 #include "gstac3parse.h"
49 #include <gst/base/gstbytereader.h>
50 #include <gst/base/gstbitreader.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"));
158 static void gst_ac3_parse_finalize (GObject * object);
160 static gboolean gst_ac3_parse_start (GstBaseParse * parse);
161 static gboolean gst_ac3_parse_stop (GstBaseParse * parse);
162 static gboolean gst_ac3_parse_check_valid_frame (GstBaseParse * parse,
163 GstBaseParseFrame * frame, guint * size, gint * skipsize);
164 static GstFlowReturn gst_ac3_parse_parse_frame (GstBaseParse * parse,
165 GstBaseParseFrame * frame);
166 static gboolean gst_ac3_parse_src_event (GstBaseParse * parse,
168 static GstCaps *gst_ac3_parse_get_sink_caps (GstBaseParse * parse);
170 GST_BOILERPLATE (GstAc3Parse, gst_ac3_parse, GstBaseParse, GST_TYPE_BASE_PARSE);
173 gst_ac3_parse_base_init (gpointer klass)
175 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
177 gst_element_class_add_static_pad_template (element_class,
179 gst_element_class_add_static_pad_template (element_class, &src_template);
181 gst_element_class_set_details_simple (element_class,
182 "AC3 audio stream parser", "Codec/Parser/Converter/Audio",
183 "AC3 parser", "Tim-Philipp Müller <tim centricular net>");
187 gst_ac3_parse_class_init (GstAc3ParseClass * klass)
189 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
190 GObjectClass *object_class = G_OBJECT_CLASS (klass);
192 GST_DEBUG_CATEGORY_INIT (ac3_parse_debug, "ac3parse", 0,
193 "AC3 audio stream parser");
195 object_class->finalize = gst_ac3_parse_finalize;
197 parse_class->start = GST_DEBUG_FUNCPTR (gst_ac3_parse_start);
198 parse_class->stop = GST_DEBUG_FUNCPTR (gst_ac3_parse_stop);
199 parse_class->check_valid_frame =
200 GST_DEBUG_FUNCPTR (gst_ac3_parse_check_valid_frame);
201 parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_ac3_parse_parse_frame);
202 parse_class->src_event = GST_DEBUG_FUNCPTR (gst_ac3_parse_src_event);
203 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_ac3_parse_get_sink_caps);
207 gst_ac3_parse_reset (GstAc3Parse * ac3parse)
209 ac3parse->channels = -1;
210 ac3parse->sample_rate = -1;
211 ac3parse->blocks = -1;
212 ac3parse->eac = FALSE;
213 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_NONE);
217 gst_ac3_parse_init (GstAc3Parse * ac3parse, GstAc3ParseClass * klass)
219 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (ac3parse), 6);
220 gst_ac3_parse_reset (ac3parse);
224 gst_ac3_parse_finalize (GObject * object)
226 G_OBJECT_CLASS (parent_class)->finalize (object);
230 gst_ac3_parse_start (GstBaseParse * parse)
232 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
234 GST_DEBUG_OBJECT (parse, "starting");
236 gst_ac3_parse_reset (ac3parse);
242 gst_ac3_parse_stop (GstBaseParse * parse)
244 GST_DEBUG_OBJECT (parse, "stopping");
250 gst_ac3_parse_set_alignment (GstAc3Parse * ac3parse, gboolean eac)
254 const gchar *str = NULL;
260 caps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (ac3parse));
265 for (i = 0; i < gst_caps_get_size (caps); i++) {
266 st = gst_caps_get_structure (caps, i);
268 if (!g_str_equal (gst_structure_get_name (st), "audio/x-eac3"))
271 if ((str = gst_structure_get_string (st, "alignment"))) {
272 if (g_str_equal (str, "iec61937")) {
273 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
274 GST_DEBUG_OBJECT (ac3parse, "picked iec61937 alignment");
275 } else if (g_str_equal (str, "frame") == 0) {
276 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
277 GST_DEBUG_OBJECT (ac3parse, "picked frame alignment");
279 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
280 GST_WARNING_OBJECT (ac3parse, "unknown alignment: %s", str);
287 gst_caps_unref (caps);
291 if (ac3parse->align == GST_AC3_PARSE_ALIGN_NONE) {
292 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
293 GST_DEBUG_OBJECT (ac3parse, "picked syncframe alignment");
298 gst_ac3_parse_frame_header_ac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
299 gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
302 GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
303 guint8 fscod, frmsizcod, bsid, acmod, lfe_on, rate_scale;
305 GST_LOG_OBJECT (ac3parse, "parsing ac3");
307 gst_bit_reader_skip_unchecked (&bits, skip * 8);
309 gst_bit_reader_skip_unchecked (&bits, 16 + 16);
310 fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);
311 frmsizcod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 6);
313 if (G_UNLIKELY (fscod == 3 || frmsizcod >= G_N_ELEMENTS (frmsizcod_table))) {
314 GST_DEBUG_OBJECT (ac3parse, "bad fscod=%d frmsizcod=%d", fscod, frmsizcod);
318 bsid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 5);
319 gst_bit_reader_skip_unchecked (&bits, 3); /* bsmod */
320 acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);
322 /* spec not quite clear here: decoder should decode if less than 8,
323 * but seemingly only defines 6 and 8 cases */
324 /* Files with 9 and 10 happen, and seem to comply with the <= 8
325 format, so let them through. The spec says nothing about 9 and 10 */
327 GST_DEBUG_OBJECT (ac3parse, "unexpected bsid=%d", bsid);
329 } else if (bsid != 8 && bsid != 6) {
330 GST_DEBUG_OBJECT (ac3parse, "undefined bsid=%d", bsid);
333 if ((acmod & 0x1) && (acmod != 0x1)) /* 3 front channels */
334 gst_bit_reader_skip_unchecked (&bits, 2);
335 if ((acmod & 0x4)) /* if a surround channel exists */
336 gst_bit_reader_skip_unchecked (&bits, 2);
337 if (acmod == 0x2) /* if in 2/0 mode */
338 gst_bit_reader_skip_unchecked (&bits, 2);
340 lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);
342 /* 6/8->0, 9->1, 10->2,
343 see http://matroska.org/technical/specs/codecid/index.html */
344 rate_scale = (CLAMP (bsid, 8, 10) - 8);
347 *frame_size = frmsizcod_table[frmsizcod].frame_size[fscod] * 2;
349 *rate = fscod_rates[fscod] >> rate_scale;
351 *chans = acmod_chans[acmod] + lfe_on;
361 gst_ac3_parse_frame_header_eac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
362 gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
365 GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
366 guint16 frmsiz, sample_rate, blocks;
367 guint8 strmtyp, fscod, fscod2, acmod, lfe_on, strmid, numblkscod;
369 GST_LOG_OBJECT (ac3parse, "parsing e-ac3");
371 gst_bit_reader_skip_unchecked (&bits, skip * 8);
373 gst_bit_reader_skip_unchecked (&bits, 16);
374 strmtyp = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* strmtyp */
375 if (G_UNLIKELY (strmtyp == 3)) {
376 GST_DEBUG_OBJECT (ac3parse, "bad strmtyp %d", strmtyp);
380 strmid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* substreamid */
381 frmsiz = gst_bit_reader_get_bits_uint16_unchecked (&bits, 11); /* frmsiz */
382 fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod */
384 fscod2 = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod2 */
385 if (G_UNLIKELY (fscod2 == 3)) {
386 GST_DEBUG_OBJECT (ac3parse, "invalid fscod2");
389 sample_rate = fscod_rates[fscod2] / 2;
392 numblkscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* numblkscod */
393 sample_rate = fscod_rates[fscod];
394 blocks = numblks[numblkscod];
397 acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* acmod */
398 lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1); /* lfeon */
400 gst_bit_reader_skip_unchecked (&bits, 5); /* bsid */
403 *frame_size = (frmsiz + 1) * 2;
407 *chans = acmod_chans[acmod] + lfe_on;
411 *sid = (strmtyp & 0x1) << 3 | strmid;
417 gst_ac3_parse_frame_header (GstAc3Parse * parse, GstBuffer * buf, gint skip,
418 guint * framesize, guint * rate, guint * chans, guint * blocks,
419 guint * sid, gboolean * eac)
421 GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
425 GST_MEMDUMP_OBJECT (parse, "AC3 frame sync", GST_BUFFER_DATA (buf), 16);
427 gst_bit_reader_skip_unchecked (&bits, skip * 8);
429 sync = gst_bit_reader_get_bits_uint16_unchecked (&bits, 16);
430 gst_bit_reader_skip_unchecked (&bits, 16 + 8);
431 bsid = gst_bit_reader_peek_bits_uint8_unchecked (&bits, 5);
433 if (G_UNLIKELY (sync != 0x0b77))
436 GST_LOG_OBJECT (parse, "bsid = %d", bsid);
441 return gst_ac3_parse_frame_header_ac3 (parse, buf, skip, framesize, rate,
443 } else if (bsid <= 16) {
446 return gst_ac3_parse_frame_header_eac3 (parse, buf, skip, framesize, rate,
449 GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
455 gst_ac3_parse_check_valid_frame (GstBaseParse * parse,
456 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
458 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
459 GstBuffer *buf = frame->buffer;
460 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buf);
462 gboolean lost_sync, draining, eac, more = FALSE;
463 guint frmsiz, blocks, sid;
464 gint have_blocks = 0;
466 if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < 6))
469 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000, 0x0b770000,
470 0, GST_BUFFER_SIZE (buf));
472 GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
474 /* didn't find anything that looks like a sync word, skip */
476 *skipsize = GST_BUFFER_SIZE (buf) - 3;
480 /* possible frame header, but not at offset 0? skip bytes before sync */
486 /* make sure the values in the frame header look sane */
487 if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &frmsiz, NULL, NULL,
488 &blocks, &sid, &eac)) {
495 if (G_UNLIKELY (g_atomic_int_get (&ac3parse->align) ==
496 GST_AC3_PARSE_ALIGN_NONE))
497 gst_ac3_parse_set_alignment (ac3parse, eac);
499 GST_LOG_OBJECT (parse, "got frame");
501 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
502 draining = GST_BASE_PARSE_DRAINING (parse);
504 if (g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937) {
505 /* We need 6 audio blocks from each substream, so we keep going forwards
508 g_assert (blocks > 0);
509 GST_LOG_OBJECT (ac3parse, "Need %d frames before pushing", 6 / blocks);
512 /* We need the first substream to be the one with id 0 */
513 GST_LOG_OBJECT (ac3parse, "Skipping till we find sid 0");
520 /* Loop till we have 6 blocks per substream */
521 for (have_blocks = 0; !more && have_blocks < 6; have_blocks += blocks) {
522 /* Loop till we get one frame from each substream */
524 *framesize += frmsiz;
526 if (!gst_byte_reader_skip (&reader, frmsiz) ||
527 GST_BUFFER_SIZE (buf) < (*framesize + 6)) {
532 if (!gst_ac3_parse_frame_header (ac3parse, buf, *framesize, &frmsiz,
533 NULL, NULL, NULL, &sid, &eac)) {
540 /* We're now at the next frame, so no need to skip if resyncing */
544 if (lost_sync && !draining) {
547 GST_DEBUG_OBJECT (ac3parse, "resyncing; checking next frame syncword");
549 if (more || !gst_byte_reader_skip (&reader, frmsiz) ||
550 !gst_byte_reader_get_uint16_be (&reader, &word)) {
551 GST_DEBUG_OBJECT (ac3parse, "... but not sufficient data");
552 gst_base_parse_set_min_frame_size (parse, *framesize + 6);
556 if (word != 0x0b77) {
557 GST_DEBUG_OBJECT (ac3parse, "0x%x not OK", word);
561 /* ok, got sync now, let's assume constant frame size */
562 gst_base_parse_set_min_frame_size (parse, *framesize);
571 gst_ac3_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
573 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
574 GstBuffer *buf = frame->buffer;
575 guint fsize, rate, chans, blocks, sid;
576 gboolean eac, update_rate = FALSE;
578 if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &fsize, &rate, &chans,
579 &blocks, &sid, &eac))
582 GST_LOG_OBJECT (parse, "size: %u, blocks: %u, rate: %u, chans: %u", fsize,
583 blocks, rate, chans);
585 if (G_UNLIKELY (sid)) {
586 /* dependent frame, no need to (ac)count for or consider further */
587 GST_LOG_OBJECT (parse, "sid: %d", sid);
588 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME;
589 /* TODO maybe also mark as DELTA_UNIT,
590 * if that does not surprise baseparse elsewhere */
591 /* occupies same time space as previous base frame */
592 if (G_LIKELY (GST_BUFFER_TIMESTAMP (buf) >= GST_BUFFER_DURATION (buf)))
593 GST_BUFFER_TIMESTAMP (buf) -= GST_BUFFER_DURATION (buf);
594 /* only return if we already arranged for caps */
595 if (G_LIKELY (ac3parse->sample_rate > 0))
599 if (G_UNLIKELY (ac3parse->sample_rate != rate || ac3parse->channels != chans
600 || ac3parse->eac != eac)) {
601 GstCaps *caps = gst_caps_new_simple (eac ? "audio/x-eac3" : "audio/x-ac3",
602 "framed", G_TYPE_BOOLEAN, TRUE, "rate", G_TYPE_INT, rate,
603 "channels", G_TYPE_INT, chans, NULL);
604 gst_caps_set_simple (caps, "alignment", G_TYPE_STRING,
605 g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937 ?
606 "iec61937" : "frame", NULL);
607 gst_buffer_set_caps (buf, caps);
608 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
609 gst_caps_unref (caps);
611 ac3parse->sample_rate = rate;
612 ac3parse->channels = chans;
618 if (G_UNLIKELY (ac3parse->blocks != blocks)) {
619 ac3parse->blocks = blocks;
624 if (G_UNLIKELY (update_rate))
625 gst_base_parse_set_frame_rate (parse, rate, 256 * blocks, 2, 2);
632 /* this really shouldn't ever happen */
633 GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
634 return GST_FLOW_ERROR;
639 gst_ac3_parse_src_event (GstBaseParse * parse, GstEvent * event)
641 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
643 if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) &&
644 gst_event_has_name (event, "ac3parse-set-alignment")) {
645 const GstStructure *st = gst_event_get_structure (event);
646 const gchar *align = gst_structure_get_string (st, "alignment");
648 if (g_str_equal (align, "iec61937")) {
649 GST_DEBUG_OBJECT (ac3parse, "Switching to iec61937 alignment");
650 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
651 } else if (g_str_equal (align, "frame")) {
652 GST_DEBUG_OBJECT (ac3parse, "Switching to frame alignment");
653 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
655 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
656 GST_WARNING_OBJECT (ac3parse, "Got unknown alignment request (%s) "
657 "reverting to frame alignment.",
658 gst_structure_get_string (st, "alignment"));
661 gst_event_unref (event);
665 return GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
669 gst_ac3_parse_get_sink_caps (GstBaseParse * parse)
674 peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
678 /* Remove the framed and alignment field. We can convert
679 * between different alignments. */
680 peercaps = gst_caps_make_writable (peercaps);
681 n = gst_caps_get_size (peercaps);
682 for (i = 0; i < n; i++) {
683 GstStructure *s = gst_caps_get_structure (peercaps, i);
685 gst_structure_remove_field (s, "framed");
686 gst_structure_remove_field (s, "alignment");
690 gst_caps_intersect_full (peercaps,
691 gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)),
692 GST_CAPS_INTERSECT_FIRST);
693 gst_caps_unref (peercaps);
696 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD