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