yuv_2_rgb.asm 9.8 KB

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