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