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 #define MAXSAMPLES (1L << 12) /* Max number of samples in a wma pro subframe */
30 #define BUFSIZE MAXCHANNELS * MAXSAMPLES
31 int32_t decoded
[BUFSIZE
];
33 /* this is the codec entry point */
34 enum codec_status
codec_main(void)
38 asf_waveformatex_t wfx
; /* Holds the stream properties */
40 int res
; /* Return values from asf_read_packet() and decode_packet() */
41 uint8_t* audiobuf
; /* Pointer to the payload of one wma pro packet */
42 int audiobufsize
; /* Payload size */
43 int packetlength
= 0; /* Logical packet size (minus the header size) */
44 int outlen
= 0; /* Number of bytes written to the output buffer */
45 int pktcnt
= 0; /* Count of the packets played */
46 uint8_t *data
; /* Pointer to decoder input buffer */
47 int size
; /* Size of the input frame to the decoder */
49 /* Generic codec initialisation */
50 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 17);
55 /* Wait for the metadata to be read */
56 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
61 /* Remember the resume position */
62 resume_offset
= ci
->id3
->offset
;
65 LOGF("(WMA PRO) Error: Error initialising codec\n");
70 /* Copy the format metadata we've stored in the id3 TOC field. This
71 saves us from parsing it again here. */
72 memcpy(&wfx
, ci
->id3
->toc
, sizeof(wfx
));
74 ci
->configure(DSP_SWITCH_FREQUENCY
, wfx
.rate
);
75 ci
->configure(DSP_SET_STEREO_MODE
, wfx
.channels
== 1 ?
76 STEREO_MONO
: STEREO_INTERLEAVED
);
77 codec_set_replaygain(ci
->id3
);
79 if (decode_init(&wfx
) < 0) {
80 LOGF("(WMA PRO) Error: Unsupported or corrupt file\n");
85 /* Now advance the file position to the first frame */
86 ci
->seek_buffer(ci
->id3
->first_frame_offset
);
91 /* The main decoding loop */
93 while (pktcnt
< wfx
.numpackets
)
96 if (ci
->stop_codec
|| ci
->new_track
) {
100 /* Deal with any pending seek requests */
103 if (ci
->seek_time
== 1) {
105 goto restart_track
; /* Pretend you never saw this... */
108 elapsedtime
= asf_seek(ci
->seek_time
, &wfx
);
109 if (elapsedtime
< 1){
114 ci
->set_elapsed(elapsedtime
);
118 res
= asf_read_packet(&audiobuf
, &audiobufsize
, &packetlength
, &wfx
);
121 LOGF("(WMA PRO) Warning: asf_read_packet returned %d", res
);
128 /* We now loop on the packet, decoding and outputting the subframes
129 * one-by-one. For more information about how wma pro structures its
130 * audio frames, see libwmapro/wmaprodec.c */
133 outlen
= BUFSIZE
; /* decode_packet needs to know the size of the output buffer */
134 res
= decode_packet(&wfx
, decoded
, &outlen
, data
, size
);
136 LOGF("(WMA PRO) Error: decode_packet returned %d", res
);
143 /* outlen now holds the size of the data in bytes - we want the
144 * number of samples. */
145 outlen
/= (sizeof(int32_t) * wfx
.channels
);
146 ci
->pcmbuf_insert(decoded
, NULL
, outlen
);
147 elapsedtime
+= outlen
*10/(wfx
.rate
/100);
148 ci
->set_elapsed(elapsedtime
);
155 /* Advance to the next logical packet */
156 ci
->advance_buffer(packetlength
);
161 if (ci
->request_next_track())