cin: minimaze memmove of video frames

This commit is contained in:
Denis Pauk 2023-10-28 00:21:33 +03:00
parent f83d40ffe3
commit 00dcd5952b
2 changed files with 25 additions and 13 deletions

View File

@ -20,6 +20,7 @@ typedef struct cinavdecode
uint8_t *video_res; uint8_t *video_res;
long video_buffsize; long video_buffsize;
long video_pos; long video_pos;
long video_curr;
long video_frame_size; long video_frame_size;
double video_timestamp; double video_timestamp;
@ -256,7 +257,7 @@ cinavdecode_open(const char *name)
static int static int
next_frame_ready(const cinavdecode_t *state) next_frame_ready(const cinavdecode_t *state)
{ {
if (state->video_pos < state->video_frame_size) if ((state->video_pos - state->video_curr) < state->video_frame_size)
{ {
/* need more frames */ /* need more frames */
return 0; return 0;
@ -371,6 +372,19 @@ cinavdecode_next_frame(cinavdecode_t *state, uint8_t *video, uint8_t *audio)
); );
required_space = state->video_frame_size; required_space = state->video_frame_size;
/* compact frame */
if ((state->video_pos + required_space) > state->video_buffsize &&
(state->video_curr > 0))
{
memmove(state->video_res,
state->video_res + state->video_curr,
state->video_pos - state->video_curr);
state->video_pos -= state->video_curr;
state->video_curr = 0;
}
/* realloc memory */
if ((state->video_pos + required_space) > state->video_buffsize) if ((state->video_pos + required_space) > state->video_buffsize)
{ {
uint8_t *new_buffer; uint8_t *new_buffer;
@ -411,8 +425,9 @@ cinavdecode_next_frame(cinavdecode_t *state, uint8_t *video, uint8_t *audio)
/* use 2.5 second of audio buffering and 1.5 video buffer for async in /* use 2.5 second of audio buffering and 1.5 video buffer for async in
* media stream */ * media stream */
if ((state->audio_pos / state->audio_frame_size) > (state->fps * 2.5)|| if ((state->audio_pos / state->audio_frame_size) > (state->fps * 2.5) ||
(state->video_pos / state->video_frame_size) > (state->fps * 1.5)) ((state->video_pos - state->video_curr) / state->video_frame_size) >
(state->fps * 1.5))
{ {
/* flush buffer if more than 1.5 second async in video stream */ /* flush buffer if more than 1.5 second async in video stream */
break; break;
@ -431,19 +446,15 @@ cinavdecode_next_frame(cinavdecode_t *state, uint8_t *video, uint8_t *audio)
state->audio_pos = 0; state->audio_pos = 0;
} }
memcpy(video, state->video_res, state->video_frame_size); memcpy(video, state->video_res + state->video_curr, state->video_frame_size);
if (state->video_pos >= state->video_frame_size) state->video_curr += state->video_frame_size;
{ if (state->video_curr >= state->video_pos)
memmove(state->video_res, state->video_res + state->video_frame_size,
state->video_pos - state->video_frame_size);
}
state->video_pos -= state->video_frame_size;
if (state->video_pos < 0)
{ {
state->video_curr = 0;
state->video_pos = 0; state->video_pos = 0;
} }
if (!state->eof || state->video_pos || state->audio_pos) if (!state->eof || ((state->video_pos - state->video_curr) > 0) || state->audio_pos)
{ {
return 1; return 1;
} }

View File

@ -663,7 +663,8 @@ SCR_ReadNextAVFrame(void)
Com_DPrintf("Audio %.2f (%.2f): Video %.2f (%.2f)\n", Com_DPrintf("Audio %.2f (%.2f): Video %.2f (%.2f)\n",
(float)cin.av_video->audio_pos / cin.av_video->audio_frame_size, (float)cin.av_video->audio_pos / cin.av_video->audio_frame_size,
cin.av_video->audio_timestamp, cin.av_video->audio_timestamp,
(float)cin.av_video->video_pos / cin.av_video->video_frame_size, (float)(cin.av_video->video_pos - cin.av_video->video_curr) /
cin.av_video->video_frame_size,
cin.av_video->video_timestamp); cin.av_video->video_timestamp);
/* force untransparent image show */ /* force untransparent image show */