]> git.sesse.net Git - ffmpeg/blob - libavcodec/vc1_mc.c
Merge commit 'aef0be08756e00f363c524453c948a6e2a348614'
[ffmpeg] / 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         src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
258         uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
259         uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
260     }
261
262     srcY += src_y   * s->linesize   + src_x;
263     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
264     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
265
266     if (v->field_mode && v->ref_field_type[dir]) {
267         srcY += s->current_picture_ptr->f->linesize[0];
268         srcU += s->current_picture_ptr->f->linesize[1];
269         srcV += s->current_picture_ptr->f->linesize[2];
270     }
271
272     /* for grayscale we should not try to read from unknown area */
273     if (s->flags & CODEC_FLAG_GRAY) {
274         srcU = s->edge_emu_buffer + 18 * s->linesize;
275         srcV = s->edge_emu_buffer + 18 * s->linesize;
276     }
277
278     if (v->rangeredfrm || use_ic
279         || s->h_edge_pos < 22 || v_edge_pos < 22
280         || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
281         || (unsigned)(src_y - 1)        > v_edge_pos    - (my&3) - 16 - 3) {
282         uint8_t *ubuf = s->edge_emu_buffer + 19 * s->linesize;
283         uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
284         const int k = 17 + s->mspel * 2;
285
286         srcY -= s->mspel * (1 + s->linesize);
287         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
288                                  s->linesize, s->linesize,
289                                  k, k,
290                                  src_x - s->mspel, src_y - s->mspel,
291                                  s->h_edge_pos, v_edge_pos);
292         srcY = s->edge_emu_buffer;
293         s->vdsp.emulated_edge_mc(ubuf, srcU,
294                                  s->uvlinesize, s->uvlinesize,
295                                  8 + 1, 8 + 1,
296                                  uvsrc_x, uvsrc_y,
297                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
298         s->vdsp.emulated_edge_mc(vbuf, srcV,
299                                  s->uvlinesize, s->uvlinesize,
300                                  8 + 1, 8 + 1,
301                                  uvsrc_x, uvsrc_y,
302                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
303         srcU = ubuf;
304         srcV = vbuf;
305         /* if we deal with range reduction we need to scale source blocks */
306         if (v->rangeredfrm) {
307             vc1_scale_luma(srcY, k, s->linesize);
308             vc1_scale_chroma(srcU, srcV, 9, s->uvlinesize);
309         }
310         /* if we deal with intensity compensation we need to scale source blocks */
311         if (use_ic) {
312             vc1_lut_scale_luma(srcY,
313                                luty[v->field_mode ? v->ref_field_type[dir] : ((0 + src_y - s->mspel) & 1)],
314                                luty[v->field_mode ? v->ref_field_type[dir] : ((1 + src_y - s->mspel) & 1)],
315                                k, s->linesize);
316             vc1_lut_scale_chroma(srcU, srcV,
317                                  lutuv[v->field_mode ? v->ref_field_type[dir] : ((0 + uvsrc_y) & 1)],
318                                  lutuv[v->field_mode ? v->ref_field_type[dir] : ((1 + uvsrc_y) & 1)],
319                                  9, s->uvlinesize);
320         }
321         srcY += s->mspel * (1 + s->linesize);
322     }
323
324     if (s->mspel) {
325         dxy = ((my & 3) << 2) | (mx & 3);
326         v->vc1dsp.put_vc1_mspel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, v->rnd);
327     } else { // hpel mc - always used for luma
328         dxy = (my & 2) | ((mx & 2) >> 1);
329         if (!v->rnd)
330             s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
331         else
332             s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
333     }
334
335     if (s->flags & CODEC_FLAG_GRAY) return;
336     /* Chroma MC always uses qpel bilinear */
337     uvmx = (uvmx & 3) << 1;
338     uvmy = (uvmy & 3) << 1;
339     if (!v->rnd) {
340         h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
341         h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
342     } else {
343         v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
344         v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
345     }
346 }
347
348 /** Do motion compensation for 4-MV macroblock - luminance block
349  */
350 void ff_vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
351 {
352     MpegEncContext *s = &v->s;
353     uint8_t *srcY;
354     int dxy, mx, my, src_x, src_y;
355     int off;
356     int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
357     int v_edge_pos = s->v_edge_pos >> v->field_mode;
358     uint8_t (*luty)[256];
359     int use_ic;
360
361     if ((!v->field_mode ||
362          (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
363         !v->s.last_picture.f->data[0])
364         return;
365
366     mx = s->mv[dir][n][0];
367     my = s->mv[dir][n][1];
368
369     if (!dir) {
370         if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
371             srcY = s->current_picture.f->data[0];
372             luty = v->curr_luty;
373             use_ic = *v->curr_use_ic;
374         } else {
375             srcY = s->last_picture.f->data[0];
376             luty = v->last_luty;
377             use_ic = v->last_use_ic;
378         }
379     } else {
380         srcY = s->next_picture.f->data[0];
381         luty = v->next_luty;
382         use_ic = v->next_use_ic;
383     }
384
385     if (!srcY) {
386         av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
387         return;
388     }
389
390     if (v->field_mode) {
391         if (v->cur_field_type != v->ref_field_type[dir])
392             my = my - 2 + 4 * v->cur_field_type;
393     }
394
395     if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
396         int opp_count = get_luma_mv(v, 0,
397                                     &s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
398                                     &s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1]);
399         int k, f = opp_count > 2;
400         for (k = 0; k < 4; k++)
401             v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
402     }
403
404     if (v->fcm == ILACE_FRAME) {  // not sure if needed for other types of picture
405         int qx, qy;
406         int width  = s->avctx->coded_width;
407         int height = s->avctx->coded_height >> 1;
408         if (s->pict_type == AV_PICTURE_TYPE_P) {
409             s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
410             s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
411         }
412         qx = (s->mb_x * 16) + (mx >> 2);
413         qy = (s->mb_y *  8) + (my >> 3);
414
415         if (qx < -17)
416             mx -= 4 * (qx + 17);
417         else if (qx > width)
418             mx -= 4 * (qx - width);
419         if (qy < -18)
420             my -= 8 * (qy + 18);
421         else if (qy > height + 1)
422             my -= 8 * (qy - height - 1);
423     }
424
425     if ((v->fcm == ILACE_FRAME) && fieldmv)
426         off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
427     else
428         off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
429
430     src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
431     if (!fieldmv)
432         src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
433     else
434         src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
435
436     if (v->profile != PROFILE_ADVANCED) {
437         src_x = av_clip(src_x, -16, s->mb_width  * 16);
438         src_y = av_clip(src_y, -16, s->mb_height * 16);
439     } else {
440         src_x = av_clip(src_x, -17, s->avctx->coded_width);
441         if (v->fcm == ILACE_FRAME) {
442             if (src_y & 1)
443                 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
444             else
445                 src_y = av_clip(src_y, -18, s->avctx->coded_height);
446         } else {
447             src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
448         }
449     }
450
451     srcY += src_y * s->linesize + src_x;
452     if (v->field_mode && v->ref_field_type[dir])
453         srcY += s->current_picture_ptr->f->linesize[0];
454
455     if (fieldmv) {
456         if (!(src_y & 1))
457             v_edge_pos--;
458         else
459             src_y -= (src_y < 4);
460     }
461     if (v->rangeredfrm || use_ic
462         || s->h_edge_pos < 13 || v_edge_pos < 23
463         || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
464         || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
465         const int k = 9 + s->mspel * 2;
466
467         srcY -= s->mspel * (1 + (s->linesize << fieldmv));
468         /* check emulate edge stride and offset */
469         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
470                                  s->linesize, s->linesize,
471                                  k, k << fieldmv,
472                                  src_x - s->mspel, src_y - (s->mspel << fieldmv),
473                                  s->h_edge_pos, v_edge_pos);
474         srcY = s->edge_emu_buffer;
475         /* if we deal with range reduction we need to scale source blocks */
476         if (v->rangeredfrm) {
477             vc1_scale_luma(srcY, k, s->linesize << fieldmv);
478         }
479         /* if we deal with intensity compensation we need to scale source blocks */
480         if (use_ic) {
481             vc1_lut_scale_luma(srcY,
482                                luty[v->field_mode ? v->ref_field_type[dir] : (((0<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1)],
483                                luty[v->field_mode ? v->ref_field_type[dir] : (((1<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1)],
484                                k, s->linesize << fieldmv);
485         }
486         srcY += s->mspel * (1 + (s->linesize << fieldmv));
487     }
488
489     if (s->mspel) {
490         dxy = ((my & 3) << 2) | (mx & 3);
491         if (avg)
492             v->vc1dsp.avg_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
493         else
494             v->vc1dsp.put_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
495     } else { // hpel mc - always used for luma
496         dxy = (my & 2) | ((mx & 2) >> 1);
497         if (!v->rnd)
498             s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
499         else
500             s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
501     }
502 }
503
504 /** Do motion compensation for 4-MV macroblock - both chroma blocks
505  */
506 void ff_vc1_mc_4mv_chroma(VC1Context *v, int dir)
507 {
508     MpegEncContext *s = &v->s;
509     H264ChromaContext *h264chroma = &v->h264chroma;
510     uint8_t *srcU, *srcV;
511     int uvmx, uvmy, uvsrc_x, uvsrc_y;
512     int16_t tx, ty;
513     int chroma_ref_type;
514     int v_edge_pos = s->v_edge_pos >> v->field_mode;
515     uint8_t (*lutuv)[256];
516     int use_ic;
517
518     if (!v->field_mode && !v->s.last_picture.f->data[0])
519         return;
520     if (s->flags & CODEC_FLAG_GRAY)
521         return;
522
523     /* calculate chroma MV vector from four luma MVs */
524     if (!v->field_mode || !v->numref) {
525         int valid_count = get_chroma_mv(v, dir, &tx, &ty);
526         if (!valid_count) {
527             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
528             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
529             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
530             return; //no need to do MC for intra blocks
531         }
532         chroma_ref_type = v->ref_field_type[dir];
533     } else {
534         int opp_count = get_luma_mv(v, dir, &tx, &ty);
535         chroma_ref_type = v->cur_field_type ^ (opp_count > 2);
536     }
537     if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f->data[0])
538         return;
539     s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
540     s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
541     uvmx = (tx + ((tx & 3) == 3)) >> 1;
542     uvmy = (ty + ((ty & 3) == 3)) >> 1;
543
544     v->luma_mv[s->mb_x][0] = uvmx;
545     v->luma_mv[s->mb_x][1] = uvmy;
546
547     if (v->fastuvmc) {
548         uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
549         uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
550     }
551     // Field conversion bias
552     if (v->cur_field_type != chroma_ref_type)
553         uvmy += 2 - 4 * chroma_ref_type;
554
555     uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
556     uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
557
558     if (v->profile != PROFILE_ADVANCED) {
559         uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width  * 8);
560         uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
561     } else {
562         uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width  >> 1);
563         uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
564     }
565
566     if (!dir) {
567         if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
568             srcU = s->current_picture.f->data[1];
569             srcV = s->current_picture.f->data[2];
570             lutuv = v->curr_lutuv;
571             use_ic = *v->curr_use_ic;
572         } else {
573             srcU = s->last_picture.f->data[1];
574             srcV = s->last_picture.f->data[2];
575             lutuv = v->last_lutuv;
576             use_ic = v->last_use_ic;
577         }
578     } else {
579         srcU = s->next_picture.f->data[1];
580         srcV = s->next_picture.f->data[2];
581         lutuv = v->next_lutuv;
582         use_ic = v->next_use_ic;
583     }
584
585     if (!srcU) {
586         av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
587         return;
588     }
589
590     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
591     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
592
593     if (v->field_mode) {
594         if (chroma_ref_type) {
595             srcU += s->current_picture_ptr->f->linesize[1];
596             srcV += s->current_picture_ptr->f->linesize[2];
597         }
598     }
599
600     if (v->rangeredfrm || use_ic
601         || s->h_edge_pos < 18 || v_edge_pos < 18
602         || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
603         || (unsigned)uvsrc_y > (v_edge_pos    >> 1) - 9) {
604         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
605                                  s->uvlinesize, s->uvlinesize,
606                                  8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
607                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
608         s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
609                                  s->uvlinesize, s->uvlinesize,
610                                  8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
611                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
612         srcU = s->edge_emu_buffer;
613         srcV = s->edge_emu_buffer + 16;
614
615         /* if we deal with range reduction we need to scale source blocks */
616         if (v->rangeredfrm) {
617             vc1_scale_chroma(srcU, srcV, 9, s->uvlinesize);
618         }
619         /* if we deal with intensity compensation we need to scale source blocks */
620         if (use_ic) {
621             vc1_lut_scale_chroma(srcU, srcV,
622                                  lutuv[v->field_mode ? chroma_ref_type : ((0 + uvsrc_y) & 1)],
623                                  lutuv[v->field_mode ? chroma_ref_type : ((1 + uvsrc_y) & 1)],
624                                  9, s->uvlinesize);
625         }
626     }
627
628     /* Chroma MC always uses qpel bilinear */
629     uvmx = (uvmx & 3) << 1;
630     uvmy = (uvmy & 3) << 1;
631     if (!v->rnd) {
632         h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
633         h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
634     } else {
635         v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
636         v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
637     }
638 }
639
640 /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
641  */
642 void ff_vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
643 {
644     MpegEncContext *s = &v->s;
645     H264ChromaContext *h264chroma = &v->h264chroma;
646     uint8_t *srcU, *srcV;
647     int uvsrc_x, uvsrc_y;
648     int uvmx_field[4], uvmy_field[4];
649     int i, off, tx, ty;
650     int fieldmv = v->blk_mv_type[s->block_index[0]];
651     static const uint8_t s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
652     int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
653     int v_edge_pos = s->v_edge_pos >> 1;
654     int use_ic;
655     uint8_t (*lutuv)[256];
656
657     if (s->flags & CODEC_FLAG_GRAY)
658         return;
659
660     for (i = 0; i < 4; i++) {
661         int d = i < 2 ? dir: dir2;
662         tx = s->mv[d][i][0];
663         uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
664         ty = s->mv[d][i][1];
665         if (fieldmv)
666             uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
667         else
668             uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
669     }
670
671     for (i = 0; i < 4; i++) {
672         off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
673         uvsrc_x = s->mb_x * 8 +  (i & 1) * 4           + (uvmx_field[i] >> 2);
674         uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
675         // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
676         uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width  >> 1);
677         uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
678         if (i < 2 ? dir : dir2) {
679             srcU = s->next_picture.f->data[1];
680             srcV = s->next_picture.f->data[2];
681             lutuv  = v->next_lutuv;
682             use_ic = v->next_use_ic;
683         } else {
684             srcU = s->last_picture.f->data[1];
685             srcV = s->last_picture.f->data[2];
686             lutuv  = v->last_lutuv;
687             use_ic = v->last_use_ic;
688         }
689         if (!srcU)
690             return;
691         srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
692         srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
693         uvmx_field[i] = (uvmx_field[i] & 3) << 1;
694         uvmy_field[i] = (uvmy_field[i] & 3) << 1;
695
696         if (fieldmv) {
697             if (!(uvsrc_y & 1))
698                 v_edge_pos = (s->v_edge_pos >> 1) - 1;
699             else
700                 uvsrc_y -= (uvsrc_y < 2);
701         }
702         if (use_ic
703             || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
704             || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
705             || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
706             s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
707                                      s->uvlinesize, s->uvlinesize,
708                                      5, (5 << fieldmv), uvsrc_x, uvsrc_y,
709                                      s->h_edge_pos >> 1, v_edge_pos);
710             s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
711                                      s->uvlinesize, s->uvlinesize,
712                                      5, (5 << fieldmv), uvsrc_x, uvsrc_y,
713                                      s->h_edge_pos >> 1, v_edge_pos);
714             srcU = s->edge_emu_buffer;
715             srcV = s->edge_emu_buffer + 16;
716
717             /* if we deal with intensity compensation we need to scale source blocks */
718             if (use_ic) {
719                 vc1_lut_scale_chroma(srcU, srcV,
720                                      lutuv[(uvsrc_y + (0 << fieldmv)) & 1],
721                                      lutuv[(uvsrc_y + (1 << fieldmv)) & 1],
722                                      5, s->uvlinesize << fieldmv);
723             }
724         }
725         if (avg) {
726             if (!v->rnd) {
727                 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
728                 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
729             } else {
730                 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]);
731                 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]);
732             }
733         } else {
734             if (!v->rnd) {
735                 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
736                 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
737             } else {
738                 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]);
739                 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]);
740             }
741         }
742     }
743 }
744
745 /** Motion compensation for direct or interpolated blocks in B-frames
746  */
747 void ff_vc1_interp_mc(VC1Context *v)
748 {
749     MpegEncContext *s = &v->s;
750     H264ChromaContext *h264chroma = &v->h264chroma;
751     uint8_t *srcY, *srcU, *srcV;
752     int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
753     int v_edge_pos = s->v_edge_pos >> v->field_mode;
754     int use_ic = v->next_use_ic;
755
756     if (!v->field_mode && !v->s.next_picture.f->data[0])
757         return;
758
759     mx   = s->mv[1][0][0];
760     my   = s->mv[1][0][1];
761     uvmx = (mx + ((mx & 3) == 3)) >> 1;
762     uvmy = (my + ((my & 3) == 3)) >> 1;
763     if (v->field_mode && v->cur_field_type != v->ref_field_type[1]) {
764         my   = my   - 2 + 4 * v->cur_field_type;
765         uvmy = uvmy - 2 + 4 * v->cur_field_type;
766     }
767     if (v->fastuvmc) {
768         uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
769         uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
770     }
771     srcY = s->next_picture.f->data[0];
772     srcU = s->next_picture.f->data[1];
773     srcV = s->next_picture.f->data[2];
774
775     src_x   = s->mb_x * 16 + (mx   >> 2);
776     src_y   = s->mb_y * 16 + (my   >> 2);
777     uvsrc_x = s->mb_x *  8 + (uvmx >> 2);
778     uvsrc_y = s->mb_y *  8 + (uvmy >> 2);
779
780     if (v->profile != PROFILE_ADVANCED) {
781         src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
782         src_y   = av_clip(  src_y, -16, s->mb_height * 16);
783         uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
784         uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
785     } else {
786         src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
787         src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
788         uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
789         uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
790     }
791
792     srcY += src_y   * s->linesize   + src_x;
793     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
794     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
795
796     if (v->field_mode && v->ref_field_type[1]) {
797         srcY += s->current_picture_ptr->f->linesize[0];
798         srcU += s->current_picture_ptr->f->linesize[1];
799         srcV += s->current_picture_ptr->f->linesize[2];
800     }
801
802     /* for grayscale we should not try to read from unknown area */
803     if (s->flags & CODEC_FLAG_GRAY) {
804         srcU = s->edge_emu_buffer + 18 * s->linesize;
805         srcV = s->edge_emu_buffer + 18 * s->linesize;
806     }
807
808     if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
809         || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
810         || (unsigned)(src_y - 1) > v_edge_pos    - (my & 3) - 16 - 3) {
811         uint8_t *ubuf = s->edge_emu_buffer + 19 * s->linesize;
812         uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
813         const int k = 17 + s->mspel * 2;
814
815         srcY -= s->mspel * (1 + s->linesize);
816         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
817                                  s->linesize, s->linesize,
818                                  k, k,
819                                  src_x - s->mspel, src_y - s->mspel,
820                                  s->h_edge_pos, v_edge_pos);
821         srcY = s->edge_emu_buffer;
822         s->vdsp.emulated_edge_mc(ubuf, srcU,
823                                  s->uvlinesize, s->uvlinesize,
824                                  8 + 1, 8 + 1,
825                                  uvsrc_x, uvsrc_y,
826                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
827         s->vdsp.emulated_edge_mc(vbuf, srcV,
828                                  s->uvlinesize, s->uvlinesize,
829                                  8 + 1, 8 + 1,
830                                  uvsrc_x, uvsrc_y,
831                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
832         srcU = ubuf;
833         srcV = vbuf;
834         /* if we deal with range reduction we need to scale source blocks */
835         if (v->rangeredfrm) {
836             vc1_scale_luma(srcY, k, s->linesize);
837             vc1_scale_chroma(srcU, srcV, 9, s->uvlinesize);
838         }
839
840         if (use_ic) {
841             uint8_t (*luty )[256] = v->next_luty;
842             uint8_t (*lutuv)[256] = v->next_lutuv;
843             vc1_lut_scale_luma(srcY,
844                                luty[v->field_mode ? v->ref_field_type[1] : ((0+src_y - s->mspel) & 1)],
845                                luty[v->field_mode ? v->ref_field_type[1] : ((1+src_y - s->mspel) & 1)],
846                                k, s->linesize);
847             vc1_lut_scale_chroma(srcU, srcV,
848                                  lutuv[v->field_mode ? v->ref_field_type[1] : ((0+uvsrc_y) & 1)],
849                                  lutuv[v->field_mode ? v->ref_field_type[1] : ((1+uvsrc_y) & 1)],
850                                  9, s->uvlinesize);
851         }
852         srcY += s->mspel * (1 + s->linesize);
853     }
854
855     if (s->mspel) {
856         dxy = ((my & 3) << 2) | (mx & 3);
857         v->vc1dsp.avg_vc1_mspel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, v->rnd);
858     } else { // hpel mc
859         dxy = (my & 2) | ((mx & 2) >> 1);
860
861         if (!v->rnd)
862             s->hdsp.avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
863         else
864             s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0], srcY, s->linesize, 16);
865     }
866
867     if (s->flags & CODEC_FLAG_GRAY) return;
868     /* Chroma MC always uses qpel blilinear */
869     uvmx = (uvmx & 3) << 1;
870     uvmy = (uvmy & 3) << 1;
871     if (!v->rnd) {
872         h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
873         h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
874     } else {
875         v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
876         v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
877     }
878 }