Makes sure codec API builds correctly if ROCKBOX_HAS_LOGF is defined
[kugel-rb.git] / apps / codecs / raac.c
blobb886136bd97cb4a4c082a2b3d1f9292bb34c1f69
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 RMContext rmctx;
37 RMPacket pkt;
38 /* this is the codec entry point */
39 enum codec_status codec_main(void)
41 static NeAACDecFrameInfo frame_info;
42 NeAACDecHandle decoder;
43 size_t n;
44 int32_t *output;
45 unsigned int i;
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 */
50 int playback_on = -1;
52 /* Generic codec initialisation */
53 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
54 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
56 next_track:
57 err = CODEC_OK;
59 if (codec_init()) {
60 DEBUGF("FAAD: Codec init error\n");
61 return CODEC_ERROR;
64 while (!*ci->taginfo_ready && !ci->stop_codec)
65 ci->sleep(1);
67 ci->memset(&rmctx,0,sizeof(RMContext));
68 ci->memset(&pkt,0,sizeof(RMPacket));
69 init_rm(&rmctx);
70 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
71 codec_set_replaygain(ci->id3);
73 /* initialise the sound converter */
74 decoder = NeAACDecOpen();
76 if (!decoder) {
77 DEBUGF("FAAD: Decode open error\n");
78 err = CODEC_ERROR;
79 goto done;
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);
89 if (err) {
90 DEBUGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
91 err = CODEC_ERROR;
92 goto done;
94 ci->id3->frequency = s;
95 ci->set_elapsed(0);
96 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
98 /* The main decoding loop */
99 seek_start:
100 while (1) {
101 ci->yield();
102 if (ci->stop_codec || ci->new_track) {
103 break;
106 if (ci->seek_time) {
108 /* Do not allow seeking beyond the file's length */
109 if ((unsigned) ci->seek_time > ci->id3->length) {
110 ci->seek_complete();
111 goto done;
114 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
116 /* Seek to the start of the track */
117 if (ci->seek_time == 1) {
118 ci->set_elapsed(0);
119 ci->seek_complete();
120 goto seek_start;
123 skipped = 0;
124 while(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");
132 return CODEC_ERROR;
134 else
135 goto done;
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);
143 ci->seek_complete();
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");
154 return CODEC_ERROR;
156 else
157 goto done;
160 playback_on = 1;
161 if (pkt.timestamp >= ci->id3->length)
162 goto done;
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));
169 err = CODEC_ERROR;
170 goto exit;
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);
181 err = CODEC_OK;
183 done:
184 if (ci->request_next_track())
185 goto next_track;
187 exit:
188 return err;