server.cmd: Cosmetics.
[paraslash.git] / aac_common.c
1 /*
2  * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6 /*
7  * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
8  * Ahead Software AG
9  */
10
11 /** \file aac_common.c common functions of aac_afh and aadcec */
12
13 #include "para.h"
14 #include "aac.h"
15 #include "error.h"
16
17 /**
18  * get a new libfaad decoder handle
19  *
20  */
21 NeAACDecHandle aac_open(void)
22 {
23         NeAACDecHandle h = NeAACDecOpen();
24         NeAACDecConfigurationPtr c = NeAACDecGetCurrentConfiguration(h);
25
26         c->defObjectType = LC;
27         c->outputFormat = FAAD_FMT_16BIT;
28         c->downMatrix = 0;
29         NeAACDecSetConfiguration(h, c);
30         return h;
31 };
32
33 static unsigned long aac_read_decoder_length(unsigned char *buf, int *description_len)
34 {
35         uint8_t b;
36         uint8_t numBytes = 0;
37         unsigned long length = 0;
38
39         do {
40                 b = buf[numBytes];
41                 numBytes++;
42                 length = (length << 7) | (b & 0x7F);
43         } while
44                 ((b & 0x80) && numBytes < 4);
45         *description_len = numBytes;
46         return length;
47 }
48
49 /**
50  * search for the position and the length of the decoder configuration
51  *
52  * \param buf buffer to seach
53  * \param buflen length of \a buf
54  * \param skip Upon succesful return, this contains the offset in \a buf where
55  * the decoder config starts.
56  * \param decoder_length result pointer that is filled in with the length of
57  * the decoder configuration on success.
58  *
59  * \return positive on success, negative on errors
60  */
61 int aac_find_esds(unsigned char *buf, size_t buflen, size_t *skip,
62                 unsigned long *decoder_length)
63 {
64         size_t i;
65
66         for (i = 0; i + 4 < buflen; i++) {
67                 unsigned char *p = buf + i;
68                 int description_len;
69
70                 if (p[0] != 'e' || p[1] != 's' || p[2] != 'd' || p[3] != 's')
71                         continue;
72                 i += 8;
73                 p = buf + i;
74                 PARA_INFO_LOG("found esds@%zu, next: %x\n", i, *p);
75                 if (*p == 3)
76                         i += 8;
77                 else
78                         i += 6;
79                 p = buf + i;
80                 PARA_INFO_LOG("next: %x\n", *p);
81                 if (*p != 4)
82                         continue;
83                 i += 18;
84                 p = buf + i;
85                 PARA_INFO_LOG("next: %x\n", *p);
86                 if (*p != 5)
87                         continue;
88                 i++;
89                 p = buf + i;
90                 *decoder_length = aac_read_decoder_length(p, &description_len);
91                 PARA_INFO_LOG("decoder length: %lu\n", *decoder_length);
92                 i += description_len;
93                 *skip = i;
94                 return 1;
95         }
96         return -E_ESDS;
97 }
98
99 /**
100  * search for the first entry in the stco table
101  *
102  * \param buf buffer to seach
103  * \param buflen length of \a buf
104  * \param skip Upon succesful return, this contains the number
105  * of bytes to skip from the input buffer.
106  *
107  * \return the position of the first entry in the table on success,
108  * -E_STCO on errors.
109  */
110 ssize_t aac_find_entry_point(unsigned char *buf, size_t buflen, size_t *skip)
111 {
112         ssize_t ret;
113         size_t i;
114
115         for (i = 0; i + 20 < buflen; i++) {
116                 unsigned char *p = buf + i;
117
118                 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
119                         continue;
120                 PARA_INFO_LOG("found stco@%zu\n", i);
121                 i += 12;
122                 ret = aac_read_int32(buf + i); /* first offset */
123                 i += 4;
124                 PARA_INFO_LOG("entry point: %zd\n", ret);
125                 *skip = i;
126                 return ret;
127         }
128         PARA_WARNING_LOG("stco not found, buflen: %zu\n", buflen);
129         return -E_STCO;
130 }