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