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,
171 #define gst_ac3_parse_parent_class parent_class
172 G_DEFINE_TYPE (GstAc3Parse, gst_ac3_parse, GST_TYPE_BASE_PARSE);
175 gst_ac3_parse_class_init (GstAc3ParseClass * klass)
177 GObjectClass *object_class = G_OBJECT_CLASS (klass);
178 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
179 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
181 GST_DEBUG_CATEGORY_INIT (ac3_parse_debug, "ac3parse", 0,
182 "AC3 audio stream parser");
184 object_class->finalize = gst_ac3_parse_finalize;
186 gst_element_class_add_pad_template (element_class,
187 gst_static_pad_template_get (&sink_template));
188 gst_element_class_add_pad_template (element_class,
189 gst_static_pad_template_get (&src_template));
191 gst_element_class_set_details_simple (element_class,
192 "AC3 audio stream parser", "Codec/Parser/Converter/Audio",
193 "AC3 parser", "Tim-Philipp Müller <tim centricular net>");
195 parse_class->start = GST_DEBUG_FUNCPTR (gst_ac3_parse_start);
196 parse_class->stop = GST_DEBUG_FUNCPTR (gst_ac3_parse_stop);
197 parse_class->check_valid_frame =
198 GST_DEBUG_FUNCPTR (gst_ac3_parse_check_valid_frame);
199 parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_ac3_parse_parse_frame);
200 parse_class->src_event = GST_DEBUG_FUNCPTR (gst_ac3_parse_src_event);
201 parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_ac3_parse_get_sink_caps);
205 gst_ac3_parse_reset (GstAc3Parse * ac3parse)
207 ac3parse->channels = -1;
208 ac3parse->sample_rate = -1;
209 ac3parse->blocks = -1;
210 ac3parse->eac = FALSE;
211 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_NONE);
215 gst_ac3_parse_init (GstAc3Parse * ac3parse)
217 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (ac3parse), 6);
218 gst_ac3_parse_reset (ac3parse);
222 gst_ac3_parse_finalize (GObject * object)
224 G_OBJECT_CLASS (parent_class)->finalize (object);
228 gst_ac3_parse_start (GstBaseParse * parse)
230 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
232 GST_DEBUG_OBJECT (parse, "starting");
234 gst_ac3_parse_reset (ac3parse);
240 gst_ac3_parse_stop (GstBaseParse * parse)
242 GST_DEBUG_OBJECT (parse, "stopping");
248 gst_ac3_parse_set_alignment (GstAc3Parse * ac3parse, gboolean eac)
252 const gchar *str = NULL;
258 caps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (ac3parse));
263 for (i = 0; i < gst_caps_get_size (caps); i++) {
264 st = gst_caps_get_structure (caps, i);
266 if (!g_str_equal (gst_structure_get_name (st), "audio/x-eac3"))
269 if ((str = gst_structure_get_string (st, "alignment"))) {
270 if (g_str_equal (str, "iec61937")) {
271 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
272 GST_DEBUG_OBJECT (ac3parse, "picked iec61937 alignment");
273 } else if (g_str_equal (str, "frame") == 0) {
274 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
275 GST_DEBUG_OBJECT (ac3parse, "picked frame alignment");
277 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
278 GST_WARNING_OBJECT (ac3parse, "unknown alignment: %s", str);
285 gst_caps_unref (caps);
289 if (ac3parse->align == GST_AC3_PARSE_ALIGN_NONE) {
290 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
291 GST_DEBUG_OBJECT (ac3parse, "picked syncframe alignment");
296 gst_ac3_parse_frame_header_ac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
297 gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
302 guint8 fscod, frmsizcod, bsid, acmod, lfe_on, rate_scale;
303 gboolean ret = FALSE;
305 GST_LOG_OBJECT (ac3parse, "parsing ac3");
307 gst_buffer_map (buf, &map, GST_MAP_READ);
308 gst_bit_reader_init (&bits, map.data, map.size);
309 gst_bit_reader_skip_unchecked (&bits, skip * 8);
311 gst_bit_reader_skip_unchecked (&bits, 16 + 16);
312 fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);
313 frmsizcod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 6);
315 if (G_UNLIKELY (fscod == 3 || frmsizcod >= G_N_ELEMENTS (frmsizcod_table))) {
316 GST_DEBUG_OBJECT (ac3parse, "bad fscod=%d frmsizcod=%d", fscod, frmsizcod);
320 bsid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 5);
321 gst_bit_reader_skip_unchecked (&bits, 3); /* bsmod */
322 acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);
324 /* spec not quite clear here: decoder should decode if less than 8,
325 * but seemingly only defines 6 and 8 cases */
326 /* Files with 9 and 10 happen, and seem to comply with the <= 8
327 format, so let them through. The spec says nothing about 9 and 10 */
329 GST_DEBUG_OBJECT (ac3parse, "unexpected bsid=%d", bsid);
331 } else if (bsid != 8 && bsid != 6) {
332 GST_DEBUG_OBJECT (ac3parse, "undefined bsid=%d", bsid);
335 if ((acmod & 0x1) && (acmod != 0x1)) /* 3 front channels */
336 gst_bit_reader_skip_unchecked (&bits, 2);
337 if ((acmod & 0x4)) /* if a surround channel exists */
338 gst_bit_reader_skip_unchecked (&bits, 2);
339 if (acmod == 0x2) /* if in 2/0 mode */
340 gst_bit_reader_skip_unchecked (&bits, 2);
342 lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);
344 /* 6/8->0, 9->1, 10->2,
345 see http://matroska.org/technical/specs/codecid/index.html */
346 rate_scale = (CLAMP (bsid, 8, 10) - 8);
349 *frame_size = frmsizcod_table[frmsizcod].frame_size[fscod] * 2;
351 *rate = fscod_rates[fscod] >> rate_scale;
353 *chans = acmod_chans[acmod] + lfe_on;
362 gst_buffer_unmap (buf, &map);
368 gst_ac3_parse_frame_header_eac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
369 gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
374 guint16 frmsiz, sample_rate, blocks;
375 guint8 strmtyp, fscod, fscod2, acmod, lfe_on, strmid, numblkscod;
376 gboolean ret = FALSE;
378 GST_LOG_OBJECT (ac3parse, "parsing e-ac3");
380 gst_buffer_map (buf, &map, GST_MAP_READ);
381 gst_bit_reader_init (&bits, map.data, map.size);
382 gst_bit_reader_skip_unchecked (&bits, skip * 8);
384 gst_bit_reader_skip_unchecked (&bits, 16);
385 strmtyp = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* strmtyp */
386 if (G_UNLIKELY (strmtyp == 3)) {
387 GST_DEBUG_OBJECT (ac3parse, "bad strmtyp %d", strmtyp);
391 strmid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* substreamid */
392 frmsiz = gst_bit_reader_get_bits_uint16_unchecked (&bits, 11); /* frmsiz */
393 fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod */
395 fscod2 = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod2 */
396 if (G_UNLIKELY (fscod2 == 3)) {
397 GST_DEBUG_OBJECT (ac3parse, "invalid fscod2");
400 sample_rate = fscod_rates[fscod2] / 2;
403 numblkscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* numblkscod */
404 sample_rate = fscod_rates[fscod];
405 blocks = numblks[numblkscod];
408 acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* acmod */
409 lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1); /* lfeon */
411 gst_bit_reader_skip_unchecked (&bits, 5); /* bsid */
414 *frame_size = (frmsiz + 1) * 2;
418 *chans = acmod_chans[acmod] + lfe_on;
422 *sid = (strmtyp & 0x1) << 3 | strmid;
427 gst_buffer_unmap (buf, &map);
433 gst_ac3_parse_frame_header (GstAc3Parse * parse, GstBuffer * buf, gint skip,
434 guint * framesize, guint * rate, guint * chans, guint * blocks,
435 guint * sid, gboolean * eac)
441 gboolean ret = FALSE;
443 gst_buffer_map (buf, &map, GST_MAP_READ);
444 gst_bit_reader_init (&bits, map.data, map.size);
446 GST_MEMDUMP_OBJECT (parse, "AC3 frame sync", map.data, MIN (map.size, 16));
448 gst_bit_reader_skip_unchecked (&bits, skip * 8);
450 sync = gst_bit_reader_get_bits_uint16_unchecked (&bits, 16);
451 gst_bit_reader_skip_unchecked (&bits, 16 + 8);
452 bsid = gst_bit_reader_peek_bits_uint8_unchecked (&bits, 5);
454 if (G_UNLIKELY (sync != 0x0b77))
457 GST_LOG_OBJECT (parse, "bsid = %d", bsid);
462 ret = gst_ac3_parse_frame_header_ac3 (parse, buf, skip, framesize, rate,
465 } else if (bsid <= 16) {
468 ret = gst_ac3_parse_frame_header_eac3 (parse, buf, skip, framesize, rate,
472 GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
476 GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
479 gst_buffer_unmap (buf, &map);
485 gst_ac3_parse_check_valid_frame (GstBaseParse * parse,
486 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
488 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
489 GstBuffer *buf = frame->buffer;
490 GstByteReader reader;
492 gboolean lost_sync, draining, eac, more = FALSE;
493 guint frmsiz, blocks, sid;
494 gint have_blocks = 0;
496 gboolean ret = FALSE;
498 gst_buffer_map (buf, &map, GST_MAP_READ);
500 if (G_UNLIKELY (map.size < 6))
503 gst_byte_reader_init (&reader, map.data, map.size);
504 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000, 0x0b770000,
507 GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
509 /* didn't find anything that looks like a sync word, skip */
511 *skipsize = map.size - 3;
515 /* possible frame header, but not at offset 0? skip bytes before sync */
521 /* make sure the values in the frame header look sane */
522 if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &frmsiz, NULL, NULL,
523 &blocks, &sid, &eac)) {
530 if (G_UNLIKELY (g_atomic_int_get (&ac3parse->align) ==
531 GST_AC3_PARSE_ALIGN_NONE))
532 gst_ac3_parse_set_alignment (ac3parse, eac);
534 GST_LOG_OBJECT (parse, "got frame");
536 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
537 draining = GST_BASE_PARSE_DRAINING (parse);
539 if (g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937) {
540 /* We need 6 audio blocks from each substream, so we keep going forwards
543 g_assert (blocks > 0);
544 GST_LOG_OBJECT (ac3parse, "Need %d frames before pushing", 6 / blocks);
547 /* We need the first substream to be the one with id 0 */
548 GST_LOG_OBJECT (ac3parse, "Skipping till we find sid 0");
555 /* Loop till we have 6 blocks per substream */
556 for (have_blocks = 0; !more && have_blocks < 6; have_blocks += blocks) {
557 /* Loop till we get one frame from each substream */
559 *framesize += frmsiz;
561 if (!gst_byte_reader_skip (&reader, frmsiz)
562 || map.size < (*framesize + 6)) {
567 if (!gst_ac3_parse_frame_header (ac3parse, buf, *framesize, &frmsiz,
568 NULL, NULL, NULL, &sid, &eac)) {
575 /* We're now at the next frame, so no need to skip if resyncing */
579 if (lost_sync && !draining) {
582 GST_DEBUG_OBJECT (ac3parse, "resyncing; checking next frame syncword");
584 if (more || !gst_byte_reader_skip (&reader, frmsiz) ||
585 !gst_byte_reader_get_uint16_be (&reader, &word)) {
586 GST_DEBUG_OBJECT (ac3parse, "... but not sufficient data");
587 gst_base_parse_set_min_frame_size (parse, *framesize + 6);
591 if (word != 0x0b77) {
592 GST_DEBUG_OBJECT (ac3parse, "0x%x not OK", word);
596 /* ok, got sync now, let's assume constant frame size */
597 gst_base_parse_set_min_frame_size (parse, *framesize);
605 gst_buffer_unmap (buf, &map);
611 gst_ac3_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
613 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
614 GstBuffer *buf = frame->buffer;
615 guint fsize, rate, chans, blocks, sid;
616 gboolean eac, update_rate = FALSE;
618 if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &fsize, &rate, &chans,
619 &blocks, &sid, &eac))
622 GST_LOG_OBJECT (parse, "size: %u, blocks: %u, rate: %u, chans: %u", fsize,
623 blocks, rate, chans);
625 if (G_UNLIKELY (sid)) {
626 /* dependent frame, no need to (ac)count for or consider further */
627 GST_LOG_OBJECT (parse, "sid: %d", sid);
628 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME;
629 /* TODO maybe also mark as DELTA_UNIT,
630 * if that does not surprise baseparse elsewhere */
631 /* occupies same time space as previous base frame */
632 if (G_LIKELY (GST_BUFFER_TIMESTAMP (buf) >= GST_BUFFER_DURATION (buf)))
633 GST_BUFFER_TIMESTAMP (buf) -= GST_BUFFER_DURATION (buf);
634 /* only return if we already arranged for caps */
635 if (G_LIKELY (ac3parse->sample_rate > 0))
639 if (G_UNLIKELY (ac3parse->sample_rate != rate || ac3parse->channels != chans
640 || ac3parse->eac != eac)) {
641 GstCaps *caps = gst_caps_new_simple (eac ? "audio/x-eac3" : "audio/x-ac3",
642 "framed", G_TYPE_BOOLEAN, TRUE, "rate", G_TYPE_INT, rate,
643 "channels", G_TYPE_INT, chans, NULL);
644 gst_caps_set_simple (caps, "alignment", G_TYPE_STRING,
645 g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937 ?
646 "iec61937" : "frame", NULL);
647 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
648 gst_caps_unref (caps);
650 ac3parse->sample_rate = rate;
651 ac3parse->channels = chans;
657 if (G_UNLIKELY (ac3parse->blocks != blocks)) {
658 ac3parse->blocks = blocks;
663 if (G_UNLIKELY (update_rate))
664 gst_base_parse_set_frame_rate (parse, rate, 256 * blocks, 2, 2);
671 /* this really shouldn't ever happen */
672 GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
673 return GST_FLOW_ERROR;
678 gst_ac3_parse_src_event (GstBaseParse * parse, GstEvent * event)
680 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
682 if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) &&
683 gst_event_has_name (event, "ac3parse-set-alignment")) {
684 const GstStructure *st = gst_event_get_structure (event);
685 const gchar *align = gst_structure_get_string (st, "alignment");
687 if (g_str_equal (align, "iec61937")) {
688 GST_DEBUG_OBJECT (ac3parse, "Switching to iec61937 alignment");
689 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
690 } else if (g_str_equal (align, "frame")) {
691 GST_DEBUG_OBJECT (ac3parse, "Switching to frame alignment");
692 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
694 g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
695 GST_WARNING_OBJECT (ac3parse, "Got unknown alignment request (%s) "
696 "reverting to frame alignment.",
697 gst_structure_get_string (st, "alignment"));
700 gst_event_unref (event);
704 return GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
708 gst_ac3_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
713 /* FIXME: handle filter */
715 peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
719 /* Remove the framed and alignment field. We can convert
720 * between different alignments. */
721 peercaps = gst_caps_make_writable (peercaps);
722 n = gst_caps_get_size (peercaps);
723 for (i = 0; i < n; i++) {
724 GstStructure *s = gst_caps_get_structure (peercaps, i);
726 gst_structure_remove_field (s, "framed");
727 gst_structure_remove_field (s, "alignment");
731 gst_caps_intersect_full (peercaps,
732 gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)),
733 GST_CAPS_INTERSECT_FIRST);
734 gst_caps_unref (peercaps);
737 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD