xine-lib 1.2.13-20230125hg15249
latm.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-2018 the xine project
3 *
4 * This file is part of xine, a free video player.
5 *
6 * xine is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * xine is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 *
20 * AAC LATM parser and demuxer by Torsten Jager <t.jager@gmx.de>
21 * Limitations:
22 * - LATM v0 and v1 only
23 * - 1 program / 1 layer of the possible 16 / 8 (DVB)
24 *
25 */
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
31#include <sys/types.h>
32#include <string.h>
33#include <stdlib.h>
34
35#define LOG_MODULE "latm"
36#define LOG_VERBOSE
37/*
38#define LOG
39*/
40
41#if (defined (__GNUC__) && (__GNUC__ >= 4)) || defined (__clang__)
42# if defined (__i386) || defined (__i386__)
43# define BEBF_X86_32_ASM
44# endif
45# define bebf_UNUSED __attribute__((unused))
46# ifdef WORDS_BIGENDIAN
47# define bebf_ADJ32(v) (v)
48# else
49# define bebf_ADJ32(v) ((uint32_t)__builtin_bswap32 ((int32_t)(v)))
50# endif
51#else
52# define bebf_UNUSED
53# ifdef WORDS_BIGENDIAN
54# define bebf_ADJ32(v) (v)
55# else
56# define bebf_ADJ32(v) (((v) >> 24) | (((v) & 0xff0000) >> 8) | (((v) & 0xff00) << 8) | ((v) << 24))
57# endif
58#endif
59
60/*************************************************************************
61* Big Endian BitFile helper *
62*************************************************************************/
63
64typedef struct {
65 uint64_t readcache; /* left justified */
66 const uint32_t *readptr, *readstop;
67 const uint8_t *readstart;
68 int32_t readbits, writebits; /* # of invalid bits in cache */
69 uint64_t writecache;
70 uint32_t *writeptr;
71 uint8_t *writestart;
72} bebf_t;
73
79static void bebf_UNUSED bebf_seek (bebf_t *bebf, uint32_t nbits) {
80 const uint32_t *p = (const uint32_t *)(((uintptr_t)bebf->readstart + (nbits >> 3)) & ~(uintptr_t)3);
81 uint32_t n = (int32_t)nbits - (((const uint8_t *)p - bebf->readstart) << 3);
82 bebf->readbits = n;
83 if (p < bebf->readstop) {
84 uint64_t b = bebf_ADJ32 (p[0]);
85 b <<= 32;
86 b |= bebf_ADJ32 (p[1]);
87 bebf->readcache = b << n;
88 bebf->readptr = p + 2;
89 } else {
90 bebf->readcache = 0;
91 bebf->readptr = bebf->readstop;
92 }
93}
94
99static void bebf_UNUSED bebf_align (bebf_t *bebf) {
100 uint32_t n = (64 - bebf->readbits) & 7;
101 if (n) {
102 bebf->readcache <<= n;
103 bebf->readbits += n;
104 }
105}
106
113static void bebf_UNUSED bebf_set_read (bebf_t *bebf, const uint8_t *rp, uint32_t nbytes) {
114 bebf->readstart = rp;
115 bebf->readstop = (const uint32_t *)(((uintptr_t)rp + nbytes + 3) & ~(uintptr_t)3);
116 bebf_seek (bebf, 0);
117}
118
124static void bebf_UNUSED bebf_set_write (bebf_t *bebf, uint8_t *wp) {
125 bebf->writestart = wp;
126 bebf->writeptr = (uint32_t *)wp;
127 bebf->writecache = 0;
128 bebf->writebits = 64;
129}
130
137static uint32_t bebf_UNUSED bebf_get (bebf_t *bebf, uint32_t nbits) {
138 uint32_t b;
139 if (bebf->readbits > 31) {
140 bebf->readbits -= 32;
141 if (bebf->readptr < bebf->readstop) {
142 uint32_t v = bebf_ADJ32 (*(bebf->readptr)++);
143 bebf->readcache |= (uint64_t)v << bebf->readbits;
144 }
145 }
146 bebf->readbits += nbits;
147 b = bebf->readcache >> 32;
148 bebf->readcache <<= nbits;
149 return b >> (32 - nbits);
150}
151
158static uint32_t bebf_UNUSED bebf_sniff (bebf_t *bebf, uint32_t nbits) {
159 uint32_t b;
160 if (bebf->readbits > 31) {
161 bebf->readbits -= 32;
162 if (bebf->readptr < bebf->readstop) {
163 uint32_t v = bebf_ADJ32 (*(bebf->readptr)++);
164 bebf->readcache |= (uint64_t)v << bebf->readbits;
165 }
166 }
167 bebf->readbits += nbits;
168 b = bebf->readcache >> 32;
169 return b >> (32 - nbits);
170}
171
177static void bebf_UNUSED bebf_skip (bebf_t *bebf, uint32_t nbits) {
178 if (bebf->readbits > 31) {
179 bebf->readbits -= 32;
180 if (bebf->readptr < bebf->readstop) {
181 uint32_t v = bebf_ADJ32 (*(bebf->readptr)++);
182 bebf->readcache |= (uint64_t)v << bebf->readbits;
183 }
184 }
185 bebf->readbits += nbits;
186 bebf->readcache <<= nbits;
187}
188
194static uint32_t bebf_UNUSED bebf_tell (bebf_t *bebf) {
195 return (((const uint8_t *)bebf->readptr - bebf->readstart) << 3) + bebf->readbits - 64;
196}
197
205static int bebf_UNUSED bebf_sync (bebf_t *bebf, uint32_t pattern, uint32_t pbits) {
206 uint32_t _pat = pattern << (32 - pbits);
207 uint32_t _mask = ~0U << (32 - pbits);
208 if (bebf->readbits > 31) {
209 bebf->readbits -= 32;
210 if (bebf->readptr < bebf->readstop) {
211 uint32_t v = bebf_ADJ32 (*(bebf->readptr)++);
212 bebf->readcache |= (uint64_t)v << bebf->readbits;
213 }
214 }
215 while (1) {
216 if (bebf->readbits > 31) {
217 uint32_t v;
218 bebf->readbits -= 32;
219 if (bebf->readptr >= bebf->readstop)
220 return 0;
221 v = bebf_ADJ32 (*(bebf->readptr)++);
222 bebf->readcache |= v;
223 }
224 if (((bebf->readcache >> 32) & _mask) == _pat)
225 break;
226 bebf->readbits++;
227 bebf->readcache <<= 1;
228 }
229 bebf->readbits += pbits;
230 bebf->readcache <<= pbits;
231 return 1;
232}
233
234
241static void bebf_UNUSED bebf_put (bebf_t *bebf, uint32_t bits, uint32_t nbits) {
242 if (bebf->writebits < 33) {
243 *(bebf->writeptr)++ = bebf_ADJ32 (bebf->writecache >> 32);
244 bebf->writecache <<= 32;
245 bebf->writebits += 32;
246 }
247 bebf->writebits -= nbits;
248 bebf->writecache |= (uint64_t)bits << bebf->writebits;
249}
250
256static size_t bebf_UNUSED bebf_flush (bebf_t *bebf) {
257 if (bebf->writebits < 33) {
258 *(bebf->writeptr)++ = bebf_ADJ32 (bebf->writecache >> 32);
259 bebf->writecache <<= 32;
260 bebf->writebits += 32;
261 }
262 if (bebf->writebits < 64) {
263 *(bebf->writeptr) = bebf_ADJ32 (bebf->writecache >> 32);
264 }
265 return ((uint8_t *)bebf->writeptr - bebf->writestart) + ((64 - bebf->writebits + 7) >> 3);
266}
267
273static void bebf_UNUSED bebf_copy (bebf_t *bebf, uint32_t nbits) {
274 const uint32_t *p = bebf->readptr;
275 uint32_t *q = bebf->writeptr;
276 /* refill read */
277 if (bebf->readbits > 31) {
278 bebf->readbits -= 32;
279 bebf->readcache |= (uint64_t)bebf_ADJ32 (p[0]) << bebf->readbits;
280 p++;
281 }
282 /* flush write */
283 if (bebf->writebits < 33) {
284 *q = bebf_ADJ32 (bebf->writecache >> 32);
285 bebf->writecache <<= 32;
286 q++;
287 bebf->writebits += 32;
288 }
289 /* staying inside cache? */
290 if (bebf->writebits >= (int32_t)nbits) {
291 bebf->readptr = p;
292 bebf->writeptr = q;
293 bebf->writebits -= nbits;
294 bebf->writecache |= (bebf->readcache >> (64 - nbits)) << bebf->writebits;
295 bebf->readcache <<= nbits;
296 bebf->readbits += nbits;
297 return;
298 }
299 /* align write */
300 if (bebf->writebits < 64) {
301 uint32_t n = bebf->writebits - 32;
302 uint32_t b = bebf->readcache >> (64 - n);
303 b |= bebf->writecache >> 32;
304 *q = bebf_ADJ32 (b);
305 q++;
306 bebf->readcache <<= n;
307 bebf->readbits += n;
308 bebf->writebits = 64;
309 nbits -= n;
310 if (bebf->readbits > 31) {
311 bebf->readbits -= 32;
312 bebf->readcache |= (uint64_t)bebf_ADJ32 (p[0]) << bebf->readbits;
313 p++;
314 }
315 }
316 /* fast blocks */
317 if (nbits & ~31) {
318 uint32_t words = nbits >> 5;
319 switch ((64 - bebf->readbits) & 31) {
320 /* If only my old Amiga blitter could see this :-)) */
321#ifdef BEBF_X86_32_ASM
322# define bebf_shift(nn) { \
323 uint32_t hi = bebf->readcache >> 32, lo = bebf->readcache, dummy; \
324 do { \
325 __asm__ __volatile__ ( \
326 "movl\t%1, %4\n\t" \
327 "shldl\t%5, %0, %1\n\t" \
328 "bswap\t%4\n\t" \
329 "movl\t(%2), %0\n\t" \
330 "bswap\t%0\n\t" \
331 "movl\t%4, (%3)\n\t" \
332 "addl\t$4, %2\n\t" \
333 "leal\t4(%3), %3\n\t" \
334 "shldl\t%6, %0, %1\n\t" \
335 "sall\t%6, %0" \
336 : "=r" (lo), "=r" (hi), "=r" (p), "=r" (q), "=r" (dummy) \
337 : "I" (nn), "I" (32 - nn), "0" (lo), "1" (hi), "2" (p), "3" (q) \
338 : "cc"); \
339 } while (--words); \
340 bebf->readcache = ((uint64_t)hi << 32) | lo; \
341}
342#else
343# define bebf_shift(nn) { \
344 uint64_t b = bebf->readcache; \
345 do {*q++ = bebf_ADJ32 (b >> 32); b <<= nn; b |= bebf_ADJ32 (*p++); b <<= 32 - nn;} while (--words); \
346 bebf->readcache = b; \
347}
348#endif
349 case 1: bebf_shift ( 1); break;
350 case 2: bebf_shift ( 2); break;
351 case 3: bebf_shift ( 3); break;
352 case 4: bebf_shift ( 4); break;
353 case 5: bebf_shift ( 5); break;
354 case 6: bebf_shift ( 6); break;
355 case 7: bebf_shift ( 7); break;
356 case 9: bebf_shift ( 9); break;
357 case 10: bebf_shift (10); break;
358 case 11: bebf_shift (11); break;
359 case 12: bebf_shift (12); break;
360 case 13: bebf_shift (13); break;
361 case 14: bebf_shift (14); break;
362 case 15: bebf_shift (15); break;
363 case 17: bebf_shift (17); break;
364 case 18: bebf_shift (18); break;
365 case 19: bebf_shift (19); break;
366 case 20: bebf_shift (20); break;
367 case 21: bebf_shift (21); break;
368 case 22: bebf_shift (22); break;
369 case 23: bebf_shift (23); break;
370 case 25: bebf_shift (25); break;
371 case 26: bebf_shift (26); break;
372 case 27: bebf_shift (27); break;
373 case 28: bebf_shift (28); break;
374 case 29: bebf_shift (29); break;
375 case 30: bebf_shift (30); break;
376 case 31: bebf_shift (31); break;
377 default:
378 memcpy (q, (const uint8_t *)p - ((64 - bebf->readbits) >> 3), words << 2);
379 p += words;
380 q += words;
381 bebf->readcache = (((uint64_t)bebf_ADJ32 (p[-2]) << 32) | bebf_ADJ32 (p[-1])) << bebf->readbits;
382 }
383 nbits &= 31;
384 }
385 /* tail bits */
386 if (nbits) {
387 bebf->writecache = (bebf->readcache >> (64 - nbits)) << (64 - nbits);
388 bebf->writebits = 64 - nbits;
389 bebf->readcache <<= nbits;
390 bebf->readbits += nbits;
391 } else {
392 bebf->writecache = 0;
393 }
394 bebf->readptr = p;
395 bebf->writeptr = q;
396}
397
398/*************************************************************************
399* AAC config data parser *
400*************************************************************************/
401
402typedef enum {
403 AOT_AAC_MAIN = 1, /* Main */
404 AOT_AAC_LC = 2, /* Low Complexity */
405 AOT_AAC_SSR = 3, /* Scalable Sample Rate */
406 AOT_AAC_LTP = 4, /* Long Term Prediction */
407 AOT_SBR = 5, /* Spectral Band Replication */
408 AOT_AAC_SCALABLE = 6, /* Scalable */
409 AOT_TWINVQ = 7, /* Twin Vector Quantizer */
410 AOT_CELP = 8, /* Code Excited Linear Prediction */
411 AOT_HVXC = 9, /* Harmonic Vector eXcitation Coding */
412 AOT_ER_AAC_LC = 17, /* Error Resilient variants */
416 AOT_ER_BSAC = 22, /* Bit-Sliced Arithmetic Coding */
417 AOT_ER_AAC_LD = 23, /* Low Delay */
420 AOT_ER_HILN = 26, /* Harmonic and Individual Lines plus Noise */
421 AOT_ER_PARAM = 27, /* Parametric */
422 AOT_SSC = 28, /* SinuSoidal Coding */
423 AOT_PS = 29, /* Parametric Stereo */
424 AOT_L1 = 32, /* Layer 1 */
425 AOT_L2 = 33, /* Layer 2 */
426 AOT_L3 = 34, /* Layer 3 */
427 AOT_ALS = 36, /* Audio LosslesS */
428 AOT_ER_AAC_ELD = 39 /* Enhanced Low Delay */
430
431#define AOTF_SHORT 0x0001 /* frameLengthFlag */
432#define AOTF_CORE 0x0002 /* dependsOnCoreCoder */
433#define AOTF_EXT1 0x0004 /* extensionFlag1 */
434#define AOTF_LAYER 0x0008 /* layerNr */
435#define AOTF_CHAN 0x0010 /* custom channel layout */
436#define AOTF_SUBFR 0x0020 /* subFrames */
437#define AOTF_RESIL 0x0040 /* errorResilienceFlags */
438#define AOTF_LDSBR 0x0080 /* lowDelaySbr */
439#define AOTF_ELD 0x0100 /* enhancedLowDelay */
440#define AOTF_EPCNF 0x0200 /* epConfig */
441
442static const uint32_t bebf_latm_flags[40] = {
443 0,
444 /* AOT_AAC_MAIN */ AOTF_SHORT|AOTF_CORE|AOTF_EXT1|AOTF_CHAN,
445 /* AOT_AAC_LC */ AOTF_SHORT|AOTF_CORE|AOTF_EXT1|AOTF_CHAN,
446 /* AOT_AAC_SSR */ AOTF_SHORT|AOTF_CORE|AOTF_EXT1|AOTF_CHAN,
447 /* AOT_AAC_LTP */ AOTF_SHORT|AOTF_CORE|AOTF_EXT1|AOTF_CHAN,
448 /* AOT_SBR */ 0,
449 /* AOT_AAC_SCALABLE */ AOTF_SHORT|AOTF_CORE|AOTF_EXT1|AOTF_LAYER|AOTF_CHAN,
450 /* AOT_TWINVQ */ 0,
451 /* AOT_CELP */ 0,
452 /* AOT_HVXC */ 0,
453 0, 0, 0, 0, 0, 0, 0,
455 0,
458 /* AOT_ER_TWINVQ */ 0,
461 /* AOT_ER_CELP */ 0,
462 /* AOT_ER_HVXC */ 0,
463 /* AOT_ER_HILN */ 0,
464 /* AOT_ER_PARAM */ 0,
465 /* AOT_SSC */ 0,
466 /* AOT_PS */ 0,
467 0, 0,
468 /* AOT_L1 */ 0,
469 /* AOT_L2 */ 0,
470 /* AOT_L3 */ 0,
471 0,
472 /* AOT_ALS */ 0,
473 0, 0,
475};
476
477#define BEBF_LATM_GOT_CONF 1
478#define BEBF_LATM_GOT_FRAME 2
479
480typedef struct {
482 uint8_t *frame;
483 uint32_t framelen, fbuflen;
484 uint8_t *config;
485 uint32_t conflen, confbuflen;
486 uint32_t version;
487 uint32_t frame_len_type, frame_len;
488 bebf_aot_t object_type, object_type2;
489 uint32_t samplerate_index, samplerate;
490 uint32_t samplerate_index2, samplerate2;
491 uint32_t samples;
492 uint32_t channel_conf, numchannels;
494 int32_t sbr, ps;
496
503static int bebf_UNUSED bebf_latm_configure (bebf_latm_t *latm, uint32_t nbits) {
504 /* Aargh - LATM v0 does not provide config length, so we need to fully parse it ... */
505 static const uint32_t rates[16] = {
506 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, 0, 0, 0
507 };
508 static const uint8_t channels[16] = { 0, 1, 2, 3, 4, 5, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0 };
509 uint32_t conf_pos = bebf_tell (&latm->bebf), conf_pos2;
510 uint32_t n, ext1_flag, flags, ret = 0;
511 /* object type */
512 latm->object_type = bebf_get (&latm->bebf, 5);
513 if (latm->object_type == 31)
514 latm->object_type = bebf_get (&latm->bebf, 6) + 32;
515 /* sample rate */
516 latm->samplerate_index = bebf_get (&latm->bebf, 4);
517 latm->samplerate = latm->samplerate_index == 15 ? bebf_get (&latm->bebf, 24) : rates[latm->samplerate_index];
518 /* channels */
519 latm->channel_conf = bebf_get (&latm->bebf, 4);
520 latm->numchannels = channels[latm->channel_conf];
521 /* explicit sbr/ps */
522 latm->sbr = latm->ps = -1;
523 n = 0;
524 if (latm->object_type == AOT_SBR) {
525 n = 1;
526 } else if (latm->object_type == AOT_PS) {
527 if ((latm->channel_conf == 1) && (bebf_sniff (&latm->bebf, 9) & 0x0ff)) {
528 latm->ps = 1;
529 n = 1;
530 }
531 }
532 if (n) {
533 latm->object_type2 = AOT_SBR;
534 latm->sbr = 1;
535 latm->samplerate_index2 = bebf_get (&latm->bebf, 4);
536 latm->samplerate2 = latm->samplerate_index2 == 15 ? bebf_get (&latm->bebf, 24) : rates[latm->samplerate_index2];
537 latm->object_type = bebf_get (&latm->bebf, 5);
538 if (latm->object_type == 31)
539 latm->object_type = bebf_get (&latm->bebf, 6) + 32;
540 if (latm->object_type == AOT_ER_BSAC)
541 latm->channel_conf2 = bebf_get (&latm->bebf, 4);
542 } else {
543 latm->object_type2 = 0;
544 latm->samplerate_index2 = 0;
545 latm->samplerate2 = 0;
546 }
547 /* implicit ps */
548 if (latm->ps == -1) {
549 if (!latm->sbr || (latm->object_type != AOT_AAC_LC))
550 latm->ps = 0;
551 }
552 /* more specific info */
553 conf_pos2 = bebf_tell (&latm->bebf);
554 /* lossless audio */
555 if (latm->object_type == AOT_ALS) {
556 bebf_skip (&latm->bebf, 5);
557 conf_pos2 += 5;
558 if (bebf_sniff (&latm->bebf, 24) != 0x414c53) { /* "ALS" */
559 bebf_skip (&latm->bebf, 24);
560 conf_pos2 += 24;
561 }
562 /* sniff first frame head for correct settings */
563 if (bebf_get (&latm->bebf, 32) == 0x414c5300) { /* "ALS\0" */
564 latm->samplerate = bebf_get (&latm->bebf, 32);
565 bebf_skip (&latm->bebf, 32); /* numSamples */
566 latm->channel_conf = 0;
567 latm->numchannels = bebf_get (&latm->bebf, 16) + 1;
568 }
569 }
570 n = latm->object_type;
571 if (n > 39)
572 n = 0;
573 flags = bebf_latm_flags[n];
574 if (flags & AOTF_SHORT) /* frameLengthFlag */
575 latm->samples = bebf_get (&latm->bebf, 1) ? 960 : 1024;
576 if (flags & AOTF_CORE) { /* dependsOnCoreCoder */
577 if (bebf_get (&latm->bebf, 1))
578 bebf_skip (&latm->bebf, 14); /* coreCoderDelay */
579 }
580 ext1_flag = 2;
581 if (flags & AOTF_EXT1) /* extensionFlag1 */
582 ext1_flag = bebf_get (&latm->bebf, 1);
583 if (flags & AOTF_LAYER) /* layerNr */
584 bebf_skip (&latm->bebf, 3);
585 if (flags & AOTF_CHAN) { /* custom channel layout */
586 if (!latm->channel_conf) {
587 int front, side, back, lfe, data, coupling, n;
588 /* program config element */
589 bebf_skip (&latm->bebf, 10); /* elementInstanceTag, objectType, Freq */
590 n = bebf_get (&latm->bebf, 4 + 4 + 4 + 2 + 3 + 4);
591 front = n >> (4 + 4 + 2 + 3 + 4);
592 side = (n >> ( 4 + 2 + 3 + 4)) & 15;
593 back = (n >> ( 2 + 3 + 4)) & 15;
594 lfe = (n >> ( 3 + 4)) & 3;
595 data = (n >> 4 ) & 7;
596 coupling = n & 15;
597 latm->numchannels = front + side + back + lfe;
598 if (bebf_get (&latm->bebf, 1))
599 bebf_skip (&latm->bebf, 4); /* mono downmix */
600 if (bebf_get (&latm->bebf, 1))
601 bebf_skip (&latm->bebf, 4); /* stereo downmix */
602 if (bebf_get (&latm->bebf, 1))
603 bebf_skip (&latm->bebf, 3); /* matrix downmix */
604 bebf_seek (&latm->bebf, bebf_tell (&latm->bebf) + 5 * (front + side + back + coupling) + 4 * (lfe + data));
605 /* comment text */
606 bebf_align (&latm->bebf);
607 n = bebf_get (&latm->bebf, 8);
608 bebf_seek (&latm->bebf, bebf_tell (&latm->bebf) + 8 * n);
609 }
610 }
611 if (ext1_flag) {
612 if (flags & AOTF_SUBFR) /* subFrames */
613 bebf_skip (&latm->bebf, 5 + 11); /* numSubFrames, layerLength */
614 if (flags & AOTF_RESIL) /* errorResilienceFlags */
615 bebf_skip (&latm->bebf, 3);
616 if (ext1_flag == 1)
617 bebf_skip (&latm->bebf, 1); /* extFlag3 */
618 }
619 if (flags & AOTF_LDSBR) /* lowDelaySbr */
620 bebf_skip (&latm->bebf, 1);
621 if (flags & AOTF_ELD) { /* enhancedLowDelay */
622 while (bebf_get (&latm->bebf, 4)) {
623 n = bebf_get (&latm->bebf, 4);
624 if (n == 15) {
625 n += bebf_get (&latm->bebf, 8);
626 if (n == 231)
627 n += bebf_get (&latm->bebf, 16);
628 }
629 bebf_seek (&latm->bebf, bebf_tell (&latm->bebf) + 8 * n);
630 }
631 }
632 if (flags & AOTF_EPCNF) /* epConfig */
633 bebf_skip (&latm->bebf, 2);
634 conf_pos2 = bebf_tell (&latm->bebf);
635 /* export config */
636 do {
637 size_t l = (conf_pos2 - conf_pos + 7) >> 3;
638 if (l > latm->confbuflen) {
639 free (latm->config);
640 latm->confbuflen = (3 * l / 2 + 7) & ~(size_t)7;
641 latm->config = malloc (2 * (latm->confbuflen));
642 }
643 if (!latm->config)
644 break;
645 bebf_set_write (&latm->bebf, latm->config);
646 bebf_seek (&latm->bebf, conf_pos);
647 bebf_copy (&latm->bebf, conf_pos2 - conf_pos);
648 bebf_flush (&latm->bebf);
649 ret = BEBF_LATM_GOT_CONF;
650 if (latm->conflen == l) {
651 if (!memcmp (latm->config, latm->config + latm->confbuflen, l))
652 ret = 0;
653 }
654 if (ret)
655 memcpy (latm->config + latm->confbuflen, latm->config, l);
656 latm->conflen = l;
657 } while (0);
658 bebf_seek (&latm->bebf, nbits ? conf_pos + nbits : conf_pos2);
659 return ret;
660}
661
662/*************************************************************************
663* AAC LATM demuxer *
664*************************************************************************/
665
673static int bebf_UNUSED bebf_latm_demux (bebf_latm_t *latm, const uint8_t *in, uint32_t nbytes) {
674 uint32_t ret = 0;
675 bebf_set_read (&latm->bebf, in, nbytes);
676 {
677 /* latm sync */
678 uint32_t n = bebf_get (&latm->bebf, 24);
679 if ((n >> 13) != 0x2b7)
680 return 0;
681 n &= 0x1fff;
682 n += 3;
683 if (n < nbytes)
684 nbytes = n;
685 }
686 if (bebf_get (&latm->bebf, 1)) {
687 /* same settings */
688 if (!latm->conflen)
689 return 0;
690 } else {
691 /* new settings */
692 latm->version = bebf_get (&latm->bebf, 1);
693 if (latm->version)
694 latm->version += bebf_get (&latm->bebf, 1);
695 if (latm->version < 2) {
696 uint32_t n;
697 /* some yet not mattering values */
698 if (latm->version) {
699 n = bebf_get (&latm->bebf, 2);
700 bebf_skip (&latm->bebf, 8 * n + 8); /* taraFullness */
701 }
702 bebf_skip (&latm->bebf, 1 + 6); /* allStreamSameTimeFraming, numSubFrames */
703 /* part count (DVB uses 1:1) */
704 if (bebf_get (&latm->bebf, 4)) /* numPrograms */
705 return 0;
706 if (bebf_get (&latm->bebf, 3)) /* numLayers */
707 return 0;
708 /* config len */
709 n = 0;
710 if (latm->version) {
711 n = bebf_get (&latm->bebf, 2);
712 n = bebf_get (&latm->bebf, 8 * n + 8);
713 }
714 /* extract config */
715 ret |= bebf_latm_configure (latm, n);
716 /* frame len */
717 latm->frame_len_type = n = bebf_get (&latm->bebf, 3);
718 if (n < 3) {
719 if (n == 0)
720 bebf_skip (&latm->bebf, 8); /* latmBufferFullness */
721 else if (n == 1)
722 latm->frame_len = bebf_get (&latm->bebf, 9); /* fixed frameLength */
723 } else {
724 if (n <= 5)
725 bebf_skip (&latm->bebf, 6); /* CELP frame len index */
726 else
727 bebf_skip (&latm->bebf, 1); /* HVXC frame len index */
728 }
729 /* more extra stuff */
730 if (bebf_get (&latm->bebf, 1)) { /* other data present */
731 if (latm->version) {
732 n = bebf_get (&latm->bebf, 2);
733 bebf_skip (&latm->bebf, 8 * n + 8);
734 } else {
735 do {
736 n = bebf_get (&latm->bebf, 1);
737 bebf_skip (&latm->bebf, 8);
738 } while (n);
739 }
740 }
741 if (bebf_get (&latm->bebf, 1)) /* crc present */
742 bebf_skip (&latm->bebf, 8);
743 }
744 }
745 if (latm->version < 2) { /* payload len */
746 uint32_t l = 0;
747 if (latm->frame_len_type == 0) {
748 uint32_t n;
749 do {
750 n = bebf_get (&latm->bebf, 8);
751 l += n;
752 } while (n == 255);
753 latm->frame_len = l; /* variable frameLength */
754 } else if (latm->frame_len_type == 1) {
755 l = latm->frame_len;
756 } else if (latm->frame_len & 1) { /* 3, 5, 7 */
757 bebf_skip (&latm->bebf, 2); /* mux_slot_len_coded */
758 }
759 }
760 /*
761 * Export frame.
762 * @#?!*#$!! Looks like LATM actually appends the frame contents to the
763 * bitstream without gap. They dont win much by not byte aligning it there!!
764 * Some possible workarounds:
765 * - Merge LATM demuxer and AAC decoder. Terribly complex code, ff does it
766 * that way.
767 * - Tell main decoding function to skip first 0..7 bits. Very easy, but
768 * wont work with external libfaad.
769 * - Bit shift whole frame. Thats what we do here.
770 */
771 do {
772 size_t n = nbytes - (bebf_tell (&latm->bebf) >> 3);
773 if (n > latm->fbuflen) {
774 free (latm->frame);
775 latm->fbuflen = (3 * n / 2 + 7) & ~(size_t)7;
776 latm->frame = malloc (latm->fbuflen);
777 }
778 if (!latm->frame)
779 break;
780 bebf_set_write (&latm->bebf, latm->frame);
781 bebf_copy (&latm->bebf, 8 * nbytes - bebf_tell (&latm->bebf));
782 latm->framelen = bebf_flush (&latm->bebf);
783 ret |= BEBF_LATM_GOT_FRAME;
784 } while (0);
785 return ret;
786}
787
788/*************************************************************************
789* AAC LATM parser *
790*************************************************************************/
791
793 memset (latm, 0, sizeof (*latm));
794}
795
797 free (latm->config);
798 latm->config = NULL;
799 free (latm->frame);
800 latm->frame = NULL;
801 latm->fbuflen = 0;
802 latm->framelen = 0;
803}
804
812
819static bebf_latm_parser_status_t bebf_UNUSED bebf_latm_test (const uint8_t *in, int nbytes) {
820 uint32_t word = 0;
821 int n = nbytes;
822#define BEBF_TEST_MAX (2 * (0x1fff + 3))
823 if (n > BEBF_TEST_MAX)
824 n = BEBF_TEST_MAX;
825 while (n--) {
826 word <<= 8;
827 word |= *in++;
828 if ((word & 0xfff60000) == 0xfff00000) do {
829 /* ADTS */
830 int size;
831 if (n < 7 - 4)
832 break;
833 size = ((word << 11) | ((uint32_t)in[0] << 3) | (in[1] >> 5)) & 0x1fff;
834 if ((size < 7) || (n < size + 7 - 4))
835 break;
836 if ((in[size - 4] != 0xff) || ((in[size + 1 - 4] & 0xf6) != 0xf0))
837 break;
838 return BEBF_LATM_IS_ADTS;
839 } while (0);
840 if ((word & 0xffe00000) == 0x56e00000) do {
841 /* LATM */
842 int size = ((word >> 8) & 0x1fff) + 3;
843 if (n < size + 3 - 4)
844 break;
845 if ((in[size - 4] != 0x56) || ((in[size + 1 - 4] & 0xe0) != 0xe0))
846 break;
847 return BEBF_LATM_IS_LATM;
848 } while (0);
849 }
851}
852
860static int bebf_UNUSED bebf_latm_parse (bebf_latm_t *latm, const uint8_t *in, int *nbytes) {
861 const uint8_t *p = in;
862 int n = *nbytes, size;
863 /* discard leading garbage */
864 n -= 2;
865 while (n >= 0) {
866 if (p[0] == 0x56) {
867 if ((p[1] & 0xe0) == 0xe0)
868 break;
869 }
870 p++;
871 n--;
872 }
873 n += 2;
874 *nbytes = p - in;
875 if (n < 3) {
876 if ((n == 1) && (p[0] != 0x56))
877 *nbytes += 1;
878 return 0;
879 }
880 size = ((((uint32_t)p[1] << 8) | p[2]) & 0x1fff) + 3;
881 if (n < size)
882 return 0;
883 *nbytes += size;
884 return bebf_latm_demux (latm, p, size);
885}
886#undef LOG_MODULE
887
888
#define AOTF_CORE
Definition latm.c:432
#define AOTF_RESIL
Definition latm.c:437
static void bebf_latm_open(bebf_latm_t *latm)
Definition latm.c:792
#define AOTF_SUBFR
Definition latm.c:436
static uint32_t bebf_sniff(bebf_t *bebf, uint32_t nbits)
Definition latm.c:158
static void bebf_put(bebf_t *bebf, uint32_t bits, uint32_t nbits)
Definition latm.c:241
#define AOTF_LAYER
Definition latm.c:434
#define AOTF_EXT1
Definition latm.c:433
static void bebf_skip(bebf_t *bebf, uint32_t nbits)
Definition latm.c:177
static void bebf_latm_close(bebf_latm_t *latm)
Definition latm.c:796
#define AOTF_CHAN
Definition latm.c:435
static uint32_t bebf_get(bebf_t *bebf, uint32_t nbits)
Definition latm.c:137
bebf_latm_parser_status_t
Definition latm.c:805
@ BEBF_LATM_IS_RAW
Definition latm.c:807
@ BEBF_LATM_IS_UNKNOWN
Definition latm.c:810
@ BEBF_LATM_IS_LATM
Definition latm.c:809
@ BEBF_LATM_IS_ADTS
Definition latm.c:808
@ BEBF_LATM_NEED_MORE_DATA
Definition latm.c:806
static int bebf_latm_configure(bebf_latm_t *latm, uint32_t nbits)
Definition latm.c:503
static void bebf_copy(bebf_t *bebf, uint32_t nbits)
Definition latm.c:273
#define AOTF_SHORT
Definition latm.c:431
#define BEBF_TEST_MAX
static void bebf_seek(bebf_t *bebf, uint32_t nbits)
Definition latm.c:79
#define AOTF_LDSBR
Definition latm.c:438
static const uint32_t bebf_latm_flags[40]
Definition latm.c:442
#define AOTF_EPCNF
Definition latm.c:440
#define BEBF_LATM_GOT_CONF
Definition latm.c:477
static bebf_latm_parser_status_t bebf_latm_test(const uint8_t *in, int nbytes)
Definition latm.c:819
#define bebf_shift(nn)
static size_t bebf_flush(bebf_t *bebf)
Definition latm.c:256
#define bebf_UNUSED
Definition latm.c:52
static void bebf_align(bebf_t *bebf)
Definition latm.c:99
bebf_aot_t
Definition latm.c:402
@ AOT_ALS
Definition latm.c:427
@ AOT_L3
Definition latm.c:426
@ AOT_AAC_LTP
Definition latm.c:406
@ AOT_ER_AAC_LD
Definition latm.c:417
@ AOT_ER_CELP
Definition latm.c:418
@ AOT_HVXC
Definition latm.c:411
@ AOT_ER_AAC_ELD
Definition latm.c:428
@ AOT_ER_AAC_LC
Definition latm.c:412
@ AOT_L2
Definition latm.c:425
@ AOT_SSC
Definition latm.c:422
@ AOT_ER_HVXC
Definition latm.c:419
@ AOT_ER_TWINVQ
Definition latm.c:415
@ AOT_AAC_SCALABLE
Definition latm.c:408
@ AOT_SBR
Definition latm.c:407
@ AOT_TWINVQ
Definition latm.c:409
@ AOT_ER_PARAM
Definition latm.c:421
@ AOT_L1
Definition latm.c:424
@ AOT_CELP
Definition latm.c:410
@ AOT_ER_BSAC
Definition latm.c:416
@ AOT_AAC_SSR
Definition latm.c:405
@ AOT_AAC_LC
Definition latm.c:404
@ AOT_ER_AAC_LTP
Definition latm.c:413
@ AOT_ER_AAC_SCALABLE
Definition latm.c:414
@ AOT_ER_HILN
Definition latm.c:420
@ AOT_PS
Definition latm.c:423
@ AOT_AAC_MAIN
Definition latm.c:403
#define AOTF_ELD
Definition latm.c:439
static int bebf_latm_parse(bebf_latm_t *latm, const uint8_t *in, int *nbytes)
Definition latm.c:860
static int bebf_sync(bebf_t *bebf, uint32_t pattern, uint32_t pbits)
Definition latm.c:205
#define bebf_ADJ32(v)
Definition latm.c:56
#define BEBF_LATM_GOT_FRAME
Definition latm.c:478
static int bebf_latm_demux(bebf_latm_t *latm, const uint8_t *in, uint32_t nbytes)
Definition latm.c:673
static void bebf_set_write(bebf_t *bebf, uint8_t *wp)
Definition latm.c:124
static void bebf_set_read(bebf_t *bebf, const uint8_t *rp, uint32_t nbytes)
Definition latm.c:113
static uint32_t bebf_tell(bebf_t *bebf)
Definition latm.c:194
#define bits
Definition latm.c:480
uint32_t framelen
Definition latm.c:483
uint32_t channel_conf
Definition latm.c:492
uint32_t samplerate_index2
Definition latm.c:490
uint32_t samplerate_index
Definition latm.c:489
uint32_t frame_len_type
Definition latm.c:487
uint8_t * config
Definition latm.c:484
uint8_t * frame
Definition latm.c:482
uint32_t conflen
Definition latm.c:485
int32_t ps
Definition latm.c:494
bebf_aot_t object_type
Definition latm.c:488
int32_t sbr
Definition latm.c:494
uint32_t samplerate
Definition latm.c:489
bebf_aot_t object_type2
Definition latm.c:488
uint32_t fbuflen
Definition latm.c:483
bebf_t bebf
Definition latm.c:481
uint32_t channel_conf2
Definition latm.c:493
uint32_t numchannels
Definition latm.c:492
uint32_t samplerate2
Definition latm.c:490
uint32_t confbuflen
Definition latm.c:485
uint32_t version
Definition latm.c:486
uint32_t samples
Definition latm.c:491
uint32_t frame_len
Definition latm.c:487
Definition latm.c:64
uint64_t readcache
Definition latm.c:65
int32_t writebits
Definition latm.c:68
uint8_t * writestart
Definition latm.c:71
int32_t readbits
Definition latm.c:68
const uint32_t * readstop
Definition latm.c:66
uint32_t * writeptr
Definition latm.c:70
uint64_t writecache
Definition latm.c:69
const uint8_t * readstart
Definition latm.c:67
const uint32_t * readptr
Definition latm.c:66
uint32_t v
Definition utils.c:1157
NULL
Definition xine_plugin.c:78