2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
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.
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.
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.
19 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
23 /** \file aacdec.c paraslash's aac (m4a) decoder */
34 /** the output buffer size */
35 #define AAC_OUTBUF_SIZE (32 * 1024)
37 /** give up decoding after that many errors */
41 * data specific to the aacdec filter
43 * \sa filter, filter_node
45 struct private_aacdec_data
{
46 /** the return value of aac_open */
47 NeAACDecHandle handle
;
48 /** info about the currently decoded frame */
49 NeAACDecFrameInfo frame_info
;
50 /** whether this instance of the aac decoder is already initialized */
53 * return value of aac_find_esds(). Used to call the right aacdec
57 /** number of times the decoder returned an error */
59 /** number of bytes already consumed from the imput stream */
60 size_t consumed_total
;
61 /** return value of aac_find_entry_point */
65 static ssize_t
aacdec(char *input_buffer
, size_t len
, struct filter_node
*fn
)
67 struct private_aacdec_data
*padd
= fn
->private_data
;
68 struct filter_chain
*fc
= fn
->fc
;
70 unsigned char *p
, *outbuffer
;
71 unsigned char *inbuf
= (unsigned char*)input_buffer
;
72 size_t skip
, consumed
= 0;
74 if (fn
->loaded
> fn
->bufsize
* 3 / 5)
76 if (len
< 2048 && !*fc
->input_eof
)
79 if (!padd
->initialized
) {
80 unsigned long rate
= 0;
81 unsigned char channels
= 0;
82 padd
->decoder_length
= aac_find_esds(inbuf
, len
, &skip
);
83 PARA_INFO_LOG("decoder len: %d\n", padd
->decoder_length
);
84 if (padd
->decoder_length
< 0) {
85 ret
= NeAACDecInit(padd
->handle
, inbuf
,
86 len
, &rate
, &channels
);
87 PARA_INFO_LOG("decoder init: %d\n", ret
);
97 if (NeAACDecInit2(padd
->handle
, p
,
98 padd
->decoder_length
, &rate
,
102 fc
->samplerate
= rate
;
103 fc
->channels
= channels
;
104 PARA_INFO_LOG("rate: %u, channels: %d\n",
105 fc
->samplerate
, fc
->channels
);
106 padd
->initialized
= 1;
108 if (padd
->decoder_length
> 0) {
111 ret
= aac_find_entry_point(inbuf
+ consumed
,
112 len
- consumed
, &skip
);
119 PARA_INFO_LOG("entry: %zu\n", padd
->entry
);
122 if (padd
->consumed_total
+ len
< padd
->entry
)
124 if (padd
->consumed_total
< padd
->entry
)
125 consumed
= padd
->entry
- padd
->consumed_total
;
127 for (; consumed
< len
; consumed
++)
128 if ((inbuf
[consumed
] & 0xfe) == 0x20)
132 p
= inbuf
+ consumed
;
133 outbuffer
= NeAACDecDecode(padd
->handle
, &padd
->frame_info
, p
,
135 if (padd
->frame_info
.error
) {
137 if (padd
->error_count
++ > MAX_ERRORS
)
139 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
140 padd
->frame_info
.error
, padd
->consumed_total
,
141 consumed
, padd
->frame_info
.bytesconsumed
);
142 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
143 padd
->frame_info
.error
));
144 consumed
++; /* catch 21 */
147 padd
->error_count
= 0;
148 consumed
+= padd
->frame_info
.bytesconsumed
;
150 if (!padd
->frame_info
.samples
)
152 ret
= -E_AAC_OVERRUN
;
153 if (padd
->frame_info
.samples
* 2 + fn
->loaded
> fn
->bufsize
)
155 for (i
= 0; i
< padd
->frame_info
.samples
; i
++) {
156 short *s
= (short *)outbuffer
;
157 write_int16_host_endian(fn
->buf
+ fn
->loaded
, s
[i
]);
164 padd
->consumed_total
+= ret
;
168 static void aacdec_open(struct filter_node
*fn
)
170 fn
->private_data
= para_calloc(sizeof(struct private_aacdec_data
));
171 struct private_aacdec_data
*padd
= fn
->private_data
;
173 fn
->bufsize
= AAC_OUTBUF_SIZE
;
174 fn
->buf
= para_calloc(fn
->bufsize
);
175 padd
->handle
= aac_open();
178 static void aacdec_close(struct filter_node
*fn
)
180 struct private_aacdec_data
*padd
= fn
->private_data
;
182 NeAACDecClose(padd
->handle
);
186 fn
->private_data
= NULL
;
190 * the init function of the aacdec filter
192 * \param f pointer to the filter struct to initialize
196 void aacdec_init(struct filter
*f
)
198 f
->open
= aacdec_open
;
200 f
->close
= aacdec_close
;