fix compress filter for big endian systems
[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 int 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 padd->decoder_length = aac_find_esds(inbuf, len, &skip);
83 PARA_INFO_LOG("decoder len: %d\n", padd->decoder_length);
84 if (padd->decoder_length < 0) {
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 consumed += skip;
95 p = inbuf + consumed;
96 ret = -E_AACDEC_INIT;
97 if (NeAACDecInit2(padd->handle, p,
98 padd->decoder_length, &rate,
99 &channels) < 0)
100 goto out;
101 }
102 fc->samplerate = rate;
103 fc->channels = channels;
104 PARA_INFO_LOG("rate: %u, channels: %d\n",
105 fc->samplerate, fc->channels);
106 padd->initialized = 1;
107 }
108 if (padd->decoder_length > 0) {
109 consumed = 0;
110 if (!padd->entry) {
111 ret = aac_find_entry_point(inbuf + consumed,
112 len - consumed, &skip);
113 if (ret < 0) {
114 ret = len;
115 goto out;
116 }
117 consumed += skip;
118 padd->entry = ret;
119 PARA_INFO_LOG("entry: %zu\n", padd->entry);
120 }
121 ret = len;
122 if (padd->consumed_total + len < padd->entry)
123 goto out;
124 if (padd->consumed_total < padd->entry)
125 consumed = padd->entry - padd->consumed_total;
126 }
127 for (; consumed < len; consumed++)
128 if ((inbuf[consumed] & 0xfe) == 0x20)
129 break;
130 if (consumed >= len)
131 goto success;
132 p = inbuf + consumed;
133 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
134 len - consumed);
135 if (padd->frame_info.error) {
136 ret = -E_AAC_DECODE;
137 if (padd->error_count++ > MAX_ERRORS)
138 goto out;
139 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
140 padd->frame_info.error, padd->consumed_total,
141 consumed, padd->frame_info.bytesconsumed);
142 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
143 padd->frame_info.error));
144 consumed++; /* catch 21 */
145 goto success;
146 }
147 padd->error_count = 0;
148 consumed += padd->frame_info.bytesconsumed;
149 ret = consumed;
150 if (!padd->frame_info.samples)
151 goto out;
152 ret = -E_AAC_OVERRUN;
153 if (padd->frame_info.samples * 2 + fn->loaded > fn->bufsize)
154 goto out;
155 for (i = 0; i < padd->frame_info.samples; i++) {
156 short *s = (short *)outbuffer;
157 write_int16_host_endian(fn->buf + fn->loaded, s[i]);
158 fn->loaded += 2;
159 }
160 success:
161 ret = consumed;
162 out:
163 if (ret > 0)
164 padd->consumed_total += ret;
165 return ret;
166 }
167
168 static void aacdec_open(struct filter_node *fn)
169 {
170 fn->private_data = para_calloc(sizeof(struct private_aacdec_data));
171 struct private_aacdec_data *padd = fn->private_data;
172
173 fn->bufsize = AAC_OUTBUF_SIZE;
174 fn->buf = para_calloc(fn->bufsize);
175 padd->handle = aac_open();
176 }
177
178 static void aacdec_close(struct filter_node *fn)
179 {
180 struct private_aacdec_data *padd = fn->private_data;
181
182 NeAACDecClose(padd->handle);
183 free(fn->buf);
184 fn->buf = NULL;
185 free(padd);
186 fn->private_data = NULL;
187 }
188
189 /**
190 * the init function of the aacdec filter
191 *
192 * \param f pointer to the filter struct to initialize
193 *
194 * \sa filter::init
195 */
196 void aacdec_init(struct filter *f)
197 {
198 f->open = aacdec_open;
199 f->convert = aacdec;
200 f->close = aacdec_close;
201 }