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