/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id$ * * Copyright (C) 2005 Dave Chapman * * All files in this archive are subject to the GNU General Public License. * See the file COPYING in the source tree root for full license agreement. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ #include "codec.h" #include #include "playback.h" #include "dsp.h" #include "lib/codeclib.h" #include "system.h" #include struct mad_stream Stream IDATA_ATTR; struct mad_frame Frame IDATA_ATTR; struct mad_synth Synth IDATA_ATTR; mad_timer_t Timer; /* The following function is used inside libmad - let's hope it's never called. */ void abort(void) { } #define INPUT_CHUNK_SIZE 8192 mad_fixed_t mad_frame_overlap[2][32][18] IDATA_ATTR; unsigned char mad_main_data[MAD_BUFFER_MDLEN] IDATA_ATTR; /* TODO: what latency does layer 1 have? */ int mpeg_latency[3] = { 0, 481, 529 }; #ifdef USE_IRAM extern char iramcopy[]; extern char iramstart[]; extern char iramend[]; #endif struct codec_api *ci; int64_t samplecount; int64_t samplesdone; int stop_skip, start_skip; int current_stereo_mode = -1; unsigned int current_frequency = 0; void recalc_samplecount(void) { /* NOTE: currently this doesn't work, the below calculated samples_count seems to be right, but sometimes we just don't have all the data we need... */ if (ci->id3->frame_count) { /* TODO: 1152 is the frame size in samples for MPEG1 layer 2 and layer 3, it's probably not correct at all for MPEG2 and layer 1 */ samplecount = ((int64_t) ci->id3->frame_count) * 1152; } else { samplecount = ((int64_t) ci->id3->length) * current_frequency / 1000; } samplecount -= start_skip + stop_skip; } /* this is the codec entry point */ enum codec_status codec_start(struct codec_api* api) { int Status = 0; size_t size; int file_end; int frame_skip; char *InputBuffer; ci = api; /* Generic codec inititialisation */ TEST_CODEC_API(api); #ifdef USE_IRAM ci->memcpy(iramstart, iramcopy, iramend - iramstart); #endif /* This function sets up the buffers and reads the file into RAM */ if (codec_init(api)) { return CODEC_ERROR; } /* Create a decoder instance */ ci->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*2)); ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*16)); ci->configure(DSP_SET_CLIP_MIN, (int *)-MAD_F_ONE); ci->configure(DSP_SET_CLIP_MAX, (int *)(MAD_F_ONE - 1)); ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(MAD_F_FRACBITS)); ci->configure(DSP_DITHER, (bool *)false); ci->configure(CODEC_DSP_ENABLE, (bool *)true); ci->memset(&Stream, 0, sizeof(struct mad_stream)); ci->memset(&Frame, 0, sizeof(struct mad_frame)); ci->memset(&Synth, 0, sizeof(struct mad_synth)); ci->memset(&Timer, 0, sizeof(mad_timer_t)); mad_stream_init(&Stream); mad_frame_init(&Frame); mad_synth_init(&Synth); mad_timer_reset(&Timer); /* We do this so libmad doesn't try to call codec_calloc() */ memset(mad_frame_overlap, 0, sizeof(mad_frame_overlap)); Frame.overlap = &mad_frame_overlap; Stream.main_data = &mad_main_data; /* This label might need to be moved above all the init code, but I don't think reiniting the codec is necessary for MPEG. It might even be unwanted for gapless playback */ next_track: file_end = 0; while (!*ci->taginfo_ready && !ci->stop_codec) ci->sleep(1); ci->configure(DSP_SET_FREQUENCY, (int *)ci->id3->frequency); current_frequency = ci->id3->frequency; codec_set_replaygain(ci->id3); ci->request_buffer(&size, ci->id3->first_frame_offset); ci->advance_buffer(size); if (ci->id3->lead_trim >= 0 && ci->id3->tail_trim >= 0) { stop_skip = ci->id3->tail_trim - mpeg_latency[ci->id3->layer]; if (stop_skip < 0) stop_skip = 0; start_skip = ci->id3->lead_trim + mpeg_latency[ci->id3->layer]; } else { stop_skip = 0; /* We want to skip this amount anyway */ start_skip = mpeg_latency[ci->id3->layer]; } samplesdone = ((int64_t) ci->id3->elapsed) * current_frequency / 1000; frame_skip = start_skip; recalc_samplecount(); /* This is the decoding loop. */ while (1) { int framelength; ci->yield(); if (ci->stop_codec || ci->reload_codec) { break ; } if (ci->seek_time) { int newpos; samplesdone = ((int64_t) (ci->seek_time - 1)) * current_frequency / 1000; newpos = ci->mp3_get_filepos(ci->seek_time-1) + ci->id3->first_frame_offset; if (!ci->seek_buffer(newpos)) { goto next_track; } ci->seek_time = 0; if (newpos == 0) frame_skip = start_skip; /* Optional but good thing to do. */ ci->seek_complete(); } /* Lock buffers */ if (Stream.error == 0) { InputBuffer = ci->request_buffer(&size, INPUT_CHUNK_SIZE); if (size == 0 || InputBuffer == NULL) break ; mad_stream_buffer(&Stream, InputBuffer, size); } if(mad_frame_decode(&Frame,&Stream)) { if (Stream.error == MAD_FLAG_INCOMPLETE || Stream.error == MAD/* Emacs style mode select -*- C++ -*- *----------------------------------------------------------------------------- * * * PrBoom a Doom port merged with LxDoom and LSDLDoom * based on BOOM, a modified and improved DOOM engine * Copyright (C) 1999 by * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman * Copyright (C) 1999-2000 by * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. * * DESCRIPTION: * Map utility functions * *-----------------------------------------------------------------------------*/ #ifndef __P_MAPUTL__ #define __P_MAPUTL__ #include "r_defs.h" /* mapblocks are used to check movement against lines and things */ #define MAPBLOCKUNITS 128 #define MAPBLOCKSIZE (MAPBLOCKUNITS*FRACUNIT) #define MAPBLOCKSHIFT (FRACBITS+7) #define MAPBMASK (MAPBLOCKSIZE-1) #define MAPBTOFRAC (MAPBLOCKSHIFT-FRACBITS) #define PT_ADDLINES 1 #define PT_ADDTHINGS 2 #define PT_EARLYOUT 4 typedef struct { fixed_t x; fixed_t y; fixed_t dx; fixed_t dy; } divline_t; typedef struct { fixed_t frac; /* along trace line */ boolean isaline; union { mobj_t* thing; line_t* line; } d; } intercept_t; typedef boolean (*traverser_t)(intercept_t *in); #define CONSTFUNC fixed_t CONSTFUNC P_AproxDistance (fixed_t dx, fixed_t dy); int CONSTFUNC P_PointOnLineSide (fixed_t x, fixed_t y, const line_t *line); int CONSTFUNC P_PointOnDivlineSide (fixed_t x, fixed_t y, const divline_t *line); int CONSTFUNC P_BoxOnLineSide (const fixed_t *tmbox, const line_t *ld); fixed_t CONSTFUNC P_InterceptVector (const divline_t *v2, const divline_t *v1); void P_MakeDivline (const line_t *li, divline_t *dl); void P_LineOpening (const line_t *linedef); void P_UnsetThingPosition(mobj_t *thing); void P_SetThingPosition(mobj_t *thing); boolean P_BlockLinesIterator (int x, int y, boolean func(line_t *)); boolean P_BlockThingsIterator(int x, int y, boolean func(mobj_t *)); boolean CONSTFUNC ThingIsOnLine(const mobj_t *t, const line_t *l); /* killough 3/15/98 */ boolean P_PathTraverse(fixed_t x1, fixed_t y1, fixed_t x2, fixed_t y2, int flags, boolean trav(intercept_t *)); extern fixed_t opentop; extern fixed_t openbottom; extern fixed_t openrange; extern fixed_t lowfloor; extern divline_t trace; #endif /* __P_MAPUTL__ */