Submit initial patch from FS#12176. Adds support for several new game music formats...
[kugel-rb.git] / apps / codecs / wmapro.c
blob17e311c5c01dd6bdd14bbfe6ac73ec0587e1e98b
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 static int32_t *dec[2]; /* pointers to the output buffers in WMAProDecodeCtx in
29 wmaprodec.c */
32 /* this is the codec entry point */
33 enum codec_status codec_main(enum codec_entry_call_reason reason)
35 if (reason == CODEC_LOAD) {
36 /* Generic codec initialisation */
37 ci->configure(DSP_SET_SAMPLE_DEPTH, WMAPRO_DSP_SAMPLE_DEPTH);
40 return CODEC_OK;
43 /* this is called for each file to process */
44 enum codec_status codec_run(void)
46 uint32_t elapsedtime;
47 asf_waveformatex_t wfx; /* Holds the stream properties */
48 int res; /* Return values from asf_read_packet() and decode_packet() */
49 uint8_t* audiobuf; /* Pointer to the payload of one wma pro packet */
50 int audiobufsize; /* Payload size */
51 int packetlength = 0; /* Logical packet size (minus the header size) */
52 int outlen = 0; /* Number of bytes written to the output buffer */
53 int pktcnt = 0; /* Count of the packets played */
54 uint8_t *data; /* Pointer to decoder input buffer */
55 int size; /* Size of the input frame to the decoder */
56 intptr_t param;
58 restart_track:
59 if (codec_init()) {
60 LOGF("(WMA PRO) Error: Error initialising codec\n");
61 return CODEC_ERROR;
64 /* Copy the format metadata we've stored in the id3 TOC field. This
65 saves us from parsing it again here. */
66 memcpy(&wfx, ci->id3->toc, sizeof(wfx));
68 ci->configure(DSP_SWITCH_FREQUENCY, wfx.rate);
69 ci->configure(DSP_SET_STEREO_MODE, wfx.channels == 1 ?
70 STEREO_MONO : STEREO_NONINTERLEAVED);
71 codec_set_replaygain(ci->id3);
73 if (decode_init(&wfx) < 0) {
74 LOGF("(WMA PRO) Error: Unsupported or corrupt file\n");
75 return CODEC_ERROR;
78 /* Now advance the file position to the first frame */
79 ci->seek_buffer(ci->id3->first_frame_offset);
81 elapsedtime = 0;
83 /* The main decoding loop */
85 while (pktcnt < wfx.numpackets)
87 enum codec_command_action action = ci->get_command(&param);
89 if (action == CODEC_ACTION_HALT)
90 break;
92 /* Deal with any pending seek requests */
93 if (action == CODEC_ACTION_SEEK_TIME) {
94 if (param == 0) {
95 ci->set_elapsed(0);
96 ci->seek_complete();
97 goto restart_track; /* Pretend you never saw this... */
100 elapsedtime = asf_seek(param, &wfx);
101 if (elapsedtime < 1){
102 ci->set_elapsed(0);
103 ci->seek_complete();
104 break;
107 ci->set_elapsed(elapsedtime);
108 ci->seek_complete();
111 res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx);
113 if (res < 0) {
114 LOGF("(WMA PRO) Warning: asf_read_packet returned %d", res);
115 return CODEC_ERROR;
116 } else {
117 data = audiobuf;
118 size = audiobufsize;
119 pktcnt++;
121 /* We now loop on the packet, decoding and outputting the subframes
122 * one-by-one. For more information about how wma pro structures its
123 * audio frames, see libwmapro/wmaprodec.c */
124 while(size > 0)
126 res = decode_packet(&wfx, dec, &outlen, data, size);
127 if(res < 0) {
128 LOGF("(WMA PRO) Error: decode_packet returned %d", res);
129 return CODEC_ERROR;
131 data += res;
132 size -= res;
133 if(outlen) {
134 ci->yield ();
135 outlen /= (wfx.channels);
136 ci->pcmbuf_insert(dec[0], dec[1], outlen );
137 elapsedtime += outlen*10/(wfx.rate/100);
138 ci->set_elapsed(elapsedtime);
139 ci->yield ();
145 /* Advance to the next logical packet */
146 ci->advance_buffer(packetlength);
149 return CODEC_OK;