scale.asm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. ;******************************************************************************
  2. ;* x86-optimized horizontal line scaling functions
  3. ;* Copyright (c) 2011 Ronald S. Bultje <rsbultje@gmail.com>
  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. %include "libavutil/x86/x86util.asm"
  22. SECTION_RODATA
  23. max_19bit_int: times 4 dd 0x7ffff
  24. minshort: times 8 dw 0x8000
  25. unicoeff: times 4 dd 0x20000000
  26. SECTION .text
  27. ;-----------------------------------------------------------------------------
  28. ; horizontal line scaling
  29. ;
  30. ; void hscale<source_width>to<intermediate_nbits>_<filterSize>_<opt>
  31. ; (SwsContext *c, int{16,32}_t *dst,
  32. ; int dstW, const uint{8,16}_t *src,
  33. ; const int16_t *filter,
  34. ; const int32_t *filterPos, int filterSize);
  35. ;
  36. ; Scale one horizontal line. Input is either 8-bit width or 16-bit width
  37. ; ($source_width can be either 8, 9, 10 or 16, difference is whether we have to
  38. ; downscale before multiplying). Filter is 14 bits. Output is either 15 bits
  39. ; (in int16_t) or 19 bits (in int32_t), as given in $intermediate_nbits. Each
  40. ; output pixel is generated from $filterSize input pixels, the position of
  41. ; the first pixel is given in filterPos[nOutputPixel].
  42. ;-----------------------------------------------------------------------------
  43. ; SCALE_FUNC source_width, intermediate_nbits, filtersize, filtersuffix, n_args, n_xmm
  44. %macro SCALE_FUNC 6
  45. %ifnidn %3, X
  46. cglobal hscale%1to%2_%4, %5, 7, %6, pos0, dst, w, src, filter, fltpos, pos1
  47. %else
  48. cglobal hscale%1to%2_%4, %5, 10, %6, pos0, dst, w, srcmem, filter, fltpos, fltsize
  49. %endif
  50. %if ARCH_X86_64
  51. movsxd wq, wd
  52. %define mov32 movsxd
  53. %else ; x86-32
  54. %define mov32 mov
  55. %endif ; x86-64
  56. %if %2 == 19
  57. mova m2, [max_19bit_int]
  58. %endif ; %2 == 19
  59. %if %1 == 16
  60. mova m6, [minshort]
  61. mova m7, [unicoeff]
  62. %elif %1 == 8
  63. pxor m3, m3
  64. %endif ; %1 == 8/16
  65. %if %1 == 8
  66. %define movlh movd
  67. %define movbh movh
  68. %define srcmul 1
  69. %else ; %1 == 9-16
  70. %define movlh movq
  71. %define movbh movu
  72. %define srcmul 2
  73. %endif ; %1 == 8/9-16
  74. %ifnidn %3, X
  75. ; setup loop
  76. %if %3 == 8
  77. shl wq, 1 ; this allows *16 (i.e. now *8) in lea instructions for the 8-tap filter
  78. %define wshr 1
  79. %else ; %3 == 4
  80. %define wshr 0
  81. %endif ; %3 == 8
  82. lea filterq, [filterq+wq*8]
  83. %if %2 == 15
  84. lea dstq, [dstq+wq*(2>>wshr)]
  85. %else ; %2 == 19
  86. lea dstq, [dstq+wq*(4>>wshr)]
  87. %endif ; %2 == 15/19
  88. lea fltposq, [fltposq+wq*(4>>wshr)]
  89. neg wq
  90. .loop:
  91. %if %3 == 4 ; filterSize == 4 scaling
  92. ; load 2x4 or 4x4 source pixels into m0/m1
  93. mov32 pos0q, dword [fltposq+wq*4+ 0] ; filterPos[0]
  94. mov32 pos1q, dword [fltposq+wq*4+ 4] ; filterPos[1]
  95. movlh m0, [srcq+pos0q*srcmul] ; src[filterPos[0] + {0,1,2,3}]
  96. %if mmsize == 8
  97. movlh m1, [srcq+pos1q*srcmul] ; src[filterPos[1] + {0,1,2,3}]
  98. %else ; mmsize == 16
  99. %if %1 > 8
  100. movhps m0, [srcq+pos1q*srcmul] ; src[filterPos[1] + {0,1,2,3}]
  101. %else ; %1 == 8
  102. movd m4, [srcq+pos1q*srcmul] ; src[filterPos[1] + {0,1,2,3}]
  103. %endif
  104. mov32 pos0q, dword [fltposq+wq*4+ 8] ; filterPos[2]
  105. mov32 pos1q, dword [fltposq+wq*4+12] ; filterPos[3]
  106. movlh m1, [srcq+pos0q*srcmul] ; src[filterPos[2] + {0,1,2,3}]
  107. %if %1 > 8
  108. movhps m1, [srcq+pos1q*srcmul] ; src[filterPos[3] + {0,1,2,3}]
  109. %else ; %1 == 8
  110. movd m5, [srcq+pos1q*srcmul] ; src[filterPos[3] + {0,1,2,3}]
  111. punpckldq m0, m4
  112. punpckldq m1, m5
  113. %endif ; %1 == 8
  114. %endif ; mmsize == 8/16
  115. %if %1 == 8
  116. punpcklbw m0, m3 ; byte -> word
  117. punpcklbw m1, m3 ; byte -> word
  118. %endif ; %1 == 8
  119. ; multiply with filter coefficients
  120. %if %1 == 16 ; pmaddwd needs signed adds, so this moves unsigned -> signed, we'll
  121. ; add back 0x8000 * sum(coeffs) after the horizontal add
  122. psubw m0, m6
  123. psubw m1, m6
  124. %endif ; %1 == 16
  125. pmaddwd m0, [filterq+wq*8+mmsize*0] ; *= filter[{0,1,..,6,7}]
  126. pmaddwd m1, [filterq+wq*8+mmsize*1] ; *= filter[{8,9,..,14,15}]
  127. ; add up horizontally (4 srcpix * 4 coefficients -> 1 dstpix)
  128. %if notcpuflag(ssse3) ; sse2
  129. mova m4, m0
  130. shufps m0, m1, 10001000b
  131. shufps m4, m1, 11011101b
  132. paddd m0, m4
  133. %else ; ssse3/sse4
  134. phaddd m0, m1 ; filter[{ 0, 1, 2, 3}]*src[filterPos[0]+{0,1,2,3}],
  135. ; filter[{ 4, 5, 6, 7}]*src[filterPos[1]+{0,1,2,3}],
  136. ; filter[{ 8, 9,10,11}]*src[filterPos[2]+{0,1,2,3}],
  137. ; filter[{12,13,14,15}]*src[filterPos[3]+{0,1,2,3}]
  138. %endif ; sse2/ssse3/sse4
  139. %else ; %3 == 8, i.e. filterSize == 8 scaling
  140. ; load 2x8 or 4x8 source pixels into m0, m1, m4 and m5
  141. mov32 pos0q, dword [fltposq+wq*2+0] ; filterPos[0]
  142. mov32 pos1q, dword [fltposq+wq*2+4] ; filterPos[1]
  143. movbh m0, [srcq+ pos0q *srcmul] ; src[filterPos[0] + {0,1,2,3,4,5,6,7}]
  144. %if mmsize == 8
  145. movbh m1, [srcq+(pos0q+4)*srcmul] ; src[filterPos[0] + {4,5,6,7}]
  146. movbh m4, [srcq+ pos1q *srcmul] ; src[filterPos[1] + {0,1,2,3}]
  147. movbh m5, [srcq+(pos1q+4)*srcmul] ; src[filterPos[1] + {4,5,6,7}]
  148. %else ; mmsize == 16
  149. movbh m1, [srcq+ pos1q *srcmul] ; src[filterPos[1] + {0,1,2,3,4,5,6,7}]
  150. mov32 pos0q, dword [fltposq+wq*2+8] ; filterPos[2]
  151. mov32 pos1q, dword [fltposq+wq*2+12] ; filterPos[3]
  152. movbh m4, [srcq+ pos0q *srcmul] ; src[filterPos[2] + {0,1,2,3,4,5,6,7}]
  153. movbh m5, [srcq+ pos1q *srcmul] ; src[filterPos[3] + {0,1,2,3,4,5,6,7}]
  154. %endif ; mmsize == 8/16
  155. %if %1 == 8
  156. punpcklbw m0, m3 ; byte -> word
  157. punpcklbw m1, m3 ; byte -> word
  158. punpcklbw m4, m3 ; byte -> word
  159. punpcklbw m5, m3 ; byte -> word
  160. %endif ; %1 == 8
  161. ; multiply
  162. %if %1 == 16 ; pmaddwd needs signed adds, so this moves unsigned -> signed, we'll
  163. ; add back 0x8000 * sum(coeffs) after the horizontal add
  164. psubw m0, m6
  165. psubw m1, m6
  166. psubw m4, m6
  167. psubw m5, m6
  168. %endif ; %1 == 16
  169. pmaddwd m0, [filterq+wq*8+mmsize*0] ; *= filter[{0,1,..,6,7}]
  170. pmaddwd m1, [filterq+wq*8+mmsize*1] ; *= filter[{8,9,..,14,15}]
  171. pmaddwd m4, [filterq+wq*8+mmsize*2] ; *= filter[{16,17,..,22,23}]
  172. pmaddwd m5, [filterq+wq*8+mmsize*3] ; *= filter[{24,25,..,30,31}]
  173. ; add up horizontally (8 srcpix * 8 coefficients -> 1 dstpix)
  174. %if notcpuflag(ssse3) ; sse2
  175. %if %1 == 8
  176. %define mex m6
  177. %else
  178. %define mex m3
  179. %endif
  180. ; emulate horizontal add as transpose + vertical add
  181. mova mex, m0
  182. punpckldq m0, m1
  183. punpckhdq mex, m1
  184. paddd m0, mex
  185. mova m1, m4
  186. punpckldq m4, m5
  187. punpckhdq m1, m5
  188. paddd m4, m1
  189. mova m1, m0
  190. punpcklqdq m0, m4
  191. punpckhqdq m1, m4
  192. paddd m0, m1
  193. %else ; ssse3/sse4
  194. ; FIXME if we rearrange the filter in pairs of 4, we can
  195. ; load pixels likewise and use 2 x paddd + phaddd instead
  196. ; of 3 x phaddd here, faster on older cpus
  197. phaddd m0, m1
  198. phaddd m4, m5
  199. phaddd m0, m4 ; filter[{ 0, 1,..., 6, 7}]*src[filterPos[0]+{0,1,...,6,7}],
  200. ; filter[{ 8, 9,...,14,15}]*src[filterPos[1]+{0,1,...,6,7}],
  201. ; filter[{16,17,...,22,23}]*src[filterPos[2]+{0,1,...,6,7}],
  202. ; filter[{24,25,...,30,31}]*src[filterPos[3]+{0,1,...,6,7}]
  203. %endif ; sse2/ssse3/sse4
  204. %endif ; %3 == 4/8
  205. %else ; %3 == X, i.e. any filterSize scaling
  206. %ifidn %4, X4
  207. %define dlt 4
  208. %else ; %4 == X || %4 == X8
  209. %define dlt 0
  210. %endif ; %4 ==/!= X4
  211. %if ARCH_X86_64
  212. %define srcq r8
  213. %define pos1q r7
  214. %define srcendq r9
  215. movsxd fltsizeq, fltsized ; filterSize
  216. lea srcendq, [srcmemq+(fltsizeq-dlt)*srcmul] ; &src[filterSize&~4]
  217. %else ; x86-32
  218. %define srcq srcmemq
  219. %define pos1q dstq
  220. %define srcendq r6m
  221. lea pos0q, [srcmemq+(fltsizeq-dlt)*srcmul] ; &src[filterSize&~4]
  222. mov srcendq, pos0q
  223. %endif ; x86-32/64
  224. lea fltposq, [fltposq+wq*4]
  225. %if %2 == 15
  226. lea dstq, [dstq+wq*2]
  227. %else ; %2 == 19
  228. lea dstq, [dstq+wq*4]
  229. %endif ; %2 == 15/19
  230. movifnidn dstmp, dstq
  231. neg wq
  232. .loop:
  233. mov32 pos0q, dword [fltposq+wq*4+0] ; filterPos[0]
  234. mov32 pos1q, dword [fltposq+wq*4+4] ; filterPos[1]
  235. ; FIXME maybe do 4px/iteration on x86-64 (x86-32 wouldn't have enough regs)?
  236. pxor m4, m4
  237. pxor m5, m5
  238. mov srcq, srcmemmp
  239. .innerloop:
  240. ; load 2x8 (sse) source pixels into m0/m1 -> m4/m5
  241. movbh m0, [srcq+ pos0q *srcmul] ; src[filterPos[0] + {0,1,2,3(,4,5,6,7)}]
  242. movbh m1, [srcq+(pos1q+dlt)*srcmul] ; src[filterPos[1] + {0,1,2,3(,4,5,6,7)}]
  243. %if %1 == 8
  244. punpcklbw m0, m3
  245. punpcklbw m1, m3
  246. %endif ; %1 == 8
  247. ; multiply
  248. %if %1 == 16 ; pmaddwd needs signed adds, so this moves unsigned -> signed, we'll
  249. ; add back 0x8000 * sum(coeffs) after the horizontal add
  250. psubw m0, m6
  251. psubw m1, m6
  252. %endif ; %1 == 16
  253. pmaddwd m0, [filterq] ; filter[{0,1,2,3(,4,5,6,7)}]
  254. pmaddwd m1, [filterq+(fltsizeq+dlt)*2]; filter[filtersize+{0,1,2,3(,4,5,6,7)}]
  255. paddd m4, m0
  256. paddd m5, m1
  257. add filterq, mmsize
  258. add srcq, srcmul*mmsize/2
  259. cmp srcq, srcendq ; while (src += 4) < &src[filterSize]
  260. jl .innerloop
  261. %ifidn %4, X4
  262. mov32 pos1q, dword [fltposq+wq*4+4] ; filterPos[1]
  263. movlh m0, [srcq+ pos0q *srcmul] ; split last 4 srcpx of dstpx[0]
  264. sub pos1q, fltsizeq ; and first 4 srcpx of dstpx[1]
  265. %if %1 > 8
  266. movhps m0, [srcq+(pos1q+dlt)*srcmul]
  267. %else ; %1 == 8
  268. movd m1, [srcq+(pos1q+dlt)*srcmul]
  269. punpckldq m0, m1
  270. %endif ; %1 == 8
  271. %if %1 == 8
  272. punpcklbw m0, m3
  273. %endif ; %1 == 8
  274. %if %1 == 16 ; pmaddwd needs signed adds, so this moves unsigned -> signed, we'll
  275. ; add back 0x8000 * sum(coeffs) after the horizontal add
  276. psubw m0, m6
  277. %endif ; %1 == 16
  278. pmaddwd m0, [filterq]
  279. %endif ; %4 == X4
  280. lea filterq, [filterq+(fltsizeq+dlt)*2]
  281. %if notcpuflag(ssse3) ; sse2
  282. mova m1, m4
  283. punpcklqdq m4, m5
  284. punpckhqdq m1, m5
  285. paddd m4, m1
  286. %else ; ssse3/sse4
  287. phaddd m4, m5
  288. %endif ; sse2/ssse3/sse4
  289. %ifidn %4, X4
  290. paddd m4, m0
  291. %endif ; %3 == X4
  292. %if notcpuflag(ssse3) ; sse2
  293. pshufd m4, m4, 11011000b
  294. movhlps m0, m4
  295. paddd m0, m4
  296. %else ; ssse3/sse4
  297. phaddd m4, m4
  298. SWAP 0, 4
  299. %endif ; sse2/ssse3/sse4
  300. %endif ; %3 ==/!= X
  301. %if %1 == 16 ; add 0x8000 * sum(coeffs), i.e. back from signed -> unsigned
  302. paddd m0, m7
  303. %endif ; %1 == 16
  304. ; clip, store
  305. psrad m0, 14 + %1 - %2
  306. %ifidn %3, X
  307. movifnidn dstq, dstmp
  308. %endif ; %3 == X
  309. %if %2 == 15
  310. packssdw m0, m0
  311. %ifnidn %3, X
  312. movh [dstq+wq*(2>>wshr)], m0
  313. %else ; %3 == X
  314. movd [dstq+wq*2], m0
  315. %endif ; %3 ==/!= X
  316. %else ; %2 == 19
  317. PMINSD m0, m2, m4
  318. %ifnidn %3, X
  319. mova [dstq+wq*(4>>wshr)], m0
  320. %else ; %3 == X
  321. movq [dstq+wq*4], m0
  322. %endif ; %3 ==/!= X
  323. %endif ; %2 == 15/19
  324. %ifnidn %3, X
  325. add wq, (mmsize<<wshr)/4 ; both 8tap and 4tap really only do 4 pixels
  326. ; per iteration. see "shl wq,1" above as for why we do this
  327. %else ; %3 == X
  328. add wq, 2
  329. %endif ; %3 ==/!= X
  330. jl .loop
  331. RET
  332. %endmacro
  333. ; SCALE_FUNCS source_width, intermediate_nbits, n_xmm
  334. %macro SCALE_FUNCS 3
  335. SCALE_FUNC %1, %2, 4, 4, 6, %3
  336. SCALE_FUNC %1, %2, 8, 8, 6, %3
  337. SCALE_FUNC %1, %2, X, X4, 7, %3
  338. SCALE_FUNC %1, %2, X, X8, 7, %3
  339. %endmacro
  340. ; SCALE_FUNCS2 8_xmm_args, 9to10_xmm_args, 16_xmm_args
  341. %macro SCALE_FUNCS2 3
  342. %if notcpuflag(sse4)
  343. SCALE_FUNCS 8, 15, %1
  344. SCALE_FUNCS 9, 15, %2
  345. SCALE_FUNCS 10, 15, %2
  346. SCALE_FUNCS 12, 15, %2
  347. SCALE_FUNCS 14, 15, %2
  348. SCALE_FUNCS 16, 15, %3
  349. %endif ; !sse4
  350. SCALE_FUNCS 8, 19, %1
  351. SCALE_FUNCS 9, 19, %2
  352. SCALE_FUNCS 10, 19, %2
  353. SCALE_FUNCS 12, 19, %2
  354. SCALE_FUNCS 14, 19, %2
  355. SCALE_FUNCS 16, 19, %3
  356. %endmacro
  357. INIT_XMM sse2
  358. SCALE_FUNCS2 7, 6, 8
  359. INIT_XMM ssse3
  360. SCALE_FUNCS2 6, 6, 8
  361. INIT_XMM sse4
  362. SCALE_FUNCS2 6, 6, 8