]> git.sesse.net Git - ffmpeg/blob - libavcodec/cavsdec.c
33e639b9ae3dab8a086be5f4d50f83b973cc4bf6
[ffmpeg] / libavcodec / cavsdec.c
1 /*
2  * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
3  * Copyright (c) 2006  Stefan Gehrer <stefan.gehrer@gmx.de>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
25  * @author Stefan Gehrer <stefan.gehrer@gmx.de>
26  */
27
28 #include "avcodec.h"
29 #include "get_bits.h"
30 #include "golomb.h"
31 #include "cavs.h"
32
33 static const uint8_t mv_scan[4] = {
34     MV_FWD_X0,MV_FWD_X1,
35     MV_FWD_X2,MV_FWD_X3
36 };
37
38 static const uint8_t cbp_tab[64][2] = {
39   {63, 0},{15,15},{31,63},{47,31},{ 0,16},{14,32},{13,47},{11,13},
40   { 7,14},{ 5,11},{10,12},{ 8, 5},{12,10},{61, 7},{ 4,48},{55, 3},
41   { 1, 2},{ 2, 8},{59, 4},{ 3, 1},{62,61},{ 9,55},{ 6,59},{29,62},
42   {45,29},{51,27},{23,23},{39,19},{27,30},{46,28},{53, 9},{30, 6},
43   {43,60},{37,21},{60,44},{16,26},{21,51},{28,35},{19,18},{35,20},
44   {42,24},{26,53},{44,17},{32,37},{58,39},{24,45},{20,58},{17,43},
45   {18,42},{48,46},{22,36},{33,33},{25,34},{49,40},{40,52},{36,49},
46   {34,50},{50,56},{52,25},{54,22},{41,54},{56,57},{38,41},{57,38}
47 };
48
49 static const uint8_t scan3x3[4] = { 4, 5, 7, 8 };
50
51 static const uint8_t cavs_chroma_qp[64] = {
52   0,  1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,
53   16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
54   32,33,34,35,36,37,38,39,40,41,42,42,43,43,44,44,
55   45,45,46,46,47,47,48,48,48,49,49,49,50,50,50,51
56 };
57
58 static const uint8_t dequant_shift[64] = {
59   14,14,14,14,14,14,14,14,
60   13,13,13,13,13,13,13,13,
61   13,12,12,12,12,12,12,12,
62   11,11,11,11,11,11,11,11,
63   11,10,10,10,10,10,10,10,
64   10, 9, 9, 9, 9, 9, 9, 9,
65   9, 8, 8, 8, 8, 8, 8, 8,
66   7, 7, 7, 7, 7, 7, 7, 7
67 };
68
69 static const uint16_t dequant_mul[64] = {
70   32768,36061,38968,42495,46341,50535,55437,60424,
71   32932,35734,38968,42495,46177,50535,55109,59933,
72   65535,35734,38968,42577,46341,50617,55027,60097,
73   32809,35734,38968,42454,46382,50576,55109,60056,
74   65535,35734,38968,42495,46320,50515,55109,60076,
75   65535,35744,38968,42495,46341,50535,55099,60087,
76   65535,35734,38973,42500,46341,50535,55109,60097,
77   32771,35734,38965,42497,46341,50535,55109,60099
78 };
79
80 #define EOB 0,0,0
81
82 static const struct dec_2dvlc intra_dec[7] = {
83   {
84     { //level / run / table_inc
85       {  1, 1, 1},{ -1, 1, 1},{  1, 2, 1},{ -1, 2, 1},{  1, 3, 1},{ -1, 3, 1},
86       {  1, 4, 1},{ -1, 4, 1},{  1, 5, 1},{ -1, 5, 1},{  1, 6, 1},{ -1, 6, 1},
87       {  1, 7, 1},{ -1, 7, 1},{  1, 8, 1},{ -1, 8, 1},{  1, 9, 1},{ -1, 9, 1},
88       {  1,10, 1},{ -1,10, 1},{  1,11, 1},{ -1,11, 1},{  2, 1, 2},{ -2, 1, 2},
89       {  1,12, 1},{ -1,12, 1},{  1,13, 1},{ -1,13, 1},{  1,14, 1},{ -1,14, 1},
90       {  1,15, 1},{ -1,15, 1},{  2, 2, 2},{ -2, 2, 2},{  1,16, 1},{ -1,16, 1},
91       {  1,17, 1},{ -1,17, 1},{  3, 1, 3},{ -3, 1, 3},{  1,18, 1},{ -1,18, 1},
92       {  1,19, 1},{ -1,19, 1},{  2, 3, 2},{ -2, 3, 2},{  1,20, 1},{ -1,20, 1},
93       {  1,21, 1},{ -1,21, 1},{  2, 4, 2},{ -2, 4, 2},{  1,22, 1},{ -1,22, 1},
94       {  2, 5, 2},{ -2, 5, 2},{  1,23, 1},{ -1,23, 1},{   EOB   }
95     },
96     //level_add
97     { 0, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
98       2, 2, 2, 2, 2, 2, 2,-1,-1,-1},
99     2, //golomb_order
100     0, //inc_limit
101     23, //max_run
102   },{
103     { //level / run
104       {  1, 1, 0},{ -1, 1, 0},{  1, 2, 0},{ -1, 2, 0},{  2, 1, 1},{ -2, 1, 1},
105       {  1, 3, 0},{ -1, 3, 0},{   EOB   },{  1, 4, 0},{ -1, 4, 0},{  1, 5, 0},
106       { -1, 5, 0},{  1, 6, 0},{ -1, 6, 0},{  3, 1, 2},{ -3, 1, 2},{  2, 2, 1},
107       { -2, 2, 1},{  1, 7, 0},{ -1, 7, 0},{  1, 8, 0},{ -1, 8, 0},{  1, 9, 0},
108       { -1, 9, 0},{  2, 3, 1},{ -2, 3, 1},{  4, 1, 2},{ -4, 1, 2},{  1,10, 0},
109       { -1,10, 0},{  1,11, 0},{ -1,11, 0},{  2, 4, 1},{ -2, 4, 1},{  3, 2, 2},
110       { -3, 2, 2},{  1,12, 0},{ -1,12, 0},{  2, 5, 1},{ -2, 5, 1},{  5, 1, 3},
111       { -5, 1, 3},{  1,13, 0},{ -1,13, 0},{  2, 6, 1},{ -2, 6, 1},{  1,14, 0},
112       { -1,14, 0},{  2, 7, 1},{ -2, 7, 1},{  2, 8, 1},{ -2, 8, 1},{  3, 3, 2},
113       { -3, 3, 2},{  6, 1, 3},{ -6, 1, 3},{  1,15, 0},{ -1,15, 0}
114     },
115     //level_add
116     { 0, 7, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2,-1,
117       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
118     2, //golomb_order
119     1, //inc_limit
120     15, //max_run
121   },{
122     { //level / run
123       {  1, 1, 0},{ -1, 1, 0},{  2, 1, 0},{ -2, 1, 0},{  1, 2, 0},{ -1, 2, 0},
124       {  3, 1, 1},{ -3, 1, 1},{   EOB   },{  1, 3, 0},{ -1, 3, 0},{  2, 2, 0},
125       { -2, 2, 0},{  4, 1, 1},{ -4, 1, 1},{  1, 4, 0},{ -1, 4, 0},{  5, 1, 2},
126       { -5, 1, 2},{  1, 5, 0},{ -1, 5, 0},{  3, 2, 1},{ -3, 2, 1},{  2, 3, 0},
127       { -2, 3, 0},{  1, 6, 0},{ -1, 6, 0},{  6, 1, 2},{ -6, 1, 2},{  2, 4, 0},
128       { -2, 4, 0},{  1, 7, 0},{ -1, 7, 0},{  4, 2, 1},{ -4, 2, 1},{  7, 1, 2},
129       { -7, 1, 2},{  3, 3, 1},{ -3, 3, 1},{  2, 5, 0},{ -2, 5, 0},{  1, 8, 0},
130       { -1, 8, 0},{  2, 6, 0},{ -2, 6, 0},{  8, 1, 3},{ -8, 1, 3},{  1, 9, 0},
131       { -1, 9, 0},{  5, 2, 2},{ -5, 2, 2},{  3, 4, 1},{ -3, 4, 1},{  2, 7, 0},
132       { -2, 7, 0},{  9, 1, 3},{ -9, 1, 3},{  1,10, 0},{ -1,10, 0}
133     },
134     //level_add
135     { 0,10, 6, 4, 4, 3, 3, 3, 2, 2, 2,-1,-1,-1,-1,-1,-1,
136      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
137     2, //golomb_order
138     2, //inc_limit
139     10, //max_run
140   },{
141     { //level / run
142       {  1, 1, 0},{ -1, 1, 0},{  2, 1, 0},{ -2, 1, 0},{  3, 1, 0},{ -3, 1, 0},
143       {  1, 2, 0},{ -1, 2, 0},{   EOB   },{  4, 1, 0},{ -4, 1, 0},{  5, 1, 1},
144       { -5, 1, 1},{  2, 2, 0},{ -2, 2, 0},{  1, 3, 0},{ -1, 3, 0},{  6, 1, 1},
145       { -6, 1, 1},{  3, 2, 0},{ -3, 2, 0},{  7, 1, 1},{ -7, 1, 1},{  1, 4, 0},
146       { -1, 4, 0},{  8, 1, 2},{ -8, 1, 2},{  2, 3, 0},{ -2, 3, 0},{  4, 2, 0},
147       { -4, 2, 0},{  1, 5, 0},{ -1, 5, 0},{  9, 1, 2},{ -9, 1, 2},{  5, 2, 1},
148       { -5, 2, 1},{  2, 4, 0},{ -2, 4, 0},{ 10, 1, 2},{-10, 1, 2},{  3, 3, 0},
149       { -3, 3, 0},{  1, 6, 0},{ -1, 6, 0},{ 11, 1, 3},{-11, 1, 3},{  6, 2, 1},
150       { -6, 2, 1},{  1, 7, 0},{ -1, 7, 0},{  2, 5, 0},{ -2, 5, 0},{  3, 4, 0},
151       { -3, 4, 0},{ 12, 1, 3},{-12, 1, 3},{  4, 3, 0},{ -4, 3, 0}
152     },
153     //level_add
154     { 0,13, 7, 5, 4, 3, 2, 2,-1,-1,-1 -1,-1,-1,-1,-1,-1,
155      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
156     2, //golomb_order
157     4, //inc_limit
158     7, //max_run
159   },{
160     { //level / run
161       {  1, 1, 0},{ -1, 1, 0},{  2, 1, 0},{ -2, 1, 0},{  3, 1, 0},{ -3, 1, 0},
162       {   EOB   },{  4, 1, 0},{ -4, 1, 0},{  5, 1, 0},{ -5, 1, 0},{  6, 1, 0},
163       { -6, 1, 0},{  1, 2, 0},{ -1, 2, 0},{  7, 1, 0},{ -7, 1, 0},{  8, 1, 1},
164       { -8, 1, 1},{  2, 2, 0},{ -2, 2, 0},{  9, 1, 1},{ -9, 1, 1},{ 10, 1, 1},
165       {-10, 1, 1},{  1, 3, 0},{ -1, 3, 0},{  3, 2, 0},{ -3, 2, 0},{ 11, 1, 2},
166       {-11, 1, 2},{  4, 2, 0},{ -4, 2, 0},{ 12, 1, 2},{-12, 1, 2},{ 13, 1, 2},
167       {-13, 1, 2},{  5, 2, 0},{ -5, 2, 0},{  1, 4, 0},{ -1, 4, 0},{  2, 3, 0},
168       { -2, 3, 0},{ 14, 1, 2},{-14, 1, 2},{  6, 2, 0},{ -6, 2, 0},{ 15, 1, 2},
169       {-15, 1, 2},{ 16, 1, 2},{-16, 1, 2},{  3, 3, 0},{ -3, 3, 0},{  1, 5, 0},
170       { -1, 5, 0},{  7, 2, 0},{ -7, 2, 0},{ 17, 1, 2},{-17, 1, 2}
171     },
172     //level_add
173     { 0,18, 8, 4, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
174      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
175     2, //golomb_order
176     7, //inc_limit
177     5, //max_run
178   },{
179     { //level / run
180       {   EOB   },{  1, 1, 0},{ -1, 1, 0},{  2, 1, 0},{ -2, 1, 0},{  3, 1, 0},
181       { -3, 1, 0},{  4, 1, 0},{ -4, 1, 0},{  5, 1, 0},{ -5, 1, 0},{  6, 1, 0},
182       { -6, 1, 0},{  7, 1, 0},{ -7, 1, 0},{  8, 1, 0},{ -8, 1, 0},{  9, 1, 0},
183       { -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{  1, 2, 0},{ -1, 2, 0},{ 11, 1, 1},
184       {-11, 1, 1},{ 12, 1, 1},{-12, 1, 1},{ 13, 1, 1},{-13, 1, 1},{  2, 2, 0},
185       { -2, 2, 0},{ 14, 1, 1},{-14, 1, 1},{ 15, 1, 1},{-15, 1, 1},{  3, 2, 0},
186       { -3, 2, 0},{ 16, 1, 1},{-16, 1, 1},{  1, 3, 0},{ -1, 3, 0},{ 17, 1, 1},
187       {-17, 1, 1},{  4, 2, 0},{ -4, 2, 0},{ 18, 1, 1},{-18, 1, 1},{  5, 2, 0},
188       { -5, 2, 0},{ 19, 1, 1},{-19, 1, 1},{ 20, 1, 1},{-20, 1, 1},{  6, 2, 0},
189       { -6, 2, 0},{ 21, 1, 1},{-21, 1, 1},{  2, 3, 0},{ -2, 3, 0}
190     },
191     //level_add
192     { 0,22, 7, 3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
193      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
194     2, //golomb_order
195     10, //inc_limit
196     3, //max_run
197   },{
198     { //level / run
199       {   EOB   },{  1, 1, 0},{ -1, 1, 0},{  2, 1, 0},{ -2, 1, 0},{  3, 1, 0},
200       { -3, 1, 0},{  4, 1, 0},{ -4, 1, 0},{  5, 1, 0},{ -5, 1, 0},{  6, 1, 0},
201       { -6, 1, 0},{  7, 1, 0},{ -7, 1, 0},{  8, 1, 0},{ -8, 1, 0},{  9, 1, 0},
202       { -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 11, 1, 0},{-11, 1, 0},{ 12, 1, 0},
203       {-12, 1, 0},{ 13, 1, 0},{-13, 1, 0},{ 14, 1, 0},{-14, 1, 0},{ 15, 1, 0},
204       {-15, 1, 0},{ 16, 1, 0},{-16, 1, 0},{  1, 2, 0},{ -1, 2, 0},{ 17, 1, 0},
205       {-17, 1, 0},{ 18, 1, 0},{-18, 1, 0},{ 19, 1, 0},{-19, 1, 0},{ 20, 1, 0},
206       {-20, 1, 0},{ 21, 1, 0},{-21, 1, 0},{  2, 2, 0},{ -2, 2, 0},{ 22, 1, 0},
207       {-22, 1, 0},{ 23, 1, 0},{-23, 1, 0},{ 24, 1, 0},{-24, 1, 0},{ 25, 1, 0},
208       {-25, 1, 0},{  3, 2, 0},{ -3, 2, 0},{ 26, 1, 0},{-26, 1, 0}
209     },
210     //level_add
211     { 0,27, 4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
212      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
213     2, //golomb_order
214     INT_MAX, //inc_limit
215     2, //max_run
216   }
217 };
218
219 static const struct dec_2dvlc inter_dec[7] = {
220   {
221     { //level / run
222       {  1, 1, 1},{ -1, 1, 1},{  1, 2, 1},{ -1, 2, 1},{  1, 3, 1},{ -1, 3, 1},
223       {  1, 4, 1},{ -1, 4, 1},{  1, 5, 1},{ -1, 5, 1},{  1, 6, 1},{ -1, 6, 1},
224       {  1, 7, 1},{ -1, 7, 1},{  1, 8, 1},{ -1, 8, 1},{  1, 9, 1},{ -1, 9, 1},
225       {  1,10, 1},{ -1,10, 1},{  1,11, 1},{ -1,11, 1},{  1,12, 1},{ -1,12, 1},
226       {  1,13, 1},{ -1,13, 1},{  2, 1, 2},{ -2, 1, 2},{  1,14, 1},{ -1,14, 1},
227       {  1,15, 1},{ -1,15, 1},{  1,16, 1},{ -1,16, 1},{  1,17, 1},{ -1,17, 1},
228       {  1,18, 1},{ -1,18, 1},{  1,19, 1},{ -1,19, 1},{  3, 1, 3},{ -3, 1, 3},
229       {  1,20, 1},{ -1,20, 1},{  1,21, 1},{ -1,21, 1},{  2, 2, 2},{ -2, 2, 2},
230       {  1,22, 1},{ -1,22, 1},{  1,23, 1},{ -1,23, 1},{  1,24, 1},{ -1,24, 1},
231       {  1,25, 1},{ -1,25, 1},{  1,26, 1},{ -1,26, 1},{   EOB   }
232     },
233     //level_add
234     { 0, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
235       2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
236     3, //golomb_order
237     0, //inc_limit
238     26 //max_run
239   },{
240     { //level / run
241       {  1, 1, 0},{ -1, 1, 0},{   EOB   },{  1, 2, 0},{ -1, 2, 0},{  1, 3, 0},
242       { -1, 3, 0},{  1, 4, 0},{ -1, 4, 0},{  1, 5, 0},{ -1, 5, 0},{  1, 6, 0},
243       { -1, 6, 0},{  2, 1, 1},{ -2, 1, 1},{  1, 7, 0},{ -1, 7, 0},{  1, 8, 0},
244       { -1, 8, 0},{  1, 9, 0},{ -1, 9, 0},{  1,10, 0},{ -1,10, 0},{  2, 2, 1},
245       { -2, 2, 1},{  1,11, 0},{ -1,11, 0},{  1,12, 0},{ -1,12, 0},{  3, 1, 2},
246       { -3, 1, 2},{  1,13, 0},{ -1,13, 0},{  1,14, 0},{ -1,14, 0},{  2, 3, 1},
247       { -2, 3, 1},{  1,15, 0},{ -1,15, 0},{  2, 4, 1},{ -2, 4, 1},{  1,16, 0},
248       { -1,16, 0},{  2, 5, 1},{ -2, 5, 1},{  1,17, 0},{ -1,17, 0},{  4, 1, 3},
249       { -4, 1, 3},{  2, 6, 1},{ -2, 6, 1},{  1,18, 0},{ -1,18, 0},{  1,19, 0},
250       { -1,19, 0},{  2, 7, 1},{ -2, 7, 1},{  3, 2, 2},{ -3, 2, 2}
251     },
252     //level_add
253     { 0, 5, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2,
254       2, 2, 2,-1,-1,-1,-1,-1,-1,-1},
255     2, //golomb_order
256     1, //inc_limit
257     19 //max_run
258   },{
259     { //level / run
260       {  1, 1, 0},{ -1, 1, 0},{   EOB   },{  1, 2, 0},{ -1, 2, 0},{  2, 1, 0},
261       { -2, 1, 0},{  1, 3, 0},{ -1, 3, 0},{  1, 4, 0},{ -1, 4, 0},{  3, 1, 1},
262       { -3, 1, 1},{  2, 2, 0},{ -2, 2, 0},{  1, 5, 0},{ -1, 5, 0},{  1, 6, 0},
263       { -1, 6, 0},{  1, 7, 0},{ -1, 7, 0},{  2, 3, 0},{ -2, 3, 0},{  4, 1, 2},
264       { -4, 1, 2},{  1, 8, 0},{ -1, 8, 0},{  3, 2, 1},{ -3, 2, 1},{  2, 4, 0},
265       { -2, 4, 0},{  1, 9, 0},{ -1, 9, 0},{  1,10, 0},{ -1,10, 0},{  5, 1, 2},
266       { -5, 1, 2},{  2, 5, 0},{ -2, 5, 0},{  1,11, 0},{ -1,11, 0},{  2, 6, 0},
267       { -2, 6, 0},{  1,12, 0},{ -1,12, 0},{  3, 3, 1},{ -3, 3, 1},{  6, 1, 2},
268       { -6, 1, 2},{  4, 2, 2},{ -4, 2, 2},{  1,13, 0},{ -1,13, 0},{  2, 7, 0},
269       { -2, 7, 0},{  3, 4, 1},{ -3, 4, 1},{  1,14, 0},{ -1,14, 0}
270     },
271     //level_add
272     { 0, 7, 5, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2,-1,-1,
273       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
274     2, //golomb_order
275     2, //inc_limit
276     14 //max_run
277   },{
278     { //level / run
279       {  1, 1, 0},{ -1, 1, 0},{   EOB   },{  2, 1, 0},{ -2, 1, 0},{  1, 2, 0},
280       { -1, 2, 0},{  3, 1, 0},{ -3, 1, 0},{  1, 3, 0},{ -1, 3, 0},{  2, 2, 0},
281       { -2, 2, 0},{  4, 1, 1},{ -4, 1, 1},{  1, 4, 0},{ -1, 4, 0},{  5, 1, 1},
282       { -5, 1, 1},{  1, 5, 0},{ -1, 5, 0},{  3, 2, 0},{ -3, 2, 0},{  2, 3, 0},
283       { -2, 3, 0},{  1, 6, 0},{ -1, 6, 0},{  6, 1, 1},{ -6, 1, 1},{  2, 4, 0},
284       { -2, 4, 0},{  1, 7, 0},{ -1, 7, 0},{  4, 2, 1},{ -4, 2, 1},{  7, 1, 2},
285       { -7, 1, 2},{  3, 3, 0},{ -3, 3, 0},{  1, 8, 0},{ -1, 8, 0},{  2, 5, 0},
286       { -2, 5, 0},{  8, 1, 2},{ -8, 1, 2},{  1, 9, 0},{ -1, 9, 0},{  3, 4, 0},
287       { -3, 4, 0},{  2, 6, 0},{ -2, 6, 0},{  5, 2, 1},{ -5, 2, 1},{  1,10, 0},
288       { -1,10, 0},{  9, 1, 2},{ -9, 1, 2},{  4, 3, 1},{ -4, 3, 1}
289     },
290     //level_add
291     { 0,10, 6, 5, 4, 3, 3, 2, 2, 2, 2,-1,-1,-1,-1,-1,-1,
292      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
293     2, //golomb_order
294     3, //inc_limit
295     10 //max_run
296   },{
297     { //level / run
298       {  1, 1, 0},{ -1, 1, 0},{   EOB   },{  2, 1, 0},{ -2, 1, 0},{  3, 1, 0},
299       { -3, 1, 0},{  1, 2, 0},{ -1, 2, 0},{  4, 1, 0},{ -4, 1, 0},{  5, 1, 0},
300       { -5, 1, 0},{  2, 2, 0},{ -2, 2, 0},{  1, 3, 0},{ -1, 3, 0},{  6, 1, 0},
301       { -6, 1, 0},{  3, 2, 0},{ -3, 2, 0},{  7, 1, 1},{ -7, 1, 1},{  1, 4, 0},
302       { -1, 4, 0},{  8, 1, 1},{ -8, 1, 1},{  2, 3, 0},{ -2, 3, 0},{  4, 2, 0},
303       { -4, 2, 0},{  1, 5, 0},{ -1, 5, 0},{  9, 1, 1},{ -9, 1, 1},{  5, 2, 0},
304       { -5, 2, 0},{  2, 4, 0},{ -2, 4, 0},{  1, 6, 0},{ -1, 6, 0},{ 10, 1, 2},
305       {-10, 1, 2},{  3, 3, 0},{ -3, 3, 0},{ 11, 1, 2},{-11, 1, 2},{  1, 7, 0},
306       { -1, 7, 0},{  6, 2, 0},{ -6, 2, 0},{  3, 4, 0},{ -3, 4, 0},{  2, 5, 0},
307       { -2, 5, 0},{ 12, 1, 2},{-12, 1, 2},{  4, 3, 0},{ -4, 3, 0}
308     },
309     //level_add
310     { 0,13, 7, 5, 4, 3, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,
311      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
312     2, //golomb_order
313     6, //inc_limit
314     7  //max_run
315   },{
316     { //level / run
317       {   EOB   },{  1, 1, 0},{ -1, 1, 0},{  2, 1, 0},{ -2, 1, 0},{  3, 1, 0},
318       { -3, 1, 0},{  4, 1, 0},{ -4, 1, 0},{  5, 1, 0},{ -5, 1, 0},{  1, 2, 0},
319       { -1, 2, 0},{  6, 1, 0},{ -6, 1, 0},{  7, 1, 0},{ -7, 1, 0},{  8, 1, 0},
320       { -8, 1, 0},{  2, 2, 0},{ -2, 2, 0},{  9, 1, 0},{ -9, 1, 0},{  1, 3, 0},
321       { -1, 3, 0},{ 10, 1, 1},{-10, 1, 1},{  3, 2, 0},{ -3, 2, 0},{ 11, 1, 1},
322       {-11, 1, 1},{  4, 2, 0},{ -4, 2, 0},{ 12, 1, 1},{-12, 1, 1},{  1, 4, 0},
323       { -1, 4, 0},{  2, 3, 0},{ -2, 3, 0},{ 13, 1, 1},{-13, 1, 1},{  5, 2, 0},
324       { -5, 2, 0},{ 14, 1, 1},{-14, 1, 1},{  6, 2, 0},{ -6, 2, 0},{  1, 5, 0},
325       { -1, 5, 0},{ 15, 1, 1},{-15, 1, 1},{  3, 3, 0},{ -3, 3, 0},{ 16, 1, 1},
326       {-16, 1, 1},{  2, 4, 0},{ -2, 4, 0},{  7, 2, 0},{ -7, 2, 0}
327     },
328     //level_add
329     { 0,17, 8, 4, 3, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
330      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
331     2, //golomb_order
332     9, //inc_limit
333     5  //max_run
334   },{
335     { //level / run
336       {   EOB   },{  1, 1, 0},{ -1, 1, 0},{  2, 1, 0},{ -2, 1, 0},{  3, 1, 0},
337       { -3, 1, 0},{  4, 1, 0},{ -4, 1, 0},{  5, 1, 0},{ -5, 1, 0},{  6, 1, 0},
338       { -6, 1, 0},{  7, 1, 0},{ -7, 1, 0},{  1, 2, 0},{ -1, 2, 0},{  8, 1, 0},
339       { -8, 1, 0},{  9, 1, 0},{ -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 11, 1, 0},
340       {-11, 1, 0},{ 12, 1, 0},{-12, 1, 0},{  2, 2, 0},{ -2, 2, 0},{ 13, 1, 0},
341       {-13, 1, 0},{  1, 3, 0},{ -1, 3, 0},{ 14, 1, 0},{-14, 1, 0},{ 15, 1, 0},
342       {-15, 1, 0},{  3, 2, 0},{ -3, 2, 0},{ 16, 1, 0},{-16, 1, 0},{ 17, 1, 0},
343       {-17, 1, 0},{ 18, 1, 0},{-18, 1, 0},{  4, 2, 0},{ -4, 2, 0},{ 19, 1, 0},
344       {-19, 1, 0},{ 20, 1, 0},{-20, 1, 0},{  2, 3, 0},{ -2, 3, 0},{  1, 4, 0},
345       { -1, 4, 0},{  5, 2, 0},{ -5, 2, 0},{ 21, 1, 0},{-21, 1, 0}
346     },
347     //level_add
348     { 0,22, 6, 3, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
349      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
350     2, //golomb_order
351     INT_MAX, //inc_limit
352     4 //max_run
353   }
354 };
355
356 static const struct dec_2dvlc chroma_dec[5] = {
357   {
358     { //level / run
359       {  1, 1, 1},{ -1, 1, 1},{  1, 2, 1},{ -1, 2, 1},{  1, 3, 1},{ -1, 3, 1},
360       {  1, 4, 1},{ -1, 4, 1},{  1, 5, 1},{ -1, 5, 1},{  1, 6, 1},{ -1, 6, 1},
361       {  1, 7, 1},{ -1, 7, 1},{  2, 1, 2},{ -2, 1, 2},{  1, 8, 1},{ -1, 8, 1},
362       {  1, 9, 1},{ -1, 9, 1},{  1,10, 1},{ -1,10, 1},{  1,11, 1},{ -1,11, 1},
363       {  1,12, 1},{ -1,12, 1},{  1,13, 1},{ -1,13, 1},{  1,14, 1},{ -1,14, 1},
364       {  1,15, 1},{ -1,15, 1},{  3, 1, 3},{ -3, 1, 3},{  1,16, 1},{ -1,16, 1},
365       {  1,17, 1},{ -1,17, 1},{  1,18, 1},{ -1,18, 1},{  1,19, 1},{ -1,19, 1},
366       {  1,20, 1},{ -1,20, 1},{  1,21, 1},{ -1,21, 1},{  1,22, 1},{ -1,22, 1},
367       {  2, 2, 2},{ -2, 2, 2},{  1,23, 1},{ -1,23, 1},{  1,24, 1},{ -1,24, 1},
368       {  1,25, 1},{ -1,25, 1},{  4, 1, 3},{ -4, 1, 3},{   EOB   }
369     },
370     //level_add
371     { 0, 5, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
372       2, 2, 2, 2, 2, 2, 2, 2, 2,-1},
373     2, //golomb_order
374     0, //inc_limit
375     25 //max_run
376   },{
377     { //level / run
378       {   EOB   },{  1, 1, 0},{ -1, 1, 0},{  1, 2, 0},{ -1, 2, 0},{  2, 1, 1},
379       { -2, 1, 1},{  1, 3, 0},{ -1, 3, 0},{  1, 4, 0},{ -1, 4, 0},{  1, 5, 0},
380       { -1, 5, 0},{  1, 6, 0},{ -1, 6, 0},{  3, 1, 2},{ -3, 1, 2},{  1, 7, 0},
381       { -1, 7, 0},{  1, 8, 0},{ -1, 8, 0},{  2, 2, 1},{ -2, 2, 1},{  1, 9, 0},
382       { -1, 9, 0},{  1,10, 0},{ -1,10, 0},{  1,11, 0},{ -1,11, 0},{  4, 1, 2},
383       { -4, 1, 2},{  1,12, 0},{ -1,12, 0},{  1,13, 0},{ -1,13, 0},{  1,14, 0},
384       { -1,14, 0},{  2, 3, 1},{ -2, 3, 1},{  1,15, 0},{ -1,15, 0},{  2, 4, 1},
385       { -2, 4, 1},{  5, 1, 3},{ -5, 1, 3},{  3, 2, 2},{ -3, 2, 2},{  1,16, 0},
386       { -1,16, 0},{  1,17, 0},{ -1,17, 0},{  1,18, 0},{ -1,18, 0},{  2, 5, 1},
387       { -2, 5, 1},{  1,19, 0},{ -1,19, 0},{  1,20, 0},{ -1,20, 0}
388     },
389     //level_add
390     { 0, 6, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
391       2, 2, 2, 2,-1,-1,-1,-1,-1,-1},
392     0, //golomb_order
393     1, //inc_limit
394     20 //max_run
395   },{
396     { //level / run
397       {  1, 1, 0},{ -1, 1, 0},{   EOB   },{  2, 1, 0},{ -2, 1, 0},{  1, 2, 0},
398       { -1, 2, 0},{  3, 1, 1},{ -3, 1, 1},{  1, 3, 0},{ -1, 3, 0},{  4, 1, 1},
399       { -4, 1, 1},{  2, 2, 0},{ -2, 2, 0},{  1, 4, 0},{ -1, 4, 0},{  5, 1, 2},
400       { -5, 1, 2},{  1, 5, 0},{ -1, 5, 0},{  3, 2, 1},{ -3, 2, 1},{  2, 3, 0},
401       { -2, 3, 0},{  1, 6, 0},{ -1, 6, 0},{  6, 1, 2},{ -6, 1, 2},{  1, 7, 0},
402       { -1, 7, 0},{  2, 4, 0},{ -2, 4, 0},{  7, 1, 2},{ -7, 1, 2},{  1, 8, 0},
403       { -1, 8, 0},{  4, 2, 1},{ -4, 2, 1},{  1, 9, 0},{ -1, 9, 0},{  3, 3, 1},
404       { -3, 3, 1},{  2, 5, 0},{ -2, 5, 0},{  2, 6, 0},{ -2, 6, 0},{  8, 1, 2},
405       { -8, 1, 2},{  1,10, 0},{ -1,10, 0},{  1,11, 0},{ -1,11, 0},{  9, 1, 2},
406       { -9, 1, 2},{  5, 2, 2},{ -5, 2, 2},{  3, 4, 1},{ -3, 4, 1},
407     },
408     //level_add
409     { 0,10, 6, 4, 4, 3, 3, 2, 2, 2, 2, 2,-1,-1,-1,-1,-1,
410      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
411     1, //golomb_order
412     2, //inc_limit
413     11 //max_run
414   },{
415     { //level / run
416       {   EOB   },{  1, 1, 0},{ -1, 1, 0},{  2, 1, 0},{ -2, 1, 0},{  3, 1, 0},
417       { -3, 1, 0},{  4, 1, 0},{ -4, 1, 0},{  1, 2, 0},{ -1, 2, 0},{  5, 1, 1},
418       { -5, 1, 1},{  2, 2, 0},{ -2, 2, 0},{  6, 1, 1},{ -6, 1, 1},{  1, 3, 0},
419       { -1, 3, 0},{  7, 1, 1},{ -7, 1, 1},{  3, 2, 0},{ -3, 2, 0},{  8, 1, 1},
420       { -8, 1, 1},{  1, 4, 0},{ -1, 4, 0},{  2, 3, 0},{ -2, 3, 0},{  9, 1, 1},
421       { -9, 1, 1},{  4, 2, 0},{ -4, 2, 0},{  1, 5, 0},{ -1, 5, 0},{ 10, 1, 1},
422       {-10, 1, 1},{  3, 3, 0},{ -3, 3, 0},{  5, 2, 1},{ -5, 2, 1},{  2, 4, 0},
423       { -2, 4, 0},{ 11, 1, 1},{-11, 1, 1},{  1, 6, 0},{ -1, 6, 0},{ 12, 1, 1},
424       {-12, 1, 1},{  1, 7, 0},{ -1, 7, 0},{  6, 2, 1},{ -6, 2, 1},{ 13, 1, 1},
425       {-13, 1, 1},{  2, 5, 0},{ -2, 5, 0},{  1, 8, 0},{ -1, 8, 0},
426     },
427     //level_add
428     { 0,14, 7, 4, 3, 3, 2, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,
429      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
430     1, //golomb_order
431     4, //inc_limit
432     8  //max_run
433   },{
434     { //level / run
435       {   EOB   },{  1, 1, 0},{ -1, 1, 0},{  2, 1, 0},{ -2, 1, 0},{  3, 1, 0},
436       { -3, 1, 0},{  4, 1, 0},{ -4, 1, 0},{  5, 1, 0},{ -5, 1, 0},{  6, 1, 0},
437       { -6, 1, 0},{  7, 1, 0},{ -7, 1, 0},{  8, 1, 0},{ -8, 1, 0},{  1, 2, 0},
438       { -1, 2, 0},{  9, 1, 0},{ -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 11, 1, 0},
439       {-11, 1, 0},{  2, 2, 0},{ -2, 2, 0},{ 12, 1, 0},{-12, 1, 0},{ 13, 1, 0},
440       {-13, 1, 0},{  3, 2, 0},{ -3, 2, 0},{ 14, 1, 0},{-14, 1, 0},{  1, 3, 0},
441       { -1, 3, 0},{ 15, 1, 0},{-15, 1, 0},{  4, 2, 0},{ -4, 2, 0},{ 16, 1, 0},
442       {-16, 1, 0},{ 17, 1, 0},{-17, 1, 0},{  5, 2, 0},{ -5, 2, 0},{  1, 4, 0},
443       { -1, 4, 0},{  2, 3, 0},{ -2, 3, 0},{ 18, 1, 0},{-18, 1, 0},{  6, 2, 0},
444       { -6, 2, 0},{ 19, 1, 0},{-19, 1, 0},{  1, 5, 0},{ -1, 5, 0},
445     },
446     //level_add
447     { 0,20, 7, 3, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
448      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
449     0, //golomb_order
450     INT_MAX, //inc_limit
451     5, //max_run
452   }
453 };
454
455 #undef EOB
456
457 /*****************************************************************************
458  *
459  * motion vector prediction
460  *
461  ****************************************************************************/
462
463 static inline void store_mvs(AVSContext *h) {
464     h->col_mv[h->mbidx*4 + 0] = h->mv[MV_FWD_X0];
465     h->col_mv[h->mbidx*4 + 1] = h->mv[MV_FWD_X1];
466     h->col_mv[h->mbidx*4 + 2] = h->mv[MV_FWD_X2];
467     h->col_mv[h->mbidx*4 + 3] = h->mv[MV_FWD_X3];
468 }
469
470 static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
471                                   cavs_vector *col_mv) {
472     cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
473     int den = h->direct_den[col_mv->ref];
474     int m = col_mv->x >> 31;
475
476     pmv_fw->dist = h->dist[1];
477     pmv_bw->dist = h->dist[0];
478     pmv_fw->ref = 1;
479     pmv_bw->ref = 0;
480     /* scale the co-located motion vector according to its temporal span */
481     pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
482     pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
483     m = col_mv->y >> 31;
484     pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
485     pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
486 }
487
488 static inline void mv_pred_sym(AVSContext *h, cavs_vector *src, enum cavs_block size) {
489     cavs_vector *dst = src + MV_BWD_OFFS;
490
491     /* backward mv is the scaled and negated forward mv */
492     dst->x = -((src->x * h->sym_factor + 256) >> 9);
493     dst->y = -((src->y * h->sym_factor + 256) >> 9);
494     dst->ref = 0;
495     dst->dist = h->dist[0];
496     set_mvs(dst, size);
497 }
498
499 /*****************************************************************************
500  *
501  * residual data decoding
502  *
503  ****************************************************************************/
504
505 /** kth-order exponential golomb code */
506 static inline int get_ue_code(GetBitContext *gb, int order) {
507     if(order) {
508         int ret = get_ue_golomb(gb) << order;
509         return ret + get_bits(gb,order);
510     }
511     return get_ue_golomb(gb);
512 }
513
514 static inline int dequant(AVSContext *h, DCTELEM *level_buf, uint8_t *run_buf,
515                           DCTELEM *dst, int mul, int shift, int coeff_num) {
516     int round = 1 << (shift - 1);
517     int pos = -1;
518     const uint8_t *scantab = h->scantable.permutated;
519
520     /* inverse scan and dequantization */
521     while(--coeff_num >= 0){
522         pos += run_buf[coeff_num];
523         if(pos > 63) {
524             av_log(h->s.avctx, AV_LOG_ERROR,
525                 "position out of block bounds at pic %d MB(%d,%d)\n",
526                 h->picture.poc, h->mbx, h->mby);
527             return -1;
528         }
529         dst[scantab[pos]] = (level_buf[coeff_num]*mul + round) >> shift;
530     }
531     return 0;
532 }
533
534 /**
535  * decode coefficients from one 8x8 block, dequantize, inverse transform
536  *  and add them to sample block
537  * @param r pointer to 2D VLC table
538  * @param esc_golomb_order escape codes are k-golomb with this order k
539  * @param qp quantizer
540  * @param dst location of sample block
541  * @param stride line stride in frame buffer
542  */
543 static int decode_residual_block(AVSContext *h, GetBitContext *gb,
544                                  const struct dec_2dvlc *r, int esc_golomb_order,
545                                  int qp, uint8_t *dst, int stride) {
546     int i, level_code, esc_code, level, run, mask;
547     DCTELEM level_buf[65];
548     uint8_t run_buf[65];
549     DCTELEM *block = h->block;
550
551     for(i=0;i<65;i++) {
552         level_code = get_ue_code(gb,r->golomb_order);
553         if(level_code >= ESCAPE_CODE) {
554             run = ((level_code - ESCAPE_CODE) >> 1) + 1;
555             esc_code = get_ue_code(gb,esc_golomb_order);
556             level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
557             while(level > r->inc_limit)
558                 r++;
559             mask = -(level_code & 1);
560             level = (level^mask) - mask;
561         } else if (level_code >= 0) {
562             level = r->rltab[level_code][0];
563             if(!level) //end of block signal
564                 break;
565             run   = r->rltab[level_code][1];
566             r += r->rltab[level_code][2];
567         } else {
568             break;
569         }
570         level_buf[i] = level;
571         run_buf[i] = run;
572     }
573     if (dequant(h, level_buf, run_buf, block, dequant_mul[qp],
574                 dequant_shift[qp], i))
575         return -1;
576     h->cdsp.cavs_idct8_add(dst,block,stride);
577     h->s.dsp.clear_block(block);
578     return 0;
579 }
580
581
582 static inline void decode_residual_chroma(AVSContext *h) {
583     if(h->cbp & (1<<4))
584         decode_residual_block(h, &h->s.gb, chroma_dec, 0,
585                               cavs_chroma_qp[h->qp], h->cu, h->c_stride);
586     if(h->cbp & (1<<5))
587         decode_residual_block(h, &h->s.gb, chroma_dec, 0,
588                               cavs_chroma_qp[h->qp], h->cv, h->c_stride);
589 }
590
591 static inline int decode_residual_inter(AVSContext *h) {
592     int block;
593
594     /* get coded block pattern */
595     int cbp= get_ue_golomb(&h->s.gb);
596     if(cbp > 63){
597         av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
598         return -1;
599     }
600     h->cbp = cbp_tab[cbp][1];
601
602     /* get quantizer */
603     if(h->cbp && !h->qp_fixed)
604         h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
605     for(block=0;block<4;block++)
606         if(h->cbp & (1<<block))
607             decode_residual_block(h, &h->s.gb, inter_dec, 0, h->qp,
608                                   h->cy + h->luma_scan[block], h->l_stride);
609     decode_residual_chroma(h);
610
611     return 0;
612 }
613
614 /*****************************************************************************
615  *
616  * macroblock level
617  *
618  ****************************************************************************/
619
620 static inline void set_mv_intra(AVSContext *h) {
621     h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
622     set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
623     h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
624     set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
625     if(h->pic_type != AV_PICTURE_TYPE_B)
626         h->col_type_base[h->mbidx] = I_8X8;
627 }
628
629 static int decode_mb_i(AVSContext *h, int cbp_code) {
630     GetBitContext *gb = &h->s.gb;
631     unsigned pred_mode_uv;
632     int block;
633     uint8_t top[18];
634     uint8_t *left = NULL;
635     uint8_t *d;
636
637     ff_cavs_init_mb(h);
638
639     /* get intra prediction modes from stream */
640     for(block=0;block<4;block++) {
641         int nA,nB,predpred;
642         int pos = scan3x3[block];
643
644         nA = h->pred_mode_Y[pos-1];
645         nB = h->pred_mode_Y[pos-3];
646         predpred = FFMIN(nA,nB);
647         if(predpred == NOT_AVAIL) // if either is not available
648             predpred = INTRA_L_LP;
649         if(!get_bits1(gb)){
650             int rem_mode= get_bits(gb, 2);
651             predpred = rem_mode + (rem_mode >= predpred);
652         }
653         h->pred_mode_Y[pos] = predpred;
654     }
655     pred_mode_uv = get_ue_golomb(gb);
656     if(pred_mode_uv > 6) {
657         av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
658         return -1;
659     }
660     ff_cavs_modify_mb_i(h, &pred_mode_uv);
661
662     /* get coded block pattern */
663     if(h->pic_type == AV_PICTURE_TYPE_I)
664         cbp_code = get_ue_golomb(gb);
665     if(cbp_code > 63){
666         av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
667         return -1;
668     }
669     h->cbp = cbp_tab[cbp_code][0];
670     if(h->cbp && !h->qp_fixed)
671         h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
672
673     /* luma intra prediction interleaved with residual decode/transform/add */
674     for(block=0;block<4;block++) {
675         d = h->cy + h->luma_scan[block];
676         ff_cavs_load_intra_pred_luma(h, top, &left, block);
677         h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
678             (d, top, left, h->l_stride);
679         if(h->cbp & (1<<block))
680             decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
681     }
682
683     /* chroma intra prediction */
684     ff_cavs_load_intra_pred_chroma(h);
685     h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
686                                   h->left_border_u, h->c_stride);
687     h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
688                                   h->left_border_v, h->c_stride);
689
690     decode_residual_chroma(h);
691     ff_cavs_filter(h,I_8X8);
692     set_mv_intra(h);
693     return 0;
694 }
695
696 static inline void set_intra_mode_default(AVSContext *h) {
697     if(h->stream_revision > 0) {
698         h->pred_mode_Y[3] =  h->pred_mode_Y[6] = NOT_AVAIL;
699         h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = NOT_AVAIL;
700     } else {
701         h->pred_mode_Y[3] =  h->pred_mode_Y[6] = INTRA_L_LP;
702         h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
703     }
704 }
705
706 static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type) {
707     GetBitContext *gb = &h->s.gb;
708     int ref[4];
709
710     ff_cavs_init_mb(h);
711     switch(mb_type) {
712     case P_SKIP:
713         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP,  BLK_16X16, 0);
714         break;
715     case P_16X16:
716         ref[0] = h->ref_flag ? 0 : get_bits1(gb);
717         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
718         break;
719     case P_16X8:
720         ref[0] = h->ref_flag ? 0 : get_bits1(gb);
721         ref[2] = h->ref_flag ? 0 : get_bits1(gb);
722         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,    BLK_16X8, ref[0]);
723         ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT,   BLK_16X8, ref[2]);
724         break;
725     case P_8X16:
726         ref[0] = h->ref_flag ? 0 : get_bits1(gb);
727         ref[1] = h->ref_flag ? 0 : get_bits1(gb);
728         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT,   BLK_8X16, ref[0]);
729         ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, ref[1]);
730         break;
731     case P_8X8:
732         ref[0] = h->ref_flag ? 0 : get_bits1(gb);
733         ref[1] = h->ref_flag ? 0 : get_bits1(gb);
734         ref[2] = h->ref_flag ? 0 : get_bits1(gb);
735         ref[3] = h->ref_flag ? 0 : get_bits1(gb);
736         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN,   BLK_8X8, ref[0]);
737         ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN,   BLK_8X8, ref[1]);
738         ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN,   BLK_8X8, ref[2]);
739         ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN,   BLK_8X8, ref[3]);
740     }
741     ff_cavs_inter(h, mb_type);
742     set_intra_mode_default(h);
743     store_mvs(h);
744     if(mb_type != P_SKIP)
745         decode_residual_inter(h);
746     ff_cavs_filter(h,mb_type);
747     h->col_type_base[h->mbidx] = mb_type;
748 }
749
750 static void decode_mb_b(AVSContext *h, enum cavs_mb mb_type) {
751     int block;
752     enum cavs_sub_mb sub_type[4];
753     int flags;
754
755     ff_cavs_init_mb(h);
756
757     /* reset all MVs */
758     h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
759     set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
760     h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
761     set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
762     switch(mb_type) {
763     case B_SKIP:
764     case B_DIRECT:
765         if(!h->col_type_base[h->mbidx]) {
766             /* intra MB at co-location, do in-plane prediction */
767             ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
768             ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
769         } else
770             /* direct prediction from co-located P MB, block-wise */
771             for(block=0;block<4;block++)
772                 mv_pred_direct(h,&h->mv[mv_scan[block]],
773                                  &h->col_mv[h->mbidx*4 + block]);
774         break;
775     case B_FWD_16X16:
776         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
777         break;
778     case B_SYM_16X16:
779         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
780         mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
781         break;
782     case B_BWD_16X16:
783         ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
784         break;
785     case B_8X8:
786         for(block=0;block<4;block++)
787             sub_type[block] = get_bits(&h->s.gb,2);
788         for(block=0;block<4;block++) {
789             switch(sub_type[block]) {
790             case B_SUB_DIRECT:
791                 if(!h->col_type_base[h->mbidx]) {
792                     /* intra MB at co-location, do in-plane prediction */
793                     ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
794                             MV_PRED_BSKIP, BLK_8X8, 1);
795                     ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
796                             mv_scan[block]-3+MV_BWD_OFFS,
797                             MV_PRED_BSKIP, BLK_8X8, 0);
798                 } else
799                     mv_pred_direct(h,&h->mv[mv_scan[block]],
800                                    &h->col_mv[h->mbidx*4 + block]);
801                 break;
802             case B_SUB_FWD:
803                 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
804                         MV_PRED_MEDIAN, BLK_8X8, 1);
805                 break;
806             case B_SUB_SYM:
807                 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
808                         MV_PRED_MEDIAN, BLK_8X8, 1);
809                 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
810                 break;
811             }
812         }
813         for(block=0;block<4;block++) {
814             if(sub_type[block] == B_SUB_BWD)
815                 ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
816                         mv_scan[block]+MV_BWD_OFFS-3,
817                         MV_PRED_MEDIAN, BLK_8X8, 0);
818         }
819         break;
820     default:
821         assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
822         flags = ff_cavs_partition_flags[mb_type];
823         if(mb_type & 1) { /* 16x8 macroblock types */
824             if(flags & FWD0)
825                 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,  BLK_16X8, 1);
826             if(flags & SYM0)
827                 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
828             if(flags & FWD1)
829                 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
830             if(flags & SYM1)
831                 mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
832             if(flags & BWD0)
833                 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP,  BLK_16X8, 0);
834             if(flags & BWD1)
835                 ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
836         } else {          /* 8x16 macroblock types */
837             if(flags & FWD0)
838                 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
839             if(flags & SYM0)
840                 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
841             if(flags & FWD1)
842                 ff_cavs_mv(h,MV_FWD_X1,MV_FWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,1);
843             if(flags & SYM1)
844                 mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
845             if(flags & BWD0)
846                 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
847             if(flags & BWD1)
848                 ff_cavs_mv(h,MV_BWD_X1,MV_BWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,0);
849         }
850     }
851     ff_cavs_inter(h, mb_type);
852     set_intra_mode_default(h);
853     if(mb_type != B_SKIP)
854         decode_residual_inter(h);
855     ff_cavs_filter(h,mb_type);
856 }
857
858 /*****************************************************************************
859  *
860  * slice level
861  *
862  ****************************************************************************/
863
864 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
865     if(h->stc > 0xAF)
866         av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
867     h->mby = h->stc;
868     h->mbidx = h->mby*h->mb_width;
869
870     /* mark top macroblocks as unavailable */
871     h->flags &= ~(B_AVAIL|C_AVAIL);
872     if((h->mby == 0) && (!h->qp_fixed)){
873         h->qp_fixed = get_bits1(gb);
874         h->qp = get_bits(gb,6);
875     }
876     /* inter frame or second slice can have weighting params */
877     if((h->pic_type != AV_PICTURE_TYPE_I) || (!h->pic_structure && h->mby >= h->mb_width/2))
878         if(get_bits1(gb)) { //slice_weighting_flag
879             av_log(h->s.avctx, AV_LOG_ERROR,
880                    "weighted prediction not yet supported\n");
881         }
882     return 0;
883 }
884
885 static inline int check_for_slice(AVSContext *h) {
886     GetBitContext *gb = &h->s.gb;
887     int align;
888
889     if(h->mbx)
890         return 0;
891     align = (-get_bits_count(gb)) & 7;
892     /* check for stuffing byte */
893     if(!align && (show_bits(gb,8) == 0x80))
894         align = 8;
895     if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
896         skip_bits_long(gb,24+align);
897         h->stc = get_bits(gb,8);
898         if (h->stc >= h->mb_height)
899             return 0;
900         decode_slice_header(h,gb);
901         return 1;
902     }
903     return 0;
904 }
905
906 /*****************************************************************************
907  *
908  * frame level
909  *
910  ****************************************************************************/
911
912 static int decode_pic(AVSContext *h) {
913     MpegEncContext *s = &h->s;
914     int skip_count = -1;
915     enum cavs_mb mb_type;
916
917     if (!s->context_initialized) {
918         if (ff_MPV_common_init(s) < 0)
919             return -1;
920         ff_init_scantable_permutation(s->dsp.idct_permutation,
921                                       h->cdsp.idct_perm);
922         ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
923     }
924     skip_bits(&s->gb,16);//bbv_dwlay
925     if(h->stc == PIC_PB_START_CODE) {
926         h->pic_type = get_bits(&s->gb,2) + AV_PICTURE_TYPE_I;
927         if(h->pic_type > AV_PICTURE_TYPE_B) {
928             av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
929             return -1;
930         }
931         /* make sure we have the reference frames we need */
932         if(!h->DPB[0].f.data[0] ||
933           (!h->DPB[1].f.data[0] && h->pic_type == AV_PICTURE_TYPE_B))
934             return -1;
935     } else {
936         h->pic_type = AV_PICTURE_TYPE_I;
937         if(get_bits1(&s->gb))
938             skip_bits(&s->gb,24);//time_code
939         /* old sample clips were all progressive and no low_delay,
940            bump stream revision if detected otherwise */
941         if (s->low_delay || !(show_bits(&s->gb,9) & 1))
942             h->stream_revision = 1;
943         /* similarly test top_field_first and repeat_first_field */
944         else if(show_bits(&s->gb,11) & 3)
945             h->stream_revision = 1;
946         if(h->stream_revision > 0)
947             skip_bits(&s->gb,1); //marker_bit
948     }
949     /* release last B frame */
950     if(h->picture.f.data[0])
951         s->avctx->release_buffer(s->avctx, &h->picture.f);
952
953     s->avctx->get_buffer(s->avctx, &h->picture.f);
954     ff_cavs_init_pic(h);
955     h->picture.poc = get_bits(&s->gb,8)*2;
956
957     /* get temporal distances and MV scaling factors */
958     if(h->pic_type != AV_PICTURE_TYPE_B) {
959         h->dist[0] = (h->picture.poc - h->DPB[0].poc  + 512) % 512;
960     } else {
961         h->dist[0] = (h->DPB[0].poc  - h->picture.poc + 512) % 512;
962     }
963     h->dist[1] = (h->picture.poc - h->DPB[1].poc  + 512) % 512;
964     h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
965     h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
966     if(h->pic_type == AV_PICTURE_TYPE_B) {
967         h->sym_factor = h->dist[0]*h->scale_den[1];
968     } else {
969         h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
970         h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
971     }
972
973     if(s->low_delay)
974         get_ue_golomb(&s->gb); //bbv_check_times
975     h->progressive             = get_bits1(&s->gb);
976     h->pic_structure = 1;
977     if(!h->progressive)
978         h->pic_structure = get_bits1(&s->gb);
979     if(!h->pic_structure && h->stc == PIC_PB_START_CODE)
980         skip_bits1(&s->gb);     //advanced_pred_mode_disable
981     skip_bits1(&s->gb);        //top_field_first
982     skip_bits1(&s->gb);        //repeat_first_field
983     h->qp_fixed                = get_bits1(&s->gb);
984     h->qp                      = get_bits(&s->gb,6);
985     if(h->pic_type == AV_PICTURE_TYPE_I) {
986         if(!h->progressive && !h->pic_structure)
987             skip_bits1(&s->gb);//what is this?
988         skip_bits(&s->gb,4);   //reserved bits
989     } else {
990         if(!(h->pic_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
991             h->ref_flag        = get_bits1(&s->gb);
992         skip_bits(&s->gb,4);   //reserved bits
993         h->skip_mode_flag      = get_bits1(&s->gb);
994     }
995     h->loop_filter_disable     = get_bits1(&s->gb);
996     if(!h->loop_filter_disable && get_bits1(&s->gb)) {
997         h->alpha_offset        = get_se_golomb(&s->gb);
998         h->beta_offset         = get_se_golomb(&s->gb);
999     } else {
1000         h->alpha_offset = h->beta_offset  = 0;
1001     }
1002     if(h->pic_type == AV_PICTURE_TYPE_I) {
1003         do {
1004             check_for_slice(h);
1005             decode_mb_i(h, 0);
1006         } while(ff_cavs_next_mb(h));
1007     } else if(h->pic_type == AV_PICTURE_TYPE_P) {
1008         do {
1009             if(check_for_slice(h))
1010                 skip_count = -1;
1011             if(h->skip_mode_flag && (skip_count < 0))
1012                 skip_count = get_ue_golomb(&s->gb);
1013             if(h->skip_mode_flag && skip_count--) {
1014                 decode_mb_p(h,P_SKIP);
1015             } else {
1016                 mb_type = get_ue_golomb(&s->gb) + P_SKIP + h->skip_mode_flag;
1017                 if(mb_type > P_8X8)
1018                     decode_mb_i(h, mb_type - P_8X8 - 1);
1019                 else
1020                     decode_mb_p(h,mb_type);
1021             }
1022         } while(ff_cavs_next_mb(h));
1023     } else { /* AV_PICTURE_TYPE_B */
1024         do {
1025             if(check_for_slice(h))
1026                 skip_count = -1;
1027             if(h->skip_mode_flag && (skip_count < 0))
1028                 skip_count = get_ue_golomb(&s->gb);
1029             if(h->skip_mode_flag && skip_count--) {
1030                 decode_mb_b(h,B_SKIP);
1031             } else {
1032                 mb_type = get_ue_golomb(&s->gb) + B_SKIP + h->skip_mode_flag;
1033                 if(mb_type > B_8X8)
1034                     decode_mb_i(h, mb_type - B_8X8 - 1);
1035                 else
1036                     decode_mb_b(h,mb_type);
1037             }
1038         } while(ff_cavs_next_mb(h));
1039     }
1040     if(h->pic_type != AV_PICTURE_TYPE_B) {
1041         if(h->DPB[1].f.data[0])
1042             s->avctx->release_buffer(s->avctx, &h->DPB[1].f);
1043         h->DPB[1] = h->DPB[0];
1044         h->DPB[0] = h->picture;
1045         memset(&h->picture,0,sizeof(Picture));
1046     }
1047     return 0;
1048 }
1049
1050 /*****************************************************************************
1051  *
1052  * headers and interface
1053  *
1054  ****************************************************************************/
1055
1056 static int decode_seq_header(AVSContext *h) {
1057     MpegEncContext *s = &h->s;
1058     int frame_rate_code;
1059
1060     h->profile =         get_bits(&s->gb,8);
1061     h->level =           get_bits(&s->gb,8);
1062     skip_bits1(&s->gb); //progressive sequence
1063     s->width =           get_bits(&s->gb,14);
1064     s->height =          get_bits(&s->gb,14);
1065     skip_bits(&s->gb,2); //chroma format
1066     skip_bits(&s->gb,3); //sample_precision
1067     h->aspect_ratio =    get_bits(&s->gb,4);
1068     frame_rate_code =    get_bits(&s->gb,4);
1069     skip_bits(&s->gb,18);//bit_rate_lower
1070     skip_bits1(&s->gb);  //marker_bit
1071     skip_bits(&s->gb,12);//bit_rate_upper
1072     s->low_delay =       get_bits1(&s->gb);
1073     h->mb_width  = (s->width  + 15) >> 4;
1074     h->mb_height = (s->height + 15) >> 4;
1075     h->s.avctx->time_base.den = avpriv_frame_rate_tab[frame_rate_code].num;
1076     h->s.avctx->time_base.num = avpriv_frame_rate_tab[frame_rate_code].den;
1077     h->s.avctx->width  = s->width;
1078     h->s.avctx->height = s->height;
1079     if(!h->top_qp)
1080         ff_cavs_init_top_lines(h);
1081     return 0;
1082 }
1083
1084 static void cavs_flush(AVCodecContext * avctx) {
1085     AVSContext *h = avctx->priv_data;
1086     h->got_keyframe = 0;
1087 }
1088
1089 static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
1090                              AVPacket *avpkt) {
1091     const uint8_t *buf = avpkt->data;
1092     int buf_size = avpkt->size;
1093     AVSContext *h = avctx->priv_data;
1094     MpegEncContext *s = &h->s;
1095     int input_size;
1096     const uint8_t *buf_end;
1097     const uint8_t *buf_ptr;
1098     AVFrame *picture = data;
1099     uint32_t stc = -1;
1100
1101     s->avctx = avctx;
1102
1103     if (buf_size == 0) {
1104         if (!s->low_delay && h->DPB[0].f.data[0]) {
1105             *data_size = sizeof(AVPicture);
1106             *picture = h->DPB[0].f;
1107             memset(&h->DPB[0], 0, sizeof(h->DPB[0]));
1108         }
1109         return 0;
1110     }
1111
1112     buf_ptr = buf;
1113     buf_end = buf + buf_size;
1114     for(;;) {
1115         buf_ptr = avpriv_mpv_find_start_code(buf_ptr,buf_end, &stc);
1116         if((stc & 0xFFFFFE00) || buf_ptr == buf_end)
1117             return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
1118         input_size = (buf_end - buf_ptr)*8;
1119         switch(stc) {
1120         case CAVS_START_CODE:
1121             init_get_bits(&s->gb, buf_ptr, input_size);
1122             decode_seq_header(h);
1123             break;
1124         case PIC_I_START_CODE:
1125             if(!h->got_keyframe) {
1126                 if(h->DPB[0].f.data[0])
1127                     avctx->release_buffer(avctx, &h->DPB[0].f);
1128                 if(h->DPB[1].f.data[0])
1129                     avctx->release_buffer(avctx, &h->DPB[1].f);
1130                 h->got_keyframe = 1;
1131             }
1132         case PIC_PB_START_CODE:
1133             *data_size = 0;
1134             if(!h->got_keyframe)
1135                 break;
1136             init_get_bits(&s->gb, buf_ptr, input_size);
1137             h->stc = stc;
1138             if(decode_pic(h))
1139                 break;
1140             *data_size = sizeof(AVPicture);
1141             if(h->pic_type != AV_PICTURE_TYPE_B) {
1142                 if(h->DPB[1].f.data[0]) {
1143                     *picture = h->DPB[1].f;
1144                 } else {
1145                     *data_size = 0;
1146                 }
1147             } else
1148                 *picture = h->picture.f;
1149             break;
1150         case EXT_START_CODE:
1151             //mpeg_decode_extension(avctx,buf_ptr, input_size);
1152             break;
1153         case USER_START_CODE:
1154             //mpeg_decode_user_data(avctx,buf_ptr, input_size);
1155             break;
1156         default:
1157             if (stc <= SLICE_MAX_START_CODE) {
1158                 init_get_bits(&s->gb, buf_ptr, input_size);
1159                 decode_slice_header(h, &s->gb);
1160             }
1161             break;
1162         }
1163     }
1164 }
1165
1166 AVCodec ff_cavs_decoder = {
1167     .name           = "cavs",
1168     .type           = AVMEDIA_TYPE_VIDEO,
1169     .id             = AV_CODEC_ID_CAVS,
1170     .priv_data_size = sizeof(AVSContext),
1171     .init           = ff_cavs_init,
1172     .close          = ff_cavs_end,
1173     .decode         = cavs_decode_frame,
1174     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1175     .flush          = cavs_flush,
1176     .long_name      = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
1177 };