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