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) * (1 << 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) * (1 << hpel);
196 n = scaleforopp_x(v, n) * (1 << hpel);
199 if (v->s.pict_type != AV_PICTURE_TYPE_B)
200 refdist = v->refdist;
202 refdist = dir ? v->brfd : v->frfd;
203 refdist = FFMIN(refdist, 3);
204 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
206 n = (n * scaleopp >> 8) * (1 << hpel);
210 /** Predict and set motion vector
212 void ff_vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
213 int mv1, int r_x, int r_y, uint8_t* is_intra,
214 int pred_flag, int dir)
216 MpegEncContext *s = &v->s;
217 int xy, wrap, off = 0;
221 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
222 int opposite, a_f, b_f, c_f;
223 int16_t field_predA[2];
224 int16_t field_predB[2];
225 int16_t field_predC[2];
226 int a_valid, b_valid, c_valid;
227 int hybridmv_thresh, y_bias = 0;
229 if (v->mv_mode == MV_PMODE_MIXED_MV ||
230 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
234 /* scale MV difference to be quad-pel */
235 if (!s->quarter_sample) {
241 xy = s->block_index[n];
244 s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
245 s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
246 s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
247 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
248 if (mv1) { /* duplicate motion data for 1-MV block */
249 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
250 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
251 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
252 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
253 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
254 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
255 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
256 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
257 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
258 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][0] = 0;
259 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
260 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
261 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
266 a_valid = !s->first_slice_line || (n == 2 || n == 3);
268 c_valid = s->mb_x || (n == 1 || n == 3);
270 if (v->field_mode && mixedmv_pic)
271 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
273 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
274 b_valid = b_valid && s->mb_width > 1;
276 //in 4-MV mode different blocks have different B predictor position
280 off = s->mb_x ? -1 : 1;
282 off = s->mb_x ? -1 : 2 * s->mb_width - wrap - 1;
285 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
293 if (v->field_mode && s->mb_width == 1)
294 b_valid = b_valid && c_valid;
298 a_valid = a_valid && !is_intra[xy - wrap];
299 b_valid = b_valid && !is_intra[xy - wrap + off];
300 c_valid = c_valid && !is_intra[xy - 1];
304 A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
305 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
307 num_samefield += 1 - a_f;
308 field_predA[0] = A[0];
309 field_predA[1] = A[1];
311 field_predA[0] = field_predA[1] = 0;
315 B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
316 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
318 num_samefield += 1 - b_f;
319 field_predB[0] = B[0];
320 field_predB[1] = B[1];
322 field_predB[0] = field_predB[1] = 0;
326 C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
327 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
329 num_samefield += 1 - c_f;
330 field_predC[0] = C[0];
331 field_predC[1] = C[1];
333 field_predC[0] = field_predC[1] = 0;
339 // REFFIELD determines if the last field or the second-last field is
340 // to be used as reference
341 opposite = 1 - v->reffield;
343 if (num_samefield <= num_oppfield)
344 opposite = 1 - pred_flag;
346 opposite = pred_flag;
351 v->mv_f[dir][xy + v->blocks_off] = 1;
352 v->ref_field_type[dir] = !v->cur_field_type;
353 if (a_valid && !a_f) {
354 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
355 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
357 if (b_valid && !b_f) {
358 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
359 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
361 if (c_valid && !c_f) {
362 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
363 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
366 v->mv_f[dir][xy + v->blocks_off] = 0;
367 v->ref_field_type[dir] = v->cur_field_type;
368 if (a_valid && a_f) {
369 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
370 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
372 if (b_valid && b_f) {
373 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
374 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
376 if (c_valid && c_f) {
377 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
378 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
385 } else if (c_valid) {
388 } else if (b_valid) {
396 if (num_samefield + num_oppfield > 1) {
397 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
398 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
401 /* Pullback MV as specified in 8.3.5.3.4 */
402 if (!v->field_mode) {
404 int MV = mv1 ? -60 : -28;
405 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
406 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
407 X = (s->mb_width << 6) - 4;
408 Y = (s->mb_height << 6) - 4;
409 if (qx + px < MV) px = MV - qx;
410 if (qy + py < MV) py = MV - qy;
411 if (qx + px > X) px = X - qx;
412 if (qy + py > Y) py = Y - qy;
415 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
416 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
417 hybridmv_thresh = 32;
418 if (a_valid && c_valid) {
419 if (is_intra[xy - wrap])
420 sum = FFABS(px) + FFABS(py);
422 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
423 if (sum > hybridmv_thresh) {
424 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
432 if (is_intra[xy - 1])
433 sum = FFABS(px) + FFABS(py);
435 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
436 if (sum > hybridmv_thresh) {
437 if (get_bits1(&s->gb)) {
449 if (v->field_mode && v->numref)
451 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
453 /* store MV using signed modulus of MV range defined in 4.11 */
454 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;
455 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;
456 if (mv1) { /* duplicate motion data for 1-MV block */
457 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
458 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
459 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
460 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
461 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
462 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
463 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
464 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];
468 /** Predict and set motion vector for interlaced frame picture MBs
470 void ff_vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
471 int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
473 MpegEncContext *s = &v->s;
474 int xy, wrap, off = 0;
475 int A[2], B[2], C[2];
477 int a_valid = 0, b_valid = 0, c_valid = 0;
478 int field_a, field_b, field_c; // 0: same, 1: opposite
479 int total_valid, num_samefield, num_oppfield;
480 int pos_c, pos_b, n_adj;
483 xy = s->block_index[n];
486 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
487 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
488 s->current_picture.motion_val[1][xy][0] = 0;
489 s->current_picture.motion_val[1][xy][1] = 0;
490 if (mvn == 1) { /* duplicate motion data for 1-MV block */
491 s->current_picture.motion_val[0][xy + 1][0] = 0;
492 s->current_picture.motion_val[0][xy + 1][1] = 0;
493 s->current_picture.motion_val[0][xy + wrap][0] = 0;
494 s->current_picture.motion_val[0][xy + wrap][1] = 0;
495 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
496 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
497 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
498 s->current_picture.motion_val[1][xy + 1][0] = 0;
499 s->current_picture.motion_val[1][xy + 1][1] = 0;
500 s->current_picture.motion_val[1][xy + wrap][0] = 0;
501 s->current_picture.motion_val[1][xy + wrap][1] = 0;
502 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
503 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
508 off = ((n == 0) || (n == 1)) ? 1 : -1;
510 if (s->mb_x || (n == 1) || (n == 3)) {
511 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
512 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
513 A[0] = s->current_picture.motion_val[dir][xy - 1][0];
514 A[1] = s->current_picture.motion_val[dir][xy - 1][1];
516 } else { // current block has frame mv and cand. has field MV (so average)
517 A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
518 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
519 A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
520 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
523 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
529 /* Predict B and C */
530 B[0] = B[1] = C[0] = C[1] = 0;
531 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
532 if (!s->first_slice_line) {
533 if (!v->is_intra[s->mb_x - s->mb_stride]) {
536 pos_b = s->block_index[n_adj] - 2 * wrap;
537 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
538 n_adj = (n & 2) | (n & 1);
540 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
541 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
542 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
543 B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
544 B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
547 if (s->mb_width > 1) {
548 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
551 pos_c = s->block_index[2] - 2 * wrap + 2;
552 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
555 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
556 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
557 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
558 C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
559 C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
561 if (s->mb_x == s->mb_width - 1) {
562 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
565 pos_c = s->block_index[3] - 2 * wrap - 2;
566 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
569 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
570 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
571 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
572 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
573 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
582 pos_b = s->block_index[1];
584 B[0] = s->current_picture.motion_val[dir][pos_b][0];
585 B[1] = s->current_picture.motion_val[dir][pos_b][1];
586 pos_c = s->block_index[0];
588 C[0] = s->current_picture.motion_val[dir][pos_c][0];
589 C[1] = s->current_picture.motion_val[dir][pos_c][1];
592 total_valid = a_valid + b_valid + c_valid;
593 // check if predictor A is out of bounds
594 if (!s->mb_x && !(n == 1 || n == 3)) {
597 // check if predictor B is out of bounds
598 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
599 B[0] = B[1] = C[0] = C[1] = 0;
601 if (!v->blk_mv_type[xy]) {
602 if (s->mb_width == 1) {
606 if (total_valid >= 2) {
607 px = mid_pred(A[0], B[0], C[0]);
608 py = mid_pred(A[1], B[1], C[1]);
609 } else if (total_valid) {
610 if (a_valid) { px = A[0]; py = A[1]; }
611 else if (b_valid) { px = B[0]; py = B[1]; }
612 else { px = C[0]; py = C[1]; }
617 field_a = (A[1] & 4) ? 1 : 0;
621 field_b = (B[1] & 4) ? 1 : 0;
625 field_c = (C[1] & 4) ? 1 : 0;
629 num_oppfield = field_a + field_b + field_c;
630 num_samefield = total_valid - num_oppfield;
631 if (total_valid == 3) {
632 if ((num_samefield == 3) || (num_oppfield == 3)) {
633 px = mid_pred(A[0], B[0], C[0]);
634 py = mid_pred(A[1], B[1], C[1]);
635 } else if (num_samefield >= num_oppfield) {
636 /* take one MV from same field set depending on priority
637 the check for B may not be necessary */
638 px = !field_a ? A[0] : B[0];
639 py = !field_a ? A[1] : B[1];
641 px = field_a ? A[0] : B[0];
642 py = field_a ? A[1] : B[1];
644 } else if (total_valid == 2) {
645 if (num_samefield >= num_oppfield) {
646 if (!field_a && a_valid) {
649 } else if (!field_b && b_valid) {
652 } else /*if (c_valid)*/ {
658 if (field_a && a_valid) {
661 } else /*if (field_b && b_valid)*/ {
662 av_assert1(field_b && b_valid);
667 } else if (total_valid == 1) {
668 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
669 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
673 /* store MV using signed modulus of MV range defined in 4.11 */
674 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
675 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
676 if (mvn == 1) { /* duplicate motion data for 1-MV block */
677 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
678 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
679 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
680 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
681 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
682 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
683 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
684 s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
685 s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
686 s->mv[dir][n + 1][0] = s->mv[dir][n][0];
687 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
691 void ff_vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
692 int direct, int mvtype)
694 MpegEncContext *s = &v->s;
695 int xy, wrap, off = 0;
700 const uint8_t *is_intra = v->mb_type[0];
702 av_assert0(!v->field_mode);
706 /* scale MV difference to be quad-pel */
707 if (!s->quarter_sample) {
715 xy = s->block_index[0];
718 s->current_picture.motion_val[0][xy][0] =
719 s->current_picture.motion_val[0][xy][1] =
720 s->current_picture.motion_val[1][xy][0] =
721 s->current_picture.motion_val[1][xy][1] = 0;
724 if (direct && s->next_picture_ptr->field_picture)
725 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
727 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
728 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
729 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
730 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
732 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
733 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));
734 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));
735 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));
736 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));
738 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
739 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
740 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
741 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
745 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
746 C = s->current_picture.motion_val[0][xy - 2];
747 A = s->current_picture.motion_val[0][xy - wrap * 2];
748 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
749 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
751 if (!s->mb_x) C[0] = C[1] = 0;
752 if (!s->first_slice_line) { // predictor A is not out of bounds
753 if (s->mb_width == 1) {
757 px = mid_pred(A[0], B[0], C[0]);
758 py = mid_pred(A[1], B[1], C[1]);
760 } else if (s->mb_x) { // predictor C is not out of bounds
766 /* Pullback MV as specified in 8.3.5.3.4 */
769 int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
770 int MV = 4 - (1 << sh);
771 qx = (s->mb_x << sh);
772 qy = (s->mb_y << sh);
773 X = (s->mb_width << sh) - 4;
774 Y = (s->mb_height << sh) - 4;
775 if (qx + px < MV) px = MV - qx;
776 if (qy + py < MV) py = MV - qy;
777 if (qx + px > X) px = X - qx;
778 if (qy + py > Y) py = Y - qy;
780 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
781 if (0 && !s->first_slice_line && s->mb_x) {
782 if (is_intra[xy - wrap])
783 sum = FFABS(px) + FFABS(py);
785 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
787 if (get_bits1(&s->gb)) {
795 if (is_intra[xy - 2])
796 sum = FFABS(px) + FFABS(py);
798 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
800 if (get_bits1(&s->gb)) {
810 /* store MV using signed modulus of MV range defined in 4.11 */
811 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
812 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
814 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
815 C = s->current_picture.motion_val[1][xy - 2];
816 A = s->current_picture.motion_val[1][xy - wrap * 2];
817 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
818 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
822 if (!s->first_slice_line) { // predictor A is not out of bounds
823 if (s->mb_width == 1) {
827 px = mid_pred(A[0], B[0], C[0]);
828 py = mid_pred(A[1], B[1], C[1]);
830 } else if (s->mb_x) { // predictor C is not out of bounds
836 /* Pullback MV as specified in 8.3.5.3.4 */
839 int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
840 int MV = 4 - (1 << sh);
841 qx = (s->mb_x << sh);
842 qy = (s->mb_y << sh);
843 X = (s->mb_width << sh) - 4;
844 Y = (s->mb_height << sh) - 4;
845 if (qx + px < MV) px = MV - qx;
846 if (qy + py < MV) py = MV - qy;
847 if (qx + px > X) px = X - qx;
848 if (qy + py > Y) py = Y - qy;
850 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
851 if (0 && !s->first_slice_line && s->mb_x) {
852 if (is_intra[xy - wrap])
853 sum = FFABS(px) + FFABS(py);
855 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
857 if (get_bits1(&s->gb)) {
865 if (is_intra[xy - 2])
866 sum = FFABS(px) + FFABS(py);
868 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
870 if (get_bits1(&s->gb)) {
880 /* store MV using signed modulus of MV range defined in 4.11 */
882 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
883 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
885 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
886 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
887 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
888 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
891 void ff_vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y,
892 int mv1, int *pred_flag)
894 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
895 MpegEncContext *s = &v->s;
896 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
898 if (v->bmvtype == BMV_TYPE_DIRECT) {
900 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
901 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
902 v->bfraction, 0, s->quarter_sample);
903 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
904 v->bfraction, 0, s->quarter_sample);
905 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
906 v->bfraction, 1, s->quarter_sample);
907 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
908 v->bfraction, 1, s->quarter_sample);
910 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
911 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
912 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
913 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
914 f = (total_opp > 2) ? 1 : 0;
916 s->mv[0][0][0] = s->mv[0][0][1] = 0;
917 s->mv[1][0][0] = s->mv[1][0][1] = 0;
920 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
921 for (k = 0; k < 4; k++) {
922 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
923 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
924 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
925 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
926 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
927 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
931 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
932 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);
933 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);
936 if (dir) { // backward
937 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);
939 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);
942 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);
944 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);