| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id: hardware.cpp,v 1.22 2009-04-26 18:24:36 qbix79 Exp $ */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include "dosbox.h" |
| 25 | #include "hardware.h" |
| 26 | #include "setup.h" |
| 27 | #include "support.h" |
| 28 | #include "mem.h" |
| 29 | #include "mapper.h" |
| 30 | #include "pic.h" |
| 31 | #include "render.h" |
| 32 | #include "cross.h" |
| 33 | |
| 34 | #if (C_SSHOT) |
| 35 | #include <png.h> |
| 36 | #include "../libs/zmbv/zmbv.cpp" |
| 37 | #endif |
| 38 | |
| 39 | static std::string capturedir; |
| 40 | extern const char* RunningProgram; |
| 41 | Bitu CaptureState; |
| 42 | |
| 43 | #define WAVE_BUF 16*1024 |
| 44 | #define MIDI_BUF 4*1024 |
| 45 | #define AVI_HEADER_SIZE 500 |
| 46 | |
| 47 | static struct { |
| 48 | struct { |
| 49 | FILE * handle; |
| 50 | Bit16s buf[WAVE_BUF][2]; |
| 51 | Bitu used; |
| 52 | Bit32u length; |
| 53 | Bit32u freq; |
| 54 | } wave; |
| 55 | struct { |
| 56 | FILE * handle; |
| 57 | Bit8u buffer[MIDI_BUF]; |
| 58 | Bitu used,done; |
| 59 | Bit32u last; |
| 60 | } midi; |
| 61 | struct { |
| 62 | Bitu rowlen; |
| 63 | } image; |
| 64 | #if (C_SSHOT) |
| 65 | struct { |
| 66 | FILE *handle; |
| 67 | Bitu frames; |
| 68 | Bit16s audiobuf[WAVE_BUF][2]; |
| 69 | Bitu audioused; |
| 70 | Bitu audiorate; |
| 71 | Bitu audiowritten; |
| 72 | VideoCodec *codec; |
| 73 | Bitu width, height, bpp; |
| 74 | Bitu written; |
| 75 | float fps; |
| 76 | int bufSize; |
| 77 | void *buf; |
| 78 | Bit8u *index; |
| 79 | Bitu indexsize, indexused; |
| 80 | } video; |
| 81 | #endif |
| 82 | } capture; |
| 83 | |
| 84 | FILE * OpenCaptureFile(const char * type,const char * ext) { |
| 85 | if(capturedir.empty()) { |
| 86 | LOG_MSG("Please specify a capture directory"); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | Bitu last=0; |
| 91 | char file_start[16]; |
| 92 | dir_information * dir; |
| 93 | /* Find a filename to open */ |
| 94 | dir = open_directory(capturedir.c_str()); |
| 95 | if (!dir) { |
| 96 | //Try creating it first |
| 97 | Cross::CreateDir(capturedir); |
| 98 | dir=open_directory(capturedir.c_str()); |
| 99 | if(!dir) { |
| 100 | |
| 101 | LOG_MSG("Can't open dir %s for capturing %s",capturedir.c_str(),type); |
| 102 | return 0; |
| 103 | } |
| 104 | } |
| 105 | strcpy(file_start,RunningProgram); |
| 106 | lowcase(file_start); |
| 107 | strcat(file_start,"_"); |
| 108 | bool is_directory; |
| 109 | char tempname[CROSS_LEN]; |
| 110 | bool testRead = read_directory_first(dir, tempname, is_directory ); |
| 111 | for ( ; testRead; testRead = read_directory_next(dir, tempname, is_directory) ) { |
| 112 | char * test=strstr(tempname,ext); |
| 113 | if (!test || strlen(test)!=strlen(ext)) |
| 114 | continue; |
| 115 | *test=0; |
| 116 | if (strncasecmp(tempname,file_start,strlen(file_start))!=0) continue; |
| 117 | Bitu num=atoi(&tempname[strlen(file_start)]); |
| 118 | if (num>=last) last=num+1; |
| 119 | } |
| 120 | close_directory( dir ); |
| 121 | char file_name[CROSS_LEN]; |
| 122 | sprintf(file_name,"%s%c%s%03d%s",capturedir.c_str(),CROSS_FILESPLIT,file_start,last,ext); |
| 123 | /* Open the actual file */ |
| 124 | FILE * handle=fopen(file_name,"wb"); |
| 125 | if (handle) { |
| 126 | LOG_MSG("Capturing %s to %s",type,file_name); |
| 127 | } else { |
| 128 | LOG_MSG("Failed to open %s for capturing %s",file_name,type); |
| 129 | } |
| 130 | return handle; |
| 131 | } |
| 132 | |
| 133 | #if (C_SSHOT) |
| 134 | static void CAPTURE_AddAviChunk(const char * tag, Bit32u size, void * data, Bit32u flags) { |
| 135 | Bit8u chunk[8];Bit8u *index;Bit32u pos, writesize; |
| 136 | |
| 137 | chunk[0] = tag[0];chunk[1] = tag[1];chunk[2] = tag[2];chunk[3] = tag[3]; |
| 138 | host_writed(&chunk[4], size); |
| 139 | /* Write the actual data */ |
| 140 | fwrite(chunk,1,8,capture.video.handle); |
| 141 | writesize = (size+1)&~1; |
| 142 | fwrite(data,1,writesize,capture.video.handle); |
| 143 | pos = capture.video.written + 4; |
| 144 | capture.video.written += writesize + 8; |
| 145 | if (capture.video.indexused + 16 >= capture.video.indexsize ) { |
| 146 | capture.video.index = (Bit8u*)realloc( capture.video.index, capture.video.indexsize + 16 * 4096); |
| 147 | if (!capture.video.index) |
| 148 | E_Exit("Ran out of memory during AVI capturing"); |
| 149 | capture.video.indexsize += 16*4096; |
| 150 | } |
| 151 | index = capture.video.index+capture.video.indexused; |
| 152 | capture.video.indexused += 16; |
| 153 | index[0] = tag[0]; |
| 154 | index[1] = tag[1]; |
| 155 | index[2] = tag[2]; |
| 156 | index[3] = tag[3]; |
| 157 | host_writed(index+4, flags); |
| 158 | host_writed(index+8, pos); |
| 159 | host_writed(index+12, size); |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | #if (C_SSHOT) |
| 164 | static void CAPTURE_VideoEvent(bool pressed) { |
| 165 | if (!pressed) |
| 166 | return; |
| 167 | if (CaptureState & CAPTURE_VIDEO) { |
| 168 | /* Close the video */ |
| 169 | CaptureState &= ~CAPTURE_VIDEO; |
| 170 | LOG_MSG("Stopped capturing video."); |
| 171 | |
| 172 | Bit8u avi_header[AVI_HEADER_SIZE]; |
| 173 | Bitu main_list; |
| 174 | Bitu header_pos=0; |
| 175 | #define AVIOUT4(_S_) memcpy(&avi_header[header_pos],_S_,4);header_pos+=4; |
| 176 | #define AVIOUTw(_S_) host_writew(&avi_header[header_pos], _S_);header_pos+=2; |
| 177 | #define AVIOUTd(_S_) host_writed(&avi_header[header_pos], _S_);header_pos+=4; |
| 178 | /* Try and write an avi header */ |
| 179 | AVIOUT4("RIFF"); // Riff header |
| 180 | AVIOUTd(AVI_HEADER_SIZE + capture.video.written - 8 + capture.video.indexused); |
| 181 | AVIOUT4("AVI "); |
| 182 | AVIOUT4("LIST"); // List header |
| 183 | main_list = header_pos; |
| 184 | AVIOUTd(0); // TODO size of list |
| 185 | AVIOUT4("hdrl"); |
| 186 | |
| 187 | AVIOUT4("avih"); |
| 188 | AVIOUTd(56); /* # of bytes to follow */ |
| 189 | AVIOUTd((Bit32u)(1000000 / capture.video.fps)); /* Microseconds per frame */ |
| 190 | AVIOUTd(0); |
| 191 | AVIOUTd(0); /* PaddingGranularity (whatever that might be) */ |
| 192 | AVIOUTd(0x110); /* Flags,0x10 has index, 0x100 interleaved */ |
| 193 | AVIOUTd(capture.video.frames); /* TotalFrames */ |
| 194 | AVIOUTd(0); /* InitialFrames */ |
| 195 | AVIOUTd(2); /* Stream count */ |
| 196 | AVIOUTd(0); /* SuggestedBufferSize */ |
| 197 | AVIOUTd(capture.video.width); /* Width */ |
| 198 | AVIOUTd(capture.video.height); /* Height */ |
| 199 | AVIOUTd(0); /* TimeScale: Unit used to measure time */ |
| 200 | AVIOUTd(0); /* DataRate: Data rate of playback */ |
| 201 | AVIOUTd(0); /* StartTime: Starting time of AVI data */ |
| 202 | AVIOUTd(0); /* DataLength: Size of AVI data chunk */ |
| 203 | |
| 204 | /* Video stream list */ |
| 205 | AVIOUT4("LIST"); |
| 206 | AVIOUTd(4 + 8 + 56 + 8 + 40); /* Size of the list */ |
| 207 | AVIOUT4("strl"); |
| 208 | /* video stream header */ |
| 209 | AVIOUT4("strh"); |
| 210 | AVIOUTd(56); /* # of bytes to follow */ |
| 211 | AVIOUT4("vids"); /* Type */ |
| 212 | AVIOUT4(CODEC_4CC); /* Handler */ |
| 213 | AVIOUTd(0); /* Flags */ |
| 214 | AVIOUTd(0); /* Reserved, MS says: wPriority, wLanguage */ |
| 215 | AVIOUTd(0); /* InitialFrames */ |
| 216 | AVIOUTd(1000000); /* Scale */ |
| 217 | AVIOUTd((Bit32u)(1000000 * capture.video.fps)); /* Rate: Rate/Scale == samples/second */ |
| 218 | AVIOUTd(0); /* Start */ |
| 219 | AVIOUTd(capture.video.frames); /* Length */ |
| 220 | AVIOUTd(0); /* SuggestedBufferSize */ |
| 221 | AVIOUTd(~0); /* Quality */ |
| 222 | AVIOUTd(0); /* SampleSize */ |
| 223 | AVIOUTd(0); /* Frame */ |
| 224 | AVIOUTd(0); /* Frame */ |
| 225 | /* The video stream format */ |
| 226 | AVIOUT4("strf"); |
| 227 | AVIOUTd(40); /* # of bytes to follow */ |
| 228 | AVIOUTd(40); /* Size */ |
| 229 | AVIOUTd(capture.video.width); /* Width */ |
| 230 | AVIOUTd(capture.video.height); /* Height */ |
| 231 | // OUTSHRT(1); OUTSHRT(24); /* Planes, Count */ |
| 232 | AVIOUTd(0); |
| 233 | AVIOUT4(CODEC_4CC); /* Compression */ |
| 234 | AVIOUTd(capture.video.width * capture.video.height*4); /* SizeImage (in bytes?) */ |
| 235 | AVIOUTd(0); /* XPelsPerMeter */ |
| 236 | AVIOUTd(0); /* YPelsPerMeter */ |
| 237 | AVIOUTd(0); /* ClrUsed: Number of colors used */ |
| 238 | AVIOUTd(0); /* ClrImportant: Number of colors important */ |
| 239 | |
| 240 | /* Audio stream list */ |
| 241 | AVIOUT4("LIST"); |
| 242 | AVIOUTd(4 + 8 + 56 + 8 + 16); /* Length of list in bytes */ |
| 243 | AVIOUT4("strl"); |
| 244 | /* The audio stream header */ |
| 245 | AVIOUT4("strh"); |
| 246 | AVIOUTd(56); /* # of bytes to follow */ |
| 247 | AVIOUT4("auds"); |
| 248 | AVIOUTd(0); /* Format (Optionally) */ |
| 249 | AVIOUTd(0); /* Flags */ |
| 250 | AVIOUTd(0); /* Reserved, MS says: wPriority, wLanguage */ |
| 251 | AVIOUTd(0); /* InitialFrames */ |
| 252 | AVIOUTd(4); /* Scale */ |
| 253 | AVIOUTd(capture.video.audiorate*4); /* Rate, actual rate is scale/rate */ |
| 254 | AVIOUTd(0); /* Start */ |
| 255 | if (!capture.video.audiorate) |
| 256 | capture.video.audiorate = 1; |
| 257 | AVIOUTd(capture.video.audiowritten/4); /* Length */ |
| 258 | AVIOUTd(0); /* SuggestedBufferSize */ |
| 259 | AVIOUTd(~0); /* Quality */ |
| 260 | AVIOUTd(4); /* SampleSize */ |
| 261 | AVIOUTd(0); /* Frame */ |
| 262 | AVIOUTd(0); /* Frame */ |
| 263 | /* The audio stream format */ |
| 264 | AVIOUT4("strf"); |
| 265 | AVIOUTd(16); /* # of bytes to follow */ |
| 266 | AVIOUTw(1); /* Format, WAVE_ZMBV_FORMAT_PCM */ |
| 267 | AVIOUTw(2); /* Number of channels */ |
| 268 | AVIOUTd(capture.video.audiorate); /* SamplesPerSec */ |
| 269 | AVIOUTd(capture.video.audiorate*4); /* AvgBytesPerSec*/ |
| 270 | AVIOUTw(4); /* BlockAlign */ |
| 271 | AVIOUTw(16); /* BitsPerSample */ |
| 272 | int nmain = header_pos - main_list - 4; |
| 273 | /* Finish stream list, i.e. put number of bytes in the list to proper pos */ |
| 274 | |
| 275 | int njunk = AVI_HEADER_SIZE - 8 - 12 - header_pos; |
| 276 | AVIOUT4("JUNK"); |
| 277 | AVIOUTd(njunk); |
| 278 | /* Fix the size of the main list */ |
| 279 | header_pos = main_list; |
| 280 | AVIOUTd(nmain); |
| 281 | header_pos = AVI_HEADER_SIZE - 12; |
| 282 | AVIOUT4("LIST"); |
| 283 | AVIOUTd(capture.video.written+4); /* Length of list in bytes */ |
| 284 | AVIOUT4("movi"); |
| 285 | /* First add the index table to the end */ |
| 286 | memcpy(capture.video.index, "idx1", 4); |
| 287 | host_writed( capture.video.index+4, capture.video.indexused - 8 ); |
| 288 | fwrite( capture.video.index, 1, capture.video.indexused, capture.video.handle); |
| 289 | fseek(capture.video.handle, 0, SEEK_SET); |
| 290 | fwrite(&avi_header, 1, AVI_HEADER_SIZE, capture.video.handle); |
| 291 | fclose( capture.video.handle ); |
| 292 | free( capture.video.index ); |
| 293 | free( capture.video.buf ); |
| 294 | delete capture.video.codec; |
| 295 | capture.video.handle = 0; |
| 296 | } else { |
| 297 | CaptureState |= CAPTURE_VIDEO; |
| 298 | } |
| 299 | } |
| 300 | #endif |
| 301 | |
| 302 | void CAPTURE_AddImage(Bitu width, Bitu height, Bitu bpp, Bitu pitch, Bitu flags, float fps, Bit8u * data, Bit8u * pal) { |
| 303 | #if (C_SSHOT) |
| 304 | Bitu i; |
| 305 | Bit8u doubleRow[SCALER_MAXWIDTH*4]; |
| 306 | Bitu countWidth = width; |
| 307 | |
| 308 | if (flags & CAPTURE_FLAG_DBLH) |
| 309 | height *= 2; |
| 310 | if (flags & CAPTURE_FLAG_DBLW) |
| 311 | width *= 2; |
| 312 | |
| 313 | if (height > SCALER_MAXHEIGHT) |
| 314 | return; |
| 315 | if (width > SCALER_MAXWIDTH) |
| 316 | return; |
| 317 | |
| 318 | if (CaptureState & CAPTURE_IMAGE) { |
| 319 | png_structp png_ptr; |
| 320 | png_infop info_ptr; |
| 321 | png_color palette[256]; |
| 322 | |
| 323 | CaptureState &= ~CAPTURE_IMAGE; |
| 324 | /* Open the actual file */ |
| 325 | FILE * fp=OpenCaptureFile("Screenshot",".png"); |
| 326 | if (!fp) goto skip_shot; |
| 327 | /* First try to alloacte the png structures */ |
| 328 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,NULL, NULL); |
| 329 | if (!png_ptr) goto skip_shot; |
| 330 | info_ptr = png_create_info_struct(png_ptr); |
| 331 | if (!info_ptr) { |
| 332 | png_destroy_write_struct(&png_ptr,(png_infopp)NULL); |
| 333 | goto skip_shot; |
| 334 | } |
| 335 | |
| 336 | /* Finalize the initing of png library */ |
| 337 | png_init_io(png_ptr, fp); |
| 338 | png_set_compression_level(png_ptr,Z_BEST_COMPRESSION); |
| 339 | |
| 340 | /* set other zlib parameters */ |
| 341 | png_set_compression_mem_level(png_ptr, 8); |
| 342 | png_set_compression_strategy(png_ptr,Z_DEFAULT_STRATEGY); |
| 343 | png_set_compression_window_bits(png_ptr, 15); |
| 344 | png_set_compression_method(png_ptr, 8); |
| 345 | png_set_compression_buffer_size(png_ptr, 8192); |
| 346 | |
| 347 | if (bpp==8) { |
| 348 | png_set_IHDR(png_ptr, info_ptr, width, height, |
| 349 | 8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, |
| 350 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 351 | for (i=0;i<256;i++) { |
| 352 | palette[i].red=pal[i*4+0]; |
| 353 | palette[i].green=pal[i*4+1]; |
| 354 | palette[i].blue=pal[i*4+2]; |
| 355 | } |
| 356 | png_set_PLTE(png_ptr, info_ptr, palette,256); |
| 357 | } else { |
| 358 | png_set_bgr( png_ptr ); |
| 359 | png_set_IHDR(png_ptr, info_ptr, width, height, |
| 360 | 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, |
| 361 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 362 | } |
| 363 | png_write_info(png_ptr, info_ptr); |
| 364 | for (i=0;i<height;i++) { |
| 365 | void *rowPointer; |
| 366 | void *srcLine; |
| 367 | if (flags & CAPTURE_FLAG_DBLH) |
| 368 | srcLine=(data+(i >> 1)*pitch); |
| 369 | else |
| 370 | srcLine=(data+(i >> 0)*pitch); |
| 371 | rowPointer=srcLine; |
| 372 | switch (bpp) { |
| 373 | case 8: |
| 374 | if (flags & CAPTURE_FLAG_DBLW) { |
| 375 | for (Bitu x=0;x<countWidth;x++) |
| 376 | doubleRow[x*2+0] = |
| 377 | doubleRow[x*2+1] = ((Bit8u *)srcLine)[x]; |
| 378 | rowPointer = doubleRow; |
| 379 | } |
| 380 | break; |
| 381 | case 15: |
| 382 | if (flags & CAPTURE_FLAG_DBLW) { |
| 383 | for (Bitu x=0;x<countWidth;x++) { |
| 384 | Bitu pixel = ((Bit16u *)srcLine)[x]; |
| 385 | doubleRow[x*6+0] = doubleRow[x*6+3] = ((pixel& 0x001f) * 0x21) >> 2; |
| 386 | doubleRow[x*6+1] = doubleRow[x*6+4] = ((pixel& 0x03e0) * 0x21) >> 7; |
| 387 | doubleRow[x*6+2] = doubleRow[x*6+5] = ((pixel& 0x7c00) * 0x21) >> 12; |
| 388 | } |
| 389 | } else { |
| 390 | for (Bitu x=0;x<countWidth;x++) { |
| 391 | Bitu pixel = ((Bit16u *)srcLine)[x]; |
| 392 | doubleRow[x*3+0] = ((pixel& 0x001f) * 0x21) >> 2; |
| 393 | doubleRow[x*3+1] = ((pixel& 0x03e0) * 0x21) >> 7; |
| 394 | doubleRow[x*3+2] = ((pixel& 0x7c00) * 0x21) >> 12; |
| 395 | } |
| 396 | } |
| 397 | rowPointer = doubleRow; |
| 398 | break; |
| 399 | case 16: |
| 400 | if (flags & CAPTURE_FLAG_DBLW) { |
| 401 | for (Bitu x=0;x<countWidth;x++) { |
| 402 | Bitu pixel = ((Bit16u *)srcLine)[x]; |
| 403 | doubleRow[x*6+0] = doubleRow[x*6+3] = ((pixel& 0x001f) * 0x21) >> 2; |
| 404 | doubleRow[x*6+1] = doubleRow[x*6+4] = ((pixel& 0x07e0) * 0x41) >> 9; |
| 405 | doubleRow[x*6+2] = doubleRow[x*6+5] = ((pixel& 0xf800) * 0x21) >> 13; |
| 406 | } |
| 407 | } else { |
| 408 | for (Bitu x=0;x<countWidth;x++) { |
| 409 | Bitu pixel = ((Bit16u *)srcLine)[x]; |
| 410 | doubleRow[x*3+0] = ((pixel& 0x001f) * 0x21) >> 2; |
| 411 | doubleRow[x*3+1] = ((pixel& 0x07e0) * 0x41) >> 9; |
| 412 | doubleRow[x*3+2] = ((pixel& 0xf800) * 0x21) >> 13; |
| 413 | } |
| 414 | } |
| 415 | rowPointer = doubleRow; |
| 416 | break; |
| 417 | case 32: |
| 418 | if (flags & CAPTURE_FLAG_DBLW) { |
| 419 | for (Bitu x=0;x<countWidth;x++) { |
| 420 | doubleRow[x*6+0] = doubleRow[x*6+3] = ((Bit8u *)srcLine)[x*4+0]; |
| 421 | doubleRow[x*6+1] = doubleRow[x*6+4] = ((Bit8u *)srcLine)[x*4+1]; |
| 422 | doubleRow[x*6+2] = doubleRow[x*6+5] = ((Bit8u *)srcLine)[x*4+2]; |
| 423 | } |
| 424 | } else { |
| 425 | for (Bitu x=0;x<countWidth;x++) { |
| 426 | doubleRow[x*3+0] = ((Bit8u *)srcLine)[x*4+0]; |
| 427 | doubleRow[x*3+1] = ((Bit8u *)srcLine)[x*4+1]; |
| 428 | doubleRow[x*3+2] = ((Bit8u *)srcLine)[x*4+2]; |
| 429 | } |
| 430 | } |
| 431 | rowPointer = doubleRow; |
| 432 | break; |
| 433 | } |
| 434 | png_write_row(png_ptr, (png_bytep)rowPointer); |
| 435 | } |
| 436 | /* Finish writing */ |
| 437 | png_write_end(png_ptr, 0); |
| 438 | /*Destroy PNG structs*/ |
| 439 | png_destroy_write_struct(&png_ptr, &info_ptr); |
| 440 | /*close file*/ |
| 441 | fclose(fp); |
| 442 | } |
| 443 | skip_shot: |
| 444 | if (CaptureState & CAPTURE_VIDEO) { |
| 445 | zmbv_format_t format; |
| 446 | /* Disable capturing if any of the test fails */ |
| 447 | if (capture.video.handle && ( |
| 448 | capture.video.width != width || |
| 449 | capture.video.height != height || |
| 450 | capture.video.bpp != bpp || |
| 451 | capture.video.fps != fps)) |
| 452 | { |
| 453 | CAPTURE_VideoEvent(true); |
| 454 | } |
| 455 | CaptureState &= ~CAPTURE_VIDEO; |
| 456 | switch (bpp) { |
| 457 | case 8:format = ZMBV_FORMAT_8BPP;break; |
| 458 | case 15:format = ZMBV_FORMAT_15BPP;break; |
| 459 | case 16:format = ZMBV_FORMAT_16BPP;break; |
| 460 | case 32:format = ZMBV_FORMAT_32BPP;break; |
| 461 | default: |
| 462 | goto skip_video; |
| 463 | } |
| 464 | if (!capture.video.handle) { |
| 465 | capture.video.handle = OpenCaptureFile("Video",".avi"); |
| 466 | if (!capture.video.handle) |
| 467 | goto skip_video; |
| 468 | capture.video.codec = new VideoCodec(); |
| 469 | if (!capture.video.codec) |
| 470 | goto skip_video; |
| 471 | if (!capture.video.codec->SetupCompress( width, height)) |
| 472 | goto skip_video; |
| 473 | capture.video.bufSize = capture.video.codec->NeededSize(width, height, format); |
| 474 | capture.video.buf = malloc( capture.video.bufSize ); |
| 475 | if (!capture.video.buf) |
| 476 | goto skip_video; |
| 477 | capture.video.index = (Bit8u*)malloc( 16*4096 ); |
| 478 | if (!capture.video.buf) |
| 479 | goto skip_video; |
| 480 | capture.video.indexsize = 16*4096; |
| 481 | capture.video.indexused = 8; |
| 482 | |
| 483 | capture.video.width = width; |
| 484 | capture.video.height = height; |
| 485 | capture.video.bpp = bpp; |
| 486 | capture.video.fps = fps; |
| 487 | for (i=0;i<AVI_HEADER_SIZE;i++) |
| 488 | fputc(0,capture.video.handle); |
| 489 | capture.video.frames = 0; |
| 490 | capture.video.written = 0; |
| 491 | capture.video.audioused = 0; |
| 492 | capture.video.audiowritten = 0; |
| 493 | } |
| 494 | int codecFlags; |
| 495 | if (capture.video.frames % 300 == 0) |
| 496 | codecFlags = 1; |
| 497 | else codecFlags = 0; |
| 498 | if (!capture.video.codec->PrepareCompressFrame( codecFlags, format, (char *)pal, capture.video.buf, capture.video.bufSize)) |
| 499 | goto skip_video; |
| 500 | |
| 501 | for (i=0;i<height;i++) { |
| 502 | void * rowPointer; |
| 503 | if (flags & CAPTURE_FLAG_DBLW) { |
| 504 | void *srcLine; |
| 505 | Bitu x; |
| 506 | Bitu countWidth = width >> 1; |
| 507 | if (flags & CAPTURE_FLAG_DBLH) |
| 508 | srcLine=(data+(i >> 1)*pitch); |
| 509 | else |
| 510 | srcLine=(data+(i >> 0)*pitch); |
| 511 | switch ( bpp) { |
| 512 | case 8: |
| 513 | for (x=0;x<countWidth;x++) |
| 514 | ((Bit8u *)doubleRow)[x*2+0] = |
| 515 | ((Bit8u *)doubleRow)[x*2+1] = ((Bit8u *)srcLine)[x]; |
| 516 | break; |
| 517 | case 15: |
| 518 | case 16: |
| 519 | for (x=0;x<countWidth;x++) |
| 520 | ((Bit16u *)doubleRow)[x*2+0] = |
| 521 | ((Bit16u *)doubleRow)[x*2+1] = ((Bit16u *)srcLine)[x]; |
| 522 | break; |
| 523 | case 32: |
| 524 | for (x=0;x<countWidth;x++) |
| 525 | ((Bit32u *)doubleRow)[x*2+0] = |
| 526 | ((Bit32u *)doubleRow)[x*2+1] = ((Bit32u *)srcLine)[x]; |
| 527 | break; |
| 528 | } |
| 529 | rowPointer=doubleRow; |
| 530 | } else { |
| 531 | if (flags & CAPTURE_FLAG_DBLH) |
| 532 | rowPointer=(data+(i >> 1)*pitch); |
| 533 | else |
| 534 | rowPointer=(data+(i >> 0)*pitch); |
| 535 | } |
| 536 | capture.video.codec->CompressLines( 1, &rowPointer ); |
| 537 | } |
| 538 | int written = capture.video.codec->FinishCompressFrame(); |
| 539 | if (written < 0) |
| 540 | goto skip_video; |
| 541 | CAPTURE_AddAviChunk( "00dc", written, capture.video.buf, codecFlags & 1 ? 0x10 : 0x0); |
| 542 | capture.video.frames++; |
| 543 | // LOG_MSG("Frame %d video %d audio %d",capture.video.frames, written, capture.video.audioused *4 ); |
| 544 | if ( capture.video.audioused ) { |
| 545 | CAPTURE_AddAviChunk( "01wb", capture.video.audioused * 4, capture.video.audiobuf, 0); |
| 546 | capture.video.audiowritten = capture.video.audioused*4; |
| 547 | capture.video.audioused = 0; |
| 548 | } |
| 549 | |
| 550 | /* Everything went okay, set flag again for next frame */ |
| 551 | CaptureState |= CAPTURE_VIDEO; |
| 552 | } |
| 553 | skip_video: |
| 554 | #endif |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | |
| 559 | #if (C_SSHOT) |
| 560 | static void CAPTURE_ScreenShotEvent(bool pressed) { |
| 561 | if (!pressed) |
| 562 | return; |
| 563 | CaptureState |= CAPTURE_IMAGE; |
| 564 | } |
| 565 | #endif |
| 566 | |
| 567 | |
| 568 | /* WAV capturing */ |
| 569 | static Bit8u wavheader[]={ |
| 570 | 'R','I','F','F', 0x0,0x0,0x0,0x0, /* Bit32u Riff Chunk ID / Bit32u riff size */ |
| 571 | 'W','A','V','E', 'f','m','t',' ', /* Bit32u Riff Format / Bit32u fmt chunk id */ |
| 572 | 0x10,0x0,0x0,0x0, 0x1,0x0,0x2,0x0, /* Bit32u fmt size / Bit16u encoding/ Bit16u channels */ |
| 573 | 0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0, /* Bit32u freq / Bit32u byterate */ |
| 574 | 0x4,0x0,0x10,0x0, 'd','a','t','a', /* Bit16u byte-block / Bit16u bits / Bit16u data chunk id */ |
| 575 | 0x0,0x0,0x0,0x0, /* Bit32u data size */ |
| 576 | }; |
| 577 | |
| 578 | void CAPTURE_AddWave(Bit32u freq, Bit32u len, Bit16s * data) { |
| 579 | #if (C_SSHOT) |
| 580 | if (CaptureState & CAPTURE_VIDEO) { |
| 581 | Bitu left = WAVE_BUF - capture.video.audioused; |
| 582 | if (left > len) |
| 583 | left = len; |
| 584 | memcpy( &capture.video.audiobuf[capture.video.audioused], data, left*4); |
| 585 | capture.video.audioused += left; |
| 586 | capture.video.audiorate = freq; |
| 587 | } |
| 588 | #endif |
| 589 | if (CaptureState & CAPTURE_WAVE) { |
| 590 | if (!capture.wave.handle) { |
| 591 | capture.wave.handle=OpenCaptureFile("Wave Output",".wav"); |
| 592 | if (!capture.wave.handle) { |
| 593 | CaptureState &= ~CAPTURE_WAVE; |
| 594 | return; |
| 595 | } |
| 596 | capture.wave.length = 0; |
| 597 | capture.wave.used = 0; |
| 598 | capture.wave.freq = freq; |
| 599 | fwrite(wavheader,1,sizeof(wavheader),capture.wave.handle); |
| 600 | } |
| 601 | Bit16s * read = data; |
| 602 | while (len > 0 ) { |
| 603 | Bitu left = WAVE_BUF - capture.wave.used; |
| 604 | if (!left) { |
| 605 | fwrite(capture.wave.buf,1,4*WAVE_BUF,capture.wave.handle); |
| 606 | capture.wave.length += 4*WAVE_BUF; |
| 607 | capture.wave.used = 0; |
| 608 | left = WAVE_BUF; |
| 609 | } |
| 610 | if (left > len) |
| 611 | left = len; |
| 612 | memcpy( &capture.wave.buf[capture.wave.used], read, left*4); |
| 613 | capture.wave.used += left; |
| 614 | read += left*2; |
| 615 | len -= left; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | static void CAPTURE_WaveEvent(bool pressed) { |
| 620 | if (!pressed) |
| 621 | return; |
| 622 | /* Check for previously opened wave file */ |
| 623 | if (capture.wave.handle) { |
| 624 | LOG_MSG("Stopped capturing wave output."); |
| 625 | /* Write last piece of audio in buffer */ |
| 626 | fwrite(capture.wave.buf,1,capture.wave.used*4,capture.wave.handle); |
| 627 | capture.wave.length+=capture.wave.used*4; |
| 628 | /* Fill in the header with useful information */ |
| 629 | host_writed(&wavheader[0x04],capture.wave.length+sizeof(wavheader)-8); |
| 630 | host_writed(&wavheader[0x18],capture.wave.freq); |
| 631 | host_writed(&wavheader[0x1C],capture.wave.freq*4); |
| 632 | host_writed(&wavheader[0x28],capture.wave.length); |
| 633 | |
| 634 | fseek(capture.wave.handle,0,0); |
| 635 | fwrite(wavheader,1,sizeof(wavheader),capture.wave.handle); |
| 636 | fclose(capture.wave.handle); |
| 637 | capture.wave.handle=0; |
| 638 | CaptureState |= CAPTURE_WAVE; |
| 639 | } |
| 640 | CaptureState ^= CAPTURE_WAVE; |
| 641 | } |
| 642 | |
| 643 | /* MIDI capturing */ |
| 644 | |
| 645 | static Bit8u midi_header[]={ |
| 646 | 'M','T','h','d', /* Bit32u, Header Chunk */ |
| 647 | 0x0,0x0,0x0,0x6, /* Bit32u, Chunk Length */ |
| 648 | 0x0,0x0, /* Bit16u, Format, 0=single track */ |
| 649 | 0x0,0x1, /* Bit16u, Track Count, 1 track */ |
| 650 | 0x01,0xf4, /* Bit16u, Timing, 2 beats/second with 500 frames */ |
| 651 | 'M','T','r','k', /* Bit32u, Track Chunk */ |
| 652 | 0x0,0x0,0x0,0x0, /* Bit32u, Chunk Length */ |
| 653 | //Track data |
| 654 | }; |
| 655 | |
| 656 | |
| 657 | static void RawMidiAdd(Bit8u data) { |
| 658 | capture.midi.buffer[capture.midi.used++]=data; |
| 659 | if (capture.midi.used >= MIDI_BUF ) { |
| 660 | capture.midi.done += capture.midi.used; |
| 661 | fwrite(capture.midi.buffer,1,MIDI_BUF,capture.midi.handle); |
| 662 | capture.midi.used = 0; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | static void RawMidiAddNumber(Bit32u val) { |
| 667 | if (val & 0xfe00000) RawMidiAdd((Bit8u)(0x80|((val >> 21) & 0x7f))); |
| 668 | if (val & 0xfffc000) RawMidiAdd((Bit8u)(0x80|((val >> 14) & 0x7f))); |
| 669 | if (val & 0xfffff80) RawMidiAdd((Bit8u)(0x80|((val >> 7) & 0x7f))); |
| 670 | RawMidiAdd((Bit8u)(val & 0x7f)); |
| 671 | } |
| 672 | |
| 673 | void CAPTURE_AddMidi(bool sysex, Bitu len, Bit8u * data) { |
| 674 | if (!capture.midi.handle) { |
| 675 | capture.midi.handle=OpenCaptureFile("Raw Midi",".mid"); |
| 676 | if (!capture.midi.handle) { |
| 677 | return; |
| 678 | } |
| 679 | fwrite(midi_header,1,sizeof(midi_header),capture.midi.handle); |
| 680 | capture.midi.last=PIC_Ticks; |
| 681 | } |
| 682 | Bit32u delta=PIC_Ticks-capture.midi.last; |
| 683 | capture.midi.last=PIC_Ticks; |
| 684 | RawMidiAddNumber(delta); |
| 685 | if (sysex) { |
| 686 | RawMidiAdd( 0xf0 ); |
| 687 | RawMidiAddNumber( len ); |
| 688 | } |
| 689 | for (Bitu i=0;i<len;i++) |
| 690 | RawMidiAdd(data[i]); |
| 691 | } |
| 692 | |
| 693 | static void CAPTURE_MidiEvent(bool pressed) { |
| 694 | if (!pressed) |
| 695 | return; |
| 696 | /* Check for previously opened wave file */ |
| 697 | if (capture.midi.handle) { |
| 698 | LOG_MSG("Stopping raw midi saving and finalizing file."); |
| 699 | //Delta time |
| 700 | RawMidiAdd(0x00); |
| 701 | //End of track event |
| 702 | RawMidiAdd(0xff); |
| 703 | RawMidiAdd(0x2F); |
| 704 | RawMidiAdd(0x00); |
| 705 | /* clear out the final data in the buffer if any */ |
| 706 | fwrite(capture.midi.buffer,1,capture.midi.used,capture.midi.handle); |
| 707 | capture.midi.done+=capture.midi.used; |
| 708 | fseek(capture.midi.handle,18, SEEK_SET); |
| 709 | Bit8u size[4]; |
| 710 | size[0]=(Bit8u)(capture.midi.done >> 24); |
| 711 | size[1]=(Bit8u)(capture.midi.done >> 16); |
| 712 | size[2]=(Bit8u)(capture.midi.done >> 8); |
| 713 | size[3]=(Bit8u)(capture.midi.done >> 0); |
| 714 | fwrite(&size,1,4,capture.midi.handle); |
| 715 | fclose(capture.midi.handle); |
| 716 | capture.midi.handle=0; |
| 717 | CaptureState &= ~CAPTURE_MIDI; |
| 718 | return; |
| 719 | } |
| 720 | CaptureState ^= CAPTURE_MIDI; |
| 721 | if (CaptureState & CAPTURE_MIDI) { |
| 722 | LOG_MSG("Preparing for raw midi capture, will start with first data."); |
| 723 | capture.midi.used=0; |
| 724 | capture.midi.done=0; |
| 725 | capture.midi.handle=0; |
| 726 | } else { |
| 727 | LOG_MSG("Stopped capturing raw midi before any data arrived."); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | class HARDWARE:public Module_base{ |
| 732 | public: |
| 733 | HARDWARE(Section* configuration):Module_base(configuration){ |
| 734 | Section_prop * section = static_cast<Section_prop *>(configuration); |
| 735 | Prop_path* proppath= section->Get_path("captures"); |
| 736 | capturedir = proppath->realpath; |
| 737 | CaptureState = 0; |
| 738 | MAPPER_AddHandler(CAPTURE_WaveEvent,MK_f6,MMOD1,"recwave","Rec Wave"); |
| 739 | MAPPER_AddHandler(CAPTURE_MidiEvent,MK_f8,MMOD1|MMOD2,"caprawmidi","Cap MIDI"); |
| 740 | #if (C_SSHOT) |
| 741 | MAPPER_AddHandler(CAPTURE_ScreenShotEvent,MK_f5,MMOD1,"scrshot","Screenshot"); |
| 742 | MAPPER_AddHandler(CAPTURE_VideoEvent,MK_f5,MMOD1|MMOD2,"video","Video"); |
| 743 | #endif |
| 744 | } |
| 745 | ~HARDWARE(){ |
| 746 | #if (C_SSHOT) |
| 747 | if (capture.video.handle) CAPTURE_VideoEvent(true); |
| 748 | #endif |
| 749 | if (capture.wave.handle) CAPTURE_WaveEvent(true); |
| 750 | if (capture.midi.handle) CAPTURE_MidiEvent(true); |
| 751 | } |
| 752 | }; |
| 753 | |
| 754 | static HARDWARE* test; |
| 755 | |
| 756 | void HARDWARE_Destroy(Section * sec) { |
| 757 | delete test; |
| 758 | } |
| 759 | |
| 760 | void HARDWARE_Init(Section * sec) { |
| 761 | test = new HARDWARE(sec); |
| 762 | sec->AddDestroyFunction(&HARDWARE_Destroy,true); |
| 763 | } |