Trivial %d -> %zu format string fix
[paraslash.git] / aac_common.c
1 /*
2  * Copyright (C) 2006-2007 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_common.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 unsigned long aac_read_decoder_length(unsigned char *buf, int *description_len)
46 {
47         uint8_t b;
48         uint8_t numBytes = 0;
49         unsigned long 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 /**
62  * search for the position and the length of the decoder configuration
63  *
64  * \param buf buffer to seach
65  * \param buflen length of \a buf
66  * \param skip Upon succesful return, this contains the offset in \a buf where
67  * the decoder config starts.
68  * \param decoder_length result pointer that is filled in with the length of
69  * the decoder configuration on success.
70  *
71  * \return positive on success, negative on errors
72  */
73 int aac_find_esds(unsigned char *buf, size_t buflen, size_t *skip,
74                 unsigned long *decoder_length)
75 {
76         size_t i;
77
78         for (i = 0; i + 4 < buflen; i++) {
79                 unsigned char *p = buf + i;
80                 int description_len;
81
82                 if (p[0] != 'e' || p[1] != 's' || p[2] != 'd' || p[3] != 's')
83                         continue;
84                 i += 8;
85                 p = buf + i;
86                 PARA_INFO_LOG("found esds@%zu, next: %x\n", i, *p);
87                 if (*p == 3)
88                         i += 8;
89                 else
90                         i += 6;
91                 p = buf + i;
92                 PARA_INFO_LOG("next: %x\n", *p);
93                 if (*p != 4)
94                         continue;
95                 i += 18;
96                 p = buf + i;
97                 PARA_INFO_LOG("next: %x\n", *p);
98                 if (*p != 5)
99                         continue;
100                 i++;
101                 p = buf + i;
102                 *decoder_length = aac_read_decoder_length(p, &description_len);
103                 PARA_INFO_LOG("decoder length: %lu\n", *decoder_length);
104                 i += description_len;
105                 *skip = i;
106                 return 1;
107         }
108         return -E_ESDS;
109 }
110
111 /**
112  * search for the first entry in the stco table
113  *
114  * \param buf buffer to seach
115  * \param buflen length of \a buf
116  * \param skip Upon succesful return, this contains the number
117  * of bytes to skip from the input buffer.
118  *
119  * \return the position of the first entry in the table on success,
120  * -E_STCO on errors.
121  */
122 ssize_t aac_find_entry_point(unsigned char *buf, size_t buflen, size_t *skip)
123 {
124         ssize_t ret;
125         size_t i;
126
127         for (i = 0; i + 20 < buflen; i++) {
128                 unsigned char *p = buf + i;
129
130                 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
131                         continue;
132                 PARA_INFO_LOG("found stco@%zu\n", i);
133                 i += 12;
134                 ret = aac_read_int32(buf + i); /* first offset */
135                 i += 4;
136                 PARA_INFO_LOG("entry point: %zd\n", ret);
137                 *skip = i;
138                 return ret;
139         }
140         PARA_WARNING_LOG("stco not found, buflen: %zu\n", buflen);
141         return -E_STCO;
142 }