Libav
cavsdec.c
Go to the documentation of this file.
1 /*
2  * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
3  * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21 
28 #include "avcodec.h"
29 #include "get_bits.h"
30 #include "golomb.h"
31 #include "cavs.h"
32 #include "internal.h"
33 #include "mpeg12data.h"
34 
35 static const uint8_t mv_scan[4] = {
38 };
39 
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 }
49 };
50 
51 static const uint8_t scan3x3[4] = { 4, 5, 7, 8 };
52 
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
58 };
59 
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
69 };
70 
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
80 };
81 
82 #define EOB 0, 0, 0
83 
84 static const struct dec_2dvlc intra_dec[7] = {
85  {
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 }
97  },
98  //level_add
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 },
100  2, //golomb_order
101  0, //inc_limit
102  23, //max_run
103  },
104  {
105  { //level / run
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 }
116  },
117  //level_add
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 },
119  2, //golomb_order
120  1, //inc_limit
121  15, //max_run
122  },
123  {
124  { //level / run
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 }
135  },
136  //level_add
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 },
138  2, //golomb_order
139  2, //inc_limit
140  10, //max_run
141  },
142  {
143  { //level / run
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 }
154  },
155  //level_add
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 },
157  2, //golomb_order
158  4, //inc_limit
159  7, //max_run
160  },
161  {
162  { //level / run
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 }
173  },
174  //level_add
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 },
176  2, //golomb_order
177  7, //inc_limit
178  5, //max_run
179  },
180  {
181  { //level / run
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 }
192  },
193  //level_add
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 },
195  2, //golomb_order
196  10, //inc_limit
197  3, //max_run
198  },
199  {
200  { //level / run
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 }
211  },
212  //level_add
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 },
214  2, //golomb_order
215  INT_MAX, //inc_limit
216  2, //max_run
217  }
218 };
219 
220 static const struct dec_2dvlc inter_dec[7] = {
221  {
222  { //level / run
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 }
233  },
234  //level_add
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 },
236  3, //golomb_order
237  0, //inc_limit
238  26 //max_run
239  },
240  {
241  { //level / run
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 }
252  },
253  //level_add
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 },
255  2, //golomb_order
256  1, //inc_limit
257  19 //max_run
258  },
259  {
260  { //level / run
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 }
271  },
272  //level_add
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 },
274  2, //golomb_order
275  2, //inc_limit
276  14 //max_run
277  },
278  {
279  { //level / run
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 }
290  },
291  //level_add
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 },
293  2, //golomb_order
294  3, //inc_limit
295  10 //max_run
296  },
297  {
298  { //level / run
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 }
309  },
310  //level_add
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 },
312  2, //golomb_order
313  6, //inc_limit
314  7 //max_run
315  },
316  {
317  { //level / run
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 }
328  },
329  //level_add
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 },
331  2, //golomb_order
332  9, //inc_limit
333  5 //max_run
334  },
335  {
336  { //level / run
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 }
347  },
348  //level_add
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 },
350  2, //golomb_order
351  INT_MAX, //inc_limit
352  4 //max_run
353  }
354 };
355 
356 static const struct dec_2dvlc chroma_dec[5] = {
357  {
358  { //level / run
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 }
369  },
370  //level_add
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 },
372  2, //golomb_order
373  0, //inc_limit
374  25 //max_run
375  },
376  {
377  { //level / run
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 }
388  },
389  //level_add
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 },
391  0, //golomb_order
392  1, //inc_limit
393  20 //max_run
394  },
395  {
396  { //level / run
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 },
407  },
408  //level_add
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 },
410  1, //golomb_order
411  2, //inc_limit
412  11 //max_run
413  },
414  {
415  { //level / run
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 },
426  },
427  //level_add
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 },
429  1, //golomb_order
430  4, //inc_limit
431  8 //max_run
432  },
433  {
434  { //level / run
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 },
445  },
446  //level_add
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 },
448  0, //golomb_order
449  INT_MAX, //inc_limit
450  5, //max_run
451  }
452 };
453 
454 #undef EOB
455 
456 /*****************************************************************************
457  *
458  * motion vector prediction
459  *
460  ****************************************************************************/
461 
462 static inline void store_mvs(AVSContext *h)
463 {
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];
468 }
469 
470 static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
471  cavs_vector *col_mv)
472 {
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);
476 
477  pmv_fw->dist = h->dist[1];
478  pmv_bw->dist = h->dist[0];
479  pmv_fw->ref = 1;
480  pmv_bw->ref = 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);
487 }
488 
489 static inline void mv_pred_sym(AVSContext *h, cavs_vector *src,
490  enum cavs_block size)
491 {
492  cavs_vector *dst = src + MV_BWD_OFFS;
493 
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);
497  dst->ref = 0;
498  dst->dist = h->dist[0];
499  set_mvs(dst, size);
500 }
501 
502 /*****************************************************************************
503  *
504  * residual data decoding
505  *
506  ****************************************************************************/
507 
509 static inline int get_ue_code(GetBitContext *gb, int order)
510 {
511  if (order) {
512  int ret = get_ue_golomb(gb) << order;
513  return ret + get_bits(gb, order);
514  }
515  return get_ue_golomb(gb);
516 }
517 
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)
520 {
521  int round = 1 << (shift - 1);
522  int pos = -1;
523  const uint8_t *scantab = h->scantable.permutated;
524 
525  /* inverse scan and dequantization */
526  while (--coeff_num >= 0) {
527  pos += run_buf[coeff_num];
528  if (pos > 63) {
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;
533  }
534  dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
535  }
536  return 0;
537 }
538 
549  const struct dec_2dvlc *r, int esc_golomb_order,
550  int qp, uint8_t *dst, ptrdiff_t stride)
551 {
552  int i, level_code, esc_code, level, run, mask, ret;
553  int16_t level_buf[65];
554  uint8_t run_buf[65];
555  int16_t *block = h->block;
556 
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;
561  esc_code = get_ue_code(gb, esc_golomb_order);
562  level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
563  while (level > r->inc_limit)
564  r++;
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
570  break;
571  run = r->rltab[level_code][1];
572  r += r->rltab[level_code][2];
573  } else {
574  break;
575  }
576  level_buf[i] = level;
577  run_buf[i] = run;
578  }
579  if ((ret = dequant(h, level_buf, run_buf, block, dequant_mul[qp],
580  dequant_shift[qp], i)) < 0)
581  return ret;
582  h->cdsp.cavs_idct8_add(dst, block, stride);
583  h->bdsp.clear_block(block);
584  return 0;
585 }
586 
587 
588 static inline void decode_residual_chroma(AVSContext *h)
589 {
590  if (h->cbp & (1 << 4))
591  decode_residual_block(h, &h->gb, 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->gb, chroma_dec, 0,
595  cavs_chroma_qp[h->qp], h->cv, h->c_stride);
596 }
597 
598 static inline int decode_residual_inter(AVSContext *h)
599 {
600  int block;
601 
602  /* get coded block pattern */
603  int cbp = get_ue_golomb(&h->gb);
604  if (cbp > 63 || cbp < 0) {
605  av_log(h->avctx, AV_LOG_ERROR, "illegal inter cbp %d\n", cbp);
606  return AVERROR_INVALIDDATA;
607  }
608  h->cbp = cbp_tab[cbp][1];
609 
610  /* get quantizer */
611  if (h->cbp && !h->qp_fixed)
612  h->qp = (h->qp + get_se_golomb(&h->gb)) & 63;
613  for (block = 0; block < 4; block++)
614  if (h->cbp & (1 << block))
615  decode_residual_block(h, &h->gb, inter_dec, 0, h->qp,
616  h->cy + h->luma_scan[block], h->l_stride);
618 
619  return 0;
620 }
621 
622 /*****************************************************************************
623  *
624  * macroblock level
625  *
626  ****************************************************************************/
627 
628 static inline void set_mv_intra(AVSContext *h)
629 {
631  set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
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;
636 }
637 
638 static int decode_mb_i(AVSContext *h, int cbp_code)
639 {
640  GetBitContext *gb = &h->gb;
641  unsigned pred_mode_uv;
642  int block;
643  uint8_t top[18];
644  uint8_t *left = NULL;
645  uint8_t *d;
646 
647  ff_cavs_init_mb(h);
648 
649  /* get intra prediction modes from stream */
650  for (block = 0; block < 4; block++) {
651  int nA, nB, predpred;
652  int pos = scan3x3[block];
653 
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 (!get_bits1(gb)) {
660  int rem_mode = get_bits(gb, 2);
661  predpred = rem_mode + (rem_mode >= predpred);
662  }
663  h->pred_mode_Y[pos] = predpred;
664  }
665  pred_mode_uv = get_ue_golomb(gb);
666  if (pred_mode_uv > 6) {
667  av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
668  return AVERROR_INVALIDDATA;
669  }
670  ff_cavs_modify_mb_i(h, &pred_mode_uv);
671 
672  /* get coded block pattern */
673  if (h->cur.f->pict_type == AV_PICTURE_TYPE_I)
674  cbp_code = get_ue_golomb(gb);
675  if (cbp_code > 63 || cbp_code < 0) {
676  av_log(h->avctx, AV_LOG_ERROR, "illegal intra cbp\n");
677  return AVERROR_INVALIDDATA;
678  }
679  h->cbp = cbp_tab[cbp_code][0];
680  if (h->cbp && !h->qp_fixed)
681  h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
682 
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);
688  (d, top, left, h->l_stride);
689  if (h->cbp & (1<<block))
690  decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
691  }
692 
693  /* chroma intra prediction */
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);
699 
701  ff_cavs_filter(h, I_8X8);
702  set_mv_intra(h);
703  return 0;
704 }
705 
706 static inline void set_intra_mode_default(AVSContext *h)
707 {
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;
711  } else {
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;
714  }
715 }
716 
717 static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
718 {
719  GetBitContext *gb = &h->gb;
720  int ref[4];
721 
722  ff_cavs_init_mb(h);
723  switch (mb_type) {
724  case P_SKIP:
726  break;
727  case P_16X16:
728  ref[0] = h->ref_flag ? 0 : get_bits1(gb);
730  break;
731  case P_16X8:
732  ref[0] = h->ref_flag ? 0 : get_bits1(gb);
733  ref[2] = h->ref_flag ? 0 : get_bits1(gb);
736  break;
737  case P_8X16:
738  ref[0] = h->ref_flag ? 0 : get_bits1(gb);
739  ref[1] = h->ref_flag ? 0 : get_bits1(gb);
742  break;
743  case P_8X8:
744  ref[0] = h->ref_flag ? 0 : get_bits1(gb);
745  ref[1] = h->ref_flag ? 0 : get_bits1(gb);
746  ref[2] = h->ref_flag ? 0 : get_bits1(gb);
747  ref[3] = h->ref_flag ? 0 : get_bits1(gb);
752  }
753  ff_cavs_inter(h, mb_type);
755  store_mvs(h);
756  if (mb_type != P_SKIP)
758  ff_cavs_filter(h, mb_type);
759  h->col_type_base[h->mbidx] = mb_type;
760 }
761 
762 static void decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
763 {
764  int block;
765  enum cavs_sub_mb sub_type[4];
766  int flags;
767 
768  ff_cavs_init_mb(h);
769 
770  /* reset all MVs */
772  set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
774  set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
775  switch (mb_type) {
776  case B_SKIP:
777  case B_DIRECT:
778  if (!h->col_type_base[h->mbidx]) {
779  /* intra MB at co-location, do in-plane prediction */
782  } else
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]);
787  break;
788  case B_FWD_16X16:
790  break;
791  case B_SYM_16X16:
793  mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
794  break;
795  case B_BWD_16X16:
797  break;
798  case B_8X8:
799  for (block = 0; block < 4; block++)
800  sub_type[block] = get_bits(&h->gb, 2);
801  for (block = 0; block < 4; block++) {
802  switch (sub_type[block]) {
803  case B_SUB_DIRECT:
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);
811  } else
812  mv_pred_direct(h, &h->mv[mv_scan[block]],
813  &h->col_mv[h->mbidx * 4 + block]);
814  break;
815  case B_SUB_FWD:
816  ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
817  MV_PRED_MEDIAN, BLK_8X8, 1);
818  break;
819  case B_SUB_SYM:
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);
823  break;
824  }
825  }
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);
831  }
832  break;
833  default:
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 */
837  if (flags & FWD0)
839  if (flags & SYM0)
840  mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
841  if (flags & FWD1)
843  if (flags & SYM1)
844  mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
845  if (flags & BWD0)
847  if (flags & BWD1)
849  } else { /* 8x16 macroblock types */
850  if (flags & FWD0)
852  if (flags & SYM0)
853  mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
854  if (flags & FWD1)
856  if (flags & SYM1)
857  mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
858  if (flags & BWD0)
860  if (flags & BWD1)
862  }
863  }
864  ff_cavs_inter(h, mb_type);
866  if (mb_type != B_SKIP)
868  ff_cavs_filter(h, mb_type);
869 }
870 
871 /*****************************************************************************
872  *
873  * slice level
874  *
875  ****************************************************************************/
876 
877 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb)
878 {
879  if (h->stc > 0xAF)
880  av_log(h->avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
881  h->mby = h->stc;
882  h->mbidx = h->mby * h->mb_width;
883 
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 = get_bits1(gb);
888  h->qp = get_bits(gb, 6);
889  }
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 (get_bits1(gb)) { //slice_weighting_flag
895  "weighted prediction not yet supported\n");
896  }
897  return 0;
898 }
899 
900 static inline int check_for_slice(AVSContext *h)
901 {
902  GetBitContext *gb = &h->gb;
903  int align;
904 
905  if (h->mbx)
906  return 0;
907  align = (-get_bits_count(gb)) & 7;
908  /* check for stuffing byte */
909  if (!align && (show_bits(gb, 8) == 0x80))
910  align = 8;
911  if ((show_bits_long(gb, 24 + align) & 0xFFFFFF) == 0x000001) {
912  skip_bits_long(gb, 24 + align);
913  h->stc = get_bits(gb, 8);
914  if (h->stc >= h->mb_height)
915  return 0;
916  decode_slice_header(h, gb);
917  return 1;
918  }
919  return 0;
920 }
921 
922 /*****************************************************************************
923  *
924  * frame level
925  *
926  ****************************************************************************/
927 
928 static int decode_pic(AVSContext *h)
929 {
930  int ret;
931  int skip_count = -1;
932  enum cavs_mb mb_type;
933 
934  if (!h->top_qp) {
935  av_log(h->avctx, AV_LOG_ERROR, "No sequence header decoded yet\n");
936  return AVERROR_INVALIDDATA;
937  }
938 
939  av_frame_unref(h->cur.f);
940 
941  skip_bits(&h->gb, 16);//bbv_dwlay
942  if (h->stc == PIC_PB_START_CODE) {
943  h->cur.f->pict_type = get_bits(&h->gb, 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;
947  }
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;
952  } else {
954  if (get_bits1(&h->gb))
955  skip_bits(&h->gb, 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 || !(show_bits(&h->gb, 9) & 1))
959  h->stream_revision = 1;
960  /* similarly test top_field_first and repeat_first_field */
961  else if (show_bits(&h->gb, 11) & 3)
962  h->stream_revision = 1;
963  if (h->stream_revision > 0)
964  skip_bits(&h->gb, 1); //marker_bit
965  }
966 
967  ret = ff_get_buffer(h->avctx, h->cur.f, h->cur.f->pict_type == AV_PICTURE_TYPE_B ?
969  if (ret < 0)
970  return ret;
971 
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);
977  }
978 
979  ff_cavs_init_pic(h);
980  h->cur.poc = get_bits(&h->gb, 8) * 2;
981 
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;
985  } else {
986  h->dist[0] = (h->DPB[0].poc - h->cur.poc + 512) % 512;
987  }
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];
993  } else {
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;
996  }
997 
998  if (h->low_delay)
999  get_ue_golomb(&h->gb); //bbv_check_times
1000  h->progressive = get_bits1(&h->gb);
1001  h->pic_structure = 1;
1002  if (!h->progressive)
1003  h->pic_structure = get_bits1(&h->gb);
1004  if (!h->pic_structure && h->stc == PIC_PB_START_CODE)
1005  skip_bits1(&h->gb); //advanced_pred_mode_disable
1006  skip_bits1(&h->gb); //top_field_first
1007  skip_bits1(&h->gb); //repeat_first_field
1008  h->qp_fixed = get_bits1(&h->gb);
1009  h->qp = get_bits(&h->gb, 6);
1010  if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1011  if (!h->progressive && !h->pic_structure)
1012  skip_bits1(&h->gb);//what is this?
1013  skip_bits(&h->gb, 4); //reserved bits
1014  } else {
1015  if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
1016  h->ref_flag = get_bits1(&h->gb);
1017  skip_bits(&h->gb, 4); //reserved bits
1018  h->skip_mode_flag = get_bits1(&h->gb);
1019  }
1020  h->loop_filter_disable = get_bits1(&h->gb);
1021  if (!h->loop_filter_disable && get_bits1(&h->gb)) {
1022  h->alpha_offset = get_se_golomb(&h->gb);
1023  h->beta_offset = get_se_golomb(&h->gb);
1024  } else {
1025  h->alpha_offset = h->beta_offset = 0;
1026  }
1027  if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1028  do {
1029  check_for_slice(h);
1030  decode_mb_i(h, 0);
1031  } while (ff_cavs_next_mb(h));
1032  } else if (h->cur.f->pict_type == AV_PICTURE_TYPE_P) {
1033  do {
1034  if (check_for_slice(h))
1035  skip_count = -1;
1036  if (h->skip_mode_flag && (skip_count < 0))
1037  skip_count = get_ue_golomb(&h->gb);
1038  if (h->skip_mode_flag && skip_count--) {
1039  decode_mb_p(h, P_SKIP);
1040  } else {
1041  mb_type = get_ue_golomb(&h->gb) + P_SKIP + h->skip_mode_flag;
1042  if (mb_type > P_8X8)
1043  decode_mb_i(h, mb_type - P_8X8 - 1);
1044  else
1045  decode_mb_p(h, mb_type);
1046  }
1047  } while (ff_cavs_next_mb(h));
1048  } else { /* AV_PICTURE_TYPE_B */
1049  do {
1050  if (check_for_slice(h))
1051  skip_count = -1;
1052  if (h->skip_mode_flag && (skip_count < 0))
1053  skip_count = get_ue_golomb(&h->gb);
1054  if (h->skip_mode_flag && skip_count--) {
1055  decode_mb_b(h, B_SKIP);
1056  } else {
1057  mb_type = get_ue_golomb(&h->gb) + B_SKIP + h->skip_mode_flag;
1058  if (mb_type > B_8X8)
1059  decode_mb_i(h, mb_type - B_8X8 - 1);
1060  else
1061  decode_mb_b(h, mb_type);
1062  }
1063  } while (ff_cavs_next_mb(h));
1064  }
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]);
1069  }
1070  return 0;
1071 }
1072 
1073 /*****************************************************************************
1074  *
1075  * headers and interface
1076  *
1077  ****************************************************************************/
1078 
1080 {
1081  int frame_rate_code;
1082  int width, height;
1083 
1084  h->profile = get_bits(&h->gb, 8);
1085  h->level = get_bits(&h->gb, 8);
1086  skip_bits1(&h->gb); //progressive sequence
1087 
1088  width = get_bits(&h->gb, 14);
1089  height = get_bits(&h->gb, 14);
1090  if ((h->width || h->height) && (h->width != width || h->height != height)) {
1092  "Width/height changing in CAVS");
1093  return AVERROR_PATCHWELCOME;
1094  }
1095  h->width = width;
1096  h->height = height;
1097 
1098  skip_bits(&h->gb, 2); //chroma format
1099  skip_bits(&h->gb, 3); //sample_precision
1100  h->aspect_ratio = get_bits(&h->gb, 4);
1101  frame_rate_code = get_bits(&h->gb, 4);
1102  skip_bits(&h->gb, 18); //bit_rate_lower
1103  skip_bits1(&h->gb); //marker_bit
1104  skip_bits(&h->gb, 12); //bit_rate_upper
1105  h->low_delay = get_bits1(&h->gb);
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;
1111  if (!h->top_qp)
1113  return 0;
1114 }
1115 
1116 static void cavs_flush(AVCodecContext * avctx)
1117 {
1118  AVSContext *h = avctx->priv_data;
1119  h->got_keyframe = 0;
1120 }
1121 
1122 static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1123  AVPacket *avpkt)
1124 {
1125  AVSContext *h = avctx->priv_data;
1126  const uint8_t *buf = avpkt->data;
1127  int buf_size = avpkt->size;
1128  uint32_t stc = -1;
1129  int input_size, ret;
1130  const uint8_t *buf_end;
1131  const uint8_t *buf_ptr;
1132 
1133  if (buf_size == 0) {
1134  if (!h->low_delay && h->DPB[0].f->data[0]) {
1135  *got_frame = 1;
1136  av_frame_move_ref(data, h->DPB[0].f);
1137  }
1138  return 0;
1139  }
1140 
1141  buf_ptr = buf;
1142  buf_end = buf + buf_size;
1143  for(;;) {
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;
1148  switch (stc) {
1149  case CAVS_START_CODE:
1150  init_get_bits(&h->gb, buf_ptr, input_size);
1151  decode_seq_header(h);
1152  break;
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;
1158  }
1159  case PIC_PB_START_CODE:
1160  *got_frame = 0;
1161  if (!h->got_keyframe)
1162  break;
1163  init_get_bits(&h->gb, buf_ptr, input_size);
1164  h->stc = stc;
1165  if (decode_pic(h))
1166  break;
1167  *got_frame = 1;
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)
1171  return ret;
1172  } else {
1173  *got_frame = 0;
1174  }
1175  } else {
1176  av_frame_move_ref(data, h->cur.f);
1177  }
1178  break;
1179  case EXT_START_CODE:
1180  //mpeg_decode_extension(avctx, buf_ptr, input_size);
1181  break;
1182  case USER_START_CODE:
1183  //mpeg_decode_user_data(avctx, buf_ptr, input_size);
1184  break;
1185  default:
1186  if (stc <= SLICE_MAX_START_CODE) {
1187  init_get_bits(&h->gb, buf_ptr, input_size);
1188  decode_slice_header(h, &h->gb);
1189  }
1190  break;
1191  }
1192  }
1193 }
1194 
1196  .name = "cavs",
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,
1204  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1205  .flush = cavs_flush,
1206 };
uint8_t * top_border_v
Definition: cavs.h:224
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:344
int pic_structure
Definition: cavs.h:181
uint8_t * top_border_u
Definition: cavs.h:224
void ff_cavs_modify_mb_i(AVSContext *h, int *pred_mode_uv)
Definition: cavs.c:353
AVRational framerate
Definition: avcodec.h:3063
static const struct dec_2dvlc chroma_dec[5]
Definition: cavsdec.c:356
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
Definition: cavs.h:73
#define SLICE_MAX_START_CODE
Definition: cavs.h:32
int got_keyframe
Definition: cavs.h:240
AVCodecContext * avctx
Definition: cavs.h:164
int inc_limit
Definition: cavs.h:154
int size
uint8_t * edge_emu_buffer
Definition: cavs.h:238
int direct_den[2]
for scaling in direct B block
Definition: cavs.h:235
int cbp
Definition: cavs.h:219
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:179
const cavs_vector ff_cavs_intra_mv
mark block as using intra prediction
Definition: cavsdata.c:62
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
Definition: cavs.h:62
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:187
int16_t x
Definition: cavs.h:144
static const uint8_t dequant_shift[64]
Definition: cavsdec.c:60
void(* clear_block)(int16_t *block)
Definition: blockdsp.h:35
av_cold int ff_cavs_end(AVCodecContext *avctx)
Definition: cavs.c:805
int mbidx
macroblock coordinates
Definition: cavs.h:186
#define BWD1
Definition: cavs.h:52
static const uint8_t scan3x3[4]
Definition: cavsdec.c:51
cavs_vector * col_mv
Definition: cavs.h:207
int poc
Definition: cavs.h:160
Definition: cavs.h:69
#define SYM1
Definition: cavs.h:54
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
void ff_cavs_init_mb(AVSContext *h)
initialise predictors for motion vectors and intra prediction
Definition: cavs.c:621
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:319
Definition: cavs.h:66
int qp
Definition: cavs.h:217
uint8_t permutated[64]
Definition: idctdsp.h:31
uint8_t run
Definition: svq3.c:203
int loop_filter_disable
Definition: cavs.h:183
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3120
static int decode_seq_header(AVSContext *h)
Definition: cavsdec.c:1079
cavs_mb
Definition: cavs.h:61
static int16_t block[64]
Definition: dct.c:97
#define MV_BWD_OFFS
Definition: cavs.h:58
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:863
int mbx
Definition: cavs.h:186
static int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf, int16_t *dst, int mul, int shift, int coeff_num)
Definition: cavsdec.c:518
ScanTable scantable
Definition: cavs.h:220
#define FWD1
Definition: cavs.h:50
void(* cavs_idct8_add)(uint8_t *dst, int16_t *block, ptrdiff_t stride)
Definition: cavsdsp.h:37
static void set_mv_intra(AVSContext *h)
Definition: cavsdec.c:628
uint8_t
int ref_flag
Definition: cavs.h:185
static int decode_residual_block(AVSContext *h, GetBitContext *gb, const struct dec_2dvlc *r, int esc_golomb_order, int qp, uint8_t *dst, ptrdiff_t stride)
decode coefficients from one 8x8 block, dequantize, inverse transform and add them to sample block ...
Definition: cavsdec.c:548
uint8_t * top_qp
Definition: cavs.h:191
int8_t rltab[59][3]
Definition: cavs.h:151
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:199
static const uint8_t mv_scan[4]
Definition: cavsdec.c:35
cavs_vector mv[2 *4 *3]
mv motion vector cache 0: D3 B2 B3 C2 4: A1 X0 X1 - 8: A3 X2 X3 -
Definition: cavs.h:205
int width
Definition: cavs.h:178
const char data[16]
Definition: mxf.c:70
#define PIC_I_START_CODE
Definition: cavs.h:36
static void decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
Definition: cavsdec.c:762
uint8_t * data
Definition: avcodec.h:1346
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:182
static int flags
Definition: log.c:50
int8_t golomb_order
Definition: cavs.h:153
bitstream reader API header.
static void cavs_flush(AVCodecContext *avctx)
Definition: cavsdec.c:1116
void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type)
in-loop deblocking filter for a single macroblock
Definition: cavs.c:108
int dist[2]
temporal distances from current frame to ref frames
Definition: cavs.h:173
int mby
Definition: cavs.h:186
GetBitContext gb
Definition: cavs.h:170
#define FFALIGN(x, a)
Definition: macros.h:48
uint8_t * cy
Definition: cavs.h:189
#define r
Definition: input.c:51
#define src
Definition: vp8dsp.c:254
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
uint8_t * cu
Definition: cavs.h:189
Definition: cavs.h:67
int scale_den[2]
for scaling neighbouring MVs
Definition: cavs.h:236
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
Definition: golomb.h:53
static int decode_pic(AVSContext *h)
Definition: cavsdec.c:928
int progressive
Definition: cavs.h:180
AVSFrame cur
currently decoded frame
Definition: cavs.h:171
static const uint16_t mask[17]
Definition: lzw.c:38
int ff_cavs_next_mb(AVSContext *h)
save predictors for later macroblocks and increase macroblock address
Definition: cavs.c:662
#define FF_SIGNBIT(x)
Definition: internal.h:76
Definition: cavs.h:78
#define AVERROR(e)
Definition: error.h:43
void ff_cavs_inter(AVSContext *h, enum cavs_mb mb_type)
Definition: cavs.c:484
CAVSDSPContext cdsp
Definition: cavs.h:169
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
static int decode_slice_header(AVSContext *h, GetBitContext *gb)
Definition: cavsdec.c:877
static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
Definition: cavsdec.c:717
static const uint8_t cbp_tab[64][2]
Definition: cavsdec.c:40
#define C_AVAIL
Definition: cavs.h:41
void ff_cavs_load_intra_pred_luma(AVSContext *h, uint8_t *top, uint8_t **left, int block)
Definition: cavs.c:180
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static av_always_inline av_const double round(double x)
Definition: libm.h:151
#define FFMAX(a, b)
Definition: common.h:64
Definition: cavs.h:65
int skip_mode_flag
select between skip_count or one skip_flag per MB
Definition: cavs.h:182
AVSFrame DPB[2]
reference frames
Definition: cavs.h:172
#define B_AVAIL
Definition: cavs.h:40
#define EOB
Definition: cavsdec.c:82
static const uint16_t dequant_mul[64]
Definition: cavsdec.c:71
#define SYM0
Definition: cavs.h:53
uint8_t * cv
current MB sample pointers
Definition: cavs.h:189
void ff_cavs_load_intra_pred_chroma(AVSContext *h)
Definition: cavs.c:231
static int check_for_slice(AVSContext *h)
Definition: cavsdec.c:900
int stream_revision
0 for samples from 2006, 1 for rm52j encoder
Definition: cavs.h:179
void ff_cavs_init_pic(AVSContext *h)
Definition: cavs.c:707
Definition: cavs.h:116
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
#define NOT_AVAIL
Definition: cavs.h:43
av_cold int ff_cavs_init(AVCodecContext *avctx)
Definition: cavs.c:759
#define FFMIN(a, b)
Definition: common.h:66
AVCodec ff_cavs_decoder
Definition: cavsdec.c:1195
cavs_sub_mb
Definition: cavs.h:76
int16_t dist
Definition: cavs.h:146
uint8_t left_border_u[10]
Definition: cavs.h:225
void(* intra_pred_c[7])(uint8_t *d, uint8_t *top, uint8_t *left, ptrdiff_t stride)
Definition: cavs.h:230
int width
picture width / height.
Definition: avcodec.h:1580
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
const uint8_t ff_cavs_partition_flags[30]
Definition: cavsdata.c:24
int16_t * block
Definition: cavs.h:241
void ff_cavs_init_top_lines(AVSContext *h)
some predictions require data from the top-neighbouring macroblock.
Definition: cavs.c:741
#define ESCAPE_CODE
Definition: cavs.h:47
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:250
const cavs_vector ff_cavs_dir_mv
mark block as "no prediction from this direction" e.g.
Definition: cavsdata.c:59
#define FFABS(a)
Definition: common.h:61
#define EXT_START_CODE
Definition: cavs.h:33
Definition: cavs.h:80
uint8_t left_border_v[10]
Definition: cavs.h:225
int level
Definition: cavs.h:175
void ff_cavs_mv(AVSContext *h, enum cavs_mv_loc nP, enum cavs_mv_loc nC, enum cavs_mv_pred mode, enum cavs_block size, int ref)
Definition: cavs.c:566
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
static void decode_residual_chroma(AVSContext *h)
Definition: cavsdec.c:588
Definition: cavs.h:115
NULL
Definition: eval.c:55
int8_t max_run
Definition: cavs.h:155
static int width
Definition: utils.c:156
static void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw, cavs_vector *col_mv)
Definition: cavsdec.c:470
Libavcodec external API header.
#define BWD0
Definition: cavs.h:51
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
main external API structure.
Definition: avcodec.h:1409
int sym_factor
for scaling in symmetrical B block
Definition: cavs.h:234
#define USER_START_CODE
Definition: cavs.h:34
int pred_mode_Y[3 *3]
luma pred mode cache 0: – B2 B3 3: A1 X0 X1 6: A3 X2 X3
Definition: cavs.h:213
MPEG-1/2 tables.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
cavs_block
Definition: cavs.h:113
Definition: cavs.h:79
int * top_pred_Y
Definition: cavs.h:214
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:292
static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: cavsdec.c:1122
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:259
#define CAVS_START_CODE
Definition: cavs.h:35
static const struct dec_2dvlc intra_dec[7]
Definition: cavsdec.c:84
int16_t ref
Definition: cavs.h:147
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
int8_t level_add[27]
Definition: cavs.h:152
int height
Definition: cavs.h:178
int alpha_offset
Definition: cavs.h:184
Definition: cavs.h:64
static void store_mvs(AVSContext *h)
Definition: cavsdec.c:462
int profile
Definition: cavs.h:175
ptrdiff_t c_stride
Definition: cavs.h:215
#define PIC_PB_START_CODE
Definition: cavs.h:37
int stc
last start code
Definition: cavs.h:188
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:302
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
BlockDSPContext bdsp
Definition: cavs.h:165
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
uint8_t level
Definition: svq3.c:204
const uint8_t * avpriv_find_start_code(const uint8_t *restrict p, const uint8_t *end, uint32_t *restrict state)
Definition: utils.c:2707
int height
Definition: gxfenc.c:72
common internal api header.
int qp_fixed
Definition: cavs.h:218
Definition: cavs.h:158
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1797
static void mv_pred_sym(AVSContext *h, cavs_vector *src, enum cavs_block size)
Definition: cavsdec.c:489
static const uint8_t cavs_chroma_qp[64]
Definition: cavsdec.c:53
Bi-dir predicted.
Definition: avutil.h:262
int flags
availability flags of neighbouring macroblocks
Definition: cavs.h:187
ptrdiff_t l_stride
Definition: cavs.h:215
Definition: cavs.h:63
int luma_scan[4]
Definition: cavs.h:216
static int get_ue_code(GetBitContext *gb, int order)
kth-order exponential golomb code
Definition: cavsdec.c:509
int mb_height
Definition: cavs.h:177
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
void * priv_data
Definition: avcodec.h:1451
int beta_offset
Definition: cavs.h:184
static const struct dec_2dvlc inter_dec[7]
Definition: cavsdec.c:220
static int decode_mb_i(AVSContext *h, int cbp_code)
Definition: cavsdec.c:638
Definition: cavs.h:117
#define FWD0
Definition: cavs.h:49
static void set_mvs(cavs_vector *mv, enum cavs_block size)
Definition: cavs.h:248
Definition: cavs.h:68
int aspect_ratio
Definition: cavs.h:176
static int decode_residual_inter(AVSContext *h)
Definition: cavsdec.c:598
#define FFSWAP(type, a, b)
Definition: common.h:69
static void set_intra_mode_default(AVSContext *h)
Definition: cavsdec.c:706
int16_t y
Definition: cavs.h:145
void(* intra_pred_l[8])(uint8_t *d, uint8_t *top, uint8_t *left, ptrdiff_t stride)
Definition: cavs.h:229
exp golomb vlc stuff
This structure stores compressed data.
Definition: avcodec.h:1323
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1183
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
int mb_width
Definition: cavs.h:177
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838
AVFrame * f
Definition: cavs.h:159
Predicted.
Definition: avutil.h:261
int low_delay
Definition: cavs.h:174
uint8_t * col_type_base
Definition: cavs.h:231
const AVRational ff_mpeg12_frame_rate_tab[]