summaryrefslogtreecommitdiff
path: root/apps/codecs/atrac3_rm.c
diff options
context:
space:
mode:
authorMohamed Tarek <mt@rockbox.org>2009-08-14 17:36:57 +0000
committerMohamed Tarek <mt@rockbox.org>2009-08-14 17:36:57 +0000
commit685628cf18f815b1ccc80a84590037960b2b4ec1 (patch)
treeef3f78abb2ff114dda2cc1ce0381194fb7c3288d /apps/codecs/atrac3_rm.c
parent4f26112b1b50b644c3c3245906c996c16bb0c616 (diff)
downloadrockbox-685628cf18f815b1ccc80a84590037960b2b4ec1.tar.gz
rockbox-685628cf18f815b1ccc80a84590037960b2b4ec1.zip
Support for playback of atrac3 audio in rm, in sim.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22311 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs/atrac3_rm.c')
-rw-r--r--apps/codecs/atrac3_rm.c173
1 files changed, 173 insertions, 0 deletions
diff --git a/apps/codecs/atrac3_rm.c b/apps/codecs/atrac3_rm.c
new file mode 100644
index 0000000000..a9c1c49fea
--- /dev/null
+++ b/apps/codecs/atrac3_rm.c
@@ -0,0 +1,173 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Copyright (C) 2009 Mohamed Tarek
9 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include <string.h>
21
22#include "logf.h"
23#include "codeclib.h"
24#include "inttypes.h"
25#include "libatrac/atrac3.h"
26
27CODEC_HEADER
28
29RMContext rmctx;
30RMPacket pkt;
31ATRAC3Context q;
32
33static void init_rm(RMContext *rmctx)
34{
35 memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
36}
37
38/* this is the codec entry point */
39enum codec_status codec_main(void)
40{
41 static size_t buff_size;
42 int datasize, res, consumed, i, time_offset;
43 uint8_t *bit_buffer;
44 int16_t outbuf[2048] __attribute__((aligned(32)));
45 uint16_t fs,sps,h;
46 uint32_t packet_count;
47 int scrambling_unit_size, num_units, elapsed = 0;
48
49next_track:
50 if (codec_init()) {
51 DEBUGF("codec init failed\n");
52 return CODEC_ERROR;
53 }
54 while (!*ci->taginfo_ready && !ci->stop_codec)
55 ci->sleep(1);
56
57 codec_set_replaygain(ci->id3);
58 ci->memset(&rmctx,0,sizeof(RMContext));
59 ci->memset(&pkt,0,sizeof(RMPacket));
60 ci->memset(&q,0,sizeof(ATRAC3Context));
61
62 init_rm(&rmctx);
63
64 ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
65 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
66 ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ?
67 STEREO_MONO : STEREO_INTERLEAVED);
68
69 packet_count = rmctx.nb_packets;
70 rmctx.audio_framesize = rmctx.block_align;
71 rmctx.block_align = rmctx.sub_packet_size;
72 fs = rmctx.audio_framesize;
73 sps= rmctx.block_align;
74 h = rmctx.sub_packet_h;
75 scrambling_unit_size = h*fs;
76
77 res =atrac3_decode_init(&q, &rmctx);
78 if(res < 0) {
79 DEBUGF("failed to initialize atrac decoder\n");
80 return CODEC_ERROR;
81 }
82
83 ci->set_elapsed(0);
84 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
85
86 /* The main decoder loop */
87seek_start :
88 while((unsigned)elapsed < rmctx.duration)
89 {
90 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
91 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
92 if(consumed < 0) {
93 DEBUGF("rm_get_packet failed\n");
94 return CODEC_ERROR;
95 }
96
97 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
98 {
99 ci->yield();
100 if (ci->stop_codec || ci->new_track)
101 goto done;
102
103 if (ci->seek_time) {
104 ci->set_elapsed(ci->seek_time);
105
106 /* Do not allow seeking beyond the file's length */
107 if ((unsigned) ci->seek_time > ci->id3->length) {
108 ci->seek_complete();
109 goto done;
110 }
111
112 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
113 packet_count = rmctx.nb_packets;
114 rmctx.audio_pkt_cnt = 0;
115 rmctx.frame_number = 0;
116
117 /* Seek to the start of the track */
118 if (ci->seek_time == 1) {
119 ci->set_elapsed(0);
120 ci->seek_complete();
121 goto seek_start;
122 }
123 num_units = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate))/(h*(fs/sps));
124 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * num_units);
125 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
126 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
127 if(consumed < 0) {
128 DEBUGF("rm_get_packet failed\n");
129 return CODEC_ERROR;
130 }
131 packet_count = rmctx.nb_packets - rmctx.audio_pkt_cnt * num_units;
132 rmctx.frame_number = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate));
133 while(rmctx.audiotimestamp > (unsigned) ci->seek_time) {
134 rmctx.audio_pkt_cnt = 0;
135 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * (num_units-1));
136 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
137 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
138 packet_count += rmctx.audio_pkt_cnt;
139 num_units--;
140 }
141 time_offset = ci->seek_time - rmctx.audiotimestamp;
142 i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
143 elapsed = rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i;
144 ci->set_elapsed(elapsed);
145 ci->seek_complete();
146 }
147 if(pkt.length)
148 res = atrac3_decode_frame(&rmctx,&q, outbuf, &datasize, pkt.frames[i], rmctx.block_align);
149 else /* indicates that there are no remaining frames */
150 goto done;
151
152 if(res != rmctx.block_align) {
153 DEBUGF("codec error\n");
154 return CODEC_ERROR;
155 }
156
157 if(datasize)
158 ci->pcmbuf_insert(outbuf, NULL, q.samples_per_frame / rmctx.nb_channels);
159 elapsed = rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i;
160 ci->set_elapsed(elapsed);
161 rmctx.frame_number++;
162 }
163 packet_count -= rmctx.audio_pkt_cnt;
164 rmctx.audio_pkt_cnt = 0;
165 ci->advance_buffer(consumed);
166 }
167
168 done :
169 if (ci->request_next_track())
170 goto next_track;
171
172 return CODEC_OK;
173}