NEWS update.
[paraslash.git] / aacdec_filter.c
1 /*
2 * Copyright (C) 2006-2011 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 /** Give up decoding after that many errors. */
27 #define MAX_ERRORS 20
28
29 /**
30 * data specific to the aacdec filter
31 *
32 * \sa filter, filter_node
33 */
34 struct private_aacdec_data {
35 /** the return value of aac_open */
36 NeAACDecHandle handle;
37 /** info about the currently decoded frame */
38 NeAACDecFrameInfo frame_info;
39 /** whether this instance of the aac decoder is already initialized */
40 int initialized;
41 /**
42 * return value of aac_find_esds(). Used to call the right aacdec
43 * init function
44 */
45 unsigned long decoder_length;
46 /** number of times the decoder returned an error */
47 unsigned error_count;
48 /** number of bytes already consumed from the imput stream */
49 size_t consumed_total;
50 /** return value of aac_find_entry_point */
51 size_t entry;
52 /** The number of channels of the current stream. */
53 unsigned int channels;
54 /** Current sample rate in Hz. */
55 unsigned int sample_rate;
56 };
57
58 static int aacdec_execute(struct btr_node *btrn, const char *cmd, char **result)
59 {
60 struct filter_node *fn = btr_context(btrn);
61 struct private_aacdec_data *padd = fn->private_data;
62
63 return decoder_execute(cmd, padd->sample_rate, padd->channels, result);
64 }
65
66 static void aacdec_open(struct filter_node *fn)
67 {
68 struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
69
70 fn->private_data = padd;
71 fn->min_iqs = 2048;
72 padd->handle = aac_open();
73 }
74
75 static void aacdec_close(struct filter_node *fn)
76 {
77 struct private_aacdec_data *padd = fn->private_data;
78
79 NeAACDecClose(padd->handle);
80 free(padd);
81 fn->private_data = NULL;
82 }
83
84 static void aacdec_post_select(__a_unused struct sched *s, struct task *t)
85 {
86 struct filter_node *fn = container_of(t, struct filter_node, task);
87 struct btr_node *btrn = fn->btrn;
88 struct private_aacdec_data *padd = fn->private_data;
89 int i, ret;
90 unsigned char *p, *inbuf, *outbuffer;
91 char *btr_buf;
92 size_t len, skip, consumed, loaded, iqs;
93
94 next_buffer:
95 t->error = 0;
96 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
97 if (ret < 0)
98 goto err;
99 if (ret == 0)
100 return;
101 btr_merge(btrn, fn->min_iqs);
102 len = btr_next_buffer(btrn, (char **)&inbuf);
103 len = PARA_MIN(len, (size_t)8192);
104 consumed = 0;
105 iqs = btr_get_input_queue_size(btrn);
106 if (!padd->initialized) {
107 unsigned long rate = 0;
108 unsigned char channels = 0;
109 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
110 if (ret < 0) {
111 PARA_INFO_LOG("%s\n", para_strerror(-ret));
112 ret = NeAACDecInit(padd->handle, inbuf,
113 len, &rate, &channels);
114 PARA_INFO_LOG("decoder init: %d\n", ret);
115 if (ret < 0) {
116 ret = -E_AACDEC_INIT;
117 goto out;
118 }
119 consumed = ret;
120 } else {
121 PARA_INFO_LOG("decoder len: %lu\n",
122 padd->decoder_length);
123 consumed += skip;
124 p = inbuf + consumed;
125 ret = -E_AACDEC_INIT;
126 if (NeAACDecInit2(padd->handle, p,
127 padd->decoder_length, &rate,
128 &channels) < 0)
129 goto out;
130 }
131 padd->sample_rate = rate;
132 padd->channels = channels;
133 PARA_INFO_LOG("rate: %u, channels: %d\n",
134 padd->sample_rate, padd->channels);
135 padd->initialized = 1;
136 }
137 if (padd->decoder_length > 0) {
138 consumed = 0;
139 if (!padd->entry) {
140 ret = aac_find_entry_point(inbuf + consumed,
141 len - consumed, &skip);
142 if (ret < 0) {
143 ret = len;
144 goto out;
145 }
146 consumed += skip;
147 padd->entry = ret;
148 PARA_INFO_LOG("entry: %zu\n", padd->entry);
149 }
150 ret = len;
151 if (padd->consumed_total + len < padd->entry)
152 goto out;
153 if (padd->consumed_total < padd->entry)
154 consumed = padd->entry - padd->consumed_total;
155 }
156 for (; consumed < len; consumed++)
157 if ((inbuf[consumed] & 0xfe) == 0x20)
158 break;
159 if (consumed >= len)
160 goto success;
161 p = inbuf + consumed;
162 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
163 // padd->consumed_total, consumed, len - consumed);
164 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
165 len - consumed);
166 if (padd->frame_info.error) {
167 int err = padd->frame_info.error;
168 ret = -E_AAC_DECODE;
169 if (padd->error_count++ > MAX_ERRORS)
170 goto err;
171 PARA_ERROR_LOG("frame_error: %d (%s), consumed: %zu + %zd + %lu\n",
172 err, NeAACDecGetErrorMessage(padd->frame_info.error),
173 padd->consumed_total, consumed,
174 padd->frame_info.bytesconsumed);
175 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
176 padd->frame_info.error));
177 if (consumed < len)
178 consumed++; /* catch 21 */
179 goto success;
180 }
181 padd->error_count = 0;
182 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
183 consumed += padd->frame_info.bytesconsumed;
184 ret = consumed;
185 if (!padd->frame_info.samples)
186 goto out;
187 btr_buf = para_malloc(2 * padd->frame_info.samples);
188 loaded = 0;
189 for (i = 0; i < padd->frame_info.samples; i++) {
190 short sh = ((short *)outbuffer)[i];
191 write_int16_host_endian(btr_buf + loaded, sh);
192 loaded += 2;
193 }
194 btr_add_output(btr_buf, loaded, btrn);
195 success:
196 ret = consumed;
197 out:
198 if (ret >= 0) {
199 padd->consumed_total += ret;
200 btr_consume(btrn, ret);
201 goto next_buffer;
202 }
203 err:
204 assert(ret < 0);
205 t->error = ret;
206 btr_remove_node(btrn);
207 }
208
209 /**
210 * The init function of the aacdec filter.
211 *
212 * \param f Pointer to the filter struct to initialize.
213 *
214 * \sa filter::init
215 */
216 void aacdec_filter_init(struct filter *f)
217 {
218 f->open = aacdec_open;
219 f->close = aacdec_close;
220 f->pre_select = generic_filter_pre_select;
221 f->post_select = aacdec_post_select;
222 f->execute = aacdec_execute;
223 }