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"
28 #include "libfaad/output.h"
33 static void init_rm(RMContext
*rmctx
)
35 memcpy(rmctx
, (void*)(( (intptr_t)ci
->id3
->id3v2buf
+ 3 ) &~ 3), sizeof(RMContext
));
38 static RMContext rmctx
;
41 /* this is the codec entry point */
42 enum codec_status
codec_main(enum codec_entry_call_reason reason
)
44 if (reason
== CODEC_LOAD
) {
45 /* Generic codec initialisation */
46 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
47 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 29);
53 /* this is called for each file to process */
54 enum codec_status
codec_run(void)
56 static NeAACDecFrameInfo frame_info
;
57 NeAACDecHandle decoder
;
60 unsigned char* buffer
;
61 int err
, consumed
, pkt_offset
, skipped
= 0;
62 uint32_t s
= 0; /* sample rate */
63 unsigned char c
= 0; /* channels */
67 enum codec_command_action action
= CODEC_ACTION_NULL
;
70 DEBUGF("FAAD: Codec init error\n");
74 resume_offset
= ci
->id3
->offset
;
76 ci
->memset(&rmctx
,0,sizeof(RMContext
));
77 ci
->memset(&pkt
,0,sizeof(RMPacket
));
82 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
83 codec_set_replaygain(ci
->id3
);
85 /* initialise the sound converter */
86 decoder
= NeAACDecOpen();
89 DEBUGF("FAAD: Decode open error\n");
93 NeAACDecConfigurationPtr conf
= NeAACDecGetCurrentConfiguration(decoder
);
94 conf
->outputFormat
= FAAD_FMT_16BIT
; /* irrelevant, we don't convert */
95 NeAACDecSetConfiguration(decoder
, conf
);
97 decoder
->config
.defObjectType
= rmctx
.codec_extradata
[0];
98 decoder
->config
.defSampleRate
= rmctx
.sample_rate
;
99 err
= NeAACDecInit(decoder
, NULL
, 0, &s
, &c
);
102 DEBUGF("FAAD: DecInit: %d, %d\n", err
, decoder
->object_type
);
106 /* check for a mid-track resume and force a seek time accordingly */
107 if(resume_offset
> rmctx
.data_offset
+ DATA_HEADER_SIZE
) {
108 resume_offset
-= rmctx
.data_offset
+ DATA_HEADER_SIZE
;
109 /* put number of subpackets to skip in resume_offset */
110 resume_offset
/= (rmctx
.block_align
+ PACKET_HEADER_SIZE
);
111 param
= (int)resume_offset
* ((rmctx
.block_align
* 8 * 1000)/rmctx
.bit_rate
);
112 action
= CODEC_ACTION_SEEK_TIME
;
115 ci
->advance_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
117 /* The main decoding loop */
119 if (action
== CODEC_ACTION_NULL
)
120 action
= ci
->get_command(¶m
);
122 if (action
== CODEC_ACTION_HALT
)
125 if (action
== CODEC_ACTION_SEEK_TIME
) {
126 /* Do not allow seeking beyond the file's length */
127 if ((unsigned) param
> ci
->id3
->length
) {
128 ci
->set_elapsed(ci
->id3
->length
);
133 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
135 /* Seek to the start of the track */
139 action
= CODEC_ACTION_NULL
;
145 buffer
= ci
->request_buffer(&n
,rmctx
.audio_framesize
+ 1000);
146 pkt_offset
= skipped
- pkt
.length
;
147 consumed
= rm_get_packet(&buffer
, &rmctx
, &pkt
);
148 if(consumed
< 0 && playback_on
!= 0) {
149 if(playback_on
== -1) {
150 /* Error only if packet-parsing failed and playback hadn't started */
151 DEBUGF("rm_get_packet failed\n");
160 skipped
+= pkt
.length
;
162 if(pkt
.timestamp
> (unsigned)param
)
165 ci
->advance_buffer(pkt
.length
);
167 ci
->seek_buffer(pkt_offset
+ rmctx
.data_offset
+ DATA_HEADER_SIZE
);
168 buffer
= ci
->request_buffer(&n
,rmctx
.audio_framesize
+ 1000);
169 NeAACDecPostSeekReset(decoder
, decoder
->frame
);
170 ci
->set_elapsed(pkt
.timestamp
);
174 action
= CODEC_ACTION_NULL
;
176 /* Request the required number of bytes from the input buffer */
177 buffer
=ci
->request_buffer(&n
,rmctx
.audio_framesize
+ 1000);
178 consumed
= rm_get_packet(&buffer
, &rmctx
, &pkt
);
180 if(consumed
< 0 && playback_on
!= 0) {
181 if(playback_on
== -1) {
182 /* Error only if packet-parsing failed and playback hadn't started */
183 DEBUGF("rm_get_packet failed\n");
191 if (pkt
.timestamp
>= ci
->id3
->length
)
194 /* Decode one block - returned samples will be host-endian */
195 for(i
= 0; i
< rmctx
.sub_packet_cnt
; i
++) {
196 NeAACDecDecode(decoder
, &frame_info
, buffer
, rmctx
.sub_packet_lengths
[i
]);
197 buffer
+= rmctx
.sub_packet_lengths
[i
];
198 if (frame_info
.error
> 0) {
199 DEBUGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info
.error
));
202 ci
->pcmbuf_insert(decoder
->time_out
[0],
203 decoder
->time_out
[1],
204 decoder
->frameLength
);
205 ci
->set_elapsed(pkt
.timestamp
);
208 ci
->advance_buffer(pkt
.length
);