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_pad_template (element_class,
178 gst_static_pad_template_get (&sink_template));
179 gst_element_class_add_pad_template (element_class,
180 gst_static_pad_template_get (&src_template));
182 gst_element_class_set_details_simple (element_class,
183 "AC3 audio stream parser", "Codec/Parser/Converter/Audio",
184 "AC3 parser", "Tim-Philipp Müller <tim centricular net>");
188 gst_ac3_parse_class_init (GstAc3ParseClass * klass)
190 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
191 GObjectClass *object_class = G_OBJECT_CLASS (klass);
193 GST_DEBUG_CATEGORY_INIT (ac3_parse_debug, "ac3parse", 0,
194 "AC3 audio stream parser");
196 object_class->finalize = gst_ac3_parse_finalize;
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->check_valid_frame =
201 GST_DEBUG_FUNCPTR (gst_ac3_parse_check_valid_frame);
202 parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_ac3_parse_parse_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);
208 gst_ac3_parse_reset (GstAc3Parse * ac3parse)
210 ac3parse->channels = -1;
211 ac3parse->sample_rate = -1;
212 ac3parse->blocks = -1;
213 ac3parse->eac = FALSE;
214 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_NONE);
218 gst_ac3_parse_init (GstAc3Parse * ac3parse, GstAc3ParseClass * klass)
220 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (ac3parse), 6);
221 gst_ac3_parse_reset (ac3parse);
225 gst_ac3_parse_finalize (GObject * object)
227 G_OBJECT_CLASS (parent_class)->finalize (object);
231 gst_ac3_parse_start (GstBaseParse * parse)
233 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
235 GST_DEBUG_OBJECT (parse, "starting");
237 gst_ac3_parse_reset (ac3parse);
243 gst_ac3_parse_stop (GstBaseParse * parse)
245 GST_DEBUG_OBJECT (parse, "stopping");
251 gst_ac3_parse_set_alignment (GstAc3Parse * ac3parse, gboolean eac)
255 const gchar *str = NULL;
261 caps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (ac3parse));
266 for (i = 0; i < gst_caps_get_size (caps); i++) {
267 st = gst_caps_get_structure (caps, i);
269 if (!g_str_equal (gst_structure_get_name (st), "audio/x-eac3"))
272 if ((str = gst_structure_get_string (st, "alignment"))) {
273 if (g_str_equal (str, "iec61937")) {
274 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
275 GST_DEBUG_OBJECT (ac3parse, "picked iec61937 alignment");
276 } else if (g_str_equal (str, "frame") == 0) {
277 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
278 GST_DEBUG_OBJECT (ac3parse, "picked frame alignment");
280 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
281 GST_WARNING_OBJECT (ac3parse, "unknown alignment: %s", str);
288 gst_caps_unref (caps);
292 if (ac3parse->align == GST_AC3_PARSE_ALIGN_NONE) {
293 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
294 GST_DEBUG_OBJECT (ac3parse, "picked syncframe alignment");
299 gst_ac3_parse_frame_header_ac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
300 gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
303 GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
304 guint8 fscod, frmsizcod, bsid, acmod, lfe_on, rate_scale;
306 GST_LOG_OBJECT (ac3parse, "parsing ac3");
308 gst_bit_reader_skip_unchecked (&bits, skip * 8);
310 gst_bit_reader_skip_unchecked (&bits, 16 + 16);
311 fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);
312 frmsizcod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 6);
314 if (G_UNLIKELY (fscod == 3 || frmsizcod >= G_N_ELEMENTS (frmsizcod_table))) {
315 GST_DEBUG_OBJECT (ac3parse, "bad fscod=%d frmsizcod=%d", fscod, frmsizcod);
319 bsid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 5);
320 gst_bit_reader_skip_unchecked (&bits, 3); /* bsmod */
321 acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);
323 /* spec not quite clear here: decoder should decode if less than 8,
324 * but seemingly only defines 6 and 8 cases */
326 GST_DEBUG_OBJECT (ac3parse, "unexpected bsid=%d", bsid);
328 } else if (bsid != 8 && bsid != 6) {
329 GST_DEBUG_OBJECT (ac3parse, "undefined bsid=%d", bsid);
332 if ((acmod & 0x1) && (acmod != 0x1)) /* 3 front channels */
333 gst_bit_reader_skip_unchecked (&bits, 2);
334 if ((acmod & 0x4)) /* if a surround channel exists */
335 gst_bit_reader_skip_unchecked (&bits, 2);
336 if (acmod == 0x2) /* if in 2/0 mode */
337 gst_bit_reader_skip_unchecked (&bits, 2);
339 lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);
341 /* 6/8->0, 9->1, 10->2,
342 see http://matroska.org/technical/specs/codecid/index.html */
343 rate_scale = (CLAMP (bsid, 8, 10) - 8);
346 *frame_size = frmsizcod_table[frmsizcod].frame_size[fscod] * 2;
348 *rate = fscod_rates[fscod] >> rate_scale;
350 *chans = acmod_chans[acmod] + lfe_on;
360 gst_ac3_parse_frame_header_eac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
361 gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
364 GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
365 guint16 frmsiz, sample_rate, blocks;
366 guint8 strmtyp, fscod, fscod2, acmod, lfe_on, strmid, numblkscod;
368 GST_LOG_OBJECT (ac3parse, "parsing e-ac3");
370 gst_bit_reader_skip_unchecked (&bits, skip * 8);
372 gst_bit_reader_skip_unchecked (&bits, 16);
373 strmtyp = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* strmtyp */
374 if (G_UNLIKELY (strmtyp == 3)) {
375 GST_DEBUG_OBJECT (ac3parse, "bad strmtyp %d", strmtyp);
379 strmid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* substreamid */
380 frmsiz = gst_bit_reader_get_bits_uint16_unchecked (&bits, 11); /* frmsiz */
381 fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod */
383 fscod2 = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod2 */
384 if (G_UNLIKELY (fscod2 == 3)) {
385 GST_DEBUG_OBJECT (ac3parse, "invalid fscod2");
388 sample_rate = fscod_rates[fscod2] / 2;
391 numblkscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* numblkscod */
392 sample_rate = fscod_rates[fscod];
393 blocks = numblks[numblkscod];
396 acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* acmod */
397 lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1); /* lfeon */
399 gst_bit_reader_skip_unchecked (&bits, 5); /* bsid */
402 *frame_size = (frmsiz + 1) * 2;
406 *chans = acmod_chans[acmod] + lfe_on;
410 *sid = (strmtyp & 0x1) << 3 | strmid;
416 gst_ac3_parse_frame_header (GstAc3Parse * parse, GstBuffer * buf, gint skip,
417 guint * framesize, guint * rate, guint * chans, guint * blocks,
418 guint * sid, gboolean * eac)
420 GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
424 GST_MEMDUMP_OBJECT (parse, "AC3 frame sync", GST_BUFFER_DATA (buf), 16);
426 gst_bit_reader_skip_unchecked (&bits, skip * 8);
428 sync = gst_bit_reader_get_bits_uint16_unchecked (&bits, 16);
429 gst_bit_reader_skip_unchecked (&bits, 16 + 8);
430 bsid = gst_bit_reader_peek_bits_uint8_unchecked (&bits, 5);
432 if (G_UNLIKELY (sync != 0x0b77))
435 GST_LOG_OBJECT (parse, "bsid = %d", bsid);
440 return gst_ac3_parse_frame_header_ac3 (parse, buf, skip, framesize, rate,
442 } else if (bsid <= 16) {
445 return gst_ac3_parse_frame_header_eac3 (parse, buf, skip, framesize, rate,
448 GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
454 gst_ac3_parse_check_valid_frame (GstBaseParse * parse,
455 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
457 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
458 GstBuffer *buf = frame->buffer;
459 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buf);
461 gboolean lost_sync, draining, eac, more = FALSE;
462 guint frmsiz, blocks, sid;
463 gint have_blocks = 0;
465 if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < 6))
468 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000, 0x0b770000,
469 0, GST_BUFFER_SIZE (buf));
471 GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
473 /* didn't find anything that looks like a sync word, skip */
475 *skipsize = GST_BUFFER_SIZE (buf) - 3;
479 /* possible frame header, but not at offset 0? skip bytes before sync */
485 /* make sure the values in the frame header look sane */
486 if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &frmsiz, NULL, NULL,
487 &blocks, &sid, &eac)) {
494 if (G_UNLIKELY (g_atomic_int_get (&ac3parse->align) ==
495 GST_AC3_PARSE_ALIGN_NONE))
496 gst_ac3_parse_set_alignment (ac3parse, eac);
498 GST_LOG_OBJECT (parse, "got frame");
500 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
501 draining = GST_BASE_PARSE_DRAINING (parse);
503 if (g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937) {
504 /* We need 6 audio blocks from each substream, so we keep going forwards
507 g_assert (blocks > 0);
508 GST_LOG_OBJECT (ac3parse, "Need %d frames before pushing", 6 / blocks);
511 /* We need the first substream to be the one with id 0 */
512 GST_LOG_OBJECT (ac3parse, "Skipping till we find sid 0");
519 /* Loop till we have 6 blocks per substream */
520 for (have_blocks = 0; !more && have_blocks < 6; have_blocks += blocks) {
521 /* Loop till we get one frame from each substream */
523 *framesize += frmsiz;
525 if (!gst_byte_reader_skip (&reader, frmsiz) ||
526 GST_BUFFER_SIZE (buf) < (*framesize + 6)) {
531 if (!gst_ac3_parse_frame_header (ac3parse, buf, *framesize, &frmsiz,
532 NULL, NULL, NULL, &sid, &eac)) {
539 /* We're now at the next frame, so no need to skip if resyncing */
543 if (lost_sync && !draining) {
546 GST_DEBUG_OBJECT (ac3parse, "resyncing; checking next frame syncword");
548 if (more || !gst_byte_reader_skip (&reader, frmsiz) ||
549 !gst_byte_reader_get_uint16_be (&reader, &word)) {
550 GST_DEBUG_OBJECT (ac3parse, "... but not sufficient data");
551 gst_base_parse_set_min_frame_size (parse, *framesize + 6);
555 if (word != 0x0b77) {
556 GST_DEBUG_OBJECT (ac3parse, "0x%x not OK", word);
560 /* ok, got sync now, let's assume constant frame size */
561 gst_base_parse_set_min_frame_size (parse, *framesize);
570 gst_ac3_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
572 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
573 GstBuffer *buf = frame->buffer;
574 guint fsize, rate, chans, blocks, sid;
575 gboolean eac, update_rate = FALSE;
577 if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &fsize, &rate, &chans,
578 &blocks, &sid, &eac))
581 GST_LOG_OBJECT (parse, "size: %u, blocks: %u, rate: %u, chans: %u", fsize,
582 blocks, rate, chans);
584 if (G_UNLIKELY (sid)) {
585 /* dependent frame, no need to (ac)count for or consider further */
586 GST_LOG_OBJECT (parse, "sid: %d", sid);
587 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME;
588 /* TODO maybe also mark as DELTA_UNIT,
589 * if that does not surprise baseparse elsewhere */
590 /* occupies same time space as previous base frame */
591 if (G_LIKELY (GST_BUFFER_TIMESTAMP (buf) >= GST_BUFFER_DURATION (buf)))
592 GST_BUFFER_TIMESTAMP (buf) -= GST_BUFFER_DURATION (buf);
593 /* only return if we already arranged for caps */
594 if (G_LIKELY (ac3parse->sample_rate > 0))
598 if (G_UNLIKELY (ac3parse->sample_rate != rate || ac3parse->channels != chans
599 || ac3parse->eac != eac)) {
600 GstCaps *caps = gst_caps_new_simple (eac ? "audio/x-eac3" : "audio/x-ac3",
601 "framed", G_TYPE_BOOLEAN, TRUE, "rate", G_TYPE_INT, rate,
602 "channels", G_TYPE_INT, chans, NULL);
603 gst_caps_set_simple (caps, "alignment", G_TYPE_STRING,
604 g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937 ?
605 "iec61937" : "frame", NULL);
606 gst_buffer_set_caps (buf, caps);
607 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
608 gst_caps_unref (caps);
610 ac3parse->sample_rate = rate;
611 ac3parse->channels = chans;
617 if (G_UNLIKELY (ac3parse->blocks != blocks)) {
618 ac3parse->blocks = blocks;
623 if (G_UNLIKELY (update_rate))
624 gst_base_parse_set_frame_rate (parse, rate, 256 * blocks, 2, 2);
631 /* this really shouldn't ever happen */
632 GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
633 return GST_FLOW_ERROR;
638 gst_ac3_parse_src_event (GstBaseParse * parse, GstEvent * event)
640 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
642 if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) &&
643 gst_event_has_name (event, "ac3parse-set-alignment")) {
644 const GstStructure *st = gst_event_get_structure (event);
645 const gchar *align = gst_structure_get_string (st, "alignment");
647 if (g_str_equal (align, "iec61937")) {
648 GST_DEBUG_OBJECT (ac3parse, "Switching to iec61937 alignment");
649 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
650 } else if (g_str_equal (align, "frame")) {
651 GST_DEBUG_OBJECT (ac3parse, "Switching to frame alignment");
652 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
654 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
655 GST_WARNING_OBJECT (ac3parse, "Got unknown alignment request (%s) "
656 "reverting to frame alignment.",
657 gst_structure_get_string (st, "alignment"));
660 gst_event_unref (event);
664 return GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
668 gst_ac3_parse_get_sink_caps (GstBaseParse * parse)
673 peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
677 /* Remove the framed and alignment field. We can convert
678 * between different alignments. */
679 peercaps = gst_caps_make_writable (peercaps);
680 n = gst_caps_get_size (peercaps);
681 for (i = 0; i < n; i++) {
682 GstStructure *s = gst_caps_get_structure (peercaps, i);
684 gst_structure_remove_field (s, "framed");
685 gst_structure_remove_field (s, "alignment");
689 gst_caps_intersect_full (peercaps,
690 gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)),
691 GST_CAPS_INTERSECT_FIRST);
692 gst_caps_unref (peercaps);
695 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD