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.

275 lines
9.9KB

  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 "vp56.h"
  24. #include "vp9.h"
  25. #include "vp9data.h"
  26. static av_always_inline void adapt_prob(uint8_t *p, unsigned ct0, unsigned ct1,
  27. int max_count, int update_factor)
  28. {
  29. unsigned ct = ct0 + ct1, p2, p1;
  30. if (!ct)
  31. return;
  32. p1 = *p;
  33. p2 = ((ct0 << 8) + (ct >> 1)) / ct;
  34. p2 = av_clip(p2, 1, 255);
  35. ct = FFMIN(ct, max_count);
  36. update_factor = FASTDIV(update_factor * ct, max_count);
  37. // (p1 * (256 - update_factor) + p2 * update_factor + 128) >> 8
  38. *p = p1 + (((p2 - p1) * update_factor + 128) >> 8);
  39. }
  40. void ff_vp9_adapt_probs(VP9Context *s)
  41. {
  42. int i, j, k, l, m;
  43. ProbContext *p = &s->prob_ctx[s->framectxid].p;
  44. int uf = (s->keyframe || s->intraonly || !s->last_keyframe) ? 112 : 128;
  45. // coefficients
  46. for (i = 0; i < 4; i++)
  47. for (j = 0; j < 2; j++)
  48. for (k = 0; k < 2; k++)
  49. for (l = 0; l < 6; l++)
  50. for (m = 0; m < 6; m++) {
  51. uint8_t *pp = s->prob_ctx[s->framectxid].coef[i][j][k][l][m];
  52. unsigned *e = s->counts.eob[i][j][k][l][m];
  53. unsigned *c = s->counts.coef[i][j][k][l][m];
  54. if (l == 0 && m >= 3) // dc only has 3 pt
  55. break;
  56. adapt_prob(&pp[0], e[0], e[1], 24, uf);
  57. adapt_prob(&pp[1], c[0], c[1] + c[2], 24, uf);
  58. adapt_prob(&pp[2], c[1], c[2], 24, uf);
  59. }
  60. if (s->keyframe || s->intraonly) {
  61. memcpy(p->skip, s->prob.p.skip, sizeof(p->skip));
  62. memcpy(p->tx32p, s->prob.p.tx32p, sizeof(p->tx32p));
  63. memcpy(p->tx16p, s->prob.p.tx16p, sizeof(p->tx16p));
  64. memcpy(p->tx8p, s->prob.p.tx8p, sizeof(p->tx8p));
  65. return;
  66. }
  67. // skip flag
  68. for (i = 0; i < 3; i++)
  69. adapt_prob(&p->skip[i], s->counts.skip[i][0],
  70. s->counts.skip[i][1], 20, 128);
  71. // intra/inter flag
  72. for (i = 0; i < 4; i++)
  73. adapt_prob(&p->intra[i], s->counts.intra[i][0],
  74. s->counts.intra[i][1], 20, 128);
  75. // comppred flag
  76. if (s->comppredmode == PRED_SWITCHABLE) {
  77. for (i = 0; i < 5; i++)
  78. adapt_prob(&p->comp[i], s->counts.comp[i][0],
  79. s->counts.comp[i][1], 20, 128);
  80. }
  81. // reference frames
  82. if (s->comppredmode != PRED_SINGLEREF) {
  83. for (i = 0; i < 5; i++)
  84. adapt_prob(&p->comp_ref[i], s->counts.comp_ref[i][0],
  85. s->counts.comp_ref[i][1], 20, 128);
  86. }
  87. if (s->comppredmode != PRED_COMPREF) {
  88. for (i = 0; i < 5; i++) {
  89. uint8_t *pp = p->single_ref[i];
  90. unsigned (*c)[2] = s->counts.single_ref[i];
  91. adapt_prob(&pp[0], c[0][0], c[0][1], 20, 128);
  92. adapt_prob(&pp[1], c[1][0], c[1][1], 20, 128);
  93. }
  94. }
  95. // block partitioning
  96. for (i = 0; i < 4; i++)
  97. for (j = 0; j < 4; j++) {
  98. uint8_t *pp = p->partition[i][j];
  99. unsigned *c = s->counts.partition[i][j];
  100. adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
  101. adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
  102. adapt_prob(&pp[2], c[2], c[3], 20, 128);
  103. }
  104. // tx size
  105. if (s->txfmmode == TX_SWITCHABLE) {
  106. for (i = 0; i < 2; i++) {
  107. unsigned *c16 = s->counts.tx16p[i], *c32 = s->counts.tx32p[i];
  108. adapt_prob(&p->tx8p[i], s->counts.tx8p[i][0],
  109. s->counts.tx8p[i][1], 20, 128);
  110. adapt_prob(&p->tx16p[i][0], c16[0], c16[1] + c16[2], 20, 128);
  111. adapt_prob(&p->tx16p[i][1], c16[1], c16[2], 20, 128);
  112. adapt_prob(&p->tx32p[i][0], c32[0], c32[1] + c32[2] + c32[3], 20, 128);
  113. adapt_prob(&p->tx32p[i][1], c32[1], c32[2] + c32[3], 20, 128);
  114. adapt_prob(&p->tx32p[i][2], c32[2], c32[3], 20, 128);
  115. }
  116. }
  117. // interpolation filter
  118. if (s->filtermode == FILTER_SWITCHABLE) {
  119. for (i = 0; i < 4; i++) {
  120. uint8_t *pp = p->filter[i];
  121. unsigned *c = s->counts.filter[i];
  122. adapt_prob(&pp[0], c[0], c[1] + c[2], 20, 128);
  123. adapt_prob(&pp[1], c[1], c[2], 20, 128);
  124. }
  125. }
  126. // inter modes
  127. for (i = 0; i < 7; i++) {
  128. uint8_t *pp = p->mv_mode[i];
  129. unsigned *c = s->counts.mv_mode[i];
  130. adapt_prob(&pp[0], c[2], c[1] + c[0] + c[3], 20, 128);
  131. adapt_prob(&pp[1], c[0], c[1] + c[3], 20, 128);
  132. adapt_prob(&pp[2], c[1], c[3], 20, 128);
  133. }
  134. // mv joints
  135. {
  136. uint8_t *pp = p->mv_joint;
  137. unsigned *c = s->counts.mv_joint;
  138. adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
  139. adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
  140. adapt_prob(&pp[2], c[2], c[3], 20, 128);
  141. }
  142. // mv components
  143. for (i = 0; i < 2; i++) {
  144. uint8_t *pp;
  145. unsigned *c, (*c2)[2], sum;
  146. adapt_prob(&p->mv_comp[i].sign, s->counts.mv_comp[i].sign[0],
  147. s->counts.mv_comp[i].sign[1], 20, 128);
  148. pp = p->mv_comp[i].classes;
  149. c = s->counts.mv_comp[i].classes;
  150. sum = c[1] + c[2] + c[3] + c[4] + c[5] +
  151. c[6] + c[7] + c[8] + c[9] + c[10];
  152. adapt_prob(&pp[0], c[0], sum, 20, 128);
  153. sum -= c[1];
  154. adapt_prob(&pp[1], c[1], sum, 20, 128);
  155. sum -= c[2] + c[3];
  156. adapt_prob(&pp[2], c[2] + c[3], sum, 20, 128);
  157. adapt_prob(&pp[3], c[2], c[3], 20, 128);
  158. sum -= c[4] + c[5];
  159. adapt_prob(&pp[4], c[4] + c[5], sum, 20, 128);
  160. adapt_prob(&pp[5], c[4], c[5], 20, 128);
  161. sum -= c[6];
  162. adapt_prob(&pp[6], c[6], sum, 20, 128);
  163. adapt_prob(&pp[7], c[7] + c[8], c[9] + c[10], 20, 128);
  164. adapt_prob(&pp[8], c[7], c[8], 20, 128);
  165. adapt_prob(&pp[9], c[9], c[10], 20, 128);
  166. adapt_prob(&p->mv_comp[i].class0, s->counts.mv_comp[i].class0[0],
  167. s->counts.mv_comp[i].class0[1], 20, 128);
  168. pp = p->mv_comp[i].bits;
  169. c2 = s->counts.mv_comp[i].bits;
  170. for (j = 0; j < 10; j++)
  171. adapt_prob(&pp[j], c2[j][0], c2[j][1], 20, 128);
  172. for (j = 0; j < 2; j++) {
  173. pp = p->mv_comp[i].class0_fp[j];
  174. c = s->counts.mv_comp[i].class0_fp[j];
  175. adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
  176. adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
  177. adapt_prob(&pp[2], c[2], c[3], 20, 128);
  178. }
  179. pp = p->mv_comp[i].fp;
  180. c = s->counts.mv_comp[i].fp;
  181. adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
  182. adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
  183. adapt_prob(&pp[2], c[2], c[3], 20, 128);
  184. if (s->highprecisionmvs) {
  185. adapt_prob(&p->mv_comp[i].class0_hp,
  186. s->counts.mv_comp[i].class0_hp[0],
  187. s->counts.mv_comp[i].class0_hp[1], 20, 128);
  188. adapt_prob(&p->mv_comp[i].hp, s->counts.mv_comp[i].hp[0],
  189. s->counts.mv_comp[i].hp[1], 20, 128);
  190. }
  191. }
  192. // y intra modes
  193. for (i = 0; i < 4; i++) {
  194. uint8_t *pp = p->y_mode[i];
  195. unsigned *c = s->counts.y_mode[i], sum, s2;
  196. sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
  197. adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
  198. sum -= c[TM_VP8_PRED];
  199. adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
  200. sum -= c[VERT_PRED];
  201. adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
  202. s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
  203. sum -= s2;
  204. adapt_prob(&pp[3], s2, sum, 20, 128);
  205. s2 -= c[HOR_PRED];
  206. adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
  207. adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED],
  208. 20, 128);
  209. sum -= c[DIAG_DOWN_LEFT_PRED];
  210. adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
  211. sum -= c[VERT_LEFT_PRED];
  212. adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
  213. adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
  214. }
  215. // uv intra modes
  216. for (i = 0; i < 10; i++) {
  217. uint8_t *pp = p->uv_mode[i];
  218. unsigned *c = s->counts.uv_mode[i], sum, s2;
  219. sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
  220. adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
  221. sum -= c[TM_VP8_PRED];
  222. adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
  223. sum -= c[VERT_PRED];
  224. adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
  225. s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
  226. sum -= s2;
  227. adapt_prob(&pp[3], s2, sum, 20, 128);
  228. s2 -= c[HOR_PRED];
  229. adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
  230. adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED],
  231. 20, 128);
  232. sum -= c[DIAG_DOWN_LEFT_PRED];
  233. adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
  234. sum -= c[VERT_LEFT_PRED];
  235. adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
  236. adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
  237. }
  238. }