]> git.tuebingen.mpg.de Git - paraslash.git/blob - aacdec.c
315f338ced283c063d9012e20040fb0b3f0b82a1
[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         int consumed;
50         long unsigned consumed_total;
51
52         long unsigned entry;
53 };
54
55 static ssize_t mp4dec(char *input_buffer, size_t len, struct filter_node *fn)
56 {
57         struct private_mp4dec_data *padd = fn->private_data;
58         struct filter_chain_info *fci = fn->fci;
59         unsigned long rate = 0;
60         unsigned char channels = 0;
61         int i, ret, skip;
62         unsigned char *p, *outbuffer;
63         unsigned char *inbuf = (unsigned char*)input_buffer;
64
65         if (fn->loaded > fn->bufsize * 4 / 5)
66                 return 0;
67         if (len < 1000 && !*fci->eof)
68                 return 0;
69         padd->consumed = 0;
70
71         if (!padd->initialized) {
72                 padd->decoder_length = aac_find_esds(inbuf, len, &skip);
73                 PARA_INFO_LOG("decoder len: %d\n", padd->decoder_length);
74                 if (padd->decoder_length < 0) {
75                         ret = NeAACDecInit(padd->decoder, inbuf,
76                                 len, &rate, &channels);
77                         PARA_INFO_LOG("decoder init: %d\n", ret);
78                         if (ret < 0) {
79                                 ret = -E_AACDEC_INIT;
80                                 goto out;
81                         }
82                         padd->consumed = ret;
83                 } else {
84                         padd->consumed += skip;
85                         p = inbuf + padd->consumed;
86                         ret = -E_AACDEC_INIT;
87                         if (NeAACDecInit2(padd->decoder, p,
88                                         padd->decoder_length, &rate,
89                                         &channels) < 0)
90                                 goto out;
91                 }
92                 fci->samplerate = rate;
93                 fci->channels = channels;
94                 PARA_INFO_LOG("rate: %u, channels: %d\n",
95                         fci->samplerate, fci->channels);
96                 padd->initialized = 1;
97         }
98         if (padd->decoder_length > 0) {
99                 padd->consumed = 0;
100                 if (!padd->entry) {
101                         ret = aac_find_stco(inbuf + padd->consumed,
102                                 len - padd->consumed, &skip);
103                         if (ret < 0) {
104                                 ret = len;
105                                 goto out;
106                         }
107                         padd->consumed += skip;
108                         padd->entry = aac_read_int32(inbuf + padd->consumed);
109                         PARA_INFO_LOG("entry: %lu\n", padd->entry);
110                 }
111                 ret = len;
112                 if (padd->consumed_total + len < padd->entry)
113                         goto out;
114                 if (padd->consumed_total < padd->entry)
115                         padd->consumed = padd->entry - padd->consumed_total;
116         }
117         for (; padd->consumed < len; padd->consumed++)
118                 if ((inbuf[padd->consumed] & 0xfe) == 0x20)
119                         break;
120         if (padd->consumed >= len)
121                 goto success;
122         p = inbuf + padd->consumed;
123         outbuffer = NeAACDecDecode(padd->decoder, &padd->frame_info, p,
124                 len - padd->consumed);
125         ret = -E_AAC_DECODE;
126         if (padd->frame_info.error != 0) {
127                 PARA_ERROR_LOG("frame_error: %d, consumed: %lu + %d + %lu\n",
128                         padd->frame_info.error, padd->consumed_total,
129                         padd->consumed, padd->frame_info.bytesconsumed);
130                 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
131                         padd->frame_info.error));
132                 padd->consumed++; /* catch 21 */
133                 goto success;
134         }
135         padd->consumed += padd->frame_info.bytesconsumed;
136         ret = padd->consumed;
137         if (!padd->frame_info.samples)
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 = padd->consumed;
146 out:
147         if (ret > 0)
148                 padd->consumed_total += ret;
149         return ret;
150 }
151
152 static void mp4dec_open(struct filter_node *fn)
153 {
154         fn->private_data = para_calloc(sizeof(struct private_mp4dec_data));
155         struct private_mp4dec_data *padd = fn->private_data;
156
157         fn->bufsize = AAC_OUTBUF_SIZE;
158         fn->buf = para_calloc(fn->bufsize);
159         padd->decoder = aac_open();
160 }
161
162 static void mp4dec_close(struct filter_node *fn)
163 {
164         struct private_mp4dec_data *padd = fn->private_data;
165
166         NeAACDecClose(padd->decoder);
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 mp3dec filter
175  *
176  * \sa filter::init
177  */
178 void aacdec_init(struct filter *f)
179 {
180         f->open = mp4dec_open;
181         f->convert = mp4dec;
182         f->close = mp4dec_close;
183 }