fix some signed warnings in aac and mp3 decoders
[paraslash.git] / aacdec.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 aacdec.c paraslash's aac (m4a) decoder */
24
25 #include "para.h"
26
27 #include "list.h"
28 #include "sched.h"
29 #include "filter.h"
30 #include "error.h"
31 #include "string.h"
32 #include "aac.h"
33
34 /** the output buffer size */
35 #define AAC_OUTBUF_SIZE (32 * 1024)
36
37 /** give up decoding after that many errors */
38 #define MAX_ERRORS 20
39
40 /**
41 * data specific to the aacdec filter
42 *
43 * \sa filter, filter_node
44 */
45 struct private_aacdec_data {
46 /** the return value of aac_open */
47 NeAACDecHandle handle;
48 /** info about the currently decoded frame */
49 NeAACDecFrameInfo frame_info;
50 /** whether this instance of the aac decoder is already initialized */
51 int initialized;
52 /**
53 * return value of aac_find_esds(). Used to call the right aacdec
54 * init function
55 */
56 unsigned long decoder_length;
57 /** number of times the decoder returned an error */
58 unsigned error_count;
59 /** number of bytes already consumed from the imput stream */
60 size_t consumed_total;
61 /** return value of aac_find_entry_point */
62 size_t entry;
63 };
64
65 static ssize_t aacdec(char *input_buffer, size_t len, struct filter_node *fn)
66 {
67 struct private_aacdec_data *padd = fn->private_data;
68 struct filter_chain *fc = fn->fc;
69 int i, ret;
70 unsigned char *p, *outbuffer;
71 unsigned char *inbuf = (unsigned char*)input_buffer;
72 size_t skip, consumed = 0;
73
74 if (fn->loaded > fn->bufsize * 3 / 5)
75 return 0;
76 if (len < 2048 && !*fc->input_eof)
77 return 0;
78
79 if (!padd->initialized) {
80 unsigned long rate = 0;
81 unsigned char channels = 0;
82 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
83 if (ret < 0) {
84 PARA_INFO_LOG("%s\n", PARA_STRERROR(-ret));
85 ret = NeAACDecInit(padd->handle, inbuf,
86 len, &rate, &channels);
87 PARA_INFO_LOG("decoder init: %d\n", ret);
88 if (ret < 0) {
89 ret = -E_AACDEC_INIT;
90 goto out;
91 }
92 consumed = ret;
93 } else {
94 PARA_INFO_LOG("decoder len: %lu\n",
95 padd->decoder_length);
96 consumed += skip;
97 p = inbuf + consumed;
98 ret = -E_AACDEC_INIT;
99 if (NeAACDecInit2(padd->handle, p,
100 padd->decoder_length, &rate,
101 &channels) < 0)
102 goto out;
103 }
104 fc->samplerate = rate;
105 fc->channels = channels;
106 PARA_INFO_LOG("rate: %u, channels: %d\n",
107 fc->samplerate, fc->channels);
108 padd->initialized = 1;
109 }
110 if (padd->decoder_length > 0) {
111 consumed = 0;
112 if (!padd->entry) {
113 ret = aac_find_entry_point(inbuf + consumed,
114 len - consumed, &skip);
115 if (ret < 0) {
116 ret = len;
117 goto out;
118 }
119 consumed += skip;
120 padd->entry = ret;
121 PARA_INFO_LOG("entry: %zu\n", padd->entry);
122 }
123 ret = len;
124 if (padd->consumed_total + len < padd->entry)
125 goto out;
126 if (padd->consumed_total < padd->entry)
127 consumed = padd->entry - padd->consumed_total;
128 }
129 for (; consumed < len; consumed++)
130 if ((inbuf[consumed] & 0xfe) == 0x20)
131 break;
132 if (consumed >= len)
133 goto success;
134 p = inbuf + consumed;
135 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
136 len - consumed);
137 if (padd->frame_info.error) {
138 ret = -E_AAC_DECODE;
139 if (padd->error_count++ > MAX_ERRORS)
140 goto out;
141 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
142 padd->frame_info.error, padd->consumed_total,
143 consumed, padd->frame_info.bytesconsumed);
144 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
145 padd->frame_info.error));
146 consumed++; /* catch 21 */
147 goto success;
148 }
149 padd->error_count = 0;
150 consumed += padd->frame_info.bytesconsumed;
151 ret = consumed;
152 if (!padd->frame_info.samples)
153 goto out;
154 ret = -E_AAC_OVERRUN;
155 if (padd->frame_info.samples * 2 + fn->loaded > fn->bufsize)
156 goto out;
157 for (i = 0; i < padd->frame_info.samples; i++) {
158 short *s = (short *)outbuffer;
159 write_int16_host_endian(fn->buf + fn->loaded, s[i]);
160 fn->loaded += 2;
161 }
162 success:
163 ret = consumed;
164 out:
165 if (ret > 0)
166 padd->consumed_total += ret;
167 return ret;
168 }
169
170 static void aacdec_open(struct filter_node *fn)
171 {
172 fn->private_data = para_calloc(sizeof(struct private_aacdec_data));
173 struct private_aacdec_data *padd = fn->private_data;
174
175 fn->bufsize = AAC_OUTBUF_SIZE;
176 fn->buf = para_calloc(fn->bufsize);
177 padd->handle = aac_open();
178 }
179
180 static void aacdec_close(struct filter_node *fn)
181 {
182 struct private_aacdec_data *padd = fn->private_data;
183
184 NeAACDecClose(padd->handle);
185 free(fn->buf);
186 fn->buf = NULL;
187 free(padd);
188 fn->private_data = NULL;
189 }
190
191 /**
192 * the init function of the aacdec filter
193 *
194 * \param f pointer to the filter struct to initialize
195 *
196 * \sa filter::init
197 */
198 void aacdec_init(struct filter *f)
199 {
200 f->open = aacdec_open;
201 f->convert = aacdec;
202 f->close = aacdec_close;
203 }