Silence two Doxygen warnings.
[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;
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 if (!padd->initialized) {
106 unsigned long rate = 0;
107 unsigned char channels = 0;
108 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
109 if (ret < 0) {
110 PARA_INFO_LOG("%s\n", para_strerror(-ret));
111 ret = NeAACDecInit(padd->handle, inbuf,
112 len, &rate, &channels);
113 PARA_INFO_LOG("decoder init: %d\n", ret);
114 if (ret < 0) {
115 ret = -E_AACDEC_INIT;
116 goto out;
117 }
118 consumed = ret;
119 } else {
120 PARA_INFO_LOG("decoder len: %lu\n",
121 padd->decoder_length);
122 consumed += skip;
123 p = inbuf + consumed;
124 ret = -E_AACDEC_INIT;
125 if (NeAACDecInit2(padd->handle, p,
126 padd->decoder_length, &rate,
127 &channels) < 0)
128 goto out;
129 }
130 padd->sample_rate = rate;
131 padd->channels = channels;
132 PARA_INFO_LOG("rate: %u, channels: %d\n",
133 padd->sample_rate, padd->channels);
134 padd->initialized = 1;
135 }
136 if (padd->decoder_length > 0) {
137 consumed = 0;
138 if (!padd->entry) {
139 ret = aac_find_entry_point(inbuf + consumed,
140 len - consumed, &skip);
141 if (ret < 0) {
142 ret = len;
143 goto out;
144 }
145 consumed += skip;
146 padd->entry = ret;
147 PARA_INFO_LOG("entry: %zu\n", padd->entry);
148 }
149 ret = len;
150 if (padd->consumed_total + len < padd->entry)
151 goto out;
152 if (padd->consumed_total < padd->entry)
153 consumed = padd->entry - padd->consumed_total;
154 }
155 for (; consumed < len; consumed++)
156 if ((inbuf[consumed] & 0xfe) == 0x20)
157 break;
158 if (consumed >= len)
159 goto success;
160 p = inbuf + consumed;
161 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
162 // padd->consumed_total, consumed, len - consumed);
163 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
164 len - consumed);
165 if (padd->frame_info.error) {
166 int err = padd->frame_info.error;
167 ret = -E_AAC_DECODE;
168 if (padd->error_count++ > MAX_ERRORS)
169 goto err;
170 /* Suppress non-fatal bitstream error message at BOF/EOF */
171 if (len < fn->min_iqs || padd->consumed_total == 0) {
172 consumed = len;
173 goto success;
174 }
175 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(err));
176 PARA_ERROR_LOG("consumed: %zu + %zd + %lu\n",
177 padd->consumed_total, consumed,
178 padd->frame_info.bytesconsumed);
179 if (consumed < len)
180 consumed++; /* catch 21 */
181 goto success;
182 }
183 padd->error_count = 0;
184 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
185 consumed += padd->frame_info.bytesconsumed;
186 ret = consumed;
187 if (!padd->frame_info.samples)
188 goto out;
189 btr_buf = para_malloc(2 * padd->frame_info.samples);
190 loaded = 0;
191 for (i = 0; i < padd->frame_info.samples; i++) {
192 short sh = ((short *)outbuffer)[i];
193 write_int16_host_endian(btr_buf + loaded, sh);
194 loaded += 2;
195 }
196 btr_add_output(btr_buf, loaded, btrn);
197 success:
198 ret = consumed;
199 out:
200 if (ret >= 0) {
201 padd->consumed_total += ret;
202 btr_consume(btrn, ret);
203 goto next_buffer;
204 }
205 err:
206 assert(ret < 0);
207 t->error = ret;
208 btr_remove_node(btrn);
209 }
210
211 /**
212 * The init function of the aacdec filter.
213 *
214 * \param f Pointer to the filter struct to initialize.
215 *
216 * \sa filter::init
217 */
218 void aacdec_filter_init(struct filter *f)
219 {
220 f->open = aacdec_open;
221 f->close = aacdec_close;
222 f->pre_select = generic_filter_pre_select;
223 f->post_select = aacdec_post_select;
224 f->execute = aacdec_execute;
225 }