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