1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2009 Mohamed Tarek
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
24 #include "libfaad/common.h"
25 #include "libfaad/structs.h"
26 #include "libfaad/decoder.h"
27 #include "libfaad/output.h"
31 static void init_rm(RMContext
*rmctx
)
33 memcpy(rmctx
, (void*)(( (intptr_t)ci
->id3
->id3v2buf
+ 3 ) &~ 3), sizeof(RMContext
));
38 /* this is the codec entry point */
39 enum codec_status
codec_main(void)
41 static NeAACDecFrameInfo frame_info
;
42 NeAACDecHandle decoder
;
46 unsigned char* buffer
;
47 int err
, consumed
, pkt_offset
, skipped
= 0;
48 uint32_t s
= 0; /* sample rate */
49 unsigned char c
= 0; /* channels */
52 /* Generic codec initialisation */
53 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_INTERLEAVED
);
54 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 16);
60 DEBUGF("FAAD: Codec init error\n");
64 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
67 ci
->memset(&rmctx
,0,sizeof(RMContext
));
68 ci
->memset(&pkt
,0,sizeof(RMPacket
));
70 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
71 codec_set_replaygain(ci
->id3
);
73 /* initialise the sound converter */
74 decoder
= NeAACDecOpen();
77 DEBUGF("FAAD: Decode open error\n");
81 NeAACDecConfigurationPtr conf
= NeAACDecGetCurrentConfiguration(decoder
);
82 conf
->outputFormat
= FAAD_FMT_16BIT
;
83 NeAACDecSetConfiguration(decoder
, conf
);
85 decoder
->config
.defObjectType
= rmctx
.codec_extradata
[0];
86 decoder
->config
.defSampleRate
= rmctx
.sample_rate
;
87 err
= NeAACDecInit(decoder
, NULL
, 0, &s
, &c
);
90 DEBUGF("FAAD: DecInit: %d, %d\n", err
, decoder
->object_type
);
94 ci
->id3
->frequency
= s
;
96 ci
->advance_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
98 /* The main decoding loop */
102 if (ci
->stop_codec
|| ci
->new_track
) {
108 /* Do not allow seeking beyond the file's length */
109 if ((unsigned) ci
->seek_time
> ci
->id3
->length
) {
114 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
116 /* Seek to the start of the track */
117 if (ci
->seek_time
== 1) {
125 buffer
= ci
->request_buffer(&n
,rmctx
.audio_framesize
+ 1000);
126 pkt_offset
= skipped
- pkt
.length
;
127 consumed
= rm_get_packet(&buffer
, &rmctx
, &pkt
);
128 if(consumed
< 0 && playback_on
!= 0) {
129 if(playback_on
== -1) {
130 /* Error only if packet-parsing failed and playback hadn't started */
131 DEBUGF("rm_get_packet failed\n");
137 skipped
+= pkt
.length
;
138 if(pkt
.timestamp
> (unsigned)ci
->seek_time
) break;
139 ci
->advance_buffer(pkt
.length
);
141 ci
->seek_buffer(pkt_offset
+ rmctx
.data_offset
+ DATA_HEADER_SIZE
);
142 buffer
= ci
->request_buffer(&n
,rmctx
.audio_framesize
+ 1000);
146 /* Request the required number of bytes from the input buffer */
147 buffer
=ci
->request_buffer(&n
,rmctx
.audio_framesize
+ 1000);
148 consumed
= rm_get_packet(&buffer
, &rmctx
, &pkt
);
150 if(consumed
< 0 && playback_on
!= 0) {
151 if(playback_on
== -1) {
152 /* Error only if packet-parsing failed and playback hadn't started */
153 DEBUGF("rm_get_packet failed\n");
161 if (pkt
.timestamp
>= ci
->id3
->length
)
163 /* Decode one block - returned samples will be host-endian */
164 for(i
= 0; i
< rmctx
.sub_packet_cnt
; i
++) {
165 output
= (int32_t *)NeAACDecDecode(decoder
, &frame_info
, buffer
, rmctx
.sub_packet_lengths
[i
]);
166 buffer
+= rmctx
.sub_packet_lengths
[i
];
167 if (frame_info
.error
> 0) {
168 DEBUGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info
.error
));
172 output
= (int32_t *) output_to_PCM(decoder
, decoder
->time_out
, output
,
173 rmctx
.nb_channels
, decoder
->frameLength
, decoder
->config
.outputFormat
);
174 ci
->pcmbuf_insert(output
, NULL
, frame_info
.samples
/rmctx
.nb_channels
);
175 ci
->set_elapsed(pkt
.timestamp
);
178 ci
->advance_buffer(pkt
.length
);
184 if (ci
->request_next_track())