various: fix pad template leaks
[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_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   if (bsid > 8) {
325     GST_DEBUG_OBJECT (ac3parse, "unexpected bsid=%d", bsid);
326     return FALSE;
327   } else if (bsid != 8 && bsid != 6) {
328     GST_DEBUG_OBJECT (ac3parse, "undefined bsid=%d", bsid);
329   }
330
331   if ((acmod & 0x1) && (acmod != 0x1))  /* 3 front channels */
332     gst_bit_reader_skip_unchecked (&bits, 2);
333   if ((acmod & 0x4))            /* if a surround channel exists */
334     gst_bit_reader_skip_unchecked (&bits, 2);
335   if (acmod == 0x2)             /* if in 2/0 mode */
336     gst_bit_reader_skip_unchecked (&bits, 2);
337
338   lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);
339
340   /* 6/8->0, 9->1, 10->2,
341      see http://matroska.org/technical/specs/codecid/index.html */
342   rate_scale = (CLAMP (bsid, 8, 10) - 8);
343
344   if (frame_size)
345     *frame_size = frmsizcod_table[frmsizcod].frame_size[fscod] * 2;
346   if (rate)
347     *rate = fscod_rates[fscod] >> rate_scale;
348   if (chans)
349     *chans = acmod_chans[acmod] + lfe_on;
350   if (blks)
351     *blks = 6;
352   if (sid)
353     *sid = 0;
354
355   return TRUE;
356 }
357
358 static gboolean
359 gst_ac3_parse_frame_header_eac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
360     gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
361     guint * sid)
362 {
363   GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
364   guint16 frmsiz, sample_rate, blocks;
365   guint8 strmtyp, fscod, fscod2, acmod, lfe_on, strmid, numblkscod;
366
367   GST_LOG_OBJECT (ac3parse, "parsing e-ac3");
368
369   gst_bit_reader_skip_unchecked (&bits, skip * 8);
370
371   gst_bit_reader_skip_unchecked (&bits, 16);
372   strmtyp = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* strmtyp     */
373   if (G_UNLIKELY (strmtyp == 3)) {
374     GST_DEBUG_OBJECT (ac3parse, "bad strmtyp %d", strmtyp);
375     return FALSE;
376   }
377
378   strmid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);  /* substreamid */
379   frmsiz = gst_bit_reader_get_bits_uint16_unchecked (&bits, 11);        /* frmsiz      */
380   fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);   /* fscod       */
381   if (fscod == 3) {
382     fscod2 = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);        /* fscod2      */
383     if (G_UNLIKELY (fscod2 == 3)) {
384       GST_DEBUG_OBJECT (ac3parse, "invalid fscod2");
385       return FALSE;
386     }
387     sample_rate = fscod_rates[fscod2] / 2;
388     blocks = 6;
389   } else {
390     numblkscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);    /* numblkscod  */
391     sample_rate = fscod_rates[fscod];
392     blocks = numblks[numblkscod];
393   }
394
395   acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);   /* acmod       */
396   lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);  /* lfeon       */
397
398   gst_bit_reader_skip_unchecked (&bits, 5);     /* bsid        */
399
400   if (frame_size)
401     *frame_size = (frmsiz + 1) * 2;
402   if (rate)
403     *rate = sample_rate;
404   if (chans)
405     *chans = acmod_chans[acmod] + lfe_on;
406   if (blks)
407     *blks = blocks;
408   if (sid)
409     *sid = (strmtyp & 0x1) << 3 | strmid;
410
411   return TRUE;
412 }
413
414 static gboolean
415 gst_ac3_parse_frame_header (GstAc3Parse * parse, GstBuffer * buf, gint skip,
416     guint * framesize, guint * rate, guint * chans, guint * blocks,
417     guint * sid, gboolean * eac)
418 {
419   GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf);
420   guint16 sync;
421   guint8 bsid;
422
423   GST_MEMDUMP_OBJECT (parse, "AC3 frame sync", GST_BUFFER_DATA (buf), 16);
424
425   gst_bit_reader_skip_unchecked (&bits, skip * 8);
426
427   sync = gst_bit_reader_get_bits_uint16_unchecked (&bits, 16);
428   gst_bit_reader_skip_unchecked (&bits, 16 + 8);
429   bsid = gst_bit_reader_peek_bits_uint8_unchecked (&bits, 5);
430
431   if (G_UNLIKELY (sync != 0x0b77))
432     return FALSE;
433
434   GST_LOG_OBJECT (parse, "bsid = %d", bsid);
435
436   if (bsid <= 10) {
437     if (eac)
438       *eac = FALSE;
439     return gst_ac3_parse_frame_header_ac3 (parse, buf, skip, framesize, rate,
440         chans, blocks, sid);
441   } else if (bsid <= 16) {
442     if (eac)
443       *eac = TRUE;
444     return gst_ac3_parse_frame_header_eac3 (parse, buf, skip, framesize, rate,
445         chans, blocks, sid);
446   } else {
447     GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
448     return FALSE;
449   }
450 }
451
452 static gboolean
453 gst_ac3_parse_check_valid_frame (GstBaseParse * parse,
454     GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
455 {
456   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
457   GstBuffer *buf = frame->buffer;
458   GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buf);
459   gint off;
460   gboolean lost_sync, draining, eac, more = FALSE;
461   guint frmsiz, blocks, sid;
462   gint have_blocks = 0;
463
464   if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < 6))
465     return FALSE;
466
467   off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000, 0x0b770000,
468       0, GST_BUFFER_SIZE (buf));
469
470   GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
471
472   /* didn't find anything that looks like a sync word, skip */
473   if (off < 0) {
474     *skipsize = GST_BUFFER_SIZE (buf) - 3;
475     return FALSE;
476   }
477
478   /* possible frame header, but not at offset 0? skip bytes before sync */
479   if (off > 0) {
480     *skipsize = off;
481     return FALSE;
482   }
483
484   /* make sure the values in the frame header look sane */
485   if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &frmsiz, NULL, NULL,
486           &blocks, &sid, &eac)) {
487     *skipsize = off + 2;
488     return FALSE;
489   }
490
491   *framesize = frmsiz;
492
493   if (G_UNLIKELY (g_atomic_int_get (&ac3parse->align) ==
494           GST_AC3_PARSE_ALIGN_NONE))
495     gst_ac3_parse_set_alignment (ac3parse, eac);
496
497   GST_LOG_OBJECT (parse, "got frame");
498
499   lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
500   draining = GST_BASE_PARSE_DRAINING (parse);
501
502   if (g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937) {
503     /* We need 6 audio blocks from each substream, so we keep going forwards
504      * till we have it */
505
506     g_assert (blocks > 0);
507     GST_LOG_OBJECT (ac3parse, "Need %d frames before pushing", 6 / blocks);
508
509     if (sid != 0) {
510       /* We need the first substream to be the one with id 0 */
511       GST_LOG_OBJECT (ac3parse, "Skipping till we find sid 0");
512       *skipsize = off + 2;
513       return FALSE;
514     }
515
516     *framesize = 0;
517
518     /* Loop till we have 6 blocks per substream */
519     for (have_blocks = 0; !more && have_blocks < 6; have_blocks += blocks) {
520       /* Loop till we get one frame from each substream */
521       do {
522         *framesize += frmsiz;
523
524         if (!gst_byte_reader_skip (&reader, frmsiz) ||
525             GST_BUFFER_SIZE (buf) < (*framesize + 6)) {
526           more = TRUE;
527           break;
528         }
529
530         if (!gst_ac3_parse_frame_header (ac3parse, buf, *framesize, &frmsiz,
531                 NULL, NULL, NULL, &sid, &eac)) {
532           *skipsize = off + 2;
533           return FALSE;
534         }
535       } while (sid);
536     }
537
538     /* We're now at the next frame, so no need to skip if resyncing */
539     frmsiz = 0;
540   }
541
542   if (lost_sync && !draining) {
543     guint16 word = 0;
544
545     GST_DEBUG_OBJECT (ac3parse, "resyncing; checking next frame syncword");
546
547     if (more || !gst_byte_reader_skip (&reader, frmsiz) ||
548         !gst_byte_reader_get_uint16_be (&reader, &word)) {
549       GST_DEBUG_OBJECT (ac3parse, "... but not sufficient data");
550       gst_base_parse_set_min_frame_size (parse, *framesize + 6);
551       *skipsize = 0;
552       return FALSE;
553     } else {
554       if (word != 0x0b77) {
555         GST_DEBUG_OBJECT (ac3parse, "0x%x not OK", word);
556         *skipsize = off + 2;
557         return FALSE;
558       } else {
559         /* ok, got sync now, let's assume constant frame size */
560         gst_base_parse_set_min_frame_size (parse, *framesize);
561       }
562     }
563   }
564
565   return TRUE;
566 }
567
568 static GstFlowReturn
569 gst_ac3_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
570 {
571   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
572   GstBuffer *buf = frame->buffer;
573   guint fsize, rate, chans, blocks, sid;
574   gboolean eac, update_rate = FALSE;
575
576   if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &fsize, &rate, &chans,
577           &blocks, &sid, &eac))
578     goto broken_header;
579
580   GST_LOG_OBJECT (parse, "size: %u, blocks: %u, rate: %u, chans: %u", fsize,
581       blocks, rate, chans);
582
583   if (G_UNLIKELY (sid)) {
584     /* dependent frame, no need to (ac)count for or consider further */
585     GST_LOG_OBJECT (parse, "sid: %d", sid);
586     frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME;
587     /* TODO maybe also mark as DELTA_UNIT,
588      * if that does not surprise baseparse elsewhere */
589     /* occupies same time space as previous base frame */
590     if (G_LIKELY (GST_BUFFER_TIMESTAMP (buf) >= GST_BUFFER_DURATION (buf)))
591       GST_BUFFER_TIMESTAMP (buf) -= GST_BUFFER_DURATION (buf);
592     /* only return if we already arranged for caps */
593     if (G_LIKELY (ac3parse->sample_rate > 0))
594       return GST_FLOW_OK;
595   }
596
597   if (G_UNLIKELY (ac3parse->sample_rate != rate || ac3parse->channels != chans
598           || ac3parse->eac != eac)) {
599     GstCaps *caps = gst_caps_new_simple (eac ? "audio/x-eac3" : "audio/x-ac3",
600         "framed", G_TYPE_BOOLEAN, TRUE, "rate", G_TYPE_INT, rate,
601         "channels", G_TYPE_INT, chans, NULL);
602     gst_caps_set_simple (caps, "alignment", G_TYPE_STRING,
603         g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937 ?
604         "iec61937" : "frame", NULL);
605     gst_buffer_set_caps (buf, caps);
606     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
607     gst_caps_unref (caps);
608
609     ac3parse->sample_rate = rate;
610     ac3parse->channels = chans;
611     ac3parse->eac = eac;
612
613     update_rate = TRUE;
614   }
615
616   if (G_UNLIKELY (ac3parse->blocks != blocks)) {
617     ac3parse->blocks = blocks;
618
619     update_rate = TRUE;
620   }
621
622   if (G_UNLIKELY (update_rate))
623     gst_base_parse_set_frame_rate (parse, rate, 256 * blocks, 2, 2);
624
625   return GST_FLOW_OK;
626
627 /* ERRORS */
628 broken_header:
629   {
630     /* this really shouldn't ever happen */
631     GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
632     return GST_FLOW_ERROR;
633   }
634 }
635
636 static gboolean
637 gst_ac3_parse_src_event (GstBaseParse * parse, GstEvent * event)
638 {
639   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
640
641   if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) &&
642       gst_event_has_name (event, "ac3parse-set-alignment")) {
643     const GstStructure *st = gst_event_get_structure (event);
644     const gchar *align = gst_structure_get_string (st, "alignment");
645
646     if (g_str_equal (align, "iec61937")) {
647       GST_DEBUG_OBJECT (ac3parse, "Switching to iec61937 alignment");
648       g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
649     } else if (g_str_equal (align, "frame")) {
650       GST_DEBUG_OBJECT (ac3parse, "Switching to frame alignment");
651       g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
652     } else {
653       g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
654       GST_WARNING_OBJECT (ac3parse, "Got unknown alignment request (%s) "
655           "reverting to frame alignment.",
656           gst_structure_get_string (st, "alignment"));
657     }
658
659     gst_event_unref (event);
660     return TRUE;
661   }
662
663   return GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
664 }
665
666 static GstCaps *
667 gst_ac3_parse_get_sink_caps (GstBaseParse * parse)
668 {
669   GstCaps *peercaps;
670   GstCaps *res;
671
672   peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
673   if (peercaps) {
674     guint i, n;
675
676     /* Remove the framed and alignment field. We can convert
677      * between different alignments. */
678     peercaps = gst_caps_make_writable (peercaps);
679     n = gst_caps_get_size (peercaps);
680     for (i = 0; i < n; i++) {
681       GstStructure *s = gst_caps_get_structure (peercaps, i);
682
683       gst_structure_remove_field (s, "framed");
684       gst_structure_remove_field (s, "alignment");
685     }
686
687     res =
688         gst_caps_intersect_full (peercaps,
689         gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)),
690         GST_CAPS_INTERSECT_FIRST);
691     gst_caps_unref (peercaps);
692   } else {
693     res =
694         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD
695             (parse)));
696   }
697
698   return res;
699 }