Cleanup and optimize FFmpeg code

This commit is contained in:
Cameron Gutman
2021-07-25 13:26:45 -05:00
parent 66ec344a31
commit ef719dc938
4 changed files with 25 additions and 31 deletions

View File

@@ -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;

View File

@@ -21,18 +21,8 @@
#include <libavcodec/avcodec.h>
// 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

View File

@@ -29,13 +29,12 @@
#include <stdbool.h>
#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,
};

View File

@@ -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 = {