aacdec: Remove pointless 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 *inbuf, *outbuf, *btrbuf;
101 size_t len, consumed, loaded;
102
103 next_buffer:
104 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
105 if (ret < 0)
106 goto err;
107 if (ret == 0)
108 return 0;
109 btr_merge(btrn, fn->min_iqs);
110 len = btr_next_buffer(btrn, &inbuf);
111 len = PARA_MIN(len, (size_t)8192);
112 consumed = 0;
113 if (!padd->initialized) {
114 unsigned long rate = 0;
115 unsigned char channels = 0;
116 ret = NeAACDecInit(padd->handle, (unsigned char *)inbuf,
117 len, &rate, &channels);
118 PARA_INFO_LOG("decoder init: %d\n", ret);
119 if (ret < 0) {
120 ret = -E_AACDEC_INIT;
121 goto err;
122 }
123 consumed = ret;
124 padd->sample_rate = rate;
125 padd->channels = channels;
126 PARA_INFO_LOG("rate: %u, channels: %u\n",
127 padd->sample_rate, padd->channels);
128 padd->initialized = 1;
129 }
130 if (consumed >= len)
131 goto success;
132 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
133 // padd->consumed_total, consumed, len - consumed);
134 outbuf = NeAACDecDecode(padd->handle, &padd->frame_info,
135 (unsigned char *)inbuf + consumed, len - consumed);
136 if (padd->frame_info.error) {
137 int err = padd->frame_info.error;
138 ret = -E_AAC_DECODE;
139 if (padd->error_count++ > MAX_ERRORS)
140 goto err;
141 PARA_NOTICE_LOG("error #%u: (%s)\n", padd->error_count,
142 NeAACDecGetErrorMessage(err));
143 PARA_NOTICE_LOG("consumed (total, buffer, frame): "
144 "%zu, %zu, %lu\n",
145 padd->consumed_total, consumed,
146 padd->frame_info.bytesconsumed);
147 consumed++; /* just eat one byte and hope for the best */
148 goto success;
149 }
150 padd->error_count = 0;
151 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
152 consumed += padd->frame_info.bytesconsumed;
153 if (!padd->frame_info.samples)
154 goto success;
155 btrbuf = para_malloc(2 * padd->frame_info.samples);
156 loaded = 0;
157 for (i = 0; i < padd->frame_info.samples; i++) {
158 short sh = ((short *)outbuf)[i];
159 write_int16_host_endian(btrbuf + loaded, sh);
160 loaded += 2;
161 }
162 btr_add_output(btrbuf, loaded, btrn);
163 success:
164 btr_consume(btrn, consumed);
165 padd->consumed_total += consumed;
166 goto next_buffer;
167 err:
168 assert(ret < 0);
169 btr_remove_node(&fn->btrn);
170 return ret;
171 }
172
173 /**
174 * The init function of the aacdec filter.
175 *
176 * \param f Pointer to the filter struct to initialize.
177 *
178 * \sa filter::init
179 */
180 void aacdec_filter_init(struct filter *f)
181 {
182 f->open = aacdec_open;
183 f->close = aacdec_close;
184 f->pre_select = generic_filter_pre_select;
185 f->post_select = aacdec_post_select;
186 f->execute = aacdec_execute;
187 }