input.asm 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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 FFmpeg.
  8. ;*
  9. ;* FFmpeg 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. ;* FFmpeg 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 FFmpeg; if not, write to the Free Software
  21. ;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. ;******************************************************************************
  23. %include "libavutil/x86/x86util.asm"
  24. SECTION_RODATA 32
  25. %define RY 0x20DE
  26. %define GY 0x4087
  27. %define BY 0x0C88
  28. %define RU 0xECFF
  29. %define GU 0xDAC8
  30. %define BU 0x3838
  31. %define RV 0x3838
  32. %define GV 0xD0E3
  33. %define BV 0xF6E4
  34. rgb_Yrnd: times 4 dd 0x80100 ; 16.5 << 15
  35. rgb_UVrnd: times 4 dd 0x400100 ; 128.5 << 15
  36. %define bgr_Ycoeff_12x4 16*4 + 16* 0 + tableq
  37. %define bgr_Ycoeff_3x56 16*4 + 16* 1 + tableq
  38. %define rgb_Ycoeff_12x4 16*4 + 16* 2 + tableq
  39. %define rgb_Ycoeff_3x56 16*4 + 16* 3 + tableq
  40. %define bgr_Ucoeff_12x4 16*4 + 16* 4 + tableq
  41. %define bgr_Ucoeff_3x56 16*4 + 16* 5 + tableq
  42. %define rgb_Ucoeff_12x4 16*4 + 16* 6 + tableq
  43. %define rgb_Ucoeff_3x56 16*4 + 16* 7 + tableq
  44. %define bgr_Vcoeff_12x4 16*4 + 16* 8 + tableq
  45. %define bgr_Vcoeff_3x56 16*4 + 16* 9 + tableq
  46. %define rgb_Vcoeff_12x4 16*4 + 16*10 + tableq
  47. %define rgb_Vcoeff_3x56 16*4 + 16*11 + tableq
  48. %define rgba_Ycoeff_rb 16*4 + 16*12 + tableq
  49. %define rgba_Ycoeff_br 16*4 + 16*13 + tableq
  50. %define rgba_Ycoeff_ga 16*4 + 16*14 + tableq
  51. %define rgba_Ycoeff_ag 16*4 + 16*15 + tableq
  52. %define rgba_Ucoeff_rb 16*4 + 16*16 + tableq
  53. %define rgba_Ucoeff_br 16*4 + 16*17 + tableq
  54. %define rgba_Ucoeff_ga 16*4 + 16*18 + tableq
  55. %define rgba_Ucoeff_ag 16*4 + 16*19 + tableq
  56. %define rgba_Vcoeff_rb 16*4 + 16*20 + tableq
  57. %define rgba_Vcoeff_br 16*4 + 16*21 + tableq
  58. %define rgba_Vcoeff_ga 16*4 + 16*22 + tableq
  59. %define rgba_Vcoeff_ag 16*4 + 16*23 + tableq
  60. ; bgr_Ycoeff_12x4: times 2 dw BY, GY, 0, BY
  61. ; bgr_Ycoeff_3x56: times 2 dw RY, 0, GY, RY
  62. ; rgb_Ycoeff_12x4: times 2 dw RY, GY, 0, RY
  63. ; rgb_Ycoeff_3x56: times 2 dw BY, 0, GY, BY
  64. ; bgr_Ucoeff_12x4: times 2 dw BU, GU, 0, BU
  65. ; bgr_Ucoeff_3x56: times 2 dw RU, 0, GU, RU
  66. ; rgb_Ucoeff_12x4: times 2 dw RU, GU, 0, RU
  67. ; rgb_Ucoeff_3x56: times 2 dw BU, 0, GU, BU
  68. ; bgr_Vcoeff_12x4: times 2 dw BV, GV, 0, BV
  69. ; bgr_Vcoeff_3x56: times 2 dw RV, 0, GV, RV
  70. ; rgb_Vcoeff_12x4: times 2 dw RV, GV, 0, RV
  71. ; rgb_Vcoeff_3x56: times 2 dw BV, 0, GV, BV
  72. ; rgba_Ycoeff_rb: times 4 dw RY, BY
  73. ; rgba_Ycoeff_br: times 4 dw BY, RY
  74. ; rgba_Ycoeff_ga: times 4 dw GY, 0
  75. ; rgba_Ycoeff_ag: times 4 dw 0, GY
  76. ; rgba_Ucoeff_rb: times 4 dw RU, BU
  77. ; rgba_Ucoeff_br: times 4 dw BU, RU
  78. ; rgba_Ucoeff_ga: times 4 dw GU, 0
  79. ; rgba_Ucoeff_ag: times 4 dw 0, GU
  80. ; rgba_Vcoeff_rb: times 4 dw RV, BV
  81. ; rgba_Vcoeff_br: times 4 dw BV, RV
  82. ; rgba_Vcoeff_ga: times 4 dw GV, 0
  83. ; rgba_Vcoeff_ag: times 4 dw 0, GV
  84. shuf_rgb_12x4: db 0, 0x80, 1, 0x80, 2, 0x80, 3, 0x80, \
  85. 6, 0x80, 7, 0x80, 8, 0x80, 9, 0x80, \
  86. 0, 0x80, 1, 0x80, 2, 0x80, 3, 0x80, \
  87. 6, 0x80, 7, 0x80, 8, 0x80, 9, 0x80
  88. shuf_rgb_3x56: db 2, 0x80, 3, 0x80, 4, 0x80, 5, 0x80, \
  89. 8, 0x80, 9, 0x80, 10, 0x80, 11, 0x80, \
  90. 2, 0x80, 3, 0x80, 4, 0x80, 5, 0x80, \
  91. 8, 0x80, 9, 0x80, 10, 0x80, 11, 0x80
  92. pd_65535f: times 8 dd 65535.0
  93. pb_pack_shuffle16le: db 0, 1, 4, 5, \
  94. 8, 9, 12, 13, \
  95. -1, -1, -1, -1, \
  96. -1, -1, -1, -1, \
  97. -1, -1, -1, -1, \
  98. -1, -1, -1, -1, \
  99. 0, 1, 4, 5, \
  100. 8, 9, 12, 13
  101. pb_shuffle32be: db 3, 2, 1, 0, \
  102. 7, 6, 5, 4, \
  103. 11, 10, 9, 8, \
  104. 15, 14, 13, 12, \
  105. 3, 2, 1, 0, \
  106. 7, 6, 5, 4, \
  107. 11, 10, 9, 8, \
  108. 15, 14, 13, 12
  109. pb_shuffle16be: db 1, 0, 3, 2, \
  110. 5, 4, 7, 6, \
  111. 9, 8, 11, 10, \
  112. 13, 12, 15, 14, \
  113. 1, 0, 3, 2, \
  114. 5, 4, 7, 6, \
  115. 9, 8, 11, 10, \
  116. 13, 12, 15, 14
  117. SECTION .text
  118. ;-----------------------------------------------------------------------------
  119. ; RGB to Y/UV.
  120. ;
  121. ; void <fmt>ToY_<opt>(uint8_t *dst, const uint8_t *src, int w);
  122. ; and
  123. ; void <fmt>toUV_<opt>(uint8_t *dstU, uint8_t *dstV, const uint8_t *src,
  124. ; const uint8_t *unused, int w);
  125. ;-----------------------------------------------------------------------------
  126. ; %1 = nr. of XMM registers
  127. ; %2 = rgb or bgr
  128. %macro RGB24_TO_Y_FN 2-3
  129. cglobal %2 %+ 24ToY, 6, 6, %1, dst, src, u1, u2, w, table
  130. %if ARCH_X86_64
  131. %if mmsize == 32
  132. vbroadcasti128 m8, [%2_Ycoeff_12x4]
  133. vbroadcasti128 m9, [%2_Ycoeff_3x56]
  134. %else
  135. mova m8, [%2_Ycoeff_12x4]
  136. mova m9, [%2_Ycoeff_3x56]
  137. %endif
  138. %define coeff1 m8
  139. %define coeff2 m9
  140. %else ; x86-32
  141. %define coeff1 [%2_Ycoeff_12x4]
  142. %define coeff2 [%2_Ycoeff_3x56]
  143. %endif ; x86-32/64
  144. %if ARCH_X86_64 && %0 == 3
  145. jmp mangle(private_prefix %+ _ %+ %3 %+ 24ToY %+ SUFFIX).body
  146. %else ; ARCH_X86_64 && %0 == 3
  147. .body:
  148. %if cpuflag(ssse3)
  149. mova m7, [shuf_rgb_12x4]
  150. %define shuf_rgb1 m7
  151. %if ARCH_X86_64
  152. mova m10, [shuf_rgb_3x56]
  153. %define shuf_rgb2 m10
  154. %else ; x86-32
  155. %define shuf_rgb2 [shuf_rgb_3x56]
  156. %endif ; x86-32/64
  157. %endif ; cpuflag(ssse3)
  158. %if ARCH_X86_64
  159. movsxd wq, wd
  160. %endif
  161. add wq, wq
  162. add dstq, wq
  163. neg wq
  164. %if notcpuflag(ssse3)
  165. pxor m7, m7
  166. %endif ; !cpuflag(ssse3)
  167. %if mmsize == 32
  168. vbroadcasti128 m4, [rgb_Yrnd]
  169. %else
  170. mova m4, [rgb_Yrnd]
  171. %endif
  172. .loop:
  173. %if cpuflag(ssse3)
  174. movu xm0, [srcq+0] ; (byte) { Bx, Gx, Rx }[0-3]
  175. movu xm2, [srcq+12] ; (byte) { Bx, Gx, Rx }[4-7]
  176. %if mmsize == 32
  177. vinserti128 m0, m0, [srcq+24], 1
  178. vinserti128 m2, m2, [srcq+36], 1
  179. %endif
  180. pshufb m1, m0, shuf_rgb2 ; (word) { R0, B1, G1, R1, R2, B3, G3, R3 }
  181. pshufb m0, shuf_rgb1 ; (word) { B0, G0, R0, B1, B2, G2, R2, B3 }
  182. pshufb m3, m2, shuf_rgb2 ; (word) { R4, B5, G5, R5, R6, B7, G7, R7 }
  183. pshufb m2, shuf_rgb1 ; (word) { B4, G4, R4, B5, B6, G6, R6, B7 }
  184. %else ; !cpuflag(ssse3)
  185. movd m0, [srcq+0] ; (byte) { B0, G0, R0, B1 }
  186. movd m1, [srcq+2] ; (byte) { R0, B1, G1, R1 }
  187. movd m2, [srcq+6] ; (byte) { B2, G2, R2, B3 }
  188. movd m3, [srcq+8] ; (byte) { R2, B3, G3, R3 }
  189. punpckldq m0, m2 ; (byte) { B0, G0, R0, B1, B2, G2, R2, B3 }
  190. punpckldq m1, m3 ; (byte) { R0, B1, G1, R1, R2, B3, G3, R3 }
  191. movd m2, [srcq+12] ; (byte) { B4, G4, R4, B5 }
  192. movd m3, [srcq+14] ; (byte) { R4, B5, G5, R5 }
  193. movd m5, [srcq+18] ; (byte) { B6, G6, R6, B7 }
  194. movd m6, [srcq+20] ; (byte) { R6, B7, G7, R7 }
  195. punpckldq m2, m5 ; (byte) { B4, G4, R4, B5, B6, G6, R6, B7 }
  196. punpckldq m3, m6 ; (byte) { R4, B5, G5, R5, R6, B7, G7, R7 }
  197. punpcklbw m0, m7 ; (word) { B0, G0, R0, B1, B2, G2, R2, B3 }
  198. punpcklbw m1, m7 ; (word) { R0, B1, G1, R1, R2, B3, G3, R3 }
  199. punpcklbw m2, m7 ; (word) { B4, G4, R4, B5, B6, G6, R6, B7 }
  200. punpcklbw m3, m7 ; (word) { R4, B5, G5, R5, R6, B7, G7, R7 }
  201. %endif ; cpuflag(ssse3)
  202. add srcq, 3 * mmsize / 2
  203. pmaddwd m0, coeff1 ; (dword) { B0*BY + G0*GY, B1*BY, B2*BY + G2*GY, B3*BY }
  204. pmaddwd m1, coeff2 ; (dword) { R0*RY, G1+GY + R1*RY, R2*RY, G3+GY + R3*RY }
  205. pmaddwd m2, coeff1 ; (dword) { B4*BY + G4*GY, B5*BY, B6*BY + G6*GY, B7*BY }
  206. pmaddwd m3, coeff2 ; (dword) { R4*RY, G5+GY + R5*RY, R6*RY, G7+GY + R7*RY }
  207. paddd m0, m1 ; (dword) { Bx*BY + Gx*GY + Rx*RY }[0-3]
  208. paddd m2, m3 ; (dword) { Bx*BY + Gx*GY + Rx*RY }[4-7]
  209. paddd m0, m4 ; += rgb_Yrnd, i.e. (dword) { Y[0-3] }
  210. paddd m2, m4 ; += rgb_Yrnd, i.e. (dword) { Y[4-7] }
  211. psrad m0, 9
  212. psrad m2, 9
  213. packssdw m0, m2 ; (word) { Y[0-7] }
  214. mova [dstq+wq], m0
  215. add wq, mmsize
  216. jl .loop
  217. RET
  218. %endif ; ARCH_X86_64 && %0 == 3
  219. %endmacro
  220. ; %1 = nr. of XMM registers
  221. ; %2 = aligned/unaligned output argument
  222. ; %3-4 = rgb or bgr
  223. %macro RGB24_TO_UV_FN 3-4
  224. cglobal %3 %+ 24ToUV, 7, 7, %1, dstU, dstV, u1, src, u2, w, table
  225. %if ARCH_X86_64
  226. %if mmsize == 32
  227. vbroadcasti128 m8, [%3_Ucoeff_12x4]
  228. vbroadcasti128 m9, [%3_Ucoeff_3x56]
  229. vbroadcasti128 m10, [%3_Vcoeff_12x4]
  230. vbroadcasti128 m11, [%3_Vcoeff_3x56]
  231. %else
  232. mova m8, [%3_Ucoeff_12x4]
  233. mova m9, [%3_Ucoeff_3x56]
  234. mova m10, [%3_Vcoeff_12x4]
  235. mova m11, [%3_Vcoeff_3x56]
  236. %endif
  237. %define coeffU1 m8
  238. %define coeffU2 m9
  239. %define coeffV1 m10
  240. %define coeffV2 m11
  241. %else ; x86-32
  242. %define coeffU1 [%3_Ucoeff_12x4]
  243. %define coeffU2 [%3_Ucoeff_3x56]
  244. %define coeffV1 [%3_Vcoeff_12x4]
  245. %define coeffV2 [%3_Vcoeff_3x56]
  246. %endif ; x86-32/64
  247. %if ARCH_X86_64 && %0 == 4
  248. jmp mangle(private_prefix %+ _ %+ %4 %+ 24ToUV %+ SUFFIX).body
  249. %else ; ARCH_X86_64 && %0 == 4
  250. .body:
  251. %if cpuflag(ssse3)
  252. mova m7, [shuf_rgb_12x4]
  253. %define shuf_rgb1 m7
  254. %if ARCH_X86_64
  255. mova m12, [shuf_rgb_3x56]
  256. %define shuf_rgb2 m12
  257. %else ; x86-32
  258. %define shuf_rgb2 [shuf_rgb_3x56]
  259. %endif ; x86-32/64
  260. %endif ; cpuflag(ssse3)
  261. %if ARCH_X86_64
  262. movsxd wq, dword r5m
  263. %else ; x86-32
  264. mov wq, r5m
  265. %endif
  266. add wq, wq
  267. add dstUq, wq
  268. add dstVq, wq
  269. neg wq
  270. %if mmsize == 32
  271. vbroadcasti128 m6, [rgb_UVrnd]
  272. %else
  273. mova m6, [rgb_UVrnd]
  274. %endif
  275. %if notcpuflag(ssse3)
  276. pxor m7, m7
  277. %endif
  278. .loop:
  279. %if cpuflag(ssse3)
  280. movu xm0, [srcq+0] ; (byte) { Bx, Gx, Rx }[0-3]
  281. movu xm4, [srcq+12] ; (byte) { Bx, Gx, Rx }[4-7]
  282. %if mmsize == 32
  283. vinserti128 m0, m0, [srcq+24], 1
  284. vinserti128 m4, m4, [srcq+36], 1
  285. %endif
  286. pshufb m1, m0, shuf_rgb2 ; (word) { R0, B1, G1, R1, R2, B3, G3, R3 }
  287. pshufb m0, shuf_rgb1 ; (word) { B0, G0, R0, B1, B2, G2, R2, B3 }
  288. %else ; !cpuflag(ssse3)
  289. movd m0, [srcq+0] ; (byte) { B0, G0, R0, B1 }
  290. movd m1, [srcq+2] ; (byte) { R0, B1, G1, R1 }
  291. movd m4, [srcq+6] ; (byte) { B2, G2, R2, B3 }
  292. movd m5, [srcq+8] ; (byte) { R2, B3, G3, R3 }
  293. punpckldq m0, m4 ; (byte) { B0, G0, R0, B1, B2, G2, R2, B3 }
  294. punpckldq m1, m5 ; (byte) { R0, B1, G1, R1, R2, B3, G3, R3 }
  295. movd m4, [srcq+12] ; (byte) { B4, G4, R4, B5 }
  296. movd m5, [srcq+14] ; (byte) { R4, B5, G5, R5 }
  297. punpcklbw m0, m7 ; (word) { B0, G0, R0, B1, B2, G2, R2, B3 }
  298. punpcklbw m1, m7 ; (word) { R0, B1, G1, R1, R2, B3, G3, R3 }
  299. %endif ; cpuflag(ssse3)
  300. pmaddwd m2, m0, coeffV1 ; (dword) { B0*BV + G0*GV, B1*BV, B2*BV + G2*GV, B3*BV }
  301. pmaddwd m3, m1, coeffV2 ; (dword) { R0*BV, G1*GV + R1*BV, R2*BV, G3*GV + R3*BV }
  302. pmaddwd m0, coeffU1 ; (dword) { B0*BU + G0*GU, B1*BU, B2*BU + G2*GU, B3*BU }
  303. pmaddwd m1, coeffU2 ; (dword) { R0*BU, G1*GU + R1*BU, R2*BU, G3*GU + R3*BU }
  304. paddd m0, m1 ; (dword) { Bx*BU + Gx*GU + Rx*RU }[0-3]
  305. paddd m2, m3 ; (dword) { Bx*BV + Gx*GV + Rx*RV }[0-3]
  306. %if cpuflag(ssse3)
  307. pshufb m5, m4, shuf_rgb2 ; (word) { R4, B5, G5, R5, R6, B7, G7, R7 }
  308. pshufb m4, shuf_rgb1 ; (word) { B4, G4, R4, B5, B6, G6, R6, B7 }
  309. %else ; !cpuflag(ssse3)
  310. movd m1, [srcq+18] ; (byte) { B6, G6, R6, B7 }
  311. movd m3, [srcq+20] ; (byte) { R6, B7, G7, R7 }
  312. punpckldq m4, m1 ; (byte) { B4, G4, R4, B5, B6, G6, R6, B7 }
  313. punpckldq m5, m3 ; (byte) { R4, B5, G5, R5, R6, B7, G7, R7 }
  314. punpcklbw m4, m7 ; (word) { B4, G4, R4, B5, B6, G6, R6, B7 }
  315. punpcklbw m5, m7 ; (word) { R4, B5, G5, R5, R6, B7, G7, R7 }
  316. %endif ; cpuflag(ssse3)
  317. add srcq, 3 * mmsize / 2
  318. pmaddwd m1, m4, coeffU1 ; (dword) { B4*BU + G4*GU, B5*BU, B6*BU + G6*GU, B7*BU }
  319. pmaddwd m3, m5, coeffU2 ; (dword) { R4*BU, G5*GU + R5*BU, R6*BU, G7*GU + R7*BU }
  320. pmaddwd m4, coeffV1 ; (dword) { B4*BV + G4*GV, B5*BV, B6*BV + G6*GV, B7*BV }
  321. pmaddwd m5, coeffV2 ; (dword) { R4*BV, G5*GV + R5*BV, R6*BV, G7*GV + R7*BV }
  322. paddd m1, m3 ; (dword) { Bx*BU + Gx*GU + Rx*RU }[4-7]
  323. paddd m4, m5 ; (dword) { Bx*BV + Gx*GV + Rx*RV }[4-7]
  324. paddd m0, m6 ; += rgb_UVrnd, i.e. (dword) { U[0-3] }
  325. paddd m2, m6 ; += rgb_UVrnd, i.e. (dword) { V[0-3] }
  326. paddd m1, m6 ; += rgb_UVrnd, i.e. (dword) { U[4-7] }
  327. paddd m4, m6 ; += rgb_UVrnd, i.e. (dword) { V[4-7] }
  328. psrad m0, 9
  329. psrad m2, 9
  330. psrad m1, 9
  331. psrad m4, 9
  332. packssdw m0, m1 ; (word) { U[0-7] }
  333. packssdw m2, m4 ; (word) { V[0-7] }
  334. mov%2 [dstUq+wq], m0
  335. mov%2 [dstVq+wq], m2
  336. add wq, mmsize
  337. jl .loop
  338. RET
  339. %endif ; ARCH_X86_64 && %0 == 4
  340. %endmacro
  341. ; %1 = nr. of XMM registers for rgb-to-Y func
  342. ; %2 = nr. of XMM registers for rgb-to-UV func
  343. ; %3 = aligned/unaligned output argument
  344. %macro RGB24_FUNCS 3
  345. RGB24_TO_Y_FN %1, rgb
  346. RGB24_TO_Y_FN %1, bgr, rgb
  347. RGB24_TO_UV_FN %2, %3, rgb
  348. RGB24_TO_UV_FN %2, %3, bgr, rgb
  349. %endmacro
  350. INIT_XMM sse2
  351. RGB24_FUNCS 10, 12, a
  352. INIT_XMM ssse3
  353. RGB24_FUNCS 11, 13, a
  354. %if HAVE_AVX_EXTERNAL
  355. INIT_XMM avx
  356. RGB24_FUNCS 11, 13, a
  357. %endif
  358. %if ARCH_X86_64
  359. %if HAVE_AVX2_EXTERNAL
  360. INIT_YMM avx2
  361. RGB24_FUNCS 11, 13, u
  362. %endif
  363. %endif
  364. ; %1 = nr. of XMM registers
  365. ; %2-5 = rgba, bgra, argb or abgr (in individual characters)
  366. %macro RGB32_TO_Y_FN 5-6
  367. cglobal %2%3%4%5 %+ ToY, 6, 6, %1, dst, src, u1, u2, w, table
  368. %if mmsize == 32
  369. vbroadcasti128 m5, [rgba_Ycoeff_%2%4]
  370. vbroadcasti128 m6, [rgba_Ycoeff_%3%5]
  371. %else
  372. mova m5, [rgba_Ycoeff_%2%4]
  373. mova m6, [rgba_Ycoeff_%3%5]
  374. %endif
  375. %if %0 == 6
  376. jmp mangle(private_prefix %+ _ %+ %6 %+ ToY %+ SUFFIX).body
  377. %else ; %0 == 6
  378. .body:
  379. %if ARCH_X86_64
  380. movsxd wq, wd
  381. %endif
  382. add wq, wq
  383. sub wq, mmsize - 1
  384. lea srcq, [srcq+wq*2]
  385. add dstq, wq
  386. neg wq
  387. %if mmsize == 32
  388. vbroadcasti128 m4, [rgb_Yrnd]
  389. %else
  390. mova m4, [rgb_Yrnd]
  391. %endif
  392. pcmpeqb m7, m7
  393. psrlw m7, 8 ; (word) { 0x00ff } x4
  394. .loop:
  395. ; FIXME check alignment and use mova
  396. movu xm0, [srcq+wq*2+0] ; (byte) { Bx, Gx, Rx, xx }[0-3]
  397. movu xm2, [srcq+wq*2+16] ; (byte) { Bx, Gx, Rx, xx }[4-7]
  398. %if mmsize == 32
  399. vinserti128 m0, m0, [srcq+wq*2+32], 1
  400. vinserti128 m2, m2, [srcq+wq*2+48], 1
  401. %endif
  402. DEINTB 1, 0, 3, 2, 7 ; (word) { Gx, xx (m0/m2) or Bx, Rx (m1/m3) }[0-3]/[4-7]
  403. pmaddwd m1, m5 ; (dword) { Bx*BY + Rx*RY }[0-3]
  404. pmaddwd m0, m6 ; (dword) { Gx*GY }[0-3]
  405. pmaddwd m3, m5 ; (dword) { Bx*BY + Rx*RY }[4-7]
  406. pmaddwd m2, m6 ; (dword) { Gx*GY }[4-7]
  407. paddd m0, m4 ; += rgb_Yrnd
  408. paddd m2, m4 ; += rgb_Yrnd
  409. paddd m0, m1 ; (dword) { Y[0-3] }
  410. paddd m2, m3 ; (dword) { Y[4-7] }
  411. psrad m0, 9
  412. psrad m2, 9
  413. packssdw m0, m2 ; (word) { Y[0-7] }
  414. mova [dstq+wq], m0
  415. add wq, mmsize
  416. jl .loop
  417. sub wq, mmsize - 1
  418. jz .end
  419. add srcq, 2*mmsize - 2
  420. add dstq, mmsize - 1
  421. .loop2:
  422. INIT_XMM cpuname
  423. movd m0, [srcq+wq*2+0] ; (byte) { Bx, Gx, Rx, xx }[0-3]
  424. DEINTB 1, 0, 3, 2, 7 ; (word) { Gx, xx (m0/m2) or Bx, Rx (m1/m3) }[0-3]/[4-7]
  425. pmaddwd m1, m5 ; (dword) { Bx*BY + Rx*RY }[0-3]
  426. pmaddwd m0, m6 ; (dword) { Gx*GY }[0-3]
  427. paddd m0, m4 ; += rgb_Yrnd
  428. paddd m0, m1 ; (dword) { Y[0-3] }
  429. psrad m0, 9
  430. packssdw m0, m0 ; (word) { Y[0-7] }
  431. movd [dstq+wq], m0
  432. add wq, 2
  433. jl .loop2
  434. .end:
  435. %if cpuflag(avx2)
  436. INIT_YMM cpuname
  437. %endif
  438. RET
  439. %endif ; %0 == 3
  440. %endmacro
  441. ; %1 = nr. of XMM registers
  442. ; %2 = aligned/unaligned output argument
  443. ; %3-6 = rgba, bgra, argb or abgr (in individual characters)
  444. %macro RGB32_TO_UV_FN 6-7
  445. cglobal %3%4%5%6 %+ ToUV, 7, 7, %1, dstU, dstV, u1, src, u2, w, table
  446. %if ARCH_X86_64
  447. %if mmsize == 32
  448. vbroadcasti128 m8, [rgba_Ucoeff_%3%5]
  449. vbroadcasti128 m9, [rgba_Ucoeff_%4%6]
  450. vbroadcasti128 m10, [rgba_Vcoeff_%3%5]
  451. vbroadcasti128 m11, [rgba_Vcoeff_%4%6]
  452. %else
  453. mova m8, [rgba_Ucoeff_%3%5]
  454. mova m9, [rgba_Ucoeff_%4%6]
  455. mova m10, [rgba_Vcoeff_%3%5]
  456. mova m11, [rgba_Vcoeff_%4%6]
  457. %endif
  458. %define coeffU1 m8
  459. %define coeffU2 m9
  460. %define coeffV1 m10
  461. %define coeffV2 m11
  462. %else ; x86-32
  463. %define coeffU1 [rgba_Ucoeff_%3%5]
  464. %define coeffU2 [rgba_Ucoeff_%4%6]
  465. %define coeffV1 [rgba_Vcoeff_%3%5]
  466. %define coeffV2 [rgba_Vcoeff_%4%6]
  467. %endif ; x86-64/32
  468. %if ARCH_X86_64 && %0 == 7
  469. jmp mangle(private_prefix %+ _ %+ %7 %+ ToUV %+ SUFFIX).body
  470. %else ; ARCH_X86_64 && %0 == 7
  471. .body:
  472. %if ARCH_X86_64
  473. movsxd wq, dword r5m
  474. %else ; x86-32
  475. mov wq, r5m
  476. %endif
  477. add wq, wq
  478. sub wq, mmsize - 1
  479. add dstUq, wq
  480. add dstVq, wq
  481. lea srcq, [srcq+wq*2]
  482. neg wq
  483. pcmpeqb m7, m7
  484. psrlw m7, 8 ; (word) { 0x00ff } x4
  485. %if mmsize == 32
  486. vbroadcasti128 m6, [rgb_UVrnd]
  487. %else
  488. mova m6, [rgb_UVrnd]
  489. %endif
  490. .loop:
  491. ; FIXME check alignment and use mova
  492. movu xm0, [srcq+wq*2+0] ; (byte) { Bx, Gx, Rx, xx }[0-3]
  493. movu xm4, [srcq+wq*2+16] ; (byte) { Bx, Gx, Rx, xx }[4-7]
  494. %if mmsize == 32
  495. vinserti128 m0, m0, [srcq+wq*2+32], 1
  496. vinserti128 m4, m4, [srcq+wq*2+48], 1
  497. %endif
  498. DEINTB 1, 0, 5, 4, 7 ; (word) { Gx, xx (m0/m4) or Bx, Rx (m1/m5) }[0-3]/[4-7]
  499. pmaddwd m3, m1, coeffV1 ; (dword) { Bx*BV + Rx*RV }[0-3]
  500. pmaddwd m2, m0, coeffV2 ; (dword) { Gx*GV }[0-3]
  501. pmaddwd m1, coeffU1 ; (dword) { Bx*BU + Rx*RU }[0-3]
  502. pmaddwd m0, coeffU2 ; (dword) { Gx*GU }[0-3]
  503. paddd m3, m6 ; += rgb_UVrnd
  504. paddd m1, m6 ; += rgb_UVrnd
  505. paddd m2, m3 ; (dword) { V[0-3] }
  506. paddd m0, m1 ; (dword) { U[0-3] }
  507. pmaddwd m3, m5, coeffV1 ; (dword) { Bx*BV + Rx*RV }[4-7]
  508. pmaddwd m1, m4, coeffV2 ; (dword) { Gx*GV }[4-7]
  509. pmaddwd m5, coeffU1 ; (dword) { Bx*BU + Rx*RU }[4-7]
  510. pmaddwd m4, coeffU2 ; (dword) { Gx*GU }[4-7]
  511. paddd m3, m6 ; += rgb_UVrnd
  512. paddd m5, m6 ; += rgb_UVrnd
  513. psrad m0, 9
  514. paddd m1, m3 ; (dword) { V[4-7] }
  515. paddd m4, m5 ; (dword) { U[4-7] }
  516. psrad m2, 9
  517. psrad m4, 9
  518. psrad m1, 9
  519. packssdw m0, m4 ; (word) { U[0-7] }
  520. packssdw m2, m1 ; (word) { V[0-7] }
  521. ; FIXME check alignment and use mova
  522. mov%2 [dstUq+wq], m0
  523. mov%2 [dstVq+wq], m2
  524. add wq, mmsize
  525. jl .loop
  526. sub wq, mmsize - 1
  527. jz .end
  528. add srcq , 2*mmsize - 2
  529. add dstUq, mmsize - 1
  530. add dstVq, mmsize - 1
  531. .loop2:
  532. INIT_XMM cpuname
  533. movd m0, [srcq+wq*2] ; (byte) { Bx, Gx, Rx, xx }[0-3]
  534. DEINTB 1, 0, 5, 4, 7 ; (word) { Gx, xx (m0/m4) or Bx, Rx (m1/m5) }[0-3]/[4-7]
  535. pmaddwd m3, m1, coeffV1 ; (dword) { Bx*BV + Rx*RV }[0-3]
  536. pmaddwd m2, m0, coeffV2 ; (dword) { Gx*GV }[0-3]
  537. pmaddwd m1, coeffU1 ; (dword) { Bx*BU + Rx*RU }[0-3]
  538. pmaddwd m0, coeffU2 ; (dword) { Gx*GU }[0-3]
  539. paddd m3, m6 ; += rgb_UVrnd
  540. paddd m1, m6 ; += rgb_UVrnd
  541. paddd m2, m3 ; (dword) { V[0-3] }
  542. paddd m0, m1 ; (dword) { U[0-3] }
  543. psrad m0, 9
  544. psrad m2, 9
  545. packssdw m0, m0 ; (word) { U[0-7] }
  546. packssdw m2, m2 ; (word) { V[0-7] }
  547. movd [dstUq+wq], m0
  548. movd [dstVq+wq], m2
  549. add wq, 2
  550. jl .loop2
  551. .end:
  552. %if cpuflag(avx2)
  553. INIT_YMM cpuname
  554. %endif
  555. RET
  556. %endif ; ARCH_X86_64 && %0 == 7
  557. %endmacro
  558. ; %1 = nr. of XMM registers for rgb-to-Y func
  559. ; %2 = nr. of XMM registers for rgb-to-UV func
  560. ; %3 = aligned/unaligned output argument
  561. %macro RGB32_FUNCS 3
  562. RGB32_TO_Y_FN %1, r, g, b, a
  563. RGB32_TO_Y_FN %1, b, g, r, a, rgba
  564. RGB32_TO_Y_FN %1, a, r, g, b, rgba
  565. RGB32_TO_Y_FN %1, a, b, g, r, rgba
  566. RGB32_TO_UV_FN %2, %3, r, g, b, a
  567. RGB32_TO_UV_FN %2, %3, b, g, r, a, rgba
  568. RGB32_TO_UV_FN %2, %3, a, r, g, b, rgba
  569. RGB32_TO_UV_FN %2, %3, a, b, g, r, rgba
  570. %endmacro
  571. INIT_XMM sse2
  572. RGB32_FUNCS 8, 12, a
  573. %if HAVE_AVX_EXTERNAL
  574. INIT_XMM avx
  575. RGB32_FUNCS 8, 12, a
  576. %endif
  577. %if ARCH_X86_64
  578. %if HAVE_AVX2_EXTERNAL
  579. INIT_YMM avx2
  580. RGB32_FUNCS 8, 12, u
  581. %endif
  582. %endif
  583. ;-----------------------------------------------------------------------------
  584. ; YUYV/UYVY/NV12/NV21 packed pixel shuffling.
  585. ;
  586. ; void <fmt>ToY_<opt>(uint8_t *dst, const uint8_t *src, int w);
  587. ; and
  588. ; void <fmt>toUV_<opt>(uint8_t *dstU, uint8_t *dstV, const uint8_t *src,
  589. ; const uint8_t *unused, int w);
  590. ;-----------------------------------------------------------------------------
  591. ; %1 = a (aligned) or u (unaligned)
  592. ; %2 = yuyv or uyvy
  593. %macro LOOP_YUYV_TO_Y 2
  594. .loop_%1:
  595. mov%1 m0, [srcq+wq*2] ; (byte) { Y0, U0, Y1, V0, ... }
  596. mov%1 m1, [srcq+wq*2+mmsize] ; (byte) { Y8, U4, Y9, V4, ... }
  597. %ifidn %2, yuyv
  598. pand m0, m2 ; (word) { Y0, Y1, ..., Y7 }
  599. pand m1, m2 ; (word) { Y8, Y9, ..., Y15 }
  600. %else ; uyvy
  601. psrlw m0, 8 ; (word) { Y0, Y1, ..., Y7 }
  602. psrlw m1, 8 ; (word) { Y8, Y9, ..., Y15 }
  603. %endif ; yuyv/uyvy
  604. packuswb m0, m1 ; (byte) { Y0, ..., Y15 }
  605. mova [dstq+wq], m0
  606. add wq, mmsize
  607. jl .loop_%1
  608. RET
  609. %endmacro
  610. ; %1 = nr. of XMM registers
  611. ; %2 = yuyv or uyvy
  612. ; %3 = if specified, it means that unaligned and aligned code in loop
  613. ; will be the same (i.e. YUYV+AVX), and thus we don't need to
  614. ; split the loop in an aligned and unaligned case
  615. %macro YUYV_TO_Y_FN 2-3
  616. cglobal %2ToY, 5, 5, %1, dst, unused0, unused1, src, w
  617. %if ARCH_X86_64
  618. movsxd wq, wd
  619. %endif
  620. add dstq, wq
  621. test srcq, 15
  622. lea srcq, [srcq+wq*2]
  623. %ifidn %2, yuyv
  624. pcmpeqb m2, m2 ; (byte) { 0xff } x 16
  625. psrlw m2, 8 ; (word) { 0x00ff } x 8
  626. %endif ; yuyv
  627. jnz .loop_u_start
  628. neg wq
  629. LOOP_YUYV_TO_Y a, %2
  630. .loop_u_start:
  631. neg wq
  632. LOOP_YUYV_TO_Y u, %2
  633. %endmacro
  634. ; %1 = a (aligned) or u (unaligned)
  635. ; %2 = yuyv or uyvy
  636. %macro LOOP_YUYV_TO_UV 2
  637. .loop_%1:
  638. %ifidn %2, yuyv
  639. mov%1 m0, [srcq+wq*4] ; (byte) { Y0, U0, Y1, V0, ... }
  640. mov%1 m1, [srcq+wq*4+mmsize] ; (byte) { Y8, U4, Y9, V4, ... }
  641. psrlw m0, 8 ; (word) { U0, V0, ..., U3, V3 }
  642. psrlw m1, 8 ; (word) { U4, V4, ..., U7, V7 }
  643. %else ; uyvy
  644. %if cpuflag(avx)
  645. vpand m0, m2, [srcq+wq*4] ; (word) { U0, V0, ..., U3, V3 }
  646. vpand m1, m2, [srcq+wq*4+mmsize] ; (word) { U4, V4, ..., U7, V7 }
  647. %else
  648. mov%1 m0, [srcq+wq*4] ; (byte) { Y0, U0, Y1, V0, ... }
  649. mov%1 m1, [srcq+wq*4+mmsize] ; (byte) { Y8, U4, Y9, V4, ... }
  650. pand m0, m2 ; (word) { U0, V0, ..., U3, V3 }
  651. pand m1, m2 ; (word) { U4, V4, ..., U7, V7 }
  652. %endif
  653. %endif ; yuyv/uyvy
  654. packuswb m0, m1 ; (byte) { U0, V0, ..., U7, V7 }
  655. pand m1, m0, m2 ; (word) { U0, U1, ..., U7 }
  656. psrlw m0, 8 ; (word) { V0, V1, ..., V7 }
  657. packuswb m1, m0 ; (byte) { U0, ... U7, V1, ... V7 }
  658. movh [dstUq+wq], m1
  659. movhps [dstVq+wq], m1
  660. add wq, mmsize / 2
  661. jl .loop_%1
  662. RET
  663. %endmacro
  664. ; %1 = nr. of XMM registers
  665. ; %2 = yuyv or uyvy
  666. ; %3 = if specified, it means that unaligned and aligned code in loop
  667. ; will be the same (i.e. UYVY+AVX), and thus we don't need to
  668. ; split the loop in an aligned and unaligned case
  669. %macro YUYV_TO_UV_FN 2-3
  670. cglobal %2ToUV, 4, 5, %1, dstU, dstV, unused, src, w
  671. %if ARCH_X86_64
  672. movsxd wq, dword r5m
  673. %else ; x86-32
  674. mov wq, r5m
  675. %endif
  676. add dstUq, wq
  677. add dstVq, wq
  678. %if %0 == 2
  679. test srcq, 15
  680. %endif
  681. lea srcq, [srcq+wq*4]
  682. pcmpeqb m2, m2 ; (byte) { 0xff } x 16
  683. psrlw m2, 8 ; (word) { 0x00ff } x 8
  684. ; NOTE: if uyvy+avx, u/a are identical
  685. %if %0 == 2
  686. jnz .loop_u_start
  687. neg wq
  688. LOOP_YUYV_TO_UV a, %2
  689. .loop_u_start:
  690. neg wq
  691. LOOP_YUYV_TO_UV u, %2
  692. %else
  693. neg wq
  694. LOOP_YUYV_TO_UV a, %2
  695. %endif
  696. %endmacro
  697. ; %1 = a (aligned) or u (unaligned)
  698. ; %2 = nv12 or nv21
  699. %macro LOOP_NVXX_TO_UV 2
  700. .loop_%1:
  701. mov%1 m0, [srcq+wq*2] ; (byte) { U0, V0, U1, V1, ... }
  702. mov%1 m1, [srcq+wq*2+mmsize] ; (byte) { U8, V8, U9, V9, ... }
  703. pand m2, m0, m5 ; (word) { U0, U1, ..., U7 }
  704. pand m3, m1, m5 ; (word) { U8, U9, ..., U15 }
  705. psrlw m0, 8 ; (word) { V0, V1, ..., V7 }
  706. psrlw m1, 8 ; (word) { V8, V9, ..., V15 }
  707. packuswb m2, m3 ; (byte) { U0, ..., U15 }
  708. packuswb m0, m1 ; (byte) { V0, ..., V15 }
  709. %ifidn %2, nv12
  710. mov%1 [dstUq+wq], m2
  711. mov%1 [dstVq+wq], m0
  712. %else ; nv21
  713. mov%1 [dstVq+wq], m2
  714. mov%1 [dstUq+wq], m0
  715. %endif ; nv12/21
  716. add wq, mmsize
  717. jl .loop_%1
  718. RET
  719. %endmacro
  720. ; %1 = nr. of XMM registers
  721. ; %2 = nv12 or nv21
  722. %macro NVXX_TO_UV_FN 2
  723. cglobal %2ToUV, 4, 5, %1, dstU, dstV, tmp, src, w
  724. %if ARCH_X86_64
  725. movsxd wq, dword r5m
  726. %else ; x86-32
  727. mov wq, r5m
  728. %endif
  729. mov tmpq, srcq
  730. or tmpq, dstUq
  731. or tmpq, dstVq
  732. add dstUq, wq
  733. add dstVq, wq
  734. test tmpq, 15
  735. lea srcq, [srcq+wq*2]
  736. pcmpeqb m5, m5 ; (byte) { 0xff } x 16
  737. psrlw m5, 8 ; (word) { 0x00ff } x 8
  738. jnz .loop_u_start
  739. neg wq
  740. LOOP_NVXX_TO_UV a, %2
  741. .loop_u_start:
  742. neg wq
  743. LOOP_NVXX_TO_UV u, %2
  744. %endmacro
  745. INIT_XMM sse2
  746. YUYV_TO_Y_FN 3, yuyv
  747. YUYV_TO_Y_FN 2, uyvy
  748. YUYV_TO_UV_FN 3, yuyv
  749. YUYV_TO_UV_FN 3, uyvy
  750. NVXX_TO_UV_FN 5, nv12
  751. NVXX_TO_UV_FN 5, nv21
  752. %if HAVE_AVX_EXTERNAL
  753. INIT_XMM avx
  754. ; in theory, we could write a yuy2-to-y using vpand (i.e. AVX), but
  755. ; that's not faster in practice
  756. YUYV_TO_UV_FN 3, yuyv
  757. YUYV_TO_UV_FN 3, uyvy, 1
  758. NVXX_TO_UV_FN 5, nv12
  759. NVXX_TO_UV_FN 5, nv21
  760. %endif
  761. %if ARCH_X86_64
  762. %define RY_IDX 0
  763. %define GY_IDX 1
  764. %define BY_IDX 2
  765. %define RU_IDX 3
  766. %define GU_IDX 4
  767. %define BU_IDX 5
  768. %define RV_IDX 6
  769. %define GV_IDX 7
  770. %define BV_IDX 8
  771. %define RGB2YUV_SHIFT 15
  772. %define R m0
  773. %define G m1
  774. %define B m2
  775. %macro SWAP32 1
  776. %if mmsize > 16 || cpuflag(sse4)
  777. pshufb m%1, [pb_shuffle32be]
  778. %else
  779. psrlw xm7, xm%1, 8
  780. psllw xm%1, 8
  781. por xm%1, xm7
  782. pshuflw xm%1, xm%1, (2 << 6 | 3 << 4 | 0 << 2 | 1 << 0)
  783. pshufhw xm%1, xm%1, (2 << 6 | 3 << 4 | 0 << 2 | 1 << 0)
  784. %endif
  785. %endmacro
  786. ; 1 - dest
  787. ; 2 - source
  788. ; 3 - is big endian
  789. ; 4 - load only 2 values on sse2
  790. %macro LOADF32 4
  791. %if notcpuflag(sse4) && %4
  792. %if %3 ; big endian
  793. mov tmp1q, %2
  794. bswap tmp1q
  795. movq xm%1, tmp1q
  796. %else
  797. movq m%1, %2
  798. %endif
  799. %else
  800. movu m%1, %2
  801. %if %3
  802. SWAP32 %1
  803. %endif
  804. %endif
  805. maxps m%1, m9 ; 0.0 (nan, -inf) -> 0.0
  806. mulps m%1, m8 ; [pd_65535f]
  807. minps m%1, m8 ; +inf -> 65535
  808. ; cvtps2dq rounds to nearest int
  809. ; assuming mxcsr register is default rounding
  810. ; 0.40 -> 0.0, 0.50 -> 0.0, 0.51 -> 1.0
  811. cvtps2dq m%1, m%1
  812. %if notcpuflag(sse4) && %4
  813. ; line up the 2 values in lanes 0,2
  814. %if %3 ; big endian
  815. pshufd m%1, m%1, (3 << 6 | 0 << 4 | 2 << 2 | 1 << 0)
  816. %else
  817. pshufd m%1, m%1, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  818. %endif
  819. %endif
  820. %endmacro
  821. ; 1 - dest
  822. ; 2 - source
  823. ; 3 - is big endian
  824. %macro LOAD16 3
  825. %if cpuflag(sse4) || mmsize > 16
  826. pmovzxwd m%1, %2
  827. %if %3 ; bigendian
  828. pshufb m%1, m8 ; [pb_shuffle16be]
  829. %endif
  830. %else
  831. %if %3 ; bigendian
  832. mov tmp1d, dword %2
  833. bswap tmp1d
  834. movd xm%1, tmp1d
  835. pshuflw m%1, m%1, (3 << 6 | 0 << 4 | 3 << 2 | 1 << 0)
  836. pshufd m%1, m%1, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  837. %else
  838. movd xm%1, %2
  839. punpcklwd m%1, m9 ; interleave words with zero
  840. pshufd m%1, m%1, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  841. %endif
  842. %endif
  843. %endmacro
  844. %macro LOAD8_RGB 0
  845. %if cpuflag(sse4) || mmsize > 16
  846. pmovzxbd R, [srcRq + xq]
  847. pmovzxbd G, [srcGq + xq]
  848. pmovzxbd B, [srcBq + xq]
  849. %else
  850. ; thought this would be faster but from my measurments its not
  851. ; movd m0, [srcRq + xq + 0]; overeads by 2 bytes
  852. ; punpcklbw m0, m9 ; interleave bytes with zero
  853. ; punpcklwd m0, m9 ; interleave words with zero
  854. ; pshufd m0, m0, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  855. movzx tmp2q, byte [srcRq + xq + 1]
  856. movzx tmp1q, byte [srcRq + xq + 0]
  857. shl tmp2q, 32
  858. or tmp1q, tmp2q
  859. movq xm0, tmp1q
  860. movzx tmp2q, byte [srcGq + xq + 1]
  861. movzx tmp3q, byte [srcGq + xq + 0]
  862. shl tmp2q, 32
  863. or tmp3q, tmp2q
  864. movq xm1, tmp3q
  865. movzx tmp2q, byte [srcBq + xq + 1]
  866. movzx tmp1q, byte [srcBq + xq + 0]
  867. shl tmp2q, 32
  868. or tmp1q, tmp2q
  869. movq xm2, tmp1q
  870. pshufd m0, m0, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  871. pshufd m1, m1, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  872. pshufd m2, m2, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  873. %endif
  874. %endmacro
  875. ; 1 - dest
  876. ; 2 - source
  877. ; 3 - store only 2 values on sse2
  878. %macro STORE16 3
  879. %if %3 && notcpuflag(sse4)
  880. pshufd m%2, m%2, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  881. pshuflw m%2, m%2, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  882. movd %1, m%2
  883. %elif mmsize > 16
  884. pshufb m%2, m7 ; [pb_pack_shuffle16le]
  885. vpermq m%2, m%2, (3 << 6 | 0 << 4 | 3 << 2 | 0 << 0)
  886. movu %1, xm%2
  887. %else
  888. %if cpuflag(sse4)
  889. pshufb m%2, m7 ; [pb_pack_shuffle16le]
  890. %else
  891. pshuflw m%2, m%2, (1 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  892. pshufhw m%2, m%2, (1 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  893. pshufd m%2, m%2, (3 << 6 | 3 << 4 | 2 << 2 | 0 << 0)
  894. %endif
  895. movq %1, m%2
  896. %endif
  897. %endmacro
  898. %macro PMUL 3
  899. %if cpuflag(sse4) || mmsize > 16
  900. pmulld %1, %2, %3
  901. %else
  902. pmuludq %1, %2, %3
  903. %endif
  904. %endmacro
  905. ; 1 - name
  906. ; 2 - depth
  907. ; 3 - is big endian
  908. ; 4 - is float
  909. ; in sse2 mode only 2 values are done per loop, due to lack of pmulld instruction
  910. %macro planar_rgb_to_y_fn 4
  911. %if %2 == 8
  912. %define OFFSET (0x801<<(RGB2YUV_SHIFT-7))
  913. %define RSHIFT (RGB2YUV_SHIFT-6)
  914. %else
  915. %if %2 < 16
  916. %define SHIFT %2
  917. %define BPC %2
  918. %else
  919. %define SHIFT 14
  920. %define BPC 16
  921. %endif
  922. %define OFFSET ((16 << (RGB2YUV_SHIFT + BPC - 8)) + (1 << (RGB2YUV_SHIFT + SHIFT - 15)))
  923. %define RSHIFT (RGB2YUV_SHIFT + SHIFT - 14)
  924. %endif
  925. cglobal planar_%1_to_y, 4, 12, 13, dst, src, w, rgb2yuv, srcR, srcG, srcB, x, tmp1, tmp2, tmp3, tmp4
  926. VBROADCASTSS m10, dword [rgb2yuvq + RY_IDX*4] ; ry
  927. VBROADCASTSS m11, dword [rgb2yuvq + GY_IDX*4] ; gy
  928. VBROADCASTSS m12, dword [rgb2yuvq + BY_IDX*4] ; by
  929. pxor m9, m9
  930. %if %4
  931. movu m8, [pd_65535f]
  932. %endif
  933. %if cpuflag(sse4) || mmsize > 16
  934. movu m7, [pb_pack_shuffle16le]
  935. %if %3 && %2 > 8 && %2 <= 16
  936. movu m8, [pb_shuffle16be]
  937. %endif
  938. %endif
  939. mov xq, OFFSET
  940. movq xm6, xq
  941. VBROADCASTSS m6, xm6
  942. mov srcGq, [srcq + 0]
  943. mov srcBq, [srcq + 8]
  944. mov srcRq, [srcq + 16]
  945. xor xq, xq
  946. %%loop_x:
  947. %if %4
  948. LOADF32 0, [srcRq + xq*4], %3, 1
  949. LOADF32 1, [srcGq + xq*4], %3, 1
  950. LOADF32 2, [srcBq + xq*4], %3, 1
  951. %elif %2 == 8
  952. LOAD8_RGB
  953. %else
  954. LOAD16 0, [srcRq + xq*2], %3
  955. LOAD16 1, [srcGq + xq*2], %3
  956. LOAD16 2, [srcBq + xq*2], %3
  957. %endif
  958. PMUL R, R, m10 ; r*ry
  959. PMUL G, G, m11 ; g*gy
  960. PMUL B, B, m12 ; b*by
  961. paddd m0, m6 ; + OFFSET
  962. paddd B, G
  963. paddd m0, B
  964. psrad m0, RSHIFT
  965. STORE16 [dstq + 2*xq], 0, 1
  966. %if cpuflag(avx2) || cpuflag(sse4)
  967. add xq, mmsize/4
  968. %else
  969. add xd, 2
  970. %endif
  971. cmp xd, wd
  972. jl %%loop_x
  973. RET
  974. %endmacro
  975. ; 1 - name
  976. ; 2 - depth
  977. ; 3 - is big endian
  978. ; 4 - is float
  979. ; in sse2 mode only 2 values are done per loop, due to lack of pmulld instruction
  980. %macro planar_rgb_to_uv_fn 4
  981. %if %2 == 8
  982. %define OFFSET (0x4001<<(RGB2YUV_SHIFT-7))
  983. %define RSHIFT (RGB2YUV_SHIFT-6)
  984. %else
  985. %if %2 < 16
  986. %define SHIFT %2
  987. %define BPC %2
  988. %else
  989. %define SHIFT 14
  990. %define BPC 16
  991. %endif
  992. %define OFFSET ((128 << (RGB2YUV_SHIFT + BPC - 8)) + (1 << (RGB2YUV_SHIFT + SHIFT - 15)))
  993. %define RSHIFT (RGB2YUV_SHIFT + SHIFT - 14)
  994. %endif
  995. cglobal planar_%1_to_uv, 5, 12, 16, dstU, dstV, src, w, rgb2yuv, srcR, srcG, srcB, x, tmp1, tmp2, tmp3
  996. VBROADCASTSS m10, dword [rgb2yuvq + RU_IDX*4] ; ru
  997. VBROADCASTSS m11, dword [rgb2yuvq + GU_IDX*4] ; gu
  998. VBROADCASTSS m12, dword [rgb2yuvq + BU_IDX*4] ; bu
  999. VBROADCASTSS m13, dword [rgb2yuvq + RV_IDX*4] ; rv
  1000. VBROADCASTSS m14, dword [rgb2yuvq + GV_IDX*4] ; gv
  1001. VBROADCASTSS m15, dword [rgb2yuvq + BV_IDX*4] ; bv
  1002. pxor m9, m9
  1003. %if %4
  1004. movu m8, [pd_65535f]
  1005. %endif
  1006. %if cpuflag(sse4) || mmsize > 16
  1007. movu m7, [pb_pack_shuffle16le]
  1008. %if %3 && %2 > 8 && %2 <= 16
  1009. movu m8, [pb_shuffle16be]
  1010. %endif
  1011. %endif
  1012. mov xq, OFFSET
  1013. movq xm6, xq
  1014. VBROADCASTSS m6, xm6
  1015. mov srcGq, [srcq + 0]
  1016. mov srcBq, [srcq + 8]
  1017. mov srcRq, [srcq + 16]
  1018. xor xq, xq
  1019. %%loop_x:
  1020. %if %4
  1021. LOADF32 0, [srcRq + xq*4], %3, 1
  1022. LOADF32 1, [srcGq + xq*4], %3, 1
  1023. LOADF32 2, [srcBq + xq*4], %3, 1
  1024. %elif %2 == 8
  1025. LOAD8_RGB
  1026. %else
  1027. LOAD16 0, [srcRq + xq*2], %3
  1028. LOAD16 1, [srcGq + xq*2], %3
  1029. LOAD16 2, [srcBq + xq*2], %3
  1030. %endif
  1031. PMUL m5, R, m10 ; r*ru
  1032. PMUL m4, G, m11 ; b*gu
  1033. paddd m4, m5
  1034. PMUL m5, B, m12 ; b*bu
  1035. paddd m4, m6 ; + OFFSET
  1036. paddd m4, m5
  1037. psrad m4, RSHIFT
  1038. STORE16 [dstUq + 2*xq], 4, 1
  1039. PMUL R, R, m13 ; r*rv
  1040. PMUL G, G, m14 ; g*gv*g
  1041. PMUL B, B, m15 ; b*bv
  1042. paddd m0, m6 ; + OFFSET
  1043. paddd B, G
  1044. paddd m0, B
  1045. psrad m0, RSHIFT
  1046. STORE16 [dstVq + 2*xq], 0, 1
  1047. %if cpuflag(avx2) || cpuflag(sse4)
  1048. add xd, mmsize/4
  1049. %else
  1050. add xd, 2
  1051. %endif
  1052. cmp xd, wd
  1053. jl %%loop_x
  1054. RET
  1055. %endmacro
  1056. ; 1 - name
  1057. ; 2 - depth
  1058. ; 3 - is big endian
  1059. ; 4 - is float
  1060. %macro planar_rgb_to_a_fn 4
  1061. cglobal planar_%1_to_a, 4, 6, 10, dst, src, w, rgb2yuv, srcA, x
  1062. %if %4 && (cpuflag(sse4) || mmsize > 16)
  1063. movu m7, [pb_pack_shuffle16le]
  1064. %elif %3 && (cpuflag(sse4) || mmsize > 16)
  1065. movu m7, [pb_shuffle16be]
  1066. %endif
  1067. %if %4
  1068. movu m8, [pd_65535f]
  1069. %endif
  1070. pxor m9, m9
  1071. mov srcAq, [srcq + 24]
  1072. xor xq, xq
  1073. %%loop_x:
  1074. %if %4 ; float
  1075. LOADF32 0, [srcAq + xq*4], %3, 0
  1076. STORE16 [dstq + xq*2], 0, 0
  1077. add xq, mmsize/4
  1078. %elif %2 == 8
  1079. ; only need to convert 8bit value to 16bit
  1080. %if cpuflag(sse4) || mmsize > 16
  1081. pmovzxbw m0, [srcAq + xq]
  1082. %else
  1083. movsd m0, [srcAq + xq]
  1084. punpcklbw m0, m9 ; interleave bytes with zero
  1085. %endif
  1086. psllw m0, 6
  1087. movu [dstq + xq*2], m0
  1088. add xq, mmsize/2
  1089. %else
  1090. ; only need to convert 16bit format to 16le
  1091. movu m0, [srcAq + xq*2]
  1092. %if %3 ; bigendian
  1093. %if cpuflag(sse4) || mmsize > 16
  1094. pshufb m0, m7 ; [pb_shuffle16be]
  1095. %else
  1096. psrlw m7, m0, 8
  1097. psllw m0, 8
  1098. por m0, m7
  1099. %endif
  1100. %endif
  1101. %if %2 < 16
  1102. psllw m0, (14 - %2)
  1103. %endif
  1104. movu [dstq + xq*2], m0
  1105. add xq, mmsize/2
  1106. %endif
  1107. cmp xd, wd
  1108. jl %%loop_x
  1109. RET
  1110. %endmacro
  1111. ; 1 - name
  1112. ; 2 - depth
  1113. ; 3 - is float
  1114. %macro planer_rgbxx_y_fn_decl 3
  1115. planar_rgb_to_y_fn %1le, %2, 0, %3
  1116. planar_rgb_to_y_fn %1be, %2, 1, %3
  1117. %endmacro
  1118. ; 1 - name
  1119. ; 2 - depth
  1120. ; 3 - is float
  1121. %macro planer_rgbxx_uv_fn_decl 3
  1122. planar_rgb_to_uv_fn %1le, %2, 0, %3
  1123. planar_rgb_to_uv_fn %1be, %2, 1, %3
  1124. %endmacro
  1125. ; 1 - name
  1126. ; 2 - depth
  1127. ; 3 - is float
  1128. %macro planer_rgbxx_a_fn_decl 3
  1129. planar_rgb_to_a_fn %1le, %2, 0, %3
  1130. planar_rgb_to_a_fn %1be, %2, 1, %3
  1131. %endmacro
  1132. %macro planar_rgb_y_all_fn_decl 0
  1133. planar_rgb_to_y_fn rgb, 8, 0, 0
  1134. planer_rgbxx_y_fn_decl rgb9, 9, 0
  1135. planer_rgbxx_y_fn_decl rgb10, 10, 0
  1136. planer_rgbxx_y_fn_decl rgb12, 12, 0
  1137. planer_rgbxx_y_fn_decl rgb14, 14, 0
  1138. planer_rgbxx_y_fn_decl rgb16, 16, 0
  1139. planer_rgbxx_y_fn_decl rgbf32, 32, 1
  1140. %endmacro
  1141. %macro planar_rgb_uv_all_fn_decl 0
  1142. planar_rgb_to_uv_fn rgb, 8, 0, 0
  1143. planer_rgbxx_uv_fn_decl rgb9, 9, 0
  1144. planer_rgbxx_uv_fn_decl rgb10, 10, 0
  1145. planer_rgbxx_uv_fn_decl rgb12, 12, 0
  1146. planer_rgbxx_uv_fn_decl rgb14, 14, 0
  1147. planer_rgbxx_uv_fn_decl rgb16, 16, 0
  1148. planer_rgbxx_uv_fn_decl rgbf32, 32, 1
  1149. %endmacro
  1150. %macro planar_rgb_a_all_fn_decl 0
  1151. planar_rgb_to_a_fn rgb, 8, 0, 0
  1152. planer_rgbxx_a_fn_decl rgb10, 10, 0
  1153. planer_rgbxx_a_fn_decl rgb12, 12, 0
  1154. planer_rgbxx_a_fn_decl rgb16, 16, 0
  1155. planer_rgbxx_a_fn_decl rgbf32, 32, 1
  1156. %endmacro
  1157. ; sse2 to_y only matches c speed with current implementation
  1158. ; except on floating point formats
  1159. INIT_XMM sse2
  1160. planer_rgbxx_y_fn_decl rgbf32, 32, 1
  1161. planar_rgb_uv_all_fn_decl
  1162. planar_rgb_a_all_fn_decl
  1163. ; sse4 to_a conversions are just the sse2 ones
  1164. ; except on floating point formats
  1165. INIT_XMM sse4
  1166. planar_rgb_y_all_fn_decl
  1167. planar_rgb_uv_all_fn_decl
  1168. planer_rgbxx_a_fn_decl rgbf32, 32, 1
  1169. %if HAVE_AVX2_EXTERNAL
  1170. INIT_YMM avx2
  1171. planar_rgb_y_all_fn_decl
  1172. planar_rgb_uv_all_fn_decl
  1173. planar_rgb_a_all_fn_decl
  1174. %endif
  1175. %endif ; ARCH_X86_64