* Introducing IIDC1394 grabbing interface.
[libav.git] / libavcodec / imgconvert.c
CommitLineData
de6d9b64
FB
1/*
2 * Misc image convertion routines
524c6b63 3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
de6d9b64 4 *
ff4ec49e
FB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
de6d9b64 9 *
ff4ec49e 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
ff4ec49e
FB
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
de6d9b64 18 */
983e3246
MN
19
20/**
1ab3d669 21 * @file imgconvert.c
983e3246
MN
22 * Misc image convertion routines.
23 */
24
c50c0bc8
FB
25/* TODO:
26 * - write 'ffimg' program to test all the image related stuff
27 * - move all api to slice based system
28 * - integrate deinterlacing, postprocessing and scaling in the conversion process
29 */
983e3246 30
de6d9b64 31#include "avcodec.h"
85c242d8 32#include "dsputil.h"
de6d9b64 33
54329dd5
NK
34#ifdef USE_FASTMEMCPY
35#include "fastmemcpy.h"
36#endif
5981f4e6
F
37
38#ifdef HAVE_MMX
39#include "i386/mmx.h"
40#endif
524c6b63 41
7e7e5940
FB
42#define xglue(x, y) x ## y
43#define glue(x, y) xglue(x, y)
44
b6147995
FB
45#define FF_COLOR_RGB 0 /* RGB color space */
46#define FF_COLOR_GRAY 1 /* gray color space */
47#define FF_COLOR_YUV 2 /* YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
48#define FF_COLOR_YUV_JPEG 3 /* YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
49
7e7e5940
FB
50#define FF_PIXEL_PLANAR 0 /* each channel has one component in AVPicture */
51#define FF_PIXEL_PACKED 1 /* only one components containing all the channels */
52#define FF_PIXEL_PALETTE 2 /* one components containing indexes for a palette */
53
524c6b63
FB
54typedef struct PixFmtInfo {
55 const char *name;
7e7e5940
FB
56 uint8_t nb_channels; /* number of channels (including alpha) */
57 uint8_t color_type; /* color type (see FF_COLOR_xxx constants) */
58 uint8_t pixel_type; /* pixel storage type (see FF_PIXEL_xxx constants) */
0c1a9eda 59 uint8_t is_alpha : 1; /* true if alpha can be specified */
7e7e5940
FB
60 uint8_t x_chroma_shift; /* X chroma subsampling factor is 2 ^ shift */
61 uint8_t y_chroma_shift; /* Y chroma subsampling factor is 2 ^ shift */
62 uint8_t depth; /* bit depth of the color components */
524c6b63
FB
63} PixFmtInfo;
64
65/* this table gives more information about formats */
66static PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
67 /* YUV formats */
68 [PIX_FMT_YUV420P] = {
ef9f7306 69 .name = "yuv420p",
7e7e5940 70 .nb_channels = 3,
b6147995 71 .color_type = FF_COLOR_YUV,
7e7e5940 72 .pixel_type = FF_PIXEL_PLANAR,
b6147995 73 .depth = 8,
ef9f7306 74 .x_chroma_shift = 1, .y_chroma_shift = 1,
524c6b63
FB
75 },
76 [PIX_FMT_YUV422P] = {
ef9f7306 77 .name = "yuv422p",
7e7e5940 78 .nb_channels = 3,
b6147995 79 .color_type = FF_COLOR_YUV,
7e7e5940 80 .pixel_type = FF_PIXEL_PLANAR,
b6147995 81 .depth = 8,
ef9f7306 82 .x_chroma_shift = 1, .y_chroma_shift = 0,
524c6b63
FB
83 },
84 [PIX_FMT_YUV444P] = {
ef9f7306 85 .name = "yuv444p",
7e7e5940 86 .nb_channels = 3,
b6147995 87 .color_type = FF_COLOR_YUV,
7e7e5940 88 .pixel_type = FF_PIXEL_PLANAR,
b6147995 89 .depth = 8,
ef9f7306 90 .x_chroma_shift = 0, .y_chroma_shift = 0,
524c6b63
FB
91 },
92 [PIX_FMT_YUV422] = {
ef9f7306 93 .name = "yuv422",
7e7e5940 94 .nb_channels = 1,
b6147995 95 .color_type = FF_COLOR_YUV,
7e7e5940 96 .pixel_type = FF_PIXEL_PACKED,
b6147995 97 .depth = 8,
ef9f7306 98 .x_chroma_shift = 1, .y_chroma_shift = 0,
524c6b63 99 },
ebb177dd
TK
100 [PIX_FMT_UYVY422] = {
101 .name = "uyvy422",
102 .nb_channels = 1,
103 .color_type = FF_COLOR_YUV,
104 .pixel_type = FF_PIXEL_PACKED,
105 .depth = 8,
106 .x_chroma_shift = 1, .y_chroma_shift = 0,
107 },
524c6b63 108 [PIX_FMT_YUV410P] = {
ef9f7306 109 .name = "yuv410p",
7e7e5940 110 .nb_channels = 3,
b6147995 111 .color_type = FF_COLOR_YUV,
7e7e5940 112 .pixel_type = FF_PIXEL_PLANAR,
b6147995 113 .depth = 8,
ef9f7306 114 .x_chroma_shift = 2, .y_chroma_shift = 2,
524c6b63
FB
115 },
116 [PIX_FMT_YUV411P] = {
ef9f7306 117 .name = "yuv411p",
7e7e5940 118 .nb_channels = 3,
b6147995 119 .color_type = FF_COLOR_YUV,
7e7e5940 120 .pixel_type = FF_PIXEL_PLANAR,
b6147995 121 .depth = 8,
ef9f7306 122 .x_chroma_shift = 2, .y_chroma_shift = 0,
524c6b63
FB
123 },
124
b6147995
FB
125 /* JPEG YUV */
126 [PIX_FMT_YUVJ420P] = {
127 .name = "yuvj420p",
7e7e5940 128 .nb_channels = 3,
b6147995 129 .color_type = FF_COLOR_YUV_JPEG,
7e7e5940 130 .pixel_type = FF_PIXEL_PLANAR,
b6147995
FB
131 .depth = 8,
132 .x_chroma_shift = 1, .y_chroma_shift = 1,
133 },
134 [PIX_FMT_YUVJ422P] = {
135 .name = "yuvj422p",
7e7e5940 136 .nb_channels = 3,
b6147995 137 .color_type = FF_COLOR_YUV_JPEG,
7e7e5940 138 .pixel_type = FF_PIXEL_PLANAR,
b6147995
FB
139 .depth = 8,
140 .x_chroma_shift = 1, .y_chroma_shift = 0,
141 },
142 [PIX_FMT_YUVJ444P] = {
143 .name = "yuvj444p",
7e7e5940 144 .nb_channels = 3,
b6147995 145 .color_type = FF_COLOR_YUV_JPEG,
7e7e5940 146 .pixel_type = FF_PIXEL_PLANAR,
b6147995
FB
147 .depth = 8,
148 .x_chroma_shift = 0, .y_chroma_shift = 0,
149 },
150
524c6b63
FB
151 /* RGB formats */
152 [PIX_FMT_RGB24] = {
ef9f7306 153 .name = "rgb24",
7e7e5940 154 .nb_channels = 3,
b6147995 155 .color_type = FF_COLOR_RGB,
7e7e5940 156 .pixel_type = FF_PIXEL_PACKED,
b6147995 157 .depth = 8,
2cbb7820 158 .x_chroma_shift = 0, .y_chroma_shift = 0,
524c6b63
FB
159 },
160 [PIX_FMT_BGR24] = {
ef9f7306 161 .name = "bgr24",
7e7e5940 162 .nb_channels = 3,
b6147995 163 .color_type = FF_COLOR_RGB,
7e7e5940 164 .pixel_type = FF_PIXEL_PACKED,
b6147995 165 .depth = 8,
2cbb7820 166 .x_chroma_shift = 0, .y_chroma_shift = 0,
524c6b63
FB
167 },
168 [PIX_FMT_RGBA32] = {
ef9f7306 169 .name = "rgba32",
7e7e5940 170 .nb_channels = 4, .is_alpha = 1,
b6147995 171 .color_type = FF_COLOR_RGB,
7e7e5940 172 .pixel_type = FF_PIXEL_PACKED,
b6147995 173 .depth = 8,
2cbb7820 174 .x_chroma_shift = 0, .y_chroma_shift = 0,
524c6b63
FB
175 },
176 [PIX_FMT_RGB565] = {
ef9f7306 177 .name = "rgb565",
7e7e5940 178 .nb_channels = 3,
b6147995 179 .color_type = FF_COLOR_RGB,
7e7e5940 180 .pixel_type = FF_PIXEL_PACKED,
b6147995 181 .depth = 5,
2cbb7820 182 .x_chroma_shift = 0, .y_chroma_shift = 0,
524c6b63
FB
183 },
184 [PIX_FMT_RGB555] = {
ef9f7306 185 .name = "rgb555",
7e7e5940 186 .nb_channels = 4, .is_alpha = 1,
b6147995 187 .color_type = FF_COLOR_RGB,
7e7e5940 188 .pixel_type = FF_PIXEL_PACKED,
b6147995 189 .depth = 5,
2cbb7820 190 .x_chroma_shift = 0, .y_chroma_shift = 0,
524c6b63
FB
191 },
192
193 /* gray / mono formats */
194 [PIX_FMT_GRAY8] = {
ef9f7306 195 .name = "gray",
7e7e5940 196 .nb_channels = 1,
b6147995 197 .color_type = FF_COLOR_GRAY,
7e7e5940 198 .pixel_type = FF_PIXEL_PLANAR,
b6147995 199 .depth = 8,
524c6b63
FB
200 },
201 [PIX_FMT_MONOWHITE] = {
ef9f7306 202 .name = "monow",
7e7e5940 203 .nb_channels = 1,
b6147995 204 .color_type = FF_COLOR_GRAY,
7e7e5940 205 .pixel_type = FF_PIXEL_PLANAR,
b6147995 206 .depth = 1,
524c6b63
FB
207 },
208 [PIX_FMT_MONOBLACK] = {
ef9f7306 209 .name = "monob",
7e7e5940 210 .nb_channels = 1,
b6147995 211 .color_type = FF_COLOR_GRAY,
7e7e5940 212 .pixel_type = FF_PIXEL_PLANAR,
b6147995 213 .depth = 1,
524c6b63 214 },
7e6d70d0
FB
215
216 /* paletted formats */
217 [PIX_FMT_PAL8] = {
218 .name = "pal8",
7e7e5940 219 .nb_channels = 4, .is_alpha = 1,
b6147995 220 .color_type = FF_COLOR_RGB,
7e7e5940 221 .pixel_type = FF_PIXEL_PALETTE,
b6147995 222 .depth = 8,
7e6d70d0 223 },
eab895aa
TK
224 [PIX_FMT_XVMC_MPEG2_MC] = {
225 .name = "xvmcmc",
226 },
227 [PIX_FMT_XVMC_MPEG2_IDCT] = {
228 .name = "xvmcidct",
229 },
f02be79d
RS
230 [PIX_FMT_UYVY411] = {
231 .name = "uyvy411",
232 .nb_channels = 1,
233 .color_type = FF_COLOR_YUV,
234 .pixel_type = FF_PIXEL_PACKED,
235 .depth = 8,
236 .x_chroma_shift = 2, .y_chroma_shift = 0,
237 },
524c6b63
FB
238};
239
240void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
241{
b6147995
FB
242 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
243 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
524c6b63
FB
244}
245
246const char *avcodec_get_pix_fmt_name(int pix_fmt)
247{
248 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
249 return "???";
250 else
251 return pix_fmt_info[pix_fmt].name;
252}
253
63167088
RS
254enum PixelFormat avcodec_get_pix_fmt(const char* name)
255{
256 int i;
257
258 for (i=0; i < PIX_FMT_NB; i++)
259 if (!strcmp(pix_fmt_info[i].name, name))
260 break;
261 return i;
262}
263
2a877875 264/* Picture field are filled with 'ptr' addresses. Also return size */
0c1a9eda 265int avpicture_fill(AVPicture *picture, uint8_t *ptr,
da64ecc3 266 int pix_fmt, int width, int height)
2a877875 267{
4c7e8619
FB
268 int size, w2, h2, size2;
269 PixFmtInfo *pinfo;
270
271 pinfo = &pix_fmt_info[pix_fmt];
2a877875
FB
272 size = width * height;
273 switch(pix_fmt) {
274 case PIX_FMT_YUV420P:
4c7e8619
FB
275 case PIX_FMT_YUV422P:
276 case PIX_FMT_YUV444P:
277 case PIX_FMT_YUV410P:
278 case PIX_FMT_YUV411P:
c50c0bc8
FB
279 case PIX_FMT_YUVJ420P:
280 case PIX_FMT_YUVJ422P:
281 case PIX_FMT_YUVJ444P:
4c7e8619
FB
282 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
283 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
284 size2 = w2 * h2;
2a877875
FB
285 picture->data[0] = ptr;
286 picture->data[1] = picture->data[0] + size;
4c7e8619 287 picture->data[2] = picture->data[1] + size2;
2a877875 288 picture->linesize[0] = width;
4c7e8619
FB
289 picture->linesize[1] = w2;
290 picture->linesize[2] = w2;
291 return size + 2 * size2;
2a877875
FB
292 case PIX_FMT_RGB24:
293 case PIX_FMT_BGR24:
294 picture->data[0] = ptr;
295 picture->data[1] = NULL;
296 picture->data[2] = NULL;
297 picture->linesize[0] = width * 3;
298 return size * 3;
2a877875
FB
299 case PIX_FMT_RGBA32:
300 picture->data[0] = ptr;
301 picture->data[1] = NULL;
302 picture->data[2] = NULL;
303 picture->linesize[0] = width * 4;
304 return size * 4;
2a877875
FB
305 case PIX_FMT_RGB555:
306 case PIX_FMT_RGB565:
307 case PIX_FMT_YUV422:
308 picture->data[0] = ptr;
309 picture->data[1] = NULL;
310 picture->data[2] = NULL;
311 picture->linesize[0] = width * 2;
312 return size * 2;
ebb177dd
TK
313 case PIX_FMT_UYVY422:
314 picture->data[0] = ptr;
315 picture->data[1] = NULL;
316 picture->data[2] = NULL;
317 picture->linesize[0] = width * 2;
318 return size * 2;
f02be79d
RS
319 case PIX_FMT_UYVY411:
320 picture->data[0] = ptr;
321 picture->data[1] = NULL;
322 picture->data[2] = NULL;
323 picture->linesize[0] = width + width/2;
324 return size + size/2;
2a877875
FB
325 case PIX_FMT_GRAY8:
326 picture->data[0] = ptr;
327 picture->data[1] = NULL;
328 picture->data[2] = NULL;
329 picture->linesize[0] = width;
330 return size;
331 case PIX_FMT_MONOWHITE:
332 case PIX_FMT_MONOBLACK:
333 picture->data[0] = ptr;
334 picture->data[1] = NULL;
335 picture->data[2] = NULL;
336 picture->linesize[0] = (width + 7) >> 3;
337 return picture->linesize[0] * height;
7e6d70d0
FB
338 case PIX_FMT_PAL8:
339 size2 = (size + 3) & ~3;
340 picture->data[0] = ptr;
341 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
342 picture->data[2] = NULL;
343 picture->linesize[0] = width;
344 picture->linesize[1] = 4;
345 return size2 + 256 * 4;
2a877875
FB
346 default:
347 picture->data[0] = NULL;
348 picture->data[1] = NULL;
349 picture->data[2] = NULL;
7e6d70d0 350 picture->data[3] = NULL;
2a877875
FB
351 return -1;
352 }
353}
354
da64ecc3 355int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
63167088
RS
356 unsigned char *dest, int dest_size)
357{
358 PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
359 int i, j, w, h, data_planes;
da64ecc3 360 const unsigned char* s;
63167088
RS
361 int size = avpicture_get_size(pix_fmt, width, height);
362
363 if (size > dest_size)
364 return -1;
365
affd55a1 366 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
ebb177dd
TK
367 if (pix_fmt == PIX_FMT_YUV422 ||
368 pix_fmt == PIX_FMT_UYVY422 ||
369 pix_fmt == PIX_FMT_RGB565 ||
370 pix_fmt == PIX_FMT_RGB555)
371 w = width * 2;
f02be79d
RS
372 else if (pix_fmt == PIX_FMT_UYVY411)
373 w = width + width/2;
affd55a1
RS
374 else if (pix_fmt == PIX_FMT_PAL8)
375 w = width;
63167088
RS
376 else
377 w = width * (pf->depth * pf->nb_channels / 8);
affd55a1 378
63167088
RS
379 data_planes = 1;
380 h = height;
381 } else {
382 data_planes = pf->nb_channels;
383 w = width;
384 h = height;
385 }
386
387 for (i=0; i<data_planes; i++) {
388 if (i == 1) {
389 w = width >> pf->x_chroma_shift;
390 h = height >> pf->y_chroma_shift;
391 }
392 s = src->data[i];
393 for(j=0; j<h; j++) {
394 memcpy(dest, s, w);
395 dest += w;
396 s += src->linesize[i];
397 }
398 }
affd55a1
RS
399
400 if (pf->pixel_type == FF_PIXEL_PALETTE)
401 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
402
63167088
RS
403 return size;
404}
405
2a877875
FB
406int avpicture_get_size(int pix_fmt, int width, int height)
407{
408 AVPicture dummy_pict;
409 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
410}
411
b6147995
FB
412/**
413 * compute the loss when converting from a pixel format to another
414 */
415int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
416 int has_alpha)
417{
418 const PixFmtInfo *pf, *ps;
419 int loss;
420
421 ps = &pix_fmt_info[src_pix_fmt];
422 pf = &pix_fmt_info[dst_pix_fmt];
423
424 /* compute loss */
425 loss = 0;
426 pf = &pix_fmt_info[dst_pix_fmt];
0a9ad8d1
FB
427 if (pf->depth < ps->depth ||
428 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
b6147995 429 loss |= FF_LOSS_DEPTH;
0a9ad8d1
FB
430 if (pf->x_chroma_shift > ps->x_chroma_shift ||
431 pf->y_chroma_shift > ps->y_chroma_shift)
b6147995
FB
432 loss |= FF_LOSS_RESOLUTION;
433 switch(pf->color_type) {
434 case FF_COLOR_RGB:
435 if (ps->color_type != FF_COLOR_RGB &&
436 ps->color_type != FF_COLOR_GRAY)
437 loss |= FF_LOSS_COLORSPACE;
438 break;
439 case FF_COLOR_GRAY:
440 if (ps->color_type != FF_COLOR_GRAY)
441 loss |= FF_LOSS_COLORSPACE;
442 break;
443 case FF_COLOR_YUV:
444 if (ps->color_type != FF_COLOR_YUV)
445 loss |= FF_LOSS_COLORSPACE;
446 break;
447 case FF_COLOR_YUV_JPEG:
448 if (ps->color_type != FF_COLOR_YUV_JPEG &&
0a9ad8d1
FB
449 ps->color_type != FF_COLOR_YUV &&
450 ps->color_type != FF_COLOR_GRAY)
b6147995
FB
451 loss |= FF_LOSS_COLORSPACE;
452 break;
453 default:
454 /* fail safe test */
455 if (ps->color_type != pf->color_type)
456 loss |= FF_LOSS_COLORSPACE;
457 break;
458 }
459 if (pf->color_type == FF_COLOR_GRAY &&
460 ps->color_type != FF_COLOR_GRAY)
461 loss |= FF_LOSS_CHROMA;
462 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
463 loss |= FF_LOSS_ALPHA;
7e7e5940
FB
464 if (pf->pixel_type == FF_PIXEL_PALETTE &&
465 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
b6147995
FB
466 loss |= FF_LOSS_COLORQUANT;
467 return loss;
468}
469
470static int avg_bits_per_pixel(int pix_fmt)
471{
472 int bits;
473 const PixFmtInfo *pf;
474
475 pf = &pix_fmt_info[pix_fmt];
7e7e5940
FB
476 switch(pf->pixel_type) {
477 case FF_PIXEL_PACKED:
b6147995 478 switch(pix_fmt) {
7e7e5940 479 case PIX_FMT_YUV422:
ebb177dd 480 case PIX_FMT_UYVY422:
b6147995
FB
481 case PIX_FMT_RGB565:
482 case PIX_FMT_RGB555:
483 bits = 16;
b6147995 484 break;
f02be79d
RS
485 case PIX_FMT_UYVY411:
486 bits = 12;
487 break;
b6147995 488 default:
7e7e5940 489 bits = pf->depth * pf->nb_channels;
b6147995
FB
490 break;
491 }
7e7e5940
FB
492 break;
493 case FF_PIXEL_PLANAR:
494 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
495 bits = pf->depth * pf->nb_channels;
496 } else {
497 bits = pf->depth + ((2 * pf->depth) >>
498 (pf->x_chroma_shift + pf->y_chroma_shift));
499 }
500 break;
501 case FF_PIXEL_PALETTE:
502 bits = 8;
503 break;
504 default:
505 bits = -1;
506 break;
b6147995
FB
507 }
508 return bits;
509}
510
511static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
512 int src_pix_fmt,
513 int has_alpha,
514 int loss_mask)
515{
516 int dist, i, loss, min_dist, dst_pix_fmt;
517
518 /* find exact color match with smallest size */
519 dst_pix_fmt = -1;
520 min_dist = 0x7fffffff;
521 for(i = 0;i < PIX_FMT_NB; i++) {
522 if (pix_fmt_mask & (1 << i)) {
523 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
524 if (loss == 0) {
525 dist = avg_bits_per_pixel(i);
526 if (dist < min_dist) {
527 min_dist = dist;
528 dst_pix_fmt = i;
529 }
530 }
531 }
532 }
533 return dst_pix_fmt;
534}
535
536/**
537 * find best pixel format to convert to. Return -1 if none found
538 */
539int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
540 int has_alpha, int *loss_ptr)
541{
542 int dst_pix_fmt, loss_mask, i;
543 static const int loss_mask_order[] = {
544 ~0, /* no loss first */
545 ~FF_LOSS_ALPHA,
546 ~FF_LOSS_RESOLUTION,
547 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
548 ~FF_LOSS_COLORQUANT,
549 ~FF_LOSS_DEPTH,
550 0,
551 };
552
553 /* try with successive loss */
554 i = 0;
555 for(;;) {
556 loss_mask = loss_mask_order[i++];
557 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
558 has_alpha, loss_mask);
559 if (dst_pix_fmt >= 0)
560 goto found;
561 if (loss_mask == 0)
562 break;
563 }
564 return -1;
565 found:
566 if (loss_ptr)
567 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
568 return dst_pix_fmt;
569}
570
7e7e5940 571static void img_copy_plane(uint8_t *dst, int dst_wrap,
e352ff08
FB
572 const uint8_t *src, int src_wrap,
573 int width, int height)
7e7e5940
FB
574{
575 for(;height > 0; height--) {
576 memcpy(dst, src, width);
577 dst += dst_wrap;
578 src += src_wrap;
579 }
580}
581
0469baf1
FB
582/**
583 * Copy image 'src' to 'dst'.
584 */
da64ecc3 585void img_copy(AVPicture *dst, const AVPicture *src,
7e7e5940
FB
586 int pix_fmt, int width, int height)
587{
588 int bwidth, bits, i;
589 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
590
591 pf = &pix_fmt_info[pix_fmt];
592 switch(pf->pixel_type) {
593 case FF_PIXEL_PACKED:
594 switch(pix_fmt) {
595 case PIX_FMT_YUV422:
ebb177dd 596 case PIX_FMT_UYVY422:
7e7e5940
FB
597 case PIX_FMT_RGB565:
598 case PIX_FMT_RGB555:
599 bits = 16;
600 break;
f02be79d
RS
601 case PIX_FMT_UYVY411:
602 bits = 12;
603 break;
7e7e5940
FB
604 default:
605 bits = pf->depth * pf->nb_channels;
606 break;
607 }
608 bwidth = (width * bits + 7) >> 3;
609 img_copy_plane(dst->data[0], dst->linesize[0],
610 src->data[0], src->linesize[0],
611 bwidth, height);
612 break;
613 case FF_PIXEL_PLANAR:
614 for(i = 0; i < pf->nb_channels; i++) {
615 int w, h;
616 w = width;
617 h = height;
618 if (i == 1 || i == 2) {
619 w >>= pf->x_chroma_shift;
620 h >>= pf->y_chroma_shift;
621 }
622 bwidth = (w * pf->depth + 7) >> 3;
623 img_copy_plane(dst->data[i], dst->linesize[i],
624 src->data[i], src->linesize[i],
625 bwidth, h);
626 }
627 break;
628 case FF_PIXEL_PALETTE:
629 img_copy_plane(dst->data[0], dst->linesize[0],
630 src->data[0], src->linesize[0],
631 width, height);
632 /* copy the palette */
633 img_copy_plane(dst->data[1], dst->linesize[1],
634 src->data[1], src->linesize[1],
635 4, 256);
636 break;
637 }
638}
2a877875 639
de6d9b64
FB
640/* XXX: totally non optimized */
641
da64ecc3 642static void yuv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
524c6b63 643 int width, int height)
de6d9b64 644{
c50c0bc8
FB
645 const uint8_t *p, *p1;
646 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
d4f5d74a 647 int w;
524c6b63 648
c50c0bc8
FB
649 p1 = src->data[0];
650 lum1 = dst->data[0];
0a05e494
FB
651 cb1 = dst->data[1];
652 cr1 = dst->data[2];
c50c0bc8 653
d4f5d74a 654 for(;height >= 1; height -= 2) {
c50c0bc8
FB
655 p = p1;
656 lum = lum1;
657 cb = cb1;
658 cr = cr1;
d4f5d74a 659 for(w = width; w >= 2; w -= 2) {
e78df699
MN
660 lum[0] = p[0];
661 cb[0] = p[1];
662 lum[1] = p[2];
663 cr[0] = p[3];
de6d9b64
FB
664 p += 4;
665 lum += 2;
666 cb++;
667 cr++;
668 }
d4f5d74a 669 if (w) {
e78df699 670 lum[0] = p[0];
d4f5d74a
GM
671 cb[0] = p[1];
672 cr[0] = p[3];
673 cb++;
674 cr++;
de6d9b64 675 }
c50c0bc8
FB
676 p1 += src->linesize[0];
677 lum1 += dst->linesize[0];
d4f5d74a
GM
678 if (height>1) {
679 p = p1;
680 lum = lum1;
681 for(w = width; w >= 2; w -= 2) {
682 lum[0] = p[0];
683 lum[1] = p[2];
684 p += 4;
685 lum += 2;
686 }
687 if (w) {
688 lum[0] = p[0];
689 }
690 p1 += src->linesize[0];
691 lum1 += dst->linesize[0];
692 }
c50c0bc8
FB
693 cb1 += dst->linesize[1];
694 cr1 += dst->linesize[2];
695 }
696}
697
ebb177dd
TK
698static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
699 int width, int height)
700{
701 const uint8_t *p, *p1;
702 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
703 int w;
704
705 p1 = src->data[0];
706
707 lum1 = dst->data[0];
708 cb1 = dst->data[1];
709 cr1 = dst->data[2];
710
711 for(;height >= 1; height -= 2) {
712 p = p1;
713 lum = lum1;
714 cb = cb1;
715 cr = cr1;
716 for(w = width; w >= 2; w -= 2) {
717 lum[0] = p[1];
718 cb[0] = p[0];
719 lum[1] = p[3];
720 cr[0] = p[2];
721 p += 4;
722 lum += 2;
723 cb++;
724 cr++;
725 }
726 if (w) {
727 lum[0] = p[1];
728 cb[0] = p[0];
729 cr[0] = p[2];
730 cb++;
731 cr++;
732 }
733 p1 += src->linesize[0];
734 lum1 += dst->linesize[0];
735 if (height>1) {
736 p = p1;
737 lum = lum1;
738 for(w = width; w >= 2; w -= 2) {
739 lum[0] = p[1];
740 lum[1] = p[3];
741 p += 4;
742 lum += 2;
743 }
744 if (w) {
745 lum[0] = p[1];
746 }
747 p1 += src->linesize[0];
748 lum1 += dst->linesize[0];
749 }
750 cb1 += dst->linesize[1];
751 cr1 += dst->linesize[2];
752 }
753}
754
755
756static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
757 int width, int height)
758{
759 const uint8_t *p, *p1;
760 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
761 int w;
762
763 p1 = src->data[0];
764 lum1 = dst->data[0];
765 cb1 = dst->data[1];
766 cr1 = dst->data[2];
767 for(;height > 0; height--) {
768 p = p1;
769 lum = lum1;
770 cb = cb1;
771 cr = cr1;
772 for(w = width; w >= 2; w -= 2) {
773 lum[0] = p[1];
774 cb[0] = p[0];
775 lum[1] = p[3];
776 cr[0] = p[2];
777 p += 4;
778 lum += 2;
779 cb++;
780 cr++;
781 }
782 p1 += src->linesize[0];
783 lum1 += dst->linesize[0];
784 cb1 += dst->linesize[1];
785 cr1 += dst->linesize[2];
786 }
787}
788
789
da64ecc3 790static void yuv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
c50c0bc8
FB
791 int width, int height)
792{
793 const uint8_t *p, *p1;
794 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
795 int w;
796
797 p1 = src->data[0];
798 lum1 = dst->data[0];
0a05e494
FB
799 cb1 = dst->data[1];
800 cr1 = dst->data[2];
801 for(;height > 0; height--) {
c50c0bc8
FB
802 p = p1;
803 lum = lum1;
804 cb = cb1;
805 cr = cr1;
806 for(w = width; w >= 2; w -= 2) {
807 lum[0] = p[0];
808 cb[0] = p[1];
809 lum[1] = p[2];
810 cr[0] = p[3];
811 p += 4;
812 lum += 2;
813 cb++;
814 cr++;
815 }
816 p1 += src->linesize[0];
817 lum1 += dst->linesize[0];
818 cb1 += dst->linesize[1];
819 cr1 += dst->linesize[2];
de6d9b64
FB
820 }
821}
822
da64ecc3 823static void yuv422p_to_yuv422(AVPicture *dst, const AVPicture *src,
c50c0bc8
FB
824 int width, int height)
825{
826 uint8_t *p, *p1;
827 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
828 int w;
829
830 p1 = dst->data[0];
831 lum1 = src->data[0];
0a05e494
FB
832 cb1 = src->data[1];
833 cr1 = src->data[2];
834 for(;height > 0; height--) {
c50c0bc8
FB
835 p = p1;
836 lum = lum1;
837 cb = cb1;
838 cr = cr1;
839 for(w = width; w >= 2; w -= 2) {
840 p[0] = lum[0];
841 p[1] = cb[0];
842 p[2] = lum[1];
843 p[3] = cr[0];
844 p += 4;
845 lum += 2;
846 cb++;
847 cr++;
848 }
0a05e494
FB
849 p1 += dst->linesize[0];
850 lum1 += src->linesize[0];
851 cb1 += src->linesize[1];
852 cr1 += src->linesize[2];
c50c0bc8
FB
853 }
854}
855
ebb177dd
TK
856static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
857 int width, int height)
858{
859 uint8_t *p, *p1;
860 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
861 int w;
862
863 p1 = dst->data[0];
864 lum1 = src->data[0];
865 cb1 = src->data[1];
866 cr1 = src->data[2];
867 for(;height > 0; height--) {
868 p = p1;
869 lum = lum1;
870 cb = cb1;
871 cr = cr1;
872 for(w = width; w >= 2; w -= 2) {
873 p[1] = lum[0];
874 p[0] = cb[0];
875 p[3] = lum[1];
876 p[2] = cr[0];
877 p += 4;
878 lum += 2;
879 cb++;
880 cr++;
881 }
882 p1 += dst->linesize[0];
883 lum1 += src->linesize[0];
884 cb1 += src->linesize[1];
885 cr1 += src->linesize[2];
886 }
887}
888
f02be79d
RS
889static void uyvy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
890 int width, int height)
891{
892 const uint8_t *p, *p1;
893 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
894 int w;
895
896 p1 = src->data[0];
897 lum1 = dst->data[0];
898 cb1 = dst->data[1];
899 cr1 = dst->data[2];
900 for(;height > 0; height--) {
901 p = p1;
902 lum = lum1;
903 cb = cb1;
904 cr = cr1;
905 for(w = width; w >= 4; w -= 4) {
906 cb[0] = p[0];
907 lum[0] = p[1];
908 lum[1] = p[2];
909 cr[0] = p[3];
910 lum[2] = p[4];
911 lum[3] = p[5];
912 p += 6;
913 lum += 4;
914 cb++;
915 cr++;
916 }
917 p1 += src->linesize[0];
918 lum1 += dst->linesize[0];
919 cb1 += dst->linesize[1];
920 cr1 += dst->linesize[2];
921 }
922}
ebb177dd
TK
923
924
c50c0bc8 925#define SCALEBITS 10
de6d9b64 926#define ONE_HALF (1 << (SCALEBITS - 1))
c50c0bc8 927#define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
de6d9b64 928
c50c0bc8
FB
929#define YUV_TO_RGB1_CCIR(cb1, cr1)\
930{\
931 cb = (cb1) - 128;\
932 cr = (cr1) - 128;\
933 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
934 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
935 ONE_HALF;\
936 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
937}
b6147995 938
c50c0bc8
FB
939#define YUV_TO_RGB2_CCIR(r, g, b, y1)\
940{\
941 y = ((y1) - 16) * FIX(255.0/219.0);\
942 r = cm[(y + r_add) >> SCALEBITS];\
943 g = cm[(y + g_add) >> SCALEBITS];\
944 b = cm[(y + b_add) >> SCALEBITS];\
945}
946
947#define YUV_TO_RGB1(cb1, cr1)\
948{\
949 cb = (cb1) - 128;\
950 cr = (cr1) - 128;\
951 r_add = FIX(1.40200) * cr + ONE_HALF;\
952 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
953 b_add = FIX(1.77200) * cb + ONE_HALF;\
954}
b6147995
FB
955
956#define YUV_TO_RGB2(r, g, b, y1)\
957{\
c50c0bc8
FB
958 y = (y1) << SCALEBITS;\
959 r = cm[(y + r_add) >> SCALEBITS];\
960 g = cm[(y + g_add) >> SCALEBITS];\
961 b = cm[(y + b_add) >> SCALEBITS];\
b6147995
FB
962}
963
c50c0bc8 964#define Y_CCIR_TO_JPEG(y)\
b6147995
FB
965 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
966
c50c0bc8 967#define Y_JPEG_TO_CCIR(y)\
b6147995
FB
968 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
969
c50c0bc8
FB
970#define C_CCIR_TO_JPEG(y)\
971 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
972
973/* NOTE: the clamp is really necessary! */
4cfbf61b
FH
974static inline int C_JPEG_TO_CCIR(int y) {
975 y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
976 if (y < 16)
977 y = 16;
978 return y;
979}
980
c50c0bc8 981
b6147995
FB
982#define RGB_TO_Y(r, g, b) \
983((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
984 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
985
c50c0bc8 986#define RGB_TO_U(r1, g1, b1, shift)\
b6147995 987(((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
c50c0bc8 988 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
b6147995 989
c50c0bc8 990#define RGB_TO_V(r1, g1, b1, shift)\
b6147995 991(((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
c50c0bc8 992 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
b6147995
FB
993
994#define RGB_TO_Y_CCIR(r, g, b) \
995((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
996 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
997
c50c0bc8 998#define RGB_TO_U_CCIR(r1, g1, b1, shift)\
b6147995 999(((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
c50c0bc8 1000 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
b6147995 1001
c50c0bc8 1002#define RGB_TO_V_CCIR(r1, g1, b1, shift)\
b6147995 1003(((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
c50c0bc8 1004 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
b6147995 1005
c50c0bc8
FB
1006static uint8_t y_ccir_to_jpeg[256];
1007static uint8_t y_jpeg_to_ccir[256];
1008static uint8_t c_ccir_to_jpeg[256];
1009static uint8_t c_jpeg_to_ccir[256];
1010
1011/* init various conversion tables */
1012static void img_convert_init(void)
b6147995 1013{
c50c0bc8 1014 int i;
b6147995
FB
1015 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1016
c50c0bc8
FB
1017 for(i = 0;i < 256; i++) {
1018 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1019 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1020 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1021 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
b6147995
FB
1022 }
1023}
1024
c50c0bc8
FB
1025/* apply to each pixel the given table */
1026static void img_apply_table(uint8_t *dst, int dst_wrap,
1027 const uint8_t *src, int src_wrap,
1028 int width, int height, const uint8_t *table1)
b6147995
FB
1029{
1030 int n;
1031 const uint8_t *s;
1032 uint8_t *d;
c50c0bc8 1033 const uint8_t *table;
b6147995 1034
c50c0bc8 1035 table = table1;
b6147995
FB
1036 for(;height > 0; height--) {
1037 s = src;
1038 d = dst;
1039 n = width;
1040 while (n >= 4) {
c50c0bc8
FB
1041 d[0] = table[s[0]];
1042 d[1] = table[s[1]];
1043 d[2] = table[s[2]];
1044 d[3] = table[s[3]];
b6147995
FB
1045 d += 4;
1046 s += 4;
1047 n -= 4;
1048 }
1049 while (n > 0) {
c50c0bc8 1050 d[0] = table[s[0]];
b6147995
FB
1051 d++;
1052 s++;
1053 n--;
1054 }
1055 dst += dst_wrap;
1056 src += src_wrap;
1057 }
1058}
1059
85c242d8 1060/* XXX: use generic filter ? */
e352ff08
FB
1061/* XXX: in most cases, the sampling position is incorrect */
1062
1063/* 4x1 -> 1x1 */
1064static void shrink41(uint8_t *dst, int dst_wrap,
1065 const uint8_t *src, int src_wrap,
1066 int width, int height)
1067{
1068 int w;
1069 const uint8_t *s;
1070 uint8_t *d;
1071
1072 for(;height > 0; height--) {
1073 s = src;
1074 d = dst;
1075 for(w = width;w > 0; w--) {
1076 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1077 s += 4;
1078 d++;
1079 }
1080 src += src_wrap;
1081 dst += dst_wrap;
1082 }
1083}
1084
1085/* 2x1 -> 1x1 */
1086static void shrink21(uint8_t *dst, int dst_wrap,
1087 const uint8_t *src, int src_wrap,
1088 int width, int height)
1089{
1090 int w;
1091 const uint8_t *s;
1092 uint8_t *d;
1093
1094 for(;height > 0; height--) {
1095 s = src;
1096 d = dst;
1097 for(w = width;w > 0; w--) {
1098 d[0] = (s[0] + s[1]) >> 1;
1099 s += 2;
1100 d++;
1101 }
1102 src += src_wrap;
1103 dst += dst_wrap;
1104 }
1105}
1106
85c242d8 1107/* 1x2 -> 1x1 */
e352ff08
FB
1108static void shrink12(uint8_t *dst, int dst_wrap,
1109 const uint8_t *src, int src_wrap,
1110 int width, int height)
85c242d8
FB
1111{
1112 int w;
e352ff08
FB
1113 uint8_t *d;
1114 const uint8_t *s1, *s2;
85c242d8
FB
1115
1116 for(;height > 0; height--) {
1117 s1 = src;
1118 s2 = s1 + src_wrap;
1119 d = dst;
1120 for(w = width;w >= 4; w-=4) {
1121 d[0] = (s1[0] + s2[0]) >> 1;
1122 d[1] = (s1[1] + s2[1]) >> 1;
1123 d[2] = (s1[2] + s2[2]) >> 1;
1124 d[3] = (s1[3] + s2[3]) >> 1;
1125 s1 += 4;
1126 s2 += 4;
1127 d += 4;
1128 }
1129 for(;w > 0; w--) {
1130 d[0] = (s1[0] + s2[0]) >> 1;
1131 s1++;
1132 s2++;
1133 d++;
1134 }
1135 src += 2 * src_wrap;
1136 dst += dst_wrap;
1137 }
1138}
1139
1140/* 2x2 -> 1x1 */
0c1a9eda 1141static void shrink22(uint8_t *dst, int dst_wrap,
e352ff08 1142 const uint8_t *src, int src_wrap,
85c242d8
FB
1143 int width, int height)
1144{
1145 int w;
e352ff08
FB
1146 const uint8_t *s1, *s2;
1147 uint8_t *d;
85c242d8
FB
1148
1149 for(;height > 0; height--) {
1150 s1 = src;
1151 s2 = s1 + src_wrap;
1152 d = dst;
1153 for(w = width;w >= 4; w-=4) {
0a9ad8d1
FB
1154 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1155 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1156 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1157 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
85c242d8
FB
1158 s1 += 8;
1159 s2 += 8;
1160 d += 4;
1161 }
1162 for(;w > 0; w--) {
0a9ad8d1 1163 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
85c242d8
FB
1164 s1 += 2;
1165 s2 += 2;
1166 d++;
1167 }
1168 src += 2 * src_wrap;
1169 dst += dst_wrap;
1170 }
1171}
1172
e352ff08
FB
1173/* 4x4 -> 1x1 */
1174static void shrink44(uint8_t *dst, int dst_wrap,
1175 const uint8_t *src, int src_wrap,
6742d95d
FR
1176 int width, int height)
1177{
1178 int w;
e352ff08
FB
1179 const uint8_t *s1, *s2, *s3, *s4;
1180 uint8_t *d;
6742d95d
FR
1181
1182 for(;height > 0; height--) {
1183 s1 = src;
e352ff08
FB
1184 s2 = s1 + src_wrap;
1185 s3 = s2 + src_wrap;
1186 s4 = s3 + src_wrap;
6742d95d 1187 d = dst;
e352ff08
FB
1188 for(w = width;w > 0; w--) {
1189 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1190 s2[0] + s2[1] + s2[2] + s2[3] +
1191 s3[0] + s3[1] + s3[2] + s3[3] +
1192 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1193 s1 += 4;
1194 s2 += 4;
1195 s3 += 4;
1196 s4 += 4;
6742d95d
FR
1197 d++;
1198 }
e352ff08
FB
1199 src += 4 * src_wrap;
1200 dst += dst_wrap;
1201 }
1202}
1203
1204static void grow21_line(uint8_t *dst, const uint8_t *src,
1205 int width)
1206{
1207 int w;
1208 const uint8_t *s1;
1209 uint8_t *d;
1210
1211 s1 = src;
1212 d = dst;
1213 for(w = width;w >= 4; w-=4) {
1214 d[1] = d[0] = s1[0];
1215 d[3] = d[2] = s1[1];
1216 s1 += 2;
1217 d += 4;
1218 }
1219 for(;w >= 2; w -= 2) {
1220 d[1] = d[0] = s1[0];
1221 s1 ++;
1222 d += 2;
1223 }
1224 /* only needed if width is not a multiple of two */
1225 /* XXX: veryfy that */
1226 if (w) {
1227 d[0] = s1[0];
1228 }
1229}
1230
1231static void grow41_line(uint8_t *dst, const uint8_t *src,
1232 int width)
1233{
1234 int w, v;
1235 const uint8_t *s1;
1236 uint8_t *d;
1237
1238 s1 = src;
1239 d = dst;
1240 for(w = width;w >= 4; w-=4) {
1241 v = s1[0];
1242 d[0] = v;
1243 d[1] = v;
1244 d[2] = v;
1245 d[3] = v;
1246 s1 ++;
1247 d += 4;
1248 }
1249}
1250
1251/* 1x1 -> 2x1 */
1252static void grow21(uint8_t *dst, int dst_wrap,
1253 const uint8_t *src, int src_wrap,
1254 int width, int height)
1255{
1256 for(;height > 0; height--) {
1257 grow21_line(dst, src, width);
1258 src += src_wrap;
1259 dst += dst_wrap;
1260 }
1261}
1262
1263/* 1x1 -> 2x2 */
1264static void grow22(uint8_t *dst, int dst_wrap,
1265 const uint8_t *src, int src_wrap,
1266 int width, int height)
1267{
1268 for(;height > 0; height--) {
1269 grow21_line(dst, src, width);
6742d95d
FR
1270 if (height%2)
1271 src += src_wrap;
1272 dst += dst_wrap;
1273 }
1274}
1275
e352ff08
FB
1276/* 1x1 -> 4x1 */
1277static void grow41(uint8_t *dst, int dst_wrap,
1278 const uint8_t *src, int src_wrap,
1279 int width, int height)
1280{
1281 for(;height > 0; height--) {
1282 grow41_line(dst, src, width);
1283 src += src_wrap;
1284 dst += dst_wrap;
1285 }
1286}
1287
1288/* 1x1 -> 4x4 */
1289static void grow44(uint8_t *dst, int dst_wrap,
1290 const uint8_t *src, int src_wrap,
1291 int width, int height)
1292{
1293 for(;height > 0; height--) {
1294 grow41_line(dst, src, width);
1295 if ((height & 3) == 1)
1296 src += src_wrap;
1297 dst += dst_wrap;
1298 }
1299}
1300
524c6b63 1301/* 1x2 -> 2x1 */
0c1a9eda 1302static void conv411(uint8_t *dst, int dst_wrap,
e352ff08 1303 const uint8_t *src, int src_wrap,
789587d5
FB
1304 int width, int height)
1305{
1306 int w, c;
e352ff08
FB
1307 const uint8_t *s1, *s2;
1308 uint8_t *d;
789587d5 1309
50643575
MN
1310 width>>=1;
1311
524c6b63 1312 for(;height > 0; height--) {
789587d5
FB
1313 s1 = src;
1314 s2 = src + src_wrap;
1315 d = dst;
1316 for(w = width;w > 0; w--) {
1317 c = (s1[0] + s2[0]) >> 1;
1318 d[0] = c;
1319 d[1] = c;
1320 s1++;
1321 s2++;
1322 d += 2;
1323 }
1324 src += src_wrap * 2;
1325 dst += dst_wrap;
1326 }
1327}
1328
7e7e5940
FB
1329/* XXX: add jpeg quantize code */
1330
1331#define TRANSP_INDEX (6*6*6)
1332
1333/* this is maybe slow, but allows for extensions */
1334static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
85c242d8 1335{
7e7e5940 1336 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
85c242d8
FB
1337}
1338
7e7e5940
FB
1339static void build_rgb_palette(uint8_t *palette, int has_alpha)
1340{
1341 uint32_t *pal;
1342 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1343 int i, r, g, b;
1344
1345 pal = (uint32_t *)palette;
1346 i = 0;
1347 for(r = 0; r < 6; r++) {
1348 for(g = 0; g < 6; g++) {
1349 for(b = 0; b < 6; b++) {
1350 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1351 (pal_value[g] << 8) | pal_value[b];
1352 }
1353 }
1354 }
1355 if (has_alpha)
1356 pal[i++] = 0;
1357 while (i < 256)
1358 pal[i++] = 0xff000000;
524c6b63
FB
1359}
1360
1361/* copy bit n to bits 0 ... n - 1 */
1362static inline unsigned int bitcopy_n(unsigned int a, int n)
b71472eb 1363{
524c6b63
FB
1364 int mask;
1365 mask = (1 << n) - 1;
1366 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1367}
1368
1369/* rgb555 handling */
1370
7e7e5940
FB
1371#define RGB_NAME rgb555
1372
524c6b63
FB
1373#define RGB_IN(r, g, b, s)\
1374{\
0c1a9eda 1375 unsigned int v = ((const uint16_t *)(s))[0];\
524c6b63
FB
1376 r = bitcopy_n(v >> (10 - 3), 3);\
1377 g = bitcopy_n(v >> (5 - 3), 3);\
1378 b = bitcopy_n(v << 3, 3);\
1379}
1380
7e7e5940 1381#define RGBA_IN(r, g, b, a, s)\
524c6b63 1382{\
7e7e5940
FB
1383 unsigned int v = ((const uint16_t *)(s))[0];\
1384 r = bitcopy_n(v >> (10 - 3), 3);\
1385 g = bitcopy_n(v >> (5 - 3), 3);\
1386 b = bitcopy_n(v << 3, 3);\
b5ff5e22 1387 a = (-(v >> 15)) & 0xff;\
7e7e5940
FB
1388}
1389
1390#define RGBA_OUT(d, r, g, b, a)\
1391{\
1392 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1393 ((a << 8) & 0x8000);\
524c6b63
FB
1394}
1395
1396#define BPP 2
1397
7e7e5940 1398#include "imgconvert_template.h"
524c6b63
FB
1399
1400/* rgb565 handling */
1401
7e7e5940
FB
1402#define RGB_NAME rgb565
1403
524c6b63
FB
1404#define RGB_IN(r, g, b, s)\
1405{\
0c1a9eda 1406 unsigned int v = ((const uint16_t *)(s))[0];\
524c6b63
FB
1407 r = bitcopy_n(v >> (11 - 3), 3);\
1408 g = bitcopy_n(v >> (5 - 2), 2);\
1409 b = bitcopy_n(v << 3, 3);\
1410}
1411
1412#define RGB_OUT(d, r, g, b)\
1413{\
0c1a9eda 1414 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
524c6b63
FB
1415}
1416
1417#define BPP 2
1418
7e7e5940 1419#include "imgconvert_template.h"
524c6b63
FB
1420
1421/* bgr24 handling */
1422
7e7e5940
FB
1423#define RGB_NAME bgr24
1424
524c6b63
FB
1425#define RGB_IN(r, g, b, s)\
1426{\
1427 b = (s)[0];\
1428 g = (s)[1];\
1429 r = (s)[2];\
1430}
1431
1432#define RGB_OUT(d, r, g, b)\
1433{\
1434 (d)[0] = b;\
1435 (d)[1] = g;\
1436 (d)[2] = r;\
1437}
1438
1439#define BPP 3
1440
7e7e5940 1441#include "imgconvert_template.h"
524c6b63
FB
1442
1443#undef RGB_IN
1444#undef RGB_OUT
1445#undef BPP
1446
1447/* rgb24 handling */
1448
7e7e5940
FB
1449#define RGB_NAME rgb24
1450#define FMT_RGB24
1451
524c6b63
FB
1452#define RGB_IN(r, g, b, s)\
1453{\
1454 r = (s)[0];\
1455 g = (s)[1];\
1456 b = (s)[2];\
1457}
1458
1459#define RGB_OUT(d, r, g, b)\
1460{\
1461 (d)[0] = r;\
1462 (d)[1] = g;\
1463 (d)[2] = b;\
1464}
1465
1466#define BPP 3
1467
7e7e5940 1468#include "imgconvert_template.h"
524c6b63
FB
1469
1470/* rgba32 handling */
1471
7e7e5940
FB
1472#define RGB_NAME rgba32
1473#define FMT_RGBA32
1474
524c6b63
FB
1475#define RGB_IN(r, g, b, s)\
1476{\
0c1a9eda 1477 unsigned int v = ((const uint32_t *)(s))[0];\
524c6b63
FB
1478 r = (v >> 16) & 0xff;\
1479 g = (v >> 8) & 0xff;\
1480 b = v & 0xff;\
1481}
1482
7e7e5940 1483#define RGBA_IN(r, g, b, a, s)\
524c6b63 1484{\
7e7e5940
FB
1485 unsigned int v = ((const uint32_t *)(s))[0];\
1486 a = (v >> 24) & 0xff;\
1487 r = (v >> 16) & 0xff;\
1488 g = (v >> 8) & 0xff;\
1489 b = v & 0xff;\
524c6b63
FB
1490}
1491
7e7e5940
FB
1492#define RGBA_OUT(d, r, g, b, a)\
1493{\
1494 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
b71472eb
PG
1495}
1496
7e7e5940 1497#define BPP 4
524c6b63 1498
7e7e5940 1499#include "imgconvert_template.h"
b71472eb 1500
da64ecc3 1501static void mono_to_gray(AVPicture *dst, const AVPicture *src,
2a877875 1502 int width, int height, int xor_mask)
524c6b63
FB
1503{
1504 const unsigned char *p;
1505 unsigned char *q;
1506 int v, dst_wrap, src_wrap;
1507 int y, w;
1508
1509 p = src->data[0];
1510 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1511
1512 q = dst->data[0];
2a877875 1513 dst_wrap = dst->linesize[0] - width;
524c6b63
FB
1514 for(y=0;y<height;y++) {
1515 w = width;
1516 while (w >= 8) {
2a877875
FB
1517 v = *p++ ^ xor_mask;
1518 q[0] = -(v >> 7);
1519 q[1] = -((v >> 6) & 1);
1520 q[2] = -((v >> 5) & 1);
1521 q[3] = -((v >> 4) & 1);
1522 q[4] = -((v >> 3) & 1);
1523 q[5] = -((v >> 2) & 1);
1524 q[6] = -((v >> 1) & 1);
1525 q[7] = -((v >> 0) & 1);
524c6b63 1526 w -= 8;
2a877875 1527 q += 8;
524c6b63
FB
1528 }
1529 if (w > 0) {
2a877875 1530 v = *p++ ^ xor_mask;
524c6b63 1531 do {
2a877875
FB
1532 q[0] = -((v >> 7) & 1);
1533 q++;
524c6b63
FB
1534 v <<= 1;
1535 } while (--w);
85c242d8 1536 }
524c6b63
FB
1537 p += src_wrap;
1538 q += dst_wrap;
85c242d8
FB
1539 }
1540}
1541
da64ecc3 1542static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
524c6b63
FB
1543 int width, int height)
1544{
2a877875
FB
1545 mono_to_gray(dst, src, width, height, 0xff);
1546}
524c6b63 1547
da64ecc3 1548static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
2a877875
FB
1549 int width, int height)
1550{
1551 mono_to_gray(dst, src, width, height, 0x00);
1552}
524c6b63 1553
da64ecc3 1554static void gray_to_mono(AVPicture *dst, const AVPicture *src,
2a877875
FB
1555 int width, int height, int xor_mask)
1556{
1557 int n;
0c1a9eda
ZK
1558 const uint8_t *s;
1559 uint8_t *d;
2a877875
FB
1560 int j, b, v, n1, src_wrap, dst_wrap, y;
1561
1562 s = src->data[0];
1563 src_wrap = src->linesize[0] - width;
1564
1565 d = dst->data[0];
1566 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
524c6b63
FB
1567
1568 for(y=0;y<height;y++) {
2a877875
FB
1569 n = width;
1570 while (n >= 8) {
1571 v = 0;
1572 for(j=0;j<8;j++) {
1573 b = s[0];
1574 s++;
1575 v = (v << 1) | (b >> 7);
1576 }
1577 d[0] = v ^ xor_mask;
1578 d++;
1579 n -= 8;
524c6b63 1580 }
2a877875
FB
1581 if (n > 0) {
1582 n1 = n;
1583 v = 0;
1584 while (n > 0) {
1585 b = s[0];
1586 s++;
1587 v = (v << 1) | (b >> 7);
1588 n--;
1589 }
1590 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1591 d++;
524c6b63 1592 }
2a877875
FB
1593 s += src_wrap;
1594 d += dst_wrap;
524c6b63
FB
1595 }
1596}
1597
da64ecc3 1598static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
2a877875
FB
1599 int width, int height)
1600{
1601 gray_to_mono(dst, src, width, height, 0xff);
1602}
1603
da64ecc3 1604static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
2a877875
FB
1605 int width, int height)
1606{
1607 gray_to_mono(dst, src, width, height, 0x00);
1608}
1609
524c6b63 1610typedef struct ConvertEntry {
da64ecc3
DH
1611 void (*convert)(AVPicture *dst,
1612 const AVPicture *src, int width, int height);
524c6b63
FB
1613} ConvertEntry;
1614
c50c0bc8
FB
1615/* Add each new convertion function in this table. In order to be able
1616 to convert from any format to any format, the following constraints
1617 must be satisfied:
1618
1619 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1620
1621 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1622
1623 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1624
e352ff08 1625 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
c50c0bc8
FB
1626 PIX_FMT_RGB24.
1627
1628 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
e352ff08
FB
1629
1630 The other conversion functions are just optimisations for common cases.
524c6b63
FB
1631*/
1632static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1633 [PIX_FMT_YUV420P] = {
1634 [PIX_FMT_RGB555] = {
ef9f7306 1635 .convert = yuv420p_to_rgb555
524c6b63
FB
1636 },
1637 [PIX_FMT_RGB565] = {
ef9f7306 1638 .convert = yuv420p_to_rgb565
524c6b63
FB
1639 },
1640 [PIX_FMT_BGR24] = {
ef9f7306 1641 .convert = yuv420p_to_bgr24
524c6b63
FB
1642 },
1643 [PIX_FMT_RGB24] = {
ef9f7306 1644 .convert = yuv420p_to_rgb24
524c6b63
FB
1645 },
1646 [PIX_FMT_RGBA32] = {
ef9f7306 1647 .convert = yuv420p_to_rgba32
524c6b63
FB
1648 },
1649 },
c50c0bc8
FB
1650 [PIX_FMT_YUV422P] = {
1651 [PIX_FMT_YUV422] = {
1652 .convert = yuv422p_to_yuv422,
1653 },
ebb177dd
TK
1654 [PIX_FMT_UYVY422] = {
1655 .convert = yuv422p_to_uyvy422,
1656 },
c50c0bc8
FB
1657 },
1658 [PIX_FMT_YUV444P] = {
1659 [PIX_FMT_RGB24] = {
1660 .convert = yuv444p_to_rgb24
1661 },
1662 },
1663 [PIX_FMT_YUVJ420P] = {
524c6b63 1664 [PIX_FMT_RGB555] = {
c50c0bc8 1665 .convert = yuvj420p_to_rgb555
524c6b63
FB
1666 },
1667 [PIX_FMT_RGB565] = {
c50c0bc8 1668 .convert = yuvj420p_to_rgb565
524c6b63
FB
1669 },
1670 [PIX_FMT_BGR24] = {
c50c0bc8 1671 .convert = yuvj420p_to_bgr24
524c6b63
FB
1672 },
1673 [PIX_FMT_RGB24] = {
c50c0bc8 1674 .convert = yuvj420p_to_rgb24
524c6b63
FB
1675 },
1676 [PIX_FMT_RGBA32] = {
c50c0bc8
FB
1677 .convert = yuvj420p_to_rgba32
1678 },
1679 },
1680 [PIX_FMT_YUVJ444P] = {
1681 [PIX_FMT_RGB24] = {
1682 .convert = yuvj444p_to_rgb24
524c6b63
FB
1683 },
1684 },
1685 [PIX_FMT_YUV422] = {
1686 [PIX_FMT_YUV420P] = {
ef9f7306 1687 .convert = yuv422_to_yuv420p,
524c6b63 1688 },
c50c0bc8
FB
1689 [PIX_FMT_YUV422P] = {
1690 .convert = yuv422_to_yuv422p,
1691 },
524c6b63 1692 },
ebb177dd
TK
1693 [PIX_FMT_UYVY422] = {
1694 [PIX_FMT_YUV420P] = {
1695 .convert = uyvy422_to_yuv420p,
1696 },
1697 [PIX_FMT_YUV422P] = {
1698 .convert = uyvy422_to_yuv422p,
1699 },
1700 },
524c6b63
FB
1701 [PIX_FMT_RGB24] = {
1702 [PIX_FMT_YUV420P] = {
ef9f7306 1703 .convert = rgb24_to_yuv420p
524c6b63
FB
1704 },
1705 [PIX_FMT_RGB565] = {
ef9f7306 1706 .convert = rgb24_to_rgb565
524c6b63
FB
1707 },
1708 [PIX_FMT_RGB555] = {
ef9f7306 1709 .convert = rgb24_to_rgb555
524c6b63 1710 },
7e7e5940
FB
1711 [PIX_FMT_RGBA32] = {
1712 .convert = rgb24_to_rgba32
1713 },
1714 [PIX_FMT_BGR24] = {
1715 .convert = rgb24_to_bgr24
1716 },
524c6b63 1717 [PIX_FMT_GRAY8] = {
ef9f7306 1718 .convert = rgb24_to_gray
524c6b63 1719 },
7e7e5940 1720 [PIX_FMT_PAL8] = {
7e6d70d0
FB
1721 .convert = rgb24_to_pal8
1722 },
c50c0bc8
FB
1723 [PIX_FMT_YUV444P] = {
1724 .convert = rgb24_to_yuv444p
1725 },
1726 [PIX_FMT_YUVJ420P] = {
1727 .convert = rgb24_to_yuvj420p
1728 },
1729 [PIX_FMT_YUVJ444P] = {
1730 .convert = rgb24_to_yuvj444p
1731 },
524c6b63
FB
1732 },
1733 [PIX_FMT_RGBA32] = {
7e7e5940
FB
1734 [PIX_FMT_RGB24] = {
1735 .convert = rgba32_to_rgb24
1736 },
1737 [PIX_FMT_RGB555] = {
1738 .convert = rgba32_to_rgb555
1739 },
1740 [PIX_FMT_PAL8] = {
1741 .convert = rgba32_to_pal8
1742 },
524c6b63 1743 [PIX_FMT_YUV420P] = {
ef9f7306 1744 .convert = rgba32_to_yuv420p
524c6b63 1745 },
69572401
FB
1746 [PIX_FMT_GRAY8] = {
1747 .convert = rgba32_to_gray
1748 },
524c6b63
FB
1749 },
1750 [PIX_FMT_BGR24] = {
7e7e5940
FB
1751 [PIX_FMT_RGB24] = {
1752 .convert = bgr24_to_rgb24
1753 },
524c6b63 1754 [PIX_FMT_YUV420P] = {
ef9f7306 1755 .convert = bgr24_to_yuv420p
524c6b63 1756 },
69572401
FB
1757 [PIX_FMT_GRAY8] = {
1758 .convert = bgr24_to_gray
1759 },
524c6b63
FB
1760 },
1761 [PIX_FMT_RGB555] = {
7e7e5940
FB
1762 [PIX_FMT_RGB24] = {
1763 .convert = rgb555_to_rgb24
1764 },
1765 [PIX_FMT_RGBA32] = {
1766 .convert = rgb555_to_rgba32
1767 },
524c6b63 1768 [PIX_FMT_YUV420P] = {
ef9f7306 1769 .convert = rgb555_to_yuv420p
524c6b63 1770 },
69572401
FB
1771 [PIX_FMT_GRAY8] = {
1772 .convert = rgb555_to_gray
1773 },
524c6b63
FB
1774 },
1775 [PIX_FMT_RGB565] = {
7e7e5940
FB
1776 [PIX_FMT_RGB24] = {
1777 .convert = rgb565_to_rgb24
1778 },
524c6b63 1779 [PIX_FMT_YUV420P] = {
ef9f7306 1780 .convert = rgb565_to_yuv420p
524c6b63 1781 },
69572401
FB
1782 [PIX_FMT_GRAY8] = {
1783 .convert = rgb565_to_gray
1784 },
524c6b63
FB
1785 },
1786 [PIX_FMT_GRAY8] = {
69572401
FB
1787 [PIX_FMT_RGB555] = {
1788 .convert = gray_to_rgb555
1789 },
1790 [PIX_FMT_RGB565] = {
1791 .convert = gray_to_rgb565
1792 },
524c6b63 1793 [PIX_FMT_RGB24] = {
ef9f7306 1794 .convert = gray_to_rgb24
524c6b63 1795 },
69572401
FB
1796 [PIX_FMT_BGR24] = {
1797 .convert = gray_to_bgr24
1798 },
1799 [PIX_FMT_RGBA32] = {
1800 .convert = gray_to_rgba32
1801 },
2a877875 1802 [PIX_FMT_MONOWHITE] = {
ef9f7306 1803 .convert = gray_to_monowhite
2a877875
FB
1804 },
1805 [PIX_FMT_MONOBLACK] = {
ef9f7306 1806 .convert = gray_to_monoblack
2a877875 1807 },
524c6b63
FB
1808 },
1809 [PIX_FMT_MONOWHITE] = {
2a877875 1810 [PIX_FMT_GRAY8] = {
ef9f7306 1811 .convert = monowhite_to_gray
524c6b63
FB
1812 },
1813 },
1814 [PIX_FMT_MONOBLACK] = {
2a877875 1815 [PIX_FMT_GRAY8] = {
ef9f7306 1816 .convert = monoblack_to_gray
524c6b63
FB
1817 },
1818 },
7e6d70d0
FB
1819 [PIX_FMT_PAL8] = {
1820 [PIX_FMT_RGB555] = {
1821 .convert = pal8_to_rgb555
1822 },
1823 [PIX_FMT_RGB565] = {
1824 .convert = pal8_to_rgb565
1825 },
1826 [PIX_FMT_BGR24] = {
1827 .convert = pal8_to_bgr24
1828 },
1829 [PIX_FMT_RGB24] = {
1830 .convert = pal8_to_rgb24
1831 },
1832 [PIX_FMT_RGBA32] = {
1833 .convert = pal8_to_rgba32
1834 },
1835 },
f02be79d
RS
1836 [PIX_FMT_UYVY411] = {
1837 [PIX_FMT_YUV411P] = {
1838 .convert = uyvy411_to_yuv411p,
1839 },
1840 },
1841
524c6b63
FB
1842};
1843
75917b88 1844int avpicture_alloc(AVPicture *picture,
524c6b63
FB
1845 int pix_fmt, int width, int height)
1846{
5c91a675 1847 unsigned int size;
524c6b63
FB
1848 void *ptr;
1849
1850 size = avpicture_get_size(pix_fmt, width, height);
524c6b63
FB
1851 ptr = av_malloc(size);
1852 if (!ptr)
1853 goto fail;
1854 avpicture_fill(picture, ptr, pix_fmt, width, height);
1855 return 0;
1856 fail:
1857 memset(picture, 0, sizeof(AVPicture));
1858 return -1;
1859}
1860
75917b88 1861void avpicture_free(AVPicture *picture)
524c6b63 1862{
8e1e6f31 1863 av_free(picture->data[0]);
524c6b63
FB
1864}
1865
c50c0bc8
FB
1866/* return true if yuv planar */
1867static inline int is_yuv_planar(PixFmtInfo *ps)
1868{
1869 return (ps->color_type == FF_COLOR_YUV ||
7e7e5940
FB
1870 ps->color_type == FF_COLOR_YUV_JPEG) &&
1871 ps->pixel_type == FF_PIXEL_PLANAR;
c50c0bc8
FB
1872}
1873
85c242d8
FB
1874/* XXX: always use linesize. Return -1 if not supported */
1875int img_convert(AVPicture *dst, int dst_pix_fmt,
da64ecc3 1876 const AVPicture *src, int src_pix_fmt,
524c6b63 1877 int src_width, int src_height)
85c242d8 1878{
c50c0bc8 1879 static int inited;
2a877875 1880 int i, ret, dst_width, dst_height, int_pix_fmt;
524c6b63
FB
1881 PixFmtInfo *src_pix, *dst_pix;
1882 ConvertEntry *ce;
1883 AVPicture tmp1, *tmp = &tmp1;
1884
1885 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
1886 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
1887 return -1;
1888 if (src_width <= 0 || src_height <= 0)
1889 return 0;
69572401 1890
c50c0bc8
FB
1891 if (!inited) {
1892 inited = 1;
1893 img_convert_init();
1894 }
1895
524c6b63
FB
1896 dst_width = src_width;
1897 dst_height = src_height;
69572401 1898
524c6b63
FB
1899 dst_pix = &pix_fmt_info[dst_pix_fmt];
1900 src_pix = &pix_fmt_info[src_pix_fmt];
1901 if (src_pix_fmt == dst_pix_fmt) {
7e7e5940
FB
1902 /* no conversion needed: just copy */
1903 img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
524c6b63
FB
1904 return 0;
1905 }
1906
1907 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
1908 if (ce->convert) {
ebb177dd 1909 /* specific conversion routine */
524c6b63
FB
1910 ce->convert(dst, src, dst_width, dst_height);
1911 return 0;
1912 }
1913
524c6b63 1914 /* gray to YUV */
c50c0bc8 1915 if (is_yuv_planar(dst_pix) &&
b6147995 1916 src_pix_fmt == PIX_FMT_GRAY8) {
524c6b63
FB
1917 int w, h, y;
1918 uint8_t *d;
1919
b6147995 1920 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
7e7e5940 1921 img_copy_plane(dst->data[0], dst->linesize[0],
b6147995
FB
1922 src->data[0], src->linesize[0],
1923 dst_width, dst_height);
1924 } else {
c50c0bc8
FB
1925 img_apply_table(dst->data[0], dst->linesize[0],
1926 src->data[0], src->linesize[0],
1927 dst_width, dst_height,
1928 y_jpeg_to_ccir);
b6147995 1929 }
524c6b63
FB
1930 /* fill U and V with 128 */
1931 w = dst_width;
1932 h = dst_height;
1933 w >>= dst_pix->x_chroma_shift;
1934 h >>= dst_pix->y_chroma_shift;
1935 for(i = 1; i <= 2; i++) {
1936 d = dst->data[i];
2a877875
FB
1937 for(y = 0; y< h; y++) {
1938 memset(d, 128, w);
524c6b63
FB
1939 d += dst->linesize[i];
1940 }
b71472eb 1941 }
524c6b63
FB
1942 return 0;
1943 }
1944
1945 /* YUV to gray */
c50c0bc8 1946 if (is_yuv_planar(src_pix) &&
b6147995
FB
1947 dst_pix_fmt == PIX_FMT_GRAY8) {
1948 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
7e7e5940 1949 img_copy_plane(dst->data[0], dst->linesize[0],
b6147995
FB
1950 src->data[0], src->linesize[0],
1951 dst_width, dst_height);
1952 } else {
c50c0bc8
FB
1953 img_apply_table(dst->data[0], dst->linesize[0],
1954 src->data[0], src->linesize[0],
1955 dst_width, dst_height,
1956 y_ccir_to_jpeg);
b6147995 1957 }
524c6b63
FB
1958 return 0;
1959 }
1960
c50c0bc8
FB
1961 /* YUV to YUV planar */
1962 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
e352ff08 1963 int x_shift, y_shift, w, h, xy_shift;
0c1a9eda 1964 void (*resize_func)(uint8_t *dst, int dst_wrap,
e352ff08 1965 const uint8_t *src, int src_wrap,
524c6b63
FB
1966 int width, int height);
1967
1968 /* compute chroma size of the smallest dimensions */
1969 w = dst_width;
1970 h = dst_height;
1971 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
1972 w >>= dst_pix->x_chroma_shift;
1973 else
1974 w >>= src_pix->x_chroma_shift;
1975 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
1976 h >>= dst_pix->y_chroma_shift;
1977 else
1978 h >>= src_pix->y_chroma_shift;
1979
1980 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
1981 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
e352ff08
FB
1982 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
1983 /* there must be filters for conversion at least from and to
1984 YUV444 format */
1985 switch(xy_shift) {
1986 case 0x00:
7e7e5940 1987 resize_func = img_copy_plane;
e352ff08
FB
1988 break;
1989 case 0x10:
1990 resize_func = shrink21;
1991 break;
1992 case 0x20:
1993 resize_func = shrink41;
1994 break;
1995 case 0x01:
1996 resize_func = shrink12;
1997 break;
1998 case 0x11:
524c6b63 1999 resize_func = shrink22;
e352ff08
FB
2000 break;
2001 case 0x22:
2002 resize_func = shrink44;
2003 break;
2004 case 0xf0:
2005 resize_func = grow21;
2006 break;
2007 case 0xe0:
2008 resize_func = grow41;
2009 break;
2010 case 0xff:
524c6b63 2011 resize_func = grow22;
e352ff08
FB
2012 break;
2013 case 0xee:
2014 resize_func = grow44;
2015 break;
2016 case 0xf1:
524c6b63 2017 resize_func = conv411;
e352ff08
FB
2018 break;
2019 default:
524c6b63 2020 /* currently not handled */
e352ff08 2021 goto no_chroma_filter;
85c242d8 2022 }
524c6b63 2023
7e7e5940
FB
2024 img_copy_plane(dst->data[0], dst->linesize[0],
2025 src->data[0], src->linesize[0],
2026 dst_width, dst_height);
566986ee 2027
524c6b63 2028 for(i = 1;i <= 2; i++)
566986ee
MK
2029 resize_func(dst->data[i], dst->linesize[i],
2030 src->data[i], src->linesize[i],
185fdc54 2031 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
c50c0bc8
FB
2032 /* if yuv color space conversion is needed, we do it here on
2033 the destination image */
2034 if (dst_pix->color_type != src_pix->color_type) {
2035 const uint8_t *y_table, *c_table;
2036 if (dst_pix->color_type == FF_COLOR_YUV) {
2037 y_table = y_jpeg_to_ccir;
2038 c_table = c_jpeg_to_ccir;
2039 } else {
2040 y_table = y_ccir_to_jpeg;
2041 c_table = c_ccir_to_jpeg;
2042 }
2043 img_apply_table(dst->data[0], dst->linesize[0],
2044 dst->data[0], dst->linesize[0],
2045 dst_width, dst_height,
2046 y_table);
2047
2048 for(i = 1;i <= 2; i++)
2049 img_apply_table(dst->data[i], dst->linesize[i],
2050 dst->data[i], dst->linesize[i],
2051 dst_width>>dst_pix->x_chroma_shift,
2052 dst_height>>dst_pix->y_chroma_shift,
2053 c_table);
2054 }
2055 return 0;
85c242d8 2056 }
e352ff08 2057 no_chroma_filter:
524c6b63 2058
2a877875 2059 /* try to use an intermediate format */
c50c0bc8
FB
2060 if (src_pix_fmt == PIX_FMT_YUV422 ||
2061 dst_pix_fmt == PIX_FMT_YUV422) {
2062 /* specific case: convert to YUV422P first */
2063 int_pix_fmt = PIX_FMT_YUV422P;
ebb177dd
TK
2064 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2065 dst_pix_fmt == PIX_FMT_UYVY422) {
2066 /* specific case: convert to YUV422P first */
2067 int_pix_fmt = PIX_FMT_YUV422P;
f02be79d
RS
2068 } else if (src_pix_fmt == PIX_FMT_UYVY411 ||
2069 dst_pix_fmt == PIX_FMT_UYVY411) {
2070 /* specific case: convert to YUV411P first */
2071 int_pix_fmt = PIX_FMT_YUV411P;
c50c0bc8
FB
2072 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2073 src_pix_fmt != PIX_FMT_GRAY8) ||
2074 (dst_pix->color_type == FF_COLOR_GRAY &&
2075 dst_pix_fmt != PIX_FMT_GRAY8)) {
2076 /* gray8 is the normalized format */
2a877875 2077 int_pix_fmt = PIX_FMT_GRAY8;
c50c0bc8
FB
2078 } else if ((is_yuv_planar(src_pix) &&
2079 src_pix_fmt != PIX_FMT_YUV444P &&
2080 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2081 /* yuv444 is the normalized format */
2082 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2083 int_pix_fmt = PIX_FMT_YUVJ444P;
2084 else
2085 int_pix_fmt = PIX_FMT_YUV444P;
2086 } else if ((is_yuv_planar(dst_pix) &&
2087 dst_pix_fmt != PIX_FMT_YUV444P &&
2088 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2089 /* yuv444 is the normalized format */
2090 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2091 int_pix_fmt = PIX_FMT_YUVJ444P;
2092 else
2093 int_pix_fmt = PIX_FMT_YUV444P;
2a877875 2094 } else {
c50c0bc8
FB
2095 /* the two formats are rgb or gray8 or yuv[j]444p */
2096 if (src_pix->is_alpha && dst_pix->is_alpha)
2097 int_pix_fmt = PIX_FMT_RGBA32;
2098 else
2099 int_pix_fmt = PIX_FMT_RGB24;
2a877875
FB
2100 }
2101 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2102 return -1;
2103 ret = -1;
2104 if (img_convert(tmp, int_pix_fmt,
2105 src, src_pix_fmt, src_width, src_height) < 0)
2106 goto fail1;
2107 if (img_convert(dst, dst_pix_fmt,
2108 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2109 goto fail1;
2110 ret = 0;
2111 fail1:
2112 avpicture_free(tmp);
2113 return ret;
85c242d8
FB
2114}
2115
0469baf1 2116/* NOTE: we scan all the pixels to have an exact information */
da64ecc3 2117static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
0469baf1
FB
2118{
2119 const unsigned char *p;
2120 int src_wrap, ret, x, y;
2121 unsigned int a;
2122 uint32_t *palette = (uint32_t *)src->data[1];
2123
2124 p = src->data[0];
2125 src_wrap = src->linesize[0] - width;
2126 ret = 0;
2127 for(y=0;y<height;y++) {
2128 for(x=0;x<width;x++) {
2129 a = palette[p[0]] >> 24;
2130 if (a == 0x00) {
2131 ret |= FF_ALPHA_TRANSP;
2132 } else if (a != 0xff) {
2133 ret |= FF_ALPHA_SEMI_TRANSP;
2134 }
2135 p++;
2136 }
2137 p += src_wrap;
2138 }
2139 return ret;
2140}
2141
2142/**
2143 * Tell if an image really has transparent alpha values.
2144 * @return ored mask of FF_ALPHA_xxx constants
2145 */
da64ecc3
DH
2146int img_get_alpha_info(const AVPicture *src,
2147 int pix_fmt, int width, int height)
0469baf1
FB
2148{
2149 PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2150 int ret;
2151
2152 pf = &pix_fmt_info[pix_fmt];
2153 /* no alpha can be represented in format */
2154 if (!pf->is_alpha)
2155 return 0;
2156 switch(pix_fmt) {
2157 case PIX_FMT_RGBA32:
2158 ret = get_alpha_info_rgba32(src, width, height);
2159 break;
2160 case PIX_FMT_RGB555:
2161 ret = get_alpha_info_rgb555(src, width, height);
2162 break;
2163 case PIX_FMT_PAL8:
2164 ret = get_alpha_info_pal8(src, width, height);
2165 break;
2166 default:
2167 /* we do not know, so everything is indicated */
2168 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2169 break;
2170 }
2171 return ret;
2172}
5981f4e6
F
2173
2174#ifdef HAVE_MMX
2175#define DEINT_INPLACE_LINE_LUM \
2176 movd_m2r(lum_m4[0],mm0);\
2177 movd_m2r(lum_m3[0],mm1);\
2178 movd_m2r(lum_m2[0],mm2);\
2179 movd_m2r(lum_m1[0],mm3);\
2180 movd_m2r(lum[0],mm4);\
2181 punpcklbw_r2r(mm7,mm0);\
2182 movd_r2m(mm2,lum_m4[0]);\
2183 punpcklbw_r2r(mm7,mm1);\
2184 punpcklbw_r2r(mm7,mm2);\
2185 punpcklbw_r2r(mm7,mm3);\
2186 punpcklbw_r2r(mm7,mm4);\
2187 paddw_r2r(mm3,mm1);\
2188 psllw_i2r(1,mm2);\
2189 paddw_r2r(mm4,mm0);\
2190 psllw_i2r(2,mm1);\
2191 paddw_r2r(mm6,mm2);\
2192 paddw_r2r(mm2,mm1);\
2193 psubusw_r2r(mm0,mm1);\
2194 psrlw_i2r(3,mm1);\
2195 packuswb_r2r(mm7,mm1);\
2196 movd_r2m(mm1,lum_m2[0]);
2197
2198#define DEINT_LINE_LUM \
2199 movd_m2r(lum_m4[0],mm0);\
2200 movd_m2r(lum_m3[0],mm1);\
2201 movd_m2r(lum_m2[0],mm2);\
2202 movd_m2r(lum_m1[0],mm3);\
2203 movd_m2r(lum[0],mm4);\
2204 punpcklbw_r2r(mm7,mm0);\
2205 punpcklbw_r2r(mm7,mm1);\
2206 punpcklbw_r2r(mm7,mm2);\
2207 punpcklbw_r2r(mm7,mm3);\
2208 punpcklbw_r2r(mm7,mm4);\
2209 paddw_r2r(mm3,mm1);\
2210 psllw_i2r(1,mm2);\
2211 paddw_r2r(mm4,mm0);\
2212 psllw_i2r(2,mm1);\
2213 paddw_r2r(mm6,mm2);\
2214 paddw_r2r(mm2,mm1);\
2215 psubusw_r2r(mm0,mm1);\
2216 psrlw_i2r(3,mm1);\
2217 packuswb_r2r(mm7,mm1);\
2218 movd_r2m(mm1,dst[0]);
2219#endif
2220
85c242d8 2221/* filter parameters: [-1 4 2 4 -1] // 8 */
da64ecc3
DH
2222static void deinterlace_line(uint8_t *dst,
2223 const uint8_t *lum_m4, const uint8_t *lum_m3,
2224 const uint8_t *lum_m2, const uint8_t *lum_m1,
2225 const uint8_t *lum,
2226 int size)
85c242d8 2227{
5981f4e6 2228#ifndef HAVE_MMX
0c1a9eda 2229 uint8_t *cm = cropTbl + MAX_NEG_CROP;
85c242d8 2230 int sum;
85c242d8
FB
2231
2232 for(;size > 0;size--) {
5981f4e6
F
2233 sum = -lum_m4[0];
2234 sum += lum_m3[0] << 2;
2235 sum += lum_m2[0] << 1;
2236 sum += lum_m1[0] << 2;
2237 sum += -lum[0];
85c242d8 2238 dst[0] = cm[(sum + 4) >> 3];
5981f4e6
F
2239 lum_m4++;
2240 lum_m3++;
2241 lum_m2++;
2242 lum_m1++;
2243 lum++;
85c242d8 2244 dst++;
85c242d8 2245 }
5981f4e6
F
2246#else
2247
782c5984
MN
2248 {
2249 mmx_t rounder;
2250 rounder.uw[0]=4;
2251 rounder.uw[1]=4;
2252 rounder.uw[2]=4;
2253 rounder.uw[3]=4;
2254 pxor_r2r(mm7,mm7);
2255 movq_m2r(rounder,mm6);
2256 }
5981f4e6
F
2257 for (;size > 3; size-=4) {
2258 DEINT_LINE_LUM
2259 lum_m4+=4;
2260 lum_m3+=4;
2261 lum_m2+=4;
2262 lum_m1+=4;
2263 lum+=4;
2264 dst+=4;
2265 }
2266#endif
2267}
0c1a9eda 2268static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
5981f4e6
F
2269 int size)
2270{
2271#ifndef HAVE_MMX
0c1a9eda 2272 uint8_t *cm = cropTbl + MAX_NEG_CROP;
5981f4e6
F
2273 int sum;
2274
2275 for(;size > 0;size--) {
2276 sum = -lum_m4[0];
2277 sum += lum_m3[0] << 2;
2278 sum += lum_m2[0] << 1;
2279 lum_m4[0]=lum_m2[0];
2280 sum += lum_m1[0] << 2;
2281 sum += -lum[0];
2282 lum_m2[0] = cm[(sum + 4) >> 3];
2283 lum_m4++;
2284 lum_m3++;
2285 lum_m2++;
2286 lum_m1++;
2287 lum++;
2288 }
2289#else
2290
782c5984
MN
2291 {
2292 mmx_t rounder;
2293 rounder.uw[0]=4;
2294 rounder.uw[1]=4;
2295 rounder.uw[2]=4;
2296 rounder.uw[3]=4;
2297 pxor_r2r(mm7,mm7);
2298 movq_m2r(rounder,mm6);
2299 }
5981f4e6
F
2300 for (;size > 3; size-=4) {
2301 DEINT_INPLACE_LINE_LUM
2302 lum_m4+=4;
2303 lum_m3+=4;
2304 lum_m2+=4;
2305 lum_m1+=4;
2306 lum+=4;
2307 }
2308#endif
85c242d8
FB
2309}
2310
2311/* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2312 top field is copied as is, but the bottom field is deinterlaced
2313 against the top field. */
0c1a9eda 2314static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
da64ecc3 2315 const uint8_t *src1, int src_wrap,
5981f4e6 2316 int width, int height)
85c242d8 2317{
da64ecc3 2318 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
5981f4e6
F
2319 int y;
2320
2321 src_m2 = src1;
2322 src_m1 = src1;
2323 src_0=&src_m1[src_wrap];
2324 src_p1=&src_0[src_wrap];
2325 src_p2=&src_p1[src_wrap];
2326 for(y=0;y<(height-2);y+=2) {
2327 memcpy(dst,src_m1,width);
85c242d8 2328 dst += dst_wrap;
5981f4e6
F
2329 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2330 src_m2 = src_0;
2331 src_m1 = src_p1;
2332 src_0 = src_p2;
2333 src_p1 += 2*src_wrap;
2334 src_p2 += 2*src_wrap;
85c242d8 2335 dst += dst_wrap;
85c242d8 2336 }
5981f4e6
F
2337 memcpy(dst,src_m1,width);
2338 dst += dst_wrap;
2339 /* do last line */
2340 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2341}
2342
0c1a9eda 2343static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
da64ecc3 2344 int width, int height)
5981f4e6 2345{
0c1a9eda 2346 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
5981f4e6 2347 int y;
0c1a9eda
ZK
2348 uint8_t *buf;
2349 buf = (uint8_t*)av_malloc(width);
5981f4e6
F
2350
2351 src_m1 = src1;
2352 memcpy(buf,src_m1,width);
2353 src_0=&src_m1[src_wrap];
2354 src_p1=&src_0[src_wrap];
2355 src_p2=&src_p1[src_wrap];
2356 for(y=0;y<(height-2);y+=2) {
2357 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2358 src_m1 = src_p1;
2359 src_0 = src_p2;
2360 src_p1 += 2*src_wrap;
2361 src_p2 += 2*src_wrap;
2362 }
2363 /* do last line */
2364 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
6000abfa 2365 av_free(buf);
85c242d8
FB
2366}
2367
2368
5981f4e6 2369/* deinterlace - if not supported return -1 */
da64ecc3 2370int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
de6d9b64
FB
2371 int pix_fmt, int width, int height)
2372{
85c242d8
FB
2373 int i;
2374
2375 if (pix_fmt != PIX_FMT_YUV420P &&
2376 pix_fmt != PIX_FMT_YUV422P &&
47017dd8
RS
2377 pix_fmt != PIX_FMT_YUV444P &&
2378 pix_fmt != PIX_FMT_YUV411P)
85c242d8 2379 return -1;
5981f4e6 2380 if ((width & 3) != 0 || (height & 3) != 0)
85c242d8 2381 return -1;
5981f4e6 2382
85c242d8
FB
2383 for(i=0;i<3;i++) {
2384 if (i == 1) {
2385 switch(pix_fmt) {
2386 case PIX_FMT_YUV420P:
2387 width >>= 1;
2388 height >>= 1;
2389 break;
2390 case PIX_FMT_YUV422P:
2391 width >>= 1;
2392 break;
47017dd8
RS
2393 case PIX_FMT_YUV411P:
2394 width >>= 2;
2395 break;
85c242d8
FB
2396 default:
2397 break;
2398 }
2399 }
5981f4e6 2400 if (src == dst) {
da64ecc3 2401 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
85c242d8 2402 width, height);
5981f4e6
F
2403 } else {
2404 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2405 src->data[i], src->linesize[i],
2406 width, height);
2407 }
de6d9b64 2408 }
5981f4e6
F
2409#ifdef HAVE_MMX
2410 emms();
2411#endif
85c242d8 2412 return 0;
de6d9b64 2413}
cd4af68a
ZK
2414
2415#undef FIX