2 * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
11 /** \file aacdec_filter.c paraslash's aac (m4a) decoder. */
23 /** the output buffer size */
24 #define AAC_OUTBUF_SIZE (32 * 1024)
26 /** give up decoding after that many errors */
30 * data specific to the aacdec filter
32 * \sa filter, filter_node
34 struct private_aacdec_data
{
35 /** the return value of aac_open */
36 NeAACDecHandle handle
;
37 /** info about the currently decoded frame */
38 NeAACDecFrameInfo frame_info
;
39 /** whether this instance of the aac decoder is already initialized */
42 * return value of aac_find_esds(). Used to call the right aacdec
45 unsigned long decoder_length
;
46 /** number of times the decoder returned an error */
48 /** number of bytes already consumed from the imput stream */
49 size_t consumed_total
;
50 /** return value of aac_find_entry_point */
54 static ssize_t
aacdec(char *input_buffer
, size_t len
, struct filter_node
*fn
)
56 struct private_aacdec_data
*padd
= fn
->private_data
;
57 struct filter_chain
*fc
= fn
->fc
;
59 unsigned char *p
, *outbuffer
;
60 unsigned char *inbuf
= (unsigned char*)input_buffer
;
61 size_t skip
, consumed
= 0;
63 if (fn
->loaded
> fn
->bufsize
* 3 / 5)
65 if (len
< 2048 && !*fc
->input_error
)
68 if (!padd
->initialized
) {
69 unsigned long rate
= 0;
70 unsigned char channels
= 0;
71 ret
= aac_find_esds(inbuf
, len
, &skip
, &padd
->decoder_length
);
73 PARA_INFO_LOG("%s\n", para_strerror(-ret
));
74 ret
= NeAACDecInit(padd
->handle
, inbuf
,
75 len
, &rate
, &channels
);
76 PARA_INFO_LOG("decoder init: %d\n", ret
);
83 PARA_INFO_LOG("decoder len: %lu\n",
84 padd
->decoder_length
);
88 if (NeAACDecInit2(padd
->handle
, p
,
89 padd
->decoder_length
, &rate
,
93 fc
->samplerate
= rate
;
94 fc
->channels
= channels
;
95 PARA_INFO_LOG("rate: %u, channels: %d\n",
96 fc
->samplerate
, fc
->channels
);
97 padd
->initialized
= 1;
99 if (padd
->decoder_length
> 0) {
102 ret
= aac_find_entry_point(inbuf
+ consumed
,
103 len
- consumed
, &skip
);
110 PARA_INFO_LOG("entry: %zu\n", padd
->entry
);
113 if (padd
->consumed_total
+ len
< padd
->entry
)
115 if (padd
->consumed_total
< padd
->entry
)
116 consumed
= padd
->entry
- padd
->consumed_total
;
118 for (; consumed
< len
; consumed
++)
119 if ((inbuf
[consumed
] & 0xfe) == 0x20)
123 p
= inbuf
+ consumed
;
124 outbuffer
= NeAACDecDecode(padd
->handle
, &padd
->frame_info
, p
,
126 if (padd
->frame_info
.error
) {
128 if (padd
->error_count
++ > MAX_ERRORS
)
130 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
131 padd
->frame_info
.error
, padd
->consumed_total
,
132 consumed
, padd
->frame_info
.bytesconsumed
);
133 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
134 padd
->frame_info
.error
));
135 consumed
++; /* catch 21 */
138 padd
->error_count
= 0;
139 consumed
+= padd
->frame_info
.bytesconsumed
;
141 if (!padd
->frame_info
.samples
)
143 ret
= -E_AAC_OVERRUN
;
144 if (padd
->frame_info
.samples
* 2 + fn
->loaded
> fn
->bufsize
)
146 for (i
= 0; i
< padd
->frame_info
.samples
; i
++) {
147 short *s
= (short *)outbuffer
;
148 write_int16_host_endian(fn
->buf
+ fn
->loaded
, s
[i
]);
155 padd
->consumed_total
+= ret
;
159 static void aacdec_open(struct filter_node
*fn
)
161 struct private_aacdec_data
*padd
= para_calloc(sizeof(*padd
));
163 fn
->private_data
= padd
;
164 fn
->bufsize
= AAC_OUTBUF_SIZE
;
165 fn
->buf
= para_calloc(fn
->bufsize
);
166 padd
->handle
= aac_open();
169 static void aacdec_close(struct filter_node
*fn
)
171 struct private_aacdec_data
*padd
= fn
->private_data
;
173 NeAACDecClose(padd
->handle
);
177 fn
->private_data
= NULL
;
181 * the init function of the aacdec filter
183 * \param f pointer to the filter struct to initialize
187 void aacdec_filter_init(struct filter
*f
)
189 f
->open
= aacdec_open
;
191 f
->close
= aacdec_close
;