]> git.tuebingen.mpg.de Git - paraslash.git/blob - aac_common.c
aac: add GPL headers to all files
[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 NeAACDecHandle aac_open(void)
30 {
31         NeAACDecHandle h = NeAACDecOpen();
32         NeAACDecConfigurationPtr c = NeAACDecGetCurrentConfiguration(h);
33
34         c->defObjectType = LC;
35         c->outputFormat = FAAD_FMT_16BIT;
36         c->downMatrix = 0;
37         NeAACDecSetConfiguration(h, c);
38         return h;
39 };
40
41 static int aac_read_decoder_length(unsigned char *buf, int *description_len)
42 {
43         uint8_t b;
44         uint8_t numBytes = 0;
45         uint32_t length = 0;
46
47         do {
48                 b = buf[numBytes];
49                 numBytes++;
50                 length = (length << 7) | (b & 0x7F);
51         } while
52                 ((b & 0x80) && numBytes < 4);
53         *description_len = numBytes;
54         return length;
55 }
56
57 int aac_find_esds(unsigned char *buf, unsigned buflen, int *skip)
58 {
59         int i;
60
61         for (i = 0; i + 4 < buflen; i++) {
62                 unsigned char *p = buf + i;
63                 int decoder_length, description_len;
64
65                 if (p[0] != 'e' || p[1] != 's' || p[2] != 'd' || p[3] != 's')
66                         continue;
67                 i += 8;
68                 p = buf + i;
69                 PARA_INFO_LOG("found esds@%d, next: %x\n", i, *p);
70                 if (*p == 3)
71                         i += 8;
72                 else
73                         i += 6;
74                 p = buf + i;
75                 PARA_INFO_LOG("next: %x\n", *p);
76                 if (*p != 4)
77                         continue;
78                 i += 18;
79                 p = buf + i;
80                 PARA_INFO_LOG("next: %x\n", *p);
81                 if (*p != 5)
82                         continue;
83                 i++;
84                 p = buf + i;
85                 decoder_length = aac_read_decoder_length(p, &description_len);
86                 PARA_INFO_LOG("decoder length: %d\n", decoder_length);
87                 i += description_len;
88                 *skip = i;
89                 return decoder_length;
90         }
91         return -E_ESDS;
92 }
93
94 unsigned aac_read_int32(unsigned char *buf)
95 {
96         uint8_t *d = (uint8_t*)buf;
97         return (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3];
98 }
99
100
101 int aac_find_entry_point(unsigned char *buf, unsigned buflen, int *skip)
102 {
103         int i, ret;
104
105         for (i = 0; i + 20 < buflen; i++) {
106                 unsigned char *p = buf + i;
107
108                 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
109                         continue;
110                 PARA_INFO_LOG("found stco@%d\n", i);
111                 i += 12;
112                 ret = aac_read_int32(buf + i); /* first offset */
113                 i += 4;
114                 PARA_INFO_LOG("num entries: %d\n", ret);
115                 *skip = i;
116                 return ret;
117         }
118         PARA_WARNING_LOG("stco not found, buflen: %d\n", buflen);
119         return -E_STCO;
120 }
121