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