upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.git] / gst / audioparsers / gstac3parse.c
1 /* GStreamer AC3 parser
2  * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
3  * Copyright (C) 2009 Mark Nauwelaerts <mnauw users sf net>
4  * Copyright (C) 2009 Nokia Corporation. All rights reserved.
5  *   Contact: Stefan Kost <stefan.kost@nokia.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 /**
23  * SECTION:element-ac3parse
24  * @short_description: AC3 parser
25  * @see_also: #GstAmrParse, #GstAACParse
26  *
27  * This is an AC3 parser.
28  *
29  * <refsect2>
30  * <title>Example launch line</title>
31  * |[
32  * gst-launch filesrc location=abc.ac3 ! ac3parse ! a52dec ! audioresample ! audioconvert ! autoaudiosink
33  * ]|
34  * </refsect2>
35  */
36
37 /* TODO:
38  *  - add support for audio/x-private1-ac3 as well
39  *  - should accept framed and unframed input (needs decodebin fixes first)
40  */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include <string.h>
47
48 #include "gstac3parse.h"
49 #include <gst/base/gstbytereader.h>
50 #include <gst/base/gstbitreader.h>
51
52 GST_DEBUG_CATEGORY_STATIC (ac3_parse_debug);
53 #define GST_CAT_DEFAULT ac3_parse_debug
54
55 static const struct
56 {
57   const guint bit_rate;         /* nominal bit rate */
58   const guint frame_size[3];    /* frame size for 32kHz, 44kHz, and 48kHz */
59 } frmsizcod_table[38] = {
60   {
61     32, {
62   64, 69, 96}}, {
63     32, {
64   64, 70, 96}}, {
65     40, {
66   80, 87, 120}}, {
67     40, {
68   80, 88, 120}}, {
69     48, {
70   96, 104, 144}}, {
71     48, {
72   96, 105, 144}}, {
73     56, {
74   112, 121, 168}}, {
75     56, {
76   112, 122, 168}}, {
77     64, {
78   128, 139, 192}}, {
79     64, {
80   128, 140, 192}}, {
81     80, {
82   160, 174, 240}}, {
83     80, {
84   160, 175, 240}}, {
85     96, {
86   192, 208, 288}}, {
87     96, {
88   192, 209, 288}}, {
89     112, {
90   224, 243, 336}}, {
91     112, {
92   224, 244, 336}}, {
93     128, {
94   256, 278, 384}}, {
95     128, {
96   256, 279, 384}}, {
97     160, {
98   320, 348, 480}}, {
99     160, {
100   320, 349, 480}}, {
101     192, {
102   384, 417, 576}}, {
103     192, {
104   384, 418, 576}}, {
105     224, {
106   448, 487, 672}}, {
107     224, {
108   448, 488, 672}}, {
109     256, {
110   512, 557, 768}}, {
111     256, {
112   512, 558, 768}}, {
113     320, {
114   640, 696, 960}}, {
115     320, {
116   640, 697, 960}}, {
117     384, {
118   768, 835, 1152}}, {
119     384, {
120   768, 836, 1152}}, {
121     448, {
122   896, 975, 1344}}, {
123     448, {
124   896, 976, 1344}}, {
125     512, {
126   1024, 1114, 1536}}, {
127     512, {
128   1024, 1115, 1536}}, {
129     576, {
130   1152, 1253, 1728}}, {
131     576, {
132   1152, 1254, 1728}}, {
133     640, {
134   1280, 1393, 1920}}, {
135     640, {
136   1280, 1394, 1920}}
137 };
138
139 static const guint fscod_rates[4] = { 48000, 44100, 32000, 0 };
140 static const guint acmod_chans[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
141 static const guint numblks[4] = { 1, 2, 3, 6 };
142
143 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
144     GST_PAD_SRC,
145     GST_PAD_ALWAYS,
146     GST_STATIC_CAPS ("audio/x-ac3, framed = (boolean) true, "
147         " channels = (int) [ 1, 6 ], rate = (int) [ 32000, 48000 ]; "
148         "audio/x-eac3, framed = (boolean) true, "
149         " channels = (int) [ 1, 6 ], rate = (int) [ 32000, 48000 ] "));
150
151 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
152     GST_PAD_SINK,
153     GST_PAD_ALWAYS,
154     GST_STATIC_CAPS ("audio/x-ac3, framed = (boolean) false; "
155         "audio/x-eac3, framed = (boolean) false; "
156         "audio/ac3, framed = (boolean) false "));
157
158 static void gst_ac3_parse_finalize (GObject * object);
159
160 static gboolean gst_ac3_parse_start (GstBaseParse * parse);
161 static gboolean gst_ac3_parse_stop (GstBaseParse * parse);
162 static gboolean gst_ac3_parse_check_valid_frame (GstBaseParse * parse,
163     GstBaseParseFrame * frame, guint * size, gint * skipsize);
164 static GstFlowReturn gst_ac3_parse_parse_frame (GstBaseParse * parse,
165     GstBaseParseFrame * frame);
166
167 GST_BOILERPLATE (GstAc3Parse, gst_ac3_parse, GstBaseParse, GST_TYPE_BASE_PARSE);
168
169 static void
170 gst_ac3_parse_base_init (gpointer klass)
171 {
172   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
173
174   gst_element_class_add_pad_template (element_class,
175       gst_static_pad_template_get (&sink_template));
176   gst_element_class_add_pad_template (element_class,
177       gst_static_pad_template_get (&src_template));
178
179   gst_element_class_set_details_simple (element_class,
180       "AC3 audio stream parser", "Codec/Parser/Audio",
181       "AC3 parser", "Tim-Philipp Müller <tim centricular net>");
182 }
183
184 static void
185 gst_ac3_parse_class_init (GstAc3ParseClass * klass)
186 {
187   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
188   GObjectClass *object_class = G_OBJECT_CLASS (klass);
189
190   GST_DEBUG_CATEGORY_INIT (ac3_parse_debug, "ac3parse", 0,
191       "AC3 audio stream parser");
192
193   object_class->finalize = gst_ac3_parse_finalize;
194
195   parse_class->start = GST_DEBUG_FUNCPTR (gst_ac3_parse_start);
196   parse_class->stop = GST_DEBUG_FUNCPTR (gst_ac3_parse_stop);
197   parse_class->check_valid_frame =
198       GST_DEBUG_FUNCPTR (gst_ac3_parse_check_valid_frame);
199   parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_ac3_parse_parse_frame);
200 }
201
202 static void
203 gst_ac3_parse_reset (GstAc3Parse * ac3parse)
204 {
205   ac3parse->channels = -1;
206   ac3parse->sample_rate = -1;
207   ac3parse->eac = FALSE;
208 }
209
210 static void
211 gst_ac3_parse_init (GstAc3Parse * ac3parse, GstAc3ParseClass * klass)
212 {
213   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (ac3parse), 64 * 2);
214   gst_ac3_parse_reset (ac3parse);
215 }
216
217 static void
218 gst_ac3_parse_finalize (GObject * object)
219 {
220   G_OBJECT_CLASS (parent_class)->finalize (object);
221 }
222
223 static gboolean
224 gst_ac3_parse_start (GstBaseParse * parse)
225 {
226   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
227
228   GST_DEBUG_OBJECT (parse, "starting");
229
230   gst_ac3_parse_reset (ac3parse);
231
232   return TRUE;
233 }
234
235 static gboolean
236 gst_ac3_parse_stop (GstBaseParse * parse)
237 {
238   GST_DEBUG_OBJECT (parse, "stopping");
239
240   return TRUE;
241 }
242
243 static gboolean
244 gst_ac3_parse_frame_header_ac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
245     guint * frame_size, guint * rate, guint * chans, guint * blks, guint * sid)
246 {
247   GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
248   guint8 fscod, frmsizcod, bsid, acmod, lfe_on;
249
250   GST_LOG_OBJECT (ac3parse, "parsing ac3");
251
252   gst_bit_reader_skip_unchecked (&bits, 16 + 16);
253   fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);
254   frmsizcod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 6);
255
256   if (G_UNLIKELY (fscod == 3 || frmsizcod >= G_N_ELEMENTS (frmsizcod_table))) {
257     GST_DEBUG_OBJECT (ac3parse, "bad fscod=%d frmsizcod=%d", fscod, frmsizcod);
258     return FALSE;
259   }
260
261   bsid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 5);
262   gst_bit_reader_skip_unchecked (&bits, 3);     /* bsmod */
263   acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);
264
265   /* spec not quite clear here: decoder should decode if less than 8,
266    * but seemingly only defines 6 and 8 cases */
267   if (bsid > 8) {
268     GST_DEBUG_OBJECT (ac3parse, "unexpected bsid=%d", bsid);
269     return FALSE;
270   } else if (bsid != 8 && bsid != 6) {
271     GST_DEBUG_OBJECT (ac3parse, "undefined bsid=%d", bsid);
272   }
273
274   if ((acmod & 0x1) && (acmod != 0x1))  /* 3 front channels */
275     gst_bit_reader_skip_unchecked (&bits, 2);
276   if ((acmod & 0x4))            /* if a surround channel exists */
277     gst_bit_reader_skip_unchecked (&bits, 2);
278   if (acmod == 0x2)             /* if in 2/0 mode */
279     gst_bit_reader_skip_unchecked (&bits, 2);
280
281   lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);
282
283   if (frame_size)
284     *frame_size = frmsizcod_table[frmsizcod].frame_size[fscod] * 2;
285   if (rate)
286     *rate = fscod_rates[fscod];
287   if (chans)
288     *chans = acmod_chans[acmod] + lfe_on;
289   if (blks)
290     *blks = 6;
291   if (sid)
292     *sid = 0;
293
294   return TRUE;
295 }
296
297 static gboolean
298 gst_ac3_parse_frame_header_eac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
299     guint * frame_size, guint * rate, guint * chans, guint * blks, guint * sid)
300 {
301   GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
302   guint16 frmsiz, sample_rate, blocks;
303   guint8 strmtyp, fscod, fscod2, acmod, lfe_on, strmid, numblkscod;
304
305   GST_LOG_OBJECT (ac3parse, "parsing e-ac3");
306
307   gst_bit_reader_skip_unchecked (&bits, 16);
308   strmtyp = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* strmtyp     */
309   if (G_UNLIKELY (strmtyp == 3)) {
310     GST_DEBUG_OBJECT (ac3parse, "bad strmtyp %d", strmtyp);
311     return FALSE;
312   }
313
314   strmid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);  /* substreamid */
315   frmsiz = gst_bit_reader_get_bits_uint16_unchecked (&bits, 11);        /* frmsiz      */
316   fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);   /* fscod       */
317   if (fscod == 3) {
318     fscod2 = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);        /* fscod2      */
319     if (G_UNLIKELY (fscod2 == 3)) {
320       GST_DEBUG_OBJECT (ac3parse, "invalid fscod2");
321       return FALSE;
322     }
323     sample_rate = fscod_rates[fscod2] / 2;
324     blocks = 6;
325   } else {
326     numblkscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);    /* numblkscod  */
327     sample_rate = fscod_rates[fscod];
328     blocks = numblks[numblkscod];
329   }
330
331   acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);   /* acmod       */
332   lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);  /* lfeon       */
333
334   gst_bit_reader_skip_unchecked (&bits, 5);     /* bsid        */
335
336   if (frame_size)
337     *frame_size = (frmsiz + 1) * 2;
338   if (rate)
339     *rate = sample_rate;
340   if (chans)
341     *chans = acmod_chans[acmod] + lfe_on;
342   if (blks)
343     *blks = blocks;
344   if (sid)
345     *sid = (strmtyp & 0x1) << 3 | strmid;
346
347   return TRUE;
348 }
349
350 static gboolean
351 gst_ac3_parse_frame_header (GstAc3Parse * parse, GstBuffer * buf,
352     guint * framesize, guint * rate, guint * chans, guint * blocks,
353     guint * sid, gboolean * eac)
354 {
355   GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
356   guint16 sync;
357   guint8 bsid;
358
359   GST_MEMDUMP_OBJECT (parse, "AC3 frame sync", GST_BUFFER_DATA (buf), 16);
360
361   sync = gst_bit_reader_get_bits_uint16_unchecked (&bits, 16);
362   gst_bit_reader_skip_unchecked (&bits, 16 + 8);
363   bsid = gst_bit_reader_peek_bits_uint8_unchecked (&bits, 5);
364
365   if (G_UNLIKELY (sync != 0x0b77))
366     return FALSE;
367
368   GST_LOG_OBJECT (parse, "bsid = %d", bsid);
369
370   if (bsid <= 10) {
371     if (eac)
372       *eac = FALSE;
373     return gst_ac3_parse_frame_header_ac3 (parse, buf, framesize, rate, chans,
374         blocks, sid);
375   } else if (bsid <= 16) {
376     if (eac)
377       *eac = TRUE;
378     return gst_ac3_parse_frame_header_eac3 (parse, buf, framesize, rate, chans,
379         blocks, sid);
380   } else {
381     GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
382     return FALSE;
383   }
384 }
385
386 static gboolean
387 gst_ac3_parse_check_valid_frame (GstBaseParse * parse,
388     GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
389 {
390   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
391   GstBuffer *buf = frame->buffer;
392   GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buf);
393   gint off;
394   gboolean lost_sync, draining;
395
396   if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < 6))
397     return FALSE;
398
399   off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000, 0x0b770000,
400       0, GST_BUFFER_SIZE (buf));
401
402   GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
403
404   /* didn't find anything that looks like a sync word, skip */
405   if (off < 0) {
406     *skipsize = GST_BUFFER_SIZE (buf) - 3;
407     return FALSE;
408   }
409
410   /* possible frame header, but not at offset 0? skip bytes before sync */
411   if (off > 0) {
412     *skipsize = off;
413     return FALSE;
414   }
415
416   /* make sure the values in the frame header look sane */
417   if (!gst_ac3_parse_frame_header (ac3parse, buf, framesize, NULL, NULL,
418           NULL, NULL, NULL)) {
419     *skipsize = off + 2;
420     return FALSE;
421   }
422
423   GST_LOG_OBJECT (parse, "got frame");
424
425   lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
426   draining = GST_BASE_PARSE_DRAINING (parse);
427
428   if (lost_sync && !draining) {
429     guint16 word = 0;
430
431     GST_DEBUG_OBJECT (ac3parse, "resyncing; checking next frame syncword");
432
433     if (!gst_byte_reader_skip (&reader, *framesize) ||
434         !gst_byte_reader_get_uint16_be (&reader, &word)) {
435       GST_DEBUG_OBJECT (ac3parse, "... but not sufficient data");
436       gst_base_parse_set_min_frame_size (parse, *framesize + 6);
437       *skipsize = 0;
438       return FALSE;
439     } else {
440       if (word != 0x0b77) {
441         GST_DEBUG_OBJECT (ac3parse, "0x%x not OK", word);
442         *skipsize = off + 2;
443         return FALSE;
444       } else {
445         /* ok, got sync now, let's assume constant frame size */
446         gst_base_parse_set_min_frame_size (parse, *framesize);
447       }
448     }
449   }
450
451   return TRUE;
452 }
453
454 static GstFlowReturn
455 gst_ac3_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
456 {
457   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
458   GstBuffer *buf = frame->buffer;
459   guint fsize, rate, chans, blocks, sid;
460   gboolean eac;
461
462   if (!gst_ac3_parse_frame_header (ac3parse, buf, &fsize, &rate, &chans,
463           &blocks, &sid, &eac))
464     goto broken_header;
465
466   GST_LOG_OBJECT (parse, "size: %u, rate: %u, chans: %u", fsize, rate, chans);
467
468   if (G_UNLIKELY (sid)) {
469     /* dependent frame, no need to (ac)count for or consider further */
470     GST_LOG_OBJECT (parse, "sid: %d", sid);
471     frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME;
472     /* TODO maybe also mark as DELTA_UNIT,
473      * if that does not surprise baseparse elsewhere */
474     /* occupies same time space as previous base frame */
475     if (G_LIKELY (GST_BUFFER_TIMESTAMP (buf) >= GST_BUFFER_DURATION (buf)))
476       GST_BUFFER_TIMESTAMP (buf) -= GST_BUFFER_DURATION (buf);
477     /* only return if we already arranged for caps */
478     if (G_LIKELY (ac3parse->sample_rate > 0))
479       return GST_FLOW_OK;
480   }
481
482   if (G_UNLIKELY (ac3parse->sample_rate != rate || ac3parse->channels != chans
483           || ac3parse->eac != ac3parse->eac)) {
484     GstCaps *caps = gst_caps_new_simple (eac ? "audio/x-eac3" : "audio/x-ac3",
485         "framed", G_TYPE_BOOLEAN, TRUE, "rate", G_TYPE_INT, rate,
486         "channels", G_TYPE_INT, chans, NULL);
487     gst_buffer_set_caps (buf, caps);
488     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
489     gst_caps_unref (caps);
490
491     ac3parse->sample_rate = rate;
492     ac3parse->channels = chans;
493     ac3parse->eac = eac;
494
495     gst_base_parse_set_frame_rate (parse, rate, 256 * blocks, 2, 2);
496   }
497
498   return GST_FLOW_OK;
499
500 /* ERRORS */
501 broken_header:
502   {
503     /* this really shouldn't ever happen */
504     GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
505     return GST_FLOW_ERROR;
506   }
507 }