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.

180 lines
5.0KB

  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 "dsputil.h"
  27. #include "imgconvert.h"
  28. #include "mpegvideoencdsp.h"
  29. static int try_8x8basis_c(int16_t rem[64], int16_t weight[64],
  30. int16_t basis[64], int scale)
  31. {
  32. int i;
  33. unsigned int sum = 0;
  34. for (i = 0; i < 8 * 8; i++) {
  35. int b = rem[i] + ((basis[i] * scale +
  36. (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
  37. (BASIS_SHIFT - RECON_SHIFT));
  38. int w = weight[i];
  39. b >>= RECON_SHIFT;
  40. av_assert2(-512 < b && b < 512);
  41. sum += (w * b) * (w * b) >> 4;
  42. }
  43. return sum >> 2;
  44. }
  45. static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale)
  46. {
  47. int i;
  48. for (i = 0; i < 8 * 8; i++)
  49. rem[i] += (basis[i] * scale +
  50. (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
  51. (BASIS_SHIFT - RECON_SHIFT);
  52. }
  53. static int pix_sum_c(uint8_t *pix, int line_size)
  54. {
  55. int s = 0, i, j;
  56. for (i = 0; i < 16; i++) {
  57. for (j = 0; j < 16; j += 8) {
  58. s += pix[0];
  59. s += pix[1];
  60. s += pix[2];
  61. s += pix[3];
  62. s += pix[4];
  63. s += pix[5];
  64. s += pix[6];
  65. s += pix[7];
  66. pix += 8;
  67. }
  68. pix += line_size - 16;
  69. }
  70. return s;
  71. }
  72. static int pix_norm1_c(uint8_t *pix, int line_size)
  73. {
  74. int s = 0, i, j;
  75. uint32_t *sq = ff_square_tab + 256;
  76. for (i = 0; i < 16; i++) {
  77. for (j = 0; j < 16; j += 8) {
  78. #if 0
  79. s += sq[pix[0]];
  80. s += sq[pix[1]];
  81. s += sq[pix[2]];
  82. s += sq[pix[3]];
  83. s += sq[pix[4]];
  84. s += sq[pix[5]];
  85. s += sq[pix[6]];
  86. s += sq[pix[7]];
  87. #else
  88. #if HAVE_FAST_64BIT
  89. register uint64_t x = *(uint64_t *) pix;
  90. s += sq[x & 0xff];
  91. s += sq[(x >> 8) & 0xff];
  92. s += sq[(x >> 16) & 0xff];
  93. s += sq[(x >> 24) & 0xff];
  94. s += sq[(x >> 32) & 0xff];
  95. s += sq[(x >> 40) & 0xff];
  96. s += sq[(x >> 48) & 0xff];
  97. s += sq[(x >> 56) & 0xff];
  98. #else
  99. register uint32_t x = *(uint32_t *) pix;
  100. s += sq[x & 0xff];
  101. s += sq[(x >> 8) & 0xff];
  102. s += sq[(x >> 16) & 0xff];
  103. s += sq[(x >> 24) & 0xff];
  104. x = *(uint32_t *) (pix + 4);
  105. s += sq[x & 0xff];
  106. s += sq[(x >> 8) & 0xff];
  107. s += sq[(x >> 16) & 0xff];
  108. s += sq[(x >> 24) & 0xff];
  109. #endif
  110. #endif
  111. pix += 8;
  112. }
  113. pix += line_size - 16;
  114. }
  115. return s;
  116. }
  117. /* draw the edges of width 'w' of an image of size width, height */
  118. // FIXME: Check that this is OK for MPEG-4 interlaced.
  119. static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
  120. int w, int h, int sides)
  121. {
  122. uint8_t *ptr = buf, *last_line;
  123. int i;
  124. /* left and right */
  125. for (i = 0; i < height; i++) {
  126. memset(ptr - w, ptr[0], w);
  127. memset(ptr + width, ptr[width - 1], w);
  128. ptr += wrap;
  129. }
  130. /* top and bottom + corners */
  131. buf -= w;
  132. last_line = buf + (height - 1) * wrap;
  133. if (sides & EDGE_TOP)
  134. for (i = 0; i < h; i++)
  135. // top
  136. memcpy(buf - (i + 1) * wrap, buf, width + w + w);
  137. if (sides & EDGE_BOTTOM)
  138. for (i = 0; i < h; i++)
  139. // bottom
  140. memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
  141. }
  142. av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c,
  143. AVCodecContext *avctx)
  144. {
  145. c->try_8x8basis = try_8x8basis_c;
  146. c->add_8x8basis = add_8x8basis_c;
  147. c->shrink[0] = av_image_copy_plane;
  148. c->shrink[1] = ff_shrink22;
  149. c->shrink[2] = ff_shrink44;
  150. c->shrink[3] = ff_shrink88;
  151. c->pix_sum = pix_sum_c;
  152. c->pix_norm1 = pix_norm1_c;
  153. c->draw_edges = draw_edges_8_c;
  154. if (ARCH_ARM)
  155. ff_mpegvideoencdsp_init_arm(c, avctx);
  156. if (ARCH_PPC)
  157. ff_mpegvideoencdsp_init_ppc(c, avctx);
  158. if (ARCH_X86)
  159. ff_mpegvideoencdsp_init_x86(c, avctx);
  160. }