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