rectangle.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * rectangle filling function
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * useful rectangle filling function
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #ifndef AVCODEC_RECTANGLE_H
  27. #define AVCODEC_RECTANGLE_H
  28. #include "config.h"
  29. #include "libavutil/common.h"
  30. #include "libavutil/avassert.h"
  31. /**
  32. * fill a rectangle.
  33. * @param h height of the rectangle, should be a constant
  34. * @param w width of the rectangle, should be a constant
  35. * @param size the size of val (1, 2 or 4), should be a constant
  36. */
  37. static av_always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  38. uint8_t *p= (uint8_t*)vp;
  39. av_assert2(size==1 || size==2 || size==4);
  40. av_assert2(w<=4);
  41. w *= size;
  42. stride *= size;
  43. av_assert2((((long)vp)&(FFMIN(w, 8<<(HAVE_NEON|ARCH_PPC|HAVE_MMX))-1)) == 0);
  44. av_assert2((stride&(w-1))==0);
  45. if(w==2){
  46. const uint16_t v= size==4 ? val : val*0x0101;
  47. *(uint16_t*)(p + 0*stride)= v;
  48. if(h==1) return;
  49. *(uint16_t*)(p + 1*stride)= v;
  50. if(h==2) return;
  51. *(uint16_t*)(p + 2*stride)= v;
  52. *(uint16_t*)(p + 3*stride)= v;
  53. }else if(w==4){
  54. const uint32_t v= size==4 ? val : size==2 ? val*0x00010001 : val*0x01010101;
  55. *(uint32_t*)(p + 0*stride)= v;
  56. if(h==1) return;
  57. *(uint32_t*)(p + 1*stride)= v;
  58. if(h==2) return;
  59. *(uint32_t*)(p + 2*stride)= v;
  60. *(uint32_t*)(p + 3*stride)= v;
  61. }else if(w==8){
  62. // gcc cannot optimize 64-bit math on x86_32
  63. #if HAVE_FAST_64BIT
  64. const uint64_t v= size==2 ? val*0x0001000100010001ULL : val*0x0100000001ULL;
  65. *(uint64_t*)(p + 0*stride)= v;
  66. if(h==1) return;
  67. *(uint64_t*)(p + 1*stride)= v;
  68. if(h==2) return;
  69. *(uint64_t*)(p + 2*stride)= v;
  70. *(uint64_t*)(p + 3*stride)= v;
  71. }else if(w==16){
  72. const uint64_t v= val*0x0100000001ULL;
  73. *(uint64_t*)(p + 0+0*stride)= v;
  74. *(uint64_t*)(p + 8+0*stride)= v;
  75. *(uint64_t*)(p + 0+1*stride)= v;
  76. *(uint64_t*)(p + 8+1*stride)= v;
  77. if(h==2) return;
  78. *(uint64_t*)(p + 0+2*stride)= v;
  79. *(uint64_t*)(p + 8+2*stride)= v;
  80. *(uint64_t*)(p + 0+3*stride)= v;
  81. *(uint64_t*)(p + 8+3*stride)= v;
  82. #else
  83. const uint32_t v= size==2 ? val*0x00010001 : val;
  84. *(uint32_t*)(p + 0+0*stride)= v;
  85. *(uint32_t*)(p + 4+0*stride)= v;
  86. if(h==1) return;
  87. *(uint32_t*)(p + 0+1*stride)= v;
  88. *(uint32_t*)(p + 4+1*stride)= v;
  89. if(h==2) return;
  90. *(uint32_t*)(p + 0+2*stride)= v;
  91. *(uint32_t*)(p + 4+2*stride)= v;
  92. *(uint32_t*)(p + 0+3*stride)= v;
  93. *(uint32_t*)(p + 4+3*stride)= v;
  94. }else if(w==16){
  95. *(uint32_t*)(p + 0+0*stride)= val;
  96. *(uint32_t*)(p + 4+0*stride)= val;
  97. *(uint32_t*)(p + 8+0*stride)= val;
  98. *(uint32_t*)(p +12+0*stride)= val;
  99. *(uint32_t*)(p + 0+1*stride)= val;
  100. *(uint32_t*)(p + 4+1*stride)= val;
  101. *(uint32_t*)(p + 8+1*stride)= val;
  102. *(uint32_t*)(p +12+1*stride)= val;
  103. if(h==2) return;
  104. *(uint32_t*)(p + 0+2*stride)= val;
  105. *(uint32_t*)(p + 4+2*stride)= val;
  106. *(uint32_t*)(p + 8+2*stride)= val;
  107. *(uint32_t*)(p +12+2*stride)= val;
  108. *(uint32_t*)(p + 0+3*stride)= val;
  109. *(uint32_t*)(p + 4+3*stride)= val;
  110. *(uint32_t*)(p + 8+3*stride)= val;
  111. *(uint32_t*)(p +12+3*stride)= val;
  112. #endif
  113. }else
  114. av_assert2(0);
  115. av_assert2(h==4);
  116. }
  117. #endif /* AVCODEC_RECTANGLE_H */