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