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.

458 lines
14KB

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