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