Skip to content

Commit 2a415ad

Browse files
Fix crash when the pixel format is unknown in frames (#794)
### Fix crash when the pixel format is unknown in frames ### Linked issues n/a ### Summarize your change. Save the pixel format when the media is opened and use that as fallback if the pixel format of some frames can not be detected. This assumes that there is a pixel format when opening the file and if it does not have one, it will fail at that point before getting to `decodeImageAtFrame`. ### Describe the reason for the change. OpenRV was crashing on some AV1 media because the pixel format was unknown for all the frames. ### Describe what you have tested and on which operating system. MacOS ### Add a list of changes, and note any that might need special attention during the review. ### If possible, provide screenshots. Signed-off-by: Cédrik Fuoco <[email protected]>
1 parent d0470e1 commit 2a415ad

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/lib/image/MovieFFMpeg/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ TARGET_INCLUDE_DIRECTORIES(
2424

2525
TARGET_LINK_LIBRARIES(
2626
${_target}
27-
PUBLIC TwkMovie
27+
PUBLIC TwkMovie ffmpeg::avutil
2828
PRIVATE mp4v2Utils
2929
Boost::filesystem
3030
TwkUtil
3131
TwkFB
3232
ffmpeg::avcodec
3333
ffmpeg::avformat
34-
ffmpeg::avutil
3534
ffmpeg::swscale
3635
)
3736

src/lib/image/MovieFFMpeg/MovieFFMpeg.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,7 @@ namespace TwkMovie
14801480
avcodec_free_context(avCodecContext);
14811481
return false;
14821482
}
1483+
m_pxlFormatOnOpen = nativeFormat;
14831484
}
14841485

14851486
//
@@ -3912,6 +3913,26 @@ namespace TwkMovie
39123913
AVPixelFormat nativeFormat = videoCodecContext->sw_pix_fmt;
39133914
outFrame->format = nativeFormat;
39143915
const AVPixFmtDescriptor* desc = av_pix_fmt_desc_get(nativeFormat);
3916+
if (!desc && nativeFormat == AV_PIX_FMT_NONE)
3917+
{
3918+
static bool warned = false;
3919+
if (!warned)
3920+
{
3921+
std::cout << "WARNING: FFmpeg detected pixel format "
3922+
"AV_PIX_FMT_NONE for frames in "
3923+
<< m_filename << ". Using fallback pixel format: "
3924+
<< av_get_pix_fmt_name(m_pxlFormatOnOpen)
3925+
<< std::endl;
3926+
warned = true;
3927+
}
3928+
// Use the pixel format detected when the file was opened as a
3929+
// fallback. Assumes that m_pxlFormatOnOpen is set because the file
3930+
// was opened.
3931+
outFrame->format = m_pxlFormatOnOpen;
3932+
nativeFormat = m_pxlFormatOnOpen;
3933+
desc = av_pix_fmt_desc_get(nativeFormat);
3934+
}
3935+
39153936
int bitSize = desc->comp[0].depth - desc->comp[0].shift;
39163937
int numPlanes = 0;
39173938
bool hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA);

src/lib/image/MovieFFMpeg/MovieFFMpeg/MovieFFMpeg.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <TwkMovie/MovieWriter.h>
1212
#include <TwkMovie/MovieIO.h>
1313
#include <stdint.h>
14+
extern "C"
15+
{
16+
#include <libavutil/pixfmt.h>
17+
}
1418

1519
//
1620
// AVClass Forward Declaration Placeholders
@@ -284,6 +288,7 @@ namespace TwkMovie
284288
AudioState* m_audioState;
285289
bool m_cloning{false};
286290
bool m_mustReadFirstFrame{false};
291+
AVPixelFormat m_pxlFormatOnOpen{AV_PIX_FMT_NONE};
287292

288293
friend class ContextPool;
289294
};

0 commit comments

Comments
 (0)