14 //#include "palette.h"
22 #define MOVIE_MAGIC 0x1a4d4346 // FCM\x1a
23 #define MOVIE_VERSION 2 // still at 2 since the format itself is still compatible - to detect which version the movie was made with, check the fceu version stored in the movie header (e.g against FCEU_VERSION_NUMERIC)
25 #define MOVIE_FLAG_NOSYNCHACK (1<<4) // set in newer version, used for old movie compatibility
28 #define read32le read32
32 int movie_version; // version of the movie format in the file
34 uint32 rerecord_count;
37 uint32 emu_version_used; // 9813 = 0.98.13
38 char* metadata; // caller-supplied buffer to store metadata. can be NULL.
39 int metadata_size; // size of the buffer pointed to by metadata
40 uint8 md5_of_rom_used[16];
41 int md5_of_rom_used_present; // v1 movies don't have md5 info available
42 char* name_of_rom_used; // caller-supplied buffer to store metadata. can be NULL.
43 int name_of_rom_used_size; // size of the buffer pointer to by name_of_rom_used
49 static void FCEUI_LoadMovie_v1(char *fname, int _read_only);
50 static int FCEUI_MovieGetInfo_v1(const char* fname, MOVIE_INFO* info);
52 extern char FileBase[];
58 uint32 version=2; // +4
60 uint32 length_frames; // +12
61 uint32 rerecord_count; // +16
62 uint32 movie_data_size; // +20
63 uint32 offset_to_savestate; // +24, should be 4-byte-aligned
64 uint32 offset_to_movie_data; // +28, should be 4-byte-aligned
65 uint8 md5_of_rom_used[16]; // +32
66 uint32 version_of_emu_used // +48
67 char name_of_rom_used[] // +52, utf-8, null-terminated
68 char metadata[]; // utf-8, null-terminated
70 uint8 savestate[]; // always present, even in a "from reset" recording
71 uint8 padding[]; // used for byte-alignment
77 int current = 0; // > 0 for recording, < 0 for playback
78 static FILE *slots[10]={0};
80 static uint32 framets = 0;
81 static uint32 frameptr = 0;
82 static uint8* moviedata = NULL;
83 static uint32 moviedatasize = 0;
84 static uint32 firstframeoffset = 0;
85 static uint32 savestate_offset = 0;
86 static uint32 framecount = 0;
87 static uint32 rerecord_count = 0;
88 /*static*/ int movie_readonly = 1;
89 int frame_display = 0;
90 static uint32 last_frame_display = ~0;
91 int input_display = 0;
92 static uint32 cur_input_display = 0;
93 static uint32 last_input_display = ~0;
97 /* Cache variables used for playback. */
98 static uint32 nextts = 0;
99 static int32 nextd = 0;
101 #define FCEUSTATE_RLSB 0x80000000
103 SFORMAT FCEUMOV_STATEINFO[]={
105 { &framets, 4|FCEUSTATE_RLSB, "FTS "},
106 { &nextts, 4|FCEUSTATE_RLSB, "NXTS"},
107 { &nextd, 4|FCEUSTATE_RLSB, "NXTD"},
108 { &frameptr, 4|FCEUSTATE_RLSB, "FPTR"},
109 { &framecount, 4|FCEUSTATE_RLSB, "FCNT"},
114 static int CurrentMovie = 1;
115 static int MovieShow = 0;
117 static int MovieStatus[10];
119 static void DoEncode(int joy, int button, int);
121 int FCEUMOV_IsPlaying(void)
123 if(current < 0) return(1);
127 int FCEUMOV_IsRecording(void)
129 if(current > 0) return(1);
133 int suppressMovieStop=0;
134 int movieConvertOffset1=0, movieConvertOffset2=0,movieConvertOK=0,movieSyncHackOn=0;
136 static void StopPlayback(void)
138 if(suppressMovieStop)
140 resetDMCacc=movieSyncHackOn=0;
141 fclose(slots[-1 - current]);
143 FCEU_DispMessage("Movie playback stopped.");
147 static void FlushHeader(void)
150 return;// only write header data if recording
152 FILE* fp = slots[current - 1];
156 unsigned long loc = ftell(fp);
157 fseek(fp, 4, SEEK_SET);
158 write32le(MOVIE_VERSION, fp);
159 fseek(fp, 12, SEEK_SET);
160 write32le(framecount, fp);
161 write32le(rerecord_count, fp);
162 write32le(frameptr, fp);
163 fseek(fp, 32, SEEK_SET);
164 fwrite(FCEUGameInfo->MD5, 1, 16, fp); // write ROM checksum
165 write32le(FCEU_VERSION_NUMERIC, fp); // write emu version used
167 // write ROM name used
168 fseek(fp, 52, SEEK_SET);
172 int strdiff=strlen(FileBase)-strlen(str);
175 // resize the whole damn movie because the ROM name in the header is of variable length
177 fseek(fp, 52, SEEK_SET);
179 } while(fgetc(fp) && !feof(fp) && !ferror(fp));
181 if(feof(fp) || ferror(fp))
183 fseek(fp, loc, SEEK_SET);
187 fseek(fp, 0, SEEK_END);
188 uint32 fsize=ftell(fp)-off;
189 char* ctemp=(char*)FCEU_malloc(fsize*sizeof(char)+4);
192 fseek(fp, loc, SEEK_SET);
195 fseek(fp, off, SEEK_SET);
196 fread(ctemp, 1,fsize, fp);
197 fseek(fp, 52+strlen(FileBase)+1, SEEK_SET);
198 int wrote = fwrite(ctemp, fsize,1, fp);
202 fseek(fp, loc, SEEK_SET);
206 if(loc >= firstframeoffset)
208 savestate_offset += strdiff;
209 firstframeoffset += strdiff;
210 fseek(fp, 24, SEEK_SET);
211 write32le(savestate_offset, fp);
212 write32le(firstframeoffset, fp);
214 fseek(fp, 52, SEEK_SET);
218 fseek(fp, loc, SEEK_SET);
221 static void StopRecording(void)
223 if(suppressMovieStop)
225 resetDMCacc=movieSyncHackOn=0;
226 DoEncode(0,0,1); /* Write a dummy timestamp value so that the movie will keep
227 "playing" after user input has stopped. */
231 // FIXME: truncate movie to length
233 fclose(slots[current - 1]);
234 MovieStatus[current - 1] = 1;
236 FCEU_DispMessage("Movie recording stopped.");
240 void FCEUI_StopMovie(void)
242 if(current < 0) StopPlayback();
244 if(current > 0) StopRecording();
250 void executeCommand(const char* cmd)
256 argv[0] = getenv("COMSPEC");
260 if(*argv && *(*argv))
261 _spawnve(_P_WAIT, argv[0], argv, NULL);
265 int justAutoConverted=0;
266 static const char* convertToFCM(const char *fname, char *buffer)
271 // convert to fcm if not already
272 const char* dot = strrchr(fname, '.');
275 int fmv = !stricmp(dot, ".fmv");
276 int nmv = !stricmp(dot, ".nmv");
277 int vmv = !stricmp(dot, ".vmv");
278 if(fmv || nmv || vmv)
280 strcpy(buffer, fname);
281 buffer[dot-fname]='\0';
282 strcat(buffer,"-autoconverted.fcm");
292 extern char lastLoadedGameName [2048];
293 char cmd [1024], offset[64], romInfo[1024];
295 sprintf(romInfo, "-smd5=\"%s\" -sromname=\"%s (MAYBE)\" -s", lastLoadedGameName, FileBase);
297 sprintf(romInfo, "-sromname=\"(unknown)\" -s");
298 if(movieConvertOffset2) sprintf(offset, "-o %d:%d", movieConvertOffset2,movieConvertOffset1);
299 else sprintf(offset, "-o %d", movieConvertOffset1);
300 sprintf(cmd, ".\\util\\nesmock\\nesmock.exe %s %s -spal=%c -sfceuver=%d \"%s\" \"%s\" ", offset, romInfo, FCEUI_GetCurrentVidSystem(0,0)?'1':'0', fceuver, fname, buffer);
301 // FCEU_PrintError(cmd);
304 FILE* file = FCEUD_UTF8fopen(buffer,"rb");
307 fseek(file, 12, SEEK_SET);
309 read32le(&frames, file);
317 static int errAlready=0;
321 FCEU_PrintError("For some reason, nesmock was unable to create a valid FCM from the given file.\nThe command given was:\n%s\nPerhaps the file specified is not a movie file or contains no input data,\nor perhaps it is a movie file of a version unsupported by nesmock.\n\n(This error message will self-destruct until you restart FCEU.)", cmd);
330 GetCurrentDirectory(512,str);
331 strcat(str, "\\util\\nesmock\\nesmock.exe");
332 file = FCEUD_UTF8fopen(str, "rb");
335 static int errAlready=0;
339 FCEU_PrintError("For some reason, nesmock was unable to convert the movie to FCM format.\nThe command given was:\n%s\n\n(This error message will self-destruct until you restart FCEU.)", cmd);
345 static int errAlready=0;
349 FCEU_PrintError("Nesmock not found, so the movie could not be converted to FCM format.\nYou must place nesmock.exe at this location so FCEU can find it:\n%s\n\n(This error message will self-destruct until you restart FCEU.)", str);
359 static void ResetInputTypes()
362 extern int UsrInputType[3];
363 UsrInputType[0] = SI_GAMEPAD;
364 UsrInputType[1] = SI_GAMEPAD;
365 UsrInputType[2] = SIFC_NONE;
367 ParseGIInput(NULL/*FCEUGameInfo*/);
368 extern int cspec, gametype;
369 cspec=FCEUGameInfo->cspecial;
370 gametype=FCEUGameInfo->type;
376 char curMovieFilename[512];
379 // PlayMovie / MoviePlay function
380 void FCEUI_LoadMovie(char *fname, int _read_only)
383 fname = (char*)convertToFCM(fname,buffer);
392 fname = fn = FCEU_MakeFName(FCEUMKF_MOVIE,CurrentMovie,0);
397 strcpy(origname,fname);
400 // check movie_readonly
401 movie_readonly = _read_only;
402 if(access(fname, W_OK))
405 fp = fopen(fname, (movie_readonly>=2) ? "rb" : "r+b");
421 read32le(&magic, fp);
422 if(magic != MOVIE_MAGIC)
427 //DEBUG_COMPARE_RAM(__LINE__);
429 read32le(&version, fp);
432 // attempt to load previous version's format
434 printf("trying movie v1\n");
435 FCEUI_LoadMovie_v1(fname, _read_only);
438 else if(version == MOVIE_VERSION)
442 // unsupported version
447 fread(flags, 1, 4, fp);
448 read32le(&framecount, fp);
449 read32le(&rerecord_count, fp);
450 read32le(&moviedatasize, fp);
451 read32le(&savestate_offset, fp);
452 read32le(&firstframeoffset, fp);
453 if(fseek(fp, savestate_offset, SEEK_SET))
459 // FCEU_PrintError("flags[0] & MOVIE_FLAG_NOSYNCHACK=%d",flags[0] & MOVIE_FLAG_NOSYNCHACK);
460 if(flags[0] & MOVIE_FLAG_NOSYNCHACK)
466 // fully reload the game to reinitialize everything before playing any movie
467 // to try fixing nondeterministic playback of some games
468 #if 0 // do we need this?
470 extern char lastLoadedGameName [2048];
472 extern int disableBatteryLoading, suppressAddPowerCommand;
473 suppressAddPowerCommand=1;
477 FCEUGI * gi = FCEUI_LoadGame(lastLoadedGameName);
483 suppressAddPowerCommand=0;
488 if(!FCEUSS_LoadFP(fp,1)) return;
492 fseek(fp, firstframeoffset, SEEK_SET);
493 moviedata = (uint8*)realloc(moviedata, moviedatasize);
494 fread(moviedata, 1, moviedatasize, fp);
496 framecount = 0; // movies start at frame 0!
498 current = CurrentMovie;
501 memset(joop,0,sizeof(joop));
502 current = -1 - current;
507 MovieStatus[CurrentMovie] = 1;
510 FCEUI_SelectMovie(CurrentMovie,1); /* Quick hack to display status. */
513 FCEU_DispMessage("Movie playback started.");
516 strcpy(curMovieFilename, origname);
518 strcpy(curMovieFilename, fname);
523 void FCEUI_SaveMovie(char *fname, uint8 flags, const char* metadata)
528 uint8 padding[4] = {0,0,0,0};
536 fp = FCEUD_UTF8fopen(fname, "wb");
537 strcpy(origname,fname);
541 fp=FCEUD_UTF8fopen(fn=FCEU_MakeFName(FCEUMKF_MOVIE,CurrentMovie,0),"wb");
548 // don't need the movieSyncHackOn sync hack for newly recorded movies
549 flags |= MOVIE_FLAG_NOSYNCHACK;
550 resetDMCacc=movieSyncHackOn=0;
553 if(FCEUI_GetCurrentVidSystem(0,0))
554 flags |= MOVIE_FLAG_PAL;
556 if(flags & MOVIE_FLAG_FROM_POWERON)
559 flags &= ~MOVIE_FLAG_FROM_POWERON;
560 flags |= MOVIE_FLAG_FROM_RESET;
564 write32le(MOVIE_MAGIC, fp);
565 write32le(MOVIE_VERSION, fp);
567 fputc(0, fp); // reserved
568 fputc(0, fp); // reserved
569 fputc(0, fp); // reserved
570 write32le(0, fp); // leave room for length frames
571 write32le(0, fp); // leave room for rerecord count
572 write32le(0, fp); // leave room for movie data size
573 write32le(0, fp); // leave room for savestate_offset
574 write32le(0, fp); // leave room for offset_to_controller_data
575 fwrite(FCEUGameInfo->MD5, 1, 16, fp); // write ROM checksum
576 write32le(FCEU_VERSION_NUMERIC, fp); // write emu version used
577 fputs(FileBase, fp); // write ROM name used
581 if(strlen(metadata) < MOVIE_MAX_METADATA)
584 fwrite(metadata, 1, MOVIE_MAX_METADATA-1, fp);
589 n_padding = (4 - (ftell(fp) & 0x3)) & 0x3;
590 fwrite(padding, 1, n_padding, fp);
592 if(flags & MOVIE_FLAG_FROM_RESET)
596 // make a for-movie-recording power-on clear the game's save data, too
597 // (note: FCEU makes a save state immediately after this and that loads that on movie playback)
598 extern char lastLoadedGameName [2048];
599 extern int disableBatteryLoading, suppressAddPowerCommand;
600 suppressAddPowerCommand=1;
601 disableBatteryLoading=1;
604 // NOTE: this will NOT write an FCEUNPCMD_POWER into the movie file
605 FCEUGI * gi = FCEUI_LoadGame(lastLoadedGameName);
607 PowerNES(); // and neither will this, if it can even happen
610 disableBatteryLoading=0;
611 suppressAddPowerCommand=0;
615 savestate_offset = ftell(fp);
617 fseek(fp, 0, SEEK_END);
622 n_padding = (4 - (ftell(fp) & 0x3)) & 0x3;
623 fwrite(padding, 1, n_padding, fp);
625 firstframeoffset = ftell(fp);
628 fseek(fp, 24, SEEK_SET); // offset_to_savestate offset
629 write32le(savestate_offset, fp);
630 write32le(firstframeoffset, fp);
632 fseek(fp, firstframeoffset, SEEK_SET);
634 // set recording flag
635 current=CurrentMovie;
642 memset(joop,0,sizeof(joop));
648 if(flags & MOVIE_FLAG_FROM_RESET)
652 PowerNES(); // NOTE: this will write an FCEUNPCMD_POWER into the movie file
655 ResetNES(); // NOTE: this will write an FCEUNPCMD_RESET into the movie file
658 FCEUI_SelectMovie(CurrentMovie,1); /* Quick hack to display status. */
660 FCEU_DispMessage("Movie recording started.");
662 strcpy(curMovieFilename, origname);
665 static void movie_writechar(int c)
667 if(frameptr == moviedatasize)
669 moviedatasize += 4096;
670 moviedata = (uint8*)realloc(moviedata, moviedatasize);
672 moviedata[frameptr++] = (uint8)(c & 0xff);
673 fputc(c, slots[current - 1]);
677 static int movie_readchar()
679 if(frameptr >= moviedatasize)
683 return (int)(moviedata[frameptr++]);
687 static void DoEncode(int joy, int button, int dummy)
695 else if(framets >= 256)
706 //printf("Wr: %02x, %d\n",d,slots[current-1]);
709 movie_writechar(framets & 0xff);
710 //printf("Wrts: %02x\n",framets & 0xff);
716 // TODO: make this function legible! (what are all these magic numbers and weirdly named variables and crazy unexplained loops?)
717 void FCEUMOV_AddJoy(uint8 *js)
721 // if(!current) return; // Not playback nor recording.
723 if(current < 0) // Playback
725 while(nextts == framets || nextd == -1)
737 FCEU_DoSimpleCommand(nextd&0x1F);
741 joop[(nextd >> 3)&0x3] ^= 1 << (nextd&0x7);
745 tmp = movie_readchar();
751 memcpy(&cur_input_display,js,4);
761 while(tmp--) { nextts |= movie_readchar() << (ti * 8); ti++; }
763 // This fixes a bug in movies recorded before version 0.98.11
764 // It's probably not necessary, but it may keep away CRAZY PEOPLE who recorded
765 // movies on <= 0.98.10 and don't work on playback.
766 if(tmpfix == 1 && !nextts)
767 {nextts |= movie_readchar()<<8; }
768 else if(tmpfix == 2 && !nextts) {nextts |= movie_readchar()<<16; }
778 else if(current > 0) // Recording
780 // flush header info every 300 frames in case of crash
782 static int fcounter=0;
793 if((js[x] ^ joop[x]) & (1 << y))
797 else if(framets == ((1<<24)-1)) DoEncode(0,0,1); // Overflow will happen, so do dummy update.
808 memcpy(&cur_input_display,js,4);
812 void FCEUMOV_AddCommand(int cmd)
814 if(current <= 0) return; // Return if not recording a movie
815 //printf("%d\n",cmd);
816 DoEncode((cmd>>3)&0x3,cmd&0x7,1);
821 void FCEUMOV_CheckMovies(void)
827 for(ssel=0;ssel<10;ssel++)
829 st=FCEUD_UTF8fopen(fn=FCEU_MakeFName(FCEUMKF_MOVIE,ssel,0),"rb");
842 void FCEUI_SelectMovieNext(int n)
845 CurrentMovie=(CurrentMovie+1)%10;
847 CurrentMovie=(CurrentMovie+9)%10;
848 FCEUI_SelectMovie(CurrentMovie, 1);
852 int FCEUI_SelectMovie(int w, int show)
854 int oldslot=CurrentMovie;
855 if(w == -1) { MovieShow = 0; return; }
856 FCEUI_SelectState(-1,0);
865 FCEU_DispMessage("-recording movie %d-",current-1);
866 else if (current < 0)
867 FCEU_DispMessage("-playing movie %d-",-1 - current);
869 FCEU_DispMessage("-select movie-");
875 void FCEU_DrawMovies(uint8 *XBuf)
877 int frameDisplayOn = current != 0 && frame_display;
880 extern int32 fps_scale;
884 int howl=(180-(FCEUI_EmulationPaused()?(60):(20*fps_scale/256)));
885 if(howl>176) howl=180;
887 if((howlong<howl || movcounter)
888 && (frameDisplayOn && (!movcounter || last_frame_display!=framecount) || input_display && (!movcounter || last_input_display!=cur_input_display)))
893 uint32 c = cur_input_display;
894 sprintf(inputstr, "%c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
895 (c&0x40)?'<':' ', (c&0x10)?'^':' ', (c&0x80)?'>':' ', (c&0x20)?'v':' ',
896 (c&0x01)?'A':' ', (c&0x02)?'B':' ', (c&0x08)?'S':' ', (c&0x04)?'s':' ',
897 (c&0x4000)?'<':' ', (c&0x1000)?'^':' ', (c&0x8000)?'>':' ', (c&0x2000)?'v':' ',
898 (c&0x0100)?'A':' ', (c&0x0200)?'B':' ', (c&0x0800)?'S':' ', (c&0x0400)?'s':' ');
902 if(frameDisplayOn && !input_display)
903 FCEU_DispMessage("%s frame %u",current >= 0?"Recording":"Playing",framecount);
904 else if(input_display && !frameDisplayOn)
905 FCEU_DispMessage("Input: %s",inputstr);
906 else //if(input_display && frame_display)
907 FCEU_DispMessage("%s %u %s",current >= 0?"Recording":"Playing",framecount,inputstr);
909 last_frame_display = framecount;
910 last_input_display = cur_input_display;
915 if(movcounter) movcounter--;
917 if(!MovieShow) return;
919 FCEU_DrawNumberRow(XBuf,MovieStatus, CurrentMovie);
923 int FCEUMOV_WriteState(FILE* st)
927 to_write = moviedatasize;
935 fwrite(moviedata, 1, to_write, st);
939 static int load_successful;
941 int FCEUMOV_ReadState(FILE* st, uint32 size)
943 // if this savestate was made while replaying,
944 // we need to "undo" nextd and nextts
950 else if(nextts > 256)
958 // if(current > 0 || (!movie_readonly && current < 0)) /* Recording or Playback (!read-only) */
959 if(current!=0 && !movie_readonly)
961 // copy movie data from savestate
962 moviedata = (uint8*)realloc(moviedata, size);
963 moviedatasize = size;
964 if(size && fread(moviedata, 1, size, st)<size)
966 if(current < 0) // switch to recording
968 fseek(slots[current - 1], firstframeoffset, SEEK_SET);
969 fwrite(moviedata, 1, frameptr, slots[current - 1]);
972 // else if(current < 0) /* Playback (read-only) */
973 else if(current!=0 && movie_readonly)
975 if(current > 0) // switch to playback
977 // allow frameptr to be updated but keep movie data
978 fseek(st, size, SEEK_CUR);
979 // prevent seeking beyond the end of the movie
980 if(frameptr > moviedatasize)
981 frameptr = moviedatasize;
983 else /* Neither recording or replaying */
986 fseek(st, size, SEEK_CUR);
993 void FCEUMOV_PreLoad(void)
998 int FCEUMOV_PostLoad(void)
1000 if(!FCEUI_IsMovieActive())
1003 return load_successful;
1006 char* FCEUI_MovieGetCurrentName(int addSlotNumber)
1008 return FCEU_MakeFName(FCEUMKF_MOVIE,(addSlotNumber ? CurrentMovie : -1),0);
1012 int FCEUI_IsMovieActive(void)
1018 void FCEUI_MovieToggleFrameDisplay(void)
1020 frame_display=!frame_display;
1021 if(!(current != 0 && frame_display) && !input_display)
1022 FCEU_ResetMessages();
1025 last_frame_display = ~framecount;
1026 last_input_display = ~cur_input_display;
1030 void FCEUI_ToggleInputDisplay(void)
1032 input_display=!input_display;
1033 if(!input_display && !(current != 0 && frame_display))
1034 FCEU_ResetMessages();
1037 last_frame_display = ~framecount;
1038 last_input_display = ~cur_input_display;
1042 void FCEUI_MovieToggleReadOnly(void)
1044 if(movie_readonly < 2)
1046 movie_readonly = !movie_readonly;
1048 FCEU_DispMessage("Movie is now Read-Only.");
1050 FCEU_DispMessage("Movie is now Read+Write.");
1054 FCEU_DispMessage("Movie file is Read-Only.");
1058 char lastMovieInfoFilename [512] = {'\0',};
1060 int FCEUI_MovieGetInfo(const char* fname, MOVIE_INFO* info)
1065 fname = (const char*)convertToFCM(fname,buffer);
1066 strncpy(lastMovieInfoFilename, fname, 512);
1068 // main get info part of function
1074 FILE* fp = FCEUD_UTF8fopen(fname, "rb");
1078 read32le(&magic, fp);
1079 if(magic != MOVIE_MAGIC)
1085 read32le(&version, fp);
1086 if(version != MOVIE_VERSION)
1090 return FCEUI_MovieGetInfo_v1(fname, info);
1095 info->movie_version = MOVIE_VERSION;
1097 fread(_flags, 1, 4, fp);
1099 info->flags = _flags[0];
1100 read32le(&info->num_frames, fp);
1101 read32le(&info->rerecord_count, fp);
1103 if(access(fname, W_OK))
1104 info->read_only = 1;
1106 info->read_only = 0;
1108 fseek(fp, 12, SEEK_CUR); // skip movie_data_size, offset_to_savestate, and offset_to_movie_data
1110 fread(info->md5_of_rom_used, 1, 16, fp);
1111 info->md5_of_rom_used_present = 1;
1113 read32le(&info->emu_version_used, fp);
1115 // I probably could have planned this better...
1123 if(info->name_of_rom_used && info->name_of_rom_used_size)
1124 info->name_of_rom_used[0]='\0';
1126 r=fread(str, 1, 256, fp);
1129 for(p=0; p<r && last_c != '\0'; ++p)
1131 if(info->name_of_rom_used && info->name_of_rom_used_size && (p2 < info->name_of_rom_used_size-1))
1133 info->name_of_rom_used[p2]=str[p];
1140 memmove(str, str+p, r-p);
1144 r=fread(str, 1, 256, fp);
1150 if(info->metadata && info->metadata_size)
1151 info->metadata[0]='\0';
1155 for(p=0; p<r && last_c != '\0'; ++p)
1157 if(info->metadata && info->metadata_size && (p2 < info->metadata_size-1))
1159 info->metadata[p2]=str[p];
1166 r=fread(str, 1, 256, fp);
1171 // somehow failed to read romname and metadata
1177 // check what hacks are necessary
1178 fseek(fp, 24, SEEK_SET); // offset_to_savestate offset
1179 uint32 temp_savestate_offset;
1180 read32le(&temp_savestate_offset, fp);
1181 if(fseek(fp, temp_savestate_offset, SEEK_SET))
1186 if(!FCEUSS_LoadFP(fp,2)) return 0; // 2 -> don't really load, just load to find what's there then load backup
1199 struct MovieHeader_v1
1204 uint32 length_frames;
1205 uint32 rerecord_count;
1206 uint32 movie_data_size;
1207 uint32 offset_to_savestate;
1208 uint32 offset_to_movie_data;
1209 uint16 metadata_ucs2[]; // ucs-2, ick! sizeof(metadata) = offset_to_savestate - MOVIE_HEADER_SIZE
1213 #define MOVIE_V1_HEADER_SIZE 32
1215 static void FCEUI_LoadMovie_v1(char *fname, int _read_only)
1224 fname = fn = FCEU_MakeFName(FCEUMKF_MOVIE,CurrentMovie,0);
1227 // check movie_readonly
1228 movie_readonly = _read_only;
1229 if(access(fname, W_OK))
1233 fp = FCEUD_UTF8fopen(fname, (movie_readonly>=2) ? "rb" : "r+b");
1235 fp = fopen(fname, (movie_readonly>=2) ? "rb" : "r+b");
1253 read32le(&magic, fp);
1254 if(magic != MOVIE_MAGIC)
1260 read32le(&version, fp);
1267 fread(flags, 1, 4, fp);
1269 read32le(&rerecord_count, fp);
1270 read32le(&moviedatasize, fp);
1271 read32le(&savestate_offset, fp);
1272 read32le(&firstframeoffset, fp);
1273 if(fseek(fp, savestate_offset, SEEK_SET))
1279 if(flags[0] & MOVIE_FLAG_NOSYNCHACK)
1285 // fully reload the game to reinitialize everything before playing any movie
1286 // to try fixing nondeterministic playback of some games
1288 extern char lastLoadedGameName [2048];
1290 extern int disableBatteryLoading, suppressAddPowerCommand;
1291 suppressAddPowerCommand=1;
1292 suppressMovieStop=1;
1295 FCEUGI * gi = FCEUI_LoadGame(lastLoadedGameName);
1300 suppressMovieStop=0;
1301 suppressAddPowerCommand=0;
1305 if(!FCEUSS_LoadFP(fp,1)) return;
1309 fseek(fp, firstframeoffset, SEEK_SET);
1310 moviedata = (uint8*)realloc(moviedata, moviedatasize);
1311 fread(moviedata, 1, moviedatasize, fp);
1313 framecount = 0; // movies start at frame 0!
1315 current = CurrentMovie;
1316 slots[current] = fp;
1318 memset(joop,0,sizeof(joop));
1319 current = -1 - current;
1323 MovieStatus[CurrentMovie] = 1;
1326 FCEUI_SelectMovie(CurrentMovie,1); /* Quick hack to display status. */
1329 FCEU_DispMessage("Movie playback started.");
1333 static int FCEUI_MovieGetInfo_v1(const char* fname, MOVIE_INFO* info)
1338 uint32 savestateoffset;
1339 uint8 tmp[MOVIE_MAX_METADATA<<1];
1340 int metadata_length;
1342 FILE* fp = FCEUD_UTF8fopen(fname, "rb");
1346 read32le(&magic, fp);
1347 if(magic != MOVIE_MAGIC)
1353 read32le(&version, fp);
1360 info->movie_version = 1;
1361 info->emu_version_used = 0; // unknown
1363 fread(_flags, 1, 4, fp);
1365 info->flags = _flags[0];
1366 read32le(&info->num_frames, fp);
1367 read32le(&info->rerecord_count, fp);
1369 if(access(fname, W_OK))
1370 info->read_only = 1;
1372 info->read_only = 0;
1374 fseek(fp, 4, SEEK_CUR);
1375 read32le(&savestateoffset, fp);
1377 metadata_length = (int)savestateoffset - MOVIE_V1_HEADER_SIZE;
1378 if(metadata_length > 0)
1382 metadata_length >>= 1;
1383 if(metadata_length >= MOVIE_MAX_METADATA)
1384 metadata_length = MOVIE_MAX_METADATA-1;
1386 fseek(fp, MOVIE_V1_HEADER_SIZE, SEEK_SET);
1387 fread(tmp, 1, metadata_length<<1, fp);
1390 // turn old ucs2 metadata into utf8
1391 if(info->metadata && info->metadata_size)
1393 char* ptr=info->metadata;
1394 char* ptr_end=info->metadata+info->metadata_size-1;
1396 while(ptr<ptr_end && c_ptr<metadata_length)
1398 uint16 c=(tmp[c_ptr<<1] | (tmp[(c_ptr<<1)+1] << 8));
1402 *ptr++ = (char)(c&0x7f);
1404 case 0x80 ... 0x7ff:
1409 *ptr++=(0xc0 | (c>>6));
1410 *ptr++=(0x80 | (c & 0x3f));
1413 case 0x800 ... 0xffff:
1418 *ptr++=(0xe0 | (c>>12));
1419 *ptr++=(0x80 | ((c>>6) & 0x3f));
1420 *ptr++=(0x80 | (c & 0x3f));
1429 // md5 info not available from v1
1430 info->md5_of_rom_used_present = 0;
1431 // rom name used for the movie not available from v1
1432 if(info->name_of_rom_used && info->name_of_rom_used_size)
1433 info->name_of_rom_used[0] = '\0';
1435 // check what hacks are necessary
1436 fseek(fp, 24, SEEK_SET); // offset_to_savestate offset
1437 uint32 temp_savestate_offset;
1438 read32le(&temp_savestate_offset, fp);
1439 if(fseek(fp, temp_savestate_offset, SEEK_SET))
1444 if(!FCEUSS_LoadFP(fp,2)) return 0; // 2 -> don't really load, just load to find what's there then load backup