43e56000c4c4c1fab3746153dbb06a28a498f91d
[paraslash.git] / aac_common.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18 /*
19  * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
20  * Ahead Software AG
21  */
22
23 /** \file aac_ccomon.c common functions of aac_afh and aadcec */
24
25 #include "para.h"
26 #include "aac.h"
27 #include "error.h"
28
29 /**
30  * get a new libfaad decoder handle
31  *
32  */
33 NeAACDecHandle aac_open(void)
34 {
35         NeAACDecHandle h = NeAACDecOpen();
36         NeAACDecConfigurationPtr c = NeAACDecGetCurrentConfiguration(h);
37
38         c->defObjectType = LC;
39         c->outputFormat = FAAD_FMT_16BIT;
40         c->downMatrix = 0;
41         NeAACDecSetConfiguration(h, c);
42         return h;
43 };
44
45 static int aac_read_decoder_length(unsigned char *buf, int *description_len)
46 {
47         uint8_t b;
48         uint8_t numBytes = 0;
49         uint32_t length = 0;
50
51         do {
52                 b = buf[numBytes];
53                 numBytes++;
54                 length = (length << 7) | (b & 0x7F);
55         } while
56                 ((b & 0x80) && numBytes < 4);
57         *description_len = numBytes;
58         return length;
59 }
60
61 int aac_find_esds(unsigned char *buf, unsigned buflen, int *skip)
62 {
63         int i;
64
65         for (i = 0; i + 4 < buflen; i++) {
66                 unsigned char *p = buf + i;
67                 int decoder_length, description_len;
68
69                 if (p[0] != 'e' || p[1] != 's' || p[2] != 'd' || p[3] != 's')
70                         continue;
71                 i += 8;
72                 p = buf + i;
73                 PARA_INFO_LOG("found esds@%d, next: %x\n", i, *p);
74                 if (*p == 3)
75                         i += 8;
76                 else
77                         i += 6;
78                 p = buf + i;
79                 PARA_INFO_LOG("next: %x\n", *p);
80                 if (*p != 4)
81                         continue;
82                 i += 18;
83                 p = buf + i;
84                 PARA_INFO_LOG("next: %x\n", *p);
85                 if (*p != 5)
86                         continue;
87                 i++;
88                 p = buf + i;
89                 decoder_length = aac_read_decoder_length(p, &description_len);
90                 PARA_INFO_LOG("decoder length: %d\n", decoder_length);
91                 i += description_len;
92                 *skip = i;
93                 return decoder_length;
94         }
95         return -E_ESDS;
96 }
97
98 unsigned aac_read_int32(unsigned char *buf)
99 {
100         uint8_t *d = (uint8_t*)buf;
101         return (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3];
102 }
103
104 /**
105  * search for the first entry in the stco table
106  *
107  * \param buf buffer to seach
108  * \param buflen length of \a buf
109  * \param skip Upon succesful return, this contains the number
110  * of bytes to skip from the input buffer.
111  *
112  * \return the position of the first entry in the table on success,
113  * -E_STCO on errors.
114  */
115
116 int aac_find_entry_point(unsigned char *buf, unsigned buflen, int *skip)
117 {
118         int i, ret;
119
120         for (i = 0; i + 20 < buflen; i++) {
121                 unsigned char *p = buf + i;
122
123                 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
124                         continue;
125                 PARA_INFO_LOG("found stco@%d\n", i);
126                 i += 12;
127                 ret = aac_read_int32(buf + i); /* first offset */
128                 i += 4;
129                 PARA_INFO_LOG("num entries: %d\n", ret);
130                 *skip = i;
131                 return ret;
132         }
133         PARA_WARNING_LOG("stco not found, buflen: %d\n", buflen);
134         return -E_STCO;
135 }
136