Skip to content

Commit cd93d17

Browse files
authored
Fix pixel format detection for VP8/VP9 streams (#1070)
1 parent 6beecfc commit cd93d17

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/modules/avformat/producer_avformat.c

+35
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,44 @@ static mlt_properties find_default_streams(producer_avformat self)
490490
// Allow for multiple audio and video streams in the file and select first of each (if available)
491491
for (i = 0; i < context->nb_streams; i++) {
492492
// Get the codec context
493+
AVFormatContext *vpx_context = NULL;
493494
AVStream *stream = context->streams[i];
494495
if (!stream)
495496
continue;
496497
AVCodecParameters *codec_params = stream->codecpar;
497498
const AVCodec *codec = avcodec_find_decoder(codec_params->codec_id);
498499
if (!codec)
499500
continue;
501+
int switch_to_vpx = 0;
502+
if (codec_params->codec_id == AV_CODEC_ID_VP9) {
503+
if (!(codec = avcodec_find_decoder_by_name("libvpx-vp9"))) {
504+
codec = avcodec_find_decoder(codec_params->codec_id);
505+
} else {
506+
switch_to_vpx = 1;
507+
}
508+
} else if (codec_params->codec_id == AV_CODEC_ID_VP8) {
509+
if (!(codec = avcodec_find_decoder_by_name("libvpx"))) {
510+
codec = avcodec_find_decoder(codec_params->codec_id);
511+
} else {
512+
switch_to_vpx = 1;
513+
}
514+
}
515+
if (switch_to_vpx) {
516+
// Use a temporary format context to get the real pixel format with the libvpx decoder,
517+
// since the native decoder incorreclty detects yuva420p as yuv420p
518+
int error = avformat_open_input(&vpx_context,
519+
mlt_properties_get(meta_media, "resource"),
520+
NULL,
521+
NULL);
522+
if (!error) {
523+
vpx_context->video_codec = codec;
524+
avformat_find_stream_info(vpx_context, NULL);
525+
AVStream *vpx_stream = vpx_context->streams[i];
526+
if (vpx_stream) {
527+
codec_params = vpx_stream->codecpar;
528+
}
529+
}
530+
}
500531

501532
snprintf(key, sizeof(key), "meta.media.%u.stream.type", i);
502533

@@ -634,6 +665,10 @@ static mlt_properties find_default_streams(producer_avformat self)
634665
free(value);
635666
}
636667
}
668+
if (vpx_context) {
669+
avformat_close_input(&vpx_context);
670+
vpx_context = NULL;
671+
}
637672
}
638673

639674
// Use the album art if that is all we have

0 commit comments

Comments
 (0)