- refactored file access in the movie player.

This commit is contained in:
Christoph Oelckers 2019-10-20 22:48:21 +02:00
parent 449a5a4717
commit d3c2d8e96f
5 changed files with 47 additions and 55 deletions

View file

@ -36,7 +36,7 @@ typedef struct
#include "vfs.h" #include "vfs.h"
extern const char *animvpx_read_ivf_header_errmsg[7]; extern const char *animvpx_read_ivf_header_errmsg[7];
int32_t animvpx_read_ivf_header(buildvfs_kfd inhandle, animvpx_ivf_header_t *hdr); int32_t animvpx_read_ivf_header(FileReader & inhandle, animvpx_ivf_header_t *hdr);
typedef struct typedef struct
{ {
@ -48,7 +48,7 @@ typedef struct
// VVV everything that follows should be considered private! VVV // VVV everything that follows should be considered private! VVV
buildvfs_kfd inhandle; // the kread() file handle FileReader *inhandle; // the kread() file handle
// state of this struct: // state of this struct:
// 0: uninited (either not yet or already) // 0: uninited (either not yet or already)
@ -79,7 +79,7 @@ typedef struct
} animvpx_codec_ctx; } animvpx_codec_ctx;
int32_t animvpx_init_codec(const animvpx_ivf_header_t *info, buildvfs_kfd inhandle, animvpx_codec_ctx *codec); int32_t animvpx_init_codec(const animvpx_ivf_header_t *info, FileReader & inhandle, animvpx_codec_ctx *codec);
int32_t animvpx_uninit_codec(animvpx_codec_ctx *codec); int32_t animvpx_uninit_codec(animvpx_codec_ctx *codec);
extern const char *animvpx_nextpic_errmsg[8]; extern const char *animvpx_nextpic_errmsg[8];

View file

