swscale-example: Don't return random value from main().
[libav.git] / libswscale / swscale-example.c
CommitLineData
c9b99ea6 1/*
d026b45e
DB
2 * Copyright (C) 2003 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
54e3ae2e
DB
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
d026b45e
DB
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
54e3ae2e
DB
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
d026b45e 15 *
54e3ae2e
DB
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
b19bcbaa 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d026b45e 19 */
c9b99ea6
MN
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <inttypes.h>
25#include <stdarg.h>
26
e9e12f0e 27#undef HAVE_AV_CONFIG_H
83da2c6f 28#include "libavutil/avutil.h"
9a034dde 29#include "libavutil/lfg.h"
c9b99ea6 30#include "swscale.h"
c9b99ea6 31
71ed6c8f
RP
32/* HACK Duplicated from swscale_internal.h.
33 * Should be removed when a cleaner pixel format system exists. */
34const char *sws_format_name(enum PixelFormat format);
f5a9c4ee
RP
35#define isGray(x) ( \
36 (x)==PIX_FMT_GRAY8 \
37 || (x)==PIX_FMT_GRAY16BE \
38 || (x)==PIX_FMT_GRAY16LE \
39 )
71ed6c8f
RP
40#define isALPHA(x) ( \
41 (x)==PIX_FMT_BGR32 \
42 || (x)==PIX_FMT_BGR32_1 \
43 || (x)==PIX_FMT_RGB32 \
44 || (x)==PIX_FMT_RGB32_1 \
45 || (x)==PIX_FMT_YUVA420P \
46 )
dc822901 47
c9b99ea6 48static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h){
221b804f
DB
49 int x,y;
50 uint64_t ssd=0;
c9b99ea6
MN
51
52//printf("%d %d\n", w, h);
6a4970ab 53
221b804f
DB
54 for (y=0; y<h; y++){
55 for (x=0; x<w; x++){
56 int d= src1[x + y*stride1] - src2[x + y*stride2];
57 ssd+= d*d;
c9b99ea6 58//printf("%d", abs(src1[x + y*stride1] - src2[x + y*stride2])/26 );
221b804f 59 }
c9b99ea6 60//printf("\n");
221b804f
DB
61 }
62 return ssd;
c9b99ea6
MN
63}
64
65// test by ref -> src -> dst -> out & compare out against ref
66// ref & out are YV12
110f147a 67static int doTest(uint8_t *ref[4], int refStride[4], int w, int h, int srcFormat, int dstFormat,
221b804f 68 int srcW, int srcH, int dstW, int dstH, int flags){
1cd98da8
BF
69 uint8_t *src[4] = {0};
70 uint8_t *dst[4] = {0};
71 uint8_t *out[4] = {0};
110f147a 72 int srcStride[4], dstStride[4];
221b804f 73 int i;
f5a9c4ee 74 uint64_t ssdY, ssdU=0, ssdV=0, ssdA=0;
54ea7736
DB
75 struct SwsContext *srcContext = NULL, *dstContext = NULL,
76 *outContext = NULL;
221b804f
DB
77 int res;
78
79 res = 0;
110f147a 80 for (i=0; i<4; i++){
221b804f
DB
81 // avoid stride % bpp != 0
82 if (srcFormat==PIX_FMT_RGB24 || srcFormat==PIX_FMT_BGR24)
83 srcStride[i]= srcW*3;
5d5a8813
KS
84 else if (srcFormat==PIX_FMT_RGB48BE || srcFormat==PIX_FMT_RGB48LE)
85 srcStride[i]= srcW*6;
221b804f
DB
86 else
87 srcStride[i]= srcW*4;
88
89 if (dstFormat==PIX_FMT_RGB24 || dstFormat==PIX_FMT_BGR24)
90 dstStride[i]= dstW*3;
5d5a8813
KS
91 else if (dstFormat==PIX_FMT_RGB48BE || dstFormat==PIX_FMT_RGB48LE)
92 dstStride[i]= dstW*6;
221b804f
DB
93 else
94 dstStride[i]= dstW*4;
95
96 src[i]= (uint8_t*) malloc(srcStride[i]*srcH);
97 dst[i]= (uint8_t*) malloc(dstStride[i]*dstH);
98 out[i]= (uint8_t*) malloc(refStride[i]*h);
1b0a4572 99 if (!src[i] || !dst[i] || !out[i]) {
221b804f
DB
100 perror("Malloc");
101 res = -1;
102
103 goto end;
104 }
105 }
106
110f147a 107 srcContext= sws_getContext(w, h, PIX_FMT_YUVA420P, srcW, srcH, srcFormat, flags, NULL, NULL, NULL);
1b0a4572 108 if (!srcContext) {
221b804f 109 fprintf(stderr, "Failed to get %s ---> %s\n",
110f147a 110 sws_format_name(PIX_FMT_YUVA420P),
221b804f
DB
111 sws_format_name(srcFormat));
112 res = -1;
113
114 goto end;
115 }
116 dstContext= sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL, NULL);
1b0a4572 117 if (!dstContext) {
221b804f
DB
118 fprintf(stderr, "Failed to get %s ---> %s\n",
119 sws_format_name(srcFormat),
120 sws_format_name(dstFormat));
121 res = -1;
122
123 goto end;
124 }
110f147a 125 outContext= sws_getContext(dstW, dstH, dstFormat, w, h, PIX_FMT_YUVA420P, flags, NULL, NULL, NULL);
1b0a4572 126 if (!outContext) {
221b804f
DB
127 fprintf(stderr, "Failed to get %s ---> %s\n",
128 sws_format_name(dstFormat),
110f147a 129 sws_format_name(PIX_FMT_YUVA420P));
221b804f
DB
130 res = -1;
131
132 goto end;
133 }
134// printf("test %X %X %X -> %X %X %X\n", (int)ref[0], (int)ref[1], (int)ref[2],
135// (int)src[0], (int)src[1], (int)src[2]);
136
137 sws_scale(srcContext, ref, refStride, 0, h , src, srcStride);
138 sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
139 sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
27161c06 140
221b804f 141 ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
f5a9c4ee
RP
142 if (!isGray(srcFormat) && !isGray(dstFormat)) {
143 //FIXME check that output is really gray
221b804f
DB
144 ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
145 ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);
f5a9c4ee 146 }
110f147a
CS
147 if (isALPHA(srcFormat) && isALPHA(dstFormat))
148 ssdA= getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
6a4970ab 149
221b804f
DB
150 ssdY/= w*h;
151 ssdU/= w*h/4;
152 ssdV/= w*h/4;
110f147a 153 ssdA/= w*h;
6a4970ab 154
4ed88643 155 printf(" %s %dx%d -> %s %4dx%4d flags=%2d SSD=%5"PRId64",%5"PRId64",%5"PRId64",%5"PRId64"\n",
ed2164f3
DB
156 sws_format_name(srcFormat), srcW, srcH,
157 sws_format_name(dstFormat), dstW, dstH,
110f147a 158 flags, ssdY, ssdU, ssdV, ssdA);
ed2164f3 159 fflush(stdout);
c9b99ea6 160
221b804f 161 end:
6a4970ab 162
221b804f
DB
163 sws_freeContext(srcContext);
164 sws_freeContext(dstContext);
165 sws_freeContext(outContext);
c9b99ea6 166
110f147a 167 for (i=0; i<4; i++){
221b804f
DB
168 free(src[i]);
169 free(dst[i]);
170 free(out[i]);
171 }
c9b99ea6 172
221b804f 173 return res;
c9b99ea6
MN
174}
175
110f147a 176static void selfTest(uint8_t *src[4], int stride[4], int w, int h){
221b804f
DB
177 enum PixelFormat srcFormat, dstFormat;
178 int srcW, srcH, dstW, dstH;
179 int flags;
180
181 for (srcFormat = 0; srcFormat < PIX_FMT_NB; srcFormat++) {
182 for (dstFormat = 0; dstFormat < PIX_FMT_NB; dstFormat++) {
183 printf("%s -> %s\n",
184 sws_format_name(srcFormat),
185 sws_format_name(dstFormat));
ae9e0e83 186 fflush(stdout);
221b804f
DB
187
188 srcW= w;
189 srcH= h;
190 for (dstW=w - w/3; dstW<= 4*w/3; dstW+= w/3){
191 for (dstH=h - h/3; dstH<= 4*h/3; dstH+= h/3){
192 for (flags=1; flags<33; flags*=2) {
193 int res;
194
195 res = doTest(src, stride, w, h, srcFormat, dstFormat,
196 srcW, srcH, dstW, dstH, flags);
197 if (res < 0) {
198 dstW = 4 * w / 3;
199 dstH = 4 * h / 3;
200 flags = 33;
201 }
202 }
203 }
204 }
205 }
206 }
c9b99ea6
MN
207}
208
209#define W 96
210#define H 96
211
212int main(int argc, char **argv){
221b804f
DB
213 uint8_t *rgb_data = malloc (W*H*4);
214 uint8_t *rgb_src[3]= {rgb_data, NULL, NULL};
215 int rgb_stride[3]={4*W, 0, 0};
110f147a
CS
216 uint8_t *data = malloc (4*W*H);
217 uint8_t *src[4]= {data, data+W*H, data+W*H*2, data+W*H*3};
218 int stride[4]={W, W, W, W};
221b804f
DB
219 int x, y;
220 struct SwsContext *sws;
9a034dde 221 AVLFG rand;
221b804f 222
110f147a 223 sws= sws_getContext(W/12, H/12, PIX_FMT_RGB32, W, H, PIX_FMT_YUVA420P, 2, NULL, NULL, NULL);
221b804f 224
9a034dde
RP
225 av_lfg_init(&rand, 1);
226
221b804f
DB
227 for (y=0; y<H; y++){
228 for (x=0; x<W*4; x++){
9a034dde 229 rgb_data[ x + y*4*W]= av_lfg_get(&rand);
221b804f
DB
230 }
231 }
e5091488 232 sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
27161c06 233
e5091488 234 selfTest(src, stride, W, H);
516b1f82 235
d7cf0a6f 236 return 0;
c9b99ea6 237}