From 5dbf9531ea9fa077bee19eb790994ed83702a627 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Sun, 20 Sep 2020 10:17:49 +0200 Subject: [PATCH] ffmpeg: remove some asserts in aspect ratio computations Use a default or undefined aspect ratio when ffmpeg won't tell us the video size, rather than crashing the whole app. --- src/add-ons/media/plugins/ffmpeg/Utilities.h | 39 +++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/add-ons/media/plugins/ffmpeg/Utilities.h b/src/add-ons/media/plugins/ffmpeg/Utilities.h index 32674fbf0d..4246bec605 100644 --- a/src/add-ons/media/plugins/ffmpeg/Utilities.h +++ b/src/add-ons/media/plugins/ffmpeg/Utilities.h @@ -14,6 +14,7 @@ #include +#include #include @@ -44,13 +45,15 @@ inline void ConvertAVCodecContextToVideoAspectWidthAndHeight(AVCodecContext& contextIn, uint16& pixelWidthAspectOut, uint16& pixelHeightAspectOut) { - assert(contextIn.sample_aspect_ratio.num >= 0); - assert(contextIn.width > 0); - assert(contextIn.height > 0); + if (contextIn.width <= 0 || contextIn.height <= 0) { + fprintf(stderr, "Cannot compute video aspect ratio correctly\n"); + pixelWidthAspectOut = 1; + pixelHeightAspectOut = 1; + return; + } + + assert(contextIn.sample_aspect_ratio.num >= 0); - // The following code is based on code originally located in - // AVFormatReader::Stream::Init() and thus should be copyrighted to Stephan - // Aßmus AVRational pixelAspectRatio; if (contextIn.sample_aspect_ratio.num == 0 @@ -80,13 +83,15 @@ inline void ConvertAVCodecParametersToVideoAspectWidthAndHeight(AVCodecParameters& parametersIn, uint16& pixelWidthAspectOut, uint16& pixelHeightAspectOut) { - assert(parametersIn.sample_aspect_ratio.num >= 0); - assert(parametersIn.width > 0); - assert(parametersIn.height > 0); + if (parametersIn.width <= 0 || parametersIn.height <= 0) { + fprintf(stderr, "Cannot compute video aspect ratio correctly\n"); + pixelWidthAspectOut = 1; + pixelHeightAspectOut = 1; + return; + } + + assert(parametersIn.sample_aspect_ratio.num >= 0); - // The following code is based on code originally located in - // AVFormatReader::Stream::Init() and thus should be copyrighted to Stephan - // Aßmus AVRational pixelAspectRatio; if (parametersIn.sample_aspect_ratio.num == 0 @@ -136,10 +141,16 @@ inline void ConvertVideoAspectWidthAndHeightToAVCodecContext(uint16 pixelWidthAspectIn, uint16 pixelHeightAspectIn, AVCodecContext& contextInOut) { + if (contextInOut.width <= 0 || contextInOut.height <= 0) { + fprintf(stderr, "Cannot compute video aspect ratio correctly\n"); + // We can't do anything, set the aspect ratio to 'ignore'. + contextInOut.sample_aspect_ratio.num = 0; + contextInOut.sample_aspect_ratio.den = 1; + return; + } + assert(pixelWidthAspectIn > 0); assert(pixelHeightAspectIn > 0); - assert(contextInOut.width > 0); - assert(contextInOut.height > 0); AVRational pureVideoDimensionAspectRatio; av_reduce(&pureVideoDimensionAspectRatio.num,