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.

251 lines
13KB

  1. /*
  2. * Copyright (C) 2009 David Conrad
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avcodec.h"
  21. #include "diracdsp.h"
  22. #define FILTER(src, stride) \
  23. ((21*((src)[ 0*stride] + (src)[1*stride]) \
  24. -7*((src)[-1*stride] + (src)[2*stride]) \
  25. +3*((src)[-2*stride] + (src)[3*stride]) \
  26. -1*((src)[-3*stride] + (src)[4*stride]) + 16) >> 5)
  27. static void dirac_hpel_filter(uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, const uint8_t *src,
  28. int stride, int width, int height)
  29. {
  30. int x, y;
  31. for (y = 0; y < height; y++) {
  32. for (x = -3; x < width+5; x++)
  33. dstv[x] = av_clip_uint8(FILTER(src+x, stride));
  34. for (x = 0; x < width; x++)
  35. dstc[x] = av_clip_uint8(FILTER(dstv+x, 1));
  36. for (x = 0; x < width; x++)
  37. dsth[x] = av_clip_uint8(FILTER(src+x, 1));
  38. src += stride;
  39. dsth += stride;
  40. dstv += stride;
  41. dstc += stride;
  42. }
  43. }
  44. #define PIXOP_BILINEAR(PFX, OP, WIDTH) \
  45. static void ff_ ## PFX ## _dirac_pixels ## WIDTH ## _bilinear_c(uint8_t *dst, const uint8_t *src[5], int stride, int h) \
  46. { \
  47. int x; \
  48. const uint8_t *s0 = src[0]; \
  49. const uint8_t *s1 = src[1]; \
  50. const uint8_t *s2 = src[2]; \
  51. const uint8_t *s3 = src[3]; \
  52. const uint8_t *w = src[4]; \
  53. \
  54. while (h--) { \
  55. for (x = 0; x < WIDTH; x++) { \
  56. OP(dst[x], (s0[x]*w[0] + s1[x]*w[1] + s2[x]*w[2] + s3[x]*w[3] + 8) >> 4); \
  57. } \
  58. \
  59. dst += stride; \
  60. s0 += stride; \
  61. s1 += stride; \
  62. s2 += stride; \
  63. s3 += stride; \
  64. } \
  65. }
  66. #define OP_PUT(dst, val) (dst) = (val)
  67. #define OP_AVG(dst, val) (dst) = (((dst) + (val) + 1)>>1)
  68. PIXOP_BILINEAR(put, OP_PUT, 8)
  69. PIXOP_BILINEAR(put, OP_PUT, 16)
  70. PIXOP_BILINEAR(put, OP_PUT, 32)
  71. PIXOP_BILINEAR(avg, OP_AVG, 8)
  72. PIXOP_BILINEAR(avg, OP_AVG, 16)
  73. PIXOP_BILINEAR(avg, OP_AVG, 32)
  74. #define op_scale1(x) block[x] = av_clip_uint8( (block[x]*weight + (1<<(log2_denom-1))) >> log2_denom)
  75. #define op_scale2(x) dst[x] = av_clip_uint8( (src[x]*weights + dst[x]*weightd + (1<<(log2_denom-1))) >> log2_denom)
  76. #define DIRAC_WEIGHT(W) \
  77. static void weight_dirac_pixels ## W ## _c(uint8_t *block, int stride, int log2_denom, \
  78. int weight, int h) { \
  79. int x; \
  80. while (h--) { \
  81. for (x = 0; x < W; x++) { \
  82. op_scale1(x); \
  83. op_scale1(x+1); \
  84. } \
  85. block += stride; \
  86. } \
  87. } \
  88. static void biweight_dirac_pixels ## W ## _c(uint8_t *dst, const uint8_t *src, int stride, int log2_denom, \
  89. int weightd, int weights, int h) { \
  90. int x; \
  91. while (h--) { \
  92. for (x = 0; x < W; x++) { \
  93. op_scale2(x); \
  94. op_scale2(x+1); \
  95. } \
  96. dst += stride; \
  97. src += stride; \
  98. } \
  99. }
  100. DIRAC_WEIGHT(8)
  101. DIRAC_WEIGHT(16)
  102. DIRAC_WEIGHT(32)
  103. #define ADD_OBMC(xblen) \
  104. static void add_obmc ## xblen ## _c(uint16_t *dst, const uint8_t *src, int stride, \
  105. const uint8_t *obmc_weight, int yblen) \
  106. { \
  107. int x; \
  108. while (yblen--) { \
  109. for (x = 0; x < xblen; x += 2) { \
  110. dst[x ] += src[x ] * obmc_weight[x ]; \
  111. dst[x+1] += src[x+1] * obmc_weight[x+1]; \
  112. } \
  113. dst += stride; \
  114. src += stride; \
  115. obmc_weight += 32; \
  116. } \
  117. }
  118. ADD_OBMC(8)
  119. ADD_OBMC(16)
  120. ADD_OBMC(32)
  121. static void put_signed_rect_clamped_8bit_c(uint8_t *dst, int dst_stride, const uint8_t *_src, int src_stride, int width, int height)
  122. {
  123. int x, y;
  124. int16_t *src = (int16_t *)_src;
  125. for (y = 0; y < height; y++) {
  126. for (x = 0; x < width; x+=4) {
  127. dst[x ] = av_clip_uint8(src[x ] + 128);
  128. dst[x+1] = av_clip_uint8(src[x+1] + 128);
  129. dst[x+2] = av_clip_uint8(src[x+2] + 128);
  130. dst[x+3] = av_clip_uint8(src[x+3] + 128);
  131. }
  132. dst += dst_stride;
  133. src += src_stride >> 1;
  134. }
  135. }
  136. #define PUT_SIGNED_RECT_CLAMPED(PX) \
  137. static void put_signed_rect_clamped_ ## PX ## bit_c(uint8_t *_dst, int dst_stride, const uint8_t *_src, \
  138. int src_stride, int width, int height) \
  139. { \
  140. int x, y; \
  141. uint16_t *dst = (uint16_t *)_dst; \
  142. int32_t *src = (int32_t *)_src; \
  143. for (y = 0; y < height; y++) { \
  144. for (x = 0; x < width; x+=4) { \
  145. dst[x ] = av_clip_uintp2(src[x ] + (1U << (PX - 1)), PX); \
  146. dst[x+1] = av_clip_uintp2(src[x+1] + (1U << (PX - 1)), PX); \
  147. dst[x+2] = av_clip_uintp2(src[x+2] + (1U << (PX - 1)), PX); \
  148. dst[x+3] = av_clip_uintp2(src[x+3] + (1U << (PX - 1)), PX); \
  149. } \
  150. dst += dst_stride >> 1; \
  151. src += src_stride >> 2; \
  152. } \
  153. }
  154. PUT_SIGNED_RECT_CLAMPED(10)
  155. PUT_SIGNED_RECT_CLAMPED(12)
  156. static void add_rect_clamped_c(uint8_t *dst, const uint16_t *src, int stride,
  157. const int16_t *idwt, int idwt_stride,
  158. int width, int height)
  159. {
  160. int x, y;
  161. for (y = 0; y < height; y++) {
  162. for (x = 0; x < width; x+=2) {
  163. dst[x ] = av_clip_uint8(((src[x ]+32)>>6) + idwt[x ]);
  164. dst[x+1] = av_clip_uint8(((src[x+1]+32)>>6) + idwt[x+1]);
  165. }
  166. dst += stride;
  167. src += stride;
  168. idwt += idwt_stride;
  169. }
  170. }
  171. #define DEQUANT_SUBBAND(PX) \
  172. static void dequant_subband_ ## PX ## _c(uint8_t *src, uint8_t *dst, ptrdiff_t stride, \
  173. const int qf, const int qs, int tot_v, int tot_h) \
  174. { \
  175. int i, y; \
  176. for (y = 0; y < tot_v; y++) { \
  177. PX c, sign, *src_r = (PX *)src, *dst_r = (PX *)dst; \
  178. for (i = 0; i < tot_h; i++) { \
  179. c = *src_r++; \
  180. if (c < 0) c = -((-(unsigned)c*qf + qs) >> 2); \
  181. else if(c > 0) c = (( (unsigned)c*qf + qs) >> 2); \
  182. *dst_r++ = c; \
  183. } \
  184. src += tot_h << (sizeof(PX) >> 1); \
  185. dst += stride; \
  186. } \
  187. }
  188. DEQUANT_SUBBAND(int16_t)
  189. DEQUANT_SUBBAND(int32_t)
  190. #define PIXFUNC(PFX, WIDTH) \
  191. c->PFX ## _dirac_pixels_tab[WIDTH>>4][0] = ff_ ## PFX ## _dirac_pixels ## WIDTH ## _c; \
  192. c->PFX ## _dirac_pixels_tab[WIDTH>>4][1] = ff_ ## PFX ## _dirac_pixels ## WIDTH ## _l2_c; \
  193. c->PFX ## _dirac_pixels_tab[WIDTH>>4][2] = ff_ ## PFX ## _dirac_pixels ## WIDTH ## _l4_c; \
  194. c->PFX ## _dirac_pixels_tab[WIDTH>>4][3] = ff_ ## PFX ## _dirac_pixels ## WIDTH ## _bilinear_c
  195. av_cold void ff_diracdsp_init(DiracDSPContext *c)
  196. {
  197. c->dirac_hpel_filter = dirac_hpel_filter;
  198. c->add_rect_clamped = add_rect_clamped_c;
  199. c->put_signed_rect_clamped[0] = put_signed_rect_clamped_8bit_c;
  200. c->put_signed_rect_clamped[1] = put_signed_rect_clamped_10bit_c;
  201. c->put_signed_rect_clamped[2] = put_signed_rect_clamped_12bit_c;
  202. c->add_dirac_obmc[0] = add_obmc8_c;
  203. c->add_dirac_obmc[1] = add_obmc16_c;
  204. c->add_dirac_obmc[2] = add_obmc32_c;
  205. c->weight_dirac_pixels_tab[0] = weight_dirac_pixels8_c;
  206. c->weight_dirac_pixels_tab[1] = weight_dirac_pixels16_c;
  207. c->weight_dirac_pixels_tab[2] = weight_dirac_pixels32_c;
  208. c->biweight_dirac_pixels_tab[0] = biweight_dirac_pixels8_c;
  209. c->biweight_dirac_pixels_tab[1] = biweight_dirac_pixels16_c;
  210. c->biweight_dirac_pixels_tab[2] = biweight_dirac_pixels32_c;
  211. c->dequant_subband[0] = c->dequant_subband[2] = dequant_subband_int16_t_c;
  212. c->dequant_subband[1] = c->dequant_subband[3] = dequant_subband_int32_t_c;
  213. PIXFUNC(put, 8);
  214. PIXFUNC(put, 16);
  215. PIXFUNC(put, 32);
  216. PIXFUNC(avg, 8);
  217. PIXFUNC(avg, 16);
  218. PIXFUNC(avg, 32);
  219. if (ARCH_X86)
  220. ff_diracdsp_init_x86(c);
  221. }