Reduce decoder latency.
[paraslash.git] / aacdec_filter.c
1 /*
2 * Copyright (C) 2006-2010 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 samplerate;
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 if (!strcmp(cmd, "samplerate")) {
64 if (padd->samplerate == 0)
65 return -E_BTR_NAVAIL;
66 *result = make_message("%u", padd->samplerate);
67 return 1;
68 }
69 if (!strcmp(cmd, "channels")) {
70 if (padd->channels == 0)
71 return -E_BTR_NAVAIL;
72 *result = make_message("%u", padd->channels);
73 return 1;
74 }
75 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
76 }
77
78 static void aacdec_open(struct filter_node *fn)
79 {
80 struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
81
82 fn->private_data = padd;
83 fn->min_iqs = 2048;
84 padd->handle = aac_open();
85 }
86
87 static void aacdec_close(struct filter_node *fn)
88 {
89 struct private_aacdec_data *padd = fn->private_data;
90
91 NeAACDecClose(padd->handle);
92 free(padd);
93 fn->private_data = NULL;
94 }
95
96 static void aacdec_post_select(__a_unused struct sched *s, struct task *t)
97 {
98 struct filter_node *fn = container_of(t, struct filter_node, task);
99 struct btr_node *btrn = fn->btrn;
100 struct private_aacdec_data *padd = fn->private_data;
101 int i, ret;
102 unsigned char *p, *inbuf, *outbuffer;
103 char *btr_buf;
104 size_t len, skip, consumed, loaded, iqs;
105
106 next_buffer:
107 t->error = 0;
108 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
109 if (ret < 0)
110 goto err;
111 if (ret == 0)
112 return;
113 btr_merge(btrn, fn->min_iqs);
114 len = btr_next_buffer(btrn, (char **)&inbuf);
115 len = PARA_MAX(len, (size_t)8192);
116 consumed = 0;
117 iqs = btr_get_input_queue_size(btrn);
118 if (!padd->initialized) {
119 unsigned long rate = 0;
120 unsigned char channels = 0;
121 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
122 if (ret < 0) {
123 PARA_INFO_LOG("%s\n", para_strerror(-ret));
124 ret = NeAACDecInit(padd->handle, inbuf,
125 len, &rate, &channels);
126 PARA_INFO_LOG("decoder init: %d\n", ret);
127 if (ret < 0) {
128 ret = -E_AACDEC_INIT;
129 goto out;
130 }
131 consumed = ret;
132 } else {
133 PARA_INFO_LOG("decoder len: %lu\n",
134 padd->decoder_length);
135 consumed += skip;
136 p = inbuf + consumed;
137 ret = -E_AACDEC_INIT;
138 if (NeAACDecInit2(padd->handle, p,
139 padd->decoder_length, &rate,
140 &channels) < 0)
141 goto out;
142 }
143 padd->samplerate = rate;
144 padd->channels = channels;
145 PARA_INFO_LOG("rate: %u, channels: %d\n",
146 padd->samplerate, padd->channels);
147 padd->initialized = 1;
148 }
149 if (padd->decoder_length > 0) {
150 consumed = 0;
151 if (!padd->entry) {
152 ret = aac_find_entry_point(inbuf + consumed,
153 len - consumed, &skip);
154 if (ret < 0) {
155 ret = len;
156 goto out;
157 }
158 consumed += skip;
159 padd->entry = ret;
160 PARA_INFO_LOG("entry: %zu\n", padd->entry);
161 }
162 ret = len;
163 if (padd->consumed_total + len < padd->entry)
164 goto out;
165 if (padd->consumed_total < padd->entry)
166 consumed = padd->entry - padd->consumed_total;
167 }
168 for (; consumed < len; consumed++)
169 if ((inbuf[consumed] & 0xfe) == 0x20)
170 break;
171 if (consumed >= len)
172 goto success;
173 p = inbuf + consumed;
174 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
175 // padd->consumed_total, consumed, len - consumed);
176 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
177 len - consumed);
178 if (padd->frame_info.error) {
179 int err = padd->frame_info.error;
180 ret = -E_AAC_DECODE;
181 if (padd->error_count++ > MAX_ERRORS)
182 goto err;
183 PARA_ERROR_LOG("frame_error: %d (%s), consumed: %zu + %zd + %lu\n",
184 err, NeAACDecGetErrorMessage(padd->frame_info.error),
185 padd->consumed_total, consumed,
186 padd->frame_info.bytesconsumed);
187 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
188 padd->frame_info.error));
189 consumed++; /* catch 21 */
190 goto success;
191 }
192 padd->error_count = 0;
193 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
194 consumed += padd->frame_info.bytesconsumed;
195 ret = consumed;
196 if (!padd->frame_info.samples)
197 goto out;
198 btr_buf = para_malloc(2 * padd->frame_info.samples);
199 loaded = 0;
200 for (i = 0; i < padd->frame_info.samples; i++) {
201 short sh = ((short *)outbuffer)[i];
202 write_int16_host_endian(btr_buf + loaded, sh);
203 loaded += 2;
204 }
205 btr_add_output(btr_buf, loaded, btrn);
206 success:
207 ret = consumed;
208 out:
209 if (ret >= 0) {
210 padd->consumed_total += ret;
211 btr_consume(btrn, ret);
212 goto next_buffer;
213 }
214 err:
215 assert(ret < 0);
216 t->error = ret;
217 btr_remove_node(btrn);
218 }
219
220 /**
221 * The init function of the aacdec filter.
222 *
223 * \param f Pointer to the filter struct to initialize.
224 *
225 * \sa filter::init
226 */
227 void aacdec_filter_init(struct filter *f)
228 {
229 f->open = aacdec_open;
230 f->close = aacdec_close;
231 f->pre_select = generic_filter_pre_select;
232 f->post_select = aacdec_post_select;
233 f->execute = aacdec_execute;
234 }