lossless_videodsp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Lossless video DSP utils
  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 "lossless_videodsp.h"
  22. #include "libavcodec/mathops.h"
  23. static void add_int16_c(uint16_t *dst, const uint16_t *src, unsigned mask, int w){
  24. long i;
  25. unsigned long pw_lsb = (mask >> 1) * 0x0001000100010001ULL;
  26. unsigned long pw_msb = pw_lsb + 0x0001000100010001ULL;
  27. for (i = 0; i <= w - (int)sizeof(long)/2; i += sizeof(long)/2) {
  28. long a = *(long*)(src+i);
  29. long b = *(long*)(dst+i);
  30. *(long*)(dst+i) = ((a&pw_lsb) + (b&pw_lsb)) ^ ((a^b)&pw_msb);
  31. }
  32. for(; i<w; i++)
  33. dst[i] = (dst[i] + src[i]) & mask;
  34. }
  35. static void diff_int16_c(uint16_t *dst, const uint16_t *src1, const uint16_t *src2, unsigned mask, int w){
  36. long i;
  37. #if !HAVE_FAST_UNALIGNED
  38. if((long)src2 & (sizeof(long)-1)){
  39. for(i=0; i+3<w; i+=4){
  40. dst[i+0] = (src1[i+0]-src2[i+0]) & mask;
  41. dst[i+1] = (src1[i+1]-src2[i+1]) & mask;
  42. dst[i+2] = (src1[i+2]-src2[i+2]) & mask;
  43. dst[i+3] = (src1[i+3]-src2[i+3]) & mask;
  44. }
  45. }else
  46. #endif
  47. {
  48. unsigned long pw_lsb = (mask >> 1) * 0x0001000100010001ULL;
  49. unsigned long pw_msb = pw_lsb + 0x0001000100010001ULL;
  50. for (i = 0; i <= w - (int)sizeof(long)/2; i += sizeof(long)/2) {
  51. long a = *(long*)(src1+i);
  52. long b = *(long*)(src2+i);
  53. *(long*)(dst+i) = ((a|pw_msb) - (b&pw_lsb)) ^ ((a^b^pw_msb)&pw_msb);
  54. }
  55. }
  56. for (; i<w; i++)
  57. dst[i] = (src1[i] - src2[i]) & mask;
  58. }
  59. static void add_hfyu_median_pred_int16_c(uint16_t *dst, const uint16_t *src, const uint16_t *diff, unsigned mask, int w, int *left, int *left_top){
  60. int i;
  61. uint16_t l, lt;
  62. l = *left;
  63. lt = *left_top;
  64. for(i=0; i<w; i++){
  65. l = (mid_pred(l, src[i], (l + src[i] - lt) & mask) + diff[i]) & mask;
  66. lt = src[i];
  67. dst[i] = l;
  68. }
  69. *left = l;
  70. *left_top = lt;
  71. }
  72. static void sub_hfyu_median_pred_int16_c(uint16_t *dst, const uint16_t *src1, const uint16_t *src2, unsigned mask, int w, int *left, int *left_top){
  73. int i;
  74. uint16_t l, lt;
  75. l = *left;
  76. lt = *left_top;
  77. for(i=0; i<w; i++){
  78. const int pred = mid_pred(l, src1[i], (l + src1[i] - lt) & mask);
  79. lt = src1[i];
  80. l = src2[i];
  81. dst[i] = (l - pred) & mask;
  82. }
  83. *left = l;
  84. *left_top = lt;
  85. }
  86. static int add_hfyu_left_pred_int16_c(uint16_t *dst, const uint16_t *src, unsigned mask, int w, unsigned acc){
  87. int i;
  88. for(i=0; i<w-1; i++){
  89. acc+= src[i];
  90. dst[i]= acc & mask;
  91. i++;
  92. acc+= src[i];
  93. dst[i]= acc & mask;
  94. }
  95. for(; i<w; i++){
  96. acc+= src[i];
  97. dst[i]= acc & mask;
  98. }
  99. return acc;
  100. }
  101. void ff_llviddsp_init(LLVidDSPContext *c, AVCodecContext *avctx)
  102. {
  103. c->add_int16 = add_int16_c;
  104. c->diff_int16= diff_int16_c;
  105. c->add_hfyu_left_pred_int16 = add_hfyu_left_pred_int16_c;
  106. c->add_hfyu_median_pred_int16 = add_hfyu_median_pred_int16_c;
  107. c->sub_hfyu_median_pred_int16 = sub_hfyu_median_pred_int16_c;
  108. if (ARCH_X86)
  109. ff_llviddsp_init_x86(c, avctx);
  110. }