h263dsp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <stdint.h>
  19. #include "libavutil/attributes.h"
  20. #include "libavutil/common.h"
  21. #include "config.h"
  22. #include "h263dsp.h"
  23. const uint8_t ff_h263_loop_filter_strength[32] = {
  24. 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 7,
  25. 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12
  26. };
  27. static void h263_h_loop_filter_c(uint8_t *src, int stride, int qscale)
  28. {
  29. int y;
  30. const int strength = ff_h263_loop_filter_strength[qscale];
  31. for (y = 0; y < 8; y++) {
  32. int d1, d2, ad1;
  33. int p0 = src[y * stride - 2];
  34. int p1 = src[y * stride - 1];
  35. int p2 = src[y * stride + 0];
  36. int p3 = src[y * stride + 1];
  37. int d = (p0 - p3 + 4 * (p2 - p1)) / 8;
  38. if (d < -2 * strength)
  39. d1 = 0;
  40. else if (d < -strength)
  41. d1 = -2 * strength - d;
  42. else if (d < strength)
  43. d1 = d;
  44. else if (d < 2 * strength)
  45. d1 = 2 * strength - d;
  46. else
  47. d1 = 0;
  48. p1 += d1;
  49. p2 -= d1;
  50. if (p1 & 256)
  51. p1 = ~(p1 >> 31);
  52. if (p2 & 256)
  53. p2 = ~(p2 >> 31);
  54. src[y * stride - 1] = p1;
  55. src[y * stride + 0] = p2;
  56. ad1 = FFABS(d1) >> 1;
  57. d2 = av_clip((p0 - p3) / 4, -ad1, ad1);
  58. src[y * stride - 2] = p0 - d2;
  59. src[y * stride + 1] = p3 + d2;
  60. }
  61. }
  62. static void h263_v_loop_filter_c(uint8_t *src, int stride, int qscale)
  63. {
  64. int x;
  65. const int strength = ff_h263_loop_filter_strength[qscale];
  66. for (x = 0; x < 8; x++) {
  67. int d1, d2, ad1;
  68. int p0 = src[x - 2 * stride];
  69. int p1 = src[x - 1 * stride];
  70. int p2 = src[x + 0 * stride];
  71. int p3 = src[x + 1 * stride];
  72. int d = (p0 - p3 + 4 * (p2 - p1)) / 8;
  73. if (d < -2 * strength)
  74. d1 = 0;
  75. else if (d < -strength)
  76. d1 = -2 * strength - d;
  77. else if (d < strength)
  78. d1 = d;
  79. else if (d < 2 * strength)
  80. d1 = 2 * strength - d;
  81. else
  82. d1 = 0;
  83. p1 += d1;
  84. p2 -= d1;
  85. if (p1 & 256)
  86. p1 = ~(p1 >> 31);
  87. if (p2 & 256)
  88. p2 = ~(p2 >> 31);
  89. src[x - 1 * stride] = p1;
  90. src[x + 0 * stride] = p2;
  91. ad1 = FFABS(d1) >> 1;
  92. d2 = av_clip((p0 - p3) / 4, -ad1, ad1);
  93. src[x - 2 * stride] = p0 - d2;
  94. src[x + stride] = p3 + d2;
  95. }
  96. }
  97. av_cold void ff_h263dsp_init(H263DSPContext *ctx)
  98. {
  99. ctx->h263_h_loop_filter = h263_h_loop_filter_c;
  100. ctx->h263_v_loop_filter = h263_v_loop_filter_c;
  101. if (ARCH_X86)
  102. ff_h263dsp_init_x86(ctx);
  103. if (ARCH_MIPS)
  104. ff_h263dsp_init_mips(ctx);
  105. }