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 Libav.
7 * Libav 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 * Libav 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 Libav; 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>
29 #include "bitstream.h"
33 #include "mpeg12data.h"
35 static const uint8_t mv_scan[4] = {
40 static const uint8_t cbp_tab[64][2] = {
41 { 63, 0 }, { 15, 15 }, { 31, 63 }, { 47, 31 }, { 0, 16 }, { 14, 32 }, { 13, 47 }, { 11, 13 },
42 { 7, 14 }, { 5, 11 }, { 10, 12 }, { 8, 5 }, { 12, 10 }, { 61, 7 }, { 4, 48 }, { 55, 3 },
43 { 1, 2 }, { 2, 8 }, { 59, 4 }, { 3, 1 }, { 62, 61 }, { 9, 55 }, { 6, 59 }, { 29, 62 },
44 { 45, 29 }, { 51, 27 }, { 23, 23 }, { 39, 19 }, { 27, 30 }, { 46, 28 }, { 53, 9 }, { 30, 6 },
45 { 43, 60 }, { 37, 21 }, { 60, 44 }, { 16, 26 }, { 21, 51 }, { 28, 35 }, { 19, 18 }, { 35, 20 },
46 { 42, 24 }, { 26, 53 }, { 44, 17 }, { 32, 37 }, { 58, 39 }, { 24, 45 }, { 20, 58 }, { 17, 43 },
47 { 18, 42 }, { 48, 46 }, { 22, 36 }, { 33, 33 }, { 25, 34 }, { 49, 40 }, { 40, 52 }, { 36, 49 },
48 { 34, 50 }, { 50, 56 }, { 52, 25 }, { 54, 22 }, { 41, 54 }, { 56, 57 }, { 38, 41 }, { 57, 38 }
51 static const uint8_t scan3x3[4] = { 4, 5, 7, 8 };
53 static const uint8_t cavs_chroma_qp[64] = {
54 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
55 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
56 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 42, 43, 43, 44, 44,
57 45, 45, 46, 46, 47, 47, 48, 48, 48, 49, 49, 49, 50, 50, 50, 51
60 static const uint8_t dequant_shift[64] = {
61 14, 14, 14, 14, 14, 14, 14, 14,
62 13, 13, 13, 13, 13, 13, 13, 13,
63 13, 12, 12, 12, 12, 12, 12, 12,
64 11, 11, 11, 11, 11, 11, 11, 11,
65 11, 10, 10, 10, 10, 10, 10, 10,
66 10, 9, 9, 9, 9, 9, 9, 9,
67 9, 8, 8, 8, 8, 8, 8, 8,
68 7, 7, 7, 7, 7, 7, 7, 7
71 static const uint16_t dequant_mul[64] = {
72 32768, 36061, 38968, 42495, 46341, 50535, 55437, 60424,
73 32932, 35734, 38968, 42495, 46177, 50535, 55109, 59933,
74 65535, 35734, 38968, 42577, 46341, 50617, 55027, 60097,
75 32809, 35734, 38968, 42454, 46382, 50576, 55109, 60056,
76 65535, 35734, 38968, 42495, 46320, 50515, 55109, 60076,
77 65535, 35744, 38968, 42495, 46341, 50535, 55099, 60087,
78 65535, 35734, 38973, 42500, 46341, 50535, 55109, 60097,
79 32771, 35734, 38965, 42497, 46341, 50535, 55109, 60099
84 static const struct dec_2dvlc intra_dec[7] = {
86 { //level / run / table_inc
87 { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
88 { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
89 { 1, 7, 1 }, { -1, 7, 1 }, { 1, 8, 1 }, { -1, 8, 1 }, { 1, 9, 1 }, { -1, 9, 1 },
90 { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 }, { 2, 1, 2 }, { -2, 1, 2 },
91 { 1, 12, 1 }, { -1, 12, 1 }, { 1, 13, 1 }, { -1, 13, 1 }, { 1, 14, 1 }, { -1, 14, 1 },
92 { 1, 15, 1 }, { -1, 15, 1 }, { 2, 2, 2 }, { -2, 2, 2 }, { 1, 16, 1 }, { -1, 16, 1 },
93 { 1, 17, 1 }, { -1, 17, 1 }, { 3, 1, 3 }, { -3, 1, 3 }, { 1, 18, 1 }, { -1, 18, 1 },
94 { 1, 19, 1 }, { -1, 19, 1 }, { 2, 3, 2 }, { -2, 3, 2 }, { 1, 20, 1 }, { -1, 20, 1 },
95 { 1, 21, 1 }, { -1, 21, 1 }, { 2, 4, 2 }, { -2, 4, 2 }, { 1, 22, 1 }, { -1, 22, 1 },
96 { 2, 5, 2 }, { -2, 5, 2 }, { 1, 23, 1 }, { -1, 23, 1 }, { EOB }
99 { 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 },
106 { 1, 1, 0 }, { -1, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 1 }, { -2, 1, 1 },
107 { 1, 3, 0 }, { -1, 3, 0 }, { EOB }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 },
108 { -1, 5, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 3, 1, 2 }, { -3, 1, 2 }, { 2, 2, 1 },
109 { -2, 2, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 1, 9, 0 },
110 { -1, 9, 0 }, { 2, 3, 1 }, { -2, 3, 1 }, { 4, 1, 2 }, { -4, 1, 2 }, { 1, 10, 0 },
111 { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 2, 4, 1 }, { -2, 4, 1 }, { 3, 2, 2 },
112 { -3, 2, 2 }, { 1, 12, 0 }, { -1, 12, 0 }, { 2, 5, 1 }, { -2, 5, 1 }, { 5, 1, 3 },
113 { -5, 1, 3 }, { 1, 13, 0 }, { -1, 13, 0 }, { 2, 6, 1 }, { -2, 6, 1 }, { 1, 14, 0 },
114 { -1, 14, 0 }, { 2, 7, 1 }, { -2, 7, 1 }, { 2, 8, 1 }, { -2, 8, 1 }, { 3, 3, 2 },
115 { -3, 3, 2 }, { 6, 1, 3 }, { -6, 1, 3 }, { 1, 15, 0 }, { -1, 15, 0 }
118 { 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 },
125 { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 },
126 { 3, 1, 1 }, { -3, 1, 1 }, { EOB }, { 1, 3, 0 }, { -1, 3, 0 }, { 2, 2, 0 },
127 { -2, 2, 0 }, { 4, 1, 1 }, { -4, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 2 },
128 { -5, 1, 2 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 3, 0 },
129 { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 2 }, { -6, 1, 2 }, { 2, 4, 0 },
130 { -2, 4, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 7, 1, 2 },
131 { -7, 1, 2 }, { 3, 3, 1 }, { -3, 3, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 8, 0 },
132 { -1, 8, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 8, 1, 3 }, { -8, 1, 3 }, { 1, 9, 0 },
133 { -1, 9, 0 }, { 5, 2, 2 }, { -5, 2, 2 }, { 3, 4, 1 }, { -3, 4, 1 }, { 2, 7, 0 },
134 { -2, 7, 0 }, { 9, 1, 3 }, { -9, 1, 3 }, { 1, 10, 0 }, { -1, 10, 0 }
137 { 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 },
144 { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 }, { -3, 1, 0 },
145 { 1, 2, 0 }, { -1, 2, 0 }, { EOB }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 1 },
146 { -5, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 6, 1, 1 },
147 { -6, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 1, 4, 0 },
148 { -1, 4, 0 }, { 8, 1, 2 }, { -8, 1, 2 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 2, 0 },
149 { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 9, 1, 2 }, { -9, 1, 2 }, { 5, 2, 1 },
150 { -5, 2, 1 }, { 2, 4, 0 }, { -2, 4, 0 }, { 10, 1, 2 }, {-10, 1, 2 }, { 3, 3, 0 },
151 { -3, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 11, 1, 3 }, {-11, 1, 3 }, { 6, 2, 1 },
152 { -6, 2, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 2, 5, 0 }, { -2, 5, 0 }, { 3, 4, 0 },
153 { -3, 4, 0 }, { 12, 1, 3 }, {-12, 1, 3 }, { 4, 3, 0 }, { -4, 3, 0 }
156 { 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 },
163 { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 }, { -3, 1, 0 },
164 { EOB }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
165 { -6, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 1 },
166 { -8, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 9, 1, 1 }, { -9, 1, 1 }, { 10, 1, 1 },
167 {-10, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 11, 1, 2 },
168 {-11, 1, 2 }, { 4, 2, 0 }, { -4, 2, 0 }, { 12, 1, 2 }, {-12, 1, 2 }, { 13, 1, 2 },
169 {-13, 1, 2 }, { 5, 2, 0 }, { -5, 2, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 2, 3, 0 },
170 { -2, 3, 0 }, { 14, 1, 2 }, {-14, 1, 2 }, { 6, 2, 0 }, { -6, 2, 0 }, { 15, 1, 2 },
171 {-15, 1, 2 }, { 16, 1, 2 }, {-16, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 1, 5, 0 },
172 { -1, 5, 0 }, { 7, 2, 0 }, { -7, 2, 0 }, { 17, 1, 2 }, {-17, 1, 2 }
175 { 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 },
182 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
183 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
184 { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 9, 1, 0 },
185 { -9, 1, 0 }, { 10, 1, 0 }, {-10, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 11, 1, 1 },
186 {-11, 1, 1 }, { 12, 1, 1 }, {-12, 1, 1 }, { 13, 1, 1 }, {-13, 1, 1 }, { 2, 2, 0 },
187 { -2, 2, 0 }, { 14, 1, 1 }, {-14, 1, 1 }, { 15, 1, 1 }, {-15, 1, 1 }, { 3, 2, 0 },
188 { -3, 2, 0 }, { 16, 1, 1 }, {-16, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 17, 1, 1 },
189 {-17, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 18, 1, 1 }, {-18, 1, 1 }, { 5, 2, 0 },
190 { -5, 2, 0 }, { 19, 1, 1 }, {-19, 1, 1 }, { 20, 1, 1 }, {-20, 1, 1 }, { 6, 2, 0 },
191 { -6, 2, 0 }, { 21, 1, 1 }, {-21, 1, 1 }, { 2, 3, 0 }, { -2, 3, 0 }
194 { 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 },
201 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
202 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
203 { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 9, 1, 0 },
204 { -9, 1, 0 }, { 10, 1, 0 }, {-10, 1, 0 }, { 11, 1, 0 }, {-11, 1, 0 }, { 12, 1, 0 },
205 {-12, 1, 0 }, { 13, 1, 0 }, {-13, 1, 0 }, { 14, 1, 0 }, {-14, 1, 0 }, { 15, 1, 0 },
206 {-15, 1, 0 }, { 16, 1, 0 }, {-16, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 17, 1, 0 },
207 {-17, 1, 0 }, { 18, 1, 0 }, {-18, 1, 0 }, { 19, 1, 0 }, {-19, 1, 0 }, { 20, 1, 0 },
208 {-20, 1, 0 }, { 21, 1, 0 }, {-21, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 22, 1, 0 },
209 {-22, 1, 0 }, { 23, 1, 0 }, {-23, 1, 0 }, { 24, 1, 0 }, {-24, 1, 0 }, { 25, 1, 0 },
210 {-25, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 26, 1, 0 }, {-26, 1, 0 }
213 { 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 },
220 static const struct dec_2dvlc inter_dec[7] = {
223 { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
224 { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
225 { 1, 7, 1 }, { -1, 7, 1 }, { 1, 8, 1 }, { -1, 8, 1 }, { 1, 9, 1 }, { -1, 9, 1 },
226 { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 }, { 1, 12, 1 }, { -1, 12, 1 },
227 { 1, 13, 1 }, { -1, 13, 1 }, { 2, 1, 2 }, { -2, 1, 2 }, { 1, 14, 1 }, { -1, 14, 1 },
228 { 1, 15, 1 }, { -1, 15, 1 }, { 1, 16, 1 }, { -1, 16, 1 }, { 1, 17, 1 }, { -1, 17, 1 },
229 { 1, 18, 1 }, { -1, 18, 1 }, { 1, 19, 1 }, { -1, 19, 1 }, { 3, 1, 3 }, { -3, 1, 3 },
230 { 1, 20, 1 }, { -1, 20, 1 }, { 1, 21, 1 }, { -1, 21, 1 }, { 2, 2, 2 }, { -2, 2, 2 },
231 { 1, 22, 1 }, { -1, 22, 1 }, { 1, 23, 1 }, { -1, 23, 1 }, { 1, 24, 1 }, { -1, 24, 1 },
232 { 1, 25, 1 }, { -1, 25, 1 }, { 1, 26, 1 }, { -1, 26, 1 }, { EOB }
235 { 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 },
242 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 1, 2, 0 }, { -1, 2, 0 }, { 1, 3, 0 },
243 { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 1, 6, 0 },
244 { -1, 6, 0 }, { 2, 1, 1 }, { -2, 1, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 1, 8, 0 },
245 { -1, 8, 0 }, { 1, 9, 0 }, { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 2, 2, 1 },
246 { -2, 2, 1 }, { 1, 11, 0 }, { -1, 11, 0 }, { 1, 12, 0 }, { -1, 12, 0 }, { 3, 1, 2 },
247 { -3, 1, 2 }, { 1, 13, 0 }, { -1, 13, 0 }, { 1, 14, 0 }, { -1, 14, 0 }, { 2, 3, 1 },
248 { -2, 3, 1 }, { 1, 15, 0 }, { -1, 15, 0 }, { 2, 4, 1 }, { -2, 4, 1 }, { 1, 16, 0 },
249 { -1, 16, 0 }, { 2, 5, 1 }, { -2, 5, 1 }, { 1, 17, 0 }, { -1, 17, 0 }, { 4, 1, 3 },
250 { -4, 1, 3 }, { 2, 6, 1 }, { -2, 6, 1 }, { 1, 18, 0 }, { -1, 18, 0 }, { 1, 19, 0 },
251 { -1, 19, 0 }, { 2, 7, 1 }, { -2, 7, 1 }, { 3, 2, 2 }, { -3, 2, 2 }
254 { 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 },
261 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 0 },
262 { -2, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 3, 1, 1 },
263 { -3, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 1, 6, 0 },
264 { -1, 6, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 1, 2 },
265 { -4, 1, 2 }, { 1, 8, 0 }, { -1, 8, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 4, 0 },
266 { -2, 4, 0 }, { 1, 9, 0 }, { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 5, 1, 2 },
267 { -5, 1, 2 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 2, 6, 0 },
268 { -2, 6, 0 }, { 1, 12, 0 }, { -1, 12, 0 }, { 3, 3, 1 }, { -3, 3, 1 }, { 6, 1, 2 },
269 { -6, 1, 2 }, { 4, 2, 2 }, { -4, 2, 2 }, { 1, 13, 0 }, { -1, 13, 0 }, { 2, 7, 0 },
270 { -2, 7, 0 }, { 3, 4, 1 }, { -3, 4, 1 }, { 1, 14, 0 }, { -1, 14, 0 }
273 { 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 },
280 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 },
281 { -1, 2, 0 }, { 3, 1, 0 }, { -3, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 2, 2, 0 },
282 { -2, 2, 0 }, { 4, 1, 1 }, { -4, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 1 },
283 { -5, 1, 1 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 2, 3, 0 },
284 { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 1 }, { -6, 1, 1 }, { 2, 4, 0 },
285 { -2, 4, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 7, 1, 2 },
286 { -7, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 2, 5, 0 },
287 { -2, 5, 0 }, { 8, 1, 2 }, { -8, 1, 2 }, { 1, 9, 0 }, { -1, 9, 0 }, { 3, 4, 0 },
288 { -3, 4, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 5, 2, 1 }, { -5, 2, 1 }, { 1, 10, 0 },
289 { -1, 10, 0 }, { 9, 1, 2 }, { -9, 1, 2 }, { 4, 3, 1 }, { -4, 3, 1 }
292 { 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 },
299 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
300 { -3, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 },
301 { -5, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 6, 1, 0 },
302 { -6, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 1, 4, 0 },
303 { -1, 4, 0 }, { 8, 1, 1 }, { -8, 1, 1 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 2, 0 },
304 { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 9, 1, 1 }, { -9, 1, 1 }, { 5, 2, 0 },
305 { -5, 2, 0 }, { 2, 4, 0 }, { -2, 4, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 10, 1, 2 },
306 {-10, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 11, 1, 2 }, {-11, 1, 2 }, { 1, 7, 0 },
307 { -1, 7, 0 }, { 6, 2, 0 }, { -6, 2, 0 }, { 3, 4, 0 }, { -3, 4, 0 }, { 2, 5, 0 },
308 { -2, 5, 0 }, { 12, 1, 2 }, {-12, 1, 2 }, { 4, 3, 0 }, { -4, 3, 0 }
311 { 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 },
318 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
319 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 1, 2, 0 },
320 { -1, 2, 0 }, { 6, 1, 0 }, { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 },
321 { -8, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 1, 3, 0 },
322 { -1, 3, 0 }, { 10, 1, 1 }, { -10, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 11, 1, 1 },
323 { -11, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 12, 1, 1 }, { -12, 1, 1 }, { 1, 4, 0 },
324 { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 13, 1, 1 }, { -13, 1, 1 }, { 5, 2, 0 },
325 { -5, 2, 0 }, { 14, 1, 1 }, { -14, 1, 1 }, { 6, 2, 0 }, { -6, 2, 0 }, { 1, 5, 0 },
326 { -1, 5, 0 }, { 15, 1, 1 }, { -15, 1, 1 }, { 3, 3, 0 }, { -3, 3, 0 }, { 16, 1, 1 },
327 { -16, 1, 1 }, { 2, 4, 0 }, { -2, 4, 0 }, { 7, 2, 0 }, { -7, 2, 0 }
330 { 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 },
337 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
338 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
339 { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 8, 1, 0 },
340 { -8, 1, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 10, 1, 0 }, { -10, 1, 0 }, { 11, 1, 0 },
341 { -11, 1, 0 }, { 12, 1, 0 }, { -12, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 13, 1, 0 },
342 { -13, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 14, 1, 0 }, { -14, 1, 0 }, { 15, 1, 0 },
343 { -15, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 16, 1, 0 }, { -16, 1, 0 }, { 17, 1, 0 },
344 { -17, 1, 0 }, { 18, 1, 0 }, { -18, 1, 0 }, { 4, 2, 0 }, { -4, 2, 0 }, { 19, 1, 0 },
345 { -19, 1, 0 }, { 20, 1, 0 }, { -20, 1, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 1, 4, 0 },
346 { -1, 4, 0 }, { 5, 2, 0 }, { -5, 2, 0 }, { 21, 1, 0 }, { -21, 1, 0 }
349 { 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 },
356 static const struct dec_2dvlc chroma_dec[5] = {
359 { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
360 { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
361 { 1, 7, 1 }, { -1, 7, 1 }, { 2, 1, 2 }, { -2, 1, 2 }, { 1, 8, 1 }, { -1, 8, 1 },
362 { 1, 9, 1 }, { -1, 9, 1 }, { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 },
363 { 1, 12, 1 }, { -1, 12, 1 }, { 1, 13, 1 }, { -1, 13, 1 }, { 1, 14, 1 }, { -1, 14, 1 },
364 { 1, 15, 1 }, { -1, 15, 1 }, { 3, 1, 3 }, { -3, 1, 3 }, { 1, 16, 1 }, { -1, 16, 1 },
365 { 1, 17, 1 }, { -1, 17, 1 }, { 1, 18, 1 }, { -1, 18, 1 }, { 1, 19, 1 }, { -1, 19, 1 },
366 { 1, 20, 1 }, { -1, 20, 1 }, { 1, 21, 1 }, { -1, 21, 1 }, { 1, 22, 1 }, { -1, 22, 1 },
367 { 2, 2, 2 }, { -2, 2, 2 }, { 1, 23, 1 }, { -1, 23, 1 }, { 1, 24, 1 }, { -1, 24, 1 },
368 { 1, 25, 1 }, { -1, 25, 1 }, { 4, 1, 3 }, { -4, 1, 3 }, { EOB }
371 { 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 },
378 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 1 },
379 { -2, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 },
380 { -1, 5, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 3, 1, 2 }, { -3, 1, 2 }, { 1, 7, 0 },
381 { -1, 7, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 2, 2, 1 }, { -2, 2, 1 }, { 1, 9, 0 },
382 { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 4, 1, 2 },
383 { -4, 1, 2 }, { 1, 12, 0 }, { -1, 12, 0 }, { 1, 13, 0 }, { -1, 13, 0 }, { 1, 14, 0 },
384 { -1, 14, 0 }, { 2, 3, 1 }, { -2, 3, 1 }, { 1, 15, 0 }, { -1, 15, 0 }, { 2, 4, 1 },
385 { -2, 4, 1 }, { 5, 1, 3 }, { -5, 1, 3 }, { 3, 2, 2 }, { -3, 2, 2 }, { 1, 16, 0 },
386 { -1, 16, 0 }, { 1, 17, 0 }, { -1, 17, 0 }, { 1, 18, 0 }, { -1, 18, 0 }, { 2, 5, 1 },
387 { -2, 5, 1 }, { 1, 19, 0 }, { -1, 19, 0 }, { 1, 20, 0 }, { -1, 20, 0 }
390 { 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 },
397 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 },
398 { -1, 2, 0 }, { 3, 1, 1 }, { -3, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 4, 1, 1 },
399 { -4, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 2 },
400 { -5, 1, 2 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 3, 0 },
401 { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 2 }, { -6, 1, 2 }, { 1, 7, 0 },
402 { -1, 7, 0 }, { 2, 4, 0 }, { -2, 4, 0 }, { 7, 1, 2 }, { -7, 1, 2 }, { 1, 8, 0 },
403 { -1, 8, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 1, 9, 0 }, { -1, 9, 0 }, { 3, 3, 1 },
404 { -3, 3, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 8, 1, 2 },
405 { -8, 1, 2 }, { 1, 10, 0 }, { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 9, 1, 2 },
406 { -9, 1, 2 }, { 5, 2, 2 }, { -5, 2, 2 }, { 3, 4, 1 }, { -3, 4, 1 },
409 { 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 },
416 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
417 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 5, 1, 1 },
418 { -5, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 6, 1, 1 }, { -6, 1, 1 }, { 1, 3, 0 },
419 { -1, 3, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 8, 1, 1 },
420 { -8, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 9, 1, 1 },
421 { -9, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 10, 1, 1 },
422 {-10, 1, 1 }, { 3, 3, 0 }, { -3, 3, 0 }, { 5, 2, 1 }, { -5, 2, 1 }, { 2, 4, 0 },
423 { -2, 4, 0 }, { 11, 1, 1 }, {-11, 1, 1 }, { 1, 6, 0 }, { -1, 6, 0 }, { 12, 1, 1 },
424 {-12, 1, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 6, 2, 1 }, { -6, 2, 1 }, { 13, 1, 1 },
425 {-13, 1, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 8, 0 }, { -1, 8, 0 },
428 { 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 },
435 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
436 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
437 { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 1, 2, 0 },
438 { -1, 2, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 10, 1, 0 }, { -10, 1, 0 }, { 11, 1, 0 },
439 { -11, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 12, 1, 0 }, { -12, 1, 0 }, { 13, 1, 0 },
440 { -13, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 14, 1, 0 }, { -14, 1, 0 }, { 1, 3, 0 },
441 { -1, 3, 0 }, { 15, 1, 0 }, { -15, 1, 0 }, { 4, 2, 0 }, { -4, 2, 0 }, { 16, 1, 0 },
442 { -16, 1, 0 }, { 17, 1, 0 }, { -17, 1, 0 }, { 5, 2, 0 }, { -5, 2, 0 }, { 1, 4, 0 },
443 { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 18, 1, 0 }, { -18, 1, 0 }, { 6, 2, 0 },
444 { -6, 2, 0 }, { 19, 1, 0 }, { -19, 1, 0 }, { 1, 5, 0 }, { -1, 5, 0 },
447 { 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 },
456 /*****************************************************************************
458 * motion vector prediction
460 ****************************************************************************/
462 static inline void store_mvs(AVSContext *h)
464 h->col_mv[h->mbidx * 4 + 0] = h->mv[MV_FWD_X0];
465 h->col_mv[h->mbidx * 4 + 1] = h->mv[MV_FWD_X1];
466 h->col_mv[h->mbidx * 4 + 2] = h->mv[MV_FWD_X2];
467 h->col_mv[h->mbidx * 4 + 3] = h->mv[MV_FWD_X3];
470 static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
473 cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
474 int den = h->direct_den[col_mv->ref];
475 int m = FF_SIGNBIT(col_mv->x);
477 pmv_fw->dist = h->dist[1];
478 pmv_bw->dist = h->dist[0];
481 /* scale the co-located motion vector according to its temporal span */
482 pmv_fw->x = (((den + (den * col_mv->x * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
483 pmv_bw->x = m - (((den + (den * col_mv->x * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
484 m = FF_SIGNBIT(col_mv->y);
485 pmv_fw->y = (((den + (den * col_mv->y * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
486 pmv_bw->y = m - (((den + (den * col_mv->y * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
489 static inline void mv_pred_sym(AVSContext *h, cavs_vector *src,
490 enum cavs_block size)
492 cavs_vector *dst = src + MV_BWD_OFFS;
494 /* backward mv is the scaled and negated forward mv */
495 dst->x = -((src->x * h->sym_factor + 256) >> 9);
496 dst->y = -((src->y * h->sym_factor + 256) >> 9);
498 dst->dist = h->dist[0];
502 /*****************************************************************************
504 * residual data decoding
506 ****************************************************************************/
508 /** kth-order exponential golomb code */
509 static inline int get_ue_code(BitstreamContext *bc, int order)
512 int ret = get_ue_golomb(bc) << order;
513 return ret + bitstream_read(bc, order);
515 return get_ue_golomb(bc);
518 static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf,
519 int16_t *dst, int mul, int shift, int coeff_num)
521 int round = 1 << (shift - 1);
523 const uint8_t *scantab = h->scantable.permutated;
525 /* inverse scan and dequantization */
526 while (--coeff_num >= 0) {
527 pos += run_buf[coeff_num];
529 av_log(h->avctx, AV_LOG_ERROR,
530 "position out of block bounds at pic %d MB(%d,%d)\n",
531 h->cur.poc, h->mbx, h->mby);
532 return AVERROR_INVALIDDATA;
534 dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
540 * decode coefficients from one 8x8 block, dequantize, inverse transform
541 * and add them to sample block
542 * @param r pointer to 2D VLC table
543 * @param esc_golomb_order escape codes are k-golomb with this order k
544 * @param qp quantizer
545 * @param dst location of sample block
546 * @param stride line stride in frame buffer
548 static int decode_residual_block(AVSContext *h, BitstreamContext *bc,
549 const struct dec_2dvlc *r, int esc_golomb_order,
550 int qp, uint8_t *dst, ptrdiff_t stride)
552 int i, level_code, esc_code, level, run, mask, ret;
553 int16_t level_buf[65];
555 int16_t *block = h->block;
557 for (i = 0;i < 65; i++) {
558 level_code = get_ue_code(bc, r->golomb_order);
559 if (level_code >= ESCAPE_CODE) {
560 run = ((level_code - ESCAPE_CODE) >> 1) + 1;
561 esc_code = get_ue_code(bc, esc_golomb_order);
562 level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
563 while (level > r->inc_limit)
565 mask = -(level_code & 1);
566 level = (level ^ mask) - mask;
567 } else if (level_code >= 0) {
568 level = r->rltab[level_code][0];
569 if (!level) //end of block signal
571 run = r->rltab[level_code][1];
572 r += r->rltab[level_code][2];
576 level_buf[i] = level;
579 if ((ret = dequant(h, level_buf, run_buf, block, dequant_mul[qp],
580 dequant_shift[qp], i)) < 0)
582 h->cdsp.cavs_idct8_add(dst, block, stride);
583 h->bdsp.clear_block(block);
588 static inline void decode_residual_chroma(AVSContext *h)
590 if (h->cbp & (1 << 4))
591 decode_residual_block(h, &h->bc, chroma_dec, 0,
592 cavs_chroma_qp[h->qp], h->cu, h->c_stride);
593 if (h->cbp & (1 << 5))
594 decode_residual_block(h, &h->bc, chroma_dec, 0,
595 cavs_chroma_qp[h->qp], h->cv, h->c_stride);
598 static inline int decode_residual_inter(AVSContext *h)
602 /* get coded block pattern */
603 int cbp = get_ue_golomb(&h->bc);
604 if (cbp > 63 || cbp < 0) {
605 av_log(h->avctx, AV_LOG_ERROR, "illegal inter cbp %d\n", cbp);
606 return AVERROR_INVALIDDATA;
608 h->cbp = cbp_tab[cbp][1];
611 if (h->cbp && !h->qp_fixed)
612 h->qp = (h->qp + get_se_golomb(&h->bc)) & 63;
613 for (block = 0; block < 4; block++)
614 if (h->cbp & (1 << block))
615 decode_residual_block(h, &h->bc, inter_dec, 0, h->qp,
616 h->cy + h->luma_scan[block], h->l_stride);
617 decode_residual_chroma(h);
622 /*****************************************************************************
626 ****************************************************************************/
628 static inline void set_mv_intra(AVSContext *h)
630 h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
631 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
632 h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
633 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
634 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B)
635 h->col_type_base[h->mbidx] = I_8X8;
638 static int decode_mb_i(AVSContext *h, int cbp_code)
640 BitstreamContext *bc = &h->bc;
641 unsigned pred_mode_uv;
644 uint8_t *left = NULL;
649 /* get intra prediction modes from stream */
650 for (block = 0; block < 4; block++) {
651 int nA, nB, predpred;
652 int pos = scan3x3[block];
654 nA = h->pred_mode_Y[pos - 1];
655 nB = h->pred_mode_Y[pos - 3];
656 predpred = FFMIN(nA, nB);
657 if (predpred == NOT_AVAIL) // if either is not available
658 predpred = INTRA_L_LP;
659 if (!bitstream_read_bit(bc)) {
660 int rem_mode = bitstream_read(bc, 2);
661 predpred = rem_mode + (rem_mode >= predpred);
663 h->pred_mode_Y[pos] = predpred;
665 pred_mode_uv = get_ue_golomb(bc);
666 if (pred_mode_uv > 6) {
667 av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
668 return AVERROR_INVALIDDATA;
670 ff_cavs_modify_mb_i(h, &pred_mode_uv);
672 /* get coded block pattern */
673 if (h->cur.f->pict_type == AV_PICTURE_TYPE_I)
674 cbp_code = get_ue_golomb(bc);
675 if (cbp_code > 63 || cbp_code < 0) {
676 av_log(h->avctx, AV_LOG_ERROR, "illegal intra cbp\n");
677 return AVERROR_INVALIDDATA;
679 h->cbp = cbp_tab[cbp_code][0];
680 if (h->cbp && !h->qp_fixed)
681 h->qp = (h->qp + get_se_golomb(bc)) & 63; // qp_delta
683 /* luma intra prediction interleaved with residual decode/transform/add */
684 for (block = 0; block < 4; block++) {
685 d = h->cy + h->luma_scan[block];
686 ff_cavs_load_intra_pred_luma(h, top, &left, block);
687 h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
688 (d, top, left, h->l_stride);
689 if (h->cbp & (1<<block))
690 decode_residual_block(h, bc, intra_dec, 1, h->qp, d, h->l_stride);
693 /* chroma intra prediction */
694 ff_cavs_load_intra_pred_chroma(h);
695 h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx * 10],
696 h->left_border_u, h->c_stride);
697 h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx * 10],
698 h->left_border_v, h->c_stride);
700 decode_residual_chroma(h);
701 ff_cavs_filter(h, I_8X8);
706 static inline void set_intra_mode_default(AVSContext *h)
708 if (h->stream_revision > 0) {
709 h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
710 h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = NOT_AVAIL;
712 h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
713 h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = INTRA_L_LP;
717 static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
719 BitstreamContext *bc = &h->bc;
725 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
728 ref[0] = h->ref_flag ? 0 : bitstream_read_bit(bc);
729 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, ref[0]);
732 ref[0] = h->ref_flag ? 0 : bitstream_read_bit(bc);
733 ref[2] = h->ref_flag ? 0 : bitstream_read_bit(bc);
734 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
735 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
738 ref[0] = h->ref_flag ? 0 : bitstream_read_bit(bc);
739 ref[1] = h->ref_flag ? 0 : bitstream_read_bit(bc);
740 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
741 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
744 ref[0] = h->ref_flag ? 0 : bitstream_read_bit(bc);
745 ref[1] = h->ref_flag ? 0 : bitstream_read_bit(bc);
746 ref[2] = h->ref_flag ? 0 : bitstream_read_bit(bc);
747 ref[3] = h->ref_flag ? 0 : bitstream_read_bit(bc);
748 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
749 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
750 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
751 ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
753 ff_cavs_inter(h, mb_type);
754 set_intra_mode_default(h);
756 if (mb_type != P_SKIP)
757 decode_residual_inter(h);
758 ff_cavs_filter(h, mb_type);
759 h->col_type_base[h->mbidx] = mb_type;
762 static void decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
765 enum cavs_sub_mb sub_type[4];
771 h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
772 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
773 h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
774 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
778 if (!h->col_type_base[h->mbidx]) {
779 /* intra MB at co-location, do in-plane prediction */
780 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
781 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
783 /* direct prediction from co-located P MB, block-wise */
784 for (block = 0; block < 4; block++)
785 mv_pred_direct(h, &h->mv[mv_scan[block]],
786 &h->col_mv[h->mbidx * 4 + block]);
789 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
792 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
793 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
796 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
799 for (block = 0; block < 4; block++)
800 sub_type[block] = bitstream_read(&h->bc, 2);
801 for (block = 0; block < 4; block++) {
802 switch (sub_type[block]) {
804 if (!h->col_type_base[h->mbidx]) {
805 /* intra MB at co-location, do in-plane prediction */
806 ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
807 MV_PRED_BSKIP, BLK_8X8, 1);
808 ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
809 mv_scan[block] - 3 + MV_BWD_OFFS,
810 MV_PRED_BSKIP, BLK_8X8, 0);
812 mv_pred_direct(h, &h->mv[mv_scan[block]],
813 &h->col_mv[h->mbidx * 4 + block]);
816 ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
817 MV_PRED_MEDIAN, BLK_8X8, 1);
820 ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
821 MV_PRED_MEDIAN, BLK_8X8, 1);
822 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
826 for (block = 0; block < 4; block++) {
827 if (sub_type[block] == B_SUB_BWD)
828 ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
829 mv_scan[block] + MV_BWD_OFFS - 3,
830 MV_PRED_MEDIAN, BLK_8X8, 0);
834 assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
835 flags = ff_cavs_partition_flags[mb_type];
836 if (mb_type & 1) { /* 16x8 macroblock types */
838 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
840 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
842 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
844 mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
846 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
848 ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
849 } else { /* 8x16 macroblock types */
851 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
853 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
855 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 1);
857 mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
859 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
861 ff_cavs_mv(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 0);
864 ff_cavs_inter(h, mb_type);
865 set_intra_mode_default(h);
866 if (mb_type != B_SKIP)
867 decode_residual_inter(h);
868 ff_cavs_filter(h, mb_type);
871 /*****************************************************************************
875 ****************************************************************************/
877 static inline int decode_slice_header(AVSContext *h, BitstreamContext *bc)
880 av_log(h->avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
882 h->mbidx = h->mby * h->mb_width;
884 /* mark top macroblocks as unavailable */
885 h->flags &= ~(B_AVAIL | C_AVAIL);
886 if ((h->mby == 0) && (!h->qp_fixed)) {
887 h->qp_fixed = bitstream_read_bit(bc);
888 h->qp = bitstream_read(bc, 6);
890 /* inter frame or second slice can have weighting params */
891 if ((h->cur.f->pict_type != AV_PICTURE_TYPE_I) ||
892 (!h->pic_structure && h->mby >= h->mb_width / 2))
893 if (bitstream_read_bit(bc)) { // slice_weighting_flag
894 av_log(h->avctx, AV_LOG_ERROR,
895 "weighted prediction not yet supported\n");
900 static inline int check_for_slice(AVSContext *h)
902 BitstreamContext *bc = &h->bc;
907 align = (-bitstream_tell(bc)) & 7;
908 /* check for stuffing byte */
909 if (!align && (bitstream_peek(bc, 8) == 0x80))
911 if ((bitstream_peek(bc, 24 + align) & 0xFFFFFF) == 0x000001) {
912 bitstream_skip(bc, 24 + align);
913 h->stc = bitstream_read(bc, 8);
914 if (h->stc >= h->mb_height)
916 decode_slice_header(h, bc);
922 /*****************************************************************************
926 ****************************************************************************/
928 static int decode_pic(AVSContext *h)
932 enum cavs_mb mb_type;
935 av_log(h->avctx, AV_LOG_ERROR, "No sequence header decoded yet\n");
936 return AVERROR_INVALIDDATA;
939 av_frame_unref(h->cur.f);
941 bitstream_skip(&h->bc, 16); // bbv_dwlay
942 if (h->stc == PIC_PB_START_CODE) {
943 h->cur.f->pict_type = bitstream_read(&h->bc, 2) + AV_PICTURE_TYPE_I;
944 if (h->cur.f->pict_type > AV_PICTURE_TYPE_B) {
945 av_log(h->avctx, AV_LOG_ERROR, "illegal picture type\n");
946 return AVERROR_INVALIDDATA;
948 /* make sure we have the reference frames we need */
949 if (!h->DPB[0].f->data[0] ||
950 (!h->DPB[1].f->data[0] && h->cur.f->pict_type == AV_PICTURE_TYPE_B))
951 return AVERROR_INVALIDDATA;
953 h->cur.f->pict_type = AV_PICTURE_TYPE_I;
954 if (bitstream_read_bit(&h->bc))
955 bitstream_skip(&h->bc, 24); // time_code
956 /* old sample clips were all progressive and no low_delay,
957 bump stream revision if detected otherwise */
958 if (h->low_delay || !(bitstream_peek(&h->bc, 9) & 1))
959 h->stream_revision = 1;
960 /* similarly test top_field_first and repeat_first_field */
961 else if (bitstream_peek(&h->bc, 11) & 3)
962 h->stream_revision = 1;
963 if (h->stream_revision > 0)
964 bitstream_skip(&h->bc, 1); // marker_bit
967 ret = ff_get_buffer(h->avctx, h->cur.f, h->cur.f->pict_type == AV_PICTURE_TYPE_B ?
968 0 : AV_GET_BUFFER_FLAG_REF);
972 if (!h->edge_emu_buffer) {
973 int alloc_size = FFALIGN(FFABS(h->cur.f->linesize[0]) + 32, 32);
974 h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 24);
975 if (!h->edge_emu_buffer)
976 return AVERROR(ENOMEM);
980 h->cur.poc = bitstream_read(&h->bc, 8) * 2;
982 /* get temporal distances and MV scaling factors */
983 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
984 h->dist[0] = (h->cur.poc - h->DPB[0].poc + 512) % 512;
986 h->dist[0] = (h->DPB[0].poc - h->cur.poc + 512) % 512;
988 h->dist[1] = (h->cur.poc - h->DPB[1].poc + 512) % 512;
989 h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
990 h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
991 if (h->cur.f->pict_type == AV_PICTURE_TYPE_B) {
992 h->sym_factor = h->dist[0] * h->scale_den[1];
994 h->direct_den[0] = h->dist[0] ? 16384 / h->dist[0] : 0;
995 h->direct_den[1] = h->dist[1] ? 16384 / h->dist[1] : 0;
999 get_ue_golomb(&h->bc); // bbv_check_times
1000 h->progressive = bitstream_read_bit(&h->bc);
1001 h->pic_structure = 1;
1002 if (!h->progressive)
1003 h->pic_structure = bitstream_read_bit(&h->bc);
1004 if (!h->pic_structure && h->stc == PIC_PB_START_CODE)
1005 bitstream_skip(&h->bc, 1); // advanced_pred_mode_disable
1006 bitstream_skip(&h->bc, 1); // top_field_first
1007 bitstream_skip(&h->bc, 1); // repeat_first_field
1008 h->qp_fixed = bitstream_read_bit(&h->bc);
1009 h->qp = bitstream_read(&h->bc, 6);
1010 if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1011 if (!h->progressive && !h->pic_structure)
1012 bitstream_skip(&h->bc, 1); // what is this?
1013 bitstream_skip(&h->bc, 4); // reserved bits
1015 if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
1016 h->ref_flag = bitstream_read_bit(&h->bc);
1017 bitstream_skip(&h->bc, 4); // reserved bits
1018 h->skip_mode_flag = bitstream_read_bit(&h->bc);
1020 h->loop_filter_disable = bitstream_read_bit(&h->bc);
1021 if (!h->loop_filter_disable && bitstream_read_bit(&h->bc)) {
1022 h->alpha_offset = get_se_golomb(&h->bc);
1023 h->beta_offset = get_se_golomb(&h->bc);
1025 h->alpha_offset = h->beta_offset = 0;
1027 if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1031 } while (ff_cavs_next_mb(h));
1032 } else if (h->cur.f->pict_type == AV_PICTURE_TYPE_P) {
1034 if (check_for_slice(h))
1036 if (h->skip_mode_flag && (skip_count < 0))
1037 skip_count = get_ue_golomb(&h->bc);
1038 if (h->skip_mode_flag && skip_count--) {
1039 decode_mb_p(h, P_SKIP);
1041 mb_type = get_ue_golomb(&h->bc) + P_SKIP + h->skip_mode_flag;
1042 if (mb_type > P_8X8)
1043 decode_mb_i(h, mb_type - P_8X8 - 1);
1045 decode_mb_p(h, mb_type);
1047 } while (ff_cavs_next_mb(h));
1048 } else { /* AV_PICTURE_TYPE_B */
1050 if (check_for_slice(h))
1052 if (h->skip_mode_flag && (skip_count < 0))
1053 skip_count = get_ue_golomb(&h->bc);
1054 if (h->skip_mode_flag && skip_count--) {
1055 decode_mb_b(h, B_SKIP);
1057 mb_type = get_ue_golomb(&h->bc) + B_SKIP + h->skip_mode_flag;
1058 if (mb_type > B_8X8)
1059 decode_mb_i(h, mb_type - B_8X8 - 1);
1061 decode_mb_b(h, mb_type);
1063 } while (ff_cavs_next_mb(h));
1065 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1066 av_frame_unref(h->DPB[1].f);
1067 FFSWAP(AVSFrame, h->cur, h->DPB[1]);
1068 FFSWAP(AVSFrame, h->DPB[0], h->DPB[1]);
1073 /*****************************************************************************
1075 * headers and interface
1077 ****************************************************************************/
1079 static int decode_seq_header(AVSContext *h)
1081 int frame_rate_code;
1084 h->profile = bitstream_read(&h->bc, 8);
1085 h->level = bitstream_read(&h->bc, 8);
1086 bitstream_skip(&h->bc, 1); // progressive sequence
1088 width = bitstream_read(&h->bc, 14);
1089 height = bitstream_read(&h->bc, 14);
1090 if ((h->width || h->height) && (h->width != width || h->height != height)) {
1091 avpriv_report_missing_feature(h->avctx,
1092 "Width/height changing in CAVS");
1093 return AVERROR_PATCHWELCOME;
1098 bitstream_skip(&h->bc, 2); // chroma format
1099 bitstream_skip(&h->bc, 3); // sample_precision
1100 h->aspect_ratio = bitstream_read(&h->bc, 4);
1101 frame_rate_code = bitstream_read(&h->bc, 4);
1102 bitstream_skip(&h->bc, 18); // bit_rate_lower
1103 bitstream_skip(&h->bc, 1); // marker_bit
1104 bitstream_skip(&h->bc, 12); // bit_rate_upper
1105 h->low_delay = bitstream_read_bit(&h->bc);
1106 h->mb_width = (h->width + 15) >> 4;
1107 h->mb_height = (h->height + 15) >> 4;
1108 h->avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];
1109 h->avctx->width = h->width;
1110 h->avctx->height = h->height;
1112 ff_cavs_init_top_lines(h);
1116 static void cavs_flush(AVCodecContext * avctx)
1118 AVSContext *h = avctx->priv_data;
1119 h->got_keyframe = 0;
1122 static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1125 AVSContext *h = avctx->priv_data;
1126 const uint8_t *buf = avpkt->data;
1127 int buf_size = avpkt->size;
1129 int input_size, ret;
1130 const uint8_t *buf_end;
1131 const uint8_t *buf_ptr;
1133 if (buf_size == 0) {
1134 if (!h->low_delay && h->DPB[0].f->data[0]) {
1136 av_frame_move_ref(data, h->DPB[0].f);
1142 buf_end = buf + buf_size;
1144 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
1145 if ((stc & 0xFFFFFE00) || buf_ptr == buf_end)
1146 return FFMAX(0, buf_ptr - buf);
1147 input_size = (buf_end - buf_ptr) * 8;
1149 case CAVS_START_CODE:
1150 bitstream_init(&h->bc, buf_ptr, input_size);
1151 decode_seq_header(h);
1153 case PIC_I_START_CODE:
1154 if (!h->got_keyframe) {
1155 av_frame_unref(h->DPB[0].f);
1156 av_frame_unref(h->DPB[1].f);
1157 h->got_keyframe = 1;
1159 case PIC_PB_START_CODE:
1161 if (!h->got_keyframe)
1163 bitstream_init(&h->bc, buf_ptr, input_size);
1168 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1169 if (h->DPB[1].f->data[0]) {
1170 if ((ret = av_frame_ref(data, h->DPB[1].f)) < 0)
1176 av_frame_move_ref(data, h->cur.f);
1179 case EXT_START_CODE:
1180 //mpeg_decode_extension(avctx, buf_ptr, input_size);
1182 case USER_START_CODE:
1183 //mpeg_decode_user_data(avctx, buf_ptr, input_size);
1186 if (stc <= SLICE_MAX_START_CODE) {
1187 bitstream_init(&h->bc, buf_ptr, input_size);
1188 decode_slice_header(h, &h->bc);
1195 AVCodec ff_cavs_decoder = {
1197 .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
1198 .type = AVMEDIA_TYPE_VIDEO,
1199 .id = AV_CODEC_ID_CAVS,
1200 .priv_data_size = sizeof(AVSContext),
1201 .init = ff_cavs_init,
1202 .close = ff_cavs_end,
1203 .decode = cavs_decode_frame,
1204 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1205 .flush = cavs_flush,