1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 Dave Chapman
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #include "libasf/asf.h"
24 #include "libwma/wmadec.h"
28 /* NOTE: WMADecodeContext is 120152 bytes (on x86) */
29 static WMADecodeContext wmadec
;
31 /* this is the codec entry point */
32 enum codec_status
codec_main(void)
36 asf_waveformatex_t wfx
;
45 /* Generic codec initialisation */
46 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 29);
50 /* Wait for the metadata to be read */
51 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
56 /* Remember the resume position - when the codec is opened, the
57 playback engine will reset it. */
58 resume_offset
= ci
->id3
->offset
;
61 LOGF("WMA: Error initialising codec\n");
66 /* Copy the format metadata we've stored in the id3 TOC field. This
67 saves us from parsing it again here. */
68 memcpy(&wfx
, ci
->id3
->toc
, sizeof(wfx
));
70 if (wma_decode_init(&wmadec
,&wfx
) < 0) {
71 LOGF("WMA: Unsupported or corrupt file\n");
76 if (resume_offset
> ci
->id3
->first_frame_offset
)
78 /* Get start of current packet */
79 int packet_offset
= (resume_offset
- ci
->id3
->first_frame_offset
)
81 ci
->seek_buffer(resume_offset
- packet_offset
);
82 elapsedtime
= asf_get_timestamp(&i
);
83 ci
->set_elapsed(elapsedtime
);
87 /* Now advance the file position to the first frame */
88 ci
->seek_buffer(ci
->id3
->first_frame_offset
);
93 ci
->configure(DSP_SWITCH_FREQUENCY
, wfx
.rate
);
94 ci
->configure(DSP_SET_STEREO_MODE
, wfx
.channels
== 1 ?
95 STEREO_MONO
: STEREO_NONINTERLEAVED
);
96 codec_set_replaygain(ci
->id3
);
98 /* The main decoding loop */
104 if (ci
->stop_codec
|| ci
->new_track
) {
108 /* Deal with any pending seek requests */
111 if (ci
->seek_time
== 1) {
113 goto restart_track
; /* Pretend you never saw this... */
116 elapsedtime
= asf_seek(ci
->seek_time
, &wfx
);
117 if (elapsedtime
< 1){
121 /*DEBUGF("Seek returned %d\n", (int)elapsedtime);*/
122 ci
->set_elapsed(elapsedtime
);
124 /*flush the wma decoder state*/
125 wmadec
.last_superframe_len
= 0;
126 wmadec
.last_bitoffset
= 0;
131 res
= asf_read_packet(&audiobuf
, &audiobufsize
, &packetlength
, &wfx
);
134 /* We'll try to recover from a parse error a certain number of
135 * times. If we succeed, the error counter will be reset.
139 DEBUGF("read_packet error %d, errcount %d\n",wmares
, errcount
);
143 ci
->advance_buffer(packetlength
);
146 } else if (res
> 0) {
147 wma_decode_superframe_init(&wmadec
, audiobuf
, audiobufsize
);
149 for (i
=0; i
< wmadec
.nb_frames
; i
++)
151 wmares
= wma_decode_superframe_frame(&wmadec
,
152 audiobuf
, audiobufsize
);
157 /* Do the above, but for errors in decode. */
159 DEBUGF("WMA decode error %d, errcount %d\n",wmares
, errcount
);
163 ci
->advance_buffer(packetlength
);
166 } else if (wmares
> 0) {
167 ci
->pcmbuf_insert((*wmadec
.frame_out
)[0], (*wmadec
.frame_out
)[1], wmares
);
168 elapsedtime
+= (wmares
*10)/(wfx
.rate
/100);
169 ci
->set_elapsed(elapsedtime
);
175 ci
->advance_buffer(packetlength
);
180 /*LOGF("WMA: Decoded %ld samples\n",elapsedtime*wfx.rate/1000);*/
182 if (ci
->request_next_track())