5220104b94117b407e2ad14e0dee5ffaecbcde2a
[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, f;
1479 int16_t samefield_pred[2], oppfield_pred[2];
1480 int16_t samefield_predA[2], oppfield_predA[2];
1481 int16_t samefield_predB[2], oppfield_predB[2];
1482 int16_t samefield_predC[2], oppfield_predC[2];
1483 int16_t *predA, *predC;
1484 int a_valid, b_valid, c_valid;
1485 int hybridmv_thresh, y_bias = 0;
1486
1487 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1488 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1489 mixedmv_pic = 1;
1490 else
1491 mixedmv_pic = 0;
1492 /* scale MV difference to be quad-pel */
1493 dmv_x <<= 1 - s->quarter_sample;
1494 dmv_y <<= 1 - s->quarter_sample;
1495
1496 wrap = s->b8_stride;
1497 xy = s->block_index[n];
1498
1499 if (s->mb_intra) {
1500 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1501 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1502 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1503 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
1504 if (mv1) { /* duplicate motion data for 1-MV block */
1505 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1506 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1507 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1508 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1509 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1510 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1511 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1512 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1513 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1514 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1515 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1516 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1517 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1518 }
1519 return;
1520 }
1521
1522 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
1523 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
1524 if (mv1) {
1525 if (v->field_mode && mixedmv_pic)
1526 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1527 else
1528 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1529 } else {
1530 //in 4-MV mode different blocks have different B predictor position
1531 switch (n) {
1532 case 0:
1533 off = (s->mb_x > 0) ? -1 : 1;
1534 break;
1535 case 1:
1536 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1537 break;
1538 case 2:
1539 off = 1;
1540 break;
1541 case 3:
1542 off = -1;
1543 }
1544 }
1545 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1546
1547 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1548 b_valid = a_valid && (s->mb_width > 1);
1549 c_valid = s->mb_x || (n == 1 || n == 3);
1550 if (v->field_mode) {
1551 a_valid = a_valid && !is_intra[xy - wrap];
1552 b_valid = b_valid && !is_intra[xy - wrap + off];
1553 c_valid = c_valid && !is_intra[xy - 1];
1554 }
1555
1556 if (a_valid) {
1557 f = v->mv_f[dir][xy - wrap + v->blocks_off];
1558 num_oppfield += f;
1559 num_samefield += 1 - f;
1560 if (f) {
1561 oppfield_predA[0] = A[0];
1562 oppfield_predA[1] = A[1];
1563 samefield_predA[0] = scaleforsame(v, 0, A[0], 0, dir);
1564 samefield_predA[1] = scaleforsame(v, n, A[1], 1, dir);
1565 } else {
1566 samefield_predA[0] = A[0];
1567 samefield_predA[1] = A[1];
1568 if (v->numref)
1569 oppfield_predA[0] = scaleforopp(v, A[0], 0, dir);
1570 if (v->numref)
1571 oppfield_predA[1] = scaleforopp(v, A[1], 1, dir);
1572 }
1573 } else {
1574 samefield_predA[0] = samefield_predA[1] = 0;
1575 oppfield_predA[0] = oppfield_predA[1] = 0;
1576 }
1577 if (c_valid) {
1578 f = v->mv_f[dir][xy - 1 + v->blocks_off];
1579 num_oppfield += f;
1580 num_samefield += 1 - f;
1581 if (f) {
1582 oppfield_predC[0] = C[0];
1583 oppfield_predC[1] = C[1];
1584 samefield_predC[0] = scaleforsame(v, 0, C[0], 0, dir);
1585 samefield_predC[1] = scaleforsame(v, n, C[1], 1, dir);
1586 } else {
1587 samefield_predC[0] = C[0];
1588 samefield_predC[1] = C[1];
1589 if (v->numref)
1590 oppfield_predC[0] = scaleforopp(v, C[0], 0, dir);
1591 if (v->numref)
1592 oppfield_predC[1] = scaleforopp(v, C[1], 1, dir);
1593 }
1594 } else {
1595 samefield_predC[0] = samefield_predC[1] = 0;
1596 oppfield_predC[0] = oppfield_predC[1] = 0;
1597 }
1598 if (b_valid) {
1599 f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1600 num_oppfield += f;
1601 num_samefield += 1 - f;
1602 if (f) {
1603 oppfield_predB[0] = B[0];
1604 oppfield_predB[1] = B[1];
1605 samefield_predB[0] = scaleforsame(v, 0, B[0], 0, dir);
1606 samefield_predB[1] = scaleforsame(v, n, B[1], 1, dir);
1607 } else {
1608 samefield_predB[0] = B[0];
1609 samefield_predB[1] = B[1];
1610 if (v->numref)
1611 oppfield_predB[0] = scaleforopp(v, B[0], 0, dir);
1612 if (v->numref)
1613 oppfield_predB[1] = scaleforopp(v, B[1], 1, dir);
1614 }
1615 } else {
1616 samefield_predB[0] = samefield_predB[1] = 0;
1617 oppfield_predB[0] = oppfield_predB[1] = 0;
1618 }
1619
1620 if (a_valid) {
1621 samefield_pred[0] = samefield_predA[0];
1622 samefield_pred[1] = samefield_predA[1];
1623 oppfield_pred[0] = oppfield_predA[0];
1624 oppfield_pred[1] = oppfield_predA[1];
1625 } else if (c_valid) {
1626 samefield_pred[0] = samefield_predC[0];
1627 samefield_pred[1] = samefield_predC[1];
1628 oppfield_pred[0] = oppfield_predC[0];
1629 oppfield_pred[1] = oppfield_predC[1];
1630 } else if (b_valid) {
1631 samefield_pred[0] = samefield_predB[0];
1632 samefield_pred[1] = samefield_predB[1];
1633 oppfield_pred[0] = oppfield_predB[0];
1634 oppfield_pred[1] = oppfield_predB[1];
1635 } else {
1636 samefield_pred[0] = samefield_pred[1] = 0;
1637 oppfield_pred[0] = oppfield_pred[1] = 0;
1638 }
1639
1640 if (num_samefield + num_oppfield > 1) {
1641 samefield_pred[0] = mid_pred(samefield_predA[0], samefield_predB[0], samefield_predC[0]);
1642 samefield_pred[1] = mid_pred(samefield_predA[1], samefield_predB[1], samefield_predC[1]);
1643 if (v->numref)
1644 oppfield_pred[0] = mid_pred(oppfield_predA[0], oppfield_predB[0], oppfield_predC[0]);
1645 if (v->numref)
1646 oppfield_pred[1] = mid_pred(oppfield_predA[1], oppfield_predB[1], oppfield_predC[1]);
1647 }
1648
1649 if (v->field_mode) {
1650 if (num_samefield <= num_oppfield)
1651 opposit = 1 - pred_flag;
1652 else
1653 opposit = pred_flag;
1654 } else
1655 opposit = 0;
1656 if (opposit) {
1657 px = oppfield_pred[0];
1658 py = oppfield_pred[1];
1659 predA = oppfield_predA;
1660 predC = oppfield_predC;
1661 v->mv_f[dir][xy + v->blocks_off] = f = 1;
1662 v->ref_field_type[dir] = !v->cur_field_type;
1663 } else {
1664 px = samefield_pred[0];
1665 py = samefield_pred[1];
1666 predA = samefield_predA;
1667 predC = samefield_predC;
1668 v->mv_f[dir][xy + v->blocks_off] = f = 0;
1669 v->ref_field_type[dir] = v->cur_field_type;
1670 }
1671
1672 /* Pullback MV as specified in 8.3.5.3.4 */
1673 if (!v->field_mode) {
1674 int qx, qy, X, Y;
1675 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1676 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1677 X = (s->mb_width << 6) - 4;
1678 Y = (s->mb_height << 6) - 4;
1679 if (mv1) {
1680 if (qx + px < -60) px = -60 - qx;
1681 if (qy + py < -60) py = -60 - qy;
1682 } else {
1683 if (qx + px < -28) px = -28 - qx;
1684 if (qy + py < -28) py = -28 - qy;
1685 }
1686 if (qx + px > X) px = X - qx;
1687 if (qy + py > Y) py = Y - qy;
1688 }
1689
1690 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1691 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1692 if (v->field_mode && !s->quarter_sample)
1693 hybridmv_thresh = 16;
1694 else
1695 hybridmv_thresh = 32;
1696 if (a_valid && c_valid) {
1697 if (is_intra[xy - wrap])
1698 sum = FFABS(px) + FFABS(py);
1699 else
1700 sum = FFABS(px - predA[0]) + FFABS(py - predA[1]);
1701 if (sum > hybridmv_thresh) {
1702 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1703 px = predA[0];
1704 py = predA[1];
1705 } else {
1706 px = predC[0];
1707 py = predC[1];
1708 }
1709 } else {
1710 if (is_intra[xy - 1])
1711 sum = FFABS(px) + FFABS(py);
1712 else
1713 sum = FFABS(px - predC[0]) + FFABS(py - predC[1]);
1714 if (sum > hybridmv_thresh) {
1715 if (get_bits1(&s->gb)) {
1716 px = predA[0];
1717 py = predA[1];
1718 } else {
1719 px = predC[0];
1720 py = predC[1];
1721 }
1722 }
1723 }
1724 }
1725 }
1726
1727 if (v->field_mode && !s->quarter_sample) {
1728 r_x <<= 1;
1729 r_y <<= 1;
1730 }
1731 if (v->field_mode && v->numref)
1732 r_y >>= 1;
1733 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1734 y_bias = 1;
1735 /* store MV using signed modulus of MV range defined in 4.11 */
1736 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;
1737 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;
1738 if (mv1) { /* duplicate motion data for 1-MV block */
1739 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];
1740 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];
1741 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];
1742 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];
1743 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];
1744 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];
1745 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1746 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];
1747 }
1748 }
1749
1750 /** Predict and set motion vector for interlaced frame picture MBs
1751 */
1752 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1753 int mvn, int r_x, int r_y, uint8_t* is_intra)
1754 {
1755 MpegEncContext *s = &v->s;
1756 int xy, wrap, off = 0;
1757 int A[2], B[2], C[2];
1758 int px, py;
1759 int a_valid = 0, b_valid = 0, c_valid = 0;
1760 int field_a, field_b, field_c; // 0: same, 1: opposit
1761 int total_valid, num_samefield, num_oppfield;
1762 int pos_c, pos_b, n_adj;
1763
1764 wrap = s->b8_stride;
1765 xy = s->block_index[n];
1766
1767 if (s->mb_intra) {
1768 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1769 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1770 s->current_picture.f.motion_val[1][xy][0] = 0;
1771 s->current_picture.f.motion_val[1][xy][1] = 0;
1772 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1773 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1774 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1775 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1776 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1777 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1778 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1779 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1780 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1781 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1782 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1783 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1784 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1785 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1786 }
1787 return;
1788 }
1789
1790 off = ((n == 0) || (n == 1)) ? 1 : -1;
1791 /* predict A */
1792 if (s->mb_x || (n == 1) || (n == 3)) {
1793 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1794 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1795 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1796 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1797 a_valid = 1;
1798 } else { // current block has frame mv and cand. has field MV (so average)
1799 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1800 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
1801 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1802 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
1803 a_valid = 1;
1804 }
1805 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1806 a_valid = 0;
1807 A[0] = A[1] = 0;
1808 }
1809 } else
1810 A[0] = A[1] = 0;
1811 /* Predict B and C */
1812 B[0] = B[1] = C[0] = C[1] = 0;
1813 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1814 if (!s->first_slice_line) {
1815 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1816 b_valid = 1;
1817 n_adj = n | 2;
1818 pos_b = s->block_index[n_adj] - 2 * wrap;
1819 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1820 n_adj = (n & 2) | (n & 1);
1821 }
1822 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1823 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1824 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1825 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1826 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1827 }
1828 }
1829 if (s->mb_width > 1) {
1830 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1831 c_valid = 1;
1832 n_adj = 2;
1833 pos_c = s->block_index[2] - 2 * wrap + 2;
1834 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1835 n_adj = n & 2;
1836 }
1837 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1838 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
1839 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1840 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1841 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1842 }
1843 if (s->mb_x == s->mb_width - 1) {
1844 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1845 c_valid = 1;
1846 n_adj = 3;
1847 pos_c = s->block_index[3] - 2 * wrap - 2;
1848 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1849 n_adj = n | 1;
1850 }
1851 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1852 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
1853 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1854 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1855 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1856 }
1857 } else
1858 c_valid = 0;
1859 }
1860 }
1861 }
1862 }
1863 } else {
1864 pos_b = s->block_index[1];
1865 b_valid = 1;
1866 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1867 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1868 pos_c = s->block_index[0];
1869 c_valid = 1;
1870 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1871 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1872 }
1873
1874 total_valid = a_valid + b_valid + c_valid;
1875 // check if predictor A is out of bounds
1876 if (!s->mb_x && !(n == 1 || n == 3)) {
1877 A[0] = A[1] = 0;
1878 }
1879 // check if predictor B is out of bounds
1880 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1881 B[0] = B[1] = C[0] = C[1] = 0;
1882 }
1883 if (!v->blk_mv_type[xy]) {
1884 if (s->mb_width == 1) {
1885 px = B[0];
1886 py = B[1];
1887 } else {
1888 if (total_valid >= 2) {
1889 px = mid_pred(A[0], B[0], C[0]);
1890 py = mid_pred(A[1], B[1], C[1]);
1891 } else if (total_valid) {
1892 if (a_valid) { px = A[0]; py = A[1]; }
1893 if (b_valid) { px = B[0]; py = B[1]; }
1894 if (c_valid) { px = C[0]; py = C[1]; }
1895 } else
1896 px = py = 0;
1897 }
1898 } else {
1899 if (a_valid)
1900 field_a = (A[1] & 4) ? 1 : 0;
1901 else
1902 field_a = 0;
1903 if (b_valid)
1904 field_b = (B[1] & 4) ? 1 : 0;
1905 else
1906 field_b = 0;
1907 if (c_valid)
1908 field_c = (C[1] & 4) ? 1 : 0;
1909 else
1910 field_c = 0;
1911
1912 num_oppfield = field_a + field_b + field_c;
1913 num_samefield = total_valid - num_oppfield;
1914 if (total_valid == 3) {
1915 if ((num_samefield == 3) || (num_oppfield == 3)) {
1916 px = mid_pred(A[0], B[0], C[0]);
1917 py = mid_pred(A[1], B[1], C[1]);
1918 } else if (num_samefield >= num_oppfield) {
1919 /* take one MV from same field set depending on priority
1920 the check for B may not be necessary */
1921 px = !field_a ? A[0] : B[0];
1922 py = !field_a ? A[1] : B[1];
1923 } else {
1924 px = field_a ? A[0] : B[0];
1925 py = field_a ? A[1] : B[1];
1926 }
1927 } else if (total_valid == 2) {
1928 if (num_samefield >= num_oppfield) {
1929 if (!field_a && a_valid) {
1930 px = A[0];
1931 py = A[1];
1932 } else if (!field_b && b_valid) {
1933 px = B[0];
1934 py = B[1];
1935 } else if (c_valid) {
1936 px = C[0];
1937 py = C[1];
1938 } else px = py = 0;
1939 } else {
1940 if (field_a && a_valid) {
1941 px = A[0];
1942 py = A[1];
1943 } else if (field_b && b_valid) {
1944 px = B[0];
1945 py = B[1];
1946 } else if (c_valid) {
1947 px = C[0];
1948 py = C[1];
1949 }
1950 }
1951 } else if (total_valid == 1) {
1952 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1953 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1954 } else
1955 px = py = 0;
1956 }
1957
1958 /* store MV using signed modulus of MV range defined in 4.11 */
1959 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;
1960 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;
1961 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1962 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1963 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1964 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1965 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1966 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1967 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1968 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1969 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1970 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1971 s->mv[0][n + 1][0] = s->mv[0][n][0];
1972 s->mv[0][n + 1][1] = s->mv[0][n][1];
1973 }
1974 }
1975
1976 /** Motion compensation for direct or interpolated blocks in B-frames
1977 */
1978 static void vc1_interp_mc(VC1Context *v)
1979 {
1980 MpegEncContext *s = &v->s;
1981 DSPContext *dsp = &v->s.dsp;
1982 uint8_t *srcY, *srcU, *srcV;
1983 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1984 int off, off_uv;
1985 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1986
1987 if (!v->field_mode && !v->s.next_picture.f.data[0])
1988 return;
1989
1990 mx = s->mv[1][0][0];
1991 my = s->mv[1][0][1];
1992 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1993 uvmy = (my + ((my & 3) == 3)) >> 1;
1994 if (v->field_mode) {
1995 if (v->cur_field_type != v->ref_field_type[1])
1996 my = my - 2 + 4 * v->cur_field_type;
1997 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1998 }
1999 if (v->fastuvmc) {
2000 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
2001 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
2002 }
2003 srcY = s->next_picture.f.data[0];
2004 srcU = s->next_picture.f.data[1];
2005 srcV = s->next_picture.f.data[2];
2006
2007 src_x = s->mb_x * 16 + (mx >> 2);
2008 src_y = s->mb_y * 16 + (my >> 2);
2009 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
2010 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
2011
2012 if (v->profile != PROFILE_ADVANCED) {
2013 src_x = av_clip( src_x, -16, s->mb_width * 16);
2014 src_y = av_clip( src_y, -16, s->mb_height * 16);
2015 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
2016 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
2017 } else {
2018 src_x = av_clip( src_x, -17, s->avctx->coded_width);
2019 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
2020 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
2021 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
2022 }
2023
2024 srcY += src_y * s->linesize + src_x;
2025 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
2026 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
2027
2028 if (v->field_mode && v->ref_field_type[1]) {
2029 srcY += s->current_picture_ptr->f.linesize[0];
2030 srcU += s->current_picture_ptr->f.linesize[1];
2031 srcV += s->current_picture_ptr->f.linesize[2];
2032 }
2033
2034 /* for grayscale we should not try to read from unknown area */
2035 if (s->flags & CODEC_FLAG_GRAY) {
2036 srcU = s->edge_emu_buffer + 18 * s->linesize;
2037 srcV = s->edge_emu_buffer + 18 * s->linesize;
2038 }
2039
2040 if (v->rangeredfrm
2041 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 16 - s->mspel * 3
2042 || (unsigned)(src_y - s->mspel) > v_edge_pos - (my & 3) - 16 - s->mspel * 3) {
2043 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
2044
2045 srcY -= s->mspel * (1 + s->linesize);
2046 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
2047 17 + s->mspel * 2, 17 + s->mspel * 2,
2048 src_x - s->mspel, src_y - s->mspel,
2049 s->h_edge_pos, v_edge_pos);
2050 srcY = s->edge_emu_buffer;
2051 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
2052 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
2053 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
2054 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
2055 srcU = uvbuf;
2056 srcV = uvbuf + 16;
2057 /* if we deal with range reduction we need to scale source blocks */
2058 if (v->rangeredfrm) {
2059 int i, j;
2060 uint8_t *src, *src2;
2061
2062 src = srcY;
2063 for (j = 0; j < 17 + s->mspel * 2; j++) {
2064 for (i = 0; i < 17 + s->mspel * 2; i++)
2065 src[i] = ((src[i] - 128) >> 1) + 128;
2066 src += s->linesize;
2067 }
2068 src = srcU;
2069 src2 = srcV;
2070 for (j = 0; j < 9; j++) {
2071 for (i = 0; i < 9; i++) {
2072 src[i] = ((src[i] - 128) >> 1) + 128;
2073 src2[i] = ((src2[i] - 128) >> 1) + 128;
2074 }
2075 src += s->uvlinesize;
2076 src2 += s->uvlinesize;
2077 }
2078 }
2079 srcY += s->mspel * (1 + s->linesize);
2080 }
2081
2082 if (v->field_mode && v->cur_field_type) {
2083 off = s->current_picture_ptr->f.linesize[0];
2084 off_uv = s->current_picture_ptr->f.linesize[1];
2085 } else {
2086 off = 0;
2087 off_uv = 0;
2088 }
2089
2090 if (s->mspel) {
2091 dxy = ((my & 3) << 2) | (mx & 3);
2092 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2093 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
2094 srcY += s->linesize * 8;
2095 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2096 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
2097 } else { // hpel mc
2098 dxy = (my & 2) | ((mx & 2) >> 1);
2099
2100 if (!v->rnd)
2101 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2102 else
2103 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2104 }
2105
2106 if (s->flags & CODEC_FLAG_GRAY) return;
2107 /* Chroma MC always uses qpel blilinear */
2108 uvmx = (uvmx & 3) << 1;
2109 uvmy = (uvmy & 3) << 1;
2110 if (!v->rnd) {
2111 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2112 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2113 } else {
2114 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2115 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2116 }
2117 }
2118
2119 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2120 {
2121 int n = bfrac;
2122
2123 #if B_FRACTION_DEN==256
2124 if (inv)
2125 n -= 256;
2126 if (!qs)
2127 return 2 * ((value * n + 255) >> 9);
2128 return (value * n + 128) >> 8;
2129 #else
2130 if (inv)
2131 n -= B_FRACTION_DEN;
2132 if (!qs)
2133 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2134 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2135 #endif
2136 }
2137
2138 static av_always_inline int scale_mv_intfi(int value, int bfrac, int inv,
2139 int qs, int qs_last)
2140 {
2141 int n = bfrac;
2142
2143 if (inv)
2144 n -= 256;
2145 n <<= !qs_last;
2146 if (!qs)
2147 return (value * n + 255) >> 9;
2148 else
2149 return (value * n + 128) >> 8;
2150 }
2151
2152 /** Reconstruct motion vector for B-frame and do motion compensation
2153 */
2154 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2155 int direct, int mode)
2156 {
2157 if (v->use_ic) {
2158 v->mv_mode2 = v->mv_mode;
2159 v->mv_mode = MV_PMODE_INTENSITY_COMP;
2160 }
2161 if (direct) {
2162 vc1_mc_1mv(v, 0);
2163 vc1_interp_mc(v);
2164 if (v->use_ic)
2165 v->mv_mode = v->mv_mode2;
2166 return;
2167 }
2168 if (mode == BMV_TYPE_INTERPOLATED) {
2169 vc1_mc_1mv(v, 0);
2170 vc1_interp_mc(v);
2171 if (v->use_ic)
2172 v->mv_mode = v->mv_mode2;
2173 return;
2174 }
2175
2176 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2177 v->mv_mode = v->mv_mode2;
2178 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2179 if (v->use_ic)
2180 v->mv_mode = v->mv_mode2;
2181 }
2182
2183 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2184 int direct, int mvtype)
2185 {
2186 MpegEncContext *s = &v->s;
2187 int xy, wrap, off = 0;
2188 int16_t *A, *B, *C;
2189 int px, py;
2190 int sum;
2191 int r_x, r_y;
2192 const uint8_t *is_intra = v->mb_type[0];
2193
2194 r_x = v->range_x;
2195 r_y = v->range_y;
2196 /* scale MV difference to be quad-pel */
2197 dmv_x[0] <<= 1 - s->quarter_sample;
2198 dmv_y[0] <<= 1 - s->quarter_sample;
2199 dmv_x[1] <<= 1 - s->quarter_sample;
2200 dmv_y[1] <<= 1 - s->quarter_sample;
2201
2202 wrap = s->b8_stride;
2203 xy = s->block_index[0];
2204
2205 if (s->mb_intra) {
2206 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2207 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2208 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2209 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2210 return;
2211 }
2212 if (!v->field_mode) {
2213 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2214 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2215 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2216 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2217
2218 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2219 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));
2220 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));
2221 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));
2222 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));
2223 }
2224 if (direct) {
2225 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2226 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2227 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2228 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2229 return;
2230 }
2231
2232 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2233 C = s->current_picture.f.motion_val[0][xy - 2];
2234 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2235 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2236 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2237
2238 if (!s->mb_x) C[0] = C[1] = 0;
2239 if (!s->first_slice_line) { // predictor A is not out of bounds
2240 if (s->mb_width == 1) {
2241 px = A[0];
2242 py = A[1];
2243 } else {
2244 px = mid_pred(A[0], B[0], C[0]);
2245 py = mid_pred(A[1], B[1], C[1]);
2246 }
2247 } else if (s->mb_x) { // predictor C is not out of bounds
2248 px = C[0];
2249 py = C[1];
2250 } else {
2251 px = py = 0;
2252 }
2253 /* Pullback MV as specified in 8.3.5.3.4 */
2254 {
2255 int qx, qy, X, Y;
2256 if (v->profile < PROFILE_ADVANCED) {
2257 qx = (s->mb_x << 5);
2258 qy = (s->mb_y << 5);
2259 X = (s->mb_width << 5) - 4;
2260 Y = (s->mb_height << 5) - 4;
2261 if (qx + px < -28) px = -28 - qx;
2262 if (qy + py < -28) py = -28 - qy;
2263 if (qx + px > X) px = X - qx;
2264 if (qy + py > Y) py = Y - qy;
2265 } else {
2266 qx = (s->mb_x << 6);
2267 qy = (s->mb_y << 6);
2268 X = (s->mb_width << 6) - 4;
2269 Y = (s->mb_height << 6) - 4;
2270 if (qx + px < -60) px = -60 - qx;
2271 if (qy + py < -60) py = -60 - qy;
2272 if (qx + px > X) px = X - qx;
2273 if (qy + py > Y) py = Y - qy;
2274 }
2275 }
2276 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2277 if (0 && !s->first_slice_line && s->mb_x) {
2278 if (is_intra[xy - wrap])
2279 sum = FFABS(px) + FFABS(py);
2280 else
2281 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2282 if (sum > 32) {
2283 if (get_bits1(&s->gb)) {
2284 px = A[0];
2285 py = A[1];
2286 } else {
2287 px = C[0];
2288 py = C[1];
2289 }
2290 } else {
2291 if (is_intra[xy - 2])
2292 sum = FFABS(px) + FFABS(py);
2293 else
2294 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2295 if (sum > 32) {
2296 if (get_bits1(&s->gb)) {
2297 px = A[0];
2298 py = A[1];
2299 } else {
2300 px = C[0];
2301 py = C[1];
2302 }
2303 }
2304 }
2305 }
2306 /* store MV using signed modulus of MV range defined in 4.11 */
2307 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2308 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2309 }
2310 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2311 C = s->current_picture.f.motion_val[1][xy - 2];
2312 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2313 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2314 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2315
2316 if (!s->mb_x)
2317 C[0] = C[1] = 0;
2318 if (!s->first_slice_line) { // predictor A is not out of bounds
2319 if (s->mb_width == 1) {
2320 px = A[0];
2321 py = A[1];
2322 } else {
2323 px = mid_pred(A[0], B[0], C[0]);
2324 py = mid_pred(A[1], B[1], C[1]);
2325 }
2326 } else if (s->mb_x) { // predictor C is not out of bounds
2327 px = C[0];
2328 py = C[1];
2329 } else {
2330 px = py = 0;
2331 }
2332 /* Pullback MV as specified in 8.3.5.3.4 */
2333 {
2334 int qx, qy, X, Y;
2335 if (v->profile < PROFILE_ADVANCED) {
2336 qx = (s->mb_x << 5);
2337 qy = (s->mb_y << 5);
2338 X = (s->mb_width << 5) - 4;
2339 Y = (s->mb_height << 5) - 4;
2340 if (qx + px < -28) px = -28 - qx;
2341 if (qy + py < -28) py = -28 - qy;
2342 if (qx + px > X) px = X - qx;
2343 if (qy + py > Y) py = Y - qy;
2344 } else {
2345 qx = (s->mb_x << 6);
2346 qy = (s->mb_y << 6);
2347 X = (s->mb_width << 6) - 4;
2348 Y = (s->mb_height << 6) - 4;
2349 if (qx + px < -60) px = -60 - qx;
2350 if (qy + py < -60) py = -60 - qy;
2351 if (qx + px > X) px = X - qx;
2352 if (qy + py > Y) py = Y - qy;
2353 }
2354 }
2355 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2356 if (0 && !s->first_slice_line && s->mb_x) {
2357 if (is_intra[xy - wrap])
2358 sum = FFABS(px) + FFABS(py);
2359 else
2360 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2361 if (sum > 32) {
2362 if (get_bits1(&s->gb)) {
2363 px = A[0];
2364 py = A[1];
2365 } else {
2366 px = C[0];
2367 py = C[1];
2368 }
2369 } else {
2370 if (is_intra[xy - 2])
2371 sum = FFABS(px) + FFABS(py);
2372 else
2373 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2374 if (sum > 32) {
2375 if (get_bits1(&s->gb)) {
2376 px = A[0];
2377 py = A[1];
2378 } else {
2379 px = C[0];
2380 py = C[1];
2381 }
2382 }
2383 }
2384 }
2385 /* store MV using signed modulus of MV range defined in 4.11 */
2386
2387 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2388 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2389 }
2390 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2391 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2392 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2393 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2394 }
2395
2396 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2397 {
2398 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2399 MpegEncContext *s = &v->s;
2400 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2401
2402 if (v->bmvtype == BMV_TYPE_DIRECT) {
2403 int total_opp, k, f;
2404 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2405 s->mv[0][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2406 v->bfraction, 0, s->quarter_sample, v->qs_last);
2407 s->mv[0][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2408 v->bfraction, 0, s->quarter_sample, v->qs_last);
2409 s->mv[1][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2410 v->bfraction, 1, s->quarter_sample, v->qs_last);
2411 s->mv[1][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2412 v->bfraction, 1, s->quarter_sample, v->qs_last);
2413
2414 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2415 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2416 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2417 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2418 f = (total_opp > 2) ? 1 : 0;
2419 } else {
2420 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2421 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2422 f = 0;
2423 }
2424 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2425 for (k = 0; k < 4; k++) {
2426 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2427 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2428 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2429 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2430 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2431 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2432 }
2433 return;
2434 }
2435 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2436 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);
2437 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);
2438 return;
2439 }
2440 if (dir) { // backward
2441 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);
2442 if (n == 3 || mv1) {
2443 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2444 }
2445 } else { // forward
2446 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);
2447 if (n == 3 || mv1) {
2448 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2449 }
2450 }
2451 }
2452
2453 /** Get predicted DC value for I-frames only
2454 * prediction dir: left=0, top=1
2455 * @param s MpegEncContext
2456 * @param overlap flag indicating that overlap filtering is used
2457 * @param pq integer part of picture quantizer
2458 * @param[in] n block index in the current MB
2459 * @param dc_val_ptr Pointer to DC predictor
2460 * @param dir_ptr Prediction direction for use in AC prediction
2461 */
2462 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2463 int16_t **dc_val_ptr, int *dir_ptr)
2464 {
2465 int a, b, c, wrap, pred, scale;
2466 int16_t *dc_val;
2467 static const uint16_t dcpred[32] = {
2468 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2469 114, 102, 93, 85, 79, 73, 68, 64,
2470 60, 57, 54, 51, 49, 47, 45, 43,
2471 41, 39, 38, 37, 35, 34, 33
2472 };
2473
2474 /* find prediction - wmv3_dc_scale always used here in fact */
2475 if (n < 4) scale = s->y_dc_scale;
2476 else scale = s->c_dc_scale;
2477
2478 wrap = s->block_wrap[n];
2479 dc_val = s->dc_val[0] + s->block_index[n];
2480
2481 /* B A
2482 * C X
2483 */
2484 c = dc_val[ - 1];
2485 b = dc_val[ - 1 - wrap];
2486 a = dc_val[ - wrap];
2487
2488 if (pq < 9 || !overlap) {
2489 /* Set outer values */
2490 if (s->first_slice_line && (n != 2 && n != 3))
2491 b = a = dcpred[scale];
2492 if (s->mb_x == 0 && (n != 1 && n != 3))
2493 b = c = dcpred[scale];
2494 } else {
2495 /* Set outer values */
2496 if (s->first_slice_line && (n != 2 && n != 3))
2497 b = a = 0;
2498 if (s->mb_x == 0 && (n != 1 && n != 3))
2499 b = c = 0;
2500 }
2501
2502 if (abs(a - b) <= abs(b - c)) {
2503 pred = c;
2504 *dir_ptr = 1; // left
2505 } else {
2506 pred = a;
2507 *dir_ptr = 0; // top
2508 }
2509
2510 /* update predictor */
2511 *dc_val_ptr = &dc_val[0];
2512 return pred;
2513 }
2514
2515
2516 /** Get predicted DC value
2517 * prediction dir: left=0, top=1
2518 * @param s MpegEncContext
2519 * @param overlap flag indicating that overlap filtering is used
2520 * @param pq integer part of picture quantizer
2521 * @param[in] n block index in the current MB
2522 * @param a_avail flag indicating top block availability
2523 * @param c_avail flag indicating left block availability
2524 * @param dc_val_ptr Pointer to DC predictor
2525 * @param dir_ptr Prediction direction for use in AC prediction
2526 */
2527 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2528 int a_avail, int c_avail,
2529 int16_t **dc_val_ptr, int *dir_ptr)
2530 {
2531 int a, b, c, wrap, pred;
2532 int16_t *dc_val;
2533 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2534 int q1, q2 = 0;
2535
2536 wrap = s->block_wrap[n];
2537 dc_val = s->dc_val[0] + s->block_index[n];
2538
2539 /* B A
2540 * C X
2541 */
2542 c = dc_val[ - 1];
2543 b = dc_val[ - 1 - wrap];
2544 a = dc_val[ - wrap];
2545 /* scale predictors if needed */
2546 q1 = s->current_picture.f.qscale_table[mb_pos];
2547 if (c_avail && (n != 1 && n != 3)) {
2548 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2549 if (q2 && q2 != q1)
2550 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2551 }
2552 if (a_avail && (n != 2 && n != 3)) {
2553 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2554 if (q2 && q2 != q1)
2555 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2556 }
2557 if (a_avail && c_avail && (n != 3)) {
2558 int off = mb_pos;
2559 if (n != 1)
2560 off--;
2561 if (n != 2)
2562 off -= s->mb_stride;
2563 q2 = s->current_picture.f.qscale_table[off];
2564 if (q2 && q2 != q1)
2565 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2566 }
2567
2568 if (a_avail && c_avail) {
2569 if (abs