Theme Editor: Added support for upper-case wps file extensions
[kugel-rb.git] / apps / codecs / wmapro.c
blobe929c1f428787ba97fc501591f36e547f8f0c756
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include "codeclib.h"
23 #include "libasf/asf.h"
24 #include "libwmapro/wmaprodec.h"
26 CODEC_HEADER
28 #define MAXSAMPLES (1L << 12) /* Max number of samples in a wma pro subframe */
29 #define MAXCHANNELS 8
30 #define BUFSIZE MAXCHANNELS * MAXSAMPLES
31 int32_t decoded[BUFSIZE];
33 /* this is the codec entry point */
34 enum codec_status codec_main(void)
36 uint32_t elapsedtime;
37 int retval;
38 asf_waveformatex_t wfx; /* Holds the stream properties */
39 size_t resume_offset;
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);
53 next_track:
55 /* Wait for the metadata to be read */
56 while (!*ci->taginfo_ready && !ci->stop_codec)
57 ci->sleep(1);
59 retval = CODEC_OK;
61 /* Remember the resume position */
62 resume_offset = ci->id3->offset;
63 restart_track:
64 if (codec_init()) {
65 LOGF("(WMA PRO) Error: Error initialising codec\n");
66 retval = CODEC_ERROR;
67 goto done;
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");
81 retval = CODEC_ERROR;
82 goto done;
85 /* Now advance the file position to the first frame */
86 ci->seek_buffer(ci->id3->first_frame_offset);
88 elapsedtime = 0;
89 resume_offset = 0;
91 /* The main decoding loop */
93 while (pktcnt < wfx.numpackets)
95 ci->yield();
96 if (ci->stop_codec || ci->new_track) {
97 goto done;
100 /* Deal with any pending seek requests */
101 if (ci->seek_time){
103 if (ci->seek_time == 1) {
104 ci->seek_complete();
105 goto restart_track; /* Pretend you never saw this... */
108 elapsedtime = asf_seek(ci->seek_time, &wfx);
109 if (elapsedtime < 1){
110 ci->seek_complete();
111 goto next_track;
114 ci->set_elapsed(elapsedtime);
115 ci->seek_complete();
118 res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx);
120 if (res < 0) {
121 LOGF("(WMA PRO) Warning: asf_read_packet returned %d", res);
122 goto done;
123 } else {
124 data = audiobuf;
125 size = audiobufsize;
126 pktcnt++;
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 */
131 while(size > 0)
133 outlen = BUFSIZE; /* decode_packet needs to know the size of the output buffer */
134 res = decode_packet(&wfx, decoded, &outlen, data, size);
135 if(res < 0) {
136 LOGF("(WMA PRO) Error: decode_packet returned %d", res);
137 goto done;
139 data += res;
140 size -= res;
141 if(outlen) {
142 ci->yield ();
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);
149 ci->yield ();
155 /* Advance to the next logical packet */
156 ci->advance_buffer(packetlength);
158 retval = CODEC_OK;
160 done:
161 if (ci->request_next_track())
162 goto next_track;
164 return retval;