FFmpeg
yuv2rgb_lsx.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Loongson Technology Co. Ltd.
3  * Contributed by Bo Jin(jinbo@loongson.cn)
4  * All rights reserved.
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "swscale_loongarch.h"
25 
26 #define YUV2RGB_LOAD_COE \
27  /* Load x_offset */ \
28  __m128i y_offset = __lsx_vreplgr2vr_d(c->yOffset); \
29  __m128i u_offset = __lsx_vreplgr2vr_d(c->uOffset); \
30  __m128i v_offset = __lsx_vreplgr2vr_d(c->vOffset); \
31  /* Load x_coeff */ \
32  __m128i ug_coeff = __lsx_vreplgr2vr_d(c->ugCoeff); \
33  __m128i vg_coeff = __lsx_vreplgr2vr_d(c->vgCoeff); \
34  __m128i y_coeff = __lsx_vreplgr2vr_d(c->yCoeff); \
35  __m128i ub_coeff = __lsx_vreplgr2vr_d(c->ubCoeff); \
36  __m128i vr_coeff = __lsx_vreplgr2vr_d(c->vrCoeff); \
37 
38 #define LOAD_YUV_16 \
39  m_y1 = __lsx_vld(py_1, 0); \
40  m_y2 = __lsx_vld(py_2, 0); \
41  m_u = __lsx_vldrepl_d(pu, 0); \
42  m_v = __lsx_vldrepl_d(pv, 0); \
43  DUP2_ARG2(__lsx_vilvl_b, m_u, m_u, m_v, m_v, m_u, m_v); \
44  DUP2_ARG2(__lsx_vilvh_b, zero, m_u, zero, m_v, m_u_h, m_v_h); \
45  DUP2_ARG2(__lsx_vilvl_b, zero, m_u, zero, m_v, m_u, m_v); \
46  DUP2_ARG2(__lsx_vilvh_b, zero, m_y1, zero, m_y2, m_y1_h, m_y2_h); \
47  DUP2_ARG2(__lsx_vilvl_b, zero, m_y1, zero, m_y2, m_y1, m_y2); \
48 
49 /* YUV2RGB method
50  * The conversion method is as follows:
51  * R = Y' * y_coeff + V' * vr_coeff
52  * G = Y' * y_coeff + V' * vg_coeff + U' * ug_coeff
53  * B = Y' * y_coeff + U' * ub_coeff
54  *
55  * where X' = X * 8 - x_offset
56  *
57  */
58 
59 #define YUV2RGB(y1, y2, u, v, r1, g1, b1, r2, g2, b2) \
60 { \
61  y1 = __lsx_vslli_h(y1, 3); \
62  y2 = __lsx_vslli_h(y2, 3); \
63  u = __lsx_vslli_h(u, 3); \
64  v = __lsx_vslli_h(v, 3); \
65  y1 = __lsx_vsub_h(y1, y_offset); \
66  y2 = __lsx_vsub_h(y2, y_offset); \
67  u = __lsx_vsub_h(u, u_offset); \
68  v = __lsx_vsub_h(v, v_offset); \
69  y_1 = __lsx_vmuh_h(y1, y_coeff); \
70  y_2 = __lsx_vmuh_h(y2, y_coeff); \
71  u2g = __lsx_vmuh_h(u, ug_coeff); \
72  u2b = __lsx_vmuh_h(u, ub_coeff); \
73  v2r = __lsx_vmuh_h(v, vr_coeff); \
74  v2g = __lsx_vmuh_h(v, vg_coeff); \
75  r1 = __lsx_vsadd_h(y_1, v2r); \
76  v2g = __lsx_vsadd_h(v2g, u2g); \
77  g1 = __lsx_vsadd_h(y_1, v2g); \
78  b1 = __lsx_vsadd_h(y_1, u2b); \
79  r2 = __lsx_vsadd_h(y_2, v2r); \
80  g2 = __lsx_vsadd_h(y_2, v2g); \
81  b2 = __lsx_vsadd_h(y_2, u2b); \
82  DUP4_ARG1(__lsx_vclip255_h, r1, g1, b1, r2, r1, g1, b1, r2); \
83  DUP2_ARG1(__lsx_vclip255_h, g2, b2, g2, b2); \
84 }
85 
86 #define RGB_PACK(r, g, b, rgb_l, rgb_h) \
87 { \
88  __m128i rg; \
89  rg = __lsx_vpackev_b(g, r); \
90  DUP2_ARG3(__lsx_vshuf_b, b, rg, shuf2, b, rg, shuf3, rgb_l, rgb_h); \
91 }
92 
93 #define RGB32_PACK(a, r, g, b, rgb_l, rgb_h) \
94 { \
95  __m128i ra, bg; \
96  ra = __lsx_vpackev_b(r, a); \
97  bg = __lsx_vpackev_b(b, g); \
98  rgb_l = __lsx_vilvl_h(bg, ra); \
99  rgb_h = __lsx_vilvh_h(bg, ra); \
100 }
101 
102 #define RGB_STORE(rgb_l, rgb_h, image) \
103 { \
104  __lsx_vstelm_d(rgb_l, image, 0, 0); \
105  __lsx_vstelm_d(rgb_l, image, 8, 1); \
106  __lsx_vstelm_d(rgb_h, image, 16, 0); \
107 }
108 
109 #define RGB32_STORE(rgb_l, rgb_h, image) \
110 { \
111  __lsx_vst(rgb_l, image, 0); \
112  __lsx_vst(rgb_h, image, 16); \
113 }
114 
115 #define YUV2RGBFUNC(func_name, dst_type, alpha) \
116  int func_name(SwsContext *c, const uint8_t *const src[], \
117  const int srcStride[], int srcSliceY, int srcSliceH, \
118  uint8_t *const dst[], const int dstStride[]) \
119 { \
120  int x, y, h_size, vshift, res; \
121  __m128i m_y1, m_y2, m_u, m_v; \
122  __m128i m_y1_h, m_y2_h, m_u_h, m_v_h; \
123  __m128i y_1, y_2, u2g, v2g, u2b, v2r, rgb1_l, rgb1_h; \
124  __m128i rgb2_l, rgb2_h, r1, g1, b1, r2, g2, b2; \
125  __m128i shuf2 = {0x0504120302100100, 0x0A18090816070614}; \
126  __m128i shuf3 = {0x1E0F0E1C0D0C1A0B, 0x0101010101010101}; \
127  __m128i zero = __lsx_vldi(0); \
128  \
129  YUV2RGB_LOAD_COE \
130  \
131  h_size = c->dstW >> 4; \
132  res = (c->dstW & 15) >> 1; \
133  vshift = c->srcFormat != AV_PIX_FMT_YUV422P; \
134  for (y = 0; y < srcSliceH; y += 2) { \
135  dst_type av_unused *r, *g, *b; \
136  dst_type *image1 = (dst_type *)(dst[0] + (y + srcSliceY) * dstStride[0]);\
137  dst_type *image2 = (dst_type *)(image1 + dstStride[0]);\
138  const uint8_t *py_1 = src[0] + y * srcStride[0]; \
139  const uint8_t *py_2 = py_1 + srcStride[0]; \
140  const uint8_t *pu = src[1] + (y >> vshift) * srcStride[1]; \
141  const uint8_t *pv = src[2] + (y >> vshift) * srcStride[2]; \
142  for(x = 0; x < h_size; x++) { \
143 
144 #define YUV2RGBFUNC32(func_name, dst_type, alpha) \
145  int func_name(SwsContext *c, const uint8_t *const src[], \
146  const int srcStride[], int srcSliceY, int srcSliceH, \
147  uint8_t *const dst[], const int dstStride[]) \
148 { \
149  int x, y, h_size, vshift, res; \
150  __m128i m_y1, m_y2, m_u, m_v; \
151  __m128i m_y1_h, m_y2_h, m_u_h, m_v_h; \
152  __m128i y_1, y_2, u2g, v2g, u2b, v2r, rgb1_l, rgb1_h; \
153  __m128i rgb2_l, rgb2_h, r1, g1, b1, r2, g2, b2; \
154  __m128i a = __lsx_vldi(0xFF); \
155  __m128i zero = __lsx_vldi(0); \
156  \
157  YUV2RGB_LOAD_COE \
158  \
159  h_size = c->dstW >> 4; \
160  res = (c->dstW & 15) >> 1; \
161  vshift = c->srcFormat != AV_PIX_FMT_YUV422P; \
162  for (y = 0; y < srcSliceH; y += 2) { \
163  int yd = y + srcSliceY; \
164  dst_type av_unused *r, *g, *b; \
165  dst_type *image1 = (dst_type *)(dst[0] + (yd) * dstStride[0]); \
166  dst_type *image2 = (dst_type *)(dst[0] + (yd + 1) * dstStride[0]); \
167  const uint8_t *py_1 = src[0] + y * srcStride[0]; \
168  const uint8_t *py_2 = py_1 + srcStride[0]; \
169  const uint8_t *pu = src[1] + (y >> vshift) * srcStride[1]; \
170  const uint8_t *pv = src[2] + (y >> vshift) * srcStride[2]; \
171  for(x = 0; x < h_size; x++) { \
172 
173 #define DEALYUV2RGBREMAIN \
174  py_1 += 16; \
175  py_2 += 16; \
176  pu += 8; \
177  pv += 8; \
178  image1 += 48; \
179  image2 += 48; \
180  } \
181  for (x = 0; x < res; x++) { \
182  int av_unused U, V, Y; \
183  U = pu[0]; \
184  V = pv[0]; \
185  r = (void *)c->table_rV[V+YUVRGB_TABLE_HEADROOM]; \
186  g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] \
187  + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
188  b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM];
189 
190 #define DEALYUV2RGBREMAIN32 \
191  py_1 += 16; \
192  py_2 += 16; \
193  pu += 8; \
194  pv += 8; \
195  image1 += 16; \
196  image2 += 16; \
197  } \
198  for (x = 0; x < res; x++) { \
199  int av_unused U, V, Y; \
200  U = pu[0]; \
201  V = pv[0]; \
202  r = (void *)c->table_rV[V+YUVRGB_TABLE_HEADROOM]; \
203  g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] \
204  + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
205  b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM]; \
206 
207 #define PUTRGB24(dst, src) \
208  Y = src[0]; \
209  dst[0] = r[Y]; \
210  dst[1] = g[Y]; \
211  dst[2] = b[Y]; \
212  Y = src[1]; \
213  dst[3] = r[Y]; \
214  dst[4] = g[Y]; \
215  dst[5] = b[Y];
216 
217 #define PUTBGR24(dst, src) \
218  Y = src[0]; \
219  dst[0] = b[Y]; \
220  dst[1] = g[Y]; \
221  dst[2] = r[Y]; \
222  Y = src[1]; \
223  dst[3] = b[Y]; \
224  dst[4] = g[Y]; \
225  dst[5] = r[Y];
226 
227 #define PUTRGB(dst, src) \
228  Y = src[0]; \
229  dst[0] = r[Y] + g[Y] + b[Y]; \
230  Y = src[1]; \
231  dst[1] = r[Y] + g[Y] + b[Y]; \
232 
233 #define ENDRES \
234  pu += 1; \
235  pv += 1; \
236  py_1 += 2; \
237  py_2 += 2; \
238  image1 += 6; \
239  image2 += 6; \
240 
241 #define ENDRES32 \
242  pu += 1; \
243  pv += 1; \
244  py_1 += 2; \
245  py_2 += 2; \
246  image1 += 2; \
247  image2 += 2; \
248 
249 #define END_FUNC() \
250  } \
251  } \
252  return srcSliceH; \
253 }
254 
255 YUV2RGBFUNC(yuv420_rgb24_lsx, uint8_t, 0)
257  YUV2RGB(m_y1, m_y2, m_u, m_v, r1, g1, b1, r2, g2, b2);
258  RGB_PACK(r1, g1, b1, rgb1_l, rgb1_h);
259  RGB_PACK(r2, g2, b2, rgb2_l, rgb2_h);
260  RGB_STORE(rgb1_l, rgb1_h, image1);
261  RGB_STORE(rgb2_l, rgb2_h, image2);
262  YUV2RGB(m_y1_h, m_y2_h, m_u_h, m_v_h, r1, g1, b1, r2, g2, b2);
263  RGB_PACK(r1, g1, b1, rgb1_l, rgb1_h);
264  RGB_PACK(r2, g2, b2, rgb2_l, rgb2_h);
265  RGB_STORE(rgb1_l, rgb1_h, image1 + 24);
266  RGB_STORE(rgb2_l, rgb2_h, image2 + 24);
268  PUTRGB24(image1, py_1);
269  PUTRGB24(image2, py_2);
270  ENDRES
271  END_FUNC()
272 
273 YUV2RGBFUNC(yuv420_bgr24_lsx, uint8_t, 0)
275  YUV2RGB(m_y1, m_y2, m_u, m_v, r1, g1, b1, r2, g2, b2);
276  RGB_PACK(b1, g1, r1, rgb1_l, rgb1_h);
277  RGB_PACK(b2, g2, r2, rgb2_l, rgb2_h);
278  RGB_STORE(rgb1_l, rgb1_h, image1);
279  RGB_STORE(rgb2_l, rgb2_h, image2);
280  YUV2RGB(m_y1_h, m_y2_h, m_u_h, m_v_h, r1, g1, b1, r2, g2, b2);
281  RGB_PACK(b1, g1, r1, rgb1_l, rgb1_h);
282  RGB_PACK(b2, g2, r2, rgb2_l, rgb2_h);
283  RGB_STORE(rgb1_l, rgb1_h, image1 + 24);
284  RGB_STORE(rgb2_l, rgb2_h, image2 + 24);
286  PUTBGR24(image1, py_1);
287  PUTBGR24(image2, py_2);
288  ENDRES
289  END_FUNC()
290 
291 YUV2RGBFUNC32(yuv420_rgba32_lsx, uint32_t, 0)
293  YUV2RGB(m_y1, m_y2, m_u, m_v, r1, g1, b1, r2, g2, b2);
294  RGB32_PACK(r1, g1, b1, a, rgb1_l, rgb1_h);
295  RGB32_PACK(r2, g2, b2, a, rgb2_l, rgb2_h);
296  RGB32_STORE(rgb1_l, rgb1_h, image1);
297  RGB32_STORE(rgb2_l, rgb2_h, image2);
298  YUV2RGB(m_y1_h, m_y2_h, m_u_h, m_v_h, r1, g1, b1, r2, g2, b2);
299  RGB32_PACK(r1, g1, b1, a, rgb1_l, rgb1_h);
300  RGB32_PACK(r2, g2, b2, a, rgb2_l, rgb2_h);
301  RGB32_STORE(rgb1_l, rgb1_h, image1 + 8);
302  RGB32_STORE(rgb2_l, rgb2_h, image2 + 8);
304  PUTRGB(image1, py_1);
305  PUTRGB(image2, py_2);
306  ENDRES32
307  END_FUNC()
308 
309 YUV2RGBFUNC32(yuv420_bgra32_lsx, uint32_t, 0)
311  YUV2RGB(m_y1, m_y2, m_u, m_v, r1, g1, b1, r2, g2, b2);
312  RGB32_PACK(b1, g1, r1, a, rgb1_l, rgb1_h);
313  RGB32_PACK(b2, g2, r2, a, rgb2_l, rgb2_h);
314  RGB32_STORE(rgb1_l, rgb1_h, image1);
315  RGB32_STORE(rgb2_l, rgb2_h, image2);
316  YUV2RGB(m_y1_h, m_y2_h, m_u_h, m_v_h, r1, g1, b1, r2, g2, b2);
317  RGB32_PACK(b1, g1, r1, a, rgb1_l, rgb1_h);
318  RGB32_PACK(b2, g2, r2, a, rgb2_l, rgb2_h);
319  RGB32_STORE(rgb1_l, rgb1_h, image1 + 8);
320  RGB32_STORE(rgb2_l, rgb2_h, image2 + 8);
322  PUTRGB(image1, py_1);
323  PUTRGB(image2, py_2);
324  ENDRES32
325  END_FUNC()
326 
327 YUV2RGBFUNC32(yuv420_argb32_lsx, uint32_t, 0)
329  YUV2RGB(m_y1, m_y2, m_u, m_v, r1, g1, b1, r2, g2, b2);
330  RGB32_PACK(a, r1, g1, b1, rgb1_l, rgb1_h);
331  RGB32_PACK(a, r2, g2, b2, rgb2_l, rgb2_h);
332  RGB32_STORE(rgb1_l, rgb1_h, image1);
333  RGB32_STORE(rgb2_l, rgb2_h, image2);
334  YUV2RGB(m_y1_h, m_y2_h, m_u_h, m_v_h, r1, g1, b1, r2, g2, b2);
335  RGB32_PACK(a, r1, g1, b1, rgb1_l, rgb1_h);
336  RGB32_PACK(a, r2, g2, b2, rgb2_l, rgb2_h);
337  RGB32_STORE(rgb1_l, rgb1_h, image1 + 8);
338  RGB32_STORE(rgb2_l, rgb2_h, image2 + 8);
340  PUTRGB(image1, py_1);
341  PUTRGB(image2, py_2);
342  ENDRES32
343  END_FUNC()
344 
345 YUV2RGBFUNC32(yuv420_abgr32_lsx, uint32_t, 0)
347  YUV2RGB(m_y1, m_y2, m_u, m_v, r1, g1, b1, r2, g2, b2);
348  RGB32_PACK(a, b1, g1, r1, rgb1_l, rgb1_h);
349  RGB32_PACK(a, b2, g2, r2, rgb2_l, rgb2_h);
350  RGB32_STORE(rgb1_l, rgb1_h, image1);
351  RGB32_STORE(rgb2_l, rgb2_h, image2);
352  YUV2RGB(m_y1_h, m_y2_h, m_u_h, m_v_h, r1, g1, b1, r2, g2, b2);
353  RGB32_PACK(a, b1, g1, r1, rgb1_l, rgb1_h);
354  RGB32_PACK(a, b2, g2, r2, rgb2_l, rgb2_h);
355  RGB32_STORE(rgb1_l, rgb1_h, image1 + 8);
356  RGB32_STORE(rgb2_l, rgb2_h, image2 + 8);
358  PUTRGB(image1, py_1);
359  PUTRGB(image2, py_2);
360  ENDRES32
361  END_FUNC()
DEALYUV2RGBREMAIN32
#define DEALYUV2RGBREMAIN32
Definition: yuv2rgb_lsx.c:190
yuv420_rgb24_lsx
int yuv420_rgb24_lsx(SwsContext *c, const uint8_t *const src[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
yuv420_argb32_lsx
int yuv420_argb32_lsx(SwsContext *c, const uint8_t *const src[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
RGB_PACK
#define RGB_PACK(r, g, b, rgb_l, rgb_h)
Definition: yuv2rgb_lsx.c:86
PUTBGR24
#define PUTBGR24(dst, src)
Definition: yuv2rgb_lsx.c:217
LOAD_YUV_16
#define LOAD_YUV_16
Definition: yuv2rgb_lsx.c:38
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:2034
yuv420_abgr32_lsx
int yuv420_abgr32_lsx(SwsContext *c, const uint8_t *const src[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
RGB32_STORE
#define RGB32_STORE(rgb_l, rgb_h, image)
Definition: yuv2rgb_lsx.c:109
PUTRGB24
#define PUTRGB24(dst, src)
Definition: yuv2rgb_lsx.c:207
swscale_loongarch.h
DEALYUV2RGBREMAIN
#define DEALYUV2RGBREMAIN
Definition: yuv2rgb_lsx.c:173
ENDRES
#define ENDRES
Definition: yuv2rgb_lsx.c:233
ENDRES32
#define ENDRES32
Definition: yuv2rgb_lsx.c:241
yuv420_bgra32_lsx
int yuv420_bgra32_lsx(SwsContext *c, const uint8_t *const src[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
py_1
py_1
Definition: yuv2rgb.c:483
YUV2RGB
#define YUV2RGB(y1, y2, u, v, r1, g1, b1, r2, g2, b2)
Definition: yuv2rgb_lsx.c:59
RGB32_PACK
#define RGB32_PACK(a, r, g, b, rgb_l, rgb_h)
Definition: yuv2rgb_lsx.c:93
PUTRGB
#define PUTRGB(dst, src)
Definition: yuv2rgb_lsx.c:227
YUV2RGBFUNC32
#define YUV2RGBFUNC32(func_name, dst_type, alpha)
Definition: yuv2rgb_lsx.c:144
b2
static double b2(void *priv, double x, double y)
Definition: vf_xfade.c:2035
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
END_FUNC
#define END_FUNC()
Definition: yuv2rgb_lsx.c:249
RGB_STORE
#define RGB_STORE(rgb_l, rgb_h, image)
Definition: yuv2rgb_lsx.c:102
py_2
py_2
Definition: yuv2rgb.c:484
YUV2RGBFUNC
#define YUV2RGBFUNC(func_name, dst_type, alpha)
Definition: yuv2rgb_lsx.c:115
loongson_intrinsics.h
yuv420_bgr24_lsx
int yuv420_bgr24_lsx(SwsContext *c, const uint8_t *const src[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
yuv420_rgba32_lsx
int yuv420_rgba32_lsx(SwsContext *c, const uint8_t *const src[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])