]> git.sesse.net Git - ffmpeg/blob - libavcodec/cavsdec.c
xsubdec: Convert to the new bitstream reader
[ffmpeg] / libavcodec / cavsdec.c
1 /*
2  * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
3  * Copyright (c) 2006  Stefan Gehrer <stefan.gehrer@gmx.de>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
25  * @author Stefan Gehrer <stefan.gehrer@gmx.de>
26  */
27
28 #include "avcodec.h"
29 #include "get_bits.h"
30 #include "golomb.h"
31 #include "cavs.h"
32 #include "internal.h"
33 #include "mpeg12data.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, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1 },
100         2, //golomb_order
101         0, //inc_limit
102         23, //max_run
103     },
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
119         2, //golomb_order
120         1, //inc_limit
121         15, //max_run
122     },
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
138         2, //golomb_order
139         2, //inc_limit
140         10, //max_run
141     },
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
157         2, //golomb_order
158         4, //inc_limit
159         7, //max_run
160     },
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
176         2, //golomb_order
177         7, //inc_limit
178         5, //max_run
179     },
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
195         2, //golomb_order
196         10, //inc_limit
197         3, //max_run
198     },
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, -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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
236         3, //golomb_order
237         0, //inc_limit
238         26 //max_run
239     },
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, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1 },
255         2, //golomb_order
256         1, //inc_limit
257         19 //max_run
258     },
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
274         2, //golomb_order
275         2, //inc_limit
276         14 //max_run
277     },
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
293         2, //golomb_order
294         3, //inc_limit
295         10 //max_run
296     },
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
312         2, //golomb_order
313         6, //inc_limit
314         7  //max_run
315     },
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
331         2, //golomb_order
332         9, //inc_limit
333         5  //max_run
334     },
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
350         2, //golomb_order
351         INT_MAX, //inc_limit
352         4 //max_run
353     }
354 };
355
356 static const struct dec_2dvlc chroma_dec[5] = {
357     {
358         { //level / run
359             {  1,  1,  1 }, { -1,  1,  1 }, {  1,  2,  1 }, { -1,  2,  1 }, {  1,  3,  1 }, { -1,  3,  1 },
360             {  1,  4,  1 }, { -1,  4,  1 }, {  1,  5,  1 }, { -1,  5,  1 }, {  1,  6,  1 }, { -1,  6,  1 },
361             {  1,  7,  1 }, { -1,  7,  1 }, {  2,  1,  2 }, { -2,  1,  2 }, {  1,  8,  1 }, { -1,  8,  1 },
362             {  1,  9,  1 }, { -1,  9,  1 }, {  1, 10,  1 }, { -1, 10,  1 }, {  1, 11,  1 }, { -1, 11,  1 },
363             {  1, 12,  1 }, { -1, 12,  1 }, {  1, 13,  1 }, { -1, 13,  1 }, {  1, 14,  1 }, { -1, 14,  1 },
364             {  1, 15,  1 }, { -1, 15,  1 }, {  3,  1,  3 }, { -3,  1,  3 }, {  1, 16,  1 }, { -1, 16,  1 },
365             {  1, 17,  1 }, { -1, 17,  1 }, {  1, 18,  1 }, { -1, 18,  1 }, {  1, 19,  1 }, { -1, 19,  1 },
366             {  1, 20,  1 }, { -1, 20,  1 }, {  1, 21,  1 }, { -1, 21,  1 }, {  1, 22,  1 }, { -1, 22,  1 },
367             {  2,  2,  2 }, { -2,  2,  2 }, {  1, 23,  1 }, { -1, 23,  1 }, {  1, 24,  1 }, { -1, 24,  1 },
368             {  1, 25,  1 }, { -1, 25,  1 }, {  4,  1,  3 }, { -4,  1,  3 }, {   EOB    }
369         },
370         //level_add
371         { 0, 5, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
372         2, //golomb_order
373         0, //inc_limit
374         25 //max_run
375     },
376     {
377         { //level / run
378             {     EOB    }, {  1,  1,  0 }, { -1,  1,  0 }, {  1,  2,  0 }, { -1,  2,  0 }, {  2,  1,  1 },
379             { -2,  1,  1 }, {  1,  3,  0 }, { -1,  3,  0 }, {  1,  4,  0 }, { -1,  4,  0 }, {  1,  5,  0 },
380             { -1,  5,  0 }, {  1,  6,  0 }, { -1,  6,  0 }, {  3,  1,  2 }, { -3,  1,  2 }, {  1,  7,  0 },
381             { -1,  7,  0 }, {  1,  8,  0 }, { -1,  8,  0 }, {  2,  2,  1 }, { -2,  2,  1 }, {  1,  9,  0 },
382             { -1,  9,  0 }, {  1, 10,  0 }, { -1, 10,  0 }, {  1, 11,  0 }, { -1, 11,  0 }, {  4,  1,  2 },
383             { -4,  1,  2 }, {  1, 12,  0 }, { -1, 12,  0 }, {  1, 13,  0 }, { -1, 13,  0 }, {  1, 14,  0 },
384             { -1, 14,  0 }, {  2,  3,  1 }, { -2,  3,  1 }, {  1, 15,  0 }, { -1, 15,  0 }, {  2,  4,  1 },
385             { -2,  4,  1 }, {  5,  1,  3 }, { -5,  1,  3 }, {  3,  2,  2 }, { -3,  2,  2 }, {  1, 16,  0 },
386             { -1, 16,  0 }, {  1, 17,  0 }, { -1, 17,  0 }, {  1, 18,  0 }, { -1, 18,  0 }, {  2,  5,  1 },
387             { -2,  5,  1 }, {  1, 19,  0 }, { -1, 19,  0 }, {  1, 20,  0 }, { -1, 20,  0 }
388         },
389         //level_add
390         { 0, 6, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1 },
391         0, //golomb_order
392         1, //inc_limit
393         20 //max_run
394     },
395     {
396         { //level / run
397             {  1,  1,  0 }, { -1,  1,  0 }, {     EOB    }, {  2,  1,  0 }, { -2,  1,  0 }, {  1,  2,  0 },
398             { -1,  2,  0 }, {  3,  1,  1 }, { -3,  1,  1 }, {  1,  3,  0 }, { -1,  3,  0 }, {  4,  1,  1 },
399             { -4,  1,  1 }, {  2,  2,  0 }, { -2,  2,  0 }, {  1,  4,  0 }, { -1,  4,  0 }, {  5,  1,  2 },
400             { -5,  1,  2 }, {  1,  5,  0 }, { -1,  5,  0 }, {  3,  2,  1 }, { -3,  2,  1 }, {  2,  3,  0 },
401             { -2,  3,  0 }, {  1,  6,  0 }, { -1,  6,  0 }, {  6,  1,  2 }, { -6,  1,  2 }, {  1,  7,  0 },
402             { -1,  7,  0 }, {  2,  4,  0 }, { -2,  4,  0 }, {  7,  1,  2 }, { -7,  1,  2 }, {  1,  8,  0 },
403             { -1,  8,  0 }, {  4,  2,  1 }, { -4,  2,  1 }, {  1,  9,  0 }, { -1,  9,  0 }, {  3,  3,  1 },
404             { -3,  3,  1 }, {  2,  5,  0 }, { -2,  5,  0 }, {  2,  6,  0 }, { -2,  6,  0 }, {  8,  1,  2 },
405             { -8,  1,  2 }, {  1, 10,  0 }, { -1, 10,  0 }, {  1, 11,  0 }, { -1, 11,  0 }, {  9,  1,  2 },
406             { -9,  1,  2 }, {  5,  2,  2 }, { -5,  2,  2 }, {  3,  4,  1 }, { -3,  4,  1 },
407         },
408         //level_add
409         { 0,10, 6, 4, 4, 3, 3, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
410         1, //golomb_order
411         2, //inc_limit
412         11 //max_run
413     },
414     {
415         { //level / run
416             {     EOB    }, {  1,  1,  0 }, { -1,  1,  0 }, {  2,  1,  0 }, { -2,  1,  0 }, {  3,  1,  0 },
417             { -3,  1,  0 }, {  4,  1,  0 }, { -4,  1,  0 }, {  1,  2,  0 }, { -1,  2,  0 }, {  5,  1,  1 },
418             { -5,  1,  1 }, {  2,  2,  0 }, { -2,  2,  0 }, {  6,  1,  1 }, { -6,  1,  1 }, {  1,  3,  0 },
419             { -1,  3,  0 }, {  7,  1,  1 }, { -7,  1,  1 }, {  3,  2,  0 }, { -3,  2,  0 }, {  8,  1,  1 },
420             { -8,  1,  1 }, {  1,  4,  0 }, { -1,  4,  0 }, {  2,  3,  0 }, { -2,  3,  0 }, {  9,  1,  1 },
421             { -9,  1,  1 }, {  4,  2,  0 }, { -4,  2,  0 }, {  1,  5,  0 }, { -1,  5,  0 }, { 10,  1,  1 },
422             {-10,  1,  1 }, {  3,  3,  0 }, { -3,  3,  0 }, {  5,  2,  1 }, { -5,  2,  1 }, {  2,  4,  0 },
423             { -2,  4,  0 }, { 11,  1,  1 }, {-11,  1,  1 }, {  1,  6,  0 }, { -1,  6,  0 }, { 12,  1,  1 },
424             {-12,  1,  1 }, {  1,  7,  0 }, { -1,  7,  0 }, {  6,  2,  1 }, { -6,  2,  1 }, { 13,  1,  1 },
425             {-13,  1,  1 }, {  2,  5,  0 }, { -2,  5,  0 }, {  1,  8,  0 }, { -1,  8,  0 },
426         },
427         //level_add
428         { 0, 14, 7, 4, 3, 3, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
429         1, //golomb_order
430         4, //inc_limit
431         8  //max_run
432     },
433     {
434         { //level / run
435             {      EOB    }, {  1,  1,  0 }, {  -1,  1,  0 }, {  2,  1,  0 }, {  -2,  1,  0 }, {  3,  1,  0 },
436             {  -3,  1,  0 }, {  4,  1,  0 }, {  -4,  1,  0 }, {  5,  1,  0 }, {  -5,  1,  0 }, {  6,  1,  0 },
437             {  -6,  1,  0 }, {  7,  1,  0 }, {  -7,  1,  0 }, {  8,  1,  0 }, {  -8,  1,  0 }, {  1,  2,  0 },
438             {  -1,  2,  0 }, {  9,  1,  0 }, {  -9,  1,  0 }, { 10,  1,  0 }, { -10,  1,  0 }, { 11,  1,  0 },
439             { -11,  1,  0 }, {  2,  2,  0 }, {  -2,  2,  0 }, { 12,  1,  0 }, { -12,  1,  0 }, { 13,  1,  0 },
440             { -13,  1,  0 }, {  3,  2,  0 }, {  -3,  2,  0 }, { 14,  1,  0 }, { -14,  1,  0 }, {  1,  3,  0 },
441             {  -1,  3,  0 }, { 15,  1,  0 }, { -15,  1,  0 }, {  4,  2,  0 }, {  -4,  2,  0 }, { 16,  1,  0 },
442             { -16,  1,  0 }, { 17,  1,  0 }, { -17,  1,  0 }, {  5,  2,  0 }, {  -5,  2,  0 }, {  1,  4,  0 },
443             {  -1,  4,  0 }, {  2,  3,  0 }, {  -2,  3,  0 }, { 18,  1,  0 }, { -18,  1,  0 }, {  6,  2,  0 },
444             {  -6,  2,  0 }, { 19,  1,  0 }, { -19,  1,  0 }, {  1,  5,  0 }, {  -1,  5,  0 },
445         },
446         //level_add
447         { 0, 20, 7, 3, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
448         0, //golomb_order
449         INT_MAX, //inc_limit
450         5, //max_run
451     }
452 };
453
454 #undef EOB
455
456 /*****************************************************************************
457  *
458  * motion vector prediction
459  *
460  ****************************************************************************/
461
462 static inline void store_mvs(AVSContext *h)
463 {
464     h->col_mv[h->mbidx * 4 + 0] = h->mv[MV_FWD_X0];
465     h->col_mv[h->mbidx * 4 + 1] = h->mv[MV_FWD_X1];
466     h->col_mv[h->mbidx * 4 + 2] = h->mv[MV_FWD_X2];
467     h->col_mv[h->mbidx * 4 + 3] = h->mv[MV_FWD_X3];
468 }
469
470 static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
471                                   cavs_vector *col_mv)
472 {
473     cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
474     int den = h->direct_den[col_mv->ref];
475     int m = FF_SIGNBIT(col_mv->x);
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 = FF_SIGNBIT(col_mv->y);
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,
490                                enum cavs_block size)
491 {
492     cavs_vector *dst = src + MV_BWD_OFFS;
493
494     /* backward mv is the scaled and negated forward mv */
495     dst->x = -((src->x * h->sym_factor + 256) >> 9);
496     dst->y = -((src->y * h->sym_factor + 256) >> 9);
497     dst->ref = 0;
498     dst->dist = h->dist[0];
499     set_mvs(dst, size);
500 }
501
502 /*****************************************************************************
503  *
504  * residual data decoding
505  *
506  ****************************************************************************/
507
508 /** kth-order exponential golomb code */
509 static inline int get_ue_code(GetBitContext *gb, int order)
510 {
511     if (order) {
512         int ret = get_ue_golomb(gb) << order;
513         return ret + get_bits(gb, order);
514     }
515     return get_ue_golomb(gb);
516 }
517
518 static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf,
519                           int16_t *dst, int mul, int shift, int coeff_num)
520 {
521     int round = 1 << (shift - 1);
522     int pos = -1;
523     const uint8_t *scantab = h->scantable.permutated;
524
525     /* inverse scan and dequantization */
526     while (--coeff_num >= 0) {
527         pos += run_buf[coeff_num];
528         if (pos > 63) {
529             av_log(h->avctx, AV_LOG_ERROR,
530                    "position out of block bounds at pic %d MB(%d,%d)\n",
531                    h->cur.poc, h->mbx, h->mby);
532             return AVERROR_INVALIDDATA;
533         }
534         dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
535     }
536     return 0;
537 }
538
539 /**
540  * decode coefficients from one 8x8 block, dequantize, inverse transform
541  *  and add them to sample block
542  * @param r pointer to 2D VLC table
543  * @param esc_golomb_order escape codes are k-golomb with this order k
544  * @param qp quantizer
545  * @param dst location of sample block
546  * @param stride line stride in frame buffer
547  */
548 static int decode_residual_block(AVSContext *h, GetBitContext *gb,
549                                  const struct dec_2dvlc *r, int esc_golomb_order,
550                                  int qp, uint8_t *dst, ptrdiff_t stride)
551 {
552     int i, level_code, esc_code, level, run, mask, ret;
553     int16_t level_buf[65];
554     uint8_t run_buf[65];
555     int16_t *block = h->block;
556
557     for (i = 0;i < 65; i++) {
558         level_code = get_ue_code(gb, r->golomb_order);
559         if (level_code >= ESCAPE_CODE) {
560             run      = ((level_code - ESCAPE_CODE) >> 1) + 1;
561             esc_code = get_ue_code(gb, esc_golomb_order);
562             level    = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
563             while (level > r->inc_limit)
564                 r++;
565             mask  = -(level_code & 1);
566             level = (level ^ mask) - mask;
567         } else if (level_code >= 0) {
568             level = r->rltab[level_code][0];
569             if (!level) //end of block signal
570                 break;
571             run = r->rltab[level_code][1];
572             r  += r->rltab[level_code][2];
573         } else {
574             break;
575         }
576         level_buf[i] = level;
577         run_buf[i]   = run;
578     }
579     if ((ret = dequant(h, level_buf, run_buf, block, dequant_mul[qp],
580                       dequant_shift[qp], i)) < 0)
581         return ret;
582     h->cdsp.cavs_idct8_add(dst, block, stride);
583     h->bdsp.clear_block(block);
584     return 0;
585 }
586
587
588 static inline void decode_residual_chroma(AVSContext *h)
589 {
590     if (h->cbp & (1 << 4))
591         decode_residual_block(h, &h->gb, chroma_dec, 0,
592                               cavs_chroma_qp[h->qp], h->cu, h->c_stride);
593     if (h->cbp & (1 << 5))
594         decode_residual_block(h, &h->gb, chroma_dec, 0,
595                               cavs_chroma_qp[h->qp], h->cv, h->c_stride);
596 }
597
598 static inline int decode_residual_inter(AVSContext *h)
599 {
600     int block;
601
602     /* get coded block pattern */
603     int cbp = get_ue_golomb(&h->gb);
604     if (cbp > 63 || cbp < 0) {
605         av_log(h->avctx, AV_LOG_ERROR, "illegal inter cbp %d\n", cbp);
606         return AVERROR_INVALIDDATA;
607     }
608     h->cbp = cbp_tab[cbp][1];
609
610     /* get quantizer */
611     if (h->cbp && !h->qp_fixed)
612         h->qp = (h->qp + get_se_golomb(&h->gb)) & 63;
613     for (block = 0; block < 4; block++)
614         if (h->cbp & (1 << block))
615             decode_residual_block(h, &h->gb, inter_dec, 0, h->qp,
616                                   h->cy + h->luma_scan[block], h->l_stride);
617     decode_residual_chroma(h);
618
619     return 0;
620 }
621
622 /*****************************************************************************
623  *
624  * macroblock level
625  *
626  ****************************************************************************/
627
628 static inline void set_mv_intra(AVSContext *h)
629 {
630     h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
631     set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
632     h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
633     set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
634     if (h->cur.f->pict_type != AV_PICTURE_TYPE_B)
635         h->col_type_base[h->mbidx] = I_8X8;
636 }
637
638 static int decode_mb_i(AVSContext *h, int cbp_code)
639 {
640     GetBitContext *gb = &h->gb;
641     unsigned pred_mode_uv;
642     int block;
643     uint8_t top[18];
644     uint8_t *left = NULL;
645     uint8_t *d;
646
647     ff_cavs_init_mb(h);
648
649     /* get intra prediction modes from stream */
650     for (block = 0; block < 4; block++) {
651         int nA, nB, predpred;
652         int pos = scan3x3[block];
653
654         nA = h->pred_mode_Y[pos - 1];
655         nB = h->pred_mode_Y[pos - 3];
656         predpred = FFMIN(nA, nB);
657         if (predpred == NOT_AVAIL) // if either is not available
658             predpred = INTRA_L_LP;
659         if (!get_bits1(gb)) {
660             int rem_mode = get_bits(gb, 2);
661             predpred     = rem_mode + (rem_mode >= predpred);
662         }
663         h->pred_mode_Y[pos] = predpred;
664     }
665     pred_mode_uv = get_ue_golomb(gb);
666     if (pred_mode_uv > 6) {
667         av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
668         return AVERROR_INVALIDDATA;
669     }
670     ff_cavs_modify_mb_i(h, &pred_mode_uv);
671
672     /* get coded block pattern */
673     if (h->cur.f->pict_type == AV_PICTURE_TYPE_I)
674         cbp_code = get_ue_golomb(gb);
675     if (cbp_code > 63 || cbp_code < 0) {
676         av_log(h->avctx, AV_LOG_ERROR, "illegal intra cbp\n");
677         return AVERROR_INVALIDDATA;
678     }
679     h->cbp = cbp_tab[cbp_code][0];
680     if (h->cbp && !h->qp_fixed)
681         h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
682
683     /* luma intra prediction interleaved with residual decode/transform/add */
684     for (block = 0; block < 4; block++) {
685         d = h->cy + h->luma_scan[block];
686         ff_cavs_load_intra_pred_luma(h, top, &left, block);
687         h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
688             (d, top, left, h->l_stride);
689         if (h->cbp & (1<<block))
690             decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
691     }
692
693     /* chroma intra prediction */
694     ff_cavs_load_intra_pred_chroma(h);
695     h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx * 10],
696                                   h->left_border_u, h->c_stride);
697     h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx * 10],
698                                   h->left_border_v, h->c_stride);
699
700     decode_residual_chroma(h);
701     ff_cavs_filter(h, I_8X8);
702     set_mv_intra(h);
703     return 0;
704 }
705
706 static inline void set_intra_mode_default(AVSContext *h)
707 {
708     if (h->stream_revision > 0) {
709         h->pred_mode_Y[3] =  h->pred_mode_Y[6] = NOT_AVAIL;
710         h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = NOT_AVAIL;
711     } else {
712         h->pred_mode_Y[3] =  h->pred_mode_Y[6] = INTRA_L_LP;
713         h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = INTRA_L_LP;
714     }
715 }
716
717 static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
718 {
719     GetBitContext *gb = &h->gb;
720     int ref[4];
721
722     ff_cavs_init_mb(h);
723     switch (mb_type) {
724     case P_SKIP:
725         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP,  BLK_16X16, 0);
726         break;
727     case P_16X16:
728         ref[0] = h->ref_flag ? 0 : get_bits1(gb);
729         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, ref[0]);
730         break;
731     case P_16X8:
732         ref[0] = h->ref_flag ? 0 : get_bits1(gb);
733         ref[2] = h->ref_flag ? 0 : get_bits1(gb);
734         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,    BLK_16X8, ref[0]);
735         ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT,   BLK_16X8, ref[2]);
736         break;
737     case P_8X16:
738         ref[0] = h->ref_flag ? 0 : get_bits1(gb);
739         ref[1] = h->ref_flag ? 0 : get_bits1(gb);
740         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT,     BLK_8X16, ref[0]);
741         ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
742         break;
743     case P_8X8:
744         ref[0] = h->ref_flag ? 0 : get_bits1(gb);
745         ref[1] = h->ref_flag ? 0 : get_bits1(gb);
746         ref[2] = h->ref_flag ? 0 : get_bits1(gb);
747         ref[3] = h->ref_flag ? 0 : get_bits1(gb);
748         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN,   BLK_8X8, ref[0]);
749         ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN,   BLK_8X8, ref[1]);
750         ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN,   BLK_8X8, ref[2]);
751         ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN,   BLK_8X8, ref[3]);
752     }
753     ff_cavs_inter(h, mb_type);
754     set_intra_mode_default(h);
755     store_mvs(h);
756     if (mb_type != P_SKIP)
757         decode_residual_inter(h);
758     ff_cavs_filter(h, mb_type);
759     h->col_type_base[h->mbidx] = mb_type;
760 }
761
762 static void decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
763 {
764     int block;
765     enum cavs_sub_mb sub_type[4];
766     int flags;
767
768     ff_cavs_init_mb(h);
769
770     /* reset all MVs */
771     h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
772     set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
773     h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
774     set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
775     switch (mb_type) {
776     case B_SKIP:
777     case B_DIRECT:
778         if (!h->col_type_base[h->mbidx]) {
779             /* intra MB at co-location, do in-plane prediction */
780             ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
781             ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
782         } else
783             /* direct prediction from co-located P MB, block-wise */
784             for (block = 0; block < 4; block++)
785                 mv_pred_direct(h, &h->mv[mv_scan[block]],
786                                &h->col_mv[h->mbidx * 4 + block]);
787         break;
788     case B_FWD_16X16:
789         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
790         break;
791     case B_SYM_16X16:
792         ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
793         mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
794         break;
795     case B_BWD_16X16:
796         ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
797         break;
798     case B_8X8:
799         for (block = 0; block < 4; block++)
800             sub_type[block] = get_bits(&h->gb, 2);
801         for (block = 0; block < 4; block++) {
802             switch (sub_type[block]) {
803             case B_SUB_DIRECT:
804                 if (!h->col_type_base[h->mbidx]) {
805                     /* intra MB at co-location, do in-plane prediction */
806                     ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
807                                MV_PRED_BSKIP, BLK_8X8, 1);
808                     ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
809                                mv_scan[block] - 3 + MV_BWD_OFFS,
810                                MV_PRED_BSKIP, BLK_8X8, 0);
811                 } else
812                     mv_pred_direct(h, &h->mv[mv_scan[block]],
813                                    &h->col_mv[h->mbidx * 4 + block]);
814                 break;
815             case B_SUB_FWD:
816                 ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
817                            MV_PRED_MEDIAN, BLK_8X8, 1);
818                 break;
819             case B_SUB_SYM:
820                 ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
821                            MV_PRED_MEDIAN, BLK_8X8, 1);
822                 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
823                 break;
824             }
825         }
826         for (block = 0; block < 4; block++) {
827             if (sub_type[block] == B_SUB_BWD)
828                 ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
829                            mv_scan[block] + MV_BWD_OFFS - 3,
830                            MV_PRED_MEDIAN, BLK_8X8, 0);
831         }
832         break;
833     default:
834         assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
835         flags = ff_cavs_partition_flags[mb_type];
836         if (mb_type & 1) { /* 16x8 macroblock types */
837             if (flags & FWD0)
838                 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,  BLK_16X8, 1);
839             if (flags & SYM0)
840                 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
841             if (flags & FWD1)
842                 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
843             if (flags & SYM1)
844                 mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
845             if (flags & BWD0)
846                 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP,  BLK_16X8, 0);
847             if (flags & BWD1)
848                 ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
849         } else {          /* 8x16 macroblock types */
850             if (flags & FWD0)
851                 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
852             if (flags & SYM0)
853                 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
854             if (flags & FWD1)
855                 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 1);
856             if (flags & SYM1)
857                 mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
858             if (flags & BWD0)
859                 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
860             if (flags & BWD1)
861                 ff_cavs_mv(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 0);
862         }
863     }
864     ff_cavs_inter(h, mb_type);
865     set_intra_mode_default(h);
866     if (mb_type != B_SKIP)
867         decode_residual_inter(h);
868     ff_cavs_filter(h, mb_type);
869 }
870
871 /*****************************************************************************
872  *
873  * slice level
874  *
875  ****************************************************************************/
876
877 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb)
878 {
879     if (h->stc > 0xAF)
880         av_log(h->avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
881     h->mby   = h->stc;
882     h->mbidx = h->mby * h->mb_width;
883
884     /* mark top macroblocks as unavailable */
885     h->flags &= ~(B_AVAIL | C_AVAIL);
886     if ((h->mby == 0) && (!h->qp_fixed)) {
887         h->qp_fixed = get_bits1(gb);
888         h->qp       = get_bits(gb, 6);
889     }
890     /* inter frame or second slice can have weighting params */
891     if ((h->cur.f->pict_type != AV_PICTURE_TYPE_I) ||
892         (!h->pic_structure && h->mby >= h->mb_width / 2))
893         if (get_bits1(gb)) { //slice_weighting_flag
894             av_log(h->avctx, AV_LOG_ERROR,
895                    "weighted prediction not yet supported\n");
896         }
897     return 0;
898 }
899
900 static inline int check_for_slice(AVSContext *h)
901 {
902     GetBitContext *gb = &h->gb;
903     int align;
904
905     if (h->mbx)
906         return 0;
907     align = (-get_bits_count(gb)) & 7;
908     /* check for stuffing byte */
909     if (!align && (show_bits(gb, 8) == 0x80))
910         align = 8;
911     if ((show_bits_long(gb, 24 + align) & 0xFFFFFF) == 0x000001) {
912         skip_bits_long(gb, 24 + align);
913         h->stc = get_bits(gb, 8);
914         if (h->stc >= h->mb_height)
915             return 0;
916         decode_slice_header(h, gb);
917         return 1;
918     }
919     return 0;
920 }
921
922 /*****************************************************************************
923  *
924  * frame level
925  *
926  ****************************************************************************/
927
928 static int decode_pic(AVSContext *h)
929 {
930     int ret;
931     int skip_count    = -1;
932     enum cavs_mb mb_type;
933
934     if (!h->top_qp) {
935         av_log(h->avctx, AV_LOG_ERROR, "No sequence header decoded yet\n");
936         return AVERROR_INVALIDDATA;
937     }
938
939     av_frame_unref(h->cur.f);
940
941     skip_bits(&h->gb, 16);//bbv_dwlay
942     if (h->stc == PIC_PB_START_CODE) {
943         h->cur.f->pict_type = get_bits(&h->gb, 2) + AV_PICTURE_TYPE_I;
944         if (h->cur.f->pict_type > AV_PICTURE_TYPE_B) {
945             av_log(h->avctx, AV_LOG_ERROR, "illegal picture type\n");
946             return AVERROR_INVALIDDATA;
947         }
948         /* make sure we have the reference frames we need */
949         if (!h->DPB[0].f->data[0] ||
950            (!h->DPB[1].f->data[0] && h->cur.f->pict_type == AV_PICTURE_TYPE_B))
951             return AVERROR_INVALIDDATA;
952     } else {
953         h->cur.f->pict_type = AV_PICTURE_TYPE_I;
954         if (get_bits1(&h->gb))
955             skip_bits(&h->gb, 24);//time_code
956         /* old sample clips were all progressive and no low_delay,
957            bump stream revision if detected otherwise */
958         if (h->low_delay || !(show_bits(&h->gb, 9) & 1))
959             h->stream_revision = 1;
960         /* similarly test top_field_first and repeat_first_field */
961         else if (show_bits(&h->gb, 11) & 3)
962             h->stream_revision = 1;
963         if (h->stream_revision > 0)
964             skip_bits(&h->gb, 1); //marker_bit
965     }
966
967     ret = ff_get_buffer(h->avctx, h->cur.f, h->cur.f->pict_type == AV_PICTURE_TYPE_B ?
968                         0 : AV_GET_BUFFER_FLAG_REF);
969     if (ret < 0)
970         return ret;
971
972     if (!h->edge_emu_buffer) {
973         int alloc_size = FFALIGN(FFABS(h->cur.f->linesize[0]) + 32, 32);
974         h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 24);
975         if (!h->edge_emu_buffer)
976             return AVERROR(ENOMEM);
977     }
978
979     ff_cavs_init_pic(h);
980     h->cur.poc = get_bits(&h->gb, 8) * 2;
981
982     /* get temporal distances and MV scaling factors */
983     if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
984         h->dist[0] = (h->cur.poc - h->DPB[0].poc  + 512) % 512;
985     } else {
986         h->dist[0] = (h->DPB[0].poc  - h->cur.poc + 512) % 512;
987     }
988     h->dist[1] = (h->cur.poc - h->DPB[1].poc  + 512) % 512;
989     h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
990     h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
991     if (h->cur.f->pict_type == AV_PICTURE_TYPE_B) {
992         h->sym_factor = h->dist[0] * h->scale_den[1];
993     } else {
994         h->direct_den[0] = h->dist[0] ? 16384 / h->dist[0] : 0;
995         h->direct_den[1] = h->dist[1] ? 16384 / h->dist[1] : 0;
996     }
997
998     if (h->low_delay)
999         get_ue_golomb(&h->gb); //bbv_check_times
1000     h->progressive   = get_bits1(&h->gb);
1001     h->pic_structure = 1;
1002     if (!h->progressive)
1003         h->pic_structure = get_bits1(&h->gb);
1004     if (!h->pic_structure && h->stc == PIC_PB_START_CODE)
1005         skip_bits1(&h->gb);     //advanced_pred_mode_disable
1006     skip_bits1(&h->gb);        //top_field_first
1007     skip_bits1(&h->gb);        //repeat_first_field
1008     h->qp_fixed = get_bits1(&h->gb);
1009     h->qp       = get_bits(&h->gb, 6);
1010     if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1011         if (!h->progressive && !h->pic_structure)
1012             skip_bits1(&h->gb);//what is this?
1013         skip_bits(&h->gb, 4);   //reserved bits
1014     } else {
1015         if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
1016             h->ref_flag        = get_bits1(&h->gb);
1017         skip_bits(&h->gb, 4);   //reserved bits
1018         h->skip_mode_flag      = get_bits1(&h->gb);
1019     }
1020     h->loop_filter_disable     = get_bits1(&h->gb);
1021     if (!h->loop_filter_disable && get_bits1(&h->gb)) {
1022         h->alpha_offset        = get_se_golomb(&h->gb);
1023         h->beta_offset         = get_se_golomb(&h->gb);
1024     } else {
1025         h->alpha_offset = h->beta_offset  = 0;
1026     }
1027     if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1028         do {
1029             check_for_slice(h);
1030             decode_mb_i(h, 0);
1031         } while (ff_cavs_next_mb(h));
1032     } else if (h->cur.f->pict_type == AV_PICTURE_TYPE_P) {
1033         do {
1034             if (check_for_slice(h))
1035                 skip_count = -1;
1036             if (h->skip_mode_flag && (skip_count < 0))
1037                 skip_count = get_ue_golomb(&h->gb);
1038             if (h->skip_mode_flag && skip_count--) {
1039                 decode_mb_p(h, P_SKIP);
1040             } else {
1041                 mb_type = get_ue_golomb(&h->gb) + P_SKIP + h->skip_mode_flag;
1042                 if (mb_type > P_8X8)
1043                     decode_mb_i(h, mb_type - P_8X8 - 1);
1044                 else
1045                     decode_mb_p(h, mb_type);
1046             }
1047         } while (ff_cavs_next_mb(h));
1048     } else { /* AV_PICTURE_TYPE_B */
1049         do {
1050             if (check_for_slice(h))
1051                 skip_count = -1;
1052             if (h->skip_mode_flag && (skip_count < 0))
1053                 skip_count = get_ue_golomb(&h->gb);
1054             if (h->skip_mode_flag && skip_count--) {
1055                 decode_mb_b(h, B_SKIP);
1056             } else {
1057                 mb_type = get_ue_golomb(&h->gb) + B_SKIP + h->skip_mode_flag;
1058                 if (mb_type > B_8X8)
1059                     decode_mb_i(h, mb_type - B_8X8 - 1);
1060                 else
1061                     decode_mb_b(h, mb_type);
1062             }
1063         } while (ff_cavs_next_mb(h));
1064     }
1065     if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1066         av_frame_unref(h->DPB[1].f);
1067         FFSWAP(AVSFrame, h->cur, h->DPB[1]);
1068         FFSWAP(AVSFrame, h->DPB[0], h->DPB[1]);
1069     }
1070     return 0;
1071 }
1072
1073 /*****************************************************************************
1074  *
1075  * headers and interface
1076  *
1077  ****************************************************************************/
1078
1079 static int decode_seq_header(AVSContext *h)
1080 {
1081     int frame_rate_code;
1082     int width, height;
1083
1084     h->profile = get_bits(&h->gb, 8);
1085     h->level   = get_bits(&h->gb, 8);
1086     skip_bits1(&h->gb); //progressive sequence
1087
1088     width  = get_bits(&h->gb, 14);
1089     height = get_bits(&h->gb, 14);
1090     if ((h->width || h->height) && (h->width != width || h->height != height)) {
1091         avpriv_report_missing_feature(h->avctx,
1092                                       "Width/height changing in CAVS");
1093         return AVERROR_PATCHWELCOME;
1094     }
1095     h->width  = width;
1096     h->height = height;
1097
1098     skip_bits(&h->gb, 2); //chroma format
1099     skip_bits(&h->gb, 3); //sample_precision
1100     h->aspect_ratio = get_bits(&h->gb, 4);
1101     frame_rate_code = get_bits(&h->gb, 4);
1102     skip_bits(&h->gb, 18); //bit_rate_lower
1103     skip_bits1(&h->gb);    //marker_bit
1104     skip_bits(&h->gb, 12); //bit_rate_upper
1105     h->low_delay =  get_bits1(&h->gb);
1106     h->mb_width  = (h->width  + 15) >> 4;
1107     h->mb_height = (h->height + 15) >> 4;
1108     h->avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];
1109     h->avctx->width  = h->width;
1110     h->avctx->height = h->height;
1111     if (!h->top_qp)
1112         ff_cavs_init_top_lines(h);
1113     return 0;
1114 }
1115
1116 static void cavs_flush(AVCodecContext * avctx)
1117 {
1118     AVSContext *h = avctx->priv_data;
1119     h->got_keyframe = 0;
1120 }
1121
1122 static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1123                              AVPacket *avpkt)
1124 {
1125     AVSContext *h      = avctx->priv_data;
1126     const uint8_t *buf = avpkt->data;
1127     int buf_size       = avpkt->size;
1128     uint32_t stc       = -1;
1129     int input_size, ret;
1130     const uint8_t *buf_end;
1131     const uint8_t *buf_ptr;
1132
1133     if (buf_size == 0) {
1134         if (!h->low_delay && h->DPB[0].f->data[0]) {
1135             *got_frame = 1;
1136             av_frame_move_ref(data, h->DPB[0].f);
1137         }
1138         return 0;
1139     }
1140
1141     buf_ptr = buf;
1142     buf_end = buf + buf_size;
1143     for(;;) {
1144         buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
1145         if ((stc & 0xFFFFFE00) || buf_ptr == buf_end)
1146             return FFMAX(0, buf_ptr - buf);
1147         input_size = (buf_end - buf_ptr) * 8;
1148         switch (stc) {
1149         case CAVS_START_CODE:
1150             init_get_bits(&h->gb, buf_ptr, input_size);
1151             decode_seq_header(h);
1152             break;
1153         case PIC_I_START_CODE:
1154             if (!h->got_keyframe) {
1155                 av_frame_unref(h->DPB[0].f);
1156                 av_frame_unref(h->DPB[1].f);
1157                 h->got_keyframe = 1;
1158             }
1159         case PIC_PB_START_CODE:
1160             *got_frame = 0;
1161             if (!h->got_keyframe)
1162                 break;
1163             init_get_bits(&h->gb, buf_ptr, input_size);
1164             h->stc = stc;
1165             if (decode_pic(h))
1166                 break;
1167             *got_frame = 1;
1168             if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1169                 if (h->DPB[1].f->data[0]) {
1170                     if ((ret = av_frame_ref(data, h->DPB[1].f)) < 0)
1171                         return ret;
1172                 } else {
1173                     *got_frame = 0;
1174                 }
1175             } else {
1176                 av_frame_move_ref(data, h->cur.f);
1177             }
1178             break;
1179         case EXT_START_CODE:
1180             //mpeg_decode_extension(avctx, buf_ptr, input_size);
1181             break;
1182         case USER_START_CODE:
1183             //mpeg_decode_user_data(avctx, buf_ptr, input_size);
1184             break;
1185         default:
1186             if (stc <= SLICE_MAX_START_CODE) {
1187                 init_get_bits(&h->gb, buf_ptr, input_size);
1188                 decode_slice_header(h, &h->gb);
1189             }
1190             break;
1191         }
1192     }
1193 }
1194
1195 AVCodec ff_cavs_decoder = {
1196     .name           = "cavs",
1197     .long_name      = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
1198     .type           = AVMEDIA_TYPE_VIDEO,
1199     .id             = AV_CODEC_ID_CAVS,
1200     .priv_data_size = sizeof(AVSContext),
1201     .init           = ff_cavs_init,
1202     .close          = ff_cavs_end,
1203     .decode         = cavs_decode_frame,
1204     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1205     .flush          = cavs_flush,
1206 };