Improve SDL port 'configure --prefix' error message
[kugel-rb.git] / apps / codecs / wmavoice.c
blobddf66828f1a0615696c2f12bc6756a857f5000c5
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 "libwmavoice/wmavoice.h"
26 CODEC_HEADER
28 static AVCodecContext avctx;
29 static AVPacket avpkt;
31 #define MAX_FRAMES 3 /*maximum number of frames per superframe*/
32 #define MAX_FRAMESIZE 160 /* maximum number of samples per frame */
33 #define BUFSIZE MAX_FRAMES*MAX_FRAMESIZE
34 static int32_t decoded[BUFSIZE] IBSS_ATTR;
37 /* This function initialises AVCodecContext with the data needed for the wmapro
38 * decoder to work. The required data is taken from asf_waveformatex_t because that's
39 * what the rockbox asf metadata parser fill/work with. In the future, when the
40 * codec is being optimised for on-target playback this function should not be needed. */
41 static void init_codec_ctx(AVCodecContext *avctx, asf_waveformatex_t *wfx)
43 /* Copy the extra-data */
44 avctx->extradata_size = wfx->datalen;
45 avctx->extradata = (uint8_t *)malloc(wfx->datalen*sizeof(uint8_t));
46 memcpy(avctx->extradata, wfx->data, wfx->datalen*sizeof(uint8_t));
48 avctx->block_align = wfx->blockalign;
49 avctx->sample_rate = wfx->rate;
50 avctx->channels = wfx->channels;
54 /* this is the codec entry point */
55 enum codec_status codec_main(void)
57 uint32_t elapsedtime;
58 int retval;
59 asf_waveformatex_t wfx; /* Holds the stream properties */
60 size_t resume_offset;
61 int res; /* Return values from asf_read_packet() and decode_packet() */
62 uint8_t* audiobuf; /* Pointer to the payload of one wma pro packet */
63 int audiobufsize; /* Payload size */
64 int packetlength = 0; /* Logical packet size (minus the header size) */
65 int outlen = 0; /* Number of bytes written to the output buffer */
66 int pktcnt = 0; /* Count of the packets played */
68 /* Generic codec initialisation */
69 ci->configure(DSP_SET_SAMPLE_DEPTH, 31);
72 next_track:
73 retval = CODEC_OK;
75 /* Wait for the metadata to be read */
76 if (codec_wait_taginfo() != 0)
77 goto done;
79 /* Remember the resume position */
80 resume_offset = ci->id3->offset;
81 restart_track:
82 retval = CODEC_OK;
84 if (codec_init()) {
85 LOGF("(WMA Voice) Error: Error initialising codec\n");
86 retval = CODEC_ERROR;
87 goto done;
90 /* Copy the format metadata we've stored in the id3 TOC field. This
91 saves us from parsing it again here. */
92 memcpy(&wfx, ci->id3->toc, sizeof(wfx));
93 memset(&avctx, 0, sizeof(AVCodecContext));
94 memset(&avpkt, 0, sizeof(AVPacket));
96 ci->configure(DSP_SWITCH_FREQUENCY, wfx.rate);
97 ci->configure(DSP_SET_STEREO_MODE, wfx.channels == 1 ?
98 STEREO_MONO : STEREO_INTERLEAVED);
99 codec_set_replaygain(ci->id3);
101 /* Initialise the AVCodecContext */
102 init_codec_ctx(&avctx, &wfx);
104 if (wmavoice_decode_init(&avctx) < 0) {
105 LOGF("(WMA Voice) Error: Unsupported or corrupt file\n");
106 retval = CODEC_ERROR;
107 goto done;
110 /* Now advance the file position to the first frame */
111 ci->seek_buffer(ci->id3->first_frame_offset);
113 elapsedtime = 0;
114 resume_offset = 0;
116 /* The main decoding loop */
118 while (pktcnt < wfx.numpackets)
120 ci->yield();
121 if (ci->stop_codec || ci->new_track) {
122 goto done;
125 /* Deal with any pending seek requests */
126 if (ci->seek_time){
128 if (ci->seek_time == 1) {
129 ci->seek_complete();
130 goto restart_track; /* Pretend you never saw this... */
133 elapsedtime = asf_seek(ci->seek_time, &wfx);
134 if (elapsedtime < 1){
135 ci->seek_complete();
136 goto next_track;
139 ci->set_elapsed(elapsedtime);
140 ci->seek_complete();
143 new_packet:
144 res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx);
146 if (res < 0) {
147 LOGF("(WMA Voice) read_packet error %d\n",res);
148 goto done;
149 } else {
150 avpkt.data = audiobuf;
151 avpkt.size = audiobufsize;
152 pktcnt++;
154 while(avpkt.size > 0)
156 /* wmavoice_decode_packet checks for the output buffer size to
157 avoid overflows */
158 outlen = BUFSIZE*sizeof(int32_t);
160 res = wmavoice_decode_packet(&avctx, decoded, &outlen, &avpkt);
161 if(res < 0) {
162 LOGF("(WMA Voice) Error: decode_packet returned %d", res);
163 if(res == ERROR_WMAPRO_IN_WMAVOICE){
164 /* Just skip this packet */
165 ci->advance_buffer(packetlength);
166 goto new_packet;
168 else
169 goto done;
171 avpkt.data += res;
172 avpkt.size -= res;
173 if(outlen) {
174 ci->yield ();
175 outlen /= sizeof(int32_t);
176 ci->pcmbuf_insert(decoded, NULL, outlen);
177 elapsedtime += outlen*10/(wfx.rate/100);
178 ci->set_elapsed(elapsedtime);
179 ci->yield ();
185 /* Advance to the next logical packet */
186 ci->advance_buffer(packetlength);
189 done:
190 if (ci->request_next_track())
191 goto next_track;
193 return retval;