1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2010 Mohamed Tarek
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 "libwmapro/wmaprodec.h"
28 int32_t *dec
[2]; /* pointers to the output buffers in WMAProDecodeCtx in wmaprodec.c */
30 /* this is the codec entry point */
31 enum codec_status
codec_main(void)
35 asf_waveformatex_t wfx
; /* Holds the stream properties */
37 int res
; /* Return values from asf_read_packet() and decode_packet() */
38 uint8_t* audiobuf
; /* Pointer to the payload of one wma pro packet */
39 int audiobufsize
; /* Payload size */
40 int packetlength
= 0; /* Logical packet size (minus the header size) */
41 int outlen
= 0; /* Number of bytes written to the output buffer */
42 int pktcnt
= 0; /* Count of the packets played */
43 uint8_t *data
; /* Pointer to decoder input buffer */
44 int size
; /* Size of the input frame to the decoder */
46 /* Generic codec initialisation */
47 ci
->configure(DSP_SET_SAMPLE_DEPTH
, WMAPRO_DSP_SAMPLE_DEPTH
);
52 /* Wait for the metadata to be read */
53 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
58 /* Remember the resume position */
59 resume_offset
= ci
->id3
->offset
;
62 LOGF("(WMA PRO) Error: Error initialising codec\n");
67 /* Copy the format metadata we've stored in the id3 TOC field. This
68 saves us from parsing it again here. */
69 memcpy(&wfx
, ci
->id3
->toc
, sizeof(wfx
));
71 ci
->configure(DSP_SWITCH_FREQUENCY
, wfx
.rate
);
72 ci
->configure(DSP_SET_STEREO_MODE
, wfx
.channels
== 1 ?
73 STEREO_MONO
: STEREO_NONINTERLEAVED
);
74 codec_set_replaygain(ci
->id3
);
76 if (decode_init(&wfx
) < 0) {
77 LOGF("(WMA PRO) Error: Unsupported or corrupt file\n");
82 /* Now advance the file position to the first frame */
83 ci
->seek_buffer(ci
->id3
->first_frame_offset
);
88 /* The main decoding loop */
90 while (pktcnt
< wfx
.numpackets
)
93 if (ci
->stop_codec
|| ci
->new_track
) {
97 /* Deal with any pending seek requests */
100 if (ci
->seek_time
== 1) {
102 goto restart_track
; /* Pretend you never saw this... */
105 elapsedtime
= asf_seek(ci
->seek_time
, &wfx
);
106 if (elapsedtime
< 1){
111 ci
->set_elapsed(elapsedtime
);
115 res
= asf_read_packet(&audiobuf
, &audiobufsize
, &packetlength
, &wfx
);
118 LOGF("(WMA PRO) Warning: asf_read_packet returned %d", res
);
125 /* We now loop on the packet, decoding and outputting the subframes
126 * one-by-one. For more information about how wma pro structures its
127 * audio frames, see libwmapro/wmaprodec.c */
130 res
= decode_packet(&wfx
, dec
, &outlen
, data
, size
);
132 LOGF("(WMA PRO) Error: decode_packet returned %d", res
);
139 outlen
/= (wfx
.channels
);
140 ci
->pcmbuf_insert(dec
[0], dec
[1], outlen
);
141 elapsedtime
+= outlen
*10/(wfx
.rate
/100);
142 ci
->set_elapsed(elapsedtime
);
149 /* Advance to the next logical packet */
150 ci
->advance_buffer(packetlength
);
155 if (ci
->request_next_track())