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.

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