From ef719dc9386612f76a1df2fbb60915b5fc0bed57 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 25 Jul 2021 13:26:45 -0500 Subject: [PATCH] Cleanup and optimize FFmpeg code --- src/video/ffmpeg.c | 24 +++++++++++++----------- src/video/ffmpeg.h | 12 +----------- src/video/sdl.c | 7 +++---- src/video/x11.c | 13 ++++++++----- 4 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/video/ffmpeg.c b/src/video/ffmpeg.c index 764ea39..d34849a 100644 --- a/src/video/ffmpeg.c +++ b/src/video/ffmpeg.c @@ -80,20 +80,22 @@ int ffmpeg_init(int videoFormat, int width, int height, int perf_lvl, int buffer return -1; } - if (perf_lvl & DISABLE_LOOP_FILTER) - // Skip the loop filter for performance reasons - decoder_ctx->skip_loop_filter = AVDISCARD_ALL; + // Use low delay decoding + decoder_ctx->flags |= AV_CODEC_FLAG_LOW_DELAY; - if (perf_lvl & LOW_LATENCY_DECODE) - // Use low delay single threaded encoding - decoder_ctx->flags |= AV_CODEC_FLAG_LOW_DELAY; + // Allow display of corrupt frames and frames missing references + decoder_ctx->flags |= AV_CODEC_FLAG_OUTPUT_CORRUPT; + decoder_ctx->flags2 |= AV_CODEC_FLAG2_SHOW_ALL; - if (perf_lvl & SLICE_THREADING) + // Report decoding errors to allow us to request a key frame + decoder_ctx->err_recognition = AV_EF_EXPLODE; + + if (perf_lvl & SLICE_THREADING) { decoder_ctx->thread_type = FF_THREAD_SLICE; - else - decoder_ctx->thread_type = FF_THREAD_FRAME; - - decoder_ctx->thread_count = thread_count; + decoder_ctx->thread_count = thread_count; + } else { + decoder_ctx->thread_count = 1; + } decoder_ctx->width = width; decoder_ctx->height = height; diff --git a/src/video/ffmpeg.h b/src/video/ffmpeg.h index 1fab434..a6e9af8 100644 --- a/src/video/ffmpeg.h +++ b/src/video/ffmpeg.h @@ -21,18 +21,8 @@ #include -// Disables the deblocking filter at the cost of image quality -#define DISABLE_LOOP_FILTER 0x1 -// Uses the low latency decode flag (disables multithreading) -#define LOW_LATENCY_DECODE 0x2 -// Threads process each slice, rather than each frame +// Enable multi-threaded decoding #define SLICE_THREADING 0x4 -// Uses nonstandard speedup tricks -#define FAST_DECODE 0x8 -// Uses bilinear filtering instead of bicubic -#define BILINEAR_FILTERING 0x10 -// Uses a faster bilinear filtering with lower image quality -#define FAST_BILINEAR_FILTERING 0x20 // Uses hardware acceleration #define VDPAU_ACCELERATION 0x40 #define VAAPI_ACCELERATION 0x80 diff --git a/src/video/sdl.c b/src/video/sdl.c index ef31291..992287a 100644 --- a/src/video/sdl.c +++ b/src/video/sdl.c @@ -29,13 +29,12 @@ #include #define DECODER_BUFFER_SIZE 92*1024 +#define SLICES_PER_FRAME 4 static char* ffmpeg_buffer; static int sdl_setup(int videoFormat, int width, int height, int redrawRate, void* context, int drFlags) { - int avc_flags = SLICE_THREADING; - - if (ffmpeg_init(videoFormat, width, height, avc_flags, SDL_BUFFER_FRAMES, sysconf(_SC_NPROCESSORS_ONLN)) < 0) { + if (ffmpeg_init(videoFormat, width, height, SLICE_THREADING, SDL_BUFFER_FRAMES, SLICES_PER_FRAME) < 0) { fprintf(stderr, "Couldn't initialize video decoding\n"); return -1; } @@ -93,5 +92,5 @@ DECODER_RENDERER_CALLBACKS decoder_callbacks_sdl = { .setup = sdl_setup, .cleanup = sdl_cleanup, .submitDecodeUnit = sdl_submit_decode_unit, - .capabilities = CAPABILITY_SLICES_PER_FRAME(4) | CAPABILITY_REFERENCE_FRAME_INVALIDATION_AVC | CAPABILITY_DIRECT_SUBMIT, + .capabilities = CAPABILITY_SLICES_PER_FRAME(SLICES_PER_FRAME) | CAPABILITY_REFERENCE_FRAME_INVALIDATION_AVC | CAPABILITY_DIRECT_SUBMIT, }; diff --git a/src/video/x11.c b/src/video/x11.c index e15ecb1..13bf3f7 100644 --- a/src/video/x11.c +++ b/src/video/x11.c @@ -40,6 +40,7 @@ #define DECODER_BUFFER_SIZE 92*1024 #define X11_VDPAU_ACCELERATION ENABLE_HARDWARE_ACCELERATION_1 #define X11_VAAPI_ACCELERATION ENABLE_HARDWARE_ACCELERATION_2 +#define SLICES_PER_FRAME 4 static char* ffmpeg_buffer = NULL; @@ -124,13 +125,15 @@ int x11_setup(int videoFormat, int width, int height, int redrawRate, void* cont } XFlush(display); - int avc_flags = SLICE_THREADING; + int avc_flags; if (drFlags & X11_VDPAU_ACCELERATION) - avc_flags |= VDPAU_ACCELERATION; + avc_flags = VDPAU_ACCELERATION; else if (drFlags & X11_VAAPI_ACCELERATION) - avc_flags |= VAAPI_ACCELERATION; + avc_flags = VAAPI_ACCELERATION; + else + avc_flags = SLICE_THREADING; - if (ffmpeg_init(videoFormat, width, height, avc_flags, 2, 2) < 0) { + if (ffmpeg_init(videoFormat, width, height, avc_flags, 2, SLICES_PER_FRAME) < 0) { fprintf(stderr, "Couldn't initialize video decoding\n"); return -1; } @@ -185,7 +188,7 @@ DECODER_RENDERER_CALLBACKS decoder_callbacks_x11 = { .setup = x11_setup, .cleanup = x11_cleanup, .submitDecodeUnit = x11_submit_decode_unit, - .capabilities = CAPABILITY_SLICES_PER_FRAME(4) | CAPABILITY_REFERENCE_FRAME_INVALIDATION_AVC | CAPABILITY_DIRECT_SUBMIT, + .capabilities = CAPABILITY_SLICES_PER_FRAME(SLICES_PER_FRAME) | CAPABILITY_REFERENCE_FRAME_INVALIDATION_AVC | CAPABILITY_DIRECT_SUBMIT, }; DECODER_RENDERER_CALLBACKS decoder_callbacks_x11_vdpau = {