2 * H.26L/H.264/AVC/JVT/14496-10/... motion vector predicion
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
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.
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.
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
24 * H.264 / AVC / MPEG4 part10 motion vector predicion.
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #ifndef AVCODEC_H264_MVPRED_H
29 #define AVCODEC_H264_MVPRED_H
34 #include "mpegutils.h"
35 #include "libavutil/avassert.h"
38 static av_always_inline
int fetch_diagonal_mv(H264Context
*h
, const int16_t **C
,
39 int i
, int list
, int part_width
)
41 const int topright_ref
= h
->ref_cache
[list
][i
- 8 + part_width
];
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;
56 if (topright_ref
== PART_NOT_AVAILABLE
57 && i
>= scan8
[0] + 8 && (i
& 7) == 4
58 && h
->ref_cache
[list
][scan8
[0] - 1] != PART_NOT_AVAILABLE
) {
59 const uint32_t *mb_types
= h
->cur_pic_ptr
->mb_type
;
61 AV_ZERO32(h
->mv_cache
[list
][scan8
[0] - 2]);
62 *C
= h
->mv_cache
[list
][scan8
[0] - 2];
64 if (!MB_FIELD(h
) && IS_INTERLACED(h
->left_type
[0])) {
65 SET_DIAG_MV(* 2, >> 1, h
->left_mb_xy
[0] + h
->mb_stride
,
66 (h
->mb_y
& 1) * 2 + (i
>> 5));
68 if (MB_FIELD(h
) && !IS_INTERLACED(h
->left_type
[0])) {
69 // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
70 SET_DIAG_MV(/ 2, << 1, h
->left_mb_xy
[i
>= 36], ((i
>> 2)) & 3);
76 if (topright_ref
!= PART_NOT_AVAILABLE
) {
77 *C
= h
->mv_cache
[list
][i
- 8 + part_width
];
80 tprintf(h
->avctx
, "topright MV not available\n");
82 *C
= h
->mv_cache
[list
][i
- 8 - 1];
83 return h
->ref_cache
[list
][i
- 8 - 1];
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
94 static av_always_inline
void pred_motion(H264Context
*const h
, int n
,
95 int part_width
, int list
, int ref
,
96 int *const mx
, int *const my
)
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
;
106 av_assert2(part_width
== 1 || part_width
== 2 || part_width
== 4);
116 diagonal_ref
= fetch_diagonal_mv(h
, &C
, index8
, list
, part_width
);
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
120 *mx
= mid_pred(A
[0], B
[0], C
[0]);
121 *my
= mid_pred(A
[1], B
[1], C
[1]);
122 } else if (match_count
== 1) {
123 if (left_ref
== ref
) {
126 } else if (top_ref
== ref
) {
134 if (top_ref
== PART_NOT_AVAILABLE
&&
135 diagonal_ref
== PART_NOT_AVAILABLE
&&
136 left_ref
!= PART_NOT_AVAILABLE
) {
140 *mx
= mid_pred(A
[0], B
[0], C
[0]);
141 *my
= mid_pred(A
[1], B
[1], C
[1]);
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
);
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
157 static av_always_inline
void pred_16x8_motion(H264Context
*const h
,
158 int n
, int list
, int ref
,
159 int *const mx
, int *const my
)
162 const int top_ref
= h
->ref_cache
[list
][scan8
[0] - 8];
163 const int16_t *const B
= h
->mv_cache
[list
][scan8
[0] - 8];
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
);
168 if (top_ref
== ref
) {
174 const int left_ref
= h
->ref_cache
[list
][scan8
[8] - 1];
175 const int16_t *const A
= h
->mv_cache
[list
][scan8
[8] - 1];
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
);
180 if (left_ref
== ref
) {
188 pred_motion(h
, n
, 4, list
, ref
, mx
, my
);
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
197 static av_always_inline
void pred_8x16_motion(H264Context
*const h
,
198 int n
, int list
, int ref
,
199 int *const mx
, int *const my
)
202 const int left_ref
= h
->ref_cache
[list
][scan8
[0] - 1];
203 const int16_t *const A
= h
->mv_cache
[list
][scan8
[0] - 1];
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
);
208 if (left_ref
== ref
) {
217 diagonal_ref
= fetch_diagonal_mv(h
, &C
, scan8
[4], list
, 2);
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
);
222 if (diagonal_ref
== ref
) {
230 pred_motion(h
, n
, 2, list
, ref
, mx
, my
);
233 #define FIX_MV_MBAFF(type, refn, mvn, idx) \
234 if (FRAME_MBAFF(h)) { \
236 if (!IS_INTERLACED(type)) { \
238 AV_COPY32(mvbuf[idx], mvn); \
239 mvbuf[idx][1] /= 2; \
243 if (IS_INTERLACED(type)) { \
245 AV_COPY32(mvbuf[idx], mvn); \
246 mvbuf[idx][1] <<= 1; \
252 static av_always_inline
void pred_pskip_motion(H264Context
*const h
)
254 DECLARE_ALIGNED(4, static const int16_t, zeromv
)[2] = { 0 };
255 DECLARE_ALIGNED(4, int16_t, mvbuf
)[3][2];
256 int8_t *ref
= h
->cur_pic
.ref_index
[0];
257 int16_t(*mv
)[2] = h
->cur_pic
.motion_val
[0];
258 int top_ref
, left_ref
, diagonal_ref
, match_count
, mx
, my
;
259 const int16_t *A
, *B
, *C
;
260 int b_stride
= h
->b_stride
;
262 fill_rectangle(&h
->ref_cache
[0][scan8
[0]], 4, 4, 8, 0, 1);
264 /* To avoid doing an entire fill_decode_caches, we inline the relevant
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?
269 if (USES_LIST(h
->left_type
[LTOP
], 0)) {
270 left_ref
= ref
[4 * h
->left_mb_xy
[LTOP
] + 1 + (h
->left_block
[0] & ~1)];
271 A
= mv
[h
->mb2b_xy
[h
->left_mb_xy
[LTOP
]] + 3 + b_stride
* h
->left_block
[0]];
272 FIX_MV_MBAFF(h
->left_type
[LTOP
], left_ref
, A
, 0);
273 if (!(left_ref
| AV_RN32A(A
)))
275 } else if (h
->left_type
[LTOP
]) {
276 left_ref
= LIST_NOT_USED
;
282 if (USES_LIST(h
->top_type
, 0)) {
283 top_ref
= ref
[4 * h
->top_mb_xy
+ 2];
284 B
= mv
[h
->mb2b_xy
[h
->top_mb_xy
] + 3 * b_stride
];
285 FIX_MV_MBAFF(h
->top_type
, top_ref
, B
, 1);
286 if (!(top_ref
| AV_RN32A(B
)))
288 } else if (h
->top_type
) {
289 top_ref
= LIST_NOT_USED
;
295 tprintf(h
->avctx
, "pred_pskip: (%d) (%d) at %2d %2d\n",
296 top_ref
, left_ref
, h
->mb_x
, h
->mb_y
);
298 if (USES_LIST(h
->topright_type
, 0)) {
299 diagonal_ref
= ref
[4 * h
->topright_mb_xy
+ 2];
300 C
= mv
[h
->mb2b_xy
[h
->topright_mb_xy
] + 3 * b_stride
];
301 FIX_MV_MBAFF(h
->topright_type
, diagonal_ref
, C
, 2);
302 } else if (h
->topright_type
) {
303 diagonal_ref
= LIST_NOT_USED
;
306 if (USES_LIST(h
->topleft_type
, 0)) {
307 diagonal_ref
= ref
[4 * h
->topleft_mb_xy
+ 1 +
308 (h
->topleft_partition
& 2)];
309 C
= mv
[h
->mb2b_xy
[h
->topleft_mb_xy
] + 3 + b_stride
+
310 (h
->topleft_partition
& 2 * b_stride
)];
311 FIX_MV_MBAFF(h
->topleft_type
, diagonal_ref
, C
, 2);
312 } else if (h
->topleft_type
) {
313 diagonal_ref
= LIST_NOT_USED
;
316 diagonal_ref
= PART_NOT_AVAILABLE
;
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) {
324 mx
= mid_pred(A
[0], B
[0], C
[0]);
325 my
= mid_pred(A
[1], B
[1], C
[1]);
326 } else if (match_count
== 1) {
330 } else if (!top_ref
) {
338 mx
= mid_pred(A
[0], B
[0], C
[0]);
339 my
= mid_pred(A
[1], B
[1], C
[1]);
342 fill_rectangle(h
->mv_cache
[0][scan8
[0]], 4, 4, 8, pack16to32(mx
, my
), 4);
346 fill_rectangle(h
->mv_cache
[0][scan8
[0]], 4, 4, 8, 0, 4);
350 static void fill_decode_neighbors(H264Context
*h
, int mb_type
)
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 }
361 h
->topleft_partition
= -1;
363 top_xy
= mb_xy
- (h
->mb_stride
<< MB_FIELD(h
));
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. */
368 topleft_xy
= top_xy
- 1;
369 topright_xy
= top_xy
+ 1;
370 left_xy
[LBOT
] = left_xy
[LTOP
] = mb_xy
- 1;
371 h
->left_block
= left_block_options
[0];
372 if (FRAME_MBAFF(h
)) {
373 const int left_mb_field_flag
= IS_INTERLACED(h
->cur_pic
.mb_type
[mb_xy
- 1]);
374 const int curr_mb_field_flag
= IS_INTERLACED(mb_type
);
376 if (left_mb_field_flag
!= curr_mb_field_flag
) {
377 left_xy
[LBOT
] = left_xy
[LTOP
] = mb_xy
- h
->mb_stride
- 1;
378 if (curr_mb_field_flag
) {
379 left_xy
[LBOT
] += h
->mb_stride
;
380 h
->left_block
= left_block_options
[3];
382 topleft_xy
+= h
->mb_stride
;
383 /* take top left mv from the middle of the mb, as opposed
384 * to all other modes which use the bottom right partition */
385 h
->topleft_partition
= 0;
386 h
->left_block
= left_block_options
[1];
390 if (curr_mb_field_flag
) {
391 topleft_xy
+= h
->mb_stride
& (((h
->cur_pic
.mb_type
[top_xy
- 1] >> 7) & 1) - 1);
392 topright_xy
+= h
->mb_stride
& (((h
->cur_pic
.mb_type
[top_xy
+ 1] >> 7) & 1) - 1);
393 top_xy
+= h
->mb_stride
& (((h
->cur_pic
.mb_type
[top_xy
] >> 7) & 1) - 1);
395 if (left_mb_field_flag
!= curr_mb_field_flag
) {
396 if (curr_mb_field_flag
) {
397 left_xy
[LBOT
] += h
->mb_stride
;
398 h
->left_block
= left_block_options
[3];
400 h
->left_block
= left_block_options
[2];
406 h
->topleft_mb_xy
= topleft_xy
;
407 h
->top_mb_xy
= top_xy
;
408 h
->topright_mb_xy
= topright_xy
;
409 h
->left_mb_xy
[LTOP
] = left_xy
[LTOP
];
410 h
->left_mb_xy
[LBOT
] = left_xy
[LBOT
];
411 //FIXME do we need all in the context?
413 h
->topleft_type
= h
->cur_pic
.mb_type
[topleft_xy
];
414 h
->top_type
= h
->cur_pic
.mb_type
[top_xy
];
415 h
->topright_type
= h
->cur_pic
.mb_type
[topright_xy
];
416 h
->left_type
[LTOP
] = h
->cur_pic
.mb_type
[left_xy
[LTOP
]];
417 h
->left_type
[LBOT
] = h
->cur_pic
.mb_type
[left_xy
[LBOT
]];
420 if (h
->slice_table
[topleft_xy
] != h
->slice_num
)
422 if (h
->slice_table
[top_xy
] != h
->slice_num
)
424 if (h
->slice_table
[left_xy
[LTOP
]] != h
->slice_num
)
425 h
->left_type
[LTOP
] = h
->left_type
[LBOT
] = 0;
427 if (h
->slice_table
[topleft_xy
] != h
->slice_num
) {
429 if (h
->slice_table
[top_xy
] != h
->slice_num
)
431 if (h
->slice_table
[left_xy
[LTOP
]] != h
->slice_num
)
432 h
->left_type
[LTOP
] = h
->left_type
[LBOT
] = 0;
435 if (h
->slice_table
[topright_xy
] != h
->slice_num
)
436 h
->topright_type
= 0;
439 static void fill_decode_caches(H264Context
*h
, int mb_type
)
441 int topleft_xy
, top_xy
, topright_xy
, left_xy
[LEFT_MBS
];
442 int topleft_type
, top_type
, topright_type
, left_type
[LEFT_MBS
];
443 const uint8_t *left_block
= h
->left_block
;
448 topleft_xy
= h
->topleft_mb_xy
;
449 top_xy
= h
->top_mb_xy
;
450 topright_xy
= h
->topright_mb_xy
;
451 left_xy
[LTOP
] = h
->left_mb_xy
[LTOP
];
452 left_xy
[LBOT
] = h
->left_mb_xy
[LBOT
];
453 topleft_type
= h
->topleft_type
;
454 top_type
= h
->top_type
;
455 topright_type
= h
->topright_type
;
456 left_type
[LTOP
] = h
->left_type
[LTOP
];
457 left_type
[LBOT
] = h
->left_type
[LBOT
];
459 if (!IS_SKIP(mb_type
)) {
460 if (IS_INTRA(mb_type
)) {
461 int type_mask
= h
->pps
.constrained_intra_pred
? IS_INTRA(-1) : -1;
462 h
->topleft_samples_available
=
463 h
->top_samples_available
=
464 h
->left_samples_available
= 0xFFFF;
465 h
->topright_samples_available
= 0xEEEA;
467 if (!(top_type
& type_mask
)) {
468 h
->topleft_samples_available
= 0xB3FF;
469 h
->top_samples_available
= 0x33FF;
470 h
->topright_samples_available
= 0x26EA;
472 if (IS_INTERLACED(mb_type
) != IS_INTERLACED(left_type
[LTOP
])) {
473 if (IS_INTERLACED(mb_type
)) {
474 if (!(left_type
[LTOP
] & type_mask
)) {
475 h
->topleft_samples_available
&= 0xDFFF;
476 h
->left_samples_available
&= 0x5FFF;
478 if (!(left_type
[LBOT
] & type_mask
)) {
479 h
->topleft_samples_available
&= 0xFF5F;
480 h
->left_samples_available
&= 0xFF5F;
483 int left_typei
= h
->cur_pic
.mb_type
[left_xy
[LTOP
] + h
->mb_stride
];
485 av_assert2(left_xy
[LTOP
] == left_xy
[LBOT
]);
486 if (!((left_typei
& type_mask
) && (left_type
[LTOP
] & type_mask
))) {
487 h
->topleft_samples_available
&= 0xDF5F;
488 h
->left_samples_available
&= 0x5F5F;
492 if (!(left_type
[LTOP
] & type_mask
)) {
493 h
->topleft_samples_available
&= 0xDF5F;
494 h
->left_samples_available
&= 0x5F5F;
498 if (!(topleft_type
& type_mask
))
499 h
->topleft_samples_available
&= 0x7FFF;
501 if (!(topright_type
& type_mask
))
502 h
->topright_samples_available
&= 0xFBFF;
504 if (IS_INTRA4x4(mb_type
)) {
505 if (IS_INTRA4x4(top_type
)) {
506 AV_COPY32(h
->intra4x4_pred_mode_cache
+ 4 + 8 * 0, h
->intra4x4_pred_mode
+ h
->mb2br_xy
[top_xy
]);
508 h
->intra4x4_pred_mode_cache
[4 + 8 * 0] =
509 h
->intra4x4_pred_mode_cache
[5 + 8 * 0] =
510 h
->intra4x4_pred_mode_cache
[6 + 8 * 0] =
511 h
->intra4x4_pred_mode_cache
[7 + 8 * 0] = 2 - 3 * !(top_type
& type_mask
);
513 for (i
= 0; i
< 2; i
++) {
514 if (IS_INTRA4x4(left_type
[LEFT(i
)])) {
515 int8_t *mode
= h
->intra4x4_pred_mode
+ h
->mb2br_xy
[left_xy
[LEFT(i
)]];
516 h
->intra4x4_pred_mode_cache
[3 + 8 * 1 + 2 * 8 * i
] = mode
[6 - left_block
[0 + 2 * i
]];
517 h
->intra4x4_pred_mode_cache
[3 + 8 * 2 + 2 * 8 * i
] = mode
[6 - left_block
[1 + 2 * i
]];
519 h
->intra4x4_pred_mode_cache
[3 + 8 * 1 + 2 * 8 * i
] =
520 h
->intra4x4_pred_mode_cache
[3 + 8 * 2 + 2 * 8 * i
] = 2 - 3 * !(left_type
[LEFT(i
)] & type_mask
);
534 /* FIXME: constraint_intra_pred & partitioning & nnz
535 * (let us hope this is just a typo in the spec) */
536 nnz_cache
= h
->non_zero_count_cache
;
538 nnz
= h
->non_zero_count
[top_xy
];
539 AV_COPY32(&nnz_cache
[4 + 8 * 0], &nnz
[4 * 3]);
540 if (!h
->chroma_y_shift
) {
541 AV_COPY32(&nnz_cache
[4 + 8 * 5], &nnz
[4 * 7]);
542 AV_COPY32(&nnz_cache
[4 + 8 * 10], &nnz
[4 * 11]);
544 AV_COPY32(&nnz_cache
[4 + 8 * 5], &nnz
[4 * 5]);
545 AV_COPY32(&nnz_cache
[4 + 8 * 10], &nnz
[4 * 9]);
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
);
554 for (i
= 0; i
< 2; i
++) {
555 if (left_type
[LEFT(i
)]) {
556 nnz
= h
->non_zero_count
[left_xy
[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];
564 } else if (CHROMA422(h
)) {
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];
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
]];
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;
586 h
->top_cbp
= h
->cbp_table
[top_xy
];
588 h
->top_cbp
= IS_INTRA(mb_type
) ? 0x7CF : 0x00F;
590 if (left_type
[LTOP
]) {
591 h
->left_cbp
= (h
->cbp_table
[left_xy
[LTOP
]] & 0x7F0) |
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);
595 h
->left_cbp
= IS_INTRA(mb_type
) ? 0x7CF : 0x00F;
600 if (IS_INTER(mb_type
) || (IS_DIRECT(mb_type
) && h
->direct_spatial_mv_pred
)) {
602 int b_stride
= h
->b_stride
;
603 for (list
= 0; list
< h
->list_count
; list
++) {
604 int8_t *ref_cache
= &h
->ref_cache
[list
][scan8
[0]];
605 int8_t *ref
= h
->cur_pic
.ref_index
[list
];
606 int16_t(*mv_cache
)[2] = &h
->mv_cache
[list
][scan8
[0]];
607 int16_t(*mv
)[2] = h
->cur_pic
.motion_val
[list
];
608 if (!USES_LIST(mb_type
, list
))
610 av_assert2(!(IS_DIRECT(mb_type
) && !h
->direct_spatial_mv_pred
));
612 if (USES_LIST(top_type
, list
)) {
613 const int b_xy
= h
->mb2b_xy
[top_xy
] + 3 * b_stride
;
614 AV_COPY128(mv_cache
[0 - 1 * 8], mv
[b_xy
+ 0]);
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];
620 AV_ZERO128(mv_cache
[0 - 1 * 8]);
621 AV_WN32A(&ref_cache
[0 - 1 * 8],
622 ((top_type
? LIST_NOT_USED
: PART_NOT_AVAILABLE
) & 0xFF) * 0x01010101u
);
625 if (mb_type
& (MB_TYPE_16x8
| MB_TYPE_8x8
)) {
626 for (i
= 0; i
< 2; i
++) {
627 int cache_idx
= -1 + i
* 2 * 8;
628 if (USES_LIST(left_type
[LEFT(i
)], list
)) {
629 const int b_xy
= h
->mb2b_xy
[left_xy
[LEFT(i
)]] + 3;
630 const int b8_xy
= 4 * left_xy
[LEFT(i
)] + 1;
631 AV_COPY32(mv_cache
[cache_idx
],
632 mv
[b_xy
+ b_stride
* left_block
[0 + i
* 2]]);
633 AV_COPY32(mv_cache
[cache_idx
+ 8],
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)];
638 AV_ZERO32(mv_cache
[cache_idx
]);
639 AV_ZERO32(mv_cache
[cache_idx
+ 8]);
640 ref_cache
[cache_idx
] =
641 ref_cache
[cache_idx
+ 8] = (left_type
[LEFT(i
)]) ? LIST_NOT_USED
642 : PART_NOT_AVAILABLE
;
646 if (USES_LIST(left_type
[LTOP
], list
)) {
647 const int b_xy
= h
->mb2b_xy
[left_xy
[LTOP
]] + 3;
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)];
652 AV_ZERO32(mv_cache
[-1]);
653 ref_cache
[-1] = left_type
[LTOP
] ? LIST_NOT_USED
654 : PART_NOT_AVAILABLE
;
658 if (USES_LIST(topright_type
, list
)) {
659 const int b_xy
= h
->mb2b_xy
[topright_xy
] + 3 * b_stride
;
660 AV_COPY32(mv_cache
[4 - 1 * 8], mv
[b_xy
]);
661 ref_cache
[4 - 1 * 8] = ref
[4 * topright_xy
+ 2];
663 AV_ZERO32(mv_cache
[4 - 1 * 8]);
664 ref_cache
[4 - 1 * 8] = topright_type
? LIST_NOT_USED
665 : PART_NOT_AVAILABLE
;
667 if(ref_cache
[2 - 1*8] < 0 || ref_cache
[4 - 1 * 8] < 0) {
668 if (USES_LIST(topleft_type
, list
)) {
669 const int b_xy
= h
->mb2b_xy
[topleft_xy
] + 3 + b_stride
+
670 (h
->topleft_partition
& 2 * b_stride
);
671 const int b8_xy
= 4 * topleft_xy
+ 1 + (h
->topleft_partition
& 2);
672 AV_COPY32(mv_cache
[-1 - 1 * 8], mv
[b_xy
]);
673 ref_cache
[-1 - 1 * 8] = ref
[b8_xy
];
675 AV_ZERO32(mv_cache
[-1 - 1 * 8]);
676 ref_cache
[-1 - 1 * 8] = topleft_type
? LIST_NOT_USED
677 : PART_NOT_AVAILABLE
;
681 if ((mb_type
& (MB_TYPE_SKIP
| MB_TYPE_DIRECT2
)) && !FRAME_MBAFF(h
))
684 if (!(mb_type
& (MB_TYPE_SKIP
| MB_TYPE_DIRECT2
))) {
685 uint8_t(*mvd_cache
)[2] = &h
->mvd_cache
[list
][scan8
[0]];
686 uint8_t(*mvd
)[2] = h
->mvd_table
[list
];
687 ref_cache
[2 + 8 * 0] =
688 ref_cache
[2 + 8 * 2] = PART_NOT_AVAILABLE
;
689 AV_ZERO32(mv_cache
[2 + 8 * 0]);
690 AV_ZERO32(mv_cache
[2 + 8 * 2]);
693 if (USES_LIST(top_type
, list
)) {
694 const int b_xy
= h
->mb2br_xy
[top_xy
];
695 AV_COPY64(mvd_cache
[0 - 1 * 8], mvd
[b_xy
+ 0]);
697 AV_ZERO64(mvd_cache
[0 - 1 * 8]);
699 if (USES_LIST(left_type
[LTOP
], list
)) {
700 const int b_xy
= h
->mb2br_xy
[left_xy
[LTOP
]] + 6;
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]]);
704 AV_ZERO16(mvd_cache
[-1 + 0 * 8]);
705 AV_ZERO16(mvd_cache
[-1 + 1 * 8]);
707 if (USES_LIST(left_type
[LBOT
], list
)) {
708 const int b_xy
= h
->mb2br_xy
[left_xy
[LBOT
]] + 6;
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]]);
712 AV_ZERO16(mvd_cache
[-1 + 2 * 8]);
713 AV_ZERO16(mvd_cache
[-1 + 3 * 8]);
715 AV_ZERO16(mvd_cache
[2 + 8 * 0]);
716 AV_ZERO16(mvd_cache
[2 + 8 * 2]);
717 if (h
->slice_type_nos
== AV_PICTURE_TYPE_B
) {
718 uint8_t *direct_cache
= &h
->direct_cache
[scan8
[0]];
719 uint8_t *direct_table
= h
->direct_table
;
720 fill_rectangle(direct_cache
, 4, 4, 8, MB_TYPE_16x16
>> 1, 1);
722 if (IS_DIRECT(top_type
)) {
723 AV_WN32A(&direct_cache
[-1 * 8],
724 0x01010101u
* (MB_TYPE_DIRECT2
>> 1));
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];
730 AV_WN32A(&direct_cache
[-1 * 8],
731 0x01010101 * (MB_TYPE_16x16
>> 1));
734 if (IS_DIRECT(left_type
[LTOP
]))
735 direct_cache
[-1 + 0 * 8] = MB_TYPE_DIRECT2
>> 1;
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)];
739 direct_cache
[-1 + 0 * 8] = MB_TYPE_16x16
>> 1;
741 if (IS_DIRECT(left_type
[LBOT
]))
742 direct_cache
[-1 + 2 * 8] = MB_TYPE_DIRECT2
>> 1;
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)];
746 direct_cache
[-1 + 2 * 8] = MB_TYPE_16x16
>> 1;
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])
763 if (FRAME_MBAFF(h
)) {
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; \
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; \
791 h
->neighbor_transform_size
= !!IS_8x8DCT(top_type
) + !!IS_8x8DCT(left_type
[LTOP
]);
795 * decodes a P_SKIP or B_SKIP macroblock
797 static void av_unused
decode_mb_skip(H264Context
*h
)
799 const int mb_xy
= h
->mb_xy
;
802 memset(h
->non_zero_count
[mb_xy
], 0, 48);
805 mb_type
|= MB_TYPE_INTERLACED
;
807 if (h
->slice_type_nos
== AV_PICTURE_TYPE_B
) {
808 // just for fill_caches. pred_direct_motion will set the real mb_type
809 mb_type
|= MB_TYPE_L0L1
| MB_TYPE_DIRECT2
| MB_TYPE_SKIP
;
810 if (h
->direct_spatial_mv_pred
) {
811 fill_decode_neighbors(h
, mb_type
);
812 fill_decode_caches(h
, mb_type
); //FIXME check what is needed and what not ...
814 ff_h264_pred_direct_motion(h
, &mb_type
);
815 mb_type
|= MB_TYPE_SKIP
;
817 mb_type
|= MB_TYPE_16x16
| MB_TYPE_P0L0
| MB_TYPE_P1L0
| MB_TYPE_SKIP
;
819 fill_decode_neighbors(h
, mb_type
);
820 pred_pskip_motion(h
);
823 write_back_motion(h
, mb_type
);
824 h
->cur_pic
.mb_type
[mb_xy
] = mb_type
;
825 h
->cur_pic
.qscale_table
[mb_xy
] = h
->qscale
;
826 h
->slice_table
[mb_xy
] = h
->slice_num
;
827 h
->prev_mb_skipped
= 1;
830 #endif /* AVCODEC_H264_MVPRED_H */