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