]> git.sesse.net Git - ffmpeg/blob - libswscale/x86/yuv_2_rgb.asm
libswscale/x86/yuv2rgb: Change inline assembly into nasm code
[ffmpeg] / libswscale / x86 / yuv_2_rgb.asm
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
24 %include "libavutil/x86/x86util.asm"
25
26 SECTION_RODATA
27
28 pw_00ff: times 4 dw 255
29 pb_f8:   times 8 db 248
30 pb_e0:   times 8 db 224
31 pb_03:   times 8 db 3
32 pb_07:   times 8 db 7
33
34 mask_1101: dw -1, -1,  0, -1
35 mask_0010: dw  0,  0, -1,  0
36 mask_0110: dw  0, -1, -1,  0
37 mask_1001: dw -1,  0,  0, -1
38 mask_0100: dw  0, -1,  0,  0
39
40 SECTION .text
41
42 ;-----------------------------------------------------------------------------
43 ;
44 ; YUV420/YUVA420 to RGB/BGR 15/16/24/32
45 ; R = Y + ((vrCoff * (v - 128)) >> 8)
46 ; G = Y - ((ugCoff * (u - 128) + vgCoff * (v - 128)) >> 8)
47 ; B = Y + ((ubCoff * (u - 128)) >> 8)
48 ;
49 ;-----------------------------------------------------------------------------
50
51 %macro MOV_H2L 1
52 psrlq %1, 32
53 %endmacro
54
55 %macro yuv2rgb_fn 3
56
57 %if %3 == 32
58     %ifidn %1, yuva
59     %define parameters index, image, pu_index, pv_index, pointer_c_dither, py_2index, pa_2index
60     %define GPR_num 7
61     %endif
62 %else
63     %define parameters index, image, pu_index, pv_index, pointer_c_dither, py_2index
64     %define GPR_num 6
65 %endif
66
67 %define m_green m2
68 %define m_alpha m3
69 %define m_y m6
70 %define m_u m0
71 %define m_v m1
72 %ifidn %2, rgb
73 %define m_red m1
74 %define m_blue m0
75 %else
76 %define m_red m0
77 %define m_blue m1
78 %endif
79
80 %define time_num 1
81 %define reg_num 8
82 %define y_offset [pointer_c_ditherq + 8  * 8]
83 %define u_offset [pointer_c_ditherq + 9  * 8]
84 %define v_offset [pointer_c_ditherq + 10 * 8]
85 %define ug_coff  [pointer_c_ditherq + 7  * 8]
86 %define vg_coff  [pointer_c_ditherq + 6  * 8]
87 %define y_coff   [pointer_c_ditherq + 3  * 8]
88 %define ub_coff  [pointer_c_ditherq + 5  * 8]
89 %define vr_coff  [pointer_c_ditherq + 4  * 8]
90
91 cglobal %1_420_%2%3, GPR_num, GPR_num, reg_num, parameters
92
93 %if ARCH_X86_64
94     movsxd indexq, indexd
95 %endif
96     mova m_y, [py_2indexq + 2 * indexq]
97     movh m_u, [pu_indexq  +     indexq]
98     movh m_v, [pv_indexq  +     indexq]
99 .loop0:
100     pxor m4, m4
101     mova m7, m6
102     punpcklbw m0, m4
103     punpcklbw m1, m4
104     mova m2, [pw_00ff]
105     pand m6, m2
106     psrlw m7, 8
107     psllw m0, 3
108     psllw m1, 3
109     psllw m6, 3
110     psllw m7, 3
111     psubsw m0, u_offset ; U = U - 128
112     psubsw m1, v_offset ; V = V - 128
113     psubw m6, y_offset
114     psubw m7, y_offset
115     mova m2, m0
116     mova m3, m1
117     pmulhw m2, ug_coff
118     pmulhw m3, vg_coff
119     pmulhw m6, y_coff
120     pmulhw m7, y_coff
121     pmulhw m0, ub_coff
122     pmulhw m1, vr_coff
123     paddsw m2, m3
124     mova m3, m7
125     mova m5, m7
126     paddsw m3, m0 ; B1 B3 B5 B7 ...
127     paddsw m5, m1 ; R1 R3 R5 R7 ...
128     paddsw m7, m2 ; G1 G3 G4 G7 ...
129     paddsw m0, m6 ; B0 B2 B4 B6 ...
130     paddsw m1, m6 ; R0 R2 R4 R6 ...
131     paddsw m2, m6 ; G0 G2 G4 G6 ...
132
133 %if %3 == 24 ; PACK RGB24
134 %define depth 3
135     packuswb m0, m3 ; R0 R2 R4 R6 ... R1 R3 R5 R7 ...
136     packuswb m1, m5 ; B0 B2 B4 B6 ... B1 B3 B5 B7 ...
137     packuswb m2, m7 ; G0 G2 G4 G6 ... G1 G3 G5 G7 ...
138     mova m3, m_red
139     mova m6, m_blue
140     MOV_H2L m_red
141     punpcklbw m3, m2     ; R0 G0 R2 G2 R4 G4 R6 G6 R8 G8 ...
142     punpcklbw m6, m_red  ; B0 R1 B2 R3 B4 R5 B6 R7 B8 R9 ...
143     mova m5, m3
144     punpckhbw m2, m_blue ; G1 B1 G3 B3 G5 B5 G7 B7 G9 B9 ...
145     punpcklwd m3 ,m6     ; R0 G0 B0 R1 R2 G2 B2 R3
146     punpckhwd m5, m6     ; R4 G4 B4 R5 R6 G6 B6 R7
147 %if cpuflag(mmxext)
148     pshufw m1, m2, 0xc6
149     pshufw m6, m3, 0x84
150     pshufw m7, m5, 0x38
151     pand m6, [mask_1101] ; R0 G0 B0 R1 -- -- R2 G2
152     movq m0, m1
153     pand m7, [mask_0110] ; -- -- R6 G6 B6 R7 -- --
154     movq m2, m1
155     pand m1, [mask_0100] ; -- -- G3 B3 -- -- -- --
156     psrlq m3, 48         ; B2 R3 -- -- -- -- -- --
157     pand m0, [mask_0010] ; -- -- -- -- G1 B1 -- --
158     psllq m5, 32         ; -- -- -- -- R4 G4 B4 R5
159     pand m2, [mask_1001] ; G5 B5 -- -- -- -- G7 B7
160     por m1, m3
161     por m0, m6
162     por m1, m5
163     por m2, m7
164     movntq [imageq], m0
165     movntq [imageq + 8], m1
166     movntq [imageq + 16], m2
167 %else ; cpuflag(mmx)
168     movd [imageq], m3      ; R0 G0 R2 G2
169     movd [imageq + 4], m2  ; G1 B1
170     psrlq m3, 32
171     psrlq m2, 16
172     movd [imageq + 6], m3  ; R2 G2 B2 R3
173     movd [imageq + 10], m2 ; G3 B3
174     psrlq m2, 16
175     movd [imageq + 12], m5 ; R4 G4 B4 R5
176     movd [imageq + 16], m2 ; G5 B5
177     psrlq m5, 32
178     movd [imageq + 20], m2 ; -- -- G7 B7
179     movd [imageq + 18], m5 ; R6 G6 B6 R7
180 %endif
181 %else ; PACK RGB15/16/32
182     packuswb m0, m1
183     packuswb m3, m5
184     packuswb m2, m2
185     mova m1, m0
186     packuswb m7, m7
187     punpcklbw m0, m3 ; B0 B1 B2 B3 ... B7
188     punpckhbw m1, m3 ; R0 R1 R2 R3 ... R7
189     punpcklbw m2, m7 ; G0 G1 G2 G3 ... G7
190 %if %3 == 32 ; PACK RGB32
191 %define depth 4
192 %ifidn %1, yuv
193     pcmpeqd m3, m3 ; Set alpha empty
194 %else
195     mova m3, [pa_2indexq + 2 * indexq] ; Load alpha
196 %endif
197     mova m5, m_blue
198     mova m6, m_red
199     punpckhbw m5, m_green
200     punpcklbw m_blue, m_green
201     punpckhbw m6, m_alpha
202     punpcklbw m_red, m_alpha
203     mova m_green, m_blue
204     mova m_alpha, m5
205     punpcklwd m_blue, m_red
206     punpckhwd m_green, m_red
207     punpcklwd m5, m6
208     punpckhwd m_alpha, m6
209     mova [imageq + 0], m_blue
210     mova [imageq + 8 * time_num], m_green
211     mova [imageq + 16 * time_num], m5
212     mova [imageq + 24 * time_num], m_alpha
213 %else ; PACK RGB15/16
214 %define depth 2
215 %define blue_dither  [pointer_c_ditherq + 2 * 8]
216 %define green_dither [pointer_c_ditherq + 1 * 8]
217 %define red_dither   [pointer_c_ditherq + 0 * 8]
218 %if %3 == 15
219 %define gmask pb_03
220 %define isRGB15 1
221 %else
222 %define gmask pb_07
223 %define isRGB15 0
224 %endif
225     paddusb m0, blue_dither
226     paddusb m2, green_dither
227     paddusb m1, red_dither
228     pand m0, [pb_f8]
229     pand m1, [pb_f8]
230     mova m3, m2
231     psllw m2, 3 - isRGB15
232     psrlw m3, 5 + isRGB15
233     psrlw m0, 3
234     psrlw m1, isRGB15
235     pand m2, [pb_e0]
236     pand m3, [gmask]
237     por m0, m2
238     por m1, m3
239     mova m2, m0
240     punpcklbw m0, m1
241     punpckhbw m2, m1
242     mova [imageq], m0
243     mova [imageq + 8 * time_num], m2
244 %endif ; PACK RGB15/16
245 %endif ; PACK RGB15/16/32
246
247 mova m_y, [py_2indexq + 2 * indexq + 8 * time_num]
248 movh m_v, [pv_indexq  +     indexq + 4 * time_num]
249 movh m_u, [pu_indexq  +     indexq + 4 * time_num]
250 add imageq, 8 * depth * time_num
251 add indexq, 4 * time_num
252 js .loop0
253
254 REP_RET
255
256 %endmacro
257
258 INIT_MMX mmx
259 yuv2rgb_fn yuv,  rgb, 24
260 yuv2rgb_fn yuv,  bgr, 24
261 yuv2rgb_fn yuv,  rgb, 32
262 yuv2rgb_fn yuv,  bgr, 32
263 yuv2rgb_fn yuva, rgb, 32
264 yuv2rgb_fn yuva, bgr, 32
265 yuv2rgb_fn yuv,  rgb, 15
266 yuv2rgb_fn yuv,  rgb, 16
267
268 INIT_MMX mmxext
269 yuv2rgb_fn yuv, rgb, 24
270 yuv2rgb_fn yuv, bgr, 24