Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / audioparsers / gstdcaparse.c
1 /* GStreamer DCA parser
2  * Copyright (C) 2010 Tim-Philipp Müller <tim centricular net>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /**
21  * SECTION:element-dcaparse
22  * @short_description: DCA (DTS Coherent Acoustics) parser
23  * @see_also: #GstAmrParse, #GstAACParse, #GstAc3Parse
24  *
25  * This is a DCA (DTS Coherent Acoustics) parser.
26  *
27  * <refsect2>
28  * <title>Example launch line</title>
29  * |[
30  * gst-launch filesrc location=abc.dts ! dcaparse ! dtsdec ! audioresample ! audioconvert ! autoaudiosink
31  * ]|
32  * </refsect2>
33  */
34
35 /* TODO:
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
38  *
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)
42  */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <string.h>
49
50 #include "gstdcaparse.h"
51 #include <gst/base/gstbytereader.h>
52 #include <gst/base/gstbitreader.h>
53
54 GST_DEBUG_CATEGORY_STATIC (dca_parse_debug);
55 #define GST_CAT_DEFAULT dca_parse_debug
56
57 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
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]"));
67
68 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
69     GST_PAD_SINK,
70     GST_PAD_ALWAYS,
71     GST_STATIC_CAPS ("audio/x-dts"));
72
73 static void gst_dca_parse_finalize (GObject * object);
74
75 static gboolean gst_dca_parse_start (GstBaseParse * parse);
76 static gboolean gst_dca_parse_stop (GstBaseParse * parse);
77 static GstFlowReturn gst_dca_parse_handle_frame (GstBaseParse * parse,
78     GstBaseParseFrame * frame, gint * skipsize);
79 static GstCaps *gst_dca_parse_get_sink_caps (GstBaseParse * parse,
80     GstCaps * filter);
81
82 #define gst_dca_parse_parent_class parent_class
83 G_DEFINE_TYPE (GstDcaParse, gst_dca_parse, GST_TYPE_BASE_PARSE);
84
85 static void
86 gst_dca_parse_class_init (GstDcaParseClass * klass)
87 {
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);
91
92   GST_DEBUG_CATEGORY_INIT (dca_parse_debug, "dcaparse", 0,
93       "DCA audio stream parser");
94
95   object_class->finalize = gst_dca_parse_finalize;
96
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->handle_frame = GST_DEBUG_FUNCPTR (gst_dca_parse_handle_frame);
100   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_dca_parse_get_sink_caps);
101
102   gst_element_class_add_pad_template (element_class,
103       gst_static_pad_template_get (&sink_template));
104   gst_element_class_add_pad_template (element_class,
105       gst_static_pad_template_get (&src_template));
106
107   gst_element_class_set_details_simple (element_class,
108       "DTS Coherent Acoustics audio stream parser", "Codec/Parser/Audio",
109       "DCA parser", "Tim-Philipp Müller <tim centricular net>");
110 }
111
112 static void
113 gst_dca_parse_reset (GstDcaParse * dcaparse)
114 {
115   dcaparse->channels = -1;
116   dcaparse->rate = -1;
117   dcaparse->depth = -1;
118   dcaparse->endianness = -1;
119   dcaparse->block_size = -1;
120   dcaparse->frame_size = -1;
121   dcaparse->last_sync = 0;
122 }
123
124 static void
125 gst_dca_parse_init (GstDcaParse * dcaparse)
126 {
127   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (dcaparse),
128       DCA_MIN_FRAMESIZE);
129   gst_dca_parse_reset (dcaparse);
130 }
131
132 static void
133 gst_dca_parse_finalize (GObject * object)
134 {
135   G_OBJECT_CLASS (parent_class)->finalize (object);
136 }
137
138 static gboolean
139 gst_dca_parse_start (GstBaseParse * parse)
140 {
141   GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
142
143   GST_DEBUG_OBJECT (parse, "starting");
144
145   gst_dca_parse_reset (dcaparse);
146
147   return TRUE;
148 }
149
150 static gboolean
151 gst_dca_parse_stop (GstBaseParse * parse)
152 {
153   GST_DEBUG_OBJECT (parse, "stopping");
154
155   return TRUE;
156 }
157
158 static gboolean
159 gst_dca_parse_parse_header (GstDcaParse * dcaparse,
160     const GstByteReader * reader, guint * frame_size,
161     guint * sample_rate, guint * channels, guint * depth,
162     gint * endianness, guint * num_blocks, guint * samples_per_block,
163     gboolean * terminator)
164 {
165   static const int sample_rates[16] = { 0, 8000, 16000, 32000, 0, 0, 11025,
166     22050, 44100, 0, 0, 12000, 24000, 48000, 96000, 192000
167   };
168   static const guint8 channels_table[16] = { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5,
169     6, 6, 6, 7, 8, 8
170   };
171   GstByteReader r = *reader;
172   guint16 hdr[8];
173   guint32 marker;
174   guint chans, lfe, i;
175
176   if (gst_byte_reader_get_remaining (&r) < (4 + sizeof (hdr)))
177     return FALSE;
178
179   marker = gst_byte_reader_peek_uint32_be_unchecked (&r);
180
181   /* raw big endian or 14-bit big endian */
182   if (marker == 0x7FFE8001 || marker == 0x1FFFE800) {
183     for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
184       hdr[i] = gst_byte_reader_get_uint16_be_unchecked (&r);
185   } else
186     /* raw little endian or 14-bit little endian */
187   if (marker == 0xFE7F0180 || marker == 0xFF1F00E8) {
188     for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
189       hdr[i] = gst_byte_reader_get_uint16_le_unchecked (&r);
190   } else {
191     return FALSE;
192   }
193
194   GST_LOG_OBJECT (dcaparse, "dts sync marker 0x%08x at offset %u", marker,
195       gst_byte_reader_get_pos (reader));
196
197   /* 14-bit mode */
198   if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) {
199     if ((hdr[2] & 0xFFF0) != 0x07F0)
200       return FALSE;
201     /* discard top 2 bits (2 void), shift in 2 */
202     hdr[0] = (hdr[0] << 2) | ((hdr[1] >> 12) & 0x0003);
203     /* discard top 4 bits (2 void, 2 shifted into hdr[0]), shift in 4 etc. */
204     hdr[1] = (hdr[1] << 4) | ((hdr[2] >> 10) & 0x000F);
205     hdr[2] = (hdr[2] << 6) | ((hdr[3] >> 8) & 0x003F);
206     hdr[3] = (hdr[3] << 8) | ((hdr[4] >> 6) & 0x00FF);
207     hdr[4] = (hdr[4] << 10) | ((hdr[5] >> 4) & 0x03FF);
208     hdr[5] = (hdr[5] << 12) | ((hdr[6] >> 2) & 0x0FFF);
209     hdr[6] = (hdr[6] << 14) | ((hdr[7] >> 0) & 0x3FFF);
210     g_assert (hdr[0] == 0x7FFE && hdr[1] == 0x8001);
211   }
212
213   GST_LOG_OBJECT (dcaparse, "frame header: %04x%04x%04x%04x",
214       hdr[2], hdr[3], hdr[4], hdr[5]);
215
216   *terminator = (hdr[2] & 0x80) ? FALSE : TRUE;
217   *samples_per_block = ((hdr[2] >> 10) & 0x1f) + 1;
218   *num_blocks = ((hdr[2] >> 2) & 0x7F) + 1;
219   *frame_size = (((hdr[2] & 0x03) << 12) | (hdr[3] >> 4)) + 1;
220   chans = ((hdr[3] & 0x0F) << 2) | (hdr[4] >> 14);
221   *sample_rate = sample_rates[(hdr[4] >> 10) & 0x0F];
222   lfe = (hdr[5] >> 9) & 0x03;
223
224   GST_TRACE_OBJECT (dcaparse, "frame size %u, num_blocks %u, rate %u, "
225       "samples per block %u", *frame_size, *num_blocks, *sample_rate,
226       *samples_per_block);
227
228   if (*num_blocks < 6 || *frame_size < 96 || *sample_rate == 0)
229     return FALSE;
230
231   if (marker == 0x1FFFE800 || marker == 0xFF1F00E8)
232     *frame_size = (*frame_size * 16) / 14;      /* FIXME: round up? */
233
234   if (chans < G_N_ELEMENTS (channels_table))
235     *channels = channels_table[chans] + ((lfe) ? 1 : 0);
236   else
237     *channels = 0;
238
239   if (depth)
240     *depth = (marker == 0x1FFFE800 || marker == 0xFF1F00E8) ? 14 : 16;
241   if (endianness)
242     *endianness = (marker == 0xFE7F0180 || marker == 0xFF1F00E8) ?
243         G_LITTLE_ENDIAN : G_BIG_ENDIAN;
244
245   GST_TRACE_OBJECT (dcaparse, "frame size %u, channels %u, rate %u, "
246       "num_blocks %u, samples_per_block %u", *frame_size, *channels,
247       *sample_rate, *num_blocks, *samples_per_block);
248
249   return TRUE;
250 }
251
252 static gint
253 gst_dca_parse_find_sync (GstDcaParse * dcaparse, GstByteReader * reader,
254     gsize bufsize, guint32 * sync)
255 {
256   guint32 best_sync = 0;
257   guint best_offset = G_MAXUINT;
258   gint off;
259
260   /* FIXME: verify syncs via _parse_header() here already */
261
262   /* Raw little endian */
263   off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0xfe7f0180,
264       0, bufsize);
265   if (off >= 0 && off < best_offset) {
266     best_offset = off;
267     best_sync = 0xfe7f0180;
268   }
269
270   /* Raw big endian */
271   off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x7ffe8001,
272       0, bufsize);
273   if (off >= 0 && off < best_offset) {
274     best_offset = off;
275     best_sync = 0x7ffe8001;
276   }
277
278   /* FIXME: check next 2 bytes as well for 14-bit formats (but then don't
279    * forget to adjust the *skipsize= in _check_valid_frame() */
280
281   /* 14-bit little endian  */
282   off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0xff1f00e8,
283       0, bufsize);
284   if (off >= 0 && off < best_offset) {
285     best_offset = off;
286     best_sync = 0xff1f00e8;
287   }
288
289   /* 14-bit big endian  */
290   off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x1fffe800,
291       0, bufsize);
292   if (off >= 0 && off < best_offset) {
293     best_offset = off;
294     best_sync = 0x1fffe800;
295   }
296
297   if (best_offset == G_MAXUINT)
298     return -1;
299
300   *sync = best_sync;
301   return best_offset;
302 }
303
304 static GstFlowReturn
305 gst_dca_parse_handle_frame (GstBaseParse * parse,
306     GstBaseParseFrame * frame, gint * skipsize)
307 {
308   GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
309   GstBuffer *buf = frame->buffer;
310   GstByteReader r;
311   gboolean parser_draining;
312   gboolean parser_in_sync;
313   gboolean terminator;
314   guint32 sync = 0;
315   guint size, rate, chans, num_blocks, samples_per_block, depth;
316   gint block_size;
317   gint endianness;
318   gint off = -1;
319   GstMapInfo map;
320   GstFlowReturn ret = GST_FLOW_EOS;
321
322   gst_buffer_map (buf, &map, GST_MAP_READ);
323
324   if (G_UNLIKELY (map.size < 16)) {
325     *skipsize = 1;
326     goto cleanup;
327   }
328
329   parser_in_sync = !GST_BASE_PARSE_LOST_SYNC (parse);
330
331   gst_byte_reader_init (&r, map.data, map.size);
332
333   if (G_LIKELY (parser_in_sync && dcaparse->last_sync != 0)) {
334     off = gst_byte_reader_masked_scan_uint32 (&r, 0xffffffff,
335         dcaparse->last_sync, 0, map.size);
336   }
337
338   if (G_UNLIKELY (off < 0)) {
339     off = gst_dca_parse_find_sync (dcaparse, &r, map.size, &sync);
340   }
341
342   /* didn't find anything that looks like a sync word, skip */
343   if (off < 0) {
344     *skipsize = map.size - 3;
345     GST_DEBUG_OBJECT (dcaparse, "no sync, skipping %d bytes", *skipsize);
346     goto cleanup;
347   }
348
349   GST_LOG_OBJECT (parse, "possible sync %08x at buffer offset %d", sync, off);
350
351   /* possible frame header, but not at offset 0? skip bytes before sync */
352   if (off > 0) {
353     *skipsize = off;
354     goto cleanup;
355   }
356
357   /* make sure the values in the frame header look sane */
358   if (!gst_dca_parse_parse_header (dcaparse, &r, &size, &rate, &chans, &depth,
359           &endianness, &num_blocks, &samples_per_block, &terminator)) {
360     *skipsize = 4;
361     goto cleanup;
362   }
363
364   GST_LOG_OBJECT (parse, "got frame, sync %08x, size %u, rate %d, channels %d",
365       sync, size, rate, chans);
366
367   dcaparse->last_sync = sync;
368
369   parser_draining = GST_BASE_PARSE_DRAINING (parse);
370
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 (map.size >= (size + 16)) {
375       guint s2, r2, c2, n2, s3;
376       gboolean t;
377
378       GST_MEMDUMP ("buf", map.data, size + 16);
379       gst_byte_reader_init (&r, map.data, map.size);
380       gst_byte_reader_skip_unchecked (&r, size);
381
382       if (!gst_dca_parse_parse_header (dcaparse, &r, &s2, &r2, &c2, NULL, NULL,
383               &n2, &s3, &t)) {
384         GST_DEBUG_OBJECT (dcaparse, "didn't find second syncword");
385         *skipsize = 4;
386         goto cleanup;
387       }
388
389       /* ok, got sync now, let's assume constant frame size */
390       gst_base_parse_set_min_frame_size (parse, size);
391     } else {
392       /* wait for some more data */
393       GST_LOG_OBJECT (dcaparse,
394           "next sync out of reach (%" G_GSIZE_FORMAT " < %u)", map.size,
395           size + 16);
396       goto cleanup;
397     }
398   }
399
400   /* found frame */
401   ret = GST_FLOW_OK;
402
403   /* metadata handling */
404   block_size = num_blocks * samples_per_block;
405
406   if (G_UNLIKELY (dcaparse->rate != rate || dcaparse->channels != chans
407           || dcaparse->depth != depth || dcaparse->endianness != endianness
408           || (!terminator && dcaparse->block_size != block_size)
409           || (size != dcaparse->frame_size))) {
410     GstCaps *caps;
411
412     caps = gst_caps_new_simple ("audio/x-dts",
413         "framed", G_TYPE_BOOLEAN, TRUE,
414         "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, chans,
415         "endianness", G_TYPE_INT, endianness, "depth", G_TYPE_INT, depth,
416         "block-size", G_TYPE_INT, block_size, "frame-size", G_TYPE_INT, size,
417         NULL);
418     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
419     gst_caps_unref (caps);
420
421     dcaparse->rate = rate;
422     dcaparse->channels = chans;
423     dcaparse->depth = depth;
424     dcaparse->endianness = endianness;
425     dcaparse->block_size = block_size;
426     dcaparse->frame_size = size;
427
428     gst_base_parse_set_frame_rate (parse, rate, block_size, 0, 0);
429   }
430
431 cleanup:
432   gst_buffer_unmap (buf, &map);
433
434   if (ret == GST_FLOW_OK && size <= map.size) {
435     ret = gst_base_parse_finish_frame (parse, frame, size);
436   } else {
437     ret = GST_FLOW_OK;
438   }
439
440   return ret;
441 }
442
443 static GstCaps *
444 gst_dca_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
445 {
446   GstCaps *peercaps;
447   GstCaps *res;
448
449   /* FIXME: handle filter caps */
450
451   peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
452   if (peercaps) {
453     guint i, n;
454
455     /* Remove the framed field */
456     peercaps = gst_caps_make_writable (peercaps);
457     n = gst_caps_get_size (peercaps);
458     for (i = 0; i < n; i++) {
459       GstStructure *s = gst_caps_get_structure (peercaps, i);
460
461       gst_structure_remove_field (s, "framed");
462     }
463
464     res =
465         gst_caps_intersect_full (peercaps,
466         gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)),
467         GST_CAPS_INTERSECT_FIRST);
468     gst_caps_unref (peercaps);
469   } else {
470     res =
471         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD
472             (parse)));
473   }
474
475   return res;
476 }