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