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