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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, 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-1.0 filesrc location=abc.ac3 ! ac3parse ! a52dec ! audioresample ! audioconvert ! autoaudiosink
33  * ]|
34  * </refsect2>
35  */
36 
37 /* TODO:
38  *  - audio/ac3 to audio/x-private1-ac3 is not implemented (done in the muxer)
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/base.h>
50 #include <gst/pbutils/pbutils.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         "audio/x-private1-ac3"));
158 
159 static void gst_ac3_parse_finalize (GObject * object);
160 
161 static gboolean gst_ac3_parse_start (GstBaseParse * parse);
162 static gboolean gst_ac3_parse_stop (GstBaseParse * parse);
163 static GstFlowReturn gst_ac3_parse_handle_frame (GstBaseParse * parse,
164     GstBaseParseFrame * frame, gint * skipsize);
165 static GstFlowReturn gst_ac3_parse_pre_push_frame (GstBaseParse * parse,
166     GstBaseParseFrame * frame);
167 static gboolean gst_ac3_parse_src_event (GstBaseParse * parse,
168     GstEvent * event);
169 static GstCaps *gst_ac3_parse_get_sink_caps (GstBaseParse * parse,
170     GstCaps * filter);
171 static gboolean gst_ac3_parse_set_sink_caps (GstBaseParse * parse,
172     GstCaps * caps);
173 
174 #define gst_ac3_parse_parent_class parent_class
175 G_DEFINE_TYPE (GstAc3Parse, gst_ac3_parse, GST_TYPE_BASE_PARSE);
176 
177 static void
gst_ac3_parse_class_init(GstAc3ParseClass * klass)178 gst_ac3_parse_class_init (GstAc3ParseClass * klass)
179 {
180   GObjectClass *object_class = G_OBJECT_CLASS (klass);
181   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
182   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
183 
184   GST_DEBUG_CATEGORY_INIT (ac3_parse_debug, "ac3parse", 0,
185       "AC3 audio stream parser");
186 
187   object_class->finalize = gst_ac3_parse_finalize;
188 
189   gst_element_class_add_static_pad_template (element_class, &sink_template);
190   gst_element_class_add_static_pad_template (element_class, &src_template);
191 
192   gst_element_class_set_static_metadata (element_class,
193       "AC3 audio stream parser", "Codec/Parser/Converter/Audio",
194       "AC3 parser", "Tim-Philipp Müller <tim centricular net>");
195 
196   parse_class->start = GST_DEBUG_FUNCPTR (gst_ac3_parse_start);
197   parse_class->stop = GST_DEBUG_FUNCPTR (gst_ac3_parse_stop);
198   parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_ac3_parse_handle_frame);
199   parse_class->pre_push_frame =
200       GST_DEBUG_FUNCPTR (gst_ac3_parse_pre_push_frame);
201   parse_class->src_event = GST_DEBUG_FUNCPTR (gst_ac3_parse_src_event);
202   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_ac3_parse_get_sink_caps);
203   parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_ac3_parse_set_sink_caps);
204 }
205 
206 static void
gst_ac3_parse_reset(GstAc3Parse * ac3parse)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   ac3parse->sent_codec_tag = FALSE;
214   g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_NONE);
215 }
216 
217 static void
gst_ac3_parse_init(GstAc3Parse * ac3parse)218 gst_ac3_parse_init (GstAc3Parse * ac3parse)
219 {
220   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (ac3parse), 8);
221   gst_ac3_parse_reset (ac3parse);
222   ac3parse->baseparse_chainfunc =
223       GST_BASE_PARSE_SINK_PAD (GST_BASE_PARSE (ac3parse))->chainfunc;
224   GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (ac3parse));
225   GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (ac3parse));
226 }
227 
228 static void
gst_ac3_parse_finalize(GObject * object)229 gst_ac3_parse_finalize (GObject * object)
230 {
231   G_OBJECT_CLASS (parent_class)->finalize (object);
232 }
233 
234 static gboolean
gst_ac3_parse_start(GstBaseParse * parse)235 gst_ac3_parse_start (GstBaseParse * parse)
236 {
237   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
238 
239   GST_DEBUG_OBJECT (parse, "starting");
240 
241   gst_ac3_parse_reset (ac3parse);
242 
243   return TRUE;
244 }
245 
246 static gboolean
gst_ac3_parse_stop(GstBaseParse * parse)247 gst_ac3_parse_stop (GstBaseParse * parse)
248 {
249   GST_DEBUG_OBJECT (parse, "stopping");
250 
251   return TRUE;
252 }
253 
254 static void
gst_ac3_parse_set_alignment(GstAc3Parse * ac3parse,gboolean eac)255 gst_ac3_parse_set_alignment (GstAc3Parse * ac3parse, gboolean eac)
256 {
257   GstCaps *caps;
258   GstStructure *st;
259   const gchar *str = NULL;
260   int i;
261 
262   if (G_LIKELY (!eac))
263     goto done;
264 
265   caps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (ac3parse));
266 
267   if (!caps)
268     goto done;
269 
270   for (i = 0; i < gst_caps_get_size (caps); i++) {
271     st = gst_caps_get_structure (caps, i);
272 
273     if (!g_str_equal (gst_structure_get_name (st), "audio/x-eac3"))
274       continue;
275 
276     if ((str = gst_structure_get_string (st, "alignment"))) {
277       if (g_str_equal (str, "iec61937")) {
278         g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
279         GST_DEBUG_OBJECT (ac3parse, "picked iec61937 alignment");
280       } else if (g_str_equal (str, "frame") == 0) {
281         g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
282         GST_DEBUG_OBJECT (ac3parse, "picked frame alignment");
283       } else {
284         g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
285         GST_WARNING_OBJECT (ac3parse, "unknown alignment: %s", str);
286       }
287       break;
288     }
289   }
290 
291   if (caps)
292     gst_caps_unref (caps);
293 
294 done:
295   /* default */
296   if (ac3parse->align == GST_AC3_PARSE_ALIGN_NONE) {
297     g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
298     GST_DEBUG_OBJECT (ac3parse, "picked syncframe alignment");
299   }
300 }
301 
302 static gboolean
gst_ac3_parse_frame_header_ac3(GstAc3Parse * ac3parse,GstBuffer * buf,gint skip,guint * frame_size,guint * rate,guint * chans,guint * blks,guint * sid)303 gst_ac3_parse_frame_header_ac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
304     gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
305     guint * sid)
306 {
307   GstBitReader bits;
308   GstMapInfo map;
309   guint8 fscod, frmsizcod, bsid, acmod, lfe_on, rate_scale;
310   gboolean ret = FALSE;
311 
312   GST_LOG_OBJECT (ac3parse, "parsing ac3");
313 
314   gst_buffer_map (buf, &map, GST_MAP_READ);
315   gst_bit_reader_init (&bits, map.data, map.size);
316   gst_bit_reader_skip_unchecked (&bits, skip * 8);
317 
318   gst_bit_reader_skip_unchecked (&bits, 16 + 16);
319   fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);
320   frmsizcod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 6);
321 
322   if (G_UNLIKELY (fscod == 3 || frmsizcod >= G_N_ELEMENTS (frmsizcod_table))) {
323     GST_DEBUG_OBJECT (ac3parse, "bad fscod=%d frmsizcod=%d", fscod, frmsizcod);
324     goto cleanup;
325   }
326 
327   bsid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 5);
328   gst_bit_reader_skip_unchecked (&bits, 3);     /* bsmod */
329   acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);
330 
331   /* spec not quite clear here: decoder should decode if less than 8,
332    * but seemingly only defines 6 and 8 cases */
333   /* Files with 9 and 10 happen, and seem to comply with the <= 8
334      format, so let them through. The spec says nothing about 9 and 10 */
335   if (bsid > 10) {
336     GST_DEBUG_OBJECT (ac3parse, "unexpected bsid=%d", bsid);
337     goto cleanup;
338   } else if (bsid != 8 && bsid != 6) {
339     GST_DEBUG_OBJECT (ac3parse, "undefined bsid=%d", bsid);
340   }
341 
342   if ((acmod & 0x1) && (acmod != 0x1))  /* 3 front channels */
343     gst_bit_reader_skip_unchecked (&bits, 2);
344   if ((acmod & 0x4))            /* if a surround channel exists */
345     gst_bit_reader_skip_unchecked (&bits, 2);
346   if (acmod == 0x2)             /* if in 2/0 mode */
347     gst_bit_reader_skip_unchecked (&bits, 2);
348 
349   lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);
350 
351   /* 6/8->0, 9->1, 10->2,
352      see http://matroska.org/technical/specs/codecid/index.html */
353   rate_scale = (CLAMP (bsid, 8, 10) - 8);
354 
355   if (frame_size)
356     *frame_size = frmsizcod_table[frmsizcod].frame_size[fscod] * 2;
357   if (rate)
358     *rate = fscod_rates[fscod] >> rate_scale;
359   if (chans)
360     *chans = acmod_chans[acmod] + lfe_on;
361   if (blks)
362     *blks = 6;
363   if (sid)
364     *sid = 0;
365 
366   ret = TRUE;
367 
368 cleanup:
369   gst_buffer_unmap (buf, &map);
370 
371   return ret;
372 }
373 
374 static gboolean
gst_ac3_parse_frame_header_eac3(GstAc3Parse * ac3parse,GstBuffer * buf,gint skip,guint * frame_size,guint * rate,guint * chans,guint * blks,guint * sid)375 gst_ac3_parse_frame_header_eac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
376     gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
377     guint * sid)
378 {
379   GstBitReader bits;
380   GstMapInfo map;
381   guint16 frmsiz, sample_rate, blocks;
382   guint8 strmtyp, fscod, fscod2, acmod, lfe_on, strmid, numblkscod;
383   gboolean ret = FALSE;
384 
385   GST_LOG_OBJECT (ac3parse, "parsing e-ac3");
386 
387   gst_buffer_map (buf, &map, GST_MAP_READ);
388   gst_bit_reader_init (&bits, map.data, map.size);
389   gst_bit_reader_skip_unchecked (&bits, skip * 8);
390 
391   gst_bit_reader_skip_unchecked (&bits, 16);
392   strmtyp = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* strmtyp     */
393   if (G_UNLIKELY (strmtyp == 3)) {
394     GST_DEBUG_OBJECT (ac3parse, "bad strmtyp %d", strmtyp);
395     goto cleanup;
396   }
397 
398   strmid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);  /* substreamid */
399   frmsiz = gst_bit_reader_get_bits_uint16_unchecked (&bits, 11);        /* frmsiz      */
400   fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);   /* fscod       */
401   if (fscod == 3) {
402     fscod2 = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);        /* fscod2      */
403     if (G_UNLIKELY (fscod2 == 3)) {
404       GST_DEBUG_OBJECT (ac3parse, "invalid fscod2");
405       goto cleanup;
406     }
407     sample_rate = fscod_rates[fscod2] / 2;
408     blocks = 6;
409   } else {
410     numblkscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2);    /* numblkscod  */
411     sample_rate = fscod_rates[fscod];
412     blocks = numblks[numblkscod];
413   }
414 
415   acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);   /* acmod       */
416   lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);  /* lfeon       */
417 
418   gst_bit_reader_skip_unchecked (&bits, 5);     /* bsid        */
419 
420   if (frame_size)
421     *frame_size = (frmsiz + 1) * 2;
422   if (rate)
423     *rate = sample_rate;
424   if (chans)
425     *chans = acmod_chans[acmod] + lfe_on;
426   if (blks)
427     *blks = blocks;
428   if (sid)
429     *sid = (strmtyp & 0x1) << 3 | strmid;
430 
431   ret = TRUE;
432 
433 cleanup:
434   gst_buffer_unmap (buf, &map);
435 
436   return ret;
437 }
438 
439 static gboolean
gst_ac3_parse_frame_header(GstAc3Parse * parse,GstBuffer * buf,gint skip,guint * framesize,guint * rate,guint * chans,guint * blocks,guint * sid,gboolean * eac)440 gst_ac3_parse_frame_header (GstAc3Parse * parse, GstBuffer * buf, gint skip,
441     guint * framesize, guint * rate, guint * chans, guint * blocks,
442     guint * sid, gboolean * eac)
443 {
444   GstBitReader bits;
445   guint16 sync;
446   guint8 bsid;
447   GstMapInfo map;
448   gboolean ret = FALSE;
449 
450   gst_buffer_map (buf, &map, GST_MAP_READ);
451   gst_bit_reader_init (&bits, map.data, map.size);
452 
453   GST_MEMDUMP_OBJECT (parse, "AC3 frame sync", map.data, MIN (map.size, 16));
454 
455   gst_bit_reader_skip_unchecked (&bits, skip * 8);
456 
457   sync = gst_bit_reader_get_bits_uint16_unchecked (&bits, 16);
458   gst_bit_reader_skip_unchecked (&bits, 16 + 8);
459   bsid = gst_bit_reader_peek_bits_uint8_unchecked (&bits, 5);
460 
461   if (G_UNLIKELY (sync != 0x0b77))
462     goto cleanup;
463 
464   GST_LOG_OBJECT (parse, "bsid = %d", bsid);
465 
466   if (bsid <= 10) {
467     if (eac)
468       *eac = FALSE;
469     ret = gst_ac3_parse_frame_header_ac3 (parse, buf, skip, framesize, rate,
470         chans, blocks, sid);
471     goto cleanup;
472   } else if (bsid <= 16) {
473     if (eac)
474       *eac = TRUE;
475     ret = gst_ac3_parse_frame_header_eac3 (parse, buf, skip, framesize, rate,
476         chans, blocks, sid);
477     goto cleanup;
478   } else {
479     GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
480     ret = FALSE;
481     goto cleanup;
482   }
483 
484   GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
485 
486 cleanup:
487   gst_buffer_unmap (buf, &map);
488 
489   return ret;
490 }
491 
492 static GstFlowReturn
gst_ac3_parse_handle_frame(GstBaseParse * parse,GstBaseParseFrame * frame,gint * skipsize)493 gst_ac3_parse_handle_frame (GstBaseParse * parse,
494     GstBaseParseFrame * frame, gint * skipsize)
495 {
496   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
497   GstBuffer *buf = frame->buffer;
498   GstByteReader reader;
499   gint off;
500   gboolean lost_sync, draining, eac, more = FALSE;
501   guint frmsiz, blocks, sid;
502   guint rate, chans;
503   gboolean update_rate = FALSE;
504   gint framesize = 0;
505   gint have_blocks = 0;
506   GstMapInfo map;
507   gboolean ret = FALSE;
508   GstFlowReturn res = GST_FLOW_OK;
509 
510   gst_buffer_map (buf, &map, GST_MAP_READ);
511 
512   if (G_UNLIKELY (map.size < 8)) {
513     *skipsize = 1;
514     goto cleanup;
515   }
516 
517   gst_byte_reader_init (&reader, map.data, map.size);
518   off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000, 0x0b770000,
519       0, map.size);
520 
521   GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
522 
523   /* didn't find anything that looks like a sync word, skip */
524   if (off < 0) {
525     *skipsize = map.size - 3;
526     goto cleanup;
527   }
528 
529   /* possible frame header, but not at offset 0? skip bytes before sync */
530   if (off > 0) {
531     *skipsize = off;
532     goto cleanup;
533   }
534 
535   /* make sure the values in the frame header look sane */
536   if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &frmsiz, &rate, &chans,
537           &blocks, &sid, &eac)) {
538     *skipsize = off + 2;
539     goto cleanup;
540   }
541 
542   GST_LOG_OBJECT (parse, "size: %u, blocks: %u, rate: %u, chans: %u", frmsiz,
543       blocks, rate, chans);
544 
545   framesize = frmsiz;
546 
547   if (G_UNLIKELY (g_atomic_int_get (&ac3parse->align) ==
548           GST_AC3_PARSE_ALIGN_NONE))
549     gst_ac3_parse_set_alignment (ac3parse, eac);
550 
551   GST_LOG_OBJECT (parse, "got frame");
552 
553   lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
554   draining = GST_BASE_PARSE_DRAINING (parse);
555 
556   if (g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937) {
557     /* We need 6 audio blocks from each substream, so we keep going forwards
558      * till we have it */
559 
560     g_assert (blocks > 0);
561     GST_LOG_OBJECT (ac3parse, "Need %d frames before pushing", 6 / blocks);
562 
563     if (sid != 0) {
564       /* We need the first substream to be the one with id 0 */
565       GST_LOG_OBJECT (ac3parse, "Skipping till we find sid 0");
566       *skipsize = off + 2;
567       goto cleanup;
568     }
569 
570     framesize = 0;
571 
572     /* Loop till we have 6 blocks per substream */
573     for (have_blocks = 0; !more && have_blocks < 6; have_blocks += blocks) {
574       /* Loop till we get one frame from each substream */
575       do {
576         framesize += frmsiz;
577 
578         if (!gst_byte_reader_skip (&reader, frmsiz)
579             || map.size < (framesize + 6)) {
580           more = TRUE;
581           break;
582         }
583 
584         if (!gst_ac3_parse_frame_header (ac3parse, buf, framesize, &frmsiz,
585                 NULL, NULL, NULL, &sid, &eac)) {
586           *skipsize = off + 2;
587           goto cleanup;
588         }
589       } while (sid);
590     }
591 
592     /* We're now at the next frame, so no need to skip if resyncing */
593     frmsiz = 0;
594   }
595 
596   if (lost_sync && !draining) {
597     guint16 word = 0;
598 
599     GST_DEBUG_OBJECT (ac3parse, "resyncing; checking next frame syncword");
600 
601     if (more || !gst_byte_reader_skip (&reader, frmsiz) ||
602         !gst_byte_reader_get_uint16_be (&reader, &word)) {
603       GST_DEBUG_OBJECT (ac3parse, "... but not sufficient data");
604       gst_base_parse_set_min_frame_size (parse, framesize + 8);
605       *skipsize = 0;
606       goto cleanup;
607     } else {
608       if (word != 0x0b77) {
609         GST_DEBUG_OBJECT (ac3parse, "0x%x not OK", word);
610         *skipsize = off + 2;
611         goto cleanup;
612       } else {
613         /* ok, got sync now, let's assume constant frame size */
614         gst_base_parse_set_min_frame_size (parse, framesize);
615       }
616     }
617   }
618 
619   /* expect to have found a frame here */
620   g_assert (framesize);
621   ret = TRUE;
622 
623   /* arrange for metadata setup */
624   if (G_UNLIKELY (sid)) {
625     /* dependent frame, no need to (ac)count for or consider further */
626     GST_LOG_OBJECT (parse, "sid: %d", sid);
627     frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME;
628     /* TODO maybe also mark as DELTA_UNIT,
629      * if that does not surprise baseparse elsewhere */
630     /* occupies same time space as previous base frame */
631     if (G_LIKELY (GST_BUFFER_TIMESTAMP (buf) >= GST_BUFFER_DURATION (buf)))
632       GST_BUFFER_TIMESTAMP (buf) -= GST_BUFFER_DURATION (buf);
633     /* only shortcut if we already arranged for caps */
634     if (G_LIKELY (ac3parse->sample_rate > 0))
635       goto cleanup;
636   }
637 
638   if (G_UNLIKELY (ac3parse->sample_rate != rate || ac3parse->channels != chans
639           || ac3parse->eac != eac)) {
640     GstCaps *caps = gst_caps_new_simple (eac ? "audio/x-eac3" : "audio/x-ac3",
641         "framed", G_TYPE_BOOLEAN, TRUE, "rate", G_TYPE_INT, rate,
642         "channels", G_TYPE_INT, chans, NULL);
643     gst_caps_set_simple (caps, "alignment", G_TYPE_STRING,
644         g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937 ?
645         "iec61937" : "frame", NULL);
646     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
647     gst_caps_unref (caps);
648 
649     ac3parse->sample_rate = rate;
650     ac3parse->channels = chans;
651     ac3parse->eac = eac;
652 
653     update_rate = TRUE;
654   }
655 
656   if (G_UNLIKELY (ac3parse->blocks != blocks)) {
657     ac3parse->blocks = blocks;
658 
659     update_rate = TRUE;
660   }
661 
662   if (G_UNLIKELY (update_rate))
663     gst_base_parse_set_frame_rate (parse, rate, 256 * blocks, 2, 2);
664 
665 cleanup:
666   gst_buffer_unmap (buf, &map);
667 
668   if (ret && framesize <= map.size) {
669     res = gst_base_parse_finish_frame (parse, frame, framesize);
670   }
671 
672   return res;
673 }
674 
675 
676 /*
677  * MPEG-PS private1 streams add a 2 bytes "Audio Substream Headers" for each
678  * buffer (not each frame) with the offset of the next frame's start.
679  *
680  * Buffer 1:
681  * -------------------------------------------
682  * |firstAccUnit|AC3SyncWord|xxxxxxxxxxxxxxxxx
683  * -------------------------------------------
684  * Buffer 2:
685  * -------------------------------------------
686  * |firstAccUnit|xxxxxx|AC3SyncWord|xxxxxxxxxx
687  * -------------------------------------------
688  *
689  * These 2 bytes can be dropped safely as they do not include any timing
690  * information, only the offset to the start of the next frame.
691  *
692  * From http://stnsoft.com/DVD/ass-hdr.html:
693  * "FirstAccUnit offset to frame which corresponds to PTS value offset 0 is the
694  * last byte of FirstAccUnit, ie add the offset of byte 2 to get the AU's offset
695  * The value 0000 indicates there is no first access unit"
696  * */
697 
698 static GstFlowReturn
gst_ac3_parse_chain_priv(GstPad * pad,GstObject * parent,GstBuffer * buf)699 gst_ac3_parse_chain_priv (GstPad * pad, GstObject * parent, GstBuffer * buf)
700 {
701   GstAc3Parse *ac3parse = GST_AC3_PARSE (parent);
702   GstFlowReturn ret;
703   gsize size;
704   guint8 data[2];
705   gint offset;
706   gint len;
707   GstBuffer *subbuf;
708   gint first_access;
709 
710   size = gst_buffer_get_size (buf);
711   if (size < 2)
712     goto not_enough_data;
713 
714   gst_buffer_extract (buf, 0, data, 2);
715   first_access = (data[0] << 8) | data[1];
716 
717   /* Skip the first_access header */
718   offset = 2;
719 
720   if (first_access > 1) {
721     /* Length of data before first_access */
722     len = first_access - 1;
723 
724     if (len <= 0 || offset + len > size)
725       goto bad_first_access_parameter;
726 
727     subbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset, len);
728     GST_BUFFER_DTS (subbuf) = GST_CLOCK_TIME_NONE;
729     GST_BUFFER_PTS (subbuf) = GST_CLOCK_TIME_NONE;
730     ret = ac3parse->baseparse_chainfunc (pad, parent, subbuf);
731     if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED) {
732       gst_buffer_unref (buf);
733       goto done;
734     }
735 
736     offset += len;
737     len = size - offset;
738 
739     if (len > 0) {
740       subbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset, len);
741       GST_BUFFER_PTS (subbuf) = GST_BUFFER_PTS (buf);
742       GST_BUFFER_DTS (subbuf) = GST_BUFFER_DTS (buf);
743 
744       ret = ac3parse->baseparse_chainfunc (pad, parent, subbuf);
745     }
746     gst_buffer_unref (buf);
747   } else {
748     /* first_access = 0 or 1, so if there's a timestamp it applies to the first byte */
749     subbuf =
750         gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset,
751         size - offset);
752     GST_BUFFER_PTS (subbuf) = GST_BUFFER_PTS (buf);
753     GST_BUFFER_DTS (subbuf) = GST_BUFFER_DTS (buf);
754     gst_buffer_unref (buf);
755     ret = ac3parse->baseparse_chainfunc (pad, parent, subbuf);
756   }
757 
758 done:
759   return ret;
760 
761 /* ERRORS */
762 not_enough_data:
763   {
764     GST_ELEMENT_ERROR (GST_ELEMENT (ac3parse), STREAM, FORMAT, (NULL),
765         ("Insufficient data in buffer. Can't determine first_acess"));
766     gst_buffer_unref (buf);
767     return GST_FLOW_ERROR;
768   }
769 bad_first_access_parameter:
770   {
771     GST_ELEMENT_ERROR (GST_ELEMENT (ac3parse), STREAM, FORMAT, (NULL),
772         ("Bad first_access parameter (%d) in buffer", first_access));
773     gst_buffer_unref (buf);
774     return GST_FLOW_ERROR;
775   }
776 }
777 
778 static GstFlowReturn
gst_ac3_parse_pre_push_frame(GstBaseParse * parse,GstBaseParseFrame * frame)779 gst_ac3_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
780 {
781   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
782 
783   if (!ac3parse->sent_codec_tag) {
784     GstTagList *taglist;
785     GstCaps *caps;
786 
787     /* codec tag */
788     caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
789     if (G_UNLIKELY (caps == NULL)) {
790       if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
791         GST_INFO_OBJECT (parse, "Src pad is flushing");
792         return GST_FLOW_FLUSHING;
793       } else {
794         GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
795         return GST_FLOW_NOT_NEGOTIATED;
796       }
797     }
798 
799     taglist = gst_tag_list_new_empty ();
800     gst_pb_utils_add_codec_description_to_tag_list (taglist,
801         GST_TAG_AUDIO_CODEC, caps);
802     gst_caps_unref (caps);
803 
804     gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
805     gst_tag_list_unref (taglist);
806 
807     /* also signals the end of first-frame processing */
808     ac3parse->sent_codec_tag = TRUE;
809   }
810 
811   return GST_FLOW_OK;
812 }
813 
814 static gboolean
gst_ac3_parse_src_event(GstBaseParse * parse,GstEvent * event)815 gst_ac3_parse_src_event (GstBaseParse * parse, GstEvent * event)
816 {
817   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
818 
819   if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) &&
820       gst_event_has_name (event, "ac3parse-set-alignment")) {
821     const GstStructure *st = gst_event_get_structure (event);
822     const gchar *align = gst_structure_get_string (st, "alignment");
823 
824     if (g_str_equal (align, "iec61937")) {
825       GST_DEBUG_OBJECT (ac3parse, "Switching to iec61937 alignment");
826       g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
827     } else if (g_str_equal (align, "frame")) {
828       GST_DEBUG_OBJECT (ac3parse, "Switching to frame alignment");
829       g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
830     } else {
831       g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
832       GST_WARNING_OBJECT (ac3parse, "Got unknown alignment request (%s) "
833           "reverting to frame alignment.",
834           gst_structure_get_string (st, "alignment"));
835     }
836 
837     gst_event_unref (event);
838     return TRUE;
839   }
840 
841   return GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
842 }
843 
844 static void
remove_fields(GstCaps * caps)845 remove_fields (GstCaps * caps)
846 {
847   guint i, n;
848 
849   n = gst_caps_get_size (caps);
850   for (i = 0; i < n; i++) {
851     GstStructure *s = gst_caps_get_structure (caps, i);
852 
853     gst_structure_remove_field (s, "framed");
854     gst_structure_remove_field (s, "alignment");
855   }
856 }
857 
858 static GstCaps *
extend_caps(GstCaps * caps,gboolean add_private)859 extend_caps (GstCaps * caps, gboolean add_private)
860 {
861   guint i, n;
862   GstCaps *ncaps = gst_caps_new_empty ();
863 
864   n = gst_caps_get_size (caps);
865   for (i = 0; i < n; i++) {
866     GstStructure *s = gst_caps_get_structure (caps, i);
867 
868     if (add_private && !gst_structure_has_name (s, "audio/x-private1-ac3")) {
869       GstStructure *ns = gst_structure_copy (s);
870       gst_structure_set_name (ns, "audio/x-private1-ac3");
871       gst_caps_append_structure (ncaps, ns);
872     } else if (!add_private &&
873         gst_structure_has_name (s, "audio/x-private1-ac3")) {
874       GstStructure *ns = gst_structure_copy (s);
875       gst_structure_set_name (ns, "audio/x-ac3");
876       gst_caps_append_structure (ncaps, ns);
877       ns = gst_structure_copy (s);
878       gst_structure_set_name (ns, "audio/x-eac3");
879       gst_caps_append_structure (ncaps, ns);
880     } else if (!add_private) {
881       gst_caps_append_structure (ncaps, gst_structure_copy (s));
882     }
883   }
884 
885   if (add_private) {
886     gst_caps_append (caps, ncaps);
887   } else {
888     gst_caps_unref (caps);
889     caps = ncaps;
890   }
891 
892   return caps;
893 }
894 
895 static GstCaps *
gst_ac3_parse_get_sink_caps(GstBaseParse * parse,GstCaps * filter)896 gst_ac3_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
897 {
898   GstCaps *peercaps, *templ;
899   GstCaps *res;
900 
901   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
902   if (filter) {
903     GstCaps *fcopy = gst_caps_copy (filter);
904     /* Remove the fields we convert */
905     remove_fields (fcopy);
906     /* we do not ask downstream to handle x-private1-ac3 */
907     fcopy = extend_caps (fcopy, FALSE);
908     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
909     gst_caps_unref (fcopy);
910   } else
911     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
912 
913   if (peercaps) {
914     /* Remove the framed and alignment field. We can convert
915      * between different alignments. */
916     peercaps = gst_caps_make_writable (peercaps);
917     remove_fields (peercaps);
918     /* also allow for x-private1-ac3 input */
919     peercaps = extend_caps (peercaps, TRUE);
920 
921     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
922     gst_caps_unref (peercaps);
923     gst_caps_unref (templ);
924   } else {
925     res = templ;
926   }
927 
928   if (filter) {
929     GstCaps *intersection;
930 
931     intersection =
932         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
933     gst_caps_unref (res);
934     res = intersection;
935   }
936 
937   return res;
938 }
939 
940 static gboolean
gst_ac3_parse_set_sink_caps(GstBaseParse * parse,GstCaps * caps)941 gst_ac3_parse_set_sink_caps (GstBaseParse * parse, GstCaps * caps)
942 {
943   GstStructure *s;
944   GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
945 
946   s = gst_caps_get_structure (caps, 0);
947   if (gst_structure_has_name (s, "audio/x-private1-ac3")) {
948     gst_pad_set_chain_function (parse->sinkpad, gst_ac3_parse_chain_priv);
949   } else {
950     gst_pad_set_chain_function (parse->sinkpad, ac3parse->baseparse_chainfunc);
951   }
952   return TRUE;
953 }
954