roq.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "roq.h"
  23. #include "codec.h"
  24. roq *theRoQ; // current roq file
  25. roq::roq( void )
  26. {
  27. image = 0;
  28. quietMode = false;
  29. encoder = 0;
  30. previousSize = 0;
  31. lastFrame = false;
  32. dataStuff=false;
  33. }
  34. roq::~roq( void )
  35. {
  36. if (image) delete image;
  37. if (encoder) delete encoder;
  38. return;
  39. }
  40. void roq::EncodeQuietly( bool which )
  41. {
  42. quietMode = which;
  43. }
  44. bool roq::IsQuiet( void )
  45. {
  46. return quietMode;
  47. }
  48. bool roq::IsLastFrame( void )
  49. {
  50. return lastFrame;
  51. }
  52. bool roq::Scaleable( void )
  53. {
  54. return paramFile->IsScaleable();
  55. }
  56. bool roq::ParamNoAlpha( void )
  57. {
  58. return paramFile->NoAlpha();
  59. }
  60. bool roq::MakingVideo( void )
  61. {
  62. return true; //paramFile->timecode];
  63. }
  64. bool roq::SearchType( void )
  65. {
  66. return paramFile->SearchType();
  67. }
  68. bool roq::HasSound( void )
  69. {
  70. return paramFile->HasSound();
  71. }
  72. int roq::PreviousFrameSize( void )
  73. {
  74. return previousSize;
  75. }
  76. int roq::FirstFrameSize( void )
  77. {
  78. return paramFile->FirstFrameSize();
  79. }
  80. int roq::NormalFrameSize( void )
  81. {
  82. return paramFile->NormalFrameSize();
  83. }
  84. const char * roq::CurrentFilename( void )
  85. {
  86. return currentFile.c_str();
  87. }
  88. void roq::EncodeStream( const char *paramInputFile )
  89. {
  90. int onFrame;
  91. idStr f0, f1, f2;
  92. int morestuff;
  93. onFrame = 1;
  94. encoder = new codec;
  95. paramFile = new roqParam;
  96. paramFile->numInputFiles = 0;
  97. paramFile->InitFromFile( paramInputFile );
  98. if (!paramFile->NumberOfFrames()) {
  99. return;
  100. }
  101. InitRoQFile( paramFile->outputFilename);
  102. numberOfFrames = paramFile->NumberOfFrames();
  103. if (paramFile->NoAlpha()==true) common->Printf("encodeStream: eluding alpha\n");
  104. f0 = "";
  105. f1 = paramFile->GetNextImageFilename();
  106. if (( paramFile->MoreFrames() == true )) {
  107. f2 = paramFile->GetNextImageFilename();
  108. }
  109. morestuff = numberOfFrames;
  110. while( morestuff ) {
  111. LoadAndDisplayImage( f1 );
  112. if (onFrame==1) {
  113. encoder->SparseEncode();
  114. // WriteLossless();
  115. } else {
  116. if (!strcmp( f0, f1 ) && strcmp( f1, f2) ) {
  117. WriteHangFrame();
  118. } else {
  119. encoder->SparseEncode();
  120. }
  121. }
  122. onFrame++;
  123. f0 = f1;
  124. f1 = f2;
  125. if (paramFile->MoreFrames() == true) {
  126. f2 = paramFile->GetNextImageFilename();
  127. }
  128. morestuff--;
  129. session->UpdateScreen();
  130. }
  131. // if (numberOfFrames != 1) {
  132. // if (image->hasAlpha() && paramFile->NoAlpha()==false) {
  133. // lastFrame = true;
  134. // encoder->SparseEncode();
  135. // } else {
  136. // WriteLossless();
  137. // }
  138. // }
  139. CloseRoQFile();
  140. }
  141. void roq::Write16Word( word *aWord, idFile *stream )
  142. {
  143. byte a, b;
  144. a = *aWord & 0xff;
  145. b = *aWord >> 8;
  146. stream->Write( &a, 1 );
  147. stream->Write( &b, 1 );
  148. }
  149. void roq::Write32Word( unsigned int *aWord, idFile *stream )
  150. {
  151. byte a, b, c, d;
  152. a = *aWord & 0xff;
  153. b = (*aWord >> 8) & 0xff;
  154. c = (*aWord >> 16) & 0xff;
  155. d = (*aWord >> 24) & 0xff;
  156. stream->Write( &a, 1 );
  157. stream->Write( &b, 1 );
  158. stream->Write( &c, 1 );
  159. stream->Write( &d, 1 );
  160. }
  161. int roq::SizeFile( idFile *ftosize )
  162. {
  163. return ftosize->Length();
  164. }
  165. /* Expanded data destination object for stdio output */
  166. typedef struct {
  167. struct jpeg_destination_mgr pub; /* public fields */
  168. byte* outfile; /* target stream */
  169. int size;
  170. } my_destination_mgr;
  171. typedef my_destination_mgr * my_dest_ptr;
  172. /*
  173. * Initialize destination --- called by jpeg_start_compress
  174. * before any data is actually written.
  175. */
  176. void roq::JPEGInitDestination (j_compress_ptr cinfo) {
  177. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  178. dest->pub.next_output_byte = dest->outfile;
  179. dest->pub.free_in_buffer = dest->size;
  180. }
  181. /*
  182. * Empty the output buffer --- called whenever buffer fills up.
  183. *
  184. * In typical applications, this should write the entire output buffer
  185. * (ignoring the current state of next_output_byte & free_in_buffer),
  186. * reset the pointer & count to the start of the buffer, and return true
  187. * indicating that the buffer has been dumped.
  188. *
  189. * In applications that need to be able to suspend compression due to output
  190. * overrun, a FALSE return indicates that the buffer cannot be emptied now.
  191. * In this situation, the compressor will return to its caller (possibly with
  192. * an indication that it has not accepted all the supplied scanlines). The
  193. * application should resume compression after it has made more room in the
  194. * output buffer. Note that there are substantial restrictions on the use of
  195. * suspension --- see the documentation.
  196. *
  197. * When suspending, the compressor will back up to a convenient restart point
  198. * (typically the start of the current MCU). next_output_byte & free_in_buffer
  199. * indicate where the restart point will be if the current call returns FALSE.
  200. * Data beyond this point will be regenerated after resumption, so do not
  201. * write it out when emptying the buffer externally.
  202. */
  203. boolean roq::JPEGEmptyOutputBuffer (j_compress_ptr cinfo) {
  204. return true;
  205. }
  206. /*
  207. * Compression initialization.
  208. * Before calling this, all parameters and a data destination must be set up.
  209. *
  210. * We require a write_all_tables parameter as a failsafe check when writing
  211. * multiple datastreams from the same compression object. Since prior runs
  212. * will have left all the tables marked sent_table=true, a subsequent run
  213. * would emit an abbreviated stream (no tables) by default. This may be what
  214. * is wanted, but for safety's sake it should not be the default behavior:
  215. * programmers should have to make a deliberate choice to emit abbreviated
  216. * images. Therefore the documentation and examples should encourage people
  217. * to pass write_all_tables=true; then it will take active thought to do the
  218. * wrong thing.
  219. */
  220. void roq::JPEGStartCompress (j_compress_ptr cinfo, bool write_all_tables) {
  221. if (cinfo->global_state != CSTATE_START)
  222. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  223. if (write_all_tables)
  224. jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */
  225. /* (Re)initialize error mgr and destination modules */
  226. (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  227. (*cinfo->dest->init_destination) (cinfo);
  228. /* Perform master selection of active modules */
  229. jinit_compress_master(cinfo);
  230. /* Set up for the first pass */
  231. (*cinfo->master->prepare_for_pass) (cinfo);
  232. /* Ready for application to drive first pass through jpeg_write_scanlines
  233. * or jpeg_write_raw_data.
  234. */
  235. cinfo->next_scanline = 0;
  236. cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
  237. }
  238. /*
  239. * Write some scanlines of data to the JPEG compressor.
  240. *
  241. * The return value will be the number of lines actually written.
  242. * This should be less than the supplied num_lines only in case that
  243. * the data destination module has requested suspension of the compressor,
  244. * or if more than image_height scanlines are passed in.
  245. *
  246. * Note: we warn about excess calls to jpeg_write_scanlines() since
  247. * this likely signals an application programmer error. However,
  248. * excess scanlines passed in the last valid call are *silently* ignored,
  249. * so that the application need not adjust num_lines for end-of-image
  250. * when using a multiple-scanline buffer.
  251. */
  252. JDIMENSION roq::JPEGWriteScanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION num_lines) {
  253. JDIMENSION row_ctr, rows_left;
  254. if (cinfo->global_state != CSTATE_SCANNING)
  255. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  256. if (cinfo->next_scanline >= cinfo->image_height)
  257. WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  258. /* Call progress monitor hook if present */
  259. if (cinfo->progress != NULL) {
  260. cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  261. cinfo->progress->pass_limit = (long) cinfo->image_height;
  262. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  263. }
  264. /* Give master control module another chance if this is first call to
  265. * jpeg_write_scanlines. This lets output of the frame/scan headers be
  266. * delayed so that application can write COM, etc, markers between
  267. * jpeg_start_compress and jpeg_write_scanlines.
  268. */
  269. if (cinfo->master->call_pass_startup)
  270. (*cinfo->master->pass_startup) (cinfo);
  271. /* Ignore any extra scanlines at bottom of image. */
  272. rows_left = cinfo->image_height - cinfo->next_scanline;
  273. if (num_lines > rows_left)
  274. num_lines = rows_left;
  275. row_ctr = 0;
  276. (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
  277. cinfo->next_scanline += row_ctr;
  278. return row_ctr;
  279. }
  280. /*
  281. * Terminate destination --- called by jpeg_finish_compress
  282. * after all data has been written. Usually needs to flush buffer.
  283. *
  284. * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  285. * application must deal with any cleanup that should happen even
  286. * for error exit.
  287. */
  288. static int hackSize;
  289. void roq::JPEGTermDestination (j_compress_ptr cinfo) {
  290. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  291. size_t datacount = dest->size - dest->pub.free_in_buffer;
  292. hackSize = datacount;
  293. }
  294. /*
  295. * Prepare for output to a stdio stream.
  296. * The caller must have already opened the stream, and is responsible
  297. * for closing it after finishing compression.
  298. */
  299. void roq::JPEGDest (j_compress_ptr cinfo, byte* outfile, int size) {
  300. my_dest_ptr dest;
  301. /* The destination object is made permanent so that multiple JPEG images
  302. * can be written to the same file without re-executing jpeg_stdio_dest.
  303. * This makes it dangerous to use this manager and a different destination
  304. * manager serially with the same JPEG object, because their private object
  305. * sizes may be different. Caveat programmer.
  306. */
  307. if (cinfo->dest == NULL) { /* first time for this JPEG object? */
  308. cinfo->dest = (struct jpeg_destination_mgr *)
  309. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  310. sizeof(my_destination_mgr));
  311. }
  312. dest = (my_dest_ptr) cinfo->dest;
  313. dest->pub.init_destination = JPEGInitDestination;
  314. dest->pub.empty_output_buffer = JPEGEmptyOutputBuffer;
  315. dest->pub.term_destination = JPEGTermDestination;
  316. dest->outfile = outfile;
  317. dest->size = size;
  318. }
  319. void roq::WriteLossless( void ) {
  320. word direct;
  321. uint directdw;
  322. if (!dataStuff) {
  323. InitRoQPatterns();
  324. dataStuff=true;
  325. }
  326. direct = RoQ_QUAD_JPEG;
  327. Write16Word( &direct, RoQFile);
  328. /* This struct contains the JPEG compression parameters and pointers to
  329. * working space (which is allocated as needed by the JPEG library).
  330. * It is possible to have several such structures, representing multiple
  331. * compression/decompression processes, in existence at once. We refer
  332. * to any one struct (and its associated working data) as a "JPEG object".
  333. */
  334. struct jpeg_compress_struct cinfo;
  335. /* This struct represents a JPEG error handler. It is declared separately
  336. * because applications often want to supply a specialized error handler
  337. * (see the second half of this file for an example). But here we just
  338. * take the easy way out and use the standard error handler, which will
  339. * print a message on stderr and call exit() if compression fails.
  340. * Note that this struct must live as long as the main JPEG parameter
  341. * struct, to avoid dangling-pointer problems.
  342. */
  343. struct jpeg_error_mgr jerr;
  344. /* More stuff */
  345. JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
  346. int row_stride; /* physical row width in image buffer */
  347. byte *out;
  348. /* Step 1: allocate and initialize JPEG compression object */
  349. /* We have to set up the error handler first, in case the initialization
  350. * step fails. (Unlikely, but it could happen if you are out of memory.)
  351. * This routine fills in the contents of struct jerr, and returns jerr's
  352. * address which we place into the link field in cinfo.
  353. */
  354. cinfo.err = jpeg_std_error(&jerr);
  355. /* Now we can initialize the JPEG compression object. */
  356. jpeg_create_compress(&cinfo);
  357. /* Step 2: specify data destination (eg, a file) */
  358. /* Note: steps 2 and 3 can be done in either order. */
  359. /* Here we use the library-supplied code to send compressed data to a
  360. * stdio stream. You can also write your own code to do something else.
  361. * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
  362. * requires it in order to write binary files.
  363. */
  364. out = (byte *)Mem_Alloc(image->pixelsWide()*image->pixelsHigh()*4);
  365. JPEGDest(&cinfo, out, image->pixelsWide()*image->pixelsHigh()*4);
  366. /* Step 3: set parameters for compression */
  367. /* First we supply a description of the input image.
  368. * Four fields of the cinfo struct must be filled in:
  369. */
  370. cinfo.image_width = image->pixelsWide(); /* image width and height, in pixels */
  371. cinfo.image_height = image->pixelsHigh();
  372. cinfo.input_components = 4; /* # of color components per pixel */
  373. cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
  374. /* Now use the library's routine to set default compression parameters.
  375. * (You must set at least cinfo.in_color_space before calling this,
  376. * since the defaults depend on the source color space.)
  377. */
  378. jpeg_set_defaults(&cinfo);
  379. /* Now you can set any non-default parameters you wish to.
  380. * Here we just illustrate the use of quality (quantization table) scaling:
  381. */
  382. jpeg_set_quality(&cinfo, paramFile->JpegQuality(), true /* limit to baseline-JPEG values */);
  383. /* Step 4: Start compressor */
  384. /* true ensures that we will write a complete interchange-JPEG file.
  385. * Pass true unless you are very sure of what you're doing.
  386. */
  387. JPEGStartCompress(&cinfo, true);
  388. /* Step 5: while (scan lines remain to be written) */
  389. /* jpeg_write_scanlines(...); */
  390. /* Here we use the library's state variable cinfo.next_scanline as the
  391. * loop counter, so that we don't have to keep track ourselves.
  392. * To keep things simple, we pass one scanline per call; you can pass
  393. * more if you wish, though.
  394. */
  395. row_stride = image->pixelsWide() * 4; /* JSAMPLEs per row in image_buffer */
  396. byte *pixbuf = image->bitmapData();
  397. while (cinfo.next_scanline < cinfo.image_height) {
  398. /* jpeg_write_scanlines expects an array of pointers to scanlines.
  399. * Here the array is only one element long, but you could pass
  400. * more than one scanline at a time if that's more convenient.
  401. */
  402. row_pointer[0] = &pixbuf[((cinfo.image_height-1)*row_stride)-cinfo.next_scanline * row_stride];
  403. (void) JPEGWriteScanlines(&cinfo, row_pointer, 1);
  404. }
  405. /* Step 6: Finish compression */
  406. jpeg_finish_compress(&cinfo);
  407. /* After finish_compress, we can close the output file. */
  408. directdw = hackSize;
  409. common->Printf("writeLossless: writing %d bytes to RoQ_QUAD_JPEG\n", hackSize);
  410. Write32Word( &directdw, RoQFile );
  411. direct = 0; // flags
  412. Write16Word( &direct, RoQFile );
  413. RoQFile->Write( out, hackSize );
  414. Mem_Free(out);
  415. /* Step 7: release JPEG compression object */
  416. /* This is an important step since it will release a good deal of memory. */
  417. jpeg_destroy_compress(&cinfo);
  418. /* And we're done! */
  419. encoder->SetPreviousImage( "first frame", image );
  420. }
  421. void roq::InitRoQFile( const char *RoQFilename )
  422. {
  423. word i;
  424. static int finit = 0;
  425. if (!finit) {
  426. finit++;
  427. common->Printf("initRoQFile: %s\n", RoQFilename);
  428. RoQFile = fileSystem->OpenFileWrite( RoQFilename );
  429. // chmod(RoQFilename, S_IREAD|S_IWRITE|S_ISUID|S_ISGID|0070|0007 );
  430. if ( !RoQFile ) {
  431. common->Error("Unable to open output file %s.\n", RoQFilename);
  432. }
  433. i = RoQ_ID;
  434. Write16Word( &i, RoQFile );
  435. i = 0xffff;
  436. Write16Word( &i, RoQFile );
  437. Write16Word( &i, RoQFile );
  438. // to retain exact file format write out 32 for new roq's
  439. // on loading this will be noted and converted to 1000 / 30
  440. // as with any new sound dump avi demos we need to playback
  441. // at the speed the sound engine dumps the audio
  442. i = 30; // framerate
  443. Write16Word( &i, RoQFile );
  444. }
  445. roqOutfile = RoQFilename;
  446. }
  447. void roq::InitRoQPatterns( void )
  448. {
  449. uint j;
  450. word direct;
  451. direct = RoQ_QUAD_INFO;
  452. Write16Word( &direct, RoQFile );
  453. j = 8;
  454. Write32Word( &j, RoQFile );
  455. common->Printf("initRoQPatterns: outputting %d bytes to RoQ_INFO\n", j);
  456. direct = image->hasAlpha();
  457. if (ParamNoAlpha() == true) direct = 0;
  458. Write16Word( &direct, RoQFile );
  459. direct = image->pixelsWide();
  460. Write16Word( &direct, RoQFile );
  461. direct = image->pixelsHigh();
  462. Write16Word( &direct, RoQFile );
  463. direct = 8;
  464. Write16Word( &direct, RoQFile );
  465. direct = 4;
  466. Write16Word( &direct, RoQFile );
  467. }
  468. void roq::CloseRoQFile( void )
  469. {
  470. common->Printf("closeRoQFile: closing RoQ file\n");
  471. fileSystem->CloseFile( RoQFile );
  472. }
  473. void roq::WriteHangFrame( void )
  474. {
  475. uint j;
  476. word direct;
  477. common->Printf("*******************************************************************\n");
  478. direct = RoQ_QUAD_HANG;
  479. Write16Word( &direct, RoQFile);
  480. j = 0;
  481. Write32Word( &j, RoQFile);
  482. direct = 0;
  483. Write16Word( &direct, RoQFile);
  484. }
  485. void roq::WriteCodeBookToStream( byte *codebook, int csize, word cflags )
  486. {
  487. uint j;
  488. word direct;
  489. if (!csize) {
  490. common->Printf("writeCodeBook: false VQ DATA!!!!\n");
  491. return;
  492. }
  493. direct = RoQ_QUAD_CODEBOOK;
  494. Write16Word( &direct, RoQFile);
  495. j = csize;
  496. Write32Word( &j, RoQFile);
  497. common->Printf("writeCodeBook: outputting %d bytes to RoQ_QUAD_CODEBOOK\n", j);
  498. direct = cflags;
  499. Write16Word( &direct, RoQFile);
  500. RoQFile->Write( codebook, j );
  501. }
  502. void roq::WriteCodeBook( byte *codebook )
  503. {
  504. memcpy( codes, codebook, 4096 );
  505. }
  506. void roq::WriteFrame( quadcel *pquad )
  507. {
  508. word action, direct;
  509. int onCCC, onAction, i, code;
  510. uint j;
  511. byte *cccList;
  512. bool *use2, *use4;
  513. int dx,dy,dxMean,dyMean,index2[256],index4[256], dimension;
  514. cccList = (byte *)Mem_Alloc( numQuadCels * 8); // maximum length
  515. use2 = (bool *)Mem_Alloc(256*sizeof(bool));
  516. use4 = (bool *)Mem_Alloc(256*sizeof(bool));
  517. for(i=0;i<256;i++) {
  518. use2[i] = false;
  519. use4[i] = false;
  520. }
  521. action = 0;
  522. j = onAction = 0;
  523. onCCC = 2; // onAction going to go at zero
  524. dxMean = encoder->MotMeanX();
  525. dyMean = encoder->MotMeanY();
  526. if (image->hasAlpha()) dimension = 10; else dimension = 6;
  527. for (i=0; i<numQuadCels; i++) {
  528. if ( pquad[i].size && pquad[i].size < 16 ) {
  529. switch( pquad[i].status ) {
  530. case SLD:
  531. use4[pquad[i].patten[0]] = true;
  532. use2[codes[dimension*256+(pquad[i].patten[0]*4)+0]] = true;
  533. use2[codes[dimension*256+(pquad[i].patten[0]*4)+1]] = true;
  534. use2[codes[dimension*256+(pquad[i].patten[0]*4)+2]] = true;
  535. use2[codes[dimension*256+(pquad[i].patten[0]*4)+3]] = true;
  536. break;
  537. case PAT:
  538. use4[pquad[i].patten[0]] = true;
  539. use2[codes[dimension*256+(pquad[i].patten[0]*4)+0]] = true;
  540. use2[codes[dimension*256+(pquad[i].patten[0]*4)+1]] = true;
  541. use2[codes[dimension*256+(pquad[i].patten[0]*4)+2]] = true;
  542. use2[codes[dimension*256+(pquad[i].patten[0]*4)+3]] = true;
  543. break;
  544. case CCC:
  545. use2[pquad[i].patten[1]] = true;
  546. use2[pquad[i].patten[2]] = true;
  547. use2[pquad[i].patten[3]] = true;
  548. use2[pquad[i].patten[4]] = true;
  549. }
  550. }
  551. }
  552. if (!dataStuff) {
  553. dataStuff=true;
  554. InitRoQPatterns();
  555. if (image->hasAlpha()) i = 3584; else i = 2560;
  556. WriteCodeBookToStream( codes, i, 0 );
  557. for(i=0;i<256;i++) {
  558. index2[i] = i;
  559. index4[i] = i;
  560. }
  561. } else {
  562. j = 0;
  563. for(i=0;i<256;i++) {
  564. if (use2[i]) {
  565. index2[i] = j;
  566. for(dx=0;dx<dimension;dx++) cccList[j*dimension+dx] = codes[i*dimension+dx];
  567. j++;
  568. }
  569. }
  570. code = j*dimension;
  571. direct = j;
  572. common->Printf("writeFrame: really used %d 2x2 cels\n", j);
  573. j = 0;
  574. for(i=0;i<256;i++) {
  575. if (use4[i]) {
  576. index4[i] = j;
  577. for(dx=0;dx<4;dx++) cccList[j*4+code+dx] = index2[codes[i*4+(dimension*256)+dx]];
  578. j++;
  579. }
  580. }
  581. code += j*4;
  582. direct = (direct<<8) + j;
  583. common->Printf("writeFrame: really used %d 4x4 cels\n", j);
  584. if (image->hasAlpha()) i = 3584; else i = 2560;
  585. if ( code == i || j == 256) {
  586. WriteCodeBookToStream( codes, i, 0 );
  587. } else {
  588. WriteCodeBookToStream( cccList, code, direct );
  589. }
  590. }
  591. action = 0;
  592. j = onAction = 0;
  593. for (i=0; i<numQuadCels; i++) {
  594. if ( pquad[i].size && pquad[i].size < 16 ) {
  595. code = -1;
  596. switch( pquad[i].status ) {
  597. case DEP:
  598. code = 3;
  599. break;
  600. case SLD:
  601. code = 2;
  602. cccList[onCCC++] = index4[pquad[i].patten[0]];
  603. break;
  604. case MOT:
  605. code = 0;
  606. break;
  607. case FCC:
  608. code = 1;
  609. dx = ((pquad[i].domain >> 8 )) - 128 - dxMean + 8;
  610. dy = ((pquad[i].domain & 0xff)) - 128 - dyMean + 8;
  611. if (dx>15 || dx<0 || dy>15 || dy<0 ) {
  612. common->Error("writeFrame: FCC error %d,%d mean %d,%d at %d,%d,%d rmse %f\n", dx,dy, dxMean, dyMean,pquad[i].xat,pquad[i].yat,pquad[i].size, pquad[i].snr[FCC] );
  613. }
  614. cccList[onCCC++] = (dx<<4)+dy;
  615. break;
  616. case PAT:
  617. code = 2;
  618. cccList[onCCC++] = index4[pquad[i].patten[0]];
  619. break;
  620. case CCC:
  621. code = 3;
  622. cccList[onCCC++] = index2[pquad[i].patten[1]];
  623. cccList[onCCC++] = index2[pquad[i].patten[2]];
  624. cccList[onCCC++] = index2[pquad[i].patten[3]];
  625. cccList[onCCC++] = index2[pquad[i].patten[4]];
  626. break;
  627. case DEAD:
  628. common->Error("dead cels in picture\n");
  629. break;
  630. }
  631. if (code == -1) {
  632. common->Error( "writeFrame: an error occurred writing the frame\n");
  633. }
  634. action = (action<<2)|code;
  635. j++;
  636. if (j == 8) {
  637. j = 0;
  638. cccList[onAction+0] = (action & 0xff);
  639. cccList[onAction+1] = ((action >> 8) & 0xff);
  640. onAction = onCCC;
  641. onCCC += 2;
  642. }
  643. }
  644. }
  645. if (j) {
  646. action <<= ((8-j)*2);
  647. cccList[onAction+0] = (action & 0xff);
  648. cccList[onAction+1] = ((action >> 8) & 0xff);
  649. }
  650. direct = RoQ_QUAD_VQ;
  651. Write16Word( &direct, RoQFile);
  652. j = onCCC;
  653. Write32Word( &j, RoQFile);
  654. direct = dyMean;
  655. direct &= 0xff;
  656. direct += (dxMean<<8); // flags
  657. Write16Word( &direct, RoQFile);
  658. common->Printf("writeFrame: outputting %d bytes to RoQ_QUAD_VQ\n", j);
  659. previousSize = j;
  660. RoQFile->Write( cccList, onCCC );
  661. Mem_Free( cccList );
  662. Mem_Free( use2 );
  663. Mem_Free( use4 );
  664. }
  665. //
  666. // load a frame, create a window (if neccesary) and display the frame
  667. //
  668. void roq::LoadAndDisplayImage( const char * filename )
  669. {
  670. if (image) delete image;
  671. common->Printf("loadAndDisplayImage: %s\n", filename);
  672. currentFile = filename;
  673. image = new NSBitmapImageRep( filename );
  674. numQuadCels = ((image->pixelsWide() & 0xfff0)*(image->pixelsHigh() & 0xfff0))/(MINSIZE*MINSIZE);
  675. numQuadCels += numQuadCels/4 + numQuadCels/16;
  676. // if (paramFile->deltaFrames] == true && cleared == false && [image isPlanar] == false) {
  677. // cleared = true;
  678. // imageData = [image data];
  679. // memset( imageData, 0, image->pixelsWide()*image->pixelsHigh()*[image samplesPerPixel]);
  680. // }
  681. if (!quietMode) common->Printf("loadAndDisplayImage: %dx%d\n", image->pixelsWide(), image->pixelsHigh());
  682. }
  683. void roq::MarkQuadx( int xat, int yat, int size, float cerror, int choice ) {
  684. }
  685. NSBitmapImageRep* roq::CurrentImage( void )
  686. {
  687. return image;
  688. }
  689. int roq::NumberOfFrames( void ) {
  690. return numberOfFrames;
  691. }
  692. void RoQFileEncode_f( const idCmdArgs &args ) {
  693. if ( args.Argc() != 2 ) {
  694. common->Printf( "Usage: roq <paramfile>\n" );
  695. return;
  696. }
  697. theRoQ = new roq;
  698. int startMsec = Sys_Milliseconds();
  699. theRoQ->EncodeStream( args.Argv( 1 ) );
  700. int stopMsec = Sys_Milliseconds();
  701. common->Printf( "total encoding time: %i second\n", ( stopMsec - startMsec ) / 1000 );
  702. }