Skip to content

Commit 2a817f9

Browse files
committed
Add AV1 decoder extern and improve codec safety checks
- Add missing extern declaration for ff_av1_decoder - Add fallback to direct codec lookup for AV1 - Add safety checks for codec function pointers to prevent segfaults
1 parent b104741 commit 2a817f9

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

avcodec.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern AVCodec ff_hevc_decoder;
3232
extern AVCodec ff_mpeg4_decoder;
3333
extern AVCodec ff_vp9_decoder;
3434
extern AVCodec ff_vp8_decoder;
35+
extern AVCodec ff_av1_decoder;
3536
extern AVCodec ff_mp3_decoder;
3637
extern AVCodec ff_flac_decoder;
3738
extern AVCodec ff_aac_decoder;
@@ -243,6 +244,9 @@ avcodec_decoder avcodec_decoder_create(const opencv_mat buf, const bool hevc_ena
243244
const AVCodec* libaom_codec = avcodec_find_decoder_by_name("libaom-av1");
244245
if (libaom_codec) {
245246
codec = libaom_codec;
247+
} else {
248+
// If no named decoder found, try the direct codec lookup
249+
codec = avcodec_find_decoder(AV_CODEC_ID_AV1);
246250
}
247251
}
248252
}
@@ -366,9 +370,22 @@ int avcodec_decoder_get_orientation(const avcodec_decoder d)
366370
rotation = atoi(tag->value);
367371
}
368372
else {
369-
// For now, skip display matrix rotation detection to avoid deprecated API
370-
// Most rotation information is available via metadata tags above
371-
rotation = 0;
373+
uint8_t* displaymatrix = NULL;
374+
const AVPacketSideData* sd = NULL;
375+
376+
// access side data from codecpar instead of directly from the stream
377+
AVCodecParameters* codecpar = d->container->streams[d->video_stream_index]->codecpar;
378+
for (int i = 0; i < codecpar->nb_coded_side_data; i++) {
379+
if (codecpar->coded_side_data[i].type == AV_PKT_DATA_DISPLAYMATRIX) {
380+
sd = &codecpar->coded_side_data[i];
381+
break;
382+
}
383+
}
384+
385+
displaymatrix = sd ? sd->data : NULL;
386+
if (displaymatrix) {
387+
rotation = (360 - (int)(av_display_rotation_get((const int32_t*)displaymatrix))) % 360;
388+
}
372389
}
373390
switch (rotation) {
374391
case 90:
@@ -442,6 +459,11 @@ static int avcodec_decoder_copy_frame(const avcodec_decoder d, opencv_mat mat, A
442459
return -1;
443460
}
444461

462+
// Extra safety check for AV1 decoder context
463+
if (!d->codec->codec || !d->codec->codec->decode) {
464+
return AVERROR(EINVAL);
465+
}
466+
445467
int res = avcodec_receive_frame(d->codec, frame);
446468
if (res >= 0) {
447469
// Calculate the step size based on the cv::Mat's width

0 commit comments

Comments
 (0)