1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... motion vector predicion
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * H.264 / AVC / MPEG4 part10 motion vector predicion.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #ifndef AVCODEC_H264_MVPRED_H
29 #define AVCODEC_H264_MVPRED_H
30
36
37
39 int i, int list, int part_width)
40 {
41 const int topright_ref = h->
ref_cache[list][i - 8 + part_width];
42
43 /* there is no consistent mapping of mvs to neighboring locations that will
44 * make mbaff happy, so we can't move all this logic to fill_caches */
46 #define SET_DIAG_MV(MV_OP, REF_OP, XY, Y4) \
47 const int xy = XY, y4 = Y4; \
48 const int mb_type = mb_types[xy + (y4 >> 2) * h->mb_stride]; \
49 if (!USES_LIST(mb_type, list)) \
50 return LIST_NOT_USED; \
51 mv = h->cur_pic_ptr->motion_val[list][h->mb2b_xy[xy] + 3 + y4 * h->b_stride]; \
52 h->mv_cache[list][scan8[0] - 2][0] = mv[0]; \
53 h->mv_cache[list][scan8[0] - 2][1] = mv[1] MV_OP; \
54 return h->cur_pic_ptr->ref_index[list][4 * xy + 1 + (y4 & ~1)] REF_OP;
55
57 && i >=
scan8[0] + 8 && (i & 7) == 4
63
66 (h->
mb_y & 1) * 2 + (i >> 5));
67 }
69 // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
71 }
72 }
73 #undef SET_DIAG_MV
74 }
75
77 *C = h->
mv_cache[list][i - 8 + part_width];
78 return topright_ref;
79 } else {
81
84 }
85 }
86
87 /**
88 * Get the predicted MV.
89 * @param n the block index
90 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
91 * @param mx the x component of the predicted motion vector
92 * @param my the y component of the predicted motion vector
93 */
95 int part_width, int list, int ref,
96 int *const mx, int *const my)
97 {
98 const int index8 =
scan8[
n];
99 const int top_ref = h->
ref_cache[list][index8 - 8];
100 const int left_ref = h->
ref_cache[list][index8 - 1];
101 const int16_t *
const A = h->
mv_cache[list][index8 - 1];
102 const int16_t *
const B = h->
mv_cache[list][index8 - 8];
104 int diagonal_ref, match_count;
105
106 av_assert2(part_width == 1 || part_width == 2 || part_width == 4);
107
108 /* mv_cache
109 * B . . A T T T T
110 * U . . L . . , .
111 * U . . L . . . .
112 * U . . L . . , .
113 * . . . L . . . .
114 */
115
117 match_count = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
118 tprintf(h->
avctx,
"pred_motion match_count=%d\n", match_count);
119 if (match_count > 1) { //most common
122 } else if (match_count == 1) {
123 if (left_ref == ref) {
124 *mx = A[0];
125 *my = A[1];
126 } else if (top_ref == ref) {
127 *mx = B[0];
128 *my = B[1];
129 } else {
130 *mx = C[0];
131 *my = C[1];
132 }
133 } else {
137 *mx = A[0];
138 *my = A[1];
139 } else {
142 }
143 }
144
146 "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n",
147 top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref,
148 A[0], A[1], ref, *mx, *my, h->
mb_x, h->
mb_y, n, list);
149 }
150
151 /**
152 * Get the directionally predicted 16x8 MV.
153 * @param n the block index
154 * @param mx the x component of the predicted motion vector
155 * @param my the y component of the predicted motion vector
156 */
158 int n,
int list,
int ref,
159 int *const mx, int *const my)
160 {
161 if (n == 0) {
164
165 tprintf(h->
avctx,
"pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
166 top_ref, B[0], B[1], h->
mb_x, h->
mb_y, n, list);
167
168 if (top_ref == ref) {
169 *mx = B[0];
170 *my = B[1];
171 return;
172 }
173 } else {
176
177 tprintf(h->
avctx,
"pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
178 left_ref, A[0], A[1], h->
mb_x, h->
mb_y, n, list);
179
180 if (left_ref == ref) {
181 *mx = A[0];
182 *my = A[1];
183 return;
184 }
185 }
186
187 //RARE
189 }
190
191 /**
192 * Get the directionally predicted 8x16 MV.
193 * @param n the block index
194 * @param mx the x component of the predicted motion vector
195 * @param my the y component of the predicted motion vector
196 */
198 int n,
int list,
int ref,
199 int *const mx, int *const my)
200 {
201 if (n == 0) {
204
205 tprintf(h->
avctx,
"pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
206 left_ref, A[0], A[1], h->
mb_x, h->
mb_y, n, list);
207
208 if (left_ref == ref) {
209 *mx = A[0];
210 *my = A[1];
211 return;
212 }
213 } else {
215 int diagonal_ref;
216
218
219 tprintf(h->
avctx,
"pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
220 diagonal_ref, C[0], C[1], h->
mb_x, h->
mb_y, n, list);
221
222 if (diagonal_ref == ref) {
223 *mx = C[0];
224 *my = C[1];
225 return;
226 }
227 }
228
229 //RARE
231 }
232
233 #define FIX_MV_MBAFF(type, refn, mvn, idx) \
234 if (FRAME_MBAFF(h)) { \
235 if (MB_FIELD(h)) { \
236 if (!IS_INTERLACED(type)) { \
237 refn <<= 1; \
238 AV_COPY32(mvbuf[idx], mvn); \
239 mvbuf[idx][1] /= 2; \
240 mvn = mvbuf[idx]; \
241 } \
242 } else { \
243 if (IS_INTERLACED(type)) { \
244 refn >>= 1; \
245 AV_COPY32(mvbuf[idx], mvn); \
246 mvbuf[idx][1] <<= 1; \
247 mvn = mvbuf[idx]; \
248 } \
249 } \
250 }
251
253 {
258 int top_ref, left_ref, diagonal_ref, match_count, mx, my;
259 const int16_t *
A, *
B, *
C;
261
263
264 /* To avoid doing an entire fill_decode_caches, we inline the relevant
265 * parts here.
266 * FIXME: this is a partial duplicate of the logic in fill_decode_caches,
267 * but it's faster this way. Is there a way to avoid this duplication?
268 */
274 goto zeromv;
277 A = zeromv;
278 } else {
279 goto zeromv;
280 }
281
287 goto zeromv;
290 B = zeromv;
291 } else {
292 goto zeromv;
293 }
294
296 top_ref, left_ref, h->
mb_x, h->
mb_y);
297
304 C = zeromv;
305 } else {
314 C = zeromv;
315 } else {
317 C = zeromv;
318 }
319 }
320
321 match_count = !diagonal_ref + !top_ref + !left_ref;
322 tprintf(h->
avctx,
"pred_pskip_motion match_count=%d\n", match_count);
323 if (match_count > 1) {
326 } else if (match_count == 1) {
327 if (!left_ref) {
328 mx = A[0];
329 my = A[1];
330 } else if (!top_ref) {
331 mx = B[0];
332 my = B[1];
333 } else {
334 mx = C[0];
335 my = C[1];
336 }
337 } else {
340 }
341
343 return;
344
345 zeromv:
347 return;
348 }
349
351 {
352 const int mb_xy = h->
mb_xy;
353 int topleft_xy, top_xy, topright_xy, left_xy[
LEFT_MBS];
354 static const uint8_t left_block_options[4][32] = {
355 { 0, 1, 2, 3, 7, 10, 8, 11, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 5 * 4, 1 + 9 * 4 },
356 { 2, 2, 3, 3, 8, 11, 8, 11, 3 + 2 * 4, 3 + 2 * 4, 3 + 3 * 4, 3 + 3 * 4, 1 + 5 * 4, 1 + 9 * 4, 1 + 5 * 4, 1 + 9 * 4 },
357 { 0, 0, 1, 1, 7, 10, 7, 10, 3 + 0 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 1 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 4 * 4, 1 + 8 * 4 },
358 { 0, 2, 0, 2, 7, 10, 7, 10, 3 + 0 * 4, 3 + 2 * 4, 3 + 0 * 4, 3 + 2 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 4 * 4, 1 + 8 * 4 }
359 };
360
362
364
365 /* Wow, what a mess, why didn't they simplify the interlacing & intra
366 * stuff, I can't imagine that these complex rules are worth it. */
367
368 topleft_xy = top_xy - 1;
369 topright_xy = top_xy + 1;
370 left_xy[
LBOT] = left_xy[
LTOP] = mb_xy - 1;
376 if (left_mb_field_flag != curr_mb_field_flag) {
378 if (curr_mb_field_flag) {
381 } else {
383 /* take top left mv from the middle of the mb, as opposed
384 * to all other modes which use the bottom right partition */
387 }
388 }
389 } else {
390 if (curr_mb_field_flag) {
394 }
395 if (left_mb_field_flag != curr_mb_field_flag) {
396 if (curr_mb_field_flag) {
399 } else {
401 }
402 }
403 }
404 }
405
411 //FIXME do we need all in the context?
412
418
426 } else {
433 }
434 }
437 }
438
440 {
441 int topleft_xy, top_xy, topright_xy, left_xy[
LEFT_MBS];
442 int topleft_type, top_type, topright_type, left_type[
LEFT_MBS];
444 int i;
447
458
466
467 if (!(top_type & type_mask)) {
471 }
474 if (!(left_type[LTOP] & type_mask)) {
477 }
478 if (!(left_type[
LBOT] & type_mask)) {
481 }
482 } else {
484
486 if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {
489 }
490 }
491 } else {
492 if (!(left_type[LTOP] & type_mask)) {
495 }
496 }
497
498 if (!(topleft_type & type_mask))
500
501 if (!(topright_type & type_mask))
503
507 } else {
512 }
513 for (i = 0; i < 2; i++) {
518 } else {
521 }
522 }
523 }
524 }
525
526 /*
527 * 0 . T T. T T T T
528 * 1 L . .L . . . .
529 * 2 L . .L . . . .
530 * 3 . T TL . . . .
531 * 4 L . .L . . . .
532 * 5 L . .. . . . .
533 */
534 /* FIXME: constraint_intra_pred & partitioning & nnz
535 * (let us hope this is just a typo in the spec) */
537 if (top_type) {
539 AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);
541 AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 7]);
542 AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);
543 } else {
544 AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 5]);
545 AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);
546 }
547 } else {
548 uint32_t top_empty =
CABAC(h) && !
IS_INTRA(mb_type) ? 0 : 0x40404040;
549 AV_WN32A(&nnz_cache[4 + 8 * 0], top_empty);
550 AV_WN32A(&nnz_cache[4 + 8 * 5], top_empty);
551 AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);
552 }
553
554 for (i = 0; i < 2; i++) {
555 if (left_type[
LEFT(i)]) {
557 nnz_cache[3 + 8 * 1 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i]];
558 nnz_cache[3 + 8 * 2 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i]];
560 nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 4 * 4];
561 nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 4 * 4];
562 nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 8 * 4];
563 nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 8 * 4];
565 nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 4 * 4];
566 nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 4 * 4];
567 nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 8 * 4];
568 nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 8 * 4];
569 } else {
570 nnz_cache[3 + 8 * 6 + 8 * i] = nnz[left_block[8 + 4 + 2 * i]];
571 nnz_cache[3 + 8 * 11 + 8 * i] = nnz[left_block[8 + 5 + 2 * i]];
572 }
573 } else {
574 nnz_cache[3 + 8 * 1 + 2 * 8 * i] =
575 nnz_cache[3 + 8 * 2 + 2 * 8 * i] =
576 nnz_cache[3 + 8 * 6 + 2 * 8 * i] =
577 nnz_cache[3 + 8 * 7 + 2 * 8 * i] =
578 nnz_cache[3 + 8 * 11 + 2 * 8 * i] =
579 nnz_cache[3 + 8 * 12 + 2 * 8 * i] =
CABAC(h) && !
IS_INTRA(mb_type) ? 0 : 64;
580 }
581 }
582
584 // top_cbp
585 if (top_type)
587 else
589 // left_cbp
590 if (left_type[
LTOP]) {
592 ((h->
cbp_table[left_xy[LTOP]] >> (left_block[0] & (~1))) & 2) |
593 (((h->
cbp_table[left_xy[
LBOT]] >> (left_block[2] & (~1))) & 2) << 2);
594 } else {
596 }
597 }
598 }
599
601 int list;
603 for (list = 0; list < h->
list_count; list++) {
609 continue;
611
613 const int b_xy = h->
mb2b_xy[top_xy] + 3 * b_stride;
615 ref_cache[0 - 1 * 8] =
616 ref_cache[1 - 1 * 8] = ref[4 * top_xy + 2];
617 ref_cache[2 - 1 * 8] =
618 ref_cache[3 - 1 * 8] = ref[4 * top_xy + 3];
619 } else {
623 }
624
626 for (i = 0; i < 2; i++) {
627 int cache_idx = -1 + i * 2 * 8;
630 const int b8_xy = 4 * left_xy[
LEFT(i)] + 1;
632 mv[b_xy + b_stride * left_block[0 + i * 2]]);
634 mv[b_xy + b_stride * left_block[1 + i * 2]]);
635 ref_cache[cache_idx] = ref[b8_xy + (left_block[0 + i * 2] & ~1)];
636 ref_cache[cache_idx + 8] = ref[b8_xy + (left_block[1 + i * 2] & ~1)];
637 } else {
640 ref_cache[cache_idx] =
643 }
644 }
645 } else {
648 const int b8_xy = 4 * left_xy[
LTOP] + 1;
649 AV_COPY32(mv_cache[-1],
mv[b_xy + b_stride * left_block[0]]);
650 ref_cache[-1] = ref[b8_xy + (left_block[0] & ~1)];
651 } else {
655 }
656 }
657
659 const int b_xy = h->
mb2b_xy[topright_xy] + 3 * b_stride;
661 ref_cache[4 - 1 * 8] = ref[4 * topright_xy + 2];
662 } else {
666 }
667 if(ref_cache[2 - 1*8] < 0 || ref_cache[4 - 1 * 8] < 0) {
669 const int b_xy = h->
mb2b_xy[topleft_xy] + 3 + b_stride +
673 ref_cache[-1 - 1 * 8] = ref[b8_xy];
674 } else {
678 }
679 }
680
682 continue;
683
687 ref_cache[2 + 8 * 0] =
691
694 const int b_xy = h->
mb2br_xy[top_xy];
695 AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);
696 } else {
698 }
701 AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);
702 AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);
703 } else {
706 }
709 AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);
710 AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);
711 } else {
714 }
721
725 }
else if (
IS_8X8(top_type)) {
726 int b8_xy = 4 * top_xy;
727 direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];
728 direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];
729 } else {
732 }
733
736 else if (
IS_8X8(left_type[LTOP]))
737 direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[
LTOP] + 1 + (left_block[0] & ~1)];
738 else
740
743 else if (
IS_8X8(left_type[LBOT]))
744 direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[LBOT] + 1 + (left_block[2] & ~1)];
745 else
747 }
748 }
749 }
750
751 #define MAP_MVS \
752 MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type) \
753 MAP_F2F(scan8[0] + 0 - 1 * 8, top_type) \
754 MAP_F2F(scan8[0] + 1 - 1 * 8, top_type) \
755 MAP_F2F(scan8[0] + 2 - 1 * 8, top_type) \
756 MAP_F2F(scan8[0] + 3 - 1 * 8, top_type) \
757 MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type) \
758 MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP]) \
759 MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP]) \
760 MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT]) \
761 MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])
762
765
766 #define MAP_F2F(idx, mb_type) \
767 if (!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) { \
768 h->ref_cache[list][idx] <<= 1; \
769 h->mv_cache[list][idx][1] /= 2; \
770 h->mvd_cache[list][idx][1] >>= 1; \
771 }
772
774 } else {
775
776 #undef MAP_F2F
777 #define MAP_F2F(idx, mb_type) \
778 if (IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) { \
779 h->ref_cache[list][idx] >>= 1; \
780 h->mv_cache[list][idx][1] <<= 1; \
781 h->mvd_cache[list][idx][1] <<= 1; \
782 }
783
785 #undef MAP_F2F
786 }
787 }
788 }
789 }
790
792 }
793
794 /**
795 * decodes a P_SKIP or B_SKIP macroblock
796 */
798 {
799 const int mb_xy = h->
mb_xy;
800 int mb_type = 0;
801
803
806
808 // just for fill_caches. pred_direct_motion will set the real mb_type
813 }
816 } else {
818
821 }
822
828 }
829
830 #endif /* AVCODEC_H264_MVPRED_H */