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