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("movie: trying movie v1\n");
435 FCEUI_LoadMovie_v1(fname, _read_only);
438 else if(version == MOVIE_VERSION)
442 // unsupported version
443 printf("movie: unsupported version\n");
448 fread(flags, 1, 4, fp);
449 read32le(&framecount, fp);
450 read32le(&rerecord_count, fp);
451 read32le(&moviedatasize, fp);
452 read32le(&savestate_offset, fp);
453 read32le(&firstframeoffset, fp);
454 if(fseek(fp, savestate_offset, SEEK_SET))
460 // FCEU_PrintError("flags[0] & MOVIE_FLAG_NOSYNCHACK=%d",flags[0] & MOVIE_FLAG_NOSYNCHACK);
461 if(flags[0] & MOVIE_FLAG_NOSYNCHACK)
467 // fully reload the game to reinitialize everything before playing any movie
468 // to try fixing nondeterministic playback of some games
469 #if 0 // do we need this?
471 extern char lastLoadedGameName [2048];
473 extern int disableBatteryLoading, suppressAddPowerCommand;
474 suppressAddPowerCommand=1;
478 FCEUGI * gi = FCEUI_LoadGame(lastLoadedGameName);
484 suppressAddPowerCommand=0;
489 // Loading new savestates doesn't work and even breaks FDS
490 //if(!FCEUSS_LoadFP(fp,1)) return;
494 fseek(fp, firstframeoffset, SEEK_SET);
495 moviedata = (uint8*)realloc(moviedata, moviedatasize);
496 fread(moviedata, 1, moviedatasize, fp);
498 framecount = 0; // movies start at frame 0!
500 current = CurrentMovie;
503 memset(joop,0,sizeof(joop));
504 current = -1 - current;
509 MovieStatus[CurrentMovie] = 1;
512 FCEUI_SelectMovie(CurrentMovie,1); /* Quick hack to display status. */
515 FCEU_DispMessage("Movie playback started.");
518 strcpy(curMovieFilename, origname);
520 strcpy(curMovieFilename, fname);
525 void FCEUI_SaveMovie(char *fname, uint8 flags, const char* metadata)
530 uint8 padding[4] = {0,0,0,0};
538 fp = FCEUD_UTF8fopen(fname, "wb");
539 strcpy(origname,fname);
543 fp=FCEUD_UTF8fopen(fn=FCEU_MakeFName(FCEUMKF_MOVIE,CurrentMovie,0),"wb");
550 // don't need the movieSyncHackOn sync hack for newly recorded movies
551 flags |= MOVIE_FLAG_NOSYNCHACK;
552 resetDMCacc=movieSyncHackOn=0;
555 if(FCEUI_GetCurrentVidSystem(0,0))
556 flags |= MOVIE_FLAG_PAL;
558 if(flags & MOVIE_FLAG_FROM_POWERON)
561 flags &= ~MOVIE_FLAG_FROM_POWERON;
562 flags |= MOVIE_FLAG_FROM_RESET;
566 write32le(MOVIE_MAGIC, fp);
567 write32le(MOVIE_VERSION, fp);
569 fputc(0, fp); // reserved
570 fputc(0, fp); // reserved
571 fputc(0, fp); // reserved
572 write32le(0, fp); // leave room for length frames
573 write32le(0, fp); // leave room for rerecord count
574 write32le(0, fp); // leave room for movie data size
575 write32le(0, fp); // leave room for savestate_offset
576 write32le(0, fp); // leave room for offset_to_controller_data
577 fwrite(FCEUGameInfo->MD5, 1, 16, fp); // write ROM checksum
578 write32le(FCEU_VERSION_NUMERIC, fp); // write emu version used
579 fputs(FileBase, fp); // write ROM name used
583 if(strlen(metadata) < MOVIE_MAX_METADATA)
586 fwrite(metadata, 1, MOVIE_MAX_METADATA-1, fp);
591 n_padding = (4 - (ftell(fp) & 0x3)) & 0x3;
592 fwrite(padding, 1, n_padding, fp);
594 if(flags & MOVIE_FLAG_FROM_RESET)
598 // make a for-movie-recording power-on clear the game's save data, too
599 // (note: FCEU makes a save state immediately after this and that loads that on movie playback)
600 extern char lastLoadedGameName [2048];
601 extern int disableBatteryLoading, suppressAddPowerCommand;
602 suppressAddPowerCommand=1;
603 disableBatteryLoading=1;
606 // NOTE: this will NOT write an FCEUNPCMD_POWER into the movie file
607 FCEUGI * gi = FCEUI_LoadGame(lastLoadedGameName);
609 PowerNES(); // and neither will this, if it can even happen
612 disableBatteryLoading=0;
613 suppressAddPowerCommand=0;
617 savestate_offset = ftell(fp);
619 fseek(fp, 0, SEEK_END);
624 n_padding = (4 - (ftell(fp) & 0x3)) & 0x3;
625 fwrite(padding, 1, n_padding, fp);
627 firstframeoffset = ftell(fp);
630 fseek(fp, 24, SEEK_SET); // offset_to_savestate offset
631 write32le(savestate_offset, fp);
632 write32le(firstframeoffset, fp);
634 fseek(fp, firstframeoffset, SEEK_SET);
636 // set recording flag
637 current=CurrentMovie;
644 memset(joop,0,sizeof(joop));
650 if(flags & MOVIE_FLAG_FROM_RESET)
654 PowerNES(); // NOTE: this will write an FCEUNPCMD_POWER into the movie file
657 ResetNES(); // NOTE: this will write an FCEUNPCMD_RESET into the movie file
660 FCEUI_SelectMovie(CurrentMovie,1); /* Quick hack to display status. */
662 FCEU_DispMessage("Movie recording started.");
664 strcpy(curMovieFilename, origname);
667 static void movie_writechar(int c)
669 if(frameptr == moviedatasize)
671 moviedatasize += 4096;
672 moviedata = (uint8*)realloc(moviedata, moviedatasize);
674 moviedata[frameptr++] = (uint8)(c & 0xff);
675 fputc(c, slots[current - 1]);
679 static int movie_readchar()
681 if(frameptr >= moviedatasize)
685 return (int)(moviedata[frameptr++]);
689 static void DoEncode(int joy, int button, int dummy)
697 else if(framets >= 256)
708 //printf("Wr: %02x, %d\n",d,slots[current-1]);
711 movie_writechar(framets & 0xff);
712 //printf("Wrts: %02x\n",framets & 0xff);
718 // TODO: make this function legible! (what are all these magic numbers and weirdly named variables and crazy unexplained loops?)
719 void FCEUMOV_AddJoy(uint8 *js)
723 if(!current) return; // Not playback nor recording.
725 if(current < 0) // Playback
727 while(nextts == framets || nextd == -1)
737 FCEU_DoSimpleCommand(nextd&0x1F);
740 joop[(nextd >> 3)&0x3] ^= 1 << (nextd&0x7);
744 tmp = movie_readchar();
750 memcpy(&cur_input_display,js,4);
760 while(tmp--) { nextts |= movie_readchar() << (ti * 8); ti++; }
762 // This fixes a bug in movies recorded before version 0.98.11
763 // It's probably not necessary, but it may keep away CRAZY PEOPLE who recorded
764 // movies on <= 0.98.10 and don't work on playback.
765 if(tmpfix == 1 && !nextts)
766 {nextts |= movie_readchar()<<8; }
767 else if(tmpfix == 2 && !nextts) {nextts |= movie_readchar()<<16; }
777 else if(current > 0) // Recording
779 // flush header info every 300 frames in case of crash
781 static int fcounter=0;
792 if((js[x] ^ joop[x]) & (1 << y))
796 else if(framets == ((1<<24)-1)) DoEncode(0,0,1); // Overflow will happen, so do dummy update.
807 memcpy(&cur_input_display,js,4);
811 void FCEUMOV_AddCommand(int cmd)
813 if(current <= 0) return; // Return if not recording a movie
814 //printf("%d\n",cmd);
815 DoEncode((cmd>>3)&0x3,cmd&0x7,1);
820 void FCEUMOV_CheckMovies(void)
826 for(ssel=0;ssel<10;ssel++)
828 st=FCEUD_UTF8fopen(fn=FCEU_MakeFName(FCEUMKF_MOVIE,ssel,0),"rb");
841 void FCEUI_SelectMovieNext(int n)
844 CurrentMovie=(CurrentMovie+1)%10;
846 CurrentMovie=(CurrentMovie+9)%10;
847 FCEUI_SelectMovie(CurrentMovie, 1);
851 int FCEUI_SelectMovie(int w, int show)
853 int oldslot=CurrentMovie;
854 if(w == -1) { MovieShow = 0; return; }
855 FCEUI_SelectState(-1,0);
864 FCEU_DispMessage("-recording movie %d-",current-1);
865 else if (current < 0)
866 FCEU_DispMessage("-playing movie %d-",-1 - current);
868 FCEU_DispMessage("-select movie-");
874 void FCEU_DrawMovies(uint8 *XBuf)
876 int frameDisplayOn = current != 0 && frame_display;
879 extern int32 fps_scale;
883 int howl=(180-(FCEUI_EmulationPaused()?(60):(20*fps_scale/256)));
884 if(howl>176) howl=180;
886 if((howlong<howl || movcounter)
887 && (frameDisplayOn && (!movcounter || last_frame_display!=framecount) || input_display && (!movcounter || last_input_display!=cur_input_display)))
892 uint32 c = cur_input_display;
893 sprintf(inputstr, "%c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
894 (c&0x40)?'<':' ', (c&0x10)?'^':' ', (c&0x80)?'>':' ', (c&0x20)?'v':' ',
895 (c&0x01)?'A':' ', (c&0x02)?'B':' ', (c&0x08)?'S':' ', (c&0x04)?'s':' ',
896 (c&0x4000)?'<':' ', (c&0x1000)?'^':' ', (c&0x8000)?'>':' ', (c&0x2000)?'v':' ',
897 (c&0x0100)?'A':' ', (c&0x0200)?'B':' ', (c&0x0800)?'S':' ', (c&0x0400)?'s':' ');
901 if(frameDisplayOn && !input_display)
902 FCEU_DispMessage("%s frame %u",current >= 0?"Recording":"Playing",framecount);
903 else if(input_display && !frameDisplayOn)
904 FCEU_DispMessage("Input: %s",inputstr);
905 else //if(input_display && frame_display)
906 FCEU_DispMessage("%s %u %s",current >= 0?"Recording":"Playing",framecount,inputstr);
908 last_frame_display = framecount;
909 last_input_display = cur_input_display;
914 if(movcounter) movcounter--;
916 if(!MovieShow) return;
918 FCEU_DrawNumberRow(XBuf,MovieStatus, CurrentMovie);
922 int FCEUMOV_WriteState(FILE* st)
926 to_write = moviedatasize;
934 fwrite(moviedata, 1, to_write, st);
938 static int load_successful;
940 int FCEUMOV_ReadState(FILE* st, uint32 size)
942 // if this savestate was made while replaying,
943 // we need to "undo" nextd and nextts
949 else if(nextts > 256)
957 // if(current > 0 || (!movie_readonly && current < 0)) /* Recording or Playback (!read-only) */
958 if(current!=0 && !movie_readonly)
960 // copy movie data from savestate
961 moviedata = (uint8*)realloc(moviedata, size);
962 moviedatasize = size;
963 if(size && fread(moviedata, 1, size, st)<size)
965 if(current < 0) // switch to recording
967 fseek(slots[current - 1], firstframeoffset, SEEK_SET);
968 fwrite(moviedata, 1, frameptr, slots[current - 1]);
971 // else if(current < 0) /* Playback (read-only) */
972 else if(current!=0 && movie_readonly)
974 if(current > 0) // switch to playback
976 // allow frameptr to be updated but keep movie data
977 fseek(st, size, SEEK_CUR);
978 // prevent seeking beyond the end of the movie
979 if(frameptr > moviedatasize)
980 frameptr = moviedatasize;
982 else /* Neither recording or replaying */
985 fseek(st, size, SEEK_CUR);
992 void FCEUMOV_PreLoad(void)
997 int FCEUMOV_PostLoad(void)
999 if(!FCEUI_IsMovieActive())
1002 return load_successful;
1005 char* FCEUI_MovieGetCurrentName(int addSlotNumber)
1007 return FCEU_MakeFName(FCEUMKF_MOVIE,(addSlotNumber ? CurrentMovie : -1),0);
1011 int FCEUI_IsMovieActive(void)
1017 void FCEUI_MovieToggleFrameDisplay(void)
1019 frame_display=!frame_display;
1020 if(!(current != 0 && frame_display) && !input_display)
1021 FCEU_ResetMessages();
1024 last_frame_display = ~framecount;
1025 last_input_display = ~cur_input_display;
1029 void FCEUI_ToggleInputDisplay(void)
1031 input_display=!input_display;
1032 if(!input_display && !(current != 0 && frame_display))
1033 FCEU_ResetMessages();
1036 last_frame_display = ~framecount;
1037 last_input_display = ~cur_input_display;
1041 void FCEUI_MovieToggleReadOnly(void)
1043 if(movie_readonly < 2)
1045 movie_readonly = !movie_readonly;
1047 FCEU_DispMessage("Movie is now Read-Only.");
1049 FCEU_DispMessage("Movie is now Read+Write.");
1053 FCEU_DispMessage("Movie file is Read-Only.");
1057 char lastMovieInfoFilename [512] = {'\0',};
1059 int FCEUI_MovieGetInfo(const char* fname, MOVIE_INFO* info)
1064 fname = (const char*)convertToFCM(fname,buffer);
1065 strncpy(lastMovieInfoFilename, fname, 512);
1067 // main get info part of function
1073 FILE* fp = FCEUD_UTF8fopen(fname, "rb");
1077 read32le(&magic, fp);
1078 if(magic != MOVIE_MAGIC)
1084 read32le(&version, fp);
1085 if(version != MOVIE_VERSION)
1089 return FCEUI_MovieGetInfo_v1(fname, info);
1094 info->movie_version = MOVIE_VERSION;
1096 fread(_flags, 1, 4, fp);
1098 info->flags = _flags[0];
1099 read32le(&info->num_frames, fp);
1100 read32le(&info->rerecord_count, fp);
1102 if(access(fname, W_OK))
1103 info->read_only = 1;
1105 info->read_only = 0;
1107 fseek(fp, 12, SEEK_CUR); // skip movie_data_size, offset_to_savestate, and offset_to_movie_data
1109 fread(info->md5_of_rom_used, 1, 16, fp);
1110 info->md5_of_rom_used_present = 1;
1112 read32le(&info->emu_version_used, fp);
1114 // I probably could have planned this better...
1122 if(info->name_of_rom_used && info->name_of_rom_used_size)
1123 info->name_of_rom_used[0]='\0';
1125 r=fread(str, 1, 256, fp);
1128 for(p=0; p<r && last_c != '\0'; ++p)
1130 if(info->name_of_rom_used && info->name_of_rom_used_size && (p2 < info->name_of_rom_used_size-1))
1132 info->name_of_rom_used[p2]=str[p];
1139 memmove(str, str+p, r-p);
1143 r=fread(str, 1, 256, fp);
1149 if(info->metadata && info->metadata_size)
1150 info->metadata[0]='\0';
1154 for(p=0; p<r && last_c != '\0'; ++p)
1156 if(info->metadata && info->metadata_size && (p2 < info->metadata_size-1))
1158 info->metadata[p2]=str[p];
1165 r=fread(str, 1, 256, fp);
1170 // somehow failed to read romname and metadata
1176 // check what hacks are necessary
1177 fseek(fp, 24, SEEK_SET); // offset_to_savestate offset
1178 uint32 temp_savestate_offset;
1179 read32le(&temp_savestate_offset, fp);
1180 if(fseek(fp, temp_savestate_offset, SEEK_SET))
1185 if(!FCEUSS_LoadFP(fp,2)) return 0; // 2 -> don't really load, just load to find what's there then load backup
1198 struct MovieHeader_v1
1203 uint32 length_frames;
1204 uint32 rerecord_count;
1205 uint32 movie_data_size;
1206 uint32 offset_to_savestate;
1207 uint32 offset_to_movie_data;
1208 uint16 metadata_ucs2[]; // ucs-2, ick! sizeof(metadata) = offset_to_savestate - MOVIE_HEADER_SIZE
1212 #define MOVIE_V1_HEADER_SIZE 32
1214 static void FCEUI_LoadMovie_v1(char *fname, int _read_only)
1223 fname = fn = FCEU_MakeFName(FCEUMKF_MOVIE,CurrentMovie,0);
1226 // check movie_readonly
1227 movie_readonly = _read_only;
1228 if(access(fname, W_OK))
1232 fp = FCEUD_UTF8fopen(fname, (movie_readonly>=2) ? "rb" : "r+b");
1234 fp = fopen(fname, (movie_readonly>=2) ? "rb" : "r+b");
1252 read32le(&magic, fp);
1253 if(magic != MOVIE_MAGIC)
1259 read32le(&version, fp);
1266 fread(flags, 1, 4, fp);
1268 read32le(&rerecord_count, fp);
1269 read32le(&moviedatasize, fp);
1270 read32le(&savestate_offset, fp);
1271 read32le(&firstframeoffset, fp);
1272 if(fseek(fp, savestate_offset, SEEK_SET))
1278 if(flags[0] & MOVIE_FLAG_NOSYNCHACK)
1284 // fully reload the game to reinitialize everything before playing any movie
1285 // to try fixing nondeterministic playback of some games
1287 extern char lastLoadedGameName [2048];
1289 extern int disableBatteryLoading, suppressAddPowerCommand;
1290 suppressAddPowerCommand=1;
1291 suppressMovieStop=1;
1294 FCEUGI * gi = FCEUI_LoadGame(lastLoadedGameName);
1299 suppressMovieStop=0;
1300 suppressAddPowerCommand=0;
1304 if(!FCEUSS_LoadFP(fp,1)) return;
1308 fseek(fp, firstframeoffset, SEEK_SET);
1309 moviedata = (uint8*)realloc(moviedata, moviedatasize);
1310 fread(moviedata, 1, moviedatasize, fp);
1312 framecount = 0; // movies start at frame 0!
1314 current = CurrentMovie;
1315 slots[current] = fp;
1317 memset(joop,0,sizeof(joop));
1318 current = -1 - current;
1322 MovieStatus[CurrentMovie] = 1;
1325 FCEUI_SelectMovie(CurrentMovie,1); /* Quick hack to display status. */
1328 FCEU_DispMessage("Movie playback started.");
1332 static int FCEUI_MovieGetInfo_v1(const char* fname, MOVIE_INFO* info)
1337 uint32 savestateoffset;
1338 uint8 tmp[MOVIE_MAX_METADATA<<1];
1339 int metadata_length;
1341 FILE* fp = FCEUD_UTF8fopen(fname, "rb");
1345 read32le(&magic, fp);
1346 if(magic != MOVIE_MAGIC)
1352 read32le(&version, fp);
1359 info->movie_version = 1;
1360 info->emu_version_used = 0; // unknown
1362 fread(_flags, 1, 4, fp);
1364 info->flags = _flags[0];
1365 read32le(&info->num_frames, fp);
1366 read32le(&info->rerecord_count, fp);
1368 if(access(fname, W_OK))
1369 info->read_only = 1;
1371 info->read_only = 0;
1373 fseek(fp, 4, SEEK_CUR);
1374 read32le(&savestateoffset, fp);
1376 metadata_length = (int)savestateoffset - MOVIE_V1_HEADER_SIZE;
1377 if(metadata_length > 0)
1381 metadata_length >>= 1;
1382 if(metadata_length >= MOVIE_MAX_METADATA)
1383 metadata_length = MOVIE_MAX_METADATA-1;
1385 fseek(fp, MOVIE_V1_HEADER_SIZE, SEEK_SET);
1386 fread(tmp, 1, metadata_length<<1, fp);
1389 // turn old ucs2 metadata into utf8
1390 if(info->metadata && info->metadata_size)
1392 char* ptr=info->metadata;
1393 char* ptr_end=info->metadata+info->metadata_size-1;
1395 while(ptr<ptr_end && c_ptr<metadata_length)
1397 uint16 c=(tmp[c_ptr<<1] | (tmp[(c_ptr<<1)+1] << 8));
1401 *ptr++ = (char)(c&0x7f);
1403 case 0x80 ... 0x7ff:
1408 *ptr++=(0xc0 | (c>>6));
1409 *ptr++=(0x80 | (c & 0x3f));
1412 case 0x800 ... 0xffff:
1417 *ptr++=(0xe0 | (c>>12));
1418 *ptr++=(0x80 | ((c>>6) & 0x3f));
1419 *ptr++=(0x80 | (c & 0x3f));
1428 // md5 info not available from v1
1429 info->md5_of_rom_used_present = 0;
1430 // rom name used for the movie not available from v1
1431 if(info->name_of_rom_used && info->name_of_rom_used_size)
1432 info->name_of_rom_used[0] = '\0';
1434 // check what hacks are necessary
1435 fseek(fp, 24, SEEK_SET); // offset_to_savestate offset
1436 uint32 temp_savestate_offset;
1437 read32le(&temp_savestate_offset, fp);
1438 if(fseek(fp, temp_savestate_offset, SEEK_SET))
1443 if(!FCEUSS_LoadFP(fp,2)) return 0; // 2 -> don't really load, just load to find what's there then load backup