@ -28,11 +28,11 @@ const char *animvpx_read_ivf_header_errmsg[] = {
EDUKE32_STATIC_ASSERT(sizeof(animvpx_ivf_header_t) == 32); EDUKE32_STATIC_ASSERT(sizeof(animvpx_ivf_header_t) == 32);
int32_t animvpx_read_ivf_header(buildvfs_kfd inhandle, animvpx_ivf_header_t *hdr) int32_t animvpx_read_ivf_header(FileReader & inhandle, animvpx_ivf_header_t *hdr)
{ {
int32_t err; int32_t err;
if (kread(inhandle, hdr, sizeof(animvpx_ivf_header_t)) != sizeof(animvpx_ivf_header_t)) if (inhandle.Read(hdr, sizeof(animvpx_ivf_header_t)) != sizeof(animvpx_ivf_header_t))
return 1; // "couldn't read header" return 1; // "couldn't read header"
err = animvpx_check_header(hdr); err = animvpx_check_header(hdr);
@ -86,7 +86,7 @@ static void get_codec_error(animvpx_codec_ctx *codec)
} }
// no checks for double-init! // no checks for double-init!
int32_t animvpx_init_codec(const animvpx_ivf_header_t *info, buildvfs_kfd inhandle, animvpx_codec_ctx *codec) int32_t animvpx_init_codec(const animvpx_ivf_header_t *info, FileReader & inhandle, animvpx_codec_ctx *codec)
{ {
vpx_codec_dec_cfg_t cfg; vpx_codec_dec_cfg_t cfg;
@ -98,7 +98,7 @@ int32_t animvpx_init_codec(const animvpx_ivf_header_t *info, buildvfs_kfd inhand
codec->height = info->height; codec->height = info->height;
// //
codec->inhandle = inhandle; codec->inhandle = &inhandle;
codec->pic = (uint8_t *)Xcalloc(info->width*info->height,4); codec->pic = (uint8_t *)Xcalloc(info->width*info->height,4);
codec->compbuflen = codec->compbufallocsiz = 0; codec->compbuflen = codec->compbufallocsiz = 0;
@ -153,13 +153,13 @@ int32_t animvpx_uninit_codec(animvpx_codec_ctx *codec)
////////// FRAME RETRIEVAL ////////// ////////// FRAME RETRIEVAL //////////
// read one IVF/VP8 frame, which may code multiple "picture-frames" // read one IVF/VP8 frame, which may code multiple "picture-frames"
static int32_t animvpx_read_frame(buildvfs_kfd inhandle, uint8_t **bufptr, uint32_t *bufsizptr, uint32_t *bufallocsizptr) static int32_t animvpx_read_frame(FileReader & inhandle, uint8_t **bufptr, uint32_t *bufsizptr, uint32_t *bufallocsizptr)
{ {
#pragma pack(push,1) #pragma pack(push,1)
struct { uint32_t framesiz; uint64_t timestamp; } hdr; struct { uint32_t framesiz; uint64_t timestamp; } hdr;
#pragma pack(pop) #pragma pack(pop)
if (kread(inhandle, &hdr, sizeof(hdr)) != sizeof(hdr)) if (inhandle.Read(&hdr, sizeof(hdr)) != sizeof(hdr))
return 1; return 1;
if (hdr.framesiz == 0) if (hdr.framesiz == 0)
@ -184,7 +184,7 @@ static int32_t animvpx_read_frame(buildvfs_kfd inhandle, uint8_t **bufptr, uint3
*bufsizptr = hdr.framesiz; *bufsizptr = hdr.framesiz;
if (kread(inhandle, *bufptr, hdr.framesiz) != (signed)hdr.framesiz) if (inhandle.Read(*bufptr, hdr.framesiz) != (signed)hdr.framesiz)
return 3; return 3;
return 0; return 0;
@ -226,7 +226,7 @@ int32_t animvpx_nextpic(animvpx_codec_ctx *codec, uint8_t **picptr)
read_ivf_frame: read_ivf_frame:
corrupted = 0; corrupted = 0;
ret = animvpx_read_frame(codec->inhandle, &codec->compbuf, &codec->compbuflen, ret = animvpx_read_frame(*codec->inhandle, &codec->compbuf, &codec->compbuflen,
&codec->compbufallocsiz); &codec->compbufallocsiz);
if (ret == 1) if (ret == 1)
{ {

View file

@ -210,6 +210,15 @@ public:
return buffer; return buffer;
} }
TArray<uint8_t> ReadPadded(int padding)
{
TArray<uint8_t> buffer(mReader->Length + padding, true);
Size length = mReader->Read(&buffer[0], mReader->Length);
if (length < mReader->Length) buffer.Clear();
else memset(buffer.Data() + mReader->Length, 0, padding);
return buffer;
}
char *Gets(char *strbuf, Size len) char *Gets(char *strbuf, Size len)
{ {
return mReader->Gets(strbuf, (int)len); return mReader->Gets(strbuf, (int)len);

View file

@ -252,12 +252,12 @@ int32_t Anim_Play(const char *fn)
break; break;
dukeanim_t const * origanim = anim; dukeanim_t const * origanim = anim;
buildvfs_kfd handle = buildvfs_kfd_invalid; FileReader handle;
if (!Bstrcmp(dot, ".ivf")) if (!Bstrcmp(dot, ".ivf"))
{ {
handle = kopen4loadfrommod(fn, 0); handle = kopenFileReader(fn, 0);
if (handle == buildvfs_kfd_invalid) if (!handle.isOpen())
break; break;
} }
else else
{ {
@ -274,9 +274,9 @@ int32_t Anim_Play(const char *fn)
vpxfndot[3] = 'f'; vpxfndot[3] = 'f';
vpxfndot[4] = '\0'; vpxfndot[4] = '\0';
handle = kopen4loadfrommod(vpxfn, 0); handle = kopenFileReader(vpxfn, 0);
if (handle == buildvfs_kfd_invalid) if (!handle.isOpen())
break; break;
anim = Anim_Find(vpxfn); anim = Anim_Find(vpxfn);
} }
@ -287,7 +287,6 @@ int32_t Anim_Play(const char *fn)
if (i) if (i)
{ {
OSD_Printf("Failed reading IVF file: %s\n", animvpx_read_ivf_header_errmsg[i]); OSD_Printf("Failed reading IVF file: %s\n", animvpx_read_ivf_header_errmsg[i]);
kclose(handle);
return 0; return 0;
} }
@ -302,7 +301,6 @@ int32_t Anim_Play(const char *fn)
{ {
OSD_Printf("Error initializing VPX codec.\n"); OSD_Printf("Error initializing VPX codec.\n");
animvpx_restore_glstate(); animvpx_restore_glstate();
kclose(handle);
return 0; return 0;
} }
@ -411,7 +409,6 @@ int32_t Anim_Play(const char *fn)
animvpx_print_stats(&codec); animvpx_print_stats(&codec);
// //
kclose(handle);
animvpx_restore_glstate(); animvpx_restore_glstate();
animvpx_uninit_codec(&codec); animvpx_uninit_codec(&codec);
@ -424,25 +421,17 @@ int32_t Anim_Play(const char *fn)
#ifdef USE_OPENGL #ifdef USE_OPENGL
int32_t ogltexfiltermode = gltexfiltermode; int32_t ogltexfiltermode = gltexfiltermode;
#endif #endif
buildvfs_kfd handle = kopen4load(fn, 0); TArray<uint8_t> buffer;
auto fr = kopenFileReader(fn, 0);
if (handle == buildvfs_kfd_invalid) if (!fr.isOpen())
return 0; goto end_anim;
int32_t length = kfilelength(handle); buffer = fr.ReadPadded(1);
TArray<uint8_t> buffer(length + 1, true); fr.Close();
if (length <= 4)
{
OSD_Printf("Warning: skipping playback of empty ANM file \"%s\".\n", fn);
goto end_anim;
}
anim->animbuf = buffer.Data(); anim->animbuf = buffer.Data();
kread(handle, anim->animbuf, length);
kclose(handle);
uint32_t firstfour; uint32_t firstfour;
Bmemcpy(&firstfour, anim->animbuf, 4); Bmemcpy(&firstfour, anim->animbuf, 4);
@ -454,7 +443,7 @@ int32_t Anim_Play(const char *fn)
// "LPF " (.anm) // "LPF " (.anm)
if (firstfour != B_LITTLE32(0x2046504C) || if (firstfour != B_LITTLE32(0x2046504C) ||
ANIM_LoadAnim(anim->animbuf, length) < 0 || ANIM_LoadAnim(anim->animbuf, buffer.Size()-1) < 0 ||
(numframes = ANIM_NumFrames()) <= 0) (numframes = ANIM_NumFrames()) <= 0)
{ {
// XXX: ANM_LoadAnim() still checks less than the bare minimum, // XXX: ANM_LoadAnim() still checks less than the bare minimum,

View file

@ -286,11 +286,11 @@ int32_t Anim_Play(const char *fn)
break; break;
dukeanim_t const * origanim = anim; dukeanim_t const * origanim = anim;
int32_t handle = -1; FileReader handle;
if (!Bstrcmp(dot, ".ivf")) if (!Bstrcmp(dot, ".ivf"))
{ {
handle = kopen4loadfrommod(fn, 0); handle = kopenFileReader(fn, 0);
if (handle == -1) if (!handle.isOpen())
break; break;
} }
else else
@ -308,9 +308,9 @@ int32_t Anim_Play(const char *fn)
vpxfndot[3] = 'f'; vpxfndot[3] = 'f';
vpxfndot[4] = '\0'; vpxfndot[4] = '\0';
handle = kopen4loadfrommod(vpxfn, 0); handle = kopenFileReader(vpxfn, 0);
if (handle == -1) if (!handle.isOpen())
break; break;
anim = Anim_Find(vpxfn); anim = Anim_Find(vpxfn);
} }
@ -321,7 +321,6 @@ int32_t Anim_Play(const char *fn)
if (i) if (i)
{ {
OSD_Printf("Failed reading IVF file: %s\n", animvpx_read_ivf_header_errmsg[i]); OSD_Printf("Failed reading IVF file: %s\n", animvpx_read_ivf_header_errmsg[i]);
kclose(handle);
return 0; return 0;
} }
@ -336,7 +335,6 @@ int32_t Anim_Play(const char *fn)
{ {
OSD_Printf("Error initializing VPX codec.\n"); OSD_Printf("Error initializing VPX codec.\n");
animvpx_restore_glstate(); animvpx_restore_glstate();
kclose(handle);
return 0; return 0;
} }
@ -441,7 +439,6 @@ int32_t Anim_Play(const char *fn)
animvpx_print_stats(&codec); animvpx_print_stats(&codec);
// //
kclose(handle);
animvpx_restore_glstate(); animvpx_restore_glstate();
animvpx_uninit_codec(&codec); animvpx_uninit_codec(&codec);
@ -454,15 +451,15 @@ int32_t Anim_Play(const char *fn)
#ifdef USE_OPENGL #ifdef USE_OPENGL
int32_t ogltexfiltermode = gltexfiltermode; int32_t ogltexfiltermode = gltexfiltermode;
#endif #endif
int32_t handle = kopen4load(fn, 0); auto fr = kopenFileReader(fn, 0);
if (handle == -1) if (!fr.isOpen())
return 0; return 0;
int32_t length = kfilelength(handle); auto buffer = fr.ReadPadded(1);
TArray<uint8_t> buffer(length + 1, true); fr.Close();
if (length <= 4) if (buffer.Size() <= 5)
{ {
OSD_Printf("Warning: skipping playback of empty ANM file \"%s\".\n", fn); OSD_Printf("Warning: skipping playback of empty ANM file \"%s\".\n", fn);
goto end_anim; goto end_anim;
@ -472,9 +469,6 @@ int32_t Anim_Play(const char *fn)
TileFiles.tileCreate(TILE_ANIM, 200, 320); TileFiles.tileCreate(TILE_ANIM, 200, 320);
kread(handle, anim->animbuf, length);
kclose(handle);
uint32_t firstfour; uint32_t firstfour;
Bmemcpy(&firstfour, anim->animbuf, 4); Bmemcpy(&firstfour, anim->animbuf, 4);
@ -486,7 +480,7 @@ int32_t Anim_Play(const char *fn)
// "LPF " (.anm) // "LPF " (.anm)
if (firstfour != B_LITTLE32(0x2046504C) || if (firstfour != B_LITTLE32(0x2046504C) ||
ANIM_LoadAnim(anim->animbuf, length) < 0 || ANIM_LoadAnim(anim->animbuf, buffer.Size()-1) < 0 ||
(numframes = ANIM_NumFrames()) <= 0) (numframes = ANIM_NumFrames()) <= 0)
{ {
// XXX: ANM_LoadAnim() still checks less than the bare minimum, // XXX: ANM_LoadAnim() still checks less than the bare minimum,