vc1dec: Partial rewrite of vc1_pred_mv()
[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 if (v->fastuvmc && (v->fcm != 1)) { // fastuvmc shall be ignored for interlaced frame picture
505 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
506 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
507 }
508 if (v->field_mode) { // interlaced field picture
509 if (!dir) {
510 if ((v->cur_field_type != v->ref_field_type[dir]) && v->cur_field_type) {
511 srcY = s->current_picture.f.data[0];
512 srcU = s->current_picture.f.data[1];
513 srcV = s->current_picture.f.data[2];
514 } else {
515 srcY = s->last_picture.f.data[0];
516 srcU = s->last_picture.f.data[1];
517 srcV = s->last_picture.f.data[2];
518 }
519 } else {
520 srcY = s->next_picture.f.data[0];
521 srcU = s->next_picture.f.data[1];
522 srcV = s->next_picture.f.data[2];
523 }
524 } else {
525 if (!dir) {
526 srcY = s->last_picture.f.data[0];
527 srcU = s->last_picture.f.data[1];
528 srcV = s->last_picture.f.data[2];
529 } else {
530 srcY = s->next_picture.f.data[0];
531 srcU = s->next_picture.f.data[1];
532 srcV = s->next_picture.f.data[2];
533 }
534 }
535
536 src_x = s->mb_x * 16 + (mx >> 2);
537 src_y = s->mb_y * 16 + (my >> 2);
538 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
539 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
540
541 if (v->profile != PROFILE_ADVANCED) {
542 src_x = av_clip( src_x, -16, s->mb_width * 16);
543 src_y = av_clip( src_y, -16, s->mb_height * 16);
544 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
545 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
546 } else {
547 src_x = av_clip( src_x, -17, s->avctx->coded_width);
548 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
549 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
550 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
551 }
552
553 srcY += src_y * s->linesize + src_x;
554 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
555 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
556
557 if (v->field_mode && v->ref_field_type[dir]) {
558 srcY += s->current_picture_ptr->f.linesize[0];
559 srcU += s->current_picture_ptr->f.linesize[1];
560 srcV += s->current_picture_ptr->f.linesize[2];
561 }
562
563 /* for grayscale we should not try to read from unknown area */
564 if (s->flags & CODEC_FLAG_GRAY) {
565 srcU = s->edge_emu_buffer + 18 * s->linesize;
566 srcV = s->edge_emu_buffer + 18 * s->linesize;
567 }
568
569 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
570 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
571 || (unsigned)(src_y - s->mspel) > v_edge_pos - (my&3) - 16 - s->mspel * 3) {
572 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
573
574 srcY -= s->mspel * (1 + s->linesize);
575 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
576 17 + s->mspel * 2, 17 + s->mspel * 2,
577 src_x - s->mspel, src_y - s->mspel,
578 s->h_edge_pos, v_edge_pos);
579 srcY = s->edge_emu_buffer;
580 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
581 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
582 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
583 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
584 srcU = uvbuf;
585 srcV = uvbuf + 16;
586 /* if we deal with range reduction we need to scale source blocks */
587 if (v->rangeredfrm) {
588 int i, j;
589 uint8_t *src, *src2;
590
591 src = srcY;
592 for (j = 0; j < 17 + s->mspel * 2; j++) {
593 for (i = 0; i < 17 + s->mspel * 2; i++)
594 src[i] = ((src[i] - 128) >> 1) + 128;
595 src += s->linesize;
596 }
597 src = srcU;
598 src2 = srcV;
599 for (j = 0; j < 9; j++) {
600 for (i = 0; i < 9; i++) {
601 src[i] = ((src[i] - 128) >> 1) + 128;
602 src2[i] = ((src2[i] - 128) >> 1) + 128;
603 }
604 src += s->uvlinesize;
605 src2 += s->uvlinesize;
606 }
607 }
608 /* if we deal with intensity compensation we need to scale source blocks */
609 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
610 int i, j;
611 uint8_t *src, *src2;
612
613 src = srcY;
614 for (j = 0; j < 17 + s->mspel * 2; j++) {
615 for (i = 0; i < 17 + s->mspel * 2; i++)
616 src[i] = v->luty[src[i]];
617 src += s->linesize;
618 }
619 src = srcU;
620 src2 = srcV;
621 for (j = 0; j < 9; j++) {
622 for (i = 0; i < 9; i++) {
623 src[i] = v->lutuv[src[i]];
624 src2[i] = v->lutuv[src2[i]];
625 }
626 src += s->uvlinesize;
627 src2 += s->uvlinesize;
628 }
629 }
630 srcY += s->mspel * (1 + s->linesize);
631 }
632
633 if (v->field_mode && v->cur_field_type) {
634 off = s->current_picture_ptr->f.linesize[0];
635 off_uv = s->current_picture_ptr->f.linesize[1];
636 } else {
637 off = 0;
638 off_uv = 0;
639 }
640 if (s->mspel) {
641 dxy = ((my & 3) << 2) | (mx & 3);
642 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
643 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
644 srcY += s->linesize * 8;
645 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
646 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
647 } else { // hpel mc - always used for luma
648 dxy = (my & 2) | ((mx & 2) >> 1);
649 if (!v->rnd)
650 dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
651 else
652 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
653 }
654
655 if (s->flags & CODEC_FLAG_GRAY) return;
656 /* Chroma MC always uses qpel bilinear */
657 uvmx = (uvmx & 3) << 1;
658 uvmy = (uvmy & 3) << 1;
659 if (!v->rnd) {
660 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
661 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
662 } else {
663 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
664 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
665 }
666 }
667
668 static inline int median4(int a, int b, int c, int d)
669 {
670 if (a < b) {
671 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
672 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
673 } else {
674 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
675 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
676 }
677 }
678
679 /** Do motion compensation for 4-MV macroblock - luminance block
680 */
681 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
682 {
683 MpegEncContext *s = &v->s;
684 DSPContext *dsp = &v->s.dsp;
685 uint8_t *srcY;
686 int dxy, mx, my, src_x, src_y;
687 int off;
688 int fieldmv = (v->fcm == 1) ? v->blk_mv_type[s->block_index[n]] : 0;
689 int v_edge_pos = s->v_edge_pos >> v->field_mode;
690
691 if (!v->field_mode && !v->s.last_picture.f.data[0])
692 return;
693
694 mx = s->mv[dir][n][0];
695 my = s->mv[dir][n][1];
696
697 if (!dir) {
698 if (v->field_mode) {
699 if ((v->cur_field_type != v->ref_field_type[dir]) && v->cur_field_type)
700 srcY = s->current_picture.f.data[0];
701 else
702 srcY = s->last_picture.f.data[0];
703 } else
704 srcY = s->last_picture.f.data[0];
705 } else
706 srcY = s->next_picture.f.data[0];
707
708 if (v->field_mode) {
709 if (v->cur_field_type != v->ref_field_type[dir])
710 my = my - 2 + 4 * v->cur_field_type;
711 }
712
713 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
714 int same_count = 0, opp_count = 0, k;
715 int chosen_mv[2][4][2], f;
716 int tx, ty;
717 for (k = 0; k < 4; k++) {
718 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
719 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
720 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
721 opp_count += f;
722 same_count += 1 - f;
723 }
724 f = opp_count > same_count;
725 switch (f ? opp_count : same_count) {
726 case 4:
727 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
728 chosen_mv[f][2][0], chosen_mv[f][3][0]);
729 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
730 chosen_mv[f][2][1], chosen_mv[f][3][1]);
731 break;
732 case 3:
733 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
734 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
735 break;
736 case 2:
737 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
738 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
739 break;
740 }
741 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
742 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
743 for (k = 0; k < 4; k++)
744 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
745 }
746
747 if (v->fcm == 1) { // not sure if needed for other types of picture
748 int qx, qy;
749 int width = s->avctx->coded_width;
750 int height = s->avctx->coded_height >> 1;
751 qx = (s->mb_x * 16) + (mx >> 2);
752 qy = (s->mb_y * 8) + (my >> 3);
753
754 if (qx < -17)
755 mx -= 4 * (qx + 17);
756 else if (qx > width)
757 mx -= 4 * (qx - width);
758 if (qy < -18)
759 my -= 8 * (qy + 18);
760 else if (qy > height + 1)
761 my -= 8 * (qy - height - 1);
762 }
763
764 if ((v->fcm == 1) && fieldmv)
765 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
766 else
767 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
768 if (v->field_mode && v->cur_field_type)
769 off += s->current_picture_ptr->f.linesize[0];
770
771 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
772 if (!fieldmv)
773 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
774 else
775 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
776
777 if (v->profile != PROFILE_ADVANCED) {
778 src_x = av_clip(src_x, -16, s->mb_width * 16);
779 src_y = av_clip(src_y, -16, s->mb_height * 16);
780 } else {
781 src_x = av_clip(src_x, -17, s->avctx->coded_width);
782 if (v->fcm == 1) {
783 if (src_y & 1)
784 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
785 else
786 src_y = av_clip(src_y, -18, s->avctx->coded_height);
787 } else {
788 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
789 }
790 }
791
792 srcY += src_y * s->linesize + src_x;
793 if (v->field_mode && v->ref_field_type[dir])
794 srcY += s->current_picture_ptr->f.linesize[0];
795
796 if (fieldmv && !(src_y & 1))
797 v_edge_pos--;
798 if (fieldmv && (src_y & 1) && src_y < 4)
799 src_y--;
800 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
801 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
802 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
803 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
804 /* check emulate edge stride and offset */
805 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
806 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
807 src_x - s->mspel, src_y - (s->mspel << fieldmv),
808 s->h_edge_pos, v_edge_pos);
809 srcY = s->edge_emu_buffer;
810 /* if we deal with range reduction we need to scale source blocks */
811 if (v->rangeredfrm) {
812 int i, j;
813 uint8_t *src;
814
815 src = srcY;
816 for (j = 0; j < 9 + s->mspel * 2; j++) {
817 for (i = 0; i < 9 + s->mspel * 2; i++)
818 src[i] = ((src[i] - 128) >> 1) + 128;
819 src += s->linesize << fieldmv;
820 }
821 }
822 /* if we deal with intensity compensation we need to scale source blocks */
823 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
824 int i, j;
825 uint8_t *src;
826
827 src = srcY;
828 for (j = 0; j < 9 + s->mspel * 2; j++) {
829 for (i = 0; i < 9 + s->mspel * 2; i++)
830 src[i] = v->luty[src[i]];
831 src += s->linesize << fieldmv;
832 }
833 }
834 srcY += s->mspel * (1 + (s->linesize << fieldmv));
835 }
836
837 if (s->mspel) {
838 dxy = ((my & 3) << 2) | (mx & 3);
839 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
840 } else { // hpel mc - always used for luma
841 dxy = (my & 2) | ((mx & 2) >> 1);
842 if (!v->rnd)
843 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
844 else
845 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
846 }
847 }
848
849 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
850 {
851 int idx, i;
852 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
853
854 idx = ((a[3] != flag) << 3)
855 | ((a[2] != flag) << 2)
856 | ((a[1] != flag) << 1)
857 | (a[0] != flag);
858 if (!idx) {
859 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
860 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
861 return 4;
862 } else if (count[idx] == 1) {
863 switch (idx) {
864 case 0x1:
865 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
866 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
867 return 3;
868 case 0x2:
869 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
870 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
871 return 3;
872 case 0x4:
873 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
874 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
875 return 3;
876 case 0x8:
877 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
878 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
879 return 3;
880 }
881 } else if (count[idx] == 2) {
882 int t1 = 0, t2 = 0;
883 for (i = 0; i < 3; i++)
884 if (!a[i]) {
885 t1 = i;
886 break;
887 }
888 for (i = t1 + 1; i < 4; i++)
889 if (!a[i]) {
890 t2 = i;
891 break;
892 }
893 *tx = (mvx[t1] + mvx[t2]) / 2;
894 *ty = (mvy[t1] + mvy[t2]) / 2;
895 return 2;
896 } else {
897 return 0;
898 }
899 return -1;
900 }
901
902 /** Do motion compensation for 4-MV macroblock - both chroma blocks
903 */
904 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
905 {
906 MpegEncContext *s = &v->s;
907 DSPContext *dsp = &v->s.dsp;
908 uint8_t *srcU, *srcV;
909 int uvmx, uvmy, uvsrc_x, uvsrc_y;
910 int k, tx = 0, ty = 0;
911 int mvx[4], mvy[4], intra[4], mv_f[4];
912 int valid_count;
913 int chroma_ref_type = v->cur_field_type, off = 0;
914 int v_edge_pos = s->v_edge_pos >> v->field_mode;
915
916 if (!v->field_mode && !v->s.last_picture.f.data[0])
917 return;
918 if (s->flags & CODEC_FLAG_GRAY)
919 return;
920
921 for (k = 0; k < 4; k++) {
922 mvx[k] = s->mv[dir][k][0];
923 mvy[k] = s->mv[dir][k][1];
924 intra[k] = v->mb_type[0][s->block_index[k]];
925 if (v->field_mode)
926 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
927 }
928
929 /* calculate chroma MV vector from four luma MVs */
930 if (!v->field_mode || (v->field_mode && !v->numref)) {
931 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
932 if (!valid_count) {
933 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
934 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
935 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
936 return; //no need to do MC for intra blocks
937 }
938 } else {
939 int dominant = 0;
940 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
941 dominant = 1;
942 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
943 if (dominant)
944 chroma_ref_type = !v->cur_field_type;
945 }
946 s->current_picture.f.motion_val[1][s->block_index[0]][0] = tx;
947 s->current_picture.f.motion_val[1][s->block_index[0]][1] = ty;
948 uvmx = (tx + ((tx & 3) == 3)) >> 1;
949 uvmy = (ty + ((ty & 3) == 3)) >> 1;
950
951 v->luma_mv[s->mb_x][0] = uvmx;
952 v->luma_mv[s->mb_x][1] = uvmy;
953
954 if (v->fastuvmc) {
955 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
956 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
957 }
958 // Field conversion bias
959 if (v->cur_field_type != chroma_ref_type)
960 uvmy += 2 - 4 * chroma_ref_type;
961
962 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
963 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
964
965 if (v->profile != PROFILE_ADVANCED) {
966 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
967 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
968 } else {
969 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
970 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
971 }
972
973 if (!dir) {
974 if (v->field_mode) {
975 if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
976 srcU = s->current_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
977 srcV = s->current_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
978 } else {
979 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
980 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
981 }
982 } else {
983 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
984 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
985 }
986 } else {
987 srcU = s->next_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
988 srcV = s->next_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
989 }
990
991 if (v->field_mode) {
992 if (chroma_ref_type) {
993 srcU += s->current_picture_ptr->f.linesize[1];
994 srcV += s->current_picture_ptr->f.linesize[2];
995 }
996 off = v->cur_field_type ? s->current_picture_ptr->f.linesize[1] : 0;
997 }
998
999 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1000 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
1001 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
1002 s->dsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
1003 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
1004 s->h_edge_pos >> 1, v_edge_pos >> 1);
1005 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1006 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
1007 s->h_edge_pos >> 1, v_edge_pos >> 1);
1008 srcU = s->edge_emu_buffer;
1009 srcV = s->edge_emu_buffer + 16;
1010
1011 /* if we deal with range reduction we need to scale source blocks */
1012 if (v->rangeredfrm) {
1013 int i, j;
1014 uint8_t *src, *src2;
1015
1016 src = srcU;
1017 src2 = srcV;
1018 for (j = 0; j < 9; j++) {
1019 for (i = 0; i < 9; i++) {
1020 src[i] = ((src[i] - 128) >> 1) + 128;
1021 src2[i] = ((src2[i] - 128) >> 1) + 128;
1022 }
1023 src += s->uvlinesize;
1024 src2 += s->uvlinesize;
1025 }
1026 }
1027 /* if we deal with intensity compensation we need to scale source blocks */
1028 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1029 int i, j;
1030 uint8_t *src, *src2;
1031
1032 src = srcU;
1033 src2 = srcV;
1034 for (j = 0; j < 9; j++) {
1035 for (i = 0; i < 9; i++) {
1036 src[i] = v->lutuv[src[i]];
1037 src2[i] = v->lutuv[src2[i]];
1038 }
1039 src += s->uvlinesize;
1040 src2 += s->uvlinesize;
1041 }
1042 }
1043 }
1044
1045 /* Chroma MC always uses qpel bilinear */
1046 uvmx = (uvmx & 3) << 1;
1047 uvmy = (uvmy & 3) << 1;
1048 if (!v->rnd) {
1049 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
1050 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
1051 } else {
1052 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
1053 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
1054 }
1055 }
1056
1057 /** Do motion compensation for 4-MV field chroma macroblock (both U and V)
1058 */
1059 static void vc1_mc_4mv_chroma4(VC1Context *v)
1060 {
1061 MpegEncContext *s = &v->s;
1062 DSPContext *dsp = &v->s.dsp;
1063 uint8_t *srcU, *srcV;
1064 int uvsrc_x, uvsrc_y;
1065 int uvmx_field[4], uvmy_field[4];
1066 int i, off, tx, ty;
1067 int fieldmv = v->blk_mv_type[s->block_index[0]];
1068 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
1069 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
1070 int v_edge_pos = s->v_edge_pos >> 1;
1071
1072 if (!v->s.last_picture.f.data[0])
1073 return;
1074 if (s->flags & CODEC_FLAG_GRAY)
1075 return;
1076
1077 for (i = 0; i < 4; i++) {
1078 tx = s->mv[0][i][0];
1079 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
1080 ty = s->mv[0][i][1];
1081 if (fieldmv)
1082 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
1083 else
1084 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
1085 }
1086
1087 for (i = 0; i < 4; i++) {
1088 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
1089 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
1090 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
1091 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
1092 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1093 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1094 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
1095 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
1096 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1097 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1098
1099 if (fieldmv && !(uvsrc_y & 1))
1100 v_edge_pos--;
1101 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1102 uvsrc_y--;
1103 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
1104 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1105 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1106 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
1107 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1108 s->h_edge_pos >> 1, v_edge_pos);
1109 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1110 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1111 s->h_edge_pos >> 1, v_edge_pos);
1112 srcU = s->edge_emu_buffer;
1113 srcV = s->edge_emu_buffer + 16;
1114
1115 /* if we deal with intensity compensation we need to scale source blocks */
1116 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1117 int i, j;
1118 uint8_t *src, *src2;
1119
1120 src = srcU;
1121 src2 = srcV;
1122 for (j = 0; j < 5; j++) {
1123 for (i = 0; i < 5; i++) {
1124 src[i] = v->lutuv[src[i]];
1125 src2[i] = v->lutuv[src2[i]];
1126 }
1127 src += s->uvlinesize << 1;
1128 src2 += s->uvlinesize << 1;
1129 }
1130 }
1131 }
1132 if (!v->rnd) {
1133 dsp->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1134 dsp->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1135 } else {
1136 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]);
1137 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]);
1138 }
1139 }
1140 }
1141
1142 /***********************************************************************/
1143 /**
1144 * @name VC-1 Block-level functions
1145 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1146 * @{
1147 */
1148
1149 /**
1150 * @def GET_MQUANT
1151 * @brief Get macroblock-level quantizer scale
1152 */
1153 #define GET_MQUANT() \
1154 if (v->dquantfrm) { \
1155 int edges = 0; \
1156 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1157 if (v->dqbilevel) { \
1158 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1159 } else { \
1160 mqdiff = get_bits(gb, 3); \
1161 if (mqdiff != 7) \
1162 mquant = v->pq + mqdiff; \
1163 else \
1164 mquant = get_bits(gb, 5); \
1165 } \
1166 } \
1167 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1168 edges = 1 << v->dqsbedge; \
1169 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1170 edges = (3 << v->dqsbedge) % 15; \
1171 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1172 edges = 15; \
1173 if ((edges&1) && !s->mb_x) \
1174 mquant = v->altpq; \
1175 if ((edges&2) && s->first_slice_line) \
1176 mquant = v->altpq; \
1177 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1178 mquant = v->altpq; \
1179 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1180 mquant = v->altpq; \
1181 }
1182
1183 /**
1184 * @def GET_MVDATA(_dmv_x, _dmv_y)
1185 * @brief Get MV differentials
1186 * @see MVDATA decoding from 8.3.5.2, p(1)20
1187 * @param _dmv_x Horizontal differential for decoded MV
1188 * @param _dmv_y Vertical differential for decoded MV
1189 */
1190 #define GET_MVDATA(_dmv_x, _dmv_y) \
1191 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1192 VC1_MV_DIFF_VLC_BITS, 2); \
1193 if (index > 36) { \
1194 mb_has_coeffs = 1; \
1195 index -= 37; \
1196 } else \
1197 mb_has_coeffs = 0; \
1198 s->mb_intra = 0; \
1199 if (!index) { \
1200 _dmv_x = _dmv_y = 0; \
1201 } else if (index == 35) { \
1202 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1203 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1204 } else if (index == 36) { \
1205 _dmv_x = 0; \
1206 _dmv_y = 0; \
1207 s->mb_intra = 1; \
1208 } else { \
1209 index1 = index % 6; \
1210 if (!s->quarter_sample && index1 == 5) val = 1; \
1211 else val = 0; \
1212 if (size_table[index1] - val > 0) \
1213 val = get_bits(gb, size_table[index1] - val); \
1214 else val = 0; \
1215 sign = 0 - (val&1); \
1216 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1217 \
1218 index1 = index / 6; \
1219 if (!s->quarter_sample && index1 == 5) val = 1; \
1220 else val = 0; \
1221 if (size_table[index1] - val > 0) \
1222 val = get_bits(gb, size_table[index1] - val); \
1223 else val = 0; \
1224 sign = 0 - (val & 1); \
1225 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1226 }
1227
1228 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1229 int *dmv_y, int *pred_flag)
1230 {
1231 int index, index1;
1232 int extend_x = 0, extend_y = 0;
1233 GetBitContext *gb = &v->s.gb;
1234 int bits, esc;
1235 int val, sign;
1236 const int* offs_tab;
1237
1238 if (v->numref) {
1239 bits = VC1_2REF_MVDATA_VLC_BITS;
1240 esc = 125;
1241 } else {
1242 bits = VC1_1REF_MVDATA_VLC_BITS;
1243 esc = 71;
1244 }
1245 switch (v->dmvrange) {
1246 case 1:
1247 extend_x = 1;
1248 break;
1249 case 2:
1250 extend_y = 1;
1251 break;
1252 case 3:
1253 extend_x = extend_y = 1;
1254 break;
1255 }
1256 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1257 if (index == esc) {
1258 *dmv_x = get_bits(gb, v->k_x);
1259 *dmv_y = get_bits(gb, v->k_y);
1260 if (v->numref) {
1261 *pred_flag = *dmv_y & 1;
1262 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1263 }
1264 }
1265 else {
1266 if (extend_x)
1267 offs_tab = offset_table2;
1268 else
1269 offs_tab = offset_table1;
1270 index1 = (index + 1) % 9;
1271 if (index1 != 0) {
1272 val = get_bits(gb, index1 + extend_x);
1273 sign = 0 -(val & 1);
1274 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1275 } else
1276 *dmv_x = 0;
1277 if (extend_y)
1278 offs_tab = offset_table2;
1279 else
1280 offs_tab = offset_table1;
1281 index1 = (index + 1) / 9;
1282 if (index1 > v->numref) {
1283 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1284 sign = 0 - (val & 1);
1285 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1286 } else
1287 *dmv_y = 0;
1288 if (v->numref)
1289 *pred_flag = index1 & 1;
1290 }
1291 }
1292
1293 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1294 {
1295 int scaledvalue, refdist;
1296 int scalesame1, scalesame2;
1297 int scalezone1_x, zone1offset_x;
1298 int table_index = dir ^ v->second_field;
1299
1300 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1301 refdist = v->refdist;
1302 else
1303 refdist = dir ? v->brfd : v->frfd;
1304 if (refdist > 3)
1305 refdist = 3;
1306 scalesame1 = vc1_field_mvpred_scales[table_index][1][refdist];
1307 scalesame2 = vc1_field_mvpred_scales[table_index][2][refdist];
1308 scalezone1_x = vc1_field_mvpred_scales[table_index][3][refdist];
1309 zone1offset_x = vc1_field_mvpred_scales[table_index][5][refdist];
1310
1311 if (FFABS(n) > 255)
1312 scaledvalue = n;
1313 else {
1314 if (FFABS(n) < scalezone1_x)
1315 scaledvalue = (n * scalesame1) >> 8;
1316 else {
1317 if (n < 0)
1318 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1319 else
1320 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1321 }
1322 }
1323 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1324 }
1325
1326 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1327 {
1328 int scaledvalue, refdist;
1329 int scalesame1, scalesame2;
1330 int scalezone1_y, zone1offset_y;
1331 int table_index = dir ^ v->second_field;
1332
1333 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1334 refdist = v->refdist;
1335 else
1336 refdist = dir ? v->brfd : v->frfd;
1337 if (refdist > 3)
1338 refdist = 3;
1339 scalesame1 = vc1_field_mvpred_scales[table_index][1][refdist];
1340 scalesame2 = vc1_field_mvpred_scales[table_index][2][refdist];
1341 scalezone1_y = vc1_field_mvpred_scales[table_index][4][refdist];
1342 zone1offset_y = vc1_field_mvpred_scales[table_index][6][refdist];
1343
1344 if (FFABS(n) > 63)
1345 scaledvalue = n;
1346 else {
1347 if (FFABS(n) < scalezone1_y)
1348 scaledvalue = (n * scalesame1) >> 8;
1349 else {
1350 if (n < 0)
1351 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1352 else
1353 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1354 }
1355 }
1356
1357 if (v->cur_field_type && !v->ref_field_type[dir])
1358 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1359 else
1360 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1361 }
1362
1363 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1364 {
1365 int scalezone1_x, zone1offset_x;
1366 int scaleopp1, scaleopp2, brfd;
1367 int scaledvalue;
1368
1369 brfd = FFMIN(v->brfd, 3);
1370 scalezone1_x = vc1_b_field_mvpred_scales[3][brfd];
1371 zone1offset_x = vc1_b_field_mvpred_scales[5][brfd];
1372 scaleopp1 = vc1_b_field_mvpred_scales[1][brfd];
1373 scaleopp2 = vc1_b_field_mvpred_scales[2][brfd];
1374
1375 if (FFABS(n) > 255)
1376 scaledvalue = n;
1377 else {
1378 if (FFABS(n) < scalezone1_x)
1379 scaledvalue = (n * scaleopp1) >> 8;
1380 else {
1381 if (n < 0)
1382 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1383 else
1384 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1385 }
1386 }
1387 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1388 }
1389
1390 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1391 {
1392 int scalezone1_y, zone1offset_y;
1393 int scaleopp1, scaleopp2, brfd;
1394 int scaledvalue;
1395
1396 brfd = FFMIN(v->brfd, 3);
1397 scalezone1_y = vc1_b_field_mvpred_scales[4][brfd];
1398 zone1offset_y = vc1_b_field_mvpred_scales[6][brfd];
1399 scaleopp1 = vc1_b_field_mvpred_scales[1][brfd];
1400 scaleopp2 = vc1_b_field_mvpred_scales[2][brfd];
1401
1402 if (FFABS(n) > 63)
1403 scaledvalue = n;
1404 else {
1405 if (FFABS(n) < scalezone1_y)
1406 scaledvalue = (n * scaleopp1) >> 8;
1407 else {
1408 if (n < 0)
1409 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1410 else
1411 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1412 }
1413 }
1414 if (v->cur_field_type && !v->ref_field_type[dir]) {
1415 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1416 } else {
1417 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1418 }
1419 }
1420
1421 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1422 int dim, int dir)
1423 {
1424 int brfd, scalesame;
1425 int hpel = 1 - v->s.quarter_sample;
1426
1427 n >>= hpel;
1428 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1429 if (dim)
1430 n = scaleforsame_y(v, i, n, dir) << hpel;
1431 else
1432 n = scaleforsame_x(v, n, dir) << hpel;
1433 return n;
1434 }
1435 brfd = FFMIN(v->brfd, 3);
1436 scalesame = vc1_b_field_mvpred_scales[0][brfd];
1437
1438 n = (n * scalesame >> 8) << hpel;
1439 return n;
1440 }
1441
1442 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1443 int dim, int dir)
1444 {
1445 int refdist, scaleopp;
1446 int hpel = 1 - v->s.quarter_sample;
1447
1448 n >>= hpel;
1449 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1450 if (dim)
1451 n = scaleforopp_y(v, n, dir) << hpel;
1452 else
1453 n = scaleforopp_x(v, n) << hpel;
1454 return n;
1455 }
1456 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1457 refdist = FFMIN(v->refdist, 3);
1458 else
1459 refdist = dir ? v->brfd : v->frfd;
1460 scaleopp = vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1461
1462 n = (n * scaleopp >> 8) << hpel;
1463 return n;
1464 }
1465
1466 /** Predict and set motion vector
1467 */
1468 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1469 int mv1, int r_x, int r_y, uint8_t* is_intra,
1470 int pred_flag, int dir)
1471 {
1472 MpegEncContext *s = &v->s;
1473 int xy, wrap, off = 0;
1474 int16_t *A, *B, *C;
1475 int px, py;
1476 int sum;
1477 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1478 int opposit, a_f, b_f, c_f;
1479 int16_t field_predA[2];
1480 int16_t field_predB[2];
1481 int16_t field_predC[2];
1482 int a_valid, b_valid, c_valid;
1483 int hybridmv_thresh, y_bias = 0;
1484
1485 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1486 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1487 mixedmv_pic = 1;
1488 else
1489 mixedmv_pic = 0;
1490 /* scale MV difference to be quad-pel */
1491 dmv_x <<= 1 - s->quarter_sample;
1492 dmv_y <<= 1 - s->quarter_sample;
1493
1494 wrap = s->b8_stride;
1495 xy = s->block_index[n];
1496
1497 if (s->mb_intra) {
1498 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1499 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1500 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1501 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
1502 if (mv1) { /* duplicate motion data for 1-MV block */
1503 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1504 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1505 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1506 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1507 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1508 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1509 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1510 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1511 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1512 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1513 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1514 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1515 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1516 }
1517 return;
1518 }
1519
1520 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
1521 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
1522 if (mv1) {
1523 if (v->field_mode && mixedmv_pic)
1524 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1525 else
1526 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1527 } else {
1528 //in 4-MV mode different blocks have different B predictor position
1529 switch (n) {
1530 case 0:
1531 off = (s->mb_x > 0) ? -1 : 1;
1532 break;
1533 case 1:
1534 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1535 break;
1536 case 2:
1537 off = 1;
1538 break;
1539 case 3:
1540 off = -1;
1541 }
1542 }
1543 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1544
1545 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1546 b_valid = a_valid && (s->mb_width > 1);
1547 c_valid = s->mb_x || (n == 1 || n == 3);
1548 if (v->field_mode) {
1549 a_valid = a_valid && !is_intra[xy - wrap];
1550 b_valid = b_valid && !is_intra[xy - wrap + off];
1551 c_valid = c_valid && !is_intra[xy - 1];
1552 }
1553
1554 if (a_valid) {
1555 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1556 num_oppfield += a_f;
1557 num_samefield += 1 - a_f;
1558 field_predA[0] = A[0];
1559 field_predA[1] = A[1];
1560 } else {
1561 field_predA[0] = field_predA[1] = 0;
1562 a_f = 0;
1563 }
1564 if (c_valid) {
1565 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1566 num_oppfield += c_f;
1567 num_samefield += 1 - c_f;
1568 field_predC[0] = C[0];
1569 field_predC[1] = C[1];
1570 } else {
1571 field_predC[0] = field_predC[1] = 0;
1572 c_f = 0;
1573 }
1574 if (b_valid) {
1575 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1576 num_oppfield += b_f;
1577 num_samefield += 1 - b_f;
1578 field_predB[0] = B[0];
1579 field_predB[1] = B[1];
1580 } else {
1581 field_predB[0] = field_predB[1] = 0;
1582 b_f = 0;
1583 }
1584
1585 if (v->field_mode) {
1586 if (num_samefield <= num_oppfield)
1587 opposit = 1 - pred_flag;
1588 else
1589 opposit = pred_flag;
1590 } else
1591 opposit = 0;
1592 if (opposit) {
1593 if (a_valid && !a_f) {
1594 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1595 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1596 }
1597 if (b_valid && !b_f) {
1598 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1599 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1600 }
1601 if (c_valid && !c_f) {
1602 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1603 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1604 }
1605 v->mv_f[dir][xy + v->blocks_off] = 1;
1606 v->ref_field_type[dir] = !v->cur_field_type;
1607 } else {
1608 if (a_valid && a_f) {
1609 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1610 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1611 }
1612 if (b_valid && b_f) {
1613 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1614 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1615 }
1616 if (c_valid && c_f) {
1617 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1618 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1619 }
1620 v->mv_f[dir][xy + v->blocks_off] = 0;
1621 v->ref_field_type[dir] = v->cur_field_type;
1622 }
1623
1624 if (a_valid) {
1625 px = field_predA[0];
1626 py = field_predA[1];
1627 } else if (c_valid) {
1628 px = field_predC[0];
1629 py = field_predC[1];
1630 } else if (b_valid) {
1631 px = field_predB[0];
1632 py = field_predB[1];
1633 } else {
1634 px = 0;
1635 py = 0;
1636 }
1637
1638 if (num_samefield + num_oppfield > 1) {
1639 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1640 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1641 }
1642
1643 /* Pullback MV as specified in 8.3.5.3.4 */
1644 if (!v->field_mode) {
1645 int qx, qy, X, Y;
1646 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1647 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1648 X = (s->mb_width << 6) - 4;
1649 Y = (s->mb_height << 6) - 4;
1650 if (mv1) {
1651 if (qx + px < -60) px = -60 - qx;
1652 if (qy + py < -60) py = -60 - qy;
1653 } else {
1654 if (qx + px < -28) px = -28 - qx;
1655 if (qy + py < -28) py = -28 - qy;
1656 }
1657 if (qx + px > X) px = X - qx;
1658 if (qy + py > Y) py = Y - qy;
1659 }
1660
1661 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1662 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1663 if (v->field_mode && !s->quarter_sample)
1664 hybridmv_thresh = 16;
1665 else
1666 hybridmv_thresh = 32;
1667 if (a_valid && c_valid) {
1668 if (is_intra[xy - wrap])
1669 sum = FFABS(px) + FFABS(py);
1670 else
1671 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1672 if (sum > hybridmv_thresh) {
1673 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1674 px = field_predA[0];
1675 py = field_predA[1];
1676 } else {
1677 px = field_predC[0];
1678 py = field_predC[1];
1679 }
1680 } else {
1681 if (is_intra[xy - 1])
1682 sum = FFABS(px) + FFABS(py);
1683 else
1684 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1685 if (sum > hybridmv_thresh) {
1686 if (get_bits1(&s->gb)) {
1687 px = field_predA[0];
1688 py = field_predA[1];
1689 } else {
1690 px = field_predC[0];
1691 py = field_predC[1];
1692 }
1693 }
1694 }
1695 }
1696 }
1697
1698 if (v->field_mode && !s->quarter_sample) {
1699 r_x <<= 1;
1700 r_y <<= 1;
1701 }
1702 if (v->field_mode && v->numref)
1703 r_y >>= 1;
1704 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1705 y_bias = 1;
1706 /* store MV using signed modulus of MV range defined in 4.11 */
1707 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;
1708 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;
1709 if (mv1) { /* duplicate motion data for 1-MV block */
1710 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];
1711 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];
1712 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];
1713 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];
1714 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];
1715 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];
1716 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1717 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];
1718 }
1719 }
1720
1721 /** Predict and set motion vector for interlaced frame picture MBs
1722 */
1723 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1724 int mvn, int r_x, int r_y, uint8_t* is_intra)
1725 {
1726 MpegEncContext *s = &v->s;
1727 int xy, wrap, off = 0;
1728 int A[2], B[2], C[2];
1729 int px, py;
1730 int a_valid = 0, b_valid = 0, c_valid = 0;
1731 int field_a, field_b, field_c; // 0: same, 1: opposit
1732 int total_valid, num_samefield, num_oppfield;
1733 int pos_c, pos_b, n_adj;
1734
1735 wrap = s->b8_stride;
1736 xy = s->block_index[n];
1737
1738 if (s->mb_intra) {
1739 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1740 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1741 s->current_picture.f.motion_val[1][xy][0] = 0;
1742 s->current_picture.f.motion_val[1][xy][1] = 0;
1743 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1744 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1745 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1746 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1747 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1748 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1749 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1750 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1751 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1752 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1753 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1754 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1755 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1756 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1757 }
1758 return;
1759 }
1760
1761 off = ((n == 0) || (n == 1)) ? 1 : -1;
1762 /* predict A */
1763 if (s->mb_x || (n == 1) || (n == 3)) {
1764 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1765 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1766 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1767 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1768 a_valid = 1;
1769 } else { // current block has frame mv and cand. has field MV (so average)
1770 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1771 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
1772 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1773 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
1774 a_valid = 1;
1775 }
1776 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1777 a_valid = 0;
1778 A[0] = A[1] = 0;
1779 }
1780 } else
1781 A[0] = A[1] = 0;
1782 /* Predict B and C */
1783 B[0] = B[1] = C[0] = C[1] = 0;
1784 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1785 if (!s->first_slice_line) {
1786 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1787 b_valid = 1;
1788 n_adj = n | 2;
1789 pos_b = s->block_index[n_adj] - 2 * wrap;
1790 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1791 n_adj = (n & 2) | (n & 1);
1792 }
1793 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1794 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1795 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1796 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1797 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1798 }
1799 }
1800 if (s->mb_width > 1) {
1801 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1802 c_valid = 1;
1803 n_adj = 2;
1804 pos_c = s->block_index[2] - 2 * wrap + 2;
1805 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1806 n_adj = n & 2;
1807 }
1808 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1809 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
1810 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1811 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1812 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1813 }
1814 if (s->mb_x == s->mb_width - 1) {
1815 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1816 c_valid = 1;
1817 n_adj = 3;
1818 pos_c = s->block_index[3] - 2 * wrap - 2;
1819 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1820 n_adj = n | 1;
1821 }
1822 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1823 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
1824 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1825 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1826 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1827 }
1828 } else
1829 c_valid = 0;
1830 }
1831 }
1832 }
1833 }
1834 } else {
1835 pos_b = s->block_index[1];
1836 b_valid = 1;
1837 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1838 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1839 pos_c = s->block_index[0];
1840 c_valid = 1;
1841 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1842 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1843 }
1844
1845 total_valid = a_valid + b_valid + c_valid;
1846 // check if predictor A is out of bounds
1847 if (!s->mb_x && !(n == 1 || n == 3)) {
1848 A[0] = A[1] = 0;
1849 }
1850 // check if predictor B is out of bounds
1851 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1852 B[0] = B[1] = C[0] = C[1] = 0;
1853 }
1854 if (!v->blk_mv_type[xy]) {
1855 if (s->mb_width == 1) {
1856 px = B[0];
1857 py = B[1];
1858 } else {
1859 if (total_valid >= 2) {
1860 px = mid_pred(A[0], B[0], C[0]);
1861 py = mid_pred(A[1], B[1], C[1]);
1862 } else if (total_valid) {
1863 if (a_valid) { px = A[0]; py = A[1]; }
1864 if (b_valid) { px = B[0]; py = B[1]; }
1865 if (c_valid) { px = C[0]; py = C[1]; }
1866 } else
1867 px = py = 0;
1868 }
1869 } else {
1870 if (a_valid)
1871 field_a = (A[1] & 4) ? 1 : 0;
1872 else
1873 field_a = 0;
1874 if (b_valid)
1875 field_b = (B[1] & 4) ? 1 : 0;
1876 else
1877 field_b = 0;
1878 if (c_valid)
1879 field_c = (C[1] & 4) ? 1 : 0;
1880 else
1881 field_c = 0;
1882
1883 num_oppfield = field_a + field_b + field_c;
1884 num_samefield = total_valid - num_oppfield;
1885 if (total_valid == 3) {
1886 if ((num_samefield == 3) || (num_oppfield == 3)) {
1887 px = mid_pred(A[0], B[0], C[0]);
1888 py = mid_pred(A[1], B[1], C[1]);
1889 } else if (num_samefield >= num_oppfield) {
1890 /* take one MV from same field set depending on priority
1891 the check for B may not be necessary */
1892 px = !field_a ? A[0] : B[0];
1893 py = !field_a ? A[1] : B[1];
1894 } else {
1895 px = field_a ? A[0] : B[0];
1896 py = field_a ? A[1] : B[1];
1897 }
1898 } else if (total_valid == 2) {
1899 if (num_samefield >= num_oppfield) {
1900 if (!field_a && a_valid) {
1901 px = A[0];
1902 py = A[1];
1903 } else if (!field_b && b_valid) {
1904 px = B[0];
1905 py = B[1];
1906 } else if (c_valid) {
1907 px = C[0];
1908 py = C[1];
1909 } else px = py = 0;
1910 } else {
1911 if (field_a && a_valid) {
1912 px = A[0];
1913 py = A[1];
1914 } else if (field_b && b_valid) {
1915 px = B[0];
1916 py = B[1];
1917 } else if (c_valid) {
1918 px = C[0];
1919 py = C[1];
1920 }
1921 }
1922 } else if (total_valid == 1) {
1923 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1924 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1925 } else
1926 px = py = 0;
1927 }
1928
1929 /* store MV using signed modulus of MV range defined in 4.11 */
1930 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;
1931 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;
1932 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1933 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1934 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1935 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1936 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1937 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1938 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1939 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1940 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1941 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1942 s->mv[0][n + 1][0] = s->mv[0][n][0];
1943 s->mv[0][n + 1][1] = s->mv[0][n][1];
1944 }
1945 }
1946
1947 /** Motion compensation for direct or interpolated blocks in B-frames
1948 */
1949 static void vc1_interp_mc(VC1Context *v)
1950 {
1951 MpegEncContext *s = &v->s;
1952 DSPContext *dsp = &v->s.dsp;
1953 uint8_t *srcY, *srcU, *srcV;
1954 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1955 int off, off_uv;
1956 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1957
1958 if (!v->field_mode && !v->s.next_picture.f.data[0])
1959 return;
1960
1961 mx = s->mv[1][0][0];
1962 my = s->mv[1][0][1];
1963 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1964 uvmy = (my + ((my & 3) == 3)) >> 1;
1965 if (v->field_mode) {
1966 if (v->cur_field_type != v->ref_field_type[1])
1967 my = my - 2 + 4 * v->cur_field_type;
1968 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1969 }
1970 if (v->fastuvmc) {
1971 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1972 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1973 }
1974 srcY = s->next_picture.f.data[0];
1975 srcU = s->next_picture.f.data[1];
1976 srcV = s->next_picture.f.data[2];
1977
1978 src_x = s->mb_x * 16 + (mx >> 2);
1979 src_y = s->mb_y * 16 + (my >> 2);
1980 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1981 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1982
1983 if (v->profile != PROFILE_ADVANCED) {
1984 src_x = av_clip( src_x, -16, s->mb_width * 16);
1985 src_y = av_clip( src_y, -16, s->mb_height * 16);
1986 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1987 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1988 } else {
1989 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1990 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1991 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1992 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1993 }
1994
1995 srcY += src_y * s->linesize + src_x;
1996 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1997 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1998
1999 if (v->field_mode && v->ref_field_type[1]) {
2000 srcY += s->current_picture_ptr->f.linesize[0];
2001 srcU += s->current_picture_ptr->f.linesize[1];
2002 srcV += s->current_picture_ptr->f.linesize[2];
2003 }
2004
2005 /* for grayscale we should not try to read from unknown area */
2006 if (s->flags & CODEC_FLAG_GRAY) {
2007 srcU = s->edge_emu_buffer + 18 * s->linesize;
2008 srcV = s->edge_emu_buffer + 18 * s->linesize;
2009 }
2010
2011 if (v->rangeredfrm
2012 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 16 - s->mspel * 3
2013 || (unsigned)(src_y - s->mspel) > v_edge_pos - (my & 3) - 16 - s->mspel * 3) {
2014 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
2015
2016 srcY -= s->mspel * (1 + s->linesize);
2017 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
2018 17 + s->mspel * 2, 17 + s->mspel * 2,
2019 src_x - s->mspel, src_y - s->mspel,
2020 s->h_edge_pos, v_edge_pos);
2021 srcY = s->edge_emu_buffer;
2022 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
2023 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
2024 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
2025 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
2026 srcU = uvbuf;
2027 srcV = uvbuf + 16;
2028 /* if we deal with range reduction we need to scale source blocks */
2029 if (v->rangeredfrm) {
2030 int i, j;
2031 uint8_t *src, *src2;
2032
2033 src = srcY;
2034 for (j = 0; j < 17 + s->mspel * 2; j++) {
2035 for (i = 0; i < 17 + s->mspel * 2; i++)
2036 src[i] = ((src[i] - 128) >> 1) + 128;
2037 src += s->linesize;
2038 }
2039 src = srcU;
2040 src2 = srcV;
2041 for (j = 0; j < 9; j++) {
2042 for (i = 0; i < 9; i++) {
2043 src[i] = ((src[i] - 128) >> 1) + 128;
2044 src2[i] = ((src2[i] - 128) >> 1) + 128;
2045 }
2046 src += s->uvlinesize;
2047 src2 += s->uvlinesize;
2048 }
2049 }
2050 srcY += s->mspel * (1 + s->linesize);
2051 }
2052
2053 if (v->field_mode && v->cur_field_type) {
2054 off = s->current_picture_ptr->f.linesize[0];
2055 off_uv = s->current_picture_ptr->f.linesize[1];
2056 } else {
2057 off = 0;
2058 off_uv = 0;
2059 }
2060
2061 if (s->mspel) {
2062 dxy = ((my & 3) << 2) | (mx & 3);
2063 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2064 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
2065 srcY += s->linesize * 8;
2066 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2067 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
2068 } else { // hpel mc
2069 dxy = (my & 2) | ((mx & 2) >> 1);
2070
2071 if (!v->rnd)
2072 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2073 else
2074 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2075 }
2076
2077 if (s->flags & CODEC_FLAG_GRAY) return;
2078 /* Chroma MC always uses qpel blilinear */
2079 uvmx = (uvmx & 3) << 1;
2080 uvmy = (uvmy & 3) << 1;
2081 if (!v->rnd) {
2082 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2083 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2084 } else {
2085 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2086 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2087 }
2088 }
2089
2090 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2091 {
2092 int n = bfrac;
2093
2094 #if B_FRACTION_DEN==256
2095 if (inv)
2096 n -= 256;
2097 if (!qs)
2098 return 2 * ((value * n + 255) >> 9);
2099 return (value * n + 128) >> 8;
2100 #else
2101 if (inv)
2102 n -= B_FRACTION_DEN;
2103 if (!qs)
2104 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2105 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2106 #endif
2107 }
2108
2109 static av_always_inline int scale_mv_intfi(int value, int bfrac, int inv,
2110 int qs, int qs_last)
2111 {
2112 int n = bfrac;
2113
2114 if (inv)
2115 n -= 256;
2116 n <<= !qs_last;
2117 if (!qs)
2118 return (value * n + 255) >> 9;
2119 else
2120 return (value * n + 128) >> 8;
2121 }
2122
2123 /** Reconstruct motion vector for B-frame and do motion compensation
2124 */
2125 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2126 int direct, int mode)
2127 {
2128 if (v->use_ic) {
2129 v->mv_mode2 = v->mv_mode;
2130 v->mv_mode = MV_PMODE_INTENSITY_COMP;
2131 }
2132 if (direct) {
2133 vc1_mc_1mv(v, 0);
2134 vc1_interp_mc(v);
2135 if (v->use_ic)
2136 v->mv_mode = v->mv_mode2;
2137 return;
2138 }
2139 if (mode == BMV_TYPE_INTERPOLATED) {
2140 vc1_mc_1mv(v, 0);
2141 vc1_interp_mc(v);
2142 if (v->use_ic)
2143 v->mv_mode = v->mv_mode2;
2144 return;
2145 }
2146
2147 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2148 v->mv_mode = v->mv_mode2;
2149 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2150 if (v->use_ic)
2151 v->mv_mode = v->mv_mode2;
2152 }
2153
2154 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2155 int direct, int mvtype)
2156 {
2157 MpegEncContext *s = &v->s;
2158 int xy, wrap, off = 0;
2159 int16_t *A, *B, *C;
2160 int px, py;
2161 int sum;
2162 int r_x, r_y;
2163 const uint8_t *is_intra = v->mb_type[0];
2164
2165 r_x = v->range_x;
2166 r_y = v->range_y;
2167 /* scale MV difference to be quad-pel */
2168 dmv_x[0] <<= 1 - s->quarter_sample;
2169 dmv_y[0] <<= 1 - s->quarter_sample;
2170 dmv_x[1] <<= 1 - s->quarter_sample;
2171 dmv_y[1] <<= 1 - s->quarter_sample;
2172
2173 wrap = s->b8_stride;
2174 xy = s->block_index[0];
2175
2176 if (s->mb_intra) {
2177 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2178 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2179 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2180 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2181 return;
2182 }
2183 if (!v->field_mode) {
2184 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2185 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2186 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2187 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2188
2189 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2190 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));
2191 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));
2192 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));
2193 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));
2194 }
2195 if (direct) {
2196 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2197 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2198 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2199 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2200 return;
2201 }
2202
2203 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2204 C = s->current_picture.f.motion_val[0][xy - 2];
2205 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2206 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2207 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2208
2209 if (!s->mb_x) C[0] = C[1] = 0;
2210 if (!s->first_slice_line) { // predictor A is not out of bounds
2211 if (s->mb_width == 1) {
2212 px = A[0];
2213 py = A[1];
2214 } else {
2215 px = mid_pred(A[0], B[0], C[0]);
2216 py = mid_pred(A[1], B[1], C[1]);
2217 }
2218 } else if (s->mb_x) { // predictor C is not out of bounds
2219 px = C[0];
2220 py = C[1];
2221 } else {
2222 px = py = 0;
2223 }
2224 /* Pullback MV as specified in 8.3.5.3.4 */
2225 {
2226 int qx, qy, X, Y;
2227 if (v->profile < PROFILE_ADVANCED) {
2228 qx = (s->mb_x << 5);
2229 qy = (s->mb_y << 5);
2230 X = (s->mb_width << 5) - 4;
2231 Y = (s->mb_height << 5) - 4;
2232 if (qx + px < -28) px = -28 - qx;
2233 if (qy + py < -28) py = -28 - qy;
2234 if (qx + px > X) px = X - qx;
2235 if (qy + py > Y) py = Y - qy;
2236 } else {
2237 qx = (s->mb_x << 6);
2238 qy = (s->mb_y << 6);
2239 X = (s->mb_width << 6) - 4;
2240 Y = (s->mb_height << 6) - 4;
2241 if (qx + px < -60) px = -60 - qx;
2242 if (qy + py < -60) py = -60 - qy;
2243 if (qx + px > X) px = X - qx;
2244 if (qy + py > Y) py = Y - qy;
2245 }
2246 }
2247 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2248 if (0 && !s->first_slice_line && s->mb_x) {
2249 if (is_intra[xy - wrap])
2250 sum = FFABS(px) + FFABS(py);
2251 else
2252 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2253 if (sum > 32) {
2254 if (get_bits1(&s->gb)) {
2255 px = A[0];
2256 py = A[1];
2257 } else {
2258 px = C[0];
2259 py = C[1];
2260 }
2261 } else {
2262 if (is_intra[xy - 2])
2263 sum = FFABS(px) + FFABS(py);
2264 else
2265 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2266 if (sum > 32) {
2267 if (get_bits1(&s->gb)) {
2268 px = A[0];
2269 py = A[1];
2270 } else {
2271 px = C[0];
2272 py = C[1];
2273 }
2274 }
2275 }
2276 }
2277 /* store MV using signed modulus of MV range defined in 4.11 */
2278 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2279 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2280 }
2281 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2282 C = s->current_picture.f.motion_val[1][xy - 2];
2283 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2284 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2285 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2286
2287 if (!s->mb_x)
2288 C[0] = C[1] = 0;
2289 if (!s->first_slice_line) { // predictor A is not out of bounds
2290 if (s->mb_width == 1) {
2291 px = A[0];
2292 py = A[1];
2293 } else {
2294 px = mid_pred(A[0], B[0], C[0]);
2295 py = mid_pred(A[1], B[1], C[1]);
2296 }
2297 } else if (s->mb_x) { // predictor C is not out of bounds
2298 px = C[0];
2299 py = C[1];
2300 } else {
2301 px = py = 0;
2302 }
2303 /* Pullback MV as specified in 8.3.5.3.4 */
2304 {
2305 int qx, qy, X, Y;
2306 if (v->profile < PROFILE_ADVANCED) {
2307 qx = (s->mb_x << 5);
2308 qy = (s->mb_y << 5);
2309 X = (s->mb_width << 5) - 4;
2310 Y = (s->mb_height << 5) - 4;
2311 if (qx + px < -28) px = -28 - qx;
2312 if (qy + py < -28) py = -28 - qy;
2313 if (qx + px > X) px = X - qx;
2314 if (qy + py > Y) py = Y - qy;
2315 } else {
2316 qx = (s->mb_x << 6);
2317 qy = (s->mb_y << 6);
2318 X = (s->mb_width << 6) - 4;
2319 Y = (s->mb_height << 6) - 4;
2320 if (qx + px < -60) px = -60 - qx;
2321 if (qy + py < -60) py = -60 - qy;
2322 if (qx + px > X) px = X - qx;
2323 if (qy + py > Y) py = Y - qy;
2324 }
2325 }
2326 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2327 if (0 && !s->first_slice_line && s->mb_x) {
2328 if (is_intra[xy - wrap])
2329 sum = FFABS(px) + FFABS(py);
2330 else
2331 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2332 if (sum > 32) {
2333 if (get_bits1(&s->gb)) {
2334 px = A[0];
2335 py = A[1];
2336 } else {
2337 px = C[0];
2338 py = C[1];
2339 }
2340 } else {
2341 if (is_intra[xy - 2])
2342 sum = FFABS(px) + FFABS(py);
2343 else
2344 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2345 if (sum > 32) {
2346 if (get_bits1(&s->gb)) {
2347 px = A[0];
2348 py = A[1];
2349 } else {
2350 px = C[0];
2351 py = C[1];
2352 }
2353 }
2354 }
2355 }
2356 /* store MV using signed modulus of MV range defined in 4.11 */
2357
2358 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2359 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2360 }
2361 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2362 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2363 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2364 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2365 }
2366
2367 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2368 {
2369 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2370 MpegEncContext *s = &v->s;
2371 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2372
2373 if (v->bmvtype == BMV_TYPE_DIRECT) {
2374 int total_opp, k, f;
2375 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2376 s->mv[0][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2377 v->bfraction, 0, s->quarter_sample, v->qs_last);
2378 s->mv[0][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2379 v->bfraction, 0, s->quarter_sample, v->qs_last);
2380 s->mv[1][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2381 v->bfraction, 1, s->quarter_sample, v->qs_last);
2382 s->mv[1][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2383 v->bfraction, 1, s->quarter_sample, v->qs_last);
2384
2385 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2386 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2387 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2388 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2389 f = (total_opp > 2) ? 1 : 0;
2390 } else {
2391 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2392 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2393 f = 0;
2394 }
2395 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2396 for (k = 0; k < 4; k++) {
2397 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2398 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2399 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2400 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2401 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2402 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2403 }
2404 return;
2405 }
2406 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2407 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);
2408 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);
2409 return;
2410 }
2411 if (dir) { // backward
2412 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);
2413 if (n == 3 || mv1) {
2414 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2415 }
2416 } else { // forward
2417 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);
2418 if (n == 3 || mv1) {
2419 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2420 }
2421 }
2422 }
2423
2424 /** Get predicted DC value for I-frames only
2425 * prediction dir: left=0, top=1
2426 * @param s MpegEncContext
2427 * @param overlap flag indicating that overlap filtering is used
2428 * @param pq integer part of picture quantizer
2429 * @param[in] n block index in the current MB
2430 * @param dc_val_ptr Pointer to DC predictor
2431 * @param dir_ptr Prediction direction for use in AC prediction
2432 */
2433 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2434 int16_t **dc_val_ptr, int *dir_ptr)
2435 {
2436 int a, b, c, wrap, pred, scale;
2437 int16_t *dc_val;
2438 static const uint16_t dcpred[32] = {
2439 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2440 114, 102, 93, 85, 79, 73, 68, 64,
2441 60, 57, 54, 51, 49, 47, 45, 43,
2442 41, 39, 38, 37, 35, 34, 33
2443 };
2444
2445 /* find prediction - wmv3_dc_scale always used here in fact */
2446 if (n < 4) scale = s->y_dc_scale;
2447 else scale = s->c_dc_scale;
2448
2449 wrap = s->block_wrap[n];
2450 dc_val = s->dc_val[0] + s->block_index[n];
2451
2452 /* B A
2453 * C X
2454 */
2455 c = dc_val[ - 1];
2456 b = dc_val[ - 1 - wrap];
2457 a = dc_val[ - wrap];
2458
2459 if (pq < 9 || !overlap) {
2460 /* Set outer values */
2461 if (s->first_slice_line && (n != 2 && n != 3))
2462 b = a = dcpred[scale];
2463 if (s->mb_x == 0 && (n != 1 && n != 3))
2464 b = c = dcpred[scale];
2465 } else {
2466 /* Set outer values */
2467 if (s->first_slice_line && (n != 2 && n != 3))
2468 b = a = 0;
2469 if (s->mb_x == 0 && (n != 1 && n != 3))
2470 b = c = 0;
2471 }
2472
2473 if (abs(a - b) <= abs(b - c)) {
2474 pred = c;
2475 *dir_ptr = 1; // left
2476 } else {
2477 pred = a;
2478 *dir_ptr = 0; // top
2479 }
2480
2481 /* update predictor */
2482 *dc_val_ptr = &dc_val[0];
2483 return pred;
2484 }
2485
2486
2487 /** Get predicted DC value
2488 * prediction dir: left=0, top=1
2489 * @param s MpegEncContext
2490 * @param overlap flag indicating that overlap filtering is used
2491 * @param pq integer part of picture quantizer
2492 * @param[in] n block index in the current MB
2493 * @param a_avail flag indicating top block availability
2494 * @param c_avail flag indicating left block availability
2495 * @param dc_val_ptr Pointer to DC predictor
2496 * @param dir_ptr Prediction direction for use in AC prediction
2497 */
2498 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2499 int a_avail, int c_avail,
2500 int16_t **dc_val_ptr, int *dir_ptr)
2501 {
2502 int a, b, c, wrap, pred;
2503 int16_t *dc_val;
2504 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2505 int q1, q2 = 0;
2506
2507 wrap = s->block_wrap[n];
2508 dc_val = s->dc_val[0] + s->block_index[n];
2509
2510 /* B A
2511 * C X
2512 */
2513 c = dc_val[ - 1];
2514 b = dc_val[ - 1 - wrap];
2515 a = dc_val[ - wrap];
2516 /* scale predictors if needed */
2517 q1 = s->current_picture.f.qscale_table[mb_pos];
2518 if (c_avail && (n != 1 && n != 3)) {
2519 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2520 if (q2 && q2 != q1)
2521 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2522 }
2523 if (a_avail && (n != 2 && n != 3)) {
2524 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2525 if (q2 && q2 != q1)
2526 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2527 }
2528 if (a_avail && c_avail && (n != 3)) {
2529 int off = mb_pos;
2530 if (n != 1)
2531 off--;
2532 if (n != 2)
2533 off -= s->mb_stride;
2534 q2 = s->current_picture.f.qscale_table[off];
2535 if (q2 && q2 != q1)
2536 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2537 }
2538
2539 if (a_avail && c_avail) {
2540 if (abs(a - b) <= abs(b - c)) {
2541 pred = c;
2542 *dir_ptr = 1; // left
2543 } else {
2544 pred = a;
2545 *dir_ptr = 0; // top
2546 }
2547 } else if (a_avail) {
2548 pred = a;
2549 *dir_ptr = 0; // top
2550 } else if (c_avail) {
2551 pred = c;
2552 *dir_ptr = 1; // left
2553 } else {
2554 pred = 0;
2555 *dir_ptr = 1; // left
2556 }
2557
2558 /* update predictor */
2559 *dc_val_ptr = &dc_val[0];
2560 return pred;
2561 }
2562
2563 /** @} */ // Block group
2564
2565 /**
2566 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2567 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2568 * @{
2569 */
2570
2571 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2572 uint8_t **coded_block_ptr)
2573 {
2574 int xy, wrap, pred, a, b, c;
2575
2576 xy = s->block_index[n];
2577 wrap = s->b8_stride;
2578
2579 /* B C
2580 * A X
2581 */