yuv_2_rgb.asm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. ;******************************************************************************
  2. ;* software YUV to RGB converter
  3. ;*
  4. ;* Copyright (C) 2001-2007 Michael Niedermayer
  5. ;* (c) 2010 Konstantin Shishkov
  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
  25. ; below variables are named like mask_dwXY, which means to preserve dword No.X & No.Y
  26. mask_dw036 : db -1, -1, 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, -1, -1, 0, 0
  27. mask_dw147 : db 0, 0, -1, -1, 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, -1, -1
  28. mask_dw25 : db 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, -1, -1, 0, 0, 0, 0
  29. rgb24_shuf1: db 0, 1, 6, 7, 12, 13, 2, 3, 8, 9, 14, 15, 4, 5, 10, 11
  30. rgb24_shuf2: db 10, 11, 0, 1, 6, 7, 12, 13, 2, 3, 8, 9, 14, 15, 4, 5
  31. rgb24_shuf3: db 4, 5, 10, 11, 0, 1, 6, 7, 12, 13, 2, 3, 8, 9, 14, 15
  32. pw_00ff: times 8 dw 255
  33. pb_f8: times 16 db 248
  34. pb_e0: times 16 db 224
  35. pb_03: times 16 db 3
  36. pb_07: times 16 db 7
  37. mask_1101: dw -1, -1, 0, -1
  38. mask_0010: dw 0, 0, -1, 0
  39. mask_0110: dw 0, -1, -1, 0
  40. mask_1001: dw -1, 0, 0, -1
  41. mask_0100: dw 0, -1, 0, 0
  42. SECTION .text
  43. ;-----------------------------------------------------------------------------
  44. ;
  45. ; YUV420/YUVA420 to RGB/BGR 15/16/24/32
  46. ; R = Y + ((vrCoff * (v - 128)) >> 8)
  47. ; G = Y - ((ugCoff * (u - 128) + vgCoff * (v - 128)) >> 8)
  48. ; B = Y + ((ubCoff * (u - 128)) >> 8)
  49. ;
  50. ;-----------------------------------------------------------------------------
  51. %macro MOV_H2L 1
  52. %if mmsize == 8
  53. psrlq %1, 32
  54. %else ; mmsize == 16
  55. psrldq %1, 8
  56. %endif
  57. %endmacro
  58. %macro yuv2rgb_fn 3
  59. %if %3 == 32
  60. %ifidn %1, yuva
  61. %define parameters index, image, pu_index, pv_index, pointer_c_dither, py_2index, pa_2index
  62. %define GPR_num 7
  63. %else
  64. %define parameters index, image, pu_index, pv_index, pointer_c_dither, py_2index
  65. %define GPR_num 6
  66. %endif
  67. %else
  68. %define parameters index, image, pu_index, pv_index, pointer_c_dither, py_2index
  69. %define GPR_num 6
  70. %endif
  71. %define m_green m2
  72. %define m_alpha m3
  73. %define m_y m6
  74. %define m_u m0
  75. %define m_v m1
  76. %ifidn %2, rgb
  77. %define m_red m1
  78. %define m_blue m0
  79. %else
  80. %define m_red m0
  81. %define m_blue m1
  82. %endif
  83. %if mmsize == 8
  84. %define time_num 1
  85. %define reg_num 8
  86. %define y_offset [pointer_c_ditherq + 8 * 8]
  87. %define u_offset [pointer_c_ditherq + 9 * 8]
  88. %define v_offset [pointer_c_ditherq + 10 * 8]
  89. %define ug_coff [pointer_c_ditherq + 7 * 8]
  90. %define vg_coff [pointer_c_ditherq + 6 * 8]
  91. %define y_coff [pointer_c_ditherq + 3 * 8]
  92. %define ub_coff [pointer_c_ditherq + 5 * 8]
  93. %define vr_coff [pointer_c_ditherq + 4 * 8]
  94. %elif mmsize == 16
  95. %define time_num 2
  96. %if ARCH_X86_32
  97. %define reg_num 8
  98. %define my_offset [pointer_c_ditherq + 8 * 8]
  99. %define mu_offset [pointer_c_ditherq + 9 * 8]
  100. %define mv_offset [pointer_c_ditherq + 10 * 8]
  101. %define mug_coff [pointer_c_ditherq + 7 * 8]
  102. %define mvg_coff [pointer_c_ditherq + 6 * 8]
  103. %define my_coff [pointer_c_ditherq + 3 * 8]
  104. %define mub_coff [pointer_c_ditherq + 5 * 8]
  105. %define mvr_coff [pointer_c_ditherq + 4 * 8]
  106. %else ; ARCH_X86_64
  107. %define reg_num 16
  108. %define y_offset m8
  109. %define u_offset m9
  110. %define v_offset m10
  111. %define ug_coff m11
  112. %define vg_coff m12
  113. %define y_coff m13
  114. %define ub_coff m14
  115. %define vr_coff m15
  116. %endif ; ARCH_X86_32/64
  117. %endif ; coeff define mmsize == 8/16
  118. cglobal %1_420_%2%3, GPR_num, GPR_num, reg_num, parameters
  119. %if ARCH_X86_64
  120. movsxd indexq, indexd
  121. %if mmsize == 16
  122. VBROADCASTSD y_offset, [pointer_c_ditherq + 8 * 8]
  123. VBROADCASTSD u_offset, [pointer_c_ditherq + 9 * 8]
  124. VBROADCASTSD v_offset, [pointer_c_ditherq + 10 * 8]
  125. VBROADCASTSD ug_coff, [pointer_c_ditherq + 7 * 8]
  126. VBROADCASTSD vg_coff, [pointer_c_ditherq + 6 * 8]
  127. VBROADCASTSD y_coff, [pointer_c_ditherq + 3 * 8]
  128. VBROADCASTSD ub_coff, [pointer_c_ditherq + 5 * 8]
  129. VBROADCASTSD vr_coff, [pointer_c_ditherq + 4 * 8]
  130. %endif
  131. %endif
  132. .loop0:
  133. movu m_y, [py_2indexq + 2 * indexq]
  134. movh m_u, [pu_indexq + indexq]
  135. movh m_v, [pv_indexq + indexq]
  136. pxor m4, m4
  137. mova m7, m6
  138. punpcklbw m0, m4
  139. punpcklbw m1, m4
  140. mova m2, [pw_00ff]
  141. pand m6, m2
  142. psrlw m7, 8
  143. psllw m0, 3
  144. psllw m1, 3
  145. psllw m6, 3
  146. psllw m7, 3
  147. %if (ARCH_X86_32 && mmsize == 16)
  148. VBROADCASTSD m2, mu_offset
  149. VBROADCASTSD m3, mv_offset
  150. VBROADCASTSD m4, my_offset
  151. psubsw m0, m2 ; U = U - 128
  152. psubsw m1, m3 ; V = V - 128
  153. psubw m6, m4
  154. psubw m7, m4
  155. VBROADCASTSD m2, mug_coff
  156. VBROADCASTSD m3, mvg_coff
  157. VBROADCASTSD m4, my_coff
  158. VBROADCASTSD m5, mub_coff
  159. pmulhw m2, m0
  160. pmulhw m3, m1
  161. pmulhw m6, m4
  162. pmulhw m7, m4
  163. pmulhw m0, m5
  164. VBROADCASTSD m4, mvr_coff
  165. pmulhw m1, m4
  166. %else ; ARCH_X86_64 || mmsize == 8
  167. psubsw m0, u_offset ; U = U - 128
  168. psubsw m1, v_offset ; V = V - 128
  169. psubw m6, y_offset
  170. psubw m7, y_offset
  171. mova m2, m0
  172. mova m3, m1
  173. pmulhw m2, ug_coff
  174. pmulhw m3, vg_coff
  175. pmulhw m6, y_coff
  176. pmulhw m7, y_coff
  177. pmulhw m0, ub_coff
  178. pmulhw m1, vr_coff
  179. %endif
  180. paddsw m2, m3
  181. mova m3, m7
  182. mova m5, m7
  183. paddsw m3, m0 ; B1 B3 B5 B7 ...
  184. paddsw m5, m1 ; R1 R3 R5 R7 ...
  185. paddsw m7, m2 ; G1 G3 G4 G7 ...
  186. paddsw m0, m6 ; B0 B2 B4 B6 ...
  187. paddsw m1, m6 ; R0 R2 R4 R6 ...
  188. paddsw m2, m6 ; G0 G2 G4 G6 ...
  189. %if %3 == 24 ; PACK RGB24
  190. %define depth 3
  191. packuswb m0, m3 ; R0 R2 R4 R6 ... R1 R3 R5 R7 ...
  192. packuswb m1, m5 ; B0 B2 B4 B6 ... B1 B3 B5 B7 ...
  193. packuswb m2, m7 ; G0 G2 G4 G6 ... G1 G3 G5 G7 ...
  194. mova m3, m_red
  195. mova m6, m_blue
  196. MOV_H2L m_red
  197. punpcklbw m3, m2 ; R0 G0 R2 G2 R4 G4 R6 G6 R8 G8 ...
  198. punpcklbw m6, m_red ; B0 R1 B2 R3 B4 R5 B6 R7 B8 R9 ...
  199. mova m5, m3
  200. punpckhbw m2, m_blue ; G1 B1 G3 B3 G5 B5 G7 B7 G9 B9 ...
  201. %if mmsize == 8
  202. punpcklwd m3 ,m6 ; R0 G0 B0 R1 R2 G2 B2 R3
  203. punpckhwd m5, m6 ; R4 G4 B4 R5 R6 G6 B6 R7
  204. %if cpuflag(mmxext)
  205. pshufw m1, m2, 0xc6
  206. pshufw m6, m3, 0x84
  207. pshufw m7, m5, 0x38
  208. pand m6, [mask_1101] ; R0 G0 B0 R1 -- -- R2 G2
  209. movq m0, m1
  210. pand m7, [mask_0110] ; -- -- R6 G6 B6 R7 -- --
  211. movq m2, m1
  212. pand m1, [mask_0100] ; -- -- G3 B3 -- -- -- --
  213. psrlq m3, 48 ; B2 R3 -- -- -- -- -- --
  214. pand m0, [mask_0010] ; -- -- -- -- G1 B1 -- --
  215. psllq m5, 32 ; -- -- -- -- R4 G4 B4 R5
  216. pand m2, [mask_1001] ; G5 B5 -- -- -- -- G7 B7
  217. por m1, m3
  218. por m0, m6
  219. por m1, m5
  220. por m2, m7
  221. movntq [imageq], m0
  222. movntq [imageq + 8], m1
  223. movntq [imageq + 16], m2
  224. %else ; cpuflag(mmx)
  225. movd [imageq], m3 ; R0 G0 R2 G2
  226. movd [imageq + 4], m2 ; G1 B1
  227. psrlq m3, 32
  228. psrlq m2, 16
  229. movd [imageq + 6], m3 ; R2 G2 B2 R3
  230. movd [imageq + 10], m2 ; G3 B3
  231. psrlq m2, 16
  232. movd [imageq + 12], m5 ; R4 G4 B4 R5
  233. movd [imageq + 16], m2 ; G5 B5
  234. psrlq m5, 32
  235. movd [imageq + 20], m2 ; -- -- G7 B7
  236. movd [imageq + 18], m5 ; R6 G6 B6 R7
  237. %endif ; mmsize = 8
  238. %else ; mmsize == 16
  239. pshufb m3, [rgb24_shuf1] ; r0 g0 r6 g6 r12 g12 r2 g2 r8 g8 r14 g14 r4 g4 r10 g10
  240. pshufb m6, [rgb24_shuf2] ; b10 r11 b0 r1 b6 r7 b12 r13 b2 r3 b8 r9 b14 r15 b4 r5
  241. pshufb m2, [rgb24_shuf3] ; g5 b5 g11 b11 g1 b1 g7 b7 g13 b13 g3 b3 g9 b9 g15 b15
  242. mova m7, [mask_dw036]
  243. mova m4, [mask_dw147]
  244. mova m5, [mask_dw25]
  245. pand m0, m7, m3 ; r0 g0 --- --- --- --- r2 g2 --- --- --- --- r4 g4 --- ---
  246. pand m1, m4, m6 ; --- --- b0 r1 --- --- --- --- b2 r3 --- --- --- --- b4 r5
  247. por m0, m1
  248. pand m1, m5, m2 ; --- --- --- --- g1 b1 --- --- --- --- g3 b3 --- --- --- ---
  249. por m0, m1 ; r0 g0 b0 r1 g1 b1 r2 g2 b2 r3 g3 b3 r4 g4 b4 r5
  250. pand m1, m7, m2 ; g5 b5 --- --- --- --- g7 b7 --- --- --- --- g9 b9 --- ---
  251. pand m7, m6 ; b10 r11 --- --- --- --- b12 r13 --- --- --- --- b14 r15 --- ---
  252. pand m6, m5 ; --- --- --- --- b6 r7 --- --- --- --- b8 r9 --- --- --- ---
  253. por m1, m6
  254. pand m6, m4, m3 ; --- --- r6 g6 --- --- --- --- r8 g8 --- --- --- --- r10 g10
  255. pand m2, m4 ; --- --- g11 b11 --- --- --- --- g13 b13 --- --- --- --- g15 b15
  256. pand m3, m5 ; --- --- --- --- r12 g12 --- --- --- --- r14 g14 --- --- --- ---
  257. por m2, m7
  258. por m1, m6 ; g5 b5 r6 g6 b6 r7 g7 b7 r8 g8 b8 r9 g9 b9 r10 g10
  259. por m2, m3 ; b10 r11 g11 b11 r12 g12 b12 r13 g13 b13 r14 g14 b14 r15 g15 b15
  260. movu [imageq], m0
  261. movu [imageq + 16], m1
  262. movu [imageq + 32], m2
  263. %endif ; mmsize = 16
  264. %else ; PACK RGB15/16/32
  265. packuswb m0, m1
  266. packuswb m3, m5
  267. packuswb m2, m2
  268. mova m1, m0
  269. packuswb m7, m7
  270. punpcklbw m0, m3 ; B0 B1 B2 B3 ... B7
  271. punpckhbw m1, m3 ; R0 R1 R2 R3 ... R7
  272. punpcklbw m2, m7 ; G0 G1 G2 G3 ... G7
  273. %if %3 == 32 ; PACK RGB32
  274. %define depth 4
  275. %ifidn %1, yuv
  276. pcmpeqd m3, m3 ; Set alpha empty
  277. %else
  278. movu m3, [pa_2indexq + 2 * indexq] ; Load alpha
  279. %endif
  280. mova m5, m_blue
  281. mova m6, m_red
  282. punpckhbw m5, m_green
  283. punpcklbw m_blue, m_green
  284. punpckhbw m6, m_alpha
  285. punpcklbw m_red, m_alpha
  286. mova m_green, m_blue
  287. mova m_alpha, m5
  288. punpcklwd m_blue, m_red
  289. punpckhwd m_green, m_red
  290. punpcklwd m5, m6
  291. punpckhwd m_alpha, m6
  292. movu [imageq + 0], m_blue
  293. movu [imageq + 8 * time_num], m_green
  294. movu [imageq + 16 * time_num], m5
  295. movu [imageq + 24 * time_num], m_alpha
  296. %else ; PACK RGB15/16
  297. %define depth 2
  298. %if cpuflag(ssse3)
  299. %define red_dither m3
  300. %define green_dither m4
  301. %define blue_dither m5
  302. VBROADCASTSD red_dither, [pointer_c_ditherq + 0 * 8]
  303. VBROADCASTSD green_dither, [pointer_c_ditherq + 1 * 8]
  304. VBROADCASTSD blue_dither, [pointer_c_ditherq + 2 * 8]
  305. %else ; cpuflag(mmx/mmxext)
  306. %define blue_dither [pointer_c_ditherq + 2 * 8]
  307. %define green_dither [pointer_c_ditherq + 1 * 8]
  308. %define red_dither [pointer_c_ditherq + 0 * 8]
  309. %endif
  310. %if %3 == 15
  311. %define gmask pb_03
  312. %define isRGB15 1
  313. %else
  314. %define gmask pb_07
  315. %define isRGB15 0
  316. %endif
  317. paddusb m0, blue_dither
  318. paddusb m2, green_dither
  319. paddusb m1, red_dither
  320. pand m0, [pb_f8]
  321. pand m1, [pb_f8]
  322. mova m3, m2
  323. psllw m2, 3 - isRGB15
  324. psrlw m3, 5 + isRGB15
  325. psrlw m0, 3
  326. psrlw m1, isRGB15
  327. pand m2, [pb_e0]
  328. pand m3, [gmask]
  329. por m0, m2
  330. por m1, m3
  331. mova m2, m0
  332. punpcklbw m0, m1
  333. punpckhbw m2, m1
  334. movu [imageq], m0
  335. movu [imageq + 8 * time_num], m2
  336. %endif ; PACK RGB15/16
  337. %endif ; PACK RGB15/16/32
  338. add imageq, 8 * depth * time_num
  339. add indexq, 4 * time_num
  340. js .loop0
  341. REP_RET
  342. %endmacro
  343. INIT_MMX mmx
  344. yuv2rgb_fn yuv, rgb, 32
  345. yuv2rgb_fn yuv, bgr, 32
  346. yuv2rgb_fn yuva, rgb, 32
  347. yuv2rgb_fn yuva, bgr, 32
  348. yuv2rgb_fn yuv, rgb, 15
  349. yuv2rgb_fn yuv, rgb, 16
  350. INIT_MMX mmxext
  351. yuv2rgb_fn yuv, rgb, 24
  352. yuv2rgb_fn yuv, bgr, 24
  353. INIT_XMM ssse3
  354. yuv2rgb_fn yuv, rgb, 24
  355. yuv2rgb_fn yuv, bgr, 24
  356. yuv2rgb_fn yuv, rgb, 32
  357. yuv2rgb_fn yuv, bgr, 32
  358. yuv2rgb_fn yuva, rgb, 32
  359. yuv2rgb_fn yuva, bgr, 32
  360. yuv2rgb_fn yuv, rgb, 15
  361. yuv2rgb_fn yuv, rgb, 16