remove "empty download cache" menu entry -- clearing the cache will be in the configu...
[Rockbox.git] / apps / codecs / shorten.c
blob4b803675f93f53e93551b14ed89515d48f9e0077
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id:
10 * Copyright (C) 2005 Mark Arigo
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "codeclib.h"
21 #include <codecs/libffmpegFLAC/shndec.h>
23 CODEC_HEADER
25 #ifndef IBSS_ATTR_SHORTEN_DECODED0
26 #define IBSS_ATTR_SHORTEN_DECODED0 IBSS_ATTR
27 #endif
29 int32_t decoded0[MAX_DECODE_SIZE] IBSS_ATTR_SHORTEN_DECODED0;
30 int32_t decoded1[MAX_DECODE_SIZE] IBSS_ATTR;
32 int32_t offset0[MAX_OFFSET_SIZE] IBSS_ATTR;
33 int32_t offset1[MAX_OFFSET_SIZE] IBSS_ATTR;
35 int8_t ibuf[MAX_BUFFER_SIZE] IBSS_ATTR;
37 /* this is the codec entry point */
38 enum codec_status codec_main(void)
40 ShortenContext sc;
41 uint32_t samplesdone;
42 uint32_t elapsedtime;
43 int8_t *buf;
44 int consumed, res, nsamples;
45 size_t bytesleft;
47 /* Generic codec initialisation */
48 ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512);
49 ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, 1024*128);
51 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
52 ci->configure(DSP_SET_SAMPLE_DEPTH, SHN_OUTPUT_DEPTH-1);
54 next_track:
55 /* Codec initialization */
56 if (codec_init()) {
57 LOGF("Shorten: codec_init error\n");
58 return CODEC_ERROR;
61 while (!*ci->taginfo_ready)
62 ci->yield();
64 codec_set_replaygain(ci->id3);
66 /* Shorten decoder initialization */
67 ci->memset(&sc, 0, sizeof(ShortenContext));
69 /* Skip id3v2 tags */
70 if (ci->id3->first_frame_offset) {
71 buf = ci->request_buffer(&bytesleft, ci->id3->first_frame_offset);
72 ci->advance_buffer(ci->id3->first_frame_offset);
75 /* Read the shorten & wave headers */
76 buf = ci->request_buffer(&bytesleft, MAX_HEADER_SIZE);
77 res = shorten_init(&sc, (unsigned char *)buf, bytesleft);
78 if (res < 0) {
79 LOGF("Shorten: shorten_init error: %d\n", res);
80 return CODEC_ERROR;
83 ci->id3->frequency = sc.sample_rate;
84 ci->configure(DSP_SWITCH_FREQUENCY, sc.sample_rate);
86 if (sc.sample_rate) {
87 ci->id3->length = (sc.totalsamples / sc.sample_rate) * 1000;
88 } else {
89 ci->id3->length = 0;
92 if (ci->id3->length) {
93 ci->id3->bitrate = (ci->id3->filesize * 8) / ci->id3->length;
96 consumed = sc.gb.index/8;
97 ci->advance_buffer(consumed);
98 sc.bitindex = sc.gb.index - 8*consumed;
100 seek_start:
101 /* The main decoding loop */
102 ci->memset(&decoded0, 0, sizeof(int32_t)*MAX_DECODE_SIZE);
103 ci->memset(&decoded1, 0, sizeof(int32_t)*MAX_DECODE_SIZE);
104 ci->memset(&offset0, 0, sizeof(int32_t)*MAX_OFFSET_SIZE);
105 ci->memset(&offset1, 0, sizeof(int32_t)*MAX_OFFSET_SIZE);
107 samplesdone = 0;
108 buf = ci->request_buffer(&bytesleft, MAX_BUFFER_SIZE);
109 while (bytesleft) {
110 ci->yield();
111 if (ci->stop_codec || ci->new_track) {
112 break;
115 /* Seek to start of track */
116 if (ci->seek_time == 1) {
117 if (ci->seek_buffer(sc.header_bits/8 + ci->id3->first_frame_offset)) {
118 sc.bitindex = sc.header_bits - 8*(sc.header_bits/8);
119 ci->set_elapsed(0);
120 ci->seek_complete();
121 goto seek_start;
123 ci->seek_complete();
126 /* Decode a frame */
127 ci->memcpy(ibuf, buf, bytesleft); /* copy buf to iram */
128 res = shorten_decode_frames(&sc, &nsamples, decoded0, decoded1,
129 offset0, offset1, (unsigned char *)ibuf,
130 bytesleft, ci->yield);
132 if (res == FN_ERROR) {
133 LOGF("Shorten: shorten_decode_frames error (%ld)\n", samplesdone);
134 break;
135 } else {
136 /* Insert decoded samples in pcmbuf */
137 if (nsamples) {
138 ci->yield();
139 ci->pcmbuf_insert(decoded0 + sc.nwrap, decoded1 + sc.nwrap,
140 nsamples);
142 /* Update the elapsed-time indicator */
143 samplesdone += nsamples;
144 elapsedtime = (samplesdone*10) / (sc.sample_rate/100);
145 ci->set_elapsed(elapsedtime);
148 /* End of shorten stream...go to next track */
149 if (res == FN_QUIT)
150 break;
153 consumed = sc.gb.index/8;
154 ci->advance_buffer(consumed);
155 buf = ci->request_buffer(&bytesleft, MAX_BUFFER_SIZE);
156 sc.bitindex = sc.gb.index - 8*consumed;
159 if (ci->request_next_track())
160 goto next_track;
162 return CODEC_OK;