You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
13KB

  1. /*
  2. * VP9 compatible video decoder
  3. *
  4. * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
  5. * Copyright (C) 2013 Clément Bœsch <u pkh me>
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "internal.h"
  24. #include "vp56.h"
  25. #include "vp9.h"
  26. #include "vp9data.h"
  27. static av_always_inline void clamp_mv(VP56mv *dst, const VP56mv *src,
  28. VP9Context *s)
  29. {
  30. dst->x = av_clip(src->x, s->min_mv.x, s->max_mv.x);
  31. dst->y = av_clip(src->y, s->min_mv.y, s->max_mv.y);
  32. }
  33. static void find_ref_mvs(VP9Context *s,
  34. VP56mv *pmv, int ref, int z, int idx, int sb)
  35. {
  36. static const int8_t mv_ref_blk_off[N_BS_SIZES][8][2] = {
  37. [BS_64x64] = { { 3, -1 }, { -1, 3 }, { 4, -1 }, { -1, 4 },
  38. { -1, -1 }, { 0, -1 }, { -1, 0 }, { 6, -1 } },
  39. [BS_64x32] = { { 0, -1 }, { -1, 0 }, { 4, -1 }, { -1, 2 },
  40. { -1, -1 }, { 0, -3 }, { -3, 0 }, { 2, -1 } },
  41. [BS_32x64] = { { -1, 0 }, { 0, -1 }, { -1, 4 }, { 2, -1 },
  42. { -1, -1 }, { -3, 0 }, { 0, -3 }, { -1, 2 } },
  43. [BS_32x32] = { { 1, -1 }, { -1, 1 }, { 2, -1 }, { -1, 2 },
  44. { -1, -1 }, { 0, -3 }, { -3, 0 }, { -3, -3 } },
  45. [BS_32x16] = { { 0, -1 }, { -1, 0 }, { 2, -1 }, { -1, -1 },
  46. { -1, 1 }, { 0, -3 }, { -3, 0 }, { -3, -3 } },
  47. [BS_16x32] = { { -1, 0 }, { 0, -1 }, { -1, 2 }, { -1, -1 },
  48. { 1, -1 }, { -3, 0 }, { 0, -3 }, { -3, -3 } },
  49. [BS_16x16] = { { 0, -1 }, { -1, 0 }, { 1, -1 }, { -1, 1 },
  50. { -1, -1 }, { 0, -3 }, { -3, 0 }, { -3, -3 } },
  51. [BS_16x8] = { { 0, -1 }, { -1, 0 }, { 1, -1 }, { -1, -1 },
  52. { 0, -2 }, { -2, 0 }, { -2, -1 }, { -1, -2 } },
  53. [BS_8x16] = { { -1, 0 }, { 0, -1 }, { -1, 1 }, { -1, -1 },
  54. { -2, 0 }, { 0, -2 }, { -1, -2 }, { -2, -1 } },
  55. [BS_8x8] = { { 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
  56. { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } },
  57. [BS_8x4] = { { 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
  58. { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } },
  59. [BS_4x8] = { { 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
  60. { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } },
  61. [BS_4x4] = { { 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
  62. { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } },
  63. };
  64. VP9Block *const b = &s->b;
  65. int row = b->row, col = b->col, row7 = b->row7;
  66. const int8_t (*p)[2] = mv_ref_blk_off[b->bs];
  67. #define INVALID_MV 0x80008000U
  68. uint32_t mem = INVALID_MV;
  69. int i;
  70. #define RETURN_DIRECT_MV(mv) \
  71. do { \
  72. uint32_t m = AV_RN32A(&mv); \
  73. if (!idx) { \
  74. AV_WN32A(pmv, m); \
  75. return; \
  76. } else if (mem == INVALID_MV) { \
  77. mem = m; \
  78. } else if (m != mem) { \
  79. AV_WN32A(pmv, m); \
  80. return; \
  81. } \
  82. } while (0)
  83. if (sb >= 0) {
  84. if (sb == 2 || sb == 1) {
  85. RETURN_DIRECT_MV(b->mv[0][z]);
  86. } else if (sb == 3) {
  87. RETURN_DIRECT_MV(b->mv[2][z]);
  88. RETURN_DIRECT_MV(b->mv[1][z]);
  89. RETURN_DIRECT_MV(b->mv[0][z]);
  90. }
  91. #define RETURN_MV(mv) \
  92. do { \
  93. if (sb > 0) { \
  94. VP56mv tmp; \
  95. uint32_t m; \
  96. clamp_mv(&tmp, &mv, s); \
  97. m = AV_RN32A(&tmp); \
  98. if (!idx) { \
  99. AV_WN32A(pmv, m); \
  100. return; \
  101. } else if (mem == INVALID_MV) { \
  102. mem = m; \
  103. } else if (m != mem) { \
  104. AV_WN32A(pmv, m); \
  105. return; \
  106. } \
  107. } else { \
  108. uint32_t m = AV_RN32A(&mv); \
  109. if (!idx) { \
  110. clamp_mv(pmv, &mv, s); \
  111. return; \
  112. } else if (mem == INVALID_MV) { \
  113. mem = m; \
  114. } else if (m != mem) { \
  115. clamp_mv(pmv, &mv, s); \
  116. return; \
  117. } \
  118. } \
  119. } while (0)
  120. if (row > 0) {
  121. VP9MVRefPair *mv = &s->mv[0][(row - 1) * s->sb_cols * 8 + col];
  122. if (mv->ref[0] == ref)
  123. RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][0]);
  124. else if (mv->ref[1] == ref)
  125. RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][1]);
  126. }
  127. if (col > s->tiling.tile_col_start) {
  128. VP9MVRefPair *mv = &s->mv[0][row * s->sb_cols * 8 + col - 1];
  129. if (mv->ref[0] == ref)
  130. RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][0]);
  131. else if (mv->ref[1] == ref)
  132. RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][1]);
  133. }
  134. i = 2;
  135. } else {
  136. i = 0;
  137. }
  138. // previously coded MVs in the neighborhood, using same reference frame
  139. for (; i < 8; i++) {
  140. int c = p[i][0] + col, r = p[i][1] + row;
  141. if (c >= s->tiling.tile_col_start && c < s->cols &&
  142. r >= 0 && r < s->rows) {
  143. VP9MVRefPair *mv = &s->mv[0][r * s->sb_cols * 8 + c];
  144. if (mv->ref[0] == ref)
  145. RETURN_MV(mv->mv[0]);
  146. else if (mv->ref[1] == ref)
  147. RETURN_MV(mv->mv[1]);
  148. }
  149. }
  150. // MV at this position in previous frame, using same reference frame
  151. if (s->use_last_frame_mvs) {
  152. VP9MVRefPair *mv = &s->mv[1][row * s->sb_cols * 8 + col];
  153. if (mv->ref[0] == ref)
  154. RETURN_MV(mv->mv[0]);
  155. else if (mv->ref[1] == ref)
  156. RETURN_MV(mv->mv[1]);
  157. }
  158. #define RETURN_SCALE_MV(mv, scale) \
  159. do { \
  160. if (scale) { \
  161. VP56mv mv_temp = { -mv.x, -mv.y }; \
  162. RETURN_MV(mv_temp); \
  163. } else { \
  164. RETURN_MV(mv); \
  165. } \
  166. } while (0)
  167. // previously coded MVs in the neighborhood, using different reference frame
  168. for (i = 0; i < 8; i++) {
  169. int c = p[i][0] + col, r = p[i][1] + row;
  170. if (c >= s->tiling.tile_col_start && c < s->cols &&
  171. r >= 0 && r < s->rows) {
  172. VP9MVRefPair *mv = &s->mv[0][r * s->sb_cols * 8 + c];
  173. if (mv->ref[0] != ref && mv->ref[0] >= 0)
  174. RETURN_SCALE_MV(mv->mv[0],
  175. s->signbias[mv->ref[0]] != s->signbias[ref]);
  176. if (mv->ref[1] != ref && mv->ref[1] >= 0)
  177. RETURN_SCALE_MV(mv->mv[1],
  178. s->signbias[mv->ref[1]] != s->signbias[ref]);
  179. }
  180. }
  181. // MV at this position in previous frame, using different reference frame
  182. if (s->use_last_frame_mvs) {
  183. VP9MVRefPair *mv = &s->mv[1][row * s->sb_cols * 8 + col];
  184. if (mv->ref[0] != ref && mv->ref[0] >= 0)
  185. RETURN_SCALE_MV(mv->mv[0],
  186. s->signbias[mv->ref[0]] != s->signbias[ref]);
  187. if (mv->ref[1] != ref && mv->ref[1] >= 0)
  188. RETURN_SCALE_MV(mv->mv[1],
  189. s->signbias[mv->ref[1]] != s->signbias[ref]);
  190. }
  191. AV_ZERO32(pmv);
  192. #undef INVALID_MV
  193. #undef RETURN_MV
  194. #undef RETURN_SCALE_MV
  195. }
  196. static av_always_inline int read_mv_component(VP9Context *s, int idx, int hp)
  197. {
  198. int bit, sign = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].sign);
  199. int n, c = vp8_rac_get_tree(&s->c, ff_vp9_mv_class_tree,
  200. s->prob.p.mv_comp[idx].classes);
  201. s->counts.mv_comp[idx].sign[sign]++;
  202. s->counts.mv_comp[idx].classes[c]++;
  203. if (c) {
  204. int m;
  205. for (n = 0, m = 0; m < c; m++) {
  206. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].bits[m]);
  207. n |= bit << m;
  208. s->counts.mv_comp[idx].bits[m][bit]++;
  209. }
  210. n <<= 3;
  211. bit = vp8_rac_get_tree(&s->c, ff_vp9_mv_fp_tree,
  212. s->prob.p.mv_comp[idx].fp);
  213. n |= bit << 1;
  214. s->counts.mv_comp[idx].fp[bit]++;
  215. if (hp) {
  216. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].hp);
  217. s->counts.mv_comp[idx].hp[bit]++;
  218. n |= bit;
  219. } else {
  220. n |= 1;
  221. // bug in libvpx - we count for bw entropy purposes even if the
  222. // bit wasn't coded
  223. s->counts.mv_comp[idx].hp[1]++;
  224. }
  225. n += 8 << c;
  226. } else {
  227. n = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0);
  228. s->counts.mv_comp[idx].class0[n]++;
  229. bit = vp8_rac_get_tree(&s->c, ff_vp9_mv_fp_tree,
  230. s->prob.p.mv_comp[idx].class0_fp[n]);
  231. s->counts.mv_comp[idx].class0_fp[n][bit]++;
  232. n = (n << 3) | (bit << 1);
  233. if (hp) {
  234. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0_hp);
  235. s->counts.mv_comp[idx].class0_hp[bit]++;
  236. n |= bit;
  237. } else {
  238. n |= 1;
  239. // bug in libvpx - we count for bw entropy purposes even if the
  240. // bit wasn't coded
  241. s->counts.mv_comp[idx].class0_hp[1]++;
  242. }
  243. }
  244. return sign ? -(n + 1) : (n + 1);
  245. }
  246. void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb)
  247. {
  248. VP9Block *const b = &s->b;
  249. if (mode == ZEROMV) {
  250. memset(mv, 0, sizeof(*mv) * 2);
  251. } else {
  252. int hp;
  253. // FIXME cache this value and reuse for other subblocks
  254. find_ref_mvs(s, &mv[0], b->ref[0], 0, mode == NEARMV,
  255. mode == NEWMV ? -1 : sb);
  256. // FIXME maybe move this code into find_ref_mvs()
  257. if ((mode == NEWMV || sb == -1) &&
  258. !(hp = s->highprecisionmvs &&
  259. abs(mv[0].x) < 64 && abs(mv[0].y) < 64)) {
  260. if (mv[0].y & 1) {
  261. if (mv[0].y < 0)
  262. mv[0].y++;
  263. else
  264. mv[0].y--;
  265. }
  266. if (mv[0].x & 1) {
  267. if (mv[0].x < 0)
  268. mv[0].x++;
  269. else
  270. mv[0].x--;
  271. }
  272. }
  273. if (mode == NEWMV) {
  274. enum MVJoint j = vp8_rac_get_tree(&s->c, ff_vp9_mv_joint_tree,
  275. s->prob.p.mv_joint);
  276. s->counts.mv_joint[j]++;
  277. if (j >= MV_JOINT_V)
  278. mv[0].y += read_mv_component(s, 0, hp);
  279. if (j & 1)
  280. mv[0].x += read_mv_component(s, 1, hp);
  281. }
  282. if (b->comp) {
  283. // FIXME cache this value and reuse for other subblocks
  284. find_ref_mvs(s, &mv[1], b->ref[1], 1, mode == NEARMV,
  285. mode == NEWMV ? -1 : sb);
  286. if ((mode == NEWMV || sb == -1) &&
  287. !(hp = s->highprecisionmvs &&
  288. abs(mv[1].x) < 64 && abs(mv[1].y) < 64)) {
  289. if (mv[1].y & 1) {
  290. if (mv[1].y < 0)
  291. mv[1].y++;
  292. else
  293. mv[1].y--;
  294. }
  295. if (mv[1].x & 1) {
  296. if (mv[1].x < 0)
  297. mv[1].x++;
  298. else
  299. mv[1].x--;
  300. }
  301. }
  302. if (mode == NEWMV) {
  303. enum MVJoint j = vp8_rac_get_tree(&s->c, ff_vp9_mv_joint_tree,
  304. s->prob.p.mv_joint);
  305. s->counts.mv_joint[j]++;
  306. if (j >= MV_JOINT_V)
  307. mv[1].y += read_mv_component(s, 0, hp);
  308. if (j & 1)
  309. mv[1].x += read_mv_component(s, 1, hp);
  310. }
  311. }
  312. }
  313. }