FFmpeg
sw_rgb.c
Go to the documentation of this file.
1 /*
2  *
3  * This file is part of FFmpeg.
4  *
5  * FFmpeg is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * FFmpeg is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include <string.h>
21 
22 #include "libavutil/common.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem_internal.h"
25 #include "libavutil/pixdesc.h"
26 
27 #include "libswscale/rgb2rgb.h"
28 #include "libswscale/swscale.h"
30 
31 #include "checkasm.h"
32 
33 #define randomize_buffers(buf, size) \
34  do { \
35  int j; \
36  for (j = 0; j < size; j+=4) \
37  AV_WN32(buf + j, rnd()); \
38  } while (0)
39 
40 static const uint8_t width[] = {12, 16, 20, 32, 36, 128};
41 static const struct {uint8_t w, h, s;} planes[] = {
42  {12,16,12}, {16,16,16}, {20,23,25}, {32,18,48}, {8,128,16}, {128,128,128}
43 };
44 
45 #define MAX_STRIDE 128
46 #define MAX_HEIGHT 128
47 
48 static void check_shuffle_bytes(void * func, const char * report)
49 {
50  int i;
51  LOCAL_ALIGNED_32(uint8_t, src0, [MAX_STRIDE]);
52  LOCAL_ALIGNED_32(uint8_t, src1, [MAX_STRIDE]);
53  LOCAL_ALIGNED_32(uint8_t, dst0, [MAX_STRIDE]);
54  LOCAL_ALIGNED_32(uint8_t, dst1, [MAX_STRIDE]);
55 
56  declare_func(void, const uint8_t *src, uint8_t *dst, int src_size);
57 
58  memset(dst0, 0, MAX_STRIDE);
59  memset(dst1, 0, MAX_STRIDE);
61  memcpy(src1, src0, MAX_STRIDE);
62 
63  if (check_func(func, "%s", report)) {
64  for (i = 0; i < 6; i ++) {
65  call_ref(src0, dst0, width[i]);
66  call_new(src1, dst1, width[i]);
67  if (memcmp(dst0, dst1, MAX_STRIDE))
68  fail();
69  }
70  bench_new(src0, dst0, width[5]);
71  }
72 }
73 
74 static void check_uyvy_to_422p(void)
75 {
76  int i;
77 
78  LOCAL_ALIGNED_32(uint8_t, src0, [MAX_STRIDE * MAX_HEIGHT * 2]);
79  LOCAL_ALIGNED_32(uint8_t, src1, [MAX_STRIDE * MAX_HEIGHT * 2]);
80  LOCAL_ALIGNED_32(uint8_t, dst_y_0, [MAX_STRIDE * MAX_HEIGHT]);
81  LOCAL_ALIGNED_32(uint8_t, dst_y_1, [MAX_STRIDE * MAX_HEIGHT]);
82  LOCAL_ALIGNED_32(uint8_t, dst_u_0, [(MAX_STRIDE/2) * MAX_HEIGHT]);
83  LOCAL_ALIGNED_32(uint8_t, dst_u_1, [(MAX_STRIDE/2) * MAX_HEIGHT]);
84  LOCAL_ALIGNED_32(uint8_t, dst_v_0, [(MAX_STRIDE/2) * MAX_HEIGHT]);
85  LOCAL_ALIGNED_32(uint8_t, dst_v_1, [(MAX_STRIDE/2) * MAX_HEIGHT]);
86 
87  declare_func(void, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
88  const uint8_t *src, int width, int height,
89  int lumStride, int chromStride, int srcStride);
90 
92  memcpy(src1, src0, MAX_STRIDE * MAX_HEIGHT * 2);
93 
94  if (check_func(uyvytoyuv422, "uyvytoyuv422")) {
95  for (i = 0; i < 6; i ++) {
96  memset(dst_y_0, 0, MAX_STRIDE * MAX_HEIGHT);
97  memset(dst_y_1, 0, MAX_STRIDE * MAX_HEIGHT);
98  memset(dst_u_0, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
99  memset(dst_u_1, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
100  memset(dst_v_0, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
101  memset(dst_v_1, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
102 
103  call_ref(dst_y_0, dst_u_0, dst_v_0, src0, planes[i].w, planes[i].h,
104  MAX_STRIDE, MAX_STRIDE / 2, planes[i].s);
105  call_new(dst_y_1, dst_u_1, dst_v_1, src1, planes[i].w, planes[i].h,
106  MAX_STRIDE, MAX_STRIDE / 2, planes[i].s);
107  if (memcmp(dst_y_0, dst_y_1, MAX_STRIDE * MAX_HEIGHT) ||
108  memcmp(dst_u_0, dst_u_1, (MAX_STRIDE/2) * MAX_HEIGHT) ||
109  memcmp(dst_v_0, dst_v_1, (MAX_STRIDE/2) * MAX_HEIGHT))
110  fail();
111  }
112  bench_new(dst_y_1, dst_u_1, dst_v_1, src1, planes[5].w, planes[5].h,
113  MAX_STRIDE, MAX_STRIDE / 2, planes[5].s);
114  }
115 }
116 
117 static void check_interleave_bytes(void)
118 {
119  LOCAL_ALIGNED_16(uint8_t, src0_buf, [MAX_STRIDE*MAX_HEIGHT+1]);
120  LOCAL_ALIGNED_16(uint8_t, src1_buf, [MAX_STRIDE*MAX_HEIGHT+1]);
121  LOCAL_ALIGNED_16(uint8_t, dst0_buf, [2*MAX_STRIDE*MAX_HEIGHT+2]);
122  LOCAL_ALIGNED_16(uint8_t, dst1_buf, [2*MAX_STRIDE*MAX_HEIGHT+2]);
123  // Intentionally using unaligned buffers, as this function doesn't have
124  // any alignment requirements.
125  uint8_t *src0 = src0_buf + 1;
126  uint8_t *src1 = src1_buf + 1;
127  uint8_t *dst0 = dst0_buf + 2;
128  uint8_t *dst1 = dst1_buf + 2;
129 
130  declare_func(void, const uint8_t *, const uint8_t *,
131  uint8_t *, int, int, int, int, int);
132 
135 
136  if (check_func(interleaveBytes, "interleave_bytes")) {
137  for (int i = 0; i <= 16; i++) {
138  // Try all widths [1,16], and try one random width.
139 
140  int w = i > 0 ? i : (1 + (rnd() % (MAX_STRIDE-2)));
141  int h = 1 + (rnd() % (MAX_HEIGHT-2));
142 
143  int src0_offset = 0, src0_stride = MAX_STRIDE;
144  int src1_offset = 0, src1_stride = MAX_STRIDE;
145  int dst_offset = 0, dst_stride = 2 * MAX_STRIDE;
146 
147  memset(dst0, 0, 2 * MAX_STRIDE * MAX_HEIGHT);
148  memset(dst1, 0, 2 * MAX_STRIDE * MAX_HEIGHT);
149 
150  // Try different combinations of negative strides
151  if (i & 1) {
152  src0_offset = (h-1)*src0_stride;
153  src0_stride = -src0_stride;
154  }
155  if (i & 2) {
156  src1_offset = (h-1)*src1_stride;
157  src1_stride = -src1_stride;
158  }
159  if (i & 4) {
160  dst_offset = (h-1)*dst_stride;
161  dst_stride = -dst_stride;
162  }
163 
164  call_ref(src0 + src0_offset, src1 + src1_offset, dst0 + dst_offset,
165  w, h, src0_stride, src1_stride, dst_stride);
166  call_new(src0 + src0_offset, src1 + src1_offset, dst1 + dst_offset,
167  w, h, src0_stride, src1_stride, dst_stride);
168  // Check a one pixel-pair edge around the destination area,
169  // to catch overwrites past the end.
170  checkasm_check(uint8_t, dst0, 2*MAX_STRIDE, dst1, 2*MAX_STRIDE,
171  2 * w + 2, h + 1, "dst");
172  }
173 
174  bench_new(src0, src1, dst1, 127, MAX_HEIGHT,
176  }
177  if (check_func(interleaveBytes, "interleave_bytes_aligned")) {
178  // Bench the function in a more typical case, with aligned
179  // buffers and widths.
180  bench_new(src0_buf, src1_buf, dst1_buf, 128, MAX_HEIGHT,
182  }
183 }
184 
185 #define MAX_LINE_SIZE 1920
186 static const int input_sizes[] = {8, 128, 1080, MAX_LINE_SIZE};
187 static const enum AVPixelFormat rgb_formats[] = {
194 };
195 
196 static void check_rgb_to_y(struct SwsContext *ctx)
197 {
198  LOCAL_ALIGNED_16(uint8_t, src24, [MAX_LINE_SIZE * 3]);
199  LOCAL_ALIGNED_16(uint8_t, src32, [MAX_LINE_SIZE * 4]);
200  LOCAL_ALIGNED_32(uint8_t, dst0_y, [MAX_LINE_SIZE * 2]);
201  LOCAL_ALIGNED_32(uint8_t, dst1_y, [MAX_LINE_SIZE * 2]);
202 
203  declare_func(void, uint8_t *dst, const uint8_t *src,
204  const uint8_t *unused1, const uint8_t *unused2, int width,
205  uint32_t *rgb2yuv, void *opq);
206 
207  randomize_buffers(src24, MAX_LINE_SIZE * 3);
208  randomize_buffers(src32, MAX_LINE_SIZE * 4);
209 
210  for (int i = 0; i < FF_ARRAY_ELEMS(rgb_formats); i++) {
212 
213  ctx->srcFormat = rgb_formats[i];
215 
216  for (int j = 0; j < FF_ARRAY_ELEMS(input_sizes); j++) {
217  int w = input_sizes[j];
218 
219  if (check_func(ctx->lumToYV12, "%s_to_y_%d", desc->name, w)) {
220  const uint8_t *src = desc->nb_components == 3 ? src24 : src32;
221  memset(dst0_y, 0xFA, MAX_LINE_SIZE * 2);
222  memset(dst1_y, 0xFA, MAX_LINE_SIZE * 2);
223 
224  call_ref(dst0_y, src, NULL, NULL, w, ctx->input_rgb2yuv_table, NULL);
225  call_new(dst1_y, src, NULL, NULL, w, ctx->input_rgb2yuv_table, NULL);
226 
227  if (memcmp(dst0_y, dst1_y, w * 2))
228  fail();
229 
230  if (desc->nb_components == 3 ||
231  // only bench native endian formats
232  (ctx->srcFormat == AV_PIX_FMT_RGB32 || ctx->srcFormat == AV_PIX_FMT_RGB32_1))
233  bench_new(dst1_y, src, NULL, NULL, w, ctx->input_rgb2yuv_table, NULL);
234  }
235  }
236  }
237 }
238 
239 static void check_rgb_to_uv(struct SwsContext *ctx)
240 {
241  LOCAL_ALIGNED_16(uint8_t, src24, [MAX_LINE_SIZE * 3]);
242  LOCAL_ALIGNED_16(uint8_t, src32, [MAX_LINE_SIZE * 4]);
243  LOCAL_ALIGNED_16(uint8_t, dst0_u, [MAX_LINE_SIZE * 2]);
244  LOCAL_ALIGNED_16(uint8_t, dst0_v, [MAX_LINE_SIZE * 2]);
245  LOCAL_ALIGNED_16(uint8_t, dst1_u, [MAX_LINE_SIZE * 2]);
246  LOCAL_ALIGNED_16(uint8_t, dst1_v, [MAX_LINE_SIZE * 2]);
247 
248  declare_func(void, uint8_t *dstU, uint8_t *dstV,
249  const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
250  int width, uint32_t *pal, void *opq);
251 
252  randomize_buffers(src24, MAX_LINE_SIZE * 3);
253  randomize_buffers(src32, MAX_LINE_SIZE * 4);
254 
255  for (int i = 0; i < 2 * FF_ARRAY_ELEMS(rgb_formats); i++) {
256  enum AVPixelFormat src_fmt = rgb_formats[i / 2];
257  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src_fmt);
258 
259  ctx->chrSrcHSubSample = (i % 2) ? 0 : 1;
260  ctx->srcFormat = src_fmt;
261  ctx->dstFormat = ctx->chrSrcHSubSample ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUV444P;
263 
264  for (int j = 0; j < FF_ARRAY_ELEMS(input_sizes); j++) {
265  int w = input_sizes[j] >> ctx->chrSrcHSubSample;
266 
267  if (check_func(ctx->chrToYV12, "%s_to_uv%s_%d", desc->name,
268  ctx->chrSrcHSubSample ? "_half" : "",
269  input_sizes[j])) {
270  const uint8_t *src = desc->nb_components == 3 ? src24 : src32;
271  memset(dst0_u, 0xFF, MAX_LINE_SIZE * 2);
272  memset(dst0_v, 0xFF, MAX_LINE_SIZE * 2);
273  memset(dst1_u, 0xFF, MAX_LINE_SIZE * 2);
274  memset(dst1_v, 0xFF, MAX_LINE_SIZE * 2);
275 
276  call_ref(dst0_u, dst0_v, NULL, src, src, w, ctx->input_rgb2yuv_table, NULL);
277  call_new(dst1_u, dst1_v, NULL, src, src, w, ctx->input_rgb2yuv_table, NULL);
278 
279  if (memcmp(dst0_u, dst1_u, w * 2) || memcmp(dst0_v, dst1_v, w * 2))
280  fail();
281 
282  if (desc->nb_components == 3 ||
283  // only bench native endian formats
284  (ctx->srcFormat == AV_PIX_FMT_RGB32 || ctx->srcFormat == AV_PIX_FMT_RGB32_1))
285  bench_new(dst1_u, dst1_v, NULL, src, src, w, ctx->input_rgb2yuv_table, NULL);
286  }
287  }
288  }
289 }
290 
292 {
293  struct SwsContext *ctx;
294 
296 
297  check_shuffle_bytes(shuffle_bytes_2103, "shuffle_bytes_2103");
298  report("shuffle_bytes_2103");
299 
300  check_shuffle_bytes(shuffle_bytes_0321, "shuffle_bytes_0321");
301  report("shuffle_bytes_0321");
302 
303  check_shuffle_bytes(shuffle_bytes_1230, "shuffle_bytes_1230");
304  report("shuffle_bytes_1230");
305 
306  check_shuffle_bytes(shuffle_bytes_3012, "shuffle_bytes_3012");
307  report("shuffle_bytes_3012");
308 
309  check_shuffle_bytes(shuffle_bytes_3210, "shuffle_bytes_3210");
310  report("shuffle_bytes_3210");
311 
313  report("uyvytoyuv422");
314 
316  report("interleave_bytes");
317 
321  if (!ctx)
322  fail();
323 
325  report("rgb_to_y");
326 
328  report("rgb_to_uv");
329 
331 }
func
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:68
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
shuffle_bytes_3012
void(* shuffle_bytes_3012)(const uint8_t *src, uint8_t *dst, int src_size)
Definition: rgb2rgb.c:57
mem_internal.h
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
MAX_STRIDE
#define MAX_STRIDE
Definition: sw_rgb.c:45
src1
const pixel * src1
Definition: h264pred_template.c:421
rgb_formats
static enum AVPixelFormat rgb_formats[]
Definition: sw_rgb.c:187
h
uint8_t h
Definition: sw_rgb.c:41
pixdesc.h
check_func
#define check_func(func,...)
Definition: checkasm.h:177
shuffle_bytes_3210
void(* shuffle_bytes_3210)(const uint8_t *src, uint8_t *dst, int src_size)
Definition: rgb2rgb.c:58
rgb2yuv
static const char rgb2yuv[]
Definition: vf_scale_vulkan.c:70
AV_PIX_FMT_RGB32_1
#define AV_PIX_FMT_RGB32_1
Definition: pixfmt.h:452
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:76
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
call_ref
#define call_ref(...)
Definition: checkasm.h:192
check_shuffle_bytes
static void check_shuffle_bytes(void *func, const char *report)
Definition: sw_rgb.c:48
s
uint8_t s
Definition: sw_rgb.c:41
SWS_BITEXACT
#define SWS_BITEXACT
Definition: swscale.h:91
fail
#define fail()
Definition: checkasm.h:186
checkasm.h
randomize_buffers
#define randomize_buffers(buf, size)
Definition: sw_rgb.c:33
rnd
#define rnd()
Definition: checkasm.h:170
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
intreadwrite.h
check_uyvy_to_422p
static void check_uyvy_to_422p(void)
Definition: sw_rgb.c:74
input_sizes
static const int input_sizes[]
Definition: sw_rgb.c:186
shuffle_bytes_1230
void(* shuffle_bytes_1230)(const uint8_t *src, uint8_t *dst, int src_size)
Definition: rgb2rgb.c:56
LOCAL_ALIGNED_16
#define LOCAL_ALIGNED_16(t, v,...)
Definition: mem_internal.h:150
ctx
AVFormatContext * ctx
Definition: movenc.c:49
shuffle_bytes_2103
void(* shuffle_bytes_2103)(const uint8_t *src, uint8_t *dst, int src_size)
Definition: rgb2rgb.c:55
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
interleaveBytes
void(* interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dst, int width, int height, int src1Stride, int src2Stride, int dstStride)
Definition: rgb2rgb.c:88
checkasm_check_sw_rgb
void checkasm_check_sw_rgb(void)
Definition: sw_rgb.c:291
call_new
#define call_new(...)
Definition: checkasm.h:295
NULL
#define NULL
Definition: coverity.c:32
LOCAL_ALIGNED_32
#define LOCAL_ALIGNED_32(t, v,...)
Definition: mem_internal.h:156
ff_sws_rgb2rgb_init
av_cold void ff_sws_rgb2rgb_init(void)
Definition: rgb2rgb.c:137
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:101
MAX_HEIGHT
#define MAX_HEIGHT
Definition: sw_rgb.c:46
check_rgb_to_y
static void check_rgb_to_y(struct SwsContext *ctx)
Definition: sw_rgb.c:196
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:75
ff_sws_init_scale
void ff_sws_init_scale(SwsContext *c)
Definition: swscale.c:591
check_interleave_bytes
static void check_interleave_bytes(void)
Definition: sw_rgb.c:117
sws_getContext
struct SwsContext * sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
Definition: utils.c:2101
MAX_LINE_SIZE
#define MAX_LINE_SIZE
Definition: sw_rgb.c:185
shuffle_bytes_0321
void(* shuffle_bytes_0321)(const uint8_t *src, uint8_t *dst, int src_size)
Definition: rgb2rgb.c:54
height
#define height
AV_PIX_FMT_RGB32
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:451
SWS_ACCURATE_RND
#define SWS_ACCURATE_RND
Definition: swscale.h:90
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:99
uyvytoyuv422
void(* uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src, int width, int height, int lumStride, int chromStride, int srcStride)
Definition: rgb2rgb.c:107
report
#define report
Definition: checkasm.h:189
bench_new
#define bench_new(...)
Definition: checkasm.h:366
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
src2
const pixel * src2
Definition: h264pred_template.c:422
common.h
swscale_internal.h
width
static const uint8_t width[]
Definition: sw_rgb.c:40
w
uint8_t w
Definition: sw_rgb.c:41
check_rgb_to_uv
static void check_rgb_to_uv(struct SwsContext *ctx)
Definition: sw_rgb.c:239
sws_freeContext
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:2432
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
src0
const pixel *const src0
Definition: h264pred_template.c:420
desc
const char * desc
Definition: libsvtav1.c:79
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
declare_func
#define declare_func(ret,...)
Definition: checkasm.h:181
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
planes
static const struct @439 planes[]
checkasm_check
#define checkasm_check(prefix,...)
Definition: checkasm.h:386
SwsContext
Definition: swscale_internal.h:299
rgb2rgb.h
swscale.h