aacdec: Remove pointless variable p.
[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 *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 err;
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 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
134 // padd->consumed_total, consumed, len - consumed);
135 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info,
136 (unsigned char *)inbuf + consumed, len - consumed);
137 if (padd->frame_info.error) {
138 int err = padd->frame_info.error;
139 ret = -E_AAC_DECODE;
140 if (padd->error_count++ > MAX_ERRORS)
141 goto err;
142 PARA_NOTICE_LOG("error #%u: (%s)\n", padd->error_count,
143 NeAACDecGetErrorMessage(err));
144 PARA_NOTICE_LOG("consumed (total, buffer, frame): "
145 "%zu, %zu, %lu\n",
146 padd->consumed_total, consumed,
147 padd->frame_info.bytesconsumed);
148 if (consumed < len)
149 consumed++; /* catch 21 */
150 goto success;
151 }
152 padd->error_count = 0;
153 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
154 consumed += padd->frame_info.bytesconsumed;
155 if (!padd->frame_info.samples)
156 goto success;
157 btr_buf = para_malloc(2 * padd->frame_info.samples);
158 loaded = 0;
159 for (i = 0; i < padd->frame_info.samples; i++) {
160 short sh = ((short *)outbuffer)[i];
161 write_int16_host_endian(btr_buf + loaded, sh);
162 loaded += 2;
163 }
164 btr_add_output(btr_buf, loaded, btrn);
165 success:
166 ret = consumed;
167 padd->consumed_total += ret;
168 btr_consume(btrn, ret);
169 goto next_buffer;
170 err:
171 assert(ret < 0);
172 btr_remove_node(&fn->btrn);
173 return ret;
174 }
175
176 /**
177 * The init function of the aacdec filter.
178 *
179 * \param f Pointer to the filter struct to initialize.
180 *
181 * \sa filter::init
182 */
183 void aacdec_filter_init(struct filter *f)
184 {
185 f->open = aacdec_open;
186 f->close = aacdec_close;
187 f->pre_select = generic_filter_pre_select;
188 f->post_select = aacdec_post_select;
189 f->execute = aacdec_execute;
190 }