Browse code

mpeg4video: Add support for MPEG-4 Simple Studio Profile.

This is a profile supporting > 8-bit video and has a higher quality DCT

Kieran Kunhya authored on 2017/12/30 00:42:14
Showing 11 changed files
... ...
@@ -814,7 +814,8 @@ static int er_supported(ERContext *s)
814 814
 {
815 815
     if(s->avctx->hwaccel && s->avctx->hwaccel->decode_slice           ||
816 816
        !s->cur_pic.f                                                  ||
817
-       s->cur_pic.field_picture
817
+       s->cur_pic.field_picture                                       ||
818
+       s->avctx->profile == FF_PROFILE_MPEG4_SIMPLE_STUDIO
818 819
     )
819 820
         return 0;
820 821
     return 1;
... ...
@@ -47,6 +47,12 @@
47 47
 
48 48
 static enum AVPixelFormat h263_get_format(AVCodecContext *avctx)
49 49
 {
50
+    /* MPEG-4 Studio Profile only, not supported by hardware */
51
+    if (avctx->bits_per_raw_sample > 8) {
52
+        av_assert1(avctx->profile == FF_PROFILE_MPEG4_SIMPLE_STUDIO);
53
+        return avctx->pix_fmt;
54
+    }
55
+
50 56
     if (avctx->codec->id == AV_CODEC_ID_MSS2)
51 57
         return AV_PIX_FMT_YUV420P;
52 58
 
... ...
@@ -197,6 +203,11 @@ static int decode_slice(MpegEncContext *s)
197 197
 
198 198
     ff_set_qscale(s, s->qscale);
199 199
 
200
+    if (s->studio_profile) {
201
+        if ((ret = ff_mpeg4_decode_studio_slice_header(s->avctx->priv_data)) < 0)
202
+            return ret;
203
+    }
204
+
200 205
     if (s->avctx->hwaccel) {
201 206
         const uint8_t *start = s->gb.buffer + get_bits_count(&s->gb) / 8;
202 207
         ret = s->avctx->hwaccel->decode_slice(s->avctx, start, s->gb.buffer_end - start);
... ...
@@ -256,9 +256,15 @@ av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
256 256
         c->perm_type = FF_IDCT_PERM_NONE;
257 257
     } else {
258 258
         if (avctx->bits_per_raw_sample == 10 || avctx->bits_per_raw_sample == 9) {
259
-            c->idct_put              = ff_simple_idct_put_int16_10bit;
260
-            c->idct_add              = ff_simple_idct_add_int16_10bit;
261
-            c->idct                  = ff_simple_idct_int16_10bit;
259
+            /* 10-bit MPEG-4 Simple Studio Profile requires a higher precision IDCT
260
+               However, it only uses idct_put */
261
+            if (avctx->codec_id == AV_CODEC_ID_MPEG4 && avctx->profile == FF_PROFILE_MPEG4_SIMPLE_STUDIO)
262
+                c->idct_put              = ff_simple_idct_put_int32_10bit;
263
+            else {
264
+                c->idct_put              = ff_simple_idct_put_int16_10bit;
265
+                c->idct_add              = ff_simple_idct_add_int16_10bit;
266
+                c->idct                  = ff_simple_idct_int16_10bit;
267
+            }
262 268
             c->perm_type             = FF_IDCT_PERM_NONE;
263 269
         } else if (avctx->bits_per_raw_sample == 12) {
264 270
             c->idct_put              = ff_simple_idct_put_int16_12bit;
... ...
@@ -207,12 +207,27 @@ static int h263_decode_gob_header(MpegEncContext *s)
207 207
 }
208 208
 
209 209
 /**
210
- * Decode the group of blocks / video packet header.
210
+ * Decode the group of blocks / video packet header / slice header (MPEG-4 Studio).
211 211
  * @return bit position of the resync_marker, or <0 if none was found
212 212
  */
213 213
 int ff_h263_resync(MpegEncContext *s){
214 214
     int left, pos, ret;
215 215
 
216
+    /* In MPEG-4 studio mode look for a new slice startcode
217
+     * and decode slice header */
218
+    if(s->codec_id==AV_CODEC_ID_MPEG4 && s->studio_profile) {
219
+        align_get_bits(&s->gb);
220
+
221
+        while (get_bits_left(&s->gb) >= 32 && show_bits_long(&s->gb, 32) != SLICE_START_CODE) {
222
+            get_bits(&s->gb, 8);
223
+        }
224
+
225
+        if (show_bits_long(&s->gb, 32) == SLICE_START_CODE)
226
+            return get_bits_count(&s->gb);
227
+        else
228
+            return -1;
229
+    }
230
+
216 231
     if(s->codec_id==AV_CODEC_ID_MPEG4){
217 232
         skip_bits1(&s->gb);
218 233
         align_get_bits(&s->gb);
... ...
@@ -649,16 +649,6 @@ static inline int get_dmv(MpegEncContext *s)
649 649
         return 0;
650 650
 }
651 651
 
652
-static inline int get_qscale(MpegEncContext *s)
653
-{
654
-    int qscale = get_bits(&s->gb, 5);
655
-    if (s->q_scale_type)
656
-        return ff_mpeg2_non_linear_qscale[qscale];
657
-    else
658
-        return qscale << 1;
659
-}
660
-
661
-
662 652
 /* motion type (for MPEG-2) */
663 653
 #define MT_FIELD 1
664 654
 #define MT_FRAME 2
... ...
@@ -751,7 +741,7 @@ static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
751 751
             s->interlaced_dct = get_bits1(&s->gb);
752 752
 
753 753
         if (IS_QUANT(mb_type))
754
-            s->qscale = get_qscale(s);
754
+            s->qscale = mpeg_get_qscale(s);
755 755
 
756 756
         if (s->concealment_motion_vectors) {
757 757
             /* just parse them */
... ...
@@ -819,7 +809,7 @@ static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
819 819
             }
820 820
 
821 821
             if (IS_QUANT(mb_type))
822
-                s->qscale = get_qscale(s);
822
+                s->qscale = mpeg_get_qscale(s);
823 823
 
824 824
             s->last_mv[0][0][0] = 0;
825 825
             s->last_mv[0][0][1] = 0;
... ...
@@ -840,7 +830,7 @@ static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
840 840
             }
841 841
 
842 842
             if (IS_QUANT(mb_type))
843
-                s->qscale = get_qscale(s);
843
+                s->qscale = mpeg_get_qscale(s);
844 844
 
845 845
             /* motion vectors */
846 846
             s->mv_dir = (mb_type >> 13) & 3;
... ...
@@ -1728,7 +1718,7 @@ static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1728 1728
     ff_mpeg1_clean_buffers(s);
1729 1729
     s->interlaced_dct = 0;
1730 1730
 
1731
-    s->qscale = get_qscale(s);
1731
+    s->qscale = mpeg_get_qscale(s);
1732 1732
 
1733 1733
     if (s->qscale == 0) {
1734 1734
         av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
... ...
@@ -373,4 +373,120 @@ const uint8_t ff_mpeg4_dc_threshold[8]={
373 373
     99, 13, 15, 17, 19, 21, 23, 0
374 374
 };
375 375
 
376
+/* Note these are different in studio mode */
377
+const uint16_t ff_mpeg4_studio_dc_luma[19][2]={
378
+    {0x0e,  6}, {0x06,  5}, {0x00,  4}, {0x02,  4},
379
+    {0x07,  3}, {0x05,  3}, {0x03,  3}, {0x02,  3},
380
+    {0x04,  3}, {0x06,  3}, {0x01,  4}, {0x1e,  7},
381
+    {0x3e,  8}, {0x7e,  9}, {0xfe, 10}, {0x1fe, 11},
382
+    {0x3fe, 12}, {0x7fe, 13}, {0x7ff, 13}
383
+};
384
+
385
+const uint16_t ff_mpeg4_studio_dc_chroma[19][2]={
386
+    {0x00,  4}, {0x02,  4}, {0x07,  3}, {0x05,  3},
387
+    {0x03,  3}, {0x02,  3}, {0x04,  3}, {0x06,  3},
388
+    {0x01,  4}, {0x06,  5}, {0x0e,  6}, {0x1e,  7},
389
+    {0x3e,  8}, {0x7e,  9}, {0xfe, 10}, {0x1fe, 11},
390
+    {0x3fe, 12}, {0x7fe, 13}, {0x7ff, 13}
391
+};
392
+
393
+const uint16_t ff_mpeg4_studio_intra[12][22][2]={
394
+    {
395
+        {0x05,  4}, {0x04,  4}, {0x05,  7}, {0x09,  9},
396
+        {0x21, 11}, {0x41, 12}, {0x81, 13}, {0x03,  4},
397
+        {0x03,  5}, {0x05,  6}, {0x04,  7}, {0x03,  7},
398
+        {0x05,  8}, {0x03,  2}, {0x05,  3}, {0x04,  3},
399
+        {0x03,  3}, {0x02,  4}, {0x04,  6}, {0x03,  6},
400
+        {0x11, 10}, {0x80, 13}
401
+    },
402
+    {
403
+        {0x00,  0}, {0x00,  0}, {0x00,  0}, {0x00,  0},
404
+        {0x00,  0}, {0x00,  0}, {0x00,  0}, {0x00,  0},
405
+        {0x00,  0}, {0x00,  0}, {0x00,  0}, {0x00,  0},
406
+        {0x00,  0}, {0x00,  0}, {0x01,  1}, {0x01,  2},
407
+        {0x01,  3}, {0x01,  4}, {0x01,  5}, {0x03,  7},
408
+        {0x05,  8}, {0x04,  8}
409
+    },
410
+    {
411
+        {0x05,  3},  {0x03,  5},  {0x02,  5},  {0x03,  7},
412
+        {0x09,  9},  {0x103, 14}, {0x102, 14}, {0x04,  3},
413
+        {0x03,  3},  {0x03,  4},  {0x02,  4},  {0x03,  6},
414
+        {0x11, 10},  {0x03,  2},  {0x02,  3},  {0x02,  6},
415
+        {0x05,  8},  {0x21, 11},  {0x83, 13},  {0x101, 14},
416
+        {0x201, 15}, {0x82, 13}
417
+    },
418
+    {
419
+        {0x05,  5}, {0x05,  4}, {0x04,  5}, {0x03,  6},
420
+        {0x09,  9}, {0x83, 13}, {0x82, 13}, {0x03,  3},
421
+        {0x04,  4}, {0x03,  4}, {0x03,  5}, {0x05,  8},
422
+        {0x81, 13}, {0x03,  2}, {0x02,  2}, {0x02,  5},
423
+        {0x02,  6}, {0x03,  7}, {0x11, 10}, {0x43, 12},
424
+        {0x80, 13}, {0x42, 12}
425
+    },
426
+    {
427
+        {0x05,  7},  {0x03,  4}, {0x03,  5},  {0x04,  7},
428
+        {0x09,  9},  {0x83, 13}, {0x101, 14}, {0x03,  3},
429
+        {0x02,  4},  {0x05,  6}, {0x03,  7},  {0x11, 10},
430
+        {0x201, 15}, {0x03,  2}, {0x02,  2},  {0x02,  3},
431
+        {0x04,  6},  {0x03,  6}, {0x05,  8},  {0x21, 11},
432
+        {0x82, 13},  {0x81, 13}
433
+    },
434
+    {
435
+        {0x13, 10},  {0x03,  5}, {0x05,  7}, {0x12, 10},
436
+        {0x43, 12},  {0x83, 13}, {0x82, 13}, {0x02,  5},
437
+        {0x04,  7},  {0x05,  8}, {0x23, 11}, {0x81, 13},
438
+        {0x101, 14}, {0x03,  2}, {0x02,  2}, {0x01,  2},
439
+        {0x01,  3},  {0x03,  6}, {0x03,  7}, {0x22, 11},
440
+        {0x201, 15}, {0x42, 12}
441
+    },
442
+    {
443
+        {0x23, 11},  {0x01,  4},  {0x07,  8},  {0x13, 10},
444
+        {0x22, 11},  {0x103, 14}, {0x102, 14}, {0x03,  6},
445
+        {0x06,  8},  {0x12, 10},  {0x43, 12},  {0x101, 14},
446
+        {0x201, 15}, {0x03,  3},  {0x02,  3},  {0x03,  2},
447
+        {0x02,  2},  {0x01,  3},  {0x02,  6},  {0x05,  8},
448
+        {0x42, 12},  {0x41, 12}
449
+    },
450
+    {
451
+        {0x0b,  9}, {0x03,  5}, {0x07,  8}, {0x07,  7},
452
+        {0x06,  7}, {0x23, 11}, {0x41, 12}, {0x05,  7},
453
+        {0x06,  8}, {0x0a,  9}, {0x13, 10}, {0x22, 11},
454
+        {0x40, 12}, {0x03,  4}, {0x02,  4}, {0x03,  2},
455
+        {0x02,  2}, {0x01,  2}, {0x02,  5}, {0x04,  7},
456
+        {0x12, 10}, {0x21, 11}
457
+    },
458
+    {
459
+        {0x15, 10}, {0x03,  6}, {0x14, 10}, {0x23, 11},
460
+        {0x07,  8}, {0x43, 12}, {0x81, 13}, {0x06,  8},
461
+        {0x0b,  9}, {0x13, 10}, {0x12, 10}, {0x42, 12},
462
+        {0x80, 13}, {0x01,  4}, {0x03,  3}, {0x02,  3},
463
+        {0x03,  2}, {0x02,  2}, {0x01,  3}, {0x02,  6},
464
+        {0x22, 11}, {0x41, 12}
465
+    },
466
+    {
467
+        {0x43, 12}, {0x05,  6}, {0x07,  8}, {0x04,  6},
468
+        {0x03,  6}, {0x13, 10}, {0x42, 12}, {0x05,  7},
469
+        {0x04,  7}, {0x06,  8}, {0x12, 10}, {0x41, 12},
470
+        {0x40, 12}, {0x03,  5}, {0x03,  4}, {0x03,  3},
471
+        {0x02,  3}, {0x03,  2}, {0x02,  2}, {0x02,  4},
472
+        {0x05,  8}, {0x11, 10}
473
+    },
474
+    {
475
+        {0x83, 13}, {0x05,  7}, {0x07,  8}, {0x03,  4},
476
+        {0x21, 11}, {0x82, 13}, {0x81, 13}, {0x04,  7},
477
+        {0x06,  8}, {0x0b,  9}, {0x0a,  9}, {0x11, 10},
478
+        {0x80, 13}, {0x03,  5}, {0x02,  5}, {0x02,  4},
479
+        {0x03,  3}, {0x02,  3}, {0x03,  2}, {0x02,  2},
480
+        {0x03,  6}, {0x09,  9}
481
+    },
482
+    {
483
+        {0x13, 10}, {0x03,  5}, {0x03,  6}, {0x0d,  9},
484
+        {0x0c,  9}, {0x21, 11}, {0x20, 11}, {0x02,  5},
485
+        {0x02,  6}, {0x07,  8}, {0x0b,  9}, {0x12, 10},
486
+        {0x11, 10}, {0x05,  3}, {0x04,  3}, {0x05,  4},
487
+        {0x04,  4}, {0x03,  4}, {0x02,  4}, {0x03,  3},
488
+        {0x03,  2}, {0x0a,  9}
489
+    }
490
+};
491
+
376 492
 #endif /* AVCODEC_MPEG4DATA_H */
... ...
@@ -61,6 +61,10 @@
61 61
 #define GOP_STARTCODE        0x1B3
62 62
 #define VISUAL_OBJ_STARTCODE 0x1B5
63 63
 #define VOP_STARTCODE        0x1B6
64
+#define SLICE_STARTCODE      0x1B7
65
+#define EXT_STARTCODE        0x1B8
66
+
67
+#define QUANT_MATRIX_EXT_ID  0x3
64 68
 
65 69
 /* smaller packets likely don't contain a real frame */
66 70
 #define MAX_NVOP_SIZE 19
... ...
@@ -108,8 +112,16 @@ typedef struct Mpeg4DecContext {
108 108
     int cplx_estimation_trash_i;
109 109
     int cplx_estimation_trash_p;
110 110
     int cplx_estimation_trash_b;
111
+
112
+    VLC studio_intra_tab[12];
113
+    VLC studio_luma_dc;
114
+    VLC studio_chroma_dc;
115
+
116
+    int rgb;
111 117
 } Mpeg4DecContext;
112 118
 
119
+static const uint8_t mpeg4_block_count[4] = {0, 6, 8, 12};
120
+
113 121
 /* dc encoding for MPEG-4 */
114 122
 extern const uint8_t ff_mpeg4_DCtab_lum[13][2];
115 123
 extern const uint8_t ff_mpeg4_DCtab_chrom[13][2];
... ...
@@ -137,6 +149,10 @@ extern const uint16_t ff_mpeg4_resync_prefix[8];
137 137
 
138 138
 extern const uint8_t ff_mpeg4_dc_threshold[8];
139 139
 
140
+extern const uint16_t ff_mpeg4_studio_dc_luma[19][2];
141
+extern const uint16_t ff_mpeg4_studio_dc_chroma[19][2];
142
+extern const uint16_t ff_mpeg4_studio_intra[12][22][2];
143
+
140 144
 void ff_mpeg4_encode_mb(MpegEncContext *s,
141 145
                         int16_t block[6][64],
142 146
                         int motion_x, int motion_y);
... ...
@@ -155,6 +171,7 @@ void ff_clean_mpeg4_qscales(MpegEncContext *s);
155 155
 int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx);
156 156
 int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s);
157 157
 int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx);
158
+int ff_mpeg4_decode_studio_slice_header(Mpeg4DecContext *ctx);
158 159
 void ff_mpeg4_init_direct_mv(MpegEncContext *s);
159 160
 void ff_mpeg4videodec_static_init(void);
160 161
 int ff_mpeg4_workaround_bugs(AVCodecContext *avctx);
... ...
@@ -44,6 +44,7 @@
44 44
 #define SPRITE_TRAJ_VLC_BITS 6
45 45
 #define DC_VLC_BITS 9
46 46
 #define MB_TYPE_B_VLC_BITS 4
47
+#define STUDIO_INTRA_BITS 9
47 48
 
48 49
 static VLC dc_lum, dc_chrom;
49 50
 static VLC sprite_trajectory;
... ...
@@ -528,6 +529,55 @@ int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
528 528
     return 0;
529 529
 }
