blob: ccb9f41190cb2546fecfbcd179b0d644d4594a2a [file] [log] [blame]
Dave Bryant7dad7d32005-06-13 06:15:05 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 David Bryant
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Dave Bryant7dad7d32005-06-13 06:15:05 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
Thom Johansenc91e0bb2005-10-13 11:32:52 +000022#include "codeclib.h"
23#include "libwavpack/wavpack.h"
Dave Bryant7dad7d32005-06-13 06:15:05 +000024
Jens Arnoldb8749fd2006-01-18 00:05:14 +000025CODEC_HEADER
26
Dave Bryant7dad7d32005-06-13 06:15:05 +000027#define BUFFER_SIZE 4096
28
Dave Bryant0ad19c72006-03-26 22:54:15 +000029static int32_t temp_buffer [BUFFER_SIZE] IBSS_ATTR;
Dave Bryant7dad7d32005-06-13 06:15:05 +000030
Dave Bryant0ad19c72006-03-26 22:54:15 +000031static int32_t read_callback (void *buffer, int32_t bytes)
Dave Bryant7dad7d32005-06-13 06:15:05 +000032{
Dave Bryant0ad19c72006-03-26 22:54:15 +000033 int32_t retval = ci->read_filebuf (buffer, bytes);
Michael Sevakisc537d592011-04-27 03:08:23 +000034 ci->set_offset(ci->curpos);
Dave Bryant19837382005-06-30 06:09:59 +000035 return retval;
Dave Bryant7dad7d32005-06-13 06:15:05 +000036}
37
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000038/* this is the codec entry point */
Michael Sevakisc537d592011-04-27 03:08:23 +000039enum codec_status codec_main(enum codec_entry_call_reason reason)
40{
41 if (reason == CODEC_LOAD) {
42 /* Generic codec initialisation */
43 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
44 }
45
46 return CODEC_OK;
47}
48
49/* this is called for each file to process */
50enum codec_status codec_run(void)
Dave Bryant7dad7d32005-06-13 06:15:05 +000051{
52 WavpackContext *wpc;
53 char error [80];
Dave Bryant24d65352005-06-27 00:12:40 +000054 int bps, nchans, sr_100;
Michael Sevakisc537d592011-04-27 03:08:23 +000055 intptr_t param;
Dave Bryant7dad7d32005-06-13 06:15:05 +000056
Michael Sevakisc537d592011-04-27 03:08:23 +000057 if (codec_init())
58 return CODEC_ERROR;
Dave Bryant7dad7d32005-06-13 06:15:05 +000059
Michael Sevakisc537d592011-04-27 03:08:23 +000060 ci->seek_buffer (ci->id3->offset);
Dave Bryant7dad7d32005-06-13 06:15:05 +000061
Dave Bryant7dad7d32005-06-13 06:15:05 +000062 /* Create a decoder instance */
Dave Bryant7dad7d32005-06-13 06:15:05 +000063 wpc = WavpackOpenFileInput (read_callback, error);
64
Michael Sevakisc537d592011-04-27 03:08:23 +000065 if (!wpc)
66 return CODEC_ERROR;
Dave Bryant7dad7d32005-06-13 06:15:05 +000067
Dave Bryantbcf97a42007-08-12 06:36:06 +000068 ci->configure(DSP_SWITCH_FREQUENCY, WavpackGetSampleRate (wpc));
69 codec_set_replaygain(ci->id3);
Dave Bryant7dad7d32005-06-13 06:15:05 +000070 bps = WavpackGetBytesPerSample (wpc);
71 nchans = WavpackGetReducedChannels (wpc);
Michael Sevakis97f369a2007-02-10 16:34:16 +000072 ci->configure(DSP_SET_STEREO_MODE, nchans == 2 ? STEREO_INTERLEAVED : STEREO_MONO);
Dave Bryant24d65352005-06-27 00:12:40 +000073 sr_100 = ci->id3->frequency / 100;
Dave Bryant7dad7d32005-06-13 06:15:05 +000074
75 ci->set_elapsed (0);
76
77 /* The main decoder loop */
78
79 while (1) {
Michael Sevakisc537d592011-04-27 03:08:23 +000080 int32_t nsamples;
81 enum codec_command_action action = ci->get_command(&param);
Dave Bryant7dad7d32005-06-13 06:15:05 +000082
Michael Sevakisc537d592011-04-27 03:08:23 +000083 if (action == CODEC_ACTION_HALT)
84 break;
85
86 if (action == CODEC_ACTION_SEEK_TIME) {
Dave Bryant24d65352005-06-27 00:12:40 +000087 int curpos_ms = WavpackGetSampleIndex (wpc) / sr_100 * 10;
Dave Bryant7dad7d32005-06-13 06:15:05 +000088 int n, d, skip;
89
Michael Sevakisc537d592011-04-27 03:08:23 +000090 if (param > curpos_ms) {
91 n = param - curpos_ms;
Dave Bryant7dad7d32005-06-13 06:15:05 +000092 d = ci->id3->length - curpos_ms;
Dave Bryant0ad19c72006-03-26 22:54:15 +000093 skip = (int)((int64_t)(ci->filesize - ci->curpos) * n / d);
Dave Bryant7dad7d32005-06-13 06:15:05 +000094 ci->seek_buffer (ci->curpos + skip);
95 }
Michael Sevakisc537d592011-04-27 03:08:23 +000096 else if (curpos_ms != 0) {
97 n = curpos_ms - param;
Dave Bryant7dad7d32005-06-13 06:15:05 +000098 d = curpos_ms;
Dave Bryant0ad19c72006-03-26 22:54:15 +000099 skip = (int)((int64_t) ci->curpos * n / d);
Dave Bryant7dad7d32005-06-13 06:15:05 +0000100 ci->seek_buffer (ci->curpos - skip);
101 }
102
103 wpc = WavpackOpenFileInput (read_callback, error);
Dave Bryant7dad7d32005-06-13 06:15:05 +0000104 if (!wpc)
Michael Sevakisc537d592011-04-27 03:08:23 +0000105 {
106 ci->seek_complete();
Dave Bryant7dad7d32005-06-13 06:15:05 +0000107 break;
Michael Sevakisc537d592011-04-27 03:08:23 +0000108 }
Dave Bryant7dad7d32005-06-13 06:15:05 +0000109
Dave Bryant24d65352005-06-27 00:12:40 +0000110 ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
Michael Sevakisc537d592011-04-27 03:08:23 +0000111 ci->seek_complete();
Dave Bryant7dad7d32005-06-13 06:15:05 +0000112 }
113
Dave Bryantff40af42006-02-06 07:40:35 +0000114 nsamples = WavpackUnpackSamples (wpc, temp_buffer, BUFFER_SIZE / nchans);
Dave Bryant7dad7d32005-06-13 06:15:05 +0000115
Michael Sevakisc537d592011-04-27 03:08:23 +0000116 if (!nsamples)
Dave Bryant7dad7d32005-06-13 06:15:05 +0000117 break;
118
Michael Sevakisaba6ca02007-02-07 00:51:50 +0000119 ci->pcmbuf_insert (temp_buffer, NULL, nsamples);
Dave Bryant24d65352005-06-27 00:12:40 +0000120 ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
Dave Bryant7dad7d32005-06-13 06:15:05 +0000121 }
122
Michael Sevakisc537d592011-04-27 03:08:23 +0000123 return CODEC_OK;
Dave Bryant7dad7d32005-06-13 06:15:05 +0000124}