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