trivial loglevel adjustements
[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 /** the output buffer size */
35 #define AAC_OUTBUF_SIZE (32 * 1024)
36
37 /**
38 * data specific to the aacdec filter
39 *
40 * \sa filter, filter_node
41 */
42 struct private_aacdec_data {
43 NeAACDecHandle handle;
44 NeAACDecFrameInfo frame_info;
45
46 int initialized;
47 int decoder_length;
48 size_t consumed_total;
49 size_t entry;
50 };
51
52 static ssize_t aacdec(char *input_buffer, size_t len, struct filter_node *fn)
53 {
54 struct private_aacdec_data *padd = fn->private_data;
55 struct filter_chain *fc = fn->fc;
56 int i, ret;
57 unsigned char *p, *outbuffer;
58 unsigned char *inbuf = (unsigned char*)input_buffer;
59 size_t skip, consumed = 0;
60
61 if (fn->loaded > fn->bufsize * 3 / 5)
62 return 0;
63 if (len < 2048 && !*fc->input_eof)
64 return 0;
65
66 if (!padd->initialized) {
67 unsigned long rate = 0;
68 unsigned char channels = 0;
69 padd->decoder_length = aac_find_esds(inbuf, len, &skip);
70 PARA_INFO_LOG("decoder len: %d\n", padd->decoder_length);
71 if (padd->decoder_length < 0) {
72 ret = NeAACDecInit(padd->handle, inbuf,
73 len, &rate, &channels);
74 PARA_INFO_LOG("decoder init: %d\n", ret);
75 if (ret < 0) {
76 ret = -E_AACDEC_INIT;
77 goto out;
78 }
79 consumed = ret;
80 } else {
81 consumed += skip;
82 p = inbuf + consumed;
83 ret = -E_AACDEC_INIT;
84 if (NeAACDecInit2(padd->handle, p,
85 padd->decoder_length, &rate,
86 &channels) < 0)
87 goto out;
88 }
89 fc->samplerate = rate;
90 fc->channels = channels;
91 PARA_INFO_LOG("rate: %u, channels: %d\n",
92 fc->samplerate, fc->channels);
93 padd->initialized = 1;
94 }
95 if (padd->decoder_length > 0) {
96 consumed = 0;
97 if (!padd->entry) {
98 ret = aac_find_entry_point(inbuf + consumed,
99 len - consumed, &skip);
100 if (ret < 0) {
101 ret = len;
102 goto out;
103 }
104 consumed += skip;
105 padd->entry = ret;
106 PARA_INFO_LOG("entry: %zu\n", padd->entry);
107 }
108 ret = len;
109 if (padd->consumed_total + len < padd->entry)
110 goto out;
111 if (padd->consumed_total < padd->entry)
112 consumed = padd->entry - padd->consumed_total;
113 }
114 for (; consumed < len; consumed++)
115 if ((inbuf[consumed] & 0xfe) == 0x20)
116 break;
117 if (consumed >= len)
118 goto success;
119 p = inbuf + consumed;
120 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
121 len - consumed);
122 ret = -E_AAC_DECODE;
123 if (padd->frame_info.error) {
124 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
125 padd->frame_info.error, padd->consumed_total,
126 consumed, padd->frame_info.bytesconsumed);
127 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
128 padd->frame_info.error));
129 consumed++; /* catch 21 */
130 goto success;
131 }
132 consumed += padd->frame_info.bytesconsumed;
133 ret = consumed;
134 if (!padd->frame_info.samples)
135 goto out;
136 ret = -E_AAC_OVERRUN;
137 if (padd->frame_info.samples * 2 + fn->loaded > fn->bufsize)
138 goto out;
139 for (i = 0; i < padd->frame_info.samples; i++) {
140 short *s = (short *)outbuffer;
141 fn->buf[fn->loaded++] = s[i] & 0xff;
142 fn->buf[fn->loaded++] = (s[i] >> 8) & 0xff;
143 }
144 success:
145 ret = consumed;
146 out:
147 if (ret > 0)
148 padd->consumed_total += ret;
149 return ret;
150 }
151
152 static void aacdec_open(struct filter_node *fn)
153 {
154 fn->private_data = para_calloc(sizeof(struct private_aacdec_data));
155 struct private_aacdec_data *padd = fn->private_data;
156
157 fn->bufsize = AAC_OUTBUF_SIZE;
158 fn->buf = para_calloc(fn->bufsize);
159 padd->handle = aac_open();
160 }
161
162 static void aacdec_close(struct filter_node *fn)
163 {
164 struct private_aacdec_data *padd = fn->private_data;
165
166 NeAACDecClose(padd->handle);
167 free(fn->buf);
168 fn->buf = NULL;
169 free(padd);
170 fn->private_data = NULL;
171 }
172
173 /**
174 * the init function of the aacdec filter
175 *
176 * \sa filter::init
177 */
178 void aacdec_init(struct filter *f)
179 {
180 f->open = aacdec_open;
181 f->convert = aacdec;
182 f->close = aacdec_close;
183 }