9b1a365d2bc21e1f05d056ca5514b92497bcd112
[platform/upstream/gstreamer.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) [ 8000, 48000 ], "
148         " alignment = (string) { iec61937, frame}; "
149         "audio/x-eac3, framed = (boolean) true, "
150         " channels = (int) [ 1, 6 ], rate = (int) [ 8000, 48000 ], "
151         " alignment = (string) { iec61937, frame}; "));
152
153 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
154     GST_PAD_SINK,
155     GST_PAD_ALWAYS,
156     GST_STATIC_CAPS ("audio/x-ac3; " "audio/x-eac3; " "audio/ac3"));
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 static gboolean gst_ac3_parse_src_event (GstBaseParse * parse,
167     GstEvent * event);
168 static GstCaps *gst_ac3_parse_get_sink_caps (GstBaseParse * parse,
169     GstCaps * filter);
170
171 #define gst_ac3_parse_parent_class parent_class
172 G_DEFINE_TYPE (GstAc3Parse, gst_ac3_parse, GST_TYPE_BASE_PARSE);
173
174 static void
175 gst_ac3_parse_class_init (GstAc3ParseClass * klass)
176 {
177   GObjectClass *object_class = G_OBJECT_CLASS (klass);
178   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
179   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
180
181   GST_DEBUG_CATEGORY_INIT (ac3_parse_debug, "ac3parse", 0,
182       "AC3 audio stream parser");
183
184   object_class->finalize = gst_ac3_parse_finalize;
185
186   gst_element_class_add_pad_template (element_class,
187       gst_static_pad_template_get (&sink_template));
188   gst_element_class_add_pad_template (element_class,
189       gst_static_pad_template_get (&src_template));
190
191   gst_element_class_set_details_simple (element_class,
192       "AC3 audio stream parser", "Codec/Parser/Converter/Audio",
193       "AC3 parser", "Tim-Philipp Müller <tim centricular net>");
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   parse_class->src_event = GST_DEBUG_FUNCPTR (gst_ac3_parse_src_event);
201   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_ac3_parse_get_sink_caps);
202 }
203
204 static void
205 gst_ac3_parse_reset (GstAc3Parse * ac3parse)
206 {
207   ac3parse->channels = -1;
208   ac3parse->sample_rate = -1;
209   ac3parse->blocks = -1;
210   ac3parse->eac = FALSE;
211   g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_NONE);
212 }
213
214 static void
215 gst_ac3_parse_init (GstAc3Parse * ac3parse)
216 {
217   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (ac3parse), 6);
218   gst_ac3_parse_reset (ac3parse);
219 }
220
221 static void
222 gst_ac3_parse_finalize (GObject * object)
223 {
224   G_OBJECT_CLASS (parent_class)->finalize (object);
225 }
226
227 static gboolean
228 gst_ac3_parse_start (GstBaseParse * parse)
229 {
230   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
231
232   GST_DEBUG_OBJECT (parse, "starting");
233
234   gst_ac3_parse_reset (ac3parse);
235
236   return TRUE;
237 }
238
239 static gboolean
240 gst_ac3_parse_stop (GstBaseParse * parse)
241 {
242   GST_DEBUG_OBJECT (parse, "stopping");
243
244   return TRUE;
245 }
246
247 static void
248 gst_ac3_parse_set_alignment (GstAc3Parse * ac3parse, gboolean eac)
249 {
250   GstCaps *caps;
251   GstStructure *st;
252   const gchar *str = NULL;
253   int i;
254
255   if (G_LIKELY (!eac))
256     goto done;
257
258   caps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (ac3parse));
259
260   if (!caps)
261     goto done;
262
263   for (i = 0; i < gst_caps_get_size (caps); i++) {
264     st = gst_caps_get_structure (caps, i);
265
266     if (!g_str_equal (gst_structure_get_name (st), "audio/x-eac3"))
267       continue;
268
269     if ((str = gst_structure_get_string (st, "alignment"))) {
270       if (g_str_equal (str, "iec61937")) {
271         g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
272         GST_DEBUG_OBJECT (ac3parse, "picked iec61937 alignment");
273       } else if (g_str_equal (str, "frame") == 0) {
274         g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
275         GST_DEBUG_OBJECT (ac3parse, "picked frame alignment");
276       } else {
277         g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
278         GST_WARNING_OBJECT (ac3parse, "unknown alignment: %s", str);
279       }
280       break;
281     }
282   }
283
284   if (caps)
285     gst_caps_unref (caps);
286
287 done:
288   /* default */
289   if (ac3parse->align == GST_AC3_PARSE_ALIGN_NONE) {
290     g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
291     GST_DEBUG_OBJECT (ac3parse, "picked syncframe alignment");
292   }
293 }
294
295 static gboolean
296 gst_ac3_parse_frame_header_ac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
297     gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
298     guint * sid)
299 {
300   GstBitReader bits;
301   GstMapInfo map;
302   guint8 fscod, frmsizcod, bsid, acmod, lfe_on, rate_scale;
303   gboolean ret = FALSE;
304
305   GST_LOG_OBJECT (ac3parse, "parsing ac3");
306
307   gst_buffer_map (buf, &map, GST_MAP_READ);
308   gst_bit_reader_init (&bits, map.data, map.size);
309   gst_bit_reader_skip_unchecked (&bits, skip * 8);
310
311   gst_bit_reader_skip_unchecked (&bits, 16 + 16);
312   fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);
313   frmsizcod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 6);
314
315   if (G_UNLIKELY (fscod == 3 || frmsizcod >= G_N_ELEMENTS (frmsizcod_table))) {
316     GST_DEBUG_OBJECT (ac3parse, "bad fscod=%d frmsizcod=%d", fscod, frmsizcod);
317     goto cleanup;
318   }
319
320   bsid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 5);
321   gst_bit_reader_skip_unchecked (&bits, 3);     /* bsmod */
322   acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);
323
324   /* spec not quite clear here: decoder should decode if less than 8,
325    * but seemingly only defines 6 and 8 cases */
326   /* Files with 9 and 10 happen, and seem to comply with the <= 8
327      format, so let them through. The spec says nothing about 9 and 10 */
328   if (bsid > 10) {
329     GST_DEBUG_OBJECT (ac3parse, "unexpected bsid=%d", bsid);
330     goto cleanup;
331   } else if (bsid != 8 && bsid != 6) {
332     GST_DEBUG_OBJECT (ac3parse, "undefined bsid=%d", bsid);
333   }
334
335   if ((acmod & 0x1) && (acmod != 0x1))  /* 3 front channels */
336     gst_bit_reader_skip_unchecked (&bits, 2);
337   if ((acmod & 0x4))            /* if a surround channel exists */
338     gst_bit_reader_skip_unchecked (&bits, 2);
339   if (acmod == 0x2)             /* if in 2/0 mode */
340     gst_bit_reader_skip_unchecked (&bits, 2);
341
342   lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);
343
344   /* 6/8->0, 9->1, 10->2,
345      see http://matroska.org/technical/specs/codecid/index.html */
346   rate_scale = (CLAMP (bsid, 8, 10) - 8);
347
348   if (frame_size)
349     *frame_size = frmsizcod_table[frmsizcod].frame_size[fscod] * 2;
350   if (rate)
351     *rate = fscod_rates[fscod] >> rate_scale;
352   if (chans)
353     *chans = acmod_chans[acmod] + lfe_on;
354   if (blks)
355     *blks = 6;
356   if (sid)
357     *sid = 0;
358
359   ret = TRUE;
360
361 cleanup:
362   gst_buffer_unmap (buf, &map);
363
364   return ret;
365 }
366
367 static gboolean
368 gst_ac3_parse_frame_header_eac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
369     gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
370     guint * sid)
371 {
372   GstBitReader bits;
373   GstMapInfo map;
374   guint16 frmsiz, sample_rate, blocks;
375   guint8 strmtyp, fscod, fscod2, acmod, lfe_on, strmid, numblkscod;
376   gboolean ret = FALSE;
377
378   GST_LOG_OBJECT (ac3parse, "parsing e-ac3");
379
380   gst_buffer_map (buf, &map, GST_MAP_READ);
381   gst_bit_reader_init (&bits, map.data, map.size);
382   gst_bit_reader_skip_unchecked (&bits, skip * 8);
383
384   gst_bit_reader_skip_unchecked (&bits, 16);
385   strmtyp = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* strmtyp     */
386   if (G_UNLIKELY (strmtyp == 3)) {
387     GST_DEBUG_OBJECT (ac3parse, "bad strmtyp %d", strmtyp);
388     goto cleanup;
389   }
390
391   strmid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);  /* substreamid */
392   frmsiz = gst_bit_reader_get_bits_uint16_unchecked (&bits, 11);        /* frmsiz      */
393   fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);   /* fscod       */
394   if (fscod == 3) {
395     fscod2 = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);        /* fscod2      */
396     if (G_UNLIKELY (fscod2 == 3)) {
397       GST_DEBUG_OBJECT (ac3parse, "invalid fscod2");
398       goto cleanup;
399     }
400     sample_rate = fscod_rates[fscod2] / 2;
401     blocks = 6;
402   } else {
403     numblkscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);    /* numblkscod  */
404     sample_rate = fscod_rates[fscod];
405     blocks = numblks[numblkscod];
406   }
407
408   acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);   /* acmod       */
409   lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);  /* lfeon       */
410
411   gst_bit_reader_skip_unchecked (&bits, 5);     /* bsid        */
412
413   if (frame_size)
414     *frame_size = (frmsiz + 1) * 2;
415   if (rate)
416     *rate = sample_rate;
417   if (chans)
418     *chans = acmod_chans[acmod] + lfe_on;
419   if (blks)
420     *blks = blocks;
421   if (sid)
422     *sid = (strmtyp & 0x1) << 3 | strmid;
423
424   ret = TRUE;
425
426 cleanup:
427   gst_buffer_unmap (buf, &map);
428
429   return ret;
430 }
431
432 static gboolean
433 gst_ac3_parse_frame_header (GstAc3Parse * parse, GstBuffer * buf, gint skip,
434     guint * framesize, guint * rate, guint * chans, guint * blocks,
435     guint * sid, gboolean * eac)
436 {
437   GstBitReader bits;
438   guint16 sync;
439   guint8 bsid;
440   GstMapInfo map;
441   gboolean ret = FALSE;
442
443   gst_buffer_map (buf, &map, GST_MAP_READ);
444   gst_bit_reader_init (&bits, map.data, map.size);
445
446   GST_MEMDUMP_OBJECT (parse, "AC3 frame sync", map.data, MIN (map.size, 16));
447
448   gst_bit_reader_skip_unchecked (&bits, skip * 8);
449
450   sync = gst_bit_reader_get_bits_uint16_unchecked (&bits, 16);
451   gst_bit_reader_skip_unchecked (&bits, 16 + 8);
452   bsid = gst_bit_reader_peek_bits_uint8_unchecked (&bits, 5);
453
454   if (G_UNLIKELY (sync != 0x0b77))
455     goto cleanup;
456
457   GST_LOG_OBJECT (parse, "bsid = %d", bsid);
458
459   if (bsid <= 10) {
460     if (eac)
461       *eac = FALSE;
462     ret = gst_ac3_parse_frame_header_ac3 (parse, buf, skip, framesize, rate,
463         chans, blocks, sid);
464     goto cleanup;
465   } else if (bsid <= 16) {
466     if (eac)
467       *eac = TRUE;
468     ret = gst_ac3_parse_frame_header_eac3 (parse, buf, skip, framesize, rate,
469         chans, blocks, sid);
470     goto cleanup;
471   } else {
472     GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
473     return FALSE;
474   }
475
476   GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
477
478 cleanup:
479   gst_buffer_unmap (buf, &map);
480
481   return ret;
482 }
483
484 static gboolean
485 gst_ac3_parse_check_valid_frame (GstBaseParse * parse,
486     GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
487 {
488   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
489   GstBuffer *buf = frame->buffer;
490   GstByteReader reader;
491   gint off;
492   gboolean lost_sync, draining, eac, more = FALSE;
493   guint frmsiz, blocks, sid;
494   gint have_blocks = 0;
495   GstMapInfo map;
496   gboolean ret = FALSE;
497
498   gst_buffer_map (buf, &map, GST_MAP_READ);
499
500   if (G_UNLIKELY (map.size < 6))
501     goto cleanup;
502
503   gst_byte_reader_init (&reader, map.data, map.size);
504   off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000, 0x0b770000,
505       0, map.size);
506
507   GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
508
509   /* didn't find anything that looks like a sync word, skip */
510   if (off < 0) {
511     *skipsize = map.size - 3;
512     goto cleanup;
513   }
514
515   /* possible frame header, but not at offset 0? skip bytes before sync */
516   if (off > 0) {
517     *skipsize = off;
518     goto cleanup;
519   }
520
521   /* make sure the values in the frame header look sane */
522   if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &frmsiz, NULL, NULL,
523           &blocks, &sid, &eac)) {
524     *skipsize = off + 2;
525     goto cleanup;
526   }
527
528   *framesize = frmsiz;
529
530   if (G_UNLIKELY (g_atomic_int_get (&ac3parse->align) ==
531           GST_AC3_PARSE_ALIGN_NONE))
532     gst_ac3_parse_set_alignment (ac3parse, eac);
533
534   GST_LOG_OBJECT (parse, "got frame");
535
536   lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
537   draining = GST_BASE_PARSE_DRAINING (parse);
538
539   if (g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937) {
540     /* We need 6 audio blocks from each substream, so we keep going forwards
541      * till we have it */
542
543     g_assert (blocks > 0);
544     GST_LOG_OBJECT (ac3parse, "Need %d frames before pushing", 6 / blocks);
545
546     if (sid != 0) {
547       /* We need the first substream to be the one with id 0 */
548       GST_LOG_OBJECT (ac3parse, "Skipping till we find sid 0");
549       *skipsize = off + 2;
550       goto cleanup;
551     }
552
553     *framesize = 0;
554
555     /* Loop till we have 6 blocks per substream */
556     for (have_blocks = 0; !more && have_blocks < 6; have_blocks += blocks) {
557       /* Loop till we get one frame from each substream */
558       do {
559         *framesize += frmsiz;
560
561         if (!gst_byte_reader_skip (&reader, frmsiz)
562             || map.size < (*framesize + 6)) {
563           more = TRUE;
564           break;
565         }
566
567         if (!gst_ac3_parse_frame_header (ac3parse, buf, *framesize, &frmsiz,
568                 NULL, NULL, NULL, &sid, &eac)) {
569           *skipsize = off + 2;
570           goto cleanup;
571         }
572       } while (sid);
573     }
574
575     /* We're now at the next frame, so no need to skip if resyncing */
576     frmsiz = 0;
577   }
578
579   if (lost_sync && !draining) {
580     guint16 word = 0;
581
582     GST_DEBUG_OBJECT (ac3parse, "resyncing; checking next frame syncword");
583
584     if (more || !gst_byte_reader_skip (&reader, frmsiz) ||
585         !gst_byte_reader_get_uint16_be (&reader, &word)) {
586       GST_DEBUG_OBJECT (ac3parse, "... but not sufficient data");
587       gst_base_parse_set_min_frame_size (parse, *framesize + 6);
588       *skipsize = 0;
589       goto cleanup;
590     } else {
591       if (word != 0x0b77) {
592         GST_DEBUG_OBJECT (ac3parse, "0x%x not OK", word);
593         *skipsize = off + 2;
594         goto cleanup;
595       } else {
596         /* ok, got sync now, let's assume constant frame size */
597         gst_base_parse_set_min_frame_size (parse, *framesize);
598       }
599     }
600   }
601
602   ret = TRUE;
603
604 cleanup:
605   gst_buffer_unmap (buf, &map);
606
607   return ret;
608 }
609
610 static GstFlowReturn
611 gst_ac3_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
612 {
613   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
614   GstBuffer *buf = frame->buffer;
615   guint fsize, rate, chans, blocks, sid;
616   gboolean eac, update_rate = FALSE;
617
618   if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &fsize, &rate, &chans,
619           &blocks, &sid, &eac))
620     goto broken_header;
621
622   GST_LOG_OBJECT (parse, "size: %u, blocks: %u, rate: %u, chans: %u", fsize,
623       blocks, rate, chans);
624
625   if (G_UNLIKELY (sid)) {
626     /* dependent frame, no need to (ac)count for or consider further */
627     GST_LOG_OBJECT (parse, "sid: %d", sid);
628     frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME;
629     /* TODO maybe also mark as DELTA_UNIT,
630      * if that does not surprise baseparse elsewhere */
631     /* occupies same time space as previous base frame */
632     if (G_LIKELY (GST_BUFFER_TIMESTAMP (buf) >= GST_BUFFER_DURATION (buf)))
633       GST_BUFFER_TIMESTAMP (buf) -= GST_BUFFER_DURATION (buf);
634     /* only return if we already arranged for caps */
635     if (G_LIKELY (ac3parse->sample_rate > 0))
636       return GST_FLOW_OK;
637   }
638
639   if (G_UNLIKELY (ac3parse->sample_rate != rate || ac3parse->channels != chans
640           || ac3parse->eac != eac)) {
641     GstCaps *caps = gst_caps_new_simple (eac ? "audio/x-eac3" : "audio/x-ac3",
642         "framed", G_TYPE_BOOLEAN, TRUE, "rate", G_TYPE_INT, rate,
643         "channels", G_TYPE_INT, chans, NULL);
644     gst_caps_set_simple (caps, "alignment", G_TYPE_STRING,
645         g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937 ?
646         "iec61937" : "frame", NULL);
647     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
648     gst_caps_unref (caps);
649
650     ac3parse->sample_rate = rate;
651     ac3parse->channels = chans;
652     ac3parse->eac = eac;
653
654     update_rate = TRUE;
655   }
656
657   if (G_UNLIKELY (ac3parse->blocks != blocks)) {
658     ac3parse->blocks = blocks;
659
660     update_rate = TRUE;
661   }
662
663   if (G_UNLIKELY (update_rate))
664     gst_base_parse_set_frame_rate (parse, rate, 256 * blocks, 2, 2);
665
666   return GST_FLOW_OK;
667
668 /* ERRORS */
669 broken_header:
670   {
671     /* this really shouldn't ever happen */
672     GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
673     return GST_FLOW_ERROR;
674   }
675 }
676
677 static gboolean
678 gst_ac3_parse_src_event (GstBaseParse * parse, GstEvent * event)
679 {
680   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
681
682   if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) &&
683       gst_event_has_name (event, "ac3parse-set-alignment")) {
684     const GstStructure *st = gst_event_get_structure (event);
685     const gchar *align = gst_structure_get_string (st, "alignment");
686
687     if (g_str_equal (align, "iec61937")) {
688       GST_DEBUG_OBJECT (ac3parse, "Switching to iec61937 alignment");
689       g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
690     } else if (g_str_equal (align, "frame")) {
691       GST_DEBUG_OBJECT (ac3parse, "Switching to frame alignment");
692       g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
693     } else {
694       g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
695       GST_WARNING_OBJECT (ac3parse, "Got unknown alignment request (%s) "
696           "reverting to frame alignment.",
697           gst_structure_get_string (st, "alignment"));
698     }
699
700     gst_event_unref (event);
701     return TRUE;
702   }
703
704   return GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
705 }
706
707 static GstCaps *
708 gst_ac3_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
709 {
710   GstCaps *peercaps;
711   GstCaps *res;
712
713   /* FIXME: handle filter */
714
715   peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
716   if (peercaps) {
717     guint i, n;
718
719     /* Remove the framed and alignment field. We can convert
720      * between different alignments. */
721     peercaps = gst_caps_make_writable (peercaps);
722     n = gst_caps_get_size (peercaps);
723     for (i = 0; i < n; i++) {
724       GstStructure *s = gst_caps_get_structure (peercaps, i);
725
726       gst_structure_remove_field (s, "framed");
727       gst_structure_remove_field (s, "alignment");
728     }
729
730     res =
731         gst_caps_intersect_full (peercaps,
732         gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)),
733         GST_CAPS_INTERSECT_FIRST);
734     gst_caps_unref (peercaps);
735   } else {
736     res =
737         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD
738             (parse)));
739   }
740
741   return res;
742 }