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
));
36 static RMContext rmctx
;
39 /* this is the codec entry point */
40 enum codec_status
codec_main(enum codec_entry_call_reason reason
)
42 if (reason
== CODEC_LOAD
) {
43 /* Generic codec initialisation */
44 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
45 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 29);
51 /* this is called for each file to process */
52 enum codec_status
codec_run(void)
54 static NeAACDecFrameInfo frame_info
;
55 NeAACDecHandle decoder
;
58 unsigned char* buffer
;
59 int err
, consumed
, pkt_offset
, skipped
= 0;
60 uint32_t s
= 0; /* sample rate */
61 unsigned char c
= 0; /* channels */
65 enum codec_command_action action
= CODEC_ACTION_NULL
;
68 DEBUGF("FAAD: Codec init error\n");
72 resume_offset
= ci
->id3
->offset
;
74 ci
->memset(&rmctx
,0,sizeof(RMContext
));
75 ci
->memset(&pkt
,0,sizeof(RMPacket
));
80 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
81 codec_set_replaygain(ci
->id3
);
83 /* initialise the sound converter */
84 decoder
= NeAACDecOpen();
87 DEBUGF("FAAD: Decode open error\n");
91 NeAACDecConfigurationPtr conf
= NeAACDecGetCurrentConfiguration(decoder
);
92 conf
->outputFormat
= FAAD_FMT_16BIT
; /* irrelevant, we don't convert */
93 NeAACDecSetConfiguration(decoder
, conf
);
95 decoder
->config
.defObjectType
= rmctx
.codec_extradata
[0];
96 decoder
->config
.defSampleRate
= rmctx
.sample_rate
;
97 err
= NeAACDecInit(decoder
, NULL
, 0, &s
, &c
);
100 DEBUGF("FAAD: DecInit: %d, %d\n", err
, decoder
->object_type
);
104 /* check for a mid-track resume and force a seek time accordingly */
105 if(resume_offset
> rmctx
.data_offset
+ DATA_HEADER_SIZE
) {
106 resume_offset
-= rmctx
.data_offset
+ DATA_HEADER_SIZE
;
107 /* put number of subpackets to skip in resume_offset */
108 resume_offset
/= (rmctx
.block_align
+ PACKET_HEADER_SIZE
);
109 param
= (int)resume_offset
* ((rmctx
.block_align
* 8 * 1000)/rmctx
.bit_rate
);
110 action
= CODEC_ACTION_SEEK_TIME
;
113 ci
->advance_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
115 /* The main decoding loop */
117 if (action
== CODEC_ACTION_NULL
)
118 action
= ci
->get_command(¶m
);
120 if (action
== CODEC_ACTION_HALT
)
123 if (action
== CODEC_ACTION_SEEK_TIME
) {
124 /* Do not allow seeking beyond the file's length */
125 if ((unsigned) param
> ci
->id3
->length
) {
126 ci
->set_elapsed(ci
->id3
->length
);
131 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
133 /* Seek to the start of the track */
137 action
= CODEC_ACTION_NULL
;
143 buffer
= ci
->request_buffer(&n
,rmctx
.audio_framesize
+ 1000);
144 pkt_offset
= skipped
- pkt
.length
;
145 consumed
= rm_get_packet(&buffer
, &rmctx
, &pkt
);
146 if(consumed
< 0 && playback_on
!= 0) {
147 if(playback_on
== -1) {
148 /* Error only if packet-parsing failed and playback hadn't started */
149 DEBUGF("rm_get_packet failed\n");
158 skipped
+= pkt
.length
;
160 if(pkt
.timestamp
> (unsigned)param
)
163 ci
->advance_buffer(pkt
.length
);
165 ci
->seek_buffer(pkt_offset
+ rmctx
.data_offset
+ DATA_HEADER_SIZE
);
166 buffer
= ci
->request_buffer(&n
,rmctx
.audio_framesize
+ 1000);
167 NeAACDecPostSeekReset(decoder
, decoder
->frame
);
168 ci
->set_elapsed(pkt
.timestamp
);
172 action
= CODEC_ACTION_NULL
;
174 /* Request the required number of bytes from the input buffer */
175 buffer
=ci
->request_buffer(&n
,rmctx
.audio_framesize
+ 1000);
176 consumed
= rm_get_packet(&buffer
, &rmctx
, &pkt
);
178 if(consumed
< 0 && playback_on
!= 0) {
179 if(playback_on
== -1) {
180 /* Error only if packet-parsing failed and playback hadn't started */
181 DEBUGF("rm_get_packet failed\n");
189 if (pkt
.timestamp
>= ci
->id3
->length
)
192 /* Decode one block - returned samples will be host-endian */
193 for(i
= 0; i
< rmctx
.sub_packet_cnt
; i
++) {
194 NeAACDecDecode(decoder
, &frame_info
, buffer
, rmctx
.sub_packet_lengths
[i
]);
195 buffer
+= rmctx
.sub_packet_lengths
[i
];
196 if (frame_info
.error
> 0) {
197 DEBUGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info
.error
));
200 ci
->pcmbuf_insert(decoder
->time_out
[0],
201 decoder
->time_out
[1],
202 decoder
->frameLength
);
203 ci
->set_elapsed(pkt
.timestamp
);
206 ci
->advance_buffer(pkt
.length
);