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