aacdec: Kill non-btr code.
[paraslash.git] / aacdec_filter.c
1 /*
2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 /*
7 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
8 * Ahead Software AG
9 */
10
11 /** \file aacdec_filter.c paraslash's aac (m4a) decoder. */
12
13 #include <regex.h>
14 #include <stdbool.h>
15
16 #include "para.h"
17 #include "list.h"
18 #include "sched.h"
19 #include "ggo.h"
20 #include "buffer_tree.h"
21 #include "filter.h"
22 #include "error.h"
23 #include "string.h"
24 #include "aac.h"
25
26 /** the output buffer size */
27 #define AAC_OUTBUF_SIZE (32 * 1024)
28
29 /** give up decoding after that many errors */
30 #define MAX_ERRORS 20
31
32 /**
33 * data specific to the aacdec filter
34 *
35 * \sa filter, filter_node
36 */
37 struct private_aacdec_data {
38 /** the return value of aac_open */
39 NeAACDecHandle handle;
40 /** info about the currently decoded frame */
41 NeAACDecFrameInfo frame_info;
42 /** whether this instance of the aac decoder is already initialized */
43 int initialized;
44 /**
45 * return value of aac_find_esds(). Used to call the right aacdec
46 * init function
47 */
48 unsigned long decoder_length;
49 /** number of times the decoder returned an error */
50 unsigned error_count;
51 /** number of bytes already consumed from the imput stream */
52 size_t consumed_total;
53 /** return value of aac_find_entry_point */
54 size_t entry;
55 /** The number of channels of the current stream. */
56 unsigned int channels;
57 /** Current sample rate in Hz. */
58 unsigned int samplerate;
59 };
60
61 static int aacdec_execute(struct btr_node *btrn, const char *cmd, char **result)
62 {
63 struct filter_node *fn = btr_context(btrn);
64 struct private_aacdec_data *padd = fn->private_data;
65
66 if (!strcmp(cmd, "samplerate")) {
67 if (padd->samplerate == 0)
68 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
69 *result = make_message("%u", padd->samplerate);
70 return 1;
71 }
72 if (!strcmp(cmd, "channels")) {
73 if (padd->channels == 0)
74 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
75 *result = make_message("%u", padd->channels);
76 return 1;
77 }
78 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
79 }
80
81 static void aacdec_open(struct filter_node *fn)
82 {
83 struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
84
85 fn->private_data = padd;
86 fn->bufsize = AAC_OUTBUF_SIZE;
87 fn->buf = para_calloc(fn->bufsize);
88 fn->min_iqs = 2048;
89 padd->handle = aac_open();
90 }
91
92 static void aacdec_close(struct filter_node *fn)
93 {
94 struct private_aacdec_data *padd = fn->private_data;
95
96 NeAACDecClose(padd->handle);
97 free(fn->buf);
98 fn->buf = NULL;
99 free(padd);
100 fn->private_data = NULL;
101 }
102
103 static void aacdec_post_select(__a_unused struct sched *s, struct task *t)
104 {
105 struct filter_node *fn = container_of(t, struct filter_node, task);
106 struct btr_node *btrn = fn->btrn;
107 struct private_aacdec_data *padd = fn->private_data;
108 int i, ret;
109 unsigned char *p, *inbuf, *outbuffer;
110 char *btr_buf;
111 size_t len, skip, consumed, loaded, iqs;
112
113 next_buffer:
114 t->error = 0;
115 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
116 if (ret < 0)
117 goto err;
118 if (ret == 0)
119 return;
120 btr_merge(btrn, fn->min_iqs);
121 len = btr_next_buffer(btrn, (char **)&inbuf);
122 consumed = 0;
123 iqs = btr_get_input_queue_size(btrn);
124 if (!padd->initialized) {
125 unsigned long rate = 0;
126 unsigned char channels = 0;
127 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
128 if (ret < 0) {
129 PARA_INFO_LOG("%s\n", para_strerror(-ret));
130 ret = NeAACDecInit(padd->handle, inbuf,
131 len, &rate, &channels);
132 PARA_INFO_LOG("decoder init: %d\n", ret);
133 if (ret < 0) {
134 ret = -E_AACDEC_INIT;
135 goto out;
136 }
137 consumed = ret;
138 } else {
139 PARA_INFO_LOG("decoder len: %lu\n",
140 padd->decoder_length);
141 consumed += skip;
142 p = inbuf + consumed;
143 ret = -E_AACDEC_INIT;
144 if (NeAACDecInit2(padd->handle, p,
145 padd->decoder_length, &rate,
146 &channels) < 0)
147 goto out;
148 }
149 padd->samplerate = rate;
150 padd->channels = channels;
151 PARA_INFO_LOG("rate: %u, channels: %d\n",
152 padd->samplerate, padd->channels);
153 padd->initialized = 1;
154 }
155 if (padd->decoder_length > 0) {
156 consumed = 0;
157 if (!padd->entry) {
158 ret = aac_find_entry_point(inbuf + consumed,
159 len - consumed, &skip);
160 if (ret < 0) {
161 ret = len;
162 goto out;
163 }
164 consumed += skip;
165 padd->entry = ret;
166 PARA_INFO_LOG("entry: %zu\n", padd->entry);
167 }
168 ret = len;
169 if (padd->consumed_total + len < padd->entry)
170 goto out;
171 if (padd->consumed_total < padd->entry)
172 consumed = padd->entry - padd->consumed_total;
173 }
174 for (; consumed < len; consumed++)
175 if ((inbuf[consumed] & 0xfe) == 0x20)
176 break;
177 if (consumed >= len)
178 goto success;
179 p = inbuf + consumed;
180 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
181 // padd->consumed_total, consumed, len - consumed);
182 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
183 len - consumed);
184 if (padd->frame_info.error) {
185 int err = padd->frame_info.error;
186 ret = -E_AAC_DECODE;
187 if (padd->error_count++ > MAX_ERRORS)
188 goto err;
189 PARA_ERROR_LOG("frame_error: %d (%s), consumed: %zu + %zd + %lu\n",
190 err, NeAACDecGetErrorMessage(padd->frame_info.error),
191 padd->consumed_total, consumed,
192 padd->frame_info.bytesconsumed);
193 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
194 padd->frame_info.error));
195 consumed++; /* catch 21 */
196 goto success;
197 }
198 padd->error_count = 0;
199 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
200 consumed += padd->frame_info.bytesconsumed;
201 ret = consumed;
202 if (!padd->frame_info.samples)
203 goto out;
204 btr_buf = para_malloc(2 * padd->frame_info.samples);
205 loaded = 0;
206 for (i = 0; i < padd->frame_info.samples; i++) {
207 short sh = ((short *)outbuffer)[i];
208 write_int16_host_endian(btr_buf + loaded, sh);
209 loaded += 2;
210 }
211 btr_add_output(btr_buf, loaded, btrn);
212 success:
213 ret = consumed;
214 out:
215 if (ret >= 0) {
216 padd->consumed_total += ret;
217 btr_consume(btrn, ret);
218 goto next_buffer;
219 }
220 err:
221 assert(ret < 0);
222 t->error = ret;
223 btr_remove_node(btrn);
224 }
225
226 /**
227 * The init function of the aacdec filter.
228 *
229 * \param f Pointer to the filter struct to initialize.
230 *
231 * \sa filter::init
232 */
233 void aacdec_filter_init(struct filter *f)
234 {
235 f->open = aacdec_open;
236 f->close = aacdec_close;
237 f->pre_select = generic_filter_pre_select;
238 f->post_select = aacdec_post_select;
239 f->execute = aacdec_execute;
240 }