[FFmpeg-devel] [PATCH] avformat/utils: always preserve container dimensions for all streams

James Almer jamrial at gmail.com
Sun Jan 24 16:41:13 EET 2021


If a decoder is used for probing it may change the dimensions reported by the
demuxer, either by the lowres factor or because of assorted frames reporting
different dimensions, and in a codec copy scenario, the last dimensions
arbitrarily set by it could end up being propagated to the muxer.

Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavformat/utils.c                     | 10 ++++++----
 tests/ref/fate/cbs-vp9-vp90-2-05-resize |  2 +-
 tests/ref/fate/redcode-demux            |  2 +-
 tests/ref/fate/wtv-demux                |  4 ++--
 4 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/libavformat/utils.c b/libavformat/utils.c
index 6f100294a1..bfc8436fc9 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -4105,6 +4105,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
         st = ic->streams[i];
 
         if (st->internal->avctx_inited) {
+            AVRational orig_sar = st->codecpar->sample_aspect_ratio;
             int orig_w = st->codecpar->width;
             int orig_h = st->codecpar->height;
             ret = avcodec_parameters_from_context(st->codecpar, st->internal->avctx);
@@ -4113,13 +4114,14 @@ FF_ENABLE_DEPRECATION_WARNINGS
             ret = add_coded_side_data(st, st->internal->avctx);
             if (ret < 0)
                 goto find_stream_info_err;
-#if FF_API_LOWRES
-            // The decoder might reduce the video size by the lowres factor.
-            if (st->internal->avctx->lowres && orig_w) {
+
+            // The decoder might change the video size.
+            if (orig_w && orig_h) {
+                if (st->codecpar->width != orig_w && st->codecpar->height != orig_h)
+                    st->codecpar->sample_aspect_ratio = orig_sar;
                 st->codecpar->width = orig_w;
                 st->codecpar->height = orig_h;
             }
-#endif
         }
 
 #if FF_API_LAVF_AVCTX
diff --git a/tests/ref/fate/cbs-vp9-vp90-2-05-resize b/tests/ref/fate/cbs-vp9-vp90-2-05-resize
index 8f036bba81..37a37ff1ea 100644
--- a/tests/ref/fate/cbs-vp9-vp90-2-05-resize
+++ b/tests/ref/fate/cbs-vp9-vp90-2-05-resize
@@ -1 +1 @@
-6838422ebb45df353a2bad62b9aff8e9
+1c39300b93fe110e1db30974e5d3479d
diff --git a/tests/ref/fate/redcode-demux b/tests/ref/fate/redcode-demux
index 45119ec71e..c6e0b6de5c 100644
--- a/tests/ref/fate/redcode-demux
+++ b/tests/ref/fate/redcode-demux
@@ -1,7 +1,7 @@
 #tb 0: 1/240000
 #media_type 0: video
 #codec_id 0: jpeg2000
-#dimensions 0: 2048x1152
+#dimensions 0: 4096x2304
 #sar 0: 0/1
 #tb 1: 1/240000
 #media_type 1: audio
diff --git a/tests/ref/fate/wtv-demux b/tests/ref/fate/wtv-demux
index abe85a4ab6..388ba796d3 100644
--- a/tests/ref/fate/wtv-demux
+++ b/tests/ref/fate/wtv-demux
@@ -3,8 +3,8 @@
 #tb 0: 1/10000000
 #media_type 0: video
 #codec_id 0: mpeg2video
-#dimensions 0: 720x576
-#sar 0: 64/45
+#dimensions 0: 704x480
+#sar 0: 0/1
 #tb 1: 1/10000000
 #media_type 1: audio
 #codec_id 1: mp2
-- 
2.30.0



More information about the ffmpeg-devel mailing list