530 530
 
531
+static void reset_studio_dc_predictors(MpegEncContext *s)
532
+{
533
+    /* Reset DC Predictors */
534
+    s->last_dc[0] =
535
+    s->last_dc[1] =
536
+    s->last_dc[2] = 1 << (s->avctx->bits_per_raw_sample + s->dct_precision + s->intra_dc_precision - 1);
537
+}
538
+
539
+/**
540
+ * Decode the next video packet.
541
+ * @return <0 if something went wrong
542
+ */
543
+int ff_mpeg4_decode_studio_slice_header(Mpeg4DecContext *ctx)
544
+{
545
+    MpegEncContext *s = &ctx->m;
546
+    GetBitContext *gb = &s->gb;
547
+    unsigned vlc_len;
548
+    uint16_t mb_num;
549
+
550
+    if (get_bits_left(gb) >= 32 && get_bits_long(gb, 32) == SLICE_START_CODE) {
551
+        vlc_len = av_log2(s->mb_width * s->mb_height) + 1;
552
+        mb_num = get_bits(gb, vlc_len);
553
+
554
+        if (mb_num >= s->mb_num)
555
+            return AVERROR_INVALIDDATA;
556
+
557
+        s->mb_x = mb_num % s->mb_width;
558
+        s->mb_y = mb_num / s->mb_width;
559
+
560
+        if (ctx->shape != BIN_ONLY_SHAPE)
561
+            s->qscale = mpeg_get_qscale(s);
562
+
563
+        if (get_bits1(gb)) {  /* slice_extension_flag */
564
+            skip_bits1(gb);   /* intra_slice */
565
+            skip_bits1(gb);   /* slice_VOP_id_enable */
566
+            skip_bits(gb, 6); /* slice_VOP_id */
567
+            while (get_bits1(gb)) /* extra_bit_slice */
568
+                skip_bits(gb, 8); /* extra_information_slice */
569
+        }
570
+
571
+        reset_studio_dc_predictors(s);
572
+    }
573
+    else {
574
+        return AVERROR_INVALIDDATA;
575
+    }
576
+
577
+    return 0;
578
+}
579
+
531 580
 /**
532 581
  * Get the average motion vector for a GMC MB.
533 582
  * @param n either 0 for the x component or 1 for y
... ...
@@ -1723,6 +1773,189 @@ end:
1723 1723
     return SLICE_OK;
1724 1724
 }
1725 1725
 
1726
+/* As per spec, studio start code search isn't the same as the old type of start code */
1727
+static void next_start_code_studio(GetBitContext *gb)
1728
+{
1729
+    align_get_bits(gb);
1730
+
1731
+    while (get_bits_left(gb) >= 24 && show_bits_long(gb, 24) != 0x1) {
1732
+        get_bits(gb, 8);
1733
+    }
1734
+}
1735
+
1736
+/* additional_code, vlc index */
1737
+static const uint8_t ac_state_tab[22][2] =
1738
+{
1739
+    {0, 0},
1740
+    {0, 1},
1741
+    {1, 1},
1742
+    {2, 1},
1743
+    {3, 1},
1744
+    {4, 1},
1745
+    {5, 1},
1746
+    {1, 2},
1747
+    {2, 2},
1748
+    {3, 2},
1749
+    {4, 2},
1750
+    {5, 2},
1751
+    {6, 2},
1752
+    {1, 3},
1753
+    {2, 4},
1754
+    {3, 5},
1755
+    {4, 6},
1756
+    {5, 7},
1757
+    {6, 8},
1758
+    {7, 9},
1759
+    {8, 10},
1760
+    {0, 11}
1761
+};
1762
+
1763
+static int mpeg4_decode_studio_block(MpegEncContext *s, int32_t block[64], int n)
1764
+{
1765
+    Mpeg4DecContext *ctx = s->avctx->priv_data;
1766
+
1767
+    int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0,
1768
+        additional_code_len, sign, mismatch;
1769
+    VLC *cur_vlc = &ctx->studio_intra_tab[0];
1770
+    uint8_t *const scantable = s->intra_scantable.permutated;
1771
+    const uint16_t *quant_matrix;
1772
+    uint32_t flc;
1773
+    const int min = -1 *  (1 << (s->avctx->bits_per_raw_sample + 6));
1774
+    const int max =      ((1 << (s->avctx->bits_per_raw_sample + 6)) - 1);
1775
+
1776
+    mismatch = 1;
1777
+
1778
+    memset(block, 0, 64 * sizeof(int32_t));
1779
+
1780
+    if (n < 4) {
1781
+        cc = 0;
1782
+        dct_dc_size = get_vlc2(&s->gb, ctx->studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1783
+        quant_matrix = s->intra_matrix;
1784
+    } else {
1785
+        cc = (n & 1) + 1;
1786
+        if (ctx->rgb)
1787
+            dct_dc_size = get_vlc2(&s->gb, ctx->studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1788
+        else
1789
+            dct_dc_size = get_vlc2(&s->gb, ctx->studio_chroma_dc.table, STUDIO_INTRA_BITS, 2);
1790
+        quant_matrix = s->chroma_intra_matrix;
1791
+    }
1792
+
1793
+    if (dct_dc_size < 0) {
1794
+        av_log(s->avctx, AV_LOG_ERROR, "illegal dct_dc_size vlc\n");
1795
+        return AVERROR_INVALIDDATA;
1796
+    } else if (dct_dc_size == 0) {
1797
+        dct_diff = 0;
1798
+    } else {
1799
+        dct_diff = get_xbits(&s->gb, dct_dc_size);
1800
+
1801
+        if (dct_dc_size > 8) {
1802
+            if(!check_marker(s->avctx, &s->gb, "dct_dc_size > 8"))
1803
+                return AVERROR_INVALIDDATA;
1804
+        }
1805
+
1806
+    }
1807
+
1808
+    s->last_dc[cc] += dct_diff;
1809
+
1810
+    if (s->mpeg_quant)
1811
+        block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision);
1812
+    else
1813
+        block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision) * (8 >> s->dct_precision);
1814
+    /* TODO: support mpeg_quant for AC coefficients */
1815
+
1816
+    block[0] = av_clip(block[0], min, max);
1817
+    mismatch ^= block[0];
1818
+
1819
+    /* AC Coefficients */
1820
+    while (1) {
1821
+        group = get_vlc2(&s->gb, cur_vlc->table, STUDIO_INTRA_BITS, 2);
1822
+
1823
+        if (group < 0) {
1824
+            av_log(s->avctx, AV_LOG_ERROR, "illegal ac coefficient group vlc\n");
1825
+            return AVERROR_INVALIDDATA;
1826
+        }
1827
+
1828
+        additional_code_len = ac_state_tab[group][0];
1829
+        cur_vlc = &ctx->studio_intra_tab[ac_state_tab[group][1]];
1830
+
1831
+        if (group == 0) {
1832
+            /* End of Block */
1833
+            break;
1834
+        } else if (group >= 1 && group <= 6) {
1835
+            /* Zero run length (Table B.47) */
1836
+            run = 1 << additional_code_len;
1837
+            if (additional_code_len)
1838
+                run += get_bits(&s->gb, additional_code_len);
1839
+            idx += run;
1840
+            continue;
1841
+        } else if (group >= 7 && group <= 12) {
1842
+            /* Zero run length and +/-1 level (Table B.48) */
1843
+            code = get_bits(&s->gb, additional_code_len);
1844
+            sign = code & 1;
1845
+            code >>= 1;
1846
+            run = (1 << (additional_code_len - 1)) + code;
1847
+            idx += run;
1848
+            j = scantable[idx++];
1849
+            block[j] = sign ? 1 : -1;
1850
+        } else if (group >= 13 && group <= 20) {
1851
+            /* Level value (Table B.49) */
1852
+            j = scantable[idx++];
1853
+            block[j] = get_xbits(&s->gb, additional_code_len);
1854
+        } else if (group == 21) {
1855
+            /* Escape */
1856
+            j = scantable[idx++];
1857
+            additional_code_len = s->avctx->bits_per_raw_sample + s->dct_precision + 4;
1858
+            flc = get_bits(&s->gb, additional_code_len);
1859
+            if (flc >> (additional_code_len-1))
1860
+                block[j] = -1 * (( flc ^ ((1 << additional_code_len) -1)) + 1);
1861
+            else
1862
+                block[j] = flc;
1863
+        }
1864
+        block[j] = ((8 * 2 * block[j] * quant_matrix[j] * s->qscale) >> s->dct_precision) / 32;
1865
+        block[j] = av_clip(block[j], min, max);
1866
+        mismatch ^= block[j];
1867
+    }
1868
+
1869
+    block[63] ^= mismatch & 1;
1870
+
1871
+    return 0;
1872
+}
1873
+
1874
+static int mpeg4_decode_studio_mb(MpegEncContext *s, int16_t block_[12][64])
1875
+{
1876
+    int i;
1877
+
1878
+    /* StudioMacroblock */
1879
+    /* Assumes I-VOP */
1880
+    s->mb_intra = 1;
1881
+    if (get_bits1(&s->gb)) { /* compression_mode */
1882
+        /* DCT */
1883
+        /* macroblock_type, 1 or 2-bit VLC */
1884
+        if (!get_bits1(&s->gb)) {
1885
+            skip_bits1(&s->gb);
1886
+            s->qscale = mpeg_get_qscale(s);
1887
+        }
1888
+
1889
+        for (i = 0; i < mpeg4_block_count[s->chroma_format]; i++) {
1890
+            if (mpeg4_decode_studio_block(s, (*s->block32)[i], i) < 0)
1891
+                return AVERROR_INVALIDDATA;
1892
+        }
1893
+    } else {
1894
+        /* DPCM */
1895
+        check_marker(s->avctx, &s->gb, "DPCM block start");
1896
+        avpriv_request_sample(s->avctx, "DPCM encoded block");
1897
+        next_start_code_studio(&s->gb);
1898
+        return SLICE_ERROR;
1899
+    }
1900
+
1901
+    if (get_bits_left(&s->gb) >= 24 && show_bits(&s->gb, 23) == 0) {
1902
+        next_start_code_studio(&s->gb);
1903
+        return SLICE_END;
1904
+    }
1905
+
1906
+    return SLICE_OK;
1907
+}
1908
+
1726 1909
 static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
