Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / libavcodec / vc1data.c
1 /*
2  * VC-1 and WMV3 decoder
3  * copyright (c) 2011 Mashiat Sarker Shakkhar
4  * copyright (c) 2006 Konstantin Shishkov
5  * (c) 2005 anonymous, Alex Beregszaszi, Michael Niedermayer
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 /**
25  * @file
26  * VC-1 tables.
27  */
28
29 #include "vc1.h"
30 #include "vc1data.h"
31 #include "vlc.h"
32
33 /** Table for conversion between TTBLK and TTMB */
34 const int ff_vc1_ttblk_to_tt[3][8] = {
35     { TT_8X4, TT_4X8, TT_8X8, TT_4X4, TT_8X4_TOP, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT },
36     { TT_8X8, TT_4X8_RIGHT, TT_4X8_LEFT, TT_4X4, TT_8X4, TT_4X8, TT_8X4_BOTTOM, TT_8X4_TOP },
37     { TT_8X8, TT_4X8, TT_4X4, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT, TT_8X4, TT_8X4_TOP }
38 };
39
40 const int ff_vc1_ttfrm_to_tt[4] = { TT_8X8, TT_8X4, TT_4X8, TT_4X4 };
41
42 /** MV P mode - the 5th element is only used for mode 1 */
43 const uint8_t ff_vc1_mv_pmode_table[2][5] = {
44     { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_MIXED_MV },
45     { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_1MV_HPEL_BILIN }
46 };
47 const uint8_t ff_vc1_mv_pmode_table2[2][4] = {
48     { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV },
49     { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN }
50 };
51
52 /* MBMODE table for interlaced frame P-picture */
53 const uint8_t ff_vc1_mbmode_intfrp[2][15][4] = {
54     { /* 1: 4-MV, 0: non-4-MV */
55         /* Type, FIELDTX, 1-MV Differential present, Residuals (CBP) present */
56         /* Table 164 - Table 167 */
57         { MV_PMODE_INTFR_1MV      , 0, 1, 1 },
58         { MV_PMODE_INTFR_1MV      , 1, 1, 1 },
59         { MV_PMODE_INTFR_1MV      , 0, 1, 0 },
60         { MV_PMODE_INTFR_1MV      , 0, 0, 1 },
61         { MV_PMODE_INTFR_1MV      , 1, 0, 1 },
62         { MV_PMODE_INTFR_2MV_FIELD, 0, 0, 1 },
63         { MV_PMODE_INTFR_2MV_FIELD, 1, 0, 1 },
64         { MV_PMODE_INTFR_2MV_FIELD, 1, 0, 0 },
65         { MV_PMODE_INTFR_INTRA    , 0, 0, 0 }
66     },
67     {
68         /* Table 160 - Table 163 */
69         { MV_PMODE_INTFR_1MV      , 0, 1, 1 },
70         { MV_PMODE_INTFR_1MV      , 1, 1, 1 },
71         { MV_PMODE_INTFR_1MV      , 0, 1, 0 },
72         { MV_PMODE_INTFR_1MV      , 0, 0, 1 },
73         { MV_PMODE_INTFR_1MV      , 1, 0, 1 },
74         { MV_PMODE_INTFR_2MV_FIELD, 0, 0, 1 },
75         { MV_PMODE_INTFR_2MV_FIELD, 1, 0, 1 },
76         { MV_PMODE_INTFR_2MV_FIELD, 1, 0, 0 },
77         { MV_PMODE_INTFR_4MV      , 0, 0, 1 },
78         { MV_PMODE_INTFR_4MV      , 1, 0, 1 },
79         { MV_PMODE_INTFR_4MV      , 0, 0, 0 },
80         { MV_PMODE_INTFR_4MV_FIELD, 0, 0, 1 },
81         { MV_PMODE_INTFR_4MV_FIELD, 1, 0, 1 },
82         { MV_PMODE_INTFR_4MV_FIELD, 1, 0, 0 },
83         { MV_PMODE_INTFR_INTRA    , 0, 0, 0 }
84     }
85 };
86
87 const int ff_vc1_fps_nr[7] = { 24, 25, 30, 50, 60, 48, 72 },
88           ff_vc1_fps_dr[2] = { 1000, 1001 };
89 const uint8_t ff_vc1_pquant_table[3][32] = {
90     /* Implicit quantizer */
91     {  0,  1,  2,  3,  4,  5,  6,  7,  8,  6,  7,  8,  9, 10, 11, 12,
92       13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31 },
93     /* Explicit quantizer, pquantizer uniform */
94     {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
95       16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 },
96     /* Explicit quantizer, pquantizer non-uniform */
97     {  0,  1,  1,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,
98       14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31 }
99 };
100
101 /** @name VC-1 VLC tables
102  *  @todo TODO move this into the context
103  */
104 //@{
105 VLC ff_vc1_imode_vlc;
106 VLC ff_vc1_norm2_vlc;
107 VLC ff_vc1_norm6_vlc;
108 /* Could be optimized, one table only needs 8 bits */
109 VLC ff_vc1_ttmb_vlc[3];
110 VLC ff_vc1_mv_diff_vlc[4];
111 VLC ff_vc1_cbpcy_p_vlc[4];
112 VLC ff_vc1_icbpcy_vlc[8];
113 VLC ff_vc1_4mv_block_pattern_vlc[4];
114 VLC ff_vc1_2mv_block_pattern_vlc[4];
115 VLC ff_vc1_ttblk_vlc[3];
116 VLC ff_vc1_subblkpat_vlc[3];
117 VLC ff_vc1_intfr_4mv_mbmode_vlc[4];
118 VLC ff_vc1_intfr_non4mv_mbmode_vlc[4];
119 VLC ff_vc1_if_mmv_mbmode_vlc[8];
120 VLC ff_vc1_if_1mv_mbmode_vlc[8];
121 VLC ff_vc1_1ref_mvdata_vlc[4];
122 VLC ff_vc1_2ref_mvdata_vlc[8];
123
124 VLC ff_vc1_ac_coeff_table[8];
125
126 //@}
127
128
129 #if B_FRACTION_DEN == 840 // original bfraction from vc9data.h, not conforming to standard
130 /* bfraction is fractional, we scale to the GCD 3*5*7*8 = 840 */
131 const int16_t ff_vc1_bfraction_lut[23] = {
132     420 /*1/2*/, 280 /*1/3*/, 560 /*2/3*/, 210 /*1/4*/,
133     630 /*3/4*/, 168 /*1/5*/, 336 /*2/5*/,
134     504 /*3/5*/, 672 /*4/5*/, 140 /*1/6*/, 700 /*5/6*/,
135     120 /*1/7*/, 240 /*2/7*/, 360 /*3/7*/, 480 /*4/7*/,
136     600 /*5/7*/, 720 /*6/7*/, 105 /*1/8*/, 315 /*3/8*/,
137     525 /*5/8*/, 735 /*7/8*/,
138     -1 /*inv.*/, 0 /*BI fm*/
139 };
140 #else
141 /* pre-computed scales for all bfractions and base=256 */
142 const int16_t ff_vc1_bfraction_lut[23] = {
143     128 /*1/2*/,  85 /*1/3*/, 170 /*2/3*/,  64 /*1/4*/,
144     192 /*3/4*/,  51 /*1/5*/, 102 /*2/5*/,
145     153 /*3/5*/, 204 /*4/5*/,  43 /*1/6*/, 215 /*5/6*/,
146      37 /*1/7*/,  74 /*2/7*/, 111 /*3/7*/, 148 /*4/7*/,
147     185 /*5/7*/, 222 /*6/7*/,  32 /*1/8*/,  96 /*3/8*/,
148     160 /*5/8*/, 224 /*7/8*/,
149     -1 /*inv.*/, 0 /*BI fm*/
150 };
151 #endif
152
153 //Same as H.264
154 const AVRational ff_vc1_pixel_aspect[16] = {
155     {   0,  1 },
156     {   1,  1 },
157     {  12, 11 },
158     {  10, 11 },
159     {  16, 11 },
160     {  40, 33 },
161     {  24, 11 },
162     {  20, 11 },
163     {  32, 11 },
164     {  80, 33 },
165     {  18, 11 },
166     {  15, 11 },
167     {  64, 33 },
168     { 160, 99 },
169     {   0,  1 },
170     {   0,  1 }
171 };
172
173 const uint8_t ff_wmv3_dc_scale_table[32] = {
174      0,  2,  4,  8,  8,  8,  9,  9, 10, 10, 11, 11, 12, 12, 13, 13,
175     14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21
176 };
177
178
179 /* DC differentials low+hi-mo, p217 are the same as in msmpeg4data .h */
180
181 /* Table 232 */
182 const uint8_t ff_vc1_simple_progressive_4x4_zz [16] = {
183      0,     8,    16,     1,
184      9,    24,    17,     2,
185     10,    18,    25,     3,
186     11,    26,    19,    27
187 };
188
189 const uint8_t ff_vc1_adv_progressive_8x4_zz [32] = { /* Table 233 */
190      0,     8,     1,    16,     2,     9,    10,     3,
191     24,    17,     4,    11,    18,    12,     5,    19,
192     25,    13,    20,    26,    27,     6,    21,    28,
193     14,    22,    29,     7,    30,    15,    23,    31
194 };
195
196 const uint8_t ff_vc1_adv_progressive_4x8_zz [32] = { /* Table 234 */
197      0,     1,     8,     2,
198      9,    16,    17,    24,
199     10,    32,    25,    18,
200     40,     3,    33,    26,
201     48,    11,    56,    41,
202     34,    49,    57,    42,
203     19,    50,    27,    58,
204     35,    43,    51,    59
205 };
206
207 const uint8_t ff_vc1_adv_interlaced_8x8_zz [64] = { /* Table 235 */
208      0,     8,     1,    16,    24,     9,     2,    32,
209     40,    48,    56,    17,    10,     3,    25,    18,
210     11,     4,    33,    41,    49,    57,    26,    34,
211     42,    50,    58,    19,    12,     5,    27,    20,
212     13,     6,    35,    28,    21,    14,     7,    15,
213     22,    29,    36,    43,    51,    59,    60,    52,
214     44,    37,    30,    23,    31,    38,    45,    53,
215     61,    62,    54,    46,    39,    47,    55,    63
216 };
217
218 const uint8_t ff_vc1_adv_interlaced_8x4_zz [32] = { /* Table 236 */
219      0,     8,    16,    24,     1,     9,     2,    17,
220     25,    10,     3,    18,    26,     4,    11,    19,
221     12,     5,    13,    20,    27,     6,    21,    28,
222     14,    22,    29,     7,    30,    15,    23,    31
223 };
224
225 const uint8_t ff_vc1_adv_interlaced_4x8_zz [32] = { /* Table 237 */
226      0,     1,     2,     8,
227     16,     9,    24,    17,
228     10,     3,    32,    40,
229     48,    56,    25,    18,
230     33,    26,    41,    34,
231     49,    57,    11,    42,
232     19,    50,    27,    58,
233     35,    43,    51,    59
234 };
235
236 const uint8_t ff_vc1_adv_interlaced_4x4_zz [16] = { /* Table 238 */
237      0,     8,    16,    24,
238      1,     9,    17,     2,
239     25,    10,    18,     3,
240     26,    11,    19,    27
241 };
242
243
244 /* DQScale as specified in 8.1.3.9 - almost identical to 0x40000/i */
245 const int32_t ff_vc1_dqscale[63] = {
246     0x40000, 0x20000, 0x15555, 0x10000, 0xCCCD, 0xAAAB, 0x9249, 0x8000,
247      0x71C7,  0x6666,  0x5D17,  0x5555, 0x4EC5, 0x4925, 0x4444, 0x4000,
248      0x3C3C,  0x38E4,  0x35E5,  0x3333, 0x30C3, 0x2E8C, 0x2C86, 0x2AAB,
249      0x28F6,  0x2762,  0x25ED,  0x2492, 0x234F, 0x2222, 0x2108, 0x2000,
250      0x1F08,  0x1E1E,  0x1D42,  0x1C72, 0x1BAD, 0x1AF3, 0x1A42, 0x199A,
251      0x18FA,  0x1862,  0x17D0,  0x1746, 0x16C1, 0x1643, 0x15CA, 0x1555,
252      0x14E6,  0x147B,  0x1414,  0x13B1, 0x1352, 0x12F7, 0x129E, 0x1249,
253      0x11F7,  0x11A8,  0x115B,  0x1111, 0x10C9, 0x1084, 0x1041
254 };
255
256 /* P Interlaced field picture MV predictor scaling values (Table 114) */
257 const uint16_t ff_vc1_field_mvpred_scales[2][7][4] = {
258 // Refdist:
259 //      0       1       2       3 or greater
260   { // current field is first
261     { 128,    192,    213,    224 },   // SCALEOPP
262     { 512,    341,    307,    293 },   // SCALESAME1
263     { 219,    236,    242,    245 },   // SCALESAME2
264     {  32,     48,     53,     56 },   // SCALEZONE1_X
265     {   8,     12,     13,     14 },   // SCALEZONE1_Y
266     {  37,     20,     14,     11 },   // ZONE1OFFSET_X
267     {  10,      5,      4,      3 }    // ZONE1OFFSET_Y
268   },
269   { // current field is second
270     { 128,     64,     43,     32 },   // SCALEOPP
271     { 512,   1024,   1536,   2048 },   // SCALESAME1
272     { 219,    204,    200,    198 },   // SCALESAME2
273     {  32,     16,     11,      8 },   // SCALEZONE1_X
274     {   8,      4,      3,      2 },   // SCALEZONE1_Y
275     {  37,     52,     56,     58 },   // ZONE1OFFSET_X
276     {  10,     13,     14,     15 }    // ZONE1OFFSET_Y
277   }
278 };
279
280 /* B Interlaced field picture backward MV predictor scaling values for first field (Table 115) */
281 const uint16_t ff_vc1_b_field_mvpred_scales[7][4] = {
282     // BRFD:
283     //  0       1       2       3 or greater
284     { 171,    205,    219,    228 },   // SCALESAME
285     { 384,    320,    299,    288 },   // SCALEOPP1
286     { 230,    239,    244,    246 },   // SCALEOPP2
287     {  43,     51,     55,     57 },   // SCALEZONE1_X
288     {  11,     13,     14,     14 },   // SCALEZONE1_Y
289     {  26,     17,     12,     10 },   // ZONE1OFFSET_X
290     {   7,      4,      3,      3 }    // ZONE1OFFSET_Y
291 };