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