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.

254 lines
7.0KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <assert.h>
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include "config.h"
  22. #include "libavutil/attributes.h"
  23. #include "libavutil/imgutils.h"
  24. #include "avcodec.h"
  25. #include "me_cmp.h"
  26. #include "mpegvideoencdsp.h"
  27. static int try_8x8basis_c(int16_t rem[64], int16_t weight[64],
  28. int16_t basis[64], int scale)
  29. {
  30. int i;
  31. unsigned int sum = 0;
  32. for (i = 0; i < 8 * 8; i++) {
  33. int b = rem[i] + ((basis[i] * scale +
  34. (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
  35. (BASIS_SHIFT - RECON_SHIFT));
  36. int w = weight[i];
  37. b >>= RECON_SHIFT;
  38. assert(-512 < b && b < 512);
  39. sum += (w * b) * (w * b) >> 4;
  40. }
  41. return sum >> 2;
  42. }
  43. static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale)
  44. {
  45. int i;
  46. for (i = 0; i < 8 * 8; i++)
  47. rem[i] += (basis[i] * scale +
  48. (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
  49. (BASIS_SHIFT - RECON_SHIFT);
  50. }
  51. static int pix_sum_c(uint8_t *pix, int line_size)
  52. {
  53. int s = 0, i, j;
  54. for (i = 0; i < 16; i++) {
  55. for (j = 0; j < 16; j += 8) {
  56. s += pix[0];
  57. s += pix[1];
  58. s += pix[2];
  59. s += pix[3];
  60. s += pix[4];
  61. s += pix[5];
  62. s += pix[6];
  63. s += pix[7];
  64. pix += 8;
  65. }
  66. pix += line_size - 16;
  67. }
  68. return s;
  69. }
  70. static int pix_norm1_c(uint8_t *pix, int line_size)
  71. {
  72. int s = 0, i, j;
  73. uint32_t *sq = ff_square_tab + 256;
  74. for (i = 0; i < 16; i++) {
  75. for (j = 0; j < 16; j += 8) {
  76. #if HAVE_FAST_64BIT
  77. register uint64_t x = *(uint64_t *) pix;
  78. s += sq[x & 0xff];
  79. s += sq[(x >> 8) & 0xff];
  80. s += sq[(x >> 16) & 0xff];
  81. s += sq[(x >> 24) & 0xff];
  82. s += sq[(x >> 32) & 0xff];
  83. s += sq[(x >> 40) & 0xff];
  84. s += sq[(x >> 48) & 0xff];
  85. s += sq[(x >> 56) & 0xff];
  86. #else
  87. register uint32_t x = *(uint32_t *) pix;
  88. s += sq[x & 0xff];
  89. s += sq[(x >> 8) & 0xff];
  90. s += sq[(x >> 16) & 0xff];
  91. s += sq[(x >> 24) & 0xff];
  92. x = *(uint32_t *) (pix + 4);
  93. s += sq[x & 0xff];
  94. s += sq[(x >> 8) & 0xff];
  95. s += sq[(x >> 16) & 0xff];
  96. s += sq[(x >> 24) & 0xff];
  97. #endif
  98. pix += 8;
  99. }
  100. pix += line_size - 16;
  101. }
  102. return s;
  103. }
  104. /* draw the edges of width 'w' of an image of size width, height */
  105. // FIXME: Check that this is OK for MPEG-4 interlaced.
  106. static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
  107. int w, int h, int sides)
  108. {
  109. uint8_t *ptr = buf, *last_line;
  110. int i;
  111. /* left and right */
  112. for (i = 0; i < height; i++) {
  113. memset(ptr - w, ptr[0], w);
  114. memset(ptr + width, ptr[width - 1], w);
  115. ptr += wrap;
  116. }
  117. /* top and bottom + corners */
  118. buf -= w;
  119. last_line = buf + (height - 1) * wrap;
  120. if (sides & EDGE_TOP)
  121. for (i = 0; i < h; i++)
  122. // top
  123. memcpy(buf - (i + 1) * wrap, buf, width + w + w);
  124. if (sides & EDGE_BOTTOM)
  125. for (i = 0; i < h; i++)
  126. // bottom
  127. memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
  128. }
  129. /* 2x2 -> 1x1 */
  130. static void shrink22(uint8_t *dst, int dst_wrap,
  131. const uint8_t *src, int src_wrap,
  132. int width, int height)
  133. {
  134. int w;
  135. const uint8_t *s1, *s2;
  136. uint8_t *d;
  137. for (; height > 0; height--) {
  138. s1 = src;
  139. s2 = s1 + src_wrap;
  140. d = dst;
  141. for (w = width; w >= 4; w -= 4) {
  142. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  143. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  144. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  145. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  146. s1 += 8;
  147. s2 += 8;
  148. d += 4;
  149. }
  150. for (; w > 0; w--) {
  151. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  152. s1 += 2;
  153. s2 += 2;
  154. d++;
  155. }
  156. src += 2 * src_wrap;
  157. dst += dst_wrap;
  158. }
  159. }
  160. /* 4x4 -> 1x1 */
  161. static void shrink44(uint8_t *dst, int dst_wrap,
  162. const uint8_t *src, int src_wrap,
  163. int width, int height)
  164. {
  165. int w;
  166. const uint8_t *s1, *s2, *s3, *s4;
  167. uint8_t *d;
  168. for (; height > 0; height--) {
  169. s1 = src;
  170. s2 = s1 + src_wrap;
  171. s3 = s2 + src_wrap;
  172. s4 = s3 + src_wrap;
  173. d = dst;
  174. for (w = width; w > 0; w--) {
  175. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  176. s2[0] + s2[1] + s2[2] + s2[3] +
  177. s3[0] + s3[1] + s3[2] + s3[3] +
  178. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  179. s1 += 4;
  180. s2 += 4;
  181. s3 += 4;
  182. s4 += 4;
  183. d++;
  184. }
  185. src += 4 * src_wrap;
  186. dst += dst_wrap;
  187. }
  188. }
  189. /* 8x8 -> 1x1 */
  190. static void shrink88(uint8_t *dst, int dst_wrap,
  191. const uint8_t *src, int src_wrap,
  192. int width, int height)
  193. {
  194. int w, i;
  195. for (; height > 0; height--) {
  196. for(w = width;w > 0; w--) {
  197. int tmp = 0;
  198. for (i = 0; i < 8; i++) {
  199. tmp += src[0] + src[1] + src[2] + src[3] +
  200. src[4] + src[5] + src[6] + src[7];
  201. src += src_wrap;
  202. }
  203. *(dst++) = (tmp + 32) >> 6;
  204. src += 8 - 8 * src_wrap;
  205. }
  206. src += 8 * src_wrap - 8 * width;
  207. dst += dst_wrap - width;
  208. }
  209. }
  210. av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c,
  211. AVCodecContext *avctx)
  212. {
  213. c->try_8x8basis = try_8x8basis_c;
  214. c->add_8x8basis = add_8x8basis_c;
  215. c->shrink[0] = av_image_copy_plane;
  216. c->shrink[1] = shrink22;
  217. c->shrink[2] = shrink44;
  218. c->shrink[3] = shrink88;
  219. c->pix_sum = pix_sum_c;
  220. c->pix_norm1 = pix_norm1_c;
  221. c->draw_edges = draw_edges_8_c;
  222. if (ARCH_ARM)
  223. ff_mpegvideoencdsp_init_arm(c, avctx);
  224. if (ARCH_PPC)
  225. ff_mpegvideoencdsp_init_ppc(c, avctx);
  226. if (ARCH_X86)
  227. ff_mpegvideoencdsp_init_x86(c, avctx);
  228. }