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