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