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 "mpegutils.h"
31 #include "mpegvideo.h"
36 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
38 int scaledvalue, refdist;
39 int scalesame1, scalesame2;
40 int scalezone1_x, zone1offset_x;
41 int table_index = dir ^ v->second_field;
43 if (v->s.pict_type != AV_PICTURE_TYPE_B)
46 refdist = dir ? v->brfd : v->frfd;
49 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
50 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
51 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
52 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
57 if (FFABS(n) < scalezone1_x)
58 scaledvalue = (n * scalesame1) >> 8;
61 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
63 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
66 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
69 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
71 int scaledvalue, refdist;
72 int scalesame1, scalesame2;
73 int scalezone1_y, zone1offset_y;
74 int table_index = dir ^ v->second_field;
76 if (v->s.pict_type != AV_PICTURE_TYPE_B)
79 refdist = dir ? v->brfd : v->frfd;
82 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
83 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
84 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
85 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
90 if (FFABS(n) < scalezone1_y)
91 scaledvalue = (n * scalesame1) >> 8;
94 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
96 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
100 if (v->cur_field_type && !v->ref_field_type[dir])
101 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
103 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
106 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
108 int scalezone1_x, zone1offset_x;
109 int scaleopp1, scaleopp2, brfd;
112 brfd = FFMIN(v->brfd, 3);
113 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
114 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
115 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
116 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
121 if (FFABS(n) < scalezone1_x)
122 scaledvalue = (n * scaleopp1) >> 8;
125 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
127 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
130 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
133 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
135 int scalezone1_y, zone1offset_y;
136 int scaleopp1, scaleopp2, brfd;
139 brfd = FFMIN(v->brfd, 3);
140 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
141 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
142 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
143 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
148 if (FFABS(n) < scalezone1_y)
149 scaledvalue = (n * scaleopp1) >> 8;
152 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
154 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
157 if (v->cur_field_type && !v->ref_field_type[dir]) {
158 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
160 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
164 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
168 int hpel = 1 - v->s.quarter_sample;
171 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
173 n = scaleforsame_y(v, i, n, dir) * (1 << hpel);
175 n = scaleforsame_x(v, n, dir) * (1 << hpel);
178 brfd = FFMIN(v->brfd, 3);
179 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
181 n = (n * scalesame >> 8) << hpel;
185 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
188 int refdist, scaleopp;
189 int hpel = 1 - v->s.quarter_sample;
192 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
194 n = scaleforopp_y(v, n, dir) << hpel;
196 n = scaleforopp_x(v, n) << hpel;
199 if (v->s.pict_type != AV_PICTURE_TYPE_B)
200 refdist = FFMIN(v->refdist, 3);
202 refdist = dir ? v->brfd : v->frfd;
203 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
205 n = (n * scaleopp >> 8) * (1 << hpel);
209 /** Predict and set motion vector
211 void ff_vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
212 int mv1, int r_x, int r_y, uint8_t* is_intra,
213 int pred_flag, int dir)
215 MpegEncContext *s = &v->s;
216 int xy, wrap, off = 0;
220 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
221 int opposite, a_f, b_f, c_f;
222 int16_t field_predA[2];
223 int16_t field_predB[2];
224 int16_t field_predC[2];
225 int a_valid, b_valid, c_valid;
226 int hybridmv_thresh, y_bias = 0;
228 if (v->mv_mode == MV_PMODE_MIXED_MV ||
229 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
233 /* scale MV difference to be quad-pel */
234 if (!s->quarter_sample) {
240 xy = s->block_index[n];
243 s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
244 s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
245 s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
246 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
247 if (mv1) { /* duplicate motion data for 1-MV block */
248 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
249 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
250 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
251 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
252 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
253 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
254 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
255 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
256 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
257 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][0] = 0;
258 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
259 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
260 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
265 a_valid = !s->first_slice_line || (n == 2 || n == 3);
267 c_valid = s->mb_x || (n == 1 || n == 3);
269 if (v->field_mode && mixedmv_pic)
270 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
272 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
273 b_valid = b_valid && s->mb_width > 1;
275 //in 4-MV mode different blocks have different B predictor position
279 off = s->mb_x ? -1 : 1;
281 off = s->mb_x ? -1 : 2 * s->mb_width - wrap - 1;
284 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
292 if (v->field_mode && s->mb_width == 1)
293 b_valid = b_valid && c_valid;
297 a_valid = a_valid && !is_intra[xy - wrap];
298 b_valid = b_valid && !is_intra[xy - wrap + off];
299 c_valid = c_valid && !is_intra[xy - 1];
303 A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
304 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
306 num_samefield += 1 - a_f;
307 field_predA[0] = A[0];
308 field_predA[1] = A[1];
310 field_predA[0] = field_predA[1] = 0;
314 B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
315 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
317 num_samefield += 1 - b_f;
318 field_predB[0] = B[0];
319 field_predB[1] = B[1];
321 field_predB[0] = field_predB[1] = 0;
325 C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
326 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
328 num_samefield += 1 - c_f;
329 field_predC[0] = C[0];
330 field_predC[1] = C[1];
332 field_predC[0] = field_predC[1] = 0;
338 // REFFIELD determines if the last field or the second-last field is
339 // to be used as reference
340 opposite = 1 - v->reffield;
342 if (num_samefield <= num_oppfield)
343 opposite = 1 - pred_flag;
345 opposite = pred_flag;
350 v->mv_f[dir][xy + v->blocks_off] = 1;
351 v->ref_field_type[dir] = !v->cur_field_type;
352 if (a_valid && !a_f) {
353 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
354 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
356 if (b_valid && !b_f) {
357 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
358 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
360 if (c_valid && !c_f) {
361 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
362 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
365 v->mv_f[dir][xy + v->blocks_off] = 0;
366 v->ref_field_type[dir] = v->cur_field_type;
367 if (a_valid && a_f) {
368 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
369 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
371 if (b_valid && b_f) {
372 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
373 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
375 if (c_valid && c_f) {
376 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
377 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
384 } else if (c_valid) {
387 } else if (b_valid) {
395 if (num_samefield + num_oppfield > 1) {
396 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
397 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
400 /* Pullback MV as specified in 8.3.5.3.4 */
401 if (!v->field_mode) {
403 int MV = mv1 ? -60 : -28;
404 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
405 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
406 X = (s->mb_width << 6) - 4;
407 Y = (s->mb_height << 6) - 4;
408 if (qx + px < MV) px = MV - qx;
409 if (qy + py < MV) py = MV - qy;
410 if (qx + px > X) px = X - qx;
411 if (qy + py > Y) py = Y - qy;
414 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
415 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
416 hybridmv_thresh = 32;
417 if (a_valid && c_valid) {
418 if (is_intra[xy - wrap])
419 sum = FFABS(px) + FFABS(py);
421 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
422 if (sum > hybridmv_thresh) {
423 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
431 if (is_intra[xy - 1])
432 sum = FFABS(px) + FFABS(py);
434 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
435 if (sum > hybridmv_thresh) {
436 if (get_bits1(&s->gb)) {
448 if (v->field_mode && v->numref)
450 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
452 /* store MV using signed modulus of MV range defined in 4.11 */
453 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
454 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
455 if (mv1) { /* duplicate motion data for 1-MV block */
456 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
457 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
458 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
459 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
460 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
461 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
462 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
463 v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
467 /** Predict and set motion vector for interlaced frame picture MBs
469 void ff_vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
470 int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
472 MpegEncContext *s = &v->s;
473 int xy, wrap, off = 0;
474 int A[2], B[2], C[2];
476 int a_valid = 0, b_valid = 0, c_valid = 0;
477 int field_a, field_b, field_c; // 0: same, 1: opposite
478 int total_valid, num_samefield, num_oppfield;
479 int pos_c, pos_b, n_adj;
482 xy = s->block_index[n];
485 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
486 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
487 s->current_picture.motion_val[1][xy][0] = 0;
488 s->current_picture.motion_val[1][xy][1] = 0;
489 if (mvn == 1) { /* duplicate motion data for 1-MV block */
490 s->current_picture.motion_val[0][xy + 1][0] = 0;
491 s->current_picture.motion_val[0][xy + 1][1] = 0;
492 s->current_picture.motion_val[0][xy + wrap][0] = 0;
493 s->current_picture.motion_val[0][xy + wrap][1] = 0;
494 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
495 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
496 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
497 s->current_picture.motion_val[1][xy + 1][0] = 0;
498 s->current_picture.motion_val[1][xy + 1][1] = 0;
499 s->current_picture.motion_val[1][xy + wrap][0] = 0;
500 s->current_picture.motion_val[1][xy + wrap][1] = 0;
501 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
502 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
507 off = ((n == 0) || (n == 1)) ? 1 : -1;
509 if (s->mb_x || (n == 1) || (n == 3)) {
510 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
511 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
512 A[0] = s->current_picture.motion_val[dir][xy - 1][0];
513 A[1] = s->current_picture.motion_val[dir][xy - 1][1];
515 } else { // current block has frame mv and cand. has field MV (so average)
516 A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
517 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
518 A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
519 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
522 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
528 /* Predict B and C */
529 B[0] = B[1] = C[0] = C[1] = 0;
530 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
531 if (!s->first_slice_line) {
532 if (!v->is_intra[s->mb_x - s->mb_stride]) {
535 pos_b = s->block_index[n_adj] - 2 * wrap;
536 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
537 n_adj = (n & 2) | (n & 1);
539 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
540 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
541 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
542 B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
543 B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
546 if (s->mb_width > 1) {
547 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
550 pos_c = s->block_index[2] - 2 * wrap + 2;
551 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
554 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
555 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
556 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
557 C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
558 C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
560 if (s->mb_x == s->mb_width - 1) {
561 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
564 pos_c = s->block_index[3] - 2 * wrap - 2;
565 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
568 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
569 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
570 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
571 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
572 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
581 pos_b = s->block_index[1];
583 B[0] = s->current_picture.motion_val[dir][pos_b][0];
584 B[1] = s->current_picture.motion_val[dir][pos_b][1];
585 pos_c = s->block_index[0];
587 C[0] = s->current_picture.motion_val[dir][pos_c][0];
588 C[1] = s->current_picture.motion_val[dir][pos_c][1];
591 total_valid = a_valid + b_valid + c_valid;
592 // check if predictor A is out of bounds
593 if (!s->mb_x && !(n == 1 || n == 3)) {
596 // check if predictor B is out of bounds
597 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
598 B[0] = B[1] = C[0] = C[1] = 0;
600 if (!v->blk_mv_type[xy]) {
601 if (s->mb_width == 1) {
605 if (total_valid >= 2) {
606 px = mid_pred(A[0], B[0], C[0]);
607 py = mid_pred(A[1], B[1], C[1]);
608 } else if (total_valid) {
609 if (a_valid) { px = A[0]; py = A[1]; }
610 else if (b_valid) { px = B[0]; py = B[1]; }
611 else { px = C[0]; py = C[1]; }
616 field_a = (A[1] & 4) ? 1 : 0;
620 field_b = (B[1] & 4) ? 1 : 0;
624 field_c = (C[1] & 4) ? 1 : 0;
628 num_oppfield = field_a + field_b + field_c;
629 num_samefield = total_valid - num_oppfield;
630 if (total_valid == 3) {
631 if ((num_samefield == 3) || (num_oppfield == 3)) {
632 px = mid_pred(A[0], B[0], C[0]);
633 py = mid_pred(A[1], B[1], C[1]);
634 } else if (num_samefield >= num_oppfield) {
635 /* take one MV from same field set depending on priority
636 the check for B may not be necessary */
637 px = !field_a ? A[0] : B[0];
638 py = !field_a ? A[1] : B[1];
640 px = field_a ? A[0] : B[0];
641 py = field_a ? A[1] : B[1];
643 } else if (total_valid == 2) {
644 if (num_samefield >= num_oppfield) {
645 if (!field_a && a_valid) {
648 } else if (!field_b && b_valid) {
651 } else /*if (c_valid)*/ {
657 if (field_a && a_valid) {
660 } else /*if (field_b && b_valid)*/ {
661 av_assert1(field_b && b_valid);
666 } else if (total_valid == 1) {
667 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
668 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
672 /* store MV using signed modulus of MV range defined in 4.11 */
673 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
674 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
675 if (mvn == 1) { /* duplicate motion data for 1-MV block */
676 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
677 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
678 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
679 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
680 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
681 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
682 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
683 s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
684 s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
685 s->mv[dir][n + 1][0] = s->mv[dir][n][0];
686 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
690 void ff_vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
691 int direct, int mvtype)
693 MpegEncContext *s = &v->s;
694 int xy, wrap, off = 0;
699 const uint8_t *is_intra = v->mb_type[0];
701 av_assert0(!v->field_mode);
705 /* scale MV difference to be quad-pel */
706 if (!s->quarter_sample) {
714 xy = s->block_index[0];
717 s->current_picture.motion_val[0][xy][0] =
718 s->current_picture.motion_val[0][xy][1] =
719 s->current_picture.motion_val[1][xy][0] =
720 s->current_picture.motion_val[1][xy][1] = 0;
723 if (direct && s->next_picture_ptr->field_picture)
724 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
726 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
727 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
728 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
729 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
731 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
732 s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
733 s->mv[0][0][1] = av_clip(s->mv[0][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
734 s->mv[1][0][0] = av_clip(s->mv[1][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
735 s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
737 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
738 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
739 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
740 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
744 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
745 C = s->current_picture.motion_val[0][xy - 2];
746 A = s->current_picture.motion_val[0][xy - wrap * 2];
747 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
748 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
750 if (!s->mb_x) C[0] = C[1] = 0;
751 if (!s->first_slice_line) { // predictor A is not out of bounds
752 if (s->mb_width == 1) {
756 px = mid_pred(A[0], B[0], C[0]);
757 py = mid_pred(A[1], B[1], C[1]);
759 } else if (s->mb_x) { // predictor C is not out of bounds
765 /* Pullback MV as specified in 8.3.5.3.4 */
768 int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
769 int MV = 4 - (1 << sh);
770 qx = (s->mb_x << sh);
771 qy = (s->mb_y << sh);
772 X = (s->mb_width << sh) - 4;
773 Y = (s->mb_height << sh) - 4;
774 if (qx + px < MV) px = MV - qx;
775 if (qy + py < MV) py = MV - qy;
776 if (qx + px > X) px = X - qx;
777 if (qy + py > Y) py = Y - qy;
779 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
780 if (0 && !s->first_slice_line && s->mb_x) {
781 if (is_intra[xy - wrap])
782 sum = FFABS(px) + FFABS(py);
784 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
786 if (get_bits1(&s->gb)) {
794 if (is_intra[xy - 2])
795 sum = FFABS(px) + FFABS(py);
797 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
799 if (get_bits1(&s->gb)) {
809 /* store MV using signed modulus of MV range defined in 4.11 */
810 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
811 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
813 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
814 C = s->current_picture.motion_val[1][xy - 2];
815 A = s->current_picture.motion_val[1][xy - wrap * 2];
816 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
817 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
821 if (!s->first_slice_line) { // predictor A is not out of bounds
822 if (s->mb_width == 1) {
826 px = mid_pred(A[0], B[0], C[0]);
827 py = mid_pred(A[1], B[1], C[1]);
829 } else if (s->mb_x) { // predictor C is not out of bounds
835 /* Pullback MV as specified in 8.3.5.3.4 */
838 int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
839 int MV = 4 - (1 << sh);
840 qx = (s->mb_x << sh);
841 qy = (s->mb_y << sh);
842 X = (s->mb_width << sh) - 4;
843 Y = (s->mb_height << sh) - 4;
844 if (qx + px < MV) px = MV - qx;
845 if (qy + py < MV) py = MV - qy;
846 if (qx + px > X) px = X - qx;
847 if (qy + py > Y) py = Y - qy;
849 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
850 if (0 && !s->first_slice_line && s->mb_x) {
851 if (is_intra[xy - wrap])
852 sum = FFABS(px) + FFABS(py);
854 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
856 if (get_bits1(&s->gb)) {
864 if (is_intra[xy - 2])
865 sum = FFABS(px) + FFABS(py);
867 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
869 if (get_bits1(&s->gb)) {
879 /* store MV using signed modulus of MV range defined in 4.11 */
881 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
882 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
884 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
885 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
886 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
887 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
890 void ff_vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y,
891 int mv1, int *pred_flag)
893 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
894 MpegEncContext *s = &v->s;
895 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
897 if (v->bmvtype == BMV_TYPE_DIRECT) {
899 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
900 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
901 v->bfraction, 0, s->quarter_sample);
902 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
903 v->bfraction, 0, s->quarter_sample);
904 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
905 v->bfraction, 1, s->quarter_sample);
906 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
907 v->bfraction, 1, s->quarter_sample);
909 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
910 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
911 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
912 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
913 f = (total_opp > 2) ? 1 : 0;
915 s->mv[0][0][0] = s->mv[0][0][1] = 0;
916 s->mv[1][0][0] = s->mv[1][0][1] = 0;
919 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
920 for (k = 0; k < 4; k++) {
921 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
922 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
923 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
924 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
925 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
926 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
930 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
931 ff_vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
932 ff_vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
935 if (dir) { // backward
936 ff_vc1_pred_mv(v, n, dmv_x[1], dmv_y[1], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
938 ff_vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
941 ff_vc1_pred_mv(v, n, dmv_x[0], dmv_y[0], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
943 ff_vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);