First draft of the aac audio format handler
[paraslash.git] / aac_common.c
1 #include "para.h"
2 #include "aac.h"
3 #include "error.h"
4
5 NeAACDecHandle aac_open(void)
6 {
7 NeAACDecHandle h = NeAACDecOpen();
8 NeAACDecConfigurationPtr c = NeAACDecGetCurrentConfiguration(h);
9
10 c->defObjectType = LC;
11 c->outputFormat = FAAD_FMT_16BIT;
12 c->downMatrix = 0;
13 NeAACDecSetConfiguration(h, c);
14 return h;
15 };
16
17 static int aac_read_decoder_length(unsigned char *buf, int *description_len)
18 {
19 uint8_t b;
20 uint8_t numBytes = 0;
21 uint32_t length = 0;
22
23 do {
24 b = buf[numBytes];
25 numBytes++;
26 length = (length << 7) | (b & 0x7F);
27 } while
28 ((b & 0x80) && numBytes < 4);
29 *description_len = numBytes;
30 return length;
31 }
32
33 int aac_find_esds(unsigned char *buf, unsigned buflen, int *skip)
34 {
35 int i;
36
37 for (i = 0; i + 4 < buflen; i++) {
38 unsigned char *p = buf + i;
39 int decoder_length, description_len;
40
41 if (p[0] != 'e' || p[1] != 's' || p[2] != 'd' || p[3] != 's')
42 continue;
43 i += 8;
44 p = buf + i;
45 PARA_INFO_LOG("found esds@%d, next: %x\n", i, *p);
46 if (*p == 3)
47 i += 8;
48 else
49 i += 6;
50 p = buf + i;
51 PARA_INFO_LOG("next: %x\n", *p);
52 if (*p != 4)
53 continue;
54 i += 18;
55 p = buf + i;
56 PARA_INFO_LOG("next: %x\n", *p);
57 if (*p != 5)
58 continue;
59 i++;
60 decoder_length = aac_read_decoder_length(p, &description_len);
61 PARA_INFO_LOG("decoder length: %d\n", decoder_length);
62 i += description_len;
63 *skip = i;
64 return decoder_length;
65 }
66 return -E_ESDS;
67 }
68
69 unsigned aac_read_int32(unsigned char *buf)
70 {
71 uint8_t *d = (uint8_t*)buf;
72 return (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3];
73 }
74
75
76 int find_stco(unsigned char *buf, unsigned buflen, int *skip)
77 {
78 int i, ret;
79
80 for (i = 0; i + 16 < buflen; i++) {
81 unsigned char *p = buf + i;
82
83 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
84 continue;
85 PARA_INFO_LOG("found stco@%d\n", i);
86 i += 8;
87 ret = aac_read_int32(buf + i);
88 i += 4;
89 PARA_INFO_LOG("num entries: %d\n", ret);
90 *skip = i;
91 return ret;
92 }
93 return -E_STCO;
94 }
95