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