check_wav: return error on premature end of file.
[paraslash.git] / aacdec.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 aacdec.c paraslash's aac (m4a) decoder */
24
25 #include "para.h"
26
27 #include "list.h"
28 #include "filter.h"
29 #include "error.h"
30 #include "string.h"
31 #include "aac.h"
32
33 #define MAX_CHANNELS 6
34 /** the output buffer size */
35 #define AAC_OUTBUF_SIZE (FAAD_MIN_STREAMSIZE * MAX_CHANNELS)
36
37 /**
38 * data specific to the aacdec filter
39 *
40 * \sa filter, filter_node
41 */
42 struct private_aacdec_data {
43 NeAACDecHandle handle;
44 NeAACDecFrameInfo frame_info;
45
46 int initialized;
47 int decoder_length;
48 size_t consumed_total;
49 size_t entry;
50 };
51
52 static ssize_t aacdec(char *input_buffer, size_t len, struct filter_node *fn)
53 {
54 struct private_aacdec_data *padd = fn->private_data;
55 struct filter_chain_info *fci = fn->fci;
56 int i, ret;
57 unsigned char *p, *outbuffer;
58 unsigned char *inbuf = (unsigned char*)input_buffer;
59 size_t skip, consumed = 0;
60
61 if (fn->loaded > fn->bufsize * 4 / 5)
62 return 0;
63 if (len < 1000 && !*fci->eof)
64 return 0;
65
66 if (!padd->initialized) {
67 unsigned long rate = 0;
68 unsigned char channels = 0;
69 padd->decoder_length = aac_find_esds(inbuf, len, &skip);
70 PARA_INFO_LOG("decoder len: %d\n", padd->decoder_length);
71 if (padd->decoder_length < 0) {
72 ret = NeAACDecInit(padd->handle, inbuf,
73 len, &rate, &channels);
74 PARA_INFO_LOG("decoder init: %d\n", ret);
75 if (ret < 0) {
76 ret = -E_AACDEC_INIT;
77 goto out;
78 }
79 consumed = ret;
80 } else {
81 consumed += skip;
82 p = inbuf + consumed;
83 ret = -E_AACDEC_INIT;
84 if (NeAACDecInit2(padd->handle, p,
85 padd->decoder_length, &rate,
86 &channels) < 0)
87 goto out;
88 }
89 fci->samplerate = rate;
90 fci->channels = channels;
91 PARA_INFO_LOG("rate: %u, channels: %d\n",
92 fci->samplerate, fci->channels);
93 padd->initialized = 1;
94 }
95 if (padd->decoder_length > 0) {
96 consumed = 0;
97 if (!padd->entry) {
98 ret = aac_find_entry_point(inbuf + consumed,
99 len - consumed, &skip);
100 if (ret < 0) {
101 ret = len;
102 goto out;
103 }
104 consumed += skip;
105 padd->entry = ret;
106 PARA_INFO_LOG("entry: %zu\n", padd->entry);
107 }
108 ret = len;
109 if (padd->consumed_total + len < padd->entry)
110 goto out;
111 if (padd->consumed_total < padd->entry)
112 consumed = padd->entry - padd->consumed_total;
113 }
114 for (; consumed < len;consumed++)
115 if ((inbuf[consumed] & 0xfe) == 0x20)
116 break;
117 if (consumed >= len)
118 goto success;
119 p = inbuf + consumed;
120 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
121 len - consumed);
122 ret = -E_AAC_DECODE;
123 if (padd->frame_info.error != 0) {
124 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
125 padd->frame_info.error, padd->consumed_total,
126 consumed, padd->frame_info.bytesconsumed);
127 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
128 padd->frame_info.error));
129 consumed++; /* catch 21 */
130 goto success;
131 }
132 consumed += padd->frame_info.bytesconsumed;
133 ret = consumed;
134 if (!padd->frame_info.samples)
135 goto out;
136 for (i = 0; i < padd->frame_info.samples; i++) {
137 short *s = (short *)outbuffer;
138 fn->buf[fn->loaded++] = s[i] & 0xff;
139 fn->buf[fn->loaded++] = (s[i] >> 8) & 0xff;
140 }
141 success:
142 ret = consumed;
143 out:
144 if (ret > 0)
145 padd->consumed_total += ret;
146 return ret;
147 }
148
149 static void aacdec_open(struct filter_node *fn)
150 {
151 fn->private_data = para_calloc(sizeof(struct private_aacdec_data));
152 struct private_aacdec_data *padd = fn->private_data;
153
154 fn->bufsize = AAC_OUTBUF_SIZE;
155 fn->buf = para_calloc(fn->bufsize);
156 padd->handle = aac_open();
157 }
158
159 static void aacdec_close(struct filter_node *fn)
160 {
161 struct private_aacdec_data *padd = fn->private_data;
162
163 NeAACDecClose(padd->handle);
164 free(fn->buf);
165 fn->buf = NULL;
166 free(padd);
167 fn->private_data = NULL;
168 }
169
170 /**
171 * the init function of the aacdec filter
172 *
173 * \sa filter::init
174 */
175 void aacdec_init(struct filter *f)
176 {
177 f->open = aacdec_open;
178 f->convert = aacdec;
179 f->close = aacdec_close;
180 }