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.

495 lines
15KB

  1. /*
  2. * Copyright (C) 2004 The FFmpeg project
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Standard C DSP-oriented functions cribbed from the original VP3
  23. * source code.
  24. */
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/avassert.h"
  29. #include "avcodec.h"
  30. #include "rnd_avg.h"
  31. #include "vp3dsp.h"
  32. #define IdctAdjustBeforeShift 8
  33. #define xC1S7 64277
  34. #define xC2S6 60547
  35. #define xC3S5 54491
  36. #define xC4S4 46341
  37. #define xC5S3 36410
  38. #define xC6S2 25080
  39. #define xC7S1 12785
  40. #define M(a, b) ((int)((SUINT)(a) * (b)) >> 16)
  41. static av_always_inline void idct(uint8_t *dst, ptrdiff_t stride,
  42. int16_t *input, int type)
  43. {
  44. int16_t *ip = input;
  45. int A, B, C, D, Ad, Bd, Cd, Dd, E, F, G, H;
  46. int Ed, Gd, Add, Bdd, Fd, Hd;
  47. int i;
  48. /* Inverse DCT on the rows now */
  49. for (i = 0; i < 8; i++) {
  50. /* Check for non-zero values */
  51. if (ip[0 * 8] | ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
  52. ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8]) {
  53. A = M(xC1S7, ip[1 * 8]) + M(xC7S1, ip[7 * 8]);
  54. B = M(xC7S1, ip[1 * 8]) - M(xC1S7, ip[7 * 8]);
  55. C = M(xC3S5, ip[3 * 8]) + M(xC5S3, ip[5 * 8]);
  56. D = M(xC3S5, ip[5 * 8]) - M(xC5S3, ip[3 * 8]);
  57. Ad = M(xC4S4, (A - C));
  58. Bd = M(xC4S4, (B - D));
  59. Cd = A + C;
  60. Dd = B + D;
  61. E = M(xC4S4, (ip[0 * 8] + ip[4 * 8]));
  62. F = M(xC4S4, (ip[0 * 8] - ip[4 * 8]));
  63. G = M(xC2S6, ip[2 * 8]) + M(xC6S2, ip[6 * 8]);
  64. H = M(xC6S2, ip[2 * 8]) - M(xC2S6, ip[6 * 8]);
  65. Ed = E - G;
  66. Gd = E + G;
  67. Add = F + Ad;
  68. Bdd = Bd - H;
  69. Fd = F - Ad;
  70. Hd = Bd + H;
  71. /* Final sequence of operations over-write original inputs. */
  72. ip[0 * 8] = Gd + Cd;
  73. ip[7 * 8] = Gd - Cd;
  74. ip[1 * 8] = Add + Hd;
  75. ip[2 * 8] = Add - Hd;
  76. ip[3 * 8] = Ed + Dd;
  77. ip[4 * 8] = Ed - Dd;
  78. ip[5 * 8] = Fd + Bdd;
  79. ip[6 * 8] = Fd - Bdd;
  80. }
  81. ip += 1; /* next row */
  82. }
  83. ip = input;
  84. for (i = 0; i < 8; i++) {
  85. /* Check for non-zero values (bitwise or faster than ||) */
  86. if (ip[1] | ip[2] | ip[3] |
  87. ip[4] | ip[5] | ip[6] | ip[7]) {
  88. A = M(xC1S7, ip[1]) + M(xC7S1, ip[7]);
  89. B = M(xC7S1, ip[1]) - M(xC1S7, ip[7]);
  90. C = M(xC3S5, ip[3]) + M(xC5S3, ip[5]);
  91. D = M(xC3S5, ip[5]) - M(xC5S3, ip[3]);
  92. Ad = M(xC4S4, (A - C));
  93. Bd = M(xC4S4, (B - D));
  94. Cd = A + C;
  95. Dd = B + D;
  96. E = M(xC4S4, (ip[0] + ip[4])) + 8;
  97. F = M(xC4S4, (ip[0] - ip[4])) + 8;
  98. if (type == 1) { // HACK
  99. E += 16 * 128;
  100. F += 16 * 128;
  101. }
  102. G = M(xC2S6, ip[2]) + M(xC6S2, ip[6]);
  103. H = M(xC6S2, ip[2]) - M(xC2S6, ip[6]);
  104. Ed = E - G;
  105. Gd = E + G;
  106. Add = F + Ad;
  107. Bdd = Bd - H;
  108. Fd = F - Ad;
  109. Hd = Bd + H;
  110. /* Final sequence of operations over-write original inputs. */
  111. if (type == 1) {
  112. dst[0 * stride] = av_clip_uint8((Gd + Cd) >> 4);
  113. dst[7 * stride] = av_clip_uint8((Gd - Cd) >> 4);
  114. dst[1 * stride] = av_clip_uint8((Add + Hd) >> 4);
  115. dst[2 * stride] = av_clip_uint8((Add - Hd) >> 4);
  116. dst[3 * stride] = av_clip_uint8((Ed + Dd) >> 4);
  117. dst[4 * stride] = av_clip_uint8((Ed - Dd) >> 4);
  118. dst[5 * stride] = av_clip_uint8((Fd + Bdd) >> 4);
  119. dst[6 * stride] = av_clip_uint8((Fd - Bdd) >> 4);
  120. } else {
  121. dst[0 * stride] = av_clip_uint8(dst[0 * stride] + ((Gd + Cd) >> 4));
  122. dst[7 * stride] = av_clip_uint8(dst[7 * stride] + ((Gd - Cd) >> 4));
  123. dst[1 * stride] = av_clip_uint8(dst[1 * stride] + ((Add + Hd) >> 4));
  124. dst[2 * stride] = av_clip_uint8(dst[2 * stride] + ((Add - Hd) >> 4));
  125. dst[3 * stride] = av_clip_uint8(dst[3 * stride] + ((Ed + Dd) >> 4));
  126. dst[4 * stride] = av_clip_uint8(dst[4 * stride] + ((Ed - Dd) >> 4));
  127. dst[5 * stride] = av_clip_uint8(dst[5 * stride] + ((Fd + Bdd) >> 4));
  128. dst[6 * stride] = av_clip_uint8(dst[6 * stride] + ((Fd - Bdd) >> 4));
  129. }
  130. } else {
  131. if (type == 1) {
  132. dst[0*stride] =
  133. dst[1*stride] =
  134. dst[2*stride] =
  135. dst[3*stride] =
  136. dst[4*stride] =
  137. dst[5*stride] =
  138. dst[6*stride] =
  139. dst[7*stride] = av_clip_uint8(128 + ((xC4S4 * ip[0] + (IdctAdjustBeforeShift << 16)) >> 20));
  140. } else {
  141. if (ip[0]) {
  142. int v = (xC4S4 * ip[0] + (IdctAdjustBeforeShift << 16)) >> 20;
  143. dst[0 * stride] = av_clip_uint8(dst[0 * stride] + v);
  144. dst[1 * stride] = av_clip_uint8(dst[1 * stride] + v);
  145. dst[2 * stride] = av_clip_uint8(dst[2 * stride] + v);
  146. dst[3 * stride] = av_clip_uint8(dst[3 * stride] + v);
  147. dst[4 * stride] = av_clip_uint8(dst[4 * stride] + v);
  148. dst[5 * stride] = av_clip_uint8(dst[5 * stride] + v);
  149. dst[6 * stride] = av_clip_uint8(dst[6 * stride] + v);
  150. dst[7 * stride] = av_clip_uint8(dst[7 * stride] + v);
  151. }
  152. }
  153. }
  154. ip += 8; /* next column */
  155. dst++;
  156. }
  157. }
  158. static av_always_inline void idct10(uint8_t *dst, ptrdiff_t stride,
  159. int16_t *input, int type)
  160. {
  161. int16_t *ip = input;
  162. int A, B, C, D, Ad, Bd, Cd, Dd, E, F, G, H;
  163. int Ed, Gd, Add, Bdd, Fd, Hd;
  164. int i;
  165. /* Inverse DCT on the rows now */
  166. for (i = 0; i < 4; i++) {
  167. /* Check for non-zero values */
  168. if (ip[0 * 8] | ip[1 * 8] | ip[2 * 8] | ip[3 * 8]) {
  169. A = M(xC1S7, ip[1 * 8]);
  170. B = M(xC7S1, ip[1 * 8]);
  171. C = M(xC3S5, ip[3 * 8]);
  172. D = -M(xC5S3, ip[3 * 8]);
  173. Ad = M(xC4S4, (A - C));
  174. Bd = M(xC4S4, (B - D));
  175. Cd = A + C;
  176. Dd = B + D;
  177. E = M(xC4S4, ip[0 * 8]);
  178. F = E;
  179. G = M(xC2S6, ip[2 * 8]);
  180. H = M(xC6S2, ip[2 * 8]);
  181. Ed = E - G;
  182. Gd = E + G;
  183. Add = F + Ad;
  184. Bdd = Bd - H;
  185. Fd = F - Ad;
  186. Hd = Bd + H;
  187. /* Final sequence of operations over-write original inputs */
  188. ip[0 * 8] = Gd + Cd;
  189. ip[7 * 8] = Gd - Cd;
  190. ip[1 * 8] = Add + Hd;
  191. ip[2 * 8] = Add - Hd;
  192. ip[3 * 8] = Ed + Dd;
  193. ip[4 * 8] = Ed - Dd;
  194. ip[5 * 8] = Fd + Bdd;
  195. ip[6 * 8] = Fd - Bdd;
  196. }
  197. ip += 1;
  198. }
  199. ip = input;
  200. for (i = 0; i < 8; i++) {
  201. /* Check for non-zero values (bitwise or faster than ||) */
  202. if (ip[0] | ip[1] | ip[2] | ip[3]) {
  203. A = M(xC1S7, ip[1]);
  204. B = M(xC7S1, ip[1]);
  205. C = M(xC3S5, ip[3]);
  206. D = -M(xC5S3, ip[3]);
  207. Ad = M(xC4S4, (A - C));
  208. Bd = M(xC4S4, (B - D));
  209. Cd = A + C;
  210. Dd = B + D;
  211. E = M(xC4S4, ip[0]);
  212. if (type == 1)
  213. E += 16 * 128;
  214. F = E;
  215. G = M(xC2S6, ip[2]);
  216. H = M(xC6S2, ip[2]);
  217. Ed = E - G;
  218. Gd = E + G;
  219. Add = F + Ad;
  220. Bdd = Bd - H;
  221. Fd = F - Ad;
  222. Hd = Bd + H;
  223. Gd += 8;
  224. Add += 8;
  225. Ed += 8;
  226. Fd += 8;
  227. /* Final sequence of operations over-write original inputs. */
  228. if (type == 1) {
  229. dst[0 * stride] = av_clip_uint8((Gd + Cd) >> 4);
  230. dst[7 * stride] = av_clip_uint8((Gd - Cd) >> 4);
  231. dst[1 * stride] = av_clip_uint8((Add + Hd) >> 4);
  232. dst[2 * stride] = av_clip_uint8((Add - Hd) >> 4);
  233. dst[3 * stride] = av_clip_uint8((Ed + Dd) >> 4);
  234. dst[4 * stride] = av_clip_uint8((Ed - Dd) >> 4);
  235. dst[5 * stride] = av_clip_uint8((Fd + Bdd) >> 4);
  236. dst[6 * stride] = av_clip_uint8((Fd - Bdd) >> 4);
  237. } else {
  238. dst[0 * stride] = av_clip_uint8(dst[0 * stride] + ((Gd + Cd) >> 4));
  239. dst[7 * stride] = av_clip_uint8(dst[7 * stride] + ((Gd - Cd) >> 4));
  240. dst[1 * stride] = av_clip_uint8(dst[1 * stride] + ((Add + Hd) >> 4));
  241. dst[2 * stride] = av_clip_uint8(dst[2 * stride] + ((Add - Hd) >> 4));
  242. dst[3 * stride] = av_clip_uint8(dst[3 * stride] + ((Ed + Dd) >> 4));
  243. dst[4 * stride] = av_clip_uint8(dst[4 * stride] + ((Ed - Dd) >> 4));
  244. dst[5 * stride] = av_clip_uint8(dst[5 * stride] + ((Fd + Bdd) >> 4));
  245. dst[6 * stride] = av_clip_uint8(dst[6 * stride] + ((Fd - Bdd) >> 4));
  246. }
  247. } else {
  248. if (type == 1) {
  249. dst[0*stride] =
  250. dst[1*stride] =
  251. dst[2*stride] =
  252. dst[3*stride] =
  253. dst[4*stride] =
  254. dst[5*stride] =
  255. dst[6*stride] =
  256. dst[7*stride] = 128;
  257. }
  258. }
  259. ip += 8;
  260. dst++;
  261. }
  262. }
  263. void ff_vp3dsp_idct10_put(uint8_t *dest, ptrdiff_t stride, int16_t *block)
  264. {
  265. idct10(dest, stride, block, 1);
  266. memset(block, 0, sizeof(*block) * 64);
  267. }
  268. void ff_vp3dsp_idct10_add(uint8_t *dest, ptrdiff_t stride, int16_t *block)
  269. {
  270. idct10(dest, stride, block, 2);
  271. memset(block, 0, sizeof(*block) * 64);
  272. }
  273. static void vp3_idct_put_c(uint8_t *dest /* align 8 */, ptrdiff_t stride,
  274. int16_t *block /* align 16 */)
  275. {
  276. idct(dest, stride, block, 1);
  277. memset(block, 0, sizeof(*block) * 64);
  278. }
  279. static void vp3_idct_add_c(uint8_t *dest /* align 8 */, ptrdiff_t stride,
  280. int16_t *block /* align 16 */)
  281. {
  282. idct(dest, stride, block, 2);
  283. memset(block, 0, sizeof(*block) * 64);
  284. }
  285. static void vp3_idct_dc_add_c(uint8_t *dest /* align 8 */, ptrdiff_t stride,
  286. int16_t *block /* align 16 */)
  287. {
  288. int i, dc = (block[0] + 15) >> 5;
  289. for (i = 0; i < 8; i++) {
  290. dest[0] = av_clip_uint8(dest[0] + dc);
  291. dest[1] = av_clip_uint8(dest[1] + dc);
  292. dest[2] = av_clip_uint8(dest[2] + dc);
  293. dest[3] = av_clip_uint8(dest[3] + dc);
  294. dest[4] = av_clip_uint8(dest[4] + dc);
  295. dest[5] = av_clip_uint8(dest[5] + dc);
  296. dest[6] = av_clip_uint8(dest[6] + dc);
  297. dest[7] = av_clip_uint8(dest[7] + dc);
  298. dest += stride;
  299. }
  300. block[0] = 0;
  301. }
  302. static av_always_inline void vp3_v_loop_filter_c(uint8_t *first_pixel, ptrdiff_t stride,
  303. int *bounding_values, int count)
  304. {
  305. unsigned char *end;
  306. int filter_value;
  307. const ptrdiff_t nstride = -stride;
  308. for (end = first_pixel + count; first_pixel < end; first_pixel++) {
  309. filter_value = (first_pixel[2 * nstride] - first_pixel[stride]) +
  310. (first_pixel[0] - first_pixel[nstride]) * 3;
  311. filter_value = bounding_values[(filter_value + 4) >> 3];
  312. first_pixel[nstride] = av_clip_uint8(first_pixel[nstride] + filter_value);
  313. first_pixel[0] = av_clip_uint8(first_pixel[0] - filter_value);
  314. }
  315. }
  316. static av_always_inline void vp3_h_loop_filter_c(uint8_t *first_pixel, ptrdiff_t stride,
  317. int *bounding_values, int count)
  318. {
  319. unsigned char *end;
  320. int filter_value;
  321. for (end = first_pixel + count * stride; first_pixel != end; first_pixel += stride) {
  322. filter_value = (first_pixel[-2] - first_pixel[1]) +
  323. (first_pixel[ 0] - first_pixel[-1]) * 3;
  324. filter_value = bounding_values[(filter_value + 4) >> 3];
  325. first_pixel[-1] = av_clip_uint8(first_pixel[-1] + filter_value);
  326. first_pixel[ 0] = av_clip_uint8(first_pixel[ 0] - filter_value);
  327. }
  328. }
  329. #define LOOP_FILTER(prefix, suffix, dim, count) \
  330. void prefix##_##dim##_loop_filter_##count##suffix(uint8_t *first_pixel, ptrdiff_t stride, \
  331. int *bounding_values) \
  332. { \
  333. vp3_##dim##_loop_filter_c(first_pixel, stride, bounding_values, count); \
  334. }
  335. static LOOP_FILTER(vp3,_c, v, 8)
  336. static LOOP_FILTER(vp3,_c, h, 8)
  337. LOOP_FILTER(ff_vp3dsp, , v, 12)
  338. LOOP_FILTER(ff_vp3dsp, , h, 12)
  339. static void put_no_rnd_pixels_l2(uint8_t *dst, const uint8_t *src1,
  340. const uint8_t *src2, ptrdiff_t stride, int h)
  341. {
  342. int i;
  343. for (i = 0; i < h; i++) {
  344. uint32_t a, b;
  345. a = AV_RN32(&src1[i * stride]);
  346. b = AV_RN32(&src2[i * stride]);
  347. AV_WN32A(&dst[i * stride], no_rnd_avg32(a, b));
  348. a = AV_RN32(&src1[i * stride + 4]);
  349. b = AV_RN32(&src2[i * stride + 4]);
  350. AV_WN32A(&dst[i * stride + 4], no_rnd_avg32(a, b));
  351. }
  352. }
  353. av_cold void ff_vp3dsp_init(VP3DSPContext *c, int flags)
  354. {
  355. c->put_no_rnd_pixels_l2 = put_no_rnd_pixels_l2;
  356. c->idct_put = vp3_idct_put_c;
  357. c->idct_add = vp3_idct_add_c;
  358. c->idct_dc_add = vp3_idct_dc_add_c;
  359. c->v_loop_filter = vp3_v_loop_filter_8_c;
  360. c->h_loop_filter = vp3_h_loop_filter_8_c;
  361. if (ARCH_ARM)
  362. ff_vp3dsp_init_arm(c, flags);
  363. if (ARCH_PPC)
  364. ff_vp3dsp_init_ppc(c, flags);
  365. if (ARCH_X86)
  366. ff_vp3dsp_init_x86(c, flags);
  367. if (ARCH_MIPS)
  368. ff_vp3dsp_init_mips(c, flags);
  369. }
  370. /*
  371. * This function initializes the loop filter boundary limits if the frame's
  372. * quality index is different from the previous frame's.
  373. *
  374. * where sizeof(bounding_values_array) is 256 * sizeof(int)
  375. *
  376. * The filter_limit_values may not be larger than 127.
  377. */
  378. void ff_vp3dsp_set_bounding_values(int * bounding_values_array, int filter_limit)
  379. {
  380. int *bounding_values = bounding_values_array + 127;
  381. int x;
  382. int value;
  383. av_assert0(filter_limit < 128U);
  384. /* set up the bounding values */
  385. memset(bounding_values_array, 0, 256 * sizeof(int));
  386. for (x = 0; x < filter_limit; x++) {
  387. bounding_values[-x] = -x;
  388. bounding_values[x] = x;
  389. }
  390. for (x = value = filter_limit; x < 128 && value; x++, value--) {
  391. bounding_values[ x] = value;
  392. bounding_values[-x] = -value;
  393. }
  394. if (value)
  395. bounding_values[128] = value;
  396. bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
  397. }