yuv_2_rgb.asm 12 KB

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