Changes to test_mem. Improve readability for smaller displays, increase loop count...
[maemo-rb.git] / apps / codecs / wmapro.c
blob75bbd24cdaaa1e96c195b667081bc37ee763d05e
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:
52 /* Wait for the metadata to be read */
53 while (!*ci->taginfo_ready && !ci->stop_codec)
54 ci->sleep(1);
56 retval = CODEC_OK;
58 /* Remember the resume position */
59 resume_offset = ci->id3->offset;
60 restart_track:
61 if (codec_init()) {
62 LOGF("(WMA PRO) Error: Error initialising codec\n");
63 retval = CODEC_ERROR;
64 goto done;
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");
78 retval = CODEC_ERROR;
79 goto done;
82 /* Now advance the file position to the first frame */
83 ci->seek_buffer(ci->id3->first_frame_offset);
85 elapsedtime = 0;
86 resume_offset = 0;
88 /* The main decoding loop */
90 while (pktcnt < wfx.numpackets)
92 ci->yield();
93 if (ci->stop_codec || ci->new_track) {
94 goto done;
97 /* Deal with any pending seek requests */
98 if (ci->seek_time){
100 if (ci->seek_time == 1) {
101 ci->seek_complete();
102 goto restart_track; /* Pretend you never saw this... */
105 elapsedtime = asf_seek(ci->seek_time, &wfx);
106 if (elapsedtime < 1){
107 ci->seek_complete();
108 goto next_track;
111 ci->set_elapsed(elapsedtime);
112 ci->seek_complete();
115 res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx);
117 if (res < 0) {
118 LOGF("(WMA PRO) Warning: asf_read_packet returned %d", res);
119 goto done;
120 } else {
121 data = audiobuf;
122 size = audiobufsize;
123 pktcnt++;
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 */
128 while(size > 0)
130 res = decode_packet(&wfx, dec, &outlen, data, size);
131 if(res < 0) {
132 LOGF("(WMA PRO) Error: decode_packet returned %d", res);
133 goto done;
135 data += res;
136 size -= res;
137 if(outlen) {
138 ci->yield ();
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);
143 ci->yield ();
149 /* Advance to the next logical packet */
150 ci->advance_buffer(packetlength);
152 retval = CODEC_OK;
154 done:
155 if (ci->request_next_track())
156 goto next_track;
158 return retval;