avcodec/videotoolbox: improve logging of decoder errors
[ffmpeg.git] / libavcodec / vc1_mc.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 FFmpeg.
8  *
9  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 block decoding routines
27  */
28
29 #include "avcodec.h"
30 #include "h264chroma.h"
31 #include "mathops.h"
32 #include "mpegvideo.h"
33 #include "vc1.h"
34
35 static av_always_inline void vc1_scale_luma(uint8_t *srcY,
36                                             int k, int linesize)
37 {
38     int i, j;
39     for (j = 0; j < k; j++) {
40         for (i = 0; i < k; i++)
41             srcY[i] = ((srcY[i] - 128) >> 1) + 128;
42         srcY += linesize;
43     }
44 }
45
46 static av_always_inline void vc1_scale_chroma(uint8_t *srcU, uint8_t *srcV,
47                                               int k, int uvlinesize)
48 {
49     int i, j;
50     for (j = 0; j < k; j++) {
51         for (i = 0; i < k; i++) {
52             srcU[i] = ((srcU[i] - 128) >> 1) + 128;
53             srcV[i] = ((srcV[i] - 128) >> 1) + 128;
54         }
55         srcU += uvlinesize;
56         srcV += uvlinesize;
57     }
58 }
59
60 static av_always_inline void vc1_lut_scale_luma(uint8_t *srcY,
61                                                 uint8_t *lut1, uint8_t *lut2,
62                                                 int k, int linesize)
63 {
64     int i, j;
65
66     for (j = 0; j < k; j += 2) {
67         for (i = 0; i < k; i++)
68             srcY[i] = lut1[srcY[i]];
69         srcY += linesize;
70
71         if (j + 1 == k)
72             break;
73
74         for (i = 0; i < k; i++)
75             srcY[i] = lut2[srcY[i]];
76         srcY += linesize;
77     }
78 }
79
80 static av_always_inline void vc1_lut_scale_chroma(uint8_t *srcU, uint8_t *srcV,
81                                                   uint8_t *lut1, uint8_t *lut2,
82                                                   int k, int uvlinesize)
83 {
84     int i, j;
85
86     for (j = 0; j < k; j += 2) {
87         for (i = 0; i < k; i++) {
88             srcU[i] = lut1[srcU[i]];
89             srcV[i] = lut1[srcV[i]];
90         }
91         srcU += uvlinesize;
92         srcV += uvlinesize;
93
94         if (j + 1 == k)
95             break;
96
97         for (i = 0; i < k; i++) {
98             srcU[i] = lut2[srcU[i]];
99             srcV[i] = lut2[srcV[i]];
100         }
101         srcU += uvlinesize;
102         srcV += uvlinesize;
103     }
104 }
105
106 static const uint8_t popcount4[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
107
108 static av_always_inline int get_luma_mv(VC1Context *v, int dir, int16_t *tx, int16_t *ty)
109 {
110     MpegEncContext *s = &v->s;
111     int idx = v->mv_f[dir][s->block_index[0] + v->blocks_off] |
112              (v->mv_f[dir][s->block_index[1] + v->blocks_off] << 1) |
113              (v->mv_f[dir][s->block_index[2] + v->blocks_off] << 2) |
114              (v->mv_f[dir][s->block_index[3] + v->blocks_off] << 3);
115     static const uint8_t index2[16] = { 0, 0, 0, 0x23, 0, 0x13, 0x03, 0, 0, 0x12, 0x02, 0, 0x01, 0, 0, 0 };
116     int opp_count = popcount4[idx];
117
118     switch (opp_count) {
119     case 0:
120     case 4:
121         *tx = median4(s->mv[dir][0][0], s->mv[dir][1][0], s->mv[dir][2][0], s->mv[dir][3][0]);
122         *ty = median4(s->mv[dir][0][1], s->mv[dir][1][1], s->mv[dir][2][1], s->mv[dir][3][1]);
123         break;
124     case 1:
125         *tx = mid_pred(s->mv[dir][idx < 2][0], s->mv[dir][1 + (idx < 4)][0], s->mv[dir][2 + (idx < 8)][0]);
126         *ty = mid_pred(s->mv[dir][idx < 2][1], s->mv[dir][1 + (idx < 4)][1], s->mv[dir][2 + (idx < 8)][1]);
127         break;
128     case 3:
129         *tx = mid_pred(s->mv[dir][idx > 0xd][0], s->mv[dir][1 + (idx > 0xb)][0], s->mv[dir][2 + (idx > 0x7)][0]);
130         *ty = mid_pred(s->mv[dir][idx > 0xd][1], s->mv[dir][1 + (idx > 0xb)][1], s->mv[dir][2 + (idx > 0x7)][1]);
131         break;
132     case 2:
133         *tx = (s->mv[dir][index2[idx] >> 4][0] + s->mv[dir][index2[idx] & 0xf][0]) / 2;
134         *ty = (s->mv[dir][index2[idx] >> 4][1] + s->mv[dir][index2[idx] & 0xf][1]) / 2;
135         break;
136     }
137     return opp_count;
138 }
139
140 static av_always_inline int get_chroma_mv(VC1Context *v, int dir, int16_t *tx, int16_t *ty)
141 {
142     MpegEncContext *s = &v->s;
143     int idx = !v->mb_type[0][s->block_index[0]] |
144              (!v->mb_type[0][s->block_index[1]] << 1) |
145              (!v->mb_type[0][s->block_index[2]] << 2) |
146              (!v->mb_type[0][s->block_index[3]] << 3);
147     static const uint8_t index2[16] = { 0, 0, 0, 0x01, 0, 0x02, 0x12, 0, 0, 0x03, 0x13, 0, 0x23, 0, 0, 0 };
148     int valid_count = popcount4[idx];
149
150     switch (valid_count) {
151     case 4:
152         *tx = median4(s->mv[dir][0][0], s->mv[dir][1][0], s->mv[dir][2][0], s->mv[dir][3][0]);
153         *ty = median4(s->mv[dir][0][1], s->mv[dir][1][1], s->mv[dir][2][1], s->mv[dir][3][1]);
154         break;
155     case 3:
156         *tx = mid_pred(s->mv[dir][idx > 0xd][0], s->mv[dir][1 + (idx > 0xb)][0], s->mv[dir][2 + (idx > 0x7)][0]);
157         *ty = mid_pred(s->mv[dir][idx > 0xd][1], s->mv[dir][1 + (idx > 0xb)][1], s->mv[dir][2 + (idx > 0x7)][1]);
158         break;
159     case 2:
160         *tx = (s->mv[dir][index2[idx] >> 4][0] + s->mv[dir][index2[idx] & 0xf][0]) / 2;
161         *ty = (s->mv[dir][index2[idx] >> 4][1] + s->mv[dir][index2[idx] & 0xf][1]) / 2;
162         break;
163     default:
164         return 0;
165     }
166     return valid_count;
167 }
168
169 /** Do motion compensation over 1 macroblock
170  * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
171  */
172 void ff_vc1_mc_1mv(VC1Context *v, int dir)
173 {
174     MpegEncContext *s = &v->s;
175     H264ChromaContext *h264chroma = &v->h264chroma;
176     uint8_t *srcY, *srcU, *srcV;
177     int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
178     int v_edge_pos = s->v_edge_pos >> v->field_mode;
179     int i;
180     uint8_t (*luty)[256], (*lutuv)[256];
181     int use_ic;
182
183     if ((!v->field_mode ||
184          (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
185         !v->s.last_picture.f->data[0])
186         return;
187
188     mx = s->mv[dir][0][0];
189     my = s->mv[dir][0][1];
190
191     // store motion vectors for further use in B-frames
192     if (s->pict_type == AV_PICTURE_TYPE_P) {
193         for (i = 0; i < 4; i++) {
194             s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
195             s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
196         }
197     }
198
199     uvmx = (mx + ((mx & 3) == 3)) >> 1;
200     uvmy = (my + ((my & 3) == 3)) >> 1;
201     v->luma_mv[s->mb_x][0] = uvmx;
202     v->luma_mv[s->mb_x][1] = uvmy;
203
204     if (v->field_mode &&
205         v->cur_field_type != v->ref_field_type[dir]) {
206         my   = my   - 2 + 4 * v->cur_field_type;
207         uvmy = uvmy - 2 + 4 * v->cur_field_type;
208     }
209
210     // fastuvmc shall be ignored for interlaced frame picture
211     if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
212         uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
213         uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
214     }
215     if (!dir) {
216         if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
217             srcY = s->current_picture.f->data[0];
218             srcU = s->current_picture.f->data[1];
219             srcV = s->current_picture.f->data[2];
220             luty  = v->curr_luty;
221             lutuv = v->curr_lutuv;
222             use_ic = *v->curr_use_ic;
223         } else {
224             srcY = s->last_picture.f->data[0];
225             srcU = s->last_picture.f->data[1];
226             srcV = s->last_picture.f->data[2];
227             luty  = v->last_luty;
228             lutuv = v->last_lutuv;
229             use_ic = v->last_use_ic;
230         }
231     } else {
232         srcY = s->next_picture.f->data[0];
233         srcU = s->next_picture.f->data[1];
234         srcV = s->next_picture.f->data[2];
235         luty  = v->next_luty;
236         lutuv = v->next_lutuv;
237         use_ic = v->next_use_ic;
238     }
239
240     if (!srcY || !srcU) {
241         av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
242         return;
243     }
244
245     src_x   = s->mb_x * 16 + (mx   >> 2);
246     src_y   = s->mb_y * 16 + (my   >> 2);
247     uvsrc_x = s->mb_x *  8 + (uvmx >> 2);
248     uvsrc_y = s->mb_y *  8 + (uvmy >> 2);
249
250     if (v->profile != PROFILE_ADVANCED) {
251         src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
252         src_y   = av_clip(  src_y, -16, s->mb_height * 16);
253         uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
254         uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
255     } else {
256         src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
257         uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
258         if (v->fcm == ILACE_FRAME) {
259             src_y = av_clip(src_y, -18 + (src_y & 1), s->avctx->coded_height + (src_y & 1));
260             uvsrc_y = av_clip(uvsrc_y, -8 + (uvsrc_y & 1), (s->avctx->coded_height >> 1) + (uvsrc_y & 1));
261         } else {
262             src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
263             uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
264         }
265     }
266
267     srcY += src_y   * s->linesize   + src_x;
268     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
269     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
270
271     if (v->field_mode && v->ref_field_type[dir]) {
272         srcY += s->current_picture_ptr->f->linesize[0];
273         srcU += s->current_picture_ptr->f->linesize[1];
274         srcV += s->current_picture_ptr->f->linesize[2];
275     }
276
277     /* for grayscale we should not try to read from unknown area */
278     if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY) {
279         srcU = s->sc.edge_emu_buffer + 18 * s->linesize;
280         srcV = s->sc.edge_emu_buffer + 18 * s->linesize;
281     }
282
283     if (v->rangeredfrm || use_ic
284         || s->h_edge_pos < 22 || v_edge_pos < 22
285         || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
286         || (unsigned)(src_y - 1)        > v_edge_pos    - (my&3) - 16 - 3) {
287         uint8_t *ubuf = s->sc.edge_emu_buffer + 19 * s->linesize;
288         uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
289         const int k = 17 + s->mspel * 2;
290
291         srcY -= s->mspel * (1 + s->linesize);
292         if (v->fcm == ILACE_FRAME) {
293             if (src_y - s->mspel & 1) {
294                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
295                                          srcY,
296                                          2 * s->linesize,
297                                          2 * s->linesize,
298                                          k,
299                                          k + 1 >> 1,
300                                          src_x - s->mspel,
301                                          src_y - s->mspel >> 1,
302                                          s->h_edge_pos,
303                                          v_edge_pos + 1 >> 1);
304                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + s->linesize,
305                                          srcY + s->linesize,
306                                          2 * s->linesize,
307                                          2 * s->linesize,
308                                          k,
309                                          k >> 1,
310                                          src_x - s->mspel,
311                                          src_y - s->mspel + 1 >> 1,
312                                          s->h_edge_pos,
313                                          v_edge_pos >> 1);
314             } else {
315                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
316                                          srcY,
317                                          2 * s->linesize,
318                                          2 * s->linesize,
319                                          k,
320                                          k + 1 >> 1,
321                                          src_x - s->mspel,
322                                          src_y - s->mspel >> 1,
323                                          s->h_edge_pos,
324                                          v_edge_pos >> 1);
325                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + s->linesize,
326                                          srcY + s->linesize,
327                                          2 * s->linesize,
328                                          2 * s->linesize,
329                                          k,
330                                          k >> 1,
331                                          src_x - s->mspel,
332                                          src_y - s->mspel + 1 >> 1,
333                                          s->h_edge_pos,
334                                          v_edge_pos + 1 >> 1);
335             }
336         } else
337             s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcY,
338                                      s->linesize, s->linesize,
339                                      k, k,
340                                      src_x - s->mspel, src_y - s->mspel,
341                                      s->h_edge_pos, v_edge_pos);
342         srcY = s->sc.edge_emu_buffer;
343         if (v->fcm == ILACE_FRAME) {
344             if (uvsrc_y & 1) {
345                 s->vdsp.emulated_edge_mc(ubuf, srcU,
346                                          2 * s->uvlinesize, 2 * s->uvlinesize,
347                                          8 + 1, 5,
348                                          uvsrc_x, uvsrc_y >> 1,
349                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) + 1 >> 1);
350                 s->vdsp.emulated_edge_mc(ubuf + s->uvlinesize, srcU + s->uvlinesize,
351                                          2 * s->uvlinesize, 2 * s->uvlinesize,
352                                          8 + 1, 4,
353                                          uvsrc_x, uvsrc_y + 1 >> 1,
354                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) >> 1);
355                 s->vdsp.emulated_edge_mc(vbuf, srcV,
356                                          2 * s->uvlinesize, 2 * s->uvlinesize,
357                                          8 + 1, 5,
358                                          uvsrc_x, uvsrc_y >> 1,
359                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) + 1 >> 1);
360                 s->vdsp.emulated_edge_mc(vbuf + s->uvlinesize, srcV + s->uvlinesize,
361                                          2 * s->uvlinesize, 2 * s->uvlinesize,
362                                          8 + 1, 4,
363                                          uvsrc_x, uvsrc_y + 1 >> 1,
364                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) >> 1);
365             } else {
366                 s->vdsp.emulated_edge_mc(ubuf, srcU,
367                                          2 * s->uvlinesize, 2 * s->uvlinesize,
368                                          8 + 1, 5,
369                                          uvsrc_x, uvsrc_y >> 1,
370                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) >> 1);
371                 s->vdsp.emulated_edge_mc(ubuf + s->uvlinesize, srcU + s->uvlinesize,
372                                          2 * s->uvlinesize, 2 * s->uvlinesize,
373                                          8 + 1, 4,
374                                          uvsrc_x, uvsrc_y + 1 >> 1,
375                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) + 1 >> 1);
376                 s->vdsp.emulated_edge_mc(vbuf, srcV,
377                                          2 * s->uvlinesize, 2 * s->uvlinesize,
378                                          8 + 1, 5,
379                                          uvsrc_x, uvsrc_y >> 1,
380                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) >> 1);
381                 s->vdsp.emulated_edge_mc(vbuf + s->uvlinesize, srcV + s->uvlinesize,
382                                          2 * s->uvlinesize, 2 * s->uvlinesize,
383                                          8 + 1, 4,
384                                          uvsrc_x, uvsrc_y + 1 >> 1,
385                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) + 1 >> 1);
386             }
387         } else {
388             s->vdsp.emulated_edge_mc(ubuf, srcU,
389                                      s->uvlinesize, s->uvlinesize,
390                                      8 + 1, 8 + 1,
391                                      uvsrc_x, uvsrc_y,
392                                      s->h_edge_pos >> 1, v_edge_pos >> 1);
393             s->vdsp.emulated_edge_mc(vbuf, srcV,
394                                      s->uvlinesize, s->uvlinesize,
395                                      8 + 1, 8 + 1,
396                                      uvsrc_x, uvsrc_y,
397                                      s->h_edge_pos >> 1, v_edge_pos >> 1);
398         }
399         srcU = ubuf;
400         srcV = vbuf;
401         /* if we deal with range reduction we need to scale source blocks */
402         if (v->rangeredfrm) {
403             vc1_scale_luma(srcY, k, s->linesize);
404             vc1_scale_chroma(srcU, srcV, 9, s->uvlinesize);
405         }
406         /* if we deal with intensity compensation we need to scale source blocks */
407         if (use_ic) {
408             vc1_lut_scale_luma(srcY,
409                                luty[v->field_mode ? v->ref_field_type[dir] : ((0 + src_y - s->mspel) & 1)],
410                                luty[v->field_mode ? v->ref_field_type[dir] : ((1 + src_y - s->mspel) & 1)],
411                                k, s->linesize);
412             vc1_lut_scale_chroma(srcU, srcV,
413                                  lutuv[v->field_mode ? v->ref_field_type[dir] : ((0 + uvsrc_y) & 1)],
414                                  lutuv[v->field_mode ? v->ref_field_type[dir] : ((1 + uvsrc_y) & 1)],
415                                  9, s->uvlinesize);
416         }
417         srcY += s->mspel * (1 + s->linesize);
418     }
419
420     if (s->mspel) {
421         dxy = ((my & 3) << 2) | (mx & 3);
422         v->vc1dsp.put_vc1_mspel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, v->rnd);
423     } else { // hpel mc - always used for luma
424         dxy = (my & 2) | ((mx & 2) >> 1);
425         if (!v->rnd)
426             s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
427         else
428             s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
429     }
430
431     if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
432         return;
433     /* Chroma MC always uses qpel bilinear */
434     uvmx = (uvmx & 3) << 1;
435     uvmy = (uvmy & 3) << 1;
436     if (!v->rnd) {
437         h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
438         h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
439     } else {
440         v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
441         v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
442     }
443     if (v->field_mode) {
444         v->mv_f[dir][s->block_index[4] + v->mb_off] = v->cur_field_type != v->ref_field_type[dir];
445         v->mv_f[dir][s->block_index[5] + v->mb_off] = v->cur_field_type != v->ref_field_type[dir];
446     }
447 }
448
449 /** Do motion compensation for 4-MV macroblock - luminance block
450  */
451 void ff_vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
452 {
453     MpegEncContext *s = &v->s;
454     uint8_t *srcY;
455     int dxy, mx, my, src_x, src_y;
456     int off;
457     int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
458     int v_edge_pos = s->v_edge_pos >> v->field_mode;
459     uint8_t (*luty)[256];
460     int use_ic;
461
462     if ((!v->field_mode ||
463          (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
464         !v->s.last_picture.f->data[0])
465         return;
466
467     mx = s->mv[dir][n][0];
468     my = s->mv[dir][n][1];
469
470     if (!dir) {
471         if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
472             srcY = s->current_picture.f->data[0];
473             luty = v->curr_luty;
474             use_ic = *v->curr_use_ic;
475         } else {
476             srcY = s->last_picture.f->data[0];
477             luty = v->last_luty;
478             use_ic = v->last_use_ic;
479         }
480     } else {
481         srcY = s->next_picture.f->data[0];
482         luty = v->next_luty;
483         use_ic = v->next_use_ic;
484     }
485
486     if (!srcY) {
487         av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
488         return;
489     }
490
491     if (v->field_mode) {
492         if (v->cur_field_type != v->ref_field_type[dir])
493             my = my - 2 + 4 * v->cur_field_type;
494     }
495
496     if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
497         int opp_count = get_luma_mv(v, 0,
498                                     &s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
499                                     &s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1]);
500         int k, f = opp_count > 2;
501         for (k = 0; k < 4; k++)
502             v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
503     }
504
505     if (v->fcm == ILACE_FRAME) {  // not sure if needed for other types of picture
506         int qx, qy;
507         int width  = s->avctx->coded_width;
508         int height = s->avctx->coded_height >> 1;
509         if (s->pict_type == AV_PICTURE_TYPE_P) {
510             s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
511             s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
512         }
513         qx = (s->mb_x * 16) + (mx >> 2);
514         qy = (s->mb_y *  8) + (my >> 3);
515
516         if (qx < -17)
517             mx -= 4 * (qx + 17);
518         else if (qx > width)
519             mx -= 4 * (qx - width);
520         if (qy < -18)
521             my -= 8 * (qy + 18);
522         else if (qy > height + 1)
523             my -= 8 * (qy - height - 1);
524     }
525
526     if ((v->fcm == ILACE_FRAME) && fieldmv)
527         off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
528     else
529         off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
530
531     src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
532     if (!fieldmv)
533         src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
534     else
535         src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
536
537     if (v->profile != PROFILE_ADVANCED) {
538         src_x = av_clip(src_x, -16, s->mb_width  * 16);
539         src_y = av_clip(src_y, -16, s->mb_height * 16);
540     } else {
541         src_x = av_clip(src_x, -17, s->avctx->coded_width);
542         if (v->fcm == ILACE_FRAME)
543             src_y = av_clip(src_y, -18 + (src_y & 1), s->avctx->coded_height + (src_y & 1));
544         else
545             src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
546     }
547
548     srcY += src_y * s->linesize + src_x;
549     if (v->field_mode && v->ref_field_type[dir])
550         srcY += s->current_picture_ptr->f->linesize[0];
551
552     if (v->rangeredfrm || use_ic
553         || s->h_edge_pos < 13 || v_edge_pos < 23
554         || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
555         || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
556         const int k = 9 + s->mspel * 2;
557
558         srcY -= s->mspel * (1 + (s->linesize << fieldmv));
559         /* check emulate edge stride and offset */
560         if (v->fcm == ILACE_FRAME) {
561             if (src_y - (s->mspel << fieldmv) & 1) {
562                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcY,
563                                          2 * s->linesize, 2 * s->linesize,
564                                          k, (k << fieldmv) + 1 >> 1,
565                                          src_x - s->mspel, src_y - (s->mspel << fieldmv) >> 1,
566                                          s->h_edge_pos, v_edge_pos + 1 >> 1);
567                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + s->linesize, srcY + s->linesize,
568                                          2 * s->linesize, 2 * s->linesize,
569                                          k, (k << fieldmv) >> 1,
570                                          src_x - s->mspel, src_y - (s->mspel << fieldmv) + 1 >> 1,
571                                          s->h_edge_pos, v_edge_pos >> 1);
572             } else {
573                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcY,
574                                          2 * s->linesize, 2 * s->linesize,
575                                          k, (k << fieldmv) + 1 >> 1,
576                                          src_x - s->mspel, src_y - (s->mspel << fieldmv) >> 1,
577                                          s->h_edge_pos, v_edge_pos >> 1);
578                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + s->linesize, srcY + s->linesize,
579                                          2 * s->linesize, 2 * s->linesize,
580                                          k, (k << fieldmv) >> 1,
581                                          src_x - s->mspel, src_y - (s->mspel << fieldmv) + 1 >> 1,
582                                          s->h_edge_pos, v_edge_pos + 1 >> 1);
583             }
584         } else
585             s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcY,
586                                      s->linesize, s->linesize,
587                                      k, k,
588                                      src_x - s->mspel, src_y - s->mspel,
589                                      s->h_edge_pos, v_edge_pos);
590         srcY = s->sc.edge_emu_buffer;
591         /* if we deal with range reduction we need to scale source blocks */
592         if (v->rangeredfrm) {
593             vc1_scale_luma(srcY, k, s->linesize << fieldmv);
594         }
595         /* if we deal with intensity compensation we need to scale source blocks */
596         if (use_ic) {
597             vc1_lut_scale_luma(srcY,
598                                luty[v->field_mode ? v->ref_field_type[dir] : (((0<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1)],
599                                luty[v->field_mode ? v->ref_field_type[dir] : (((1<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1)],
600                                k, s->linesize << fieldmv);
601         }
602         srcY += s->mspel * (1 + (s->linesize << fieldmv));
603     }
604
605     if (s->mspel) {
606         dxy = ((my & 3) << 2) | (mx & 3);
607         if (avg)
608             v->vc1dsp.avg_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
609         else
610             v->vc1dsp.put_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
611     } else { // hpel mc - always used for luma
612         dxy = (my & 2) | ((mx & 2) >> 1);
613         if (!v->rnd)
614             s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
615         else
616             s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
617     }
618 }
619
620 /** Do motion compensation for 4-MV macroblock - both chroma blocks
621  */
622 void ff_vc1_mc_4mv_chroma(VC1Context *v, int dir)
623 {
624     MpegEncContext *s = &v->s;
625     H264ChromaContext *h264chroma = &v->h264chroma;
626     uint8_t *srcU, *srcV;
627     int uvmx, uvmy, uvsrc_x, uvsrc_y;
628     int16_t tx, ty;
629     int chroma_ref_type;
630     int v_edge_pos = s->v_edge_pos >> v->field_mode;
631     uint8_t (*lutuv)[256];
632     int use_ic;
633
634     if (!v->field_mode && !v->s.last_picture.f->data[0])
635         return;
636     if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
637         return;
638
639     /* calculate chroma MV vector from four luma MVs */
640     if (!v->field_mode || !v->numref) {
641         int valid_count = get_chroma_mv(v, dir, &tx, &ty);
642         if (!valid_count) {
643             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
644             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
645             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
646             return; //no need to do MC for intra blocks
647         }
648         chroma_ref_type = v->ref_field_type[dir];
649     } else {
650         int opp_count = get_luma_mv(v, dir, &tx, &ty);
651         chroma_ref_type = v->cur_field_type ^ (opp_count > 2);
652     }
653     if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f->data[0])
654         return;
655     s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
656     s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
657     uvmx = (tx + ((tx & 3) == 3)) >> 1;
658     uvmy = (ty + ((ty & 3) == 3)) >> 1;
659
660     v->luma_mv[s->mb_x][0] = uvmx;
661     v->luma_mv[s->mb_x][1] = uvmy;
662
663     if (v->fastuvmc) {
664         uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
665         uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
666     }
667     // Field conversion bias
668     if (v->cur_field_type != chroma_ref_type)
669         uvmy += 2 - 4 * chroma_ref_type;
670
671     uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
672     uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
673
674     if (v->profile != PROFILE_ADVANCED) {
675         uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width  * 8);
676         uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
677     } else {
678         uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width  >> 1);
679         uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
680     }
681
682     if (!dir) {
683         if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
684             srcU = s->current_picture.f->data[1];
685             srcV = s->current_picture.f->data[2];
686             lutuv = v->curr_lutuv;
687             use_ic = *v->curr_use_ic;
688         } else {
689             srcU = s->last_picture.f->data[1];
690             srcV = s->last_picture.f->data[2];
691             lutuv = v->last_lutuv;
692             use_ic = v->last_use_ic;
693         }
694     } else {
695         srcU = s->next_picture.f->data[1];
696         srcV = s->next_picture.f->data[2];
697         lutuv = v->next_lutuv;
698         use_ic = v->next_use_ic;
699     }
700
701     if (!srcU) {
702         av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
703         return;
704     }
705
706     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
707     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
708
709     if (v->field_mode) {
710         if (chroma_ref_type) {
711             srcU += s->current_picture_ptr->f->linesize[1];
712             srcV += s->current_picture_ptr->f->linesize[2];
713         }
714     }
715
716     if (v->rangeredfrm || use_ic
717         || s->h_edge_pos < 18 || v_edge_pos < 18
718         || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
719         || (unsigned)uvsrc_y > (v_edge_pos    >> 1) - 9) {
720         s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcU,
721                                  s->uvlinesize, s->uvlinesize,
722                                  8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
723                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
724         s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16, srcV,
725                                  s->uvlinesize, s->uvlinesize,
726                                  8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
727                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
728         srcU = s->sc.edge_emu_buffer;
729         srcV = s->sc.edge_emu_buffer + 16;
730
731         /* if we deal with range reduction we need to scale source blocks */
732         if (v->rangeredfrm) {
733             vc1_scale_chroma(srcU, srcV, 9, s->uvlinesize);
734         }
735         /* if we deal with intensity compensation we need to scale source blocks */
736         if (use_ic) {
737             vc1_lut_scale_chroma(srcU, srcV,
738                                  lutuv[v->field_mode ? chroma_ref_type : ((0 + uvsrc_y) & 1)],
739                                  lutuv[v->field_mode ? chroma_ref_type : ((1 + uvsrc_y) & 1)],
740                                  9, s->uvlinesize);
741         }
742     }
743
744     /* Chroma MC always uses qpel bilinear */
745     uvmx = (uvmx & 3) << 1;
746     uvmy = (uvmy & 3) << 1;
747     if (!v->rnd) {
748         h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
749         h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
750     } else {
751         v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
752         v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
753     }
754     if (v->field_mode) {
755         v->mv_f[dir][s->block_index[4] + v->mb_off] = v->cur_field_type != chroma_ref_type;
756         v->mv_f[dir][s->block_index[5] + v->mb_off] = v->cur_field_type != chroma_ref_type;
757     }
758 }
759
760 /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
761  */
762 void ff_vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
763 {
764     MpegEncContext *s = &v->s;
765     H264ChromaContext *h264chroma = &v->h264chroma;
766     uint8_t *srcU, *srcV;
767     int uvsrc_x, uvsrc_y;
768     int uvmx_field[4], uvmy_field[4];
769     int i, off, tx, ty;
770     int fieldmv = v->blk_mv_type[s->block_index[0]];
771     static const uint8_t s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
772     int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
773     int v_edge_pos = s->v_edge_pos >> 1;
774     int use_ic;
775     uint8_t (*lutuv)[256];
776
777     if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
778         return;
779
780     for (i = 0; i < 4; i++) {
781         int d = i < 2 ? dir: dir2;
782         tx = s->mv[d][i][0];
783         uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
784         ty = s->mv[d][i][1];
785         if (fieldmv)
786             uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
787         else
788             uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
789     }
790
791     for (i = 0; i < 4; i++) {
792         off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
793         uvsrc_x = s->mb_x * 8 +  (i & 1) * 4           + (uvmx_field[i] >> 2);
794         uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
795         // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
796         uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width  >> 1);
797         if (v->fcm == ILACE_FRAME)
798             uvsrc_y = av_clip(uvsrc_y, -8 + (uvsrc_y & 1), (s->avctx->coded_height >> 1) + (uvsrc_y & 1));
799         else
800             uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
801         if (i < 2 ? dir : dir2) {
802             srcU = s->next_picture.f->data[1];
803             srcV = s->next_picture.f->data[2];
804             lutuv  = v->next_lutuv;
805             use_ic = v->next_use_ic;
806         } else {
807             srcU = s->last_picture.f->data[1];
808             srcV = s->last_picture.f->data[2];
809             lutuv  = v->last_lutuv;
810             use_ic = v->last_use_ic;
811         }
812         if (!srcU)
813             return;
814         srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
815         srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
816         uvmx_field[i] = (uvmx_field[i] & 3) << 1;
817         uvmy_field[i] = (uvmy_field[i] & 3) << 1;
818
819         if (use_ic
820             || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
821             || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
822             || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
823             if (v->fcm == ILACE_FRAME) {
824                 if (uvsrc_y & 1) {
825                     s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcU,
826                                              2 * s->uvlinesize, 2 * s->uvlinesize,
827                                              5, (5 << fieldmv) + 1 >> 1, uvsrc_x, uvsrc_y >> 1,
828                                              s->h_edge_pos >> 1, v_edge_pos + 1 >> 1);
829                     s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + s->uvlinesize, srcU + s->uvlinesize,
830                                              2 * s->uvlinesize, 2 * s->uvlinesize,
831                                              5, (5 << fieldmv) >> 1, uvsrc_x, uvsrc_y + 1 >> 1,
832                                              s->h_edge_pos >> 1, v_edge_pos >> 1);
833                     s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16, srcV,
834                                              2 * s->uvlinesize, 2 * s->uvlinesize,
835                                              5, (5 << fieldmv) + 1 >> 1, uvsrc_x, uvsrc_y >> 1,
836                                              s->h_edge_pos >> 1, v_edge_pos + 1 >> 1);
837                     s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16 + s->uvlinesize, srcV + s->uvlinesize,
838                                              2 * s->uvlinesize, 2 * s->uvlinesize,
839                                              5, (5 << fieldmv) >> 1, uvsrc_x, uvsrc_y + 1 >> 1,
840                                              s->h_edge_pos >> 1, v_edge_pos >> 1);
841                 } else {
842                     s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcU,
843                                              2 * s->uvlinesize, 2 * s->uvlinesize,
844                                              5, (5 << fieldmv) + 1 >> 1, uvsrc_x, uvsrc_y >> 1,
845                                              s->h_edge_pos >> 1, v_edge_pos >> 1);
846                     s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + s->uvlinesize, srcU + s->uvlinesize,
847                                              2 * s->uvlinesize, 2 * s->uvlinesize,
848                                              5, (5 << fieldmv) >> 1, uvsrc_x, uvsrc_y + 1 >> 1,
849                                              s->h_edge_pos >> 1, v_edge_pos + 1 >> 1);
850                     s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16, srcV,
851                                              2 * s->uvlinesize, 2 * s->uvlinesize,
852                                              5, (5 << fieldmv) + 1 >> 1, uvsrc_x, uvsrc_y >> 1,
853                                              s->h_edge_pos >> 1, v_edge_pos >> 1);
854                     s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16 + s->uvlinesize, srcV + s->uvlinesize,
855                                              2 * s->uvlinesize, 2 * s->uvlinesize,
856                                              5, (5 << fieldmv) + 1, uvsrc_x, uvsrc_y + 1 >> 1,
857                                              s->h_edge_pos >> 1, v_edge_pos + 1 >> 1);
858                 }
859             } else {
860                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcU,
861                                          s->uvlinesize, s->uvlinesize,
862                                          5, 5, uvsrc_x, uvsrc_y,
863                                          s->h_edge_pos >> 1, v_edge_pos);
864                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + 16, srcV,
865                                          s->uvlinesize, s->uvlinesize,
866                                          5, 5, uvsrc_x, uvsrc_y,
867                                          s->h_edge_pos >> 1, v_edge_pos);
868             }
869             srcU = s->sc.edge_emu_buffer;
870             srcV = s->sc.edge_emu_buffer + 16;
871
872             /* if we deal with intensity compensation we need to scale source blocks */
873             if (use_ic) {
874                 vc1_lut_scale_chroma(srcU, srcV,
875                                      lutuv[(uvsrc_y + (0 << fieldmv)) & 1],
876                                      lutuv[(uvsrc_y + (1 << fieldmv)) & 1],
877                                      5, s->uvlinesize << fieldmv);
878             }
879         }
880         if (avg) {
881             if (!v->rnd) {
882                 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
883                 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
884             } else {
885                 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]);
886                 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]);
887             }
888         } else {
889             if (!v->rnd) {
890                 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
891                 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
892             } else {
893                 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]);
894                 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]);
895             }
896         }
897     }
898 }
899
900 /** Motion compensation for direct or interpolated blocks in B-frames
901  */
902 void ff_vc1_interp_mc(VC1Context *v)
903 {
904     MpegEncContext *s = &v->s;
905     H264ChromaContext *h264chroma = &v->h264chroma;
906     uint8_t *srcY, *srcU, *srcV;
907     int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
908     int v_edge_pos = s->v_edge_pos >> v->field_mode;
909     int use_ic = v->next_use_ic;
910
911     if (!v->field_mode && !v->s.next_picture.f->data[0])
912         return;
913
914     mx   = s->mv[1][0][0];
915     my   = s->mv[1][0][1];
916     uvmx = (mx + ((mx & 3) == 3)) >> 1;
917     uvmy = (my + ((my & 3) == 3)) >> 1;
918     if (v->field_mode && v->cur_field_type != v->ref_field_type[1]) {
919         my   = my   - 2 + 4 * v->cur_field_type;
920         uvmy = uvmy - 2 + 4 * v->cur_field_type;
921     }
922     if (v->fastuvmc) {
923         uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
924         uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
925     }
926     srcY = s->next_picture.f->data[0];
927     srcU = s->next_picture.f->data[1];
928     srcV = s->next_picture.f->data[2];
929
930     src_x   = s->mb_x * 16 + (mx   >> 2);
931     src_y   = s->mb_y * 16 + (my   >> 2);
932     uvsrc_x = s->mb_x *  8 + (uvmx >> 2);
933     uvsrc_y = s->mb_y *  8 + (uvmy >> 2);
934
935     if (v->profile != PROFILE_ADVANCED) {
936         src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
937         src_y   = av_clip(  src_y, -16, s->mb_height * 16);
938         uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
939         uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
940     } else {
941         src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
942         uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
943         if (v->fcm == ILACE_FRAME) {
944             src_y = av_clip(src_y, -18 + (src_y & 1), s->avctx->coded_height + (src_y & 1));
945             uvsrc_y = av_clip(uvsrc_y, -8 + (uvsrc_y & 1), (s->avctx->coded_height >> 1) + (uvsrc_y & 1));
946         } else {
947             src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
948             uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
949         }
950     }
951
952     srcY += src_y   * s->linesize   + src_x;
953     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
954     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
955
956     if (v->field_mode && v->ref_field_type[1]) {
957         srcY += s->current_picture_ptr->f->linesize[0];
958         srcU += s->current_picture_ptr->f->linesize[1];
959         srcV += s->current_picture_ptr->f->linesize[2];
960     }
961
962     /* for grayscale we should not try to read from unknown area */
963     if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY) {
964         srcU = s->sc.edge_emu_buffer + 18 * s->linesize;
965         srcV = s->sc.edge_emu_buffer + 18 * s->linesize;
966     }
967
968     if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
969         || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
970         || (unsigned)(src_y - 1) > v_edge_pos    - (my & 3) - 16 - 3) {
971         uint8_t *ubuf = s->sc.edge_emu_buffer + 19 * s->linesize;
972         uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
973         const int k = 17 + s->mspel * 2;
974
975         srcY -= s->mspel * (1 + s->linesize);
976         if (v->fcm == ILACE_FRAME) {
977             if (src_y - s->mspel & 1) {
978                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
979                                          srcY,
980                                          2 * s->linesize,
981                                          2 * s->linesize,
982                                          k,
983                                          k + 1 >> 1,
984                                          src_x - s->mspel,
985                                          src_y - s->mspel >> 1,
986                                          s->h_edge_pos,
987                                          v_edge_pos + 1 >> 1);
988                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + s->linesize,
989                                          srcY + s->linesize,
990                                          2 * s->linesize,
991                                          2 * s->linesize,
992                                          k,
993                                          k >> 1,
994                                          src_x - s->mspel,
995                                          src_y - s->mspel + 1 >> 1,
996                                          s->h_edge_pos,
997                                          v_edge_pos >> 1);
998             } else {
999                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer,
1000                                          srcY,
1001                                          2 * s->linesize,
1002                                          2 * s->linesize,
1003                                          k,
1004                                          k + 1 >> 1,
1005                                          src_x - s->mspel,
1006                                          src_y - s->mspel >> 1,
1007                                          s->h_edge_pos,
1008                                          v_edge_pos >> 1);
1009                 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer + s->linesize,
1010                                          srcY + s->linesize,
1011                                          2 * s->linesize,
1012                                          2 * s->linesize,
1013                                          k,
1014                                          k >> 1,
1015                                          src_x - s->mspel,
1016                                          src_y - s->mspel + 1 >> 1,
1017                                          s->h_edge_pos,
1018                                          v_edge_pos + 1 >> 1);
1019             }
1020         } else
1021             s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcY,
1022                                      s->linesize, s->linesize,
1023                                      k, k,
1024                                      src_x - s->mspel, src_y - s->mspel,
1025                                      s->h_edge_pos, v_edge_pos);
1026         srcY = s->sc.edge_emu_buffer;
1027         if (v->fcm == ILACE_FRAME) {
1028             if (uvsrc_y & 1) {
1029                 s->vdsp.emulated_edge_mc(ubuf, srcU,
1030                                          2 * s->uvlinesize, 2 * s->uvlinesize,
1031                                          8 + 1, 5,
1032                                          uvsrc_x, uvsrc_y >> 1,
1033                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) + 1 >> 1);
1034                 s->vdsp.emulated_edge_mc(ubuf + s->uvlinesize, srcU + s->uvlinesize,
1035                                          2 * s->uvlinesize, 2 * s->uvlinesize,
1036                                          8 + 1, 4,
1037                                          uvsrc_x, uvsrc_y + 1 >> 1,
1038                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) >> 1);
1039                 s->vdsp.emulated_edge_mc(vbuf, srcV,
1040                                          2 * s->uvlinesize, 2 * s->uvlinesize,
1041                                          8 + 1, 5,
1042                                          uvsrc_x, uvsrc_y >> 1,
1043                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) + 1 >> 1);
1044                 s->vdsp.emulated_edge_mc(vbuf + s->uvlinesize, srcV + s->uvlinesize,
1045                                          2 * s->uvlinesize, 2 * s->uvlinesize,
1046                                          8 + 1, 4,
1047                                          uvsrc_x, uvsrc_y + 1 >> 1,
1048                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) >> 1);
1049             } else {
1050                 s->vdsp.emulated_edge_mc(ubuf, srcU,
1051                                          2 * s->uvlinesize, 2 * s->uvlinesize,
1052                                          8 + 1, 5,
1053                                          uvsrc_x, uvsrc_y >> 1,
1054                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) >> 1);
1055                 s->vdsp.emulated_edge_mc(ubuf + s->uvlinesize, srcU + s->uvlinesize,
1056                                          2 * s->uvlinesize, 2 * s->uvlinesize,
1057                                          8 + 1, 4,
1058                                          uvsrc_x, uvsrc_y + 1 >> 1,
1059                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) + 1 >> 1);
1060                 s->vdsp.emulated_edge_mc(vbuf, srcV,
1061                                          2 * s->uvlinesize, 2 * s->uvlinesize,
1062                                          8 + 1, 5,
1063                                          uvsrc_x, uvsrc_y >> 1,
1064                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) >> 1);
1065                 s->vdsp.emulated_edge_mc(vbuf + s->uvlinesize, srcV + s->uvlinesize,
1066                                          2 * s->uvlinesize, 2 * s->uvlinesize,
1067                                          8 + 1, 4,
1068                                          uvsrc_x, uvsrc_y + 1 >> 1,
1069                                          s->h_edge_pos >> 1, (v_edge_pos >> 1) + 1 >> 1);
1070             }
1071         } else {
1072             s->vdsp.emulated_edge_mc(ubuf, srcU,
1073                                      s->uvlinesize, s->uvlinesize,
1074                                      8 + 1, 8 + 1,
1075                                      uvsrc_x, uvsrc_y,
1076                                      s->h_edge_pos >> 1, v_edge_pos >> 1);
1077             s->vdsp.emulated_edge_mc(vbuf, srcV,
1078                                      s->uvlinesize, s->uvlinesize,
1079                                      8 + 1, 8 + 1,
1080                                      uvsrc_x, uvsrc_y,
1081                                      s->h_edge_pos >> 1, v_edge_pos >> 1);
1082         }
1083         srcU = ubuf;
1084         srcV = vbuf;
1085         /* if we deal with range reduction we need to scale source blocks */
1086         if (v->rangeredfrm) {
1087             vc1_scale_luma(srcY, k, s->linesize);
1088             vc1_scale_chroma(srcU, srcV, 9, s->uvlinesize);
1089         }
1090
1091         if (use_ic) {
1092             uint8_t (*luty )[256] = v->next_luty;
1093             uint8_t (*lutuv)[256] = v->next_lutuv;
1094             vc1_lut_scale_luma(srcY,
1095                                luty[v->field_mode ? v->ref_field_type[1] : ((0+src_y - s->mspel) & 1)],
1096                                luty[v->field_mode ? v->ref_field_type[1] : ((1+src_y - s->mspel) & 1)],
1097                                k, s->linesize);
1098             vc1_lut_scale_chroma(srcU, srcV,
1099                                  lutuv[v->field_mode ? v->ref_field_type[1] : ((0+uvsrc_y) & 1)],
1100                                  lutuv[v->field_mode ? v->ref_field_type[1] : ((1+uvsrc_y) & 1)],
1101                                  9, s->uvlinesize);
1102         }
1103         srcY += s->mspel * (1 + s->linesize);
1104     }
1105
1106     if (s->mspel) {
1107         dxy = ((my & 3) << 2) | (mx & 3);
1108         v->vc1dsp.avg_vc1_mspel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, v->rnd);
1109     } else { // hpel mc
1110         dxy = (my & 2) | ((mx & 2) >> 1);
1111
1112         if (!v->rnd)
1113             s->hdsp.avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
1114         else
1115             s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0], srcY, s->linesize, 16);
1116     }
1117
1118     if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
1119         return;
1120     /* Chroma MC always uses qpel bilinear */
1121     uvmx = (uvmx & 3) << 1;
1122     uvmy = (uvmy & 3) << 1;
1123     if (!v->rnd) {
1124         h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1125         h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1126     } else {
1127         v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1128         v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1129     }
1130 }