aacdec: Remove superfluous assignment.
[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 #include <neaacdec.h>
15
16 #include "para.h"
17 #include "portable_io.h"
18 #include "list.h"
19 #include "sched.h"
20 #include "ggo.h"
21 #include "buffer_tree.h"
22 #include "filter.h"
23 #include "error.h"
24 #include "string.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 /** number of times the decoder returned an error */
42 unsigned error_count;
43 /** number of bytes already consumed from the imput stream */
44 size_t consumed_total;
45 /** The number of channels of the current stream. */
46 unsigned int channels;
47 /** Current sample rate in Hz. */
48 unsigned int sample_rate;
49 };
50
51 /*
52 * Get a new libfaad decoder handle.
53 *
54 * \return The handle returned by NeAACDecOpen().
55 */
56 static NeAACDecHandle aac_open(void)
57 {
58 NeAACDecHandle h = NeAACDecOpen();
59 NeAACDecConfigurationPtr c = NeAACDecGetCurrentConfiguration(h);
60
61 c->defObjectType = LC;
62 c->outputFormat = FAAD_FMT_16BIT;
63 c->downMatrix = 0;
64 NeAACDecSetConfiguration(h, c);
65 return h;
66 }
67
68 static int aacdec_execute(struct btr_node *btrn, const char *cmd, char **result)
69 {
70 struct filter_node *fn = btr_context(btrn);
71 struct private_aacdec_data *padd = fn->private_data;
72
73 return decoder_execute(cmd, padd->sample_rate, padd->channels, result);
74 }
75
76 static void aacdec_open(struct filter_node *fn)
77 {
78 struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
79
80 fn->private_data = padd;
81 fn->min_iqs = 2048;
82 padd->handle = aac_open();
83 }
84
85 static void aacdec_close(struct filter_node *fn)
86 {
87 struct private_aacdec_data *padd = fn->private_data;
88
89 NeAACDecClose(padd->handle);
90 free(padd);
91 fn->private_data = NULL;
92 }
93
94 static int aacdec_post_select(__a_unused struct sched *s, void *context)
95 {
96 struct filter_node *fn = context;
97 struct btr_node *btrn = fn->btrn;
98 struct private_aacdec_data *padd = fn->private_data;
99 int i, ret;
100 char *p, *inbuf, *outbuffer;
101 char *btr_buf;
102 size_t len, consumed, loaded;
103
104 next_buffer:
105 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
106 if (ret < 0)
107 goto err;
108 if (ret == 0)
109 return 0;
110 btr_merge(btrn, fn->min_iqs);
111 len = btr_next_buffer(btrn, &inbuf);
112 len = PARA_MIN(len, (size_t)8192);
113 consumed = 0;
114 if (!padd->initialized) {
115 unsigned long rate = 0;
116 unsigned char channels = 0;
117 ret = NeAACDecInit(padd->handle, (unsigned char *)inbuf,
118 len, &rate, &channels);
119 PARA_INFO_LOG("decoder init: %d\n", ret);
120 if (ret < 0) {
121 ret = -E_AACDEC_INIT;
122 goto out;
123 }
124 consumed = ret;
125 padd->sample_rate = rate;
126 padd->channels = channels;
127 PARA_INFO_LOG("rate: %u, channels: %u\n",
128 padd->sample_rate, padd->channels);
129 padd->initialized = 1;
130 }
131 if (consumed >= len)
132 goto success;
133 p = inbuf + consumed;
134 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
135 // padd->consumed_total, consumed, len - consumed);
136 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info,
137 (unsigned char *)p, len - consumed);
138 if (padd->frame_info.error) {
139 int err = padd->frame_info.error;
140 ret = -E_AAC_DECODE;
141 if (padd->error_count++ > MAX_ERRORS)
142 goto err;
143 PARA_NOTICE_LOG("error #%u: (%s)\n", padd->error_count,
144 NeAACDecGetErrorMessage(err));
145 PARA_NOTICE_LOG("consumed (total, buffer, frame): "
146 "%zu, %zu, %lu\n",
147 padd->consumed_total, consumed,
148 padd->frame_info.bytesconsumed);
149 if (consumed < len)
150 consumed++; /* catch 21 */
151 goto success;
152 }
153 padd->error_count = 0;
154 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
155 consumed += padd->frame_info.bytesconsumed;
156 if (!padd->frame_info.samples)
157 goto success;
158 btr_buf = para_malloc(2 * padd->frame_info.samples);
159 loaded = 0;
160 for (i = 0; i < padd->frame_info.samples; i++) {
161 short sh = ((short *)outbuffer)[i];
162 write_int16_host_endian(btr_buf + loaded, sh);
163 loaded += 2;
164 }
165 btr_add_output(btr_buf, loaded, btrn);
166 success:
167 ret = consumed;
168 out:
169 if (ret >= 0) {
170 padd->consumed_total += ret;
171 btr_consume(btrn, ret);
172 goto next_buffer;
173 }
174 err:
175 assert(ret < 0);
176 btr_remove_node(&fn->btrn);
177 return ret;
178 }
179
180 /**
181 * The init function of the aacdec filter.
182 *
183 * \param f Pointer to the filter struct to initialize.
184 *
185 * \sa filter::init
186 */
187 void aacdec_filter_init(struct filter *f)
188 {
189 f->open = aacdec_open;
190 f->close = aacdec_close;
191 f->pre_select = generic_filter_pre_select;
192 f->post_select = aacdec_post_select;
193 f->execute = aacdec_execute;
194 }