Merge branch 'master' into android-test-plugins
[kugel-rb.git] / apps / codecs / wmapro.c
blobd451810216047b97915c902ae491aebebd412866
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 */
31 /* this is the codec entry point */
32 enum codec_status codec_main(enum codec_entry_call_reason reason)
34 if (reason == CODEC_LOAD) {
35 /* Generic codec initialisation */
36 ci->configure(DSP_SET_SAMPLE_DEPTH, WMAPRO_DSP_SAMPLE_DEPTH);
39 return CODEC_OK;
42 /* this is called for each file to process */
43 enum codec_status codec_run(void)
45 uint32_t elapsedtime;
46 asf_waveformatex_t wfx; /* Holds the stream properties */
47 int res; /* Return values from asf_read_packet() and decode_packet() */
48 uint8_t* audiobuf; /* Pointer to the payload of one wma pro packet */
49 int audiobufsize; /* Payload size */
50 int packetlength = 0; /* Logical packet size (minus the header size) */
51 int outlen = 0; /* Number of bytes written to the output buffer */
52 int pktcnt = 0; /* Count of the packets played */
53 uint8_t *data; /* Pointer to decoder input buffer */
54 int size; /* Size of the input frame to the decoder */
55 intptr_t param;
57 restart_track:
58 if (codec_init()) {
59 LOGF("(WMA PRO) Error: Error initialising codec\n");
60 return CODEC_ERROR;
63 /* Copy the format metadata we've stored in the id3 TOC field. This
64 saves us from parsing it again here. */
65 memcpy(&wfx, ci->id3->toc, sizeof(wfx));
67 ci->configure(DSP_SWITCH_FREQUENCY, wfx.rate);
68 ci->configure(DSP_SET_STEREO_MODE, wfx.channels == 1 ?
69 STEREO_MONO : STEREO_NONINTERLEAVED);
70 codec_set_replaygain(ci->id3);
72 if (decode_init(&wfx) < 0) {
73 LOGF("(WMA PRO) Error: Unsupported or corrupt file\n");
74 return CODEC_ERROR;
77 /* Now advance the file position to the first frame */
78 ci->seek_buffer(ci->id3->first_frame_offset);
80 elapsedtime = 0;
82 /* The main decoding loop */
84 while (pktcnt < wfx.numpackets)
86 enum codec_command_action action = ci->get_command(&param);
88 if (action == CODEC_ACTION_HALT)
89 break;
91 /* Deal with any pending seek requests */
92 if (action == CODEC_ACTION_SEEK_TIME) {
93 if (param == 0) {
94 ci->set_elapsed(0);
95 ci->seek_complete();
96 goto restart_track; /* Pretend you never saw this... */
99 elapsedtime = asf_seek(param, &wfx);
100 if (elapsedtime < 1){
101 ci->set_elapsed(0);
102 ci->seek_complete();
103 break;
106 ci->set_elapsed(elapsedtime);
107 ci->seek_complete();
110 res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx);
112 if (res < 0) {
113 LOGF("(WMA PRO) Warning: asf_read_packet returned %d", res);
114 return CODEC_ERROR;
115 } else {
116 data = audiobuf;
117 size = audiobufsize;
118 pktcnt++;
120 /* We now loop on the packet, decoding and outputting the subframes
121 * one-by-one. For more information about how wma pro structures its
122 * audio frames, see libwmapro/wmaprodec.c */
123 while(size > 0)
125 res = decode_packet(&wfx, dec, &outlen, data, size);
126 if(res < 0) {
127 LOGF("(WMA PRO) Error: decode_packet returned %d", res);
128 return CODEC_ERROR;
130 data += res;
131 size -= res;
132 if(outlen) {
133 ci->yield ();
134 outlen /= (wfx.channels);
135 ci->pcmbuf_insert(dec[0], dec[1], outlen );
136 elapsedtime += outlen*10/(wfx.rate/100);
137 ci->set_elapsed(elapsedtime);
138 ci->yield ();
144 /* Advance to the next logical packet */
145 ci->advance_buffer(packetlength);
148 return CODEC_OK;