mpeg12dec: move setting first_field to mpeg_field_start()
authorAnton Khirnov <anton@khirnov.net>
Sat, 17 Dec 2016 16:04:55 +0000 (17:04 +0100)
committerDiego Biurrun <diego@biurrun.de>
Thu, 12 Jan 2017 10:11:04 +0000 (11:11 +0100)
For field picture, the first_field is set based on its previous value.
Before this commit, first_field is set when reading the picture
coding extension. However, in corrupted files there may be multiple
picture coding extension headers, so the final value of first_field that
is actually used during decoding can be wrong. That can lead to various
undefined behaviour, like predicting from a non-existing field.

Fix this problem, by setting first_field in mpeg_field_start(), which
should be called exactly once per field.

CC: libav-stable@libav.org
Bug-ID: 999
(cherry picked from commit c2fa6bb0e8703a7a6aa10e11f9ab36094416d83f)
Signed-off-by: Diego Biurrun <diego@biurrun.de>
libavcodec/mpeg12.c

index aa6e9e8..cc69166 100644 (file)
@@ -1547,10 +1547,8 @@ static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
     }
 
     if (s->picture_structure == PICT_FRAME) {
-        s->first_field = 0;
         s->v_edge_pos  = 16 * s->mb_height;
     } else {
-        s->first_field ^= 1;
         s->v_edge_pos   = 8 * s->mb_height;
         memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
     }
@@ -1580,6 +1578,11 @@ static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
     AVCodecContext *avctx = s->avctx;
     Mpeg1Context *s1 = (Mpeg1Context*)s;
 
+    if (s->picture_structure == PICT_FRAME)
+        s->first_field = 0;
+    else
+        s->first_field ^= 1;
+
     /* start frame decoding */
     if (s->first_field || s->picture_structure == PICT_FRAME) {
         if (MPV_frame_start(s, avctx) < 0)