Skip to content

Commit 7792f1b

Browse files
committed
fix bugs seeking back during record
1 parent c5b2e02 commit 7792f1b

1 file changed

Lines changed: 17 additions & 22 deletions

File tree

input/bsv/bsvmovie.c

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ bool bsv_movie_reset_recording(bsv_movie_t *handle)
149149
handle->cur_save_valid = false;
150150

151151
intfstream_seek(handle->file, REPLAY_HEADER_LEN_BYTES, SEEK_SET);
152-
intfstream_truncate(handle->file, REPLAY_HEADER_LEN_BYTES);
153152

154153
intfstream_write(handle->file, &compression, 1);
155154
intfstream_write(handle->file, &encoding, 1);
@@ -241,7 +240,7 @@ void bsv_movie_frame_rewind()
241240
RARCH_LOG("[REPLAY] rewound to %d\n", handle->frame_counter);
242241
intfstream_seek(handle->file, (int)handle->frame_pos[handle->frame_counter & handle->frame_mask], SEEK_SET);
243242
if (recording)
244-
intfstream_truncate(handle->file, (int)handle->frame_pos[handle->frame_counter & handle->frame_mask]);
243+
intfstream_truncate(handle->file, intfstream_tell(handle->file));
245244
else
246245
bsv_movie_read_next_events(handle, REPLAY_CPBEHAVIOR_DESERIALIZE, true);
247246
}
@@ -251,19 +250,11 @@ void bsv_movie_frame_rewind()
251250
RARCH_LOG("[Replay] rewound past beginning\n");
252251
/* We rewound past the beginning. */
253252
if (handle->playback)
254-
{
255-
intfstream_seek(handle->file, (int)handle->min_file_pos, SEEK_SET);
256-
#ifdef HAVE_STATESTREAM
257-
if (handle->superblocks)
258-
uint32s_index_remove_after(handle->superblocks, 0);
259-
if (handle->blocks)
260-
uint32s_index_remove_after(handle->blocks, 0);
261-
#endif
262-
bsv_movie_read_next_events(handle, REPLAY_CPBEHAVIOR_DESERIALIZE, true);
263-
}
253+
bsv_movie_reset_playback(handle);
264254
else
265255
{
266256
bsv_movie_reset_recording(handle);
257+
intfstream_truncate(handle->file, intfstream_tell(handle->file));
267258
}
268259
}
269260
}
@@ -844,6 +835,10 @@ void bsv_movie_next_frame(input_driver_state_t *input_st)
844835
/* write "next frame is not a checkpoint" */
845836
intfstream_write(handle->file, (uint8_t *)(&frame_tok), sizeof(uint8_t));
846837
}
838+
/* To support seeking forwards during a paused replay, we would
839+
need to *not* truncate here if we are in the "just paused,
840+
running a frame to get the updated image, then will pause
841+
again" state. */
847842
intfstream_truncate(handle->file, intfstream_tell(handle->file));
848843
}
849844

@@ -1591,7 +1586,7 @@ bool bsv_movie_read_deduped_state(bsv_movie_t *movie, uint8_t *encoded, size_t e
15911586
if(!ret)
15921587
{
15931588
RARCH_ERR("[STATESTREAM] made it to end without superblock seq\n");
1594-
abort();
1589+
return false;
15951590
}
15961591
total_decode_micros += cpu_features_get_time_usec() - start;
15971592
RARCH_DBG("[STATESTREAM] Total statestream decodes %d ; net time (secs): %f\n", total_decode_count, (double)total_decode_micros / (1000000.0));
@@ -1730,28 +1725,28 @@ bool movie_seek_to_frame(input_driver_state_t *input_st, int64_t frame)
17301725
}
17311726
bool bsv_movie_seek_to_pos_impl(bsv_movie_t *movie, int64_t pos)
17321727
{
1728+
/* TODO:
1729+
1. fix under "no previous replay" while recording
1730+
2. fix under "some previous replay" while recording
1731+
*/
17331732
int64_t movie_pos;
17341733
bool ret;
17351734
if (!movie || movie->version == 0)
17361735
return false;
17371736
movie_pos = intfstream_tell(movie->file);
17381737
/* assume file is at a frame boundary and frame is at a checkpoint boundary. */
17391738
if (pos < movie_pos)
1740-
{
17411739
/* TODO: this could be made more efficient with backrefs if we
17421740
had a way to scan backwards; we wouldn't need to reset to go
17431741
backwards. */
1744-
if (movie->playback)
1745-
bsv_movie_reset_playback(movie);
1746-
else
1747-
bsv_movie_reset_recording(movie);
1748-
}
1742+
/* It seems strange, but we want `reset_playback` here and not
1743+
`reset_recording`, even if the movie is in record mode. This
1744+
is because we don't want to re-serialize the initial state or
1745+
whatever and act "as if" we just started recording. */
1746+
bsv_movie_reset_playback(movie);
17491747
if (pos != movie_pos)
17501748
bsv_movie_scan_to(movie, pos);
17511749
ret = bsv_movie_read_next_events(movie, REPLAY_CPBEHAVIOR_DESERIALIZE, false);
1752-
/* truncate recording after seek */
1753-
if (!movie->playback)
1754-
intfstream_truncate(movie->file, intfstream_tell(movie->file));
17551750
return ret;
17561751
}
17571752

0 commit comments

Comments
 (0)