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