cavsdec.c
Go to the documentation of this file.
1 /*
2  * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
3  * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
25  * @author Stefan Gehrer <stefan.gehrer@gmx.de>
26  */
27 
28 #include "libavutil/avassert.h"
29 #include "avcodec.h"
30 #include "get_bits.h"
31 #include "golomb.h"
32 #include "cavs.h"
33 #include "internal.h"
34 #include "mpeg12data.h"
35 #include "mpegvideo.h"
36 
37 static const uint8_t mv_scan[4] = {
40 };
41 
42 static const uint8_t cbp_tab[64][2] = {
43  { 63, 0 }, { 15, 15 }, { 31, 63 }, { 47, 31 }, { 0, 16 }, { 14, 32 }, { 13, 47 }, { 11, 13 },
44  { 7, 14 }, { 5, 11 }, { 10, 12 }, { 8, 5 }, { 12, 10 }, { 61, 7 }, { 4, 48 }, { 55, 3 },
45  { 1, 2 }, { 2, 8 }, { 59, 4 }, { 3, 1 }, { 62, 61 }, { 9, 55 }, { 6, 59 }, { 29, 62 },
46  { 45, 29 }, { 51, 27 }, { 23, 23 }, { 39, 19 }, { 27, 30 }, { 46, 28 }, { 53, 9 }, { 30, 6 },
47  { 43, 60 }, { 37, 21 }, { 60, 44 }, { 16, 26 }, { 21, 51 }, { 28, 35 }, { 19, 18 }, { 35, 20 },
48  { 42, 24 }, { 26, 53 }, { 44, 17 }, { 32, 37 }, { 58, 39 }, { 24, 45 }, { 20, 58 }, { 17, 43 },
49  { 18, 42 }, { 48, 46 }, { 22, 36 }, { 33, 33 }, { 25, 34 }, { 49, 40 }, { 40, 52 }, { 36, 49 },
50  { 34, 50 }, { 50, 56 }, { 52, 25 }, { 54, 22 }, { 41, 54 }, { 56, 57 }, { 38, 41 }, { 57, 38 }
51 };
52 
53 static const uint8_t scan3x3[4] = { 4, 5, 7, 8 };
54 
55 static const uint8_t cavs_chroma_qp[64] = {
56  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
57  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
58  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 42, 43, 43, 44, 44,
59  45, 45, 46, 46, 47, 47, 48, 48, 48, 49, 49, 49, 50, 50, 50, 51
60 };
61 
62 static const uint8_t dequant_shift[64] = {
63  14, 14, 14, 14, 14, 14, 14, 14,
64  13, 13, 13, 13, 13, 13, 13, 13,
65  13, 12, 12, 12, 12, 12, 12, 12,
66  11, 11, 11, 11, 11, 11, 11, 11,
67  11, 10, 10, 10, 10, 10, 10, 10,
68  10, 9, 9, 9, 9, 9, 9, 9,
69  9, 8, 8, 8, 8, 8, 8, 8,
70  7, 7, 7, 7, 7, 7, 7, 7
71 };
72 
73 static const uint16_t dequant_mul[64] = {
74  32768, 36061, 38968, 42495, 46341, 50535, 55437, 60424,
75  32932, 35734, 38968, 42495, 46177, 50535, 55109, 59933,
76  65535, 35734, 38968, 42577, 46341, 50617, 55027, 60097,
77  32809, 35734, 38968, 42454, 46382, 50576, 55109, 60056,
78  65535, 35734, 38968, 42495, 46320, 50515, 55109, 60076,
79  65535, 35744, 38968, 42495, 46341, 50535, 55099, 60087,
80  65535, 35734, 38973, 42500, 46341, 50535, 55109, 60097,
81  32771, 35734, 38965, 42497, 46341, 50535, 55109, 60099
82 };
83 
84 #define EOB 0, 0, 0
85 
86 static const struct dec_2dvlc intra_dec[7] = {
87  {
88  { //level / run / table_inc
89  { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
90  { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
91  { 1, 7, 1 }, { -1, 7, 1 }, { 1, 8, 1 }, { -1, 8, 1 }, { 1, 9, 1 }, { -1, 9, 1 },
92  { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 }, { 2, 1, 2 }, { -2, 1, 2 },
93  { 1, 12, 1 }, { -1, 12, 1 }, { 1, 13, 1 }, { -1, 13, 1 }, { 1, 14, 1 }, { -1, 14, 1 },
94  { 1, 15, 1 }, { -1, 15, 1 }, { 2, 2, 2 }, { -2, 2, 2 }, { 1, 16, 1 }, { -1, 16, 1 },
95  { 1, 17, 1 }, { -1, 17, 1 }, { 3, 1, 3 }, { -3, 1, 3 }, { 1, 18, 1 }, { -1, 18, 1 },
96  { 1, 19, 1 }, { -1, 19, 1 }, { 2, 3, 2 }, { -2, 3, 2 }, { 1, 20, 1 }, { -1, 20, 1 },
97  { 1, 21, 1 }, { -1, 21, 1 }, { 2, 4, 2 }, { -2, 4, 2 }, { 1, 22, 1 }, { -1, 22, 1 },
98  { 2, 5, 2 }, { -2, 5, 2 }, { 1, 23, 1 }, { -1, 23, 1 }, { EOB }
99  },
100  //level_add
101  { 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 },
102  2, //golomb_order
103  0, //inc_limit
104  23, //max_run
105  },
106  {
107  { //level / run
108  { 1, 1, 0 }, { -1, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 1 }, { -2, 1, 1 },
109  { 1, 3, 0 }, { -1, 3, 0 }, { EOB }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 },
110  { -1, 5, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 3, 1, 2 }, { -3, 1, 2 }, { 2, 2, 1 },
111  { -2, 2, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 1, 9, 0 },
112  { -1, 9, 0 }, { 2, 3, 1 }, { -2, 3, 1 }, { 4, 1, 2 }, { -4, 1, 2 }, { 1, 10, 0 },
113  { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 2, 4, 1 }, { -2, 4, 1 }, { 3, 2, 2 },
114  { -3, 2, 2 }, { 1, 12, 0 }, { -1, 12, 0 }, { 2, 5, 1 }, { -2, 5, 1 }, { 5, 1, 3 },
115  { -5, 1, 3 }, { 1, 13, 0 }, { -1, 13, 0 }, { 2, 6, 1 }, { -2, 6, 1 }, { 1, 14, 0 },
116  { -1, 14, 0 }, { 2, 7, 1 }, { -2, 7, 1 }, { 2, 8, 1 }, { -2, 8, 1 }, { 3, 3, 2 },
117  { -3, 3, 2 }, { 6, 1, 3 }, { -6, 1, 3 }, { 1, 15, 0 }, { -1, 15, 0 }
118  },
119  //level_add
120  { 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 },
121  2, //golomb_order
122  1, //inc_limit
123  15, //max_run
124  },
125  {
126  { //level / run
127  { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 },
128  { 3, 1, 1 }, { -3, 1, 1 }, { EOB }, { 1, 3, 0 }, { -1, 3, 0 }, { 2, 2, 0 },
129  { -2, 2, 0 }, { 4, 1, 1 }, { -4, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 2 },
130  { -5, 1, 2 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 3, 0 },
131  { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 2 }, { -6, 1, 2 }, { 2, 4, 0 },
132  { -2, 4, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 7, 1, 2 },
133  { -7, 1, 2 }, { 3, 3, 1 }, { -3, 3, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 8, 0 },
134  { -1, 8, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 8, 1, 3 }, { -8, 1, 3 }, { 1, 9, 0 },
135  { -1, 9, 0 }, { 5, 2, 2 }, { -5, 2, 2 }, { 3, 4, 1 }, { -3, 4, 1 }, { 2, 7, 0 },
136  { -2, 7, 0 }, { 9, 1, 3 }, { -9, 1, 3 }, { 1, 10, 0 }, { -1, 10, 0 }
137  },
138  //level_add
139  { 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 },
140  2, //golomb_order
141  2, //inc_limit
142  10, //max_run
143  },
144  {
145  { //level / run
146  { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 }, { -3, 1, 0 },
147  { 1, 2, 0 }, { -1, 2, 0 }, { EOB }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 1 },
148  { -5, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 6, 1, 1 },
149  { -6, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 1, 4, 0 },
150  { -1, 4, 0 }, { 8, 1, 2 }, { -8, 1, 2 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 2, 0 },
151  { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 9, 1, 2 }, { -9, 1, 2 }, { 5, 2, 1 },
152  { -5, 2, 1 }, { 2, 4, 0 }, { -2, 4, 0 }, { 10, 1, 2 }, {-10, 1, 2 }, { 3, 3, 0 },
153  { -3, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 11, 1, 3 }, {-11, 1, 3 }, { 6, 2, 1 },
154  { -6, 2, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 2, 5, 0 }, { -2, 5, 0 }, { 3, 4, 0 },
155  { -3, 4, 0 }, { 12, 1, 3 }, {-12, 1, 3 }, { 4, 3, 0 }, { -4, 3, 0 }
156  },
157  //level_add
158  { 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 },
159  2, //golomb_order
160  4, //inc_limit
161  7, //max_run
162  },
163  {
164  { //level / run
165  { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 }, { -3, 1, 0 },
166  { EOB }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
167  { -6, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 1 },
168  { -8, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 9, 1, 1 }, { -9, 1, 1 }, { 10, 1, 1 },
169  {-10, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 11, 1, 2 },
170  {-11, 1, 2 }, { 4, 2, 0 }, { -4, 2, 0 }, { 12, 1, 2 }, {-12, 1, 2 }, { 13, 1, 2 },
171  {-13, 1, 2 }, { 5, 2, 0 }, { -5, 2, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 2, 3, 0 },
172  { -2, 3, 0 }, { 14, 1, 2 }, {-14, 1, 2 }, { 6, 2, 0 }, { -6, 2, 0 }, { 15, 1, 2 },
173  {-15, 1, 2 }, { 16, 1, 2 }, {-16, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 1, 5, 0 },
174  { -1, 5, 0 }, { 7, 2, 0 }, { -7, 2, 0 }, { 17, 1, 2 }, {-17, 1, 2 }
175  },
176  //level_add
177  { 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 },
178  2, //golomb_order
179  7, //inc_limit
180  5, //max_run
181  },
182  {
183  { //level / run
184  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
185  { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
186  { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 9, 1, 0 },
187  { -9, 1, 0 }, { 10, 1, 0 }, {-10, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 11, 1, 1 },
188  {-11, 1, 1 }, { 12, 1, 1 }, {-12, 1, 1 }, { 13, 1, 1 }, {-13, 1, 1 }, { 2, 2, 0 },
189  { -2, 2, 0 }, { 14, 1, 1 }, {-14, 1, 1 }, { 15, 1, 1 }, {-15, 1, 1 }, { 3, 2, 0 },
190  { -3, 2, 0 }, { 16, 1, 1 }, {-16, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 17, 1, 1 },
191  {-17, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 18, 1, 1 }, {-18, 1, 1 }, { 5, 2, 0 },
192  { -5, 2, 0 }, { 19, 1, 1 }, {-19, 1, 1 }, { 20, 1, 1 }, {-20, 1, 1 }, { 6, 2, 0 },
193  { -6, 2, 0 }, { 21, 1, 1 }, {-21, 1, 1 }, { 2, 3, 0 }, { -2, 3, 0 }
194  },
195  //level_add
196  { 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 },
197  2, //golomb_order
198  10, //inc_limit
199  3, //max_run
200  },
201  {
202  { //level / run
203  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
204  { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
205  { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 9, 1, 0 },
206  { -9, 1, 0 }, { 10, 1, 0 }, {-10, 1, 0 }, { 11, 1, 0 }, {-11, 1, 0 }, { 12, 1, 0 },
207  {-12, 1, 0 }, { 13, 1, 0 }, {-13, 1, 0 }, { 14, 1, 0 }, {-14, 1, 0 }, { 15, 1, 0 },
208  {-15, 1, 0 }, { 16, 1, 0 }, {-16, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 17, 1, 0 },
209  {-17, 1, 0 }, { 18, 1, 0 }, {-18, 1, 0 }, { 19, 1, 0 }, {-19, 1, 0 }, { 20, 1, 0 },
210  {-20, 1, 0 }, { 21, 1, 0 }, {-21, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 22, 1, 0 },
211  {-22, 1, 0 }, { 23, 1, 0 }, {-23, 1, 0 }, { 24, 1, 0 }, {-24, 1, 0 }, { 25, 1, 0 },
212  {-25, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 26, 1, 0 }, {-26, 1, 0 }
213  },
214  //level_add
215  { 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 },
216  2, //golomb_order
217  INT_MAX, //inc_limit
218  2, //max_run
219  }
220 };
221 
222 static const struct dec_2dvlc inter_dec[7] = {
223  {
224  { //level / run
225  { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
226  { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
227  { 1, 7, 1 }, { -1, 7, 1 }, { 1, 8, 1 }, { -1, 8, 1 }, { 1, 9, 1 }, { -1, 9, 1 },
228  { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 }, { 1, 12, 1 }, { -1, 12, 1 },
229  { 1, 13, 1 }, { -1, 13, 1 }, { 2, 1, 2 }, { -2, 1, 2 }, { 1, 14, 1 }, { -1, 14, 1 },
230  { 1, 15, 1 }, { -1, 15, 1 }, { 1, 16, 1 }, { -1, 16, 1 }, { 1, 17, 1 }, { -1, 17, 1 },
231  { 1, 18, 1 }, { -1, 18, 1 }, { 1, 19, 1 }, { -1, 19, 1 }, { 3, 1, 3 }, { -3, 1, 3 },
232  { 1, 20, 1 }, { -1, 20, 1 }, { 1, 21, 1 }, { -1, 21, 1 }, { 2, 2, 2 }, { -2, 2, 2 },
233  { 1, 22, 1 }, { -1, 22, 1 }, { 1, 23, 1 }, { -1, 23, 1 }, { 1, 24, 1 }, { -1, 24, 1 },
234  { 1, 25, 1 }, { -1, 25, 1 }, { 1, 26, 1 }, { -1, 26, 1 }, { EOB }
235  },
236  //level_add
237  { 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 },
238  3, //golomb_order
239  0, //inc_limit
240  26 //max_run
241  },
242  {
243  { //level / run
244  { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 1, 2, 0 }, { -1, 2, 0 }, { 1, 3, 0 },
245  { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 1, 6, 0 },
246  { -1, 6, 0 }, { 2, 1, 1 }, { -2, 1, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 1, 8, 0 },
247  { -1, 8, 0 }, { 1, 9, 0 }, { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 2, 2, 1 },
248  { -2, 2, 1 }, { 1, 11, 0 }, { -1, 11, 0 }, { 1, 12, 0 }, { -1, 12, 0 }, { 3, 1, 2 },
249  { -3, 1, 2 }, { 1, 13, 0 }, { -1, 13, 0 }, { 1, 14, 0 }, { -1, 14, 0 }, { 2, 3, 1 },
250  { -2, 3, 1 }, { 1, 15, 0 }, { -1, 15, 0 }, { 2, 4, 1 }, { -2, 4, 1 }, { 1, 16, 0 },
251  { -1, 16, 0 }, { 2, 5, 1 }, { -2, 5, 1 }, { 1, 17, 0 }, { -1, 17, 0 }, { 4, 1, 3 },
252  { -4, 1, 3 }, { 2, 6, 1 }, { -2, 6, 1 }, { 1, 18, 0 }, { -1, 18, 0 }, { 1, 19, 0 },
253  { -1, 19, 0 }, { 2, 7, 1 }, { -2, 7, 1 }, { 3, 2, 2 }, { -3, 2, 2 }
254  },
255  //level_add
256  { 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 },
257  2, //golomb_order
258  1, //inc_limit
259  19 //max_run
260  },
261  {
262  { //level / run
263  { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 0 },
264  { -2, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 3, 1, 1 },
265  { -3, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 1, 6, 0 },
266  { -1, 6, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 1, 2 },
267  { -4, 1, 2 }, { 1, 8, 0 }, { -1, 8, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 4, 0 },
268  { -2, 4, 0 }, { 1, 9, 0 }, { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 5, 1, 2 },
269  { -5, 1, 2 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 2, 6, 0 },
270  { -2, 6, 0 }, { 1, 12, 0 }, { -1, 12, 0 }, { 3, 3, 1 }, { -3, 3, 1 }, { 6, 1, 2 },
271  { -6, 1, 2 }, { 4, 2, 2 }, { -4, 2, 2 }, { 1, 13, 0 }, { -1, 13, 0 }, { 2, 7, 0 },
272  { -2, 7, 0 }, { 3, 4, 1 }, { -3, 4, 1 }, { 1, 14, 0 }, { -1, 14, 0 }
273  },
274  //level_add
275  { 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 },
276  2, //golomb_order
277  2, //inc_limit
278  14 //max_run
279  },
280  {
281  { //level / run
282  { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 },
283  { -1, 2, 0 }, { 3, 1, 0 }, { -3, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 2, 2, 0 },
284  { -2, 2, 0 }, { 4, 1, 1 }, { -4, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 1 },
285  { -5, 1, 1 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 2, 3, 0 },
286  { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 1 }, { -6, 1, 1 }, { 2, 4, 0 },
287  { -2, 4, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 7, 1, 2 },
288  { -7, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 2, 5, 0 },
289  { -2, 5, 0 }, { 8, 1, 2 }, { -8, 1, 2 }, { 1, 9, 0 }, { -1, 9, 0 }, { 3, 4, 0 },
290  { -3, 4, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 5, 2, 1 }, { -5, 2, 1 }, { 1, 10, 0 },
291  { -1, 10, 0 }, { 9, 1, 2 }, { -9, 1, 2 }, { 4, 3, 1 }, { -4, 3, 1 }
292  },
293  //level_add
294  { 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 },
295  2, //golomb_order
296  3, //inc_limit
297  10 //max_run
298  },
299  {
300  { //level / run
301  { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
302  { -3, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 },
303  { -5, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 6, 1, 0 },
304  { -6, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 1, 4, 0 },
305  { -1, 4, 0 }, { 8, 1, 1 }, { -8, 1, 1 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 2, 0 },
306  { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 9, 1, 1 }, { -9, 1, 1 }, { 5, 2, 0 },
307  { -5, 2, 0 }, { 2, 4, 0 }, { -2, 4, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 10, 1, 2 },
308  {-10, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 11, 1, 2 }, {-11, 1, 2 }, { 1, 7, 0 },
309  { -1, 7, 0 }, { 6, 2, 0 }, { -6, 2, 0 }, { 3, 4, 0 }, { -3, 4, 0 }, { 2, 5, 0 },
310  { -2, 5, 0 }, { 12, 1, 2 }, {-12, 1, 2 }, { 4, 3, 0 }, { -4, 3, 0 }
311  },
312  //level_add
313  { 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 },
314  2, //golomb_order
315  6, //inc_limit
316  7 //max_run
317  },
318  {
319  { //level / run
320  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
321  { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 1, 2, 0 },
322  { -1, 2, 0 }, { 6, 1, 0 }, { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 },
323  { -8, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 1, 3, 0 },
324  { -1, 3, 0 }, { 10, 1, 1 }, { -10, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 11, 1, 1 },
325  { -11, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 12, 1, 1 }, { -12, 1, 1 }, { 1, 4, 0 },
326  { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 13, 1, 1 }, { -13, 1, 1 }, { 5, 2, 0 },
327  { -5, 2, 0 }, { 14, 1, 1 }, { -14, 1, 1 }, { 6, 2, 0 }, { -6, 2, 0 }, { 1, 5, 0 },
328  { -1, 5, 0 }, { 15, 1, 1 }, { -15, 1, 1 }, { 3, 3, 0 }, { -3, 3, 0 }, { 16, 1, 1 },
329  { -16, 1, 1 }, { 2, 4, 0 }, { -2, 4, 0 }, { 7, 2, 0 }, { -7, 2, 0 }
330  },
331  //level_add
332  { 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 },
333  2, //golomb_order
334  9, //inc_limit
335  5 //max_run
336  },
337  {
338  { //level / run
339  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
340  { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
341  { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 8, 1, 0 },
342  { -8, 1, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 10, 1, 0 }, { -10, 1, 0 }, { 11, 1, 0 },
343  { -11, 1, 0 }, { 12, 1, 0 }, { -12, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 13, 1, 0 },
344  { -13, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 14, 1, 0 }, { -14, 1, 0 }, { 15, 1, 0 },
345  { -15, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 16, 1, 0 }, { -16, 1, 0 }, { 17, 1, 0 },
346  { -17, 1, 0 }, { 18, 1, 0 }, { -18, 1, 0 }, { 4, 2, 0 }, { -4, 2, 0 }, { 19, 1, 0 },
347  { -19, 1, 0 }, { 20, 1, 0 }, { -20, 1, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 1, 4, 0 },
348  { -1, 4, 0 }, { 5, 2, 0 }, { -5, 2, 0 }, { 21, 1, 0 }, { -21, 1, 0 }
349  },
350  //level_add
351  { 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 },
352  2, //golomb_order
353  INT_MAX, //inc_limit
354  4 //max_run
355  }
356 };
357 
358 static const struct dec_2dvlc chroma_dec[5] = {
359  {
360  { //level / run
361  { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
362  { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
363  { 1, 7, 1 }, { -1, 7, 1 }, { 2, 1, 2 }, { -2, 1, 2 }, { 1, 8, 1 }, { -1, 8, 1 },
364  { 1, 9, 1 }, { -1, 9, 1 }, { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 },
365  { 1, 12, 1 }, { -1, 12, 1 }, { 1, 13, 1 }, { -1, 13, 1 }, { 1, 14, 1 }, { -1, 14, 1 },
366  { 1, 15, 1 }, { -1, 15, 1 }, { 3, 1, 3 }, { -3, 1, 3 }, { 1, 16, 1 }, { -1, 16, 1 },
367  { 1, 17, 1 }, { -1, 17, 1 }, { 1, 18, 1 }, { -1, 18, 1 }, { 1, 19, 1 }, { -1, 19, 1 },
368  { 1, 20, 1 }, { -1, 20, 1 }, { 1, 21, 1 }, { -1, 21, 1 }, { 1, 22, 1 }, { -1, 22, 1 },
369  { 2, 2, 2 }, { -2, 2, 2 }, { 1, 23, 1 }, { -1, 23, 1 }, { 1, 24, 1 }, { -1, 24, 1 },
370  { 1, 25, 1 }, { -1, 25, 1 }, { 4, 1, 3 }, { -4, 1, 3 }, { EOB }
371  },
372  //level_add
373  { 0, 5, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
374  2, //golomb_order
375  0, //inc_limit
376  25 //max_run
377  },
378  {
379  { //level / run
380  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 1 },
381  { -2, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 },
382  { -1, 5, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 3, 1, 2 }, { -3, 1, 2 }, { 1, 7, 0 },
383  { -1, 7, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 2, 2, 1 }, { -2, 2, 1 }, { 1, 9, 0 },
384  { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 4, 1, 2 },
385  { -4, 1, 2 }, { 1, 12, 0 }, { -1, 12, 0 }, { 1, 13, 0 }, { -1, 13, 0 }, { 1, 14, 0 },
386  { -1, 14, 0 }, { 2, 3, 1 }, { -2, 3, 1 }, { 1, 15, 0 }, { -1, 15, 0 }, { 2, 4, 1 },
387  { -2, 4, 1 }, { 5, 1, 3 }, { -5, 1, 3 }, { 3, 2, 2 }, { -3, 2, 2 }, { 1, 16, 0 },
388  { -1, 16, 0 }, { 1, 17, 0 }, { -1, 17, 0 }, { 1, 18, 0 }, { -1, 18, 0 }, { 2, 5, 1 },
389  { -2, 5, 1 }, { 1, 19, 0 }, { -1, 19, 0 }, { 1, 20, 0 }, { -1, 20, 0 }
390  },
391  //level_add
392  { 0, 6, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1 },
393  0, //golomb_order
394  1, //inc_limit
395  20 //max_run
396  },
397  {
398  { //level / run
399  { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 },
400  { -1, 2, 0 }, { 3, 1, 1 }, { -3, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 4, 1, 1 },
401  { -4, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 2 },
402  { -5, 1, 2 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 3, 0 },
403  { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 2 }, { -6, 1, 2 }, { 1, 7, 0 },
404  { -1, 7, 0 }, { 2, 4, 0 }, { -2, 4, 0 }, { 7, 1, 2 }, { -7, 1, 2 }, { 1, 8, 0 },
405  { -1, 8, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 1, 9, 0 }, { -1, 9, 0 }, { 3, 3, 1 },
406  { -3, 3, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 8, 1, 2 },
407  { -8, 1, 2 }, { 1, 10, 0 }, { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 9, 1, 2 },
408  { -9, 1, 2 }, { 5, 2, 2 }, { -5, 2, 2 }, { 3, 4, 1 }, { -3, 4, 1 },
409  },
410  //level_add
411  { 0,10, 6, 4, 4, 3, 3, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
412  1, //golomb_order
413  2, //inc_limit
414  11 //max_run
415  },
416  {
417  { //level / run
418  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
419  { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 5, 1, 1 },
420  { -5, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 6, 1, 1 }, { -6, 1, 1 }, { 1, 3, 0 },
421  { -1, 3, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 8, 1, 1 },
422  { -8, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 9, 1, 1 },
423  { -9, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 10, 1, 1 },
424  {-10, 1, 1 }, { 3, 3, 0 }, { -3, 3, 0 }, { 5, 2, 1 }, { -5, 2, 1 }, { 2, 4, 0 },
425  { -2, 4, 0 }, { 11, 1, 1 }, {-11, 1, 1 }, { 1, 6, 0 }, { -1, 6, 0 }, { 12, 1, 1 },
426  {-12, 1, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 6, 2, 1 }, { -6, 2, 1 }, { 13, 1, 1 },
427  {-13, 1, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 8, 0 }, { -1, 8, 0 },
428  },
429  //level_add
430  { 0, 14, 7, 4, 3, 3, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
431  1, //golomb_order
432  4, //inc_limit
433  8 //max_run
434  },
435  {
436  { //level / run
437  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
438  { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
439  { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 1, 2, 0 },
440  { -1, 2, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 10, 1, 0 }, { -10, 1, 0 }, { 11, 1, 0 },
441  { -11, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 12, 1, 0 }, { -12, 1, 0 }, { 13, 1, 0 },
442  { -13, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 14, 1, 0 }, { -14, 1, 0 }, { 1, 3, 0 },
443  { -1, 3, 0 }, { 15, 1, 0 }, { -15, 1, 0 }, { 4, 2, 0 }, { -4, 2, 0 }, { 16, 1, 0 },
444  { -16, 1, 0 }, { 17, 1, 0 }, { -17, 1, 0 }, { 5, 2, 0 }, { -5, 2, 0 }, { 1, 4, 0 },
445  { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 18, 1, 0 }, { -18, 1, 0 }, { 6, 2, 0 },
446  { -6, 2, 0 }, { 19, 1, 0 }, { -19, 1, 0 }, { 1, 5, 0 }, { -1, 5, 0 },
447  },
448  //level_add
449  { 0, 20, 7, 3, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
450  0, //golomb_order
451  INT_MAX, //inc_limit
452  5, //max_run
453  }
454 };
455 
456 #undef EOB
457 
458 /*****************************************************************************
459  *
460  * motion vector prediction
461  *
462  ****************************************************************************/
463 
464 static inline void store_mvs(AVSContext *h)
465 {
466  h->col_mv[h->mbidx * 4 + 0] = h->mv[MV_FWD_X0];
467  h->col_mv[h->mbidx * 4 + 1] = h->mv[MV_FWD_X1];
468  h->col_mv[h->mbidx * 4 + 2] = h->mv[MV_FWD_X2];
469  h->col_mv[h->mbidx * 4 + 3] = h->mv[MV_FWD_X3];
470 }
471 
472 static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
473  cavs_vector *col_mv)
474 {
475  cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
476  int den = h->direct_den[col_mv->ref];
477  int m = col_mv->x >> 31;
478 
479  pmv_fw->dist = h->dist[1];
480  pmv_bw->dist = h->dist[0];
481  pmv_fw->ref = 1;
482  pmv_bw->ref = 0;
483  /* scale the co-located motion vector according to its temporal span */
484  pmv_fw->x = (((den + (den * col_mv->x * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
485  pmv_bw->x = m - (((den + (den * col_mv->x * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
486  m = col_mv->y >> 31;
487  pmv_fw->y = (((den + (den * col_mv->y * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
488  pmv_bw->y = m - (((den + (den * col_mv->y * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
489 }
490 
491 static inline void mv_pred_sym(AVSContext *h, cavs_vector *src,
492  enum cavs_block size)
493 {
494  cavs_vector *dst = src + MV_BWD_OFFS;
495 
496  /* backward mv is the scaled and negated forward mv */
497  dst->x = -((src->x * h->sym_factor + 256) >> 9);
498  dst->y = -((src->y * h->sym_factor + 256) >> 9);
499  dst->ref = 0;
500  dst->dist = h->dist[0];
501  set_mvs(dst, size);
502 }
503 
504 /*****************************************************************************
505  *
506  * residual data decoding
507  *
508  ****************************************************************************/
509 
510 /** kth-order exponential golomb code */
511 static inline int get_ue_code(GetBitContext *gb, int order)
512 {
513  unsigned ret = get_ue_golomb(gb);
514  if (ret >= ((1U<<31)>>order)) {
515  av_log(NULL, AV_LOG_ERROR, "get_ue_code: value too larger\n");
516  return AVERROR_INVALIDDATA;
517  }
518  if (order) {
519  return (ret<<order) + get_bits(gb, order);
520  }
521  return ret;
522 }
523 
524 static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf,
525  int16_t *dst, int mul, int shift, int coeff_num)
526 {
527  int round = 1 << (shift - 1);
528  int pos = -1;
529  const uint8_t *scantab = h->scantable.permutated;
530 
531  /* inverse scan and dequantization */
532  while (--coeff_num >= 0) {
533  pos += run_buf[coeff_num];
534  if (pos > 63) {
536  "position out of block bounds at pic %d MB(%d,%d)\n",
537  h->cur.poc, h->mbx, h->mby);
538  return -1;
539  }
540  dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
541  }
542  return 0;
543 }
544 
545 /**
546  * decode coefficients from one 8x8 block, dequantize, inverse transform
547  * and add them to sample block
548  * @param r pointer to 2D VLC table
549  * @param esc_golomb_order escape codes are k-golomb with this order k
550  * @param qp quantizer
551  * @param dst location of sample block
552  * @param stride line stride in frame buffer
553  */
555  const struct dec_2dvlc *r, int esc_golomb_order,
556  int qp, uint8_t *dst, int stride)
557 {
558  int i, esc_code, level, mask;
559  unsigned int level_code, run;
560  int16_t level_buf[65];
561  uint8_t run_buf[65];
562  int16_t *block = h->block;
563 
564  for (i = 0; i < 65; i++) {
565  level_code = get_ue_code(gb, r->golomb_order);
566  if (level_code >= ESCAPE_CODE) {
567  run = ((level_code - ESCAPE_CODE) >> 1) + 1;
568  if(run > 64)
569  return -1;
570  esc_code = get_ue_code(gb, esc_golomb_order);
571  level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
572  while (level > r->inc_limit)
573  r++;
574  mask = -(level_code & 1);
575  level = (level ^ mask) - mask;
576  } else {
577  level = r->rltab[level_code][0];
578  if (!level) //end of block signal
579  break;
580  run = r->rltab[level_code][1];
581  r += r->rltab[level_code][2];
582  }
583  level_buf[i] = level;
584  run_buf[i] = run;
585  }
586  if (dequant(h, level_buf, run_buf, block, dequant_mul[qp],
587  dequant_shift[qp], i))
588  return -1;
589  h->cdsp.cavs_idct8_add(dst, block, stride);
590  h->dsp.clear_block(block);
591  return 0;
592 }
593 
594 
595 static inline void decode_residual_chroma(AVSContext *h)
596 {
597  if (h->cbp & (1 << 4))
598  decode_residual_block(h, &h->gb, chroma_dec, 0,
599  cavs_chroma_qp[h->qp], h->cu, h->c_stride);
600  if (h->cbp & (1 << 5))
601  decode_residual_block(h, &h->gb, chroma_dec, 0,
602  cavs_chroma_qp[h->qp], h->cv, h->c_stride);
603 }
604 
605 static inline int decode_residual_inter(AVSContext *h)
606 {
607  int block;
608 
609  /* get coded block pattern */
610  int cbp = get_ue_golomb(&h->gb);
611  if (cbp > 63U) {
612  av_log(h->avctx, AV_LOG_ERROR, "illegal inter cbp\n");
613  return -1;
614  }
615  h->cbp = cbp_tab[cbp][1];
616 
617  /* get quantizer */
618  if (h->cbp && !h->qp_fixed)
619  h->qp = (h->qp + get_se_golomb(&h->gb)) & 63;
620  for (block = 0; block < 4; block++)
621  if (h->cbp & (1 << block))
622  decode_residual_block(h, &h->gb, inter_dec, 0, h->qp,
623  h->cy + h->luma_scan[block], h->l_stride);
625 
626  return 0;
627 }
628 
629 /*****************************************************************************
630  *
631  * macroblock level
632  *
633  ****************************************************************************/
634 
635 static inline void set_mv_intra(AVSContext *h)
636 {
638  set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
640  set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
641  if (h->cur.f->pict_type != AV_PICTURE_TYPE_B)
642  h->col_type_base[h->mbidx] = I_8X8;
643 }
644 
645 static int decode_mb_i(AVSContext *h, int cbp_code)
646 {
647  GetBitContext *gb = &h->gb;
648  unsigned pred_mode_uv;
649  int block;
650  uint8_t top[18];
651  uint8_t *left = NULL;
652  uint8_t *d;
653 
654  ff_cavs_init_mb(h);
655 
656  /* get intra prediction modes from stream */
657  for (block = 0; block < 4; block++) {
658  int nA, nB, predpred;
659  int pos = scan3x3[block];
660 
661  nA = h->pred_mode_Y[pos - 1];
662  nB = h->pred_mode_Y[pos - 3];
663  predpred = FFMIN(nA, nB);
664  if (predpred == NOT_AVAIL) // if either is not available
665  predpred = INTRA_L_LP;
666  if (!get_bits1(gb)) {
667  int rem_mode = get_bits(gb, 2);
668  predpred = rem_mode + (rem_mode >= predpred);
669  }
670  h->pred_mode_Y[pos] = predpred;
671  }
672  pred_mode_uv = get_ue_golomb(gb);
673  if (pred_mode_uv > 6) {
674  av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
675  return -1;
676  }
677  ff_cavs_modify_mb_i(h, &pred_mode_uv);
678 
679  /* get coded block pattern */
680  if (h->cur.f->pict_type == AV_PICTURE_TYPE_I)
681  cbp_code = get_ue_golomb(gb);
682  if (cbp_code > 63U) {
683  av_log(h->avctx, AV_LOG_ERROR, "illegal intra cbp\n");
684  return -1;
685  }
686  h->cbp = cbp_tab[cbp_code][0];
687  if (h->cbp && !h->qp_fixed)
688  h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
689 
690  /* luma intra prediction interleaved with residual decode/transform/add */
691  for (block = 0; block < 4; block++) {
692  d = h->cy + h->luma_scan[block];
693  ff_cavs_load_intra_pred_luma(h, top, &left, block);
694  h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
695  (d, top, left, h->l_stride);
696  if (h->cbp & (1<<block))
697  decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
698  }
699 
700  /* chroma intra prediction */
702  h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx * 10],
703  h->left_border_u, h->c_stride);
704  h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx * 10],
705  h->left_border_v, h->c_stride);
706 
708  ff_cavs_filter(h, I_8X8);
709  set_mv_intra(h);
710  return 0;
711 }
712 
713 static inline void set_intra_mode_default(AVSContext *h)
714 {
715  if (h->stream_revision > 0) {
716  h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
717  h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = NOT_AVAIL;
718  } else {
719  h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
720  h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = INTRA_L_LP;
721  }
722 }
723 
724 static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
725 {
726  GetBitContext *gb = &h->gb;
727  int ref[4];
728 
729  ff_cavs_init_mb(h);
730  switch (mb_type) {
731  case P_SKIP:
733  break;
734  case P_16X16:
735  ref[0] = h->ref_flag ? 0 : get_bits1(gb);
737  break;
738  case P_16X8:
739  ref[0] = h->ref_flag ? 0 : get_bits1(gb);
740  ref[2] = h->ref_flag ? 0 : get_bits1(gb);
743  break;
744  case P_8X16:
745  ref[0] = h->ref_flag ? 0 : get_bits1(gb);
746  ref[1] = h->ref_flag ? 0 : get_bits1(gb);
749  break;
750  case P_8X8:
751  ref[0] = h->ref_flag ? 0 : get_bits1(gb);
752  ref[1] = h->ref_flag ? 0 : get_bits1(gb);
753  ref[2] = h->ref_flag ? 0 : get_bits1(gb);
754  ref[3] = h->ref_flag ? 0 : get_bits1(gb);
759  }
760  ff_cavs_inter(h, mb_type);
762  store_mvs(h);
763  if (mb_type != P_SKIP)
765  ff_cavs_filter(h, mb_type);
766  h->col_type_base[h->mbidx] = mb_type;
767 }
768 
769 static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
770 {
771  int block;
772  enum cavs_sub_mb sub_type[4];
773  int flags;
774 
775  ff_cavs_init_mb(h);
776 
777  /* reset all MVs */
779  set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
781  set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
782  switch (mb_type) {
783  case B_SKIP:
784  case B_DIRECT:
785  if (!h->col_type_base[h->mbidx]) {
786  /* intra MB at co-location, do in-plane prediction */
789  } else
790  /* direct prediction from co-located P MB, block-wise */
791  for (block = 0; block < 4; block++)
792  mv_pred_direct(h, &h->mv[mv_scan[block]],
793  &h->col_mv[h->mbidx * 4 + block]);
794  break;
795  case B_FWD_16X16:
797  break;
798  case B_SYM_16X16:
800  mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
801  break;
802  case B_BWD_16X16:
804  break;
805  case B_8X8:
806  for (block = 0; block < 4; block++)
807  sub_type[block] = get_bits(&h->gb, 2);
808  for (block = 0; block < 4; block++) {
809  switch (sub_type[block]) {
810  case B_SUB_DIRECT:
811  if (!h->col_type_base[h->mbidx]) {
812  /* intra MB at co-location, do in-plane prediction */
813  ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
814  MV_PRED_BSKIP, BLK_8X8, 1);
815  ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
816  mv_scan[block] - 3 + MV_BWD_OFFS,
817  MV_PRED_BSKIP, BLK_8X8, 0);
818  } else
819  mv_pred_direct(h, &h->mv[mv_scan[block]],
820  &h->col_mv[h->mbidx * 4 + block]);
821  break;
822  case B_SUB_FWD:
823  ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
824  MV_PRED_MEDIAN, BLK_8X8, 1);
825  break;
826  case B_SUB_SYM:
827  ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
828  MV_PRED_MEDIAN, BLK_8X8, 1);
829  mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
830  break;
831  }
832  }
833  for (block = 0; block < 4; block++) {
834  if (sub_type[block] == B_SUB_BWD)
835  ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
836  mv_scan[block] + MV_BWD_OFFS - 3,
837  MV_PRED_MEDIAN, BLK_8X8, 0);
838  }
839  break;
840  default:
841  if (mb_type <= B_SYM_16X16) {
842  av_log(h->avctx, AV_LOG_ERROR, "Invalid mb_type %d in B frame\n", mb_type);
843  return AVERROR_INVALIDDATA;
844  }
845  av_assert2(mb_type < B_8X8);
846  flags = ff_cavs_partition_flags[mb_type];
847  if (mb_type & 1) { /* 16x8 macroblock types */
848  if (flags & FWD0)
850  if (flags & SYM0)
851  mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
852  if (flags & FWD1)
854  if (flags & SYM1)
855  mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
856  if (flags & BWD0)
858  if (flags & BWD1)
860  } else { /* 8x16 macroblock types */
861  if (flags & FWD0)
863  if (flags & SYM0)
864  mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
865  if (flags & FWD1)
867  if (flags & SYM1)
868  mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
869  if (flags & BWD0)
871  if (flags & BWD1)
873  }
874  }
875  ff_cavs_inter(h, mb_type);
877  if (mb_type != B_SKIP)
879  ff_cavs_filter(h, mb_type);
880 
881  return 0;
882 }
883 
884 /*****************************************************************************
885  *
886  * slice level
887  *
888  ****************************************************************************/
889 
890 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb)
891 {
892  if (h->stc > 0xAF)
893  av_log(h->avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
894 
895  if (h->stc >= h->mb_height)
896  return -1;
897 
898  h->mby = h->stc;
899  h->mbidx = h->mby * h->mb_width;
900 
901  /* mark top macroblocks as unavailable */
902  h->flags &= ~(B_AVAIL | C_AVAIL);
903  if ((h->mby == 0) && (!h->qp_fixed)) {
904  h->qp_fixed = get_bits1(gb);
905  h->qp = get_bits(gb, 6);
906  }
907  /* inter frame or second slice can have weighting params */
908  if ((h->cur.f->pict_type != AV_PICTURE_TYPE_I) ||
909  (!h->pic_structure && h->mby >= h->mb_width / 2))
910  if (get_bits1(gb)) { //slice_weighting_flag
912  "weighted prediction not yet supported\n");
913  }
914  return 0;
915 }
916 
917 static inline int check_for_slice(AVSContext *h)
918 {
919  GetBitContext *gb = &h->gb;
920  int align;
921 
922  if (h->mbx)
923  return 0;
924  align = (-get_bits_count(gb)) & 7;
925  /* check for stuffing byte */
926  if (!align && (show_bits(gb, 8) == 0x80))
927  align = 8;
928  if ((show_bits_long(gb, 24 + align) & 0xFFFFFF) == 0x000001) {
929  skip_bits_long(gb, 24 + align);
930  h->stc = get_bits(gb, 8);
931  if (h->stc >= h->mb_height)
932  return 0;
933  decode_slice_header(h, gb);
934  return 1;
935  }
936  return 0;
937 }
938 
939 /*****************************************************************************
940  *
941  * frame level
942  *
943  ****************************************************************************/
944 
945 static int decode_pic(AVSContext *h)
946 {
947  int skip_count = -1;
948  int ret;
949  enum cavs_mb mb_type;
950 
951  av_frame_unref(h->cur.f);
952 
953  skip_bits(&h->gb, 16);//bbv_dwlay
954  if (h->stc == PIC_PB_START_CODE) {
955  h->cur.f->pict_type = get_bits(&h->gb, 2) + AV_PICTURE_TYPE_I;
956  if (h->cur.f->pict_type > AV_PICTURE_TYPE_B) {
957  av_log(h->avctx, AV_LOG_ERROR, "illegal picture type\n");
958  return -1;
959  }
960  /* make sure we have the reference frames we need */
961  if (!h->DPB[0].f->data[0] ||
962  (!h->DPB[1].f->data[0] && h->cur.f->pict_type == AV_PICTURE_TYPE_B))
963  return -1;
964  } else {
966  if (get_bits1(&h->gb))
967  skip_bits(&h->gb, 24);//time_code
968  /* old sample clips were all progressive and no low_delay,
969  bump stream revision if detected otherwise */
970  if (h->low_delay || !(show_bits(&h->gb, 9) & 1))
971  h->stream_revision = 1;
972  /* similarly test top_field_first and repeat_first_field */
973  else if (show_bits(&h->gb, 11) & 3)
974  h->stream_revision = 1;
975  if (h->stream_revision > 0)
976  skip_bits(&h->gb, 1); //marker_bit
977  }
978 
979  if ((ret = ff_get_buffer(h->avctx, h->cur.f,
981  0 : AV_GET_BUFFER_FLAG_REF)) < 0)
982  return ret;
983 
984  if (!h->edge_emu_buffer) {
985  int alloc_size = FFALIGN(FFABS(h->cur.f->linesize[0]) + 32, 32);
986  h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 24);
987  if (!h->edge_emu_buffer)
988  return AVERROR(ENOMEM);
989  }
990 
991  if ((ret = ff_cavs_init_pic(h)) < 0)
992  return ret;
993  h->cur.poc = get_bits(&h->gb, 8) * 2;
994 
995  /* get temporal distances and MV scaling factors */
996  if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
997  h->dist[0] = (h->cur.poc - h->DPB[0].poc + 512) % 512;
998  } else {
999  h->dist[0] = (h->DPB[0].poc - h->cur.poc + 512) % 512;
1000  }
1001  h->dist[1] = (h->cur.poc - h->DPB[1].poc + 512) % 512;
1002  h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
1003  h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
1004  if (h->cur.f->pict_type == AV_PICTURE_TYPE_B) {
1005  h->sym_factor = h->dist[0] * h->scale_den[1];
1006  } else {
1007  h->direct_den[0] = h->dist[0] ? 16384 / h->dist[0] : 0;
1008  h->direct_den[1] = h->dist[1] ? 16384 / h->dist[1] : 0;
1009  }
1010 
1011  if (h->low_delay)
1012  get_ue_golomb(&h->gb); //bbv_check_times
1013  h->progressive = get_bits1(&h->gb);
1014  h->pic_structure = 1;
1015  if (!h->progressive)
1016  h->pic_structure = get_bits1(&h->gb);
1017  if (!h->pic_structure && h->stc == PIC_PB_START_CODE)
1018  skip_bits1(&h->gb); //advanced_pred_mode_disable
1019  skip_bits1(&h->gb); //top_field_first
1020  skip_bits1(&h->gb); //repeat_first_field
1021  h->qp_fixed = get_bits1(&h->gb);
1022  h->qp = get_bits(&h->gb, 6);
1023  if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1024  if (!h->progressive && !h->pic_structure)
1025  skip_bits1(&h->gb);//what is this?
1026  skip_bits(&h->gb, 4); //reserved bits
1027  } else {
1028  if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
1029  h->ref_flag = get_bits1(&h->gb);
1030  skip_bits(&h->gb, 4); //reserved bits
1031  h->skip_mode_flag = get_bits1(&h->gb);
1032  }
1033  h->loop_filter_disable = get_bits1(&h->gb);
1034  if (!h->loop_filter_disable && get_bits1(&h->gb)) {
1035  h->alpha_offset = get_se_golomb(&h->gb);
1036  h->beta_offset = get_se_golomb(&h->gb);
1037  } else {
1038  h->alpha_offset = h->beta_offset = 0;
1039  }
1040  if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1041  do {
1042  check_for_slice(h);
1043  decode_mb_i(h, 0);
1044  } while (ff_cavs_next_mb(h));
1045  } else if (h->cur.f->pict_type == AV_PICTURE_TYPE_P) {
1046  do {
1047  if (check_for_slice(h))
1048  skip_count = -1;
1049  if (h->skip_mode_flag && (skip_count < 0))
1050  skip_count = get_ue_golomb(&h->gb);
1051  if (h->skip_mode_flag && skip_count--) {
1052  decode_mb_p(h, P_SKIP);
1053  } else {
1054  mb_type = get_ue_golomb(&h->gb) + P_SKIP + h->skip_mode_flag;
1055  if (mb_type > P_8X8)
1056  decode_mb_i(h, mb_type - P_8X8 - 1);
1057  else
1058  decode_mb_p(h, mb_type);
1059  }
1060  } while (ff_cavs_next_mb(h));
1061  } else { /* AV_PICTURE_TYPE_B */
1062  do {
1063  if (check_for_slice(h))
1064  skip_count = -1;
1065  if (h->skip_mode_flag && (skip_count < 0))
1066  skip_count = get_ue_golomb(&h->gb);
1067  if (h->skip_mode_flag && skip_count--) {
1068  decode_mb_b(h, B_SKIP);
1069  } else {
1070  mb_type = get_ue_golomb(&h->gb) + B_SKIP + h->skip_mode_flag;
1071  if (mb_type > B_8X8)
1072  decode_mb_i(h, mb_type - B_8X8 - 1);
1073  else
1074  decode_mb_b(h, mb_type);
1075  }
1076  } while (ff_cavs_next_mb(h));
1077  }
1078  if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1079  av_frame_unref(h->DPB[1].f);
1080  FFSWAP(AVSFrame, h->cur, h->DPB[1]);
1081  FFSWAP(AVSFrame, h->DPB[0], h->DPB[1]);
1082  }
1083  return 0;
1084 }
1085 
1086 /*****************************************************************************
1087  *
1088  * headers and interface
1089  *
1090  ****************************************************************************/
1091 
1093 {
1094  int frame_rate_code;
1095  int width, height;
1096 
1097  h->profile = get_bits(&h->gb, 8);
1098  h->level = get_bits(&h->gb, 8);
1099  skip_bits1(&h->gb); //progressive sequence
1100 
1101  width = get_bits(&h->gb, 14);
1102  height = get_bits(&h->gb, 14);
1103  if ((h->width || h->height) && (h->width != width || h->height != height)) {
1105  "Width/height changing in CAVS");
1106  return AVERROR_PATCHWELCOME;
1107  }
1108  if (width <= 0 || height <= 0) {
1109  av_log(h->avctx, AV_LOG_ERROR, "Dimensions invalid\n");
1110  return AVERROR_INVALIDDATA;
1111  }
1112  h->width = width;
1113  h->height = height;
1114 
1115  skip_bits(&h->gb, 2); //chroma format
1116  skip_bits(&h->gb, 3); //sample_precision
1117  h->aspect_ratio = get_bits(&h->gb, 4);
1118  frame_rate_code = get_bits(&h->gb, 4);
1119  skip_bits(&h->gb, 18); //bit_rate_lower
1120  skip_bits1(&h->gb); //marker_bit
1121  skip_bits(&h->gb, 12); //bit_rate_upper
1122  h->low_delay = get_bits1(&h->gb);
1123  h->mb_width = (h->width + 15) >> 4;
1124  h->mb_height = (h->height + 15) >> 4;
1125  h->avctx->time_base.den = ff_mpeg12_frame_rate_tab[frame_rate_code].num;
1126  h->avctx->time_base.num = ff_mpeg12_frame_rate_tab[frame_rate_code].den;
1127  h->avctx->width = h->width;
1128  h->avctx->height = h->height;
1129  if (!h->top_qp)
1131  return 0;
1132 }
1133 
1134 static void cavs_flush(AVCodecContext * avctx)
1135 {
1136  AVSContext *h = avctx->priv_data;
1137  h->got_keyframe = 0;
1138 }
1139 
1140 static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1141  AVPacket *avpkt)
1142 {
1143  AVSContext *h = avctx->priv_data;
1144  const uint8_t *buf = avpkt->data;
1145  int buf_size = avpkt->size;
1146  uint32_t stc = -1;
1147  int input_size, ret;
1148  const uint8_t *buf_end;
1149  const uint8_t *buf_ptr;
1150 
1151  if (buf_size == 0) {
1152  if (!h->low_delay && h->DPB[0].f->data[0]) {
1153  *got_frame = 1;
1154  av_frame_move_ref(data, h->DPB[0].f);
1155  }
1156  return 0;
1157  }
1158 
1159  buf_ptr = buf;
1160  buf_end = buf + buf_size;
1161  for(;;) {
1162  buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
1163  if ((stc & 0xFFFFFE00) || buf_ptr == buf_end)
1164  return FFMAX(0, buf_ptr - buf);
1165  input_size = (buf_end - buf_ptr) * 8;
1166  switch (stc) {
1167  case CAVS_START_CODE:
1168  init_get_bits(&h->gb, buf_ptr, input_size);
1169  decode_seq_header(h);
1170  break;
1171  case PIC_I_START_CODE:
1172  if (!h->got_keyframe) {
1173  av_frame_unref(h->DPB[0].f);
1174  av_frame_unref(h->DPB[1].f);
1175  h->got_keyframe = 1;
1176  }
1177  case PIC_PB_START_CODE:
1178  *got_frame = 0;
1179  if (!h->got_keyframe)
1180  break;
1181  if(!h->top_qp)
1182  break;
1183  init_get_bits(&h->gb, buf_ptr, input_size);
1184  h->stc = stc;
1185  if (decode_pic(h))
1186  break;
1187  *got_frame = 1;
1188  if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1189  if (h->DPB[1].f->data[0]) {
1190  if ((ret = av_frame_ref(data, h->DPB[1].f)) < 0)
1191  return ret;
1192  } else {
1193  *got_frame = 0;
1194  }
1195  } else {
1196  av_frame_move_ref(data, h->cur.f);
1197  }
1198  break;
1199  case EXT_START_CODE:
1200  //mpeg_decode_extension(avctx, buf_ptr, input_size);
1201  break;
1202  case USER_START_CODE:
1203  //mpeg_decode_user_data(avctx, buf_ptr, input_size);
1204  break;
1205  default:
1206  if (stc <= SLICE_MAX_START_CODE) {
1207  init_get_bits(&h->gb, buf_ptr, input_size);
1208  decode_slice_header(h, &h->gb);
1209  }
1210  break;
1211  }
1212  }
1213 }
1214 
1216  .name = "cavs",
1217  .type = AVMEDIA_TYPE_VIDEO,
1218  .id = AV_CODEC_ID_CAVS,
1219  .priv_data_size = sizeof(AVSContext),
1220  .init = ff_cavs_init,
1221  .close = ff_cavs_end,
1223  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1224  .flush = cavs_flush,
1225  .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
1226 };
void(* intra_pred_c[7])(uint8_t *d, uint8_t *top, uint8_t *left, int stride)
Definition: cavs.h:228
uint8_t * top_border_v
Definition: cavs.h:222
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:352
int pic_structure
Definition: cavs.h:179
uint8_t * top_border_u
Definition: cavs.h:222
void ff_cavs_modify_mb_i(AVSContext *h, int *pred_mode_uv)
Definition: cavs.c:352
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
static const struct dec_2dvlc chroma_dec[5]
Definition: cavsdec.c:358
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Definition: cavs.h:72
#define SLICE_MAX_START_CODE
Definition: cavs.h:31
int got_keyframe
Definition: cavs.h:238
AVCodecContext * avctx
Definition: cavs.h:163
int inc_limit
Definition: cavs.h:153
static int shift(int a, int b)
Definition: sonic.c:86
FIXME Range Coding of cr are ref
Definition: snow.txt:367
uint8_t * edge_emu_buffer
Definition: cavs.h:236
int direct_den[2]
for scaling in direct B block
Definition: cavs.h:233
int cbp
Definition: cavs.h:217
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:175
const cavs_vector ff_cavs_intra_mv
mark block as using intra prediction
Definition: cavsdata.c:62
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
Definition: cavs.h:61
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:198
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static const uint8_t dequant_shift[64]
Definition: cavsdec.c:62
av_cold int ff_cavs_end(AVCodecContext *avctx)
Definition: cavs.c:771
int mbidx
macroblock coordinates
Definition: cavs.h:184
#define BWD1
Definition: cavs.h:51
static const uint8_t scan3x3[4]
Definition: cavsdec.c:53
cavs_vector * col_mv
Definition: cavs.h:205
int poc
Definition: cavs.h:159
Definition: cavs.h:68
int num
numerator
Definition: rational.h:44
#define SYM1
Definition: cavs.h:53
void ff_cavs_init_mb(AVSContext *h)
initialise predictors for motion vectors and intra prediction
Definition: cavs.c:592
mpegvideo header.
Definition: cavs.h:65
int qp
Definition: cavs.h:215
uint8_t permutated[64]
Definition: dsputil.h:116
uint8_t run
Definition: svq3.c:136
int loop_filter_disable
Definition: cavs.h:181
void(* clear_block)(int16_t *block)
Definition: dsputil.h:145
int stride
Definition: mace.c:144
#define FFALIGN(x, a)
Definition: common.h:63
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
set threshold d
static int decode_seq_header(AVSContext *h)
Definition: cavsdec.c:1092
cavs_mb
Definition: cavs.h:60
#define MV_BWD_OFFS
Definition: cavs.h:57
int mbx
Definition: cavs.h:184
struct AVSContext AVSContext
static int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf, int16_t *dst, int mul, int shift, int coeff_num)
Definition: cavsdec.c:524
ScanTable scantable
Definition: cavs.h:218
#define FWD1
Definition: cavs.h:49
static void set_mv_intra(AVSContext *h)
Definition: cavsdec.c:635
uint8_t
int ref_flag
Definition: cavs.h:183
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
window constants for m
uint8_t * top_qp
Definition: cavs.h:189
int8_t rltab[59][3]
Definition: cavs.h:150
static const uint8_t mv_scan[4]
Definition: cavsdec.c:37
cavs_vector
Definition: cavs.h:147
cavs_vector mv[2 *4 *3]
mv motion vector cache 0: D3 B2 B3 C2 4: A1 X0 X1 - 8: A3 X2 X3 -
Definition: cavs.h:203
int width
Definition: cavs.h:176
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
#define PIC_I_START_CODE
Definition: cavs.h:35
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everythnig contained in src to dst and reset src.
Definition: frame.c:352
uint8_t * data
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:193
int8_t golomb_order
Definition: cavs.h:152
bitstream reader API header.
static void cavs_flush(AVCodecContext *avctx)
Definition: cavsdec.c:1134
void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type)
in-loop deblocking filter for a single macroblock
Definition: cavs.c:106
int dist[2]
temporal distances from current frame to ref frames
Definition: cavs.h:171
int mby
Definition: cavs.h:184
GetBitContext gb
Definition: cavs.h:168
uint8_t * cy
Definition: cavs.h:187
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
#define U(x)
uint8_t * cu
Definition: cavs.h:187
Definition: cavs.h:66
int scale_den[2]
for scaling neighbouring MVs
Definition: cavs.h:234
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
Definition: golomb.h:53
static int decode_pic(AVSContext *h)
Definition: cavsdec.c:945
int progressive
Definition: cavs.h:178
AVSFrame cur
currently decoded frame
Definition: cavs.h:169
static const uint16_t mask[17]
Definition: lzw.c:37
int ff_cavs_next_mb(AVSContext *h)
save predictors for later macroblocks and increase macroblock address
Definition: cavs.c:632
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: cavs.h:77
void ff_cavs_inter(AVSContext *h, enum cavs_mb mb_type)
Definition: cavs.c:464
CAVSDSPContext cdsp
Definition: cavs.h:167
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
const char * r
Definition: vf_curves.c:94
static int decode_slice_header(AVSContext *h, GetBitContext *gb)
Definition: cavsdec.c:890
static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
Definition: cavsdec.c:724
static const uint8_t cbp_tab[64][2]
Definition: cavsdec.c:42
#define C_AVAIL
Definition: cavs.h:40
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
void ff_cavs_load_intra_pred_luma(AVSContext *h, uint8_t *top, uint8_t **left, int block)
Definition: cavs.c:178
const char * name
Name of the codec implementation.
static av_always_inline av_const double round(double x)
Definition: libm.h:162
int c_stride
Definition: cavs.h:213
#define FFMAX(a, b)
Definition: common.h:56
external API header
Definition: cavs.h:64
int size
int skip_mode_flag
select between skip_count or one skip_flag per MB
Definition: cavs.h:180
AVSFrame DPB[2]
reference frames
Definition: cavs.h:170
#define B_AVAIL
Definition: cavs.h:39
#define EOB
Definition: cavsdec.c:84
static const uint16_t dequant_mul[64]
Definition: cavsdec.c:73
#define SYM0
Definition: cavs.h:52
uint8_t * cv
current MB sample pointers
Definition: cavs.h:187
void ff_cavs_load_intra_pred_chroma(AVSContext *h)
Definition: cavs.c:229
static int check_for_slice(AVSContext *h)
Definition: cavsdec.c:917
int stream_revision
0 for samples from 2006, 1 for rm52j encoder
Definition: cavs.h:177
int l_stride
Definition: cavs.h:213
Definition: cavs.h:115
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
#define NOT_AVAIL
Definition: cavs.h:42
av_cold int ff_cavs_init(AVCodecContext *avctx)
Definition: cavs.c:727
#define FFMIN(a, b)
Definition: common.h:58
AVCodec ff_cavs_decoder
Definition: cavsdec.c:1215
cavs_sub_mb
Definition: cavs.h:75
uint8_t left_border_u[10]
Definition: cavs.h:223
ret
Definition: avfilter.c:821
int width
picture width / height.
const uint8_t ff_cavs_partition_flags[30]
Definition: cavsdata.c:24
int16_t * block
Definition: cavs.h:239
void ff_cavs_init_top_lines(AVSContext *h)
some predictions require data from the top-neighbouring macroblock.
Definition: cavs.c:711
#define ESCAPE_CODE
Definition: cavs.h:46
void(* intra_pred_l[8])(uint8_t *d, uint8_t *top, uint8_t *left, int stride)
Definition: cavs.h:227
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:255
const cavs_vector ff_cavs_dir_mv
mark block as "no prediction from this direction" e.g.
Definition: cavsdata.c:59
#define FFABS(a)
Definition: common.h:53
#define EXT_START_CODE
Definition: cavs.h:32
Definition: cavs.h:79
DSPContext dsp
Definition: cavs.h:164
uint8_t left_border_v[10]
Definition: cavs.h:223
int level
Definition: cavs.h:173
FIXME Range Coding of cr are level
Definition: snow.txt:367
void ff_cavs_mv(AVSContext *h, enum cavs_mv_loc nP, enum cavs_mv_loc nC, enum cavs_mv_pred mode, enum cavs_block size, int ref)
Definition: cavs.c:539
static void flush(AVCodecContext *avctx)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static void decode_residual_chroma(AVSContext *h)
Definition: cavsdec.c:595
const AVS_VideoInfo int align
Definition: avisynth_c.h:695
Definition: cavs.h:114
NULL
Definition: eval.c:55
int8_t max_run
Definition: cavs.h:154
static int width
Definition: tests/utils.c:158
static void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw, cavs_vector *col_mv)
Definition: cavsdec.c:472
AVS_Value src
Definition: avisynth_c.h:523
#define BWD0
Definition: cavs.h:50
static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
Definition: cavsdec.c:769
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
int sym_factor
for scaling in symmetrical B block
Definition: cavs.h:232
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
#define USER_START_CODE
Definition: cavs.h:33
int pred_mode_Y[3 *3]
luma pred mode cache 0: – B2 B3 3: A1 X0 X1 6: A3 X2 X3
Definition: cavs.h:211
MPEG1/2 tables.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
cavs_block
Definition: cavs.h:112
Definition: cavs.h:78
void * buf
Definition: avisynth_c.h:594
int * top_pred_Y
Definition: cavs.h:212
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:298
static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: cavsdec.c:1140
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
#define CAVS_START_CODE
Definition: cavs.h:34
static const struct dec_2dvlc intra_dec[7]
Definition: cavsdec.c:86
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
int8_t level_add[27]
Definition: cavs.h:151
int height
Definition: cavs.h:176
int alpha_offset
Definition: cavs.h:182
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:330
Definition: cavs.h:63
static void store_mvs(AVSContext *h)
Definition: cavsdec.c:464
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
int profile
Definition: cavs.h:173
#define PIC_PB_START_CODE
Definition: cavs.h:36
void(* cavs_idct8_add)(uint8_t *dst, int16_t *block, int stride)
Definition: cavsdsp.h:35
int stc
last start code
Definition: cavs.h:186
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int flags
Definition: cpu.c:23
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by an given frame.
Definition: frame.c:228
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
common internal api header.
int qp_fixed
Definition: cavs.h:216
Definition: cavs.h:157
static void mv_pred_sym(AVSContext *h, cavs_vector *src, enum cavs_block size)
Definition: cavsdec.c:491
static const uint8_t cavs_chroma_qp[64]
Definition: cavsdec.c:55
Bi-dir predicted.
Definition: avutil.h:218
int flags
availability flags of neighbouring macroblocks
Definition: cavs.h:185
int den
denominator
Definition: rational.h:45
Definition: cavs.h:62
int luma_scan[4]
Definition: cavs.h:214
static int get_ue_code(GetBitContext *gb, int order)
kth-order exponential golomb code
Definition: cavsdec.c:511
int mb_height
Definition: cavs.h:175
int beta_offset
Definition: cavs.h:182
int ff_cavs_init_pic(AVSContext *h)
Definition: cavs.c:676
static const struct dec_2dvlc inter_dec[7]
Definition: cavsdec.c:222
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
static int decode_mb_i(AVSContext *h, int cbp_code)
Definition: cavsdec.c:645
static int decode_residual_block(AVSContext *h, GetBitContext *gb, const struct dec_2dvlc *r, int esc_golomb_order, int qp, uint8_t *dst, int stride)
decode coefficients from one 8x8 block, dequantize, inverse transform and add them to sample block ...
Definition: cavsdec.c:554
Definition: cavs.h:116
#define FWD0
Definition: cavs.h:48
static void set_mvs(cavs_vector *mv, enum cavs_block size)
Definition: cavs.h:246
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
Definition: cavs.h:67
int aspect_ratio
Definition: cavs.h:174
static int decode_residual_inter(AVSContext *h)
Definition: cavsdec.c:605
#define FFSWAP(type, a, b)
Definition: common.h:61
static void set_intra_mode_default(AVSContext *h)
Definition: cavsdec.c:713
exp golomb vlc stuff
This structure stores compressed data.
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
const AVRational ff_mpeg12_frame_rate_tab[16]
Definition: mpeg12data.c:308
int mb_width
Definition: cavs.h:175
AVFrame * f
Definition: cavs.h:158
Predicted.
Definition: avutil.h:217
int low_delay
Definition: cavs.h:172
uint8_t * col_type_base
Definition: cavs.h:229