2 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
3 * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
5 * This file is part of FFmpeg.
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.
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.
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
24 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
25 * @author Stefan Gehrer <stefan.gehrer@gmx.de>
28 #include "libavutil/avassert.h"
34 #include "mpeg12data.h"
36 static const uint8_t mv_scan[4] = {
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 }
52 static const uint8_t scan3x3[4] = { 4, 5, 7, 8 };
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
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
78 static const struct dec_2dvlc intra_dec[7] = {
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 }
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 },
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 }
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 },
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 }
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 },
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 }
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 },
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 }
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 },
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 }
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 },
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 }
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 },
214 static const struct dec_2dvlc inter_dec[7] = {
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 }
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 },
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 }
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 },
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 }
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 },
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 }
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 },
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 }
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 },
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 }
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 },
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 }
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 },
350 static const struct dec_2dvlc chroma_dec[5] = {
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 }
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 },
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 }
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 },
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 },
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 },
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 },
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 },
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 },
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 },
450 /*****************************************************************************
452 * motion vector prediction
454 ****************************************************************************/
456 static inline void store_mvs(AVSContext *h)
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];
464 static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
467 cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
468 int den = h->direct_den[col_mv->ref];
469 int m = FF_SIGNBIT(col_mv->x);
471 pmv_fw->dist = h->dist[1];
472 pmv_bw->dist = h->dist[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);
483 static inline void mv_pred_sym(AVSContext *h, cavs_vector *src,
484 enum cavs_block size)
486 cavs_vector *dst = src + MV_BWD_OFFS;
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);
492 dst->dist = h->dist[0];
496 /*****************************************************************************
498 * residual data decoding
500 ****************************************************************************/
502 /** kth-order exponential golomb code */
503 static inline int get_ue_code(GetBitContext *gb, int order)
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;
511 return (ret<<order) + get_bits(gb, order);
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)
519 int round = 1 << (shift - 1);
521 const uint8_t *scantab = h->scantable.permutated;
523 /* inverse scan and dequantization */
524 while (--coeff_num >= 0) {
525 pos += run_buf[coeff_num];
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;
532 dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
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
546 static int decode_residual_block(AVSContext *h, GetBitContext *gb,
547 const struct dec_2dvlc *r, int esc_golomb_order,
548 int qp, uint8_t *dst, int stride)
550 int i, esc_code, level, mask, ret;
551 unsigned int level_code, run;
552 int16_t level_buf[65];
554 int16_t *block = h->block;
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;
561 av_log(h->avctx, AV_LOG_ERROR, "run %d is too large\n", run);
562 return AVERROR_INVALIDDATA;
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;
570 level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
571 while (level > r->inc_limit)
573 mask = -(level_code & 1);
574 level = (level ^ mask) - mask;
576 level = r->rltab[level_code][0];
577 if (!level) //end of block signal
579 run = r->rltab[level_code][1];
580 r += r->rltab[level_code][2];
582 level_buf[i] = level;
585 if ((ret = dequant(h, level_buf, run_buf, block, dequant_mul[qp],
586 dequant_shift[qp], i)) < 0)
588 h->cdsp.cavs_idct8_add(dst, block, stride);
589 h->bdsp.clear_block(block);
594 static inline void decode_residual_chroma(AVSContext *h)
596 if (h->cbp & (1 << 4))
597 decode_residual_block(h, &h->gb, chroma_dec, 0,
598 ff_cavs_chroma_qp[h->qp], h->cu, h->c_stride);
599 if (h->cbp & (1 << 5))
600 decode_residual_block(h, &h->gb, chroma_dec, 0,
601 ff_cavs_chroma_qp[h->qp], h->cv, h->c_stride);
604 static inline int decode_residual_inter(AVSContext *h)
608 /* get coded block pattern */
609 int cbp = get_ue_golomb(&h->gb);
611 av_log(h->avctx, AV_LOG_ERROR, "illegal inter cbp %d\n", cbp);
612 return AVERROR_INVALIDDATA;
614 h->cbp = cbp_tab[cbp][1];
617 if (h->cbp && !h->qp_fixed)
618 h->qp = (h->qp + get_se_golomb(&h->gb)) & 63;
619 for (block = 0; block < 4; block++)
620 if (h->cbp & (1 << block))
621 decode_residual_block(h, &h->gb, inter_dec, 0, h->qp,
622 h->cy + h->luma_scan[block], h->l_stride);
623 decode_residual_chroma(h);
628 /*****************************************************************************
632 ****************************************************************************/
634 static inline void set_mv_intra(AVSContext *h)
636 h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
637 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
638 h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
639 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
640 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B)
641 h->col_type_base[h->mbidx] = I_8X8;
644 static int decode_mb_i(AVSContext *h, int cbp_code)
646 GetBitContext *gb = &h->gb;
647 unsigned pred_mode_uv;
650 uint8_t *left = NULL;
655 /* get intra prediction modes from stream */
656 for (block = 0; block < 4; block++) {
657 int nA, nB, predpred;
658 int pos = scan3x3[block];
660 nA = h->pred_mode_Y[pos - 1];
661 nB = h->pred_mode_Y[pos - 3];
662 predpred = FFMIN(nA, nB);
663 if (predpred == NOT_AVAIL) // if either is not available
664 predpred = INTRA_L_LP;
665 if (!get_bits1(gb)) {
666 int rem_mode = get_bits(gb, 2);
667 predpred = rem_mode + (rem_mode >= predpred);
669 h->pred_mode_Y[pos] = predpred;
671 pred_mode_uv = get_ue_golomb(gb);
672 if (pred_mode_uv > 6) {
673 av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
674 return AVERROR_INVALIDDATA;
676 ff_cavs_modify_mb_i(h, &pred_mode_uv);
678 /* get coded block pattern */
679 if (h->cur.f->pict_type == AV_PICTURE_TYPE_I)
680 cbp_code = get_ue_golomb(gb);
681 if (cbp_code > 63U) {
682 av_log(h->avctx, AV_LOG_ERROR, "illegal intra cbp\n");
683 return AVERROR_INVALIDDATA;
685 h->cbp = cbp_tab[cbp_code][0];
686 if (h->cbp && !h->qp_fixed)
687 h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
689 /* luma intra prediction interleaved with residual decode/transform/add */
690 for (block = 0; block < 4; block++) {
691 d = h->cy + h->luma_scan[block];
692 ff_cavs_load_intra_pred_luma(h, top, &left, block);
693 h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
694 (d, top, left, h->l_stride);
695 if (h->cbp & (1<<block))
696 decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
699 /* chroma intra prediction */
700 ff_cavs_load_intra_pred_chroma(h);
701 h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx * 10],
702 h->left_border_u, h->c_stride);
703 h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx * 10],
704 h->left_border_v, h->c_stride);
706 decode_residual_chroma(h);
707 ff_cavs_filter(h, I_8X8);
712 static inline void set_intra_mode_default(AVSContext *h)
714 if (h->stream_revision > 0) {
715 h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
716 h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = NOT_AVAIL;
718 h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
719 h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = INTRA_L_LP;
723 static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
725 GetBitContext *gb = &h->gb;
731 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
734 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
735 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, ref[0]);
738 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
739 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
740 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
741 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
744 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
745 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
746 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
747 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
750 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
751 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
752 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
753 ref[3] = h->ref_flag ? 0 : get_bits1(gb);
754 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
755 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
756 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
757 ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
759 ff_cavs_inter(h, mb_type);
760 set_intra_mode_default(h);
762 if (mb_type != P_SKIP)
763 decode_residual_inter(h);
764 ff_cavs_filter(h, mb_type);
765 h->col_type_base[h->mbidx] = mb_type;
768 static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
771 enum cavs_sub_mb sub_type[4];
777 h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
778 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
779 h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
780 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
784 if (!h->col_type_base[h->mbidx]) {
785 /* intra MB at co-location, do in-plane prediction */
786 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
787 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
789 /* direct prediction from co-located P MB, block-wise */
790 for (block = 0; block < 4; block++)
791 mv_pred_direct(h, &h->mv[mv_scan[block]],
792 &h->col_mv[h->mbidx * 4 + block]);
795 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
798 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
799 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
802 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
805 #define TMP_UNUSED_INX 7
807 for (block = 0; block < 4; block++)
808 sub_type[block] = get_bits(&h->gb, 2);
809 for (block = 0; block < 4; block++) {
810 switch (sub_type[block]) {
812 if (!h->col_type_base[h->mbidx]) {
813 /* intra MB at co-location, do in-plane prediction */
815 // if col-MB is a Intra MB, current Block size is 16x16.
816 // AVS standard section 9.9.1
818 h->mv[TMP_UNUSED_INX ] = h->mv[MV_FWD_X0 ];
819 h->mv[TMP_UNUSED_INX + MV_BWD_OFFS] = h->mv[MV_FWD_X0 + MV_BWD_OFFS];
821 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2,
822 MV_PRED_BSKIP, BLK_8X8, 1);
823 ff_cavs_mv(h, MV_FWD_X0+MV_BWD_OFFS,
824 MV_FWD_C2+MV_BWD_OFFS,
825 MV_PRED_BSKIP, BLK_8X8, 0);
827 flags = mv_scan[block];
828 h->mv[flags ] = h->mv[MV_FWD_X0 ];
829 h->mv[flags + MV_BWD_OFFS] = h->mv[MV_FWD_X0 + MV_BWD_OFFS];
830 h->mv[MV_FWD_X0 ] = h->mv[TMP_UNUSED_INX ];
831 h->mv[MV_FWD_X0 + MV_BWD_OFFS] = h->mv[TMP_UNUSED_INX + MV_BWD_OFFS];
835 h->mv[mv_scan[block] ] = h->mv[flags ];
836 h->mv[mv_scan[block] + MV_BWD_OFFS] = h->mv[flags + MV_BWD_OFFS];
839 mv_pred_direct(h, &h->mv[mv_scan[block]],
840 &h->col_mv[h->mbidx * 4 + block]);
843 ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
844 MV_PRED_MEDIAN, BLK_8X8, 1);
847 ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
848 MV_PRED_MEDIAN, BLK_8X8, 1);
849 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
853 #undef TMP_UNUSED_INX
854 for (block = 0; block < 4; block++) {
855 if (sub_type[block] == B_SUB_BWD)
856 ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
857 mv_scan[block] + MV_BWD_OFFS - 3,
858 MV_PRED_MEDIAN, BLK_8X8, 0);
862 if (mb_type <= B_SYM_16X16) {
863 av_log(h->avctx, AV_LOG_ERROR, "Invalid mb_type %d in B frame\n", mb_type);
864 return AVERROR_INVALIDDATA;
866 av_assert2(mb_type < B_8X8);
867 flags = ff_cavs_partition_flags[mb_type];
868 if (mb_type & 1) { /* 16x8 macroblock types */
870 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
872 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
874 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
876 mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
878 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
880 ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
881 } else { /* 8x16 macroblock types */
883 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
885 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
887 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 1);
889 mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
891 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
893 ff_cavs_mv(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 0);
896 ff_cavs_inter(h, mb_type);
897 set_intra_mode_default(h);
898 if (mb_type != B_SKIP)
899 decode_residual_inter(h);
900 ff_cavs_filter(h, mb_type);
905 /*****************************************************************************
909 ****************************************************************************/
911 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb)
914 av_log(h->avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
916 if (h->stc >= h->mb_height) {
917 av_log(h->avctx, AV_LOG_ERROR, "stc 0x%02x is too large\n", h->stc);
918 return AVERROR_INVALIDDATA;
922 h->mbidx = h->mby * h->mb_width;
924 /* mark top macroblocks as unavailable */
925 h->flags &= ~(B_AVAIL | C_AVAIL);
926 if (!h->pic_qp_fixed) {
927 h->qp_fixed = get_bits1(gb);
928 h->qp = get_bits(gb, 6);
930 /* inter frame or second slice can have weighting params */
931 if ((h->cur.f->pict_type != AV_PICTURE_TYPE_I) ||
932 (!h->pic_structure && h->mby >= h->mb_width / 2))
933 if (get_bits1(gb)) { //slice_weighting_flag
934 av_log(h->avctx, AV_LOG_ERROR,
935 "weighted prediction not yet supported\n");
940 static inline int check_for_slice(AVSContext *h)
942 GetBitContext *gb = &h->gb;
947 align = (-get_bits_count(gb)) & 7;
948 /* check for stuffing byte */
949 if (!align && (show_bits(gb, 8) == 0x80))
951 if ((show_bits_long(gb, 24 + align) & 0xFFFFFF) == 0x000001) {
952 skip_bits_long(gb, 24 + align);
953 h->stc = get_bits(gb, 8);
954 if (h->stc >= h->mb_height)
956 decode_slice_header(h, gb);
962 /*****************************************************************************
966 ****************************************************************************/
968 static int decode_pic(AVSContext *h)
972 enum cavs_mb mb_type;
975 av_log(h->avctx, AV_LOG_ERROR, "No sequence header decoded yet\n");
976 return AVERROR_INVALIDDATA;
979 av_frame_unref(h->cur.f);
981 skip_bits(&h->gb, 16);//bbv_dwlay
982 if (h->stc == PIC_PB_START_CODE) {
983 h->cur.f->pict_type = get_bits(&h->gb, 2) + AV_PICTURE_TYPE_I;
984 if (h->cur.f->pict_type > AV_PICTURE_TYPE_B) {
985 av_log(h->avctx, AV_LOG_ERROR, "illegal picture type\n");
986 return AVERROR_INVALIDDATA;
988 /* make sure we have the reference frames we need */
989 if (!h->DPB[0].f->data[0] ||
990 (!h->DPB[1].f->data[0] && h->cur.f->pict_type == AV_PICTURE_TYPE_B))
991 return AVERROR_INVALIDDATA;
993 h->cur.f->pict_type = AV_PICTURE_TYPE_I;
994 if (get_bits1(&h->gb))
995 skip_bits(&h->gb, 24);//time_code
996 /* old sample clips were all progressive and no low_delay,
997 bump stream revision if detected otherwise */
998 if (h->low_delay || !(show_bits(&h->gb, 9) & 1))
999 h->stream_revision = 1;
1000 /* similarly test top_field_first and repeat_first_field */
1001 else if (show_bits(&h->gb, 11) & 3)
1002 h->stream_revision = 1;
1003 if (h->stream_revision > 0)
1004 skip_bits(&h->gb, 1); //marker_bit
1007 ret = ff_get_buffer(h->avctx, h->cur.f, h->cur.f->pict_type == AV_PICTURE_TYPE_B ?
1008 0 : AV_GET_BUFFER_FLAG_REF);
1012 if (!h->edge_emu_buffer) {
1013 int alloc_size = FFALIGN(FFABS(h->cur.f->linesize[0]) + 32, 32);
1014 h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 24);
1015 if (!h->edge_emu_buffer)
1016 return AVERROR(ENOMEM);
1019 if ((ret = ff_cavs_init_pic(h)) < 0)
1021 h->cur.poc = get_bits(&h->gb, 8) * 2;
1023 /* get temporal distances and MV scaling factors */
1024 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1025 h->dist[0] = (h->cur.poc - h->DPB[0].poc) & 511;
1027 h->dist[0] = (h->DPB[0].poc - h->cur.poc) & 511;
1029 h->dist[1] = (h->cur.poc - h->DPB[1].poc) & 511;
1030 h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
1031 h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
1032 if (h->cur.f->pict_type == AV_PICTURE_TYPE_B) {
1033 h->sym_factor = h->dist[0] * h->scale_den[1];
1035 h->direct_den[0] = h->dist[0] ? 16384 / h->dist[0] : 0;
1036 h->direct_den[1] = h->dist[1] ? 16384 / h->dist[1] : 0;
1040 get_ue_golomb(&h->gb); //bbv_check_times
1041 h->progressive = get_bits1(&h->gb);
1042 h->pic_structure = 1;
1043 if (!h->progressive)
1044 h->pic_structure = get_bits1(&h->gb);
1045 if (!h->pic_structure && h->stc == PIC_PB_START_CODE)
1046 skip_bits1(&h->gb); //advanced_pred_mode_disable
1047 skip_bits1(&h->gb); //top_field_first
1048 skip_bits1(&h->gb); //repeat_first_field
1050 h->qp_fixed = get_bits1(&h->gb);
1051 h->qp = get_bits(&h->gb, 6);
1052 if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1053 if (!h->progressive && !h->pic_structure)
1054 skip_bits1(&h->gb);//what is this?
1055 skip_bits(&h->gb, 4); //reserved bits
1057 if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
1058 h->ref_flag = get_bits1(&h->gb);
1059 skip_bits(&h->gb, 4); //reserved bits
1060 h->skip_mode_flag = get_bits1(&h->gb);
1062 h->loop_filter_disable = get_bits1(&h->gb);
1063 if (!h->loop_filter_disable && get_bits1(&h->gb)) {
1064 h->alpha_offset = get_se_golomb(&h->gb);
1065 h->beta_offset = get_se_golomb(&h->gb);
1067 h->alpha_offset = h->beta_offset = 0;
1069 if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1073 } while (ff_cavs_next_mb(h));
1074 } else if (h->cur.f->pict_type == AV_PICTURE_TYPE_P) {
1076 if (check_for_slice(h))
1078 if (h->skip_mode_flag && (skip_count < 0))
1079 skip_count = get_ue_golomb(&h->gb);
1080 if (h->skip_mode_flag && skip_count--) {
1081 decode_mb_p(h, P_SKIP);
1083 mb_type = get_ue_golomb(&h->gb) + P_SKIP + h->skip_mode_flag;
1084 if (mb_type > P_8X8)
1085 decode_mb_i(h, mb_type - P_8X8 - 1);
1087 decode_mb_p(h, mb_type);
1089 } while (ff_cavs_next_mb(h));
1090 } else { /* AV_PICTURE_TYPE_B */
1092 if (check_for_slice(h))
1094 if (h->skip_mode_flag && (skip_count < 0))
1095 skip_count = get_ue_golomb(&h->gb);
1096 if (h->skip_mode_flag && skip_count--) {
1097 decode_mb_b(h, B_SKIP);
1099 mb_type = get_ue_golomb(&h->gb) + B_SKIP + h->skip_mode_flag;
1100 if (mb_type > B_8X8)
1101 decode_mb_i(h, mb_type - B_8X8 - 1);
1103 decode_mb_b(h, mb_type);
1105 } while (ff_cavs_next_mb(h));
1107 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1108 av_frame_unref(h->DPB[1].f);
1109 FFSWAP(AVSFrame, h->cur, h->DPB[1]);
1110 FFSWAP(AVSFrame, h->DPB[0], h->DPB[1]);
1115 /*****************************************************************************
1117 * headers and interface
1119 ****************************************************************************/
1121 static int decode_seq_header(AVSContext *h)
1123 int frame_rate_code;
1127 h->profile = get_bits(&h->gb, 8);
1128 h->level = get_bits(&h->gb, 8);
1129 skip_bits1(&h->gb); //progressive sequence
1131 width = get_bits(&h->gb, 14);
1132 height = get_bits(&h->gb, 14);
1133 if ((h->width || h->height) && (h->width != width || h->height != height)) {
1134 avpriv_report_missing_feature(h->avctx,
1135 "Width/height changing in CAVS");
1136 return AVERROR_PATCHWELCOME;
1138 if (width <= 0 || height <= 0) {
1139 av_log(h->avctx, AV_LOG_ERROR, "Dimensions invalid\n");
1140 return AVERROR_INVALIDDATA;
1142 skip_bits(&h->gb, 2); //chroma format
1143 skip_bits(&h->gb, 3); //sample_precision
1144 h->aspect_ratio = get_bits(&h->gb, 4);
1145 frame_rate_code = get_bits(&h->gb, 4);
1146 if (frame_rate_code == 0 || frame_rate_code > 13) {
1147 av_log(h->avctx, AV_LOG_WARNING,
1148 "frame_rate_code %d is invalid\n", frame_rate_code);
1149 frame_rate_code = 1;
1152 skip_bits(&h->gb, 18); //bit_rate_lower
1153 skip_bits1(&h->gb); //marker_bit
1154 skip_bits(&h->gb, 12); //bit_rate_upper
1155 h->low_delay = get_bits1(&h->gb);
1157 ret = ff_set_dimensions(h->avctx, width, height);
1163 h->mb_width = (h->width + 15) >> 4;
1164 h->mb_height = (h->height + 15) >> 4;
1165 h->avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];
1167 return ff_cavs_init_top_lines(h);
1171 static void cavs_flush(AVCodecContext * avctx)
1173 AVSContext *h = avctx->priv_data;
1174 h->got_keyframe = 0;
1177 static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1180 AVSContext *h = avctx->priv_data;
1181 const uint8_t *buf = avpkt->data;
1182 int buf_size = avpkt->size;
1184 int input_size, ret;
1185 const uint8_t *buf_end;
1186 const uint8_t *buf_ptr;
1188 if (buf_size == 0) {
1189 if (!h->low_delay && h->DPB[0].f->data[0]) {
1191 av_frame_move_ref(data, h->DPB[0].f);
1199 buf_end = buf + buf_size;
1201 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
1202 if ((stc & 0xFFFFFE00) || buf_ptr == buf_end) {
1204 av_log(h->avctx, AV_LOG_WARNING, "no frame decoded\n");
1205 return FFMAX(0, buf_ptr - buf);
1207 input_size = (buf_end - buf_ptr) * 8;
1209 case CAVS_START_CODE:
1210 init_get_bits(&h->gb, buf_ptr, input_size);
1211 decode_seq_header(h);
1213 case PIC_I_START_CODE:
1214 if (!h->got_keyframe) {
1215 av_frame_unref(h->DPB[0].f);
1216 av_frame_unref(h->DPB[1].f);
1217 h->got_keyframe = 1;
1219 case PIC_PB_START_CODE:
1221 if (!h->got_keyframe)
1223 init_get_bits(&h->gb, buf_ptr, input_size);
1228 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1229 if (h->DPB[!h->low_delay].f->data[0]) {
1230 if ((ret = av_frame_ref(data, h->DPB[!h->low_delay].f)) < 0)
1236 av_frame_move_ref(data, h->cur.f);
1239 case EXT_START_CODE:
1240 //mpeg_decode_extension(avctx, buf_ptr, input_size);
1242 case USER_START_CODE:
1243 //mpeg_decode_user_data(avctx, buf_ptr, input_size);
1246 if (stc <= SLICE_MAX_START_CODE) {
1247 init_get_bits(&h->gb, buf_ptr, input_size);
1248 decode_slice_header(h, &h->gb);
1255 AVCodec ff_cavs_decoder = {
1257 .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
1258 .type = AVMEDIA_TYPE_VIDEO,
1259 .id = AV_CODEC_ID_CAVS,
1260 .priv_data_size = sizeof(AVSContext),
1261 .init = ff_cavs_init,
1262 .close = ff_cavs_end,
1263 .decode = cavs_decode_frame,
1264 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1265 .flush = cavs_flush,