audiod: kill close_writer()
[paraslash.git] / aacdec.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18 /*
19 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
20 * Ahead Software AG
21 */
22
23 /** \file aacdec.c paraslash's aac (m4a) decoder */
24
25 #include "para.h"
26
27 #include "list.h"
28 #include "sched.h"
29 #include "filter.h"
30 #include "error.h"
31 #include "string.h"
32 #include "aac.h"
33
34 #define MAX_CHANNELS 6
35 /** the output buffer size */
36 #define AAC_OUTBUF_SIZE (FAAD_MIN_STREAMSIZE * MAX_CHANNELS)
37
38 /**
39 * data specific to the aacdec filter
40 *
41 * \sa filter, filter_node
42 */
43 struct private_aacdec_data {
44 NeAACDecHandle handle;
45 NeAACDecFrameInfo frame_info;
46
47 int initialized;
48 int decoder_length;
49 size_t consumed_total;
50 size_t entry;
51 };
52
53 static ssize_t aacdec(char *input_buffer, size_t len, struct filter_node *fn)
54 {
55 struct private_aacdec_data *padd = fn->private_data;
56 struct filter_chain *fc = fn->fc;
57 int i, ret;
58 unsigned char *p, *outbuffer;
59 unsigned char *inbuf = (unsigned char*)input_buffer;
60 size_t skip, consumed = 0;
61
62 if (fn->loaded > fn->bufsize * 4 / 5)
63 return 0;
64 if (len < 1000 && !*fc->input_eof)
65 return 0;
66
67 if (!padd->initialized) {
68 unsigned long rate = 0;
69 unsigned char channels = 0;
70 padd->decoder_length = aac_find_esds(inbuf, len, &skip);
71 PARA_INFO_LOG("decoder len: %d\n", padd->decoder_length);
72 if (padd->decoder_length < 0) {
73 ret = NeAACDecInit(padd->handle, inbuf,
74 len, &rate, &channels);
75 PARA_INFO_LOG("decoder init: %d\n", ret);
76 if (ret < 0) {
77 ret = -E_AACDEC_INIT;
78 goto out;
79 }
80 consumed = ret;
81 } else {
82 consumed += skip;
83 p = inbuf + consumed;
84 ret = -E_AACDEC_INIT;
85 if (NeAACDecInit2(padd->handle, p,
86 padd->decoder_length, &rate,
87 &channels) < 0)
88 goto out;
89 }
90 fc->samplerate = rate;
91 fc->channels = channels;
92 PARA_INFO_LOG("rate: %u, channels: %d\n",
93 fc->samplerate, fc->channels);
94 padd->initialized = 1;
95 }
96 if (padd->decoder_length > 0) {
97 consumed = 0;
98 if (!padd->entry) {
99 ret = aac_find_entry_point(inbuf + consumed,
100 len - consumed, &skip);
101 if (ret < 0) {
102 ret = len;
103 goto out;
104 }
105 consumed += skip;
106 padd->entry = ret;
107 PARA_INFO_LOG("entry: %zu\n", padd->entry);
108 }
109 ret = len;
110 if (padd->consumed_total + len < padd->entry)
111 goto out;
112 if (padd->consumed_total < padd->entry)
113 consumed = padd->entry - padd->consumed_total;
114 }
115 for (; consumed < len;consumed++)
116 if ((inbuf[consumed] & 0xfe) == 0x20)
117 break;
118 if (consumed >= len)
119 goto success;
120 p = inbuf + consumed;
121 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
122 len - consumed);
123 ret = -E_AAC_DECODE;
124 if (padd->frame_info.error != 0) {
125 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
126 padd->frame_info.error, padd->consumed_total,
127 consumed, padd->frame_info.bytesconsumed);
128 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
129 padd->frame_info.error));
130 consumed++; /* catch 21 */
131 goto success;
132 }
133 consumed += padd->frame_info.bytesconsumed;
134 ret = consumed;
135 if (!padd->frame_info.samples)
136 goto out;
137 for (i = 0; i < padd->frame_info.samples; i++) {
138 short *s = (short *)outbuffer;
139 fn->buf[fn->loaded++] = s[i] & 0xff;
140 fn->buf[fn->loaded++] = (s[i] >> 8) & 0xff;
141 }
142 success:
143 ret = consumed;
144 out:
145 if (ret > 0)
146 padd->consumed_total += ret;
147 return ret;
148 }
149
150 static void aacdec_open(struct filter_node *fn)
151 {
152 fn->private_data = para_calloc(sizeof(struct private_aacdec_data));
153 struct private_aacdec_data *padd = fn->private_data;
154
155 fn->bufsize = AAC_OUTBUF_SIZE;
156 fn->buf = para_calloc(fn->bufsize);
157 padd->handle = aac_open();
158 }
159
160 static void aacdec_close(struct filter_node *fn)
161 {
162 struct private_aacdec_data *padd = fn->private_data;
163
164 NeAACDecClose(padd->handle);
165 free(fn->buf);
166 fn->buf = NULL;
167 free(padd);
168 fn->private_data = NULL;
169 }
170
171 /**
172 * the init function of the aacdec filter
173 *
174 * \sa filter::init
175 */
176 void aacdec_init(struct filter *f)
177 {
178 f->open = aacdec_open;
179 f->convert = aacdec;
180 f->close = aacdec_close;
181 }