Debian Codegraph @

gst/audioparsers/gstac3parse.c

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