mpegvideoencdsp.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/avassert.h"
  23. #include "libavutil/attributes.h"
  24. #include "libavutil/imgutils.h"
  25. #include "avcodec.h"
  26. #include "me_cmp.h"
  27. #include "mpegvideoencdsp.h"
  28. static int try_8x8basis_c(int16_t rem[64], int16_t weight[64],
  29. int16_t basis[64], int scale)
  30. {
  31. int i;
  32. unsigned int sum = 0;
  33. for (i = 0; i < 8 * 8; i++) {
  34. int b = rem[i] + ((basis[i] * scale +
  35. (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
  36. (BASIS_SHIFT - RECON_SHIFT));
  37. int w = weight[i];
  38. b >>= RECON_SHIFT;
  39. av_assert2(-512 < b && b < 512);
  40. sum += (w * b) * (w * b) >> 4;
  41. }
  42. return sum >> 2;
  43. }
  44. static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale)
  45. {
  46. int i;
  47. for (i = 0; i < 8 * 8; i++)
  48. rem[i] += (basis[i] * scale +
  49. (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
  50. (BASIS_SHIFT - RECON_SHIFT);
  51. }
  52. static int pix_sum_c(uint8_t *pix, int line_size)
  53. {
  54. int s = 0, i, j;
  55. for (i = 0; i < 16; i++) {
  56. for (j = 0; j < 16; j += 8) {
  57. s += pix[0];
  58. s += pix[1];
  59. s += pix[2];
  60. s += pix[3];
  61. s += pix[4];
  62. s += pix[5];
  63. s += pix[6];
  64. s += pix[7];
  65. pix += 8;
  66. }
  67. pix += line_size - 16;
  68. }
  69. return s;
  70. }
  71. static int pix_norm1_c(uint8_t *pix, int line_size)
  72. {
  73. int s = 0, i, j;
  74. const uint32_t *sq = ff_square_tab + 256;
  75. for (i = 0; i < 16; i++) {
  76. for (j = 0; j < 16; j += 8) {
  77. #if HAVE_FAST_64BIT
  78. register uint64_t x = *(uint64_t *) pix;
  79. s += sq[x & 0xff];
  80. s += sq[(x >> 8) & 0xff];
  81. s += sq[(x >> 16) & 0xff];
  82. s += sq[(x >> 24) & 0xff];
  83. s += sq[(x >> 32) & 0xff];
  84. s += sq[(x >> 40) & 0xff];
  85. s += sq[(x >> 48) & 0xff];
  86. s += sq[(x >> 56) & 0xff];
  87. #else
  88. register uint32_t x = *(uint32_t *) pix;
  89. s += sq[x & 0xff];
  90. s += sq[(x >> 8) & 0xff];
  91. s += sq[(x >> 16) & 0xff];
  92. s += sq[(x >> 24) & 0xff];
  93. x = *(uint32_t *) (pix + 4);
  94. s += sq[x & 0xff];
  95. s += sq[(x >> 8) & 0xff];
  96. s += sq[(x >> 16) & 0xff];
  97. s += sq[(x >> 24) & 0xff];
  98. #endif
  99. pix += 8;
  100. }
  101. pix += line_size - 16;
  102. }
  103. return s;
  104. }
  105. /* draw the edges of width 'w' of an image of size width, height */
  106. // FIXME: Check that this is OK for MPEG-4 interlaced.
  107. static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
  108. int w, int h, int sides)
  109. {
  110. uint8_t *ptr = buf, *last_line;
  111. int i;
  112. /* left and right */
  113. for (i = 0; i < height; i++) {
  114. memset(ptr - w, ptr[0], w);
  115. memset(ptr + width, ptr[width - 1], w);
  116. ptr += wrap;
  117. }
  118. /* top and bottom + corners */
  119. buf -= w;
  120. last_line = buf + (height - 1) * wrap;
  121. if (sides & EDGE_TOP)
  122. for (i = 0; i < h; i++)
  123. // top
  124. memcpy(buf - (i + 1) * wrap, buf, width + w + w);
  125. if (sides & EDGE_BOTTOM)
  126. for (i = 0; i < h; i++)
  127. // bottom
  128. memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
  129. }
  130. /* 2x2 -> 1x1 */
  131. static void shrink22(uint8_t *dst, int dst_wrap,
  132. const uint8_t *src, int src_wrap,
  133. int width, int height)
  134. {
  135. int w;
  136. const uint8_t *s1, *s2;
  137. uint8_t *d;
  138. for (; height > 0; height--) {
  139. s1 = src;
  140. s2 = s1 + src_wrap;
  141. d = dst;
  142. for (w = width; w >= 4; w -= 4) {
  143. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  144. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  145. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  146. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  147. s1 += 8;
  148. s2 += 8;
  149. d += 4;
  150. }
  151. for (; w > 0; w--) {
  152. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  153. s1 += 2;
  154. s2 += 2;
  155. d++;
  156. }
  157. src += 2 * src_wrap;
  158. dst += dst_wrap;
  159. }
  160. }
  161. /* 4x4 -> 1x1 */
  162. static void shrink44(uint8_t *dst, int dst_wrap,
  163. const uint8_t *src, int src_wrap,
  164. int width, int height)
  165. {
  166. int w;
  167. const uint8_t *s1, *s2, *s3, *s4;
  168. uint8_t *d;
  169. for (; height > 0; height--) {
  170. s1 = src;
  171. s2 = s1 + src_wrap;
  172. s3 = s2 + src_wrap;
  173. s4 = s3 + src_wrap;
  174. d = dst;
  175. for (w = width; w > 0; w--) {
  176. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  177. s2[0] + s2[1] + s2[2] + s2[3] +
  178. s3[0] + s3[1] + s3[2] + s3[3] +
  179. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  180. s1 += 4;
  181. s2 += 4;
  182. s3 += 4;
  183. s4 += 4;
  184. d++;
  185. }
  186. src += 4 * src_wrap;
  187. dst += dst_wrap;
  188. }
  189. }
  190. /* 8x8 -> 1x1 */
  191. static void shrink88(uint8_t *dst, int dst_wrap,
  192. const uint8_t *src, int src_wrap,
  193. int width, int height)
  194. {
  195. int w, i;
  196. for (; height > 0; height--) {
  197. for(w = width;w > 0; w--) {
  198. int tmp = 0;
  199. for (i = 0; i < 8; i++) {
  200. tmp += src[0] + src[1] + src[2] + src[3] +
  201. src[4] + src[5] + src[6] + src[7];
  202. src += src_wrap;
  203. }
  204. *(dst++) = (tmp + 32) >> 6;
  205. src += 8 - 8 * src_wrap;
  206. }
  207. src += 8 * src_wrap - 8 * width;
  208. dst += dst_wrap - width;
  209. }
  210. }
  211. av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c,
  212. AVCodecContext *avctx)
  213. {
  214. c->try_8x8basis = try_8x8basis_c;
  215. c->add_8x8basis = add_8x8basis_c;
  216. c->shrink[0] = av_image_copy_plane;
  217. c->shrink[1] = shrink22;
  218. c->shrink[2] = shrink44;
  219. c->shrink[3] = shrink88;
  220. c->pix_sum = pix_sum_c;
  221. c->pix_norm1 = pix_norm1_c;
  222. c->draw_edges = draw_edges_8_c;
  223. if (ARCH_ARM)
  224. ff_mpegvideoencdsp_init_arm(c, avctx);
  225. if (ARCH_PPC)
  226. ff_mpegvideoencdsp_init_ppc(c, avctx);
  227. if (ARCH_X86)
  228. ff_mpegvideoencdsp_init_x86(c, avctx);
  229. if (ARCH_MIPS)
  230. ff_mpegvideoencdsp_init_mips(c, avctx);
  231. }