2 * Copyright 2006 dnk <dnk@bjum.net>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * 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
25 #include "read_wrapper.h"
31 #include <sys/types.h>
35 /* FAAD_MIN_STREAMSIZE == 768, 6 == # of channels */
36 #define BUFFER_SIZE (FAAD_MIN_STREAMSIZE * 6 * 4)
39 char rbuf
[BUFFER_SIZE
];
43 unsigned char channels
;
44 unsigned long sample_rate
;
49 faacDecHandle decoder
; /* typedef void * */
52 static inline int buffer_length(const struct input_plugin_data
*ip_data
)
54 struct aac_private
*priv
= ip_data
->private;
56 return priv
->rbuf_len
- priv
->rbuf_pos
;
59 static inline void *buffer_data(const struct input_plugin_data
*ip_data
)
61 struct aac_private
*priv
= ip_data
->private;
63 return priv
->rbuf
+ priv
->rbuf_pos
;
66 static int buffer_fill(struct input_plugin_data
*ip_data
)
68 struct aac_private
*priv
= ip_data
->private;
71 if (priv
->rbuf_pos
> 0) {
72 priv
->rbuf_len
= buffer_length(ip_data
);
73 memmove(priv
->rbuf
, priv
->rbuf
+ priv
->rbuf_pos
, priv
->rbuf_len
);
77 if (priv
->rbuf_len
== BUFFER_SIZE
)
80 n
= read_wrapper(ip_data
, priv
->rbuf
+ priv
->rbuf_len
, BUFFER_SIZE
- priv
->rbuf_len
);
90 static inline void buffer_consume(struct input_plugin_data
*ip_data
, int n
)
92 struct aac_private
*priv
= ip_data
->private;
94 BUG_ON(n
> buffer_length(ip_data
));
99 static int buffer_fill_min(struct input_plugin_data
*ip_data
, int len
)
103 BUG_ON(len
> BUFFER_SIZE
);
105 while (buffer_length(ip_data
) < len
) {
106 rc
= buffer_fill(ip_data
);
113 /* 'data' must point to at least 6 bytes of data */
114 static inline int parse_frame(const unsigned char data
[6])
118 /* http://www.audiocoding.com/modules/wiki/?page=ADTS */
120 /* first 12 bits must be set */
123 if ((data
[1] & 0xF0) != 0xF0)
126 /* layer is always '00' */
127 if ((data
[1] & 0x06) != 0x00)
130 /* frame length is stored in 13 bits */
131 len
= data
[3] << 11; /* ..1100000000000 */
132 len
|= data
[4] << 3; /* ..xx11111111xxx */
133 len
|= data
[5] >> 5; /* ..xxxxxxxxxx111 */
134 len
&= 0x1FFF; /* 13 bits */
138 /* scans forward to the next aac frame and makes sure
139 * the entire frame is in the buffer.
141 static int buffer_fill_frame(struct input_plugin_data
*ip_data
)
148 /* need at least 6 bytes of data */
149 rc
= buffer_fill_min(ip_data
, 6);
153 len
= buffer_length(ip_data
);
154 data
= buffer_data(ip_data
);
156 /* scan for a frame */
157 for (n
= 0; n
< len
- 5; n
++) {
158 /* give up after 32KB */
160 d_print("no frame found!\n");
161 /* FIXME: set errno? */
165 /* see if there's a frame at this location */
166 rc
= parse_frame(data
+ n
);
170 /* found a frame, consume all data up to the frame */
171 buffer_consume(ip_data
, n
);
173 /* rc == frame length */
174 rc
= buffer_fill_min(ip_data
, rc
);
181 /* consume what we used */
182 buffer_consume(ip_data
, n
);
187 static int aac_open(struct input_plugin_data
*ip_data
)
189 struct aac_private
*priv
;
190 faacDecConfigurationPtr neaac_cfg
;
193 /* init private struct */
194 priv
= xnew0(struct aac_private
, 1);
195 priv
->decoder
= faacDecOpen();
196 ip_data
->private = priv
;
198 /* set decoder config */
199 neaac_cfg
= faacDecGetCurrentConfiguration(priv
->decoder
);
200 neaac_cfg
->outputFormat
= FAAD_FMT_16BIT
; /* force 16 bit audio */
201 neaac_cfg
->downMatrix
= 1; /* 5.1 -> stereo */
202 neaac_cfg
->dontUpSampleImplicitSBR
= 0; /* upsample, please! */
203 faacDecSetConfiguration(priv
->decoder
, neaac_cfg
);
206 if (buffer_fill_frame(ip_data
) <= 0) {
207 ret
= -IP_ERROR_FILE_FORMAT
;
211 /* in case of a bug, make sure there is at least some data
212 * in the buffer for faacDecInit() to work with.
214 if (buffer_fill_min(ip_data
, 256) <= 0) {
215 d_print("not enough data\n");
216 ret
= -IP_ERROR_FILE_FORMAT
;
220 /* init decoder, returns the length of the header (if any) */
221 n
= faacDecInit(priv
->decoder
, buffer_data(ip_data
), buffer_length(ip_data
),
222 &priv
->sample_rate
, &priv
->channels
);
224 d_print("faacDecInit failed\n");
225 ret
= -IP_ERROR_FILE_FORMAT
;
229 /* skip the header */
230 d_print("skipping header (%d bytes)\n", n
);
232 buffer_consume(ip_data
, n
);
234 d_print("sample rate %luhz, channels %u\n", priv
->sample_rate
, priv
->channels
);
236 /*faacDecInitDRM(priv->decoder, priv->sample_rate, priv->channels);*/
238 ip_data
->sf
= sf_rate(priv
->sample_rate
) | sf_channels(priv
->channels
) | sf_bits(16) | sf_signed(1);
239 #if defined(WORDS_BIGENDIAN)
240 ip_data
->sf
|= sf_bigendian(1);
244 faacDecClose(priv
->decoder
);
249 static int aac_close(struct input_plugin_data
*ip_data
)
251 struct aac_private
*priv
= ip_data
->private;
253 faacDecClose(priv
->decoder
);
255 ip_data
->private = NULL
;
259 /* returns -1 on fatal errors
260 * returns -2 on non-fatal errors
262 * number of bytes put in 'buffer' on success */
263 static int decode_one_frame(struct input_plugin_data
*ip_data
, void *buffer
, int count
)
265 struct aac_private
*priv
= ip_data
->private;
266 unsigned char *aac_data
;
267 unsigned int aac_data_size
;
268 faacDecFrameInfo frame_info
;
272 rc
= buffer_fill_frame(ip_data
);
276 aac_data
= buffer_data(ip_data
);
277 aac_data_size
= buffer_length(ip_data
);
279 /* aac data -> raw pcm */
280 sample_buf
= faacDecDecode(priv
->decoder
, &frame_info
, aac_data
, aac_data_size
);
282 buffer_consume(ip_data
, frame_info
.bytesconsumed
);
284 if (!sample_buf
|| frame_info
.bytesconsumed
<= 0) {
285 d_print("fatal error: %s\n", faacDecGetErrorMessage(frame_info
.error
));
290 if (frame_info
.error
!= 0) {
291 d_print("frame error: %s\n", faacDecGetErrorMessage(frame_info
.error
));
295 if (frame_info
.samples
<= 0)
298 if (frame_info
.channels
!= priv
->channels
|| frame_info
.samplerate
!= priv
->sample_rate
) {
299 d_print("invalid channel or sample_rate count\n");
304 bytes
= frame_info
.samples
* 2;
307 /* decoded too much, keep overflow */
308 priv
->overflow_buf
= sample_buf
+ count
;
309 priv
->overflow_buf_len
= bytes
- count
;
310 memcpy(buffer
, sample_buf
, count
);
313 memcpy(buffer
, sample_buf
, bytes
);
318 static int aac_read(struct input_plugin_data
*ip_data
, char *buffer
, int count
)
320 struct aac_private
*priv
= ip_data
->private;
323 /* use overflow from previous call (if any) */
324 if (priv
->overflow_buf_len
) {
325 int len
= priv
->overflow_buf_len
;
330 memcpy(buffer
, priv
->overflow_buf
, len
);
331 priv
->overflow_buf
+= len
;
332 priv
->overflow_buf_len
-= len
;
337 rc
= decode_one_frame(ip_data
, buffer
, count
);
342 static int aac_seek(struct input_plugin_data
*ip_data
, double offset
)
344 return -IP_ERROR_FUNCTION_NOT_SUPPORTED
;
347 /* copied from mad.c */
348 static void get_comment(struct keyval
*c
, int *iptr
, ID3
*id3
, enum id3_key key
, const char *key_name
)
352 c
[i
].val
= id3_get_comment(id3
, key
);
353 if (c
[i
].val
== NULL
)
355 c
[i
].key
= xstrdup(key_name
);
359 /* based on read_comments from mad.c */
360 static int aac_read_comments(struct input_plugin_data
*ip_data
,
361 struct keyval
**comments
)
366 fd
= open(ip_data
->filename
, O_RDONLY
);
372 rc
= id3_read_tags(id3
, fd
, ID3_V1
| ID3_V2
);
374 d_print("error: %s\n", strerror(errno
));
375 *comments
= xnew0(struct keyval
, 1);
379 *comments
= xnew0(struct keyval
, NUM_ID3_KEYS
+ 1);
381 get_comment(*comments
, &i
, id3
, ID3_ARTIST
, "artist");
382 get_comment(*comments
, &i
, id3
, ID3_ALBUM
, "album");
383 get_comment(*comments
, &i
, id3
, ID3_TITLE
, "title");
384 get_comment(*comments
, &i
, id3
, ID3_DATE
, "date");
385 get_comment(*comments
, &i
, id3
, ID3_GENRE
, "genre");
386 get_comment(*comments
, &i
, id3
, ID3_DISC
, "discnumber");
387 get_comment(*comments
, &i
, id3
, ID3_TRACK
, "tracknumber");
388 get_comment(*comments
, &i
, id3
, ID3_ALBUMARTIST
, "albumartist");
395 static int aac_duration(struct input_plugin_data
*ip_data
)
397 struct aac_private
*priv
= ip_data
->private;
398 faacDecFrameInfo frame_info
;
399 int samples
= 0, bytes
= 0, frames
= 0;
403 file_size
= lseek(ip_data
->fd
, 0, SEEK_END
);
405 return -IP_ERROR_FUNCTION_NOT_SUPPORTED
;
407 /* guess track length by decoding the first 10 frames */
408 while (frames
< 10) {
409 if (buffer_fill_frame(ip_data
) <= 0)
412 sample_buf
= faacDecDecode(priv
->decoder
, &frame_info
,
413 buffer_data(ip_data
), buffer_length(ip_data
));
414 if (frame_info
.error
== 0 && frame_info
.samples
> 0) {
415 samples
+= frame_info
.samples
;
416 bytes
+= frame_info
.bytesconsumed
;
419 if (frame_info
.bytesconsumed
== 0)
422 buffer_consume(ip_data
, frame_info
.bytesconsumed
);
426 return -IP_ERROR_FUNCTION_NOT_SUPPORTED
;
429 samples
/= priv
->channels
;
432 return ((file_size
/ bytes
) * samples
) / priv
->sample_rate
;
435 const struct input_plugin_ops ip_ops
= {
440 .read_comments
= aac_read_comments
,
441 .duration
= aac_duration
444 const char * const ip_extensions
[] = { "aac", NULL
};
445 const char * const ip_mime_types
[] = { "audio/aac", "audio/aacp", NULL
};