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
7 * This file is part of FFmpeg.
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.
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.
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
26 * VC-1 and WMV3 block decoding routines
30 #include "h264chroma.h"
32 #include "mpegvideo.h"
35 static av_always_inline void vc1_scale_luma(uint8_t *srcY,
39 for (j = 0; j < k; j++) {
40 for (i = 0; i < k; i++)
41 srcY[i] = ((srcY[i] - 128) >> 1) + 128;
46 static av_always_inline void vc1_scale_chroma(uint8_t *srcU, uint8_t *srcV,
47 int k, int uvlinesize)
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;
60 static av_always_inline void vc1_lut_scale_luma(uint8_t *srcY,
61 uint8_t *lut1, uint8_t *lut2,
66 for (j = 0; j < k; j += 2) {
67 for (i = 0; i < k; i++)
68 srcY[i] = lut1[srcY[i]];
74 for (i = 0; i < k; i++)
75 srcY[i] = lut2[srcY[i]];
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)
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]];
97 for (i = 0; i < k; i++) {
98 srcU[i] = lut2[srcU[i]];
99 srcV[i] = lut2[srcV[i]];
106 static const uint8_t popcount4[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
108 static av_always_inline int get_luma_mv(VC1Context *v, int dir, int16_t *tx, int16_t *ty)
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];
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]);
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]);
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]);
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;
140 static av_always_inline int get_chroma_mv(VC1Context *v, int dir, int16_t *tx, int16_t *ty)
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];
150 switch (valid_count) {
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]);
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]);
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;
169 /** Do motion compensation over 1 macroblock
170 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
172 void ff_vc1_mc_1mv(VC1Context *v, int dir)
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;
180 uint8_t (*luty)[256], (*lutuv)[256];
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])
188 mx = s->mv[dir][0][0];
189 my = s->mv[dir][0][1];
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;
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;
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;
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));
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];
221 lutuv = v->curr_lutuv;
222 use_ic = *v->curr_use_ic;
224 srcY = s->last_picture.f->data[0];
225 srcU = s->last_picture.f->data[1];
226 srcV = s->last_picture.f->data[2];
228 lutuv = v->last_lutuv;
229 use_ic = v->last_use_ic;
232 srcY = s->next_picture.f->data[0];
233 srcU = s->next_picture.f->data[1];
234 srcV = s->next_picture.f->data[2];
236 lutuv = v->next_lutuv;
237 use_ic = v->next_use_ic;
240 if (!srcY || !srcU) {
241 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
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);
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);
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);
262 srcY += src_y * s->linesize + src_x;
263 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
264 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
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];
272 /* for grayscale we should not try to read from unknown area */
273 if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY) {
274 srcU = s->edge_emu_buffer + 18 * s->linesize;
275 srcV = s->edge_emu_buffer + 18 * s->linesize;
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;
286 srcY -= s->mspel * (1 + s->linesize);
287 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
288 s->linesize, s->linesize,
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,
297 s->h_edge_pos >> 1, v_edge_pos >> 1);
298 s->vdsp.emulated_edge_mc(vbuf, srcV,
299 s->uvlinesize, s->uvlinesize,
302 s->h_edge_pos >> 1, v_edge_pos >> 1);
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);
310 /* if we deal with intensity compensation we need to scale source blocks */
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)],
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)],
321 srcY += s->mspel * (1 + s->linesize);
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);
330 s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
332 s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
335 if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY) return;
336 /* Chroma MC always uses qpel bilinear */
337 uvmx = (uvmx & 3) << 1;
338 uvmy = (uvmy & 3) << 1;
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);
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);
348 /** Do motion compensation for 4-MV macroblock - luminance block
350 void ff_vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
352 MpegEncContext *s = &v->s;
354 int dxy, mx, my, src_x, src_y;
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];
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])
366 mx = s->mv[dir][n][0];
367 my = s->mv[dir][n][1];
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];
373 use_ic = *v->curr_use_ic;
375 srcY = s->last_picture.f->data[0];
377 use_ic = v->last_use_ic;
380 srcY = s->next_picture.f->data[0];
382 use_ic = v->next_use_ic;
386 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
391 if (v->cur_field_type != v->ref_field_type[dir])
392 my = my - 2 + 4 * v->cur_field_type;
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;
404 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
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;
412 qx = (s->mb_x * 16) + (mx >> 2);
413 qy = (s->mb_y * 8) + (my >> 3);
418 mx -= 4 * (qx - width);
421 else if (qy > height + 1)
422 my -= 8 * (qy - height - 1);
425 if ((v->fcm == ILACE_FRAME) && fieldmv)
426 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
428 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
430 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
432 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
434 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
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);
440 src_x = av_clip(src_x, -17, s->avctx->coded_width);
441 if (v->fcm == ILACE_FRAME) {
443 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
445 src_y = av_clip(src_y, -18, s->avctx->coded_height);
447 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
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];
459 src_y -= (src_y < 4);
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;
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,
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);
479 /* if we deal with intensity compensation we need to scale source blocks */
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);
486 srcY += s->mspel * (1 + (s->linesize << fieldmv));
490 dxy = ((my & 3) << 2) | (mx & 3);
492 v->vc1dsp.avg_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
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);
498 s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
500 s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
504 /** Do motion compensation for 4-MV macroblock - both chroma blocks
506 void ff_vc1_mc_4mv_chroma(VC1Context *v, int dir)
508 MpegEncContext *s = &v->s;
509 H264ChromaContext *h264chroma = &v->h264chroma;
510 uint8_t *srcU, *srcV;
511 int uvmx, uvmy, uvsrc_x, uvsrc_y;
514 int v_edge_pos = s->v_edge_pos >> v->field_mode;
515 uint8_t (*lutuv)[256];
518 if (!v->field_mode && !v->s.last_picture.f->data[0])
520 if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY)
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);
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
532 chroma_ref_type = v->ref_field_type[dir];
534 int opp_count = get_luma_mv(v, dir, &tx, &ty);
535 chroma_ref_type = v->cur_field_type ^ (opp_count > 2);
537 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f->data[0])
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;
544 v->luma_mv[s->mb_x][0] = uvmx;
545 v->luma_mv[s->mb_x][1] = uvmy;
548 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
549 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
551 // Field conversion bias
552 if (v->cur_field_type != chroma_ref_type)
553 uvmy += 2 - 4 * chroma_ref_type;
555 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
556 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
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);
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);
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;
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;
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;
586 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
590 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
591 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
594 if (chroma_ref_type) {
595 srcU += s->current_picture_ptr->f->linesize[1];
596 srcV += s->current_picture_ptr->f->linesize[2];
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;
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);
619 /* if we deal with intensity compensation we need to scale source blocks */
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)],
628 /* Chroma MC always uses qpel bilinear */
629 uvmx = (uvmx & 3) << 1;
630 uvmy = (uvmy & 3) << 1;
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);
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);
640 /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
642 void ff_vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
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];
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;
655 uint8_t (*lutuv)[256];
657 if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY)
660 for (i = 0; i < 4; i++) {
661 int d = i < 2 ? dir: dir2;
663 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
666 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
668 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
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;
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;
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;
698 v_edge_pos = (s->v_edge_pos >> 1) - 1;
700 uvsrc_y -= (uvsrc_y < 2);
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;
717 /* if we deal with intensity compensation we need to scale source blocks */
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);
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]);
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]);
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]);
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]);
745 /** Motion compensation for direct or interpolated blocks in B-frames
747 void ff_vc1_interp_mc(VC1Context *v)
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;
756 if (!v->field_mode && !v->s.next_picture.f->data[0])
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;
768 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
769 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
771 srcY = s->next_picture.f->data[0];
772 srcU = s->next_picture.f->data[1];
773 srcV = s->next_picture.f->data[2];
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);
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);
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);
792 srcY += src_y * s->linesize + src_x;
793 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
794 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
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];
802 /* for grayscale we should not try to read from unknown area */
803 if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY) {
804 srcU = s->edge_emu_buffer + 18 * s->linesize;
805 srcV = s->edge_emu_buffer + 18 * s->linesize;
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;
815 srcY -= s->mspel * (1 + s->linesize);
816 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
817 s->linesize, s->linesize,
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,
826 s->h_edge_pos >> 1, v_edge_pos >> 1);
827 s->vdsp.emulated_edge_mc(vbuf, srcV,
828 s->uvlinesize, s->uvlinesize,
831 s->h_edge_pos >> 1, v_edge_pos >> 1);
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);
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)],
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)],
852 srcY += s->mspel * (1 + s->linesize);
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);
859 dxy = (my & 2) | ((mx & 2) >> 1);
862 s->hdsp.avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
864 s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0], srcY, s->linesize, 16);
867 if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY) return;
868 /* Chroma MC always uses qpel blilinear */
869 uvmx = (uvmx & 3) << 1;
870 uvmy = (uvmy & 3) << 1;
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);
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);