Poor mans workaround for hanging SDL app/sim on shutdown.
[maemo-rb.git] / apps / codecs / wmapro.c
blobc02dddeeb3f6b032fc3a8f7330bb2ff9ec8a0978
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 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)
33 uint32_t elapsedtime;
34 int retval;
35 asf_waveformatex_t wfx; /* Holds the stream properties */
36 size_t resume_offset;
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);
50 next_track:
51 retval = CODEC_OK;
53 /* Wait for the metadata to be read */
54 if (codec_wait_taginfo() != 0)
55 goto done;
57 /* Remember the resume position */
58 resume_offset = ci->id3->offset;
60 restart_track:
61 retval = CODEC_OK;
63 if (codec_init()) {
64 LOGF("(WMA PRO) Error: Error initialising codec\n");
65 retval = CODEC_ERROR;
66 goto done;
69 /* Copy the format metadata we've stored in the id3 TOC field. This
70 saves us from parsing it again here. */
71 memcpy(&wfx, ci->id3->toc, sizeof(wfx));
73 ci->configure(DSP_SWITCH_FREQUENCY, wfx.rate);
74 ci->configure(DSP_SET_STEREO_MODE, wfx.channels == 1 ?
75 STEREO_MONO : STEREO_NONINTERLEAVED);
76 codec_set_replaygain(ci->id3);
78 if (decode_init(&wfx) < 0) {
79 LOGF("(WMA PRO) Error: Unsupported or corrupt file\n");
80 retval = CODEC_ERROR;
81 goto done;
84 /* Now advance the file position to the first frame */
85 ci->seek_buffer(ci->id3->first_frame_offset);
87 elapsedtime = 0;
88 resume_offset = 0;
90 /* The main decoding loop */
92 while (pktcnt < wfx.numpackets)
94 ci->yield();
95 if (ci->stop_codec || ci->new_track) {
96 goto done;
99 /* Deal with any pending seek requests */
100 if (ci->seek_time){
102 if (ci->seek_time == 1) {
103 ci->seek_complete();
104 goto restart_track; /* Pretend you never saw this... */
107 elapsedtime = asf_seek(ci->seek_time, &wfx);
108 if (elapsedtime < 1){
109 ci->seek_complete();
110 goto next_track;
113 ci->set_elapsed(elapsedtime);
114 ci->seek_complete();
117 res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx);
119 if (res < 0) {
120 LOGF("(WMA PRO) Warning: asf_read_packet returned %d", res);
121 goto done;
122 } else {
123 data = audiobuf;
124 size = audiobufsize;
125 pktcnt++;
127 /* We now loop on the packet, decoding and outputting the subframes
128 * one-by-one. For more information about how wma pro structures its
129 * audio frames, see libwmapro/wmaprodec.c */
130 while(size > 0)
132 res = decode_packet(&wfx, dec, &outlen, data, size);
133 if(res < 0) {
134 LOGF("(WMA PRO) Error: decode_packet returned %d", res);
135 goto done;
137 data += res;
138 size -= res;
139 if(outlen) {
140 ci->yield ();
141 outlen /= (wfx.channels);
142 ci->pcmbuf_insert(dec[0], dec[1], outlen );
143 elapsedtime += outlen*10/(wfx.rate/100);
144 ci->set_elapsed(elapsedtime);
145 ci->yield ();
151 /* Advance to the next logical packet */
152 ci->advance_buffer(packetlength);
155 done:
156 if (ci->request_next_track())
157 goto next_track;
159 return retval;