1727 1910
 {
1728 1911
     int hours, minutes, seconds;
... ...
@@ -1791,6 +2024,23 @@ static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb)
1791 1791
     return 0;
1792 1792
 }
1793 1793
 
1794
+static void mpeg4_load_default_matrices(MpegEncContext *s)
1795
+{
1796
+    int i, v;
1797
+
1798
+    /* load default matrices */
1799
+    for (i = 0; i < 64; i++) {
1800
+        int j = s->idsp.idct_permutation[i];
1801
+        v = ff_mpeg4_default_intra_matrix[i];
1802
+        s->intra_matrix[j]        = v;
1803
+        s->chroma_intra_matrix[j] = v;
1804
+
1805
+        v = ff_mpeg4_default_non_intra_matrix[i];
1806
+        s->inter_matrix[j]        = v;
1807
+        s->chroma_inter_matrix[j] = v;
1808
+    }
1809
+}
1810
+
1794 1811
 static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
1795 1812
 {
1796 1813
     MpegEncContext *s = &ctx->m;
... ...
@@ -1954,17 +2204,7 @@ static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
1954 1954
         if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
1955 1955
             int i, v;
1956 1956
 
1957
-            /* load default matrixes */
1958
-            for (i = 0; i < 64; i++) {
1959
-                int j = s->idsp.idct_permutation[i];
1960
-                v = ff_mpeg4_default_intra_matrix[i];
1961
-                s->intra_matrix[j]        = v;
1962
-                s->chroma_intra_matrix[j] = v;
1963
-
1964
-                v = ff_mpeg4_default_non_intra_matrix[i];
1965
-                s->inter_matrix[j]        = v;
1966
-                s->chroma_inter_matrix[j] = v;
1967
-            }
1957
+            mpeg4_load_default_matrices(s);
1968 1958
 
1969 1959
             /* load custom intra matrix */
1970 1960
             if (get_bits1(gb)) {
... ...
@@ -2608,6 +2848,232 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2608 2608
     return 0;
2609 2609
 }
2610 2610
 
2611
+static void read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
2612
+{
2613
+    int i, j, v;
2614
+
2615
+    if (get_bits1(gb)) {
2616
+        /* intra_quantiser_matrix */
2617
+        for (i = 0; i < 64; i++) {
2618
+            v = get_bits(gb, 8);
2619
+            j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2620
+            s->intra_matrix[j]        = v;
2621
+            s->chroma_intra_matrix[j] = v;
2622
+        }
2623
+    }
2624
+
2625
+    if (get_bits1(gb)) {
2626
+        /* non_intra_quantiser_matrix */
2627
+        for (i = 0; i < 64; i++) {
2628
+            get_bits(gb, 8);
2629
+        }
2630
+    }
2631
+
2632
+    if (get_bits1(gb)) {
2633
+        /* chroma_intra_quantiser_matrix */
2634
+        for (i = 0; i < 64; i++) {
2635
+            v = get_bits(gb, 8);
2636
+            j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2637
+            s->chroma_intra_matrix[j] = v;
2638
+        }
2639
+    }
2640
+
2641
+    if (get_bits1(gb)) {
2642
+        /* chroma_non_intra_quantiser_matrix */
2643
+        for (i = 0; i < 64; i++) {
2644
+            get_bits(gb, 8);
2645
+        }
2646
+    }
2647
+
2648
+    next_start_code_studio(gb);
2649
+}
2650
+
2651
+static void extension_and_user_data(MpegEncContext *s, GetBitContext *gb, int id)
2652
+{
2653
+    uint32_t startcode;
2654
+    uint8_t extension_type;
2655
+
2656
+    startcode = show_bits_long(gb, 32);
2657
+    if (startcode == USER_DATA_STARTCODE || startcode == EXT_STARTCODE) {
2658
+
2659
+        if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) {
2660
+            skip_bits_long(gb, 32);
2661
+            extension_type = get_bits(gb, 4);
2662
+            if (extension_type == QUANT_MATRIX_EXT_ID)
2663
+                read_quant_matrix_ext(s, gb);
2664
+        }
2665
+    }
2666
+}
2667
+
2668
+static void decode_smpte_tc(Mpeg4DecContext *ctx, GetBitContext *gb)
2669
+{
2670
+    MpegEncContext *s = &ctx->m;
2671
+
2672
+    skip_bits(gb, 16); /* Time_code[63..48] */
2673
+    check_marker(s->avctx, gb, "after Time_code[63..48]");
2674
+    skip_bits(gb, 16); /* Time_code[47..32] */
2675
+    check_marker(s->avctx, gb, "after Time_code[47..32]");
2676
+    skip_bits(gb, 16); /* Time_code[31..16] */
2677
+    check_marker(s->avctx, gb, "after Time_code[31..16]");
2678
+    skip_bits(gb, 16); /* Time_code[15..0] */
2679
+    check_marker(s->avctx, gb, "after Time_code[15..0]");
2680
+    skip_bits(gb, 4); /* reserved_bits */
2681
+}
2682
+
2683
+/**
2684
+ * Decode the next studio vop header.
2685
+ * @return <0 if something went wrong
2686
+ */
2687
+static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2688
+{
2689
+    MpegEncContext *s = &ctx->m;
2690
+
2691
+    if (get_bits_left(gb) <= 32)
2692
+        return 0;
2693
+
2694
+    if (get_bits_long(gb, 32) != VOP_STARTCODE)
2695
+        return AVERROR_INVALIDDATA;
2696
+
2697
+    s->decode_mb = mpeg4_decode_studio_mb;
2698
+
2699
+    decode_smpte_tc(ctx, gb);
2700
+
2701
+    skip_bits(gb, 10); /* temporal_reference */
2702
+    skip_bits(gb, 2); /* vop_structure */
2703
+    s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */
2704
+    if (get_bits1(gb)) { /* vop_coded */
2705
+        skip_bits1(gb); /* top_field_first */
2706
+        skip_bits1(gb); /* repeat_first_field */
2707
+        s->progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */
2708
+    }
2709
+
2710
+    if (s->pict_type == AV_PICTURE_TYPE_I) {
2711
+        if (get_bits1(gb))
2712
+            reset_studio_dc_predictors(s);
2713
+    }
2714
+
2715
+    if (ctx->shape != BIN_ONLY_SHAPE) {
2716
+        s->alternate_scan = get_bits1(gb);
2717
+        s->frame_pred_frame_dct = get_bits1(gb);
2718
+        s->dct_precision = get_bits(gb, 2);
2719
+        s->intra_dc_precision = get_bits(gb, 2);
2720
+        s->q_scale_type = get_bits1(gb);
2721
+    }
2722
+
2723
+    if (s->alternate_scan) {
2724
+        ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable,   ff_alternate_vertical_scan);
2725
+        ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable,   ff_alternate_vertical_scan);
2726
+        ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2727
+        ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2728
+    } else {
2729
+        ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable,   ff_zigzag_direct);
2730
+        ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable,   ff_zigzag_direct);
2731
+        ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2732
+        ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2733
+    }
2734
+
2735
+    mpeg4_load_default_matrices(s);
2736
+
2737
+    next_start_code_studio(gb);
2738
+    extension_and_user_data(s, gb, 4);
2739
+
2740
+    return 0;
2741
+}
2742
+
2743
+static int decode_studiovisualobject(Mpeg4DecContext *ctx, GetBitContext *gb)
2744
+{
2745
+    uint32_t startcode;
2746
+    MpegEncContext *s = &ctx->m;
2747
+    int visual_object_type, width, height;
2748
+
2749
+    startcode = get_bits_long(gb, 32);
2750
+
2751
+    /* StudioVisualObject() */
2752
+    if (startcode == VISUAL_OBJ_STARTCODE) {
2753
+        skip_bits(gb, 4); /* visual_object_verid */
2754
+        visual_object_type = get_bits(gb, 4);
2755
+
2756
+        next_start_code_studio(gb);
2757
+        extension_and_user_data(s, gb, 1);
2758
+
2759
+        if (visual_object_type == VOT_VIDEO_ID) {
2760
+            /* StudioVideoObjectLayer */
2761
+            skip_bits_long(gb, 32); /* video_object_start_code */
2762
+            skip_bits_long(gb, 32); /* video_object_layer_start_code */
2763
+            skip_bits1(gb); /* random_accessible_vol */
2764
+            skip_bits(gb, 8); /* video_object_type_indication */
2765
+            skip_bits(gb, 4); /* video_object_layer_verid */
2766
+            ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */
2767
+            skip_bits(gb, 4); /* video_object_layer_shape_extension */
2768
+            skip_bits1(gb); /* progressive_sequence */
2769
+            if (ctx->shape != BIN_ONLY_SHAPE) {
2770
+                ctx->rgb = get_bits1(gb); /* rgb_components */
2771
+                s->chroma_format = get_bits(gb, 2); /* chroma_format */
2772
+                if (!s->chroma_format) {
2773
+                    av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2774
+                    return AVERROR_INVALIDDATA;
2775
+                }
2776
+
2777
+                s->avctx->bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */
2778
+                if (s->avctx->bits_per_raw_sample == 10) {
2779
+                    if (ctx->rgb) {
2780
+                        s->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
2781
+                    }
2782
+                    else {
2783
+                        s->avctx->pix_fmt = s->chroma_format == CHROMA_422 ? AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV444P10;
2784
+                    }
2785
+                }
2786
+                else {
2787
+                    avpriv_request_sample(s->avctx, "MPEG-4 Studio profile bit-depth %u", s->avctx->bits_per_raw_sample);
2788
+                    return AVERROR_PATCHWELCOME;
2789
+                }
2790
+            }
2791
+            if (ctx->shape == RECT_SHAPE) {
2792
+                check_marker(s->avctx, gb, "before video_object_layer_width");
2793
+                width = get_bits(gb, 14); /* video_object_layer_width */
2794
+                check_marker(s->avctx, gb, "before video_object_layer_height");
2795
+                height = get_bits(gb, 14); /* video_object_layer_height */
2796
+                check_marker(s->avctx, gb, "after video_object_layer_height");
2797
+
2798
+                /* Do the same check as non-studio profile */
2799
+                if (width && height) {
2800
+                    if (s->width && s->height &&
2801
+                        (s->width != width || s->height != height))
2802
+                        s->context_reinit = 1;
2803
+                    s->width  = width;
2804
+                    s->height = height;
2805
+                }
2806
+            }
2807
+            s->aspect_ratio_info = get_bits(gb, 4);
2808
+            if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
2809
+                s->avctx->sample_aspect_ratio.num = get_bits(gb, 8);  // par_width
2810
+                s->avctx->sample_aspect_ratio.den = get_bits(gb, 8);  // par_height
2811
+            } else {
2812
+                s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
2813
+            }
2814
+            skip_bits(gb, 4); /* frame_rate_code */
2815
+            skip_bits(gb, 15); /* first_half_bit_rate */
2816
+            check_marker(s->avctx, gb, "after first_half_bit_rate");
2817
+            skip_bits(gb, 15); /* latter_half_bit_rate */
2818
+            check_marker(s->avctx, gb, "after latter_half_bit_rate");
2819
+            skip_bits(gb, 15); /* first_half_vbv_buffer_size */
2820
+            check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2821
+            skip_bits(gb, 3); /* latter_half_vbv_buffer_size */
2822
+            skip_bits(gb, 11); /* first_half_vbv_buffer_size */
2823
+            check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2824
+            skip_bits(gb, 15); /* latter_half_vbv_occupancy */
2825
+            check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2826
+            s->low_delay = get_bits1(gb);
2827
+            s->mpeg_quant = get_bits1(gb); /* mpeg2_stream */
2828
+
2829
+            next_start_code_studio(gb);
2830
+            extension_and_user_data(s, gb, 2);
2831
+        }
2832
+    }
2833
+
2834
+    return 0;
2835
+}
2836
+
2611 2837
 /**
2612 2838
  * Decode MPEG-4 headers.
2613 2839
  * @return <0 if no VOP found (or a damaged one)
... ...
@@ -2721,6 +3187,16 @@ int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2721 2721
             mpeg4_decode_gop_header(s, gb);
2722 2722
         } else if (startcode == VOS_STARTCODE) {
2723 2723
             mpeg4_decode_profile_level(s, gb);
2724
+            if (s->avctx->profile == FF_PROFILE_MPEG4_SIMPLE_STUDIO &&
2725
+                (s->avctx->level > 0 && s->avctx->level < 9)) {
2726
+                s->studio_profile = 1;
2727
+                next_start_code_studio(gb);
2728
+                extension_and_user_data(s, gb, 0);
2729
+
2730
+                if ((ret = decode_studiovisualobject(ctx, gb)) < 0)
2731
+                    return ret;
2732
+                break;
2733
+            }
2724 2734
         } else if (startcode == VISUAL_OBJ_STARTCODE) {
2725 2735
             mpeg4_decode_visual_object(s, gb);
2726 2736
         } else if (startcode == VOP_STARTCODE) {
... ...
@@ -2736,7 +3212,10 @@ end:
2736 2736
         s->low_delay = 1;
2737 2737
     s->avctx->has_b_frames = !s->low_delay;
2738 2738
 
2739
-    return decode_vop_header(ctx, gb);
2739
+    if (s->studio_profile)
2740
+        return decode_studio_vop_header(ctx, gb);
2741
+    else
2742
+        return decode_vop_header(ctx, gb);
2740 2743
 }
2741 2744
 
2742 2745
 av_cold void ff_mpeg4videodec_static_init(void) {
... ...
@@ -2836,6 +3315,37 @@ static int mpeg4_update_thread_context(AVCodecContext *dst,
2836 2836
 }
2837 2837
 #endif
2838 2838
 
2839
+static av_cold int init_studio_vlcs(Mpeg4DecContext *ctx)
2840
+{
2841
+    int i, ret;
2842
+
2843
+    for (i = 0; i < 12; i++) {
2844
+        ret = init_vlc(&ctx->studio_intra_tab[i], STUDIO_INTRA_BITS, 22,
2845
+                       &ff_mpeg4_studio_intra[i][0][1], 4, 2,
2846
+                       &ff_mpeg4_studio_intra[i][0][0], 4, 2,
2847
+                       0);
2848
+
2849
+        if (ret < 0)
2850
+            return ret;
2851
+    }
2852
+
2853
+    ret = init_vlc(&ctx->studio_luma_dc, STUDIO_INTRA_BITS, 19,
2854
+                   &ff_mpeg4_studio_dc_luma[0][1], 4, 2,
2855
+                   &ff_mpeg4_studio_dc_luma[0][0], 4, 2,
2856
+                   0);
2857
+    if (ret < 0)
2858
+        return ret;
2859
+
2860
+    ret = init_vlc(&ctx->studio_chroma_dc, STUDIO_INTRA_BITS, 19,
2861
+                   &ff_mpeg4_studio_dc_chroma[0][1], 4, 2,
2862
+                   &ff_mpeg4_studio_dc_chroma[0][0], 4, 2,
2863
+                   0);
2864
+    if (ret < 0)
2865
+        return ret;
2866
+
2867
+    return 0;
2868
+}
2869
+
2839 2870
 static av_cold int decode_init(AVCodecContext *avctx)
2840 2871
 {
2841 2872
     Mpeg4DecContext *ctx = avctx->priv_data;
... ...
@@ -2851,6 +3361,8 @@ static av_cold int decode_init(AVCodecContext *avctx)
2851 2851
         return ret;
2852 2852
 
2853 2853
     ff_mpeg4videodec_static_init();
2854
+    if ((ret = init_studio_vlcs(ctx)) < 0)
2855
+        return ret;
2854 2856
 
2855 2857
     s->h263_pred = 1;
2856 2858
     s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
... ...
@@ -386,6 +386,9 @@ static int init_duplicate_context(MpegEncContext *s)
386 386
     for (i = 0; i < 12; i++) {
387 387
         s->pblocks[i] = &s->block[i];
388 388
     }
389
+
390
+    FF_ALLOCZ_OR_GOTO(s->avctx, s->block32, sizeof(*s->block32), fail)
391
+
389 392
     if (s->avctx->codec_tag == AV_RL32("VCR2")) {
390 393
         // exchange uv
391 394
         FFSWAP(void *, s->pblocks[4], s->pblocks[5]);
... ...
@@ -421,6 +424,7 @@ static void free_duplicate_context(MpegEncContext *s)
421 421
     av_freep(&s->me.map);
422 422
     av_freep(&s->me.score_map);
423 423
     av_freep(&s->blocks);
424
+    av_freep(&s->block32);
424 425
     av_freep(&s->ac_val_base);
425 426
     s->block = NULL;
426 427
 }
... ...
@@ -438,6 +442,7 @@ static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
438 438
     COPY(me.score_map);
439 439
     COPY(blocks);
440 440
     COPY(block);
441
+    COPY(block32);
441 442
     COPY(start_mb_y);
442 443
     COPY(end_mb_y);
443 444
     COPY(me.map_generation);
... ...
@@ -811,6 +816,7 @@ static void clear_context(MpegEncContext *s)
811 811
     s->dct_error_sum = NULL;
812 812
     s->block = NULL;
813 813
     s->blocks = NULL;
814
+    s->block32 = NULL;
814 815
     memset(s->pblocks, 0, sizeof(s->pblocks));
815 816
     s->ac_val_base = NULL;
816 817
     s->ac_val[0] =
... ...
@@ -2120,8 +2126,31 @@ void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64],
2120 2120
                 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2121 2121
             }
2122 2122
         } else {
2123
+            /* Only MPEG-4 Simple Studio Profile is supported in > 8-bit mode.
2124
+               TODO: Integrate 10-bit properly into mpegvideo.c so that ER works properly */
2125
+            if (s->avctx->bits_per_raw_sample > 8){
2126
+                const int act_block_size = block_size * 2;
2127
+                s->idsp.idct_put(dest_y,                           dct_linesize, (int16_t*)(*s->block32)[0]);
2128
+                s->idsp.idct_put(dest_y              + act_block_size, dct_linesize, (int16_t*)(*s->block32)[1]);
2129
+                s->idsp.idct_put(dest_y + dct_offset,              dct_linesize, (int16_t*)(*s->block32)[2]);
2130
+                s->idsp.idct_put(dest_y + dct_offset + act_block_size, dct_linesize, (int16_t*)(*s->block32)[3]);
2131
+
2132
+                dct_linesize = uvlinesize << s->interlaced_dct;
2133
+                dct_offset   = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
2134
+
2135
+                s->idsp.idct_put(dest_cb,              dct_linesize, (int16_t*)(*s->block32)[4]);
2136
+                s->idsp.idct_put(dest_cr,              dct_linesize, (int16_t*)(*s->block32)[5]);
2137
+                s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, (int16_t*)(*s->block32)[6]);
2138
+                s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, (int16_t*)(*s->block32)[7]);
2139
+                if(!s->chroma_x_shift){//Chroma444
2140
+                    s->idsp.idct_put(dest_cb + act_block_size,              dct_linesize, (int16_t*)(*s->block32)[8]);
2141
+                    s->idsp.idct_put(dest_cr + act_block_size,              dct_linesize, (int16_t*)(*s->block32)[9]);
2142
+                    s->idsp.idct_put(dest_cb + act_block_size + dct_offset, dct_linesize, (int16_t*)(*s->block32)[10]);
2143
+                    s->idsp.idct_put(dest_cr + act_block_size + dct_offset, dct_linesize, (int16_t*)(*s->block32)[11]);
2144
+                }
2145
+            }
2123 2146
             /* dct only in intra block */
