make osx_write work for any bitrate
[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_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 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 /**
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 *
69 * \return The length of the decoder configuration
70 */
71 ssize_t aac_find_esds(unsigned char *buf, size_t buflen, size_t *skip)
72 {
73 size_t i;
74
75 for (i = 0; i + 4 < buflen; i++) {
76 unsigned char *p = buf + i;
77 int decoder_length, description_len;
78
79 if (p[0] != 'e' || p[1] != 's' || p[2] != 'd' || p[3] != 's')
80 continue;
81 i += 8;
82 p = buf + i;
83 PARA_INFO_LOG("found esds@%zu, next: %x\n", i, *p);
84 if (*p == 3)
85 i += 8;
86 else
87 i += 6;
88 p = buf + i;
89 PARA_INFO_LOG("next: %x\n", *p);
90 if (*p != 4)
91 continue;
92 i += 18;
93 p = buf + i;
94 PARA_INFO_LOG("next: %x\n", *p);
95 if (*p != 5)
96 continue;
97 i++;
98 p = buf + i;
99 decoder_length = aac_read_decoder_length(p, &description_len);
100 PARA_INFO_LOG("decoder length: %d\n", decoder_length);
101 i += description_len;
102 *skip = i;
103 return decoder_length;
104 }
105 return -E_ESDS;
106 }
107
108 /**
109 * search for the first entry in the stco table
110 *
111 * \param buf buffer to seach
112 * \param buflen length of \a buf
113 * \param skip Upon succesful return, this contains the number
114 * of bytes to skip from the input buffer.
115 *
116 * \return the position of the first entry in the table on success,
117 * -E_STCO on errors.
118 */
119 ssize_t aac_find_entry_point(unsigned char *buf, size_t buflen, size_t *skip)
120 {
121 ssize_t ret;
122 size_t i;
123
124 for (i = 0; i + 20 < buflen; i++) {
125 unsigned char *p = buf + i;
126
127 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
128 continue;
129 PARA_INFO_LOG("found stco@%zu\n", i);
130 i += 12;
131 ret = aac_read_int32(buf + i); /* first offset */
132 i += 4;
133 PARA_INFO_LOG("entry point: %zd\n", ret);
134 *skip = i;
135 return ret;
136 }
137 PARA_WARNING_LOG("stco not found, buflen: %zu\n", buflen);
138 return -E_STCO;
139 }