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