2124
-            if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
2147
+            else if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
2125 2148
                 put_dct(s, block[0], 0, dest_y                          , dct_linesize, s->qscale);
2126 2149
                 put_dct(s, block[1], 1, dest_y              + block_size, dct_linesize, s->qscale);
2127 2150
                 put_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize, s->qscale);
... ...
@@ -2202,7 +2231,8 @@ void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
2202 2202
 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
2203 2203
     const int linesize   = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
2204 2204
     const int uvlinesize = s->current_picture.f->linesize[1];
2205
-    const int mb_size= 4 - s->avctx->lowres;
2205
+    const int width_of_mb = (4 + (s->avctx->bits_per_raw_sample > 8)) - s->avctx->lowres;
2206
+    const int height_of_mb = 4 - s->avctx->lowres;
2206 2207
 
2207 2208
     s->block_index[0]= s->b8_stride*(s->mb_y*2    ) - 2 + s->mb_x*2;
2208 2209
     s->block_index[1]= s->b8_stride*(s->mb_y*2    ) - 1 + s->mb_x*2;
... ...
@@ -2212,20 +2242,20 @@ void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
2212 2212
     s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
2213 2213
     //block_index is not used by mpeg2, so it is not affected by chroma_format
2214 2214
 
2215
-    s->dest[0] = s->current_picture.f->data[0] + (int)((s->mb_x - 1U) <<  mb_size);
2216
-    s->dest[1] = s->current_picture.f->data[1] + (int)((s->mb_x - 1U) << (mb_size - s->chroma_x_shift));
2217
-    s->dest[2] = s->current_picture.f->data[2] + (int)((s->mb_x - 1U) << (mb_size - s->chroma_x_shift));
2215
+    s->dest[0] = s->current_picture.f->data[0] + (int)((s->mb_x - 1U) <<  width_of_mb);
2216
+    s->dest[1] = s->current_picture.f->data[1] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
2217
+    s->dest[2] = s->current_picture.f->data[2] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
2218 2218
 
