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][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 C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
266 A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
268 if (v->field_mode && mixedmv_pic)
269 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
271 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
273 //in 4-MV mode different blocks have different B predictor position
276 off = (s->mb_x > 0) ? -1 : 1;
279 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
288 B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
290 a_valid = !s->first_slice_line || (n == 2 || n == 3);
291 b_valid = a_valid && (s->mb_width > 1);
292 c_valid = s->mb_x || (n == 1 || n == 3);
294 a_valid = a_valid && !is_intra[xy - wrap];
295 b_valid = b_valid && !is_intra[xy - wrap + off];
296 c_valid = c_valid && !is_intra[xy - 1];
300 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
302 num_samefield += 1 - a_f;
303 field_predA[0] = A[0];
304 field_predA[1] = A[1];
306 field_predA[0] = field_predA[1] = 0;
310 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
312 num_samefield += 1 - b_f;
313 field_predB[0] = B[0];
314 field_predB[1] = B[1];
316 field_predB[0] = field_predB[1] = 0;
320 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
322 num_samefield += 1 - c_f;
323 field_predC[0] = C[0];
324 field_predC[1] = C[1];
326 field_predC[0] = field_predC[1] = 0;
332 // REFFIELD determines if the last field or the second-last field is
333 // to be used as reference
334 opposite = 1 - v->reffield;
336 if (num_samefield <= num_oppfield)
337 opposite = 1 - pred_flag;
339 opposite = pred_flag;
344 if (a_valid && !a_f) {
345 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
346 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
348 if (b_valid && !b_f) {
349 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
350 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
352 if (c_valid && !c_f) {
353 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
354 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
356 v->mv_f[dir][xy + v->blocks_off] = 1;
357 v->ref_field_type[dir] = !v->cur_field_type;
359 if (a_valid && a_f) {
360 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
361 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
363 if (b_valid && b_f) {
364 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
365 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
367 if (c_valid && c_f) {
368 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
369 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
371 v->mv_f[dir][xy + v->blocks_off] = 0;
372 v->ref_field_type[dir] = v->cur_field_type;
378 } else if (c_valid) {
381 } else if (b_valid) {
389 if (num_samefield + num_oppfield > 1) {
390 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
391 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
394 /* Pullback MV as specified in 8.3.5.3.4 */
395 if (!v->field_mode) {
397 int MV = mv1 ? -60 : -28;
398 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
399 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
400 X = (s->mb_width << 6) - 4;
401 Y = (s->mb_height << 6) - 4;
402 if (qx + px < MV) px = MV - qx;
403 if (qy + py < MV) py = MV - qy;
404 if (qx + px > X) px = X - qx;
405 if (qy + py > Y) py = Y - qy;
408 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
409 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
410 hybridmv_thresh = 32;
411 if (a_valid && c_valid) {
412 if (is_intra[xy - wrap])
413 sum = FFABS(px) + FFABS(py);
415 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
416 if (sum > hybridmv_thresh) {
417 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
425 if (is_intra[xy - 1])
426 sum = FFABS(px) + FFABS(py);
428 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
429 if (sum > hybridmv_thresh) {
430 if (get_bits1(&s->gb)) {
442 if (v->field_mode && v->numref)
444 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
446 /* store MV using signed modulus of MV range defined in 4.11 */
447 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;
448 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;
449 if (mv1) { /* duplicate motion data for 1-MV block */
450 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
451 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
452 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
453 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
454 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
455 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
456 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
457 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];
461 /** Predict and set motion vector for interlaced frame picture MBs
463 void ff_vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
464 int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
466 MpegEncContext *s = &v->s;
467 int xy, wrap, off = 0;
468 int A[2], B[2], C[2];
470 int a_valid = 0, b_valid = 0, c_valid = 0;
471 int field_a, field_b, field_c; // 0: same, 1: opposite
472 int total_valid, num_samefield, num_oppfield;
473 int pos_c, pos_b, n_adj;
476 xy = s->block_index[n];
479 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
480 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
481 s->current_picture.motion_val[1][xy][0] = 0;
482 s->current_picture.motion_val[1][xy][1] = 0;
483 if (mvn == 1) { /* duplicate motion data for 1-MV block */
484 s->current_picture.motion_val[0][xy + 1][0] = 0;
485 s->current_picture.motion_val[0][xy + 1][1] = 0;
486 s->current_picture.motion_val[0][xy + wrap][0] = 0;
487 s->current_picture.motion_val[0][xy + wrap][1] = 0;
488 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
489 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
490 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
491 s->current_picture.motion_val[1][xy + 1][0] = 0;
492 s->current_picture.motion_val[1][xy + 1][1] = 0;
493 s->current_picture.motion_val[1][xy + wrap][0] = 0;
494 s->current_picture.motion_val[1][xy + wrap][1] = 0;
495 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
496 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
501 off = ((n == 0) || (n == 1)) ? 1 : -1;
503 if (s->mb_x || (n == 1) || (n == 3)) {
504 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
505 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
506 A[0] = s->current_picture.motion_val[dir][xy - 1][0];
507 A[1] = s->current_picture.motion_val[dir][xy - 1][1];
509 } else { // current block has frame mv and cand. has field MV (so average)
510 A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
511 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
512 A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
513 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
516 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
522 /* Predict B and C */
523 B[0] = B[1] = C[0] = C[1] = 0;
524 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
525 if (!s->first_slice_line) {
526 if (!v->is_intra[s->mb_x - s->mb_stride]) {
529 pos_b = s->block_index[n_adj] - 2 * wrap;
530 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
531 n_adj = (n & 2) | (n & 1);
533 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
534 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
535 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
536 B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
537 B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
540 if (s->mb_width > 1) {
541 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
544 pos_c = s->block_index[2] - 2 * wrap + 2;
545 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
548 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
549 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
550 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
551 C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
552 C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
554 if (s->mb_x == s->mb_width - 1) {
555 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
558 pos_c = s->block_index[3] - 2 * wrap - 2;
559 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
562 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
563 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
564 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
565 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
566 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
575 pos_b = s->block_index[1];
577 B[0] = s->current_picture.motion_val[dir][pos_b][0];
578 B[1] = s->current_picture.motion_val[dir][pos_b][1];
579 pos_c = s->block_index[0];
581 C[0] = s->current_picture.motion_val[dir][pos_c][0];
582 C[1] = s->current_picture.motion_val[dir][pos_c][1];
585 total_valid = a_valid + b_valid + c_valid;
586 // check if predictor A is out of bounds
587 if (!s->mb_x && !(n == 1 || n == 3)) {
590 // check if predictor B is out of bounds
591 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
592 B[0] = B[1] = C[0] = C[1] = 0;
594 if (!v->blk_mv_type[xy]) {
595 if (s->mb_width == 1) {
599 if (total_valid >= 2) {
600 px = mid_pred(A[0], B[0], C[0]);
601 py = mid_pred(A[1], B[1], C[1]);
602 } else if (total_valid) {
603 if (a_valid) { px = A[0]; py = A[1]; }
604 else if (b_valid) { px = B[0]; py = B[1]; }
605 else { px = C[0]; py = C[1]; }
610 field_a = (A[1] & 4) ? 1 : 0;
614 field_b = (B[1] & 4) ? 1 : 0;
618 field_c = (C[1] & 4) ? 1 : 0;
622 num_oppfield = field_a + field_b + field_c;
623 num_samefield = total_valid - num_oppfield;
624 if (total_valid == 3) {
625 if ((num_samefield == 3) || (num_oppfield == 3)) {
626 px = mid_pred(A[0], B[0], C[0]);
627 py = mid_pred(A[1], B[1], C[1]);
628 } else if (num_samefield >= num_oppfield) {
629 /* take one MV from same field set depending on priority
630 the check for B may not be necessary */
631 px = !field_a ? A[0] : B[0];
632 py = !field_a ? A[1] : B[1];
634 px = field_a ? A[0] : B[0];
635 py = field_a ? A[1] : B[1];
637 } else if (total_valid == 2) {
638 if (num_samefield >= num_oppfield) {
639 if (!field_a && a_valid) {
642 } else if (!field_b && b_valid) {
645 } else /*if (c_valid)*/ {
651 if (field_a && a_valid) {
654 } else /*if (field_b && b_valid)*/ {
655 av_assert1(field_b && b_valid);
660 } else if (total_valid == 1) {
661 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
662 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
666 /* store MV using signed modulus of MV range defined in 4.11 */
667 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
668 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
669 if (mvn == 1) { /* duplicate motion data for 1-MV block */
670 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
671 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
672 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
673 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
674 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
675 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
676 } else if (mvn == 2) { /* duplicate motion data for 2-Field 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->mv[dir][n + 1][0] = s->mv[dir][n][0];
680 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
684 void ff_vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
685 int direct, int mvtype)
687 MpegEncContext *s = &v->s;
688 int xy, wrap, off = 0;
693 const uint8_t *is_intra = v->mb_type[0];
695 av_assert0(!v->field_mode);
699 /* scale MV difference to be quad-pel */
700 if (!s->quarter_sample) {
708 xy = s->block_index[0];
711 s->current_picture.motion_val[0][xy][0] =
712 s->current_picture.motion_val[0][xy][1] =
713 s->current_picture.motion_val[1][xy][0] =
714 s->current_picture.motion_val[1][xy][1] = 0;
717 if (direct && s->next_picture_ptr->field_picture)
718 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
720 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
721 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
722 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
723 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
725 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
726 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));
727 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));
728 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));
729 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));
731 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
732 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
733 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
734 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
738 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
739 C = s->current_picture.motion_val[0][xy - 2];
740 A = s->current_picture.motion_val[0][xy - wrap * 2];
741 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
742 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
744 if (!s->mb_x) C[0] = C[1] = 0;
745 if (!s->first_slice_line) { // predictor A is not out of bounds
746 if (s->mb_width == 1) {
750 px = mid_pred(A[0], B[0], C[0]);
751 py = mid_pred(A[1], B[1], C[1]);
753 } else if (s->mb_x) { // predictor C is not out of bounds
759 /* Pullback MV as specified in 8.3.5.3.4 */
762 int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
763 int MV = 4 - (1 << sh);
764 qx = (s->mb_x << sh);
765 qy = (s->mb_y << sh);
766 X = (s->mb_width << sh) - 4;
767 Y = (s->mb_height << sh) - 4;
768 if (qx + px < MV) px = MV - qx;
769 if (qy + py < MV) py = MV - qy;
770 if (qx + px > X) px = X - qx;
771 if (qy + py > Y) py = Y - qy;
773 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
774 if (0 && !s->first_slice_line && s->mb_x) {
775 if (is_intra[xy - wrap])
776 sum = FFABS(px) + FFABS(py);
778 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
780 if (get_bits1(&s->gb)) {
788 if (is_intra[xy - 2])
789 sum = FFABS(px) + FFABS(py);
791 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
793 if (get_bits1(&s->gb)) {
803 /* store MV using signed modulus of MV range defined in 4.11 */
804 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
805 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
807 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
808 C = s->current_picture.motion_val[1][xy - 2];
809 A = s->current_picture.motion_val[1][xy - wrap * 2];
810 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
811 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
815 if (!s->first_slice_line) { // predictor A is not out of bounds
816 if (s->mb_width == 1) {
820 px = mid_pred(A[0], B[0], C[0]);
821 py = mid_pred(A[1], B[1], C[1]);
823 } else if (s->mb_x) { // predictor C is not out of bounds
829 /* Pullback MV as specified in 8.3.5.3.4 */
832 int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
833 int MV = 4 - (1 << sh);
834 qx = (s->mb_x << sh);
835 qy = (s->mb_y << sh);
836 X = (s->mb_width << sh) - 4;
837 Y = (s->mb_height << sh) - 4;
838 if (qx + px < MV) px = MV - qx;
839 if (qy + py < MV) py = MV - qy;
840 if (qx + px > X) px = X - qx;
841 if (qy + py > Y) py = Y - qy;
843 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
844 if (0 && !s->first_slice_line && s->mb_x) {
845 if (is_intra[xy - wrap])
846 sum = FFABS(px) + FFABS(py);
848 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
850 if (get_bits1(&s->gb)) {
858 if (is_intra[xy - 2])
859 sum = FFABS(px) + FFABS(py);
861 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
863 if (get_bits1(&s->gb)) {
873 /* store MV using signed modulus of MV range defined in 4.11 */
875 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
876 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
878 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
879 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
880 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
881 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
884 void ff_vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y,
885 int mv1, int *pred_flag)
887 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
888 MpegEncContext *s = &v->s;
889 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
891 if (v->bmvtype == BMV_TYPE_DIRECT) {
893 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
894 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
895 v->bfraction, 0, s->quarter_sample);
896 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
897 v->bfraction, 0, s->quarter_sample);
898 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
899 v->bfraction, 1, s->quarter_sample);
900 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
901 v->bfraction, 1, s->quarter_sample);
903 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
904 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
905 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
906 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
907 f = (total_opp > 2) ? 1 : 0;
909 s->mv[0][0][0] = s->mv[0][0][1] = 0;
910 s->mv[1][0][0] = s->mv[1][0][1] = 0;
913 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
914 for (k = 0; k < 4; k++) {
915 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
916 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
917 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
918 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
919 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
920 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
924 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
925 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);
926 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);
929 if (dir) { // backward
930 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);
932 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);
935 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);
937 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);