yuv_2_rgb.asm 9.2 KB

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