2219 2219
     if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
2220 2220
     {
2221 2221
         if(s->picture_structure==PICT_FRAME){
2222
-        s->dest[0] += s->mb_y *   linesize << mb_size;
2223
-        s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
2224
-        s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
2222
+        s->dest[0] += s->mb_y *   linesize << height_of_mb;
2223
+        s->dest[1] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
2224
+        s->dest[2] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
2225 2225
         }else{
2226
-            s->dest[0] += (s->mb_y>>1) *   linesize << mb_size;
2227
-            s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
2228
-            s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
2226
+            s->dest[0] += (s->mb_y>>1) *   linesize << height_of_mb;
2227
+            s->dest[1] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
2228
+            s->dest[2] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
2229 2229
             av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
2230 2230
         }
2231 2231
     }
... ...
@@ -45,6 +45,7 @@
45 45
 #include "mpegpicture.h"
46 46
 #include "mpegvideodsp.h"
47 47
 #include "mpegvideoencdsp.h"
48
+#include "mpegvideodata.h"
48 49
 #include "pixblockdsp.h"
49 50
 #include "put_bits.h"
50 51
 #include "ratecontrol.h"
... ...
@@ -71,6 +72,8 @@
71 71
 #define SLICE_MAX_START_CODE    0x000001af
72 72
 #define EXT_START_CODE          0x000001b5
