swscale_unscaled.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "config.h"
  19. #include "libswscale/swscale.h"
  20. #include "libswscale/swscale_internal.h"
  21. #include "libavutil/aarch64/cpu.h"
  22. #define YUV_TO_RGB_TABLE \
  23. c->yuv2rgb_v2r_coeff, \
  24. c->yuv2rgb_u2g_coeff, \
  25. c->yuv2rgb_v2g_coeff, \
  26. c->yuv2rgb_u2b_coeff, \
  27. #define DECLARE_FF_YUVX_TO_RGBX_FUNCS(ifmt, ofmt) \
  28. int ff_##ifmt##_to_##ofmt##_neon(int w, int h, \
  29. uint8_t *dst, int linesize, \
  30. const uint8_t *srcY, int linesizeY, \
  31. const uint8_t *srcU, int linesizeU, \
  32. const uint8_t *srcV, int linesizeV, \
  33. const int16_t *table, \
  34. int y_offset, \
  35. int y_coeff); \
  36. \
  37. static int ifmt##_to_##ofmt##_neon_wrapper(SwsInternal *c, const uint8_t *const src[], \
  38. const int srcStride[], int srcSliceY, \
  39. int srcSliceH, uint8_t *const dst[], \
  40. const int dstStride[]) { \
  41. const int16_t yuv2rgb_table[] = { YUV_TO_RGB_TABLE }; \
  42. \
  43. return ff_##ifmt##_to_##ofmt##_neon(c->opts.src_w, srcSliceH, \
  44. dst[0] + srcSliceY * dstStride[0], dstStride[0], \
  45. src[0], srcStride[0], \
  46. src[1], srcStride[1], \
  47. src[2], srcStride[2], \
  48. yuv2rgb_table, \
  49. c->yuv2rgb_y_offset >> 6, \
  50. c->yuv2rgb_y_coeff); \
  51. } \
  52. #define DECLARE_FF_YUVX_TO_GBRP_FUNCS(ifmt, ofmt) \
  53. int ff_##ifmt##_to_##ofmt##_neon(int w, int h, \
  54. uint8_t *dst, int linesize, \
  55. const uint8_t *srcY, int linesizeY, \
  56. const uint8_t *srcU, int linesizeU, \
  57. const uint8_t *srcV, int linesizeV, \
  58. const int16_t *table, \
  59. int y_offset, \
  60. int y_coeff, \
  61. uint8_t *dst1, int linesize1, \
  62. uint8_t *dst2, int linesize2); \
  63. \
  64. static int ifmt##_to_##ofmt##_neon_wrapper(SwsInternal *c, const uint8_t *const src[], \
  65. const int srcStride[], int srcSliceY, \
  66. int srcSliceH, uint8_t *const dst[], \
  67. const int dstStride[]) { \
  68. const int16_t yuv2rgb_table[] = { YUV_TO_RGB_TABLE }; \
  69. \
  70. return ff_##ifmt##_to_##ofmt##_neon(c->opts.src_w, srcSliceH, \
  71. dst[0] + srcSliceY * dstStride[0], dstStride[0], \
  72. src[0], srcStride[0], \
  73. src[1], srcStride[1], \
  74. src[2], srcStride[2], \
  75. yuv2rgb_table, \
  76. c->yuv2rgb_y_offset >> 6, \
  77. c->yuv2rgb_y_coeff, \
  78. dst[1] + srcSliceY * dstStride[1], dstStride[1], \
  79. dst[2] + srcSliceY * dstStride[2], dstStride[2]); \
  80. } \
  81. #define DECLARE_FF_YUVX_TO_ALL_RGBX_FUNCS(yuvx) \
  82. DECLARE_FF_YUVX_TO_RGBX_FUNCS(yuvx, argb) \
  83. DECLARE_FF_YUVX_TO_RGBX_FUNCS(yuvx, rgba) \
  84. DECLARE_FF_YUVX_TO_RGBX_FUNCS(yuvx, abgr) \
  85. DECLARE_FF_YUVX_TO_RGBX_FUNCS(yuvx, bgra) \
  86. DECLARE_FF_YUVX_TO_GBRP_FUNCS(yuvx, gbrp) \
  87. DECLARE_FF_YUVX_TO_ALL_RGBX_FUNCS(yuv420p)
  88. DECLARE_FF_YUVX_TO_ALL_RGBX_FUNCS(yuv422p)
  89. #define DECLARE_FF_NVX_TO_RGBX_FUNCS(ifmt, ofmt) \
  90. int ff_##ifmt##_to_##ofmt##_neon(int w, int h, \
  91. uint8_t *dst, int linesize, \
  92. const uint8_t *srcY, int linesizeY, \
  93. const uint8_t *srcC, int linesizeC, \
  94. const int16_t *table, \
  95. int y_offset, \
  96. int y_coeff); \
  97. \
  98. static int ifmt##_to_##ofmt##_neon_wrapper(SwsInternal *c, const uint8_t *const src[], \
  99. const int srcStride[], int srcSliceY, \
  100. int srcSliceH, uint8_t *const dst[], \
  101. const int dstStride[]) { \
  102. const int16_t yuv2rgb_table[] = { YUV_TO_RGB_TABLE }; \
  103. \
  104. return ff_##ifmt##_to_##ofmt##_neon(c->opts.src_w, srcSliceH, \
  105. dst[0] + srcSliceY * dstStride[0], dstStride[0], \
  106. src[0], srcStride[0], src[1], srcStride[1], \
  107. yuv2rgb_table, \
  108. c->yuv2rgb_y_offset >> 6, \
  109. c->yuv2rgb_y_coeff); \
  110. } \
  111. #define DECLARE_FF_NVX_TO_GBRP_FUNCS(ifmt, ofmt) \
  112. int ff_##ifmt##_to_##ofmt##_neon(int w, int h, \
  113. uint8_t *dst, int linesize, \
  114. const uint8_t *srcY, int linesizeY, \
  115. const uint8_t *srcC, int linesizeC, \
  116. const int16_t *table, \
  117. int y_offset, \
  118. int y_coeff, \
  119. uint8_t *dst1, int linesize1, \
  120. uint8_t *dst2, int linesize2); \
  121. \
  122. static int ifmt##_to_##ofmt##_neon_wrapper(SwsInternal *c, const uint8_t *const src[], \
  123. const int srcStride[], int srcSliceY, \
  124. int srcSliceH, uint8_t *const dst[], \
  125. const int dstStride[]) { \
  126. const int16_t yuv2rgb_table[] = { YUV_TO_RGB_TABLE }; \
  127. \
  128. return ff_##ifmt##_to_##ofmt##_neon(c->opts.src_w, srcSliceH, \
  129. dst[0] + srcSliceY * dstStride[0], dstStride[0], \
  130. src[0], srcStride[0], src[1], srcStride[1], \
  131. yuv2rgb_table, \
  132. c->yuv2rgb_y_offset >> 6, \
  133. c->yuv2rgb_y_coeff, \
  134. dst[1] + srcSliceY * dstStride[1], dstStride[1], \
  135. dst[2] + srcSliceY * dstStride[2], dstStride[2]); \
  136. } \
  137. void ff_nv24_to_yuv420p_chroma_neon(uint8_t *dst1, int dstStride1,
  138. uint8_t *dst2, int dstStride2,
  139. const uint8_t *src, int srcStride,
  140. int w, int h);
  141. static int nv24_to_yuv420p_neon_wrapper(SwsInternal *c, const uint8_t *const src[],
  142. const int srcStride[], int srcSliceY, int srcSliceH,
  143. uint8_t *const dst[], const int dstStride[])
  144. {
  145. uint8_t *dst1 = dst[1] + dstStride[1] * srcSliceY / 2;
  146. uint8_t *dst2 = dst[2] + dstStride[2] * srcSliceY / 2;
  147. ff_copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->opts.src_w,
  148. dst[0], dstStride[0]);
  149. if (c->opts.src_format == AV_PIX_FMT_NV24)
  150. ff_nv24_to_yuv420p_chroma_neon(dst1, dstStride[1], dst2, dstStride[2],
  151. src[1], srcStride[1], c->opts.src_w / 2,
  152. srcSliceH);
  153. else
  154. ff_nv24_to_yuv420p_chroma_neon(dst2, dstStride[2], dst1, dstStride[1],
  155. src[1], srcStride[1], c->opts.src_w / 2,
  156. srcSliceH);
  157. return srcSliceH;
  158. }
  159. #define DECLARE_FF_NVX_TO_ALL_RGBX_FUNCS(nvx) \
  160. DECLARE_FF_NVX_TO_RGBX_FUNCS(nvx, argb) \
  161. DECLARE_FF_NVX_TO_RGBX_FUNCS(nvx, rgba) \
  162. DECLARE_FF_NVX_TO_RGBX_FUNCS(nvx, abgr) \
  163. DECLARE_FF_NVX_TO_RGBX_FUNCS(nvx, bgra) \
  164. DECLARE_FF_NVX_TO_GBRP_FUNCS(nvx, gbrp) \
  165. DECLARE_FF_NVX_TO_ALL_RGBX_FUNCS(nv12)
  166. DECLARE_FF_NVX_TO_ALL_RGBX_FUNCS(nv21)
  167. /* We need a 16 pixel width alignment. This constraint can easily be removed
  168. * for input reading but for the output which is 4-bytes per pixel (RGBA) the
  169. * assembly might be writing as much as 4*15=60 extra bytes at the end of the
  170. * line, which won't fit the 32-bytes buffer alignment. */
  171. #define SET_FF_NVX_TO_RGBX_FUNC(ifmt, IFMT, ofmt, OFMT, accurate_rnd) do { \
  172. if (c->opts.src_format == AV_PIX_FMT_##IFMT \
  173. && c->opts.dst_format == AV_PIX_FMT_##OFMT \
  174. && !(c->opts.src_h & 1) \
  175. && !(c->opts.src_w & 15) \
  176. && !accurate_rnd) \
  177. c->convert_unscaled = ifmt##_to_##ofmt##_neon_wrapper; \
  178. } while (0)
  179. #define SET_FF_NVX_TO_ALL_RGBX_FUNC(nvx, NVX, accurate_rnd) do { \
  180. SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, argb, ARGB, accurate_rnd); \
  181. SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, rgba, RGBA, accurate_rnd); \
  182. SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, abgr, ABGR, accurate_rnd); \
  183. SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, bgra, BGRA, accurate_rnd); \
  184. SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, gbrp, GBRP, accurate_rnd); \
  185. } while (0)
  186. static void get_unscaled_swscale_neon(SwsInternal *c) {
  187. int accurate_rnd = c->opts.flags & SWS_ACCURATE_RND;
  188. SET_FF_NVX_TO_ALL_RGBX_FUNC(nv12, NV12, accurate_rnd);
  189. SET_FF_NVX_TO_ALL_RGBX_FUNC(nv21, NV21, accurate_rnd);
  190. SET_FF_NVX_TO_ALL_RGBX_FUNC(yuv420p, YUV420P, accurate_rnd);
  191. SET_FF_NVX_TO_ALL_RGBX_FUNC(yuv422p, YUV422P, accurate_rnd);
  192. if (c->opts.dst_format == AV_PIX_FMT_YUV420P &&
  193. (c->opts.src_format == AV_PIX_FMT_NV24 || c->opts.src_format == AV_PIX_FMT_NV42) &&
  194. !(c->opts.src_h & 1) && !(c->opts.src_w & 15) && !accurate_rnd)
  195. c->convert_unscaled = nv24_to_yuv420p_neon_wrapper;
  196. }
  197. void ff_get_unscaled_swscale_aarch64(SwsInternal *c)
  198. {
  199. int cpu_flags = av_get_cpu_flags();
  200. if (have_neon(cpu_flags))
  201. get_unscaled_swscale_neon(c);
  202. }