7c9e9065055affe5784e6470cfbf31008968b70d
[libav.git] / libavcodec / vc1dec.c
1 /*
2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4 * Copyright (c) 2006-2007 Konstantin Shishkov
5 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6 *
7 * This file is part of Libav.
8 *
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * VC-1 and WMV3 decoder
27 */
28
29 #include "internal.h"
30 #include "dsputil.h"
31 #include "avcodec.h"
32 #include "mpegvideo.h"
33 #include "h263.h"
34 #include "vc1.h"
35 #include "vc1data.h"
36 #include "vc1acdata.h"
37 #include "msmpeg4data.h"
38 #include "unary.h"
39 #include "simple_idct.h"
40 #include "mathops.h"
41 #include "vdpau_internal.h"
42
43 #undef NDEBUG
44 #include <assert.h>
45
46 #define MB_INTRA_VLC_BITS 9
47 #define DC_VLC_BITS 9
48 #define AC_VLC_BITS 9
49
50
51 static const uint16_t vlc_offs[] = {
52 0, 520, 552, 616, 1128, 1160, 1224, 1740, 1772, 1836, 1900, 2436,
53 2986, 3050, 3610, 4154, 4218, 4746, 5326, 5390, 5902, 6554, 7658, 8342,
54 9304, 9988, 10630, 11234, 12174, 13006, 13560, 14232, 14786, 15432, 16350, 17522,
55 20372, 21818, 22330, 22394, 23166, 23678, 23742, 24820, 25332, 25396, 26460, 26980,
56 27048, 27592, 27600, 27608, 27616, 27624, 28224, 28258, 28290, 28802, 28834, 28866,
57 29378, 29412, 29444, 29960, 29994, 30026, 30538, 30572, 30604, 31120, 31154, 31186,
58 31714, 31746, 31778, 32306, 32340, 32372
59 };
60
61 // offset tables for interlaced picture MVDATA decoding
62 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
63 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
64
65 /**
66 * Init VC-1 specific tables and VC1Context members
67 * @param v The VC1Context to initialize
68 * @return Status
69 */
70 static int vc1_init_common(VC1Context *v)
71 {
72 static int done = 0;
73 int i = 0;
74 static VLC_TYPE vlc_table[32372][2];
75
76 v->hrd_rate = v->hrd_buffer = NULL;
77
78 /* VLC tables */
79 if (!done) {
80 INIT_VLC_STATIC(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
81 ff_vc1_bfraction_bits, 1, 1,
82 ff_vc1_bfraction_codes, 1, 1, 1 << VC1_BFRACTION_VLC_BITS);
83 INIT_VLC_STATIC(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
84 ff_vc1_norm2_bits, 1, 1,
85 ff_vc1_norm2_codes, 1, 1, 1 << VC1_NORM2_VLC_BITS);
86 INIT_VLC_STATIC(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
87 ff_vc1_norm6_bits, 1, 1,
88 ff_vc1_norm6_codes, 2, 2, 556);
89 INIT_VLC_STATIC(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
90 ff_vc1_imode_bits, 1, 1,
91 ff_vc1_imode_codes, 1, 1, 1 << VC1_IMODE_VLC_BITS);
92 for (i = 0; i < 3; i++) {
93 ff_vc1_ttmb_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 0]];
94 ff_vc1_ttmb_vlc[i].table_allocated = vlc_offs[i * 3 + 1] - vlc_offs[i * 3 + 0];
95 init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
96 ff_vc1_ttmb_bits[i], 1, 1,
97 ff_vc1_ttmb_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
98 ff_vc1_ttblk_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 1]];
99 ff_vc1_ttblk_vlc[i].table_allocated = vlc_offs[i * 3 + 2] - vlc_offs[i * 3 + 1];
100 init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
101 ff_vc1_ttblk_bits[i], 1, 1,
102 ff_vc1_ttblk_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
103 ff_vc1_subblkpat_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 2]];
104 ff_vc1_subblkpat_vlc[i].table_allocated = vlc_offs[i * 3 + 3] - vlc_offs[i * 3 + 2];
105 init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
106 ff_vc1_subblkpat_bits[i], 1, 1,
107 ff_vc1_subblkpat_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
108 }
109 for (i = 0; i < 4; i++) {
110 ff_vc1_4mv_block_pattern_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 9]];
111 ff_vc1_4mv_block_pattern_vlc[i].table_allocated = vlc_offs[i * 3 + 10] - vlc_offs[i * 3 + 9];
112 init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
113 ff_vc1_4mv_block_pattern_bits[i], 1, 1,
114 ff_vc1_4mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
115 ff_vc1_cbpcy_p_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 10]];
116 ff_vc1_cbpcy_p_vlc[i].table_allocated = vlc_offs[i * 3 + 11] - vlc_offs[i * 3 + 10];
117 init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
118 ff_vc1_cbpcy_p_bits[i], 1, 1,
119 ff_vc1_cbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
120 ff_vc1_mv_diff_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 11]];
121 ff_vc1_mv_diff_vlc[i].table_allocated = vlc_offs[i * 3 + 12] - vlc_offs[i * 3 + 11];
122 init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
123 ff_vc1_mv_diff_bits[i], 1, 1,
124 ff_vc1_mv_diff_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
125 }
126 for (i = 0; i < 8; i++) {
127 ff_vc1_ac_coeff_table[i].table = &vlc_table[vlc_offs[i * 2 + 21]];
128 ff_vc1_ac_coeff_table[i].table_allocated = vlc_offs[i * 2 + 22] - vlc_offs[i * 2 + 21];
129 init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
130 &vc1_ac_tables[i][0][1], 8, 4,
131 &vc1_ac_tables[i][0][0], 8, 4, INIT_VLC_USE_NEW_STATIC);
132 /* initialize interlaced MVDATA tables (2-Ref) */
133 ff_vc1_2ref_mvdata_vlc[i].table = &vlc_table[vlc_offs[i * 2 + 22]];
134 ff_vc1_2ref_mvdata_vlc[i].table_allocated = vlc_offs[i * 2 + 23] - vlc_offs[i * 2 + 22];
135 init_vlc(&ff_vc1_2ref_mvdata_vlc[i], VC1_2REF_MVDATA_VLC_BITS, 126,
136 ff_vc1_2ref_mvdata_bits[i], 1, 1,
137 ff_vc1_2ref_mvdata_codes[i], 4, 4, INIT_VLC_USE_NEW_STATIC);
138 }
139 for (i = 0; i < 4; i++) {
140 /* initialize 4MV MBMODE VLC tables for interlaced frame P picture */
141 ff_vc1_intfr_4mv_mbmode_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 37]];
142 ff_vc1_intfr_4mv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 38] - vlc_offs[i * 3 + 37];
143 init_vlc(&ff_vc1_intfr_4mv_mbmode_vlc[i], VC1_INTFR_4MV_MBMODE_VLC_BITS, 15,
144 ff_vc1_intfr_4mv_mbmode_bits[i], 1, 1,
145 ff_vc1_intfr_4mv_mbmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
146 /* initialize NON-4MV MBMODE VLC tables for the same */
147 ff_vc1_intfr_non4mv_mbmode_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 38]];
148 ff_vc1_intfr_non4mv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 39] - vlc_offs[i * 3 + 38];
149 init_vlc(&ff_vc1_intfr_non4mv_mbmode_vlc[i], VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 9,
150 ff_vc1_intfr_non4mv_mbmode_bits[i], 1, 1,
151 ff_vc1_intfr_non4mv_mbmode_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
152 /* initialize interlaced MVDATA tables (1-Ref) */
153 ff_vc1_1ref_mvdata_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 39]];
154 ff_vc1_1ref_mvdata_vlc[i].table_allocated = vlc_offs[i * 3 + 40] - vlc_offs[i * 3 + 39];
155 init_vlc(&ff_vc1_1ref_mvdata_vlc[i], VC1_1REF_MVDATA_VLC_BITS, 72,
156 ff_vc1_1ref_mvdata_bits[i], 1, 1,
157 ff_vc1_1ref_mvdata_codes[i], 4, 4, INIT_VLC_USE_NEW_STATIC);
158 }
159 for (i = 0; i < 4; i++) {
160 /* Initialize 2MV Block pattern VLC tables */
161 ff_vc1_2mv_block_pattern_vlc[i].table = &vlc_table[vlc_offs[i + 49]];
162 ff_vc1_2mv_block_pattern_vlc[i].table_allocated = vlc_offs[i + 50] - vlc_offs[i + 49];
163 init_vlc(&ff_vc1_2mv_block_pattern_vlc[i], VC1_2MV_BLOCK_PATTERN_VLC_BITS, 4,
164 ff_vc1_2mv_block_pattern_bits[i], 1, 1,
165 ff_vc1_2mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
166 }
167 for (i = 0; i < 8; i++) {
168 /* Initialize interlaced CBPCY VLC tables (Table 124 - Table 131) */
169 ff_vc1_icbpcy_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 53]];
170 ff_vc1_icbpcy_vlc[i].table_allocated = vlc_offs[i * 3 + 54] - vlc_offs[i * 3 + 53];
171 init_vlc(&ff_vc1_icbpcy_vlc[i], VC1_ICBPCY_VLC_BITS, 63,
172 ff_vc1_icbpcy_p_bits[i], 1, 1,
173 ff_vc1_icbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
174 /* Initialize interlaced field picture MBMODE VLC tables */
175 ff_vc1_if_mmv_mbmode_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 54]];
176 ff_vc1_if_mmv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 55] - vlc_offs[i * 3 + 54];
177 init_vlc(&ff_vc1_if_mmv_mbmode_vlc[i], VC1_IF_MMV_MBMODE_VLC_BITS, 8,
178 ff_vc1_if_mmv_mbmode_bits[i], 1, 1,
179 ff_vc1_if_mmv_mbmode_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
180 ff_vc1_if_1mv_mbmode_vlc[i].table = &vlc_table[vlc_offs[i * 3 + 55]];
181 ff_vc1_if_1mv_mbmode_vlc[i].table_allocated = vlc_offs[i * 3 + 56] - vlc_offs[i * 3 + 55];
182 init_vlc(&ff_vc1_if_1mv_mbmode_vlc[i], VC1_IF_1MV_MBMODE_VLC_BITS, 6,
183 ff_vc1_if_1mv_mbmode_bits[i], 1, 1,
184 ff_vc1_if_1mv_mbmode_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
185 }
186 done = 1;
187 }
188
189 /* Other defaults */
190 v->pq = -1;
191 v->mvrange = 0; /* 7.1.1.18, p80 */
192
193 return 0;
194 }
195
196 /***********************************************************************/
197 /**
198 * @name VC-1 Bitplane decoding
199 * @see 8.7, p56
200 * @{
201 */
202
203 /**
204 * Imode types
205 * @{
206 */
207 enum Imode {
208 IMODE_RAW,
209 IMODE_NORM2,
210 IMODE_DIFF2,
211 IMODE_NORM6,
212 IMODE_DIFF6,
213 IMODE_ROWSKIP,
214 IMODE_COLSKIP
215 };
216 /** @} */ //imode defines
217
218
219 /** @} */ //Bitplane group
220
221 static void vc1_put_signed_blocks_clamped(VC1Context *v)
222 {
223 MpegEncContext *s = &v->s;
224 int topleft_mb_pos, top_mb_pos;
225 int stride_y, fieldtx;
226 int v_dist;
227
228 /* The put pixels loop is always one MB row behind the decoding loop,
229 * because we can only put pixels when overlap filtering is done, and
230 * for filtering of the bottom edge of a MB, we need the next MB row
231 * present as well.
232 * Within the row, the put pixels loop is also one MB col behind the
233 * decoding loop. The reason for this is again, because for filtering
234 * of the right MB edge, we need the next MB present. */
235 if (!s->first_slice_line) {
236 if (s->mb_x) {
237 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
238 fieldtx = v->fieldtx_plane[topleft_mb_pos];
239 stride_y = s->linesize << fieldtx;
240 v_dist = (16 - fieldtx) >> (fieldtx == 0);
241 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
242 s->dest[0] - 16 * s->linesize - 16,
243 stride_y);
244 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
245 s->dest[0] - 16 * s->linesize - 8,
246 stride_y);
247 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
248 s->dest[0] - v_dist * s->linesize - 16,
249 stride_y);
250 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
251 s->dest[0] - v_dist * s->linesize - 8,
252 stride_y);
253 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
254 s->dest[1] - 8 * s->uvlinesize - 8,
255 s->uvlinesize);
256 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
257 s->dest[2] - 8 * s->uvlinesize - 8,
258 s->uvlinesize);
259 }
260 if (s->mb_x == s->mb_width - 1) {
261 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
262 fieldtx = v->fieldtx_plane[top_mb_pos];
263 stride_y = s->linesize << fieldtx;
264 v_dist = fieldtx ? 15 : 8;
265 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
266 s->dest[0] - 16 * s->linesize,
267 stride_y);
268 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
269 s->dest[0] - 16 * s->linesize + 8,
270 stride_y);
271 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
272 s->dest[0] - v_dist * s->linesize,
273 stride_y);
274 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
275 s->dest[0] - v_dist * s->linesize + 8,
276 stride_y);
277 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
278 s->dest[1] - 8 * s->uvlinesize,
279 s->uvlinesize);
280 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
281 s->dest[2] - 8 * s->uvlinesize,
282 s->uvlinesize);
283 }
284 }
285
286 #define inc_blk_idx(idx) do { \
287 idx++; \
288 if (idx >= v->n_allocated_blks) \
289 idx = 0; \
290 } while (0)
291
292 inc_blk_idx(v->topleft_blk_idx);
293 inc_blk_idx(v->top_blk_idx);
294 inc_blk_idx(v->left_blk_idx);
295 inc_blk_idx(v->cur_blk_idx);
296 }
297
298 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
299 {
300 MpegEncContext *s = &v->s;
301 int j;
302 if (!s->first_slice_line) {
303 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
304 if (s->mb_x)
305 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
306 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
307 for (j = 0; j < 2; j++) {
308 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
309 if (s->mb_x)
310 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
311 }
312 }
313 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
314
315 if (s->mb_y == s->end_mb_y - 1) {
316 if (s->mb_x) {
317 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
318 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
319 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
320 }
321 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
322 }
323 }
324
325 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
326 {
327 MpegEncContext *s = &v->s;
328 int j;
329
330 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
331 * means it runs two rows/cols behind the decoding loop. */
332 if (!s->first_slice_line) {
333 if (s->mb_x) {
334 if (s->mb_y >= s->start_mb_y + 2) {
335 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
336
337 if (s->mb_x >= 2)
338 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
339 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
340 for (j = 0; j < 2; j++) {
341 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
342 if (s->mb_x >= 2) {
343 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
344 }
345 }
346 }
347 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
348 }
349
350 if (s->mb_x == s->mb_width - 1) {
351 if (s->mb_y >= s->start_mb_y + 2) {
352 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
353
354 if (s->mb_x)
355 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
356 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
357 for (j = 0; j < 2; j++) {
358 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
359 if (s->mb_x >= 2) {
360 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
361 }
362 }
363 }
364 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
365 }
366
367 if (s->mb_y == s->end_mb_y) {
368 if (s->mb_x) {
369 if (s->mb_x >= 2)
370 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
371 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
372 if (s->mb_x >= 2) {
373 for (j = 0; j < 2; j++) {
374 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
375 }
376 }
377 }
378
379 if (s->mb_x == s->mb_width - 1) {
380 if (s->mb_x)
381 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
382 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
383 if (s->mb_x) {
384 for (j = 0; j < 2; j++) {
385 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
386 }
387 }
388 }
389 }
390 }
391 }
392
393 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
394 {
395 MpegEncContext *s = &v->s;
396 int mb_pos;
397
398 if (v->condover == CONDOVER_NONE)
399 return;
400
401 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
402
403 /* Within a MB, the horizontal overlap always runs before the vertical.
404 * To accomplish that, we run the H on left and internal borders of the
405 * currently decoded MB. Then, we wait for the next overlap iteration
406 * to do H overlap on the right edge of this MB, before moving over and
407 * running the V overlap. Therefore, the V overlap makes us trail by one
408 * MB col and the H overlap filter makes us trail by one MB row. This
409 * is reflected in the time at which we run the put_pixels loop. */
410 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
411 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
412 v->over_flags_plane[mb_pos - 1])) {
413 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
414 v->block[v->cur_blk_idx][0]);
415 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
416 v->block[v->cur_blk_idx][2]);
417 if (!(s->flags & CODEC_FLAG_GRAY)) {
418 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
419 v->block[v->cur_blk_idx][4]);
420 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
421 v->block[v->cur_blk_idx][5]);
422 }
423 }
424 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
425 v->block[v->cur_blk_idx][1]);
426 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
427 v->block[v->cur_blk_idx][3]);
428
429 if (s->mb_x == s->mb_width - 1) {
430 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
431 v->over_flags_plane[mb_pos - s->mb_stride])) {
432 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
433 v->block[v->cur_blk_idx][0]);
434 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
435 v->block[v->cur_blk_idx][1]);
436 if (!(s->flags & CODEC_FLAG_GRAY)) {
437 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
438 v->block[v->cur_blk_idx][4]);
439 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
440 v->block[v->cur_blk_idx][5]);
441 }
442 }
443 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
444 v->block[v->cur_blk_idx][2]);
445 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
446 v->block[v->cur_blk_idx][3]);
447 }
448 }
449 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
450 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
451 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
452 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
453 v->block[v->left_blk_idx][0]);
454 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
455 v->block[v->left_blk_idx][1]);
456 if (!(s->flags & CODEC_FLAG_GRAY)) {
457 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
458 v->block[v->left_blk_idx][4]);
459 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
460 v->block[v->left_blk_idx][5]);
461 }
462 }
463 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
464 v->block[v->left_blk_idx][2]);
465 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
466 v->block[v->left_blk_idx][3]);
467 }
468 }
469
470 /** Do motion compensation over 1 macroblock
471 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
472 */
473 static void vc1_mc_1mv(VC1Context *v, int dir)
474 {
475 MpegEncContext *s = &v->s;
476 DSPContext *dsp = &v->s.dsp;
477 uint8_t *srcY, *srcU, *srcV;
478 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
479 int off, off_uv;
480 int v_edge_pos = s->v_edge_pos >> v->field_mode;
481 if (!v->field_mode && !v->s.last_picture.f.data[0])
482 return;
483
484 mx = s->mv[dir][0][0];
485 my = s->mv[dir][0][1];
486
487 // store motion vectors for further use in B frames
488 if (s->pict_type == AV_PICTURE_TYPE_P) {
489 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = mx;
490 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = my;
491 }
492
493 uvmx = (mx + ((mx & 3) == 3)) >> 1;
494 uvmy = (my + ((my & 3) == 3)) >> 1;
495 v->luma_mv[s->mb_x][0] = uvmx;
496 v->luma_mv[s->mb_x][1] = uvmy;
497
498 if (v->field_mode &&
499 v->cur_field_type != v->ref_field_type[dir]) {
500 my = my - 2 + 4 * v->cur_field_type;
501 uvmy = uvmy - 2 + 4 * v->cur_field_type;
502 }
503
504 // fastuvmc shall be ignored for interlaced frame picture
505 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
506 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
507 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
508 }
509 if (v->field_mode) { // interlaced field picture
510 if (!dir) {
511 if ((v->cur_field_type != v->ref_field_type[dir]) && v->cur_field_type) {
512 srcY = s->current_picture.f.data[0];
513 srcU = s->current_picture.f.data[1];
514 srcV = s->current_picture.f.data[2];
515 } else {
516 srcY = s->last_picture.f.data[0];
517 srcU = s->last_picture.f.data[1];
518 srcV = s->last_picture.f.data[2];
519 }
520 } else {
521 srcY = s->next_picture.f.data[0];
522 srcU = s->next_picture.f.data[1];
523 srcV = s->next_picture.f.data[2];
524 }
525 } else {
526 if (!dir) {
527 srcY = s->last_picture.f.data[0];
528 srcU = s->last_picture.f.data[1];
529 srcV = s->last_picture.f.data[2];
530 } else {
531 srcY = s->next_picture.f.data[0];
532 srcU = s->next_picture.f.data[1];
533 srcV = s->next_picture.f.data[2];
534 }
535 }
536
537 src_x = s->mb_x * 16 + (mx >> 2);
538 src_y = s->mb_y * 16 + (my >> 2);
539 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
540 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
541
542 if (v->profile != PROFILE_ADVANCED) {
543 src_x = av_clip( src_x, -16, s->mb_width * 16);
544 src_y = av_clip( src_y, -16, s->mb_height * 16);
545 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
546 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
547 } else {
548 src_x = av_clip( src_x, -17, s->avctx->coded_width);
549 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
550 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
551 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
552 }
553
554 srcY += src_y * s->linesize + src_x;
555 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
556 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
557
558 if (v->field_mode && v->ref_field_type[dir]) {
559 srcY += s->current_picture_ptr->f.linesize[0];
560 srcU += s->current_picture_ptr->f.linesize[1];
561 srcV += s->current_picture_ptr->f.linesize[2];
562 }
563
564 /* for grayscale we should not try to read from unknown area */
565 if (s->flags & CODEC_FLAG_GRAY) {
566 srcU = s->edge_emu_buffer + 18 * s->linesize;
567 srcV = s->edge_emu_buffer + 18 * s->linesize;
568 }
569
570 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
571 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
572 || (unsigned)(src_y - s->mspel) > v_edge_pos - (my&3) - 16 - s->mspel * 3) {
573 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
574
575 srcY -= s->mspel * (1 + s->linesize);
576 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
577 17 + s->mspel * 2, 17 + s->mspel * 2,
578 src_x - s->mspel, src_y - s->mspel,
579 s->h_edge_pos, v_edge_pos);
580 srcY = s->edge_emu_buffer;
581 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
582 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
583 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
584 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
585 srcU = uvbuf;
586 srcV = uvbuf + 16;
587 /* if we deal with range reduction we need to scale source blocks */
588 if (v->rangeredfrm) {
589 int i, j;
590 uint8_t *src, *src2;
591
592 src = srcY;
593 for (j = 0; j < 17 + s->mspel * 2; j++) {
594 for (i = 0; i < 17 + s->mspel * 2; i++)
595 src[i] = ((src[i] - 128) >> 1) + 128;
596 src += s->linesize;
597 }
598 src = srcU;
599 src2 = srcV;
600 for (j = 0; j < 9; j++) {
601 for (i = 0; i < 9; i++) {
602 src[i] = ((src[i] - 128) >> 1) + 128;
603 src2[i] = ((src2[i] - 128) >> 1) + 128;
604 }
605 src += s->uvlinesize;
606 src2 += s->uvlinesize;
607 }
608 }
609 /* if we deal with intensity compensation we need to scale source blocks */
610 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
611 int i, j;
612 uint8_t *src, *src2;
613
614 src = srcY;
615 for (j = 0; j < 17 + s->mspel * 2; j++) {
616 for (i = 0; i < 17 + s->mspel * 2; i++)
617 src[i] = v->luty[src[i]];
618 src += s->linesize;
619 }
620 src = srcU;
621 src2 = srcV;
622 for (j = 0; j < 9; j++) {
623 for (i = 0; i < 9; i++) {
624 src[i] = v->lutuv[src[i]];
625 src2[i] = v->lutuv[src2[i]];
626 }
627 src += s->uvlinesize;
628 src2 += s->uvlinesize;
629 }
630 }
631 srcY += s->mspel * (1 + s->linesize);
632 }
633
634 if (v->field_mode && v->cur_field_type) {
635 off = s->current_picture_ptr->f.linesize[0];
636 off_uv = s->current_picture_ptr->f.linesize[1];
637 } else {
638 off = 0;
639 off_uv = 0;
640 }
641 if (s->mspel) {
642 dxy = ((my & 3) << 2) | (mx & 3);
643 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
644 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
645 srcY += s->linesize * 8;
646 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
647 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
648 } else { // hpel mc - always used for luma
649 dxy = (my & 2) | ((mx & 2) >> 1);
650 if (!v->rnd)
651 dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
652 else
653 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
654 }
655
656 if (s->flags & CODEC_FLAG_GRAY) return;
657 /* Chroma MC always uses qpel bilinear */
658 uvmx = (uvmx & 3) << 1;
659 uvmy = (uvmy & 3) << 1;
660 if (!v->rnd) {
661 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
662 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
663 } else {
664 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
665 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
666 }
667 }
668
669 static inline int median4(int a, int b, int c, int d)
670 {
671 if (a < b) {
672 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
673 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
674 } else {
675 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
676 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
677 }
678 }
679
680 /** Do motion compensation for 4-MV macroblock - luminance block
681 */
682 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
683 {
684 MpegEncContext *s = &v->s;
685 DSPContext *dsp = &v->s.dsp;
686 uint8_t *srcY;
687 int dxy, mx, my, src_x, src_y;
688 int off;
689 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
690 int v_edge_pos = s->v_edge_pos >> v->field_mode;
691
692 if (!v->field_mode && !v->s.last_picture.f.data[0])
693 return;
694
695 mx = s->mv[dir][n][0];
696 my = s->mv[dir][n][1];
697
698 if (!dir) {
699 if (v->field_mode) {
700 if ((v->cur_field_type != v->ref_field_type[dir]) && v->cur_field_type)
701 srcY = s->current_picture.f.data[0];
702 else
703 srcY = s->last_picture.f.data[0];
704 } else
705 srcY = s->last_picture.f.data[0];
706 } else
707 srcY = s->next_picture.f.data[0];
708
709 if (v->field_mode) {
710 if (v->cur_field_type != v->ref_field_type[dir])
711 my = my - 2 + 4 * v->cur_field_type;
712 }
713
714 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
715 int same_count = 0, opp_count = 0, k;
716 int chosen_mv[2][4][2], f;
717 int tx, ty;
718 for (k = 0; k < 4; k++) {
719 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
720 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
721 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
722 opp_count += f;
723 same_count += 1 - f;
724 }
725 f = opp_count > same_count;
726 switch (f ? opp_count : same_count) {
727 case 4:
728 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
729 chosen_mv[f][2][0], chosen_mv[f][3][0]);
730 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
731 chosen_mv[f][2][1], chosen_mv[f][3][1]);
732 break;
733 case 3:
734 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
735 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
736 break;
737 case 2:
738 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
739 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
740 break;
741 }
742 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
743 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
744 for (k = 0; k < 4; k++)
745 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
746 }
747
748 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
749 int qx, qy;
750 int width = s->avctx->coded_width;
751 int height = s->avctx->coded_height >> 1;
752 qx = (s->mb_x * 16) + (mx >> 2);
753 qy = (s->mb_y * 8) + (my >> 3);
754
755 if (qx < -17)
756 mx -= 4 * (qx + 17);
757 else if (qx > width)
758 mx -= 4 * (qx - width);
759 if (qy < -18)
760 my -= 8 * (qy + 18);
761 else if (qy > height + 1)
762 my -= 8 * (qy - height - 1);
763 }
764
765 if ((v->fcm == ILACE_FRAME) && fieldmv)
766 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
767 else
768 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
769 if (v->field_mode && v->cur_field_type)
770 off += s->current_picture_ptr->f.linesize[0];
771
772 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
773 if (!fieldmv)
774 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
775 else
776 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
777
778 if (v->profile != PROFILE_ADVANCED) {
779 src_x = av_clip(src_x, -16, s->mb_width * 16);
780 src_y = av_clip(src_y, -16, s->mb_height * 16);
781 } else {
782 src_x = av_clip(src_x, -17, s->avctx->coded_width);
783 if (v->fcm == ILACE_FRAME) {
784 if (src_y & 1)
785 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
786 else
787 src_y = av_clip(src_y, -18, s->avctx->coded_height);
788 } else {
789 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
790 }
791 }
792
793 srcY += src_y * s->linesize + src_x;
794 if (v->field_mode && v->ref_field_type[dir])
795 srcY += s->current_picture_ptr->f.linesize[0];
796
797 if (fieldmv && !(src_y & 1))
798 v_edge_pos--;
799 if (fieldmv && (src_y & 1) && src_y < 4)
800 src_y--;
801 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
802 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
803 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
804 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
805 /* check emulate edge stride and offset */
806 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
807 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
808 src_x - s->mspel, src_y - (s->mspel << fieldmv),
809 s->h_edge_pos, v_edge_pos);
810 srcY = s->edge_emu_buffer;
811 /* if we deal with range reduction we need to scale source blocks */
812 if (v->rangeredfrm) {
813 int i, j;
814 uint8_t *src;
815
816 src = srcY;
817 for (j = 0; j < 9 + s->mspel * 2; j++) {
818 for (i = 0; i < 9 + s->mspel * 2; i++)
819 src[i] = ((src[i] - 128) >> 1) + 128;
820 src += s->linesize << fieldmv;
821 }
822 }
823 /* if we deal with intensity compensation we need to scale source blocks */
824 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
825 int i, j;
826 uint8_t *src;
827
828 src = srcY;
829 for (j = 0; j < 9 + s->mspel * 2; j++) {
830 for (i = 0; i < 9 + s->mspel * 2; i++)
831 src[i] = v->luty[src[i]];
832 src += s->linesize << fieldmv;
833 }
834 }
835 srcY += s->mspel * (1 + (s->linesize << fieldmv));
836 }
837
838 if (s->mspel) {
839 dxy = ((my & 3) << 2) | (mx & 3);
840 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
841 } else { // hpel mc - always used for luma
842 dxy = (my & 2) | ((mx & 2) >> 1);
843 if (!v->rnd)
844 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
845 else
846 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
847 }
848 }
849
850 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
851 {
852 int idx, i;
853 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
854
855 idx = ((a[3] != flag) << 3)
856 | ((a[2] != flag) << 2)
857 | ((a[1] != flag) << 1)
858 | (a[0] != flag);
859 if (!idx) {
860 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
861 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
862 return 4;
863 } else if (count[idx] == 1) {
864 switch (idx) {
865 case 0x1:
866 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
867 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
868 return 3;
869 case 0x2:
870 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
871 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
872 return 3;
873 case 0x4:
874 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
875 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
876 return 3;
877 case 0x8:
878 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
879 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
880 return 3;
881 }
882 } else if (count[idx] == 2) {
883 int t1 = 0, t2 = 0;
884 for (i = 0; i < 3; i++)
885 if (!a[i]) {
886 t1 = i;
887 break;
888 }
889 for (i = t1 + 1; i < 4; i++)
890 if (!a[i]) {
891 t2 = i;
892 break;
893 }
894 *tx = (mvx[t1] + mvx[t2]) / 2;
895 *ty = (mvy[t1] + mvy[t2]) / 2;
896 return 2;
897 } else {
898 return 0;
899 }
900 return -1;
901 }
902
903 /** Do motion compensation for 4-MV macroblock - both chroma blocks
904 */
905 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
906 {
907 MpegEncContext *s = &v->s;
908 DSPContext *dsp = &v->s.dsp;
909 uint8_t *srcU, *srcV;
910 int uvmx, uvmy, uvsrc_x, uvsrc_y;
911 int k, tx = 0, ty = 0;
912 int mvx[4], mvy[4], intra[4], mv_f[4];
913 int valid_count;
914 int chroma_ref_type = v->cur_field_type, off = 0;
915 int v_edge_pos = s->v_edge_pos >> v->field_mode;
916
917 if (!v->field_mode && !v->s.last_picture.f.data[0])
918 return;
919 if (s->flags & CODEC_FLAG_GRAY)
920 return;
921
922 for (k = 0; k < 4; k++) {
923 mvx[k] = s->mv[dir][k][0];
924 mvy[k] = s->mv[dir][k][1];
925 intra[k] = v->mb_type[0][s->block_index[k]];
926 if (v->field_mode)
927 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
928 }
929
930 /* calculate chroma MV vector from four luma MVs */
931 if (!v->field_mode || (v->field_mode && !v->numref)) {
932 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
933 if (!valid_count) {
934 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
935 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
936 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
937 return; //no need to do MC for intra blocks
938 }
939 } else {
940 int dominant = 0;
941 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
942 dominant = 1;
943 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
944 if (dominant)
945 chroma_ref_type = !v->cur_field_type;
946 }
947 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
948 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
949 uvmx = (tx + ((tx & 3) == 3)) >> 1;
950 uvmy = (ty + ((ty & 3) == 3)) >> 1;
951
952 v->luma_mv[s->mb_x][0] = uvmx;
953 v->luma_mv[s->mb_x][1] = uvmy;
954
955 if (v->fastuvmc) {
956 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
957 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
958 }
959 // Field conversion bias
960 if (v->cur_field_type != chroma_ref_type)
961 uvmy += 2 - 4 * chroma_ref_type;
962
963 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
964 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
965
966 if (v->profile != PROFILE_ADVANCED) {
967 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
968 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
969 } else {
970 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
971 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
972 }
973
974 if (!dir) {
975 if (v->field_mode) {
976 if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
977 srcU = s->current_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
978 srcV = s->current_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
979 } else {
980 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
981 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
982 }
983 } else {
984 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
985 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
986 }
987 } else {
988 srcU = s->next_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
989 srcV = s->next_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
990 }
991
992 if (v->field_mode) {
993 if (chroma_ref_type) {
994 srcU += s->current_picture_ptr->f.linesize[1];
995 srcV += s->current_picture_ptr->f.linesize[2];
996 }
997 off = v->cur_field_type ? s->current_picture_ptr->f.linesize[1] : 0;
998 }
999
1000 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1001 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
1002 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
1003 s->dsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
1004 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
1005 s->h_edge_pos >> 1, v_edge_pos >> 1);
1006 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1007 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
1008 s->h_edge_pos >> 1, v_edge_pos >> 1);
1009 srcU = s->edge_emu_buffer;
1010 srcV = s->edge_emu_buffer + 16;
1011
1012 /* if we deal with range reduction we need to scale source blocks */
1013 if (v->rangeredfrm) {
1014 int i, j;
1015 uint8_t *src, *src2;
1016
1017 src = srcU;
1018 src2 = srcV;
1019 for (j = 0; j < 9; j++) {
1020 for (i = 0; i < 9; i++) {
1021 src[i] = ((src[i] - 128) >> 1) + 128;
1022 src2[i] = ((src2[i] - 128) >> 1) + 128;
1023 }
1024 src += s->uvlinesize;
1025 src2 += s->uvlinesize;
1026 }
1027 }
1028 /* if we deal with intensity compensation we need to scale source blocks */
1029 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1030 int i, j;
1031 uint8_t *src, *src2;
1032
1033 src = srcU;
1034 src2 = srcV;
1035 for (j = 0; j < 9; j++) {
1036 for (i = 0; i < 9; i++) {
1037 src[i] = v->lutuv[src[i]];
1038 src2[i] = v->lutuv[src2[i]];
1039 }
1040 src += s->uvlinesize;
1041 src2 += s->uvlinesize;
1042 }
1043 }
1044 }
1045
1046 /* Chroma MC always uses qpel bilinear */
1047 uvmx = (uvmx & 3) << 1;
1048 uvmy = (uvmy & 3) << 1;
1049 if (!v->rnd) {
1050 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
1051 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
1052 } else {
1053 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
1054 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
1055 }
1056 }
1057
1058 /** Do motion compensation for 4-MV field chroma macroblock (both U and V)
1059 */
1060 static void vc1_mc_4mv_chroma4(VC1Context *v)
1061 {
1062 MpegEncContext *s = &v->s;
1063 DSPContext *dsp = &v->s.dsp;
1064 uint8_t *srcU, *srcV;
1065 int uvsrc_x, uvsrc_y;
1066 int uvmx_field[4], uvmy_field[4];
1067 int i, off, tx, ty;
1068 int fieldmv = v->blk_mv_type[s->block_index[0]];
1069 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
1070 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
1071 int v_edge_pos = s->v_edge_pos >> 1;
1072
1073 if (!v->s.last_picture.f.data[0])
1074 return;
1075 if (s->flags & CODEC_FLAG_GRAY)
1076 return;
1077
1078 for (i = 0; i < 4; i++) {
1079 tx = s->mv[0][i][0];
1080 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
1081 ty = s->mv[0][i][1];
1082 if (fieldmv)
1083 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
1084 else
1085 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
1086 }
1087
1088 for (i = 0; i < 4; i++) {
1089 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
1090 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
1091 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
1092 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
1093 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1094 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1095 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
1096 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
1097 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1098 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1099
1100 if (fieldmv && !(uvsrc_y & 1))
1101 v_edge_pos--;
1102 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1103 uvsrc_y--;
1104 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
1105 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1106 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1107 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
1108 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1109 s->h_edge_pos >> 1, v_edge_pos);
1110 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1111 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1112 s->h_edge_pos >> 1, v_edge_pos);
1113 srcU = s->edge_emu_buffer;
1114 srcV = s->edge_emu_buffer + 16;
1115
1116 /* if we deal with intensity compensation we need to scale source blocks */
1117 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1118 int i, j;
1119 uint8_t *src, *src2;
1120
1121 src = srcU;
1122 src2 = srcV;
1123 for (j = 0; j < 5; j++) {
1124 for (i = 0; i < 5; i++) {
1125 src[i] = v->lutuv[src[i]];
1126 src2[i] = v->lutuv[src2[i]];
1127 }
1128 src += s->uvlinesize << 1;
1129 src2 += s->uvlinesize << 1;
1130 }
1131 }
1132 }
1133 if (!v->rnd) {
1134 dsp->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1135 dsp->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1136 } else {
1137 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1138 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1139 }
1140 }
1141 }
1142
1143 /***********************************************************************/
1144 /**
1145 * @name VC-1 Block-level functions
1146 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1147 * @{
1148 */
1149
1150 /**
1151 * @def GET_MQUANT
1152 * @brief Get macroblock-level quantizer scale
1153 */
1154 #define GET_MQUANT() \
1155 if (v->dquantfrm) { \
1156 int edges = 0; \
1157 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1158 if (v->dqbilevel) { \
1159 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1160 } else { \
1161 mqdiff = get_bits(gb, 3); \
1162 if (mqdiff != 7) \
1163 mquant = v->pq + mqdiff; \
1164 else \
1165 mquant = get_bits(gb, 5); \
1166 } \
1167 } \
1168 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1169 edges = 1 << v->dqsbedge; \
1170 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1171 edges = (3 << v->dqsbedge) % 15; \
1172 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1173 edges = 15; \
1174 if ((edges&1) && !s->mb_x) \
1175 mquant = v->altpq; \
1176 if ((edges&2) && s->first_slice_line) \
1177 mquant = v->altpq; \
1178 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1179 mquant = v->altpq; \
1180 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1181 mquant = v->altpq; \
1182 }
1183
1184 /**
1185 * @def GET_MVDATA(_dmv_x, _dmv_y)
1186 * @brief Get MV differentials
1187 * @see MVDATA decoding from 8.3.5.2, p(1)20
1188 * @param _dmv_x Horizontal differential for decoded MV
1189 * @param _dmv_y Vertical differential for decoded MV
1190 */
1191 #define GET_MVDATA(_dmv_x, _dmv_y) \
1192 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1193 VC1_MV_DIFF_VLC_BITS, 2); \
1194 if (index > 36) { \
1195 mb_has_coeffs = 1; \
1196 index -= 37; \
1197 } else \
1198 mb_has_coeffs = 0; \
1199 s->mb_intra = 0; \
1200 if (!index) { \
1201 _dmv_x = _dmv_y = 0; \
1202 } else if (index == 35) { \
1203 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1204 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1205 } else if (index == 36) { \
1206 _dmv_x = 0; \
1207 _dmv_y = 0; \
1208 s->mb_intra = 1; \
1209 } else { \
1210 index1 = index % 6; \
1211 if (!s->quarter_sample && index1 == 5) val = 1; \
1212 else val = 0; \
1213 if (size_table[index1] - val > 0) \
1214 val = get_bits(gb, size_table[index1] - val); \
1215 else val = 0; \
1216 sign = 0 - (val&1); \
1217 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1218 \
1219 index1 = index / 6; \
1220 if (!s->quarter_sample && index1 == 5) val = 1; \
1221 else val = 0; \
1222 if (size_table[index1] - val > 0) \
1223 val = get_bits(gb, size_table[index1] - val); \
1224 else val = 0; \
1225 sign = 0 - (val & 1); \
1226 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1227 }
1228
1229 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1230 int *dmv_y, int *pred_flag)
1231 {
1232 int index, index1;
1233 int extend_x = 0, extend_y = 0;
1234 GetBitContext *gb = &v->s.gb;
1235 int bits, esc;
1236 int val, sign;
1237 const int* offs_tab;
1238
1239 if (v->numref) {
1240 bits = VC1_2REF_MVDATA_VLC_BITS;
1241 esc = 125;
1242 } else {
1243 bits = VC1_1REF_MVDATA_VLC_BITS;
1244 esc = 71;
1245 }
1246 switch (v->dmvrange) {
1247 case 1:
1248 extend_x = 1;
1249 break;
1250 case 2:
1251 extend_y = 1;
1252 break;
1253 case 3:
1254 extend_x = extend_y = 1;
1255 break;
1256 }
1257 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1258 if (index == esc) {
1259 *dmv_x = get_bits(gb, v->k_x);
1260 *dmv_y = get_bits(gb, v->k_y);
1261 if (v->numref) {
1262 *pred_flag = *dmv_y & 1;
1263 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1264 }
1265 }
1266 else {
1267 if (extend_x)
1268 offs_tab = offset_table2;
1269 else
1270 offs_tab = offset_table1;
1271 index1 = (index + 1) % 9;
1272 if (index1 != 0) {
1273 val = get_bits(gb, index1 + extend_x);
1274 sign = 0 -(val & 1);
1275 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1276 } else
1277 *dmv_x = 0;
1278 if (extend_y)
1279 offs_tab = offset_table2;
1280 else
1281 offs_tab = offset_table1;
1282 index1 = (index + 1) / 9;
1283 if (index1 > v->numref) {
1284 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1285 sign = 0 - (val & 1);
1286 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1287 } else
1288 *dmv_y = 0;
1289 if (v->numref)
1290 *pred_flag = index1 & 1;
1291 }
1292 }
1293
1294 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1295 {
1296 int scaledvalue, refdist;
1297 int scalesame1, scalesame2;
1298 int scalezone1_x, zone1offset_x;
1299 int table_index = dir ^ v->second_field;
1300
1301 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1302 refdist = v->refdist;
1303 else
1304 refdist = dir ? v->brfd : v->frfd;
1305 if (refdist > 3)
1306 refdist = 3;
1307 scalesame1 = vc1_field_mvpred_scales[table_index][1][refdist];
1308 scalesame2 = vc1_field_mvpred_scales[table_index][2][refdist];
1309 scalezone1_x = vc1_field_mvpred_scales[table_index][3][refdist];
1310 zone1offset_x = vc1_field_mvpred_scales[table_index][5][refdist];
1311
1312 if (FFABS(n) > 255)
1313 scaledvalue = n;
1314 else {
1315 if (FFABS(n) < scalezone1_x)
1316 scaledvalue = (n * scalesame1) >> 8;
1317 else {
1318 if (n < 0)
1319 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1320 else
1321 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1322 }
1323 }
1324 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1325 }
1326
1327 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1328 {
1329 int scaledvalue, refdist;
1330 int scalesame1, scalesame2;
1331 int scalezone1_y, zone1offset_y;
1332 int table_index = dir ^ v->second_field;
1333
1334 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1335 refdist = v->refdist;
1336 else
1337 refdist = dir ? v->brfd : v->frfd;
1338 if (refdist > 3)
1339 refdist = 3;
1340 scalesame1 = vc1_field_mvpred_scales[table_index][1][refdist];
1341 scalesame2 = vc1_field_mvpred_scales[table_index][2][refdist];
1342 scalezone1_y = vc1_field_mvpred_scales[table_index][4][refdist];
1343 zone1offset_y = vc1_field_mvpred_scales[table_index][6][refdist];
1344
1345 if (FFABS(n) > 63)
1346 scaledvalue = n;
1347 else {
1348 if (FFABS(n) < scalezone1_y)
1349 scaledvalue = (n * scalesame1) >> 8;
1350 else {
1351 if (n < 0)
1352 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1353 else
1354 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1355 }
1356 }
1357
1358 if (v->cur_field_type && !v->ref_field_type[dir])
1359 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1360 else
1361 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1362 }
1363
1364 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1365 {
1366 int scalezone1_x, zone1offset_x;
1367 int scaleopp1, scaleopp2, brfd;
1368 int scaledvalue;
1369
1370 brfd = FFMIN(v->brfd, 3);
1371 scalezone1_x = vc1_b_field_mvpred_scales[3][brfd];
1372 zone1offset_x = vc1_b_field_mvpred_scales[5][brfd];
1373 scaleopp1 = vc1_b_field_mvpred_scales[1][brfd];
1374 scaleopp2 = vc1_b_field_mvpred_scales[2][brfd];
1375
1376 if (FFABS(n) > 255)
1377 scaledvalue = n;
1378 else {
1379 if (FFABS(n) < scalezone1_x)
1380 scaledvalue = (n * scaleopp1) >> 8;
1381 else {
1382 if (n < 0)
1383 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1384 else
1385 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1386 }
1387 }
1388 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1389 }
1390
1391 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1392 {
1393 int scalezone1_y, zone1offset_y;
1394 int scaleopp1, scaleopp2, brfd;
1395 int scaledvalue;
1396
1397 brfd = FFMIN(v->brfd, 3);
1398 scalezone1_y = vc1_b_field_mvpred_scales[4][brfd];
1399 zone1offset_y = vc1_b_field_mvpred_scales[6][brfd];
1400 scaleopp1 = vc1_b_field_mvpred_scales[1][brfd];
1401 scaleopp2 = vc1_b_field_mvpred_scales[2][brfd];
1402
1403 if (FFABS(n) > 63)
1404 scaledvalue = n;
1405 else {
1406 if (FFABS(n) < scalezone1_y)
1407 scaledvalue = (n * scaleopp1) >> 8;
1408 else {
1409 if (n < 0)
1410 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1411 else
1412 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1413 }
1414 }
1415 if (v->cur_field_type && !v->ref_field_type[dir]) {
1416 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1417 } else {
1418 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1419 }
1420 }
1421
1422 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1423 int dim, int dir)
1424 {
1425 int brfd, scalesame;
1426 int hpel = 1 - v->s.quarter_sample;
1427
1428 n >>= hpel;
1429 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1430 if (dim)
1431 n = scaleforsame_y(v, i, n, dir) << hpel;
1432 else
1433 n = scaleforsame_x(v, n, dir) << hpel;
1434 return n;
1435 }
1436 brfd = FFMIN(v->brfd, 3);
1437 scalesame = vc1_b_field_mvpred_scales[0][brfd];
1438
1439 n = (n * scalesame >> 8) << hpel;
1440 return n;
1441 }
1442
1443 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1444 int dim, int dir)
1445 {
1446 int refdist, scaleopp;
1447 int hpel = 1 - v->s.quarter_sample;
1448
1449 n >>= hpel;
1450 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1451 if (dim)
1452 n = scaleforopp_y(v, n, dir) << hpel;
1453 else
1454 n = scaleforopp_x(v, n) << hpel;
1455 return n;
1456 }
1457 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1458 refdist = FFMIN(v->refdist, 3);
1459 else
1460 refdist = dir ? v->brfd : v->frfd;
1461 scaleopp = vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1462
1463 n = (n * scaleopp >> 8) << hpel;
1464 return n;
1465 }
1466
1467 /** Predict and set motion vector
1468 */
1469 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1470 int mv1, int r_x, int r_y, uint8_t* is_intra,
1471 int pred_flag, int dir)
1472 {
1473 MpegEncContext *s = &v->s;
1474 int xy, wrap, off = 0;
1475 int16_t *A, *B, *C;
1476 int px, py;
1477 int sum;
1478 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1479 int opposit, a_f, b_f, c_f;
1480 int16_t field_predA[2];
1481 int16_t field_predB[2];
1482 int16_t field_predC[2];
1483 int a_valid, b_valid, c_valid;
1484 int hybridmv_thresh, y_bias = 0;
1485
1486 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1487 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1488 mixedmv_pic = 1;
1489 else
1490 mixedmv_pic = 0;
1491 /* scale MV difference to be quad-pel */
1492 dmv_x <<= 1 - s->quarter_sample;
1493 dmv_y <<= 1 - s->quarter_sample;
1494
1495 wrap = s->b8_stride;
1496 xy = s->block_index[n];
1497
1498 if (s->mb_intra) {
1499 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1500 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1501 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1502 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
1503 if (mv1) { /* duplicate motion data for 1-MV block */
1504 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1505 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1506 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1507 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1508 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1509 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1510 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1511 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1512 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1513 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1514 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1515 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1516 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1517 }
1518 return;
1519 }
1520
1521 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
1522 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
1523 if (mv1) {
1524 if (v->field_mode && mixedmv_pic)
1525 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1526 else
1527 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1528 } else {
1529 //in 4-MV mode different blocks have different B predictor position
1530 switch (n) {
1531 case 0:
1532 off = (s->mb_x > 0) ? -1 : 1;
1533 break;
1534 case 1:
1535 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1536 break;
1537 case 2:
1538 off = 1;
1539 break;
1540 case 3:
1541 off = -1;
1542 }
1543 }
1544 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1545
1546 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1547 b_valid = a_valid && (s->mb_width > 1);
1548 c_valid = s->mb_x || (n == 1 || n == 3);
1549 if (v->field_mode) {
1550 a_valid = a_valid && !is_intra[xy - wrap];
1551 b_valid = b_valid && !is_intra[xy - wrap + off];
1552 c_valid = c_valid && !is_intra[xy - 1];
1553 }
1554
1555 if (a_valid) {
1556 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1557 num_oppfield += a_f;
1558 num_samefield += 1 - a_f;
1559 field_predA[0] = A[0];
1560 field_predA[1] = A[1];
1561 } else {
1562 field_predA[0] = field_predA[1] = 0;
1563 a_f = 0;
1564 }
1565 if (b_valid) {
1566 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1567 num_oppfield += b_f;
1568 num_samefield += 1 - b_f;
1569 field_predB[0] = B[0];
1570 field_predB[1] = B[1];
1571 } else {
1572 field_predB[0] = field_predB[1] = 0;
1573 b_f = 0;
1574 }
1575 if (c_valid) {
1576 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1577 num_oppfield += c_f;
1578 num_samefield += 1 - c_f;
1579 field_predC[0] = C[0];
1580 field_predC[1] = C[1];
1581 } else {
1582 field_predC[0] = field_predC[1] = 0;
1583 c_f = 0;
1584 }
1585
1586 if (v->field_mode) {
1587 if (num_samefield <= num_oppfield)
1588 opposit = 1 - pred_flag;
1589 else
1590 opposit = pred_flag;
1591 } else
1592 opposit = 0;
1593 if (opposit) {
1594 if (a_valid && !a_f) {
1595 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1596 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1597 }
1598 if (b_valid && !b_f) {
1599 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1600 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1601 }
1602 if (c_valid && !c_f) {
1603 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1604 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1605 }
1606 v->mv_f[dir][xy + v->blocks_off] = 1;
1607 v->ref_field_type[dir] = !v->cur_field_type;
1608 } else {
1609 if (a_valid && a_f) {
1610 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1611 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1612 }
1613 if (b_valid && b_f) {
1614 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1615 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1616 }
1617 if (c_valid && c_f) {
1618 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1619 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1620 }
1621 v->mv_f[dir][xy + v->blocks_off] = 0;
1622 v->ref_field_type[dir] = v->cur_field_type;
1623 }
1624
1625 if (a_valid) {
1626 px = field_predA[0];
1627 py = field_predA[1];
1628 } else if (c_valid) {
1629 px = field_predC[0];
1630 py = field_predC[1];
1631 } else if (b_valid) {
1632 px = field_predB[0];
1633 py = field_predB[1];
1634 } else {
1635 px = 0;
1636 py = 0;
1637 }
1638
1639 if (num_samefield + num_oppfield > 1) {
1640 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1641 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1642 }
1643
1644 /* Pullback MV as specified in 8.3.5.3.4 */
1645 if (!v->field_mode) {
1646 int qx, qy, X, Y;
1647 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1648 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1649 X = (s->mb_width << 6) - 4;
1650 Y = (s->mb_height << 6) - 4;
1651 if (mv1) {
1652 if (qx + px < -60) px = -60 - qx;
1653 if (qy + py < -60) py = -60 - qy;
1654 } else {
1655 if (qx + px < -28) px = -28 - qx;
1656 if (qy + py < -28) py = -28 - qy;
1657 }
1658 if (qx + px > X) px = X - qx;
1659 if (qy + py > Y) py = Y - qy;
1660 }
1661
1662 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1663 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1664 hybridmv_thresh = 32;
1665 if (a_valid && c_valid) {
1666 if (is_intra[xy - wrap])
1667 sum = FFABS(px) + FFABS(py);
1668 else
1669 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1670 if (sum > hybridmv_thresh) {
1671 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1672 px = field_predA[0];
1673 py = field_predA[1];
1674 } else {
1675 px = field_predC[0];
1676 py = field_predC[1];
1677 }
1678 } else {
1679 if (is_intra[xy - 1])
1680 sum = FFABS(px) + FFABS(py);
1681 else
1682 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1683 if (sum > hybridmv_thresh) {
1684 if (get_bits1(&s->gb)) {
1685 px = field_predA[0];
1686 py = field_predA[1];
1687 } else {
1688 px = field_predC[0];
1689 py = field_predC[1];
1690 }
1691 }
1692 }
1693 }
1694 }
1695
1696 if (v->field_mode && !s->quarter_sample) {
1697 r_x <<= 1;
1698 r_y <<= 1;
1699 }
1700 if (v->field_mode && v->numref)
1701 r_y >>= 1;
1702 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1703 y_bias = 1;
1704 /* store MV using signed modulus of MV range defined in 4.11 */
1705 s->mv[dir][n][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1706 s->mv[dir][n][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
1707 if (mv1) { /* duplicate motion data for 1-MV block */
1708 s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1709 s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1710 s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1711 s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1712 s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1713 s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1714 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1715 v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1716 }
1717 }
1718
1719 /** Predict and set motion vector for interlaced frame picture MBs
1720 */
1721 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1722 int mvn, int r_x, int r_y, uint8_t* is_intra)
1723 {
1724 MpegEncContext *s = &v->s;
1725 int xy, wrap, off = 0;
1726 int A[2], B[2], C[2];
1727 int px, py;
1728 int a_valid = 0, b_valid = 0, c_valid = 0;
1729 int field_a, field_b, field_c; // 0: same, 1: opposit
1730 int total_valid, num_samefield, num_oppfield;
1731 int pos_c, pos_b, n_adj;
1732
1733 wrap = s->b8_stride;
1734 xy = s->block_index[n];
1735
1736 if (s->mb_intra) {
1737 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1738 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1739 s->current_picture.f.motion_val[1][xy][0] = 0;
1740 s->current_picture.f.motion_val[1][xy][1] = 0;
1741 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1742 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1743 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1744 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1745 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1746 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1747 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1748 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1749 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1750 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1751 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1752 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1753 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1754 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1755 }
1756 return;
1757 }
1758
1759 off = ((n == 0) || (n == 1)) ? 1 : -1;
1760 /* predict A */
1761 if (s->mb_x || (n == 1) || (n == 3)) {
1762 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1763 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1764 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1765 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1766 a_valid = 1;
1767 } else { // current block has frame mv and cand. has field MV (so average)
1768 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1769 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
1770 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1771 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
1772 a_valid = 1;
1773 }
1774 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1775 a_valid = 0;
1776 A[0] = A[1] = 0;
1777 }
1778 } else
1779 A[0] = A[1] = 0;
1780 /* Predict B and C */
1781 B[0] = B[1] = C[0] = C[1] = 0;
1782 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1783 if (!s->first_slice_line) {
1784 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1785 b_valid = 1;
1786 n_adj = n | 2;
1787 pos_b = s->block_index[n_adj] - 2 * wrap;
1788 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1789 n_adj = (n & 2) | (n & 1);
1790 }
1791 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1792 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1793 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1794 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1795 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1796 }
1797 }
1798 if (s->mb_width > 1) {
1799 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1800 c_valid = 1;
1801 n_adj = 2;
1802 pos_c = s->block_index[2] - 2 * wrap + 2;
1803 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1804 n_adj = n & 2;
1805 }
1806 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1807 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
1808 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1809 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1810 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1811 }
1812 if (s->mb_x == s->mb_width - 1) {
1813 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1814 c_valid = 1;
1815 n_adj = 3;
1816 pos_c = s->block_index[3] - 2 * wrap - 2;
1817 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1818 n_adj = n | 1;
1819 }
1820 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1821 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
1822 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1823 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1824 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1825 }
1826 } else
1827 c_valid = 0;
1828 }
1829 }
1830 }
1831 }
1832 } else {
1833 pos_b = s->block_index[1];
1834 b_valid = 1;
1835 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1836 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1837 pos_c = s->block_index[0];
1838 c_valid = 1;
1839 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1840 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1841 }
1842
1843 total_valid = a_valid + b_valid + c_valid;
1844 // check if predictor A is out of bounds
1845 if (!s->mb_x && !(n == 1 || n == 3)) {
1846 A[0] = A[1] = 0;
1847 }
1848 // check if predictor B is out of bounds
1849 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1850 B[0] = B[1] = C[0] = C[1] = 0;
1851 }
1852 if (!v->blk_mv_type[xy]) {
1853 if (s->mb_width == 1) {
1854 px = B[0];
1855 py = B[1];
1856 } else {
1857 if (total_valid >= 2) {
1858 px = mid_pred(A[0], B[0], C[0]);
1859 py = mid_pred(A[1], B[1], C[1]);
1860 } else if (total_valid) {
1861 if (a_valid) { px = A[0]; py = A[1]; }
1862 if (b_valid) { px = B[0]; py = B[1]; }
1863 if (c_valid) { px = C[0]; py = C[1]; }
1864 } else
1865 px = py = 0;
1866 }
1867 } else {
1868 if (a_valid)
1869 field_a = (A[1] & 4) ? 1 : 0;
1870 else
1871 field_a = 0;
1872 if (b_valid)
1873 field_b = (B[1] & 4) ? 1 : 0;
1874 else
1875 field_b = 0;
1876 if (c_valid)
1877 field_c = (C[1] & 4) ? 1 : 0;
1878 else
1879 field_c = 0;
1880
1881 num_oppfield = field_a + field_b + field_c;
1882 num_samefield = total_valid - num_oppfield;
1883 if (total_valid == 3) {
1884 if ((num_samefield == 3) || (num_oppfield == 3)) {
1885 px = mid_pred(A[0], B[0], C[0]);
1886 py = mid_pred(A[1], B[1], C[1]);
1887 } else if (num_samefield >= num_oppfield) {
1888 /* take one MV from same field set depending on priority
1889 the check for B may not be necessary */
1890 px = !field_a ? A[0] : B[0];
1891 py = !field_a ? A[1] : B[1];
1892 } else {
1893 px = field_a ? A[0] : B[0];
1894 py = field_a ? A[1] : B[1];
1895 }
1896 } else if (total_valid == 2) {
1897 if (num_samefield >= num_oppfield) {
1898 if (!field_a && a_valid) {
1899 px = A[0];
1900 py = A[1];
1901 } else if (!field_b && b_valid) {
1902 px = B[0];
1903 py = B[1];
1904 } else if (c_valid) {
1905 px = C[0];
1906 py = C[1];
1907 } else px = py = 0;
1908 } else {
1909 if (field_a && a_valid) {
1910 px = A[0];
1911 py = A[1];
1912 } else if (field_b && b_valid) {
1913 px = B[0];
1914 py = B[1];
1915 } else if (c_valid) {
1916 px = C[0];
1917 py = C[1];
1918 }
1919 }
1920 } else if (total_valid == 1) {
1921 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1922 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1923 } else
1924 px = py = 0;
1925 }
1926
1927 /* store MV using signed modulus of MV range defined in 4.11 */
1928 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1929 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1930 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1931 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1932 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1933 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1934 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1935 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1936 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1937 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1938 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1939 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1940 s->mv[0][n + 1][0] = s->mv[0][n][0];
1941 s->mv[0][n + 1][1] = s->mv[0][n][1];
1942 }
1943 }
1944
1945 /** Motion compensation for direct or interpolated blocks in B-frames
1946 */
1947 static void vc1_interp_mc(VC1Context *v)
1948 {
1949 MpegEncContext *s = &v->s;
1950 DSPContext *dsp = &v->s.dsp;
1951 uint8_t *srcY, *srcU, *srcV;
1952 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1953 int off, off_uv;
1954 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1955
1956 if (!v->field_mode && !v->s.next_picture.f.data[0])
1957 return;
1958
1959 mx = s->mv[1][0][0];
1960 my = s->mv[1][0][1];
1961 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1962 uvmy = (my + ((my & 3) == 3)) >> 1;
1963 if (v->field_mode) {
1964 if (v->cur_field_type != v->ref_field_type[1])
1965 my = my - 2 + 4 * v->cur_field_type;
1966 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1967 }
1968 if (v->fastuvmc) {
1969 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1970 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1971 }
1972 srcY = s->next_picture.f.data[0];
1973 srcU = s->next_picture.f.data[1];
1974 srcV = s->next_picture.f.data[2];
1975
1976 src_x = s->mb_x * 16 + (mx >> 2);
1977 src_y = s->mb_y * 16 + (my >> 2);
1978 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1979 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1980
1981 if (v->profile != PROFILE_ADVANCED) {
1982 src_x = av_clip( src_x, -16, s->mb_width * 16);
1983 src_y = av_clip( src_y, -16, s->mb_height * 16);
1984 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1985 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1986 } else {
1987 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1988 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1989 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1990 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1991 }
1992
1993 srcY += src_y * s->linesize + src_x;
1994 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1995 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1996
1997 if (v->field_mode && v->ref_field_type[1]) {
1998 srcY += s->current_picture_ptr->f.linesize[0];
1999 srcU += s->current_picture_ptr->f.linesize[1];
2000 srcV += s->current_picture_ptr->f.linesize[2];
2001 }
2002
2003 /* for grayscale we should not try to read from unknown area */
2004 if (s->flags & CODEC_FLAG_GRAY) {
2005 srcU = s->edge_emu_buffer + 18 * s->linesize;
2006 srcV = s->edge_emu_buffer + 18 * s->linesize;
2007 }
2008
2009 if (v->rangeredfrm
2010 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 16 - s->mspel * 3
2011 || (unsigned)(src_y - s->mspel) > v_edge_pos - (my & 3) - 16 - s->mspel * 3) {
2012 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
2013
2014 srcY -= s->mspel * (1 + s->linesize);
2015 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
2016 17 + s->mspel * 2, 17 + s->mspel * 2,
2017 src_x - s->mspel, src_y - s->mspel,
2018 s->h_edge_pos, v_edge_pos);
2019 srcY = s->edge_emu_buffer;
2020 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
2021 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
2022 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
2023 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
2024 srcU = uvbuf;
2025 srcV = uvbuf + 16;
2026 /* if we deal with range reduction we need to scale source blocks */
2027 if (v->rangeredfrm) {
2028 int i, j;
2029 uint8_t *src, *src2;
2030
2031 src = srcY;
2032 for (j = 0; j < 17 + s->mspel * 2; j++) {
2033 for (i = 0; i < 17 + s->mspel * 2; i++)
2034 src[i] = ((src[i] - 128) >> 1) + 128;
2035 src += s->linesize;
2036 }
2037 src = srcU;
2038 src2 = srcV;
2039 for (j = 0; j < 9; j++) {
2040 for (i = 0; i < 9; i++) {
2041 src[i] = ((src[i] - 128) >> 1) + 128;
2042 src2[i] = ((src2[i] - 128) >> 1) + 128;
2043 }
2044 src += s->uvlinesize;
2045 src2 += s->uvlinesize;
2046 }
2047 }
2048 srcY += s->mspel * (1 + s->linesize);
2049 }
2050
2051 if (v->field_mode && v->cur_field_type) {
2052 off = s->current_picture_ptr->f.linesize[0];
2053 off_uv = s->current_picture_ptr->f.linesize[1];
2054 } else {
2055 off = 0;
2056 off_uv = 0;
2057 }
2058
2059 if (s->mspel) {
2060 dxy = ((my & 3) << 2) | (mx & 3);
2061 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2062 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
2063 srcY += s->linesize * 8;
2064 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2065 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
2066 } else { // hpel mc
2067 dxy = (my & 2) | ((mx & 2) >> 1);
2068
2069 if (!v->rnd)
2070 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2071 else
2072 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2073 }
2074
2075 if (s->flags & CODEC_FLAG_GRAY) return;
2076 /* Chroma MC always uses qpel blilinear */
2077 uvmx = (uvmx & 3) << 1;
2078 uvmy = (uvmy & 3) << 1;
2079 if (!v->rnd) {
2080 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2081 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2082 } else {
2083 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2084 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2085 }
2086 }
2087
2088 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2089 {
2090 int n = bfrac;
2091
2092 #if B_FRACTION_DEN==256
2093 if (inv)
2094 n -= 256;
2095 if (!qs)
2096 return 2 * ((value * n + 255) >> 9);
2097 return (value * n + 128) >> 8;
2098 #else
2099 if (inv)
2100 n -= B_FRACTION_DEN;
2101 if (!qs)
2102 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2103 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2104 #endif
2105 }
2106
2107 static av_always_inline int scale_mv_intfi(int value, int bfrac, int inv,
2108 int qs, int qs_last)
2109 {
2110 int n = bfrac;
2111
2112 if (inv)
2113 n -= 256;
2114 n <<= !qs_last;
2115 if (!qs)
2116 return (value * n + 255) >> 9;
2117 else
2118 return (value * n + 128) >> 8;
2119 }
2120
2121 /** Reconstruct motion vector for B-frame and do motion compensation
2122 */
2123 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2124 int direct, int mode)
2125 {
2126 if (v->use_ic) {
2127 v->mv_mode2 = v->mv_mode;
2128 v->mv_mode = MV_PMODE_INTENSITY_COMP;
2129 }
2130 if (direct) {
2131 vc1_mc_1mv(v, 0);
2132 vc1_interp_mc(v);
2133 if (v->use_ic)
2134 v->mv_mode = v->mv_mode2;
2135 return;
2136 }
2137 if (mode == BMV_TYPE_INTERPOLATED) {
2138 vc1_mc_1mv(v, 0);
2139 vc1_interp_mc(v);
2140 if (v->use_ic)
2141 v->mv_mode = v->mv_mode2;
2142 return;
2143 }
2144
2145 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2146 v->mv_mode = v->mv_mode2;
2147 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2148 if (v->use_ic)
2149 v->mv_mode = v->mv_mode2;
2150 }
2151
2152 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2153 int direct, int mvtype)
2154 {
2155 MpegEncContext *s = &v->s;
2156 int xy, wrap, off = 0;
2157 int16_t *A, *B, *C;
2158 int px, py;
2159 int sum;
2160 int r_x, r_y;
2161 const uint8_t *is_intra = v->mb_type[0];
2162
2163 r_x = v->range_x;
2164 r_y = v->range_y;
2165 /* scale MV difference to be quad-pel */
2166 dmv_x[0] <<= 1 - s->quarter_sample;
2167 dmv_y[0] <<= 1 - s->quarter_sample;
2168 dmv_x[1] <<= 1 - s->quarter_sample;
2169 dmv_y[1] <<= 1 - s->quarter_sample;
2170
2171 wrap = s->b8_stride;
2172 xy = s->block_index[0];
2173
2174 if (s->mb_intra) {
2175 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2176 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2177 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2178 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2179 return;
2180 }
2181 if (!v->field_mode) {
2182 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2183 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2184 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2185 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2186
2187 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2188 s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
2189 s->mv[0][0][1] = av_clip(s->mv[0][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
2190 s->mv[1][0][0] = av_clip(s->mv[1][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
2191 s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
2192 }
2193 if (direct) {
2194 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2195 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2196 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2197 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2198 return;
2199 }
2200
2201 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2202 C = s->current_picture.f.motion_val[0][xy - 2];
2203 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2204 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2205 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2206
2207 if (!s->mb_x) C[0] = C[1] = 0;
2208 if (!s->first_slice_line) { // predictor A is not out of bounds
2209 if (s->mb_width == 1) {
2210 px = A[0];
2211 py = A[1];
2212 } else {
2213 px = mid_pred(A[0], B[0], C[0]);
2214 py = mid_pred(A[1], B[1], C[1]);
2215 }
2216 } else if (s->mb_x) { // predictor C is not out of bounds
2217 px = C[0];
2218 py = C[1];
2219 } else {
2220 px = py = 0;
2221 }
2222 /* Pullback MV as specified in 8.3.5.3.4 */
2223 {
2224 int qx, qy, X, Y;
2225 if (v->profile < PROFILE_ADVANCED) {
2226 qx = (s->mb_x << 5);
2227 qy = (s->mb_y << 5);
2228 X = (s->mb_width << 5) - 4;
2229 Y = (s->mb_height << 5) - 4;
2230 if (qx + px < -28) px = -28 - qx;
2231 if (qy + py < -28) py = -28 - qy;
2232 if (qx + px > X) px = X - qx;
2233 if (qy + py > Y) py = Y - qy;
2234 } else {
2235 qx = (s->mb_x << 6);
2236 qy = (s->mb_y << 6);
2237 X = (s->mb_width << 6) - 4;
2238 Y = (s->mb_height << 6) - 4;
2239 if (qx + px < -60) px = -60 - qx;
2240 if (qy + py < -60) py = -60 - qy;
2241 if (qx + px > X) px = X - qx;
2242 if (qy + py > Y) py = Y - qy;
2243 }
2244 }
2245 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2246 if (0 && !s->first_slice_line && s->mb_x) {
2247 if (is_intra[xy - wrap])
2248 sum = FFABS(px) + FFABS(py);
2249 else
2250 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2251 if (sum > 32) {
2252 if (get_bits1(&s->gb)) {
2253 px = A[0];
2254 py = A[1];
2255 } else {
2256 px = C[0];
2257 py = C[1];
2258 }
2259 } else {
2260 if (is_intra[xy - 2])
2261 sum = FFABS(px) + FFABS(py);
2262 else
2263 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2264 if (sum > 32) {
2265 if (get_bits1(&s->gb)) {
2266 px = A[0];
2267 py = A[1];
2268 } else {
2269 px = C[0];
2270 py = C[1];
2271 }
2272 }
2273 }
2274 }
2275 /* store MV using signed modulus of MV range defined in 4.11 */
2276 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2277 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2278 }
2279 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2280 C = s->current_picture.f.motion_val[1][xy - 2];
2281 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2282 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2283 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2284
2285 if (!s->mb_x)
2286 C[0] = C[1] = 0;
2287 if (!s->first_slice_line) { // predictor A is not out of bounds
2288 if (s->mb_width == 1) {
2289 px = A[0];
2290 py = A[1];
2291 } else {
2292 px = mid_pred(A[0], B[0], C[0]);
2293 py = mid_pred(A[1], B[1], C[1]);
2294 }
2295 } else if (s->mb_x) { // predictor C is not out of bounds
2296 px = C[0];
2297 py = C[1];
2298 } else {
2299 px = py = 0;
2300 }
2301 /* Pullback MV as specified in 8.3.5.3.4 */
2302 {
2303 int qx, qy, X, Y;
2304 if (v->profile < PROFILE_ADVANCED) {
2305 qx = (s->mb_x << 5);
2306 qy = (s->mb_y << 5);
2307 X = (s->mb_width << 5) - 4;
2308 Y = (s->mb_height << 5) - 4;
2309 if (qx + px < -28) px = -28 - qx;
2310 if (qy + py < -28) py = -28 - qy;
2311 if (qx + px > X) px = X - qx;
2312 if (qy + py > Y) py = Y - qy;
2313 } else {
2314 qx = (s->mb_x << 6);
2315 qy = (s->mb_y << 6);
2316 X = (s->mb_width << 6) - 4;
2317 Y = (s->mb_height << 6) - 4;
2318 if (qx + px < -60) px = -60 - qx;
2319 if (qy + py < -60) py = -60 - qy;
2320 if (qx + px > X) px = X - qx;
2321 if (qy + py > Y) py = Y - qy;
2322 }
2323 }
2324 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2325 if (0 && !s->first_slice_line && s->mb_x) {
2326 if (is_intra[xy - wrap])
2327 sum = FFABS(px) + FFABS(py);
2328 else
2329 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2330 if (sum > 32) {
2331 if (get_bits1(&s->gb)) {
2332 px = A[0];
2333 py = A[1];
2334 } else {
2335 px = C[0];
2336 py = C[1];
2337 }
2338 } else {
2339 if (is_intra[xy - 2])
2340 sum = FFABS(px) + FFABS(py);
2341 else
2342 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2343 if (sum > 32) {
2344 if (get_bits1(&s->gb)) {
2345 px = A[0];
2346 py = A[1];
2347 } else {
2348 px = C[0];
2349 py = C[1];
2350 }
2351 }
2352 }
2353 }
2354 /* store MV using signed modulus of MV range defined in 4.11 */
2355
2356 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2357 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2358 }
2359 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2360 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2361 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2362 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2363 }
2364
2365 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2366 {
2367 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2368 MpegEncContext *s = &v->s;
2369 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2370
2371 if (v->bmvtype == BMV_TYPE_DIRECT) {
2372 int total_opp, k, f;
2373 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2374 s->mv[0][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2375 v->bfraction, 0, s->quarter_sample, v->qs_last);
2376 s->mv[0][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2377 v->bfraction, 0, s->quarter_sample, v->qs_last);
2378 s->mv[1][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2379 v->bfraction, 1, s->quarter_sample, v->qs_last);
2380 s->mv[1][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2381 v->bfraction, 1, s->quarter_sample, v->qs_last);
2382
2383 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2384 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2385 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2386 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2387 f = (total_opp > 2) ? 1 : 0;
2388 } else {
2389 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2390 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2391 f = 0;
2392 }
2393 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2394 for (k = 0; k < 4; k++) {
2395 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2396 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2397 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2398 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2399 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2400 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2401 }
2402 return;
2403 }
2404 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2405 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
2406 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
2407 return;
2408 }
2409 if (dir) { // backward
2410 vc1_pred_mv(v, n, dmv_x[1], dmv_y[1], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
2411 if (n == 3 || mv1) {
2412 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2413 }
2414 } else { // forward
2415 vc1_pred_mv(v, n, dmv_x[0], dmv_y[0], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
2416 if (n == 3 || mv1) {
2417 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2418 }
2419 }
2420 }
2421
2422 /** Get predicted DC value for I-frames only
2423 * prediction dir: left=0, top=1
2424 * @param s MpegEncContext
2425 * @param overlap flag indicating that overlap filtering is used
2426 * @param pq integer part of picture quantizer
2427 * @param[in] n block index in the current MB
2428 * @param dc_val_ptr Pointer to DC predictor
2429 * @param dir_ptr Prediction direction for use in AC prediction
2430 */
2431 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2432 int16_t **dc_val_ptr, int *dir_ptr)
2433 {
2434 int a, b, c, wrap, pred, scale;
2435 int16_t *dc_val;
2436 static const uint16_t dcpred[32] = {
2437 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2438 114, 102, 93, 85, 79, 73, 68, 64,
2439 60, 57, 54, 51, 49, 47, 45, 43,
2440 41, 39, 38, 37, 35, 34, 33
2441 };
2442
2443 /* find prediction - wmv3_dc_scale always used here in fact */
2444 if (n < 4) scale = s->y_dc_scale;
2445 else scale = s->c_dc_scale;
2446
2447 wrap = s->block_wrap[n];
2448 dc_val = s->dc_val[0] + s->block_index[n];
2449
2450 /* B A
2451 * C X
2452 */
2453 c = dc_val[ - 1];
2454 b = dc_val[ - 1 - wrap];
2455 a = dc_val[ - wrap];
2456
2457 if (pq < 9 || !overlap) {
2458 /* Set outer values */
2459 if (s->first_slice_line && (n != 2 && n != 3))
2460 b = a = dcpred[scale];
2461 if (s->mb_x == 0 && (n != 1 && n != 3))
2462 b = c = dcpred[scale];
2463 } else {
2464 /* Set outer values */
2465 if (s->first_slice_line && (n != 2 && n != 3))
2466 b = a = 0;
2467 if (s->mb_x == 0 && (n != 1 && n != 3))
2468 b = c = 0;
2469 }
2470
2471 if (abs(a - b) <= abs(b - c)) {
2472 pred = c;
2473 *dir_ptr = 1; // left
2474 } else {
2475 pred = a;
2476 *dir_ptr = 0; // top
2477 }
2478
2479 /* update predictor */
2480 *dc_val_ptr = &dc_val[0];
2481 return pred;
2482 }
2483
2484
2485 /** Get predicted DC value
2486 * prediction dir: left=0, top=1
2487 * @param s MpegEncContext
2488 * @param overlap flag indicating that overlap filtering is used
2489 * @param pq integer part of picture quantizer
2490 * @param[in] n block index in the current MB
2491 * @param a_avail flag indicating top block availability
2492 * @param c_avail flag indicating left block availability
2493 * @param dc_val_ptr Pointer to DC predictor
2494 * @param dir_ptr Prediction direction for use in AC prediction
2495 */
2496 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2497 int a_avail, int c_avail,
2498 int16_t **dc_val_ptr, int *dir_ptr)
2499 {
2500 int a, b, c, wrap, pred;
2501 int16_t *dc_val;
2502 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2503 int q1, q2 = 0;
2504
2505 wrap = s->block_wrap[n];
2506 dc_val = s->dc_val[0] + s->block_index[n];
2507
2508 /* B A
2509 * C X
2510 */
2511 c = dc_val[ - 1];
2512 b = dc_val[ - 1 - wrap];
2513 a = dc_val[ - wrap];
2514 /* scale predictors if needed */
2515 q1 = s->current_picture.f.qscale_table[mb_pos];
2516 if (c_avail && (n != 1 && n != 3)) {
2517 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2518 if (q2 && q2 != q1)
2519 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2520 }
2521 if (a_avail && (n != 2 && n != 3)) {
2522 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2523 if (q2 && q2 != q1)
2524 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2525 }
2526 if (a_avail && c_avail && (n != 3)) {
2527 int off = mb_pos;
2528 if (n != 1)
2529 off--;
2530 if (n != 2)
2531 off -= s->mb_stride;
2532 q2 = s->current_picture.f.qscale_table[off];
2533 if (q2 && q2 != q1)
2534 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2535 }
2536
2537 if (a_avail && c_avail) {
2538 if (abs(a - b) <= abs(b - c)) {
2539 pred = c;
2540 *dir_ptr = 1; // left
2541 } else {
2542 pred = a;
2543 *dir_ptr = 0; // top
2544 }
2545 } else if (a_avail) {
2546 pred = a;
2547 *dir_ptr = 0; // top
2548 } else if (c_avail) {
2549 pred = c;
2550 *dir_ptr = 1; // left
2551 } else {
2552 pred = 0;
2553 *dir_ptr = 1; // left
2554 }
2555
2556 /* update predictor */
2557 *dc_val_ptr = &dc_val[0];
2558 return pred;
2559 }
2560
2561 /** @} */ // Block group
2562
2563 /**
2564 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2565 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2566 * @{
2567 */
2568
2569 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2570 uint8_t **coded_block_ptr)
2571 {
2572 int xy, wrap, pred, a, b, c;
2573
2574 xy = s->block_index[n];
2575 wrap = s->b8_stride;
2576
2577 /* B C
2578 * A X
2579 */
2580 a = s->coded_block[xy - 1 ];
2581 b = s->coded_block[xy - 1 - wrap];
2582 c = s->coded_block[xy - wrap];
2583
2584 if (b == c) {
2585 pred = a;
2586 } else {
2587 pred = c;
2588 }
2589
2590 /* store value */
2591 *coded_block_ptr = &s->coded_block[xy];
2592
2593 return pred;
2594 }
2595
2596 /**
2597 * Decode one AC coefficient
2598 * @param v The VC1 context
2599 * @param last Last coefficient
2600 * @param skip How much zero coefficients to skip
2601 * @param value Decoded AC coefficient value
2602 * @param codingset set of VLC to decode data
2603 * @see 8.1.3.4
2604 */
2605 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2606 int *value, int codingset)
2607 {
2608 GetBitContext *gb = &v->s.gb;
2609 int index, escape, run = 0, level = 0, lst = 0;
2610
2611 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2612 if (index != vc1_ac_sizes[codingset] - 1) {
2613 run = vc1_index_decode_table[codingset][index][0];
2614 level = vc1_index_decode_table[codingset][index][1];
2615 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2616 if (get_bits1(gb))
2617 level = -level;
2618 } else {
2619 escape = decode210(gb);
2620 if (escape != 2) {
2621 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2622 run = vc1_index_decode_table[codingset][index][0];
2623 level = vc1_index_decode_table[codingset][index][1];
2624 lst = index >= vc1_last_decode_table[codingset];
2625 if (escape == 0) {
2626 if (lst)
2627 level += vc1_last_delta_level_table[codingset][run];
2628 else
2629 level += vc1_delta_level_table[codingset][run];
2630 } else {
2631 if (lst)
2632 run += vc1_last_delta_run_table[codingset][level] + 1;
2633 else
2634 run += vc1_delta_run_table[codingset][level] + 1;
2635 }
2636 if (get_bits1(gb))
2637 level = -level;
2638 } else {
2639 int sign;
2640 lst = get_bits1(gb);
2641 if (v->s.esc3_level_length == 0) {
2642 if (v->pq < 8 || v->dquantfrm) { // table 59
2643 v->s.esc3_level_length = get_bits(gb, 3);
2644 if (!v->s.esc3_level_length)
2645 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2646 } else { // table 60
2647 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2648 }
2649 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2650 }
2651 run = get_bits(gb, v->s.esc3_run_length);
2652 sign = get_bits1(gb);
2653 level = get_bits(gb, v->s.esc3_level_length);
2654 if (sign)
2655 level = -level;
2656 }
2657 }
2658
2659 *last = lst;
2660 *skip = run;
2661 *value = level;
2662 }
2663
2664 /** Decode intra block in intra frames - should be faster than decode_intra_block
2665 * @param v VC1Context
2666 * @param block block to decode
2667 * @param[in] n subblock index
2668 * @param coded are AC coeffs present or not
2669 * @param codingset set of VLC to decode data
2670 */
2671 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
2672 int coded, int codingset)
2673 {
2674 GetBitContext *gb = &v->s.gb;
2675 MpegEncContext *s = &v->s;
2676 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2677 int i;
2678 int16_t *dc_val;
2679 int16_t *ac_val, *ac_val2;
2680 int dcdiff;
2681
2682 /* Get DC differential */
2683 if (n < 4) {
2684 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2685 } else {
2686 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2687 }
2688 if (dcdiff < 0) {
2689 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2690 return -1;
2691 }
2692 if (dcdiff) {
2693 if (dcdiff == 119 /* ESC index value */) {
2694 /* TODO: Optimize */
2695 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2696 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2697 else dcdiff = get_bits(gb, 8);
2698 } else {
2699 if (v->pq == 1)
2700 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2701 else if (v->pq == 2)
2702 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2703 }
2704 if (get_bits1(gb))
2705 dcdiff = -dcdiff;
2706 }
2707
2708 /* Prediction */
2709 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2710 *dc_val = dcdiff;
2711
2712 /* Store the quantized DC coeff, used for prediction */
2713 if (n < 4) {
2714 block[0] = dcdiff * s->y_dc_scale;
2715 } else {
2716 block[0] = dcdiff * s->c_dc_scale;
2717 }
2718 /* Skip ? */
2719 if (!coded) {
2720 goto not_coded;
2721 }
2722
2723 // AC Decoding
2724 i = 1;
2725
2726 {
2727 int last = 0, skip, value;
2728 const uint8_t *zz_table;
2729 int scale;
2730 int k;
2731
2732 scale = v->pq * 2 + v->halfpq;
2733
2734 if (v->s.ac_pred) {
2735 if (!dc_pred_dir)
2736 zz_table = v->zz_8x8[2];
2737 else
2738 zz_table = v->zz_8x8[3];
2739 } else
2740 zz_table = v->zz_8x8[1];
2741
2742 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2743 ac_val2 = ac_val;
2744 if (dc_pred_dir) // left
2745 ac_val -= 16;
2746 else // top
2747 ac_val -= 16 * s->block_wrap[n];
2748
2749 while (!last) {
2750 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2751 i += skip;
2752 if (i > 63)
2753 break;
2754 block[zz_table[i++]] = value;
2755 }
2756
2757 /* apply AC prediction if needed */
2758 if (s->ac_pred) {
2759 if (dc_pred_dir) { // left
2760 for (k = 1; k < 8; k++)
2761 block[k << v->left_blk_sh] += ac_val[k];
2762 } else { // top
2763 for (k = 1; k < 8; k++)
2764 block[k << v->top_blk_sh] += ac_val[k + 8];
2765 }
2766 }
2767 /* save AC coeffs for further prediction */
2768 for (k = 1; k < 8; k++) {
2769 ac_val2[k] = block[k << v->left_blk_sh];
2770 ac_val2[k + 8] = block[k << v->top_blk_sh];
2771 }
2772
2773 /* scale AC coeffs */
2774 for (k = 1; k < 64; k++)
2775 if (block[k]) {
2776 block[k] *= scale;
2777 if (!v->pquantizer)
2778 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2779 }
2780
2781 if (s->ac_pred) i = 63;
2782 }
2783
2784 not_coded:
2785 if (!coded) {
2786 int k, scale;
2787 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2788 ac_val2 = ac_val;
2789
2790 i = 0;
2791 scale = v->pq * 2 + v->halfpq;
2792 memset(ac_val2, 0, 16 * 2);
2793 if (dc_pred_dir) { // left
2794 ac_val -= 16;
2795 if (s->ac_pred)
2796 memcpy(ac_val2, ac_val, 8 * 2);
2797 } else { // top
2798 ac_val -= 16 * s->block_wrap[n];
2799 if (s->ac_pred)
2800 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2801 }
2802
2803 /* apply AC prediction if needed */
2804 if (s->ac_pred) {
2805 if (dc_pred_dir) { //left
2806 for (k = 1; k < 8; k++) {
2807 block[k << v->left_blk_sh] = ac_val[k] * scale;
2808 if (!v->pquantizer && block[k << v->left_blk_sh])
2809 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2810 }
2811 } else { // top
2812 for (k = 1; k < 8; k++) {
2813 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2814 if (!v->pquantizer && block[k << v->top_blk_sh])
2815 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2816 }
2817 }
2818 i = 63;
2819 }
2820 }
2821 s->block_last_index[n] = i;
2822
2823 return 0;
2824 }
2825
2826 /** Decode intra block in intra frames - should be faster than decode_intra_block
2827 * @param v VC1Context
2828 * @param block block to decode
2829 * @param[in] n subblock number
2830 * @param coded are AC coeffs present or not
2831 * @param codingset set of VLC to decode data
2832 * @param mquant quantizer value for this macroblock
2833 */
2834 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
2835 int coded, int codingset, int mquant)
2836 {
2837 GetBitContext *gb = &v->s.gb;
2838 MpegEncContext *s = &v->s;
2839 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2840 int i;
2841 int16_t *dc_val;
2842 int16_t *ac_val, *ac_val2;
2843 int dcdiff;
2844 int a_avail = v->a_avail, c_avail = v->c_avail;
2845 int use_pred = s->ac_pred;
2846 int scale;
2847 int q1, q2 = 0;
2848 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2849
2850 /* Get DC differential */
2851 if (n < 4) {
2852 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2853 } else {
2854 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2855 }
2856 if (dcdiff < 0) {
2857 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2858 return -1;
2859 }
2860 if (dcdiff) {
2861 if (dcdiff == 119 /* ESC index value */) {
2862 /* TODO: Optimize */
2863 if (mquant == 1) dcdiff = get_bits(gb, 10);
2864 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2865 else dcdiff = get_bits(gb, 8);
2866 } else {
2867 if (mquant == 1)
2868 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2869 else if (mquant == 2)
2870 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2871 }
2872 if (get_bits1(gb))
2873 dcdiff = -dcdiff;
2874 }
2875
2876 /* Prediction */
2877 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2878 *dc_val = dcdiff;
2879
2880 /* Store the quantized DC coeff, used for prediction */
2881 if (n < 4) {
2882 block[0] = dcdiff * s->y_dc_scale;
2883 } else {
2884 block[0] = dcdiff * s->c_dc_scale;
2885 }
2886
2887 //AC Decoding
2888 i = 1;
2889
2890 /* check if AC is needed at all */
2891 if (!a_avail && !c_avail)
2892 use_pred = 0;
2893 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2894 ac_val2 = ac_val;
2895
2896 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2897
2898 if (dc_pred_dir) // left
2899 ac_val -= 16;
2900 else // top
2901 ac_val -= 16 * s->block_wrap[n];
2902
2903 q1 = s->current_picture.f.qscale_table[mb_pos];
2904 if ( dc_pred_dir && c_avail && mb_pos)
2905 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2906 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2907 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2908 if ( dc_pred_dir && n == 1)
2909 q2 = q1;
2910 if (!dc_pred_dir && n == 2)
2911 q2 = q1;
2912 if (n == 3)
2913 q2 = q1;
2914
2915 if (coded) {
2916 int last = 0, skip, value;
2917 const uint8_t *zz_table;
2918 int k;
2919
2920 if (v->s.ac_pred) {
2921 if (!use_pred && v->fcm == ILACE_FRAME) {
2922 zz_table = v->zzi_8x8;
2923 } else {
2924 if (!dc_pred_dir) // top
2925 zz_table = v->zz_8x8[2];
2926 else // left
2927 zz_table = v->zz_8x8[3];
2928 }
2929 } else {
2930 if (v->fcm != ILACE_FRAME)
2931 zz_table = v->zz_8x8[1];
2932 else
2933 zz_table = v->zzi_8x8;
2934 }
2935
2936 while (!last) {
2937 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2938 i += skip;
2939 if (i > 63)
2940 break;
2941 block[zz_table[i++]] = value;
2942 }
2943
2944 /* apply AC prediction if needed */
2945 if (use_pred) {
2946 /* scale predictors if needed*/
2947 if (q2 && q1 != q2) {
2948 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2949 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2950
2951 if (dc_pred_dir) { // left
2952 for (k = 1; k < 8; k++)
2953 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2954 } else { // top
2955 for (k = 1; k < 8; k++)
2956 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1