This rewrite cleans up the code to use AVFrames and the new swscale API. The log format has also been simplified and expanded to account for the new options. (Not yet implemented) The self testing code path has also been expanded to test the new swscale implementation against the old one, to serve as an unchanging reference. This does not accomplish much yet, but serves as a framework for future work.
Unified · Split
| @@ -1,4 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (C) 2024 Nikles Haas | ||
| 2 | * Copyright (C) 2003-2011 Michael Niedermayer <michaelni@gmx.at> | 3 | * Copyright (C) 2003-2011 Michael Niedermayer <michaelni@gmx.at> |
| 3 | * | 4 | * |
| 4 | * This file is part of FFmpeg. | 5 | * This file is part of FFmpeg. |
| @@ -26,424 +27,307 @@ · 21 unmodified lines | |||
| 26 | 27 | ||
| 27 | #undef HAVE_AV_CONFIG_H | 28 | #undef HAVE_AV_CONFIG_H |
| 28 | #include "libavutil/cpu.h" | 29 | #include "libavutil/cpu.h" |
| 29 | #include "libavutil/imgutils.h" | ||
| 30 | #include "libavutil/mem.h" | ||
| 31 | #include "libavutil/avutil.h" | ||
| 32 | #include "libavutil/crc.h" | ||
| 33 | #include "libavutil/opt.h" | ||
| 34 | #include "libavutil/pixdesc.h" | 30 | #include "libavutil/pixdesc.h" |
| 35 | #include "libavutil/lfg.h" | 31 | #include "libavutil/lfg.h" |
| 36 | #include "libavutil/sfc64.h" | 32 | #include "libavutil/sfc64.h" |
| 33 | #include "libavutil/frame.h" | ||
| 34 | #include "libavutil/pixfmt.h" | ||
| 35 | #include "libavutil/avassert.h" | ||
| 36 | #include "libavutil/macros.h" | ||
| 37 | 37 | ||
| 38 | #include "libswscale/swscale.h" | 38 | #include "libswscale/swscale.h" |
| 39 | 39 | ||
| 40 | /* HACK Duplicated from swscale_internal.h. | 40 | enum { |
| 41 | * Should be removed when a cleaner pixel format system exists. */ | 41 | WIDTH = 96, |
| 42 | #define isGray(x) \ | 42 | HEIGHT = 96, |
| 43 | ((x) == AV_PIX_FMT_GRAY8 || \ | 43 | }; |
| 44 | (x) == AV_PIX_FMT_YA8 || \ | ||
| 45 | (x) == AV_PIX_FMT_GRAY16BE || \ | ||
| 46 | (x) == AV_PIX_FMT_GRAY16LE || \ | ||
| 47 | (x) == AV_PIX_FMT_YA16BE || \ | ||
| 48 | (x) == AV_PIX_FMT_YA16LE) | ||
| 49 | #define hasChroma(x) \ | ||
| 50 | (!(isGray(x) || \ | ||
| 51 | (x) == AV_PIX_FMT_MONOBLACK || \ | ||
| 52 | (x) == AV_PIX_FMT_MONOWHITE)) | ||
| 53 | |||
| 54 | static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt) | ||
| 55 | { | ||
| 56 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | ||
| 57 | return desc->flags & AV_PIX_FMT_FLAG_ALPHA; | ||
| 58 | } | ||
| 59 | 44 | ||
| 60 | static double prob = 1; | 45 | struct options { |
| 61 | FFSFC64 prng_state; | 46 | enum AVPixelFormat src_fmt; |
| 47 | enum AVPixelFormat dst_fmt; | ||
| 48 | double prob; | ||
| 49 | }; | ||
| 62 | 50 | ||
| 63 | static uint64_t getSSD(const uint8_t *src1, const uint8_t *src2, | 51 | struct mode { |
| 64 | int stride1, int stride2, int w, int h) | 52 | SwsFlags flags; |
| 65 | { | 53 | SwsDither dither; |
| 66 | int x, y; | 54 | }; |
| 67 | uint64_t ssd = 0; | ||
| 68 | 55 | ||
| 69 | for (y = 0; y < h; y++) { | 56 | const int dst_w[] = { WIDTH, WIDTH - WIDTH / 3, WIDTH + WIDTH / 3 }; |
| 70 | for (x = 0; x < w; x++) { | 57 | const int dst_h[] = { HEIGHT, HEIGHT - HEIGHT / 3, HEIGHT + HEIGHT / 3 }; |
| 71 | int d = src1[x + y * stride1] - src2[x + y * stride2]; | 58 | |
| 72 | ssd += d * d; | 59 | const struct mode modes[] = { |
| 73 | } | 60 | { SWS_FAST_BILINEAR }, |
| 74 | } | 61 | { SWS_BILINEAR }, |
| 75 | return ssd; | 62 | { SWS_BICUBIC }, |
| 76 | } | 63 | { SWS_X | SWS_BITEXACT }, |
| 64 | { SWS_POINT }, | ||
| 65 | { SWS_AREA | SWS_ACCURATE_RND }, | ||
| 66 | { SWS_BICUBIC | SWS_FULL_CHR_H_INT | SWS_FULL_CHR_H_INP }, | ||
| 67 | {0}, // test defaults | ||
| 68 | }; | ||
| 77 | 69 | ||
| 78 | static uint64_t getSSD0(int ref, const uint8_t *src1, int stride1, | 70 | static FFSFC64 prng_state; |
| 79 | int w, int h) | 71 | static SwsContext *sws[3]; /* reused between tests for efficiency */ |
| 80 | { | ||
| 81 | int x, y; | ||
| 82 | uint64_t ssd = 0; | ||
| 83 | 72 | ||
| 84 | for (y = 0; y < h; y++) { | 73 | static int fmt_comps(enum AVPixelFormat fmt) |
| 85 | for (x = 0; x < w; x++) { | 74 | { |
| 86 | int d = src1[x + y * stride1] - ref; | 75 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt); |
| 87 | ssd += d * d; | 76 | int comps = desc->nb_components >= 3 ? 0b111 : 0b1; |
| 88 | } | 77 | if (desc->flags & AV_PIX_FMT_FLAG_ALPHA) |
| 89 | } | 78 | comps |= 0b1000; |
| 90 | return ssd; | 79 | return comps; |
| 91 | } | 80 | } |
| 92 | 81 | ||
| 93 | struct Results { | 82 | static void get_mse(int mse[4], const AVFrame *a, const AVFrame *b, int comps) |
| 94 | uint64_t ssdY; | ||
| 95 | uint64_t ssdU; | ||
| 96 | uint64_t ssdV; | ||
| 97 | uint64_t ssdA; | ||
| 98 | uint32_t crc; | ||
| 99 | }; | ||
| 100 | |||
| 101 | // test by ref -> src -> dst -> out & compare out against ref | ||
| 102 | // ref & out are YV12 | ||
| 103 | static int doTest(const uint8_t * const ref[4], int refStride[4], int w, int h, | ||
| 104 | enum AVPixelFormat srcFormat, enum AVPixelFormat dstFormat, | ||
| 105 | int srcW, int srcH, int dstW, int dstH, int flags, | ||
| 106 | struct Results *r) | ||
| 107 | { | 83 | { |
| 108 | const AVPixFmtDescriptor *desc_yuva420p = av_pix_fmt_desc_get(AV_PIX_FMT_YUVA420P); | 84 | av_assert1(a->format == AV_PIX_FMT_YUVA420P); |
| 109 | const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat); | 85 | av_assert1(b->format == a->format); |
| 110 | const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat); | 86 | av_assert1(b->width == a->width && b->height == a->height); |
| 111 | static enum AVPixelFormat cur_srcFormat; | 87 | |
| 112 | static int cur_srcW, cur_srcH; | 88 | for (int p = 0; p < 4; p++) { |
| 113 | static const uint8_t *src[4]; | 89 | const int is_chroma = p == 1 || p == 2; |
| 114 | static int srcStride[4]; | 90 | const int stride_a = a->linesize[p]; |
| 115 | uint8_t *dst[4] = { 0 }; | 91 | const int stride_b = b->linesize[p]; |
| 116 | uint8_t *out[4] = { 0 }; | 92 | const int w = (a->width + is_chroma) >> is_chroma; |
| 117 | int dstStride[4] = {0}; | 93 | const int h = (a->height + is_chroma) >> is_chroma; |
| 118 | int i; | 94 | uint64_t sum = 0; |
| 119 | uint64_t ssdY, ssdU = 0, ssdV = 0, ssdA = 0; | 95 | |
| 120 | SwsContext *dstContext = NULL, *outContext = NULL; | 96 | if (comps & (1 << p)) { |
| 121 | uint32_t crc = 0; | 97 | for (int y = 0; y < h; y++) { |
| 122 | int res = 0; | 98 | for (int x = 0; x < w; x++) { |
| 123 | 99 | int d = a->data[p][y * stride_a + x] - b->data[p][y * stride_b + x]; | |
| 124 | if (ff_sfc64_get(&prng_state) > UINT64_MAX * prob) | 100 | sum += d * d; |
| 125 | return 0; | 101 | } |
| 126 | 102 | } | |
| 127 | if (cur_srcFormat != srcFormat || cur_srcW != srcW || cur_srcH != srcH) { | 103 | } else { |
| 128 | SwsContext *srcContext = NULL; | 104 | const int ref = is_chroma ? 128 : 0xFF; |
| 129 | int p; | 105 | for (int y = 0; y < h; y++) { |
| 130 | 106 | for (int x = 0; x < w; x++) { | |
| 131 | for (p = 0; p < 4; p++) | 107 | int d = a->data[p][y * stride_a + x] - ref; |
| 132 | av_freep(&src[p]); | 108 | sum += d * d; |
| 133 | 109 | } | |
| 134 | res = av_image_fill_linesizes(srcStride, srcFormat, srcW); | ||
| 135 | if (res < 0) { | ||
| 136 | fprintf(stderr, "av_image_fill_linesizes failed\n"); | ||
| 137 | goto end; | ||
| 138 | } | ||
| 139 | for (p = 0; p < 4; p++) { | ||
| 140 | srcStride[p] = FFALIGN(srcStride[p], 16); | ||
| 141 | if (srcStride[p]) | ||
| 142 | src[p] = av_mallocz(srcStride[p] * srcH + 16); | ||
| 143 | if (srcStride[p] && !src[p]) { | ||
| 144 | perror("Malloc"); | ||
| 145 | res = -1; | ||
| 146 | goto end; | ||
| 147 | } | 110 | } |
| 148 | } | 111 | } |
| 149 | srcContext = sws_getContext(w, h, AV_PIX_FMT_YUVA420P, srcW, srcH, | ||
| 150 | srcFormat, SWS_BILINEAR, NULL, NULL, NULL); | ||
| 151 | if (!srcContext) { | ||
| 152 | fprintf(stderr, "Failed to get %s ---> %s\n", | ||
| 153 | desc_yuva420p->name, | ||
| 154 | desc_src->name); | ||
| 155 | res = -1; | ||
| 156 | goto end; | ||
| 157 | } | ||
| 158 | sws_scale(srcContext, ref, refStride, 0, h, | ||
| 159 | (uint8_t * const *) src, srcStride); | ||
| 160 | sws_freeContext(srcContext); | ||
| 161 | 112 | ||
| 162 | cur_srcFormat = srcFormat; | 113 | mse[p] = sum / (w * h); |
| 163 | cur_srcW = srcW; | ||
| 164 | cur_srcH = srcH; | ||
| 165 | } | 114 | } |
| 115 | } | ||
| 166 | 116 | ||
| 167 | res = av_image_fill_linesizes(dstStride, dstFormat, dstW); | 117 | static int scale_legacy(AVFrame *dst, const AVFrame *src, struct mode mode) |
| 168 | if (res < 0) { | 118 | { |
| 169 | fprintf(stderr, "av_image_fill_linesizes failed\n"); | 119 | SwsContext *sws_legacy; |
| 170 | goto end; | 120 | int ret; |
| 171 | } | ||
| 172 | 121 | ||
| 173 | for (i = 0; i < 4; i++) { | 122 | sws_legacy = sws_alloc_context(); |
| 174 | /* Image buffers passed into libswscale can be allocated any way you | 123 | if (!sws_legacy) |
| 175 | * prefer, as long as they're aligned enough for the architecture, and | 124 | return -1; |
| 176 | * they're freed appropriately (such as using av_free for buffers | 125 | |
| 177 | * allocated with av_malloc). */ | 126 | sws_legacy->src_w = src->width; |
| 178 | /* An extra 16 bytes is being allocated because some scalers may write | 127 | sws_legacy->src_h = src->height; |
| 179 | * out of bounds. */ | 128 | sws_legacy->src_format = src->format; |
| 180 | dstStride[i] = FFALIGN(dstStride[i], 16); | 129 | sws_legacy->dst_w = dst->width; |
| 181 | if (dstStride[i]) | 130 | sws_legacy->dst_h = dst->height; |
| 182 | dst[i] = av_mallocz(dstStride[i] * dstH + 16); | 131 | sws_legacy->dst_format = dst->format; |
| 183 | if (dstStride[i] && !dst[i]) { | 132 | sws_legacy->flags = mode.flags; |
| 184 | perror("Malloc"); | 133 | sws_legacy->dither = mode.dither; |
| 185 | res = -1; | 134 | |
| 186 | 135 | ret = sws_init_context(sws_legacy, NULL, NULL); | |
| 187 | goto end; | 136 | if (!ret) |
| 188 | } | 137 | ret = sws_scale_frame(sws_legacy, dst, src); |
| 189 | } | 138 | |
| 139 | sws_freeContext(sws_legacy); | ||
| 140 | return ret; | ||
| 141 | } | ||
| 142 | |||
| 143 | /* Runs a series of ref -> src -> dst -> out, and compares out vs ref */ | ||
| 144 | static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, | ||
| 145 | int dst_w, int dst_h, struct mode mode, const AVFrame *ref, | ||
| 146 | const int mse_ref[4]) | ||
| 147 | { | ||
| 148 | AVFrame *src = NULL, *dst = NULL, *out = NULL; | ||
| 149 | int mse[4], mse_sws[4], ret = -1; | ||
| 150 | const int comps = fmt_comps(src_fmt) & fmt_comps(dst_fmt); | ||
| 151 | |||
| 152 | src = av_frame_alloc(); | ||
| 153 | dst = av_frame_alloc(); | ||
| 154 | out = av_frame_alloc(); | ||
| 155 | if (!src || !dst || !out) | ||
| 156 | goto error; | ||
| 190 | 157 | ||
| 191 | dstContext = sws_alloc_context(); | 158 | av_frame_copy_props(src, ref); |
| 192 | if (!dstContext) { | 159 | av_frame_copy_props(dst, ref); |
| 193 | fprintf(stderr, "Failed to alloc %s ---> %s\n", | 160 | av_frame_copy_props(out, ref); |
| 194 | desc_src->name, desc_dst->name); | 161 | src->width = out->width = ref->width; |
| 195 | res = -1; | 162 | src->height = out->height = ref->height; |
| 196 | goto end; | 163 | out->format = ref->format; |
| 164 | src->format = src_fmt; | ||
| 165 | dst->format = dst_fmt; | ||
| 166 | dst->width = dst_w; | ||
| 167 | dst->height = dst_h; | ||
| 168 | |||
| 169 | if (sws_scale_frame(sws[0], src, ref) < 0) { | ||
| 170 | fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(ref->format), | ||
| 171 | av_get_pix_fmt_name(src->format)); | ||
| 172 | goto error; | ||
| 197 | } | 173 | } |
| 198 | 174 | ||
| 199 | av_opt_set_int(dstContext, "sws_flags", flags, 0); | 175 | sws[1]->flags = mode.flags; |
| 200 | av_opt_set_int(dstContext, "srcw", srcW, 0); | 176 | sws[1]->dither = mode.dither; |
| 201 | av_opt_set_int(dstContext, "srch", srcH, 0); | 177 | if (sws_scale_frame(sws[1], dst, src) < 0) { |
| 202 | av_opt_set_int(dstContext, "dstw", dstW, 0); | 178 | fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(src->format), |
| 203 | av_opt_set_int(dstContext, "dsth", dstH, 0); | 179 | av_get_pix_fmt_name(dst->format)); |
| 204 | av_opt_set_int(dstContext, "src_format", srcFormat, 0); | 180 | goto error; |
| 205 | av_opt_set_int(dstContext, "dst_format", dstFormat, 0); | ||
| 206 | av_opt_set(dstContext, "alphablend", "none", 0); | ||
| 207 | |||
| 208 | if (sws_init_context(dstContext, NULL, NULL) < 0) { | ||
| 209 | sws_freeContext(dstContext); | ||
| 210 | fprintf(stderr, "Failed to init %s ---> %s\n", | ||
| 211 | desc_src->name, desc_dst->name); | ||
| 212 | res = -1; | ||
| 213 | goto end; | ||
| 214 | } | 181 | } |
| 215 | 182 | ||
| 216 | printf(" %s %dx%d -> %s %3dx%3d flags=%2d", | 183 | if (sws_scale_frame(sws[2], out, dst) < 0) { |
| 217 | desc_src->name, srcW, srcH, | 184 | fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(dst->format), |
| 218 | desc_dst->name, dstW, dstH, | 185 | av_get_pix_fmt_name(out->format)); |
| 219 | flags); | 186 | goto error; |
| 220 | fflush(stdout); | 187 | } |
| 221 | 188 | ||
| 222 | sws_scale(dstContext, (const uint8_t * const*)src, srcStride, 0, srcH, dst, dstStride); | 189 | get_mse(mse, out, ref, comps); |
| 223 | 190 | printf("%s %dx%d -> %s %3dx%3d, flags=%u dither=%u, " | |
| 224 | for (i = 0; i < 4 && dstStride[i]; i++) | 191 | "MSE={%5d %5d %5d %5d}\n", |
| 225 | crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i], | 192 | av_get_pix_fmt_name(src->format), src->width, src->height, |
| 226 | dstStride[i] * dstH); | 193 | av_get_pix_fmt_name(dst->format), dst->width, dst->height, |
| 227 | 194 | mode.flags, mode.dither, | |
| 228 | if (r && crc == r->crc) { | 195 | mse[0], mse[1], mse[2], mse[3]); |
| 229 | ssdY = r->ssdY; | 196 | |
| 230 | ssdU = r->ssdU; | 197 | if (!mse_ref) { |
| 231 | ssdV = r->ssdV; | 198 | /* Compare against the legacy swscale API as a reference */ |
| 232 | ssdA = r->ssdA; | 199 | if (scale_legacy(dst, src, mode) < 0) { |
| 233 | } else { | 200 | fprintf(stderr, "Failed ref %s ---> %s\n", av_get_pix_fmt_name(src->format), |
| 234 | for (i = 0; i < 4; i++) { | 201 | av_get_pix_fmt_name(dst->format)); |
| 235 | refStride[i] = FFALIGN(refStride[i], 16); | 202 | goto error; |
| 236 | if (refStride[i]) | ||
| 237 | out[i] = av_mallocz(refStride[i] * h); | ||
| 238 | if (refStride[i] && !out[i]) { | ||
| 239 | perror("Malloc"); | ||
| 240 | res = -1; | ||
| 241 | goto end; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | outContext = sws_getContext(dstW, dstH, dstFormat, w, h, | ||
| 245 | AV_PIX_FMT_YUVA420P, SWS_BILINEAR, | ||
| 246 | NULL, NULL, NULL); | ||
| 247 | if (!outContext) { | ||
| 248 | fprintf(stderr, "Failed to get %s ---> %s\n", | ||
| 249 | desc_dst->name, | ||
| 250 | desc_yuva420p->name); | ||
| 251 | res = -1; | ||
| 252 | goto end; | ||
| 253 | } | ||
| 254 | sws_scale(outContext, (const uint8_t * const *) dst, dstStride, 0, dstH, | ||
| 255 | out, refStride); | ||
| 256 | |||
| 257 | ssdY = getSSD(ref[0], out[0], refStride[0], refStride[0], w, h); | ||
| 258 | if (hasChroma(srcFormat) && hasChroma(dstFormat)) { | ||
| 259 | //FIXME check that output is really gray | ||
| 260 | ssdU = getSSD(ref[1], out[1], refStride[1], refStride[1], | ||
| 261 | (w + 1) >> 1, (h + 1) >> 1); | ||
| 262 | ssdV = getSSD(ref[2], out[2], refStride[2], refStride[2], | ||
| 263 | (w + 1) >> 1, (h + 1) >> 1); | ||
| 264 | } else { | ||
| 265 | ssdU = getSSD0(128, out[1], refStride[1], | ||
| 266 | (w + 1) >> 1, (h + 1) >> 1); | ||
| 267 | ssdV = getSSD0(128, out[2], refStride[2], | ||
| 268 | (w + 1) >> 1, (h + 1) >> 1); | ||
| 269 | } | ||
| 270 | if (isALPHA(srcFormat) && isALPHA(dstFormat)) { | ||
| 271 | ssdA = getSSD(ref[3], out[3], refStride[3], refStride[3], w, h); | ||
| 272 | } else { | ||
| 273 | ssdA = getSSD0(0xFF, out[3], refStride[3], w, h); | ||
| 274 | } | 203 | } |
| 275 | 204 | ||
| 276 | ssdY /= w * h; | 205 | if (sws_scale_frame(sws[2], out, dst) < 0) |
| 277 | ssdU /= w * h / 4; | 206 | goto error; |
| 278 | ssdV /= w * h / 4; | ||
| 279 | ssdA /= w * h; | ||
| 280 | |||
| 281 | sws_freeContext(outContext); | ||
| 282 | 207 | ||
| 283 | for (i = 0; i < 4; i++) | 208 | get_mse(mse_sws, out, ref, comps); |
| 284 | if (refStride[i]) | 209 | mse_ref = mse_sws; |
| 285 | av_free(out[i]); | ||
| 286 | } | 210 | } |
| 287 | 211 | ||
| 288 | if(r){ | 212 | for (int i = 0; i < 4; i++) { |
| 289 | if(ssdY>r->ssdY*1.02+1 || ssdU>r->ssdU*1.02+1 || ssdV>r->ssdV*1.02+1|| ssdA>r->ssdA*1.02+1) | 213 | if (mse[i] > mse_ref[i]) { |
| 290 | printf("WORSE SSD=%5"PRId64",%5"PRId64",%5"PRId64",%5"PRId64"", | 214 | int bad = mse[i] > mse_ref[i] * 1.02 + 1; |
| 291 | r->ssdY, r->ssdU, r->ssdV, r->ssdA); | 215 | printf("\033[1;31m %s, ref MSE={%5d %5d %5d %5d}\033[0m\n", |
| 292 | else if(ssdY>r->ssdY || ssdU>r->ssdU || ssdV>r->ssdV|| ssdA>r->ssdA) | 216 | bad ? "WORSE" : "worse", |
| 293 | printf("worse SSD=%5"PRId64",%5"PRId64",%5"PRId64",%5"PRId64"", | 217 | mse_ref[0], mse_ref[1], mse_ref[2], mse_ref[3]); |
| 294 | r->ssdY, r->ssdU, r->ssdV, r->ssdA); | 218 | if (bad) |
| 219 | goto error; | ||
| 220 | break; | ||
| 221 | } | ||
| 295 | } | 222 | } |
| 296 | 223 | ||
| 297 | printf(" CRC=%08x SSD=%5"PRId64 ",%5"PRId64 ",%5"PRId64 ",%5"PRId64 "\n", | 224 | fflush(stdout); |
| 298 | crc, ssdY, ssdU, ssdV, ssdA); | 225 | ret = 0; /* fall through */ |
| 299 | 226 | error: | |
| 300 | end: | 227 | av_frame_free(&src); |
| 301 | sws_freeContext(dstContext); | 228 | av_frame_free(&dst); |
| 302 | 229 | av_frame_free(&out); | |
| 303 | for (i = 0; i < 4; i++) | 230 | return ret; |
| 304 | if (dstStride[i]) | ||
| 305 | av_free(dst[i]); | ||
| 306 | |||
| 307 | return !!res; | ||
| 308 | } | 231 | } |
| 309 | 232 | ||
| 310 | static void selfTest(const uint8_t * const ref[4], int refStride[4], | 233 | static int run_self_tests(const AVFrame *ref, struct options opts) |
| 311 | int w, int h, | ||
| 312 | enum AVPixelFormat srcFormat_in, | ||
| 313 | enum AVPixelFormat dstFormat_in) | ||
| 314 | { | 234 | { |
| 315 | const int flags[] = { SWS_FAST_BILINEAR, | 235 | enum AVPixelFormat src_fmt, dst_fmt, |
| 316 | SWS_BILINEAR, SWS_BICUBIC, | 236 | src_fmt_min = 0, |
| 317 | SWS_X|SWS_BITEXACT , SWS_POINT , SWS_AREA|SWS_ACCURATE_RND, | 237 | dst_fmt_min = 0, |
| 318 | SWS_BICUBIC|SWS_FULL_CHR_H_INT|SWS_FULL_CHR_H_INP, 0}; | 238 | src_fmt_max = AV_PIX_FMT_NB - 1, |
| 319 | const int srcW = w; | 239 | dst_fmt_max = AV_PIX_FMT_NB - 1; |
| 320 | const int srcH = h; | 240 | |
| 321 | const int dstW[] = { srcW - srcW / 3, srcW, srcW + srcW / 3, 0 }; | 241 | if (opts.src_fmt != AV_PIX_FMT_NONE) |
| 322 | const int dstH[] = { srcH - srcH / 3, srcH, srcH + srcH / 3, 0 }; | 242 | src_fmt_min = src_fmt_max = opts.src_fmt; |
| 323 | enum AVPixelFormat srcFormat, dstFormat; | 243 | if (opts.dst_fmt != AV_PIX_FMT_NONE) |
| 324 | const AVPixFmtDescriptor *desc_src, *desc_dst; | 244 | dst_fmt_min = dst_fmt_max = opts.dst_fmt; |
| 325 | 245 | ||
| 326 | for (srcFormat = srcFormat_in != AV_PIX_FMT_NONE ? srcFormat_in : 0; | 246 | for (src_fmt = src_fmt_min; src_fmt <= src_fmt_max; src_fmt++) { |
| 327 | srcFormat < AV_PIX_FMT_NB; srcFormat++) { | 247 | if (!sws_test_format(src_fmt, 0) || !sws_test_format(src_fmt, 1)) |
| 328 | if (!sws_isSupportedInput(srcFormat) || | ||
| 329 | !sws_isSupportedOutput(srcFormat)) | ||
| 330 | continue; | 248 | continue; |
| 331 | 249 | for (dst_fmt = dst_fmt_min; dst_fmt <= dst_fmt_max; dst_fmt++) { | |
| 332 | desc_src = av_pix_fmt_desc_get(srcFormat); | 250 | if (!sws_test_format(dst_fmt, 0) || !sws_test_format(dst_fmt, 1)) |
| 333 | |||
| 334 | for (dstFormat = dstFormat_in != AV_PIX_FMT_NONE ? dstFormat_in : 0; | ||
| 335 | dstFormat < AV_PIX_FMT_NB; dstFormat++) { | ||
| 336 | int i, j, k; | ||
| 337 | int res = 0; | ||
| 338 | |||
| 339 | if (!sws_isSupportedInput(dstFormat) || | ||
| 340 | !sws_isSupportedOutput(dstFormat)) | ||
| 341 | continue; | 251 | continue; |
| 342 | 252 | for (int h = 0; h < FF_ARRAY_ELEMS(dst_h); h++) | |
| 343 | desc_dst = av_pix_fmt_desc_get(dstFormat); | 253 | for (int w = 0; w < FF_ARRAY_ELEMS(dst_w); w++) |
| 344 | 254 | for (int m = 0; m < FF_ARRAY_ELEMS(modes); m++) { | |
| 345 | printf("%s -> %s\n", desc_src->name, desc_dst->name); | 255 | if (ff_sfc64_get(&prng_state) > UINT64_MAX * opts.prob) |
| 346 | fflush(stdout); | 256 | continue; |
| 347 | 257 | if (run_test(src_fmt, dst_fmt, dst_w[w], dst_h[h], | |
| 348 | for (k = 0; flags[k] && !res; k++) | 258 | modes[m], ref, NULL) < 0) |
| 349 | for (i = 0; dstW[i] && !res; i++) | 259 | return -1; |
| 350 | for (j = 0; dstH[j] && !res; j++) | 260 | } |
| 351 | res = doTest(ref, refStride, w, h, | ||
| 352 | srcFormat, dstFormat, | ||
| 353 | srcW, srcH, dstW[i], dstH[j], flags[k], | ||
| 354 | NULL); | ||
| 355 | if (dstFormat_in != AV_PIX_FMT_NONE) | ||
| 356 | break; | ||
| 357 | } | 261 | } |
| 358 | if (srcFormat_in != AV_PIX_FMT_NONE) | ||
| 359 | break; | ||
| 360 | } | 262 | } |
| 263 | |||
| 264 | return 0; | ||
| 361 | } | 265 | } |
| 362 | 266 | ||
| 363 | static int fileTest(const uint8_t * const ref[4], int refStride[4], | 267 | static int run_file_tests(const AVFrame *ref, FILE *fp, struct options opts) |
| 364 | int w, int h, FILE *fp, | ||
| 365 | enum AVPixelFormat srcFormat_in, | ||
| 366 | enum AVPixelFormat dstFormat_in) | ||
| 367 | { | 268 | { |
| 368 | char buf[256]; | 269 | char buf[256]; |
| 270 | int ret; | ||
| 369 | 271 | ||
| 370 | while (fgets(buf, sizeof(buf), fp)) { | 272 | while (fgets(buf, sizeof(buf), fp)) { |
| 371 | struct Results r; | 273 | char src_fmt_str[20], dst_fmt_str[20]; |
| 372 | enum AVPixelFormat srcFormat; | 274 | enum AVPixelFormat src_fmt; |
| 373 | char srcStr[21]; | 275 | enum AVPixelFormat dst_fmt; |
| 374 | int srcW = 0, srcH = 0; | 276 | int sw, sh, dw, dh, mse[4]; |
| 375 | enum AVPixelFormat dstFormat; | 277 | struct mode mode; |
| 376 | char dstStr[21]; | ||
| 377 | int dstW = 0, dstH = 0; | ||
| 378 | int flags; | ||
| 379 | int ret; | ||
| 380 | 278 | ||
| 381 | ret = sscanf(buf, | 279 | ret = sscanf(buf, |
| 382 | " %20s %dx%d -> %20s %dx%d flags=%d CRC=%x" | 280 | " %20s %dx%d -> %20s %dx%d, flags=%u dither=%u, " |
| 383 | " SSD=%"SCNu64 ", %"SCNu64 ", %"SCNu64 ", %"SCNu64 "\n", | 281 | "MSE={%d %d %d %d}\n", |
| 384 | srcStr, &srcW, &srcH, dstStr, &dstW, &dstH, | 282 | src_fmt_str, &sw, &sh, dst_fmt_str, &dw, &dh, |
| 385 | &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA); | 283 | &mode.flags, &mode.dither, |
| 386 | if (ret != 12) { | 284 | &mse[0], &mse[1], &mse[2], &mse[3]); |
| 387 | srcStr[0] = dstStr[0] = 0; | 285 | if (ret != 13) { |
| 388 | ret = sscanf(buf, "%20s -> %20s\n", srcStr, dstStr); | 286 | printf("%s", buf); |
| 287 | continue; | ||
| 389 | } | 288 | } |
| 390 | 289 | ||
| 391 | srcFormat = av_get_pix_fmt(srcStr); | 290 | src_fmt = av_get_pix_fmt(src_fmt_str); |
| 392 | dstFormat = av_get_pix_fmt(dstStr); | 291 | dst_fmt = av_get_pix_fmt(dst_fmt_str); |
| 393 | 292 | if (src_fmt == AV_PIX_FMT_NONE || dst_fmt == AV_PIX_FMT_NONE || | |
| 394 | if (srcFormat == AV_PIX_FMT_NONE || dstFormat == AV_PIX_FMT_NONE || | 293 | sw != ref->width || sh != ref->height || dw > 8192 || dh > 8192 || |
| 395 | srcW > 8192U || srcH > 8192U || dstW > 8192U || dstH > 8192U) { | 294 | mode.dither >= SWS_DITHER_NB) { |
| 396 | fprintf(stderr, "malformed input file\n"); | 295 | fprintf(stderr, "malformed input file\n"); |
| 397 | return -1; | 296 | return -1; |
| 398 | } | 297 | } |
| 399 | if ((srcFormat_in != AV_PIX_FMT_NONE && srcFormat_in != srcFormat) || | 298 | |
| 400 | (dstFormat_in != AV_PIX_FMT_NONE && dstFormat_in != dstFormat)) | 299 | if (opts.src_fmt != AV_PIX_FMT_NONE && src_fmt != opts.src_fmt || |
| 401 | continue; | 300 | opts.dst_fmt != AV_PIX_FMT_NONE && dst_fmt != opts.dst_fmt) |
| 402 | if (ret != 12) { | ||
| 403 | printf("%s", buf); | ||
| 404 | continue; | 301 | continue; |
| 405 | } | ||
| 406 | 302 | ||
| 407 | doTest(ref, refStride, w, h, | 303 | if (run_test(src_fmt, dst_fmt, dw, dh, mode, ref, mse) < 0) |
| 408 | srcFormat, dstFormat, | 304 | return -1; |
| 409 | srcW, srcH, dstW, dstH, flags, | ||
| 410 | &r); | ||
| 411 | } | 305 | } |
| 412 | 306 | ||
| 413 | return 0; | 307 | return 0; |
| 414 | } | 308 | } |
| 415 | 309 | ||
| 416 | #define W 96 | ||
| 417 | #define H 96 | ||
| 418 | |||
| 419 | int main(int argc, char **argv) | 310 | int main(int argc, char **argv) |
| 420 | { | 311 | { |
| 421 | enum AVPixelFormat srcFormat = AV_PIX_FMT_NONE; | 312 | struct options opts = { |
| 422 | enum AVPixelFormat dstFormat = AV_PIX_FMT_NONE; | 313 | .src_fmt = AV_PIX_FMT_NONE, |
| 423 | uint8_t *rgb_data = av_malloc(W * H * 4); | 314 | .dst_fmt = AV_PIX_FMT_NONE, |
| 424 | const uint8_t * const rgb_src[4] = { rgb_data, NULL, NULL, NULL }; | 315 | .prob = 1.0, |
| 425 | int rgb_stride[4] = { 4 * W, 0, 0, 0 }; | 316 | }; |
| 426 | uint8_t *data = av_malloc(4 * W * H); | ||
| 427 | const uint8_t * const src[4] = { data, data + W * H, data + W * H * 2, data + W * H * 3 }; | ||
| 428 | int stride[4] = { W, W, W, W }; | ||
| 429 | int x, y; | ||
| 430 | SwsContext *sws; | ||
| 431 | AVLFG rand; | ||
| 432 | int res = -1; | ||
| 433 | int i; | ||
| 434 | FILE *fp = NULL; | ||
| 435 | 317 | ||
| 436 | if (!rgb_data || !data) | 318 | AVFrame *rgb = NULL, *ref = NULL; |
| 437 | return -1; | 319 | FILE *fp = NULL; |
| 320 | AVLFG rand; | ||
| 321 | int ret = -1; | ||
| 438 | 322 | ||
| 439 | for (i = 1; i < argc; i += 2) { | 323 | for (int i = 1; i < argc; i += 2) { |
| 440 | if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) { | 324 | if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) { |
| 441 | fprintf(stderr, | 325 | fprintf(stderr, |
| 442 | "swscale [options...]\n" | 326 | "swscale [options...]\n" |
| 443 | " -help\n" | 327 | " -help\n" |
| 444 | " This text\n" | 328 | " This text\n" |
| 445 | " -ref <file>\n" | 329 | " -ref <file>\n" |
| 446 | " Uses file as reference to compae tests againsts. Tests that have become worse will contain the string worse or WORSE\n" | 330 | " Uses file as reference to compare tests againsts. Tests that have become worse will contain the string worse or WORSE\n" |
| 447 | " -p <number between 0.0 and 1.0>\n" | 331 | " -p <number between 0.0 and 1.0>\n" |
| 448 | " The percentage of tests or comparisons to perform. Doing all tests will take long and generate over a hundred MB text output\n" | 332 | " The percentage of tests or comparisons to perform. Doing all tests will take long and generate over a hundred MB text output\n" |
| 449 | " It is often convenient to perform a random subset\n" | 333 | " It is often convenient to perform a random subset\n" |
| @@ -454,7 +338,7 @@ int main(int argc, char **argv) · 4 unmodified lines | |||
| 454 | " -cpuflags <cpuflags>\n" | 338 | " -cpuflags <cpuflags>\n" |
| 455 | " Uses the specified cpuflags in the tests\n" | 339 | " Uses the specified cpuflags in the tests\n" |
| 456 | ); | 340 | ); |
| 457 | goto error; | 341 | return 0; |
| 458 | } | 342 | } |
| 459 | if (argv[i][0] != '-' || i + 1 == argc) | 343 | if (argv[i][0] != '-' || i + 1 == argc) |
| 460 | goto bad_option; | 344 | goto bad_option; |
| @@ -466,26 +350,26 @@ int main(int argc, char **argv) · 5 unmodified lines | |||
| 466 | } | 350 | } |
| 467 | } else if (!strcmp(argv[i], "-cpuflags")) { | 351 | } else if (!strcmp(argv[i], "-cpuflags")) { |
| 468 | unsigned flags = av_get_cpu_flags(); | 352 | unsigned flags = av_get_cpu_flags(); |
| 469 | int ret = av_parse_cpu_caps(&flags, argv[i + 1]); | 353 | int res = av_parse_cpu_caps(&flags, argv[i + 1]); |
| 470 | if (ret < 0) { | 354 | if (res < 0) { |
| 471 | fprintf(stderr, "invalid cpu flags %s\n", argv[i + 1]); | 355 | fprintf(stderr, "invalid cpu flags %s\n", argv[i + 1]); |
| 472 | return ret; | 356 | goto error; |
| 473 | } | 357 | } |
| 474 | av_force_cpu_flags(flags); | 358 | av_force_cpu_flags(flags); |
| 475 | } else if (!strcmp(argv[i], "-src")) { | 359 | } else if (!strcmp(argv[i], "-src")) { |
| 476 | srcFormat = av_get_pix_fmt(argv[i + 1]); | 360 | opts.src_fmt = av_get_pix_fmt(argv[i + 1]); |
| 477 | if (srcFormat == AV_PIX_FMT_NONE) { | 361 | if (opts.src_fmt == AV_PIX_FMT_NONE) { |
| 478 | fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]); | 362 | fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]); |
| 479 | return -1; | 363 | goto error; |
| 480 | } | 364 | } |
| 481 | } else if (!strcmp(argv[i], "-dst")) { | 365 | } else if (!strcmp(argv[i], "-dst")) { |
| 482 | dstFormat = av_get_pix_fmt(argv[i + 1]); | 366 | opts.dst_fmt = av_get_pix_fmt(argv[i + 1]); |
| 483 | if (dstFormat == AV_PIX_FMT_NONE) { | 367 | if (opts.dst_fmt == AV_PIX_FMT_NONE) { |
| 484 | fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]); | 368 | fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]); |
| 485 | return -1; | 369 | goto error; |
| 486 | } | 370 | } |
| 487 | } else if (!strcmp(argv[i], "-p")) { | 371 | } else if (!strcmp(argv[i], "-p")) { |
| 488 | prob = atof(argv[i + 1]); | 372 | opts.prob = atof(argv[i + 1]); |
| 489 | } else { | 373 | } else { |
| 490 | bad_option: | 374 | bad_option: |
| 491 | fprintf(stderr, "bad option or argument missing (%s) see -help\n", argv[i]); | 375 | fprintf(stderr, "bad option or argument missing (%s) see -help\n", argv[i]); |
| @@ -494,32 +378,51 @@ bad_option: · 2 unmodified lines | |||
| 494 | } | 378 | } |
| 495 | 379 | ||
| 496 | ff_sfc64_init(&prng_state, 0, 0, 0, 12); | 380 | ff_sfc64_init(&prng_state, 0, 0, 0, 12); |
| 497 | |||
| 498 | sws = sws_getContext(W / 12, H / 12, AV_PIX_FMT_RGB32, W, H, | ||
| 499 | AV_PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL); | ||
| 500 | |||
| 501 | av_lfg_init(&rand, 1); | 381 | av_lfg_init(&rand, 1); |
| 502 | 382 | ||
| 503 | for (y = 0; y < H; y++) | 383 | for (int i = 0; i < 3; i++) { |
| 504 | for (x = 0; x < W * 4; x++) | 384 | sws[i] = sws_alloc_context(); |
| 505 | rgb_data[ x + y * 4 * W] = av_lfg_get(&rand); | 385 | if (!sws[i]) |
| 506 | res = sws_scale(sws, rgb_src, rgb_stride, 0, H / 12, (uint8_t * const *) src, stride); | 386 | goto error; |
| 507 | if (res < 0 || res != H) { | 387 | sws[i]->flags = SWS_BILINEAR; |
| 508 | res = -1; | ||
| 509 | goto error; | ||
| 510 | } | 388 | } |
| 511 | sws_freeContext(sws); | ||
| 512 | av_free(rgb_data); | ||
| 513 | 389 | ||
| 514 | if(fp) { | 390 | rgb = av_frame_alloc(); |
| 515 | res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat); | 391 | if (!rgb) |
| 516 | fclose(fp); | 392 | goto error; |
| 517 | } else { | 393 | rgb->width = WIDTH / 12; |
| 518 | selfTest(src, stride, W, H, srcFormat, dstFormat); | 394 | rgb->height = HEIGHT / 12; |
| 519 | res = 0; | 395 | rgb->format = AV_PIX_FMT_RGBA; |
| 396 | if (av_frame_get_buffer(rgb, 32) < 0) | ||
| 397 | goto error; | ||
| 398 | |||
| 399 | for (int y = 0; y < rgb->height; y++) { | ||
| 400 | for (int x = 0; x < rgb->width; x++) { | ||
| 401 | for (int c = 0; c < 4; c++) | ||
| 402 | rgb->data[0][y * rgb->linesize[0] + x * 4 + c] = av_lfg_get(&rand); | ||
| 403 | } | ||
| 520 | } | 404 | } |
| 521 | error: | ||
| 522 | av_free(data); | ||
| 523 | 405 | ||
| 524 | return res; | 406 | ref = av_frame_alloc(); |
| 407 | if (!ref) | ||
| 408 | goto error; | ||
| 409 | ref->width = WIDTH; | ||
| 410 | ref->height = HEIGHT; | ||
| 411 | ref->format = AV_PIX_FMT_YUVA420P; | ||
| 412 | |||
| 413 | if (sws_scale_frame(sws[0], ref, rgb) < 0) | ||
| 414 | goto error; | ||
| 415 | |||
| 416 | ret = fp ? run_file_tests(ref, fp, opts) | ||
| 417 | : run_self_tests(ref, opts); | ||
| 418 | |||
| 419 | /* fall through */ | ||
| 420 | error: | ||
| 421 | for (int i = 0; i < 3; i++) | ||
| 422 | sws_free_context(&sws[i]); | ||
| 423 | av_frame_free(&rgb); | ||
| 424 | av_frame_free(&ref); | ||
| 425 | if (fp) | ||
| 426 | fclose(fp); | ||
| 427 | return ret; | ||
| 525 | } | 428 | } |