1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
21 #include <codecs/libffmpegFLAC/shndec.h>
25 #ifndef IBSS_ATTR_SHORTEN_DECODED0
26 #define IBSS_ATTR_SHORTEN_DECODED0 IBSS_ATTR
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)
44 int consumed
, res
, nsamples
;
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);
55 /* Codec initialization */
57 LOGF("Shorten: codec_init error\n");
61 while (!*ci
->taginfo_ready
)
64 codec_set_replaygain(ci
->id3
);
66 /* Shorten decoder initialization */
67 ci
->memset(&sc
, 0, sizeof(ShortenContext
));
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
);
79 LOGF("Shorten: shorten_init error: %d\n", res
);
83 ci
->id3
->frequency
= sc
.sample_rate
;
84 ci
->configure(DSP_SWITCH_FREQUENCY
, sc
.sample_rate
);
87 ci
->id3
->length
= (sc
.totalsamples
/ sc
.sample_rate
) * 1000;
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
;
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
);
108 buf
= ci
->request_buffer(&bytesleft
, MAX_BUFFER_SIZE
);
111 if (ci
->stop_codec
|| ci
->new_track
) {
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);
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
);
136 /* Insert decoded samples in pcmbuf */
139 ci
->pcmbuf_insert(decoded0
+ sc
.nwrap
, decoded1
+ sc
.nwrap
,
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 */
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())