2017-12-12 22:12:07 +08:00
|
|
|
#include "frames.h"
|
|
|
|
|
2018-02-09 02:23:24 +08:00
|
|
|
#include <SDL2/SDL_assert.h>
|
|
|
|
#include <SDL2/SDL_log.h>
|
2017-12-12 22:12:07 +08:00
|
|
|
#include <SDL2/SDL_mutex.h>
|
|
|
|
#include <libavutil/avutil.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
|
2018-02-09 02:23:24 +08:00
|
|
|
#include "config.h"
|
|
|
|
#include "lockutil.h"
|
|
|
|
|
2017-12-15 18:27:11 +08:00
|
|
|
SDL_bool frames_init(struct frames *frames) {
|
2017-12-12 22:12:07 +08:00
|
|
|
if (!(frames->decoding_frame = av_frame_alloc())) {
|
|
|
|
goto error_0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(frames->rendering_frame = av_frame_alloc())) {
|
|
|
|
goto error_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(frames->mutex = SDL_CreateMutex())) {
|
|
|
|
goto error_2;
|
|
|
|
}
|
|
|
|
|
2018-02-07 19:25:52 +08:00
|
|
|
#ifndef SKIP_FRAMES
|
2017-12-12 22:12:07 +08:00
|
|
|
if (!(frames->rendering_frame_consumed_cond = SDL_CreateCond())) {
|
2018-02-07 19:25:52 +08:00
|
|
|
SDL_DestroyMutex(frames->mutex);
|
|
|
|
goto error_2;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
2018-02-09 15:42:39 +08:00
|
|
|
frames->stopped = SDL_FALSE;
|
2018-02-07 19:25:52 +08:00
|
|
|
#endif
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2018-02-09 02:23:24 +08:00
|
|
|
// there is initially no rendering frame, so consider it has already been
|
|
|
|
// consumed
|
2017-12-12 22:12:07 +08:00
|
|
|
frames->rendering_frame_consumed = SDL_TRUE;
|
|
|
|
|
2017-12-15 18:27:11 +08:00
|
|
|
return SDL_TRUE;
|
2017-12-12 22:12:07 +08:00
|
|
|
|
|
|
|
error_2:
|
|
|
|
av_frame_free(&frames->rendering_frame);
|
|
|
|
error_1:
|
|
|
|
av_frame_free(&frames->decoding_frame);
|
|
|
|
error_0:
|
2017-12-15 18:27:11 +08:00
|
|
|
return SDL_FALSE;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void frames_destroy(struct frames *frames) {
|
2018-02-07 19:25:52 +08:00
|
|
|
#ifndef SKIP_FRAMES
|
2017-12-15 18:12:02 +08:00
|
|
|
SDL_DestroyCond(frames->rendering_frame_consumed_cond);
|
2018-02-07 19:25:52 +08:00
|
|
|
#endif
|
2017-12-18 23:26:38 +08:00
|
|
|
SDL_DestroyMutex(frames->mutex);
|
2017-12-12 22:12:07 +08:00
|
|
|
av_frame_free(&frames->rendering_frame);
|
|
|
|
av_frame_free(&frames->decoding_frame);
|
|
|
|
}
|
|
|
|
|
2018-02-09 02:23:24 +08:00
|
|
|
static void frames_swap(struct frames *frames) {
|
2017-12-12 22:12:07 +08:00
|
|
|
AVFrame *tmp = frames->decoding_frame;
|
|
|
|
frames->decoding_frame = frames->rendering_frame;
|
|
|
|
frames->rendering_frame = tmp;
|
|
|
|
}
|
2018-02-09 02:23:24 +08:00
|
|
|
|
|
|
|
SDL_bool frames_offer_decoded_frame(struct frames *frames) {
|
|
|
|
mutex_lock(frames->mutex);
|
|
|
|
#ifndef SKIP_FRAMES
|
|
|
|
// if SKIP_FRAMES is disabled, then the decoder must wait for the current
|
|
|
|
// frame to be consumed
|
2018-02-09 15:42:39 +08:00
|
|
|
while (!frames->rendering_frame_consumed && !frames->stopped) {
|
2018-02-09 02:23:24 +08:00
|
|
|
cond_wait(frames->rendering_frame_consumed_cond, frames->mutex);
|
|
|
|
}
|
|
|
|
#else
|
2018-02-09 15:42:39 +08:00
|
|
|
if (!frames->rendering_frame_consumed) {
|
2018-02-12 23:06:53 +08:00
|
|
|
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Skip frame");
|
2018-02-09 02:23:24 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
frames_swap(frames);
|
2018-02-09 15:42:39 +08:00
|
|
|
|
|
|
|
SDL_bool previous_frame_consumed = frames->rendering_frame_consumed;
|
2018-02-09 02:23:24 +08:00
|
|
|
frames->rendering_frame_consumed = SDL_FALSE;
|
2018-02-09 15:42:39 +08:00
|
|
|
|
2018-02-09 02:23:24 +08:00
|
|
|
mutex_unlock(frames->mutex);
|
|
|
|
return previous_frame_consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AVFrame *frames_consume_rendered_frame(struct frames *frames) {
|
|
|
|
SDL_assert(!frames->rendering_frame_consumed);
|
|
|
|
frames->rendering_frame_consumed = SDL_TRUE;
|
|
|
|
#ifndef SKIP_FRAMES
|
|
|
|
// if SKIP_FRAMES is disabled, then notify the decoder the current frame is
|
|
|
|
// consumed, so that it may push a new one
|
|
|
|
cond_signal(frames->rendering_frame_consumed_cond);
|
|
|
|
#endif
|
|
|
|
return frames->rendering_frame;
|
|
|
|
}
|
2018-02-09 15:42:39 +08:00
|
|
|
|
|
|
|
void frames_stop(struct frames *frames) {
|
|
|
|
#ifdef SKIP_FRAMES
|
|
|
|
(void) frames; // unused
|
|
|
|
#else
|
|
|
|
mutex_lock(frames->mutex);
|
|
|
|
frames->stopped = SDL_TRUE;
|
|
|
|
mutex_unlock(frames->mutex);
|
|
|
|
// wake up blocking wait
|
|
|
|
cond_signal(frames->rendering_frame_consumed_cond);
|
|
|
|
#endif
|
|
|
|
}
|