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