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"
35 #include "mpegvideo.h"
37 static const uint8_t mv_scan[4] = {
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 }
53 static const uint8_t scan3x3[4] = { 4, 5, 7, 8 };
55 static const uint8_t cavs_chroma_qp[64] = {
56 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
57 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
58 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 42, 43, 43, 44, 44,
59 45, 45, 46, 46, 47, 47, 48, 48, 48, 49, 49, 49, 50, 50, 50, 51
62 static const uint8_t dequant_shift[64] = {
63 14, 14, 14, 14, 14, 14, 14, 14,
64 13, 13, 13, 13, 13, 13, 13, 13,
65 13, 12, 12, 12, 12, 12, 12, 12,
66 11, 11, 11, 11, 11, 11, 11, 11,
67 11, 10, 10, 10, 10, 10, 10, 10,
68 10, 9, 9, 9, 9, 9, 9, 9,
69 9, 8, 8, 8, 8, 8, 8, 8,
70 7, 7, 7, 7, 7, 7, 7, 7
73 static const uint16_t dequant_mul[64] = {
74 32768, 36061, 38968, 42495, 46341, 50535, 55437, 60424,
75 32932, 35734, 38968, 42495, 46177, 50535, 55109, 59933,
76 65535, 35734, 38968, 42577, 46341, 50617, 55027, 60097,
77 32809, 35734, 38968, 42454, 46382, 50576, 55109, 60056,
78 65535, 35734, 38968, 42495, 46320, 50515, 55109, 60076,
79 65535, 35744, 38968, 42495, 46341, 50535, 55099, 60087,
80 65535, 35734, 38973, 42500, 46341, 50535, 55109, 60097,
81 32771, 35734, 38965, 42497, 46341, 50535, 55109, 60099
86 static const struct dec_2dvlc intra_dec[7] = {
88 { //level / run / table_inc
89 { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
90 { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
91 { 1, 7, 1 }, { -1, 7, 1 }, { 1, 8, 1 }, { -1, 8, 1 }, { 1, 9, 1 }, { -1, 9, 1 },
92 { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 }, { 2, 1, 2 }, { -2, 1, 2 },
93 { 1, 12, 1 }, { -1, 12, 1 }, { 1, 13, 1 }, { -1, 13, 1 }, { 1, 14, 1 }, { -1, 14, 1 },
94 { 1, 15, 1 }, { -1, 15, 1 }, { 2, 2, 2 }, { -2, 2, 2 }, { 1, 16, 1 }, { -1, 16, 1 },
95 { 1, 17, 1 }, { -1, 17, 1 }, { 3, 1, 3 }, { -3, 1, 3 }, { 1, 18, 1 }, { -1, 18, 1 },
96 { 1, 19, 1 }, { -1, 19, 1 }, { 2, 3, 2 }, { -2, 3, 2 }, { 1, 20, 1 }, { -1, 20, 1 },
97 { 1, 21, 1 }, { -1, 21, 1 }, { 2, 4, 2 }, { -2, 4, 2 }, { 1, 22, 1 }, { -1, 22, 1 },
98 { 2, 5, 2 }, { -2, 5, 2 }, { 1, 23, 1 }, { -1, 23, 1 }, { EOB }
101 { 0, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1 },
108 { 1, 1, 0 }, { -1, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 1 }, { -2, 1, 1 },
109 { 1, 3, 0 }, { -1, 3, 0 }, { EOB }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 },
110 { -1, 5, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 3, 1, 2 }, { -3, 1, 2 }, { 2, 2, 1 },
111 { -2, 2, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 1, 9, 0 },
112 { -1, 9, 0 }, { 2, 3, 1 }, { -2, 3, 1 }, { 4, 1, 2 }, { -4, 1, 2 }, { 1, 10, 0 },
113 { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 2, 4, 1 }, { -2, 4, 1 }, { 3, 2, 2 },
114 { -3, 2, 2 }, { 1, 12, 0 }, { -1, 12, 0 }, { 2, 5, 1 }, { -2, 5, 1 }, { 5, 1, 3 },
115 { -5, 1, 3 }, { 1, 13, 0 }, { -1, 13, 0 }, { 2, 6, 1 }, { -2, 6, 1 }, { 1, 14, 0 },
116 { -1, 14, 0 }, { 2, 7, 1 }, { -2, 7, 1 }, { 2, 8, 1 }, { -2, 8, 1 }, { 3, 3, 2 },
117 { -3, 3, 2 }, { 6, 1, 3 }, { -6, 1, 3 }, { 1, 15, 0 }, { -1, 15, 0 }
120 { 0, 7, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
127 { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 },
128 { 3, 1, 1 }, { -3, 1, 1 }, { EOB }, { 1, 3, 0 }, { -1, 3, 0 }, { 2, 2, 0 },
129 { -2, 2, 0 }, { 4, 1, 1 }, { -4, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 2 },
130 { -5, 1, 2 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 3, 0 },
131 { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 2 }, { -6, 1, 2 }, { 2, 4, 0 },
132 { -2, 4, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 7, 1, 2 },
133 { -7, 1, 2 }, { 3, 3, 1 }, { -3, 3, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 8, 0 },
134 { -1, 8, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 8, 1, 3 }, { -8, 1, 3 }, { 1, 9, 0 },
135 { -1, 9, 0 }, { 5, 2, 2 }, { -5, 2, 2 }, { 3, 4, 1 }, { -3, 4, 1 }, { 2, 7, 0 },
136 { -2, 7, 0 }, { 9, 1, 3 }, { -9, 1, 3 }, { 1, 10, 0 }, { -1, 10, 0 }
139 { 0, 10, 6, 4, 4, 3, 3, 3, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
146 { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 }, { -3, 1, 0 },
147 { 1, 2, 0 }, { -1, 2, 0 }, { EOB }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 1 },
148 { -5, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 6, 1, 1 },
149 { -6, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 1, 4, 0 },
150 { -1, 4, 0 }, { 8, 1, 2 }, { -8, 1, 2 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 2, 0 },
151 { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 9, 1, 2 }, { -9, 1, 2 }, { 5, 2, 1 },
152 { -5, 2, 1 }, { 2, 4, 0 }, { -2, 4, 0 }, { 10, 1, 2 }, {-10, 1, 2 }, { 3, 3, 0 },
153 { -3, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 11, 1, 3 }, {-11, 1, 3 }, { 6, 2, 1 },
154 { -6, 2, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 2, 5, 0 }, { -2, 5, 0 }, { 3, 4, 0 },
155 { -3, 4, 0 }, { 12, 1, 3 }, {-12, 1, 3 }, { 4, 3, 0 }, { -4, 3, 0 }
158 { 0, 13, 7, 5, 4, 3, 2, 2, -1, -1, -1 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
165 { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 }, { -3, 1, 0 },
166 { EOB }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
167 { -6, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 1 },
168 { -8, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 9, 1, 1 }, { -9, 1, 1 }, { 10, 1, 1 },
169 {-10, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 11, 1, 2 },
170 {-11, 1, 2 }, { 4, 2, 0 }, { -4, 2, 0 }, { 12, 1, 2 }, {-12, 1, 2 }, { 13, 1, 2 },
171 {-13, 1, 2 }, { 5, 2, 0 }, { -5, 2, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 2, 3, 0 },
172 { -2, 3, 0 }, { 14, 1, 2 }, {-14, 1, 2 }, { 6, 2, 0 }, { -6, 2, 0 }, { 15, 1, 2 },
173 {-15, 1, 2 }, { 16, 1, 2 }, {-16, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 1, 5, 0 },
174 { -1, 5, 0 }, { 7, 2, 0 }, { -7, 2, 0 }, { 17, 1, 2 }, {-17, 1, 2 }
177 { 0,18, 8, 4, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
184 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
185 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
186 { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 9, 1, 0 },
187 { -9, 1, 0 }, { 10, 1, 0 }, {-10, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 11, 1, 1 },
188 {-11, 1, 1 }, { 12, 1, 1 }, {-12, 1, 1 }, { 13, 1, 1 }, {-13, 1, 1 }, { 2, 2, 0 },
189 { -2, 2, 0 }, { 14, 1, 1 }, {-14, 1, 1 }, { 15, 1, 1 }, {-15, 1, 1 }, { 3, 2, 0 },
190 { -3, 2, 0 }, { 16, 1, 1 }, {-16, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 17, 1, 1 },
191 {-17, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 18, 1, 1 }, {-18, 1, 1 }, { 5, 2, 0 },
192 { -5, 2, 0 }, { 19, 1, 1 }, {-19, 1, 1 }, { 20, 1, 1 }, {-20, 1, 1 }, { 6, 2, 0 },
193 { -6, 2, 0 }, { 21, 1, 1 }, {-21, 1, 1 }, { 2, 3, 0 }, { -2, 3, 0 }
196 { 0, 22, 7, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
203 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
204 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
205 { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 9, 1, 0 },
206 { -9, 1, 0 }, { 10, 1, 0 }, {-10, 1, 0 }, { 11, 1, 0 }, {-11, 1, 0 }, { 12, 1, 0 },
207 {-12, 1, 0 }, { 13, 1, 0 }, {-13, 1, 0 }, { 14, 1, 0 }, {-14, 1, 0 }, { 15, 1, 0 },
208 {-15, 1, 0 }, { 16, 1, 0 }, {-16, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 17, 1, 0 },
209 {-17, 1, 0 }, { 18, 1, 0 }, {-18, 1, 0 }, { 19, 1, 0 }, {-19, 1, 0 }, { 20, 1, 0 },
210 {-20, 1, 0 }, { 21, 1, 0 }, {-21, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 22, 1, 0 },
211 {-22, 1, 0 }, { 23, 1, 0 }, {-23, 1, 0 }, { 24, 1, 0 }, {-24, 1, 0 }, { 25, 1, 0 },
212 {-25, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 26, 1, 0 }, {-26, 1, 0 }
215 { 0, 27, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
222 static const struct dec_2dvlc inter_dec[7] = {
225 { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
226 { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
227 { 1, 7, 1 }, { -1, 7, 1 }, { 1, 8, 1 }, { -1, 8, 1 }, { 1, 9, 1 }, { -1, 9, 1 },
228 { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 }, { 1, 12, 1 }, { -1, 12, 1 },
229 { 1, 13, 1 }, { -1, 13, 1 }, { 2, 1, 2 }, { -2, 1, 2 }, { 1, 14, 1 }, { -1, 14, 1 },
230 { 1, 15, 1 }, { -1, 15, 1 }, { 1, 16, 1 }, { -1, 16, 1 }, { 1, 17, 1 }, { -1, 17, 1 },
231 { 1, 18, 1 }, { -1, 18, 1 }, { 1, 19, 1 }, { -1, 19, 1 }, { 3, 1, 3 }, { -3, 1, 3 },
232 { 1, 20, 1 }, { -1, 20, 1 }, { 1, 21, 1 }, { -1, 21, 1 }, { 2, 2, 2 }, { -2, 2, 2 },
233 { 1, 22, 1 }, { -1, 22, 1 }, { 1, 23, 1 }, { -1, 23, 1 }, { 1, 24, 1 }, { -1, 24, 1 },
234 { 1, 25, 1 }, { -1, 25, 1 }, { 1, 26, 1 }, { -1, 26, 1 }, { EOB }
237 { 0, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
244 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 1, 2, 0 }, { -1, 2, 0 }, { 1, 3, 0 },
245 { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 1, 6, 0 },
246 { -1, 6, 0 }, { 2, 1, 1 }, { -2, 1, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 1, 8, 0 },
247 { -1, 8, 0 }, { 1, 9, 0 }, { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 2, 2, 1 },
248 { -2, 2, 1 }, { 1, 11, 0 }, { -1, 11, 0 }, { 1, 12, 0 }, { -1, 12, 0 }, { 3, 1, 2 },
249 { -3, 1, 2 }, { 1, 13, 0 }, { -1, 13, 0 }, { 1, 14, 0 }, { -1, 14, 0 }, { 2, 3, 1 },
250 { -2, 3, 1 }, { 1, 15, 0 }, { -1, 15, 0 }, { 2, 4, 1 }, { -2, 4, 1 }, { 1, 16, 0 },
251 { -1, 16, 0 }, { 2, 5, 1 }, { -2, 5, 1 }, { 1, 17, 0 }, { -1, 17, 0 }, { 4, 1, 3 },
252 { -4, 1, 3 }, { 2, 6, 1 }, { -2, 6, 1 }, { 1, 18, 0 }, { -1, 18, 0 }, { 1, 19, 0 },
253 { -1, 19, 0 }, { 2, 7, 1 }, { -2, 7, 1 }, { 3, 2, 2 }, { -3, 2, 2 }
256 { 0, 5, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1 },
263 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 0 },
264 { -2, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 3, 1, 1 },
265 { -3, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 1, 6, 0 },
266 { -1, 6, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 1, 2 },
267 { -4, 1, 2 }, { 1, 8, 0 }, { -1, 8, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 4, 0 },
268 { -2, 4, 0 }, { 1, 9, 0 }, { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 5, 1, 2 },
269 { -5, 1, 2 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 2, 6, 0 },
270 { -2, 6, 0 }, { 1, 12, 0 }, { -1, 12, 0 }, { 3, 3, 1 }, { -3, 3, 1 }, { 6, 1, 2 },
271 { -6, 1, 2 }, { 4, 2, 2 }, { -4, 2, 2 }, { 1, 13, 0 }, { -1, 13, 0 }, { 2, 7, 0 },
272 { -2, 7, 0 }, { 3, 4, 1 }, { -3, 4, 1 }, { 1, 14, 0 }, { -1, 14, 0 }
275 { 0, 7, 5, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
282 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 },
283 { -1, 2, 0 }, { 3, 1, 0 }, { -3, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 2, 2, 0 },
284 { -2, 2, 0 }, { 4, 1, 1 }, { -4, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 1 },
285 { -5, 1, 1 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 2, 3, 0 },
286 { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 1 }, { -6, 1, 1 }, { 2, 4, 0 },
287 { -2, 4, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 7, 1, 2 },
288 { -7, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 2, 5, 0 },
289 { -2, 5, 0 }, { 8, 1, 2 }, { -8, 1, 2 }, { 1, 9, 0 }, { -1, 9, 0 }, { 3, 4, 0 },
290 { -3, 4, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 5, 2, 1 }, { -5, 2, 1 }, { 1, 10, 0 },
291 { -1, 10, 0 }, { 9, 1, 2 }, { -9, 1, 2 }, { 4, 3, 1 }, { -4, 3, 1 }
294 { 0,10, 6, 5, 4, 3, 3, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
301 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
302 { -3, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 },
303 { -5, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 6, 1, 0 },
304 { -6, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 1, 4, 0 },
305 { -1, 4, 0 }, { 8, 1, 1 }, { -8, 1, 1 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 2, 0 },
306 { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 9, 1, 1 }, { -9, 1, 1 }, { 5, 2, 0 },
307 { -5, 2, 0 }, { 2, 4, 0 }, { -2, 4, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 10, 1, 2 },
308 {-10, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 11, 1, 2 }, {-11, 1, 2 }, { 1, 7, 0 },
309 { -1, 7, 0 }, { 6, 2, 0 }, { -6, 2, 0 }, { 3, 4, 0 }, { -3, 4, 0 }, { 2, 5, 0 },
310 { -2, 5, 0 }, { 12, 1, 2 }, {-12, 1, 2 }, { 4, 3, 0 }, { -4, 3, 0 }
313 { 0, 13, 7, 5, 4, 3, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
320 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
321 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 1, 2, 0 },
322 { -1, 2, 0 }, { 6, 1, 0 }, { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 },
323 { -8, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 1, 3, 0 },
324 { -1, 3, 0 }, { 10, 1, 1 }, { -10, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 11, 1, 1 },
325 { -11, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 12, 1, 1 }, { -12, 1, 1 }, { 1, 4, 0 },
326 { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 13, 1, 1 }, { -13, 1, 1 }, { 5, 2, 0 },
327 { -5, 2, 0 }, { 14, 1, 1 }, { -14, 1, 1 }, { 6, 2, 0 }, { -6, 2, 0 }, { 1, 5, 0 },
328 { -1, 5, 0 }, { 15, 1, 1 }, { -15, 1, 1 }, { 3, 3, 0 }, { -3, 3, 0 }, { 16, 1, 1 },
329 { -16, 1, 1 }, { 2, 4, 0 }, { -2, 4, 0 }, { 7, 2, 0 }, { -7, 2, 0 }
332 { 0, 17, 8, 4, 3, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
339 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
340 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
341 { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 8, 1, 0 },
342 { -8, 1, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 10, 1, 0 }, { -10, 1, 0 }, { 11, 1, 0 },
343 { -11, 1, 0 }, { 12, 1, 0 }, { -12, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 13, 1, 0 },
344 { -13, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 14, 1, 0 }, { -14, 1, 0 }, { 15, 1, 0 },
345 { -15, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 16, 1, 0 }, { -16, 1, 0 }, { 17, 1, 0 },
346 { -17, 1, 0 }, { 18, 1, 0 }, { -18, 1, 0 }, { 4, 2, 0 }, { -4, 2, 0 }, { 19, 1, 0 },
347 { -19, 1, 0 }, { 20, 1, 0 }, { -20, 1, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 1, 4, 0 },
348 { -1, 4, 0 }, { 5, 2, 0 }, { -5, 2, 0 }, { 21, 1, 0 }, { -21, 1, 0 }
351 { 0, 22, 6, 3, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
358 static const struct dec_2dvlc chroma_dec[5] = {
361 { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
362 { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
363 { 1, 7, 1 }, { -1, 7, 1 }, { 2, 1, 2 }, { -2, 1, 2 }, { 1, 8, 1 }, { -1, 8, 1 },
364 { 1, 9, 1 }, { -1, 9, 1 }, { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 },
365 { 1, 12, 1 }, { -1, 12, 1 }, { 1, 13, 1 }, { -1, 13, 1 }, { 1, 14, 1 }, { -1, 14, 1 },
366 { 1, 15, 1 }, { -1, 15, 1 }, { 3, 1, 3 }, { -3, 1, 3 }, { 1, 16, 1 }, { -1, 16, 1 },
367 { 1, 17, 1 }, { -1, 17, 1 }, { 1, 18, 1 }, { -1, 18, 1 }, { 1, 19, 1 }, { -1, 19, 1 },
368 { 1, 20, 1 }, { -1, 20, 1 }, { 1, 21, 1 }, { -1, 21, 1 }, { 1, 22, 1 }, { -1, 22, 1 },
369 { 2, 2, 2 }, { -2, 2, 2 }, { 1, 23, 1 }, { -1, 23, 1 }, { 1, 24, 1 }, { -1, 24, 1 },
370 { 1, 25, 1 }, { -1, 25, 1 }, { 4, 1, 3 }, { -4, 1, 3 }, { EOB }
373 { 0, 5, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
380 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 1 },
381 { -2, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 },
382 { -1, 5, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 3, 1, 2 }, { -3, 1, 2 }, { 1, 7, 0 },
383 { -1, 7, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 2, 2, 1 }, { -2, 2, 1 }, { 1, 9, 0 },
384 { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 4, 1, 2 },
385 { -4, 1, 2 }, { 1, 12, 0 }, { -1, 12, 0 }, { 1, 13, 0 }, { -1, 13, 0 }, { 1, 14, 0 },
386 { -1, 14, 0 }, { 2, 3, 1 }, { -2, 3, 1 }, { 1, 15, 0 }, { -1, 15, 0 }, { 2, 4, 1 },
387 { -2, 4, 1 }, { 5, 1, 3 }, { -5, 1, 3 }, { 3, 2, 2 }, { -3, 2, 2 }, { 1, 16, 0 },
388 { -1, 16, 0 }, { 1, 17, 0 }, { -1, 17, 0 }, { 1, 18, 0 }, { -1, 18, 0 }, { 2, 5, 1 },
389 { -2, 5, 1 }, { 1, 19, 0 }, { -1, 19, 0 }, { 1, 20, 0 }, { -1, 20, 0 }
392 { 0, 6, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1 },
399 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 },
400 { -1, 2, 0 }, { 3, 1, 1 }, { -3, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 4, 1, 1 },
401 { -4, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 2 },
402 { -5, 1, 2 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 3, 0 },
403 { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 2 }, { -6, 1, 2 }, { 1, 7, 0 },
404 { -1, 7, 0 }, { 2, 4, 0 }, { -2, 4, 0 }, { 7, 1, 2 }, { -7, 1, 2 }, { 1, 8, 0 },
405 { -1, 8, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 1, 9, 0 }, { -1, 9, 0 }, { 3, 3, 1 },
406 { -3, 3, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 8, 1, 2 },
407 { -8, 1, 2 }, { 1, 10, 0 }, { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 9, 1, 2 },
408 { -9, 1, 2 }, { 5, 2, 2 }, { -5, 2, 2 }, { 3, 4, 1 }, { -3, 4, 1 },
411 { 0,10, 6, 4, 4, 3, 3, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
418 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
419 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 5, 1, 1 },
420 { -5, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 6, 1, 1 }, { -6, 1, 1 }, { 1, 3, 0 },
421 { -1, 3, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 8, 1, 1 },
422 { -8, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 9, 1, 1 },
423 { -9, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 10, 1, 1 },
424 {-10, 1, 1 }, { 3, 3, 0 }, { -3, 3, 0 }, { 5, 2, 1 }, { -5, 2, 1 }, { 2, 4, 0 },
425 { -2, 4, 0 }, { 11, 1, 1 }, {-11, 1, 1 }, { 1, 6, 0 }, { -1, 6, 0 }, { 12, 1, 1 },
426 {-12, 1, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 6, 2, 1 }, { -6, 2, 1 }, { 13, 1, 1 },
427 {-13, 1, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 8, 0 }, { -1, 8, 0 },
430 { 0, 14, 7, 4, 3, 3, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
437 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
438 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
439 { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 1, 2, 0 },
440 { -1, 2, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 10, 1, 0 }, { -10, 1, 0 }, { 11, 1, 0 },
441 { -11, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 12, 1, 0 }, { -12, 1, 0 }, { 13, 1, 0 },
442 { -13, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 14, 1, 0 }, { -14, 1, 0 }, { 1, 3, 0 },
443 { -1, 3, 0 }, { 15, 1, 0 }, { -15, 1, 0 }, { 4, 2, 0 }, { -4, 2, 0 }, { 16, 1, 0 },
444 { -16, 1, 0 }, { 17, 1, 0 }, { -17, 1, 0 }, { 5, 2, 0 }, { -5, 2, 0 }, { 1, 4, 0 },
445 { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 18, 1, 0 }, { -18, 1, 0 }, { 6, 2, 0 },
446 { -6, 2, 0 }, { 19, 1, 0 }, { -19, 1, 0 }, { 1, 5, 0 }, { -1, 5, 0 },
449 { 0, 20, 7, 3, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
458 /*****************************************************************************
460 * motion vector prediction
462 ****************************************************************************/
464 static inline void store_mvs(AVSContext *h)
466 h->col_mv[h->mbidx * 4 + 0] = h->mv[MV_FWD_X0];
467 h->col_mv[h->mbidx * 4 + 1] = h->mv[MV_FWD_X1];
468 h->col_mv[h->mbidx * 4 + 2] = h->mv[MV_FWD_X2];
469 h->col_mv[h->mbidx * 4 + 3] = h->mv[MV_FWD_X3];
472 static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
475 cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
476 int den = h->direct_den[col_mv->ref];
477 int m = col_mv->x >> 31;
479 pmv_fw->dist = h->dist[1];
480 pmv_bw->dist = h->dist[0];
483 /* scale the co-located motion vector according to its temporal span */
484 pmv_fw->x = (((den + (den * col_mv->x * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
485 pmv_bw->x = m - (((den + (den * col_mv->x * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
487 pmv_fw->y = (((den + (den * col_mv->y * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
488 pmv_bw->y = m - (((den + (den * col_mv->y * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
491 static inline void mv_pred_sym(AVSContext *h, cavs_vector *src,
492 enum cavs_block size)
494 cavs_vector *dst = src + MV_BWD_OFFS;
496 /* backward mv is the scaled and negated forward mv */
497 dst->x = -((src->x * h->sym_factor + 256) >> 9);
498 dst->y = -((src->y * h->sym_factor + 256) >> 9);
500 dst->dist = h->dist[0];
504 /*****************************************************************************
506 * residual data decoding
508 ****************************************************************************/
510 /** kth-order exponential golomb code */
511 static inline int get_ue_code(GetBitContext *gb, int order)
513 unsigned ret = get_ue_golomb(gb);
514 if (ret >= ((1U<<31)>>order)) {
515 av_log(NULL, AV_LOG_ERROR, "get_ue_code: value too larger\n");
516 return AVERROR_INVALIDDATA;
519 return (ret<<order) + get_bits(gb, order);
524 static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf,
525 int16_t *dst, int mul, int shift, int coeff_num)
527 int round = 1 << (shift - 1);
529 const uint8_t *scantab = h->scantable.permutated;
531 /* inverse scan and dequantization */
532 while (--coeff_num >= 0) {
533 pos += run_buf[coeff_num];
535 av_log(h->avctx, AV_LOG_ERROR,
536 "position out of block bounds at pic %d MB(%d,%d)\n",
537 h->cur.poc, h->mbx, h->mby);
540 dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
546 * decode coefficients from one 8x8 block, dequantize, inverse transform
547 * and add them to sample block
548 * @param r pointer to 2D VLC table
549 * @param esc_golomb_order escape codes are k-golomb with this order k
550 * @param qp quantizer
551 * @param dst location of sample block
552 * @param stride line stride in frame buffer
554 static int decode_residual_block(AVSContext *h, GetBitContext *gb,
555 const struct dec_2dvlc *r, int esc_golomb_order,
556 int qp, uint8_t *dst, int stride)
558 int i, esc_code, level, mask;
559 unsigned int level_code, run;
560 int16_t level_buf[65];
562 int16_t *block = h->block;
564 for (i = 0; i < 65; i++) {
565 level_code = get_ue_code(gb, r->golomb_order);
566 if (level_code >= ESCAPE_CODE) {
567 run = ((level_code - ESCAPE_CODE) >> 1) + 1;
570 esc_code = get_ue_code(gb, esc_golomb_order);
571 level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
572 while (level > r->inc_limit)
574 mask = -(level_code & 1);
575 level = (level ^ mask) - mask;
577 level = r->rltab[level_code][0];
578 if (!level) //end of block signal
580 run = r->rltab[level_code][1];
581 r += r->rltab[level_code][2];
583 level_buf[i] = level;
586 if (dequant(h, level_buf, run_buf, block, dequant_mul[qp],
587 dequant_shift[qp], i))
589 h->cdsp.cavs_idct8_add(dst, block, stride);
590 h->dsp.clear_block(block);
595 static inline void decode_residual_chroma(AVSContext *h)
597 if (h->cbp & (1 << 4))
598 decode_residual_block(h, &h->gb, chroma_dec, 0,
599 cavs_chroma_qp[h->qp], h->cu, h->c_stride);
600 if (h->cbp & (1 << 5))
601 decode_residual_block(h, &h->gb, chroma_dec, 0,
602 cavs_chroma_qp[h->qp], h->cv, h->c_stride);
605 static inline int decode_residual_inter(AVSContext *h)
609 /* get coded block pattern */
610 int cbp = get_ue_golomb(&h->gb);
612 av_log(h->avctx, AV_LOG_ERROR, "illegal inter cbp\n");
615 h->cbp = cbp_tab[cbp][1];
618 if (h->cbp && !h->qp_fixed)
619 h->qp = (h->qp + get_se_golomb(&h->gb)) & 63;
620 for (block = 0; block < 4; block++)
621 if (h->cbp & (1 << block))
622 decode_residual_block(h, &h->gb, inter_dec, 0, h->qp,
623 h->cy + h->luma_scan[block], h->l_stride);
624 decode_residual_chroma(h);
629 /*****************************************************************************
633 ****************************************************************************/
635 static inline void set_mv_intra(AVSContext *h)
637 h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
638 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
639 h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
640 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
641 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B)
642 h->col_type_base[h->mbidx] = I_8X8;
645 static int decode_mb_i(AVSContext *h, int cbp_code)
647 GetBitContext *gb = &h->gb;
648 unsigned pred_mode_uv;
651 uint8_t *left = NULL;
656 /* get intra prediction modes from stream */
657 for (block = 0; block < 4; block++) {
658 int nA, nB, predpred;
659 int pos = scan3x3[block];
661 nA = h->pred_mode_Y[pos - 1];
662 nB = h->pred_mode_Y[pos - 3];
663 predpred = FFMIN(nA, nB);
664 if (predpred == NOT_AVAIL) // if either is not available
665 predpred = INTRA_L_LP;
666 if (!get_bits1(gb)) {
667 int rem_mode = get_bits(gb, 2);
668 predpred = rem_mode + (rem_mode >= predpred);
670 h->pred_mode_Y[pos] = predpred;
672 pred_mode_uv = get_ue_golomb(gb);
673 if (pred_mode_uv > 6) {
674 av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
677 ff_cavs_modify_mb_i(h, &pred_mode_uv);
679 /* get coded block pattern */
680 if (h->cur.f->pict_type == AV_PICTURE_TYPE_I)
681 cbp_code = get_ue_golomb(gb);
682 if (cbp_code > 63U) {
683 av_log(h->avctx, AV_LOG_ERROR, "illegal intra cbp\n");
686 h->cbp = cbp_tab[cbp_code][0];
687 if (h->cbp && !h->qp_fixed)
688 h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
690 /* luma intra prediction interleaved with residual decode/transform/add */
691 for (block = 0; block < 4; block++) {
692 d = h->cy + h->luma_scan[block];
693 ff_cavs_load_intra_pred_luma(h, top, &left, block);
694 h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
695 (d, top, left, h->l_stride);
696 if (h->cbp & (1<<block))
697 decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
700 /* chroma intra prediction */
701 ff_cavs_load_intra_pred_chroma(h);
702 h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx * 10],
703 h->left_border_u, h->c_stride);
704 h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx * 10],
705 h->left_border_v, h->c_stride);
707 decode_residual_chroma(h);
708 ff_cavs_filter(h, I_8X8);
713 static inline void set_intra_mode_default(AVSContext *h)
715 if (h->stream_revision > 0) {
716 h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
717 h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = NOT_AVAIL;
719 h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
720 h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = INTRA_L_LP;
724 static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
726 GetBitContext *gb = &h->gb;
732 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
735 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
736 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, ref[0]);
739 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
740 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
741 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
742 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
745 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
746 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
747 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
748 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
751 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
752 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
753 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
754 ref[3] = h->ref_flag ? 0 : get_bits1(gb);
755 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
756 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
757 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
758 ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
760 ff_cavs_inter(h, mb_type);
761 set_intra_mode_default(h);
763 if (mb_type != P_SKIP)
764 decode_residual_inter(h);
765 ff_cavs_filter(h, mb_type);
766 h->col_type_base[h->mbidx] = mb_type;
769 static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
772 enum cavs_sub_mb sub_type[4];
778 h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
779 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
780 h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
781 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
785 if (!h->col_type_base[h->mbidx]) {
786 /* intra MB at co-location, do in-plane prediction */
787 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
788 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
790 /* direct prediction from co-located P MB, block-wise */
791 for (block = 0; block < 4; block++)
792 mv_pred_direct(h, &h->mv[mv_scan[block]],
793 &h->col_mv[h->mbidx * 4 + block]);
796 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
799 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
800 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
803 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
806 for (block = 0; block < 4; block++)
807 sub_type[block] = get_bits(&h->gb, 2);
808 for (block = 0; block < 4; block++) {
809 switch (sub_type[block]) {
811 if (!h->col_type_base[h->mbidx]) {
812 /* intra MB at co-location, do in-plane prediction */
813 ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
814 MV_PRED_BSKIP, BLK_8X8, 1);
815 ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
816 mv_scan[block] - 3 + MV_BWD_OFFS,
817 MV_PRED_BSKIP, BLK_8X8, 0);
819 mv_pred_direct(h, &h->mv[mv_scan[block]],
820 &h->col_mv[h->mbidx * 4 + block]);
823 ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
824 MV_PRED_MEDIAN, BLK_8X8, 1);
827 ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
828 MV_PRED_MEDIAN, BLK_8X8, 1);
829 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
833 for (block = 0; block < 4; block++) {
834 if (sub_type[block] == B_SUB_BWD)
835 ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
836 mv_scan[block] + MV_BWD_OFFS - 3,
837 MV_PRED_MEDIAN, BLK_8X8, 0);
841 if (mb_type <= B_SYM_16X16) {
842 av_log(h->avctx, AV_LOG_ERROR, "Invalid mb_type %d in B frame\n", mb_type);
843 return AVERROR_INVALIDDATA;
845 av_assert2(mb_type < B_8X8);
846 flags = ff_cavs_partition_flags[mb_type];
847 if (mb_type & 1) { /* 16x8 macroblock types */
849 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
851 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
853 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
855 mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
857 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
859 ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
860 } else { /* 8x16 macroblock types */
862 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
864 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
866 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 1);
868 mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
870 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
872 ff_cavs_mv(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 0);
875 ff_cavs_inter(h, mb_type);
876 set_intra_mode_default(h);
877 if (mb_type != B_SKIP)
878 decode_residual_inter(h);
879 ff_cavs_filter(h, mb_type);
884 /*****************************************************************************
888 ****************************************************************************/
890 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb)
893 av_log(h->avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
895 if (h->stc >= h->mb_height)
899 h->mbidx = h->mby * h->mb_width;
901 /* mark top macroblocks as unavailable */
902 h->flags &= ~(B_AVAIL | C_AVAIL);
903 if ((h->mby == 0) && (!h->qp_fixed)) {
904 h->qp_fixed = get_bits1(gb);
905 h->qp = get_bits(gb, 6);
907 /* inter frame or second slice can have weighting params */
908 if ((h->cur.f->pict_type != AV_PICTURE_TYPE_I) ||
909 (!h->pic_structure && h->mby >= h->mb_width / 2))
910 if (get_bits1(gb)) { //slice_weighting_flag
911 av_log(h->avctx, AV_LOG_ERROR,
912 "weighted prediction not yet supported\n");
917 static inline int check_for_slice(AVSContext *h)
919 GetBitContext *gb = &h->gb;
924 align = (-get_bits_count(gb)) & 7;
925 /* check for stuffing byte */
926 if (!align && (show_bits(gb, 8) == 0x80))
928 if ((show_bits_long(gb, 24 + align) & 0xFFFFFF) == 0x000001) {
929 skip_bits_long(gb, 24 + align);
930 h->stc = get_bits(gb, 8);
931 if (h->stc >= h->mb_height)
933 decode_slice_header(h, gb);
939 /*****************************************************************************
943 ****************************************************************************/
945 static int decode_pic(AVSContext *h)
949 enum cavs_mb mb_type;
952 av_log(h->avctx, AV_LOG_ERROR, "No sequence header decoded yet\n");
953 return AVERROR_INVALIDDATA;
956 av_frame_unref(h->cur.f);
958 skip_bits(&h->gb, 16);//bbv_dwlay
959 if (h->stc == PIC_PB_START_CODE) {
960 h->cur.f->pict_type = get_bits(&h->gb, 2) + AV_PICTURE_TYPE_I;
961 if (h->cur.f->pict_type > AV_PICTURE_TYPE_B) {
962 av_log(h->avctx, AV_LOG_ERROR, "illegal picture type\n");
965 /* make sure we have the reference frames we need */
966 if (!h->DPB[0].f->data[0] ||
967 (!h->DPB[1].f->data[0] && h->cur.f->pict_type == AV_PICTURE_TYPE_B))
970 h->cur.f->pict_type = AV_PICTURE_TYPE_I;
971 if (get_bits1(&h->gb))
972 skip_bits(&h->gb, 24);//time_code
973 /* old sample clips were all progressive and no low_delay,
974 bump stream revision if detected otherwise */
975 if (h->low_delay || !(show_bits(&h->gb, 9) & 1))
976 h->stream_revision = 1;
977 /* similarly test top_field_first and repeat_first_field */
978 else if (show_bits(&h->gb, 11) & 3)
979 h->stream_revision = 1;
980 if (h->stream_revision > 0)
981 skip_bits(&h->gb, 1); //marker_bit
984 if ((ret = ff_get_buffer(h->avctx, h->cur.f,
985 h->cur.f->pict_type == AV_PICTURE_TYPE_B ?
986 0 : AV_GET_BUFFER_FLAG_REF)) < 0)
989 if (!h->edge_emu_buffer) {
990 int alloc_size = FFALIGN(FFABS(h->cur.f->linesize[0]) + 32, 32);
991 h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 24);
992 if (!h->edge_emu_buffer)
993 return AVERROR(ENOMEM);
996 if ((ret = ff_cavs_init_pic(h)) < 0)
998 h->cur.poc = get_bits(&h->gb, 8) * 2;
1000 /* get temporal distances and MV scaling factors */
1001 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1002 h->dist[0] = (h->cur.poc - h->DPB[0].poc + 512) % 512;
1004 h->dist[0] = (h->DPB[0].poc - h->cur.poc + 512) % 512;
1006 h->dist[1] = (h->cur.poc - h->DPB[1].poc + 512) % 512;
1007 h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
1008 h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
1009 if (h->cur.f->pict_type == AV_PICTURE_TYPE_B) {
1010 h->sym_factor = h->dist[0] * h->scale_den[1];
1012 h->direct_den[0] = h->dist[0] ? 16384 / h->dist[0] : 0;
1013 h->direct_den[1] = h->dist[1] ? 16384 / h->dist[1] : 0;
1017 get_ue_golomb(&h->gb); //bbv_check_times
1018 h->progressive = get_bits1(&h->gb);
1019 h->pic_structure = 1;
1020 if (!h->progressive)
1021 h->pic_structure = get_bits1(&h->gb);
1022 if (!h->pic_structure && h->stc == PIC_PB_START_CODE)
1023 skip_bits1(&h->gb); //advanced_pred_mode_disable
1024 skip_bits1(&h->gb); //top_field_first
1025 skip_bits1(&h->gb); //repeat_first_field
1026 h->qp_fixed = get_bits1(&h->gb);
1027 h->qp = get_bits(&h->gb, 6);
1028 if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1029 if (!h->progressive && !h->pic_structure)
1030 skip_bits1(&h->gb);//what is this?
1031 skip_bits(&h->gb, 4); //reserved bits
1033 if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
1034 h->ref_flag = get_bits1(&h->gb);
1035 skip_bits(&h->gb, 4); //reserved bits
1036 h->skip_mode_flag = get_bits1(&h->gb);
1038 h->loop_filter_disable = get_bits1(&h->gb);
1039 if (!h->loop_filter_disable && get_bits1(&h->gb)) {
1040 h->alpha_offset = get_se_golomb(&h->gb);
1041 h->beta_offset = get_se_golomb(&h->gb);
1043 h->alpha_offset = h->beta_offset = 0;
1045 if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1049 } while (ff_cavs_next_mb(h));
1050 } else if (h->cur.f->pict_type == AV_PICTURE_TYPE_P) {
1052 if (check_for_slice(h))
1054 if (h->skip_mode_flag && (skip_count < 0))
1055 skip_count = get_ue_golomb(&h->gb);
1056 if (h->skip_mode_flag && skip_count--) {
1057 decode_mb_p(h, P_SKIP);
1059 mb_type = get_ue_golomb(&h->gb) + P_SKIP + h->skip_mode_flag;
1060 if (mb_type > P_8X8)
1061 decode_mb_i(h, mb_type - P_8X8 - 1);
1063 decode_mb_p(h, mb_type);
1065 } while (ff_cavs_next_mb(h));
1066 } else { /* AV_PICTURE_TYPE_B */
1068 if (check_for_slice(h))
1070 if (h->skip_mode_flag && (skip_count < 0))
1071 skip_count = get_ue_golomb(&h->gb);
1072 if (h->skip_mode_flag && skip_count--) {
1073 decode_mb_b(h, B_SKIP);
1075 mb_type = get_ue_golomb(&h->gb) + B_SKIP + h->skip_mode_flag;
1076 if (mb_type > B_8X8)
1077 decode_mb_i(h, mb_type - B_8X8 - 1);
1079 decode_mb_b(h, mb_type);
1081 } while (ff_cavs_next_mb(h));
1083 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1084 av_frame_unref(h->DPB[1].f);
1085 FFSWAP(AVSFrame, h->cur, h->DPB[1]);
1086 FFSWAP(AVSFrame, h->DPB[0], h->DPB[1]);
1091 /*****************************************************************************
1093 * headers and interface
1095 ****************************************************************************/
1097 static int decode_seq_header(AVSContext *h)
1099 int frame_rate_code;
1102 h->profile = get_bits(&h->gb, 8);
1103 h->level = get_bits(&h->gb, 8);
1104 skip_bits1(&h->gb); //progressive sequence
1106 width = get_bits(&h->gb, 14);
1107 height = get_bits(&h->gb, 14);
1108 if ((h->width || h->height) && (h->width != width || h->height != height)) {
1109 avpriv_report_missing_feature(h->avctx,
1110 "Width/height changing in CAVS");
1111 return AVERROR_PATCHWELCOME;
1113 if (width <= 0 || height <= 0) {
1114 av_log(h->avctx, AV_LOG_ERROR, "Dimensions invalid\n");
1115 return AVERROR_INVALIDDATA;
1120 skip_bits(&h->gb, 2); //chroma format
1121 skip_bits(&h->gb, 3); //sample_precision
1122 h->aspect_ratio = get_bits(&h->gb, 4);
1123 frame_rate_code = get_bits(&h->gb, 4);
1124 skip_bits(&h->gb, 18); //bit_rate_lower
1125 skip_bits1(&h->gb); //marker_bit
1126 skip_bits(&h->gb, 12); //bit_rate_upper
1127 h->low_delay = get_bits1(&h->gb);
1128 h->mb_width = (h->width + 15) >> 4;
1129 h->mb_height = (h->height + 15) >> 4;
1130 h->avctx->time_base.den = ff_mpeg12_frame_rate_tab[frame_rate_code].num;
1131 h->avctx->time_base.num = ff_mpeg12_frame_rate_tab[frame_rate_code].den;
1132 h->avctx->width = h->width;
1133 h->avctx->height = h->height;
1135 ff_cavs_init_top_lines(h);
1139 static void cavs_flush(AVCodecContext * avctx)
1141 AVSContext *h = avctx->priv_data;
1142 h->got_keyframe = 0;
1145 static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1148 AVSContext *h = avctx->priv_data;
1149 const uint8_t *buf = avpkt->data;
1150 int buf_size = avpkt->size;
1152 int input_size, ret;
1153 const uint8_t *buf_end;
1154 const uint8_t *buf_ptr;
1156 if (buf_size == 0) {
1157 if (!h->low_delay && h->DPB[0].f->data[0]) {
1159 av_frame_move_ref(data, h->DPB[0].f);
1165 buf_end = buf + buf_size;
1167 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
1168 if ((stc & 0xFFFFFE00) || buf_ptr == buf_end)
1169 return FFMAX(0, buf_ptr - buf);
1170 input_size = (buf_end - buf_ptr) * 8;
1172 case CAVS_START_CODE:
1173 init_get_bits(&h->gb, buf_ptr, input_size);
1174 decode_seq_header(h);
1176 case PIC_I_START_CODE:
1177 if (!h->got_keyframe) {
1178 av_frame_unref(h->DPB[0].f);
1179 av_frame_unref(h->DPB[1].f);
1180 h->got_keyframe = 1;
1182 case PIC_PB_START_CODE:
1184 if (!h->got_keyframe)
1186 init_get_bits(&h->gb, buf_ptr, input_size);
1191 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1192 if (h->DPB[1].f->data[0]) {
1193 if ((ret = av_frame_ref(data, h->DPB[1].f)) < 0)
1199 av_frame_move_ref(data, h->cur.f);
1202 case EXT_START_CODE:
1203 //mpeg_decode_extension(avctx, buf_ptr, input_size);
1205 case USER_START_CODE:
1206 //mpeg_decode_user_data(avctx, buf_ptr, input_size);
1209 if (stc <= SLICE_MAX_START_CODE) {
1210 init_get_bits(&h->gb, buf_ptr, input_size);
1211 decode_slice_header(h, &h->gb);
1218 AVCodec ff_cavs_decoder = {
1220 .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
1221 .type = AVMEDIA_TYPE_VIDEO,
1222 .id = AV_CODEC_ID_CAVS,
1223 .priv_data_size = sizeof(AVSContext),
1224 .init = ff_cavs_init,
1225 .close = ff_cavs_end,
1226 .decode = cavs_decode_frame,
1227 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1228 .flush = cavs_flush,