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