Browse code

mpeg4videodec: move mpeg4-specific bug workaround variables from MpegEncContext to Mpeg4DecContext

Anton Khirnov authored on 2013/11/26 22:30:13
Showing 3 changed files
... ...
@@ -70,6 +70,12 @@ typedef struct Mpeg4DecContext {
70 70
     int rvlc;
71 71
     ///< could this stream contain resync markers
72 72
     int resync_marker;
73
+
74
+    /* bug workarounds */
75
+    int divx_version;
76
+    int divx_build;
77
+    int xvid_build;
78
+    int lavc_build;
73 79
 } Mpeg4DecContext;
74 80
 
75 81
 /* dc encoding for mpeg4 */
... ...
@@ -150,8 +150,9 @@ static inline int mpeg4_is_resync(MpegEncContext *s)
150 150
     return 0;
151 151
 }
152 152
 
153
-static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
153
+static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
154 154
 {
155
+    MpegEncContext *s = &ctx->m;
155 156
     int a     = 2 << s->sprite_warping_accuracy;
156 157
     int rho   = 3  - s->sprite_warping_accuracy;
157 158
     int r     = 16 / a;
... ...
@@ -179,7 +180,7 @@ static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
179 179
         if (length)
180 180
             x = get_xbits(gb, length);
181 181
 
182
-        if (!(s->divx_version == 500 && s->divx_build == 413))
182
+        if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
183 183
             skip_bits1(gb);     /* marker bit */
184 184
 
185 185
         length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
... ...
@@ -201,7 +202,7 @@ static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
201 201
     h2 = 1 << beta;
202 202
 
203 203
     // Note, the 4th point isn't used for GMC
204
-    if (s->divx_version == 500 && s->divx_build == 413) {
204
+    if (ctx->divx_version == 500 && ctx->divx_build == 413) {
205 205
         sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
206 206
         sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
207 207
         sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
... ...
@@ -443,7 +444,7 @@ int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
443 443
             // FIXME don't just ignore everything
444 444
             if (s->pict_type == AV_PICTURE_TYPE_S &&
445 445
                 ctx->vol_sprite_usage == GMC_SPRITE) {
446
-                if (mpeg4_decode_sprite_trajectory(s, &s->gb) < 0)
446
+                if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
447 447
                     return AVERROR_INVALIDDATA;
448 448
                 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
449 449
             }
... ...
@@ -474,8 +475,9 @@ int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
474 474
  * @param n either 0 for the x component or 1 for y
475 475
  * @return the average MV for a GMC MB
476 476
  */
477
-static inline int get_amv(MpegEncContext *s, int n)
477
+static inline int get_amv(Mpeg4DecContext *ctx, int n)
478 478
 {
479
+    MpegEncContext *s = &ctx->m;
479 480
     int x, y, mb_v, sum, dx, dy, shift;
480 481
     int len     = 1 << (s->f_code + 4);
481 482
     const int a = s->sprite_warping_accuracy;
... ...
@@ -484,7 +486,7 @@ static inline int get_amv(MpegEncContext *s, int n)
484 484
         len >>= s->quarter_sample;
485 485
 
486 486
     if (s->real_sprite_warping_points == 1) {
487
-        if (s->divx_version == 500 && s->divx_build == 413)
487
+        if (ctx->divx_version == 500 && ctx->divx_build == 413)
488 488
             sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
489 489
         else
490 490
             sum = RSHIFT(s->sprite_offset[0][n] << s->quarter_sample, a);
... ...
@@ -650,8 +652,8 @@ try_again:
650 650
                                                          MB_TYPE_16x16 |
651 651
                                                          MB_TYPE_GMC   |
652 652
                                                          MB_TYPE_L0;
653
-                        mx = get_amv(s, 0);
654
-                        my = get_amv(s, 1);
653
+                        mx = get_amv(ctx, 0);
654
+                        my = get_amv(ctx, 1);
655 655
                     } else {
656 656
                         s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
657 657
                                                          MB_TYPE_16x16 |
... ...
@@ -722,8 +724,8 @@ try_again:
722 722
                             s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
723 723
                                                              MB_TYPE_L0;
724 724
                         } else {
725
-                            mx = get_amv(s, 0);
726
-                            my = get_amv(s, 1);
725
+                            mx = get_amv(ctx, 0);
726
+                            my = get_amv(ctx, 1);
727 727
                             s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
728 728
                                                              MB_TYPE_GMC   |
729 729
                                                              MB_TYPE_L0;
... ...
@@ -1279,8 +1281,8 @@ static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1279 1279
                                                      MB_TYPE_16x16 |
1280 1280
                                                      MB_TYPE_L0;
1281 1281
                     s->mcsel       = 1;
1282
-                    s->mv[0][0][0] = get_amv(s, 0);
1283
-                    s->mv[0][0][1] = get_amv(s, 1);
1282
+                    s->mv[0][0][0] = get_amv(ctx, 0);
1283
+                    s->mv[0][0][1] = get_amv(ctx, 1);
1284 1284
                     s->mb_skipped  = 0;
1285 1285
                 } else {
1286 1286
                     s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
... ...
@@ -1329,8 +1331,8 @@ static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1329 1329
                                                  MB_TYPE_L0;
1330 1330
                 /* 16x16 global motion prediction */
1331 1331
                 s->mv_type     = MV_TYPE_16X16;
1332
-                mx             = get_amv(s, 0);
1333
-                my             = get_amv(s, 1);
1332
+                mx             = get_amv(ctx, 0);
1333
+                my             = get_amv(ctx, 1);
1334 1334
                 s->mv[0][0][0] = mx;
1335 1335
                 s->mv[0][0][1] = my;
1336 1336
             } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
... ...
@@ -1991,8 +1993,9 @@ no_cplx_est:
1991 1991
  * Decode the user data stuff in the header.
1992 1992
  * Also initializes divx/xvid/lavc_version/build.
1993 1993
  */
1994
-static int decode_user_data(MpegEncContext *s, GetBitContext *gb)
1994
+static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
1995 1995
 {
1996
+    MpegEncContext *s = &ctx->m;
1996 1997
     char buf[256];
1997 1998
     int i;
1998 1999
     int e;
... ...
@@ -2011,8 +2014,8 @@ static int decode_user_data(MpegEncContext *s, GetBitContext *gb)
2011 2011
     if (e < 2)
2012 2012
         e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2013 2013
     if (e >= 2) {
2014
-        s->divx_version = ver;
2015
-        s->divx_build   = build;
2014
+        ctx->divx_version = ver;
2015
+        ctx->divx_build   = build;
2016 2016
         s->divx_packed  = e == 3 && last == 'p';
2017 2017
         if (s->divx_packed && !s->showed_packed_warning) {
2018 2018
             av_log(s->avctx, AV_LOG_WARNING,
... ...
@@ -2032,15 +2035,15 @@ static int decode_user_data(MpegEncContext *s, GetBitContext *gb)
2032 2032
     }
2033 2033
     if (e != 4) {
2034 2034
         if (strcmp(buf, "ffmpeg") == 0)
2035
-            s->lavc_build = 4600;
2035
+            ctx->lavc_build = 4600;
2036 2036
     }
2037 2037
     if (e == 4)
2038
-        s->lavc_build = build;
2038
+        ctx->lavc_build = build;
2039 2039
 
2040 2040
     /* Xvid detection */
2041 2041
     e = sscanf(buf, "XviD%d", &build);
2042 2042
     if (e == 1)
2043
-        s->xvid_build = build;
2043
+        ctx->xvid_build = build;
2044 2044
 
2045 2045
     return 0;
2046 2046
 }
... ...
@@ -2201,7 +2204,7 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2201 2201
     if (s->pict_type == AV_PICTURE_TYPE_S &&
2202 2202
         (ctx->vol_sprite_usage == STATIC_SPRITE ||
2203 2203
          ctx->vol_sprite_usage == GMC_SPRITE)) {
2204
-        if (mpeg4_decode_sprite_trajectory(s, gb) < 0)
2204
+        if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
2205 2205
             return AVERROR_INVALIDDATA;
2206 2206
         if (s->sprite_brightness_change)
2207 2207
             av_log(s->avctx, AV_LOG_ERROR,
... ...
@@ -2265,7 +2268,7 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2265 2265
      * (divx4/xvid/opendivx). Note we cannot detect divx5 without b-frames
2266 2266
      * easily (although it's buggy too) */
2267 2267
     if (s->vo_type == 0 && s->vol_control_parameters == 0 &&
2268
-        s->divx_version == -1 && s->picture_number == 0) {
2268
+        ctx->divx_version == -1 && s->picture_number == 0) {
2269 2269
         av_log(s->avctx, AV_LOG_WARNING,
2270 2270
                "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2271 2271
         s->low_delay = 1;
... ...
@@ -2308,7 +2311,7 @@ int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2308 2308
     for (;;) {
2309 2309
         if (get_bits_count(gb) >= gb->size_in_bits) {
2310 2310
             if (gb->size_in_bits == 8 &&
2311
-                (s->divx_version >= 0 || s->xvid_build >= 0)) {
2311
+                (ctx->divx_version >= 0 || ctx->xvid_build >= 0)) {
2312 2312
                 av_log(s->avctx, AV_LOG_WARNING, "frame skip %d\n", gb->size_in_bits);
2313 2313
                 return FRAME_SKIPPED;  // divx bug
2314 2314
             } else
... ...
@@ -2385,7 +2388,7 @@ int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2385 2385
             if (decode_vol_header(ctx, gb) < 0)
2386 2386
                 return -1;
2387 2387
         } else if (startcode == USER_DATA_STARTCODE) {
2388
-            decode_user_data(s, gb);
2388
+            decode_user_data(ctx, gb);
2389 2389
         } else if (startcode == GOP_STARTCODE) {
2390 2390
             mpeg4_decode_gop_header(s, gb);
2391 2391
         } else if (startcode == VOS_STARTCODE) {
... ...
@@ -2403,24 +2406,24 @@ end:
2403 2403
         s->low_delay = 1;
2404 2404
     s->avctx->has_b_frames = !s->low_delay;
2405 2405
 
2406
-    if (s->xvid_build == -1 && s->divx_version == -1 && s->lavc_build == -1) {
2406
+    if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2407 2407
         if (s->stream_codec_tag == AV_RL32("XVID") ||
2408 2408
             s->codec_tag        == AV_RL32("XVID") ||
2409 2409
             s->codec_tag        == AV_RL32("XVIX") ||
2410 2410
             s->codec_tag        == AV_RL32("RMP4") ||
2411 2411
             s->codec_tag        == AV_RL32("ZMP4") ||
2412 2412
             s->codec_tag        == AV_RL32("SIPP"))
2413
-            s->xvid_build = 0;
2413
+            ctx->xvid_build = 0;
2414 2414
     }
2415 2415
 
2416
-    if (s->xvid_build == -1 && s->divx_version == -1 && s->lavc_build == -1)
2416
+    if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2417 2417
         if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
2418 2418
             s->vol_control_parameters == 0)
2419
-            s->divx_version = 400;  // divx 4
2419
+            ctx->divx_version = 400;  // divx 4
2420 2420
 
2421
-    if (s->xvid_build >= 0 && s->divx_version >= 0) {
2422
-        s->divx_version =
2423
-        s->divx_build   = -1;
2421
+    if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2422
+        ctx->divx_version =
2423
+        ctx->divx_build   = -1;
2424 2424
     }
2425 2425
 
2426 2426
     if (s->workaround_bugs & FF_BUG_AUTODETECT) {
... ...
@@ -2430,51 +2433,51 @@ end:
2430 2430
         if (s->codec_tag == AV_RL32("UMP4"))
2431 2431
             s->workaround_bugs |= FF_BUG_UMP4;
2432 2432
 
2433
-        if (s->divx_version >= 500 && s->divx_build < 1814)
2433
+        if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2434 2434
             s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2435 2435
 
2436
-        if (s->divx_version > 502 && s->divx_build < 1814)
2436
+        if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2437 2437
             s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
2438 2438
 
2439
-        if (s->xvid_build <= 3U)
2439
+        if (ctx->xvid_build <= 3U)
2440 2440
             s->padding_bug_score = 256 * 256 * 256 * 64;
2441 2441
 
2442
-        if (s->xvid_build <= 1U)
2442
+        if (ctx->xvid_build <= 1U)
2443 2443
             s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2444 2444
 
2445
-        if (s->xvid_build <= 12U)
2445
+        if (ctx->xvid_build <= 12U)
2446 2446
             s->workaround_bugs |= FF_BUG_EDGE;
2447 2447
 
2448
-        if (s->xvid_build <= 32U)
2448
+        if (ctx->xvid_build <= 32U)
2449 2449
             s->workaround_bugs |= FF_BUG_DC_CLIP;
2450 2450
 
2451
-        if (s->lavc_build < 4653U)
2451
+        if (ctx->lavc_build < 4653U)
2452 2452
             s->workaround_bugs |= FF_BUG_STD_QPEL;
2453 2453
 
2454
-        if (s->lavc_build < 4655U)
2454
+        if (ctx->lavc_build < 4655U)
2455 2455
             s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2456 2456
 
2457
-        if (s->lavc_build < 4670U)
2457
+        if (ctx->lavc_build < 4670U)
2458 2458
             s->workaround_bugs |= FF_BUG_EDGE;
2459 2459
 
2460
-        if (s->lavc_build <= 4712U)
2460
+        if (ctx->lavc_build <= 4712U)
2461 2461
             s->workaround_bugs |= FF_BUG_DC_CLIP;
2462 2462
 
2463
-        if (s->divx_version >= 0)
2463
+        if (ctx->divx_version >= 0)
2464 2464
             s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2465 2465
 
2466
-        if (s->divx_version == 501 && s->divx_build == 20020416)
2466
+        if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2467 2467
             s->padding_bug_score = 256 * 256 * 256 * 64;
2468 2468
 
2469
-        if (s->divx_version < 500U)
2469
+        if (ctx->divx_version < 500U)
2470 2470
             s->workaround_bugs |= FF_BUG_EDGE;
2471 2471
 
2472
-        if (s->divx_version >= 0)
2472
+        if (ctx->divx_version >= 0)
2473 2473
             s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
2474 2474
     }
2475 2475
 
2476 2476
 #if HAVE_MMX
2477
-    if (s->codec_id == AV_CODEC_ID_MPEG4 && s->xvid_build >= 0 &&
2477
+    if (s->codec_id == AV_CODEC_ID_MPEG4 && ctx->xvid_build >= 0 &&
2478 2478
         s->avctx->idct_algo == FF_IDCT_AUTO &&
2479 2479
         (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
2480 2480
         s->avctx->idct_algo = FF_IDCT_XVIDMMX;
... ...
@@ -2486,8 +2489,8 @@ end:
2486 2486
     if (s->avctx->debug & FF_DEBUG_BUGS)
2487 2487
         av_log(s->avctx, AV_LOG_DEBUG,
2488 2488
                "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2489
-               s->workaround_bugs, s->lavc_build, s->xvid_build,
2490
-               s->divx_version, s->divx_build, s->divx_packed ? "p" : "");
2489
+               s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2490
+               ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2491 2491
 
2492 2492
     return decode_vop_header(ctx, gb);
2493 2493
 }
... ...
@@ -2514,7 +2517,7 @@ int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
2514 2514
                 }
2515 2515
         }
2516 2516
         if (s->gb.buffer == s->bitstream_buffer && buf_size > 7 &&
2517
-            s->xvid_build >= 0) {       // xvid style
2517
+            ctx->xvid_build >= 0) {       // xvid style
2518 2518
             startcode_found = 1;
2519 2519
             current_pos     = 0;
2520 2520
         }
... ...
@@ -2559,10 +2562,10 @@ static av_cold int decode_init(AVCodecContext *avctx)
2559 2559
     int ret;
2560 2560
     static int done = 0;
2561 2561
 
2562
-    s->divx_version =
2563
-    s->divx_build   =
2564
-    s->xvid_build   =
2565
-    s->lavc_build   = -1;
2562
+    ctx->divx_version =
2563
+    ctx->divx_build   =
2564
+    ctx->xvid_build   =
2565
+    ctx->lavc_build   = -1;
2566 2566
 
2567 2567
     if ((ret = ff_h263_decode_init(avctx)) < 0)
2568 2568
         return ret;
... ...
@@ -614,18 +614,11 @@ typedef struct MpegEncContext {
614 614
     int cplx_estimation_trash_b;
615 615
 
616 616
     /* divx specific, used to workaround (many) bugs in divx5 */
617
-    int divx_version;
618
-    int divx_build;
619 617
     int divx_packed;
620 618
     uint8_t *bitstream_buffer; //Divx 5.01 puts several frames in a single one, this is used to reorder them
621 619
     int bitstream_buffer_size;
622 620
     unsigned int allocated_bitstream_buffer_size;
623 621
 
624
-    int xvid_build;
625
-
626
-    /* lavc specific stuff, used to workaround bugs in libavcodec */
627
-    int lavc_build;
628
-
629 622
     /* RV10 specific */
630 623
     int rv10_version; ///< RV10 version: 0 or 3
631 624
     int rv10_first_dc_coded[3];