blob: e02d4592620b961251a627c5f28cb54cfd4b387f [file] [log] [blame]
Daniel Stenberg1dd672f2005-06-22 19:41:30 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
Nicolas Pennequin357ffb32008-05-05 10:32:46 +000010 * Copyright (C) 2002 Björn Stenberg
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000011 *
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.
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
Thom Johansenc91e0bb2005-10-13 11:32:52 +000021
22#include "codeclib.h"
Björn Stenberg45bd7e02008-11-05 13:30:58 +000023#include "libtremor/ivorbisfile.h"
24#include "libtremor/ogg.h"
Magnus Holmgren3fad1522009-08-29 12:23:40 +000025#ifdef SIMULATOR
26#include "lib/tlsf/src/tlsf.h"
27#endif
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000028
Jens Arnoldb8749fd2006-01-18 00:05:14 +000029CODEC_HEADER
30
Magnus Holmgren3fad1522009-08-29 12:23:40 +000031#if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS)
Magnus Holmgrenf4515c32009-03-08 12:48:58 +000032#include <setjmp.h>
33jmp_buf rb_jump_buf;
34#endif
35
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000036/* Some standard functions and variables needed by Tremor */
37
Bertrik Sikken1e3ed742008-08-12 22:01:55 +000038static size_t read_handler(void *ptr, size_t size, size_t nmemb, void *datasource)
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000039{
Daniel Stenberg31efab42005-06-22 19:55:09 +000040 (void)datasource;
Tomasz Malesinski80da8b12006-11-26 18:31:41 +000041 return ci->read_filebuf(ptr, nmemb*size);
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000042}
43
Bertrik Sikken1e3ed742008-08-12 22:01:55 +000044static int initial_seek_handler(void *datasource, ogg_int64_t offset, int whence)
Thom Johansen0ad1ed62005-10-10 20:19:09 +000045{
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000046 (void)datasource;
47 (void)offset;
48 (void)whence;
49 return -1;
50}
51
Bertrik Sikken1e3ed742008-08-12 22:01:55 +000052static int seek_handler(void *datasource, ogg_int64_t offset, int whence)
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +000053{
54 (void)datasource;
55
Thom Johansen0ad1ed62005-10-10 20:19:09 +000056 if (whence == SEEK_CUR) {
Tomasz Malesinski80da8b12006-11-26 18:31:41 +000057 offset += ci->curpos;
Thom Johansen0ad1ed62005-10-10 20:19:09 +000058 } else if (whence == SEEK_END) {
Tomasz Malesinski80da8b12006-11-26 18:31:41 +000059 offset += ci->filesize;
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +000060 }
61
Tomasz Malesinski80da8b12006-11-26 18:31:41 +000062 if (ci->seek_buffer(offset)) {
Thom Johansen0ad1ed62005-10-10 20:19:09 +000063 return 0;
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +000064 }
65
66 return -1;
67}
68
Bertrik Sikken1e3ed742008-08-12 22:01:55 +000069static int close_handler(void *datasource)
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000070{
71 (void)datasource;
72 return 0;
73}
74
Bertrik Sikken1e3ed742008-08-12 22:01:55 +000075static long tell_handler(void *datasource)
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000076{
Daniel Stenberg31efab42005-06-22 19:55:09 +000077 (void)datasource;
Tomasz Malesinski80da8b12006-11-26 18:31:41 +000078 return ci->curpos;
Daniel Stenberg1dd672f2005-06-22 19:41:30 +000079}
80
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +000081/* This sets the DSP parameters based on the current logical bitstream
Magnus Holmgren3fad1522009-08-29 12:23:40 +000082 * (sampling rate, number of channels, etc).
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +000083 */
Bertrik Sikken1e3ed742008-08-12 22:01:55 +000084static bool vorbis_set_codec_parameters(OggVorbis_File *vf)
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +000085{
Thom Johansen0ad1ed62005-10-10 20:19:09 +000086 vorbis_info *vi;
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +000087
Thom Johansen0ad1ed62005-10-10 20:19:09 +000088 vi = ov_info(vf, -1);
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +000089
Thom Johansen0ad1ed62005-10-10 20:19:09 +000090 if (vi == NULL) {
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +000091 return false;
92 }
93
Michael Sevakis97f369a2007-02-10 16:34:16 +000094 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
Tomasz Malesinski80da8b12006-11-26 18:31:41 +000095 codec_set_replaygain(ci->id3);
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +000096
97 if (vi->channels == 2) {
Michael Sevakis97f369a2007-02-10 16:34:16 +000098 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +000099 } else if (vi->channels == 1) {
Michael Sevakis97f369a2007-02-10 16:34:16 +0000100 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +0000101 }
102
103 return true;
104}
105
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000106/* this is the codec entry point */
Michael Sevakisc537d592011-04-27 03:08:23 +0000107enum codec_status codec_main(enum codec_entry_call_reason reason)
108{
109 if (reason == CODEC_LOAD) {
110 if (codec_init())
111 return CODEC_ERROR;
112 ci->configure(DSP_SET_SAMPLE_DEPTH, 24);
113 }
114
115 return CODEC_OK;
116}
117
118/* this is called for each file to process */
119enum codec_status codec_run(void)
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000120{
121 ov_callbacks callbacks;
122 OggVorbis_File vf;
Thom Johansen0ad1ed62005-10-10 20:19:09 +0000123 ogg_int32_t **pcm;
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000124
Michael Sevakisc537d592011-04-27 03:08:23 +0000125 int error = CODEC_ERROR;
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000126 long n;
127 int current_section;
Magnus Holmgrenf4515c32009-03-08 12:48:58 +0000128 int previous_section;
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000129 int eof;
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000130 ogg_int64_t vf_offsets[2];
131 ogg_int64_t vf_dataoffsets;
132 ogg_uint32_t vf_serialnos;
133 ogg_int64_t vf_pcmlengths[2];
Michael Sevakisc537d592011-04-27 03:08:23 +0000134 intptr_t param;
Magnus Holmgren3fad1522009-08-29 12:23:40 +0000135
Magnus Holmgren3fad1522009-08-29 12:23:40 +0000136#if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS)
137 if (setjmp(rb_jump_buf) != 0) {
Michael Sevakisc537d592011-04-27 03:08:23 +0000138 /* malloc failed; finish with this track */
Magnus Holmgrenf4515c32009-03-08 12:48:58 +0000139 goto done;
140 }
141#endif
Bertrik Sikkene70bc182010-08-23 20:09:58 +0000142 ogg_malloc_init();
143
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000144 /* Create a decoder instance */
Thom Johansen0ad1ed62005-10-10 20:19:09 +0000145 callbacks.read_func = read_handler;
146 callbacks.seek_func = initial_seek_handler;
147 callbacks.tell_func = tell_handler;
148 callbacks.close_func = close_handler;
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000149
Michael Sevakisc537d592011-04-27 03:08:23 +0000150 ci->seek_buffer(0);
151
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000152 /* Open a non-seekable stream */
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000153 error = ov_open_callbacks(ci, &vf, NULL, 0, callbacks);
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000154
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000155 /* If the non-seekable open was successful, we need to supply the missing
156 * data to make it seekable. This is a hack, but it's reasonable since we
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +0000157 * don't want to run the whole file through the buffer before we start
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000158 * playing. Using Tremor's seekable open routine would cause us to do
159 * this, so we pretend not to be seekable at first. Then we fill in the
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000160 * missing fields of vf with 1) information in ci->id3, and 2) info
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000161 * obtained by Tremor in the above ov_open call.
162 *
163 * Note that this assumes there is only ONE logical Vorbis bitstream in our
164 * physical Ogg bitstream. This is verified in metadata.c, well before we
165 * get here.
166 */
Thom Johansen0ad1ed62005-10-10 20:19:09 +0000167 if (!error) {
Andree Buschmann4d5e8822010-02-07 18:38:47 +0000168 ogg_free(vf.offsets);
169 ogg_free(vf.dataoffsets);
170 ogg_free(vf.serialnos);
Magnus Holmgren3fad1522009-08-29 12:23:40 +0000171
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +0000172 vf.offsets = vf_offsets;
173 vf.dataoffsets = &vf_dataoffsets;
174 vf.serialnos = &vf_serialnos;
175 vf.pcmlengths = vf_pcmlengths;
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000176
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +0000177 vf.offsets[0] = 0;
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000178 vf.offsets[1] = ci->id3->filesize;
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +0000179 vf.dataoffsets[0] = vf.offset;
180 vf.pcmlengths[0] = 0;
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000181 vf.pcmlengths[1] = ci->id3->samples;
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +0000182 vf.serialnos[0] = vf.current_serialno;
Thom Johansen0ad1ed62005-10-10 20:19:09 +0000183 vf.callbacks.seek_func = seek_handler;
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +0000184 vf.seekable = 1;
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000185 vf.end = ci->id3->filesize;
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +0000186 vf.ready_state = OPENED;
187 vf.links = 1;
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000188 } else {
Nils Wallménius984e1a82010-12-15 21:28:52 +0000189 DEBUGF("Vorbis: ov_open failed: %d\n", error);
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000190 goto done;
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000191 }
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000192
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000193 if (ci->id3->offset) {
Michael Sevakisc537d592011-04-27 03:08:23 +0000194 ci->seek_buffer(ci->id3->offset);
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000195 ov_raw_seek(&vf, ci->id3->offset);
196 ci->set_elapsed(ov_time_tell(&vf));
197 ci->set_offset(ov_raw_tell(&vf));
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000198 }
199
Magnus Holmgrenf4515c32009-03-08 12:48:58 +0000200 previous_section = -1;
Thom Johansen0ad1ed62005-10-10 20:19:09 +0000201 eof = 0;
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000202 while (!eof) {
Michael Sevakisc537d592011-04-27 03:08:23 +0000203 enum codec_command_action action = ci->get_command(&param);
204
205 if (action == CODEC_ACTION_HALT)
Thom Johansen0ad1ed62005-10-10 20:19:09 +0000206 break;
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000207
Michael Sevakisc537d592011-04-27 03:08:23 +0000208 if (action == CODEC_ACTION_SEEK_TIME) {
209 if (ov_time_seek(&vf, param)) {
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000210 //ci->logf("ov_time_seek failed");
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000211 }
Michael Sevakisc537d592011-04-27 03:08:23 +0000212
213 ci->set_elapsed(ov_time_tell(&vf));
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000214 ci->seek_complete();
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000215 }
Magnus Holmgren4a537872005-07-24 15:32:28 +0000216
217 /* Read host-endian signed 24-bit PCM samples */
Thom Johansen0ad1ed62005-10-10 20:19:09 +0000218 n = ov_read_fixed(&vf, &pcm, 1024, &current_section);
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000219
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +0000220 /* Change DSP and buffer settings for this bitstream */
Thom Johansen0ad1ed62005-10-10 20:19:09 +0000221 if (current_section != previous_section) {
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +0000222 if (!vorbis_set_codec_parameters(&vf)) {
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000223 goto done;
Linus Nielsen Feltzingc4b7c672005-07-11 06:47:35 +0000224 } else {
225 previous_section = current_section;
226 }
227 }
228
Thom Johansen0ad1ed62005-10-10 20:19:09 +0000229 if (n == 0) {
230 eof = 1;
Linus Nielsen Feltzingeaf8b2d2005-07-05 08:43:36 +0000231 } else if (n < 0) {
Magnus Holmgren3fad1522009-08-29 12:23:40 +0000232 DEBUGF("Vorbis: Error decoding frame\n");
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000233 } else {
Michael Sevakisaba6ca02007-02-07 00:51:50 +0000234 ci->pcmbuf_insert(pcm[0], pcm[1], n);
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000235 ci->set_offset(ov_raw_tell(&vf));
236 ci->set_elapsed(ov_time_tell(&vf));
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000237 }
238 }
Michael Sevakis85e40252011-02-20 15:27:10 +0000239
Michael Sevakisc537d592011-04-27 03:08:23 +0000240 error = CODEC_OK;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000241done:
Magnus Holmgren3fad1522009-08-29 12:23:40 +0000242#if 0 /* defined(SIMULATOR) */
243 {
244 size_t bufsize;
245 void* buf = ci->codec_get_buffer(&bufsize);
246
247 DEBUGF("Vorbis: Memory max: %u\n", get_max_size(buf));
248 }
249#endif
Bertrik Sikkene70bc182010-08-23 20:09:58 +0000250 ogg_malloc_destroy();
Magnus Holmgren3fad1522009-08-29 12:23:40 +0000251
Michael Sevakisc537d592011-04-27 03:08:23 +0000252 /* Clean things up for the next track */
253 vf.dataoffsets = NULL;
254 vf.offsets = NULL;
255 vf.serialnos = NULL;
256 vf.pcmlengths = NULL;
257 ov_clear(&vf);
258
Brandon Low1060e442006-01-18 20:22:03 +0000259 return error;
Daniel Stenberg1dd672f2005-06-22 19:41:30 +0000260}