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