]> git.tuebingen.mpg.de Git - paraslash.git/blob - aacdec.c
aacdec: "consumed" is only used in mp4dec()
[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 /*
20  * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
21  * Ahead Software AG
22  */
23
24 /** \file aacdec.c paraslash's mp3 decoder */
25
26 #include "para.h"
27
28 #include "list.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 MAX_CHANNELS 6
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_mp4dec_data {
44         NeAACDecHandle decoder;
45         NeAACDecFrameInfo frame_info;
46
47         int initialized;
48         int decoder_length;
49         long unsigned consumed_total;
50
51         long unsigned entry;
52 };
53
54 static ssize_t mp4dec(char *input_buffer, size_t len, struct filter_node *fn)
55 {
56         struct private_mp4dec_data *padd = fn->private_data;
57         struct filter_chain_info *fci = fn->fci;
58         unsigned long rate = 0;
59         unsigned char channels = 0;
60         int i, ret, skip;
61         unsigned char *p, *outbuffer;
62         unsigned char *inbuf = (unsigned char*)input_buffer;
63         size_t consumed = 0;
64
65         if (fn->loaded > fn->bufsize * 4 / 5)
66                 return 0;
67         if (len < 1000 && !*fci->eof)
68                 return 0;
69
70         if (!padd->initialized) {
71                 padd->decoder_length = aac_find_esds(inbuf, len, &skip);
72                 PARA_INFO_LOG("decoder len: %d\n", padd->decoder_length);
73                 if (padd->decoder_length < 0) {
74                         ret = NeAACDecInit(padd->decoder, inbuf,
75                                 len, &rate, &channels);
76                         PARA_INFO_LOG("decoder init: %d\n", ret);
77                         if (ret < 0) {
78                                 ret = -E_AACDEC_INIT;
79                                 goto out;
80                         }
81                         consumed = ret;
82                 } else {
83                         consumed += skip;
84                         p = inbuf + consumed;
85                         ret = -E_AACDEC_INIT;
86                         if (NeAACDecInit2(padd->decoder, p,
87                                         padd->decoder_length, &rate,
88                                         &channels) < 0)
89                                 goto out;
90                 }
91                 fci->samplerate = rate;
92                 fci->channels = channels;
93                 PARA_INFO_LOG("rate: %u, channels: %d\n",
94                         fci->samplerate, fci->channels);
95                 padd->initialized = 1;
96         }
97         if (padd->decoder_length > 0) {
98                 consumed = 0;
99                 if (!padd->entry) {
100                         ret = aac_find_stco(inbuf + consumed, len - consumed,
101                                 &skip);
102                         if (ret < 0) {
103                                 ret = len;
104                                 goto out;
105                         }
106                         consumed += skip;
107                         padd->entry = aac_read_int32(inbuf + consumed);
108                         PARA_INFO_LOG("entry: %lu\n", padd->entry);
109                 }
110                 ret = len;
111                 if (padd->consumed_total + len < padd->entry)
112                         goto out;
113                 if (padd->consumed_total < padd->entry)
114                         consumed = padd->entry - padd->consumed_total;
115         }
116         for (; consumed < len;consumed++)
117                 if ((inbuf[consumed] & 0xfe) == 0x20)
118                         break;
119         if (consumed >= len)
120                 goto success;
121         p = inbuf + consumed;
122         outbuffer = NeAACDecDecode(padd->decoder, &padd->frame_info, p,
123                 len - consumed);
124         ret = -E_AAC_DECODE;
125         if (padd->frame_info.error != 0) {
126                 PARA_ERROR_LOG("frame_error: %d, consumed: %lu + %d + %lu\n",
127                         padd->frame_info.error, padd->consumed_total,
128                         consumed, padd->frame_info.bytesconsumed);
129                 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
130                         padd->frame_info.error));
131                 consumed++; /* catch 21 */
132                 goto success;
133         }
134         consumed += padd->frame_info.bytesconsumed;
135         ret = consumed;
136         if (!padd->frame_info.samples)
137                 goto out;
138         for (i = 0; i < padd->frame_info.samples; i++) {
139                 short *s = (short *)outbuffer;
140                 fn->buf[fn->loaded++] = s[i] & 0xff;
141                 fn->buf[fn->loaded++] = (s[i] >> 8) & 0xff;
142         }
143 success:
144         ret = consumed;
145 out:
146         if (ret > 0)
147                 padd->consumed_total += ret;
148         return ret;
149 }
150
151 static void mp4dec_open(struct filter_node *fn)
152 {
153         fn->private_data = para_calloc(sizeof(struct private_mp4dec_data));
154         struct private_mp4dec_data *padd = fn->private_data;
155
156         fn->bufsize = AAC_OUTBUF_SIZE;
157         fn->buf = para_calloc(fn->bufsize);
158         padd->decoder = aac_open();
159 }
160
161 static void mp4dec_close(struct filter_node *fn)
162 {
163         struct private_mp4dec_data *padd = fn->private_data;
164
165         NeAACDecClose(padd->decoder);
166         free(fn->buf);
167         fn->buf = NULL;
168         free(padd);
169         fn->private_data = NULL;
170 }
171
172 /**
173  * the init function of the mp3dec filter
174  *
175  * \sa filter::init
176  */
177 void aacdec_init(struct filter *f)
178 {
179         f->open = mp4dec_open;
180         f->convert = mp4dec;
181         f->close = mp4dec_close;
182 }