upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.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
67 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
68     GST_PAD_SINK,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS ("audio/x-dts, framed = (boolean) false"));
71
72 static void gst_dca_parse_finalize (GObject * object);
73
74 static gboolean gst_dca_parse_start (GstBaseParse * parse);
75 static gboolean gst_dca_parse_stop (GstBaseParse * parse);
76 static gboolean gst_dca_parse_check_valid_frame (GstBaseParse * parse,
77     GstBaseParseFrame * frame, guint * size, gint * skipsize);
78 static GstFlowReturn gst_dca_parse_parse_frame (GstBaseParse * parse,
79     GstBaseParseFrame * frame);
80
81 GST_BOILERPLATE (GstDcaParse, gst_dca_parse, GstBaseParse, GST_TYPE_BASE_PARSE);
82
83 static void
84 gst_dca_parse_base_init (gpointer klass)
85 {
86   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
87
88   gst_element_class_add_pad_template (element_class,
89       gst_static_pad_template_get (&sink_template));
90   gst_element_class_add_pad_template (element_class,
91       gst_static_pad_template_get (&src_template));
92
93   gst_element_class_set_details_simple (element_class,
94       "DTS Coherent Acoustics audio stream parser", "Codec/Parser/Audio",
95       "DCA parser", "Tim-Philipp Müller <tim centricular net>");
96 }
97
98 static void
99 gst_dca_parse_class_init (GstDcaParseClass * klass)
100 {
101   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
102   GObjectClass *object_class = G_OBJECT_CLASS (klass);
103
104   GST_DEBUG_CATEGORY_INIT (dca_parse_debug, "dcaparse", 0,
105       "DCA audio stream parser");
106
107   object_class->finalize = gst_dca_parse_finalize;
108
109   parse_class->start = GST_DEBUG_FUNCPTR (gst_dca_parse_start);
110   parse_class->stop = GST_DEBUG_FUNCPTR (gst_dca_parse_stop);
111   parse_class->check_valid_frame =
112       GST_DEBUG_FUNCPTR (gst_dca_parse_check_valid_frame);
113   parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_dca_parse_parse_frame);
114 }
115
116 static void
117 gst_dca_parse_reset (GstDcaParse * dcaparse)
118 {
119   dcaparse->channels = -1;
120   dcaparse->rate = -1;
121   dcaparse->depth = -1;
122   dcaparse->endianness = -1;
123   dcaparse->block_size = -1;
124   dcaparse->frame_size = -1;
125   dcaparse->last_sync = 0;
126 }
127
128 static void
129 gst_dca_parse_init (GstDcaParse * dcaparse, GstDcaParseClass * klass)
130 {
131   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (dcaparse),
132       DCA_MIN_FRAMESIZE);
133   gst_dca_parse_reset (dcaparse);
134 }
135
136 static void
137 gst_dca_parse_finalize (GObject * object)
138 {
139   G_OBJECT_CLASS (parent_class)->finalize (object);
140 }
141
142 static gboolean
143 gst_dca_parse_start (GstBaseParse * parse)
144 {
145   GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
146
147   GST_DEBUG_OBJECT (parse, "starting");
148
149   gst_dca_parse_reset (dcaparse);
150
151   return TRUE;
152 }
153
154 static gboolean
155 gst_dca_parse_stop (GstBaseParse * parse)
156 {
157   GST_DEBUG_OBJECT (parse, "stopping");
158
159   return TRUE;
160 }
161
162 static gboolean
163 gst_dca_parse_parse_header (GstDcaParse * dcaparse,
164     const GstByteReader * reader, guint * frame_size,
165     guint * sample_rate, guint * channels, guint * depth,
166     gint * endianness, guint * num_blocks, guint * samples_per_block,
167     gboolean * terminator)
168 {
169   static const int sample_rates[16] = { 0, 8000, 16000, 32000, 0, 0, 11025,
170     22050, 44100, 0, 0, 12000, 24000, 48000, 96000, 192000
171   };
172   static const guint8 channels_table[16] = { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5,
173     6, 6, 6, 7, 8, 8
174   };
175   GstByteReader r = *reader;
176   guint16 hdr[8];
177   guint32 marker;
178   guint chans, lfe, i;
179
180   if (gst_byte_reader_get_remaining (&r) < (4 + sizeof (hdr)))
181     return FALSE;
182
183   marker = gst_byte_reader_peek_uint32_be_unchecked (&r);
184
185   /* raw big endian or 14-bit big endian */
186   if (marker == 0x7FFE8001 || marker == 0x1FFFE800) {
187     for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
188       hdr[i] = gst_byte_reader_get_uint16_be_unchecked (&r);
189   } else
190     /* raw little endian or 14-bit little endian */
191   if (marker == 0xFE7F0180 || marker == 0xFF1F00E8) {
192     for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
193       hdr[i] = gst_byte_reader_get_uint16_le_unchecked (&r);
194   } else {
195     return FALSE;
196   }
197
198   GST_LOG_OBJECT (dcaparse, "dts sync marker 0x%08x at offset %u", marker,
199       gst_byte_reader_get_pos (reader));
200
201   /* 14-bit mode */
202   if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) {
203     if ((hdr[2] & 0xFFF0) != 0x07F0)
204       return FALSE;
205     /* discard top 2 bits (2 void), shift in 2 */
206     hdr[0] = (hdr[0] << 2) | ((hdr[1] >> 12) & 0x0003);
207     /* discard top 4 bits (2 void, 2 shifted into hdr[0]), shift in 4 etc. */
208     hdr[1] = (hdr[1] << 4) | ((hdr[2] >> 10) & 0x000F);
209     hdr[2] = (hdr[2] << 6) | ((hdr[3] >> 8) & 0x003F);
210     hdr[3] = (hdr[3] << 8) | ((hdr[4] >> 6) & 0x00FF);
211     hdr[4] = (hdr[4] << 10) | ((hdr[5] >> 4) & 0x03FF);
212     hdr[5] = (hdr[5] << 12) | ((hdr[6] >> 2) & 0x0FFF);
213     hdr[6] = (hdr[6] << 14) | ((hdr[7] >> 0) & 0x3FFF);
214     g_assert (hdr[0] == 0x7FFE && hdr[1] == 0x8001);
215   }
216
217   GST_LOG_OBJECT (dcaparse, "frame header: %04x%04x%04x%04x",
218       hdr[2], hdr[3], hdr[4], hdr[5]);
219
220   *terminator = (hdr[2] & 0x80) ? FALSE : TRUE;
221   *samples_per_block = ((hdr[2] >> 10) & 0x1f) + 1;
222   *num_blocks = ((hdr[2] >> 2) & 0x7F) + 1;
223   *frame_size = (((hdr[2] & 0x03) << 12) | (hdr[3] >> 4)) + 1;
224   chans = ((hdr[3] & 0x0F) << 2) | (hdr[4] >> 14);
225   *sample_rate = sample_rates[(hdr[4] >> 10) & 0x0F];
226   lfe = (hdr[5] >> 9) & 0x03;
227
228   GST_TRACE_OBJECT (dcaparse, "frame size %u, num_blocks %u, rate %u, "
229       "samples per block %u", *frame_size, *num_blocks, *sample_rate,
230       *samples_per_block);
231
232   if (*num_blocks < 6 || *frame_size < 96 || *sample_rate == 0)
233     return FALSE;
234
235   if (marker == 0x1FFFE800 || marker == 0xFF1F00E8)
236     *frame_size = (*frame_size * 16) / 14;      /* FIXME: round up? */
237
238   if (chans < G_N_ELEMENTS (channels_table))
239     *channels = channels_table[chans] + ((lfe) ? 1 : 0);
240   else
241     *channels = 0;
242
243   if (depth)
244     *depth = (marker == 0x1FFFE800 || marker == 0xFF1F00E8) ? 14 : 16;
245   if (endianness)
246     *endianness = (marker == 0xFE7F0180 || marker == 0xFF1F00E8) ?
247         G_LITTLE_ENDIAN : G_BIG_ENDIAN;
248
249   GST_TRACE_OBJECT (dcaparse, "frame size %u, channels %u, rate %u, "
250       "num_blocks %u, samples_per_block %u", *frame_size, *channels,
251       *sample_rate, *num_blocks, *samples_per_block);
252
253   return TRUE;
254 }
255
256 static gint
257 gst_dca_parse_find_sync (GstDcaParse * dcaparse, GstByteReader * reader,
258     const GstBuffer * buf, guint32 * sync)
259 {
260   guint32 best_sync = 0;
261   guint best_offset = G_MAXUINT;
262   gint off;
263
264   /* FIXME: verify syncs via _parse_header() here already */
265
266   /* Raw little endian */
267   off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0xfe7f0180,
268       0, GST_BUFFER_SIZE (buf));
269   if (off >= 0 && off < best_offset) {
270     best_offset = off;
271     best_sync = 0xfe7f0180;
272   }
273
274   /* Raw big endian */
275   off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x7ffe8001,
276       0, GST_BUFFER_SIZE (buf));
277   if (off >= 0 && off < best_offset) {
278     best_offset = off;
279     best_sync = 0x7ffe8001;
280   }
281
282   /* FIXME: check next 2 bytes as well for 14-bit formats (but then don't
283    * forget to adjust the *skipsize= in _check_valid_frame() */
284
285   /* 14-bit little endian  */
286   off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0xff1f00e8,
287       0, GST_BUFFER_SIZE (buf));
288   if (off >= 0 && off < best_offset) {
289     best_offset = off;
290     best_sync = 0xff1f00e8;
291   }
292
293   /* 14-bit big endian  */
294   off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x1fffe800,
295       0, GST_BUFFER_SIZE (buf));
296   if (off >= 0 && off < best_offset) {
297     best_offset = off;
298     best_sync = 0x1fffe800;
299   }
300
301   if (best_offset == G_MAXUINT)
302     return -1;
303
304   *sync = best_sync;
305   return best_offset;
306 }
307
308 static gboolean
309 gst_dca_parse_check_valid_frame (GstBaseParse * parse,
310     GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
311 {
312   GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
313   GstBuffer *buf = frame->buffer;
314   GstByteReader r = GST_BYTE_READER_INIT_FROM_BUFFER (buf);
315   gboolean parser_draining;
316   gboolean parser_in_sync;
317   gboolean terminator;
318   guint32 sync = 0;
319   guint size, rate, chans, num_blocks, samples_per_block;
320   gint off = -1;
321
322   if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < 16))
323     return FALSE;
324
325   parser_in_sync = !GST_BASE_PARSE_LOST_SYNC (parse);
326
327   if (G_LIKELY (parser_in_sync && dcaparse->last_sync != 0)) {
328     off = gst_byte_reader_masked_scan_uint32 (&r, 0xffffffff,
329         dcaparse->last_sync, 0, GST_BUFFER_SIZE (buf));
330   }
331
332   if (G_UNLIKELY (off < 0)) {
333     off = gst_dca_parse_find_sync (dcaparse, &r, buf, &sync);
334   }
335
336   /* didn't find anything that looks like a sync word, skip */
337   if (off < 0) {
338     *skipsize = GST_BUFFER_SIZE (buf) - 3;
339     GST_DEBUG_OBJECT (dcaparse, "no sync, skipping %d bytes", *skipsize);
340     return FALSE;
341   }
342
343   GST_LOG_OBJECT (parse, "possible sync %08x at buffer offset %d", sync, off);
344
345   /* possible frame header, but not at offset 0? skip bytes before sync */
346   if (off > 0) {
347     *skipsize = off;
348     return FALSE;
349   }
350
351   /* make sure the values in the frame header look sane */
352   if (!gst_dca_parse_parse_header (dcaparse, &r, &size, &rate, &chans, NULL,
353           NULL, &num_blocks, &samples_per_block, &terminator)) {
354     *skipsize = 4;
355     return FALSE;
356   }
357
358   GST_LOG_OBJECT (parse, "got frame, sync %08x, size %u, rate %d, channels %d",
359       sync, size, rate, chans);
360
361   *framesize = size;
362
363   dcaparse->last_sync = sync;
364
365   parser_draining = GST_BASE_PARSE_DRAINING (parse);
366
367   if (!parser_in_sync && !parser_draining) {
368     /* check for second frame to be sure */
369     GST_DEBUG_OBJECT (dcaparse, "resyncing; checking next frame syncword");
370     if (GST_BUFFER_SIZE (buf) >= (size + 16)) {
371       guint s2, r2, c2, n2, s3;
372       gboolean t;
373
374       GST_MEMDUMP ("buf", GST_BUFFER_DATA (buf), size + 16);
375       gst_byte_reader_init_from_buffer (&r, buf);
376       gst_byte_reader_skip_unchecked (&r, size);
377
378       if (!gst_dca_parse_parse_header (dcaparse, &r, &s2, &r2, &c2, NULL, NULL,
379               &n2, &s3, &t)) {
380         GST_DEBUG_OBJECT (dcaparse, "didn't find second syncword");
381         *skipsize = 4;
382         return FALSE;
383       }
384
385       /* ok, got sync now, let's assume constant frame size */
386       gst_base_parse_set_min_frame_size (parse, size);
387     } else {
388       /* FIXME: baseparse always seems to hand us buffers of min_frame_size
389        * bytes, which is unhelpful here */
390       GST_LOG_OBJECT (dcaparse, "next sync out of reach (%u < %u)",
391           GST_BUFFER_SIZE (buf), size + 16);
392       /* *skipsize = 0; */
393       /* return FALSE; */
394     }
395   }
396
397   return TRUE;
398 }
399
400 static GstFlowReturn
401 gst_dca_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
402 {
403   GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
404   GstBuffer *buf = frame->buffer;
405   GstByteReader r = GST_BYTE_READER_INIT_FROM_BUFFER (buf);
406   guint size, rate, chans, depth, block_size, num_blocks, samples_per_block;
407   gint endianness;
408   gboolean terminator;
409
410   if (!gst_dca_parse_parse_header (dcaparse, &r, &size, &rate, &chans, &depth,
411           &endianness, &num_blocks, &samples_per_block, &terminator))
412     goto broken_header;
413
414   block_size = num_blocks * samples_per_block;
415
416   if (G_UNLIKELY (dcaparse->rate != rate || dcaparse->channels != chans
417           || dcaparse->depth != depth || dcaparse->endianness != endianness
418           || (!terminator && dcaparse->block_size != block_size)
419           || (size != dcaparse->frame_size))) {
420     GstCaps *caps;
421
422     caps = gst_caps_new_simple ("audio/x-dts",
423         "framed", G_TYPE_BOOLEAN, TRUE,
424         "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, chans,
425         "endianness", G_TYPE_INT, endianness, "depth", G_TYPE_INT, depth,
426         "block-size", G_TYPE_INT, block_size, "frame-size", G_TYPE_INT, size,
427         NULL);
428     gst_buffer_set_caps (buf, caps);
429     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
430     gst_caps_unref (caps);
431
432     dcaparse->rate = rate;
433     dcaparse->channels = chans;
434     dcaparse->depth = depth;
435     dcaparse->endianness = endianness;
436     dcaparse->block_size = block_size;
437     dcaparse->frame_size = size;
438
439     gst_base_parse_set_frame_rate (parse, rate, block_size, 0, 0);
440   }
441
442   return GST_FLOW_OK;
443
444 /* ERRORS */
445 broken_header:
446   {
447     /* this really shouldn't ever happen */
448     GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
449     return GST_FLOW_ERROR;
450   }
451 }