swscale_unscaled.c 15 KB

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