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.

353 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. // BUG - libvpx has this condition regardless of whether
  178. // we used the first ref MV and pre-scaling
  179. AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
  180. RETURN_SCALE_MV(mv->mv[1],
  181. s->signbias[mv->ref[1]] != s->signbias[ref]);
  182. }
  183. }
  184. }
  185. // MV at this position in previous frame, using different reference frame
  186. if (s->use_last_frame_mvs) {
  187. VP9MVRefPair *mv = &s->mv[1][row * s->sb_cols * 8 + col];
  188. if (mv->ref[0] != ref && mv->ref[0] >= 0)
  189. RETURN_SCALE_MV(mv->mv[0],
  190. s->signbias[mv->ref[0]] != s->signbias[ref]);
  191. if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
  192. // BUG - libvpx has this condition regardless of whether
  193. // we used the first ref MV and pre-scaling
  194. AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
  195. RETURN_SCALE_MV(mv->mv[1],
  196. s->signbias[mv->ref[1]] != s->signbias[ref]);
  197. }
  198. }
  199. AV_ZERO32(pmv);
  200. #undef INVALID_MV
  201. #undef RETURN_MV
  202. #undef RETURN_SCALE_MV
  203. }
  204. static av_always_inline int read_mv_component(VP9Context *s, int idx, int hp)
  205. {
  206. int bit, sign = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].sign);
  207. int n, c = vp8_rac_get_tree(&s->c, ff_vp9_mv_class_tree,
  208. s->prob.p.mv_comp[idx].classes);
  209. s->counts.mv_comp[idx].sign[sign]++;
  210. s->counts.mv_comp[idx].classes[c]++;
  211. if (c) {
  212. int m;
  213. for (n = 0, m = 0; m < c; m++) {
  214. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].bits[m]);
  215. n |= bit << m;
  216. s->counts.mv_comp[idx].bits[m][bit]++;
  217. }
  218. n <<= 3;
  219. bit = vp8_rac_get_tree(&s->c, ff_vp9_mv_fp_tree,
  220. s->prob.p.mv_comp[idx].fp);
  221. n |= bit << 1;
  222. s->counts.mv_comp[idx].fp[bit]++;
  223. if (hp) {
  224. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].hp);
  225. s->counts.mv_comp[idx].hp[bit]++;
  226. n |= bit;
  227. } else {
  228. n |= 1;
  229. // bug in libvpx - we count for bw entropy purposes even if the
  230. // bit wasn't coded
  231. s->counts.mv_comp[idx].hp[1]++;
  232. }
  233. n += 8 << c;
  234. } else {
  235. n = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0);
  236. s->counts.mv_comp[idx].class0[n]++;
  237. bit = vp8_rac_get_tree(&s->c, ff_vp9_mv_fp_tree,
  238. s->prob.p.mv_comp[idx].class0_fp[n]);
  239. s->counts.mv_comp[idx].class0_fp[n][bit]++;
  240. n = (n << 3) | (bit << 1);
  241. if (hp) {
  242. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0_hp);
  243. s->counts.mv_comp[idx].class0_hp[bit]++;
  244. n |= bit;
  245. } else {
  246. n |= 1;
  247. // bug in libvpx - we count for bw entropy purposes even if the
  248. // bit wasn't coded
  249. s->counts.mv_comp[idx].class0_hp[1]++;
  250. }
  251. }
  252. return sign ? -(n + 1) : (n + 1);
  253. }
  254. void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb)
  255. {
  256. VP9Block *const b = &s->b;
  257. if (mode == ZEROMV) {
  258. memset(mv, 0, sizeof(*mv) * 2);
  259. } else {
  260. int hp;
  261. // FIXME cache this value and reuse for other subblocks
  262. find_ref_mvs(s, &mv[0], b->ref[0], 0, mode == NEARMV,
  263. mode == NEWMV ? -1 : sb);
  264. // FIXME maybe move this code into find_ref_mvs()
  265. if ((mode == NEWMV || sb == -1) &&
  266. !(hp = s->highprecisionmvs &&
  267. abs(mv[0].x) < 64 && abs(mv[0].y) < 64)) {
  268. if (mv[0].y & 1) {
  269. if (mv[0].y < 0)
  270. mv[0].y++;
  271. else
  272. mv[0].y--;
  273. }
  274. if (mv[0].x & 1) {
  275. if (mv[0].x < 0)
  276. mv[0].x++;
  277. else
  278. mv[0].x--;
  279. }
  280. }
  281. if (mode == NEWMV) {
  282. enum MVJoint j = vp8_rac_get_tree(&s->c, ff_vp9_mv_joint_tree,
  283. s->prob.p.mv_joint);
  284. s->counts.mv_joint[j]++;
  285. if (j >= MV_JOINT_V)
  286. mv[0].y += read_mv_component(s, 0, hp);
  287. if (j & 1)
  288. mv[0].x += read_mv_component(s, 1, hp);
  289. }
  290. if (b->comp) {
  291. // FIXME cache this value and reuse for other subblocks
  292. find_ref_mvs(s, &mv[1], b->ref[1], 1, mode == NEARMV,
  293. mode == NEWMV ? -1 : sb);
  294. if ((mode == NEWMV || sb == -1) &&
  295. !(hp = s->highprecisionmvs &&
  296. abs(mv[1].x) < 64 && abs(mv[1].y) < 64)) {
  297. if (mv[1].y & 1) {
  298. if (mv[1].y < 0)
  299. mv[1].y++;
  300. else
  301. mv[1].y--;
  302. }
  303. if (mv[1].x & 1) {
  304. if (mv[1].x < 0)
  305. mv[1].x++;
  306. else
  307. mv[1].x--;
  308. }
  309. }
  310. if (mode == NEWMV) {
  311. enum MVJoint j = vp8_rac_get_tree(&s->c, ff_vp9_mv_joint_tree,
  312. s->prob.p.mv_joint);
  313. s->counts.mv_joint[j]++;
  314. if (j >= MV_JOINT_V)
  315. mv[1].y += read_mv_component(s, 0, hp);
  316. if (j & 1)
  317. mv[1].x += read_mv_component(s, 1, hp);
  318. }
  319. }
  320. }
  321. }