Skip to content

Commit b94aa34

Browse files
committed
obs-ffmpeg: Fix hardware decode failed with av1 video
1 parent 4aa41ec commit b94aa34

File tree

1 file changed

+32
-24
lines changed
  • deps/media-playback/media-playback

1 file changed

+32
-24
lines changed

deps/media-playback/media-playback/decode.c

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,51 +27,56 @@ enum AVHWDeviceType hw_priority[] = {
2727
AV_HWDEVICE_TYPE_VIDEOTOOLBOX, AV_HWDEVICE_TYPE_NONE,
2828
};
2929

30-
static bool has_hw_type(const AVCodec *c, enum AVHWDeviceType type,
30+
static bool has_hw_type(const AVCodec **c, enum AVHWDeviceType type,
3131
enum AVPixelFormat *hw_format)
3232
{
33-
for (int i = 0;; i++) {
34-
const AVCodecHWConfig *config = avcodec_get_hw_config(c, i);
35-
if (!config) {
36-
break;
37-
}
33+
const AVCodec *codec = NULL;
34+
void *iterator = NULL;
35+
while ((codec = av_codec_iterate(&iterator))) {
36+
if (codec->id != (*c)->id || codec->type != AVMEDIA_TYPE_VIDEO ||
37+
!av_codec_is_decoder(codec))
38+
continue;
39+
for (int i = 0;; i++) {
40+
const AVCodecHWConfig *config =
41+
avcodec_get_hw_config(codec, i);
42+
if (!config) {
43+
break;
44+
}
3845

39-
if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
40-
config->device_type == type) {
41-
*hw_format = config->pix_fmt;
42-
return true;
46+
if (config->methods &
47+
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
48+
config->device_type == type) {
49+
*hw_format = config->pix_fmt;
50+
*c = codec;
51+
return true;
52+
}
4353
}
4454
}
4555

4656
return false;
4757
}
4858

49-
static void init_hw_decoder(struct mp_decode *d, AVCodecContext *c)
59+
static void init_hw_decoder(struct mp_decode *d, AVBufferRef **hw_ctx)
5060
{
5161
enum AVHWDeviceType *priority = hw_priority;
52-
AVBufferRef *hw_ctx = NULL;
5362

5463
while (*priority != AV_HWDEVICE_TYPE_NONE) {
55-
if (has_hw_type(d->codec, *priority, &d->hw_format)) {
56-
int ret = av_hwdevice_ctx_create(&hw_ctx, *priority,
64+
if (has_hw_type(&d->codec, *priority, &d->hw_format)) {
65+
int ret = av_hwdevice_ctx_create(hw_ctx, *priority,
5766
NULL, NULL, 0);
5867
if (ret == 0)
5968
break;
6069
}
6170

6271
priority++;
6372
}
64-
65-
if (hw_ctx) {
66-
c->hw_device_ctx = av_buffer_ref(hw_ctx);
67-
c->opaque = d;
68-
d->hw_ctx = hw_ctx;
69-
d->hw = true;
70-
}
7173
}
7274

7375
static int mp_open_codec(struct mp_decode *d, bool hw)
7476
{
77+
AVBufferRef *hw_ctx = NULL;
78+
if (hw)
79+
init_hw_decoder(d, &hw_ctx);
7580
AVCodecContext *c;
7681
int ret;
7782

@@ -86,9 +91,12 @@ static int mp_open_codec(struct mp_decode *d, bool hw)
8691
goto fail;
8792

8893
d->hw = false;
89-
90-
if (hw)
91-
init_hw_decoder(d, c);
94+
if (hw_ctx) {
95+
c->hw_device_ctx = av_buffer_ref(hw_ctx);
96+
c->opaque = d;
97+
d->hw_ctx = hw_ctx;
98+
d->hw = true;
99+
}
92100

93101
if (c->thread_count == 1 && c->codec_id != AV_CODEC_ID_PNG &&
94102
c->codec_id != AV_CODEC_ID_TIFF &&

0 commit comments

Comments
 (0)