encodemv.c 6.04 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10 11 12 13
 */

#include <math.h>

14 15
#include "av1/common/common.h"
#include "av1/common/entropymode.h"
Jingning Han's avatar
Jingning Han committed
16

17 18 19
#include "av1/encoder/cost.h"
#include "av1/encoder/encodemv.h"
#include "av1/encoder/subexp.h"
Jingning Han's avatar
Jingning Han committed
20

Yaowu Xu's avatar
Yaowu Xu committed
21 22
#include "aom_dsp/aom_dsp_common.h"

23
static void encode_mv_component(aom_writer *w, int comp, nmv_component *mvcomp,
Alex Converse's avatar
Alex Converse committed
24
                                MvSubpelPrecision precision) {
Jingning Han's avatar
Jingning Han committed
25 26 27
  int offset;
  const int sign = comp < 0;
  const int mag = sign ? -comp : comp;
Yaowu Xu's avatar
Yaowu Xu committed
28
  const int mv_class = av1_get_mv_class(mag - 1, &offset);
29 30 31
  const int d = offset >> 3;         // int mv data
  const int fr = (offset >> 1) & 3;  // fractional mv data
  const int hp = offset & 1;         // high precision mv data
Jingning Han's avatar
Jingning Han committed
32 33 34

  assert(comp != 0);

35
  // Sign
36
  aom_write_symbol(w, sign, mvcomp->sign_cdf, 2);
Jingning Han's avatar
Jingning Han committed
37

38
  // Class
39
  aom_write_symbol(w, mv_class, mvcomp->classes_cdf, MV_CLASSES);
Jingning Han's avatar
Jingning Han committed
40 41 42

  // Integer bits
  if (mv_class == MV_CLASS_0) {
43
    aom_write_symbol(w, d, mvcomp->class0_cdf, CLASS0_SIZE);
Jingning Han's avatar
Jingning Han committed
44 45 46
  } else {
    int i;
    const int n = mv_class + CLASS0_BITS - 1;  // number of bits
47
    for (i = 0; i < n; ++i)
48
      aom_write_symbol(w, (d >> i) & 1, mvcomp->bits_cdf[i], 2);
Jingning Han's avatar
Jingning Han committed
49
  }
Alex Converse's avatar
Alex Converse committed
50
// Fractional bits
RogerZhou's avatar
RogerZhou committed
51
#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse's avatar
Alex Converse committed
52
  if (precision > MV_SUBPEL_NONE)
RogerZhou's avatar
RogerZhou committed
53
#endif  // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse's avatar
Alex Converse committed
54
  {
clang-format's avatar
clang-format committed
55 56 57 58
    aom_write_symbol(
        w, fr,
        mv_class == MV_CLASS_0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
        MV_FP_SIZE);
Alex Converse's avatar
Alex Converse committed
59
  }
Jingning Han's avatar
Jingning Han committed
60 61

  // High precision bit
Alex Converse's avatar
Alex Converse committed
62
  if (precision > MV_SUBPEL_LOW_PRECISION)
63 64 65
    aom_write_symbol(
        w, hp, mv_class == MV_CLASS_0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf,
        2);
Jingning Han's avatar
Jingning Han committed
66 67 68
}

