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