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.

361 lines
15KB

  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 FFmpeg.
  8. *
  9. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 = s->row, col = s->col, row7 = s->row7;
  66. const int8_t (*p)[2] = mv_ref_blk_off[b->bs];
  67. #define INVALID_MV 0x80008000U
  68. uint32_t mem = INVALID_MV, mem_sub8x8 = 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. av_assert2(idx == 1); \
  97. av_assert2(mem != INVALID_MV); \
  98. if (mem_sub8x8 == INVALID_MV) { \
  99. clamp_mv(&tmp, &mv, s); \
  100. m = AV_RN32A(&tmp); \
  101. if (m != mem) { \
  102. AV_WN32A(pmv, m); \
  103. return; \
  104. } \
  105. mem_sub8x8 = AV_RN32A(&mv); \
  106. } else if (mem_sub8x8 != AV_RN32A(&mv)) { \
  107. clamp_mv(&tmp, &mv, s); \
  108. m = AV_RN32A(&tmp); \
  109. if (m != mem) { \
  110. AV_WN32A(pmv, m); \
  111. } else { \
  112. /* BUG I'm pretty sure this isn't the intention */ \
  113. AV_WN32A(pmv, 0); \
  114. } \
  115. return; \
  116. } \
  117. } else { \
  118. uint32_t m = AV_RN32A(&mv); \
  119. if (!idx) { \
  120. clamp_mv(pmv, &mv, s); \
  121. return; \
  122. } else if (mem == INVALID_MV) { \
  123. mem = m; \
  124. } else if (m != mem) { \
  125. clamp_mv(pmv, &mv, s); \
  126. return; \
  127. } \
  128. } \
  129. } while (0)
  130. if (row > 0) {
  131. VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[(row - 1) * s->sb_cols * 8 + col];
  132. if (mv->ref[0] == ref)
  133. RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][0]);
  134. else if (mv->ref[1] == ref)
  135. RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][1]);
  136. }
  137. if (col > s->tile_col_start) {
  138. VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[row * s->sb_cols * 8 + col - 1];
  139. if (mv->ref[0] == ref)
  140. RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][0]);
  141. else if (mv->ref[1] == ref)
  142. RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][1]);
  143. }
  144. i = 2;
  145. } else {
  146. i = 0;
  147. }
  148. // previously coded MVs in this neighborhood, using same reference frame
  149. for (; i < 8; i++) {
  150. int c = p[i][0] + col, r = p[i][1] + row;
  151. if (c >= s->tile_col_start && c < s->cols &&
  152. r >= 0 && r < s->rows) {
  153. VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c];
  154. if (mv->ref[0] == ref)
  155. RETURN_MV(mv->mv[0]);
  156. else if (mv->ref[1] == ref)
  157. RETURN_MV(mv->mv[1]);
  158. }
  159. }
  160. // MV at this position in previous frame, using same reference frame
  161. if (s->s.h.use_last_frame_mvs) {
  162. VP9mvrefPair *mv = &s->s.frames[REF_FRAME_MVPAIR].mv[row * s->sb_cols * 8 + col];
  163. if (!s->s.frames[REF_FRAME_MVPAIR].uses_2pass)
  164. ff_thread_await_progress(&s->s.frames[REF_FRAME_MVPAIR].tf, row >> 3, 0);
  165. if (mv->ref[0] == ref)
  166. RETURN_MV(mv->mv[0]);
  167. else if (mv->ref[1] == ref)
  168. RETURN_MV(mv->mv[1]);
  169. }
  170. #define RETURN_SCALE_MV(mv, scale) \
  171. do { \
  172. if (scale) { \
  173. VP56mv mv_temp = { -mv.x, -mv.y }; \
  174. RETURN_MV(mv_temp); \
  175. } else { \
  176. RETURN_MV(mv); \
  177. } \
  178. } while (0)
  179. // previously coded MVs in this neighborhood, using different reference frame
  180. for (i = 0; i < 8; i++) {
  181. int c = p[i][0] + col, r = p[i][1] + row;
  182. if (c >= s->tile_col_start && c < s->cols && r >= 0 && r < s->rows) {
  183. VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c];
  184. if (mv->ref[0] != ref && mv->ref[0] >= 0)
  185. RETURN_SCALE_MV(mv->mv[0],
  186. s->s.h.signbias[mv->ref[0]] != s->s.h.signbias[ref]);
  187. if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
  188. // BUG - libvpx has this condition regardless of whether
  189. // we used the first ref MV and pre-scaling
  190. AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
  191. RETURN_SCALE_MV(mv->mv[1], s->s.h.signbias[mv->ref[1]] != s->s.h.signbias[ref]);
  192. }
  193. }
  194. }
  195. // MV at this position in previous frame, using different reference frame
  196. if (s->s.h.use_last_frame_mvs) {
  197. VP9mvrefPair *mv = &s->s.frames[REF_FRAME_MVPAIR].mv[row * s->sb_cols * 8 + col];
  198. // no need to await_progress, because we already did that above
  199. if (mv->ref[0] != ref && mv->ref[0] >= 0)
  200. RETURN_SCALE_MV(mv->mv[0], s->s.h.signbias[mv->ref[0]] != s->s.h.signbias[ref]);
  201. if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
  202. // BUG - libvpx has this condition regardless of whether
  203. // we used the first ref MV and pre-scaling
  204. AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
  205. RETURN_SCALE_MV(mv->mv[1], s->s.h.signbias[mv->ref[1]] != s->s.h.signbias[ref]);
  206. }
  207. }
  208. AV_ZERO32(pmv);
  209. clamp_mv(pmv, pmv, s);
  210. #undef INVALID_MV
  211. #undef RETURN_MV
  212. #undef RETURN_SCALE_MV
  213. }
  214. static av_always_inline int read_mv_component(VP9Context *s, int idx, int hp)
  215. {
  216. int bit, sign = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].sign);
  217. int n, c = vp8_rac_get_tree(&s->c, ff_vp9_mv_class_tree,
  218. s->prob.p.mv_comp[idx].classes);
  219. s->counts.mv_comp[idx].sign[sign]++;
  220. s->counts.mv_comp[idx].classes[c]++;
  221. if (c) {
  222. int m;
  223. for (n = 0, m = 0; m < c; m++) {
  224. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].bits[m]);
  225. n |= bit << m;
  226. s->counts.mv_comp[idx].bits[m][bit]++;
  227. }
  228. n <<= 3;
  229. bit = vp8_rac_get_tree(&s->c, ff_vp9_mv_fp_tree,
  230. s->prob.p.mv_comp[idx].fp);
  231. n |= bit << 1;
  232. s->counts.mv_comp[idx].fp[bit]++;
  233. if (hp) {
  234. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].hp);
  235. s->counts.mv_comp[idx].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].hp[1]++;
  242. }
  243. n += 8 << c;
  244. } else {
  245. n = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0);
  246. s->counts.mv_comp[idx].class0[n]++;
  247. bit = vp8_rac_get_tree(&s->c, ff_vp9_mv_fp_tree,
  248. s->prob.p.mv_comp[idx].class0_fp[n]);
  249. s->counts.mv_comp[idx].class0_fp[n][bit]++;
  250. n = (n << 3) | (bit << 1);
  251. if (hp) {
  252. bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0_hp);
  253. s->counts.mv_comp[idx].class0_hp[bit]++;
  254. n |= bit;
  255. } else {
  256. n |= 1;
  257. // bug in libvpx - we count for bw entropy purposes even if the
  258. // bit wasn't coded
  259. s->counts.mv_comp[idx].class0_hp[1]++;
  260. }
  261. }
  262. return sign ? -(n + 1) : (n + 1);
  263. }
  264. void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb)
  265. {
  266. VP9Block *b = s->b;
  267. if (mode == ZEROMV) {
  268. AV_ZERO64(mv);
  269. } else {
  270. int hp;
  271. // FIXME cache this value and reuse for other subblocks
  272. find_ref_mvs(s, &mv[0], b->ref[0], 0, mode == NEARMV,
  273. mode == NEWMV ? -1 : sb);
  274. // FIXME maybe move this code into find_ref_mvs()
  275. if ((mode == NEWMV || sb == -1) &&
  276. !(hp = s->s.h.highprecisionmvs &&
  277. abs(mv[0].x) < 64 && abs(mv[0].y) < 64)) {
  278. if (mv[0].y & 1) {
  279. if (mv[0].y < 0)
  280. mv[0].y++;
  281. else
  282. mv[0].y--;
  283. }
  284. if (mv[0].x & 1) {
  285. if (mv[0].x < 0)
  286. mv[0].x++;
  287. else
  288. mv[0].x--;
  289. }
  290. }
  291. if (mode == NEWMV) {
  292. enum MVJoint j = vp8_rac_get_tree(&s->c, ff_vp9_mv_joint_tree,
  293. s->prob.p.mv_joint);
  294. s->counts.mv_joint[j]++;
  295. if (j >= MV_JOINT_V)
  296. mv[0].y += read_mv_component(s, 0, hp);
  297. if (j & 1)
  298. mv[0].x += read_mv_component(s, 1, hp);
  299. }
  300. if (b->comp) {
  301. // FIXME cache this value and reuse for other subblocks
  302. find_ref_mvs(s, &mv[1], b->ref[1], 1, mode == NEARMV,
  303. mode == NEWMV ? -1 : sb);
  304. if ((mode == NEWMV || sb == -1) &&
  305. !(hp = s->s.h.highprecisionmvs &&
  306. abs(mv[1].x) < 64 && abs(mv[1].y) < 64)) {
  307. if (mv[1].y & 1) {
  308. if (mv[1].y < 0)
  309. mv[1].y++;
  310. else
  311. mv[1].y--;
  312. }
  313. if (mv[1].x & 1) {
  314. if (mv[1].x < 0)
  315. mv[1].x++;
  316. else
  317. mv[1].x--;
  318. }
  319. }
  320. if (mode == NEWMV) {
  321. enum MVJoint j = vp8_rac_get_tree(&s->c, ff_vp9_mv_joint_tree,
  322. s->prob.p.mv_joint);
  323. s->counts.mv_joint[j]++;
  324. if (j >= MV_JOINT_V)
  325. mv[1].y += read_mv_component(s, 0, hp);
  326. if (j & 1)
  327. mv[1].x += read_mv_component(s, 1, hp);
  328. }
  329. }
  330. }
  331. }