static void build_nmv_component_cost_table(int *mvcost,
69
                                           const nmv_component *const mvcomp,
Alex Converse's avatar
Alex Converse committed
70
                                           MvSubpelPrecision precision) {
Jingning Han's avatar
Jingning Han committed
71 72 73 74 75 76
  int i, v;
  int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
  int bits_cost[MV_OFFSET_BITS][2];
  int class0_fp_cost[CLASS0_SIZE][MV_FP_SIZE], fp_cost[MV_FP_SIZE];
  int class0_hp_cost[2], hp_cost[2];

77 78 79
  av1_cost_tokens_from_cdf(sign_cost, mvcomp->sign_cdf, NULL);
  av1_cost_tokens_from_cdf(class_cost, mvcomp->classes_cdf, NULL);
  av1_cost_tokens_from_cdf(class0_cost, mvcomp->class0_cdf, NULL);
Jingning Han's avatar
Jingning Han committed
80
  for (i = 0; i < MV_OFFSET_BITS; ++i) {
81
    av1_cost_tokens_from_cdf(bits_cost[i], mvcomp->bits_cdf[i], NULL);
Jingning Han's avatar
Jingning Han committed
82 83 84
  }

  for (i = 0; i < CLASS0_SIZE; ++i)
85 86
    av1_cost_tokens_from_cdf(class0_fp_cost[i], mvcomp->class0_fp_cdf[i], NULL);
  av1_cost_tokens_from_cdf(fp_cost, mvcomp->fp_cdf, NULL);
Jingning Han's avatar
Jingning Han committed
87

Alex Converse's avatar
Alex Converse committed
88
  if (precision > MV_SUBPEL_LOW_PRECISION) {
89 90
    av1_cost_tokens_from_cdf(class0_hp_cost, mvcomp->class0_hp_cdf, NULL);
    av1_cost_tokens_from_cdf(hp_cost, mvcomp->hp_cdf, NULL);
Jingning Han's avatar
Jingning Han committed
91 92 93 94 95
  }
  mvcost[0] = 0;
  for (v = 1; v <= MV_MAX; ++v) {
    int z, c, o, d, e, f, cost = 0;
    z = v - 1;
Yaowu Xu's avatar
Yaowu Xu committed
96
    c = av1_get_mv_class(z, &o);
Jingning Han's avatar
Jingning Han committed
97
    cost += class_cost[c];
98 99 100
    d = (o >> 3);     /* int mv data */
    f = (o >> 1) & 3; /* fractional pel mv data */
    e = (o & 1);      /* high precision mv data */
Jingning Han's avatar
Jingning Han committed
101 102 103
    if (c == MV_CLASS_0) {
      cost += class0_cost[d];
    } else {
104
      const int b = c + CLASS0_BITS - 1; /* number of bits */
105
      for (i = 0; i < b; ++i) cost += bits_cost[i][((d >> i) & 1)];
Jingning Han's avatar
Jingning Han committed
106
    }
RogerZhou's avatar
RogerZhou committed
107
#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse's avatar
Alex Converse committed
108
    if (precision > MV_SUBPEL_NONE)
RogerZhou's avatar
RogerZhou committed
109
#endif  // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse's avatar
Alex Converse committed
110
    {
Jingning Han's avatar
Jingning Han committed
111
      if (c == MV_CLASS_0) {
Alex Converse's avatar
Alex Converse committed
112
        cost += class0_fp_cost[d][f];
Jingning Han's avatar
Jingning Han committed
113
      } else {
Alex Converse's avatar
Alex Converse committed
114 115 116 117 118 119 120 121
        cost += fp_cost[f];
      }
      if (precision > MV_SUBPEL_LOW_PRECISION) {
        if (c == MV_CLASS_0) {
          cost += class0_hp_cost[e];
        } else {
          cost += hp_cost[e];
        }
Jingning Han's avatar
Jingning Han committed
122 123 124 125 126 127 128
      }
    }
    mvcost[v] = cost + sign_cost[0];
    mvcost[-v] = cost + sign_cost[1];
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
129
void av1_encode_mv(AV1_COMP *cpi, aom_writer *w, const MV *mv, const MV *ref,
130
                   nmv_context *mvctx, int usehp) {
131
  const MV diff = { mv->row - ref->row, mv->col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
132
  const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
RogerZhou's avatar
RogerZhou committed
133
#if CONFIG_AMVR
134
  if (cpi->common.cur_frame_force_integer_mv) {
RogerZhou's avatar
RogerZhou committed
135 136 137
    usehp = MV_SUBPEL_NONE;
  }
#endif
138
  aom_write_symbol(w, j, mvctx->joints_cdf, MV_JOINTS);
Jingning Han's avatar
Jingning Han committed
139 140 141 142 143 144 145 146 147
  if (mv_joint_vertical(j))
    encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);

  if (mv_joint_horizontal(j))
    encode_mv_component(w, diff.col, &mvctx->comps[1], usehp);

  // If auto_mv_step_size is enabled then keep track of the largest
  // motion vector component used.
  if (cpi->sf.mv.auto_mv_step_size) {
Yaowu Xu's avatar
Yaowu Xu committed
148 149
    unsigned int maxv = AOMMAX(abs(mv->row), abs(mv->col)) >> 3;
    cpi->max_mv_magnitude = AOMMAX(maxv, cpi->max_mv_magnitude);
Jingning Han's avatar
Jingning Han committed
150 151 152
  }
}

Alex Converse's avatar
Alex Converse committed
153 154 155
#if CONFIG_INTRABC
void av1_encode_dv(aom_writer *w, const MV *mv, const MV *ref,
                   nmv_context *mvctx) {
Hui Su's avatar
Hui Su committed
156 157 158 159 160
  // DV and ref DV should not have sub-pel.
  assert((mv->col & 7) == 0);
  assert((mv->row & 7) == 0);
  assert((ref->col & 7) == 0);
  assert((ref->row & 7) == 0);
Alex Converse's avatar
Alex Converse committed
161 162 163
  const MV diff = { mv->row - ref->row, mv->col - ref->col };
  const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);

164
  aom_write_symbol(w, j, mvctx->joints_cdf, MV_JOINTS);
Alex Converse's avatar
Alex Converse committed
165
  if (mv_joint_vertical(j))
Alex Converse's avatar
Alex Converse committed
166
    encode_mv_component(w, diff.row, &mvctx->comps[0], MV_SUBPEL_NONE);
Alex Converse's avatar
Alex Converse committed
167 168

  if (mv_joint_horizontal(j))
Alex Converse's avatar
Alex Converse committed
169
    encode_mv_component(w, diff.col, &mvctx->comps[1], MV_SUBPEL_NONE);
Alex Converse's avatar
Alex Converse committed
170 171 172
}
#endif  // CONFIG_INTRABC

Yaowu Xu's avatar
Yaowu Xu committed
173
void av1_build_nmv_cost_table(int *mvjoint, int *mvcost[2],
Alex Converse's avatar
Alex Converse committed
174 175
                              const nmv_context *ctx,
                              MvSubpelPrecision precision) {
176
  av1_cost_tokens_from_cdf(mvjoint, ctx->joints_cdf, NULL);
Alex Converse's avatar
Alex Converse committed
177 178
  build_nmv_component_cost_table(mvcost[0], &ctx->comps[0], precision);
  build_nmv_component_cost_table(mvcost[1], &ctx->comps[1], precision);
Jingning Han's avatar
Jingning Han committed
179
}