Pass full argument list to mood parsers.
[paraslash.git] / aacdec_filter.c
1 /*
2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
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 "para.h"
14
15 #include "list.h"
16 #include "sched.h"
17 #include "ggo.h"
18 #include "filter.h"
19 #include "error.h"
20 #include "string.h"
21 #include "aac.h"
22
23 /** the output buffer size */
24 #define AAC_OUTBUF_SIZE (32 * 1024)
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 /**
42 * return value of aac_find_esds(). Used to call the right aacdec
43 * init function
44 */
45 unsigned long decoder_length;
46 /** number of times the decoder returned an error */
47 unsigned error_count;
48 /** number of bytes already consumed from the imput stream */
49 size_t consumed_total;
50 /** return value of aac_find_entry_point */
51 size_t entry;
52 };
53
54 static ssize_t aacdec(char *input_buffer, size_t len, struct filter_node *fn)
55 {
56 struct private_aacdec_data *padd = fn->private_data;
57 struct filter_chain *fc = fn->fc;
58 int i, ret;
59 unsigned char *p, *outbuffer;
60 unsigned char *inbuf = (unsigned char*)input_buffer;
61 size_t skip, consumed = 0;
62
63 if (fn->loaded > fn->bufsize * 3 / 5)
64 return 0;
65 ret = *fc->input_error;
66 if (ret < 0)
67 return ret;
68 if (len < 2048)
69 return 0;
70
71 if (!padd->initialized) {
72 unsigned long rate = 0;
73 unsigned char channels = 0;
74 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
75 if (ret < 0) {
76 PARA_INFO_LOG("%s\n", para_strerror(-ret));
77 ret = NeAACDecInit(padd->handle, inbuf,
78 len, &rate, &channels);
79 PARA_INFO_LOG("decoder init: %d\n", ret);
80 if (ret < 0) {
81 ret = -E_AACDEC_INIT;
82 goto out;
83 }
84 consumed = ret;
85 } else {
86 PARA_INFO_LOG("decoder len: %lu\n",
87 padd->decoder_length);
88 consumed += skip;
89 p = inbuf + consumed;
90 ret = -E_AACDEC_INIT;
91 if (NeAACDecInit2(padd->handle, p,
92 padd->decoder_length, &rate,
93 &channels) < 0)
94 goto out;
95 }
96 fc->samplerate = rate;
97 fc->channels = channels;
98 PARA_INFO_LOG("rate: %u, channels: %d\n",
99 fc->samplerate, fc->channels);
100 padd->initialized = 1;
101 }
102 if (padd->decoder_length > 0) {
103 consumed = 0;
104 if (!padd->entry) {
105 ret = aac_find_entry_point(inbuf + consumed,
106 len - consumed, &skip);
107 if (ret < 0) {
108 ret = len;
109 goto out;
110 }
111 consumed += skip;
112 padd->entry = ret;
113 PARA_INFO_LOG("entry: %zu\n", padd->entry);
114 }
115 ret = len;
116 if (padd->consumed_total + len < padd->entry)
117 goto out;
118 if (padd->consumed_total < padd->entry)
119 consumed = padd->entry - padd->consumed_total;
120 }
121 for (; consumed < len; consumed++)
122 if ((inbuf[consumed] & 0xfe) == 0x20)
123 break;
124 if (consumed >= len)
125 goto success;
126 p = inbuf + consumed;
127 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
128 len - consumed);
129 if (padd->frame_info.error) {
130 ret = -E_AAC_DECODE;
131 if (padd->error_count++ > MAX_ERRORS)
132 goto out;
133 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
134 padd->frame_info.error, padd->consumed_total,
135 consumed, padd->frame_info.bytesconsumed);
136 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
137 padd->frame_info.error));
138 consumed++; /* catch 21 */
139 goto success;
140 }
141 padd->error_count = 0;
142 consumed += padd->frame_info.bytesconsumed;
143 ret = consumed;
144 if (!padd->frame_info.samples)
145 goto out;
146 ret = -E_AAC_OVERRUN;
147 if (padd->frame_info.samples * 2 + fn->loaded > fn->bufsize)
148 goto out;
149 for (i = 0; i < padd->frame_info.samples; i++) {
150 short *s = (short *)outbuffer;
151 write_int16_host_endian(fn->buf + fn->loaded, s[i]);
152 fn->loaded += 2;
153 }
154 success:
155 ret = consumed;
156 out:
157 if (ret > 0)
158 padd->consumed_total += ret;
159 return ret;
160 }
161
162 static void aacdec_open(struct filter_node *fn)
163 {
164 struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
165
166 fn->private_data = padd;
167 fn->bufsize = AAC_OUTBUF_SIZE;
168 fn->buf = para_calloc(fn->bufsize);
169 padd->handle = aac_open();
170 }
171
172 static void aacdec_close(struct filter_node *fn)
173 {
174 struct private_aacdec_data *padd = fn->private_data;
175
176 NeAACDecClose(padd->handle);
177 free(fn->buf);
178 fn->buf = NULL;
179 free(padd);
180 fn->private_data = NULL;
181 }
182
183 /**
184 * the init function of the aacdec filter
185 *
186 * \param f pointer to the filter struct to initialize
187 *
188 * \sa filter::init
189 */
190 void aacdec_filter_init(struct filter *f)
191 {
192 f->open = aacdec_open;
193 f->convert = aacdec;
194 f->close = aacdec_close;
195 }