1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include "libasf/asf.h"
24 #include "libwmapro/wmaprodec.h"
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
);
42 /* this is called for each file to process */
43 enum codec_status
codec_run(void)
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 */
59 LOGF("(WMA PRO) Error: Error initialising codec\n");
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");
77 /* Now advance the file position to the first frame */
78 ci
->seek_buffer(ci
->id3
->first_frame_offset
);
82 /* The main decoding loop */
84 while (pktcnt
< wfx
.numpackets
)
86 enum codec_command_action action
= ci
->get_command(¶m
);
88 if (action
== CODEC_ACTION_HALT
)
91 /* Deal with any pending seek requests */
92 if (action
== CODEC_ACTION_SEEK_TIME
) {
96 goto restart_track
; /* Pretend you never saw this... */
99 elapsedtime
= asf_seek(param
, &wfx
);
100 if (elapsedtime
< 1){
106 ci
->set_elapsed(elapsedtime
);
110 res
= asf_read_packet(&audiobuf
, &audiobufsize
, &packetlength
, &wfx
);
113 LOGF("(WMA PRO) Warning: asf_read_packet returned %d", res
);
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 */
125 res
= decode_packet(&wfx
, dec
, &outlen
, data
, size
);
127 LOGF("(WMA PRO) Error: decode_packet returned %d", res
);
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
);
144 /* Advance to the next logical packet */
145 ci
->advance_buffer(packetlength
);