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 dequant_shift[64] = {
56 14, 14, 14, 14, 14, 14, 14, 14,
57 13, 13, 13, 13, 13, 13, 13, 13,
58 13, 12, 12, 12, 12, 12, 12, 12,
59 11, 11, 11, 11, 11, 11, 11, 11,
60 11, 10, 10, 10, 10, 10, 10, 10,
61 10, 9, 9, 9, 9, 9, 9, 9,
62 9, 8, 8, 8, 8, 8, 8, 8,
63 7, 7, 7, 7, 7, 7, 7, 7
66 static const uint16_t dequant_mul[64] = {
67 32768, 36061, 38968, 42495, 46341, 50535, 55437, 60424,
68 32932, 35734, 38968, 42495, 46177, 50535, 55109, 59933,
69 65535, 35734, 38968, 42577, 46341, 50617, 55027, 60097,
70 32809, 35734, 38968, 42454, 46382, 50576, 55109, 60056,
71 65535, 35734, 38968, 42495, 46320, 50515, 55109, 60076,
72 65535, 35744, 38968, 42495, 46341, 50535, 55099, 60087,
73 65535, 35734, 38973, 42500, 46341, 50535, 55109, 60097,
74 32771, 35734, 38965, 42497, 46341, 50535, 55109, 60099
79 static const struct dec_2dvlc intra_dec[7] = {
81 { //level / run / table_inc
82 { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
83 { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
84 { 1, 7, 1 }, { -1, 7, 1 }, { 1, 8, 1 }, { -1, 8, 1 }, { 1, 9, 1 }, { -1, 9, 1 },
85 { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 }, { 2, 1, 2 }, { -2, 1, 2 },
86 { 1, 12, 1 }, { -1, 12, 1 }, { 1, 13, 1 }, { -1, 13, 1 }, { 1, 14, 1 }, { -1, 14, 1 },
87 { 1, 15, 1 }, { -1, 15, 1 }, { 2, 2, 2 }, { -2, 2, 2 }, { 1, 16, 1 }, { -1, 16, 1 },
88 { 1, 17, 1 }, { -1, 17, 1 }, { 3, 1, 3 }, { -3, 1, 3 }, { 1, 18, 1 }, { -1, 18, 1 },
89 { 1, 19, 1 }, { -1, 19, 1 }, { 2, 3, 2 }, { -2, 3, 2 }, { 1, 20, 1 }, { -1, 20, 1 },
90 { 1, 21, 1 }, { -1, 21, 1 }, { 2, 4, 2 }, { -2, 4, 2 }, { 1, 22, 1 }, { -1, 22, 1 },
91 { 2, 5, 2 }, { -2, 5, 2 }, { 1, 23, 1 }, { -1, 23, 1 }, { EOB }
94 { 0, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1 },
101 { 1, 1, 0 }, { -1, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 1 }, { -2, 1, 1 },
102 { 1, 3, 0 }, { -1, 3, 0 }, { EOB }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 },
103 { -1, 5, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 3, 1, 2 }, { -3, 1, 2 }, { 2, 2, 1 },
104 { -2, 2, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 1, 9, 0 },
105 { -1, 9, 0 }, { 2, 3, 1 }, { -2, 3, 1 }, { 4, 1, 2 }, { -4, 1, 2 }, { 1, 10, 0 },
106 { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 2, 4, 1 }, { -2, 4, 1 }, { 3, 2, 2 },
107 { -3, 2, 2 }, { 1, 12, 0 }, { -1, 12, 0 }, { 2, 5, 1 }, { -2, 5, 1 }, { 5, 1, 3 },
108 { -5, 1, 3 }, { 1, 13, 0 }, { -1, 13, 0 }, { 2, 6, 1 }, { -2, 6, 1 }, { 1, 14, 0 },
109 { -1, 14, 0 }, { 2, 7, 1 }, { -2, 7, 1 }, { 2, 8, 1 }, { -2, 8, 1 }, { 3, 3, 2 },
110 { -3, 3, 2 }, { 6, 1, 3 }, { -6, 1, 3 }, { 1, 15, 0 }, { -1, 15, 0 }
113 { 0, 7, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
120 { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 },
121 { 3, 1, 1 }, { -3, 1, 1 }, { EOB }, { 1, 3, 0 }, { -1, 3, 0 }, { 2, 2, 0 },
122 { -2, 2, 0 }, { 4, 1, 1 }, { -4, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 2 },
123 { -5, 1, 2 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 3, 0 },
124 { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 2 }, { -6, 1, 2 }, { 2, 4, 0 },
125 { -2, 4, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 7, 1, 2 },
126 { -7, 1, 2 }, { 3, 3, 1 }, { -3, 3, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 8, 0 },
127 { -1, 8, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 8, 1, 3 }, { -8, 1, 3 }, { 1, 9, 0 },
128 { -1, 9, 0 }, { 5, 2, 2 }, { -5, 2, 2 }, { 3, 4, 1 }, { -3, 4, 1 }, { 2, 7, 0 },
129 { -2, 7, 0 }, { 9, 1, 3 }, { -9, 1, 3 }, { 1, 10, 0 }, { -1, 10, 0 }
132 { 0, 10, 6, 4, 4, 3, 3, 3, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
139 { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 }, { -3, 1, 0 },
140 { 1, 2, 0 }, { -1, 2, 0 }, { EOB }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 1 },
141 { -5, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 6, 1, 1 },
142 { -6, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 1, 4, 0 },
143 { -1, 4, 0 }, { 8, 1, 2 }, { -8, 1, 2 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 2, 0 },
144 { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 9, 1, 2 }, { -9, 1, 2 }, { 5, 2, 1 },
145 { -5, 2, 1 }, { 2, 4, 0 }, { -2, 4, 0 }, { 10, 1, 2 }, {-10, 1, 2 }, { 3, 3, 0 },
146 { -3, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 11, 1, 3 }, {-11, 1, 3 }, { 6, 2, 1 },
147 { -6, 2, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 2, 5, 0 }, { -2, 5, 0 }, { 3, 4, 0 },
148 { -3, 4, 0 }, { 12, 1, 3 }, {-12, 1, 3 }, { 4, 3, 0 }, { -4, 3, 0 }
151 { 0, 13, 7, 5, 4, 3, 2, 2, -1, -1, -1 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
158 { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 }, { -3, 1, 0 },
159 { EOB }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
160 { -6, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 1 },
161 { -8, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 9, 1, 1 }, { -9, 1, 1 }, { 10, 1, 1 },
162 {-10, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 11, 1, 2 },
163 {-11, 1, 2 }, { 4, 2, 0 }, { -4, 2, 0 }, { 12, 1, 2 }, {-12, 1, 2 }, { 13, 1, 2 },
164 {-13, 1, 2 }, { 5, 2, 0 }, { -5, 2, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 2, 3, 0 },
165 { -2, 3, 0 }, { 14, 1, 2 }, {-14, 1, 2 }, { 6, 2, 0 }, { -6, 2, 0 }, { 15, 1, 2 },
166 {-15, 1, 2 }, { 16, 1, 2 }, {-16, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 1, 5, 0 },
167 { -1, 5, 0 }, { 7, 2, 0 }, { -7, 2, 0 }, { 17, 1, 2 }, {-17, 1, 2 }
170 { 0,18, 8, 4, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
177 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
178 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
179 { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 9, 1, 0 },
180 { -9, 1, 0 }, { 10, 1, 0 }, {-10, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 11, 1, 1 },
181 {-11, 1, 1 }, { 12, 1, 1 }, {-12, 1, 1 }, { 13, 1, 1 }, {-13, 1, 1 }, { 2, 2, 0 },
182 { -2, 2, 0 }, { 14, 1, 1 }, {-14, 1, 1 }, { 15, 1, 1 }, {-15, 1, 1 }, { 3, 2, 0 },
183 { -3, 2, 0 }, { 16, 1, 1 }, {-16, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 17, 1, 1 },
184 {-17, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 18, 1, 1 }, {-18, 1, 1 }, { 5, 2, 0 },
185 { -5, 2, 0 }, { 19, 1, 1 }, {-19, 1, 1 }, { 20, 1, 1 }, {-20, 1, 1 }, { 6, 2, 0 },
186 { -6, 2, 0 }, { 21, 1, 1 }, {-21, 1, 1 }, { 2, 3, 0 }, { -2, 3, 0 }
189 { 0, 22, 7, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
196 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
197 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
198 { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 9, 1, 0 },
199 { -9, 1, 0 }, { 10, 1, 0 }, {-10, 1, 0 }, { 11, 1, 0 }, {-11, 1, 0 }, { 12, 1, 0 },
200 {-12, 1, 0 }, { 13, 1, 0 }, {-13, 1, 0 }, { 14, 1, 0 }, {-14, 1, 0 }, { 15, 1, 0 },
201 {-15, 1, 0 }, { 16, 1, 0 }, {-16, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 17, 1, 0 },
202 {-17, 1, 0 }, { 18, 1, 0 }, {-18, 1, 0 }, { 19, 1, 0 }, {-19, 1, 0 }, { 20, 1, 0 },
203 {-20, 1, 0 }, { 21, 1, 0 }, {-21, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 22, 1, 0 },
204 {-22, 1, 0 }, { 23, 1, 0 }, {-23, 1, 0 }, { 24, 1, 0 }, {-24, 1, 0 }, { 25, 1, 0 },
205 {-25, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 26, 1, 0 }, {-26, 1, 0 }
208 { 0, 27, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
215 static const struct dec_2dvlc inter_dec[7] = {
218 { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
219 { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
220 { 1, 7, 1 }, { -1, 7, 1 }, { 1, 8, 1 }, { -1, 8, 1 }, { 1, 9, 1 }, { -1, 9, 1 },
221 { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 }, { 1, 12, 1 }, { -1, 12, 1 },
222 { 1, 13, 1 }, { -1, 13, 1 }, { 2, 1, 2 }, { -2, 1, 2 }, { 1, 14, 1 }, { -1, 14, 1 },
223 { 1, 15, 1 }, { -1, 15, 1 }, { 1, 16, 1 }, { -1, 16, 1 }, { 1, 17, 1 }, { -1, 17, 1 },
224 { 1, 18, 1 }, { -1, 18, 1 }, { 1, 19, 1 }, { -1, 19, 1 }, { 3, 1, 3 }, { -3, 1, 3 },
225 { 1, 20, 1 }, { -1, 20, 1 }, { 1, 21, 1 }, { -1, 21, 1 }, { 2, 2, 2 }, { -2, 2, 2 },
226 { 1, 22, 1 }, { -1, 22, 1 }, { 1, 23, 1 }, { -1, 23, 1 }, { 1, 24, 1 }, { -1, 24, 1 },
227 { 1, 25, 1 }, { -1, 25, 1 }, { 1, 26, 1 }, { -1, 26, 1 }, { EOB }
230 { 0, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
237 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 1, 2, 0 }, { -1, 2, 0 }, { 1, 3, 0 },
238 { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 1, 6, 0 },
239 { -1, 6, 0 }, { 2, 1, 1 }, { -2, 1, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 1, 8, 0 },
240 { -1, 8, 0 }, { 1, 9, 0 }, { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 2, 2, 1 },
241 { -2, 2, 1 }, { 1, 11, 0 }, { -1, 11, 0 }, { 1, 12, 0 }, { -1, 12, 0 }, { 3, 1, 2 },
242 { -3, 1, 2 }, { 1, 13, 0 }, { -1, 13, 0 }, { 1, 14, 0 }, { -1, 14, 0 }, { 2, 3, 1 },
243 { -2, 3, 1 }, { 1, 15, 0 }, { -1, 15, 0 }, { 2, 4, 1 }, { -2, 4, 1 }, { 1, 16, 0 },
244 { -1, 16, 0 }, { 2, 5, 1 }, { -2, 5, 1 }, { 1, 17, 0 }, { -1, 17, 0 }, { 4, 1, 3 },
245 { -4, 1, 3 }, { 2, 6, 1 }, { -2, 6, 1 }, { 1, 18, 0 }, { -1, 18, 0 }, { 1, 19, 0 },
246 { -1, 19, 0 }, { 2, 7, 1 }, { -2, 7, 1 }, { 3, 2, 2 }, { -3, 2, 2 }
249 { 0, 5, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1 },
256 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 0 },
257 { -2, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 3, 1, 1 },
258 { -3, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 1, 6, 0 },
259 { -1, 6, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 1, 2 },
260 { -4, 1, 2 }, { 1, 8, 0 }, { -1, 8, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 4, 0 },
261 { -2, 4, 0 }, { 1, 9, 0 }, { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 5, 1, 2 },
262 { -5, 1, 2 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 2, 6, 0 },
263 { -2, 6, 0 }, { 1, 12, 0 }, { -1, 12, 0 }, { 3, 3, 1 }, { -3, 3, 1 }, { 6, 1, 2 },
264 { -6, 1, 2 }, { 4, 2, 2 }, { -4, 2, 2 }, { 1, 13, 0 }, { -1, 13, 0 }, { 2, 7, 0 },
265 { -2, 7, 0 }, { 3, 4, 1 }, { -3, 4, 1 }, { 1, 14, 0 }, { -1, 14, 0 }
268 { 0, 7, 5, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
275 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 },
276 { -1, 2, 0 }, { 3, 1, 0 }, { -3, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 2, 2, 0 },
277 { -2, 2, 0 }, { 4, 1, 1 }, { -4, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 1 },
278 { -5, 1, 1 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 2, 3, 0 },
279 { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 1 }, { -6, 1, 1 }, { 2, 4, 0 },
280 { -2, 4, 0 }, { 1, 7, 0 }, { -1, 7, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 7, 1, 2 },
281 { -7, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 2, 5, 0 },
282 { -2, 5, 0 }, { 8, 1, 2 }, { -8, 1, 2 }, { 1, 9, 0 }, { -1, 9, 0 }, { 3, 4, 0 },
283 { -3, 4, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 5, 2, 1 }, { -5, 2, 1 }, { 1, 10, 0 },
284 { -1, 10, 0 }, { 9, 1, 2 }, { -9, 1, 2 }, { 4, 3, 1 }, { -4, 3, 1 }
287 { 0,10, 6, 5, 4, 3, 3, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
294 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
295 { -3, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 },
296 { -5, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 6, 1, 0 },
297 { -6, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 1, 4, 0 },
298 { -1, 4, 0 }, { 8, 1, 1 }, { -8, 1, 1 }, { 2, 3, 0 }, { -2, 3, 0 }, { 4, 2, 0 },
299 { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 9, 1, 1 }, { -9, 1, 1 }, { 5, 2, 0 },
300 { -5, 2, 0 }, { 2, 4, 0 }, { -2, 4, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 10, 1, 2 },
301 {-10, 1, 2 }, { 3, 3, 0 }, { -3, 3, 0 }, { 11, 1, 2 }, {-11, 1, 2 }, { 1, 7, 0 },
302 { -1, 7, 0 }, { 6, 2, 0 }, { -6, 2, 0 }, { 3, 4, 0 }, { -3, 4, 0 }, { 2, 5, 0 },
303 { -2, 5, 0 }, { 12, 1, 2 }, {-12, 1, 2 }, { 4, 3, 0 }, { -4, 3, 0 }
306 { 0, 13, 7, 5, 4, 3, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
313 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
314 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 1, 2, 0 },
315 { -1, 2, 0 }, { 6, 1, 0 }, { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 },
316 { -8, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 1, 3, 0 },
317 { -1, 3, 0 }, { 10, 1, 1 }, { -10, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 11, 1, 1 },
318 { -11, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 12, 1, 1 }, { -12, 1, 1 }, { 1, 4, 0 },
319 { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 13, 1, 1 }, { -13, 1, 1 }, { 5, 2, 0 },
320 { -5, 2, 0 }, { 14, 1, 1 }, { -14, 1, 1 }, { 6, 2, 0 }, { -6, 2, 0 }, { 1, 5, 0 },
321 { -1, 5, 0 }, { 15, 1, 1 }, { -15, 1, 1 }, { 3, 3, 0 }, { -3, 3, 0 }, { 16, 1, 1 },
322 { -16, 1, 1 }, { 2, 4, 0 }, { -2, 4, 0 }, { 7, 2, 0 }, { -7, 2, 0 }
325 { 0, 17, 8, 4, 3, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
332 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
333 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
334 { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 8, 1, 0 },
335 { -8, 1, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 10, 1, 0 }, { -10, 1, 0 }, { 11, 1, 0 },
336 { -11, 1, 0 }, { 12, 1, 0 }, { -12, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 13, 1, 0 },
337 { -13, 1, 0 }, { 1, 3, 0 }, { -1, 3, 0 }, { 14, 1, 0 }, { -14, 1, 0 }, { 15, 1, 0 },
338 { -15, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 16, 1, 0 }, { -16, 1, 0 }, { 17, 1, 0 },
339 { -17, 1, 0 }, { 18, 1, 0 }, { -18, 1, 0 }, { 4, 2, 0 }, { -4, 2, 0 }, { 19, 1, 0 },
340 { -19, 1, 0 }, { 20, 1, 0 }, { -20, 1, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 1, 4, 0 },
341 { -1, 4, 0 }, { 5, 2, 0 }, { -5, 2, 0 }, { 21, 1, 0 }, { -21, 1, 0 }
344 { 0, 22, 6, 3, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
351 static const struct dec_2dvlc chroma_dec[5] = {
354 { 1, 1, 1 }, { -1, 1, 1 }, { 1, 2, 1 }, { -1, 2, 1 }, { 1, 3, 1 }, { -1, 3, 1 },
355 { 1, 4, 1 }, { -1, 4, 1 }, { 1, 5, 1 }, { -1, 5, 1 }, { 1, 6, 1 }, { -1, 6, 1 },
356 { 1, 7, 1 }, { -1, 7, 1 }, { 2, 1, 2 }, { -2, 1, 2 }, { 1, 8, 1 }, { -1, 8, 1 },
357 { 1, 9, 1 }, { -1, 9, 1 }, { 1, 10, 1 }, { -1, 10, 1 }, { 1, 11, 1 }, { -1, 11, 1 },
358 { 1, 12, 1 }, { -1, 12, 1 }, { 1, 13, 1 }, { -1, 13, 1 }, { 1, 14, 1 }, { -1, 14, 1 },
359 { 1, 15, 1 }, { -1, 15, 1 }, { 3, 1, 3 }, { -3, 1, 3 }, { 1, 16, 1 }, { -1, 16, 1 },
360 { 1, 17, 1 }, { -1, 17, 1 }, { 1, 18, 1 }, { -1, 18, 1 }, { 1, 19, 1 }, { -1, 19, 1 },
361 { 1, 20, 1 }, { -1, 20, 1 }, { 1, 21, 1 }, { -1, 21, 1 }, { 1, 22, 1 }, { -1, 22, 1 },
362 { 2, 2, 2 }, { -2, 2, 2 }, { 1, 23, 1 }, { -1, 23, 1 }, { 1, 24, 1 }, { -1, 24, 1 },
363 { 1, 25, 1 }, { -1, 25, 1 }, { 4, 1, 3 }, { -4, 1, 3 }, { EOB }
366 { 0, 5, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
373 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 2, 1, 1 },
374 { -2, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 1, 5, 0 },
375 { -1, 5, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 3, 1, 2 }, { -3, 1, 2 }, { 1, 7, 0 },
376 { -1, 7, 0 }, { 1, 8, 0 }, { -1, 8, 0 }, { 2, 2, 1 }, { -2, 2, 1 }, { 1, 9, 0 },
377 { -1, 9, 0 }, { 1, 10, 0 }, { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 4, 1, 2 },
378 { -4, 1, 2 }, { 1, 12, 0 }, { -1, 12, 0 }, { 1, 13, 0 }, { -1, 13, 0 }, { 1, 14, 0 },
379 { -1, 14, 0 }, { 2, 3, 1 }, { -2, 3, 1 }, { 1, 15, 0 }, { -1, 15, 0 }, { 2, 4, 1 },
380 { -2, 4, 1 }, { 5, 1, 3 }, { -5, 1, 3 }, { 3, 2, 2 }, { -3, 2, 2 }, { 1, 16, 0 },
381 { -1, 16, 0 }, { 1, 17, 0 }, { -1, 17, 0 }, { 1, 18, 0 }, { -1, 18, 0 }, { 2, 5, 1 },
382 { -2, 5, 1 }, { 1, 19, 0 }, { -1, 19, 0 }, { 1, 20, 0 }, { -1, 20, 0 }
385 { 0, 6, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1 },
392 { 1, 1, 0 }, { -1, 1, 0 }, { EOB }, { 2, 1, 0 }, { -2, 1, 0 }, { 1, 2, 0 },
393 { -1, 2, 0 }, { 3, 1, 1 }, { -3, 1, 1 }, { 1, 3, 0 }, { -1, 3, 0 }, { 4, 1, 1 },
394 { -4, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 1, 4, 0 }, { -1, 4, 0 }, { 5, 1, 2 },
395 { -5, 1, 2 }, { 1, 5, 0 }, { -1, 5, 0 }, { 3, 2, 1 }, { -3, 2, 1 }, { 2, 3, 0 },
396 { -2, 3, 0 }, { 1, 6, 0 }, { -1, 6, 0 }, { 6, 1, 2 }, { -6, 1, 2 }, { 1, 7, 0 },
397 { -1, 7, 0 }, { 2, 4, 0 }, { -2, 4, 0 }, { 7, 1, 2 }, { -7, 1, 2 }, { 1, 8, 0 },
398 { -1, 8, 0 }, { 4, 2, 1 }, { -4, 2, 1 }, { 1, 9, 0 }, { -1, 9, 0 }, { 3, 3, 1 },
399 { -3, 3, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 2, 6, 0 }, { -2, 6, 0 }, { 8, 1, 2 },
400 { -8, 1, 2 }, { 1, 10, 0 }, { -1, 10, 0 }, { 1, 11, 0 }, { -1, 11, 0 }, { 9, 1, 2 },
401 { -9, 1, 2 }, { 5, 2, 2 }, { -5, 2, 2 }, { 3, 4, 1 }, { -3, 4, 1 },
404 { 0,10, 6, 4, 4, 3, 3, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
411 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
412 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 1, 2, 0 }, { -1, 2, 0 }, { 5, 1, 1 },
413 { -5, 1, 1 }, { 2, 2, 0 }, { -2, 2, 0 }, { 6, 1, 1 }, { -6, 1, 1 }, { 1, 3, 0 },
414 { -1, 3, 0 }, { 7, 1, 1 }, { -7, 1, 1 }, { 3, 2, 0 }, { -3, 2, 0 }, { 8, 1, 1 },
415 { -8, 1, 1 }, { 1, 4, 0 }, { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 9, 1, 1 },
416 { -9, 1, 1 }, { 4, 2, 0 }, { -4, 2, 0 }, { 1, 5, 0 }, { -1, 5, 0 }, { 10, 1, 1 },
417 {-10, 1, 1 }, { 3, 3, 0 }, { -3, 3, 0 }, { 5, 2, 1 }, { -5, 2, 1 }, { 2, 4, 0 },
418 { -2, 4, 0 }, { 11, 1, 1 }, {-11, 1, 1 }, { 1, 6, 0 }, { -1, 6, 0 }, { 12, 1, 1 },
419 {-12, 1, 1 }, { 1, 7, 0 }, { -1, 7, 0 }, { 6, 2, 1 }, { -6, 2, 1 }, { 13, 1, 1 },
420 {-13, 1, 1 }, { 2, 5, 0 }, { -2, 5, 0 }, { 1, 8, 0 }, { -1, 8, 0 },
423 { 0, 14, 7, 4, 3, 3, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
430 { EOB }, { 1, 1, 0 }, { -1, 1, 0 }, { 2, 1, 0 }, { -2, 1, 0 }, { 3, 1, 0 },
431 { -3, 1, 0 }, { 4, 1, 0 }, { -4, 1, 0 }, { 5, 1, 0 }, { -5, 1, 0 }, { 6, 1, 0 },
432 { -6, 1, 0 }, { 7, 1, 0 }, { -7, 1, 0 }, { 8, 1, 0 }, { -8, 1, 0 }, { 1, 2, 0 },
433 { -1, 2, 0 }, { 9, 1, 0 }, { -9, 1, 0 }, { 10, 1, 0 }, { -10, 1, 0 }, { 11, 1, 0 },
434 { -11, 1, 0 }, { 2, 2, 0 }, { -2, 2, 0 }, { 12, 1, 0 }, { -12, 1, 0 }, { 13, 1, 0 },
435 { -13, 1, 0 }, { 3, 2, 0 }, { -3, 2, 0 }, { 14, 1, 0 }, { -14, 1, 0 }, { 1, 3, 0 },
436 { -1, 3, 0 }, { 15, 1, 0 }, { -15, 1, 0 }, { 4, 2, 0 }, { -4, 2, 0 }, { 16, 1, 0 },
437 { -16, 1, 0 }, { 17, 1, 0 }, { -17, 1, 0 }, { 5, 2, 0 }, { -5, 2, 0 }, { 1, 4, 0 },
438 { -1, 4, 0 }, { 2, 3, 0 }, { -2, 3, 0 }, { 18, 1, 0 }, { -18, 1, 0 }, { 6, 2, 0 },
439 { -6, 2, 0 }, { 19, 1, 0 }, { -19, 1, 0 }, { 1, 5, 0 }, { -1, 5, 0 },
442 { 0, 20, 7, 3, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
451 /*****************************************************************************
453 * motion vector prediction
455 ****************************************************************************/
457 static inline void store_mvs(AVSContext *h)
459 h->col_mv[h->mbidx * 4 + 0] = h->mv[MV_FWD_X0];
460 h->col_mv[h->mbidx * 4 + 1] = h->mv[MV_FWD_X1];
461 h->col_mv[h->mbidx * 4 + 2] = h->mv[MV_FWD_X2];
462 h->col_mv[h->mbidx * 4 + 3] = h->mv[MV_FWD_X3];
465 static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
468 cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
469 int den = h->direct_den[col_mv->ref];
470 int m = FF_SIGNBIT(col_mv->x);
472 pmv_fw->dist = h->dist[1];
473 pmv_bw->dist = h->dist[0];
476 /* scale the co-located motion vector according to its temporal span */
477 pmv_fw->x = (((den + (den * col_mv->x * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
478 pmv_bw->x = m - (((den + (den * col_mv->x * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
479 m = FF_SIGNBIT(col_mv->y);
480 pmv_fw->y = (((den + (den * col_mv->y * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
481 pmv_bw->y = m - (((den + (den * col_mv->y * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
484 static inline void mv_pred_sym(AVSContext *h, cavs_vector *src,
485 enum cavs_block size)
487 cavs_vector *dst = src + MV_BWD_OFFS;
489 /* backward mv is the scaled and negated forward mv */
490 dst->x = -((src->x * h->sym_factor + 256) >> 9);
491 dst->y = -((src->y * h->sym_factor + 256) >> 9);
493 dst->dist = h->dist[0];
497 /*****************************************************************************
499 * residual data decoding
501 ****************************************************************************/
503 /** kth-order exponential golomb code */
504 static inline int get_ue_code(GetBitContext *gb, int order)
506 unsigned ret = get_ue_golomb(gb);
507 if (ret >= ((1U<<31)>>order)) {
508 av_log(NULL, AV_LOG_ERROR, "get_ue_code: value too larger\n");
509 return AVERROR_INVALIDDATA;
512 return (ret<<order) + get_bits(gb, order);
517 static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf,
518 int16_t *dst, int mul, int shift, int coeff_num)
520 int round = 1 << (shift - 1);
522 const uint8_t *scantab = h->scantable.permutated;
524 /* inverse scan and dequantization */
525 while (--coeff_num >= 0) {
526 pos += run_buf[coeff_num];
528 av_log(h->avctx, AV_LOG_ERROR,
529 "position out of block bounds at pic %d MB(%d,%d)\n",
530 h->cur.poc, h->mbx, h->mby);
531 return AVERROR_INVALIDDATA;
533 dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
539 * decode coefficients from one 8x8 block, dequantize, inverse transform
540 * and add them to sample block
541 * @param r pointer to 2D VLC table
542 * @param esc_golomb_order escape codes are k-golomb with this order k
543 * @param qp quantizer
544 * @param dst location of sample block
545 * @param stride line stride in frame buffer
547 static int decode_residual_block(AVSContext *h, GetBitContext *gb,
548 const struct dec_2dvlc *r, int esc_golomb_order,
549 int qp, uint8_t *dst, int stride)
551 int i, esc_code, level, mask, ret;
552 unsigned int level_code, run;
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(gb, r->golomb_order);
559 if (level_code >= ESCAPE_CODE) {
560 run = ((level_code - ESCAPE_CODE) >> 1) + 1;
562 av_log(h->avctx, AV_LOG_ERROR, "run %d is too large\n", run);
563 return AVERROR_INVALIDDATA;
565 esc_code = get_ue_code(gb, esc_golomb_order);
566 level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
567 while (level > r->inc_limit)
569 mask = -(level_code & 1);
570 level = (level ^ mask) - mask;
572 level = r->rltab[level_code][0];
573 if (!level) //end of block signal
575 run = r->rltab[level_code][1];
576 r += r->rltab[level_code][2];
578 level_buf[i] = level;
581 if ((ret = dequant(h, level_buf, run_buf, block, dequant_mul[qp],
582 dequant_shift[qp], i)) < 0)
584 h->cdsp.cavs_idct8_add(dst, block, stride);
585 h->bdsp.clear_block(block);
590 static inline void decode_residual_chroma(AVSContext *h)
592 if (h->cbp & (1 << 4))
593 decode_residual_block(h, &h->gb, chroma_dec, 0,
594 ff_cavs_chroma_qp[h->qp], h->cu, h->c_stride);
595 if (h->cbp & (1 << 5))
596 decode_residual_block(h, &h->gb, chroma_dec, 0,
597 ff_cavs_chroma_qp[h->qp], h->cv, h->c_stride);
600 static inline int decode_residual_inter(AVSContext *h)
604 /* get coded block pattern */
605 int cbp = get_ue_golomb(&h->gb);
607 av_log(h->avctx, AV_LOG_ERROR, "illegal inter cbp %d\n", cbp);
608 return AVERROR_INVALIDDATA;
610 h->cbp = cbp_tab[cbp][1];
613 if (h->cbp && !h->qp_fixed)
614 h->qp = (h->qp + get_se_golomb(&h->gb)) & 63;
615 for (block = 0; block < 4; block++)
616 if (h->cbp & (1 << block))
617 decode_residual_block(h, &h->gb, inter_dec, 0, h->qp,
618 h->cy + h->luma_scan[block], h->l_stride);
619 decode_residual_chroma(h);
624 /*****************************************************************************
628 ****************************************************************************/
630 static inline void set_mv_intra(AVSContext *h)
632 h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
633 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
634 h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
635 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
636 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B)
637 h->col_type_base[h->mbidx] = I_8X8;
640 static int decode_mb_i(AVSContext *h, int cbp_code)
642 GetBitContext *gb = &h->gb;
643 unsigned pred_mode_uv;
646 uint8_t *left = NULL;
651 /* get intra prediction modes from stream */
652 for (block = 0; block < 4; block++) {
653 int nA, nB, predpred;
654 int pos = scan3x3[block];
656 nA = h->pred_mode_Y[pos - 1];
657 nB = h->pred_mode_Y[pos - 3];
658 predpred = FFMIN(nA, nB);
659 if (predpred == NOT_AVAIL) // if either is not available
660 predpred = INTRA_L_LP;
661 if (!get_bits1(gb)) {
662 int rem_mode = get_bits(gb, 2);
663 predpred = rem_mode + (rem_mode >= predpred);
665 h->pred_mode_Y[pos] = predpred;
667 pred_mode_uv = get_ue_golomb(gb);
668 if (pred_mode_uv > 6) {
669 av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
670 return AVERROR_INVALIDDATA;
672 ff_cavs_modify_mb_i(h, &pred_mode_uv);
674 /* get coded block pattern */
675 if (h->cur.f->pict_type == AV_PICTURE_TYPE_I)
676 cbp_code = get_ue_golomb(gb);
677 if (cbp_code > 63U) {
678 av_log(h->avctx, AV_LOG_ERROR, "illegal intra cbp\n");
679 return AVERROR_INVALIDDATA;
681 h->cbp = cbp_tab[cbp_code][0];
682 if (h->cbp && !h->qp_fixed)
683 h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
685 /* luma intra prediction interleaved with residual decode/transform/add */
686 for (block = 0; block < 4; block++) {
687 d = h->cy + h->luma_scan[block];
688 ff_cavs_load_intra_pred_luma(h, top, &left, block);
689 h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
690 (d, top, left, h->l_stride);
691 if (h->cbp & (1<<block))
692 decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
695 /* chroma intra prediction */
696 ff_cavs_load_intra_pred_chroma(h);
697 h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx * 10],
698 h->left_border_u, h->c_stride);
699 h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx * 10],
700 h->left_border_v, h->c_stride);
702 decode_residual_chroma(h);
703 ff_cavs_filter(h, I_8X8);
708 static inline void set_intra_mode_default(AVSContext *h)
710 if (h->stream_revision > 0) {
711 h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
712 h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = NOT_AVAIL;
714 h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
715 h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = INTRA_L_LP;
719 static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
721 GetBitContext *gb = &h->gb;
727 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
730 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
731 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, ref[0]);
734 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
735 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
736 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
737 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
740 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
741 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
742 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
743 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
746 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
747 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
748 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
749 ref[3] = h->ref_flag ? 0 : get_bits1(gb);
750 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
751 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
752 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
753 ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
755 ff_cavs_inter(h, mb_type);
756 set_intra_mode_default(h);
758 if (mb_type != P_SKIP)
759 decode_residual_inter(h);
760 ff_cavs_filter(h, mb_type);
761 h->col_type_base[h->mbidx] = mb_type;
764 static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
767 enum cavs_sub_mb sub_type[4];
773 h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
774 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
775 h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
776 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
780 if (!h->col_type_base[h->mbidx]) {
781 /* intra MB at co-location, do in-plane prediction */
782 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
783 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
785 /* direct prediction from co-located P MB, block-wise */
786 for (block = 0; block < 4; block++)
787 mv_pred_direct(h, &h->mv[mv_scan[block]],
788 &h->col_mv[h->mbidx * 4 + block]);
791 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
794 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
795 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
798 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
801 #define TMP_UNUSED_INX 7
803 for (block = 0; block < 4; block++)
804 sub_type[block] = get_bits(&h->gb, 2);
805 for (block = 0; block < 4; block++) {
806 switch (sub_type[block]) {
808 if (!h->col_type_base[h->mbidx]) {
809 /* intra MB at co-location, do in-plane prediction */
811 // if col-MB is a Intra MB, current Block size is 16x16.
812 // AVS standard section 9.9.1
814 h->mv[TMP_UNUSED_INX ] = h->mv[MV_FWD_X0 ];
815 h->mv[TMP_UNUSED_INX + MV_BWD_OFFS] = h->mv[MV_FWD_X0 + MV_BWD_OFFS];
817 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2,
818 MV_PRED_BSKIP, BLK_8X8, 1);
819 ff_cavs_mv(h, MV_FWD_X0+MV_BWD_OFFS,
820 MV_FWD_C2+MV_BWD_OFFS,
821 MV_PRED_BSKIP, BLK_8X8, 0);
823 flags = mv_scan[block];
824 h->mv[flags ] = h->mv[MV_FWD_X0 ];
825 h->mv[flags + MV_BWD_OFFS] = h->mv[MV_FWD_X0 + MV_BWD_OFFS];
826 h->mv[MV_FWD_X0 ] = h->mv[TMP_UNUSED_INX ];
827 h->mv[MV_FWD_X0 + MV_BWD_OFFS] = h->mv[TMP_UNUSED_INX + MV_BWD_OFFS];
831 h->mv[mv_scan[block] ] = h->mv[flags ];
832 h->mv[mv_scan[block] + MV_BWD_OFFS] = h->mv[flags + MV_BWD_OFFS];
835 mv_pred_direct(h, &h->mv[mv_scan[block]],
836 &h->col_mv[h->mbidx * 4 + block]);
839 ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
840 MV_PRED_MEDIAN, BLK_8X8, 1);
843 ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
844 MV_PRED_MEDIAN, BLK_8X8, 1);
845 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
849 #undef TMP_UNUSED_INX
850 for (block = 0; block < 4; block++) {
851 if (sub_type[block] == B_SUB_BWD)
852 ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
853 mv_scan[block] + MV_BWD_OFFS - 3,
854 MV_PRED_MEDIAN, BLK_8X8, 0);
858 if (mb_type <= B_SYM_16X16) {
859 av_log(h->avctx, AV_LOG_ERROR, "Invalid mb_type %d in B frame\n", mb_type);
860 return AVERROR_INVALIDDATA;
862 av_assert2(mb_type < B_8X8);
863 flags = ff_cavs_partition_flags[mb_type];
864 if (mb_type & 1) { /* 16x8 macroblock types */
866 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
868 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
870 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
872 mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
874 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
876 ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
877 } else { /* 8x16 macroblock types */
879 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
881 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
883 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 1);
885 mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
887 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
889 ff_cavs_mv(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 0);
892 ff_cavs_inter(h, mb_type);
893 set_intra_mode_default(h);
894 if (mb_type != B_SKIP)
895 decode_residual_inter(h);
896 ff_cavs_filter(h, mb_type);
901 /*****************************************************************************
905 ****************************************************************************/
907 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb)
910 av_log(h->avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
912 if (h->stc >= h->mb_height) {
913 av_log(h->avctx, AV_LOG_ERROR, "stc 0x%02x is too large\n", h->stc);
914 return AVERROR_INVALIDDATA;
918 h->mbidx = h->mby * h->mb_width;
920 /* mark top macroblocks as unavailable */
921 h->flags &= ~(B_AVAIL | C_AVAIL);
922 if (!h->pic_qp_fixed) {
923 h->qp_fixed = get_bits1(gb);
924 h->qp = get_bits(gb, 6);
926 /* inter frame or second slice can have weighting params */
927 if ((h->cur.f->pict_type != AV_PICTURE_TYPE_I) ||
928 (!h->pic_structure && h->mby >= h->mb_width / 2))
929 if (get_bits1(gb)) { //slice_weighting_flag
930 av_log(h->avctx, AV_LOG_ERROR,
931 "weighted prediction not yet supported\n");
936 static inline int check_for_slice(AVSContext *h)
938 GetBitContext *gb = &h->gb;
943 align = (-get_bits_count(gb)) & 7;
944 /* check for stuffing byte */
945 if (!align && (show_bits(gb, 8) == 0x80))
947 if ((show_bits_long(gb, 24 + align) & 0xFFFFFF) == 0x000001) {
948 skip_bits_long(gb, 24 + align);
949 h->stc = get_bits(gb, 8);
950 if (h->stc >= h->mb_height)
952 decode_slice_header(h, gb);
958 /*****************************************************************************
962 ****************************************************************************/
964 static int decode_pic(AVSContext *h)
968 enum cavs_mb mb_type;
971 av_log(h->avctx, AV_LOG_ERROR, "No sequence header decoded yet\n");
972 return AVERROR_INVALIDDATA;
975 av_frame_unref(h->cur.f);
977 skip_bits(&h->gb, 16);//bbv_dwlay
978 if (h->stc == PIC_PB_START_CODE) {
979 h->cur.f->pict_type = get_bits(&h->gb, 2) + AV_PICTURE_TYPE_I;
980 if (h->cur.f->pict_type > AV_PICTURE_TYPE_B) {
981 av_log(h->avctx, AV_LOG_ERROR, "illegal picture type\n");
982 return AVERROR_INVALIDDATA;
984 /* make sure we have the reference frames we need */
985 if (!h->DPB[0].f->data[0] ||
986 (!h->DPB[1].f->data[0] && h->cur.f->pict_type == AV_PICTURE_TYPE_B))
987 return AVERROR_INVALIDDATA;
989 h->cur.f->pict_type = AV_PICTURE_TYPE_I;
990 if (get_bits1(&h->gb))
991 skip_bits(&h->gb, 24);//time_code
992 /* old sample clips were all progressive and no low_delay,
993 bump stream revision if detected otherwise */
994 if (h->low_delay || !(show_bits(&h->gb, 9) & 1))
995 h->stream_revision = 1;
996 /* similarly test top_field_first and repeat_first_field */
997 else if (show_bits(&h->gb, 11) & 3)
998 h->stream_revision = 1;
999 if (h->stream_revision > 0)
1000 skip_bits(&h->gb, 1); //marker_bit
1003 ret = ff_get_buffer(h->avctx, h->cur.f, h->cur.f->pict_type == AV_PICTURE_TYPE_B ?
1004 0 : AV_GET_BUFFER_FLAG_REF);
1008 if (!h->edge_emu_buffer) {
1009 int alloc_size = FFALIGN(FFABS(h->cur.f->linesize[0]) + 32, 32);
1010 h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 24);
1011 if (!h->edge_emu_buffer)
1012 return AVERROR(ENOMEM);
1015 if ((ret = ff_cavs_init_pic(h)) < 0)
1017 h->cur.poc = get_bits(&h->gb, 8) * 2;
1019 /* get temporal distances and MV scaling factors */
1020 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1021 h->dist[0] = (h->cur.poc - h->DPB[0].poc) & 511;
1023 h->dist[0] = (h->DPB[0].poc - h->cur.poc) & 511;
1025 h->dist[1] = (h->cur.poc - h->DPB[1].poc) & 511;
1026 h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
1027 h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
1028 if (h->cur.f->pict_type == AV_PICTURE_TYPE_B) {
1029 h->sym_factor = h->dist[0] * h->scale_den[1];
1031 h->direct_den[0] = h->dist[0] ? 16384 / h->dist[0] : 0;
1032 h->direct_den[1] = h->dist[1] ? 16384 / h->dist[1] : 0;
1036 get_ue_golomb(&h->gb); //bbv_check_times
1037 h->progressive = get_bits1(&h->gb);
1038 h->pic_structure = 1;
1039 if (!h->progressive)
1040 h->pic_structure = get_bits1(&h->gb);
1041 if (!h->pic_structure && h->stc == PIC_PB_START_CODE)
1042 skip_bits1(&h->gb); //advanced_pred_mode_disable
1043 skip_bits1(&h->gb); //top_field_first
1044 skip_bits1(&h->gb); //repeat_first_field
1046 h->qp_fixed = get_bits1(&h->gb);
1047 h->qp = get_bits(&h->gb, 6);
1048 if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1049 if (!h->progressive && !h->pic_structure)
1050 skip_bits1(&h->gb);//what is this?
1051 skip_bits(&h->gb, 4); //reserved bits
1053 if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
1054 h->ref_flag = get_bits1(&h->gb);
1055 skip_bits(&h->gb, 4); //reserved bits
1056 h->skip_mode_flag = get_bits1(&h->gb);
1058 h->loop_filter_disable = get_bits1(&h->gb);
1059 if (!h->loop_filter_disable && get_bits1(&h->gb)) {
1060 h->alpha_offset = get_se_golomb(&h->gb);
1061 h->beta_offset = get_se_golomb(&h->gb);
1063 h->alpha_offset = h->beta_offset = 0;
1065 if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1069 } while (ff_cavs_next_mb(h));
1070 } else if (h->cur.f->pict_type == AV_PICTURE_TYPE_P) {
1072 if (check_for_slice(h))
1074 if (h->skip_mode_flag && (skip_count < 0))
1075 skip_count = get_ue_golomb(&h->gb);
1076 if (h->skip_mode_flag && skip_count--) {
1077 decode_mb_p(h, P_SKIP);
1079 mb_type = get_ue_golomb(&h->gb) + P_SKIP + h->skip_mode_flag;
1080 if (mb_type > P_8X8)
1081 decode_mb_i(h, mb_type - P_8X8 - 1);
1083 decode_mb_p(h, mb_type);
1085 } while (ff_cavs_next_mb(h));
1086 } else { /* AV_PICTURE_TYPE_B */
1088 if (check_for_slice(h))
1090 if (h->skip_mode_flag && (skip_count < 0))
1091 skip_count = get_ue_golomb(&h->gb);
1092 if (h->skip_mode_flag && skip_count--) {
1093 decode_mb_b(h, B_SKIP);
1095 mb_type = get_ue_golomb(&h->gb) + B_SKIP + h->skip_mode_flag;
1096 if (mb_type > B_8X8)
1097 decode_mb_i(h, mb_type - B_8X8 - 1);
1099 decode_mb_b(h, mb_type);
1101 } while (ff_cavs_next_mb(h));
1103 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1104 av_frame_unref(h->DPB[1].f);
1105 FFSWAP(AVSFrame, h->cur, h->DPB[1]);
1106 FFSWAP(AVSFrame, h->DPB[0], h->DPB[1]);
1111 /*****************************************************************************
1113 * headers and interface
1115 ****************************************************************************/
1117 static int decode_seq_header(AVSContext *h)
1119 int frame_rate_code;
1122 h->profile = get_bits(&h->gb, 8);
1123 h->level = get_bits(&h->gb, 8);
1124 skip_bits1(&h->gb); //progressive sequence
1126 width = get_bits(&h->gb, 14);
1127 height = get_bits(&h->gb, 14);
1128 if ((h->width || h->height) && (h->width != width || h->height != height)) {
1129 avpriv_report_missing_feature(h->avctx,
1130 "Width/height changing in CAVS");
1131 return AVERROR_PATCHWELCOME;
1133 if (width <= 0 || height <= 0) {
1134 av_log(h->avctx, AV_LOG_ERROR, "Dimensions invalid\n");
1135 return AVERROR_INVALIDDATA;
1140 skip_bits(&h->gb, 2); //chroma format
1141 skip_bits(&h->gb, 3); //sample_precision
1142 h->aspect_ratio = get_bits(&h->gb, 4);
1143 frame_rate_code = get_bits(&h->gb, 4);
1144 skip_bits(&h->gb, 18); //bit_rate_lower
1145 skip_bits1(&h->gb); //marker_bit
1146 skip_bits(&h->gb, 12); //bit_rate_upper
1147 h->low_delay = get_bits1(&h->gb);
1148 h->mb_width = (h->width + 15) >> 4;
1149 h->mb_height = (h->height + 15) >> 4;
1150 h->avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];
1151 h->avctx->width = h->width;
1152 h->avctx->height = h->height;
1154 return ff_cavs_init_top_lines(h);
1158 static void cavs_flush(AVCodecContext * avctx)
1160 AVSContext *h = avctx->priv_data;
1161 h->got_keyframe = 0;
1164 static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1167 AVSContext *h = avctx->priv_data;
1168 const uint8_t *buf = avpkt->data;
1169 int buf_size = avpkt->size;
1171 int input_size, ret;
1172 const uint8_t *buf_end;
1173 const uint8_t *buf_ptr;
1175 if (buf_size == 0) {
1176 if (!h->low_delay && h->DPB[0].f->data[0]) {
1178 av_frame_move_ref(data, h->DPB[0].f);
1186 buf_end = buf + buf_size;
1188 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
1189 if ((stc & 0xFFFFFE00) || buf_ptr == buf_end) {
1191 av_log(h->avctx, AV_LOG_WARNING, "no frame decoded\n");
1192 return FFMAX(0, buf_ptr - buf);
1194 input_size = (buf_end - buf_ptr) * 8;
1196 case CAVS_START_CODE:
1197 init_get_bits(&h->gb, buf_ptr, input_size);
1198 decode_seq_header(h);
1200 case PIC_I_START_CODE:
1201 if (!h->got_keyframe) {
1202 av_frame_unref(h->DPB[0].f);
1203 av_frame_unref(h->DPB[1].f);
1204 h->got_keyframe = 1;
1206 case PIC_PB_START_CODE:
1208 if (!h->got_keyframe)
1210 init_get_bits(&h->gb, buf_ptr, input_size);
1215 if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1216 if (h->DPB[!h->low_delay].f->data[0]) {
1217 if ((ret = av_frame_ref(data, h->DPB[!h->low_delay].f)) < 0)
1223 av_frame_move_ref(data, h->cur.f);
1226 case EXT_START_CODE:
1227 //mpeg_decode_extension(avctx, buf_ptr, input_size);
1229 case USER_START_CODE:
1230 //mpeg_decode_user_data(avctx, buf_ptr, input_size);
1233 if (stc <= SLICE_MAX_START_CODE) {
1234 init_get_bits(&h->gb, buf_ptr, input_size);
1235 decode_slice_header(h, &h->gb);
1242 AVCodec ff_cavs_decoder = {
1244 .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
1245 .type = AVMEDIA_TYPE_VIDEO,
1246 .id = AV_CODEC_ID_CAVS,
1247 .priv_data_size = sizeof(AVSContext),
1248 .init = ff_cavs_init,
1249 .close = ff_cavs_end,
1250 .decode = cavs_decode_frame,
1251 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1252 .flush = cavs_flush,