input.asm 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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. mova [dstUq+wq], m2
  711. mova [dstVq+wq], m0
  712. %else ; nv21
  713. mova [dstVq+wq], m2
  714. mova [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, unused, src, w
  724. %if ARCH_X86_64
  725. movsxd wq, dword r5m
  726. %else ; x86-32
  727. mov wq, r5m
  728. %endif
  729. add dstUq, wq
  730. add dstVq, wq
  731. test srcq, 15
  732. lea srcq, [srcq+wq*2]
  733. pcmpeqb m5, m5 ; (byte) { 0xff } x 16
  734. psrlw m5, 8 ; (word) { 0x00ff } x 8
  735. jnz .loop_u_start
  736. neg wq
  737. LOOP_NVXX_TO_UV a, %2
  738. .loop_u_start:
  739. neg wq
  740. LOOP_NVXX_TO_UV u, %2
  741. %endmacro
  742. INIT_XMM sse2
  743. YUYV_TO_Y_FN 3, yuyv
  744. YUYV_TO_Y_FN 2, uyvy
  745. YUYV_TO_UV_FN 3, yuyv
  746. YUYV_TO_UV_FN 3, uyvy
  747. NVXX_TO_UV_FN 5, nv12
  748. NVXX_TO_UV_FN 5, nv21
  749. %if HAVE_AVX_EXTERNAL
  750. INIT_XMM avx
  751. ; in theory, we could write a yuy2-to-y using vpand (i.e. AVX), but
  752. ; that's not faster in practice
  753. YUYV_TO_UV_FN 3, yuyv
  754. YUYV_TO_UV_FN 3, uyvy, 1
  755. NVXX_TO_UV_FN 5, nv12
  756. NVXX_TO_UV_FN 5, nv21
  757. %endif
  758. %if ARCH_X86_64
  759. %define RY_IDX 0
  760. %define GY_IDX 1
  761. %define BY_IDX 2
  762. %define RU_IDX 3
  763. %define GU_IDX 4
  764. %define BU_IDX 5
  765. %define RV_IDX 6
  766. %define GV_IDX 7
  767. %define BV_IDX 8
  768. %define RGB2YUV_SHIFT 15
  769. %define R m0
  770. %define G m1
  771. %define B m2
  772. %macro SWAP32 1
  773. %if mmsize > 16 || cpuflag(sse4)
  774. pshufb m%1, [pb_shuffle32be]
  775. %else
  776. psrlw xm7, xm%1, 8
  777. psllw xm%1, 8
  778. por xm%1, xm7
  779. pshuflw xm%1, xm%1, (2 << 6 | 3 << 4 | 0 << 2 | 1 << 0)
  780. pshufhw xm%1, xm%1, (2 << 6 | 3 << 4 | 0 << 2 | 1 << 0)
  781. %endif
  782. %endmacro
  783. ; 1 - dest
  784. ; 2 - source
  785. ; 3 - is big endian
  786. ; 4 - load only 2 values on sse2
  787. %macro LOADF32 4
  788. %if notcpuflag(sse4) && %4
  789. %if %3 ; big endian
  790. mov tmp1q, %2
  791. bswap tmp1q
  792. movq xm%1, tmp1q
  793. %else
  794. movq m%1, %2
  795. %endif
  796. %else
  797. movu m%1, %2
  798. %if %3
  799. SWAP32 %1
  800. %endif
  801. %endif
  802. maxps m%1, m9 ; 0.0 (nan, -inf) -> 0.0
  803. mulps m%1, m8 ; [pd_65535f]
  804. minps m%1, m8 ; +inf -> 65535
  805. ; cvtps2dq rounds to nearest int
  806. ; assuming mxcsr register is default rounding
  807. ; 0.40 -> 0.0, 0.50 -> 0.0, 0.51 -> 1.0
  808. cvtps2dq m%1, m%1
  809. %if notcpuflag(sse4) && %4
  810. ; line up the 2 values in lanes 0,2
  811. %if %3 ; big endian
  812. pshufd m%1, m%1, (3 << 6 | 0 << 4 | 2 << 2 | 1 << 0)
  813. %else
  814. pshufd m%1, m%1, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  815. %endif
  816. %endif
  817. %endmacro
  818. ; 1 - dest
  819. ; 2 - source
  820. ; 3 - is big endian
  821. %macro LOAD16 3
  822. %if cpuflag(sse4) || mmsize > 16
  823. pmovzxwd m%1, %2
  824. %if %3 ; bigendian
  825. pshufb m%1, m8 ; [pb_shuffle16be]
  826. %endif
  827. %else
  828. %if %3 ; bigendian
  829. mov tmp1d, dword %2
  830. bswap tmp1d
  831. movd xm%1, tmp1d
  832. pshuflw m%1, m%1, (3 << 6 | 0 << 4 | 3 << 2 | 1 << 0)
  833. pshufd m%1, m%1, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  834. %else
  835. movd xm%1, %2
  836. punpcklwd m%1, m9 ; interleave words with zero
  837. pshufd m%1, m%1, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  838. %endif
  839. %endif
  840. %endmacro
  841. %macro LOAD8_RGB 0
  842. %if cpuflag(sse4) || mmsize > 16
  843. pmovzxbd R, [srcRq + xq]
  844. pmovzxbd G, [srcGq + xq]
  845. pmovzxbd B, [srcBq + xq]
  846. %else
  847. ; thought this would be faster but from my measurments its not
  848. ; movd m0, [srcRq + xq + 0]; overeads by 2 bytes
  849. ; punpcklbw m0, m9 ; interleave bytes with zero
  850. ; punpcklwd m0, m9 ; interleave words with zero
  851. ; pshufd m0, m0, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  852. movzx tmp2q, byte [srcRq + xq + 1]
  853. movzx tmp1q, byte [srcRq + xq + 0]
  854. shl tmp2q, 32
  855. or tmp1q, tmp2q
  856. movq xm0, tmp1q
  857. movzx tmp2q, byte [srcGq + xq + 1]
  858. movzx tmp3q, byte [srcGq + xq + 0]
  859. shl tmp2q, 32
  860. or tmp3q, tmp2q
  861. movq xm1, tmp3q
  862. movzx tmp2q, byte [srcBq + xq + 1]
  863. movzx tmp1q, byte [srcBq + xq + 0]
  864. shl tmp2q, 32
  865. or tmp1q, tmp2q
  866. movq xm2, tmp1q
  867. pshufd m0, m0, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  868. pshufd m1, m1, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  869. pshufd m2, m2, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  870. %endif
  871. %endmacro
  872. ; 1 - dest
  873. ; 2 - source
  874. ; 3 - store only 2 values on sse2
  875. %macro STORE16 3
  876. %if %3 && notcpuflag(sse4)
  877. pshufd m%2, m%2, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  878. pshuflw m%2, m%2, (3 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  879. movd %1, m%2
  880. %elif mmsize > 16
  881. pshufb m%2, m7 ; [pb_pack_shuffle16le]
  882. vpermq m%2, m%2, (3 << 6 | 0 << 4 | 3 << 2 | 0 << 0)
  883. movu %1, xm%2
  884. %else
  885. %if cpuflag(sse4)
  886. pshufb m%2, m7 ; [pb_pack_shuffle16le]
  887. %else
  888. pshuflw m%2, m%2, (1 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  889. pshufhw m%2, m%2, (1 << 6 | 1 << 4 | 2 << 2 | 0 << 0)
  890. pshufd m%2, m%2, (3 << 6 | 3 << 4 | 2 << 2 | 0 << 0)
  891. %endif
  892. movq %1, m%2
  893. %endif
  894. %endmacro
  895. %macro PMUL 3
  896. %if cpuflag(sse4) || mmsize > 16
  897. pmulld %1, %2, %3
  898. %else
  899. pmuludq %1, %2, %3
  900. %endif
  901. %endmacro
  902. ; 1 - name
  903. ; 2 - depth
  904. ; 3 - is big endian
  905. ; 4 - is float
  906. ; in sse2 mode only 2 values are done per loop, due to lack of pmulld instruction
  907. %macro planar_rgb_to_y_fn 4
  908. %if %2 == 8
  909. %define OFFSET (0x801<<(RGB2YUV_SHIFT-7))
  910. %define RSHIFT (RGB2YUV_SHIFT-6)
  911. %else
  912. %if %2 < 16
  913. %define SHIFT %2
  914. %define BPC %2
  915. %else
  916. %define SHIFT 14
  917. %define BPC 16
  918. %endif
  919. %define OFFSET ((16 << (RGB2YUV_SHIFT + BPC - 8)) + (1 << (RGB2YUV_SHIFT + SHIFT - 15)))
  920. %define RSHIFT (RGB2YUV_SHIFT + SHIFT - 14)
  921. %endif
  922. cglobal planar_%1_to_y, 4, 12, 13, dst, src, w, rgb2yuv, srcR, srcG, srcB, x, tmp1, tmp2, tmp3, tmp4
  923. VBROADCASTSS m10, dword [rgb2yuvq + RY_IDX*4] ; ry
  924. VBROADCASTSS m11, dword [rgb2yuvq + GY_IDX*4] ; gy
  925. VBROADCASTSS m12, dword [rgb2yuvq + BY_IDX*4] ; by
  926. pxor m9, m9
  927. %if %4
  928. movu m8, [pd_65535f]
  929. %endif
  930. %if cpuflag(sse4) || mmsize > 16
  931. movu m7, [pb_pack_shuffle16le]
  932. %if %3 && %2 > 8 && %2 <= 16
  933. movu m8, [pb_shuffle16be]
  934. %endif
  935. %endif
  936. mov xq, OFFSET
  937. movq xm6, xq
  938. VBROADCASTSS m6, xm6
  939. mov srcGq, [srcq + 0]
  940. mov srcBq, [srcq + 8]
  941. mov srcRq, [srcq + 16]
  942. xor xq, xq
  943. %%loop_x:
  944. %if %4
  945. LOADF32 0, [srcRq + xq*4], %3, 1
  946. LOADF32 1, [srcGq + xq*4], %3, 1
  947. LOADF32 2, [srcBq + xq*4], %3, 1
  948. %elif %2 == 8
  949. LOAD8_RGB
  950. %else
  951. LOAD16 0, [srcRq + xq*2], %3
  952. LOAD16 1, [srcGq + xq*2], %3
  953. LOAD16 2, [srcBq + xq*2], %3
  954. %endif
  955. PMUL R, R, m10 ; r*ry
  956. PMUL G, G, m11 ; g*gy
  957. PMUL B, B, m12 ; b*by
  958. paddd m0, m6 ; + OFFSET
  959. paddd B, G
  960. paddd m0, B
  961. psrad m0, RSHIFT
  962. STORE16 [dstq + 2*xq], 0, 1
  963. %if cpuflag(avx2) || cpuflag(sse4)
  964. add xq, mmsize/4
  965. %else
  966. add xd, 2
  967. %endif
  968. cmp xd, wd
  969. jl %%loop_x
  970. RET
  971. %endmacro
  972. ; 1 - name
  973. ; 2 - depth
  974. ; 3 - is big endian
  975. ; 4 - is float
  976. ; in sse2 mode only 2 values are done per loop, due to lack of pmulld instruction
  977. %macro planar_rgb_to_uv_fn 4
  978. %if %2 == 8
  979. %define OFFSET (0x4001<<(RGB2YUV_SHIFT-7))
  980. %define RSHIFT (RGB2YUV_SHIFT-6)
  981. %else
  982. %if %2 < 16
  983. %define SHIFT %2
  984. %define BPC %2
  985. %else
  986. %define SHIFT 14
  987. %define BPC 16
  988. %endif
  989. %define OFFSET ((128 << (RGB2YUV_SHIFT + BPC - 8)) + (1 << (RGB2YUV_SHIFT + SHIFT - 15)))
  990. %define RSHIFT (RGB2YUV_SHIFT + SHIFT - 14)
  991. %endif
  992. cglobal planar_%1_to_uv, 5, 12, 16, dstU, dstV, src, w, rgb2yuv, srcR, srcG, srcB, x, tmp1, tmp2, tmp3
  993. VBROADCASTSS m10, dword [rgb2yuvq + RU_IDX*4] ; ru
  994. VBROADCASTSS m11, dword [rgb2yuvq + GU_IDX*4] ; gu
  995. VBROADCASTSS m12, dword [rgb2yuvq + BU_IDX*4] ; bu
  996. VBROADCASTSS m13, dword [rgb2yuvq + RV_IDX*4] ; rv
  997. VBROADCASTSS m14, dword [rgb2yuvq + GV_IDX*4] ; gv
  998. VBROADCASTSS m15, dword [rgb2yuvq + BV_IDX*4] ; bv
  999. pxor m9, m9
  1000. %if %4
  1001. movu m8, [pd_65535f]
  1002. %endif
  1003. %if cpuflag(sse4) || mmsize > 16
  1004. movu m7, [pb_pack_shuffle16le]
  1005. %if %3 && %2 > 8 && %2 <= 16
  1006. movu m8, [pb_shuffle16be]
  1007. %endif
  1008. %endif
  1009. mov xq, OFFSET
  1010. movq xm6, xq
  1011. VBROADCASTSS m6, xm6
  1012. mov srcGq, [srcq + 0]
  1013. mov srcBq, [srcq + 8]
  1014. mov srcRq, [srcq + 16]
  1015. xor xq, xq
  1016. %%loop_x:
  1017. %if %4
  1018. LOADF32 0, [srcRq + xq*4], %3, 1
  1019. LOADF32 1, [srcGq + xq*4], %3, 1
  1020. LOADF32 2, [srcBq + xq*4], %3, 1
  1021. %elif %2 == 8
  1022. LOAD8_RGB
  1023. %else
  1024. LOAD16 0, [srcRq + xq*2], %3
  1025. LOAD16 1, [srcGq + xq*2], %3
  1026. LOAD16 2, [srcBq + xq*2], %3
  1027. %endif
  1028. PMUL m5, R, m10 ; r*ru
  1029. PMUL m4, G, m11 ; b*gu
  1030. paddd m4, m5
  1031. PMUL m5, B, m12 ; b*bu
  1032. paddd m4, m6 ; + OFFSET
  1033. paddd m4, m5
  1034. psrad m4, RSHIFT
  1035. STORE16 [dstUq + 2*xq], 4, 1
  1036. PMUL R, R, m13 ; r*rv
  1037. PMUL G, G, m14 ; g*gv*g
  1038. PMUL B, B, m15 ; b*bv
  1039. paddd m0, m6 ; + OFFSET
  1040. paddd B, G
  1041. paddd m0, B
  1042. psrad m0, RSHIFT
  1043. STORE16 [dstVq + 2*xq], 0, 1
  1044. %if cpuflag(avx2) || cpuflag(sse4)
  1045. add xd, mmsize/4
  1046. %else
  1047. add xd, 2
  1048. %endif
  1049. cmp xd, wd
  1050. jl %%loop_x
  1051. RET
  1052. %endmacro
  1053. ; 1 - name
  1054. ; 2 - depth
  1055. ; 3 - is big endian
  1056. ; 4 - is float
  1057. %macro planar_rgb_to_a_fn 4
  1058. cglobal planar_%1_to_a, 4, 6, 10, dst, src, w, rgb2yuv, srcA, x
  1059. %if %4 && (cpuflag(sse4) || mmsize > 16)
  1060. movu m7, [pb_pack_shuffle16le]
  1061. %elif %3 && (cpuflag(sse4) || mmsize > 16)
  1062. movu m7, [pb_shuffle16be]
  1063. %endif
  1064. %if %4
  1065. movu m8, [pd_65535f]
  1066. %endif
  1067. pxor m9, m9
  1068. mov srcAq, [srcq + 24]
  1069. xor xq, xq
  1070. %%loop_x:
  1071. %if %4 ; float
  1072. LOADF32 0, [srcAq + xq*4], %3, 0
  1073. STORE16 [dstq + xq*2], 0, 0
  1074. add xq, mmsize/4
  1075. %elif %2 == 8
  1076. ; only need to convert 8bit value to 16bit
  1077. %if cpuflag(sse4) || mmsize > 16
  1078. pmovzxbw m0, [srcAq + xq]
  1079. %else
  1080. movsd m0, [srcAq + xq]
  1081. punpcklbw m0, m9 ; interleave bytes with zero
  1082. %endif
  1083. psllw m0, 6
  1084. movu [dstq + xq*2], m0
  1085. add xq, mmsize/2
  1086. %else
  1087. ; only need to convert 16bit format to 16le
  1088. movu m0, [srcAq + xq*2]
  1089. %if %3 ; bigendian
  1090. %if cpuflag(sse4) || mmsize > 16
  1091. pshufb m0, m7 ; [pb_shuffle16be]
  1092. %else
  1093. psrlw m7, m0, 8
  1094. psllw m0, 8
  1095. por m0, m7
  1096. %endif
  1097. %endif
  1098. %if %2 < 16
  1099. psllw m0, (14 - %2)
  1100. %endif
  1101. movu [dstq + xq*2], m0
  1102. add xq, mmsize/2
  1103. %endif
  1104. cmp xd, wd
  1105. jl %%loop_x
  1106. RET
  1107. %endmacro
  1108. ; 1 - name
  1109. ; 2 - depth
  1110. ; 3 - is float
  1111. %macro planer_rgbxx_y_fn_decl 3
  1112. planar_rgb_to_y_fn %1le, %2, 0, %3
  1113. planar_rgb_to_y_fn %1be, %2, 1, %3
  1114. %endmacro
  1115. ; 1 - name
  1116. ; 2 - depth
  1117. ; 3 - is float
  1118. %macro planer_rgbxx_uv_fn_decl 3
  1119. planar_rgb_to_uv_fn %1le, %2, 0, %3
  1120. planar_rgb_to_uv_fn %1be, %2, 1, %3
  1121. %endmacro
  1122. ; 1 - name
  1123. ; 2 - depth
  1124. ; 3 - is float
  1125. %macro planer_rgbxx_a_fn_decl 3
  1126. planar_rgb_to_a_fn %1le, %2, 0, %3
  1127. planar_rgb_to_a_fn %1be, %2, 1, %3
  1128. %endmacro
  1129. %macro planar_rgb_y_all_fn_decl 0
  1130. planar_rgb_to_y_fn rgb, 8, 0, 0
  1131. planer_rgbxx_y_fn_decl rgb9, 9, 0
  1132. planer_rgbxx_y_fn_decl rgb10, 10, 0
  1133. planer_rgbxx_y_fn_decl rgb12, 12, 0
  1134. planer_rgbxx_y_fn_decl rgb14, 14, 0
  1135. planer_rgbxx_y_fn_decl rgb16, 16, 0
  1136. planer_rgbxx_y_fn_decl rgbf32, 32, 1
  1137. %endmacro
  1138. %macro planar_rgb_uv_all_fn_decl 0
  1139. planar_rgb_to_uv_fn rgb, 8, 0, 0
  1140. planer_rgbxx_uv_fn_decl rgb9, 9, 0
  1141. planer_rgbxx_uv_fn_decl rgb10, 10, 0
  1142. planer_rgbxx_uv_fn_decl rgb12, 12, 0
  1143. planer_rgbxx_uv_fn_decl rgb14, 14, 0
  1144. planer_rgbxx_uv_fn_decl rgb16, 16, 0
  1145. planer_rgbxx_uv_fn_decl rgbf32, 32, 1
  1146. %endmacro
  1147. %macro planar_rgb_a_all_fn_decl 0
  1148. planar_rgb_to_a_fn rgb, 8, 0, 0
  1149. planer_rgbxx_a_fn_decl rgb10, 10, 0
  1150. planer_rgbxx_a_fn_decl rgb12, 12, 0
  1151. planer_rgbxx_a_fn_decl rgb16, 16, 0
  1152. planer_rgbxx_a_fn_decl rgbf32, 32, 1
  1153. %endmacro
  1154. ; sse2 to_y only matches c speed with current implementation
  1155. ; except on floating point formats
  1156. INIT_XMM sse2
  1157. planer_rgbxx_y_fn_decl rgbf32, 32, 1
  1158. planar_rgb_uv_all_fn_decl
  1159. planar_rgb_a_all_fn_decl
  1160. ; sse4 to_a conversions are just the sse2 ones
  1161. ; except on floating point formats
  1162. INIT_XMM sse4
  1163. planar_rgb_y_all_fn_decl
  1164. planar_rgb_uv_all_fn_decl
  1165. planer_rgbxx_a_fn_decl rgbf32, 32, 1
  1166. %if HAVE_AVX2_EXTERNAL
  1167. INIT_YMM avx2
  1168. planar_rgb_y_all_fn_decl
  1169. planar_rgb_uv_all_fn_decl
  1170. planar_rgb_a_all_fn_decl
  1171. %endif
  1172. %endif ; ARCH_X86_64