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.

357 lines
14KB

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