diff options
Diffstat (limited to 'apps/codecs/atrac3_oma.c')
-rw-r--r-- | apps/codecs/atrac3_oma.c | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/apps/codecs/atrac3_oma.c b/apps/codecs/atrac3_oma.c new file mode 100644 index 0000000000..b080b71524 --- /dev/null +++ b/apps/codecs/atrac3_oma.c | |||
@@ -0,0 +1,158 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2009 Mohamed Tarek | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include <string.h> | ||
23 | |||
24 | #include "logf.h" | ||
25 | #include "codeclib.h" | ||
26 | #include "inttypes.h" | ||
27 | #include "libatrac/atrac3.h" | ||
28 | |||
29 | CODEC_HEADER | ||
30 | |||
31 | #define FRAMESIZE ci->id3->bytesperframe | ||
32 | #define BITRATE ci->id3->bitrate | ||
33 | |||
34 | /* The codec has nothing to do with RM, it just uses an RMContext struct to * | ||
35 | * store the data needs to be passed to the decoder initializing function. */ | ||
36 | RMContext rmctx; | ||
37 | ATRAC3Context q IBSS_ATTR; | ||
38 | |||
39 | static void init_rm(RMContext *rmctx, struct mp3entry *id3) | ||
40 | { | ||
41 | rmctx->sample_rate = id3->frequency; | ||
42 | rmctx->nb_channels = 2; | ||
43 | rmctx->bit_rate = id3->bitrate; | ||
44 | rmctx->block_align = id3->bytesperframe; | ||
45 | |||
46 | /* 14-byte extra-data was faked in the metadata parser so that * | ||
47 | * the ATRAC3 decoder would parse it as WAV format extra-data. */ | ||
48 | rmctx->extradata_size = 14; | ||
49 | memcpy(rmctx->codec_extradata, &id3->id3v1buf[0][0], 14); | ||
50 | } | ||
51 | |||
52 | /* this is the codec entry point */ | ||
53 | enum codec_status codec_main(void) | ||
54 | { | ||
55 | static size_t buff_size; | ||
56 | int datasize, res, frame_counter, total_frames, seek_frame_offset; | ||
57 | uint8_t *bit_buffer; | ||
58 | int elapsed = 0; | ||
59 | size_t resume_offset = ci->id3->offset; | ||
60 | |||
61 | next_track: | ||
62 | if (codec_init()) { | ||
63 | DEBUGF("codec init failed\n"); | ||
64 | return CODEC_ERROR; | ||
65 | } | ||
66 | while (!*ci->taginfo_ready && !ci->stop_codec) | ||
67 | ci->sleep(1); | ||
68 | |||
69 | codec_set_replaygain(ci->id3); | ||
70 | ci->memset(&rmctx,0,sizeof(RMContext)); | ||
71 | ci->memset(&q,0,sizeof(ATRAC3Context)); | ||
72 | |||
73 | init_rm(&rmctx, ci->id3); | ||
74 | |||
75 | ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency); | ||
76 | ci->configure(DSP_SET_SAMPLE_DEPTH, 17); /* Remark: atrac3 uses s15.0 by default, s15.2 was hacked. */ | ||
77 | ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ? | ||
78 | STEREO_MONO : STEREO_NONINTERLEAVED); | ||
79 | |||
80 | res =atrac3_decode_init(&q, &rmctx); | ||
81 | if(res < 0) { | ||
82 | DEBUGF("failed to initialize atrac decoder\n"); | ||
83 | return CODEC_ERROR; | ||
84 | } | ||
85 | |||
86 | /* check for a mid-track resume and force a seek time accordingly */ | ||
87 | if(resume_offset > ci->id3->first_frame_offset) { | ||
88 | resume_offset -= ci->id3->first_frame_offset; | ||
89 | /* calculate resume_offset in frames */ | ||
90 | resume_offset = (int)resume_offset / FRAMESIZE; | ||
91 | ci->seek_time = (int)resume_offset * ((FRAMESIZE * 8)/BITRATE); | ||
92 | } | ||
93 | total_frames = (ci->id3->filesize - ci->id3->first_frame_offset) / FRAMESIZE; | ||
94 | frame_counter = 0; | ||
95 | |||
96 | ci->set_elapsed(0); | ||
97 | ci->advance_buffer(ci->id3->first_frame_offset); | ||
98 | |||
99 | /* The main decoder loop */ | ||
100 | seek_start : | ||
101 | while(frame_counter < total_frames) | ||
102 | { | ||
103 | bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, FRAMESIZE); | ||
104 | |||
105 | ci->yield(); | ||
106 | if (ci->stop_codec || ci->new_track) | ||
107 | goto done; | ||
108 | |||
109 | if (ci->seek_time) { | ||
110 | ci->set_elapsed(ci->seek_time); | ||
111 | |||
112 | /* Do not allow seeking beyond the file's length */ | ||
113 | if ((unsigned) ci->seek_time > ci->id3->length) { | ||
114 | ci->seek_complete(); | ||
115 | goto done; | ||
116 | } | ||
117 | |||
118 | /* Seek to the start of the track */ | ||
119 | if (ci->seek_time == 1) { | ||
120 | ci->set_elapsed(0); | ||
121 | ci->seek_complete(); | ||
122 | ci->seek_buffer(ci->id3->first_frame_offset); | ||
123 | elapsed = 0; | ||
124 | goto seek_start; | ||
125 | } | ||
126 | seek_frame_offset = (ci->seek_time * BITRATE) / (8 * FRAMESIZE); | ||
127 | frame_counter = seek_frame_offset; | ||
128 | ci->seek_buffer(ci->id3->first_frame_offset + seek_frame_offset* FRAMESIZE); | ||
129 | bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, FRAMESIZE); | ||
130 | elapsed = ci->seek_time; | ||
131 | |||
132 | ci->set_elapsed(elapsed); | ||
133 | ci->seek_complete(); | ||
134 | } | ||
135 | |||
136 | res = atrac3_decode_frame(&rmctx, &q, &datasize, bit_buffer, FRAMESIZE); | ||
137 | |||
138 | if(res != (int)FRAMESIZE) { | ||
139 | DEBUGF("codec error\n"); | ||
140 | return CODEC_ERROR; | ||
141 | } | ||
142 | |||
143 | if(datasize) | ||
144 | ci->pcmbuf_insert(q.outSamples, q.outSamples + 1024, q.samples_per_frame / rmctx.nb_channels); | ||
145 | |||
146 | elapsed += (FRAMESIZE * 8) / BITRATE; | ||
147 | ci->set_elapsed(elapsed); | ||
148 | |||
149 | ci->advance_buffer(FRAMESIZE); | ||
150 | frame_counter++; | ||
151 | } | ||
152 | |||
153 | done: | ||
154 | if (ci->request_next_track()) | ||
155 | goto next_track; | ||
156 | |||
157 | return CODEC_OK; | ||
158 | } | ||