workaround for incorrect demuxer detection in libavformat

This commit is contained in:
byte[] 2021-04-13 16:12:22 -04:00
parent 7cb5f3ae2f
commit 884115e0f7
5 changed files with 52 additions and 3 deletions

View File

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

View File

@ -6,6 +6,7 @@
#include <unistd.h>
#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;
}

View File

@ -1,6 +1,7 @@
#include <libavformat/avformat.h>
#include <stdio.h>
#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;
}

40
src/util.c Normal file
View File

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

7
src/util.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef _UTIL_H_DEFINED
#define _UTIL_H_DEFINED
#include <libavformat/avformat.h>
int open_input_correct_demuxer(AVFormatContext **ctx, const char *filename);
#endif