scale.asm 13 KB

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