gitscraper: support Python3.
[maemo-rb.git] / apps / codecs / raac.c
blob0ef7e715bcb3b192c446c18c26722ccaf66d2607
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include "codeclib.h"
23 #include "librm/rm.h"
24 #include "libfaad/common.h"
25 #include "libfaad/structs.h"
26 #include "libfaad/decoder.h"
27 /* rockbox: not used
28 #include "libfaad/output.h"
31 CODEC_HEADER
33 static void init_rm(RMContext *rmctx)
35 memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
38 static RMContext rmctx;
39 static RMPacket pkt;
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);
50 return CODEC_OK;
53 /* this is called for each file to process */
54 enum codec_status codec_run(void)
56 static NeAACDecFrameInfo frame_info;
57 NeAACDecHandle decoder;
58 size_t n;
59 unsigned int i;
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 */
64 int playback_on = -1;
65 size_t resume_offset;
66 intptr_t param;
67 enum codec_command_action action = CODEC_ACTION_NULL;
69 if (codec_init()) {
70 DEBUGF("FAAD: Codec init error\n");
71 return CODEC_ERROR;
74 resume_offset = ci->id3->offset;
76 ci->memset(&rmctx,0,sizeof(RMContext));
77 ci->memset(&pkt,0,sizeof(RMPacket));
79 ci->seek_buffer(0);
81 init_rm(&rmctx);
82 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
83 codec_set_replaygain(ci->id3);
85 /* initialise the sound converter */
86 decoder = NeAACDecOpen();
88 if (!decoder) {
89 DEBUGF("FAAD: Decode open error\n");
90 return CODEC_ERROR;
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);
101 if (err) {
102 DEBUGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
103 return CODEC_ERROR;
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;
114 ci->set_elapsed(0);
115 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
117 /* The main decoding loop */
118 while (1) {
119 if (action == CODEC_ACTION_NULL)
120 action = ci->get_command(&param);
122 if (action == CODEC_ACTION_HALT)
123 break;
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);
129 ci->seek_complete();
130 break;
133 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
135 /* Seek to the start of the track */
136 if (param == 0) {
137 ci->set_elapsed(0);
138 ci->seek_complete();
139 action = CODEC_ACTION_NULL;
140 continue;
143 skipped = 0;
144 while(1) {
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");
152 ci->seek_complete();
153 return CODEC_ERROR;
155 else {
156 ci->seek_complete();
157 return CODEC_OK;
160 skipped += pkt.length;
162 if(pkt.timestamp > (unsigned)param)
163 break;
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);
171 ci->seek_complete();
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");
184 return CODEC_ERROR;
186 else
187 break;
190 playback_on = 1;
191 if (pkt.timestamp >= ci->id3->length)
192 break;
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));
200 return CODEC_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);
211 return CODEC_OK;