2 * H.26L/H.264/AVC/JVT/14496-10/... motion vector prediction
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 / MPEG-4 part10 motion vector prediction.
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(const H264Context *h, H264SliceContext *sl,
40 int i, int list, int part_width)
42 const int topright_ref = sl->ref_cache[list][i - 8 + part_width];
44 /* there is no consistent mapping of mvs to neighboring locations that will
45 * make mbaff happy, so we can't move all this logic to fill_caches */
47 #define SET_DIAG_MV(MV_OP, REF_OP, XY, Y4) \
48 const int xy = XY, y4 = Y4; \
49 const int mb_type = mb_types[xy + (y4 >> 2) * h->mb_stride]; \
50 if (!USES_LIST(mb_type, list)) \
51 return LIST_NOT_USED; \
52 mv = h->cur_pic_ptr->motion_val[list][h->mb2b_xy[xy] + 3 + y4 * h->b_stride]; \
53 sl->mv_cache[list][scan8[0] - 2][0] = mv[0]; \
54 sl->mv_cache[list][scan8[0] - 2][1] = mv[1] MV_OP; \
55 return h->cur_pic_ptr->ref_index[list][4 * xy + 1 + (y4 & ~1)] REF_OP;
57 if (topright_ref == PART_NOT_AVAILABLE
58 && i >= scan8[0] + 8 && (i & 7) == 4
59 && sl->ref_cache[list][scan8[0] - 1] != PART_NOT_AVAILABLE) {
60 const uint32_t *mb_types = h->cur_pic_ptr->mb_type;
62 AV_ZERO32(sl->mv_cache[list][scan8[0] - 2]);
63 *C = sl->mv_cache[list][scan8[0] - 2];
65 if (!MB_FIELD(sl) && IS_INTERLACED(sl->left_type[0])) {
66 SET_DIAG_MV(* 2, >> 1, sl->left_mb_xy[0] + h->mb_stride,
67 (sl->mb_y & 1) * 2 + (i >> 5));
69 if (MB_FIELD(sl) && !IS_INTERLACED(sl->left_type[0])) {
70 // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
71 SET_DIAG_MV(/ 2, *2, sl->left_mb_xy[i >= 36], ((i >> 2)) & 3);
77 if (topright_ref != PART_NOT_AVAILABLE) {
78 *C = sl->mv_cache[list][i - 8 + part_width];
81 ff_tlog(h->avctx, "topright MV not available\n");
83 *C = sl->mv_cache[list][i - 8 - 1];
84 return sl->ref_cache[list][i - 8 - 1];
89 * Get the predicted MV.
90 * @param n the block index
91 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
92 * @param mx the x component of the predicted motion vector
93 * @param my the y component of the predicted motion vector
95 static av_always_inline void pred_motion(const H264Context *const h,
98 int part_width, int list, int ref,
99 int *const mx, int *const my)
101 const int index8 = scan8[n];
102 const int top_ref = sl->ref_cache[list][index8 - 8];
103 const int left_ref = sl->ref_cache[list][index8 - 1];
104 const int16_t *const A = sl->mv_cache[list][index8 - 1];
105 const int16_t *const B = sl->mv_cache[list][index8 - 8];
107 int diagonal_ref, match_count;
109 av_assert2(part_width == 1 || part_width == 2 || part_width == 4);
119 diagonal_ref = fetch_diagonal_mv(h, sl, &C, index8, list, part_width);
120 match_count = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
121 ff_tlog(h->avctx, "pred_motion match_count=%d\n", match_count);
122 if (match_count > 1) { //most common
123 *mx = mid_pred(A[0], B[0], C[0]);
124 *my = mid_pred(A[1], B[1], C[1]);
125 } else if (match_count == 1) {
126 if (left_ref == ref) {
129 } else if (top_ref == ref) {
137 if (top_ref == PART_NOT_AVAILABLE &&
138 diagonal_ref == PART_NOT_AVAILABLE &&
139 left_ref != PART_NOT_AVAILABLE) {
143 *mx = mid_pred(A[0], B[0], C[0]);
144 *my = mid_pred(A[1], B[1], C[1]);
149 "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n",
150 top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref,
151 A[0], A[1], ref, *mx, *my, sl->mb_x, sl->mb_y, n, list);
155 * Get the directionally predicted 16x8 MV.
156 * @param n the block index
157 * @param mx the x component of the predicted motion vector
158 * @param my the y component of the predicted motion vector
160 static av_always_inline void pred_16x8_motion(const H264Context *const h,
161 H264SliceContext *sl,
162 int n, int list, int ref,
163 int *const mx, int *const my)
166 const int top_ref = sl->ref_cache[list][scan8[0] - 8];
167 const int16_t *const B = sl->mv_cache[list][scan8[0] - 8];
169 ff_tlog(h->avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
170 top_ref, B[0], B[1], sl->mb_x, sl->mb_y, n, list);
172 if (top_ref == ref) {
178 const int left_ref = sl->ref_cache[list][scan8[8] - 1];
179 const int16_t *const A = sl->mv_cache[list][scan8[8] - 1];
181 ff_tlog(h->avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
182 left_ref, A[0], A[1], sl->mb_x, sl->mb_y, n, list);
184 if (left_ref == ref) {
192 pred_motion(h, sl, n, 4, list, ref, mx, my);
196 * Get the directionally predicted 8x16 MV.
197 * @param n the block index
198 * @param mx the x component of the predicted motion vector
199 * @param my the y component of the predicted motion vector
201 static av_always_inline void pred_8x16_motion(const H264Context *const h,
202 H264SliceContext *sl,
203 int n, int list, int ref,
204 int *const mx, int *const my)
207 const int left_ref = sl->ref_cache[list][scan8[0] - 1];
208 const int16_t *const A = sl->mv_cache[list][scan8[0] - 1];
210 ff_tlog(h->avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
211 left_ref, A[0], A[1], sl->mb_x, sl->mb_y, n, list);
213 if (left_ref == ref) {
222 diagonal_ref = fetch_diagonal_mv(h, sl, &C, scan8[4], list, 2);
224 ff_tlog(h->avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
225 diagonal_ref, C[0], C[1], sl->mb_x, sl->mb_y, n, list);
227 if (diagonal_ref == ref) {
235 pred_motion(h, sl, n, 2, list, ref, mx, my);
238 #define FIX_MV_MBAFF(type, refn, mvn, idx) \
239 if (FRAME_MBAFF(h)) { \
240 if (MB_FIELD(sl)) { \
241 if (!IS_INTERLACED(type)) { \
243 AV_COPY32(mvbuf[idx], mvn); \
244 mvbuf[idx][1] /= 2; \
248 if (IS_INTERLACED(type)) { \
250 AV_COPY32(mvbuf[idx], mvn); \
251 mvbuf[idx][1] *= 2; \
257 static av_always_inline void pred_pskip_motion(const H264Context *const h,
258 H264SliceContext *sl)
260 DECLARE_ALIGNED(4, static const int16_t, zeromv)[2] = { 0 };
261 DECLARE_ALIGNED(4, int16_t, mvbuf)[3][2];
262 int8_t *ref = h->cur_pic.ref_index[0];
263 int16_t(*mv)[2] = h->cur_pic.motion_val[0];
264 int top_ref, left_ref, diagonal_ref, match_count, mx, my;
265 const int16_t *A, *B, *C;
266 int b_stride = h->b_stride;
268 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
270 /* To avoid doing an entire fill_decode_caches, we inline the relevant
272 * FIXME: this is a partial duplicate of the logic in fill_decode_caches,
273 * but it's faster this way. Is there a way to avoid this duplication?
275 if (USES_LIST(sl->left_type[LTOP], 0)) {
276 left_ref = ref[4 * sl->left_mb_xy[LTOP] + 1 + (sl->left_block[0] & ~1)];
277 A = mv[h->mb2b_xy[sl->left_mb_xy[LTOP]] + 3 + b_stride * sl->left_block[0]];
278 FIX_MV_MBAFF(sl->left_type[LTOP], left_ref, A, 0);
279 if (!(left_ref | AV_RN32A(A)))
281 } else if (sl->left_type[LTOP]) {
282 left_ref = LIST_NOT_USED;
288 if (USES_LIST(sl->top_type, 0)) {
289 top_ref = ref[4 * sl->top_mb_xy + 2];
290 B = mv[h->mb2b_xy[sl->top_mb_xy] + 3 * b_stride];
291 FIX_MV_MBAFF(sl->top_type, top_ref, B, 1);
292 if (!(top_ref | AV_RN32A(B)))
294 } else if (sl->top_type) {
295 top_ref = LIST_NOT_USED;
301 ff_tlog(h->avctx, "pred_pskip: (%d) (%d) at %2d %2d\n",
302 top_ref, left_ref, sl->mb_x, sl->mb_y);
304 if (USES_LIST(sl->topright_type, 0)) {
305 diagonal_ref = ref[4 * sl->topright_mb_xy + 2];
306 C = mv[h->mb2b_xy[sl->topright_mb_xy] + 3 * b_stride];
307 FIX_MV_MBAFF(sl->topright_type, diagonal_ref, C, 2);
308 } else if (sl->topright_type) {
309 diagonal_ref = LIST_NOT_USED;
312 if (USES_LIST(sl->topleft_type, 0)) {
313 diagonal_ref = ref[4 * sl->topleft_mb_xy + 1 +
314 (sl->topleft_partition & 2)];
315 C = mv[h->mb2b_xy[sl->topleft_mb_xy] + 3 + b_stride +
316 (sl->topleft_partition & 2 * b_stride)];
317 FIX_MV_MBAFF(sl->topleft_type, diagonal_ref, C, 2);
318 } else if (sl->topleft_type) {
319 diagonal_ref = LIST_NOT_USED;
322 diagonal_ref = PART_NOT_AVAILABLE;
327 match_count = !diagonal_ref + !top_ref + !left_ref;
328 ff_tlog(h->avctx, "pred_pskip_motion match_count=%d\n", match_count);
329 if (match_count > 1) {
330 mx = mid_pred(A[0], B[0], C[0]);
331 my = mid_pred(A[1], B[1], C[1]);
332 } else if (match_count == 1) {
336 } else if (!top_ref) {
344 mx = mid_pred(A[0], B[0], C[0]);
345 my = mid_pred(A[1], B[1], C[1]);
348 fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx, my), 4);
352 fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
356 static void fill_decode_neighbors(const H264Context *h, H264SliceContext *sl, int mb_type)
358 const int mb_xy = sl->mb_xy;
359 int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
360 static const uint8_t left_block_options[4][32] = {
361 { 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 },
362 { 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 },
363 { 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 },
364 { 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 }
367 sl->topleft_partition = -1;
369 top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
371 /* Wow, what a mess, why didn't they simplify the interlacing & intra
372 * stuff, I can't imagine that these complex rules are worth it. */
374 topleft_xy = top_xy - 1;
375 topright_xy = top_xy + 1;
376 left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
377 sl->left_block = left_block_options[0];
378 if (FRAME_MBAFF(h)) {
379 const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
380 const int curr_mb_field_flag = IS_INTERLACED(mb_type);
382 if (left_mb_field_flag != curr_mb_field_flag) {
383 left_xy[LBOT] = left_xy[LTOP] = mb_xy - h->mb_stride - 1;
384 if (curr_mb_field_flag) {
385 left_xy[LBOT] += h->mb_stride;
386 sl->left_block = left_block_options[3];
388 topleft_xy += h->mb_stride;
389 /* take top left mv from the middle of the mb, as opposed
390 * to all other modes which use the bottom right partition */
391 sl->topleft_partition = 0;
392 sl->left_block = left_block_options[1];
396 if (curr_mb_field_flag) {
397 topleft_xy += h->mb_stride & (((h->cur_pic.mb_type[top_xy - 1] >> 7) & 1) - 1);
398 topright_xy += h->mb_stride & (((h->cur_pic.mb_type[top_xy + 1] >> 7) & 1) - 1);
399 top_xy += h->mb_stride & (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
401 if (left_mb_field_flag != curr_mb_field_flag) {
402 if (curr_mb_field_flag) {
403 left_xy[LBOT] += h->mb_stride;
404 sl->left_block = left_block_options[3];
406 sl->left_block = left_block_options[2];
412 sl->topleft_mb_xy = topleft_xy;
413 sl->top_mb_xy = top_xy;
414 sl->topright_mb_xy = topright_xy;
415 sl->left_mb_xy[LTOP] = left_xy[LTOP];
416 sl->left_mb_xy[LBOT] = left_xy[LBOT];
417 //FIXME do we need all in the context?
419 sl->topleft_type = h->cur_pic.mb_type[topleft_xy];
420 sl->top_type = h->cur_pic.mb_type[top_xy];
421 sl->topright_type = h->cur_pic.mb_type[topright_xy];
422 sl->left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
423 sl->left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
426 if (h->slice_table[topleft_xy] != sl->slice_num)
427 sl->topleft_type = 0;
428 if (h->slice_table[top_xy] != sl->slice_num)
430 if (h->slice_table[left_xy[LTOP]] != sl->slice_num)
431 sl->left_type[LTOP] = sl->left_type[LBOT] = 0;
433 if (h->slice_table[topleft_xy] != sl->slice_num) {
434 sl->topleft_type = 0;
435 if (h->slice_table[top_xy] != sl->slice_num)
437 if (h->slice_table[left_xy[LTOP]] != sl->slice_num)
438 sl->left_type[LTOP] = sl->left_type[LBOT] = 0;
441 if (h->slice_table[topright_xy] != sl->slice_num)
442 sl->topright_type = 0;
445 static void fill_decode_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
447 int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
448 int topleft_type, top_type, topright_type, left_type[LEFT_MBS];
449 const uint8_t *left_block = sl->left_block;
454 topleft_xy = sl->topleft_mb_xy;
455 top_xy = sl->top_mb_xy;
456 topright_xy = sl->topright_mb_xy;
457 left_xy[LTOP] = sl->left_mb_xy[LTOP];
458 left_xy[LBOT] = sl->left_mb_xy[LBOT];
459 topleft_type = sl->topleft_type;
460 top_type = sl->top_type;
461 topright_type = sl->topright_type;
462 left_type[LTOP] = sl->left_type[LTOP];
463 left_type[LBOT] = sl->left_type[LBOT];
465 if (!IS_SKIP(mb_type)) {
466 if (IS_INTRA(mb_type)) {
467 int type_mask = h->ps.pps->constrained_intra_pred ? IS_INTRA(-1) : -1;
468 sl->topleft_samples_available =
469 sl->top_samples_available =
470 sl->left_samples_available = 0xFFFF;
471 sl->topright_samples_available = 0xEEEA;
473 if (!(top_type & type_mask)) {
474 sl->topleft_samples_available = 0xB3FF;
475 sl->top_samples_available = 0x33FF;
476 sl->topright_samples_available = 0x26EA;
478 if (IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])) {
479 if (IS_INTERLACED(mb_type)) {
480 if (!(left_type[LTOP] & type_mask)) {
481 sl->topleft_samples_available &= 0xDFFF;
482 sl->left_samples_available &= 0x5FFF;
484 if (!(left_type[LBOT] & type_mask)) {
485 sl->topleft_samples_available &= 0xFF5F;
486 sl->left_samples_available &= 0xFF5F;
489 int left_typei = h->cur_pic.mb_type[left_xy[LTOP] + h->mb_stride];
491 av_assert2(left_xy[LTOP] == left_xy[LBOT]);
492 if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {
493 sl->topleft_samples_available &= 0xDF5F;
494 sl->left_samples_available &= 0x5F5F;
498 if (!(left_type[LTOP] & type_mask)) {
499 sl->topleft_samples_available &= 0xDF5F;
500 sl->left_samples_available &= 0x5F5F;
504 if (!(topleft_type & type_mask))
505 sl->topleft_samples_available &= 0x7FFF;
507 if (!(topright_type & type_mask))
508 sl->topright_samples_available &= 0xFBFF;
510 if (IS_INTRA4x4(mb_type)) {
511 if (IS_INTRA4x4(top_type)) {
512 AV_COPY32(sl->intra4x4_pred_mode_cache + 4 + 8 * 0, sl->intra4x4_pred_mode + h->mb2br_xy[top_xy]);
514 sl->intra4x4_pred_mode_cache[4 + 8 * 0] =
515 sl->intra4x4_pred_mode_cache[5 + 8 * 0] =
516 sl->intra4x4_pred_mode_cache[6 + 8 * 0] =
517 sl->intra4x4_pred_mode_cache[7 + 8 * 0] = 2 - 3 * !(top_type & type_mask);
519 for (i = 0; i < 2; i++) {
520 if (IS_INTRA4x4(left_type[LEFT(i)])) {
521 int8_t *mode = sl->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];
522 sl->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] = mode[6 - left_block[0 + 2 * i]];
523 sl->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = mode[6 - left_block[1 + 2 * i]];
525 sl->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] =
526 sl->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = 2 - 3 * !(left_type[LEFT(i)] & type_mask);
540 /* FIXME: constraint_intra_pred & partitioning & nnz
541 * (let us hope this is just a typo in the spec) */
542 nnz_cache = sl->non_zero_count_cache;
544 nnz = h->non_zero_count[top_xy];
545 AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);
546 if (!h->chroma_y_shift) {
547 AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 7]);
548 AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);
550 AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 5]);
551 AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);
554 uint32_t top_empty = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 0x40404040;
555 AV_WN32A(&nnz_cache[4 + 8 * 0], top_empty);
556 AV_WN32A(&nnz_cache[4 + 8 * 5], top_empty);
557 AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);
560 for (i = 0; i < 2; i++) {
561 if (left_type[LEFT(i)]) {
562 nnz = h->non_zero_count[left_xy[LEFT(i)]];
563 nnz_cache[3 + 8 * 1 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i]];
564 nnz_cache[3 + 8 * 2 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i]];
566 nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 4 * 4];
567 nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 4 * 4];
568 nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 8 * 4];
569 nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 8 * 4];
570 } else if (CHROMA422(h)) {
571 nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 4 * 4];
572 nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 4 * 4];
573 nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 8 * 4];
574 nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 8 * 4];
576 nnz_cache[3 + 8 * 6 + 8 * i] = nnz[left_block[8 + 4 + 2 * i]];
577 nnz_cache[3 + 8 * 11 + 8 * i] = nnz[left_block[8 + 5 + 2 * i]];
580 nnz_cache[3 + 8 * 1 + 2 * 8 * i] =
581 nnz_cache[3 + 8 * 2 + 2 * 8 * i] =
582 nnz_cache[3 + 8 * 6 + 2 * 8 * i] =
583 nnz_cache[3 + 8 * 7 + 2 * 8 * i] =
584 nnz_cache[3 + 8 * 11 + 2 * 8 * i] =
585 nnz_cache[3 + 8 * 12 + 2 * 8 * i] = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 64;
592 sl->top_cbp = h->cbp_table[top_xy];
594 sl->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
596 if (left_type[LTOP]) {
597 sl->left_cbp = (h->cbp_table[left_xy[LTOP]] & 0x7F0) |
598 ((h->cbp_table[left_xy[LTOP]] >> (left_block[0] & (~1))) & 2) |
599 (((h->cbp_table[left_xy[LBOT]] >> (left_block[2] & (~1))) & 2) << 2);
601 sl->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
606 if (IS_INTER(mb_type) || (IS_DIRECT(mb_type) && sl->direct_spatial_mv_pred)) {
608 int b_stride = h->b_stride;
609 for (list = 0; list < sl->list_count; list++) {
610 int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
611 int8_t *ref = h->cur_pic.ref_index[list];
612 int16_t(*mv_cache)[2] = &sl->mv_cache[list][scan8[0]];
613 int16_t(*mv)[2] = h->cur_pic.motion_val[list];
614 if (!USES_LIST(mb_type, list))
616 av_assert2(!(IS_DIRECT(mb_type) && !sl->direct_spatial_mv_pred));
618 if (USES_LIST(top_type, list)) {
619 const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
620 AV_COPY128(mv_cache[0 - 1 * 8], mv[b_xy + 0]);
621 ref_cache[0 - 1 * 8] =
622 ref_cache[1 - 1 * 8] = ref[4 * top_xy + 2];
623 ref_cache[2 - 1 * 8] =
624 ref_cache[3 - 1 * 8] = ref[4 * top_xy + 3];
626 AV_ZERO128(mv_cache[0 - 1 * 8]);
627 AV_WN32A(&ref_cache[0 - 1 * 8],
628 ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE) & 0xFF) * 0x01010101u);
631 if (mb_type & (MB_TYPE_16x8 | MB_TYPE_8x8)) {
632 for (i = 0; i < 2; i++) {
633 int cache_idx = -1 + i * 2 * 8;
634 if (USES_LIST(left_type[LEFT(i)], list)) {
635 const int b_xy = h->mb2b_xy[left_xy[LEFT(i)]] + 3;
636 const int b8_xy = 4 * left_xy[LEFT(i)] + 1;
637 AV_COPY32(mv_cache[cache_idx],
638 mv[b_xy + b_stride * left_block[0 + i * 2]]);
639 AV_COPY32(mv_cache[cache_idx + 8],
640 mv[b_xy + b_stride * left_block[1 + i * 2]]);
641 ref_cache[cache_idx] = ref[b8_xy + (left_block[0 + i * 2] & ~1)];
642 ref_cache[cache_idx + 8] = ref[b8_xy + (left_block[1 + i * 2] & ~1)];
644 AV_ZERO32(mv_cache[cache_idx]);
645 AV_ZERO32(mv_cache[cache_idx + 8]);
646 ref_cache[cache_idx] =
647 ref_cache[cache_idx + 8] = (left_type[LEFT(i)]) ? LIST_NOT_USED
648 : PART_NOT_AVAILABLE;
652 if (USES_LIST(left_type[LTOP], list)) {
653 const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
654 const int b8_xy = 4 * left_xy[LTOP] + 1;
655 AV_COPY32(mv_cache[-1], mv[b_xy + b_stride * left_block[0]]);
656 ref_cache[-1] = ref[b8_xy + (left_block[0] & ~1)];
658 AV_ZERO32(mv_cache[-1]);
659 ref_cache[-1] = left_type[LTOP] ? LIST_NOT_USED
660 : PART_NOT_AVAILABLE;
664 if (USES_LIST(topright_type, list)) {
665 const int b_xy = h->mb2b_xy[topright_xy] + 3 * b_stride;
666 AV_COPY32(mv_cache[4 - 1 * 8], mv[b_xy]);
667 ref_cache[4 - 1 * 8] = ref[4 * topright_xy + 2];
669 AV_ZERO32(mv_cache[4 - 1 * 8]);
670 ref_cache[4 - 1 * 8] = topright_type ? LIST_NOT_USED
671 : PART_NOT_AVAILABLE;
673 if(ref_cache[2 - 1*8] < 0 || ref_cache[4 - 1 * 8] < 0) {
674 if (USES_LIST(topleft_type, list)) {
675 const int b_xy = h->mb2b_xy[topleft_xy] + 3 + b_stride +
676 (sl->topleft_partition & 2 * b_stride);
677 const int b8_xy = 4 * topleft_xy + 1 + (sl->topleft_partition & 2);
678 AV_COPY32(mv_cache[-1 - 1 * 8], mv[b_xy]);
679 ref_cache[-1 - 1 * 8] = ref[b8_xy];
681 AV_ZERO32(mv_cache[-1 - 1 * 8]);
682 ref_cache[-1 - 1 * 8] = topleft_type ? LIST_NOT_USED
683 : PART_NOT_AVAILABLE;
687 if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF(h))
690 if (!(mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2))) {
691 uint8_t(*mvd_cache)[2] = &sl->mvd_cache[list][scan8[0]];
692 uint8_t(*mvd)[2] = sl->mvd_table[list];
693 ref_cache[2 + 8 * 0] =
694 ref_cache[2 + 8 * 2] = PART_NOT_AVAILABLE;
695 AV_ZERO32(mv_cache[2 + 8 * 0]);
696 AV_ZERO32(mv_cache[2 + 8 * 2]);
699 if (USES_LIST(top_type, list)) {
700 const int b_xy = h->mb2br_xy[top_xy];
701 AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);
703 AV_ZERO64(mvd_cache[0 - 1 * 8]);
705 if (USES_LIST(left_type[LTOP], list)) {
706 const int b_xy = h->mb2br_xy[left_xy[LTOP]] + 6;
707 AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);
708 AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);
710 AV_ZERO16(mvd_cache[-1 + 0 * 8]);
711 AV_ZERO16(mvd_cache[-1 + 1 * 8]);
713 if (USES_LIST(left_type[LBOT], list)) {
714 const int b_xy = h->mb2br_xy[left_xy[LBOT]] + 6;
715 AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);
716 AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);
718 AV_ZERO16(mvd_cache[-1 + 2 * 8]);
719 AV_ZERO16(mvd_cache[-1 + 3 * 8]);
721 AV_ZERO16(mvd_cache[2 + 8 * 0]);
722 AV_ZERO16(mvd_cache[2 + 8 * 2]);
723 if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
724 uint8_t *direct_cache = &sl->direct_cache[scan8[0]];
725 uint8_t *direct_table = h->direct_table;
726 fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16 >> 1, 1);
728 if (IS_DIRECT(top_type)) {
729 AV_WN32A(&direct_cache[-1 * 8],
730 0x01010101u * (MB_TYPE_DIRECT2 >> 1));
731 } else if (IS_8X8(top_type)) {
732 int b8_xy = 4 * top_xy;
733 direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];
734 direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];
736 AV_WN32A(&direct_cache[-1 * 8],
737 0x01010101 * (MB_TYPE_16x16 >> 1));
740 if (IS_DIRECT(left_type[LTOP]))
741 direct_cache[-1 + 0 * 8] = MB_TYPE_DIRECT2 >> 1;
742 else if (IS_8X8(left_type[LTOP]))
743 direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[LTOP] + 1 + (left_block[0] & ~1)];
745 direct_cache[-1 + 0 * 8] = MB_TYPE_16x16 >> 1;
747 if (IS_DIRECT(left_type[LBOT]))
748 direct_cache[-1 + 2 * 8] = MB_TYPE_DIRECT2 >> 1;
749 else if (IS_8X8(left_type[LBOT]))
750 direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[LBOT] + 1 + (left_block[2] & ~1)];
752 direct_cache[-1 + 2 * 8] = MB_TYPE_16x16 >> 1;
758 MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type) \
759 MAP_F2F(scan8[0] + 0 - 1 * 8, top_type) \
760 MAP_F2F(scan8[0] + 1 - 1 * 8, top_type) \
761 MAP_F2F(scan8[0] + 2 - 1 * 8, top_type) \
762 MAP_F2F(scan8[0] + 3 - 1 * 8, top_type) \
763 MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type) \
764 MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP]) \
765 MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP]) \
766 MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT]) \
767 MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])
769 if (FRAME_MBAFF(h)) {
772 #define MAP_F2F(idx, mb_type) \
773 if (!IS_INTERLACED(mb_type) && sl->ref_cache[list][idx] >= 0) { \
774 sl->ref_cache[list][idx] *= 2; \
775 sl->mv_cache[list][idx][1] /= 2; \
776 sl->mvd_cache[list][idx][1] >>= 1; \
783 #define MAP_F2F(idx, mb_type) \
784 if (IS_INTERLACED(mb_type) && sl->ref_cache[list][idx] >= 0) { \
785 sl->ref_cache[list][idx] >>= 1; \
786 sl->mv_cache[list][idx][1] *= 2; \
787 sl->mvd_cache[list][idx][1] <<= 1; \
797 sl->neighbor_transform_size = !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]);
801 * decodes a P_SKIP or B_SKIP macroblock
803 static void av_unused decode_mb_skip(const H264Context *h, H264SliceContext *sl)
805 const int mb_xy = sl->mb_xy;
808 memset(h->non_zero_count[mb_xy], 0, 48);
811 mb_type |= MB_TYPE_INTERLACED;
813 if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
814 // just for fill_caches. pred_direct_motion will set the real mb_type
815 mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 | MB_TYPE_SKIP;
816 if (sl->direct_spatial_mv_pred) {
817 fill_decode_neighbors(h, sl, mb_type);
818 fill_decode_caches(h, sl, mb_type); //FIXME check what is needed and what not ...
820 ff_h264_pred_direct_motion(h, sl, &mb_type);
821 mb_type |= MB_TYPE_SKIP;
823 mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P1L0 | MB_TYPE_SKIP;
825 fill_decode_neighbors(h, sl, mb_type);
826 pred_pskip_motion(h, sl);
829 write_back_motion(h, sl, mb_type);
830 h->cur_pic.mb_type[mb_xy] = mb_type;
831 h->cur_pic.qscale_table[mb_xy] = sl->qscale;
832 h->slice_table[mb_xy] = sl->slice_num;
833 sl->prev_mb_skipped = 1;
836 #endif /* AVCODEC_H264_MVPRED_H */