Merge branch 'master' into android-test-plugins
[kugel-rb.git] / apps / codecs / raac.c
blob3f6dc019703591a54e92e223503f9b8acc077944
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 #include "libfaad/output.h"
29 CODEC_HEADER
31 static void init_rm(RMContext *rmctx)
33 memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
36 static RMContext rmctx;
37 static RMPacket pkt;
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);
48 return CODEC_OK;
51 /* this is called for each file to process */
52 enum codec_status codec_run(void)
54 static NeAACDecFrameInfo frame_info;
55 NeAACDecHandle decoder;
56 size_t n;
57 unsigned int i;
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 */
62 int playback_on = -1;
63 size_t resume_offset;
64 intptr_t param;
65 enum codec_command_action action = CODEC_ACTION_NULL;
67 if (codec_init()) {
68 DEBUGF("FAAD: Codec init error\n");
69 return CODEC_ERROR;
72 resume_offset = ci->id3->offset;
74 ci->memset(&rmctx,0,sizeof(RMContext));
75 ci->memset(&pkt,0,sizeof(RMPacket));
77 ci->seek_buffer(0);
79 init_rm(&rmctx);
80 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
81 codec_set_replaygain(ci->id3);
83 /* initialise the sound converter */
84 decoder = NeAACDecOpen();
86 if (!decoder) {
87 DEBUGF("FAAD: Decode open error\n");
88 return CODEC_ERROR;
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);
99 if (err) {
100 DEBUGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
101 return CODEC_ERROR;
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;
112 ci->set_elapsed(0);
113 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
115 /* The main decoding loop */
116 while (1) {
117 if (action == CODEC_ACTION_NULL)
118 action = ci->get_command(&param);
120 if (action == CODEC_ACTION_HALT)
121 break;
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);
127 ci->seek_complete();
128 break;
131 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
133 /* Seek to the start of the track */
134 if (param == 0) {
135 ci->set_elapsed(0);
136 ci->seek_complete();
137 action = CODEC_ACTION_NULL;
138 continue;
141 skipped = 0;
142 while(1) {
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");
150 ci->seek_complete();
151 return CODEC_ERROR;
153 else {
154 ci->seek_complete();
155 return CODEC_OK;
158 skipped += pkt.length;
160 if(pkt.timestamp > (unsigned)param)
161 break;
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);
169 ci->seek_complete();
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");
182 return CODEC_ERROR;
184 else
185 break;
188 playback_on = 1;
189 if (pkt.timestamp >= ci->id3->length)
190 break;
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));
198 return CODEC_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);
209 return CODEC_OK;