1 /*
2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4 * Copyright (c) 2006-2007 Konstantin Shishkov
5 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * VC-1 and WMV3 block decoding routines
27 */
28
35
37 {
38 int scaledvalue, refdist;
39 int scalesame1, scalesame2;
40 int scalezone1_x, zone1offset_x;
42
45 else
47 if (refdist > 3)
48 refdist = 3;
53
56 else {
57 if (
FFABS(n) < scalezone1_x)
58 scaledvalue = (n * scalesame1) >> 8;
59 else {
60 if (n < 0)
61 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
62 else
63 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
64 }
65 }
67 }
68
70 {
71 int scaledvalue, refdist;
72 int scalesame1, scalesame2;
73 int scalezone1_y, zone1offset_y;
75
78 else
80 if (refdist > 3)
81 refdist = 3;
86
89 else {
90 if (
FFABS(n) < scalezone1_y)
91 scaledvalue = (n * scalesame1) >> 8;
92 else {
93 if (n < 0)
94 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
95 else
96 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
97 }
98 }
99
102 else
104 }
105
107 {
108 int scalezone1_x, zone1offset_x;
109 int scaleopp1, scaleopp2, brfd;
110 int scaledvalue;
111
117
120 else {
121 if (
FFABS(n) < scalezone1_x)
122 scaledvalue = (n * scaleopp1) >> 8;
123 else {
124 if (n < 0)
125 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
126 else
127 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
128 }
129 }
131 }
132
134 {
135 int scalezone1_y, zone1offset_y;
136 int scaleopp1, scaleopp2, brfd;
137 int scaledvalue;
138
144
147 else {
148 if (
FFABS(n) < scalezone1_y)
149 scaledvalue = (n * scaleopp1) >> 8;
150 else {
151 if (n < 0)
152 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
153 else
154 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
155 }
156 }
159 } else {
161 }
162 }
163
166 {
167 int brfd, scalesame;
169
170 n >>= hpel;
172 if (dim)
174 else
177 }
180
181 n = (n * scalesame >> 8) << hpel;
183 }
184
187 {
188 int refdist, scaleopp;
190
191 n >>= hpel;
193 if (dim)
195 else
198 }
201 else
204
205 n = (n * scaleopp >> 8) * (1 << hpel);
207 }
208
209 /** Predict and set motion vector
210 */
212 int mv1,
int r_x,
int r_y,
uint8_t* is_intra,
213 int pred_flag, int dir)
214 {
216 int xy,
wrap, off = 0;
218 int px, py;
219 int sum;
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;
227
230 mixedmv_pic = 1;
231 else
232 mixedmv_pic = 0;
233 /* scale MV difference to be quad-pel */
235 dmv_x *= 2;
236 dmv_y *= 2;
237 }
238
241
247 if (mv1) { /* duplicate motion data for 1-MV block */
261 }
262 return;
263 }
264
267 if (mv1) {
270 else
272 } else {
273 //in 4-MV mode different blocks have different B predictor position
274 switch (n) {
275 case 0:
276 off = (s->
mb_x > 0) ? -1 : 1;
277 break;
278 case 1:
280 break;
281 case 2:
282 off = 1;
283 break;
284 case 3:
285 off = -1;
286 }
287 }
289
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];
297 }
298
299 if (a_valid) {
301 num_oppfield += a_f;
302 num_samefield += 1 - a_f;
303 field_predA[0] = A[0];
304 field_predA[1] = A[1];
305 } else {
306 field_predA[0] = field_predA[1] = 0;
307 a_f = 0;
308 }
309 if (b_valid) {
311 num_oppfield += b_f;
312 num_samefield += 1 - b_f;
313 field_predB[0] = B[0];
314 field_predB[1] = B[1];
315 } else {
316 field_predB[0] = field_predB[1] = 0;
317 b_f = 0;
318 }
319 if (c_valid) {
321 num_oppfield += c_f;
322 num_samefield += 1 - c_f;
323 field_predC[0] = C[0];
324 field_predC[1] = C[1];
325 } else {
326 field_predC[0] = field_predC[1] = 0;
327 c_f = 0;
328 }
329
332 // REFFIELD determines if the last field or the second-last field is
333 // to be used as reference
335 else {
336 if (num_samefield <= num_oppfield)
337 opposite = 1 - pred_flag;
338 else
339 opposite = pred_flag;
340 }
341 } else
342 opposite = 0;
343 if (opposite) {
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);
347 }
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);
351 }
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);
355 }
358 } else {
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);
362 }
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);
366 }
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);
370 }
373 }
374
375 if (a_valid) {
376 px = field_predA[0];
377 py = field_predA[1];
378 } else if (c_valid) {
379 px = field_predC[0];
380 py = field_predC[1];
381 } else if (b_valid) {
382 px = field_predB[0];
383 py = field_predB[1];
384 } else {
385 px = 0;
386 py = 0;
387 }
388
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]);
392 }
393
394 /* Pullback MV as specified in 8.3.5.3.4 */
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);
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;
406 }
407
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])
414 else
415 sum =
FFABS(px - field_predA[0]) +
FFABS(py - field_predA[1]);
416 if (sum > hybridmv_thresh) {
418 px = field_predA[0];
419 py = field_predA[1];
420 } else {
421 px = field_predC[0];
422 py = field_predC[1];
423 }
424 } else {
425 if (is_intra[xy - 1])
427 else
428 sum =
FFABS(px - field_predC[0]) +
FFABS(py - field_predC[1]);
429 if (sum > hybridmv_thresh) {
431 px = field_predA[0];
432 py = field_predA[1];
433 } else {
434 px = field_predC[0];
435 py = field_predC[1];
436 }
437 }
438 }
439 }
440 }
441
443 r_y >>= 1;
445 y_bias = 1;
446 /* store MV using signed modulus of MV range defined in 4.11 */
449 if (mv1) { /* duplicate motion data for 1-MV block */
458 }
459 }
460
461 /** Predict and set motion vector for interlaced frame picture MBs
462 */
464 int mvn,
int r_x,
int r_y,
uint8_t* is_intra,
int dir)
465 {
467 int xy,
wrap, off = 0;
468 int A[2],
B[2],
C[2];
469 int px = 0, py = 0;
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;
474
477
483 if (mvn == 1) { /* duplicate motion data for 1-MV block */
497 }
498 return;
499 }
500
501 off = ((n == 0) || (n == 1)) ? 1 : -1;
502 /* predict A */
503 if (s->
mb_x || (n == 1) || (n == 3)) {
504 if ((v->
blk_mv_type[xy])
// current block (MB) has a field MV
508 a_valid = 1;
509 } else { // current block has frame mv and cand. has field MV (so average)
514 a_valid = 1;
515 }
517 a_valid = 0;
518 A[0] = A[1] = 0;
519 }
520 } else
521 A[0] = A[1] = 0;
522 /* Predict B and C */
523 B[0] = B[1] = C[0] = C[1] = 0;
527 b_valid = 1;
528 n_adj = n | 2;
531 n_adj = (n & 2) | (n & 1);
532 }
538 }
539 }
542 c_valid = 1;
543 n_adj = 2;
546 n_adj = n & 2;
547 }
553 }
556 c_valid = 1;
557 n_adj = 3;
560 n_adj = n | 1;
561 }
567 }
568 } else
569 c_valid = 0;
570 }
571 }
572 }
573 }
574 } else {
576 b_valid = 1;
580 c_valid = 1;
583 }
584
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)) {
588 A[0] = A[1] = 0;
589 }
590 // check if predictor B is out of bounds
592 B[0] = B[1] = C[0] = C[1] = 0;
593 }
596 px = B[0];
597 py = B[1];
598 } else {
599 if (total_valid >= 2) {
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]; }
606 }
607 }
608 } else {
609 if (a_valid)
610 field_a = (A[1] & 4) ? 1 : 0;
611 else
612 field_a = 0;
613 if (b_valid)
614 field_b = (B[1] & 4) ? 1 : 0;
615 else
616 field_b = 0;
617 if (c_valid)
618 field_c = (C[1] & 4) ? 1 : 0;
619 else
620 field_c = 0;
621
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)) {
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];
633 } else {
634 px = field_a ? A[0] : B[0];
635 py = field_a ? A[1] : B[1];
636 }
637 } else if (total_valid == 2) {
638 if (num_samefield >= num_oppfield) {
639 if (!field_a && a_valid) {
640 px = A[0];
641 py = A[1];
642 } else if (!field_b && b_valid) {
643 px = B[0];
644 py = B[1];
645 } else /*if (c_valid)*/ {
647 px = C[0];
648 py = C[1];
649 }
650 } else {
651 if (field_a && a_valid) {
652 px = A[0];
653 py = A[1];
654 } else /*if (field_b && b_valid)*/ {
656 px = B[0];
657 py = B[1];
658 }
659 }
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]);
663 }
664 }
665
666 /* store MV using signed modulus of MV range defined in 4.11 */
669 if (mvn == 1) { /* duplicate motion data for 1-MV block */
676 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
679 s->
mv[dir][n + 1][0] = s->
mv[dir][
n][0];
680 s->
mv[dir][n + 1][1] = s->
mv[dir][
n][1];
681 }
682 }
683
685 int direct, int mvtype)
686 {
688 int xy,
wrap, off = 0;
690 int px, py;
691 int sum;
692 int r_x, r_y;
694
696
699 /* scale MV difference to be quad-pel */
701 dmv_x[0] *= 2;
702 dmv_y[0] *= 2;
703 dmv_x[1] *= 2;
704 dmv_y[1] *= 2;
705 }
706
709
715 return;
716 }
719
724
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));
730 if (direct) {
735 return;
736 }
737
743
744 if (!s->
mb_x) C[0] = C[1] = 0;
747 px = A[0];
748 py = A[1];
749 } else {
752 }
753 }
else if (s->
mb_x) {
// predictor C is not out of bounds
754 px = C[0];
755 py = C[1];
756 } else {
757 px = py = 0;
758 }
759 /* Pullback MV as specified in 8.3.5.3.4 */
760 {
763 int MV = 4 - (1 << sh);
764 qx = (s->
mb_x << sh);
765 qy = (s->
mb_y << sh);
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;
772 }
773 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
775 if (is_intra[xy - wrap])
777 else
779 if (sum > 32) {
781 px = A[0];
782 py = A[1];
783 } else {
784 px = C[0];
785 py = C[1];
786 }
787 } else {
788 if (is_intra[xy - 2])
790 else
792 if (sum > 32) {
794 px = A[0];
795 py = A[1];
796 } else {
797 px = C[0];
798 py = C[1];
799 }
800 }
801 }
802 }
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;
806 }
812
814 C[0] = C[1] = 0;
817 px = A[0];
818 py = A[1];
819 } else {
822 }
823 }
else if (s->
mb_x) {
// predictor C is not out of bounds
824 px = C[0];
825 py = C[1];
826 } else {
827 px = py = 0;
828 }
829 /* Pullback MV as specified in 8.3.5.3.4 */
830 {
833 int MV = 4 - (1 << sh);
834 qx = (s->
mb_x << sh);
835 qy = (s->
mb_y << sh);
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;
842 }
843 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
845 if (is_intra[xy - wrap])
847 else
849 if (sum > 32) {
851 px = A[0];
852 py = A[1];
853 } else {
854 px = C[0];
855 py = C[1];
856 }
857 } else {
858 if (is_intra[xy - 2])
860 else
862 if (sum > 32) {
864 px = A[0];
865 py = A[1];
866 } else {
867 px = C[0];
868 py = C[1];
869 }
870 }
871 }
872 }
873 /* store MV using signed modulus of MV range defined in 4.11 */
874
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;
877 }
882 }
883
885 int mv1, int *pred_flag)
886 {
890
892 int total_opp, k, f;
902
907 f = (total_opp > 2) ? 1 : 0;
908 } else {
909 s->
mv[0][0][0] = s->
mv[0][0][1] = 0;
910 s->
mv[1][0][0] = s->
mv[1][0][1] = 0;
911 f = 0;
912 }
914 for (k = 0; k < 4; k++) {
921 }
922 return;
923 }
927 return;
928 }
929 if (dir) { // backward
931 if (n == 3 || mv1) {
933 }
934 } else { // forward
936 if (n == 3 || mv1) {
938 }
939 }
940 }
void ff_vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t *is_intra, int pred_flag, int dir)
Predict and set motion vector.
int reffield
if numref = 0 (1 reference) then reffield decides which
static av_always_inline int scaleforopp_x(VC1Context *v, int n)
#define AV_LOG_WARNING
Something somehow does not look correct.
static av_always_inline int scaleforsame(VC1Context *v, int i, int n, int dim, int dir)
int field_picture
whether or not the picture was encoded in separate fields
uint8_t * mv_f[2]
0: MV obtained from same field, 1: opposite field
const uint16_t ff_vc1_b_field_mvpred_scales[7][4]
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int refdist
distance of the current picture from reference
void ff_vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
static av_always_inline int scaleforopp_y(VC1Context *v, int n, int dir)
Picture current_picture
copy of the current picture structure.
int16_t bfraction
Relative position % anchors=> how to scale MVs.
int profile
Sequence header data for all Profiles TODO: choose between ints, uint8_ts and monobit flags...
int mb_height
number of MBs horizontally & vertically
static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n, int dir)
int quarter_sample
1->qpel, 0->half pel ME/MC
uint8_t * blk_mv_type
0: frame MV, 1: field MV (interlaced frame)
int cur_field_type
0: top, 1: bottom
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
int field_mode
1 for interlaced field pictures
uint8_t mv_mode
Frame decoding info for all profiles.
int16_t(*[2] motion_val)[2]
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
const uint16_t ff_vc1_field_mvpred_scales[2][7][4]
int block_index[6]
index to current MB in block based arrays with edges
int first_slice_line
used in MPEG-4 too to handle resync markers
static av_always_inline int scaleforsame_x(VC1Context *v, int n, int dir)
void ff_vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
static unsigned int get_bits1(GetBitContext *s)
int ref_field_type[2]
forward and backward reference field type (top or bottom)
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
int numref
number of past field pictures used as reference
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Picture * next_picture_ptr
pointer to the next picture (for bidir pred)
struct AVCodecContext * avctx
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
void ff_vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y, int mvn, int r_x, int r_y, uint8_t *is_intra, int dir)
Predict and set motion vector for interlaced frame picture MBs.
Picture next_picture
copy of the next picture structure.
int brfd
reference frame distance (forward or backward)
uint32_t * mb_type
types and macros are defined in mpegutils.h
uint8_t mv_mode2
Secondary MV coding mode (B-frames)
static av_always_inline int scaleforopp(VC1Context *v, int n, int dim, int dir)