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