FFmpeg  4.4.9
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 
36 static const uint8_t mv_scan[4] = {
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 dequant_shift[64] = {
55  14, 14, 14, 14, 14, 14, 14, 14,
56  13, 13, 13, 13, 13, 13, 13, 13,
57  13, 12, 12, 12, 12, 12, 12, 12,
58  11, 11, 11, 11, 11, 11, 11, 11,
59  11, 10, 10, 10, 10, 10, 10, 10,
60  10, 9, 9, 9, 9, 9, 9, 9,
61  9, 8, 8, 8, 8, 8, 8, 8,
62  7, 7, 7, 7, 7, 7, 7, 7
63 };
64 
65 static const uint16_t dequant_mul[64] = {
66  32768, 36061, 38968, 42495, 46341, 50535, 55437, 60424,
67  32932, 35734, 38968, 42495, 46177, 50535, 55109, 59933,
68  65535, 35734, 38968, 42577, 46341, 50617, 55027, 60097,
69  32809, 35734, 38968, 42454, 46382, 50576, 55109, 60056,
70  65535, 35734, 38968, 42495, 46320, 50515, 55109, 60076,
71  65535, 35744, 38968, 42495, 46341, 50535, 55099, 60087,
72  65535, 35734, 38973, 42500, 46341, 50535, 55109, 60097,
73  32771, 35734, 38965, 42497, 46341, 50535, 55109, 60099
74 };
75 
76 #define EOB 0, 0, 0
77 
78 static const struct dec_2dvlc intra_dec[7] = {
79  {
80  { //level / run / table_inc
81  { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
82  { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
83  { 1, 7, 1 }, { -1, 7, 1 }, { 1, 8, 1 }, { -1, 8, 1 }, { 1, 9, 1 }, { -1, 9, 1 },
84  { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 }, { 2, 1, 2 }, { -2, 1, 2 },
85  { 1, 12, 1 }, { -1, 12, 1 }, { 1, 13, 1 }, { -1, 13, 1 }, { 1, 14, 1 }, { -1, 14, 1 },
86  { 1, 15, 1 }, { -1, 15, 1 }, { 2, 2, 2 }, { -2, 2, 2 }, { 1, 16, 1 }, { -1, 16, 1 },
87  { 1, 17, 1 }, { -1, 17, 1 }, { 3, 1, 3 }, { -3, 1, 3 }, { 1, 18, 1 }, { -1, 18, 1 },
88  { 1, 19, 1 }, { -1, 19, 1 }, { 2, 3, 2 }, { -2, 3, 2 }, { 1, 20, 1 }, { -1, 20, 1 },
89  { 1, 21, 1 }, { -1, 21, 1 }, { 2, 4, 2 }, { -2, 4, 2 }, { 1, 22, 1 }, { -1, 22, 1 },
90  { 2, 5, 2 }, { -2, 5, 2 }, { 1, 23, 1 }, { -1, 23, 1 }, { EOB }
91  },
92  //level_add
93  { 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 },
94  2, //golomb_order
95  0, //inc_limit
96  23, //max_run
97  },
98  {
99  { //level / run
100  { 1, 1, 0 }, { -1, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 1 }, { -2, 1, 1 },
101  { 1, 3, 0 }, { -1, 3, 0 }, { EOB }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 },
102  { -1, 5, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 3, 1, 2 }, { -3, 1, 2 }, { 2, 2, 1 },
103  { -2, 2, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 1, 9, 0 },
104  { -1, 9, 0 }, { 2, 3, 1 }, { -2, 3, 1 }, { 4, 1, 2 }, { -4, 1, 2 }, { 1, 10, 0 },
105  { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 2, 4, 1 }, { -2, 4, 1 }, { 3, 2, 2 },
106  { -3, 2, 2 }, { 1, 12, 0 }, { -1, 12, 0 }, { 2, 5, 1 }, { -2, 5, 1 }, { 5, 1, 3 },
107  { -5, 1, 3 }, { 1, 13, 0 }, { -1, 13, 0 }, { 2, 6, 1 }, { -2, 6, 1 }, { 1, 14, 0 },
108  { -1, 14, 0 }, { 2, 7, 1 }, { -2, 7, 1 }, { 2, 8, 1 }, { -2, 8, 1 }, { 3, 3, 2 },
109  { -3, 3, 2 }, { 6, 1, 3 }, { -6, 1, 3 }, { 1, 15, 0 }, { -1, 15, 0 }
110  },
111  //level_add
112  { 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 },
113  2, //golomb_order
114  1, //inc_limit
115  15, //max_run
116  },
117  {
118  { //level / run
119  { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 },
120  { 3, 1, 1 }, { -3, 1, 1 }, { EOB }, { 1, 3, 0 }, { -1, 3, 0 }, { 2, 2, 0 },
121  { -2, 2, 0 }, { 4, 1, 1 }, { -4, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 2 },
122  { -5, 1, 2 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 3, 0 },
123  { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 2 }, { -6, 1, 2 }, { 2, 4, 0 },
124  { -2, 4, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 7, 1, 2 },
125  { -7, 1, 2 }, { 3, 3, 1 }, { -3, 3, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 8, 0 },
126  { -1, 8, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 8, 1, 3 }, { -8, 1, 3 }, { 1, 9, 0 },
127  { -1, 9, 0 }, { 5, 2, 2 }, { -5, 2, 2 }, { 3, 4, 1 }, { -3, 4, 1 }, { 2, 7, 0 },
128  { -2, 7, 0 }, { 9, 1, 3 }, { -9, 1, 3 }, { 1, 10, 0 }, { -1, 10, 0 }
129  },
130  //level_add
131  { 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 },
132  2, //golomb_order
133  2, //inc_limit
134  10, //max_run
135  },
136  {
137  { //level / run
138  { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 }, { -3, 1, 0 },
139  { 1, 2, 0 }, { -1, 2, 0 }, { EOB }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 1 },
140  { -5, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 6, 1, 1 },
141  { -6, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 1, 4, 0 },
142  { -1, 4, 0 }, { 8, 1, 2 }, { -8, 1, 2 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 2, 0 },
143  { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 9, 1, 2 }, { -9, 1, 2 }, { 5, 2, 1 },
144  { -5, 2, 1 }, { 2, 4, 0 }, { -2, 4, 0 }, { 10, 1, 2 }, {-10, 1, 2 }, { 3, 3, 0 },
145  { -3, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 11, 1, 3 }, {-11, 1, 3 }, { 6, 2, 1 },
146  { -6, 2, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 2, 5, 0 }, { -2, 5, 0 }, { 3, 4, 0 },
147  { -3, 4, 0 }, { 12, 1, 3 }, {-12, 1, 3 }, { 4, 3, 0 }, { -4, 3, 0 }
148  },
149  //level_add
150  { 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 },
151  2, //golomb_order
152  4, //inc_limit
153  7, //max_run
154  },
155  {
156  { //level / run
157  { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 }, { -3, 1, 0 },
158  { EOB }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
159  { -6, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 1 },
160  { -8, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 9, 1, 1 }, { -9, 1, 1 }, { 10, 1, 1 },
161  {-10, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 11, 1, 2 },
162  {-11, 1, 2 }, { 4, 2, 0 }, { -4, 2, 0 }, { 12, 1, 2 }, {-12, 1, 2 }, { 13, 1, 2 },
163  {-13, 1, 2 }, { 5, 2, 0 }, { -5, 2, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 2, 3, 0 },
164  { -2, 3, 0 }, { 14, 1, 2 }, {-14, 1, 2 }, { 6, 2, 0 }, { -6, 2, 0 }, { 15, 1, 2 },
165  {-15, 1, 2 }, { 16, 1, 2 }, {-16, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 1, 5, 0 },
166  { -1, 5, 0 }, { 7, 2, 0 }, { -7, 2, 0 }, { 17, 1, 2 }, {-17, 1, 2 }
167  },
168  //level_add
169  { 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 },
170  2, //golomb_order
171  7, //inc_limit
172  5, //max_run
173  },
174  {
175  { //level / run
176  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
177  { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
178  { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 9, 1, 0 },
179  { -9, 1, 0 }, { 10, 1, 0 }, {-10, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 11, 1, 1 },
180  {-11, 1, 1 }, { 12, 1, 1 }, {-12, 1, 1 }, { 13, 1, 1 }, {-13, 1, 1 }, { 2, 2, 0 },
181  { -2, 2, 0 }, { 14, 1, 1 }, {-14, 1, 1 }, { 15, 1, 1 }, {-15, 1, 1 }, { 3, 2, 0 },
182  { -3, 2, 0 }, { 16, 1, 1 }, {-16, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 17, 1, 1 },
183  {-17, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 18, 1, 1 }, {-18, 1, 1 }, { 5, 2, 0 },
184  { -5, 2, 0 }, { 19, 1, 1 }, {-19, 1, 1 }, { 20, 1, 1 }, {-20, 1, 1 }, { 6, 2, 0 },
185  { -6, 2, 0 }, { 21, 1, 1 }, {-21, 1, 1 }, { 2, 3, 0 }, { -2, 3, 0 }
186  },
187  //level_add
188  { 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 },
189  2, //golomb_order
190  10, //inc_limit
191  3, //max_run
192  },
193  {
194  { //level / run
195  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
196  { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
197  { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 9, 1, 0 },
198  { -9, 1, 0 }, { 10, 1, 0 }, {-10, 1, 0 }, { 11, 1, 0 }, {-11, 1, 0 }, { 12, 1, 0 },
199  {-12, 1, 0 }, { 13, 1, 0 }, {-13, 1, 0 }, { 14, 1, 0 }, {-14, 1, 0 }, { 15, 1, 0 },
200  {-15, 1, 0 }, { 16, 1, 0 }, {-16, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 17, 1, 0 },
201  {-17, 1, 0 }, { 18, 1, 0 }, {-18, 1, 0 }, { 19, 1, 0 }, {-19, 1, 0 }, { 20, 1, 0 },
202  {-20, 1, 0 }, { 21, 1, 0 }, {-21, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 22, 1, 0 },
203  {-22, 1, 0 }, { 23, 1, 0 }, {-23, 1, 0 }, { 24, 1, 0 }, {-24, 1, 0 }, { 25, 1, 0 },
204  {-25, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 26, 1, 0 }, {-26, 1, 0 }
205  },
206  //level_add
207  { 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 },
208  2, //golomb_order
209  INT_MAX, //inc_limit
210  2, //max_run
211  }
212 };
213 
214 static const struct dec_2dvlc inter_dec[7] = {
215  {
216  { //level / run
217  { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
218  { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
219  { 1, 7, 1 }, { -1, 7, 1 }, { 1, 8, 1 }, { -1, 8, 1 }, { 1, 9, 1 }, { -1, 9, 1 },
220  { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 }, { 1, 12, 1 }, { -1, 12, 1 },
221  { 1, 13, 1 }, { -1, 13, 1 }, { 2, 1, 2 }, { -2, 1, 2 }, { 1, 14, 1 }, { -1, 14, 1 },
222  { 1, 15, 1 }, { -1, 15, 1 }, { 1, 16, 1 }, { -1, 16, 1 }, { 1, 17, 1 }, { -1, 17, 1 },
223  { 1, 18, 1 }, { -1, 18, 1 }, { 1, 19, 1 }, { -1, 19, 1 }, { 3, 1, 3 }, { -3, 1, 3 },
224  { 1, 20, 1 }, { -1, 20, 1 }, { 1, 21, 1 }, { -1, 21, 1 }, { 2, 2, 2 }, { -2, 2, 2 },
225  { 1, 22, 1 }, { -1, 22, 1 }, { 1, 23, 1 }, { -1, 23, 1 }, { 1, 24, 1 }, { -1, 24, 1 },
226  { 1, 25, 1 }, { -1, 25, 1 }, { 1, 26, 1 }, { -1, 26, 1 }, { EOB }
227  },
228  //level_add
229  { 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 },
230  3, //golomb_order
231  0, //inc_limit
232  26 //max_run
233  },
234  {
235  { //level / run
236  { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 1, 2, 0 }, { -1, 2, 0 }, { 1, 3, 0 },
237  { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 1, 6, 0 },
238  { -1, 6, 0 }, { 2, 1, 1 }, { -2, 1, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 1, 8, 0 },
239  { -1, 8, 0 }, { 1, 9, 0 }, { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 2, 2, 1 },
240  { -2, 2, 1 }, { 1, 11, 0 }, { -1, 11, 0 }, { 1, 12, 0 }, { -1, 12, 0 }, { 3, 1, 2 },
241  { -3, 1, 2 }, { 1, 13, 0 }, { -1, 13, 0 }, { 1, 14, 0 }, { -1, 14, 0 }, { 2, 3, 1 },
242  { -2, 3, 1 }, { 1, 15, 0 }, { -1, 15, 0 }, { 2, 4, 1 }, { -2, 4, 1 }, { 1, 16, 0 },
243  { -1, 16, 0 }, { 2, 5, 1 }, { -2, 5, 1 }, { 1, 17, 0 }, { -1, 17, 0 }, { 4, 1, 3 },
244  { -4, 1, 3 }, { 2, 6, 1 }, { -2, 6, 1 }, { 1, 18, 0 }, { -1, 18, 0 }, { 1, 19, 0 },
245  { -1, 19, 0 }, { 2, 7, 1 }, { -2, 7, 1 }, { 3, 2, 2 }, { -3, 2, 2 }
246  },
247  //level_add
248  { 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 },
249  2, //golomb_order
250  1, //inc_limit
251  19 //max_run
252  },
253  {
254  { //level / run
255  { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 0 },
256  { -2, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 3, 1, 1 },
257  { -3, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 1, 6, 0 },
258  { -1, 6, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 1, 2 },
259  { -4, 1, 2 }, { 1, 8, 0 }, { -1, 8, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 4, 0 },
260  { -2, 4, 0 }, { 1, 9, 0 }, { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 5, 1, 2 },
261  { -5, 1, 2 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 2, 6, 0 },
262  { -2, 6, 0 }, { 1, 12, 0 }, { -1, 12, 0 }, { 3, 3, 1 }, { -3, 3, 1 }, { 6, 1, 2 },
263  { -6, 1, 2 }, { 4, 2, 2 }, { -4, 2, 2 }, { 1, 13, 0 }, { -1, 13, 0 }, { 2, 7, 0 },
264  { -2, 7, 0 }, { 3, 4, 1 }, { -3, 4, 1 }, { 1, 14, 0 }, { -1, 14, 0 }
265  },
266  //level_add
267  { 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 },
268  2, //golomb_order
269  2, //inc_limit
270  14 //max_run
271  },
272  {
273  { //level / run
274  { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 },
275  { -1, 2, 0 }, { 3, 1, 0 }, { -3, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 2, 2, 0 },
276  { -2, 2, 0 }, { 4, 1, 1 }, { -4, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 1 },
277  { -5, 1, 1 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 2, 3, 0 },
278  { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 1 }, { -6, 1, 1 }, { 2, 4, 0 },
279  { -2, 4, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 7, 1, 2 },
280  { -7, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 2, 5, 0 },
281  { -2, 5, 0 }, { 8, 1, 2 }, { -8, 1, 2 }, { 1, 9, 0 }, { -1, 9, 0 }, { 3, 4, 0 },
282  { -3, 4, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 5, 2, 1 }, { -5, 2, 1 }, { 1, 10, 0 },
283  { -1, 10, 0 }, { 9, 1, 2 }, { -9, 1, 2 }, { 4, 3, 1 }, { -4, 3, 1 }
284  },
285  //level_add
286  { 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 },
287  2, //golomb_order
288  3, //inc_limit
289  10 //max_run
290  },
291  {
292  { //level / run
293  { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
294  { -3, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 },
295  { -5, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 6, 1, 0 },
296  { -6, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 1, 4, 0 },
297  { -1, 4, 0 }, { 8, 1, 1 }, { -8, 1, 1 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 2, 0 },
298  { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 9, 1, 1 }, { -9, 1, 1 }, { 5, 2, 0 },
299  { -5, 2, 0 }, { 2, 4, 0 }, { -2, 4, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 10, 1, 2 },
300  {-10, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 11, 1, 2 }, {-11, 1, 2 }, { 1, 7, 0 },
301  { -1, 7, 0 }, { 6, 2, 0 }, { -6, 2, 0 }, { 3, 4, 0 }, { -3, 4, 0 }, { 2, 5, 0 },
302  { -2, 5, 0 }, { 12, 1, 2 }, {-12, 1, 2 }, { 4, 3, 0 }, { -4, 3, 0 }
303  },
304  //level_add
305  { 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 },
306  2, //golomb_order
307  6, //inc_limit
308  7 //max_run
309  },
310  {
311  { //level / run
312  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
313  { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 1, 2, 0 },
314  { -1, 2, 0 }, { 6, 1, 0 }, { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 },
315  { -8, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 1, 3, 0 },
316  { -1, 3, 0 }, { 10, 1, 1 }, { -10, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 11, 1, 1 },
317  { -11, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 12, 1, 1 }, { -12, 1, 1 }, { 1, 4, 0 },
318  { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 13, 1, 1 }, { -13, 1, 1 }, { 5, 2, 0 },
319  { -5, 2, 0 }, { 14, 1, 1 }, { -14, 1, 1 }, { 6, 2, 0 }, { -6, 2, 0 }, { 1, 5, 0 },
320  { -1, 5, 0 }, { 15, 1, 1 }, { -15, 1, 1 }, { 3, 3, 0 }, { -3, 3, 0 }, { 16, 1, 1 },
321  { -16, 1, 1 }, { 2, 4, 0 }, { -2, 4, 0 }, { 7, 2, 0 }, { -7, 2, 0 }
322  },
323  //level_add
324  { 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 },
325  2, //golomb_order
326  9, //inc_limit
327  5 //max_run
328  },
329  {
330  { //level / run
331  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
332  { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
333  { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 8, 1, 0 },
334  { -8, 1, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 10, 1, 0 }, { -10, 1, 0 }, { 11, 1, 0 },
335  { -11, 1, 0 }, { 12, 1, 0 }, { -12, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 13, 1, 0 },
336  { -13, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 14, 1, 0 }, { -14, 1, 0 }, { 15, 1, 0 },
337  { -15, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 16, 1, 0 }, { -16, 1, 0 }, { 17, 1, 0 },
338  { -17, 1, 0 }, { 18, 1, 0 }, { -18, 1, 0 }, { 4, 2, 0 }, { -4, 2, 0 }, { 19, 1, 0 },
339  { -19, 1, 0 }, { 20, 1, 0 }, { -20, 1, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 1, 4, 0 },
340  { -1, 4, 0 }, { 5, 2, 0 }, { -5, 2, 0 }, { 21, 1, 0 }, { -21, 1, 0 }
341  },
342  //level_add
343  { 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 },
344  2, //golomb_order
345  INT_MAX, //inc_limit
346  4 //max_run
347  }
348 };
349 
350 static const struct dec_2dvlc chroma_dec[5] = {
351  {
352  { //level / run
353  { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
354  { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
355  { 1, 7, 1 }, { -1, 7, 1 }, { 2, 1, 2 }, { -2, 1, 2 }, { 1, 8, 1 }, { -1, 8, 1 },
356  { 1, 9, 1 }, { -1, 9, 1 }, { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 },
357  { 1, 12, 1 }, { -1, 12, 1 }, { 1, 13, 1 }, { -1, 13, 1 }, { 1, 14, 1 }, { -1, 14, 1 },
358  { 1, 15, 1 }, { -1, 15, 1 }, { 3, 1, 3 }, { -3, 1, 3 }, { 1, 16, 1 }, { -1, 16, 1 },
359  { 1, 17, 1 }, { -1, 17, 1 }, { 1, 18, 1 }, { -1, 18, 1 }, { 1, 19, 1 }, { -1, 19, 1 },
360  { 1, 20, 1 }, { -1, 20, 1 }, { 1, 21, 1 }, { -1, 21, 1 }, { 1, 22, 1 }, { -1, 22, 1 },
361  { 2, 2, 2 }, { -2, 2, 2 }, { 1, 23, 1 }, { -1, 23, 1 }, { 1, 24, 1 }, { -1, 24, 1 },
362  { 1, 25, 1 }, { -1, 25, 1 }, { 4, 1, 3 }, { -4, 1, 3 }, { EOB }
363  },
364  //level_add
365  { 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 },
366  2, //golomb_order
367  0, //inc_limit
368  25 //max_run
369  },
370  {
371  { //level / run
372  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 1 },
373  { -2, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 },
374  { -1, 5, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 3, 1, 2 }, { -3, 1, 2 }, { 1, 7, 0 },
375  { -1, 7, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 2, 2, 1 }, { -2, 2, 1 }, { 1, 9, 0 },
376  { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 4, 1, 2 },
377  { -4, 1, 2 }, { 1, 12, 0 }, { -1, 12, 0 }, { 1, 13, 0 }, { -1, 13, 0 }, { 1, 14, 0 },
378  { -1, 14, 0 }, { 2, 3, 1 }, { -2, 3, 1 }, { 1, 15, 0 }, { -1, 15, 0 }, { 2, 4, 1 },
379  { -2, 4, 1 }, { 5, 1, 3 }, { -5, 1, 3 }, { 3, 2, 2 }, { -3, 2, 2 }, { 1, 16, 0 },
380  { -1, 16, 0 }, { 1, 17, 0 }, { -1, 17, 0 }, { 1, 18, 0 }, { -1, 18, 0 }, { 2, 5, 1 },
381  { -2, 5, 1 }, { 1, 19, 0 }, { -1, 19, 0 }, { 1, 20, 0 }, { -1, 20, 0 }
382  },
383  //level_add
384  { 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 },
385  0, //golomb_order
386  1, //inc_limit
387  20 //max_run
388  },
389  {
390  { //level / run
391  { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 },
392  { -1, 2, 0 }, { 3, 1, 1 }, { -3, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 4, 1, 1 },
393  { -4, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 2 },
394  { -5, 1, 2 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 3, 0 },
395  { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 2 }, { -6, 1, 2 }, { 1, 7, 0 },
396  { -1, 7, 0 }, { 2, 4, 0 }, { -2, 4, 0 }, { 7, 1, 2 }, { -7, 1, 2 }, { 1, 8, 0 },
397  { -1, 8, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 1, 9, 0 }, { -1, 9, 0 }, { 3, 3, 1 },
398  { -3, 3, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 8, 1, 2 },
399  { -8, 1, 2 }, { 1, 10, 0 }, { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 9, 1, 2 },
400  { -9, 1, 2 }, { 5, 2, 2 }, { -5, 2, 2 }, { 3, 4, 1 }, { -3, 4, 1 },
401  },
402  //level_add
403  { 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 },
404  1, //golomb_order
405  2, //inc_limit
406  11 //max_run
407  },
408  {
409  { //level / run
410  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
411  { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 5, 1, 1 },
412  { -5, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 6, 1, 1 }, { -6, 1, 1 }, { 1, 3, 0 },
413  { -1, 3, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 8, 1, 1 },
414  { -8, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 9, 1, 1 },
415  { -9, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 10, 1, 1 },
416  {-10, 1, 1 }, { 3, 3, 0 }, { -3, 3, 0 }, { 5, 2, 1 }, { -5, 2, 1 }, { 2, 4, 0 },
417  { -2, 4, 0 }, { 11, 1, 1 }, {-11, 1, 1 }, { 1, 6, 0 }, { -1, 6, 0 }, { 12, 1, 1 },
418  {-12, 1, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 6, 2, 1 }, { -6, 2, 1 }, { 13, 1, 1 },
419  {-13, 1, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 8, 0 }, { -1, 8, 0 },
420  },
421  //level_add
422  { 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 },
423  1, //golomb_order
424  4, //inc_limit
425  8 //max_run
426  },
427  {
428  { //level / run
429  { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
430  { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
431  { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 1, 2, 0 },
432  { -1, 2, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 10, 1, 0 }, { -10, 1, 0 }, { 11, 1, 0 },
433  { -11, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 12, 1, 0 }, { -12, 1, 0 }, { 13, 1, 0 },
434  { -13, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 14, 1, 0 }, { -14, 1, 0 }, { 1, 3, 0 },
435  { -1, 3, 0 }, { 15, 1, 0 }, { -15, 1, 0 }, { 4, 2, 0 }, { -4, 2, 0 }, { 16, 1, 0 },
436  { -16, 1, 0 }, { 17, 1, 0 }, { -17, 1, 0 }, { 5, 2, 0 }, { -5, 2, 0 }, { 1, 4, 0 },
437  { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 18, 1, 0 }, { -18, 1, 0 }, { 6, 2, 0 },
438  { -6, 2, 0 }, { 19, 1, 0 }, { -19, 1, 0 }, { 1, 5, 0 }, { -1, 5, 0 },
439  },
440  //level_add
441  { 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 },
442  0, //golomb_order
443  INT_MAX, //inc_limit
444  5, //max_run
445  }
446 };
447 
448 #undef EOB
449 
450 /*****************************************************************************
451  *
452  * motion vector prediction
453  *
454  ****************************************************************************/
455 
456 static inline void store_mvs(AVSContext *h)
457 {
458  h->col_mv[h->mbidx * 4 + 0] = h->mv[MV_FWD_X0];
459  h->col_mv[h->mbidx * 4 + 1] = h->mv[MV_FWD_X1];
460  h->col_mv[h->mbidx * 4 + 2] = h->mv[MV_FWD_X2];
461  h->col_mv[h->mbidx * 4 + 3] = h->mv[MV_FWD_X3];
462 }
463 
464 static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
465  cavs_vector *col_mv)
466 {
467  cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
468  unsigned den = h->direct_den[col_mv->ref];
469  int m = FF_SIGNBIT(col_mv->x);
470 
471  pmv_fw->dist = h->dist[1];
472  pmv_bw->dist = h->dist[0];
473  pmv_fw->ref = 1;
474  pmv_bw->ref = 0;
475  /* scale the co-located motion vector according to its temporal span */
476  pmv_fw->x = (((den + (den * col_mv->x * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
477  pmv_bw->x = m - (((den + (den * col_mv->x * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
478  m = FF_SIGNBIT(col_mv->y);
479  pmv_fw->y = (((den + (den * col_mv->y * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
480  pmv_bw->y = m - (((den + (den * col_mv->y * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
481 }
482 
483 static inline void mv_pred_sym(AVSContext *h, cavs_vector *src,
484  enum cavs_block size)
485 {
486  cavs_vector *dst = src + MV_BWD_OFFS;
487 
488  /* backward mv is the scaled and negated forward mv */
489  dst->x = -((src->x * h->sym_factor + 256) >> 9);
490  dst->y = -((src->y * h->sym_factor + 256) >> 9);
491  dst->ref = 0;
492  dst->dist = h->dist[0];
493  set_mvs(dst, size);
494 }
495 
496 /*****************************************************************************
497  *
498  * residual data decoding
499  *
500  ****************************************************************************/
501 
502 /** kth-order exponential golomb code */
503 static inline int get_ue_code(GetBitContext *gb, int order)
504 {
505  unsigned ret = get_ue_golomb(gb);
506  if (ret >= ((1U<<31)>>order)) {
507  av_log(NULL, AV_LOG_ERROR, "get_ue_code: value too larger\n");
508  return AVERROR_INVALIDDATA;
509  }
510  if (order) {
511  return (ret<<order) + get_bits(gb, order);
512  }
513  return ret;
514 }
515 
516 static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf,
517  int16_t *dst, int mul, int shift, int coeff_num)
518 {
519  int round = 1 << (shift - 1);
520  int pos = -1;
521  const uint8_t *scantab = h->scantable.permutated;
522 
523  /* inverse scan and dequantization */
524  while (--coeff_num >= 0) {
525  pos += run_buf[coeff_num];
526  if (pos > 63) {
527  av_log(h->avctx, AV_LOG_ERROR,
528  "position out of block bounds at pic %d MB(%d,%d)\n",
529  h->cur.poc, h->mbx, h->mby);
530  return AVERROR_INVALIDDATA;
531  }
532  dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
533  }
534  return 0;
535 }
536 
537 /**
538  * decode coefficients from one 8x8 block, dequantize, inverse transform
539  * and add them to sample block
540  * @param r pointer to 2D VLC table
541  * @param esc_golomb_order escape codes are k-golomb with this order k
542  * @param qp quantizer
543  * @param dst location of sample block
544  * @param stride line stride in frame buffer
545  */
547  const struct dec_2dvlc *r, int esc_golomb_order,
548  int qp, uint8_t *dst, ptrdiff_t stride)
549 {
550  int i, esc_code, level, mask, ret;
551  unsigned int level_code, run;
552  int16_t level_buf[65];
553  uint8_t run_buf[65];
554  int16_t *block = h->block;
555 
556  for (i = 0; i < 65; i++) {
557  level_code = get_ue_code(gb, r->golomb_order);
558  if (level_code >= ESCAPE_CODE) {
559  run = ((level_code - ESCAPE_CODE) >> 1) + 1;
560  if(run > 64) {
561  av_log(h->avctx, AV_LOG_ERROR, "run %d is too large\n", run);
562  return AVERROR_INVALIDDATA;
563  }
564  esc_code = get_ue_code(gb, esc_golomb_order);
565  if (esc_code < 0 || esc_code > 32767) {
566  av_log(h->avctx, AV_LOG_ERROR, "esc_code invalid\n");
567  return AVERROR_INVALIDDATA;
568  }
569 
570  level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
571  while (level > r->inc_limit)
572  r++;
573  mask = -(level_code & 1);
574  level = (level ^ mask) - mask;
575  } else {
576  level = r->rltab[level_code][0];
577  if (!level) //end of block signal
578  break;
579  run = r->rltab[level_code][1];
580  r += r->rltab[level_code][2];
581  }
582  level_buf[i] = level;
583  run_buf[i] = run;
584  }
585  ret = dequant(h, level_buf, run_buf, block, dequant_mul[qp],
586  dequant_shift[qp], i);
587  if (ret >= 0)
588  h->cdsp.cavs_idct8_add(dst, block, stride);
589  /* dequant() can write before it fails, so always clear the block */
590  h->bdsp.clear_block(block);
591  return ret;
592 }
593 
594 
596 {
597  if (h->cbp & (1 << 4)) {
598  int ret = decode_residual_block(h, &h->gb, chroma_dec, 0,
599  ff_cavs_chroma_qp[h->qp], h->cu, h->c_stride);
600  if (ret < 0)
601  return ret;
602  }
603  if (h->cbp & (1 << 5)) {
604  int ret = decode_residual_block(h, &h->gb, chroma_dec, 0,
605  ff_cavs_chroma_qp[h->qp], h->cv, h->c_stride);
606  if (ret < 0)
607  return ret;
608  }
609  return 0;
610 }
611 
612 static inline int decode_residual_inter(AVSContext *h)
613 {
614  int block, ret;
615 
616  /* get coded block pattern */
617  int cbp = get_ue_golomb(&h->gb);
618  if (cbp > 63U) {
619  av_log(h->avctx, AV_LOG_ERROR, "illegal inter cbp %d\n", cbp);
620  return AVERROR_INVALIDDATA;
621  }
622  h->cbp = cbp_tab[cbp][1];
623 
624  /* get quantizer */
625  if (h->cbp && !h->qp_fixed)
626  h->qp = (h->qp + (unsigned)get_se_golomb(&h->gb)) & 63;
627  for (block = 0; block < 4; block++)
628  if (h->cbp & (1 << block)) {
629  ret = decode_residual_block(h, &h->gb, inter_dec, 0, h->qp,
630  h->cy + h->luma_scan[block],
631  h->l_stride);
632  if (ret < 0)
633  return ret;
634  }
635 
636  return decode_residual_chroma(h);
637 }
638 
639 /*****************************************************************************
640  *
641  * macroblock level
642  *
643  ****************************************************************************/
644 
645 static inline void set_mv_intra(AVSContext *h)
646 {
648  set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
650  set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
651  if (h->cur.f->pict_type != AV_PICTURE_TYPE_B)
652  h->col_type_base[h->mbidx] = I_8X8;
653 }
654 
655 static int decode_mb_i(AVSContext *h, int cbp_code)
656 {
657  GetBitContext *gb = &h->gb;
658  unsigned pred_mode_uv;
659  int block;
660  uint8_t top[18];
661  uint8_t *left = NULL;
662  uint8_t *d;
663  int ret;
664 
666 
667  /* get intra prediction modes from stream */
668  for (block = 0; block < 4; block++) {
669  int nA, nB, predpred;
670  int pos = scan3x3[block];
671 
672  nA = h->pred_mode_Y[pos - 1];
673  nB = h->pred_mode_Y[pos - 3];
674  predpred = FFMIN(nA, nB);
675  if (predpred == NOT_AVAIL) // if either is not available
676  predpred = INTRA_L_LP;
677  if (!get_bits1(gb)) {
678  int rem_mode = get_bits(gb, 2);
679  predpred = rem_mode + (rem_mode >= predpred);
680  }
681  h->pred_mode_Y[pos] = predpred;
682  }
683  pred_mode_uv = get_ue_golomb_31(gb);
684  if (pred_mode_uv > 6) {
685  av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
686  return AVERROR_INVALIDDATA;
687  }
688  ff_cavs_modify_mb_i(h, &pred_mode_uv);
689 
690  /* get coded block pattern */
691  if (h->cur.f->pict_type == AV_PICTURE_TYPE_I)
692  cbp_code = get_ue_golomb(gb);
693  if (cbp_code > 63U) {
694  av_log(h->avctx, AV_LOG_ERROR, "illegal intra cbp\n");
695  return AVERROR_INVALIDDATA;
696  }
697  h->cbp = cbp_tab[cbp_code][0];
698  if (h->cbp && !h->qp_fixed)
699  h->qp = (h->qp + (unsigned)get_se_golomb(gb)) & 63; //qp_delta
700 
701  /* luma intra prediction interleaved with residual decode/transform/add */
702  for (block = 0; block < 4; block++) {
703  d = h->cy + h->luma_scan[block];
704  ff_cavs_load_intra_pred_luma(h, top, &left, block);
705  h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
706  (d, top, left, h->l_stride);
707  if (h->cbp & (1<<block)) {
708  ret = decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
709  if (ret < 0)
710  return ret;
711  }
712  }
713 
714  /* chroma intra prediction */
716  h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx * 10],
717  h->left_border_u, h->c_stride);
718  h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx * 10],
719  h->left_border_v, h->c_stride);
720 
721  ret = decode_residual_chroma(h);
722  if (ret < 0)
723  return ret;
725  set_mv_intra(h);
726  return 0;
727 }
728 
729 static inline void set_intra_mode_default(AVSContext *h)
730 {
731  if (h->stream_revision > 0) {
732  h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
733  h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = NOT_AVAIL;
734  } else {
735  h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
736  h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = INTRA_L_LP;
737  }
738 }
739 
740 static int decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
741 {
742  GetBitContext *gb = &h->gb;
743  int ref[4];
744  int ret;
745 
747  switch (mb_type) {
748  case P_SKIP:
750  break;
751  case P_16X16:
752  ref[0] = h->ref_flag ? 0 : get_bits1(gb);
754  break;
755  case P_16X8:
756  ref[0] = h->ref_flag ? 0 : get_bits1(gb);
757  ref[2] = h->ref_flag ? 0 : get_bits1(gb);
760  break;
761  case P_8X16:
762  ref[0] = h->ref_flag ? 0 : get_bits1(gb);
763  ref[1] = h->ref_flag ? 0 : get_bits1(gb);
766  break;
767  case P_8X8:
768  ref[0] = h->ref_flag ? 0 : get_bits1(gb);
769  ref[1] = h->ref_flag ? 0 : get_bits1(gb);
770  ref[2] = h->ref_flag ? 0 : get_bits1(gb);
771  ref[3] = h->ref_flag ? 0 : get_bits1(gb);
776  break;
777  default:
778  av_log(h->avctx, AV_LOG_ERROR, "Invalid mb_type %d in P frame\n",
779  mb_type);
780  return AVERROR_INVALIDDATA;
781  }
782  ff_cavs_inter(h, mb_type);
784  store_mvs(h);
785  if (mb_type != P_SKIP) {
786  ret = decode_residual_inter(h);
787  if (ret < 0)
788  return ret;
789  }
790  ff_cavs_filter(h, mb_type);
791  h->col_type_base[h->mbidx] = mb_type;
792 
793  return 0;
794 }
795 
796 static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
797 {
798  int block;
799  enum cavs_sub_mb sub_type[4];
800  int flags;
801  int ret;
802 
804 
805  /* reset all MVs */
806  h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
807  set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
808  h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
809  set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
810  switch (mb_type) {
811  case B_SKIP:
812  case B_DIRECT:
813  if (!h->col_type_base[h->mbidx]) {
814  /* intra MB at co-location, do in-plane prediction */
817  } else
818  /* direct prediction from co-located P MB, block-wise */
819  for (block = 0; block < 4; block++)
820  mv_pred_direct(h, &h->mv[mv_scan[block]],
821  &h->col_mv[h->mbidx * 4 + block]);
822  break;
823  case B_FWD_16X16:
825  break;
826  case B_SYM_16X16:
828  mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
829  break;
830  case B_BWD_16X16:
832  break;
833  case B_8X8:
834 #define TMP_UNUSED_INX 7
835  flags = 0;
836  for (block = 0; block < 4; block++)
837  sub_type[block] = get_bits(&h->gb, 2);
838  for (block = 0; block < 4; block++) {
839  switch (sub_type[block]) {
840  case B_SUB_DIRECT:
841  if (!h->col_type_base[h->mbidx]) {
842  /* intra MB at co-location, do in-plane prediction */
843  if(flags==0) {
844  // if col-MB is a Intra MB, current Block size is 16x16.
845  // AVS standard section 9.9.1
846  if(block>0){
847  h->mv[TMP_UNUSED_INX ] = h->mv[MV_FWD_X0 ];
849  }
851  MV_PRED_BSKIP, BLK_8X8, 1);
854  MV_PRED_BSKIP, BLK_8X8, 0);
855  if(block>0) {
856  flags = mv_scan[block];
857  h->mv[flags ] = h->mv[MV_FWD_X0 ];
858  h->mv[flags + MV_BWD_OFFS] = h->mv[MV_FWD_X0 + MV_BWD_OFFS];
859  h->mv[MV_FWD_X0 ] = h->mv[TMP_UNUSED_INX ];
861  } else
862  flags = MV_FWD_X0;
863  } else {
864  h->mv[mv_scan[block] ] = h->mv[flags ];
865  h->mv[mv_scan[block] + MV_BWD_OFFS] = h->mv[flags + MV_BWD_OFFS];
866  }
867  } else
868  mv_pred_direct(h, &h->mv[mv_scan[block]],
869  &h->col_mv[h->mbidx * 4 + block]);
870  break;
871  case B_SUB_FWD:
873  MV_PRED_MEDIAN, BLK_8X8, 1);
874  break;
875  case B_SUB_SYM:
877  MV_PRED_MEDIAN, BLK_8X8, 1);
878  mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
879  break;
880  }
881  }
882 #undef TMP_UNUSED_INX
883  for (block = 0; block < 4; block++) {
884  if (sub_type[block] == B_SUB_BWD)
886  mv_scan[block] + MV_BWD_OFFS - 3,
887  MV_PRED_MEDIAN, BLK_8X8, 0);
888  }
889  break;
890  default:
891  if (mb_type <= B_SYM_16X16) {
892  av_log(h->avctx, AV_LOG_ERROR, "Invalid mb_type %d in B frame\n", mb_type);
893  return AVERROR_INVALIDDATA;
894  }
895  av_assert2(mb_type < B_8X8);
896  flags = ff_cavs_partition_flags[mb_type];
897  if (mb_type & 1) { /* 16x8 macroblock types */
898  if (flags & FWD0)
900  if (flags & SYM0)
901  mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
902  if (flags & FWD1)
904  if (flags & SYM1)
905  mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
906  if (flags & BWD0)
908  if (flags & BWD1)
910  } else { /* 8x16 macroblock types */
911  if (flags & FWD0)
913  if (flags & SYM0)
914  mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
915  if (flags & FWD1)
917  if (flags & SYM1)
918  mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
919  if (flags & BWD0)
921  if (flags & BWD1)
923  }
924  }
925  ff_cavs_inter(h, mb_type);
927  if (mb_type != B_SKIP) {
928  ret = decode_residual_inter(h);
929  if (ret < 0)
930  return ret;
931  }
932  ff_cavs_filter(h, mb_type);
933 
934  return 0;
935 }
936 
937 /*****************************************************************************
938  *
939  * slice level
940  *
941  ****************************************************************************/
942 
944 {
945  if (h->stc > 0xAF)
946  av_log(h->avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
947 
948  if (h->stc >= h->mb_height) {
949  av_log(h->avctx, AV_LOG_ERROR, "stc 0x%02x is too large\n", h->stc);
950  return AVERROR_INVALIDDATA;
951  }
952 
953  h->mby = h->stc;
954  h->mbidx = h->mby * h->mb_width;
955 
956  /* mark top macroblocks as unavailable */
957  h->flags &= ~(B_AVAIL | C_AVAIL);
958  if (!h->pic_qp_fixed) {
959  h->qp_fixed = get_bits1(gb);
960  h->qp = get_bits(gb, 6);
961  }
962  /* inter frame or second slice can have weighting params */
963  if ((h->cur.f->pict_type != AV_PICTURE_TYPE_I) ||
964  (!h->pic_structure && h->mby >= h->mb_width / 2))
965  if (get_bits1(gb)) { //slice_weighting_flag
966  av_log(h->avctx, AV_LOG_ERROR,
967  "weighted prediction not yet supported\n");
968  }
969  return 0;
970 }
971 
972 static inline int check_for_slice(AVSContext *h)
973 {
974  GetBitContext *gb = &h->gb;
975  int align;
976 
977  if (h->mbx)
978  return 0;
979  align = (-get_bits_count(gb)) & 7;
980  /* check for stuffing byte */
981  if (!align && (show_bits(gb, 8) == 0x80))
982  align = 8;
983  if ((show_bits_long(gb, 24 + align) & 0xFFFFFF) == 0x000001) {
984  skip_bits_long(gb, 24 + align);
985  h->stc = get_bits(gb, 8);
986  if (h->stc >= h->mb_height)
987  return 0;
988  decode_slice_header(h, gb);
989  return 1;
990  }
991  return 0;
992 }
993 
994 /*****************************************************************************
995  *
996  * frame level
997  *
998  ****************************************************************************/
999 
1001 {
1002  int ret;
1003  int skip_count = -1;
1004  enum cavs_mb mb_type;
1005 
1006  if (!h->top_qp) {
1007  av_log(h->avctx, AV_LOG_ERROR, "No sequence header decoded yet\n");
1008  return AVERROR_INVALIDDATA;
1009  }
1010 
1011  av_frame_unref(h->cur.f);
1012 
1013  skip_bits(&h->gb, 16);//bbv_dwlay
1014  if (h->stc == PIC_PB_START_CODE) {
1015  h->cur.f->pict_type = get_bits(&h->gb, 2) + AV_PICTURE_TYPE_I;
1016  if (h->cur.f->pict_type > AV_PICTURE_TYPE_B) {
1017  av_log(h->avctx, AV_LOG_ERROR, "illegal picture type\n");
1018  return AVERROR_INVALIDDATA;
1019  }
1020  /* make sure we have the reference frames we need */
1021  if (!h->DPB[0].f->data[0] ||
1022  (!h->DPB[1].f->data[0] && h->cur.f->pict_type == AV_PICTURE_TYPE_B))
1023  return AVERROR_INVALIDDATA;
1024  } else {
1025  h->cur.f->pict_type = AV_PICTURE_TYPE_I;
1026  if (get_bits1(&h->gb))
1027  skip_bits(&h->gb, 24);//time_code
1028  /* old sample clips were all progressive and no low_delay,
1029  bump stream revision if detected otherwise */
1030  if (h->low_delay || !(show_bits(&h->gb, 9) & 1))
1031  h->stream_revision = 1;
1032  /* similarly test top_field_first and repeat_first_field */
1033  else if (show_bits(&h->gb, 11) & 3)
1034  h->stream_revision = 1;
1035  if (h->stream_revision > 0)
1036  skip_bits(&h->gb, 1); //marker_bit
1037  }
1038 
1039  if (get_bits_left(&h->gb) < 23)
1040  return AVERROR_INVALIDDATA;
1041 
1042  ret = ff_get_buffer(h->avctx, h->cur.f, h->cur.f->pict_type == AV_PICTURE_TYPE_B ?
1044  if (ret < 0)
1045  return ret;
1046 
1047  if (!h->edge_emu_buffer) {
1048  int alloc_size = FFALIGN(FFABS(h->cur.f->linesize[0]) + 32, 32);
1049  h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 24);
1050  if (!h->edge_emu_buffer)
1051  return AVERROR(ENOMEM);
1052  }
1053 
1054  if ((ret = ff_cavs_init_pic(h)) < 0)
1055  return ret;
1056  h->cur.poc = get_bits(&h->gb, 8) * 2;
1057 
1058  /* get temporal distances and MV scaling factors */
1059  if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1060  h->dist[0] = (h->cur.poc - h->DPB[0].poc) & 511;
1061  } else {
1062  h->dist[0] = (h->DPB[0].poc - h->cur.poc) & 511;
1063  }
1064  h->dist[1] = (h->cur.poc - h->DPB[1].poc) & 511;
1065  h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
1066  h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
1067  if (h->cur.f->pict_type == AV_PICTURE_TYPE_B) {
1068  h->sym_factor = h->dist[0] * h->scale_den[1];
1069  if (FFABS(h->sym_factor) > 32768) {
1070  av_log(h->avctx, AV_LOG_ERROR, "sym_factor %d too large\n", h->sym_factor);
1071  return AVERROR_INVALIDDATA;
1072  }
1073  } else {
1074  h->direct_den[0] = h->dist[0] ? 16384 / h->dist[0] : 0;
1075  h->direct_den[1] = h->dist[1] ? 16384 / h->dist[1] : 0;
1076  }
1077 
1078  if (h->low_delay)
1079  get_ue_golomb(&h->gb); //bbv_check_times
1080  h->progressive = get_bits1(&h->gb);
1081  h->pic_structure = 1;
1082  if (!h->progressive)
1083  h->pic_structure = get_bits1(&h->gb);
1084  if (!h->pic_structure && h->stc == PIC_PB_START_CODE)
1085  skip_bits1(&h->gb); //advanced_pred_mode_disable
1086  skip_bits1(&h->gb); //top_field_first
1087  skip_bits1(&h->gb); //repeat_first_field
1088  h->pic_qp_fixed =
1089  h->qp_fixed = get_bits1(&h->gb);
1090  h->qp = get_bits(&h->gb, 6);
1091  if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1092  if (!h->progressive && !h->pic_structure)
1093  skip_bits1(&h->gb);//what is this?
1094  skip_bits(&h->gb, 4); //reserved bits
1095  } else {
1096  if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
1097  h->ref_flag = get_bits1(&h->gb);
1098  skip_bits(&h->gb, 4); //reserved bits
1099  h->skip_mode_flag = get_bits1(&h->gb);
1100  }
1101  h->loop_filter_disable = get_bits1(&h->gb);
1102  if (!h->loop_filter_disable && get_bits1(&h->gb)) {
1103  h->alpha_offset = get_se_golomb(&h->gb);
1104  h->beta_offset = get_se_golomb(&h->gb);
1105  if ( h->alpha_offset < -64 || h->alpha_offset > 64
1106  || h-> beta_offset < -64 || h-> beta_offset > 64) {
1107  h->alpha_offset = h->beta_offset = 0;
1108  return AVERROR_INVALIDDATA;
1109  }
1110  } else {
1111  h->alpha_offset = h->beta_offset = 0;
1112  }
1113 
1114  ret = 0;
1115  if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1116  do {
1117  check_for_slice(h);
1118  ret = decode_mb_i(h, 0);
1119  if (ret < 0)
1120  break;
1121  } while (ff_cavs_next_mb(h));
1122  } else if (h->cur.f->pict_type == AV_PICTURE_TYPE_P) {
1123  do {
1124  if (check_for_slice(h))
1125  skip_count = -1;
1126  if (h->skip_mode_flag && (skip_count < 0)) {
1127  if (get_bits_left(&h->gb) < 1) {
1128  ret = AVERROR_INVALIDDATA;
1129  break;
1130  }
1131  skip_count = get_ue_golomb(&h->gb);
1132  if (skip_count < 0) {
1133  ret = AVERROR_INVALIDDATA;
1134  break;
1135  }
1136  }
1137  if (h->skip_mode_flag && skip_count--) {
1138  ret = decode_mb_p(h, P_SKIP);
1139  } else {
1140  if (get_bits_left(&h->gb) < 1) {
1141  ret = AVERROR_INVALIDDATA;
1142  break;
1143  }
1144  mb_type = get_ue_golomb(&h->gb) + P_SKIP + h->skip_mode_flag;
1145  if (mb_type > P_8X8)
1146  ret = decode_mb_i(h, mb_type - P_8X8 - 1);
1147  else
1148  ret = decode_mb_p(h, mb_type);
1149  }
1150  if (ret < 0)
1151  break;
1152  } while (ff_cavs_next_mb(h));
1153  } else { /* AV_PICTURE_TYPE_B */
1154  do {
1155  if (check_for_slice(h))
1156  skip_count = -1;
1157  if (h->skip_mode_flag && (skip_count < 0)) {
1158  if (get_bits_left(&h->gb) < 1) {
1159  ret = AVERROR_INVALIDDATA;
1160  break;
1161  }
1162  skip_count = get_ue_golomb(&h->gb);
1163  if (skip_count < 0) {
1164  ret = AVERROR_INVALIDDATA;
1165  break;
1166  }
1167  }
1168  if (h->skip_mode_flag && skip_count--) {
1169  ret = decode_mb_b(h, B_SKIP);
1170  } else {
1171  if (get_bits_left(&h->gb) < 1) {
1172  ret = AVERROR_INVALIDDATA;
1173  break;
1174  }
1175  mb_type = get_ue_golomb(&h->gb) + B_SKIP + h->skip_mode_flag;
1176  if (mb_type > B_8X8)
1177  ret = decode_mb_i(h, mb_type - B_8X8 - 1);
1178  else
1179  ret = decode_mb_b(h, mb_type);
1180  }
1181  if (ret < 0)
1182  break;
1183  } while (ff_cavs_next_mb(h));
1184  }
1185  emms_c();
1186  if (ret >= 0 && h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1187  av_frame_unref(h->DPB[1].f);
1188  FFSWAP(AVSFrame, h->cur, h->DPB[1]);
1189  FFSWAP(AVSFrame, h->DPB[0], h->DPB[1]);
1190  }
1191  return ret;
1192 }
1193 
1194 /*****************************************************************************
1195  *
1196  * headers and interface
1197  *
1198  ****************************************************************************/
1199 
1201 {
1202  int frame_rate_code;
1203  int width, height;
1204  int ret;
1205 
1206  h->profile = get_bits(&h->gb, 8);
1207  h->level = get_bits(&h->gb, 8);
1208  skip_bits1(&h->gb); //progressive sequence
1209 
1210  width = get_bits(&h->gb, 14);
1211  height = get_bits(&h->gb, 14);
1212  if ((h->width || h->height) && (h->width != width || h->height != height)) {
1214  "Width/height changing in CAVS");
1215  return AVERROR_PATCHWELCOME;
1216  }
1217  if (width <= 0 || height <= 0) {
1218  av_log(h->avctx, AV_LOG_ERROR, "Dimensions invalid\n");
1219  return AVERROR_INVALIDDATA;
1220  }
1221  skip_bits(&h->gb, 2); //chroma format
1222  skip_bits(&h->gb, 3); //sample_precision
1223  h->aspect_ratio = get_bits(&h->gb, 4);
1224  frame_rate_code = get_bits(&h->gb, 4);
1225  if (frame_rate_code == 0 || frame_rate_code > 13) {
1226  av_log(h->avctx, AV_LOG_WARNING,
1227  "frame_rate_code %d is invalid\n", frame_rate_code);
1228  frame_rate_code = 1;
1229  }
1230 
1231  skip_bits(&h->gb, 18); //bit_rate_lower
1232  skip_bits1(&h->gb); //marker_bit
1233  skip_bits(&h->gb, 12); //bit_rate_upper
1234  h->low_delay = get_bits1(&h->gb);
1235 
1236  ret = ff_set_dimensions(h->avctx, width, height);
1237  if (ret < 0)
1238  return ret;
1239 
1240  h->width = width;
1241  h->height = height;
1242  h->mb_width = (h->width + 15) >> 4;
1243  h->mb_height = (h->height + 15) >> 4;
1244  h->avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];
1245  if (!h->top_qp)
1246  return ff_cavs_init_top_lines(h);
1247  return 0;
1248 }
1249 
1250 static void cavs_flush(AVCodecContext * avctx)
1251 {
1252  AVSContext *h = avctx->priv_data;
1253  h->got_keyframe = 0;
1254 }
1255 
1256 static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1257  AVPacket *avpkt)
1258 {
1259  AVSContext *h = avctx->priv_data;
1260  const uint8_t *buf = avpkt->data;
1261  int buf_size = avpkt->size;
1262  uint32_t stc = -1;
1263  int input_size, ret;
1264  const uint8_t *buf_end;
1265  const uint8_t *buf_ptr;
1266  int frame_start = 0;
1267 
1268  if (buf_size == 0) {
1269  if (!h->low_delay && h->DPB[0].f->data[0]) {
1270  *got_frame = 1;
1271  av_frame_move_ref(data, h->DPB[0].f);
1272  }
1273  return 0;
1274  }
1275 
1276  h->stc = 0;
1277 
1278  buf_ptr = buf;
1279  buf_end = buf + buf_size;
1280  for(;;) {
1281  buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
1282  if ((stc & 0xFFFFFE00) || buf_ptr == buf_end) {
1283  if (!h->stc)
1284  av_log(h->avctx, AV_LOG_WARNING, "no frame decoded\n");
1285  return FFMAX(0, buf_ptr - buf);
1286  }
1287  input_size = buf_end - buf_ptr;
1288  if ((ret = init_get_bits8(&h->gb, buf_ptr, input_size)) < 0)
1289  return ret;
1290  switch (stc) {
1291  case CAVS_START_CODE:
1293  break;
1294  case PIC_I_START_CODE:
1295  if (!h->got_keyframe) {
1296  av_frame_unref(h->DPB[0].f);
1297  av_frame_unref(h->DPB[1].f);
1298  h->got_keyframe = 1;
1299  }
1300  case PIC_PB_START_CODE:
1301  if (frame_start > 1)
1302  return AVERROR_INVALIDDATA;
1303  frame_start ++;
1304  if (*got_frame)
1306  *got_frame = 0;
1307  if (!h->got_keyframe)
1308  break;
1309  h->stc = stc;
1310  ret = decode_pic(h);
1311  if (ret < 0)
1312  return ret;
1313  *got_frame = 1;
1314  if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1315  if (h->DPB[!h->low_delay].f->data[0]) {
1316  if ((ret = av_frame_ref(data, h->DPB[!h->low_delay].f)) < 0)
1317  return ret;
1318  } else {
1319  *got_frame = 0;
1320  }
1321  } else {
1322  av_frame_move_ref(data, h->cur.f);
1323  }
1324  break;
1325  case EXT_START_CODE:
1326  //mpeg_decode_extension(avctx, buf_ptr, input_size);
1327  break;
1328  case USER_START_CODE:
1329  //mpeg_decode_user_data(avctx, buf_ptr, input_size);
1330  break;
1331  default:
1332  if (stc <= SLICE_MAX_START_CODE) {
1333  decode_slice_header(h, &h->gb);
1334  }
1335  break;
1336  }
1337  }
1338 }
1339 
1341  .name = "cavs",
1342  .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
1343  .type = AVMEDIA_TYPE_VIDEO,
1344  .id = AV_CODEC_ID_CAVS,
1345  .priv_data_size = sizeof(AVSContext),
1346  .init = ff_cavs_init,
1347  .close = ff_cavs_end,
1349  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1350  .flush = cavs_flush,
1351 };
static void flush(AVCodecContext *avctx)
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
int ff_cavs_init_pic(AVSContext *h)
Definition: cavs.c:726
int ff_cavs_next_mb(AVSContext *h)
save predictors for later macroblocks and increase macroblock address
Definition: cavs.c:681
void ff_cavs_load_intra_pred_chroma(AVSContext *h)
Definition: cavs.c:238
void ff_cavs_load_intra_pred_luma(AVSContext *h, uint8_t *top, uint8_t **left, int block)
Definition: cavs.c:187
av_cold int ff_cavs_end(AVCodecContext *avctx)
Definition: cavs.c:843
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:578
av_cold int ff_cavs_init(AVCodecContext *avctx)
Definition: cavs.c:797
void ff_cavs_inter(AVSContext *h, enum cavs_mb mb_type)
Definition: cavs.c:497
void ff_cavs_modify_mb_i(AVSContext *h, int *pred_mode_uv)
Definition: cavs.c:365
int ff_cavs_init_top_lines(AVSContext *h)
some predictions require data from the top-neighbouring macroblock.
Definition: cavs.c:762
void ff_cavs_init_mb(AVSContext *h)
initialise predictors for motion vectors and intra prediction
Definition: cavs.c:640
void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type)
in-loop deblocking filter for a single macroblock
Definition: cavs.c:111
#define C_AVAIL
Definition: cavs.h:43
#define BWD1
Definition: cavs.h:54
#define SYM1
Definition: cavs.h:56
#define MV_BWD_OFFS
Definition: cavs.h:60
#define USER_START_CODE
Definition: cavs.h:36
cavs_sub_mb
Definition: cavs.h:78
@ B_SUB_SYM
Definition: cavs.h:82
@ B_SUB_DIRECT
Definition: cavs.h:79
@ B_SUB_FWD
Definition: cavs.h:80
@ B_SUB_BWD
Definition: cavs.h:81
#define PIC_I_START_CODE
Definition: cavs.h:38
const cavs_vector ff_cavs_dir_mv
mark block as "no prediction from this direction" e.g.
Definition: cavsdata.c:66
#define SYM0
Definition: cavs.h:55
@ MV_PRED_MEDIAN
Definition: cavs.h:107
@ MV_PRED_TOP
Definition: cavs.h:109
@ MV_PRED_BSKIP
Definition: cavs.h:112
@ MV_PRED_PSKIP
Definition: cavs.h:111
@ MV_PRED_LEFT
Definition: cavs.h:108
@ MV_PRED_TOPRIGHT
Definition: cavs.h:110
const uint8_t ff_cavs_chroma_qp[64]
Definition: cavsdata.c:57
#define EXT_START_CODE
Definition: cavs.h:35
#define SLICE_MAX_START_CODE
Definition: cavs.h:34
cavs_mb
Definition: cavs.h:63
@ B_FWD_16X16
Definition: cavs.h:72
@ P_SKIP
Definition: cavs.h:65
@ P_8X16
Definition: cavs.h:68
@ I_8X8
Definition: cavs.h:64
@ B_8X8
Definition: cavs.h:75
@ P_16X8
Definition: cavs.h:67
@ B_DIRECT
Definition: cavs.h:71
@ B_SKIP
Definition: cavs.h:70
@ B_BWD_16X16
Definition: cavs.h:73
@ P_8X8
Definition: cavs.h:69
@ P_16X16
Definition: cavs.h:66
@ B_SYM_16X16
Definition: cavs.h:74
#define FWD0
Definition: cavs.h:51
@ INTRA_L_LP
Definition: cavs.h:88
static void set_mvs(cavs_vector *mv, enum cavs_block size)
Definition: cavs.h:252
@ MV_FWD_C2
Definition: cavs.h:126
@ MV_BWD_X1
Definition: cavs.h:139
@ MV_FWD_X3
Definition: cavs.h:132
@ MV_BWD_A1
Definition: cavs.h:137
@ MV_FWD_X1
Definition: cavs.h:129
@ MV_BWD_B3
Definition: cavs.h:135
@ MV_FWD_X0
Definition: cavs.h:128
@ MV_BWD_X2
Definition: cavs.h:141
@ MV_FWD_B3
Definition: cavs.h:125
@ MV_FWD_X2
Definition: cavs.h:131
@ MV_FWD_A1
Definition: cavs.h:127
@ MV_BWD_X0
Definition: cavs.h:138
@ MV_BWD_C2
Definition: cavs.h:136
cavs_block
Definition: cavs.h:115
@ BLK_16X8
Definition: cavs.h:117
@ BLK_16X16
Definition: cavs.h:116
@ BLK_8X8
Definition: cavs.h:119
@ BLK_8X16
Definition: cavs.h:118
#define BWD0
Definition: cavs.h:53
#define FWD1
Definition: cavs.h:52
const uint8_t ff_cavs_partition_flags[30]
Definition: cavsdata.c:24
#define B_AVAIL
Definition: cavs.h:42
#define PIC_PB_START_CODE
Definition: cavs.h:39
const cavs_vector ff_cavs_intra_mv
mark block as using intra prediction
Definition: cavsdata.c:69
#define CAVS_START_CODE
Definition: cavs.h:37
#define NOT_AVAIL
Definition: cavs.h:45
#define ESCAPE_CODE
Definition: cavs.h:49
static const struct dec_2dvlc intra_dec[7]
Definition: cavsdec.c:78
static const struct dec_2dvlc inter_dec[7]
Definition: cavsdec.c:214
static int decode_seq_header(AVSContext *h)
Definition: cavsdec.c:1200
static int decode_slice_header(AVSContext *h, GetBitContext *gb)
Definition: cavsdec.c:943
static void cavs_flush(AVCodecContext *avctx)
Definition: cavsdec.c:1250
static void set_intra_mode_default(AVSContext *h)
Definition: cavsdec.c:729
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:516
static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
Definition: cavsdec.c:796
static const struct dec_2dvlc chroma_dec[5]
Definition: cavsdec.c:350
static int decode_mb_i(AVSContext *h, int cbp_code)
Definition: cavsdec.c:655
static const uint16_t dequant_mul[64]
Definition: cavsdec.c:65
static int decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
Definition: cavsdec.c:740
static void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw, cavs_vector *col_mv)
Definition: cavsdec.c:464
#define EOB
Definition: cavsdec.c:76
static int get_ue_code(GetBitContext *gb, int order)
kth-order exponential golomb code
Definition: cavsdec.c:503
static int decode_residual_chroma(AVSContext *h)
Definition: cavsdec.c:595
static const uint8_t scan3x3[4]
Definition: cavsdec.c:52
static int decode_residual_block(AVSContext *h, GetBitContext *gb, const struct dec_2dvlc *r, int esc_golomb_order, int qp, uint8_t *dst, ptrdiff_t stride)
decode coefficients from one 8x8 block, dequantize, inverse transform and add them to sample block
Definition: cavsdec.c:546
static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: cavsdec.c:1256
static const uint8_t cbp_tab[64][2]
Definition: cavsdec.c:41
static const uint8_t mv_scan[4]
Definition: cavsdec.c:36
static void mv_pred_sym(AVSContext *h, cavs_vector *src, enum cavs_block size)
Definition: cavsdec.c:483
static int decode_residual_inter(AVSContext *h)
Definition: cavsdec.c:612
static void store_mvs(AVSContext *h)
Definition: cavsdec.c:456
static int check_for_slice(AVSContext *h)
Definition: cavsdec.c:972
#define TMP_UNUSED_INX
static void set_mv_intra(AVSContext *h)
Definition: cavsdec.c:645
static const uint8_t dequant_shift[64]
Definition: cavsdec.c:54
static int decode_pic(AVSContext *h)
Definition: cavsdec.c:1000
AVCodec ff_cavs_decoder
Definition: cavsdec.c:1340
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFMIN(a, b)
Definition: common.h:105
#define FFMAX(a, b)
Definition: common.h:103
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define NULL
Definition: coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1911
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static float mul(float src0, float src1)
bitstream reader API header.
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:602
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:446
exp golomb vlc stuff
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:241
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition: golomb.h:122
static int get_ue_golomb(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to 8190.
Definition: golomb.h:55
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:77
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:514
@ AV_CODEC_ID_CAVS
Definition: codec_id.h:136
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:582
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:443
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:276
int i
Definition: input.c:407
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
#define FF_SIGNBIT(x)
Definition: internal.h:109
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:84
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define emms_c()
Definition: internal.h:54
static av_always_inline av_const double round(double x)
Definition: libm.h:444
static const uint16_t mask[17]
Definition: lzw.c:38
int stride
Definition: mace.c:144
#define FFALIGN(x, a)
Definition: macros.h:48
MPEG-1/2 tables.
const AVRational ff_mpeg12_frame_rate_tab[]
static int frame_start(MpegEncContext *s)
const char data[16]
Definition: mxf.c:142
static int shift(int a, int b)
Definition: sonic.c:82
unsigned int pos
Definition: spdifenc.c:412
main external API structure.
Definition: avcodec.h:536
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Definition: cavs.h:160
int16_t x
Definition: cavs.h:146
int16_t y
Definition: cavs.h:147
int16_t dist
Definition: cavs.h:148
int16_t ref
Definition: cavs.h:149
uint8_t run
Definition: svq3.c:205
uint8_t level
Definition: svq3.c:206
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
static int16_t block[64]
Definition: dct.c:116
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
#define height
#define width
int size
const char * r
Definition: vf_curves.c:117