| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832 |
- /*
- ** Command & Conquer Red Alert(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** 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 3 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, see <http://www.gnu.org/licenses/>.
- */
- /****************************************************************************
- *
- * C O N F I D E N T I A L -- W E S T W O O D S T U D I O S
- *
- *----------------------------------------------------------------------------
- *
- * PROJECT
- * VQA player library. (32-Bit protected mode)
- *
- * FILE
- * loader.c
- *
- * DESCRIPTION
- * Stream loading and pre-processing.
- *
- * PROGRAMMER
- * Bill Randolph
- * Denzil E. Long, Jr.
- *
- * DATE
- * August 21, 1995
- *
- *----------------------------------------------------------------------------
- *
- * PUBLIC
- * VQA_Open - Open a VQA file to play.
- * VQA_Close - Close an opened VQA file.
- * VQA_LoadFrame - Load the next video frame from the VQA data stream.
- * VQA_SeekFrame - Position the movie stream to the specified frame.
- *
- * PRIVATE
- * AllocBuffers - Allocates the numerous VQA play buffers
- * FreeBuffers - Frees the VQA play buffers
- * PrimeBuffers - Pre-Load the internal buffers.
- * Load_FINF - Loads the Frame Info Table.
- * Load_VQHD - Loads a VQA Header.
- * Load_CBF0 - Loads a full, uncompressed codebook
- * Load_CBFZ - Loads a full, compressed codebook
- * Load_CBP0 - Loads a partial uncompressed codebook
- * Load_CBPZ - Loads a partial compressed codebook
- * Load_CPL0 - Loads an uncompressed palette
- * Load_CPLZ - Loads a compressed palette
- * Load_VPT0 - Loads uncompressed pointers
- * Load_VPTZ - Loads compressed pointers
- * Load_VQF - Loads a VQ Frame chunk
- * Load_SND0 - Loads an uncompressed sound chunk
- * Load_SND1 - Loads a compressed sound chunk
- * Load_AudFrame - Loads blocks from separate audio file, if needed.
- *
- ****************************************************************************/
- #include <stdio.h>
- #include <fcntl.h>
- #include <io.h>
- #include <malloc.h>
- #include <mem.h>
- #include <dos.h>
- #include "vq.h"
- #include "vqaplayp.h"
- #include <vqm32\all.h>
- /*---------------------------------------------------------------------------
- * PRIVATE DECLARATIONS
- *-------------------------------------------------------------------------*/
- static VQAData *AllocBuffers(VQAHeader *header, VQAConfig *config);
- static void FreeBuffers(VQAData *vqa, VQAConfig *config, VQAHeader *header);
- static long PrimeBuffers(VQAHandle *vqa);
- static long Load_VQF(VQAHandleP *vqap, unsigned long iffsize);
- static long Load_FINF(VQAHandleP *vqap, unsigned long iffsize);
- static long Load_VQHD(VQAHandleP *vqap, unsigned long iffsize);
- static long Load_CBF0(VQAHandleP *vqap, unsigned long iffsize);
- static long Load_CBFZ(VQAHandleP *vqap, unsigned long iffsize);
- static long Load_CBP0(VQAHandleP *vqap, unsigned long iffsize);
- static long Load_CBPZ(VQAHandleP *vqap, unsigned long iffsize);
- static long Load_CPL0(VQAHandleP *vqap, unsigned long iffsize);
- static long Load_CPLZ(VQAHandleP *vqap, unsigned long iffsize);
- static long Load_VPT0(VQAHandleP *vqap, unsigned long iffsize);
- static long Load_VPTZ(VQAHandleP *vqap, unsigned long iffsize);
- #if(VQAAUDIO_ON)
- static long Load_SND0(VQAHandleP *vqap, unsigned long iffsize);
- static long Load_SND1(VQAHandleP *vqap, unsigned long iffsize);
- static long Load_SND2(VQAHandleP *vqap, unsigned long iffsize);
- #if(VQAVOC_ON)
- static void Load_AudFrame(VQAHandleP *vqap);
- #endif /* VQAVOC_ON */
- #endif /* VQAAUDIO_ON */
- /****************************************************************************
- *
- * NAME
- * VQA_Open - Open a VQA file to play.
- *
- * SYNOPSIS
- * Error = VQA_Open(VQA, Name, Config)
- *
- * long VQA_Open(VQAHandle *, char *, VQAConfig *);
- *
- * FUNCTION
- * - Open a VQA file for reading.
- * - Validate that it is an IFF file, of the VQA type.
- * - Read the VQA header.
- * - Open a VOC file for playback, if requested.
- * - Set the Loader's frame rate, if the caller's Config structure's
- * FrameRate is set to -1
- * - Set the Drawer's frame rate, if the caller's Config structure's
- * DrawRate is set to -1
- *
- * INPUTS
- * VQA - Pointer to initialized handle. Obtained by VQA_Alloc().
- * Name - Pointer to name of VQA file to open.
- * Config - Pointer to initialized VQA configuration structure.
- *
- * RESULT
- * Error - 0 if successful, or VQAERR_ error code.
- *
- ****************************************************************************/
- #define OPEN_VQHD (1<<0)
- #define OPEN_FINF (1<<1)
- #define OPEN_CAPTIONS (1<<2)
- #define OPEN_EVA (1<<3)
- long VQA_Open(VQAHandle *vqa, char const *filename, VQAConfig *config)
- {
- VQAHandleP *vqap;
- VQAHeader *header;
- ChunkHeader chunk;
- long max_frm_size;
- long i;
- long done;
- long found;
- char *ptr;
- /* Dereference commonly used data members for quicker access. */
- vqap = (VQAHandleP *)vqa;
- header = &vqap->Header;
- /*-------------------------------------------------------------------------
- * VERIFY VALIDITY OF VQA FILE.
- *-----------------------------------------------------------------------*/
- /* Open the file. */
- if (vqap->IOHandler(vqa, VQACMD_OPEN, (void *)filename, 0)) {
- return (VQAERR_OPEN);
- }
- /* Read the file ID & Size */
- if (vqap->IOHandler(vqa, VQACMD_READ, &chunk, 8)) {
- VQA_Close(vqa);
- return (VQAERR_READ);
- }
- /* Verify an IFF FORM */
- if ((chunk.id != ID_FORM) || (chunk.size == 0)) {
- VQA_Close(vqa);
- return (VQAERR_NOTVQA);
- }
- /* Read in WVQA ID */
- if (vqap->IOHandler(vqa, VQACMD_READ, &chunk, 4)) {
- VQA_Close(vqa);
- return (VQAERR_READ);
- }
- /* Verify VQA */
- if (chunk.id != ID_WVQA) {
- VQA_Close(vqa);
- return (VQAERR_NOTVQA);
- }
- /*-------------------------------------------------------------------------
- * INITIALIZE THE PLAYERS CONFIGURATION
- *-----------------------------------------------------------------------*/
- /* Use the clients configuration if they provided one. */
- if (config != NULL) {
- memcpy(&vqap->Config, config, sizeof(VQAConfig));
- } else {
- VQA_DefaultConfig(&vqap->Config);
- }
- /* Use the internal configuration structure from now on. */
- config = &vqap->Config;
- /*-------------------------------------------------------------------------
- * PROCESS THE PRE-FRAME CHUNKS (VQHD, CAP, FINF, ETC...)
- *-----------------------------------------------------------------------*/
- found = 0;
- done = 0;
- while (!done) {
- if (vqap->IOHandler(vqa, VQACMD_READ, &chunk, 8)) {
- VQA_Close(vqa);
- return (VQAERR_READ);
- }
- chunk.size = REVERSE_LONG(chunk.size);
- switch (chunk.id) {
- /*---------------------------------------------------------------------
- * READ IN THE VQA HEADER.
- *-------------------------------------------------------------------*/
- case ID_VQHD:
- if (chunk.size != sizeof(VQAHeader)) {
- VQA_Close(vqa);
- return (VQAERR_NOTVQA);
- }
- /* Read the header data. */
- if (vqap->IOHandler(vqa, VQACMD_READ, header, PADSIZE(chunk.size))) {
- VQA_Close(vqa);
- return (VQAERR_READ);
- }
- /*-------------------------------------------------------------------
- * SETUP THE CONFIGURATION FROM THE HEADER.
- *-----------------------------------------------------------------*/
- if (config->ImageWidth == -1) {
- config->ImageWidth = header->ImageWidth;
- }
- if (config->ImageHeight == -1) {
- config->ImageHeight = header->ImageHeight;
- }
- /* If Loaders frame rate is -1 then use the value from the header. */
- if (config->FrameRate == -1) {
- config->FrameRate = header->FPS;
- }
- /* If Drawers frame rate is -1 then use the value from the header,
- * which will result in a "variable" frame rate.
- */
- if (config->DrawRate == -1) {
- config->DrawRate = header->FPS;
- }
- /* Finally, if the DrawRate was set to -1 or 0 (ie MaxRate contained
- * bogus values), set it to the header value.
- */
- if ((config->DrawRate == -1) || (config->DrawRate == 0)) {
- config->DrawRate = header->FPS;
- }
- #if(VQAAUDIO_ON)
- /* If an alternate audio track is not available then turn it off.
- * This enables the primary audio track to be played.
- */
- if ((header->Version > VQAHD_VER1)
- && !(header->Flags & VQAHDF_ALTAUDIO)) {
- config->OptionFlags &= ~VQAOPTF_ALTAUDIO;
- }
- #endif
- /*-------------------------------------------------------------------
- * ALLOCATE THE BUFFERS THAT WE NEED TO PLAY THE VQA.
- *-----------------------------------------------------------------*/
- if ((vqap->VQABuf = AllocBuffers(header, config)) == NULL) {
- VQA_Close(vqa);
- return (VQAERR_NOMEM);
- }
- found |= OPEN_VQHD;
- break;
- /*---------------------------------------------------------------------
- * READ IN AND OPEN THE CAPTIONS STREAM.
- *-------------------------------------------------------------------*/
- case ID_CAP0:
- if ((config->CapFont != NULL)
- && (config->OptionFlags & VQAOPTF_CAPTIONS)) {
- short size = 0;
- /* Get uncompressed size of captions. */
- if (vqap->IOHandler(vqa, VQACMD_READ, &size, sizeof(short))) {
- VQA_Close(vqa);
- return (VQAERR_READ);
- }
- /* Allocate buffer for captions. */
- i = size + 50;
- if ((ptr = (char *)malloc(i)) == NULL) {
- VQA_Close(vqa);
- return (VQAERR_NOMEM);
- }
- /* Read in the captions chunk. */
- i -= PADSIZE(chunk.size);
- if (vqap->IOHandler(vqa, VQACMD_READ, (ptr + i),
- PADSIZE(chunk.size - sizeof(short)))) {
- free(ptr);
- VQA_Close(vqa);
- return (VQAERR_READ);
- }
- /* Decompress the captions. */
- LCW_Uncompress((ptr + i), ptr, size);
- vqap->Caption = OpenCaptions(ptr, config->CapFont);
- if (vqap->Caption == NULL) {
- VQA_Close(vqa);
- return (VQAERR_NOMEM);
- }
- found |= OPEN_CAPTIONS;
- } else {
- if (vqap->IOHandler(vqa, VQACMD_SEEK, (void *)SEEK_CUR,
- PADSIZE(chunk.size))) {
- VQA_Close(vqa);
- return (VQAERR_SEEK);
- }
- }
- break;
- case ID_EVA0:
- if ((config->EVAFont != NULL)
- && (config->OptionFlags & VQAOPTF_EVA)) {
- short size = 0;
- /* Get uncompressed size of captions. */
- if (vqap->IOHandler(vqa, VQACMD_READ, &size, sizeof(short))) {
- VQA_Close(vqa);
- return (VQAERR_READ);
- }
- /* Allocate buffer for captions. */
- i = size + 50;
- if ((ptr = (char *)malloc(i)) == NULL) {
- VQA_Close(vqa);
- return (VQAERR_NOMEM);
- }
- /* Read in the captions chunk. */
- i -= PADSIZE(chunk.size);
- if (vqap->IOHandler(vqa, VQACMD_READ, (ptr + i),
- PADSIZE(chunk.size - sizeof(short)))) {
- free (ptr);
- VQA_Close(vqa);
- return (VQAERR_READ);
- }
- /* Decompress the captions. */
- LCW_Uncompress((ptr + i), ptr, size);
- vqap->EVA = OpenCaptions(ptr, config->EVAFont);
- if (vqap->EVA == NULL) {
- VQA_Close(vqa);
- return (VQAERR_NOMEM);
- }
- found |= OPEN_EVA;
- } else {
- if (vqap->IOHandler(vqa, VQACMD_SEEK, (void *)SEEK_CUR,
- PADSIZE(chunk.size))) {
- VQA_Close(vqa);
- return (VQAERR_SEEK);
- }
- }
- break;
- /*---------------------------------------------------------------------
- * READ FRAME INFORMATION
- *-------------------------------------------------------------------*/
- case ID_FINF:
- if (Load_FINF(vqap, chunk.size)) {
- VQA_Close(vqa);
- return (VQAERR_READ);
- }
- done = 1;
- break;
- default:
- if (vqap->IOHandler(vqa, VQACMD_SEEK, (void *)SEEK_CUR,
- PADSIZE(chunk.size))) {
- VQA_Close(vqa);
- return (VQAERR_SEEK);
- }
- break;
- }
- }
- /*-------------------------------------------------------------------------
- * INITIALIZE THE VIDEO SYSTEM IF WE ARE REQUIRED TO HANDLE THAT.
- *-----------------------------------------------------------------------*/
- #if(VQAVIDEO_ON)
- if ((vqap->VQABuf->Drawer.Display = SetVideoMode(config->Vmode)) == 0) {
- VQA_Close(vqa);
- return (VQAERR_VIDEO);
- }
- /* Set the VBIBit polarity. */
- vqap->VQABuf->VBIBit = GetVBIBit();
- #else
- if (config->VBIBit == -1) {
- config->VBIBit = TestVBIBit();
- }
- vqap->VQABuf->VBIBit = config->VBIBit;
- if (found & (OPEN_CAPTIONS|OPEN_EVA)) {
- SetDAC(251,255,255,255); /* White */
- SetDAC(252,255,000,000); /* Red */
- SetDAC(253,000,255,000); /* Green */
- SetDAC(254,255,255,255);
- SetDAC(255,255,000,255); /* Cycle */
- }
- #endif /* VQAVIDEO_ON */
- /*-------------------------------------------------------------------------
- * AUDIO TRACK OVERRIDE FROM EXTERNAL FILE (.VOC)
- *-----------------------------------------------------------------------*/
- /* Open VOC file if one is requested. */
- #if(VQAVOC_ON && VQAAUDIO_ON)
- if (config->VocFile != NULL) {
- vqap->vocfh = open(config->VocFile, (O_RDONLY|O_BINARY));
- } else {
- vqap->vocfh = -1;
- }
- /* Make sure we won't try to play audio. */
- if ((vqap->vocfh == -1) && ((header->Flags & VQAHDF_AUDIO) == 0)) {
- config->OptionFlags &= (~VQAOPTF_AUDIO);
- }
- #else /* VQAVOC_ON */
- /* If the movie does not contain an audio track make sure we won't try
- * to play one.
- */
- if (((header->Flags & VQAHDF_AUDIO) == 0)) {
- config->OptionFlags &= (~VQAOPTF_AUDIO);
- }
- #endif /* VQAVOC_ON */
- /*-------------------------------------------------------------------------
- * INITIALIZE THE AUDIO PLAYBACK/TIMING SYSTEM.
- *-----------------------------------------------------------------------*/
- #if(VQAAUDIO_ON)
- if (config->OptionFlags & VQAOPTF_AUDIO) {
- VQAAudio *audio;
- /* Dereference for quick access. */
- audio = &vqap->VQABuf->Audio;
- /* Open HMI audio resource for playback. */
- if (VQA_OpenAudio(vqap)) {
- VQA_Close(vqa);
- return (VQAERR_AUDIO);
- }
- /* Initialize ADPCM information structure for audio stream. */
- sosCODECInitStream(&audio->ADPCM_Info);
- if (header->Version == VQAHD_VER1) {
- audio->ADPCM_Info.wBitSize = 8;
- audio->ADPCM_Info.dwUnCompSize = (22050L/header->FPS) * header->Frames;
- audio->ADPCM_Info.wChannels = 1;
- } else {
- audio->ADPCM_Info.wBitSize = audio->BitsPerSample;
- audio->ADPCM_Info.dwUnCompSize = (((audio->SampleRate / header->FPS)
- * (audio->BitsPerSample >> 3)) * audio->Channels) * header->Frames;
- audio->ADPCM_Info.wChannels = audio->Channels;
- }
- audio->ADPCM_Info.dwCompSize = audio->ADPCM_Info.dwUnCompSize
- / (audio->ADPCM_Info.wBitSize / 4);
- }
- /* Turn off audio if the HMI DigiHandle is invalid. */
- if (config->DigiHandle == -1) {
- config->OptionFlags &= ~VQAOPTF_AUDIO;
- }
- /* Setup the timer interrupt if the client requests it for the timing
- * source.
- */
- if (!(config->OptionFlags & VQAOPTF_AUDIO)
- || (config->TimerMethod == VQA_TMETHOD_INT)) {
- /* Start HMI timer system for timing. */
- if (VQA_StartTimerInt(vqap, (config->OptionFlags & VQAOPTF_HMIINIT))) {
- VQA_Close(vqa);
- return (VQAERR_AUDIO);
- }
- }
- #endif /* VQAAUDIO_ON */
- /*-------------------------------------------------------------------------
- * PRIME THE BUFFERS BY PRE-LOADING THEM WITH FRAME DATA.
- *-----------------------------------------------------------------------*/
- if (PrimeBuffers(vqa) != 0) {
- VQA_Close(vqa);
- return (VQAERR_READ);
- }
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * VQA_Close - Close an opened VQA file.
- *
- * SYNOPSIS
- * VQA_Close(VQA)
- *
- * void VQA_Close(VQAHandle *);
- *
- * FUNCTION
- * Close the file that was opened with VQA_Open().
- *
- * INPUTS
- * VQA - Pointer VQAHandle to close.
- *
- * RESULT
- * NONE
- *
- ****************************************************************************/
- void VQA_Close(VQAHandle *vqa)
- {
- long (*iohandler)(VQAHandle *, long, void *, long);
- /* Restore video mode to text. */
- #if(VQAVIDEO_ON)
- SetVideoMode(TEXT);
- #endif /* VQAVIDEO_ON */
- /* Shutdown audio/timing system. */
- #if(VQAAUDIO_ON)
- if (((VQAHandleP *)vqa)->Config.OptionFlags & VQAOPTF_AUDIO) {
- VQA_CloseAudio((VQAHandleP *)vqa);
- } else {
- VQA_StopTimerInt((VQAHandleP *)vqa);
- }
- #endif /* VQAAUDIO_ON */
- /* Free captions. */
- if (((VQAHandleP *)vqa)->Caption != NULL) {
- if (((VQAHandleP *)vqa)->Caption->Buffer != NULL) {
- free(((VQAHandleP *)vqa)->Caption->Buffer);
- }
- CloseCaptions(((VQAHandleP *)vqa)->Caption);
- }
- /* Free EVA. */
- if (((VQAHandleP *)vqa)->EVA != NULL) {
- if (((VQAHandleP *)vqa)->EVA->Buffer != NULL) {
- free(((VQAHandleP *)vqa)->EVA->Buffer);
- }
- CloseCaptions(((VQAHandleP *)vqa)->EVA);
- }
- /* Free memory */
- if (((VQAHandleP *)vqa)->VQABuf != NULL) {
- FreeBuffers(((VQAHandleP *)vqa)->VQABuf, &((VQAHandleP *)vqa)->Config,
- &((VQAHandleP *)vqa)->Header);
- }
- /* Close the VOC override file if one was opened */
- #if(VQAVOC_ON && VQAAUDIO_ON)
- if (((VQAHandleP *)vqa)->vocfh != -1) {
- close(((VQAHandleP *)vqa)->vocfh);
- }
- #endif /* VQAVOC_ON */
- /* Close the VQA file */
- ((VQAHandleP *)vqa)->IOHandler(vqa, VQACMD_CLOSE, NULL, 0);
- /* Reset the VQAHandle */
- iohandler = ((VQAHandleP *)vqa)->IOHandler;
- memset(vqa, 0, sizeof(VQAHandleP));
- ((VQAHandleP *)vqa)->IOHandler = iohandler;
- }
- /****************************************************************************
- *
- * NAME
- * VQA_LoadFrame - Load the next video frame from the VQA data stream.
- *
- * SYNOPSIS
- * Error = VQA_LoadFrame(VQA)
- *
- * long VQA_LoadFrame(VQAHandle *);
- *
- * FUNCTION
- * The codebook is split up such that the last frame of every group gets
- * a new, complete codebook, ready for the next group. The first codebook
- * in the VQA is a full codebook, and goes with the first frame's data.
- * Partial codebooks are stored per frame after that, and they add up to
- * a full codebook just before the first frame for the next group is read.
- *
- * (Currently, this routine can read either the older non-frame-grouped
- * VQA file format, or the new frame-chunk format. For the older format,
- * it's assumed that the last chunk in a frame is the pointer data.)
- *
- * This routine also does a sort of "cooperative multitasking". If the
- * Loader hits a "wait state" where it has to wait on the audio to finish
- * playing before it can continue to load, it sets a "sleep" flag and
- * just returns. The sleep flag is checked on entry to see if it needs
- * to jump to the proper execution point. This may improve performance on
- * some platforms, but it also allows the Loader to be called regardless
- * of the size of the buffers; if the buffers fill up or the audio fails
- * to play, the Loader won't just get stuck.
- *
- * INPUTS
- * VQA - Pointer to VQAHandle structure.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- long VQA_LoadFrame(VQAHandle *vqa)
- {
- VQAHandleP *vqap;
- VQAData *vqabuf;
- VQALoader *loader;
- VQAFrameNode *curframe;
- ChunkHeader *chunk;
- unsigned long iffsize;
- long frame_loaded = 0;
- /* Dereference commonly used data members for quicker access. */
- vqap = (VQAHandleP *)vqa;
- vqabuf = vqap->VQABuf;
- loader = &vqabuf->Loader;
- curframe = loader->CurFrame;
- chunk = &loader->CurChunkHdr;
- /* We have reached the end of the file if we loaded all the frames. */
- if (loader->CurFrameNum >= vqap->Header.Frames) {
- return (VQAERR_EOF);
- }
- /* If we're reading audio from a VOC file then service that requirement. */
- #if(VQAVOC_ON && VQAAUDIO_ON)
- if (vqap->vocfh != -1) {
- Load_AudFrame(vqap);
- }
- #endif /* VQAAUDIO_ON & VQAVOC_ON */
- /* If no buffer is available for loading then return. This allows the
- * drawer to service one of the buffers more readily. (We'll wait for one
- * to free up).
- */
- if (curframe->Flags & VQAFRMF_LOADED) {
- loader->WaitsOnDrawer++;
- return (VQAERR_NOBUFFER);
- }
- /* If we're not sleeping, initialize */
- if (!(vqabuf->Flags & VQADATF_LSLEEP)) {
- frame_loaded = 0;
- loader->FrameSize = 0;
- /* Initialize the codebook ptr for the frame we're about to load:
- * (This frame's codebook is the last full codebook; we have to init it
- * now, because if we're on the last frame in a group, we'll get a new
- * FullCB pointer.)
- */
- curframe->Codebook = loader->FullCB;
- }
- /*-------------------------------------------------------------------------
- * THE MAIN LOADER LOOP
- *-----------------------------------------------------------------------*/
- while (frame_loaded == 0) {
- /* Read new chunk, only if we're not sleeping */
- if (!(vqabuf->Flags & VQADATF_LSLEEP)) {
- /* Read chunk ID */
- if (vqap->IOHandler(vqa, VQACMD_READ, chunk, 8)) {
- return (VQAERR_EOF);
- }
- iffsize = REVERSE_LONG(chunk->size);
- loader->FrameSize += iffsize;
- }
- /* Handle each chunk type */
- switch (chunk->id) {
- /* VQ Normal Frame */
- case ID_VQFR:
- if (Load_VQF(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- frame_loaded = 1;
- break;
- /* VQ Key Frame */
- case ID_VQFK:
- if (Load_VQF(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- /* Flag this frame as being key. */
- curframe->Flags |= VQAFRMF_KEY;
- frame_loaded = 1;
- break;
- /* Full uncompressed codebook */
- case ID_CBF0:
- if (Load_CBF0(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- break;
- /* Full compressed codebook */
- case ID_CBFZ:
- if (Load_CBFZ(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- break;
- /* Partial uncompressed codebook */
- case ID_CBP0:
- if (Load_CBP0(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- break;
- /* Partial compressed codebook */
- case ID_CBPZ:
- if (Load_CBPZ(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- break;
- /* Uncompressed palette */
- case ID_CPL0:
- if (Load_CPL0(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- /* Flag this frame as having a palette. */
- curframe->Flags |= VQAFRMF_PALETTE;
- break;
- /* Compressed palette */
- case ID_CPLZ:
- if (Load_CPLZ(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- /* Flag this frame as having a palette. */
- curframe->Flags |= VQAFRMF_PALETTE;
- break;
- /* Uncompressed pointer data */
- case ID_VPT0:
- if (Load_VPT0(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- frame_loaded = 1;
- break;
- /* Compressed pointer data */
- case ID_VPTZ:
- case ID_VPTD:
- if (Load_VPTZ(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- frame_loaded = 1;
- break;
- /* Pointer data Key (Must draw) */
- case ID_VPTK:
- if (Load_VPTZ(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- /* Flag this frame as being key. */
- curframe->Flags |= VQAFRMF_KEY;
- frame_loaded = 1;
- break;
- /* Uncompressed audio frame.
- *
- * - Make sure the sound load buffer (Audio.TempBuf) is empty; if not
- * go into a sleep state.
- * - Load the data into TempBuf.
- */
- #if(VQAAUDIO_ON)
- case ID_SND0:
- if (!(vqap->Config.OptionFlags & VQAOPTF_ALTAUDIO)) {
- /* Move the last audio frame to the play buffer. */
- if (CopyAudio(vqap) == VQAERR_SLEEPING) {
- vqabuf->Flags |= VQADATF_LSLEEP;
- return (VQAERR_SLEEPING);
- } else {
- vqabuf->Flags &= (~VQADATF_LSLEEP);
- }
- /* Load an uncompressed audio frame. */
- if (Load_SND0(vqap, iffsize) != 0) {
- return (VQAERR_READ);
- }
- } else {
- if (vqap->IOHandler(vqa, VQACMD_SEEK, (void *)SEEK_CUR,
- PADSIZE(iffsize))) {
- return (VQAERR_SEEK);
- }
- }
- break;
- case ID_SNA0:
- if (vqap->Config.OptionFlags & VQAOPTF_ALTAUDIO) {
-
- /* Move the last audio frame to the play buffer. */
- if (CopyAudio(vqap) == VQAERR_SLEEPING) {
- vqabuf->Flags |= VQADATF_LSLEEP;
- return (VQAERR_SLEEPING);
- } else {
- vqabuf->Flags &= (~VQADATF_LSLEEP);
- }
- /* Load an uncompressed audio frame. */
- if (Load_SND0(vqap, iffsize) != 0) {
- return (VQAERR_READ);
- }
- } else {
- if (vqap->IOHandler(vqa, VQACMD_SEEK, (void *)SEEK_CUR,
- PADSIZE(iffsize))) {
- return (VQAERR_SEEK);
- }
- }
- break;
- /* Compressed audio frame.
- *
- * - Make sure the sound load buffer (Audio.TempBuf) is empty; if not
- * go into a sleep state.
- * - Load the data into TempBuf.
- */
- case ID_SND1:
- if (!(vqap->Config.OptionFlags & VQAOPTF_ALTAUDIO)) {
- /* Move the last audio frame to the play buffer. */
- if (CopyAudio(vqap) == VQAERR_SLEEPING) {
- vqabuf->Flags |= VQADATF_LSLEEP;
- return (VQAERR_SLEEPING);
- } else {
- vqabuf->Flags &= (~VQADATF_LSLEEP);
- }
- /* Load a compressed audio frame. */
- if (Load_SND1(vqap, iffsize) != 0) {
- return (VQAERR_READ);
- }
- } else {
- if (vqap->IOHandler(vqa, VQACMD_SEEK, (void *)SEEK_CUR,
- PADSIZE(iffsize))) {
- return (VQAERR_SEEK);
- }
- }
- break;
- case ID_SNA1:
- if (vqap->Config.OptionFlags & VQAOPTF_ALTAUDIO) {
- /* Move the last audio frame to the play buffer. */
- if (CopyAudio(vqap) == VQAERR_SLEEPING) {
- vqabuf->Flags |= VQADATF_LSLEEP;
- return (VQAERR_SLEEPING);
- } else {
- vqabuf->Flags &= (~VQADATF_LSLEEP);
- }
- /* Load a compressed audio frame. */
- if (Load_SND1(vqap, iffsize) != 0) {
- return (VQAERR_READ);
- }
- } else {
- if (vqap->IOHandler(vqa, VQACMD_SEEK, (void *)SEEK_CUR,
- PADSIZE(iffsize))) {
- return (VQAERR_SEEK);
- }
- }
- break;
- /* HMI ADPCM compressed audio frame.
- *
- * - Make sure the sound load buffer (Audio.TempBuf) is empty; if not
- * go into a sleep state.
- * - Load the data into TempBuf.
- */
- case ID_SND2:
- if (!(vqap->Config.OptionFlags & VQAOPTF_ALTAUDIO)) {
- /* Move the last audio frame to the play buffer. */
- if (CopyAudio(vqap) == VQAERR_SLEEPING) {
- vqabuf->Flags |= VQADATF_LSLEEP;
- return (VQAERR_SLEEPING);
- } else {
- vqabuf->Flags &= (~VQADATF_LSLEEP);
- }
- /* Load a compressed audio frame. */
- if (Load_SND2(vqap, iffsize) != 0) {
- return (VQAERR_READ);
- }
- } else {
- if (vqap->IOHandler(vqa, VQACMD_SEEK, (void *)SEEK_CUR,
- PADSIZE(iffsize))) {
- return (VQAERR_SEEK);
- }
- }
- break;
- case ID_SNA2:
- if (vqap->Config.OptionFlags & VQAOPTF_ALTAUDIO) {
- /* Move the last audio frame to the play buffer. */
- if (CopyAudio(vqap) == VQAERR_SLEEPING) {
- vqabuf->Flags |= VQADATF_LSLEEP;
- return (VQAERR_SLEEPING);
- } else {
- vqabuf->Flags &= (~VQADATF_LSLEEP);
- }
- /* Load a compressed audio frame. */
- if (Load_SND2(vqap, iffsize) != 0) {
- return (VQAERR_READ);
- }
- } else {
- if (vqap->IOHandler(vqa, VQACMD_SEEK, (void *)SEEK_CUR,
- PADSIZE(iffsize))) {
- return (VQAERR_SEEK);
- }
- }
- break;
- #endif
- /* Skip any unknown chunks. */
- default:
- if (vqap->IOHandler(vqa, VQACMD_SEEK, (void *)SEEK_CUR,
- PADSIZE(iffsize))) {
- return (VQAERR_SEEK);
- }
- break;
- }
- }
- /* Update maximum frame size stat. */
- if ((loader->CurFrameNum>0) && (loader->FrameSize>loader->MaxFrameSize)) {
- loader->MaxFrameSize = loader->FrameSize;
- }
- /*-------------------------------------------------------------------------
- * SET UP THE FRAME FOR DRAWING.
- *-----------------------------------------------------------------------*/
- /* Set the frame # */
- curframe->FrameNum = loader->CurFrameNum;
- loader->CurFrameNum++;
- /* Update data for mono output */
- loader->LastFrameNum = loader->CurFrameNum;
- /* Loader is finished with this frame; tell Drawer to draw it */
- curframe->Flags |= VQAFRMF_LOADED;
- loader->CurFrame = curframe->Next;
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * VQA_SeekFrame - Position the movie stream to the specified frame.
- *
- * SYNOPSIS
- * Frame = VQA_SeekFrame(VQA, Frame, FromWhere)
- *
- * long VQA_SeekFrame(VQAHandle *, long, long);
- *
- * FUNCTION
- * This function sets the movie stream to the new frame specified by
- * the 'offset' parameter. 'FromWhere' is a symbolic constant that is used
- * to specify from where in the stream offset should be applied.
- *
- * INPUTS
- * VQA - Pointer to VQAHandle of movie to seek into.
- * Frame - Frame to seek to.
- * FromWhere - Relative position indicator.
- *
- * RESULT
- * Frame - New frame position or -1 if error.
- *
- ****************************************************************************/
- long VQA_SeekFrame(VQAHandle *vqa, long framenum, long fromwhere)
- {
- VQAHandleP *vqap;
- VQAData *vqabuf;
- VQALoader *loader;
- VQAHeader *header;
- VQAFrameNode *frame;
- long group;
- long i;
- long rc = VQAERR_SLEEPING;
- #if(VQAAUDIO_ON)
- VQAConfig *config;
- VQAAudio *audio;
- long audio_on;
- #endif
- /* Dereference commonly used data members for quick access. */
- vqap = (VQAHandleP *)vqa;
- vqabuf = vqap->VQABuf;
- loader = &vqabuf->Loader;
- header = &vqap->Header;
- #if(VQAAUDIO_ON)
- config = &vqap->Config;
- #endif
- fromwhere = fromwhere;
-
- #if(VQAAUDIO_ON)
- audio = &vqabuf->Audio;
- /* Stop audio playback. */
- audio_on = (audio->Flags & VQAAUDF_ISPLAYING);
- VQA_StopAudio(vqap);
- #endif
- /* Make sure the requested frame is valid and the frame information
- * array is allocated before continuing.
- */
- if ((framenum < header->Frames) && (vqabuf->Foff != NULL)) {
-
- /* Get the current frame. */
- frame = loader->CurFrame;
- /* Find and load the most recent palette. */
- for (i = framenum; i >= 0; i--) {
- if (vqabuf->Foff[i] & VQAFINF_PAL) {
- /* Seek to the palette frame. */
- rc = vqap->IOHandler(vqa, VQACMD_SEEK, (void *)SEEK_SET,
- VQAFRAME_OFFSET(vqabuf->Foff[i]));
- if (!rc) {
- /* Fool the loader into thinking this frame is empty. */
- loader->NumPartialCB = 0;
- loader->PartialCBSize = 0;
- loader->FullCB = vqabuf->CBData;
- loader->CurCB = vqabuf->CBData;
- loader->CurFrameNum = 0;
- frame->Flags = 0;
- /* Load the frame with the palette. */
- if (VQA_LoadFrame(vqa) == 0) {
- /* Decompress the palette if neccessary.*/
- if (frame->Flags & VQAFRMF_PALCOMP) {
- frame->PaletteSize = LCW_Uncompress((char *)frame->Palette
- + frame->PalOffset, (char *)frame->Palette,
- vqabuf->Max_Pal_Size);
- }
- SetPalette(frame->Palette, frame->PaletteSize, 0);
- }
- } else {
- rc = VQAERR_SEEK;
- }
- break;
- }
- }
- if (!rc) {
- /* Compute the starting group frame of the requested frame. */
- group = (framenum / header->Groupsize);
- group = (group * header->Groupsize);
- /* The codebook for the group we want to goto is found in the previous
- * group, with the exception of the very first group.
- */
- if (group >= header->Groupsize) {
- group -= header->Groupsize;
- }
- /* Seek to the start of the group containing the partial codebooks for
- * the target frame.
- */
- if (!vqap->IOHandler(vqa, VQACMD_SEEK, (void *)SEEK_SET,
- VQAFRAME_OFFSET(vqabuf->Foff[group]))) {
- /* Throw away any audio frames that were loaded. */
- #if(VQAAUDIO_ON)
- if ((config->OptionFlags & VQAOPTF_AUDIO)
- && (audio->Buffer != NULL)) {
- memset(audio->IsLoaded, 0, audio->NumAudBlocks * sizeof(short));
- memset(audio->Buffer, 0, config->AudioBufSize);
- /* Position the audio buffer to 1/2 second. */
- audio->AudBufPos = (long)(((audio->SampleRate * audio->Channels)
- * (audio->BitsPerSample >> 3)) / 2);
- /* Mark 1/2 second of the audio buffer as loaded. */
- for (i = 0; i < (audio->AudBufPos / config->HMIBufSize); i++) {
- audio->IsLoaded[i] = 1;
- }
- }
- #endif
- /* Force the loader to the desired frame. */
- loader->NumPartialCB = 0;
- loader->PartialCBSize = 0;
- loader->FullCB = vqabuf->CBData;
- loader->CurCB = vqabuf->CBData;
- loader->CurFrameNum = group;
- /* Load frames up to the target frame collecting partial codebooks
- * along the way.
- */
- for (i = 0; i < (framenum - group); i++) {
-
- /* Fool the loader into thinking the frame has been drawn. */
- loader->CurFrame->Flags = 0;
- #if(VQAAUDIO_ON)
- audio->TempBufLen = 0;
- #endif
- /* Load the frame. */
- if ((rc = VQA_LoadFrame(vqa)) != 0) {
- if ((rc != VQAERR_NOBUFFER) && (rc != VQAERR_SLEEPING)) {
- break;
- } else {
- rc = 0;
- }
- }
- }
- /* If everything is okay, then re-prime the buffers. */
- if (!rc) {
- /* Mark all the frames except the current one as empty. */
- loader->CurFrame->Flags = 0;
- frame = loader->CurFrame->Next;
- while (frame != loader->CurFrame) {
- frame->Flags = 0;
- frame = frame->Next;
- }
- /* Set the drawer to the current frame and the loader
- * to the next.
- */
- vqabuf->Drawer.CurFrame = loader->CurFrame;
- /* Prime the buffers for the new position. */
- rc = PrimeBuffers(vqa);
- /* An end of file is not considered and error. */
- if ((rc == 0) || (rc == VQAERR_EOF)) {
- rc = framenum;
- }
- }
- } else {
- rc = VQAERR_SEEK;
- }
- }
- }
- /* Restart audio playback. */
- #if(VQAAUDIO_ON)
- if (audio_on) {
- VQA_StartAudio(vqap);
- }
- #endif
- return (rc);
- }
- /****************************************************************************
- *
- * NAME
- * AllocBuffers - Allocate VQA play buffers.
- *
- * SYNOPSIS
- * VQAData = AllocBuffers(Header, Config)
- *
- * VQAData *AllocBuffers(VQAHeader *, VQAConfig *);
- *
- * FUNCTION
- * For those structures that contain buffer pointers (codebook nodes,
- * frame buffer nodes), enough memory is allocated for both the structure
- * and its associated buffers, then the buffer pointers are pointed to
- * the appropriate offset from the structure pointer. This allows us
- * to perform only one malloc & free for each node.
- *
- * Buffers allocated:
- * - vqa
- * - vqa->CBData (list)
- * - vqa->FrameData (list)
- * - vqa->Drawer.ImageBuf
- * - vqa->Audio.Buffer
- * - vqa->Audio.IsLoaded
- * - vqa->Foff
- *
- * INPUTS
- * Header - Pointer to VQAHeader structure.
- * Config - Pointer to VQA configuration structure.
- *
- * RESULT
- * VQAData - Pointer to initialized VQAData structure.
- *
- ****************************************************************************/
- static VQAData *AllocBuffers(VQAHeader *header, VQAConfig *config)
- {
- VQAData *vqa;
- VQACBNode *cbnode;
- VQACBNode *this_cb;
- VQAFrameNode *framenode;
- VQAFrameNode *this_frame;
- long i;
- /* Check the configuration for valid values. */
- if ((config->NumCBBufs == 0) || (config->NumFrameBufs == 0)) {
- return (NULL);
- }
- /* Allocate the master structure */
- if ((vqa = (VQAData *)malloc(sizeof(VQAData))) == NULL) {
- return (NULL);
- }
- /*-------------------------------------------------------------------------
- * INITIALIZE THE VQA DATA STRUCTURES.
- *
- * Pointers are set to NULL initially, and filled in as the buffers are
- * allocated. The Max buffer sizes are computed with 1K of padding,
- * and'd with 0xFFFC to make the size divisible by 4, to ensure DWORD
- * alignment.
- *-----------------------------------------------------------------------*/
- DPMI_Lock(vqa, sizeof(VQAData));
- memset(vqa, 0, sizeof(VQAData));
- vqa->MemUsed = sizeof(VQAData);
- vqa->Drawer.LastTime = (-VQA_TIMETICKS);
- /* Set maximum codebook size. */
- vqa->Max_CB_Size = ((header->CBentries) * header->BlockWidth
- * header->BlockHeight + 250) & 0xFFFC;
- /* Set maximum palette size. */
- vqa->Max_Pal_Size = (768 + 1024) & 0xFFFC;
- /* Set maximum vector pointers size. */
- vqa->Max_Ptr_Size = ((header->ImageWidth / header->BlockWidth)
- * (header->ImageHeight / header->BlockHeight)
- * sizeof(short) + 1024) & 0xFFFC;
- /* Set the frame number of the frame containing the last codebook. */
- vqa->Loader.LastCBFrame = (((header->Frames - 1) / header->Groupsize)
- * header->Groupsize);
- /*-------------------------------------------------------------------------
- * ALLOCATE THE CODEBOOK BUFFERS.
- *-----------------------------------------------------------------------*/
- for (i = 0; i < config->NumCBBufs; i++) {
- /* Allocate a codebook node. */
- cbnode = (VQACBNode *)malloc((sizeof(VQACBNode) + vqa->Max_CB_Size));
- /* If failure then clean up and exit. */
- if (cbnode == NULL) {
- FreeBuffers(vqa, config, header);
- return (NULL);
- }
- /* Lock the buffer to prevent page swapping. */
- DPMI_Lock(cbnode, (sizeof(VQACBNode) + vqa->Max_CB_Size));
- /* Keep count of the memory usage. */
- vqa->MemUsed += (long)(sizeof(VQACBNode) + vqa->Max_CB_Size);
- /* Initialize the node */
- memset(cbnode, 0, sizeof(VQACBNode));
- cbnode->Buffer = (unsigned char *)cbnode + sizeof(VQACBNode);
- /* Install the node */
- if (i == 0) {
- vqa->CBData = cbnode;
- this_cb = cbnode;
- } else {
- this_cb->Next = cbnode;
- this_cb = cbnode;
- }
- }
- /* Make the list circular */
- cbnode->Next = vqa->CBData;
- /* Install the Codebook list */
- vqa->Loader.CurCB = vqa->CBData;
- vqa->Loader.FullCB = vqa->CBData;
- /*-------------------------------------------------------------------------
- * ALLOCATE THE FRAME BUFFERS.
- *-----------------------------------------------------------------------*/
- for (i = 0; i < config->NumFrameBufs; i++) {
-
- /* Allocate a pointer node */
- framenode = (VQAFrameNode *)malloc((sizeof(VQAFrameNode)
- + vqa->Max_Ptr_Size + vqa->Max_Pal_Size));
- /* If failure then clean up and exit. */
- if (framenode == NULL) {
- FreeBuffers(vqa, config, header);
- return (NULL);
- }
- /* Lock the buffer to prevent page swapping. */
- DPMI_Lock(framenode, sizeof(VQAFrameNode) + vqa->Max_Ptr_Size
- + vqa->Max_Pal_Size);
- /* Keep count of the memory usage. */
- vqa->MemUsed += (long)(sizeof(VQAFrameNode) + vqa->Max_Ptr_Size
- + vqa->Max_Pal_Size);
- /* Initialize the node */
- memset(framenode, 0, sizeof(VQAFrameNode));
- framenode->Pointers = (unsigned char *)framenode + sizeof(VQAFrameNode);
- framenode->Palette = (unsigned char *)framenode + sizeof(VQAFrameNode)
- + vqa->Max_Ptr_Size;
- framenode->Codebook = vqa->CBData;
- /* Install the node */
- if (i == 0) {
- vqa->FrameData = framenode;
- this_frame = framenode;
- } else {
- this_frame->Next = framenode;
- this_frame = framenode;
- }
- }
- /* Make the list circular */
- framenode->Next = vqa->FrameData;
- /* Install the Frame Buffer list */
- vqa->Loader.CurFrame = vqa->FrameData;
- vqa->Drawer.CurFrame = vqa->FrameData;
- vqa->Flipper.CurFrame = vqa->FrameData;
- /*-------------------------------------------------------------------------
- * ALLOCATE THE IMAGE BUFFERS IF ONE IS NOT ALREADY PROVIDED.
- *-----------------------------------------------------------------------*/
- if (config->ImageBuf == NULL) {
- /* Allocate our own buffer. */
- if (config->DrawFlags & VQACFGF_BUFFER) {
- vqa->Drawer.ImageBuf = (unsigned char *)malloc((header->ImageWidth
- * header->ImageHeight));
- /* If the allocation failed we must free up and exit. */
- if (vqa->Drawer.ImageBuf == NULL) {
- FreeBuffers(vqa, config, header);
- return (NULL);
- }
- /* Lock to prevent page swapping. */
- DPMI_Lock(vqa->Drawer.ImageBuf,header->ImageWidth*header->ImageHeight);
- /* Plugin image buffer information. */
- vqa->Drawer.ImageWidth = header->ImageWidth;
- vqa->Drawer.ImageHeight = header->ImageHeight;
- vqa->MemUsed += (long)(header->ImageWidth * header->ImageHeight);
- } else {
- vqa->Drawer.ImageWidth = config->ImageWidth;
- vqa->Drawer.ImageHeight = config->ImageHeight;
- }
- } else {
- /* Use caller provided buffer */
- vqa->Drawer.ImageBuf = config->ImageBuf;
- vqa->Drawer.ImageWidth = config->ImageWidth;
- vqa->Drawer.ImageHeight = config->ImageHeight;
- }
- /*-------------------------------------------------------------------------
- * ALLOCATE AND INITIALIZE AUDIO BUFFERS AND STRUCTURES.
- *-----------------------------------------------------------------------*/
- #if(VQAAUDIO_ON)
- if ((header->Flags & VQAHDF_AUDIO)
- && (config->OptionFlags & VQAOPTF_AUDIO)) {
- /* Dereference audio structure for quick access. */
- VQAAudio *audio = &vqa->Audio;
- /* Version 1 VQA's only supported 22050 8 bit mono audio. */
- if (header->Version < VQAHD_VER2) {
- audio->SampleRate = 22050U;
- audio->Channels = 1;
- audio->BitsPerSample = 8;
- audio->BytesPerSec = 22050;
- } else {
- if ((config->OptionFlags & VQAOPTF_ALTAUDIO)
- && (header->Flags & VQAHDF_ALTAUDIO)) {
- audio->SampleRate = header->AltSampleRate;
- audio->Channels = header->AltChannels;
- audio->BitsPerSample = header->AltBitsPerSample;
- } else {
- audio->SampleRate = header->SampleRate;
- audio->Channels = header->Channels;
- audio->BitsPerSample = header->BitsPerSample;
- }
- audio->BytesPerSec = ((audio->SampleRate * audio->Channels)
- * (audio->BitsPerSample >> 3));
- }
- /* Adjust the HMI buffer to accomodate the amount of data. */
- #if(0)
- config->HMIBufSize *= (audio->SampleRate / 22050);
- config->HMIBufSize *= audio->Channels * (audio->BitsPerSample >> 3);
- #endif
- /* The default audio buffer size should be large enough to hold
- * 1.5 seconds of data.
- */
- if (config->AudioBufSize == -1) {
- /* Compute the number of HMI buffers that will completly fit into
- * 1.5 seconds of audio data.
- */
- i = ((audio->BytesPerSec+(audio->BytesPerSec/2))/config->HMIBufSize);
- config->AudioBufSize = (config->HMIBufSize * i);
- }
- /* Do not allocate anything if the audio buffer is zero length. */
- if (config->AudioBufSize > 0) {
- /* Allocate an audio buffer if the user did not provide one.
- * Otherwise, use the user supplied buffer.
- */
- if (config->AudioBuf == NULL) {
- audio->Buffer = (unsigned char *)malloc(config->AudioBufSize);
- /* If failure then clean up and exit. */
- if (audio->Buffer == NULL) {
- FreeBuffers(vqa, config, header);
- return (NULL);
- }
- DPMI_Lock(audio->Buffer, config->AudioBufSize);
- /* Add audio buffer size to memory usage. */
- vqa->MemUsed += config->AudioBufSize;
- } else {
- audio->Buffer = config->AudioBuf;
- }
- /* Allocate IsLoaded flags */
- audio->NumAudBlocks = (config->AudioBufSize / config->HMIBufSize);
- audio->IsLoaded = (short *)malloc(audio->NumAudBlocks * sizeof(short));
- /* If failure then clean up and exit. */
- if (audio->IsLoaded == NULL) {
- FreeBuffers(vqa, config, header);
- return (NULL);
- }
- /* Lock to prevent page swapping. */
- DPMI_Lock(audio->IsLoaded, audio->NumAudBlocks * sizeof(short));
- /* Add IsLoaded flags array to memory usage. */
- vqa->MemUsed += (audio->NumAudBlocks * sizeof(short));
- /* Initialize audio IsLoaded flags to false. */
- memset(audio->IsLoaded, 0, audio->NumAudBlocks * sizeof(short));
- /* Allocate temporary staging buffer for the audio frames. */
- audio->TempBufSize = ((audio->BytesPerSec / header->FPS) * 2) + 100;
- audio->TempBuf = (unsigned char *)malloc(audio->TempBufSize);
- if (audio->TempBuf == NULL) {
- FreeBuffers(vqa, config, header);
- return (NULL);
- }
- /* Lock to prevent page swapping. */
- DPMI_Lock(audio->TempBuf, audio->TempBufSize);
- /* Add temporary buffer size to memory usage. */
- vqa->MemUsed += audio->TempBufSize;
- }
- }
- #endif /* VQAAUDIO_ON */
- /*-------------------------------------------------------------------------
- * ALLOCATE THE FRAME INFORMATION TABLE IF REQUESTED.
- *-----------------------------------------------------------------------*/
- vqa->Foff = (long *)malloc(header->Frames * sizeof(long));
- if (vqa->Foff == NULL) {
- FreeBuffers(vqa, config, header);
- return (NULL);
- }
- /* Lock to prevent page swapping. */
- DPMI_Lock(vqa->Foff, header->Frames * sizeof(long));
- /* Keep a running total of memory usage. */
- vqa->MemUsed += (header->Frames * sizeof(long));
-
- return (vqa);
- }
- /****************************************************************************
- *
- * NAME
- * FreeBuffers - Free VQA play buffers.
- *
- * SYNOPSIS
- * FreeBuffers(VQAData, Config, Header)
- *
- * void FreeBuffers(VQAData *, VQAConfig *, VQAHeader *);
- *
- * FUNCTION
- * Free the buffers allocated by AllocBuffers().
- *
- * INPUTS
- * VQAData - Pointer to VQAData structure.
- * Config - Pointer to configuration structure.
- * Header - Pointer to movie header structure.
- *
- * RESULT
- * NONE
- *
- ****************************************************************************/
- static void FreeBuffers(VQAData *vqa, VQAConfig *config, VQAHeader *header)
- {
- VQACBNode *cb_this,
- *cb_next;
- VQAFrameNode *frame_this,
- *frame_next;
- long i;
- /*-------------------------------------------------------------------------
- * FREE THE FRAME INFORMATION TABLE.
- *-----------------------------------------------------------------------*/
- if (vqa->Foff) {
- DPMI_Unlock(vqa->Foff, header->Frames * sizeof(long));
- free(vqa->Foff);
- }
- /*-------------------------------------------------------------------------
- * FREE THE AUDIO BUFFERS.
- *-----------------------------------------------------------------------*/
- #if(VQAAUDIO_ON)
- if ((config->AudioBuf == NULL) && (vqa->Audio.Buffer)) {
- DPMI_Unlock(vqa->Audio.Buffer, config->AudioBufSize);
- free(vqa->Audio.Buffer);
- }
- /* Free the audio segments loaded flag array. */
- if (vqa->Audio.IsLoaded) {
- DPMI_Unlock(vqa->Audio.IsLoaded,vqa->Audio.NumAudBlocks * sizeof(short));
- free(vqa->Audio.IsLoaded);
- }
- /* Free the temporary audio buffer. */
- if (vqa->Audio.TempBuf) {
- DPMI_Unlock(vqa->Audio.TempBuf, vqa->Audio.TempBufSize);
- free(vqa->Audio.TempBuf);
- }
- #endif /* VQAAUDIO_ON */
- /*-------------------------------------------------------------------------
- * FREE THE IMAGE BUFFER ONLY IF WE ALLOCATED IT.
- *-----------------------------------------------------------------------*/
- if ((config->ImageBuf == NULL) && vqa->Drawer.ImageBuf) {
- DPMI_Unlock(vqa->Drawer.ImageBuf,header->ImageWidth*header->ImageHeight);
- free(vqa->Drawer.ImageBuf);
- }
- /*-------------------------------------------------------------------------
- * FREE THE FRAME BUFFERS.
- *-----------------------------------------------------------------------*/
- frame_this = vqa->FrameData;
- for (i = 0; i < config->NumFrameBufs; i++) {
- if (frame_this) {
- frame_next = frame_this->Next;
- DPMI_Unlock(frame_this, sizeof(VQAFrameNode) + vqa->Max_Ptr_Size
- + vqa->Max_Pal_Size);
- free(frame_this);
- frame_this = frame_next;
- } else {
- break;
- }
- }
- /*-------------------------------------------------------------------------
- * FREE THE CODEBOOK BUFFERS.
- *-----------------------------------------------------------------------*/
- cb_this = vqa->CBData;
- for (i = 0; i < config->NumCBBufs; i++) {
- if (cb_this) {
- cb_next = cb_this->Next;
- DPMI_Unlock(cb_this, sizeof(VQACBNode) + vqa->Max_CB_Size);
- free(cb_this);
- cb_this = cb_next;
- } else {
- break;
- }
- }
- /*-------------------------------------------------------------------------
- * FREE THE VQA DATA STRUCTURES.
- *-----------------------------------------------------------------------*/
- DPMI_Unlock(vqa, sizeof(VQAData));
- free(vqa);
- }
- /****************************************************************************
- *
- * NAME
- * PrimeBuffers - Pre-Load the internal buffers.
- *
- * SYNOPSIS
- * Error = PrimeBuffers(VQA)
- *
- * long = PrimeBuffers(VQAHandle *);
- *
- * FUNCTION
- * Pre-load the internal buffers in order to give the player some slack
- * in the playback of large frames.
- *
- * INPUTS
- * VQA - Pointer to VQAHandle structure.
- *
- * RESULT
- * Error - 0 if successful, or VQAERR_??? error code.
- *
- ****************************************************************************/
- long PrimeBuffers(VQAHandle *vqa)
- {
- VQAData *vqabuf;
- VQAConfig *config;
- long rc;
- long i;
- /* Dereference commonly used data members for quick access. */
- vqabuf = ((VQAHandleP *)vqa)->VQABuf;
- config = &((VQAHandleP *)vqa)->Config;
- /* Pre-load the buffers */
- for (i = 0; i < config->NumFrameBufs; i++) {
- if ((rc = VQA_LoadFrame(vqa)) == 0) {
- vqabuf->LoadedFrames++;
- }
- else if ((rc != VQAERR_NOBUFFER) && (rc != VQAERR_SLEEPING)) {
- return (rc);
- }
- }
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_VQF - Loads a VQ Frame chunk.
- *
- * SYNOPSIS
- * Error = Load_VQF(VQA, Iffsize)
- *
- * long Load_VQF(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- * The VQ Frame Chunk contains a set of other chunks (codebooks,
- * palettes, pointers). This routine reads the frame's chunk size,
- * then loops until it's read that many bytes.
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_VQF(VQAHandleP *vqap, unsigned long frame_iffsize)
- {
- VQAData *vqabuf;
- VQAFrameNode *curframe;
- ChunkHeader *chunk;
- unsigned long iffsize;
- unsigned long framesize;
- unsigned long bytes_loaded = 0;
- /* Dereference commonly used data members for quicker access. */
- vqabuf = vqap->VQABuf;
- curframe = vqabuf->Loader.CurFrame;
- chunk = &vqabuf->Loader.CurChunkHdr;
- framesize = PADSIZE(frame_iffsize);
- /*-------------------------------------------------------------------------
- * FRAME LOADING LOOP.
- *-----------------------------------------------------------------------*/
- while (bytes_loaded < framesize) {
- /* Read chunk ID */
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, chunk, 8)) {
- return (VQAERR_EOF);
- }
- iffsize = REVERSE_LONG(chunk->size);
- bytes_loaded += 8;
- bytes_loaded += PADSIZE(iffsize);
- /* Handle each chunk type */
- switch (chunk->id) {
- /* Full uncompressed codebook */
- case ID_CBF0:
- if (Load_CBF0(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- break;
- /* Full compressed codebook */
- case ID_CBFZ:
- if (Load_CBFZ(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- break;
- /* Partial uncompressed codebook */
- case ID_CBP0:
- if (Load_CBP0(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- break;
- /* Partial compressed codebook */
- case ID_CBPZ:
- if (Load_CBPZ(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- break;
- /* Uncompressed palette */
- case ID_CPL0:
- if (Load_CPL0(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- /* Flag this frame as having a palette. */
- curframe->Flags |= VQAFRMF_PALETTE;
- break;
- /* Compressed palette */
- case ID_CPLZ:
- if (Load_CPLZ(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- /* Flag this frame as having a palette. */
- curframe->Flags |= VQAFRMF_PALETTE;
- break;
- /* Uncompressed pointer data */
- case ID_VPT0:
- if (Load_VPT0(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- break;
- /* Compressed pointer data */
- case ID_VPTZ:
- case ID_VPTD:
- if (Load_VPTZ(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- break;
- /* Compressed pointer data */
- case ID_VPTK:
- if (Load_VPTZ(vqap, iffsize)) {
- return (VQAERR_READ);
- }
- /* Flag this frame as being key. */
- curframe->Flags |= VQAFRMF_KEY;
- break;
- /* An unknown chunk in the video frame is an error. */
- default:
- return (VQAERR_READ);
- }
- }
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_FINF - Load Frame Info chunk.
- *
- * SYNOPSIS
- * Error = Load_FINF(VQA, Iffsize)
- *
- * long Load_FINF(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- * Load FINF chunk if buffer available, otherwise skip it.
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_FINF(VQAHandleP *vqap, unsigned long iffsize)
- {
- VQAData *vqabuf;
- /* Dereference commonly used data members for quicker access. */
- vqabuf = vqap->VQABuf;
- /* Load the frame information table if we need to, otherwise we will
- * skip it.
- */
- if (vqabuf->Foff != NULL) {
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, vqabuf->Foff,
- PADSIZE(iffsize))) {
- return (VQAERR_READ);
- }
- } else {
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_SEEK, (void *)SEEK_CUR,
- PADSIZE(iffsize))) {
- return (VQAERR_SEEK);
- }
- }
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_VQHD - Load VQA header chunk.
- *
- * SYNOPSIS
- * Error = Load_VQHD(VQA, Iffsize)
- *
- * long Load_VQHD(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_VQHD(VQAHandleP *vqap, unsigned long iffsize)
- {
- /* Read the header */
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, &vqap->Header,
- PADSIZE(iffsize))) {
- return (VQAERR_READ);
- }
- /* Reconfigure the Drawer for the new settings */
- VQA_Configure_Drawer(vqap);
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_CBF0 - Load full uncompressed codebook.
- *
- * SYNOPSIS
- * Error = Load_CBF0(VQA, Iffsize)
- *
- * long Load_CBF0(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_CBF0(VQAHandleP *vqap, unsigned long iffsize)
- {
- VQALoader *loader;
- VQACBNode *curcb;
- /* Dereference commonly used data members for quicker access. */
- loader = &vqap->VQABuf->Loader;
- curcb = loader->CurCB;
- /* Read into the start of the buffer */
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, curcb->Buffer,
- PADSIZE(iffsize))) {
- return (VQAERR_READ);
- }
- /* Reset the partial codebook counter. */
- loader->NumPartialCB = 0;
- /* Flag this codebook as uncompressed. */
- curcb->Flags &= (~VQACBF_CBCOMP);
- curcb->CBOffset = 0;
- /* Clock pointers to next CB Buffer. */
- loader->FullCB = curcb;
- loader->FullCB->Flags &= (~VQACBF_DOWNLOADED);
- loader->CurCB = curcb->Next;
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_CBFZ - Load full compressed codebook.
- *
- * SYNOPSIS
- * Error = Load_CBFZ(VQA, Iffsize)
- *
- * long Load_CBFZ(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_CBFZ(VQAHandleP *vqap, unsigned long iffsize)
- {
- VQALoader *loader;
- VQACBNode *curcb;
- void *buffer;
- unsigned long padsize;
- unsigned long lcwoffset;
- /* Dereference commonly used data members for quicker access. */
- loader = &vqap->VQABuf->Loader;
- curcb = loader->CurCB;
- padsize = PADSIZE(iffsize);
- /* Load the codebook into the end of the buffer. */
- lcwoffset = vqap->VQABuf->Max_CB_Size - padsize;
- buffer = curcb->Buffer + lcwoffset;
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, buffer, padsize)) {
- return (VQAERR_READ);
- }
- /* Reset the partial codebook counter. */
- loader->NumPartialCB = 0;
- /* Flag this codebook as compressed */
- curcb->Flags |= VQACBF_CBCOMP;
- curcb->CBOffset = lcwoffset;
- /* Clock pointers to next CB Buffer */
- loader->FullCB = curcb;
- loader->FullCB->Flags &= (~VQACBF_DOWNLOADED);
- loader->CurCB = curcb->Next;
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_CBP0 - Load partial uncompressed codebook.
- *
- * SYNOPSIS
- * Error = Load_CBP0(VQA, Iffsize)
- *
- * long Load_CBP0(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQA_??? error code.
- *
- ****************************************************************************/
- static long Load_CBP0(VQAHandleP *vqap, unsigned long iffsize)
- {
- VQAData *vqabuf;
- VQALoader *loader;
- VQACBNode *curcb;
- void *buffer;
- /* Dereference commonly used data members for quicker access. */
- vqabuf = vqap->VQABuf;
- loader = &vqabuf->Loader;
- curcb = loader->CurCB;
- /*-------------------------------------------------------------------------
- * ASSEMBLY PARTIAL CODEBOOKS.
- *-----------------------------------------------------------------------*/
- /* Read the partial codebook into the next position in the buffer. */
- buffer = curcb->Buffer + loader->PartialCBSize;
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, buffer,
- PADSIZE(iffsize))) {
- return (VQAERR_READ);
- }
- /* Accumulate the partial codebook values. */
- loader->PartialCBSize += iffsize;
- loader->NumPartialCB++;
- /*-------------------------------------------------------------------------
- * PROCESS FULL CODEBOOK.
- *-----------------------------------------------------------------------*/
- if (loader->NumPartialCB == vqap->Header.Groupsize) {
- /* Reset the codebook accumulator values */
- loader->NumPartialCB = 0;
- loader->PartialCBSize = 0;
- /* Flag this codebook as uncompressed */
- curcb->Flags &= (~VQACBF_CBCOMP);
- curcb->CBOffset = 0;
- /* Go to the next codebook buffer */
- loader->FullCB = curcb;
- loader->FullCB->Flags &= (~VQACBF_DOWNLOADED);
- loader->CurCB = curcb->Next;
- }
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_CBPZ - Load partial compressed codebook.
- *
- * SYNOPSIS
- * Error = Load_CBPZ(VQA, Iffsize)
- *
- * long Load_CBPZ(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_CBPZ(VQAHandleP *vqap, unsigned long iffsize)
- {
- VQAData *vqabuf;
- VQALoader *loader;
- VQACBNode *curcb;
- void *buffer;
- unsigned long padsize;
- /* Dereference commonly used data members for quicker access */
- vqabuf = vqap->VQABuf;
- loader = &vqabuf->Loader;
- curcb = loader->CurCB;
- padsize = PADSIZE(iffsize);
- /* Attempt to compute the LCW offset into the codebook buffer by
- * multiplying the size of this chunk by the # frames/group, and adding
- * a small fudge factor on, then subtracting that from the CB buffer size.
- */
- if (loader->PartialCBSize == 0) {
- curcb->CBOffset = (vqabuf->Max_CB_Size
- - (padsize * vqap->Header.Groupsize + 100));
- }
- /*-------------------------------------------------------------------------
- * ASSEMBLE PARTIAL CODEBOOKS.
- *-----------------------------------------------------------------------*/
- /* Read the partial codebook into the next position in the buffer. */
- buffer = ((curcb->Buffer + curcb->CBOffset) + loader->PartialCBSize);
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, buffer, padsize)) {
- return (VQAERR_READ);
- }
- /* Accumulate partial codebook values */
- loader->PartialCBSize += iffsize;
- loader->NumPartialCB++;
- /*-------------------------------------------------------------------------
- * PROCESS FULL CODEBOOK.
- *-----------------------------------------------------------------------*/
- if (loader->NumPartialCB == vqap->Header.Groupsize) {
- /* Reset the codebook accumulator values. */
- loader->NumPartialCB = 0;
- loader->PartialCBSize = 0;
- /* Flag this codebook as compressed. */
- curcb->Flags |= VQACBF_CBCOMP;
- /* Go to the next codebook buffer */
- loader->FullCB = curcb;
- loader->FullCB->Flags &= (~VQACBF_DOWNLOADED);
- loader->CurCB = curcb->Next;
- }
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_CPL0 - Load an uncompressed palette.
- *
- * SYNOPSIS
- * Error = Load_CPL0(VQA, Iffsize)
- *
- * long Load_CPL0(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_CPL0(VQAHandleP *vqap, unsigned long iffsize)
- {
- VQAFrameNode *curframe;
- /* Dereference commonly used data members for quicker access. */
- curframe = vqap->VQABuf->Loader.CurFrame;
- /* Read the palette into the palette buffer */
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, curframe->Palette,
- PADSIZE(iffsize))) {
- return (VQAERR_READ);
- }
- /* Flag the palette as uncompressed. */
- curframe->Flags &= ~VQAFRMF_PALCOMP;
- curframe->PalOffset = 0;
- curframe->PaletteSize = iffsize;
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_CPLZ - Load compressed palette.
- *
- * SYNOPSIS
- * Error = Load_CPLZ(VQA, Iffsize)
- *
- * long Load_CPLZ(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_CPLZ(VQAHandleP *vqap, unsigned long iffsize)
- {
- VQAFrameNode *curframe;
- void *buffer;
- unsigned long padsize;
- unsigned long lcwoffset;
- /* Dereference commonly used data members for quicker access. */
- curframe = vqap->VQABuf->Loader.CurFrame;
- padsize = PADSIZE(iffsize);
- /* Read the palette into the end of the palette buffer. */
- lcwoffset = vqap->VQABuf->Max_Pal_Size - padsize;
- buffer = curframe->Palette + lcwoffset;
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, buffer, padsize)) {
- return (VQAERR_READ);
- }
- /* Flag this palette as compressed. */
- curframe->Flags |= VQAFRMF_PALCOMP;
- curframe->PalOffset = lcwoffset;
- curframe->PaletteSize = iffsize;
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_VPT0 - Load uncompressed pointers.
- *
- * SYNOPSIS
- * Error = Load_VPT0(VQA, Iffsize)
- *
- * long Load_VPT0(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_VPT0(VQAHandleP *vqap, unsigned long iffsize)
- {
- VQAFrameNode *curframe;
- /* Dereference commonly used data members for quicker access. */
- curframe = vqap->VQABuf->Loader.CurFrame;
- /* Read the pointers into start of the pointer buffer. */
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, curframe->Pointers,
- PADSIZE(iffsize))) {
- return (VQAERR_READ);
- }
- /* Flag this frame as uncompressed */
- curframe->Flags &= ~VQAFRMF_PTRCOMP;
- curframe->PtrOffset = 0;
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_VPTZ - Load compressed pointers.
- *
- * SYNOPSIS
- * Error = Load_VPTZ(VQA, Iffsize)
- *
- * long Load_VPTZ(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_VPTZ(VQAHandleP *vqap, unsigned long iffsize)
- {
- VQAFrameNode *curframe;
- void *buffer;
- unsigned long padsize;
- unsigned long lcwoffset;
- /* Dereference commonly used data members for quicker access. */
- curframe = vqap->VQABuf->Loader.CurFrame;
- padsize = PADSIZE(iffsize);
- lcwoffset = vqap->VQABuf->Max_Ptr_Size - padsize;
- /* Read the pointers into end of the pointer buffer. */
- buffer = curframe->Pointers + lcwoffset;
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, buffer, padsize)) {
- return (VQAERR_READ);
- }
- /* Flag this frame as compressed. */
- curframe->Flags |= VQAFRMF_PTRCOMP;
- curframe->PtrOffset = lcwoffset;
- return (0);
- }
- #if(VQAAUDIO_ON)
- /****************************************************************************
- *
- * NAME
- * Load_SND0 - Load uncompressed sound chunk.
- *
- * SYNOPSIS
- * Error = Load_SND0(VQA, Iffsize)
- *
- * long Load_SND0(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- * This routine normally loads the chunk into the TempBuf, unless the
- * chunk is larger than the temp buffer size, in which case it puts it
- * directly into the audio buffer itself. This assumes that the only
- * such chunk will be the first audio chunk!
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_SND0(VQAHandleP *vqap, unsigned long iffsize)
- {
- VQAData *vqabuf;
- VQALoader *loader;
- VQAAudio *audio;
- VQAConfig *config;
- unsigned long padsize;
- long i;
- /* Dereference commonly used data members for quicker access. */
- vqabuf = vqap->VQABuf;
- loader = &vqabuf->Loader;
- audio = &vqabuf->Audio;
- config = &vqap->Config;
- padsize = PADSIZE(iffsize);
- /* If sound is disabled, or if we're playing from a VOC file, or if
- * there's no Audio Buffer, just skip the chunk.
- */
- #if(VQAVOC_ON && VQAAUDIO_ON)
- if (((config->OptionFlags & VQAOPTF_AUDIO) == 0)
- || (vqap->vocfh != -1) || (audio->Buffer == NULL)) {
- #else /* VQAVOC_ON */
- if (((config->OptionFlags & VQAOPTF_AUDIO) == 0)
- || (audio->Buffer == NULL)) {
- #endif /* VQAVOC_ON */
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_SEEK, (void *)SEEK_CUR,
- padsize)) {
- return (VQAERR_SEEK);
- } else {
- return (0);
- }
- }
- /* Read large startup chunk directly into AudioBuf */
- if ((padsize > audio->TempBufSize) && (audio->AudBufPos == 0)) {
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, audio->Buffer,
- padsize)) {
- return (VQAERR_READ);
- }
- audio->AudBufPos += iffsize;
- /* Flag the audio frame flags as loaded for the initial audio frame. */
- for (i = 0; i < (iffsize / config->HMIBufSize); i++) {
- audio->IsLoaded[i] = 1;
- }
- return (0);
- } else {
- /* Read data into TempBuf */
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, audio->TempBuf,
- padsize)) {
- return (VQAERR_READ);
- }
- }
- /* Set the TempBufLen */
- audio->TempBufLen = iffsize;
-
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_SND1 - Load compressed sound chunk.
- *
- * SYNOPSIS
- * Error = Load_SND1(VQA, Iffsize)
- *
- * long Load_SND1(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- * This routine normally loads the chunk into the TempBuf, unless the
- * chunk is larger than the temp buffer size, in which case it puts it
- * directly into the audio buffer itself. This assumes that the only
- * such chunk will be the first audio chunk!
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_SND1(VQAHandleP *vqap, unsigned long iffsize)
- {
- VQAData *vqabuf;
- VQALoader *loader;
- VQAAudio *audio;
- VQAConfig *config;
- unsigned char *loadbuf;
- unsigned long padsize;
- ZAPHeader zap;
- long i;
- /* Dereference commonly used data members for quicker access. */
- vqabuf = vqap->VQABuf;
- loader = &vqabuf->Loader;
- audio = &vqabuf->Audio;
- config = &vqap->Config;
- padsize = PADSIZE(iffsize);
- /* If sound is disabled, or if we're playing from a VOC file, or if
- * there's no Audio Buffer, just skip the chunk
- */
- #if(VQAVOC_ON && VQAAUDIO_ON)
- if (((config->OptionFlags & VQAOPTF_AUDIO) == 0) || (vqap->vocfh != -1)
- || (audio->Buffer == NULL)) {
- #else
- if (((config->OptionFlags & VQAOPTF_AUDIO)==0) || (audio->Buffer==NULL)) {
- #endif /* VQAVOC_ON */
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_SEEK, (void *)SEEK_CUR,
- padsize)) {
- return (VQAERR_SEEK);
- } else {
- return (0);
- }
- }
- /* Read the ZAP audio frame header. */
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, &zap,
- sizeof(ZAPHeader))) {
- return (VQAERR_READ);
- }
- /* Adjust chunk size */
- padsize -= sizeof(ZAPHeader);
- /* Read large startup chunk directly into AudioBuf */
- if ((zap.UnCompSize > audio->TempBufSize) && (audio->AudBufPos == 0)) {
- /* Load RAW uncompressed data. */
- if (zap.UnCompSize == zap.CompSize) {
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, audio->Buffer,
- padsize)) {
- return (VQAERR_READ);
- }
- } else {
- /* Load compressed data into the end of the buffer. */
- loadbuf = (audio->Buffer + config->AudioBufSize) - padsize;
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, loadbuf,
- padsize)) {
- return (VQAERR_READ);
- }
- /* Uncompress the audio frame. */
- AudioUnzap(loadbuf, audio->Buffer, zap.UnCompSize);
- }
- /* Set buffer positions & flags */
- audio->AudBufPos += zap.UnCompSize;
- for (i = 0; i < (zap.UnCompSize / config->HMIBufSize); i++) {
- audio->IsLoaded[i] = 1;
- }
- return (0);
- }
- /* Load an audio frame. */
- if (zap.UnCompSize == zap.CompSize) {
- /* If the frame is uncompressed the load it in directly. */
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, audio->TempBuf,
- padsize)) {
- return (VQAERR_READ);
- }
- } else {
- /* Load the audio frame into the end of the buffer. */
- loadbuf = ((audio->TempBuf + audio->TempBufSize) - padsize);
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, loadbuf, padsize)) {
- return (VQAERR_READ);
- }
- /* Uncompress the audio frame. */
- AudioUnzap(loadbuf, audio->TempBuf, zap.UnCompSize);
- }
- /* Set the TempBufLen */
- audio->TempBufLen = zap.UnCompSize;
-
- return (0);
- }
- /****************************************************************************
- *
- * NAME
- * Load_SND2 - Load ADPCM compressed sound chunk.
- *
- * SYNOPSIS
- * Error = Load_SND2(VQA, Iffsize)
- *
- * long Load_SND2(VQAHandleP *, unsigned long);
- *
- * FUNCTION
- * This routine normally loads the chunk into the TempBuf, unless the
- * chunk is larger than the temp buffer size, in which case it puts it
- * directly into the audio buffer itself. This assumes that the only
- * such chunk will be the first audio chunk!
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- * Iffsize - Size of IFF chunk.
- *
- * RESULT
- * Error - 0 if successful or VQAERR_??? error code.
- *
- ****************************************************************************/
- static long Load_SND2(VQAHandleP *vqap, unsigned long iffsize)
- {
- VQAData *vqabuf;
- VQALoader *loader;
- VQAAudio *audio;
- VQAConfig *config;
- unsigned char *loadbuf;
- unsigned long padsize;
- unsigned long uncomp_size;
- long i;
- /* Dereference commonly used data members for quicker access. */
- vqabuf = vqap->VQABuf;
- loader = &vqabuf->Loader;
- audio = &vqabuf->Audio;
- config = &vqap->Config;
- padsize = PADSIZE(iffsize);
- /* If sound is disabled, or if we're playing from a VOC file, or if
- * there's no Audio Buffer, just skip the chunk
- */
- #if(VQAVOC_ON && VQAAUDIO_ON)
- if (((config->OptionFlags & VQAOPTF_AUDIO) == 0) || (vqap->vocfh != -1)
- || (audio->Buffer == NULL)) {
- #else /* VQAVOC_ON */
- if (((config->OptionFlags & VQAOPTF_AUDIO)==0) || (audio->Buffer==NULL)) {
- #endif /* VQAVOC_ON */
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_SEEK, (void *)SEEK_CUR,
- padsize)) {
- return (VQAERR_SEEK);
- } else {
- return (0);
- }
- }
- uncomp_size = iffsize * (audio->BitsPerSample / 4);
- /* Read large startup chunk directly into AudioBuf */
- if ((uncomp_size > audio->TempBufSize) && (audio->AudBufPos == 0)) {
- /* Load compressed data into the end of the buffer. */
- loadbuf = (audio->Buffer + config->AudioBufSize) - padsize;
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, loadbuf, padsize)) {
- return (VQAERR_READ);
- }
- /* Uncompress the audio frame. */
- audio->ADPCM_Info.lpSource = (char *)loadbuf;
- audio->ADPCM_Info.lpDest = (char *)audio->Buffer;
- sosCODECDecompressData(&audio->ADPCM_Info, uncomp_size);
- /* Set buffer positions & flags */
- audio->AudBufPos += uncomp_size;
- for (i = 0; i < (uncomp_size / config->HMIBufSize); i++) {
- audio->IsLoaded[i] = 1;
- }
- return (0);
- }
- /* Load an audio frame. */
- loadbuf = ((audio->TempBuf + audio->TempBufSize) - padsize);
- if (vqap->IOHandler((VQAHandle *)vqap, VQACMD_READ, loadbuf, padsize)) {
- return (VQAERR_READ);
- }
- /* Uncompress the audio frame. */
- audio->ADPCM_Info.lpSource = (char *)loadbuf;
- audio->ADPCM_Info.lpDest = (char *)audio->TempBuf;
- sosCODECDecompressData(&audio->ADPCM_Info, uncomp_size);
- /* Set the TempBufLen */
- audio->TempBufLen = uncomp_size;
-
- return (0);
- }
- #if(VQAVOC_ON)
- /****************************************************************************
- *
- * NAME
- * Load_AudFrame - Loads blocks from seperate VOC file.
- *
- * SYNOPSIS
- * Load_AudFrame(VQA)
- *
- * void Load_AudFrame(VQAHandleP *);
- *
- * FUNCTION
- *
- * INPUTS
- * VQA - Pointer to private VQA handle.
- *
- * RESULT
- * NONE
- *
- ****************************************************************************/
- static void Load_AudFrame(VQAHandleP *vqap)
- {
- VQAData *vqabuf;
- VQALoader *loader;
- VQAAudio *audio;
- VQAConfig *config;
- static long lastplayblock = -1;
- static long myblock = 0;
- static long firsttime = 1;
- long numblocks;
- long i;
- /* Dereference commonly used data members for quicker access. */
- vqabuf = vqap->VQABuf;
- loader = &vqabuf->Loader;
- audio = &vqabuf->Audio;
- config = &vqap->Config;
- /* Do nothing if no buffer */
- if (audio->Buffer == NULL) {
- return;
- }
- /* If this is the first time we're called, pre-load the 1st 'n' audio
- * blocks, where 'n' is half the total audio buffer size; this way, we'll
- * always stay ahead of HMI.
- */
- if (firsttime) {
- numblocks = (audio->NumAudBlocks / 2);
- read(vqap->vocfh, audio->Buffer, config->HMIBufSize * numblocks);
- audio->AudBufPos += config->HMIBufSize * numblocks;
- if (audio->AudBufPos >= config->AudioBufSize) {
- audio->AudBufPos = 0;
- }
- for (i = 0; i < numblocks; i++) {
- audio->IsLoaded[i] = 1;
- }
- myblock += numblocks;
-
- if (myblock >= audio->NumAudBlocks) {
- myblock = 0;
- }
- firsttime = 0;
- }
- /* If HMI's block has changed, load the next block & mark it as loaded */
- if (audio->PlayPosition / config->HMIBufSize != lastplayblock) {
- /* update HMI's last known block position */
- lastplayblock = audio->PlayPosition / config->HMIBufSize;
- /* read the VOC data */
- read(vqap->vocfh, (audio->Buffer + myblock * config->HMIBufSize),
- config->HMIBufSize);
- audio->AudBufPos += config->HMIBufSize;
- if (audio->AudBufPos >= config->AudioBufSize) {
- audio->AudBufPos = 0;
- }
- /* set the IsLoaded flags */
- audio->IsLoaded[myblock] = 1;
- /* increment my block counter */
- myblock++;
- if (myblock >= audio->NumAudBlocks) {
- myblock = 0;
- }
- }
- }
- #endif /* VQAVOC_ON */
- #endif /* VQAAUDIO_ON */
|