Higher the button delay on the fuze a bit more, the wrong hold button reads aren...
[kugel-rb.git] / apps / codecs / atrac3_rm.c
blobf3bfa2f801f37a1c8124ae88d9bf600e7d90e759
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Copyright (C) 2009 Mohamed Tarek
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <string.h>
22 #include "logf.h"
23 #include "codeclib.h"
24 #include "inttypes.h"
25 #include "libatrac/atrac3.h"
27 CODEC_HEADER
29 RMContext rmctx;
30 RMPacket pkt;
31 ATRAC3Context q IBSS_ATTR;
33 static void init_rm(RMContext *rmctx)
35 memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
38 /* this is the codec entry point */
39 enum codec_status codec_main(void)
41 static size_t buff_size;
42 int datasize, res, consumed, i, time_offset;
43 uint8_t *bit_buffer;
44 uint16_t fs,sps,h;
45 uint32_t packet_count;
46 int scrambling_unit_size, num_units, elapsed = 0;
48 next_track:
49 if (codec_init()) {
50 DEBUGF("codec init failed\n");
51 return CODEC_ERROR;
53 while (!*ci->taginfo_ready && !ci->stop_codec)
54 ci->sleep(1);
56 codec_set_replaygain(ci->id3);
57 ci->memset(&rmctx,0,sizeof(RMContext));
58 ci->memset(&pkt,0,sizeof(RMPacket));
59 ci->memset(&q,0,sizeof(ATRAC3Context));
61 init_rm(&rmctx);
63 ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
64 ci->configure(DSP_SET_SAMPLE_DEPTH, 17); /* Remark: atrac3 uses s15.0 by default, s15.2 was hacked. */
65 ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ?
66 STEREO_MONO : STEREO_NONINTERLEAVED);
68 packet_count = rmctx.nb_packets;
69 rmctx.audio_framesize = rmctx.block_align;
70 rmctx.block_align = rmctx.sub_packet_size;
71 fs = rmctx.audio_framesize;
72 sps= rmctx.block_align;
73 h = rmctx.sub_packet_h;
74 scrambling_unit_size = h*fs;
76 res =atrac3_decode_init(&q, &rmctx);
77 if(res < 0) {
78 DEBUGF("failed to initialize atrac decoder\n");
79 return CODEC_ERROR;
82 ci->set_elapsed(0);
83 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
85 /* The main decoder loop */
86 seek_start :
87 while((unsigned)elapsed < rmctx.duration)
89 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
90 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
91 if(consumed < 0) {
92 DEBUGF("rm_get_packet failed\n");
93 return CODEC_ERROR;
96 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
98 ci->yield();
99 if (ci->stop_codec || ci->new_track)
100 goto done;
102 if (ci->seek_time) {
103 ci->set_elapsed(ci->seek_time);
105 /* Do not allow seeking beyond the file's length */
106 if ((unsigned) ci->seek_time > ci->id3->length) {
107 ci->seek_complete();
108 goto done;
111 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
112 packet_count = rmctx.nb_packets;
113 rmctx.audio_pkt_cnt = 0;
114 rmctx.frame_number = 0;
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;
122 num_units = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate))/(h*(fs/sps));
123 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * num_units);
124 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
125 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
126 if(consumed < 0) {
127 DEBUGF("rm_get_packet failed\n");
128 return CODEC_ERROR;
130 packet_count = rmctx.nb_packets - rmctx.audio_pkt_cnt * num_units;
131 rmctx.frame_number = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate));
132 while(rmctx.audiotimestamp > (unsigned) ci->seek_time) {
133 rmctx.audio_pkt_cnt = 0;
134 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * (num_units-1));
135 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
136 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
137 packet_count += rmctx.audio_pkt_cnt;
138 num_units--;
140 time_offset = ci->seek_time - rmctx.audiotimestamp;
141 i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
142 elapsed = rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i;
143 ci->set_elapsed(elapsed);
144 ci->seek_complete();
146 if(pkt.length)
147 res = atrac3_decode_frame(&rmctx, &q, &datasize, pkt.frames[i], rmctx.block_align);
148 else /* indicates that there are no remaining frames */
149 goto done;
151 if(res != rmctx.block_align) {
152 DEBUGF("codec error\n");
153 return CODEC_ERROR;
156 if(datasize)
157 ci->pcmbuf_insert(q.outSamples, q.outSamples + 1024, q.samples_per_frame / rmctx.nb_channels);
158 elapsed = rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i;
159 ci->set_elapsed(elapsed);
160 rmctx.frame_number++;
162 packet_count -= rmctx.audio_pkt_cnt;
163 rmctx.audio_pkt_cnt = 0;
164 ci->advance_buffer(consumed);
167 done :
168 if (ci->request_next_track())
169 goto next_track;
171 return CODEC_OK;