1 /* GStreamer DCA parser
2 * Copyright (C) 2010 Tim-Philipp Müller <tim centricular net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:element-dcaparse
22 * @short_description: DCA (DTS Coherent Acoustics) parser
23 * @see_also: #GstAmrParse, #GstAACParse, #GstAc3Parse
25 * This is a DCA (DTS Coherent Acoustics) parser.
28 * <title>Example launch line</title>
30 * gst-launch filesrc location=abc.dts ! dcaparse ! dtsdec ! audioresample ! audioconvert ! autoaudiosink
36 * - should accept framed and unframed input (needs decodebin fixes first)
37 * - seeking in raw .dts files doesn't seem to work, but duration estimate ok
39 * - if frames have 'odd' durations, the frame durations (plus timestamps)
40 * aren't adjusted up occasionally to make up for rounding error gaps.
41 * (e.g. if 512 samples per frame @ 48kHz = 10.666666667 ms/frame)
50 #include "gstdcaparse.h"
51 #include <gst/base/gstbytereader.h>
52 #include <gst/base/gstbitreader.h>
54 GST_DEBUG_CATEGORY_STATIC (dca_parse_debug);
55 #define GST_CAT_DEFAULT dca_parse_debug
57 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
60 GST_STATIC_CAPS ("audio/x-dts,"
61 " framed = (boolean) true,"
62 " channels = (int) [ 1, 8 ],"
63 " rate = (int) [ 8000, 192000 ],"
64 " depth = (int) { 14, 16 },"
65 " endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, "
66 " block-size = (int) [ 1, MAX], " " frame-size = (int) [ 1, MAX]"));
68 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
71 GST_STATIC_CAPS ("audio/x-dts"));
73 static void gst_dca_parse_finalize (GObject * object);
75 static gboolean gst_dca_parse_start (GstBaseParse * parse);
76 static gboolean gst_dca_parse_stop (GstBaseParse * parse);
77 static gboolean gst_dca_parse_check_valid_frame (GstBaseParse * parse,
78 GstBaseParseFrame * frame, guint * size, gint * skipsize);
79 static GstFlowReturn gst_dca_parse_parse_frame (GstBaseParse * parse,
80 GstBaseParseFrame * frame);
82 #define gst_dca_parse_parent_class parent_class
83 G_DEFINE_TYPE (GstDcaParse, gst_dca_parse, GST_TYPE_BASE_PARSE);
86 gst_dca_parse_class_init (GstDcaParseClass * klass)
88 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
89 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
90 GObjectClass *object_class = G_OBJECT_CLASS (klass);
92 GST_DEBUG_CATEGORY_INIT (dca_parse_debug, "dcaparse", 0,
93 "DCA audio stream parser");
95 object_class->finalize = gst_dca_parse_finalize;
97 parse_class->start = GST_DEBUG_FUNCPTR (gst_dca_parse_start);
98 parse_class->stop = GST_DEBUG_FUNCPTR (gst_dca_parse_stop);
99 parse_class->check_valid_frame =
100 GST_DEBUG_FUNCPTR (gst_dca_parse_check_valid_frame);
101 parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_dca_parse_parse_frame);
103 gst_element_class_add_pad_template (element_class,
104 gst_static_pad_template_get (&sink_template));
105 gst_element_class_add_pad_template (element_class,
106 gst_static_pad_template_get (&src_template));
108 gst_element_class_set_details_simple (element_class,
109 "DTS Coherent Acoustics audio stream parser", "Codec/Parser/Audio",
110 "DCA parser", "Tim-Philipp Müller <tim centricular net>");
114 gst_dca_parse_reset (GstDcaParse * dcaparse)
116 dcaparse->channels = -1;
118 dcaparse->depth = -1;
119 dcaparse->endianness = -1;
120 dcaparse->block_size = -1;
121 dcaparse->frame_size = -1;
122 dcaparse->last_sync = 0;
126 gst_dca_parse_init (GstDcaParse * dcaparse)
128 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (dcaparse),
130 gst_dca_parse_reset (dcaparse);
134 gst_dca_parse_finalize (GObject * object)
136 G_OBJECT_CLASS (parent_class)->finalize (object);
140 gst_dca_parse_start (GstBaseParse * parse)
142 GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
144 GST_DEBUG_OBJECT (parse, "starting");
146 gst_dca_parse_reset (dcaparse);
152 gst_dca_parse_stop (GstBaseParse * parse)
154 GST_DEBUG_OBJECT (parse, "stopping");
160 gst_dca_parse_parse_header (GstDcaParse * dcaparse,
161 const GstByteReader * reader, guint * frame_size,
162 guint * sample_rate, guint * channels, guint * depth,
163 gint * endianness, guint * num_blocks, guint * samples_per_block,
164 gboolean * terminator)
166 static const int sample_rates[16] = { 0, 8000, 16000, 32000, 0, 0, 11025,
167 22050, 44100, 0, 0, 12000, 24000, 48000, 96000, 192000
169 static const guint8 channels_table[16] = { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5,
172 GstByteReader r = *reader;
177 if (gst_byte_reader_get_remaining (&r) < (4 + sizeof (hdr)))
180 marker = gst_byte_reader_peek_uint32_be_unchecked (&r);
182 /* raw big endian or 14-bit big endian */
183 if (marker == 0x7FFE8001 || marker == 0x1FFFE800) {
184 for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
185 hdr[i] = gst_byte_reader_get_uint16_be_unchecked (&r);
187 /* raw little endian or 14-bit little endian */
188 if (marker == 0xFE7F0180 || marker == 0xFF1F00E8) {
189 for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
190 hdr[i] = gst_byte_reader_get_uint16_le_unchecked (&r);
195 GST_LOG_OBJECT (dcaparse, "dts sync marker 0x%08x at offset %u", marker,
196 gst_byte_reader_get_pos (reader));
199 if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) {
200 if ((hdr[2] & 0xFFF0) != 0x07F0)
202 /* discard top 2 bits (2 void), shift in 2 */
203 hdr[0] = (hdr[0] << 2) | ((hdr[1] >> 12) & 0x0003);
204 /* discard top 4 bits (2 void, 2 shifted into hdr[0]), shift in 4 etc. */
205 hdr[1] = (hdr[1] << 4) | ((hdr[2] >> 10) & 0x000F);
206 hdr[2] = (hdr[2] << 6) | ((hdr[3] >> 8) & 0x003F);
207 hdr[3] = (hdr[3] << 8) | ((hdr[4] >> 6) & 0x00FF);
208 hdr[4] = (hdr[4] << 10) | ((hdr[5] >> 4) & 0x03FF);
209 hdr[5] = (hdr[5] << 12) | ((hdr[6] >> 2) & 0x0FFF);
210 hdr[6] = (hdr[6] << 14) | ((hdr[7] >> 0) & 0x3FFF);
211 g_assert (hdr[0] == 0x7FFE && hdr[1] == 0x8001);
214 GST_LOG_OBJECT (dcaparse, "frame header: %04x%04x%04x%04x",
215 hdr[2], hdr[3], hdr[4], hdr[5]);
217 *terminator = (hdr[2] & 0x80) ? FALSE : TRUE;
218 *samples_per_block = ((hdr[2] >> 10) & 0x1f) + 1;
219 *num_blocks = ((hdr[2] >> 2) & 0x7F) + 1;
220 *frame_size = (((hdr[2] & 0x03) << 12) | (hdr[3] >> 4)) + 1;
221 chans = ((hdr[3] & 0x0F) << 2) | (hdr[4] >> 14);
222 *sample_rate = sample_rates[(hdr[4] >> 10) & 0x0F];
223 lfe = (hdr[5] >> 9) & 0x03;
225 GST_TRACE_OBJECT (dcaparse, "frame size %u, num_blocks %u, rate %u, "
226 "samples per block %u", *frame_size, *num_blocks, *sample_rate,
229 if (*num_blocks < 6 || *frame_size < 96 || *sample_rate == 0)
232 if (marker == 0x1FFFE800 || marker == 0xFF1F00E8)
233 *frame_size = (*frame_size * 16) / 14; /* FIXME: round up? */
235 if (chans < G_N_ELEMENTS (channels_table))
236 *channels = channels_table[chans] + ((lfe) ? 1 : 0);
241 *depth = (marker == 0x1FFFE800 || marker == 0xFF1F00E8) ? 14 : 16;
243 *endianness = (marker == 0xFE7F0180 || marker == 0xFF1F00E8) ?
244 G_LITTLE_ENDIAN : G_BIG_ENDIAN;
246 GST_TRACE_OBJECT (dcaparse, "frame size %u, channels %u, rate %u, "
247 "num_blocks %u, samples_per_block %u", *frame_size, *channels,
248 *sample_rate, *num_blocks, *samples_per_block);
254 gst_dca_parse_find_sync (GstDcaParse * dcaparse, GstByteReader * reader,
255 gsize bufsize, guint32 * sync)
257 guint32 best_sync = 0;
258 guint best_offset = G_MAXUINT;
261 /* FIXME: verify syncs via _parse_header() here already */
263 /* Raw little endian */
264 off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0xfe7f0180,
266 if (off >= 0 && off < best_offset) {
268 best_sync = 0xfe7f0180;
272 off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x7ffe8001,
274 if (off >= 0 && off < best_offset) {
276 best_sync = 0x7ffe8001;
279 /* FIXME: check next 2 bytes as well for 14-bit formats (but then don't
280 * forget to adjust the *skipsize= in _check_valid_frame() */
282 /* 14-bit little endian */
283 off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0xff1f00e8,
285 if (off >= 0 && off < best_offset) {
287 best_sync = 0xff1f00e8;
290 /* 14-bit big endian */
291 off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x1fffe800,
293 if (off >= 0 && off < best_offset) {
295 best_sync = 0x1fffe800;
298 if (best_offset == G_MAXUINT)
306 gst_dca_parse_check_valid_frame (GstBaseParse * parse,
307 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
309 GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
310 GstBuffer *buf = frame->buffer;
312 gboolean parser_draining;
313 gboolean parser_in_sync;
316 guint size, rate, chans, num_blocks, samples_per_block;
320 gboolean ret = FALSE;
322 data = gst_buffer_map (buf, &bufsize, NULL, GST_MAP_READ);
324 if (G_UNLIKELY (bufsize < 16))
327 parser_in_sync = !GST_BASE_PARSE_LOST_SYNC (parse);
329 gst_byte_reader_init (&r, data, bufsize);
331 if (G_LIKELY (parser_in_sync && dcaparse->last_sync != 0)) {
332 off = gst_byte_reader_masked_scan_uint32 (&r, 0xffffffff,
333 dcaparse->last_sync, 0, size);
336 if (G_UNLIKELY (off < 0)) {
337 off = gst_dca_parse_find_sync (dcaparse, &r, bufsize, &sync);
340 /* didn't find anything that looks like a sync word, skip */
342 *skipsize = bufsize - 3;
343 GST_DEBUG_OBJECT (dcaparse, "no sync, skipping %d bytes", *skipsize);
347 GST_LOG_OBJECT (parse, "possible sync %08x at buffer offset %d", sync, off);
349 /* possible frame header, but not at offset 0? skip bytes before sync */
355 /* make sure the values in the frame header look sane */
356 if (!gst_dca_parse_parse_header (dcaparse, &r, &size, &rate, &chans, NULL,
357 NULL, &num_blocks, &samples_per_block, &terminator)) {
362 GST_LOG_OBJECT (parse, "got frame, sync %08x, size %u, rate %d, channels %d",
363 sync, size, rate, chans);
367 dcaparse->last_sync = sync;
369 parser_draining = GST_BASE_PARSE_DRAINING (parse);
371 if (!parser_in_sync && !parser_draining) {
372 /* check for second frame to be sure */
373 GST_DEBUG_OBJECT (dcaparse, "resyncing; checking next frame syncword");
374 if (bufsize >= (size + 16)) {
375 guint s2, r2, c2, n2, s3;
378 GST_MEMDUMP ("buf", data, size + 16);
379 gst_byte_reader_init (&r, data, bufsize);
380 gst_byte_reader_skip_unchecked (&r, size);
382 if (!gst_dca_parse_parse_header (dcaparse, &r, &s2, &r2, &c2, NULL, NULL,
384 GST_DEBUG_OBJECT (dcaparse, "didn't find second syncword");
389 /* ok, got sync now, let's assume constant frame size */
390 gst_base_parse_set_min_frame_size (parse, size);
392 /* FIXME: baseparse always seems to hand us buffers of min_frame_size
393 * bytes, which is unhelpful here */
394 GST_LOG_OBJECT (dcaparse,
395 "next sync out of reach (%" G_GSIZE_FORMAT " < %u)", bufsize,
405 gst_buffer_unmap (buf, data, bufsize);
410 gst_dca_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
412 GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
413 GstBuffer *buf = frame->buffer;
415 guint size, rate, chans, depth, block_size, num_blocks, samples_per_block;
421 data = gst_buffer_map (buf, &bufsize, NULL, GST_MAP_READ);
422 gst_byte_reader_init (&r, data, bufsize);
424 if (!gst_dca_parse_parse_header (dcaparse, &r, &size, &rate, &chans, &depth,
425 &endianness, &num_blocks, &samples_per_block, &terminator))
428 block_size = num_blocks * samples_per_block;
430 if (G_UNLIKELY (dcaparse->rate != rate || dcaparse->channels != chans
431 || dcaparse->depth != depth || dcaparse->endianness != endianness
432 || (!terminator && dcaparse->block_size != block_size)
433 || (size != dcaparse->frame_size))) {
436 caps = gst_caps_new_simple ("audio/x-dts",
437 "framed", G_TYPE_BOOLEAN, TRUE,
438 "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, chans,
439 "endianness", G_TYPE_INT, endianness, "depth", G_TYPE_INT, depth,
440 "block-size", G_TYPE_INT, block_size, "frame-size", G_TYPE_INT, size,
442 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
443 gst_caps_unref (caps);
445 dcaparse->rate = rate;
446 dcaparse->channels = chans;
447 dcaparse->depth = depth;
448 dcaparse->endianness = endianness;
449 dcaparse->block_size = block_size;
450 dcaparse->frame_size = size;
452 gst_base_parse_set_frame_rate (parse, rate, block_size, 0, 0);
455 gst_buffer_unmap (buf, data, bufsize);
461 /* this really shouldn't ever happen */
462 GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
463 gst_buffer_unmap (buf, data, bufsize);
464 return GST_FLOW_ERROR;