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"
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,
100 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,
119 -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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, 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,
157 -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,
176 -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,
195 -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,
214 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
221 static const struct dec_2dvlc inter_dec[7] = {
224 { 1, 1, 1},{ -1, 1, 1},{ 1, 2, 1},{ -1, 2, 1},{ 1, 3, 1},{ -1, 3, 1},
225 { 1, 4, 1},{ -1, 4, 1},{ 1, 5, 1},{ -1, 5, 1},{ 1, 6, 1},{ -1, 6, 1},
226 { 1, 7, 1},{ -1, 7, 1},{ 1, 8, 1},{ -1, 8, 1},{ 1, 9, 1},{ -1, 9, 1},
227 { 1,10, 1},{ -1,10, 1},{ 1,11, 1},{ -1,11, 1},{ 1,12, 1},{ -1,12, 1},
228 { 1,13, 1},{ -1,13, 1},{ 2, 1, 2},{ -2, 1, 2},{ 1,14, 1},{ -1,14, 1},
229 { 1,15, 1},{ -1,15, 1},{ 1,16, 1},{ -1,16, 1},{ 1,17, 1},{ -1,17, 1},
230 { 1,18, 1},{ -1,18, 1},{ 1,19, 1},{ -1,19, 1},{ 3, 1, 3},{ -3, 1, 3},
231 { 1,20, 1},{ -1,20, 1},{ 1,21, 1},{ -1,21, 1},{ 2, 2, 2},{ -2, 2, 2},
232 { 1,22, 1},{ -1,22, 1},{ 1,23, 1},{ -1,23, 1},{ 1,24, 1},{ -1,24, 1},
233 { 1,25, 1},{ -1,25, 1},{ 1,26, 1},{ -1,26, 1},{ EOB }
236 { 0, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
237 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
243 { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 1, 2, 0},{ -1, 2, 0},{ 1, 3, 0},
244 { -1, 3, 0},{ 1, 4, 0},{ -1, 4, 0},{ 1, 5, 0},{ -1, 5, 0},{ 1, 6, 0},
245 { -1, 6, 0},{ 2, 1, 1},{ -2, 1, 1},{ 1, 7, 0},{ -1, 7, 0},{ 1, 8, 0},
246 { -1, 8, 0},{ 1, 9, 0},{ -1, 9, 0},{ 1,10, 0},{ -1,10, 0},{ 2, 2, 1},
247 { -2, 2, 1},{ 1,11, 0},{ -1,11, 0},{ 1,12, 0},{ -1,12, 0},{ 3, 1, 2},
248 { -3, 1, 2},{ 1,13, 0},{ -1,13, 0},{ 1,14, 0},{ -1,14, 0},{ 2, 3, 1},
249 { -2, 3, 1},{ 1,15, 0},{ -1,15, 0},{ 2, 4, 1},{ -2, 4, 1},{ 1,16, 0},
250 { -1,16, 0},{ 2, 5, 1},{ -2, 5, 1},{ 1,17, 0},{ -1,17, 0},{ 4, 1, 3},
251 { -4, 1, 3},{ 2, 6, 1},{ -2, 6, 1},{ 1,18, 0},{ -1,18, 0},{ 1,19, 0},
252 { -1,19, 0},{ 2, 7, 1},{ -2, 7, 1},{ 3, 2, 2},{ -3, 2, 2}
255 { 0, 5, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2,
256 2, 2, 2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 0},
308 { -1, 7, 0},{ 6, 2, 0},{ -6, 2, 0},{ 3, 4, 0},{ -3, 4, 0},{ 2, 5, 0},
309 { -2, 5, 0},{ 12, 1, 2},{-12, 1, 2},{ 4, 3, 0},{ -4, 3, 0}
312 { 0,13, 7, 5, 4, 3, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,
313 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
319 { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
320 { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 1, 2, 0},
321 { -1, 2, 0},{ 6, 1, 0},{ -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 0},
322 { -8, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 9, 1, 0},{ -9, 1, 0},{ 1, 3, 0},
323 { -1, 3, 0},{ 10, 1, 1},{-10, 1, 1},{ 3, 2, 0},{ -3, 2, 0},{ 11, 1, 1},
324 {-11, 1, 1},{ 4, 2, 0},{ -4, 2, 0},{ 12, 1, 1},{-12, 1, 1},{ 1, 4, 0},
325 { -1, 4, 0},{ 2, 3, 0},{ -2, 3, 0},{ 13, 1, 1},{-13, 1, 1},{ 5, 2, 0},
326 { -5, 2, 0},{ 14, 1, 1},{-14, 1, 1},{ 6, 2, 0},{ -6, 2, 0},{ 1, 5, 0},
327 { -1, 5, 0},{ 15, 1, 1},{-15, 1, 1},{ 3, 3, 0},{ -3, 3, 0},{ 16, 1, 1},
328 {-16, 1, 1},{ 2, 4, 0},{ -2, 4, 0},{ 7, 2, 0},{ -7, 2, 0}
331 { 0,17, 8, 4, 3, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
332 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
338 { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
339 { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0},
340 { -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 8, 1, 0},
341 { -8, 1, 0},{ 9, 1, 0},{ -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 11, 1, 0},
342 {-11, 1, 0},{ 12, 1, 0},{-12, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 13, 1, 0},
343 {-13, 1, 0},{ 1, 3, 0},{ -1, 3, 0},{ 14, 1, 0},{-14, 1, 0},{ 15, 1, 0},
344 {-15, 1, 0},{ 3, 2, 0},{ -3, 2, 0},{ 16, 1, 0},{-16, 1, 0},{ 17, 1, 0},
345 {-17, 1, 0},{ 18, 1, 0},{-18, 1, 0},{ 4, 2, 0},{ -4, 2, 0},{ 19, 1, 0},
346 {-19, 1, 0},{ 20, 1, 0},{-20, 1, 0},{ 2, 3, 0},{ -2, 3, 0},{ 1, 4, 0},
347 { -1, 4, 0},{ 5, 2, 0},{ -5, 2, 0},{ 21, 1, 0},{-21, 1, 0}
350 { 0,22, 6, 3, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
351 -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,
374 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},{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 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,
431 -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,
450 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
459 /*****************************************************************************
461 * motion vector prediction
463 ****************************************************************************/
465 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,
473 cavs_vector *col_mv) {
474 cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
475 int den = h->direct_den[col_mv->ref];
476 int m = col_mv->x >> 31;
478 pmv_fw->dist = h->dist[1];
479 pmv_bw->dist = h->dist[0];
482 /* scale the co-located motion vector according to its temporal span */
483 pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
484 pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
486 pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
487 pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
490 static inline void mv_pred_sym(AVSContext *h, cavs_vector *src, enum cavs_block size) {
491 cavs_vector *dst = src + MV_BWD_OFFS;
493 /* backward mv is the scaled and negated forward mv */
494 dst->x = -((src->x * h->sym_factor + 256) >> 9);
495 dst->y = -((src->y * h->sym_factor + 256) >> 9);
497 dst->dist = h->dist[0];
501 /*****************************************************************************
503 * residual data decoding
505 ****************************************************************************/
507 /** kth-order exponential golomb code */
508 static inline int get_ue_code(GetBitContext *gb, int order) {
510 int ret = get_ue_golomb(gb) << order;
511 return ret + get_bits(gb,order);
513 return get_ue_golomb(gb);
516 static inline int dequant(AVSContext *h, DCTELEM *level_buf, uint8_t *run_buf,
517 DCTELEM *dst, int mul, int shift, int coeff_num) {
518 int round = 1 << (shift - 1);
520 const uint8_t *scantab = h->scantable.permutated;
522 /* inverse scan and dequantization */
523 while(--coeff_num >= 0){
524 pos += run_buf[coeff_num];
526 av_log(h->s.avctx, AV_LOG_ERROR,
527 "position out of block bounds at pic %d MB(%d,%d)\n",
528 h->picture.poc, h->mbx, h->mby);
531 dst[scantab[pos]] = (level_buf[coeff_num]*mul + round) >> shift;
537 * decode coefficients from one 8x8 block, dequantize, inverse transform
538 * and add them to sample block
539 * @param r pointer to 2D VLC table
540 * @param esc_golomb_order escape codes are k-golomb with this order k
541 * @param qp quantizer
542 * @param dst location of sample block
543 * @param stride line stride in frame buffer
545 static int decode_residual_block(AVSContext *h, GetBitContext *gb,
546 const struct dec_2dvlc *r, int esc_golomb_order,
547 int qp, uint8_t *dst, int stride) {
548 int i, esc_code, level, mask;
549 unsigned int level_code, run;
550 DCTELEM level_buf[65];
552 DCTELEM *block = h->block;
555 level_code = get_ue_code(gb,r->golomb_order);
556 if(level_code >= ESCAPE_CODE) {
557 run = ((level_code - ESCAPE_CODE) >> 1) + 1;
560 esc_code = get_ue_code(gb,esc_golomb_order);
561 level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
562 while(level > r->inc_limit)
564 mask = -(level_code & 1);
565 level = (level^mask) - mask;
567 level = r->rltab[level_code][0];
568 if(!level) //end of block signal
570 run = r->rltab[level_code][1];
571 r += r->rltab[level_code][2];
573 level_buf[i] = level;
576 if (dequant(h, level_buf, run_buf, block, dequant_mul[qp],
577 dequant_shift[qp], i))
579 h->cdsp.cavs_idct8_add(dst,block,stride);
580 h->s.dsp.clear_block(block);
585 static inline void decode_residual_chroma(AVSContext *h) {
587 decode_residual_block(h, &h->s.gb, chroma_dec, 0,
588 cavs_chroma_qp[h->qp], h->cu, h->c_stride);
590 decode_residual_block(h, &h->s.gb, chroma_dec, 0,
591 cavs_chroma_qp[h->qp], h->cv, h->c_stride);
594 static inline int decode_residual_inter(AVSContext *h) {
597 /* get coded block pattern */
598 int cbp= get_ue_golomb(&h->s.gb);
600 av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
603 h->cbp = cbp_tab[cbp][1];
606 if(h->cbp && !h->qp_fixed)
607 h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
608 for(block=0;block<4;block++)
609 if(h->cbp & (1<<block))
610 decode_residual_block(h, &h->s.gb, inter_dec, 0, h->qp,
611 h->cy + h->luma_scan[block], h->l_stride);
612 decode_residual_chroma(h);
617 /*****************************************************************************
621 ****************************************************************************/
623 static inline void set_mv_intra(AVSContext *h) {
624 h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
625 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
626 h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
627 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
628 if(h->pic_type != AV_PICTURE_TYPE_B)
629 h->col_type_base[h->mbidx] = I_8X8;
632 static int decode_mb_i(AVSContext *h, int cbp_code) {
633 GetBitContext *gb = &h->s.gb;
634 unsigned pred_mode_uv;
637 uint8_t *left = NULL;
642 /* get intra prediction modes from stream */
643 for(block=0;block<4;block++) {
645 int pos = scan3x3[block];
647 nA = h->pred_mode_Y[pos-1];
648 nB = h->pred_mode_Y[pos-3];
649 predpred = FFMIN(nA,nB);
650 if(predpred == NOT_AVAIL) // if either is not available
651 predpred = INTRA_L_LP;
653 int rem_mode= get_bits(gb, 2);
654 predpred = rem_mode + (rem_mode >= predpred);
656 h->pred_mode_Y[pos] = predpred;
658 pred_mode_uv = get_ue_golomb(gb);
659 if(pred_mode_uv > 6) {
660 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
663 ff_cavs_modify_mb_i(h, &pred_mode_uv);
665 /* get coded block pattern */
666 if(h->pic_type == AV_PICTURE_TYPE_I)
667 cbp_code = get_ue_golomb(gb);
669 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
672 h->cbp = cbp_tab[cbp_code][0];
673 if(h->cbp && !h->qp_fixed)
674 h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
676 /* luma intra prediction interleaved with residual decode/transform/add */
677 for(block=0;block<4;block++) {
678 d = h->cy + h->luma_scan[block];
679 ff_cavs_load_intra_pred_luma(h, top, &left, block);
680 h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
681 (d, top, left, h->l_stride);
682 if(h->cbp & (1<<block))
683 decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
686 /* chroma intra prediction */
687 ff_cavs_load_intra_pred_chroma(h);
688 h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
689 h->left_border_u, h->c_stride);
690 h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
691 h->left_border_v, h->c_stride);
693 decode_residual_chroma(h);
694 ff_cavs_filter(h,I_8X8);
699 static inline void set_intra_mode_default(AVSContext *h) {
700 if(h->stream_revision > 0) {
701 h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
702 h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = NOT_AVAIL;
704 h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
705 h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
709 static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type) {
710 GetBitContext *gb = &h->s.gb;
716 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
719 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
720 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
723 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
724 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
725 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
726 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
729 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
730 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
731 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
732 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, ref[1]);
735 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
736 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
737 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
738 ref[3] = h->ref_flag ? 0 : get_bits1(gb);
739 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
740 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
741 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
742 ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
744 ff_cavs_inter(h, mb_type);
745 set_intra_mode_default(h);
747 if(mb_type != P_SKIP)
748 decode_residual_inter(h);
749 ff_cavs_filter(h,mb_type);
750 h->col_type_base[h->mbidx] = mb_type;
753 static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type) {
755 enum cavs_sub_mb sub_type[4];
761 h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
762 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
763 h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
764 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
768 if(!h->col_type_base[h->mbidx]) {
769 /* intra MB at co-location, do in-plane prediction */
770 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
771 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
773 /* direct prediction from co-located P MB, block-wise */
774 for(block=0;block<4;block++)
775 mv_pred_direct(h,&h->mv[mv_scan[block]],
776 &h->col_mv[h->mbidx*4 + block]);
779 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
782 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
783 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
786 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
789 for(block=0;block<4;block++)
790 sub_type[block] = get_bits(&h->s.gb,2);
791 for(block=0;block<4;block++) {
792 switch(sub_type[block]) {
794 if(!h->col_type_base[h->mbidx]) {
795 /* intra MB at co-location, do in-plane prediction */
796 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
797 MV_PRED_BSKIP, BLK_8X8, 1);
798 ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
799 mv_scan[block]-3+MV_BWD_OFFS,
800 MV_PRED_BSKIP, BLK_8X8, 0);
802 mv_pred_direct(h,&h->mv[mv_scan[block]],
803 &h->col_mv[h->mbidx*4 + block]);
806 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
807 MV_PRED_MEDIAN, BLK_8X8, 1);
810 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
811 MV_PRED_MEDIAN, BLK_8X8, 1);
812 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
816 for(block=0;block<4;block++) {
817 if(sub_type[block] == B_SUB_BWD)
818 ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
819 mv_scan[block]+MV_BWD_OFFS-3,
820 MV_PRED_MEDIAN, BLK_8X8, 0);
824 if (mb_type <= B_SYM_16X16) {
825 av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mb_type %d in B frame\n", mb_type);
826 return AVERROR_INVALIDDATA;
828 av_assert2(mb_type < B_8X8);
829 flags = ff_cavs_partition_flags[mb_type];
830 if(mb_type & 1) { /* 16x8 macroblock types */
832 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
834 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
836 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
838 mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
840 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
842 ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
843 } else { /* 8x16 macroblock types */
845 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
847 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
849 ff_cavs_mv(h,MV_FWD_X1,MV_FWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,1);
851 mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
853 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
855 ff_cavs_mv(h,MV_BWD_X1,MV_BWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,0);
858 ff_cavs_inter(h, mb_type);
859 set_intra_mode_default(h);
860 if(mb_type != B_SKIP)
861 decode_residual_inter(h);
862 ff_cavs_filter(h,mb_type);
867 /*****************************************************************************
871 ****************************************************************************/
873 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
875 av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
877 if (h->stc >= h->mb_height)
881 h->mbidx = h->mby*h->mb_width;
883 /* mark top macroblocks as unavailable */
884 h->flags &= ~(B_AVAIL|C_AVAIL);
885 if((h->mby == 0) && (!h->qp_fixed)){
886 h->qp_fixed = get_bits1(gb);
887 h->qp = get_bits(gb,6);
889 /* inter frame or second slice can have weighting params */
890 if((h->pic_type != AV_PICTURE_TYPE_I) || (!h->pic_structure && h->mby >= h->mb_width/2))
891 if(get_bits1(gb)) { //slice_weighting_flag
892 av_log(h->s.avctx, AV_LOG_ERROR,
893 "weighted prediction not yet supported\n");
898 static inline int check_for_slice(AVSContext *h) {
899 GetBitContext *gb = &h->s.gb;
904 align = (-get_bits_count(gb)) & 7;
905 /* check for stuffing byte */
906 if(!align && (show_bits(gb,8) == 0x80))
908 if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
909 skip_bits_long(gb,24+align);
910 h->stc = get_bits(gb,8);
911 if (h->stc >= h->mb_height)
913 decode_slice_header(h,gb);
919 /*****************************************************************************
923 ****************************************************************************/
925 static int decode_pic(AVSContext *h) {
926 MpegEncContext *s = &h->s;
928 enum cavs_mb mb_type;
930 if (!s->context_initialized) {
931 if (ff_MPV_common_init(s) < 0)
933 ff_init_scantable_permutation(s->dsp.idct_permutation,
935 ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
937 skip_bits(&s->gb,16);//bbv_dwlay
938 if(h->stc == PIC_PB_START_CODE) {
939 h->pic_type = get_bits(&s->gb,2) + AV_PICTURE_TYPE_I;
940 if(h->pic_type > AV_PICTURE_TYPE_B) {
941 av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
944 /* make sure we have the reference frames we need */
945 if(!h->DPB[0].f.data[0] ||
946 (!h->DPB[1].f.data[0] && h->pic_type == AV_PICTURE_TYPE_B))
949 h->pic_type = AV_PICTURE_TYPE_I;
950 if(get_bits1(&s->gb))
951 skip_bits(&s->gb,24);//time_code
952 /* old sample clips were all progressive and no low_delay,
953 bump stream revision if detected otherwise */
954 if (s->low_delay || !(show_bits(&s->gb,9) & 1))
955 h->stream_revision = 1;
956 /* similarly test top_field_first and repeat_first_field */
957 else if(show_bits(&s->gb,11) & 3)
958 h->stream_revision = 1;
959 if(h->stream_revision > 0)
960 skip_bits(&s->gb,1); //marker_bit
962 /* release last B frame */
963 if(h->picture.f.data[0])
964 s->avctx->release_buffer(s->avctx, &h->picture.f);
966 ff_get_buffer(s->avctx, &h->picture.f);
968 h->picture.poc = get_bits(&s->gb,8)*2;
970 /* get temporal distances and MV scaling factors */
971 if(h->pic_type != AV_PICTURE_TYPE_B) {
972 h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
974 h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
976 h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
977 h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
978 h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
979 if(h->pic_type == AV_PICTURE_TYPE_B) {
980 h->sym_factor = h->dist[0]*h->scale_den[1];
982 h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
983 h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
987 get_ue_golomb(&s->gb); //bbv_check_times
988 h->progressive = get_bits1(&s->gb);
989 h->pic_structure = 1;
991 h->pic_structure = get_bits1(&s->gb);
992 if(!h->pic_structure && h->stc == PIC_PB_START_CODE)
993 skip_bits1(&s->gb); //advanced_pred_mode_disable
994 skip_bits1(&s->gb); //top_field_first
995 skip_bits1(&s->gb); //repeat_first_field
996 h->qp_fixed = get_bits1(&s->gb);
997 h->qp = get_bits(&s->gb,6);
998 if(h->pic_type == AV_PICTURE_TYPE_I) {
999 if(!h->progressive && !h->pic_structure)
1000 skip_bits1(&s->gb);//what is this?
1001 skip_bits(&s->gb,4); //reserved bits
1003 if(!(h->pic_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
1004 h->ref_flag = get_bits1(&s->gb);
1005 skip_bits(&s->gb,4); //reserved bits
1006 h->skip_mode_flag = get_bits1(&s->gb);
1008 h->loop_filter_disable = get_bits1(&s->gb);
1009 if(!h->loop_filter_disable && get_bits1(&s->gb)) {
1010 h->alpha_offset = get_se_golomb(&s->gb);
1011 h->beta_offset = get_se_golomb(&s->gb);
1013 h->alpha_offset = h->beta_offset = 0;
1015 if(h->pic_type == AV_PICTURE_TYPE_I) {
1019 } while(ff_cavs_next_mb(h));
1020 } else if(h->pic_type == AV_PICTURE_TYPE_P) {
1022 if(check_for_slice(h))
1024 if(h->skip_mode_flag && (skip_count < 0))
1025 skip_count = get_ue_golomb(&s->gb);
1026 if(h->skip_mode_flag && skip_count--) {
1027 decode_mb_p(h,P_SKIP);
1029 mb_type = get_ue_golomb(&s->gb) + P_SKIP + h->skip_mode_flag;
1031 decode_mb_i(h, mb_type - P_8X8 - 1);
1033 decode_mb_p(h,mb_type);
1035 } while(ff_cavs_next_mb(h));
1036 } else { /* AV_PICTURE_TYPE_B */
1038 if(check_for_slice(h))
1040 if(h->skip_mode_flag && (skip_count < 0))
1041 skip_count = get_ue_golomb(&s->gb);
1042 if(h->skip_mode_flag && skip_count--) {
1043 decode_mb_b(h,B_SKIP);
1045 mb_type = get_ue_golomb(&s->gb) + B_SKIP + h->skip_mode_flag;
1047 decode_mb_i(h, mb_type - B_8X8 - 1);
1049 decode_mb_b(h,mb_type);
1051 } while(ff_cavs_next_mb(h));
1053 if(h->pic_type != AV_PICTURE_TYPE_B) {
1054 if(h->DPB[1].f.data[0])
1055 s->avctx->release_buffer(s->avctx, &h->DPB[1].f);
1056 h->DPB[1] = h->DPB[0];
1057 h->DPB[0] = h->picture;
1058 memset(&h->picture,0,sizeof(Picture));
1063 /*****************************************************************************
1065 * headers and interface
1067 ****************************************************************************/
1069 static int decode_seq_header(AVSContext *h) {
1070 MpegEncContext *s = &h->s;
1071 int frame_rate_code;
1074 h->profile = get_bits(&s->gb,8);
1075 h->level = get_bits(&s->gb,8);
1076 skip_bits1(&s->gb); //progressive sequence
1078 width = get_bits(&s->gb, 14);
1079 height = get_bits(&s->gb, 14);
1080 if ((s->width || s->height) && (s->width != width || s->height != height)) {
1081 av_log_missing_feature(s, "Width/height changing in CAVS", 0);
1082 return AVERROR_PATCHWELCOME;
1084 if (width <= 0 || height <= 0) {
1085 av_log(s, AV_LOG_ERROR, "Dimensions invalid\n");
1086 return AVERROR_INVALIDDATA;
1091 skip_bits(&s->gb,2); //chroma format
1092 skip_bits(&s->gb,3); //sample_precision
1093 h->aspect_ratio = get_bits(&s->gb,4);
1094 frame_rate_code = get_bits(&s->gb,4);
1095 skip_bits(&s->gb,18);//bit_rate_lower
1096 skip_bits1(&s->gb); //marker_bit
1097 skip_bits(&s->gb,12);//bit_rate_upper
1098 s->low_delay = get_bits1(&s->gb);
1099 h->mb_width = (s->width + 15) >> 4;
1100 h->mb_height = (s->height + 15) >> 4;
1101 h->s.avctx->time_base.den = ff_mpeg12_frame_rate_tab[frame_rate_code].num;
1102 h->s.avctx->time_base.num = ff_mpeg12_frame_rate_tab[frame_rate_code].den;
1103 h->s.avctx->width = s->width;
1104 h->s.avctx->height = s->height;
1106 ff_cavs_init_top_lines(h);
1110 static void cavs_flush(AVCodecContext * avctx) {
1111 AVSContext *h = avctx->priv_data;
1112 h->got_keyframe = 0;
1115 static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1117 const uint8_t *buf = avpkt->data;
1118 int buf_size = avpkt->size;
1119 AVSContext *h = avctx->priv_data;
1120 MpegEncContext *s = &h->s;
1122 const uint8_t *buf_end;
1123 const uint8_t *buf_ptr;
1124 AVFrame *picture = data;
1129 if (buf_size == 0) {
1130 if (!s->low_delay && h->DPB[0].f.data[0]) {
1132 *picture = h->DPB[0].f;
1133 memset(&h->DPB[0], 0, sizeof(h->DPB[0]));
1139 buf_end = buf + buf_size;
1141 buf_ptr = avpriv_mpv_find_start_code(buf_ptr,buf_end, &stc);
1142 if((stc & 0xFFFFFE00) || buf_ptr == buf_end)
1143 return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
1144 input_size = (buf_end - buf_ptr)*8;
1146 case CAVS_START_CODE:
1147 init_get_bits(&s->gb, buf_ptr, input_size);
1148 decode_seq_header(h);
1150 case PIC_I_START_CODE:
1151 if(!h->got_keyframe) {
1152 if(h->DPB[0].f.data[0])
1153 avctx->release_buffer(avctx, &h->DPB[0].f);
1154 if(h->DPB[1].f.data[0])
1155 avctx->release_buffer(avctx, &h->DPB[1].f);
1156 h->got_keyframe = 1;
1158 case PIC_PB_START_CODE:
1160 if(!h->got_keyframe)
1164 init_get_bits(&s->gb, buf_ptr, input_size);
1169 if(h->pic_type != AV_PICTURE_TYPE_B) {
1170 if(h->DPB[1].f.data[0]) {
1171 *picture = h->DPB[1].f;
1176 *picture = h->picture.f;
1178 case EXT_START_CODE:
1179 //mpeg_decode_extension(avctx,buf_ptr, input_size);
1181 case USER_START_CODE:
1182 //mpeg_decode_user_data(avctx,buf_ptr, input_size);
1185 if (stc <= SLICE_MAX_START_CODE) {
1186 init_get_bits(&s->gb, buf_ptr, input_size);
1187 decode_slice_header(h, &s->gb);
1194 AVCodec ff_cavs_decoder = {
1196 .type = AVMEDIA_TYPE_VIDEO,
1197 .id = AV_CODEC_ID_CAVS,
1198 .priv_data_size = sizeof(AVSContext),
1199 .init = ff_cavs_init,
1200 .close = ff_cavs_end,
1201 .decode = cavs_decode_frame,
1202 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1203 .flush = cavs_flush,
1204 .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),