FFmpeg
sw_yuv2rgb.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include <string.h>
20 
21 #include "libavutil/common.h"
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/mem_internal.h"
24 #include "libavutil/pixdesc.h"
25 
26 #include "libswscale/swscale.h"
28 
29 #include "checkasm.h"
30 
31 #define randomize_buffers(buf, size) \
32  do { \
33  for (int j = 0; j < size; j += 4) \
34  AV_WN32(buf + j, rnd()); \
35  } while (0)
36 
37 static const int dst_fmts[] = {
38 // AV_PIX_FMT_BGR48BE,
39 // AV_PIX_FMT_BGR48LE,
40 // AV_PIX_FMT_RGB48BE,
41 // AV_PIX_FMT_RGB48LE,
52 // AV_PIX_FMT_RGB444,
53 // AV_PIX_FMT_BGR444,
54 // AV_PIX_FMT_RGB8,
55 // AV_PIX_FMT_BGR8,
56 // AV_PIX_FMT_RGB4,
57 // AV_PIX_FMT_BGR4,
58 // AV_PIX_FMT_RGB4_BYTE,
59 // AV_PIX_FMT_BGR4_BYTE,
60 // AV_PIX_FMT_MONOBLACK,
62 };
63 
64 static int cmp_off_by_n(const uint8_t *ref, const uint8_t *test, size_t n, int accuracy)
65 {
66  for (size_t i = 0; i < n; i++) {
67  if (abs(ref[i] - test[i]) > accuracy)
68  return 1;
69  }
70  return 0;
71 }
72 
73 static int cmp_555_by_n(const uint8_t *ref, const uint8_t *test, size_t n, int accuracy)
74 {
75  const uint16_t *ref16 = (const uint16_t *) ref;
76  const uint16_t *test16 = (const uint16_t *) test;
77  for (size_t i = 0; i < n; i++) {
78  if (abs(( ref16[i] & 0x1f) - ( test16[i] & 0x1f)) > accuracy)
79  return 1;
80  if (abs(((ref16[i] >> 5) & 0x1f) - ((test16[i] >> 5) & 0x1f)) > accuracy)
81  return 1;
82  if (abs(((ref16[i] >> 10) & 0x1f) - ((test16[i] >> 10) & 0x1f)) > accuracy)
83  return 1;
84  }
85  return 0;
86 }
87 
88 static int cmp_565_by_n(const uint8_t *ref, const uint8_t *test, size_t n, int accuracy)
89 {
90  const uint16_t *ref16 = (const uint16_t *) ref;
91  const uint16_t *test16 = (const uint16_t *) test;
92  for (size_t i = 0; i < n; i++) {
93  if (abs(( ref16[i] & 0x1f) - ( test16[i] & 0x1f)) > accuracy)
94  return 1;
95  if (abs(((ref16[i] >> 5) & 0x3f) - ((test16[i] >> 5) & 0x3f)) > accuracy)
96  return 1;
97  if (abs(((ref16[i] >> 11) & 0x1f) - ((test16[i] >> 11) & 0x1f)) > accuracy)
98  return 1;
99  }
100  return 0;
101 }
102 
103 static void check_yuv2rgb(int src_pix_fmt)
104 {
105  const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
106 #define MAX_LINE_SIZE 1920
107  static const int input_sizes[] = {8, 128, 1080, MAX_LINE_SIZE};
108 
110  int, SwsInternal *c, const uint8_t *src[],
111  int srcStride[], int srcSliceY, int srcSliceH,
112  uint8_t *dst[], int dstStride[]);
113 
114  LOCAL_ALIGNED_8(uint8_t, src_y, [MAX_LINE_SIZE * 2]);
115  LOCAL_ALIGNED_8(uint8_t, src_u, [MAX_LINE_SIZE]);
116  LOCAL_ALIGNED_8(uint8_t, src_v, [MAX_LINE_SIZE]);
117  LOCAL_ALIGNED_8(uint8_t, src_a, [MAX_LINE_SIZE * 2]);
118  const uint8_t *src[4] = { src_y, src_u, src_v, src_a };
119 
120  LOCAL_ALIGNED_8(uint8_t, dst0_0, [2 * MAX_LINE_SIZE * 6]);
121  LOCAL_ALIGNED_8(uint8_t, dst0_1, [2 * MAX_LINE_SIZE]);
122  LOCAL_ALIGNED_8(uint8_t, dst0_2, [2 * MAX_LINE_SIZE]);
123  uint8_t *dst0[4] = { dst0_0, dst0_1, dst0_2 };
124  uint8_t *lines0[4][2] = {
125  { dst0_0, dst0_0 + MAX_LINE_SIZE * 6 },
126  { dst0_1, dst0_1 + MAX_LINE_SIZE },
127  { dst0_2, dst0_2 + MAX_LINE_SIZE }
128  };
129 
130  LOCAL_ALIGNED_8(uint8_t, dst1_0, [2 * MAX_LINE_SIZE * 6]);
131  LOCAL_ALIGNED_8(uint8_t, dst1_1, [2 * MAX_LINE_SIZE]);
132  LOCAL_ALIGNED_8(uint8_t, dst1_2, [2 * MAX_LINE_SIZE]);
133  uint8_t *dst1[4] = { dst1_0, dst1_1, dst1_2 };
134  uint8_t *lines1[4][2] = {
135  { dst1_0, dst1_0 + MAX_LINE_SIZE * 6 },
136  { dst1_1, dst1_1 + MAX_LINE_SIZE },
137  { dst1_2, dst1_2 + MAX_LINE_SIZE }
138  };
139 
140  randomize_buffers(src_y, MAX_LINE_SIZE * 2);
143  randomize_buffers(src_a, MAX_LINE_SIZE * 2);
144 
145  for (int dfi = 0; dfi < FF_ARRAY_ELEMS(dst_fmts); dfi++) {
146  int dst_pix_fmt = dst_fmts[dfi];
147  const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
148  int sample_size = av_get_padded_bits_per_pixel(dst_desc) >> 3;
149  for (int isi = 0; isi < FF_ARRAY_ELEMS(input_sizes); isi++) {
150  SwsContext *sws;
151  SwsInternal *c;
152  int log_level;
153  int width = input_sizes[isi];
154  int srcSliceY = 0;
155  int srcSliceH = 2;
156  int srcStride[4] = {
157  width,
158  width >> src_desc->log2_chroma_w,
159  width >> src_desc->log2_chroma_w,
160  width,
161  };
162  int dstStride[4] = {
163  MAX_LINE_SIZE * 6,
166  };
167 
168  // override log level to prevent spamming of the message
169  // "No accelerated colorspace conversion found from %s to %s"
170  log_level = av_log_get_level();
172  sws = sws_getContext(width, srcSliceH, src_pix_fmt,
173  width, srcSliceH, dst_pix_fmt,
174  0, NULL, NULL, NULL);
175  av_log_set_level(log_level);
176  if (!sws)
177  fail();
178 
179  c = sws_internal(sws);
180  if (check_func(c->convert_unscaled, "%s_%s_%d", src_desc->name, dst_desc->name, width)) {
181  memset(dst0_0, 0xFF, 2 * MAX_LINE_SIZE * 6);
182  memset(dst1_0, 0xFF, 2 * MAX_LINE_SIZE * 6);
183  if (dst_pix_fmt == AV_PIX_FMT_GBRP) {
184  memset(dst0_1, 0xFF, MAX_LINE_SIZE);
185  memset(dst0_2, 0xFF, MAX_LINE_SIZE);
186  memset(dst1_1, 0xFF, MAX_LINE_SIZE);
187  memset(dst1_2, 0xFF, MAX_LINE_SIZE);
188  }
189 
190  call_ref(c, src, srcStride, srcSliceY,
191  srcSliceH, dst0, dstStride);
192  call_new(c, src, srcStride, srcSliceY,
193  srcSliceH, dst1, dstStride);
194 
195  if (dst_pix_fmt == AV_PIX_FMT_ARGB ||
196  dst_pix_fmt == AV_PIX_FMT_ABGR ||
197  dst_pix_fmt == AV_PIX_FMT_RGBA ||
198  dst_pix_fmt == AV_PIX_FMT_BGRA ||
199  dst_pix_fmt == AV_PIX_FMT_RGB24 ||
200  dst_pix_fmt == AV_PIX_FMT_BGR24) {
201  if (cmp_off_by_n(lines0[0][0], lines1[0][0], width * sample_size, 3) ||
202  cmp_off_by_n(lines0[0][1], lines1[0][1], width * sample_size, 3))
203  fail();
204  } else if (dst_pix_fmt == AV_PIX_FMT_RGB565 ||
205  dst_pix_fmt == AV_PIX_FMT_BGR565) {
206  if (cmp_565_by_n(lines0[0][0], lines1[0][0], width, 2) ||
207  cmp_565_by_n(lines0[0][1], lines1[0][1], width, 2))
208  fail();
209  } else if (dst_pix_fmt == AV_PIX_FMT_RGB555 ||
210  dst_pix_fmt == AV_PIX_FMT_BGR555) {
211  if (cmp_555_by_n(lines0[0][0], lines1[0][0], width, 2) ||
212  cmp_555_by_n(lines0[0][1], lines1[0][1], width, 2))
213  fail();
214  } else if (dst_pix_fmt == AV_PIX_FMT_GBRP) {
215  for (int p = 0; p < 3; p++)
216  for (int l = 0; l < 2; l++)
217  if (cmp_off_by_n(lines0[p][l], lines1[p][l], width, 3))
218  fail();
219  } else {
220  fail();
221  }
222 
223  bench_new(c, src, srcStride, srcSliceY,
224  srcSliceH, dst0, dstStride);
225  }
227  }
228  }
229 }
230 
231 #undef MAX_LINE_SIZE
232 
234 {
236  report("yuv420p");
238  report("yuv422p");
240  report("yuva420p");
241 }
declare_func_emms
#define declare_func_emms(cpu_flags, ret,...)
Definition: checkasm.h:190
mem_internal.h
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3170
sws_freeContext
void sws_freeContext(SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:2450
pixdesc.h
AVPixFmtDescriptor::name
const char * name
Definition: pixdesc.h:70
check_func
#define check_func(func,...)
Definition: checkasm.h:184
test
Definition: idctdsp.c:35
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:199
randomize_buffers
#define randomize_buffers(buf, size)
Definition: sw_yuv2rgb.c:31
cmp_565_by_n
static int cmp_565_by_n(const uint8_t *ref, const uint8_t *test, size_t n, int accuracy)
Definition: sw_yuv2rgb.c:88
fail
#define fail()
Definition: checkasm.h:193
checkasm.h
MAX_LINE_SIZE
#define MAX_LINE_SIZE
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
cmp_555_by_n
static int cmp_555_by_n(const uint8_t *ref, const uint8_t *test, size_t n, int accuracy)
Definition: sw_yuv2rgb.c:73
intreadwrite.h
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:108
input_sizes
static const int input_sizes[]
Definition: sw_rgb.c:347
check_yuv2rgb
static void check_yuv2rgb(int src_pix_fmt)
Definition: sw_yuv2rgb.c:103
AVPixFmtDescriptor::log2_chroma_w
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
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
LOCAL_ALIGNED_8
#define LOCAL_ALIGNED_8(t, v,...)
Definition: mem_internal.h:128
av_log_get_level
int av_log_get_level(void)
Get the current log level.
Definition: log.c:442
call_new
#define call_new(...)
Definition: checkasm.h:302
NULL
#define NULL
Definition: coverity.c:32
abs
#define abs(x)
Definition: cuda_runtime.h:35
cmp_off_by_n
static int cmp_off_by_n(const uint8_t *ref, const uint8_t *test, size_t n, int accuracy)
Definition: sw_yuv2rgb.c:64
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:101
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:75
av_get_padded_bits_per_pixel
int av_get_padded_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel for the pixel format described by pixdesc, including any padding ...
Definition: pixdesc.c:3135
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
checkasm_check_sw_yuv2rgb
void checkasm_check_sw_yuv2rgb(void)
Definition: sw_yuv2rgb.c:233
AV_PIX_FMT_BGR555
#define AV_PIX_FMT_BGR555
Definition: pixfmt.h:495
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:99
sws
static SwsContext * sws[3]
Definition: swscale.c:69
report
#define report
Definition: checkasm.h:196
av_log_set_level
void av_log_set_level(int level)
Set the log level.
Definition: log.c:447
bench_new
#define bench_new(...)
Definition: checkasm.h:373
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
common.h
AV_PIX_FMT_RGB555
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:490
swscale_internal.h
AV_PIX_FMT_BGR565
#define AV_PIX_FMT_BGR565
Definition: pixfmt.h:494
AV_PIX_FMT_RGB565
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:489
SwsInternal
Definition: swscale_internal.h:317
sws_getContext
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:2119
AV_CPU_FLAG_MMX
#define AV_CPU_FLAG_MMX
standard MMX
Definition: cpu.h:30
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:117
AV_CPU_FLAG_MMXEXT
#define AV_CPU_FLAG_MMXEXT
SSE integer functions or AMD MMX ext.
Definition: cpu.h:31
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:165
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
dst_fmts
static const int dst_fmts[]
Definition: sw_yuv2rgb.c:37
sws_internal
static SwsInternal * sws_internal(const SwsContext *sws)
Definition: swscale_internal.h:74
width
#define width
Definition: dsp.h:85
SwsContext
Main external API structure.
Definition: swscale.h:182
src
#define src
Definition: vp8dsp.c:248
swscale.h