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.

265 lines
7.2KB

  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 0
  77. s += sq[pix[0]];
  78. s += sq[pix[1]];
  79. s += sq[pix[2]];
  80. s += sq[pix[3]];
  81. s += sq[pix[4]];
  82. s += sq[pix[5]];
  83. s += sq[pix[6]];
  84. s += sq[pix[7]];
  85. #else
  86. #if HAVE_FAST_64BIT
  87. register uint64_t x = *(uint64_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. s += sq[(x >> 32) & 0xff];
  93. s += sq[(x >> 40) & 0xff];
  94. s += sq[(x >> 48) & 0xff];
  95. s += sq[(x >> 56) & 0xff];
  96. #else
  97. register uint32_t x = *(uint32_t *) pix;
  98. s += sq[x & 0xff];
  99. s += sq[(x >> 8) & 0xff];
  100. s += sq[(x >> 16) & 0xff];
  101. s += sq[(x >> 24) & 0xff];
  102. x = *(uint32_t *) (pix + 4);
  103. s += sq[x & 0xff];
  104. s += sq[(x >> 8) & 0xff];
  105. s += sq[(x >> 16) & 0xff];
  106. s += sq[(x >> 24) & 0xff];
  107. #endif
  108. #endif
  109. pix += 8;
  110. }
  111. pix += line_size - 16;
  112. }
  113. return s;
  114. }
  115. /* draw the edges of width 'w' of an image of size width, height */
  116. // FIXME: Check that this is OK for MPEG-4 interlaced.
  117. static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
  118. int w, int h, int sides)
  119. {
  120. uint8_t *ptr = buf, *last_line;
  121. int i;
  122. /* left and right */
  123. for (i = 0; i < height; i++) {
  124. memset(ptr - w, ptr[0], w);
  125. memset(ptr + width, ptr[width - 1], w);
  126. ptr += wrap;
  127. }
  128. /* top and bottom + corners */
  129. buf -= w;
  130. last_line = buf + (height - 1) * wrap;
  131. if (sides & EDGE_TOP)
  132. for (i = 0; i < h; i++)
  133. // top
  134. memcpy(buf - (i + 1) * wrap, buf, width + w + w);
  135. if (sides & EDGE_BOTTOM)
  136. for (i = 0; i < h; i++)
  137. // bottom
  138. memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
  139. }
  140. /* 2x2 -> 1x1 */
  141. static void shrink22(uint8_t *dst, int dst_wrap,
  142. const uint8_t *src, int src_wrap,
  143. int width, int height)
  144. {
  145. int w;
  146. const uint8_t *s1, *s2;
  147. uint8_t *d;
  148. for (; height > 0; height--) {
  149. s1 = src;
  150. s2 = s1 + src_wrap;
  151. d = dst;
  152. for (w = width; w >= 4; w -= 4) {
  153. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  154. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  155. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  156. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  157. s1 += 8;
  158. s2 += 8;
  159. d += 4;
  160. }
  161. for (; w > 0; w--) {
  162. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  163. s1 += 2;
  164. s2 += 2;
  165. d++;
  166. }
  167. src += 2 * src_wrap;
  168. dst += dst_wrap;
  169. }
  170. }
  171. /* 4x4 -> 1x1 */
  172. static void shrink44(uint8_t *dst, int dst_wrap,
  173. const uint8_t *src, int src_wrap,
  174. int width, int height)
  175. {
  176. int w;
  177. const uint8_t *s1, *s2, *s3, *s4;
  178. uint8_t *d;
  179. for (; height > 0; height--) {
  180. s1 = src;
  181. s2 = s1 + src_wrap;
  182. s3 = s2 + src_wrap;
  183. s4 = s3 + src_wrap;
  184. d = dst;
  185. for (w = width; w > 0; w--) {
  186. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  187. s2[0] + s2[1] + s2[2] + s2[3] +
  188. s3[0] + s3[1] + s3[2] + s3[3] +
  189. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  190. s1 += 4;
  191. s2 += 4;
  192. s3 += 4;
  193. s4 += 4;
  194. d++;
  195. }
  196. src += 4 * src_wrap;
  197. dst += dst_wrap;
  198. }
  199. }
  200. /* 8x8 -> 1x1 */
  201. static void shrink88(uint8_t *dst, int dst_wrap,
  202. const uint8_t *src, int src_wrap,
  203. int width, int height)
  204. {
  205. int w, i;
  206. for (; height > 0; height--) {
  207. for(w = width;w > 0; w--) {
  208. int tmp = 0;
  209. for (i = 0; i < 8; i++) {
  210. tmp += src[0] + src[1] + src[2] + src[3] +
  211. src[4] + src[5] + src[6] + src[7];
  212. src += src_wrap;
  213. }
  214. *(dst++) = (tmp + 32) >> 6;
  215. src += 8 - 8 * src_wrap;
  216. }
  217. src += 8 * src_wrap - 8 * width;
  218. dst += dst_wrap - width;
  219. }
  220. }
  221. av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c,
  222. AVCodecContext *avctx)
  223. {
  224. c->try_8x8basis = try_8x8basis_c;
  225. c->add_8x8basis = add_8x8basis_c;
  226. c->shrink[0] = av_image_copy_plane;
  227. c->shrink[1] = shrink22;
  228. c->shrink[2] = shrink44;
  229. c->shrink[3] = shrink88;
  230. c->pix_sum = pix_sum_c;
  231. c->pix_norm1 = pix_norm1_c;
  232. c->draw_edges = draw_edges_8_c;
  233. if (ARCH_ARM)
  234. ff_mpegvideoencdsp_init_arm(c, avctx);
  235. if (ARCH_PPC)
  236. ff_mpegvideoencdsp_init_ppc(c, avctx);
  237. if (ARCH_X86)
  238. ff_mpegvideoencdsp_init_x86(c, avctx);
  239. }