From 884115e0f789cf0971e172b6b93c00d4547d04bd Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Tue, 13 Apr 2021 16:12:22 -0400 Subject: [PATCH] workaround for incorrect demuxer detection in libavformat --- Makefile | 2 +- src/stat.c | 3 ++- src/thumb.c | 3 ++- src/util.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/util.h | 7 +++++++ 5 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/util.c create mode 100644 src/util.h diff --git a/Makefile b/Makefile index 784eb8f..79d300d 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ LDFLAGS := INSTALL ?= install PREFIX ?= /usr/local -COMMON_OBJECTS := build/validation.o build/png.o +COMMON_OBJECTS := build/validation.o build/png.o build/util.o MEDIASTAT_OBJECTS := build/stat.o MEDIATHUMB_OBJECTS := build/thumb.o diff --git a/src/stat.c b/src/stat.c index feb6866..574aa71 100644 --- a/src/stat.c +++ b/src/stat.c @@ -6,6 +6,7 @@ #include #include "validation.h" +#include "util.h" static int64_t start_time(AVStream *stream) { @@ -34,7 +35,7 @@ int main(int argc, char *argv[]) return -1; } - if (avformat_open_input(&format, argv[1], NULL, NULL) != 0) { + if (open_input_correct_demuxer(&format, argv[1]) != 0) { printf("Couldn't read file\n"); return -1; } diff --git a/src/thumb.c b/src/thumb.c index ebcfdca..cd47d6c 100644 --- a/src/thumb.c +++ b/src/thumb.c @@ -1,6 +1,7 @@ #include #include #include "png.h" +#include "util.h" int main(int argc, char *argv[]) { @@ -24,7 +25,7 @@ int main(int argc, char *argv[]) const char *output = argv[3]; AVRational time = av_d2q(atof(argv[2]), INT_MAX); - if (avformat_open_input(&format, input, NULL, NULL) != 0) { + if (open_input_correct_demuxer(&format, input) != 0) { printf("Couldn't read file\n"); return -1; } diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..5df407c --- /dev/null +++ b/src/util.c @@ -0,0 +1,40 @@ +#include "util.h" + +static int valid_demuxer(AVInputFormat *fmt) +{ + // apng: animated PNG + // png_pipe: static PNG + // image2: JPEG + // gif: GIF + // svg_pipe: SVG (recommended not to use this currently) + // matroska: MKV/WebM + + return + fmt == av_find_input_format("apng") || + fmt == av_find_input_format("png_pipe") || + fmt == av_find_input_format("image2") || + fmt == av_find_input_format("gif") || + fmt == av_find_input_format("svg_pipe") || + fmt == av_find_input_format("matroska"); +} + +static AVInputFormat *image2_demuxer() +{ + return av_find_input_format("image2"); +} + +int open_input_correct_demuxer(AVFormatContext **ctx, const char *filename) +{ + if (avformat_open_input(ctx, filename, NULL, NULL) < 0) { + return -1; + } + + // Should usually happen + if (valid_demuxer((*ctx)->iformat)) { + return 0; + } + + // Wrong demuxer, force to image2 so we don't error out here + avformat_close_input(ctx); + return avformat_open_input(ctx, filename, image2_demuxer(), NULL); +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..0c31f9c --- /dev/null +++ b/src/util.h @@ -0,0 +1,7 @@ +#ifndef _UTIL_H_DEFINED +#define _UTIL_H_DEFINED + +#include +int open_input_correct_demuxer(AVFormatContext **ctx, const char *filename); + +#endif