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.

472 lines
15KB

  1. /**
  2. * VP8 compatible video decoder
  3. *
  4. * Copyright (C) 2010 David Conrad
  5. * Copyright (C) 2010 Ronald S. Bultje
  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 "dsputil.h"
  24. #include "vp8dsp.h"
  25. // TODO: Maybe add dequant
  26. static void vp8_luma_dc_wht_c(DCTELEM block[4][4][16], DCTELEM dc[16])
  27. {
  28. int i, t0, t1, t2, t3;
  29. for (i = 0; i < 4; i++) {
  30. t0 = dc[0*4+i] + dc[3*4+i];
  31. t1 = dc[1*4+i] + dc[2*4+i];
  32. t2 = dc[1*4+i] - dc[2*4+i];
  33. t3 = dc[0*4+i] - dc[3*4+i];
  34. dc[0*4+i] = t0 + t1;
  35. dc[1*4+i] = t3 + t2;
  36. dc[2*4+i] = t0 - t1;
  37. dc[3*4+i] = t3 - t2;
  38. }
  39. for (i = 0; i < 4; i++) {
  40. t0 = dc[i*4+0] + dc[i*4+3] + 3; // rounding
  41. t1 = dc[i*4+1] + dc[i*4+2];
  42. t2 = dc[i*4+1] - dc[i*4+2];
  43. t3 = dc[i*4+0] - dc[i*4+3] + 3; // rounding
  44. *block[i][0] = (t0 + t1) >> 3;
  45. *block[i][1] = (t3 + t2) >> 3;
  46. *block[i][2] = (t0 - t1) >> 3;
  47. *block[i][3] = (t3 - t2) >> 3;
  48. }
  49. }
  50. #define MUL_20091(a) ((((a)*20091) >> 16) + (a))
  51. #define MUL_35468(a) (((a)*35468) >> 16)
  52. static void vp8_idct_add_c(uint8_t *dst, DCTELEM block[16], int stride)
  53. {
  54. int i, t0, t1, t2, t3;
  55. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  56. DCTELEM tmp[16];
  57. for (i = 0; i < 4; i++) {
  58. t0 = block[0*4+i] + block[2*4+i];
  59. t1 = block[0*4+i] - block[2*4+i];
  60. t2 = MUL_35468(block[1*4+i]) - MUL_20091(block[3*4+i]);
  61. t3 = MUL_20091(block[1*4+i]) + MUL_35468(block[3*4+i]);
  62. tmp[i*4+0] = t0 + t3;
  63. tmp[i*4+1] = t1 + t2;
  64. tmp[i*4+2] = t1 - t2;
  65. tmp[i*4+3] = t0 - t3;
  66. }
  67. for (i = 0; i < 4; i++) {
  68. t0 = tmp[0*4+i] + tmp[2*4+i];
  69. t1 = tmp[0*4+i] - tmp[2*4+i];
  70. t2 = MUL_35468(tmp[1*4+i]) - MUL_20091(tmp[3*4+i]);
  71. t3 = MUL_20091(tmp[1*4+i]) + MUL_35468(tmp[3*4+i]);
  72. dst[0] = cm[dst[0] + ((t0 + t3 + 4) >> 3)];
  73. dst[1] = cm[dst[1] + ((t1 + t2 + 4) >> 3)];
  74. dst[2] = cm[dst[2] + ((t1 - t2 + 4) >> 3)];
  75. dst[3] = cm[dst[3] + ((t0 - t3 + 4) >> 3)];
  76. dst += stride;
  77. }
  78. }
  79. static void vp8_idct_dc_add_c(uint8_t *dst, DCTELEM block[16], int stride)
  80. {
  81. int i, dc = (block[0] + 4) >> 3;
  82. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP + dc;
  83. for (i = 0; i < 4; i++) {
  84. dst[0] = cm[dst[0]];
  85. dst[1] = cm[dst[1]];
  86. dst[2] = cm[dst[2]];
  87. dst[3] = cm[dst[3]];
  88. dst += stride;
  89. }
  90. }
  91. // because I like only having two parameters to pass functions...
  92. #define LOAD_PIXELS\
  93. int av_unused p3 = p[-4*stride];\
  94. int av_unused p2 = p[-3*stride];\
  95. int av_unused p1 = p[-2*stride];\
  96. int av_unused p0 = p[-1*stride];\
  97. int av_unused q0 = p[ 0*stride];\
  98. int av_unused q1 = p[ 1*stride];\
  99. int av_unused q2 = p[ 2*stride];\
  100. int av_unused q3 = p[ 3*stride];
  101. #define clip_int8(n) (cm[n+0x80]-0x80)
  102. static av_always_inline void filter_common(uint8_t *p, int stride, int is4tap)
  103. {
  104. LOAD_PIXELS
  105. int a, f1, f2;
  106. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  107. a = 3*(q0 - p0);
  108. if (is4tap)
  109. a += clip_int8(p1 - q1);
  110. a = clip_int8(a);
  111. // We deviate from the spec here with c(a+3) >> 3
  112. // since that's what libvpx does.
  113. f1 = FFMIN(a+4, 127) >> 3;
  114. f2 = FFMIN(a+3, 127) >> 3;
  115. // Despite what the spec says, we do need to clamp here to
  116. // be bitexact with libvpx.
  117. p[-1*stride] = cm[p0 + f2];
  118. p[ 0*stride] = cm[q0 - f1];
  119. // only used for _inner on blocks without high edge variance
  120. if (!is4tap) {
  121. a = (f1+1)>>1;
  122. p[-2*stride] = cm[p1 + a];
  123. p[ 1*stride] = cm[q1 - a];
  124. }
  125. }
  126. static av_always_inline int simple_limit(uint8_t *p, int stride, int flim)
  127. {
  128. LOAD_PIXELS
  129. return 2*FFABS(p0-q0) + (FFABS(p1-q1) >> 1) <= flim;
  130. }
  131. /**
  132. * E - limit at the macroblock edge
  133. * I - limit for interior difference
  134. */
  135. static av_always_inline int normal_limit(uint8_t *p, int stride, int E, int I)
  136. {
  137. LOAD_PIXELS
  138. return simple_limit(p, stride, 2*E+I)
  139. && FFABS(p3-p2) <= I && FFABS(p2-p1) <= I && FFABS(p1-p0) <= I
  140. && FFABS(q3-q2) <= I && FFABS(q2-q1) <= I && FFABS(q1-q0) <= I;
  141. }
  142. // high edge variance
  143. static av_always_inline int hev(uint8_t *p, int stride, int thresh)
  144. {
  145. LOAD_PIXELS
  146. return FFABS(p1-p0) > thresh || FFABS(q1-q0) > thresh;
  147. }
  148. static av_always_inline void filter_mbedge(uint8_t *p, int stride)
  149. {
  150. int a0, a1, a2, w;
  151. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  152. LOAD_PIXELS
  153. w = clip_int8(p1-q1);
  154. w = clip_int8(w + 3*(q0-p0));
  155. a0 = (27*w + 63) >> 7;
  156. a1 = (18*w + 63) >> 7;
  157. a2 = ( 9*w + 63) >> 7;
  158. p[-3*stride] = cm[p2 + a2];
  159. p[-2*stride] = cm[p1 + a1];
  160. p[-1*stride] = cm[p0 + a0];
  161. p[ 0*stride] = cm[q0 - a0];
  162. p[ 1*stride] = cm[q1 - a1];
  163. p[ 2*stride] = cm[q2 - a2];
  164. }
  165. #define LOOP_FILTER(dir, size, stridea, strideb) \
  166. static void vp8_ ## dir ## _loop_filter ## size ## _c(uint8_t *dst, int stride,\
  167. int flim_E, int flim_I, int hev_thresh)\
  168. {\
  169. int i;\
  170. \
  171. for (i = 0; i < size; i++)\
  172. if (normal_limit(dst+i*stridea, strideb, flim_E, flim_I)) {\
  173. if (hev(dst+i*stridea, strideb, hev_thresh))\
  174. filter_common(dst+i*stridea, strideb, 1);\
  175. else\
  176. filter_mbedge(dst+i*stridea, strideb);\
  177. }\
  178. }\
  179. \
  180. static void vp8_ ## dir ## _loop_filter ## size ## _inner_c(uint8_t *dst, int stride,\
  181. int flim_E, int flim_I, int hev_thresh)\
  182. {\
  183. int i;\
  184. \
  185. for (i = 0; i < size; i++)\
  186. if (normal_limit(dst+i*stridea, strideb, flim_E, flim_I)) {\
  187. int hv = hev(dst+i*stridea, strideb, hev_thresh);\
  188. if (hv) \
  189. filter_common(dst+i*stridea, strideb, 1);\
  190. else \
  191. filter_common(dst+i*stridea, strideb, 0);\
  192. }\
  193. }
  194. LOOP_FILTER(v, 16, 1, stride)
  195. LOOP_FILTER(h, 16, stride, 1)
  196. LOOP_FILTER(v, 8, 1, stride)
  197. LOOP_FILTER(h, 8, stride, 1)
  198. static void vp8_v_loop_filter_simple_c(uint8_t *dst, int stride, int flim)
  199. {
  200. int i;
  201. for (i = 0; i < 16; i++)
  202. if (simple_limit(dst+i, stride, flim))
  203. filter_common(dst+i, stride, 1);
  204. }
  205. static void vp8_h_loop_filter_simple_c(uint8_t *dst, int stride, int flim)
  206. {
  207. int i;
  208. for (i = 0; i < 16; i++)
  209. if (simple_limit(dst+i*stride, 1, flim))
  210. filter_common(dst+i*stride, 1, 1);
  211. }
  212. static const uint8_t subpel_filters[7][6] = {
  213. { 0, 6, 123, 12, 1, 0 },
  214. { 2, 11, 108, 36, 8, 1 },
  215. { 0, 9, 93, 50, 6, 0 },
  216. { 3, 16, 77, 77, 16, 3 },
  217. { 0, 6, 50, 93, 9, 0 },
  218. { 1, 8, 36, 108, 11, 2 },
  219. { 0, 1, 12, 123, 6, 0 },
  220. };
  221. #define PUT_PIXELS(WIDTH) \
  222. static void put_vp8_pixels ## WIDTH ##_c(uint8_t *dst, int dststride, uint8_t *src, int srcstride, int h, int x, int y) { \
  223. int i; \
  224. for (i = 0; i < h; i++, dst+= dststride, src+= srcstride) { \
  225. memcpy(dst, src, WIDTH); \
  226. } \
  227. }
  228. PUT_PIXELS(16)
  229. PUT_PIXELS(8)
  230. PUT_PIXELS(4)
  231. #define FILTER_6TAP(src, F, stride) \
  232. cm[(F[2]*src[x+0*stride] - F[1]*src[x-1*stride] + F[0]*src[x-2*stride] + \
  233. F[3]*src[x+1*stride] - F[4]*src[x+2*stride] + F[5]*src[x+3*stride] + 64) >> 7]
  234. #define FILTER_4TAP(src, F, stride) \
  235. cm[(F[2]*src[x+0*stride] - F[1]*src[x-1*stride] + \
  236. F[3]*src[x+1*stride] - F[4]*src[x+2*stride] + 64) >> 7]
  237. #define VP8_EPEL_H(SIZE, FILTER, FILTERNAME) \
  238. static void put_vp8_epel ## SIZE ## _ ## FILTERNAME ## _c(uint8_t *dst, int dststride, uint8_t *src, int srcstride, int h, int mx, int my) \
  239. { \
  240. const uint8_t *filter = subpel_filters[mx-1]; \
  241. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; \
  242. int x, y; \
  243. \
  244. for (y = 0; y < h; y++) { \
  245. for (x = 0; x < SIZE; x++) \
  246. dst[x] = FILTER(src, filter, 1); \
  247. dst += dststride; \
  248. src += srcstride; \
  249. } \
  250. }
  251. #define VP8_EPEL_V(SIZE, FILTER, FILTERNAME) \
  252. static void put_vp8_epel ## SIZE ## _ ## FILTERNAME ## _c(uint8_t *dst, int dststride, uint8_t *src, int srcstride, int h, int mx, int my) \
  253. { \
  254. const uint8_t *filter = subpel_filters[my-1]; \
  255. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; \
  256. int x, y; \
  257. \
  258. for (y = 0; y < h; y++) { \
  259. for (x = 0; x < SIZE; x++) \
  260. dst[x] = FILTER(src, filter, srcstride); \
  261. dst += dststride; \
  262. src += srcstride; \
  263. } \
  264. }
  265. #define VP8_EPEL_HV(SIZE, FILTERX, FILTERY, FILTERNAME) \
  266. static void put_vp8_epel ## SIZE ## _ ## FILTERNAME ## _c(uint8_t *dst, int dststride, uint8_t *src, int srcstride, int h, int mx, int my) \
  267. { \
  268. const uint8_t *filter = subpel_filters[mx-1]; \
  269. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; \
  270. int x, y; \
  271. uint8_t tmp_array[(2*SIZE+5)*SIZE]; \
  272. uint8_t *tmp = tmp_array; \
  273. src -= 2*srcstride; \
  274. \
  275. for (y = 0; y < h+5; y++) { \
  276. for (x = 0; x < SIZE; x++) \
  277. tmp[x] = FILTERX(src, filter, 1); \
  278. tmp += SIZE; \
  279. src += srcstride; \
  280. } \
  281. \
  282. tmp = tmp_array + 2*SIZE; \
  283. filter = subpel_filters[my-1]; \
  284. \
  285. for (y = 0; y < h; y++) { \
  286. for (x = 0; x < SIZE; x++) \
  287. dst[x] = FILTERY(tmp, filter, SIZE); \
  288. dst += dststride; \
  289. tmp += SIZE; \
  290. } \
  291. }
  292. VP8_EPEL_H(16, FILTER_4TAP, h4)
  293. VP8_EPEL_H(8, FILTER_4TAP, h4)
  294. VP8_EPEL_H(4, FILTER_4TAP, h4)
  295. VP8_EPEL_H(16, FILTER_6TAP, h6)
  296. VP8_EPEL_H(8, FILTER_6TAP, h6)
  297. VP8_EPEL_H(4, FILTER_6TAP, h6)
  298. VP8_EPEL_V(16, FILTER_4TAP, v4)
  299. VP8_EPEL_V(8, FILTER_4TAP, v4)
  300. VP8_EPEL_V(4, FILTER_4TAP, v4)
  301. VP8_EPEL_V(16, FILTER_6TAP, v6)
  302. VP8_EPEL_V(8, FILTER_6TAP, v6)
  303. VP8_EPEL_V(4, FILTER_6TAP, v6)
  304. VP8_EPEL_HV(16, FILTER_4TAP, FILTER_4TAP, h4v4)
  305. VP8_EPEL_HV(8, FILTER_4TAP, FILTER_4TAP, h4v4)
  306. VP8_EPEL_HV(4, FILTER_4TAP, FILTER_4TAP, h4v4)
  307. VP8_EPEL_HV(16, FILTER_4TAP, FILTER_6TAP, h4v6)
  308. VP8_EPEL_HV(8, FILTER_4TAP, FILTER_6TAP, h4v6)
  309. VP8_EPEL_HV(4, FILTER_4TAP, FILTER_6TAP, h4v6)
  310. VP8_EPEL_HV(16, FILTER_6TAP, FILTER_4TAP, h6v4)
  311. VP8_EPEL_HV(8, FILTER_6TAP, FILTER_4TAP, h6v4)
  312. VP8_EPEL_HV(4, FILTER_6TAP, FILTER_4TAP, h6v4)
  313. VP8_EPEL_HV(16, FILTER_6TAP, FILTER_6TAP, h6v6)
  314. VP8_EPEL_HV(8, FILTER_6TAP, FILTER_6TAP, h6v6)
  315. VP8_EPEL_HV(4, FILTER_6TAP, FILTER_6TAP, h6v6)
  316. #define VP8_BILINEAR(SIZE) \
  317. static void put_vp8_bilinear ## SIZE ## _h_c(uint8_t *dst, int stride, uint8_t *src, int s2, int h, int mx, int my) \
  318. { \
  319. int a = 8-mx, b = mx; \
  320. int x, y; \
  321. \
  322. for (y = 0; y < h; y++) { \
  323. for (x = 0; x < SIZE; x++) \
  324. dst[x] = (a*src[x] + b*src[x+1] + 4) >> 3; \
  325. dst += stride; \
  326. src += stride; \
  327. } \
  328. } \
  329. static void put_vp8_bilinear ## SIZE ## _v_c(uint8_t *dst, int stride, uint8_t *src, int s2, int h, int mx, int my) \
  330. { \
  331. int c = 8-my, d = my; \
  332. int x, y; \
  333. \
  334. for (y = 0; y < h; y++) { \
  335. for (x = 0; x < SIZE; x++) \
  336. dst[x] = (c*src[x] + d*src[x+stride] + 4) >> 3; \
  337. dst += stride; \
  338. src += stride; \
  339. } \
  340. } \
  341. \
  342. static void put_vp8_bilinear ## SIZE ## _hv_c(uint8_t *dst, int stride, uint8_t *src, int s2, int h, int mx, int my) \
  343. { \
  344. int a = 8-mx, b = mx; \
  345. int c = 8-my, d = my; \
  346. int x, y; \
  347. uint8_t tmp_array[(2*SIZE+1)*SIZE]; \
  348. uint8_t *tmp = tmp_array; \
  349. \
  350. for (y = 0; y < h+1; y++) { \
  351. for (x = 0; x < SIZE; x++) \
  352. tmp[x] = (a*src[x] + b*src[x+1] + 4) >> 3; \
  353. tmp += SIZE; \
  354. src += stride; \
  355. } \
  356. \
  357. tmp = tmp_array; \
  358. \
  359. for (y = 0; y < h; y++) { \
  360. for (x = 0; x < SIZE; x++) \
  361. dst[x] = (c*tmp[x] + d*tmp[x+SIZE] + 4) >> 3; \
  362. dst += stride; \
  363. tmp += SIZE; \
  364. } \
  365. }
  366. VP8_BILINEAR(16)
  367. VP8_BILINEAR(8)
  368. VP8_BILINEAR(4)
  369. #define VP8_MC_FUNC(IDX, SIZE) \
  370. dsp->put_vp8_epel_pixels_tab[IDX][0][0] = put_vp8_pixels ## SIZE ## _c; \
  371. dsp->put_vp8_epel_pixels_tab[IDX][0][1] = put_vp8_epel ## SIZE ## _h4_c; \
  372. dsp->put_vp8_epel_pixels_tab[IDX][0][2] = put_vp8_epel ## SIZE ## _h6_c; \
  373. dsp->put_vp8_epel_pixels_tab[IDX][1][0] = put_vp8_epel ## SIZE ## _v4_c; \
  374. dsp->put_vp8_epel_pixels_tab[IDX][1][1] = put_vp8_epel ## SIZE ## _h4v4_c; \
  375. dsp->put_vp8_epel_pixels_tab[IDX][1][2] = put_vp8_epel ## SIZE ## _h6v4_c; \
  376. dsp->put_vp8_epel_pixels_tab[IDX][2][0] = put_vp8_epel ## SIZE ## _v6_c; \
  377. dsp->put_vp8_epel_pixels_tab[IDX][2][1] = put_vp8_epel ## SIZE ## _h4v6_c; \
  378. dsp->put_vp8_epel_pixels_tab[IDX][2][2] = put_vp8_epel ## SIZE ## _h6v6_c
  379. #define VP8_BILINEAR_MC_FUNC(IDX, SIZE) \
  380. dsp->put_vp8_bilinear_pixels_tab[IDX][0][0] = put_vp8_pixels ## SIZE ## _c; \
  381. dsp->put_vp8_bilinear_pixels_tab[IDX][0][1] = put_vp8_bilinear ## SIZE ## _h_c; \
  382. dsp->put_vp8_bilinear_pixels_tab[IDX][0][2] = put_vp8_bilinear ## SIZE ## _h_c; \
  383. dsp->put_vp8_bilinear_pixels_tab[IDX][1][0] = put_vp8_bilinear ## SIZE ## _v_c; \
  384. dsp->put_vp8_bilinear_pixels_tab[IDX][1][1] = put_vp8_bilinear ## SIZE ## _hv_c; \
  385. dsp->put_vp8_bilinear_pixels_tab[IDX][1][2] = put_vp8_bilinear ## SIZE ## _hv_c; \
  386. dsp->put_vp8_bilinear_pixels_tab[IDX][2][0] = put_vp8_bilinear ## SIZE ## _v_c; \
  387. dsp->put_vp8_bilinear_pixels_tab[IDX][2][1] = put_vp8_bilinear ## SIZE ## _hv_c; \
  388. dsp->put_vp8_bilinear_pixels_tab[IDX][2][2] = put_vp8_bilinear ## SIZE ## _hv_c
  389. av_cold void ff_vp8dsp_init(VP8DSPContext *dsp)
  390. {
  391. dsp->vp8_luma_dc_wht = vp8_luma_dc_wht_c;
  392. dsp->vp8_idct_add = vp8_idct_add_c;
  393. dsp->vp8_idct_dc_add = vp8_idct_dc_add_c;
  394. dsp->vp8_v_loop_filter16 = vp8_v_loop_filter16_c;
  395. dsp->vp8_h_loop_filter16 = vp8_h_loop_filter16_c;
  396. dsp->vp8_v_loop_filter8 = vp8_v_loop_filter8_c;
  397. dsp->vp8_h_loop_filter8 = vp8_h_loop_filter8_c;
  398. dsp->vp8_v_loop_filter16_inner = vp8_v_loop_filter16_inner_c;
  399. dsp->vp8_h_loop_filter16_inner = vp8_h_loop_filter16_inner_c;
  400. dsp->vp8_v_loop_filter8_inner = vp8_v_loop_filter8_inner_c;
  401. dsp->vp8_h_loop_filter8_inner = vp8_h_loop_filter8_inner_c;
  402. dsp->vp8_v_loop_filter_simple = vp8_v_loop_filter_simple_c;
  403. dsp->vp8_h_loop_filter_simple = vp8_h_loop_filter_simple_c;
  404. VP8_MC_FUNC(0, 16);
  405. VP8_MC_FUNC(1, 8);
  406. VP8_MC_FUNC(2, 4);
  407. VP8_BILINEAR_MC_FUNC(0, 16);
  408. VP8_BILINEAR_MC_FUNC(1, 8);
  409. VP8_BILINEAR_MC_FUNC(2, 4);
  410. if (HAVE_MMX)
  411. ff_vp8dsp_init_x86(dsp);
  412. if (HAVE_ALTIVEC)
  413. ff_vp8dsp_init_altivec(dsp);
  414. }