28 #ifndef AVCODEC_H264_MVPRED_H
29 #define AVCODEC_H264_MVPRED_H
39 int i,
int list,
int part_width)
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 sl->mv_cache[list][scan8[0] - 2][0] = mv[0]; \
53 sl->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;
57 &&
i >=
scan8[0] + 8 && (
i & 7) == 4
59 const uint32_t *mb_types =
h->cur_pic_ptr->mb_type;
66 (sl->
mb_y & 1) * 2 + (
i >> 5));
80 ff_tlog(
h->avctx,
"topright MV not available\n");
97 int part_width,
int list,
int ref,
98 int *
const mx,
int *
const my)
100 const int index8 =
scan8[n];
106 int diagonal_ref, match_count;
108 av_assert2(part_width == 1 || part_width == 2 || part_width == 4);
119 match_count = (diagonal_ref ==
ref) + (top_ref ==
ref) + (left_ref ==
ref);
120 ff_tlog(
h->avctx,
"pred_motion match_count=%d\n", match_count);
121 if (match_count > 1) {
124 }
else if (match_count == 1) {
125 if (left_ref ==
ref) {
128 }
else if (top_ref ==
ref) {
148 "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n",
149 top_ref,
B[0],
B[1], diagonal_ref,
C[0],
C[1], left_ref,
162 int *
const mx,
int *
const my)
168 ff_tlog(
h->avctx,
"pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
171 if (top_ref ==
ref) {
180 ff_tlog(
h->avctx,
"pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
183 if (left_ref ==
ref) {
203 int *
const mx,
int *
const my)
209 ff_tlog(
h->avctx,
"pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
212 if (left_ref ==
ref) {
223 ff_tlog(
h->avctx,
"pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
226 if (diagonal_ref ==
ref) {
237 #define FIX_MV_MBAFF(type, refn, mvn, idx) \
238 if (FRAME_MBAFF(h)) { \
239 if (MB_FIELD(sl)) { \
240 if (!IS_INTERLACED(type)) { \
242 AV_COPY32(mvbuf[idx], mvn); \
243 mvbuf[idx][1] /= 2; \
247 if (IS_INTERLACED(type)) { \
249 AV_COPY32(mvbuf[idx], mvn); \
250 mvbuf[idx][1] *= 2; \
261 int8_t *
ref =
h->cur_pic.ref_index[0];
262 int16_t(*
mv)[2] =
h->cur_pic.motion_val[0];
263 int top_ref, left_ref, diagonal_ref, match_count, mx, my;
264 const int16_t *
A, *
B, *
C;
265 int b_stride =
h->b_stride;
300 ff_tlog(
h->avctx,
"pred_pskip: (%d) (%d) at %2d %2d\n",
301 top_ref, left_ref, sl->
mb_x, sl->
mb_y);
326 match_count = !diagonal_ref + !top_ref + !left_ref;
327 ff_tlog(
h->avctx,
"pred_pskip_motion match_count=%d\n", match_count);
328 if (match_count > 1) {
331 }
else if (match_count == 1) {
335 }
else if (!top_ref) {
357 const int mb_xy = sl->
mb_xy;
358 int topleft_xy, top_xy, topright_xy, left_xy[
LEFT_MBS];
359 static const uint8_t left_block_options[4][32] = {
360 { 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 },
361 { 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 },
362 { 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 },
363 { 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 }
368 top_xy = mb_xy - (
h->mb_stride <<
MB_FIELD(sl));
373 topleft_xy = top_xy - 1;
374 topright_xy = top_xy + 1;
375 left_xy[
LBOT] = left_xy[
LTOP] = mb_xy - 1;
378 const int left_mb_field_flag =
IS_INTERLACED(
h->cur_pic.mb_type[mb_xy - 1]);
381 if (left_mb_field_flag != curr_mb_field_flag) {
382 left_xy[
LBOT] = left_xy[
LTOP] = mb_xy -
h->mb_stride - 1;
383 if (curr_mb_field_flag) {
384 left_xy[
LBOT] +=
h->mb_stride;
387 topleft_xy +=
h->mb_stride;
395 if (curr_mb_field_flag) {
396 topleft_xy +=
h->mb_stride & (((
h->cur_pic.mb_type[top_xy - 1] >> 7) & 1) - 1);
397 topright_xy +=
h->mb_stride & (((
h->cur_pic.mb_type[top_xy + 1] >> 7) & 1) - 1);
398 top_xy +=
h->mb_stride & (((
h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
400 if (left_mb_field_flag != curr_mb_field_flag) {
401 if (curr_mb_field_flag) {
402 left_xy[
LBOT] +=
h->mb_stride;
419 sl->
top_type =
h->cur_pic.mb_type[top_xy];
425 if (
h->slice_table[topleft_xy] != sl->
slice_num)
432 if (
h->slice_table[topleft_xy] != sl->
slice_num) {
440 if (
h->slice_table[topright_xy] != sl->
slice_num)
446 int topleft_xy, top_xy, topright_xy, left_xy[
LEFT_MBS];
447 int topleft_type, top_type, topright_type, left_type[
LEFT_MBS];
466 int type_mask =
h->ps.pps->constrained_intra_pred ?
IS_INTRA(-1) : -1;
472 if (!(top_type & type_mask)) {
479 if (!(left_type[
LTOP] & type_mask)) {
483 if (!(left_type[
LBOT] & type_mask)) {
488 int left_typei =
h->cur_pic.mb_type[left_xy[
LTOP] +
h->mb_stride];
491 if (!((left_typei & type_mask) && (left_type[
LTOP] & type_mask))) {
497 if (!(left_type[
LTOP] & type_mask)) {
503 if (!(topleft_type & type_mask))
506 if (!(topright_type & type_mask))
518 for (
i = 0;
i < 2;
i++) {
543 nnz =
h->non_zero_count[top_xy];
544 AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);
545 if (!
h->chroma_y_shift) {
546 AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 7]);
547 AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);
549 AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 5]);
550 AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);
553 uint32_t top_empty =
CABAC(
h) && !
IS_INTRA(mb_type) ? 0 : 0x40404040;
554 AV_WN32A(&nnz_cache[4 + 8 * 0], top_empty);
555 AV_WN32A(&nnz_cache[4 + 8 * 5], top_empty);
556 AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);
559 for (
i = 0;
i < 2;
i++) {
560 if (left_type[
LEFT(
i)]) {
561 nnz =
h->non_zero_count[left_xy[
LEFT(
i)]];
562 nnz_cache[3 + 8 * 1 + 2 * 8 *
i] = nnz[left_block[8 + 0 + 2 *
i]];
563 nnz_cache[3 + 8 * 2 + 2 * 8 *
i] = nnz[left_block[8 + 1 + 2 *
i]];
565 nnz_cache[3 + 8 * 6 + 2 * 8 *
i] = nnz[left_block[8 + 0 + 2 *
i] + 4 * 4];
566 nnz_cache[3 + 8 * 7 + 2 * 8 *
i] = nnz[left_block[8 + 1 + 2 *
i] + 4 * 4];
567 nnz_cache[3 + 8 * 11 + 2 * 8 *
i] = nnz[left_block[8 + 0 + 2 *
i] + 8 * 4];
568 nnz_cache[3 + 8 * 12 + 2 * 8 *
i] = nnz[left_block[8 + 1 + 2 *
i] + 8 * 4];
570 nnz_cache[3 + 8 * 6 + 2 * 8 *
i] = nnz[left_block[8 + 0 + 2 *
i] - 2 + 4 * 4];
571 nnz_cache[3 + 8 * 7 + 2 * 8 *
i] = nnz[left_block[8 + 1 + 2 *
i] - 2 + 4 * 4];
572 nnz_cache[3 + 8 * 11 + 2 * 8 *
i] = nnz[left_block[8 + 0 + 2 *
i] - 2 + 8 * 4];
573 nnz_cache[3 + 8 * 12 + 2 * 8 *
i] = nnz[left_block[8 + 1 + 2 *
i] - 2 + 8 * 4];
575 nnz_cache[3 + 8 * 6 + 8 *
i] = nnz[left_block[8 + 4 + 2 *
i]];
576 nnz_cache[3 + 8 * 11 + 8 *
i] = nnz[left_block[8 + 5 + 2 *
i]];
579 nnz_cache[3 + 8 * 1 + 2 * 8 *
i] =
580 nnz_cache[3 + 8 * 2 + 2 * 8 *
i] =
581 nnz_cache[3 + 8 * 6 + 2 * 8 *
i] =
582 nnz_cache[3 + 8 * 7 + 2 * 8 *
i] =
583 nnz_cache[3 + 8 * 11 + 2 * 8 *
i] =
584 nnz_cache[3 + 8 * 12 + 2 * 8 *
i] =
CABAC(
h) && !
IS_INTRA(mb_type) ? 0 : 64;
595 if (left_type[
LTOP]) {
597 ((
h->cbp_table[left_xy[
LTOP]] >> (left_block[0] & (~1))) & 2) |
598 (((
h->cbp_table[left_xy[
LBOT]] >> (left_block[2] & (~1))) & 2) << 2);
607 int b_stride =
h->b_stride;
610 int8_t *
ref =
h->cur_pic.ref_index[
list];
612 int16_t(*
mv)[2] =
h->cur_pic.motion_val[
list];
618 const int b_xy =
h->mb2b_xy[top_xy] + 3 * b_stride;
620 ref_cache[0 - 1 * 8] =
621 ref_cache[1 - 1 * 8] =
ref[4 * top_xy + 2];
622 ref_cache[2 - 1 * 8] =
623 ref_cache[3 - 1 * 8] =
ref[4 * top_xy + 3];
631 for (
i = 0;
i < 2;
i++) {
632 int cache_idx = -1 +
i * 2 * 8;
634 const int b_xy =
h->mb2b_xy[left_xy[
LEFT(
i)]] + 3;
635 const int b8_xy = 4 * left_xy[
LEFT(
i)] + 1;
637 mv[b_xy + b_stride * left_block[0 +
i * 2]]);
639 mv[b_xy + b_stride * left_block[1 +
i * 2]]);
640 ref_cache[cache_idx] =
ref[b8_xy + (left_block[0 +
i * 2] & ~1)];
641 ref_cache[cache_idx + 8] =
ref[b8_xy + (left_block[1 +
i * 2] & ~1)];
645 ref_cache[cache_idx] =
652 const int b_xy =
h->mb2b_xy[left_xy[
LTOP]] + 3;
653 const int b8_xy = 4 * left_xy[
LTOP] + 1;
654 AV_COPY32(mv_cache[-1],
mv[b_xy + b_stride * left_block[0]]);
655 ref_cache[-1] =
ref[b8_xy + (left_block[0] & ~1)];
664 const int b_xy =
h->mb2b_xy[topright_xy] + 3 * b_stride;
666 ref_cache[4 - 1 * 8] =
ref[4 * topright_xy + 2];
672 if(ref_cache[2 - 1*8] < 0 || ref_cache[4 - 1 * 8] < 0) {
674 const int b_xy =
h->mb2b_xy[topleft_xy] + 3 + b_stride +
678 ref_cache[-1 - 1 * 8] =
ref[b8_xy];
692 ref_cache[2 + 8 * 0] =
699 const int b_xy =
h->mb2br_xy[top_xy];
700 AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);
705 const int b_xy =
h->mb2br_xy[left_xy[
LTOP]] + 6;
706 AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);
707 AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);
713 const int b_xy =
h->mb2br_xy[left_xy[
LBOT]] + 6;
714 AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);
715 AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);
724 uint8_t *direct_table =
h->direct_table;
730 }
else if (
IS_8X8(top_type)) {
731 int b8_xy = 4 * top_xy;
732 direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];
733 direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];
742 direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[
LTOP] + 1 + (left_block[0] & ~1)];
749 direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[
LBOT] + 1 + (left_block[2] & ~1)];
757 MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type) \
758 MAP_F2F(scan8[0] + 0 - 1 * 8, top_type) \
759 MAP_F2F(scan8[0] + 1 - 1 * 8, top_type) \
760 MAP_F2F(scan8[0] + 2 - 1 * 8, top_type) \
761 MAP_F2F(scan8[0] + 3 - 1 * 8, top_type) \
762 MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type) \
763 MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP]) \
764 MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP]) \
765 MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT]) \
766 MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])
771 #define MAP_F2F(idx, mb_type) \
772 if (!IS_INTERLACED(mb_type) && sl->ref_cache[list][idx] >= 0) { \
773 sl->ref_cache[list][idx] *= 2; \
774 sl->mv_cache[list][idx][1] /= 2; \
775 sl->mvd_cache[list][idx][1] >>= 1; \
782 #define MAP_F2F(idx, mb_type) \
783 if (IS_INTERLACED(mb_type) && sl->ref_cache[list][idx] >= 0) { \
784 sl->ref_cache[list][idx] >>= 1; \
785 sl->mv_cache[list][idx][1] *= 2; \
786 sl->mvd_cache[list][idx][1] <<= 1; \
804 const int mb_xy = sl->
mb_xy;
807 memset(
h->non_zero_count[mb_xy], 0, 48);
829 h->cur_pic.mb_type[mb_xy] = mb_type;
830 h->cur_pic.qscale_table[mb_xy] = sl->
qscale;