blob: bab3b5a027e892589b18fe9de2aa668575fc4feb [file] [log] [blame]
Mohamed Tarekcd4d80a2010-05-09 21:42:09 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 Mohamed Tarek
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "codeclib.h"
Mohamed Tarek816fca82010-06-21 10:48:34 +000023#include "libasf/asf.h"
24#include "libwmapro/wmaprodec.h"
Mohamed Tarekcd4d80a2010-05-09 21:42:09 +000025
26CODEC_HEADER
27
Nils Wallménius7c6056b2011-06-01 10:28:26 +000028static int32_t *dec[2]; /* pointers to the output buffers in WMAProDecodeCtx in
29 wmaprodec.c */
Mohamed Tarek816fca82010-06-21 10:48:34 +000030
Michael Sevakisc537d592011-04-27 03:08:23 +000031
Mohamed Tarekcd4d80a2010-05-09 21:42:09 +000032/* this is the codec entry point */
Michael Sevakisc537d592011-04-27 03:08:23 +000033enum codec_status codec_main(enum codec_entry_call_reason reason)
34{
35 if (reason == CODEC_LOAD) {
36 /* Generic codec initialisation */
37 ci->configure(DSP_SET_SAMPLE_DEPTH, WMAPRO_DSP_SAMPLE_DEPTH);
38 }
39
40 return CODEC_OK;
41}
42
43/* this is called for each file to process */
44enum codec_status codec_run(void)
Mohamed Tarekcd4d80a2010-05-09 21:42:09 +000045{
Mohamed Tarek816fca82010-06-21 10:48:34 +000046 uint32_t elapsedtime;
Mohamed Tarek816fca82010-06-21 10:48:34 +000047 asf_waveformatex_t wfx; /* Holds the stream properties */
Mohamed Tarek816fca82010-06-21 10:48:34 +000048 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 */
Andree Buschmannaa2fca32010-07-29 22:18:04 +000054 uint8_t *data; /* Pointer to decoder input buffer */
55 int size; /* Size of the input frame to the decoder */
Michael Sevakisc537d592011-04-27 03:08:23 +000056 intptr_t param;
Mohamed Tarek816fca82010-06-21 10:48:34 +000057
Michael Sevakis85e40252011-02-20 15:27:10 +000058restart_track:
Mohamed Tarek816fca82010-06-21 10:48:34 +000059 if (codec_init()) {
60 LOGF("(WMA PRO) Error: Error initialising codec\n");
Michael Sevakisc537d592011-04-27 03:08:23 +000061 return CODEC_ERROR;
Mohamed Tarek816fca82010-06-21 10:48:34 +000062 }
63
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));
67
68 ci->configure(DSP_SWITCH_FREQUENCY, wfx.rate);
69 ci->configure(DSP_SET_STEREO_MODE, wfx.channels == 1 ?
Mohamed Tarek5dd8c532010-07-26 22:03:20 +000070 STEREO_MONO : STEREO_NONINTERLEAVED);
Mohamed Tarek816fca82010-06-21 10:48:34 +000071 codec_set_replaygain(ci->id3);
Mohamed Tarek816fca82010-06-21 10:48:34 +000072
Mohamed Tarek387af972010-07-15 05:38:09 +000073 if (decode_init(&wfx) < 0) {
Mohamed Tarek816fca82010-06-21 10:48:34 +000074 LOGF("(WMA PRO) Error: Unsupported or corrupt file\n");
Michael Sevakisc537d592011-04-27 03:08:23 +000075 return CODEC_ERROR;
Mohamed Tarek816fca82010-06-21 10:48:34 +000076 }
77
78 /* Now advance the file position to the first frame */
79 ci->seek_buffer(ci->id3->first_frame_offset);
80
81 elapsedtime = 0;
Michael Sevakis7ad2cad2011-08-28 07:45:35 +000082 ci->set_elapsed(0);
Mohamed Tarek816fca82010-06-21 10:48:34 +000083
84 /* The main decoding loop */
85
86 while (pktcnt < wfx.numpackets)
Andree Buschmanne75018a2011-05-01 11:42:41 +000087 {
Michael Sevakisc537d592011-04-27 03:08:23 +000088 enum codec_command_action action = ci->get_command(&param);
Mohamed Tarek816fca82010-06-21 10:48:34 +000089
Michael Sevakisc537d592011-04-27 03:08:23 +000090 if (action == CODEC_ACTION_HALT)
91 break;
92
93 /* Deal with any pending seek requests */
94 if (action == CODEC_ACTION_SEEK_TIME) {
95 if (param == 0) {
96 ci->set_elapsed(0);
Mohamed Tarek816fca82010-06-21 10:48:34 +000097 ci->seek_complete();
98 goto restart_track; /* Pretend you never saw this... */
99 }
100
Michael Sevakisc537d592011-04-27 03:08:23 +0000101 elapsedtime = asf_seek(param, &wfx);
Mohamed Tarek816fca82010-06-21 10:48:34 +0000102 if (elapsedtime < 1){
Michael Sevakisc537d592011-04-27 03:08:23 +0000103 ci->set_elapsed(0);
Mohamed Tarek816fca82010-06-21 10:48:34 +0000104 ci->seek_complete();
Michael Sevakisc537d592011-04-27 03:08:23 +0000105 break;
Mohamed Tarek816fca82010-06-21 10:48:34 +0000106 }
107
108 ci->set_elapsed(elapsedtime);
109 ci->seek_complete();
110 }
111
112 res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx);
113
114 if (res < 0) {
Michael Sevakisc537d592011-04-27 03:08:23 +0000115 LOGF("(WMA PRO) Warning: asf_read_packet returned %d", res);
116 return CODEC_ERROR;
Mohamed Tarek816fca82010-06-21 10:48:34 +0000117 } else {
Mohamed Tarek387af972010-07-15 05:38:09 +0000118 data = audiobuf;
119 size = audiobufsize;
Mohamed Tarek816fca82010-06-21 10:48:34 +0000120 pktcnt++;
121
122 /* We now loop on the packet, decoding and outputting the subframes
123 * one-by-one. For more information about how wma pro structures its
124 * audio frames, see libwmapro/wmaprodec.c */
Mohamed Tarek387af972010-07-15 05:38:09 +0000125 while(size > 0)
Mohamed Tarek816fca82010-06-21 10:48:34 +0000126 {
Mohamed Tarek5dd8c532010-07-26 22:03:20 +0000127 res = decode_packet(&wfx, dec, &outlen, data, size);
Mohamed Tarekf98e8032010-07-17 09:55:38 +0000128 if(res < 0) {
129 LOGF("(WMA PRO) Error: decode_packet returned %d", res);
Michael Sevakisc537d592011-04-27 03:08:23 +0000130 return CODEC_ERROR;
Mohamed Tarekf98e8032010-07-17 09:55:38 +0000131 }
Mohamed Tarek387af972010-07-15 05:38:09 +0000132 data += res;
133 size -= res;
Mohamed Tarek816fca82010-06-21 10:48:34 +0000134 if(outlen) {
135 ci->yield ();
Mohamed Tarek5da23042010-08-03 22:59:03 +0000136 outlen /= (wfx.channels);
Mohamed Tarek5dd8c532010-07-26 22:03:20 +0000137 ci->pcmbuf_insert(dec[0], dec[1], outlen );
Mohamed Tarek816fca82010-06-21 10:48:34 +0000138 elapsedtime += outlen*10/(wfx.rate/100);
139 ci->set_elapsed(elapsedtime);
140 ci->yield ();
141 }
142 }
143
144 }
145
146 /* Advance to the next logical packet */
147 ci->advance_buffer(packetlength);
148 }
Mohamed Tarek816fca82010-06-21 10:48:34 +0000149
Michael Sevakisc537d592011-04-27 03:08:23 +0000150 return CODEC_OK;
Mohamed Tarekcd4d80a2010-05-09 21:42:09 +0000151}
152