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) [ 32000, 48000 ]; "
148 "audio/x-eac3, framed = (boolean) true, "
149 " channels = (int) [ 1, 6 ], rate = (int) [ 32000, 48000 ] "));
151 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
154 GST_STATIC_CAPS ("audio/x-ac3, framed = (boolean) false; "
155 "audio/x-eac3, framed = (boolean) false; "
156 "audio/ac3, framed = (boolean) false "));
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);
167 GST_BOILERPLATE (GstAc3Parse, gst_ac3_parse, GstBaseParse, GST_TYPE_BASE_PARSE);
170 gst_ac3_parse_base_init (gpointer klass)
172 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
174 gst_element_class_add_pad_template (element_class,
175 gst_static_pad_template_get (&sink_template));
176 gst_element_class_add_pad_template (element_class,
177 gst_static_pad_template_get (&src_template));
179 gst_element_class_set_details_simple (element_class,
180 "AC3 audio stream parser", "Codec/Parser/Audio",
181 "AC3 parser", "Tim-Philipp Müller <tim centricular net>");
185 gst_ac3_parse_class_init (GstAc3ParseClass * klass)
187 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
188 GObjectClass *object_class = G_OBJECT_CLASS (klass);
190 GST_DEBUG_CATEGORY_INIT (ac3_parse_debug, "ac3parse", 0,
191 "AC3 audio stream parser");
193 object_class->finalize = gst_ac3_parse_finalize;
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);
203 gst_ac3_parse_reset (GstAc3Parse * ac3parse)
205 ac3parse->channels = -1;
206 ac3parse->sample_rate = -1;
207 ac3parse->eac = FALSE;
211 gst_ac3_parse_init (GstAc3Parse * ac3parse, GstAc3ParseClass * klass)
213 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (ac3parse), 64 * 2);
214 gst_ac3_parse_reset (ac3parse);
218 gst_ac3_parse_finalize (GObject * object)
220 G_OBJECT_CLASS (parent_class)->finalize (object);
224 gst_ac3_parse_start (GstBaseParse * parse)
226 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
228 GST_DEBUG_OBJECT (parse, "starting");
230 gst_ac3_parse_reset (ac3parse);
236 gst_ac3_parse_stop (GstBaseParse * parse)
238 GST_DEBUG_OBJECT (parse, "stopping");
244 gst_ac3_parse_frame_header_ac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
245 guint * frame_size, guint * rate, guint * chans, guint * blks, guint * sid)
247 GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
248 guint8 fscod, frmsizcod, bsid, acmod, lfe_on;
250 GST_LOG_OBJECT (ac3parse, "parsing ac3");
252 gst_bit_reader_skip_unchecked (&bits, 16 + 16);
253 fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);
254 frmsizcod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 6);
256 if (G_UNLIKELY (fscod == 3 || frmsizcod >= G_N_ELEMENTS (frmsizcod_table))) {
257 GST_DEBUG_OBJECT (ac3parse, "bad fscod=%d frmsizcod=%d", fscod, frmsizcod);
261 bsid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 5);
262 gst_bit_reader_skip_unchecked (&bits, 3); /* bsmod */
263 acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);
265 /* spec not quite clear here: decoder should decode if less than 8,
266 * but seemingly only defines 6 and 8 cases */
268 GST_DEBUG_OBJECT (ac3parse, "unexpected bsid=%d", bsid);
270 } else if (bsid != 8 && bsid != 6) {
271 GST_DEBUG_OBJECT (ac3parse, "undefined bsid=%d", bsid);
274 if ((acmod & 0x1) && (acmod != 0x1)) /* 3 front channels */
275 gst_bit_reader_skip_unchecked (&bits, 2);
276 if ((acmod & 0x4)) /* if a surround channel exists */
277 gst_bit_reader_skip_unchecked (&bits, 2);
278 if (acmod == 0x2) /* if in 2/0 mode */
279 gst_bit_reader_skip_unchecked (&bits, 2);
281 lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);
284 *frame_size = frmsizcod_table[frmsizcod].frame_size[fscod] * 2;
286 *rate = fscod_rates[fscod];
288 *chans = acmod_chans[acmod] + lfe_on;
298 gst_ac3_parse_frame_header_eac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
299 guint * frame_size, guint * rate, guint * chans, guint * blks, guint * sid)
301 GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
302 guint16 frmsiz, sample_rate, blocks;
303 guint8 strmtyp, fscod, fscod2, acmod, lfe_on, strmid, numblkscod;
305 GST_LOG_OBJECT (ac3parse, "parsing e-ac3");
307 gst_bit_reader_skip_unchecked (&bits, 16);
308 strmtyp = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* strmtyp */
309 if (G_UNLIKELY (strmtyp == 3)) {
310 GST_DEBUG_OBJECT (ac3parse, "bad strmtyp %d", strmtyp);
314 strmid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* substreamid */
315 frmsiz = gst_bit_reader_get_bits_uint16_unchecked (&bits, 11); /* frmsiz */
316 fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod */
318 fscod2 = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod2 */
319 if (G_UNLIKELY (fscod2 == 3)) {
320 GST_DEBUG_OBJECT (ac3parse, "invalid fscod2");
323 sample_rate = fscod_rates[fscod2] / 2;
326 numblkscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* numblkscod */
327 sample_rate = fscod_rates[fscod];
328 blocks = numblks[numblkscod];
331 acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* acmod */
332 lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1); /* lfeon */
334 gst_bit_reader_skip_unchecked (&bits, 5); /* bsid */
337 *frame_size = (frmsiz + 1) * 2;
341 *chans = acmod_chans[acmod] + lfe_on;
345 *sid = (strmtyp & 0x1) << 3 | strmid;
351 gst_ac3_parse_frame_header (GstAc3Parse * parse, GstBuffer * buf,
352 guint * framesize, guint * rate, guint * chans, guint * blocks,
353 guint * sid, gboolean * eac)
355 GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
359 GST_MEMDUMP_OBJECT (parse, "AC3 frame sync", GST_BUFFER_DATA (buf), 16);
361 sync = gst_bit_reader_get_bits_uint16_unchecked (&bits, 16);
362 gst_bit_reader_skip_unchecked (&bits, 16 + 8);
363 bsid = gst_bit_reader_peek_bits_uint8_unchecked (&bits, 5);
365 if (G_UNLIKELY (sync != 0x0b77))
368 GST_LOG_OBJECT (parse, "bsid = %d", bsid);
373 return gst_ac3_parse_frame_header_ac3 (parse, buf, framesize, rate, chans,
375 } else if (bsid <= 16) {
378 return gst_ac3_parse_frame_header_eac3 (parse, buf, framesize, rate, chans,
381 GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
387 gst_ac3_parse_check_valid_frame (GstBaseParse * parse,
388 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
390 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
391 GstBuffer *buf = frame->buffer;
392 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buf);
394 gboolean lost_sync, draining;
396 if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < 6))
399 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000, 0x0b770000,
400 0, GST_BUFFER_SIZE (buf));
402 GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
404 /* didn't find anything that looks like a sync word, skip */
406 *skipsize = GST_BUFFER_SIZE (buf) - 3;
410 /* possible frame header, but not at offset 0? skip bytes before sync */
416 /* make sure the values in the frame header look sane */
417 if (!gst_ac3_parse_frame_header (ac3parse, buf, framesize, NULL, NULL,
423 GST_LOG_OBJECT (parse, "got frame");
425 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
426 draining = GST_BASE_PARSE_DRAINING (parse);
428 if (lost_sync && !draining) {
431 GST_DEBUG_OBJECT (ac3parse, "resyncing; checking next frame syncword");
433 if (!gst_byte_reader_skip (&reader, *framesize) ||
434 !gst_byte_reader_get_uint16_be (&reader, &word)) {
435 GST_DEBUG_OBJECT (ac3parse, "... but not sufficient data");
436 gst_base_parse_set_min_frame_size (parse, *framesize + 6);
440 if (word != 0x0b77) {
441 GST_DEBUG_OBJECT (ac3parse, "0x%x not OK", word);
445 /* ok, got sync now, let's assume constant frame size */
446 gst_base_parse_set_min_frame_size (parse, *framesize);
455 gst_ac3_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
457 GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
458 GstBuffer *buf = frame->buffer;
459 guint fsize, rate, chans, blocks, sid;
462 if (!gst_ac3_parse_frame_header (ac3parse, buf, &fsize, &rate, &chans,
463 &blocks, &sid, &eac))
466 GST_LOG_OBJECT (parse, "size: %u, rate: %u, chans: %u", fsize, rate, chans);
468 if (G_UNLIKELY (sid)) {
469 /* dependent frame, no need to (ac)count for or consider further */
470 GST_LOG_OBJECT (parse, "sid: %d", sid);
471 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME;
472 /* TODO maybe also mark as DELTA_UNIT,
473 * if that does not surprise baseparse elsewhere */
474 /* occupies same time space as previous base frame */
475 if (G_LIKELY (GST_BUFFER_TIMESTAMP (buf) >= GST_BUFFER_DURATION (buf)))
476 GST_BUFFER_TIMESTAMP (buf) -= GST_BUFFER_DURATION (buf);
477 /* only return if we already arranged for caps */
478 if (G_LIKELY (ac3parse->sample_rate > 0))
482 if (G_UNLIKELY (ac3parse->sample_rate != rate || ac3parse->channels != chans
483 || ac3parse->eac != ac3parse->eac)) {
484 GstCaps *caps = gst_caps_new_simple (eac ? "audio/x-eac3" : "audio/x-ac3",
485 "framed", G_TYPE_BOOLEAN, TRUE, "rate", G_TYPE_INT, rate,
486 "channels", G_TYPE_INT, chans, NULL);
487 gst_buffer_set_caps (buf, caps);
488 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
489 gst_caps_unref (caps);
491 ac3parse->sample_rate = rate;
492 ac3parse->channels = chans;
495 gst_base_parse_set_frame_rate (parse, rate, 256 * blocks, 2, 2);
503 /* this really shouldn't ever happen */
504 GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
505 return GST_FLOW_ERROR;