input.asm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. ;******************************************************************************
  2. ;* x86-optimized input routines; does shuffling of packed
  3. ;* YUV formats into individual planes, and converts RGB
  4. ;* into YUV planes also.
  5. ;* Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.com>
  6. ;*
  7. ;* This file is part of Libav.
  8. ;*
  9. ;* Libav is free software; you can redistribute it and/or
  10. ;* modify it under the terms of the GNU Lesser General Public
  11. ;* License as published by the Free Software Foundation; either
  12. ;* version 2.1 of the License, or (at your option) any later version.
  13. ;*
  14. ;* Libav is distributed in the hope that it will be useful,
  15. ;* but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. ;* Lesser General Public License for more details.
  18. ;*
  19. ;* You should have received a copy of the GNU Lesser General Public
  20. ;* License along with Libav; if not, write to the Free Software
  21. ;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. ;******************************************************************************
  23. %include "x86inc.asm"
  24. %include "x86util.asm"
  25. SECTION_RODATA
  26. %define RY 0x20DE
  27. %define GY 0x4087
  28. %define BY 0x0C88
  29. %define RU 0xECFF
  30. %define GU 0xDAC8
  31. %define BU 0x3838
  32. %define RV 0x3838
  33. %define GV 0xD0E3
  34. %define BV 0xF6E4
  35. rgb_Yrnd: times 4 dd 0x80100 ; 16.5 << 15
  36. rgb_UVrnd: times 4 dd 0x400100 ; 128.5 << 15
  37. bgr_Ycoeff_12x4: times 2 dw BY, GY, 0, BY
  38. bgr_Ycoeff_3x56: times 2 dw RY, 0, GY, RY
  39. rgb_Ycoeff_12x4: times 2 dw RY, GY, 0, RY
  40. rgb_Ycoeff_3x56: times 2 dw BY, 0, GY, BY
  41. bgr_Ucoeff_12x4: times 2 dw BU, GU, 0, BU
  42. bgr_Ucoeff_3x56: times 2 dw RU, 0, GU, RU
  43. rgb_Ucoeff_12x4: times 2 dw RU, GU, 0, RU
  44. rgb_Ucoeff_3x56: times 2 dw BU, 0, GU, BU
  45. bgr_Vcoeff_12x4: times 2 dw BV, GV, 0, BV
  46. bgr_Vcoeff_3x56: times 2 dw RV, 0, GV, RV
  47. rgb_Vcoeff_12x4: times 2 dw RV, GV, 0, RV
  48. rgb_Vcoeff_3x56: times 2 dw BV, 0, GV, BV
  49. rgba_Ycoeff_rb: times 4 dw RY, BY
  50. rgba_Ycoeff_br: times 4 dw BY, RY
  51. rgba_Ycoeff_ga: times 4 dw GY, 0
  52. rgba_Ycoeff_ag: times 4 dw 0, GY
  53. rgba_Ucoeff_rb: times 4 dw RU, BU
  54. rgba_Ucoeff_br: times 4 dw BU, RU
  55. rgba_Ucoeff_ga: times 4 dw GU, 0
  56. rgba_Ucoeff_ag: times 4 dw 0, GU
  57. rgba_Vcoeff_rb: times 4 dw RV, BV
  58. rgba_Vcoeff_br: times 4 dw BV, RV
  59. rgba_Vcoeff_ga: times 4 dw GV, 0
  60. rgba_Vcoeff_ag: times 4 dw 0, GV
  61. shuf_rgb_12x4: db 0, 0x80, 1, 0x80, 2, 0x80, 3, 0x80, \
  62. 6, 0x80, 7, 0x80, 8, 0x80, 9, 0x80
  63. shuf_rgb_3x56: db 2, 0x80, 3, 0x80, 4, 0x80, 5, 0x80, \
  64. 8, 0x80, 9, 0x80, 10, 0x80, 11, 0x80
  65. SECTION .text
  66. ;-----------------------------------------------------------------------------
  67. ; RGB to Y/UV.
  68. ;
  69. ; void <fmt>ToY_<opt>(uint8_t *dst, const uint8_t *src, int w);
  70. ; and
  71. ; void <fmt>toUV_<opt>(uint8_t *dstU, uint8_t *dstV, const uint8_t *src,
  72. ; const uint8_t *unused, int w);
  73. ;-----------------------------------------------------------------------------
  74. ; %1 = nr. of XMM registers
  75. ; %2 = rgb or bgr
  76. %macro RGB24_TO_Y_FN 2-3
  77. cglobal %2 %+ 24ToY, 6, 6, %1, dst, src, u1, u2, w, u3
  78. %if mmsize == 8
  79. mova m5, [%2_Ycoeff_12x4]
  80. mova m6, [%2_Ycoeff_3x56]
  81. %define coeff1 m5
  82. %define coeff2 m6
  83. %elif ARCH_X86_64
  84. mova m8, [%2_Ycoeff_12x4]
  85. mova m9, [%2_Ycoeff_3x56]
  86. %define coeff1 m8
  87. %define coeff2 m9
  88. %else ; x86-32 && mmsize == 16
  89. %define coeff1 [%2_Ycoeff_12x4]
  90. %define coeff2 [%2_Ycoeff_3x56]
  91. %endif ; x86-32/64 && mmsize == 8/16
  92. %if (ARCH_X86_64 || mmsize == 8) && %0 == 3
  93. jmp mangle(program_name %+ _ %+ %3 %+ 24ToY %+ SUFFIX).body
  94. %else ; (ARCH_X86_64 && %0 == 3) || mmsize == 8
  95. .body:
  96. %if cpuflag(ssse3)
  97. mova m7, [shuf_rgb_12x4]
  98. %define shuf_rgb1 m7
  99. %if ARCH_X86_64
  100. mova m10, [shuf_rgb_3x56]
  101. %define shuf_rgb2 m10
  102. %else ; x86-32
  103. %define shuf_rgb2 [shuf_rgb_3x56]
  104. %endif ; x86-32/64
  105. %endif ; cpuflag(ssse3)
  106. %if ARCH_X86_64
  107. movsxd wq, wd
  108. %endif
  109. add wq, wq
  110. add dstq, wq
  111. neg wq
  112. %if notcpuflag(ssse3)
  113. pxor m7, m7
  114. %endif ; !cpuflag(ssse3)
  115. mova m4, [rgb_Yrnd]
  116. .loop:
  117. %if cpuflag(ssse3)
  118. movu m0, [srcq+0] ; (byte) { Bx, Gx, Rx }[0-3]
  119. movu m2, [srcq+12] ; (byte) { Bx, Gx, Rx }[4-7]
  120. pshufb m1, m0, shuf_rgb2 ; (word) { R0, B1, G1, R1, R2, B3, G3, R3 }
  121. pshufb m0, shuf_rgb1 ; (word) { B0, G0, R0, B1, B2, G2, R2, B3 }
  122. pshufb m3, m2, shuf_rgb2 ; (word) { R4, B5, G5, R5, R6, B7, G7, R7 }
  123. pshufb m2, shuf_rgb1 ; (word) { B4, G4, R4, B5, B6, G6, R6, B7 }
  124. %else ; !cpuflag(ssse3)
  125. movd m0, [srcq+0] ; (byte) { B0, G0, R0, B1 }
  126. movd m1, [srcq+2] ; (byte) { R0, B1, G1, R1 }
  127. movd m2, [srcq+6] ; (byte) { B2, G2, R2, B3 }
  128. movd m3, [srcq+8] ; (byte) { R2, B3, G3, R3 }
  129. %if mmsize == 16 ; i.e. sse2
  130. punpckldq m0, m2 ; (byte) { B0, G0, R0, B1, B2, G2, R2, B3 }
  131. punpckldq m1, m3 ; (byte) { R0, B1, G1, R1, R2, B3, G3, R3 }
  132. movd m2, [srcq+12] ; (byte) { B4, G4, R4, B5 }
  133. movd m3, [srcq+14] ; (byte) { R4, B5, G5, R5 }
  134. movd m5, [srcq+18] ; (byte) { B6, G6, R6, B7 }
  135. movd m6, [srcq+20] ; (byte) { R6, B7, G7, R7 }
  136. punpckldq m2, m5 ; (byte) { B4, G4, R4, B5, B6, G6, R6, B7 }
  137. punpckldq m3, m6 ; (byte) { R4, B5, G5, R5, R6, B7, G7, R7 }
  138. %endif ; mmsize == 16
  139. punpcklbw m0, m7 ; (word) { B0, G0, R0, B1, B2, G2, R2, B3 }
  140. punpcklbw m1, m7 ; (word) { R0, B1, G1, R1, R2, B3, G3, R3 }
  141. punpcklbw m2, m7 ; (word) { B4, G4, R4, B5, B6, G6, R6, B7 }
  142. punpcklbw m3, m7 ; (word) { R4, B5, G5, R5, R6, B7, G7, R7 }
  143. %endif ; cpuflag(ssse3)
  144. add srcq, 3 * mmsize / 2
  145. pmaddwd m0, coeff1 ; (dword) { B0*BY + G0*GY, B1*BY, B2*BY + G2*GY, B3*BY }
  146. pmaddwd m1, coeff2 ; (dword) { R0*RY, G1+GY + R1*RY, R2*RY, G3+GY + R3*RY }
  147. pmaddwd m2, coeff1 ; (dword) { B4*BY + G4*GY, B5*BY, B6*BY + G6*GY, B7*BY }
  148. pmaddwd m3, coeff2 ; (dword) { R4*RY, G5+GY + R5*RY, R6*RY, G7+GY + R7*RY }
  149. paddd m0, m1 ; (dword) { Bx*BY + Gx*GY + Rx*RY }[0-3]
  150. paddd m2, m3 ; (dword) { Bx*BY + Gx*GY + Rx*RY }[4-7]
  151. paddd m0, m4 ; += rgb_Yrnd, i.e. (dword) { Y[0-3] }
  152. paddd m2, m4 ; += rgb_Yrnd, i.e. (dword) { Y[4-7] }
  153. psrad m0, 9
  154. psrad m2, 9
  155. packssdw m0, m2 ; (word) { Y[0-7] }
  156. mova [dstq+wq], m0
  157. add wq, mmsize
  158. jl .loop
  159. REP_RET
  160. %endif ; (ARCH_X86_64 && %0 == 3) || mmsize == 8
  161. %endmacro
  162. ; %1 = nr. of XMM registers
  163. ; %2 = rgb or bgr
  164. %macro RGB24_TO_UV_FN 2-3
  165. cglobal %2 %+ 24ToUV, 7, 7, %1, dstU, dstV, u1, src, u2, w, u3
  166. %if ARCH_X86_64
  167. mova m8, [%2_Ucoeff_12x4]
  168. mova m9, [%2_Ucoeff_3x56]
  169. mova m10, [%2_Vcoeff_12x4]
  170. mova m11, [%2_Vcoeff_3x56]
  171. %define coeffU1 m8
  172. %define coeffU2 m9
  173. %define coeffV1 m10
  174. %define coeffV2 m11
  175. %else ; x86-32
  176. %define coeffU1 [%2_Ucoeff_12x4]
  177. %define coeffU2 [%2_Ucoeff_3x56]
  178. %define coeffV1 [%2_Vcoeff_12x4]
  179. %define coeffV2 [%2_Vcoeff_3x56]
  180. %endif ; x86-32/64
  181. %if ARCH_X86_64 && %0 == 3
  182. jmp mangle(program_name %+ _ %+ %3 %+ 24ToUV %+ SUFFIX).body
  183. %else ; ARCH_X86_64 && %0 == 3
  184. .body:
  185. %if cpuflag(ssse3)
  186. mova m7, [shuf_rgb_12x4]
  187. %define shuf_rgb1 m7
  188. %if ARCH_X86_64
  189. mova m12, [shuf_rgb_3x56]
  190. %define shuf_rgb2 m12
  191. %else ; x86-32
  192. %define shuf_rgb2 [shuf_rgb_3x56]
  193. %endif ; x86-32/64
  194. %endif ; cpuflag(ssse3)
  195. %if ARCH_X86_64
  196. movsxd wq, dword r5m
  197. %else ; x86-32
  198. mov wq, r5m
  199. %endif
  200. add wq, wq
  201. add dstUq, wq
  202. add dstVq, wq
  203. neg wq
  204. mova m6, [rgb_UVrnd]
  205. %if notcpuflag(ssse3)
  206. pxor m7, m7
  207. %endif
  208. .loop:
  209. %if cpuflag(ssse3)
  210. movu m0, [srcq+0] ; (byte) { Bx, Gx, Rx }[0-3]
  211. movu m4, [srcq+12] ; (byte) { Bx, Gx, Rx }[4-7]
  212. pshufb m1, m0, shuf_rgb2 ; (word) { R0, B1, G1, R1, R2, B3, G3, R3 }
  213. pshufb m0, shuf_rgb1 ; (word) { B0, G0, R0, B1, B2, G2, R2, B3 }
  214. %else ; !cpuflag(ssse3)
  215. movd m0, [srcq+0] ; (byte) { B0, G0, R0, B1 }
  216. movd m1, [srcq+2] ; (byte) { R0, B1, G1, R1 }
  217. movd m4, [srcq+6] ; (byte) { B2, G2, R2, B3 }
  218. movd m5, [srcq+8] ; (byte) { R2, B3, G3, R3 }
  219. %if mmsize == 16
  220. punpckldq m0, m4 ; (byte) { B0, G0, R0, B1, B2, G2, R2, B3 }
  221. punpckldq m1, m5 ; (byte) { R0, B1, G1, R1, R2, B3, G3, R3 }
  222. movd m4, [srcq+12] ; (byte) { B4, G4, R4, B5 }
  223. movd m5, [srcq+14] ; (byte) { R4, B5, G5, R5 }
  224. %endif ; mmsize == 16
  225. punpcklbw m0, m7 ; (word) { B0, G0, R0, B1, B2, G2, R2, B3 }
  226. punpcklbw m1, m7 ; (word) { R0, B1, G1, R1, R2, B3, G3, R3 }
  227. %endif ; cpuflag(ssse3)
  228. pmaddwd m2, m0, coeffV1 ; (dword) { B0*BV + G0*GV, B1*BV, B2*BV + G2*GV, B3*BV }
  229. pmaddwd m3, m1, coeffV2 ; (dword) { R0*BV, G1*GV + R1*BV, R2*BV, G3*GV + R3*BV }
  230. pmaddwd m0, coeffU1 ; (dword) { B0*BU + G0*GU, B1*BU, B2*BU + G2*GU, B3*BU }
  231. pmaddwd m1, coeffU2 ; (dword) { R0*BU, G1*GU + R1*BU, R2*BU, G3*GU + R3*BU }
  232. paddd m0, m1 ; (dword) { Bx*BU + Gx*GU + Rx*RU }[0-3]
  233. paddd m2, m3 ; (dword) { Bx*BV + Gx*GV + Rx*RV }[0-3]
  234. %if cpuflag(ssse3)
  235. pshufb m5, m4, shuf_rgb2 ; (word) { R4, B5, G5, R5, R6, B7, G7, R7 }
  236. pshufb m4, shuf_rgb1 ; (word) { B4, G4, R4, B5, B6, G6, R6, B7 }
  237. %else ; !cpuflag(ssse3)
  238. %if mmsize == 16
  239. movd m1, [srcq+18] ; (byte) { B6, G6, R6, B7 }
  240. movd m3, [srcq+20] ; (byte) { R6, B7, G7, R7 }
  241. punpckldq m4, m1 ; (byte) { B4, G4, R4, B5, B6, G6, R6, B7 }
  242. punpckldq m5, m3 ; (byte) { R4, B5, G5, R5, R6, B7, G7, R7 }
  243. %endif ; mmsize == 16 && !cpuflag(ssse3)
  244. punpcklbw m4, m7 ; (word) { B4, G4, R4, B5, B6, G6, R6, B7 }
  245. punpcklbw m5, m7 ; (word) { R4, B5, G5, R5, R6, B7, G7, R7 }
  246. %endif ; cpuflag(ssse3)
  247. add srcq, 3 * mmsize / 2
  248. pmaddwd m1, m4, coeffU1 ; (dword) { B4*BU + G4*GU, B5*BU, B6*BU + G6*GU, B7*BU }
  249. pmaddwd m3, m5, coeffU2 ; (dword) { R4*BU, G5*GU + R5*BU, R6*BU, G7*GU + R7*BU }
  250. pmaddwd m4, coeffV1 ; (dword) { B4*BV + G4*GV, B5*BV, B6*BV + G6*GV, B7*BV }
  251. pmaddwd m5, coeffV2 ; (dword) { R4*BV, G5*GV + R5*BV, R6*BV, G7*GV + R7*BV }
  252. paddd m1, m3 ; (dword) { Bx*BU + Gx*GU + Rx*RU }[4-7]
  253. paddd m4, m5 ; (dword) { Bx*BV + Gx*GV + Rx*RV }[4-7]
  254. paddd m0, m6 ; += rgb_UVrnd, i.e. (dword) { U[0-3] }
  255. paddd m2, m6 ; += rgb_UVrnd, i.e. (dword) { V[0-3] }
  256. paddd m1, m6 ; += rgb_UVrnd, i.e. (dword) { U[4-7] }
  257. paddd m4, m6 ; += rgb_UVrnd, i.e. (dword) { V[4-7] }
  258. psrad m0, 9
  259. psrad m2, 9
  260. psrad m1, 9
  261. psrad m4, 9
  262. packssdw m0, m1 ; (word) { U[0-7] }
  263. packssdw m2, m4 ; (word) { V[0-7] }
  264. %if mmsize == 8
  265. mova [dstUq+wq], m0
  266. mova [dstVq+wq], m2
  267. %else ; mmsize == 16
  268. mova [dstUq+wq], m0
  269. mova [dstVq+wq], m2
  270. %endif ; mmsize == 8/16
  271. add wq, mmsize
  272. jl .loop
  273. REP_RET
  274. %endif ; ARCH_X86_64 && %0 == 3
  275. %endmacro
  276. ; %1 = nr. of XMM registers for rgb-to-Y func
  277. ; %2 = nr. of XMM registers for rgb-to-UV func
  278. %macro RGB24_FUNCS 2
  279. RGB24_TO_Y_FN %1, rgb
  280. RGB24_TO_Y_FN %1, bgr, rgb
  281. RGB24_TO_UV_FN %2, rgb
  282. RGB24_TO_UV_FN %2, bgr, rgb
  283. %endmacro
  284. %if ARCH_X86_32
  285. INIT_MMX mmx
  286. RGB24_FUNCS 0, 0
  287. %endif
  288. INIT_XMM sse2
  289. RGB24_FUNCS 10, 12
  290. INIT_XMM ssse3
  291. RGB24_FUNCS 11, 13
  292. %if HAVE_AVX
  293. INIT_XMM avx
  294. RGB24_FUNCS 11, 13
  295. %endif
  296. ; %1 = nr. of XMM registers
  297. ; %2-5 = rgba, bgra, argb or abgr (in individual characters)
  298. %macro RGB32_TO_Y_FN 5-6
  299. cglobal %2%3%4%5 %+ ToY, 6, 6, %1, dst, src, u1, u2, w, u3
  300. mova m5, [rgba_Ycoeff_%2%4]
  301. mova m6, [rgba_Ycoeff_%3%5]
  302. %if %0 == 6
  303. jmp mangle(program_name %+ _ %+ %6 %+ ToY %+ SUFFIX).body
  304. %else ; %0 == 6
  305. .body:
  306. %if ARCH_X86_64
  307. movsxd wq, wd
  308. %endif
  309. lea srcq, [srcq+wq*4]
  310. add wq, wq
  311. add dstq, wq
  312. neg wq
  313. mova m4, [rgb_Yrnd]
  314. pcmpeqb m7, m7
  315. psrlw m7, 8 ; (word) { 0x00ff } x4
  316. .loop:
  317. ; FIXME check alignment and use mova
  318. movu m0, [srcq+wq*2+0] ; (byte) { Bx, Gx, Rx, xx }[0-3]
  319. movu m2, [srcq+wq*2+mmsize] ; (byte) { Bx, Gx, Rx, xx }[4-7]
  320. DEINTB 1, 0, 3, 2, 7 ; (word) { Gx, xx (m0/m2) or Bx, Rx (m1/m3) }[0-3]/[4-7]
  321. pmaddwd m1, m5 ; (dword) { Bx*BY + Rx*RY }[0-3]
  322. pmaddwd m0, m6 ; (dword) { Gx*GY }[0-3]
  323. pmaddwd m3, m5 ; (dword) { Bx*BY + Rx*RY }[4-7]
  324. pmaddwd m2, m6 ; (dword) { Gx*GY }[4-7]
  325. paddd m0, m4 ; += rgb_Yrnd
  326. paddd m2, m4 ; += rgb_Yrnd
  327. paddd m0, m1 ; (dword) { Y[0-3] }
  328. paddd m2, m3 ; (dword) { Y[4-7] }
  329. psrad m0, 9
  330. psrad m2, 9
  331. packssdw m0, m2 ; (word) { Y[0-7] }
  332. mova [dstq+wq], m0
  333. add wq, mmsize
  334. jl .loop
  335. REP_RET
  336. %endif ; %0 == 3
  337. %endmacro
  338. ; %1 = nr. of XMM registers
  339. ; %2-5 = rgba, bgra, argb or abgr (in individual characters)
  340. %macro RGB32_TO_UV_FN 5-6
  341. cglobal %2%3%4%5 %+ ToUV, 7, 7, %1, dstU, dstV, u1, src, u2, w, u3
  342. %if ARCH_X86_64
  343. mova m8, [rgba_Ucoeff_%2%4]
  344. mova m9, [rgba_Ucoeff_%3%5]
  345. mova m10, [rgba_Vcoeff_%2%4]
  346. mova m11, [rgba_Vcoeff_%3%5]
  347. %define coeffU1 m8
  348. %define coeffU2 m9
  349. %define coeffV1 m10
  350. %define coeffV2 m11
  351. %else ; x86-32
  352. %define coeffU1 [rgba_Ucoeff_%2%4]
  353. %define coeffU2 [rgba_Ucoeff_%3%5]
  354. %define coeffV1 [rgba_Vcoeff_%2%4]
  355. %define coeffV2 [rgba_Vcoeff_%3%5]
  356. %endif ; x86-64/32
  357. %if ARCH_X86_64 && %0 == 6
  358. jmp mangle(program_name %+ _ %+ %6 %+ ToUV %+ SUFFIX).body
  359. %else ; ARCH_X86_64 && %0 == 6
  360. .body:
  361. %if ARCH_X86_64
  362. movsxd wq, dword r5m
  363. %else ; x86-32
  364. mov wq, r5m
  365. %endif
  366. add wq, wq
  367. add dstUq, wq
  368. add dstVq, wq
  369. lea srcq, [srcq+wq*2]
  370. neg wq
  371. pcmpeqb m7, m7
  372. psrlw m7, 8 ; (word) { 0x00ff } x4
  373. mova m6, [rgb_UVrnd]
  374. .loop:
  375. ; FIXME check alignment and use mova
  376. movu m0, [srcq+wq*2+0] ; (byte) { Bx, Gx, Rx, xx }[0-3]
  377. movu m4, [srcq+wq*2+mmsize] ; (byte) { Bx, Gx, Rx, xx }[4-7]
  378. DEINTB 1, 0, 5, 4, 7 ; (word) { Gx, xx (m0/m4) or Bx, Rx (m1/m5) }[0-3]/[4-7]
  379. pmaddwd m3, m1, coeffV1 ; (dword) { Bx*BV + Rx*RV }[0-3]
  380. pmaddwd m2, m0, coeffV2 ; (dword) { Gx*GV }[0-3]
  381. pmaddwd m1, coeffU1 ; (dword) { Bx*BU + Rx*RU }[0-3]
  382. pmaddwd m0, coeffU2 ; (dword) { Gx*GU }[0-3]
  383. paddd m3, m6 ; += rgb_UVrnd
  384. paddd m1, m6 ; += rgb_UVrnd
  385. paddd m2, m3 ; (dword) { V[0-3] }
  386. paddd m0, m1 ; (dword) { U[0-3] }
  387. pmaddwd m3, m5, coeffV1 ; (dword) { Bx*BV + Rx*RV }[4-7]
  388. pmaddwd m1, m4, coeffV2 ; (dword) { Gx*GV }[4-7]
  389. pmaddwd m5, coeffU1 ; (dword) { Bx*BU + Rx*RU }[4-7]
  390. pmaddwd m4, coeffU2 ; (dword) { Gx*GU }[4-7]
  391. paddd m3, m6 ; += rgb_UVrnd
  392. paddd m5, m6 ; += rgb_UVrnd
  393. psrad m0, 9
  394. paddd m1, m3 ; (dword) { V[4-7] }
  395. paddd m4, m5 ; (dword) { U[4-7] }
  396. psrad m2, 9
  397. psrad m4, 9
  398. psrad m1, 9
  399. packssdw m0, m4 ; (word) { U[0-7] }
  400. packssdw m2, m1 ; (word) { V[0-7] }
  401. %if mmsize == 8
  402. mova [dstUq+wq], m0
  403. mova [dstVq+wq], m2
  404. %else ; mmsize == 16
  405. mova [dstUq+wq], m0
  406. mova [dstVq+wq], m2
  407. %endif ; mmsize == 8/16
  408. add wq, mmsize
  409. jl .loop
  410. REP_RET
  411. %endif ; ARCH_X86_64 && %0 == 3
  412. %endmacro
  413. ; %1 = nr. of XMM registers for rgb-to-Y func
  414. ; %2 = nr. of XMM registers for rgb-to-UV func
  415. %macro RGB32_FUNCS 2
  416. RGB32_TO_Y_FN %1, r, g, b, a
  417. RGB32_TO_Y_FN %1, b, g, r, a, rgba
  418. RGB32_TO_Y_FN %1, a, r, g, b, rgba
  419. RGB32_TO_Y_FN %1, a, b, g, r, rgba
  420. RGB32_TO_UV_FN %2, r, g, b, a
  421. RGB32_TO_UV_FN %2, b, g, r, a, rgba
  422. RGB32_TO_UV_FN %2, a, r, g, b, rgba
  423. RGB32_TO_UV_FN %2, a, b, g, r, rgba
  424. %endmacro
  425. %if ARCH_X86_32
  426. INIT_MMX mmx
  427. RGB32_FUNCS 0, 0
  428. %endif
  429. INIT_XMM sse2
  430. RGB32_FUNCS 8, 12
  431. %if HAVE_AVX
  432. INIT_XMM avx
  433. RGB32_FUNCS 8, 12
  434. %endif
  435. ;-----------------------------------------------------------------------------
  436. ; YUYV/UYVY/NV12/NV21 packed pixel shuffling.
  437. ;
  438. ; void <fmt>ToY_<opt>(uint8_t *dst, const uint8_t *src, int w);
  439. ; and
  440. ; void <fmt>toUV_<opt>(uint8_t *dstU, uint8_t *dstV, const uint8_t *src,
  441. ; const uint8_t *unused, int w);
  442. ;-----------------------------------------------------------------------------
  443. ; %1 = a (aligned) or u (unaligned)
  444. ; %2 = yuyv or uyvy
  445. %macro LOOP_YUYV_TO_Y 2
  446. .loop_%1:
  447. mov%1 m0, [srcq+wq*2] ; (byte) { Y0, U0, Y1, V0, ... }
  448. mov%1 m1, [srcq+wq*2+mmsize] ; (byte) { Y8, U4, Y9, V4, ... }
  449. %ifidn %2, yuyv
  450. pand m0, m2 ; (word) { Y0, Y1, ..., Y7 }
  451. pand m1, m2 ; (word) { Y8, Y9, ..., Y15 }
  452. %else ; uyvy
  453. psrlw m0, 8 ; (word) { Y0, Y1, ..., Y7 }
  454. psrlw m1, 8 ; (word) { Y8, Y9, ..., Y15 }
  455. %endif ; yuyv/uyvy
  456. packuswb m0, m1 ; (byte) { Y0, ..., Y15 }
  457. mova [dstq+wq], m0
  458. add wq, mmsize
  459. jl .loop_%1
  460. REP_RET
  461. %endmacro
  462. ; %1 = nr. of XMM registers
  463. ; %2 = yuyv or uyvy
  464. ; %3 = if specified, it means that unaligned and aligned code in loop
  465. ; will be the same (i.e. YUYV+AVX), and thus we don't need to
  466. ; split the loop in an aligned and unaligned case
  467. %macro YUYV_TO_Y_FN 2-3
  468. cglobal %2ToY, 5, 5, %1, dst, unused0, unused1, src, w
  469. %if ARCH_X86_64
  470. movsxd wq, wd
  471. %endif
  472. add dstq, wq
  473. %if mmsize == 16
  474. test srcq, 15
  475. %endif
  476. lea srcq, [srcq+wq*2]
  477. %ifidn %2, yuyv
  478. pcmpeqb m2, m2 ; (byte) { 0xff } x 16
  479. psrlw m2, 8 ; (word) { 0x00ff } x 8
  480. %endif ; yuyv
  481. %if mmsize == 16
  482. jnz .loop_u_start
  483. neg wq
  484. LOOP_YUYV_TO_Y a, %2
  485. .loop_u_start:
  486. neg wq
  487. LOOP_YUYV_TO_Y u, %2
  488. %else ; mmsize == 8
  489. neg wq
  490. LOOP_YUYV_TO_Y a, %2
  491. %endif ; mmsize == 8/16
  492. %endmacro
  493. ; %1 = a (aligned) or u (unaligned)
  494. ; %2 = yuyv or uyvy
  495. %macro LOOP_YUYV_TO_UV 2
  496. .loop_%1:
  497. %ifidn %2, yuyv
  498. mov%1 m0, [srcq+wq*4] ; (byte) { Y0, U0, Y1, V0, ... }
  499. mov%1 m1, [srcq+wq*4+mmsize] ; (byte) { Y8, U4, Y9, V4, ... }
  500. psrlw m0, 8 ; (word) { U0, V0, ..., U3, V3 }
  501. psrlw m1, 8 ; (word) { U4, V4, ..., U7, V7 }
  502. %else ; uyvy
  503. %if cpuflag(avx)
  504. vpand m0, m2, [srcq+wq*4] ; (word) { U0, V0, ..., U3, V3 }
  505. vpand m1, m2, [srcq+wq*4+mmsize] ; (word) { U4, V4, ..., U7, V7 }
  506. %else
  507. mov%1 m0, [srcq+wq*4] ; (byte) { Y0, U0, Y1, V0, ... }
  508. mov%1 m1, [srcq+wq*4+mmsize] ; (byte) { Y8, U4, Y9, V4, ... }
  509. pand m0, m2 ; (word) { U0, V0, ..., U3, V3 }
  510. pand m1, m2 ; (word) { U4, V4, ..., U7, V7 }
  511. %endif
  512. %endif ; yuyv/uyvy
  513. packuswb m0, m1 ; (byte) { U0, V0, ..., U7, V7 }
  514. pand m1, m0, m2 ; (word) { U0, U1, ..., U7 }
  515. psrlw m0, 8 ; (word) { V0, V1, ..., V7 }
  516. %if mmsize == 16
  517. packuswb m1, m0 ; (byte) { U0, ... U7, V1, ... V7 }
  518. movh [dstUq+wq], m1
  519. movhps [dstVq+wq], m1
  520. %else ; mmsize == 8
  521. packuswb m1, m1 ; (byte) { U0, ... U3 }
  522. packuswb m0, m0 ; (byte) { V0, ... V3 }
  523. movh [dstUq+wq], m1
  524. movh [dstVq+wq], m0
  525. %endif ; mmsize == 8/16
  526. add wq, mmsize / 2
  527. jl .loop_%1
  528. REP_RET
  529. %endmacro
  530. ; %1 = nr. of XMM registers
  531. ; %2 = yuyv or uyvy
  532. ; %3 = if specified, it means that unaligned and aligned code in loop
  533. ; will be the same (i.e. UYVY+AVX), and thus we don't need to
  534. ; split the loop in an aligned and unaligned case
  535. %macro YUYV_TO_UV_FN 2-3
  536. cglobal %2ToUV, 4, 5, %1, dstU, dstV, unused, src, w
  537. %if ARCH_X86_64
  538. movsxd wq, dword r5m
  539. %else ; x86-32
  540. mov wq, r5m
  541. %endif
  542. add dstUq, wq
  543. add dstVq, wq
  544. %if mmsize == 16 && %0 == 2
  545. test srcq, 15
  546. %endif
  547. lea srcq, [srcq+wq*4]
  548. pcmpeqb m2, m2 ; (byte) { 0xff } x 16
  549. psrlw m2, 8 ; (word) { 0x00ff } x 8
  550. ; NOTE: if uyvy+avx, u/a are identical
  551. %if mmsize == 16 && %0 == 2
  552. jnz .loop_u_start
  553. neg wq
  554. LOOP_YUYV_TO_UV a, %2
  555. .loop_u_start:
  556. neg wq
  557. LOOP_YUYV_TO_UV u, %2
  558. %else ; mmsize == 8
  559. neg wq
  560. LOOP_YUYV_TO_UV a, %2
  561. %endif ; mmsize == 8/16
  562. %endmacro
  563. ; %1 = a (aligned) or u (unaligned)
  564. ; %2 = nv12 or nv21
  565. %macro LOOP_NVXX_TO_UV 2
  566. .loop_%1:
  567. mov%1 m0, [srcq+wq*2] ; (byte) { U0, V0, U1, V1, ... }
  568. mov%1 m1, [srcq+wq*2+mmsize] ; (byte) { U8, V8, U9, V9, ... }
  569. pand m2, m0, m5 ; (word) { U0, U1, ..., U7 }
  570. pand m3, m1, m5 ; (word) { U8, U9, ..., U15 }
  571. psrlw m0, 8 ; (word) { V0, V1, ..., V7 }
  572. psrlw m1, 8 ; (word) { V8, V9, ..., V15 }
  573. packuswb m2, m3 ; (byte) { U0, ..., U15 }
  574. packuswb m0, m1 ; (byte) { V0, ..., V15 }
  575. %ifidn %2, nv12
  576. mova [dstUq+wq], m2
  577. mova [dstVq+wq], m0
  578. %else ; nv21
  579. mova [dstVq+wq], m2
  580. mova [dstUq+wq], m0
  581. %endif ; nv12/21
  582. add wq, mmsize
  583. jl .loop_%1
  584. REP_RET
  585. %endmacro
  586. ; %1 = nr. of XMM registers
  587. ; %2 = nv12 or nv21
  588. %macro NVXX_TO_UV_FN 2
  589. cglobal %2ToUV, 4, 5, %1, dstU, dstV, unused, src, w
  590. %if ARCH_X86_64
  591. movsxd wq, dword r5m
  592. %else ; x86-32
  593. mov wq, r5m
  594. %endif
  595. add dstUq, wq
  596. add dstVq, wq
  597. %if mmsize == 16
  598. test srcq, 15
  599. %endif
  600. lea srcq, [srcq+wq*2]
  601. pcmpeqb m5, m5 ; (byte) { 0xff } x 16
  602. psrlw m5, 8 ; (word) { 0x00ff } x 8
  603. %if mmsize == 16
  604. jnz .loop_u_start
  605. neg wq
  606. LOOP_NVXX_TO_UV a, %2
  607. .loop_u_start:
  608. neg wq
  609. LOOP_NVXX_TO_UV u, %2
  610. %else ; mmsize == 8
  611. neg wq
  612. LOOP_NVXX_TO_UV a, %2
  613. %endif ; mmsize == 8/16
  614. %endmacro
  615. %if ARCH_X86_32
  616. INIT_MMX mmx
  617. YUYV_TO_Y_FN 0, yuyv
  618. YUYV_TO_Y_FN 0, uyvy
  619. YUYV_TO_UV_FN 0, yuyv
  620. YUYV_TO_UV_FN 0, uyvy
  621. NVXX_TO_UV_FN 0, nv12
  622. NVXX_TO_UV_FN 0, nv21
  623. %endif
  624. INIT_XMM sse2
  625. YUYV_TO_Y_FN 3, yuyv
  626. YUYV_TO_Y_FN 2, uyvy
  627. YUYV_TO_UV_FN 3, yuyv
  628. YUYV_TO_UV_FN 3, uyvy
  629. NVXX_TO_UV_FN 5, nv12
  630. NVXX_TO_UV_FN 5, nv21
  631. %if HAVE_AVX
  632. INIT_XMM avx
  633. ; in theory, we could write a yuy2-to-y using vpand (i.e. AVX), but
  634. ; that's not faster in practice
  635. YUYV_TO_UV_FN 3, yuyv
  636. YUYV_TO_UV_FN 3, uyvy, 1
  637. NVXX_TO_UV_FN 5, nv12
  638. NVXX_TO_UV_FN 5, nv21
  639. %endif