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