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