73 73
 #define USER_START_CODE         0x000001b2
74
+#define SLICE_START_CODE        0x000001b7
75
+
74 76
 
75 77
 /**
76 78
  * MpegEncContext.
... ...
@@ -378,6 +381,8 @@ typedef struct MpegEncContext {
378 378
     int custom_pcf;
379 379
 
380 380
     /* MPEG-4 specific */
381
+    int studio_profile;
382
+    int dct_precision;
381 383
     ///< number of bits to represent the fractional part of time (encoder only)
382 384
     int time_increment_bits;
383 385
     int last_time_base;
... ...
@@ -501,7 +506,10 @@ typedef struct MpegEncContext {
501 501
 
502 502
     int16_t (*block)[64]; ///< points to one of the following blocks
503 503
     int16_t (*blocks)[12][64]; // for HQ mode we need to keep the best block
504
-    int (*decode_mb)(struct MpegEncContext *s, int16_t block[6][64]); // used by some codecs to avoid a switch()
504
+    int (*decode_mb)(struct MpegEncContext *s, int16_t block[12][64]); // used by some codecs to avoid a switch()
505
+
506
+    int32_t (*block32)[12][64];
507
+
505 508
 #define SLICE_OK         0
506 509
 #define SLICE_ERROR     -1
507 510
 #define SLICE_END       -2 ///<end marker found
... ...
@@ -729,7 +737,8 @@ void ff_mpv_motion(MpegEncContext *s,
729 729
                    qpel_mc_func (*qpix_op)[16]);
730 730
 
731 731
 static inline void ff_update_block_index(MpegEncContext *s){
732
-    const int block_size= 8 >> s->avctx->lowres;
732
+    const int bytes_per_pixel = 1 + (s->avctx->bits_per_raw_sample > 8);
733
+    const int block_size= (8*bytes_per_pixel) >> s->avctx->lowres;
733 734
 
734 735
     s->block_index[0]+=2;
735 736
     s->block_index[1]+=2;
... ...
@@ -738,8 +747,8 @@ static inline void ff_update_block_index(MpegEncContext *s){
738 738
     s->block_index[4]++;
739 739
     s->block_index[5]++;
740 740
     s->dest[0]+= 2*block_size;
741
-    s->dest[1]+= block_size;
742
-    s->dest[2]+= block_size;
741
+    s->dest[1]+= (2 >> s->chroma_x_shift) * block_size;
742
+    s->dest[2]+= (2 >> s->chroma_x_shift) * block_size;
743 743
 }
744 744
 
745 745
 static inline int get_bits_diff(MpegEncContext *s){
... ...
@@ -751,4 +760,13 @@ static inline int get_bits_diff(MpegEncContext *s){
751 751
     return bits - last;
752 752
 }
753 753
 
754
+static inline int mpeg_get_qscale(MpegEncContext *s)
755
+{
756
+    int qscale = get_bits(&s->gb, 5);
757
+    if (s->q_scale_type)
758
+        return ff_mpeg2_non_linear_qscale[qscale];
759
+    else
760
+        return qscale << 1;
761
+}
762
+
754 763
 #endif /* AVCODEC_MPEGVIDEO_H */
... ...
@@ -123,6 +123,7 @@ av_cold void ff_idctdsp_init_x86(IDCTDSPContext *c, AVCodecContext *avctx,
123 123
         }
124 124
 
125 125
         if (avctx->bits_per_raw_sample == 10 &&
126
+            avctx->codec_id != AV_CODEC_ID_MPEG4 &&
126 127
             (avctx->idct_algo == FF_IDCT_AUTO ||
127 128
              avctx->idct_algo == FF_IDCT_SIMPLEAUTO ||
128 129
              avctx->idct_algo == FF_IDCT_SIMPLE)) {