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