imgconvert.c 76.5 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * Misc image convertion routines
3
 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
20
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
21 22

/**
Michael Niedermayer's avatar
Michael Niedermayer committed
23
 * @file imgconvert.c
Michael Niedermayer's avatar
Michael Niedermayer committed
24 25 26
 * Misc image convertion routines.
 */

27 28 29 30 31
/* TODO:
 * - write 'ffimg' program to test all the image related stuff
 * - move all api to slice based system
 * - integrate deinterlacing, postprocessing and scaling in the conversion process
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
32

Fabrice Bellard's avatar
Fabrice Bellard committed
33
#include "avcodec.h"
34
#include "dsputil.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
35

36
#ifdef USE_FASTMEMCPY
37
#include "libvo/fastmemcpy.h"
38
#endif
Fred's avatar
Fred committed
39 40 41 42

#ifdef HAVE_MMX
#include "i386/mmx.h"
#endif
43

44 45 46
#define xglue(x, y) x ## y
#define glue(x, y) xglue(x, y)

47 48 49 50 51
#define FF_COLOR_RGB      0 /* RGB color space */
#define FF_COLOR_GRAY     1 /* gray color space */
#define FF_COLOR_YUV      2 /* YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
#define FF_COLOR_YUV_JPEG 3 /* YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */

52 53 54 55
#define FF_PIXEL_PLANAR   0 /* each channel has one component in AVPicture */
#define FF_PIXEL_PACKED   1 /* only one components containing all the channels */
#define FF_PIXEL_PALETTE  2  /* one components containing indexes for a palette */

56 57
typedef struct PixFmtInfo {
    const char *name;
58 59 60
    uint8_t nb_channels;     /* number of channels (including alpha) */
    uint8_t color_type;      /* color type (see FF_COLOR_xxx constants) */
    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 63 64
    uint8_t x_chroma_shift;  /* X chroma subsampling factor is 2 ^ shift */
    uint8_t y_chroma_shift;  /* Y chroma subsampling factor is 2 ^ shift */
    uint8_t depth;           /* bit depth of the color components */
65 66 67
} PixFmtInfo;

/* this table gives more information about formats */
68
static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
69 70
    /* YUV formats */
    [PIX_FMT_YUV420P] = {
71
        .name = "yuv420p",
72
        .nb_channels = 3,
73
        .color_type = FF_COLOR_YUV,
74
        .pixel_type = FF_PIXEL_PLANAR,
75
        .depth = 8,
76
        .x_chroma_shift = 1, .y_chroma_shift = 1,
77 78
    },
    [PIX_FMT_YUV422P] = {
79
        .name = "yuv422p",
80
        .nb_channels = 3,
81
        .color_type = FF_COLOR_YUV,
82
        .pixel_type = FF_PIXEL_PLANAR,
83
        .depth = 8,
84
        .x_chroma_shift = 1, .y_chroma_shift = 0,
85 86
    },
    [PIX_FMT_YUV444P] = {
87
        .name = "yuv444p",
88
        .nb_channels = 3,
89
        .color_type = FF_COLOR_YUV,
90
        .pixel_type = FF_PIXEL_PLANAR,
91
        .depth = 8,
92
        .x_chroma_shift = 0, .y_chroma_shift = 0,
93
    },
94 95
    [PIX_FMT_YUYV422] = {
        .name = "yuyv422",
96
        .nb_channels = 1,
97
        .color_type = FF_COLOR_YUV,
98
        .pixel_type = FF_PIXEL_PACKED,
99
        .depth = 8,
100
        .x_chroma_shift = 1, .y_chroma_shift = 0,
101
    },
102 103 104 105 106 107 108 109
    [PIX_FMT_UYVY422] = {
        .name = "uyvy422",
        .nb_channels = 1,
        .color_type = FF_COLOR_YUV,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 8,
        .x_chroma_shift = 1, .y_chroma_shift = 0,
    },
110
    [PIX_FMT_YUV410P] = {
111
        .name = "yuv410p",
112
        .nb_channels = 3,
113
        .color_type = FF_COLOR_YUV,
114
        .pixel_type = FF_PIXEL_PLANAR,
115
        .depth = 8,
116
        .x_chroma_shift = 2, .y_chroma_shift = 2,
117 118
    },
    [PIX_FMT_YUV411P] = {
119
        .name = "yuv411p",
120
        .nb_channels = 3,
121
        .color_type = FF_COLOR_YUV,
122
        .pixel_type = FF_PIXEL_PLANAR,
123
        .depth = 8,
124
        .x_chroma_shift = 2, .y_chroma_shift = 0,
125 126
    },

127 128 129
    /* JPEG YUV */
    [PIX_FMT_YUVJ420P] = {
        .name = "yuvj420p",
130
        .nb_channels = 3,
131
        .color_type = FF_COLOR_YUV_JPEG,
132
        .pixel_type = FF_PIXEL_PLANAR,
133
        .depth = 8,
134
        .x_chroma_shift = 1, .y_chroma_shift = 1,
135 136 137
    },
    [PIX_FMT_YUVJ422P] = {
        .name = "yuvj422p",
138
        .nb_channels = 3,
139
        .color_type = FF_COLOR_YUV_JPEG,
140
        .pixel_type = FF_PIXEL_PLANAR,
141
        .depth = 8,
142
        .x_chroma_shift = 1, .y_chroma_shift = 0,
143 144 145
    },
    [PIX_FMT_YUVJ444P] = {
        .name = "yuvj444p",
146
        .nb_channels = 3,
147
        .color_type = FF_COLOR_YUV_JPEG,
148
        .pixel_type = FF_PIXEL_PLANAR,
149
        .depth = 8,
150
        .x_chroma_shift = 0, .y_chroma_shift = 0,
151 152
    },

153 154
    /* RGB formats */
    [PIX_FMT_RGB24] = {
155
        .name = "rgb24",
156
        .nb_channels = 3,
157
        .color_type = FF_COLOR_RGB,
158
        .pixel_type = FF_PIXEL_PACKED,
159
        .depth = 8,
Michael Niedermayer's avatar
Michael Niedermayer committed
160
        .x_chroma_shift = 0, .y_chroma_shift = 0,
161 162
    },
    [PIX_FMT_BGR24] = {
163
        .name = "bgr24",
164
        .nb_channels = 3,
165
        .color_type = FF_COLOR_RGB,
166
        .pixel_type = FF_PIXEL_PACKED,
167
        .depth = 8,
Michael Niedermayer's avatar
Michael Niedermayer committed
168
        .x_chroma_shift = 0, .y_chroma_shift = 0,
169
    },
170 171
    [PIX_FMT_RGB32] = {
        .name = "rgb32",
172
        .nb_channels = 4, .is_alpha = 1,
173
        .color_type = FF_COLOR_RGB,
174
        .pixel_type = FF_PIXEL_PACKED,
175
        .depth = 8,
Michael Niedermayer's avatar
Michael Niedermayer committed
176
        .x_chroma_shift = 0, .y_chroma_shift = 0,
177 178
    },
    [PIX_FMT_RGB565] = {
179
        .name = "rgb565",
180
        .nb_channels = 3,
181
        .color_type = FF_COLOR_RGB,
182
        .pixel_type = FF_PIXEL_PACKED,
183
        .depth = 5,
Michael Niedermayer's avatar
Michael Niedermayer committed
184
        .x_chroma_shift = 0, .y_chroma_shift = 0,
185 186
    },
    [PIX_FMT_RGB555] = {
187
        .name = "rgb555",
188
        .nb_channels = 3,
189
        .color_type = FF_COLOR_RGB,
190
        .pixel_type = FF_PIXEL_PACKED,
191
        .depth = 5,
Michael Niedermayer's avatar
Michael Niedermayer committed
192
        .x_chroma_shift = 0, .y_chroma_shift = 0,
193 194 195
    },

    /* gray / mono formats */
196 197 198 199 200 201 202 203 204 205 206 207 208 209
    [PIX_FMT_GRAY16BE] = {
        .name = "gray16be",
        .nb_channels = 1,
        .color_type = FF_COLOR_GRAY,
        .pixel_type = FF_PIXEL_PLANAR,
        .depth = 16,
    },
    [PIX_FMT_GRAY16LE] = {
        .name = "gray16le",
        .nb_channels = 1,
        .color_type = FF_COLOR_GRAY,
        .pixel_type = FF_PIXEL_PLANAR,
        .depth = 16,
    },
210
    [PIX_FMT_GRAY8] = {
211
        .name = "gray",
212
        .nb_channels = 1,
213
        .color_type = FF_COLOR_GRAY,
214
        .pixel_type = FF_PIXEL_PLANAR,
215
        .depth = 8,
216 217
    },
    [PIX_FMT_MONOWHITE] = {
218
        .name = "monow",
219
        .nb_channels = 1,
220
        .color_type = FF_COLOR_GRAY,
221
        .pixel_type = FF_PIXEL_PLANAR,
222
        .depth = 1,
223 224
    },
    [PIX_FMT_MONOBLACK] = {
225
        .name = "monob",
226
        .nb_channels = 1,
227
        .color_type = FF_COLOR_GRAY,
228
        .pixel_type = FF_PIXEL_PLANAR,
229
        .depth = 1,
230
    },
231 232 233 234

    /* paletted formats */
    [PIX_FMT_PAL8] = {
        .name = "pal8",
235
        .nb_channels = 4, .is_alpha = 1,
236
        .color_type = FF_COLOR_RGB,
237
        .pixel_type = FF_PIXEL_PALETTE,
238
        .depth = 8,
239
    },
240 241 242 243 244 245
    [PIX_FMT_XVMC_MPEG2_MC] = {
        .name = "xvmcmc",
    },
    [PIX_FMT_XVMC_MPEG2_IDCT] = {
        .name = "xvmcidct",
    },
246 247
    [PIX_FMT_UYYVYY411] = {
        .name = "uyyvyy411",
248 249 250 251 252 253
        .nb_channels = 1,
        .color_type = FF_COLOR_YUV,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 8,
        .x_chroma_shift = 2, .y_chroma_shift = 0,
    },
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    [PIX_FMT_BGR32] = {
        .name = "bgr32",
        .nb_channels = 4, .is_alpha = 1,
        .color_type = FF_COLOR_RGB,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 8,
        .x_chroma_shift = 0, .y_chroma_shift = 0,
    },
    [PIX_FMT_BGR565] = {
        .name = "bgr565",
        .nb_channels = 3,
        .color_type = FF_COLOR_RGB,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 5,
        .x_chroma_shift = 0, .y_chroma_shift = 0,
    },
    [PIX_FMT_BGR555] = {
        .name = "bgr555",
272
        .nb_channels = 3,
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
        .color_type = FF_COLOR_RGB,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 5,
        .x_chroma_shift = 0, .y_chroma_shift = 0,
    },
    [PIX_FMT_RGB8] = {
        .name = "rgb8",
        .nb_channels = 1,
        .color_type = FF_COLOR_RGB,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 8,
        .x_chroma_shift = 0, .y_chroma_shift = 0,
    },
    [PIX_FMT_RGB4] = {
        .name = "rgb4",
        .nb_channels = 1,
        .color_type = FF_COLOR_RGB,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 4,
        .x_chroma_shift = 0, .y_chroma_shift = 0,
    },
    [PIX_FMT_RGB4_BYTE] = {
        .name = "rgb4_byte",
        .nb_channels = 1,
        .color_type = FF_COLOR_RGB,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 8,
        .x_chroma_shift = 0, .y_chroma_shift = 0,
    },
    [PIX_FMT_BGR8] = {
        .name = "bgr8",
        .nb_channels = 1,
        .color_type = FF_COLOR_RGB,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 8,
        .x_chroma_shift = 0, .y_chroma_shift = 0,
    },
    [PIX_FMT_BGR4] = {
        .name = "bgr4",
        .nb_channels = 1,
        .color_type = FF_COLOR_RGB,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 4,
        .x_chroma_shift = 0, .y_chroma_shift = 0,
    },
    [PIX_FMT_BGR4_BYTE] = {
        .name = "bgr4_byte",
        .nb_channels = 1,
        .color_type = FF_COLOR_RGB,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 8,
        .x_chroma_shift = 0, .y_chroma_shift = 0,
    },
    [PIX_FMT_NV12] = {
        .name = "nv12",
        .nb_channels = 2,
        .color_type = FF_COLOR_YUV,
        .pixel_type = FF_PIXEL_PLANAR,
        .depth = 8,
        .x_chroma_shift = 1, .y_chroma_shift = 1,
    },
    [PIX_FMT_NV21] = {
        .name = "nv12",
        .nb_channels = 2,
        .color_type = FF_COLOR_YUV,
        .pixel_type = FF_PIXEL_PLANAR,
        .depth = 8,
        .x_chroma_shift = 1, .y_chroma_shift = 1,
    },

    [PIX_FMT_BGR32_1] = {
        .name = "bgr32_1",
        .nb_channels = 4, .is_alpha = 1,
        .color_type = FF_COLOR_RGB,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 8,
        .x_chroma_shift = 0, .y_chroma_shift = 0,
    },
    [PIX_FMT_RGB32_1] = {
        .name = "rgb32_1",
        .nb_channels = 4, .is_alpha = 1,
        .color_type = FF_COLOR_RGB,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 8,
        .x_chroma_shift = 0, .y_chroma_shift = 0,
    },
359 360 361 362
};

void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
{
363 364
    *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
    *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
365 366 367 368 369 370 371 372 373 374
}

const char *avcodec_get_pix_fmt_name(int pix_fmt)
{
    if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
        return "???";
    else
        return pix_fmt_info[pix_fmt].name;
}

375 376
enum PixelFormat avcodec_get_pix_fmt(const char* name)
{
377 378
    int i;

379 380
    for (i=0; i < PIX_FMT_NB; i++)
         if (!strcmp(pix_fmt_info[i].name, name))
381
             break;
382 383 384
    return i;
}

385
/* Picture field are filled with 'ptr' addresses. Also return size */
386
int avpicture_fill(AVPicture *picture, uint8_t *ptr,
387
                   int pix_fmt, int width, int height)
388
{
389
    int size, w2, h2, size2;
390
    const PixFmtInfo *pinfo;
391

392 393 394
    if(avcodec_check_dimensions(NULL, width, height))
        goto fail;

395
    pinfo = &pix_fmt_info[pix_fmt];
396 397 398
    size = width * height;
    switch(pix_fmt) {
    case PIX_FMT_YUV420P:
399 400 401 402
    case PIX_FMT_YUV422P:
    case PIX_FMT_YUV444P:
    case PIX_FMT_YUV410P:
    case PIX_FMT_YUV411P:
403 404 405
    case PIX_FMT_YUVJ420P:
    case PIX_FMT_YUVJ422P:
    case PIX_FMT_YUVJ444P:
406 407 408
        w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
        h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
        size2 = w2 * h2;
409 410
        picture->data[0] = ptr;
        picture->data[1] = picture->data[0] + size;
411
        picture->data[2] = picture->data[1] + size2;
412
        picture->linesize[0] = width;
413 414 415
        picture->linesize[1] = w2;
        picture->linesize[2] = w2;
        return size + 2 * size2;
416 417 418 419 420 421 422 423 424 425 426 427
    case PIX_FMT_NV12:
    case PIX_FMT_NV21:
        w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
        h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
        size2 = w2 * h2 * 2;
        picture->data[0] = ptr;
        picture->data[1] = picture->data[0] + size;
        picture->data[2] = NULL;
        picture->linesize[0] = width;
        picture->linesize[1] = w2;
        picture->linesize[2] = 0;
        return size + 2 * size2;
428 429 430 431 432 433 434
    case PIX_FMT_RGB24:
    case PIX_FMT_BGR24:
        picture->data[0] = ptr;
        picture->data[1] = NULL;
        picture->data[2] = NULL;
        picture->linesize[0] = width * 3;
        return size * 3;
435
    case PIX_FMT_RGB32:
436 437 438
    case PIX_FMT_BGR32:
    case PIX_FMT_RGB32_1:
    case PIX_FMT_BGR32_1:
439 440 441 442 443
        picture->data[0] = ptr;
        picture->data[1] = NULL;
        picture->data[2] = NULL;
        picture->linesize[0] = width * 4;
        return size * 4;
444 445
    case PIX_FMT_GRAY16BE:
    case PIX_FMT_GRAY16LE:
446 447
    case PIX_FMT_BGR555:
    case PIX_FMT_BGR565:
448 449
    case PIX_FMT_RGB555:
    case PIX_FMT_RGB565:
450
    case PIX_FMT_YUYV422:
451 452 453 454 455
        picture->data[0] = ptr;
        picture->data[1] = NULL;
        picture->data[2] = NULL;
        picture->linesize[0] = width * 2;
        return size * 2;
456 457 458 459 460 461
    case PIX_FMT_UYVY422:
        picture->data[0] = ptr;
        picture->data[1] = NULL;
        picture->data[2] = NULL;
        picture->linesize[0] = width * 2;
        return size * 2;
462
    case PIX_FMT_UYYVYY411:
463 464 465 466 467
        picture->data[0] = ptr;
        picture->data[1] = NULL;
        picture->data[2] = NULL;
        picture->linesize[0] = width + width/2;
        return size + size/2;
468 469 470 471
    case PIX_FMT_RGB8:
    case PIX_FMT_BGR8:
    case PIX_FMT_RGB4_BYTE:
    case PIX_FMT_BGR4_BYTE:
472 473 474 475 476 477
    case PIX_FMT_GRAY8:
        picture->data[0] = ptr;
        picture->data[1] = NULL;
        picture->data[2] = NULL;
        picture->linesize[0] = width;
        return size;
478 479 480 481 482 483 484
    case PIX_FMT_RGB4:
    case PIX_FMT_BGR4:
        picture->data[0] = ptr;
        picture->data[1] = NULL;
        picture->data[2] = NULL;
        picture->linesize[0] = width / 2;
        return size / 2;
485 486 487 488 489 490 491
    case PIX_FMT_MONOWHITE:
    case PIX_FMT_MONOBLACK:
        picture->data[0] = ptr;
        picture->data[1] = NULL;
        picture->data[2] = NULL;
        picture->linesize[0] = (width + 7) >> 3;
        return picture->linesize[0] * height;
492 493 494 495 496 497 498 499
    case PIX_FMT_PAL8:
        size2 = (size + 3) & ~3;
        picture->data[0] = ptr;
        picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
        picture->data[2] = NULL;
        picture->linesize[0] = width;
        picture->linesize[1] = 4;
        return size2 + 256 * 4;
500
    default:
501
fail:
502 503 504
        picture->data[0] = NULL;
        picture->data[1] = NULL;
        picture->data[2] = NULL;
505
        picture->data[3] = NULL;
506 507 508 509
        return -1;
    }
}

510
int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
511 512
                     unsigned char *dest, int dest_size)
{
513
    const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
514
    int i, j, w, h, data_planes;
515
    const unsigned char* s;
516 517
    int size = avpicture_get_size(pix_fmt, width, height);

518
    if (size > dest_size || size < 0)
519 520
        return -1;

521
    if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
522
        if (pix_fmt == PIX_FMT_YUYV422 ||
523
            pix_fmt == PIX_FMT_UYVY422 ||
524
            pix_fmt == PIX_FMT_BGR565 ||
525
            pix_fmt == PIX_FMT_BGR555 ||
526 527 528
            pix_fmt == PIX_FMT_RGB565 ||
            pix_fmt == PIX_FMT_RGB555)
            w = width * 2;
529
        else if (pix_fmt == PIX_FMT_UYYVYY411)
530 531 532 533 534 535 536 537
          w = width + width/2;
        else if (pix_fmt == PIX_FMT_PAL8)
          w = width;
        else
          w = width * (pf->depth * pf->nb_channels / 8);

        data_planes = 1;
        h = height;
538 539
    } else {
        data_planes = pf->nb_channels;
540 541
        w = (width*pf->depth + 7)/8;
        h = height;
542
    }
543

544 545
    for (i=0; i<data_planes; i++) {
         if (i == 1) {
546 547 548
             w = width >> pf->x_chroma_shift;
             h = height >> pf->y_chroma_shift;
         }
549
         s = src->data[i];
550 551 552 553 554
         for(j=0; j<h; j++) {
             memcpy(dest, s, w);
             dest += w;
             s += src->linesize[i];
         }
555
    }
556

557
    if (pf->pixel_type == FF_PIXEL_PALETTE)
558
        memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
559

560 561 562
    return size;
}

563 564 565 566 567 568
int avpicture_get_size(int pix_fmt, int width, int height)
{
    AVPicture dummy_pict;
    return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
}

569
/**
570
 * compute the loss when converting from a pixel format to another
571 572 573 574 575 576 577 578 579 580 581 582 583
 */
int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
                             int has_alpha)
{
    const PixFmtInfo *pf, *ps;
    int loss;

    ps = &pix_fmt_info[src_pix_fmt];
    pf = &pix_fmt_info[dst_pix_fmt];

    /* compute loss */
    loss = 0;
    pf = &pix_fmt_info[dst_pix_fmt];
584 585
    if (pf->depth < ps->depth ||
        (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
586
        loss |= FF_LOSS_DEPTH;
587 588
    if (pf->x_chroma_shift > ps->x_chroma_shift ||
        pf->y_chroma_shift > ps->y_chroma_shift)
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
        loss |= FF_LOSS_RESOLUTION;
    switch(pf->color_type) {
    case FF_COLOR_RGB:
        if (ps->color_type != FF_COLOR_RGB &&
            ps->color_type != FF_COLOR_GRAY)
            loss |= FF_LOSS_COLORSPACE;
        break;
    case FF_COLOR_GRAY:
        if (ps->color_type != FF_COLOR_GRAY)
            loss |= FF_LOSS_COLORSPACE;
        break;
    case FF_COLOR_YUV:
        if (ps->color_type != FF_COLOR_YUV)
            loss |= FF_LOSS_COLORSPACE;
        break;
    case FF_COLOR_YUV_JPEG:
        if (ps->color_type != FF_COLOR_YUV_JPEG &&
606
            ps->color_type != FF_COLOR_YUV &&
607
            ps->color_type != FF_COLOR_GRAY)
608 609 610 611 612 613 614 615 616 617 618 619 620
            loss |= FF_LOSS_COLORSPACE;
        break;
    default:
        /* fail safe test */
        if (ps->color_type != pf->color_type)
            loss |= FF_LOSS_COLORSPACE;
        break;
    }
    if (pf->color_type == FF_COLOR_GRAY &&
        ps->color_type != FF_COLOR_GRAY)
        loss |= FF_LOSS_CHROMA;
    if (!pf->is_alpha && (ps->is_alpha && has_alpha))
        loss |= FF_LOSS_ALPHA;
621
    if (pf->pixel_type == FF_PIXEL_PALETTE &&
622
        (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
623 624 625 626 627 628 629 630 631 632
        loss |= FF_LOSS_COLORQUANT;
    return loss;
}

static int avg_bits_per_pixel(int pix_fmt)
{
    int bits;
    const PixFmtInfo *pf;

    pf = &pix_fmt_info[pix_fmt];
633 634
    switch(pf->pixel_type) {
    case FF_PIXEL_PACKED:
635
        switch(pix_fmt) {
636
        case PIX_FMT_YUYV422:
637
        case PIX_FMT_UYVY422:
638 639
        case PIX_FMT_RGB565:
        case PIX_FMT_RGB555:
640 641
        case PIX_FMT_BGR565:
        case PIX_FMT_BGR555:
642 643
            bits = 16;
            break;
644
        case PIX_FMT_UYYVYY411:
645 646
            bits = 12;
            break;
647
        default:
648
            bits = pf->depth * pf->nb_channels;
649 650
            break;
        }
651 652 653 654 655
        break;
    case FF_PIXEL_PLANAR:
        if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
            bits = pf->depth * pf->nb_channels;
        } else {
656
            bits = pf->depth + ((2 * pf->depth) >>
657 658 659 660 661 662 663 664 665
                                (pf->x_chroma_shift + pf->y_chroma_shift));
        }
        break;
    case FF_PIXEL_PALETTE:
        bits = 8;
        break;
    default:
        bits = -1;
        break;
666 667 668 669
    }
    return bits;
}

670
static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
                                      int src_pix_fmt,
                                      int has_alpha,
                                      int loss_mask)
{
    int dist, i, loss, min_dist, dst_pix_fmt;

    /* find exact color match with smallest size */
    dst_pix_fmt = -1;
    min_dist = 0x7fffffff;
    for(i = 0;i < PIX_FMT_NB; i++) {
        if (pix_fmt_mask & (1 << i)) {
            loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
            if (loss == 0) {
                dist = avg_bits_per_pixel(i);
                if (dist < min_dist) {
                    min_dist = dist;
                    dst_pix_fmt = i;
                }
            }
        }
    }
    return dst_pix_fmt;
}

695 696
/**
 * find best pixel format to convert to. Return -1 if none found
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
 */
int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
                              int has_alpha, int *loss_ptr)
{
    int dst_pix_fmt, loss_mask, i;
    static const int loss_mask_order[] = {
        ~0, /* no loss first */
        ~FF_LOSS_ALPHA,
        ~FF_LOSS_RESOLUTION,
        ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
        ~FF_LOSS_COLORQUANT,
        ~FF_LOSS_DEPTH,
        0,
    };

    /* try with successive loss */
    i = 0;
    for(;;) {
        loss_mask = loss_mask_order[i++];
716
        dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
717 718 719 720 721 722 723 724 725 726 727 728 729
                                                 has_alpha, loss_mask);
        if (dst_pix_fmt >= 0)
            goto found;
        if (loss_mask == 0)
            break;
    }
    return -1;
 found:
    if (loss_ptr)
        *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
    return dst_pix_fmt;
}

730
void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
731 732
                           const uint8_t *src, int src_wrap,
                           int width, int height)
733
{
734
    if((!dst) || (!src))
Michael Niedermayer's avatar
Michael Niedermayer committed
735
        return;
736 737 738 739 740 741 742
    for(;height > 0; height--) {
        memcpy(dst, src, width);
        dst += dst_wrap;
        src += src_wrap;
    }
}

743 744 745
/**
 * Copy image 'src' to 'dst'.
 */
746
void img_copy(AVPicture *dst, const AVPicture *src,
747 748 749
              int pix_fmt, int width, int height)
{
    int bwidth, bits, i;
750
    const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
751

752 753 754 755
    pf = &pix_fmt_info[pix_fmt];
    switch(pf->pixel_type) {
    case FF_PIXEL_PACKED:
        switch(pix_fmt) {
756
        case PIX_FMT_YUYV422:
757
        case PIX_FMT_UYVY422:
758 759
        case PIX_FMT_RGB565:
        case PIX_FMT_RGB555:
760 761
        case PIX_FMT_BGR565:
        case PIX_FMT_BGR555:
762 763
            bits = 16;
            break;
764
        case PIX_FMT_UYYVYY411:
765 766
            bits = 12;
            break;
767 768 769 770 771
        default:
            bits = pf->depth * pf->nb_channels;
            break;
        }
        bwidth = (width * bits + 7) >> 3;
772
        ff_img_copy_plane(dst->data[0], dst->linesize[0],
773 774 775 776 777 778 779 780 781 782 783 784 785
                       src->data[0], src->linesize[0],
                       bwidth, height);
        break;
    case FF_PIXEL_PLANAR:
        for(i = 0; i < pf->nb_channels; i++) {
            int w, h;
            w = width;
            h = height;
            if (i == 1 || i == 2) {
                w >>= pf->x_chroma_shift;
                h >>= pf->y_chroma_shift;
            }
            bwidth = (w * pf->depth + 7) >> 3;
786
            ff_img_copy_plane(dst->data[i], dst->linesize[i],
787 788 789 790 791
                           src->data[i], src->linesize[i],
                           bwidth, h);
        }
        break;
    case FF_PIXEL_PALETTE:
792
        ff_img_copy_plane(dst->data[0], dst->linesize[0],
793 794 795
                       src->data[0], src->linesize[0],
                       width, height);
        /* copy the palette */
796
        ff_img_copy_plane(dst->data[1], dst->linesize[1],
797 798 799 800 801
                       src->data[1], src->linesize[1],
                       4, 256);
        break;
    }
}
802

Fabrice Bellard's avatar
Fabrice Bellard committed
803 804
/* XXX: totally non optimized */

805
static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
806
                              int width, int height)
Fabrice Bellard's avatar
Fabrice Bellard committed
807
{
808 809
    const uint8_t *p, *p1;
    uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
810
    int w;
811

812 813
    p1 = src->data[0];
    lum1 = dst->data[0];
814 815
    cb1 = dst->data[1];
    cr1 = dst->data[2];
816

817
    for(;height >= 1; height -= 2) {
818 819 820 821
        p = p1;
        lum = lum1;
        cb = cb1;
        cr = cr1;
822
        for(w = width; w >= 2; w -= 2) {
823 824 825 826
            lum[0] = p[0];
            cb[0] = p[1];
            lum[1] = p[2];
            cr[0] = p[3];
Fabrice Bellard's avatar
Fabrice Bellard committed
827 828 829 830 831
            p += 4;
            lum += 2;
            cb++;
            cr++;
        }
832
        if (w) {
833
            lum[0] = p[0];
834 835 836 837
            cb[0] = p[1];
            cr[0] = p[3];
            cb++;
            cr++;
Fabrice Bellard's avatar
Fabrice Bellard committed
838
        }
839 840
        p1 += src->linesize[0];
        lum1 += dst->linesize[0];
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
        if (height>1) {
            p = p1;
            lum = lum1;
            for(w = width; w >= 2; w -= 2) {
                lum[0] = p[0];
                lum[1] = p[2];
                p += 4;
                lum += 2;
            }
            if (w) {
                lum[0] = p[0];
            }
            p1 += src->linesize[0];
            lum1 += dst->linesize[0];
        }
856 857 858 859 860
        cb1 += dst->linesize[1];
        cr1 += dst->linesize[2];
    }
}

861 862 863 864 865 866
static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
                              int width, int height)
{
    const uint8_t *p, *p1;
    uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
    int w;
867

868
    p1 = src->data[0];
869

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
    lum1 = dst->data[0];
    cb1 = dst->data[1];
    cr1 = dst->data[2];

    for(;height >= 1; height -= 2) {
        p = p1;
        lum = lum1;
        cb = cb1;
        cr = cr1;
        for(w = width; w >= 2; w -= 2) {
            lum[0] = p[1];
            cb[0] = p[0];
            lum[1] = p[3];
            cr[0] = p[2];
            p += 4;
            lum += 2;
            cb++;
            cr++;
        }
        if (w) {
            lum[0] = p[1];
            cb[0] = p[0];
            cr[0] = p[2];
            cb++;
            cr++;
        }
        p1 += src->linesize[0];
        lum1 += dst->linesize[0];
        if (height>1) {
            p = p1;
            lum = lum1;
            for(w = width; w >= 2; w -= 2) {
                lum[0] = p[1];
                lum[1] = p[3];
                p += 4;
                lum += 2;
            }
            if (w) {
                lum[0] = p[1];
            }
            p1 += src->linesize[0];
            lum1 += dst->linesize[0];
        }
        cb1 += dst->linesize[1];
        cr1 += dst->linesize[2];
    }
}


static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
                              int width, int height)
{
    const uint8_t *p, *p1;
    uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
    int w;

    p1 = src->data[0];
    lum1 = dst->data[0];
    cb1 = dst->data[1];
    cr1 = dst->data[2];
    for(;height > 0; height--) {
        p = p1;
        lum = lum1;
        cb = cb1;
        cr = cr1;
        for(w = width; w >= 2; w -= 2) {
            lum[0] = p[1];
            cb[0] = p[0];
            lum[1] = p[3];
            cr[0] = p[2];
            p += 4;
            lum += 2;
            cb++;
            cr++;
        }
        p1 += src->linesize[0];
        lum1 += dst->linesize[0];
        cb1 += dst->linesize[1];
        cr1 += dst->linesize[2];
    }
}


953
static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
954 955 956 957 958 959 960 961
                              int width, int height)
{
    const uint8_t *p, *p1;
    uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
    int w;

    p1 = src->data[0];
    lum1 = dst->data[0];
962 963 964
    cb1 = dst->data[1];
    cr1 = dst->data[2];
    for(;height > 0; height--) {
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
        p = p1;
        lum = lum1;
        cb = cb1;
        cr = cr1;
        for(w = width; w >= 2; w -= 2) {
            lum[0] = p[0];
            cb[0] = p[1];
            lum[1] = p[2];
            cr[0] = p[3];
            p += 4;
            lum += 2;
            cb++;
            cr++;
        }
        p1 += src->linesize[0];
        lum1 += dst->linesize[0];
        cb1 += dst->linesize[1];
        cr1 += dst->linesize[2];
Fabrice Bellard's avatar
Fabrice Bellard committed
983 984 985
    }
}

986
static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
987 988 989 990 991 992 993 994
                              int width, int height)
{
    uint8_t *p, *p1;
    const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
    int w;

    p1 = dst->data[0];
    lum1 = src->data[0];
995 996 997
    cb1 = src->data[1];
    cr1 = src->data[2];
    for(;height > 0; height--) {
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
        p = p1;
        lum = lum1;
        cb = cb1;
        cr = cr1;
        for(w = width; w >= 2; w -= 2) {
            p[0] = lum[0];
            p[1] = cb[0];
            p[2] = lum[1];
            p[3] = cr[0];
            p += 4;
            lum += 2;
            cb++;
            cr++;
        }
1012 1013 1014 1015
        p1 += dst->linesize[0];
        lum1 += src->linesize[0];
        cb1 += src->linesize[1];
        cr1 += src->linesize[2];
1016 1017 1018
    }
}

1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
                              int width, int height)
{
    uint8_t *p, *p1;
    const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
    int w;

    p1 = dst->data[0];
    lum1 = src->data[0];
    cb1 = src->data[1];
    cr1 = src->data[2];
    for(;height > 0; height--) {
        p = p1;
        lum = lum1;
        cb = cb1;
        cr = cr1;
        for(w = width; w >= 2; w -= 2) {
            p[1] = lum[0];
            p[0] = cb[0];
            p[3] = lum[1];
            p[2] = cr[0];
            p += 4;
            lum += 2;
            cb++;
            cr++;
        }
        p1 += dst->linesize[0];
        lum1 += src->linesize[0];
        cb1 += src->linesize[1];
        cr1 += src->linesize[2];
    }
}

1052
static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
                              int width, int height)
{
    const uint8_t *p, *p1;
    uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
    int w;

    p1 = src->data[0];
    lum1 = dst->data[0];
    cb1 = dst->data[1];
    cr1 = dst->data[2];
    for(;height > 0; height--) {
        p = p1;
        lum = lum1;
        cb = cb1;
        cr = cr1;
        for(w = width; w >= 4; w -= 4) {
            cb[0] = p[0];
1070
            lum[0] = p[1];
1071 1072
            lum[1] = p[2];
            cr[0] = p[3];
1073 1074
            lum[2] = p[4];
            lum[3] = p[5];
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
            p += 6;
            lum += 4;
            cb++;
            cr++;
        }
        p1 += src->linesize[0];
        lum1 += dst->linesize[0];
        cb1 += dst->linesize[1];
        cr1 += dst->linesize[2];
    }
}
1086 1087


1088
static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1089 1090 1091 1092 1093 1094 1095
                              int width, int height)
{
    int w, h;
    uint8_t *line1, *line2, *linesrc = dst->data[0];
    uint8_t *lum1, *lum2, *lumsrc = src->data[0];
    uint8_t *cb1, *cb2 = src->data[1];
    uint8_t *cr1, *cr2 = src->data[2];
1096

1097 1098 1099
    for(h = height / 2; h--;) {
        line1 = linesrc;
        line2 = linesrc + dst->linesize[0];
1100

1101 1102
        lum1 = lumsrc;
        lum2 = lumsrc + src->linesize[0];
1103

1104 1105
        cb1 = cb2;
        cr1 = cr2;
1106

1107
        for(w = width / 2; w--;) {
1108 1109 1110
                *line1++ = *lum1++; *line2++ = *lum2++;
                *line1++ =          *line2++ = *cb1++;
                *line1++ = *lum1++; *line2++ = *lum2++;
1111 1112
                *line1++ =          *line2++ = *cr1++;
        }
1113

1114 1115 1116 1117 1118 1119 1120
        linesrc += dst->linesize[0] * 2;
        lumsrc += src->linesize[0] * 2;
        cb2 += src->linesize[1];
        cr2 += src->linesize[2];
    }
}

1121 1122 1123 1124 1125 1126 1127 1128
static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
                              int width, int height)
{
    int w, h;
    uint8_t *line1, *line2, *linesrc = dst->data[0];
    uint8_t *lum1, *lum2, *lumsrc = src->data[0];
    uint8_t *cb1, *cb2 = src->data[1];
    uint8_t *cr1, *cr2 = src->data[2];
1129

1130 1131 1132
    for(h = height / 2; h--;) {
        line1 = linesrc;
        line2 = linesrc + dst->linesize[0];
1133

1134 1135
        lum1 = lumsrc;
        lum2 = lumsrc + src->linesize[0];
1136

1137 1138
        cb1 = cb2;
        cr1 = cr2;
1139

1140
        for(w = width / 2; w--;) {
1141 1142
                *line1++ =          *line2++ = *cb1++;
                *line1++ = *lum1++; *line2++ = *lum2++;
1143
                *line1++ =          *line2++ = *cr1++;
1144
                *line1++ = *lum1++; *line2++ = *lum2++;
1145
        }
1146

1147 1148 1149 1150 1151 1152 1153
        linesrc += dst->linesize[0] * 2;
        lumsrc += src->linesize[0] * 2;
        cb2 += src->linesize[1];
        cr2 += src->linesize[2];
    }
}

1154
#define SCALEBITS 10
Fabrice Bellard's avatar
Fabrice Bellard committed
1155
#define ONE_HALF  (1 << (SCALEBITS - 1))
1156
#define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
Fabrice Bellard's avatar
Fabrice Bellard committed
1157

1158 1159 1160 1161 1162 1163 1164 1165 1166
#define YUV_TO_RGB1_CCIR(cb1, cr1)\
{\
    cb = (cb1) - 128;\
    cr = (cr1) - 128;\
    r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
    g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
            ONE_HALF;\
    b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
}
1167

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
#define YUV_TO_RGB2_CCIR(r, g, b, y1)\
{\
    y = ((y1) - 16) * FIX(255.0/219.0);\
    r = cm[(y + r_add) >> SCALEBITS];\
    g = cm[(y + g_add) >> SCALEBITS];\
    b = cm[(y + b_add) >> SCALEBITS];\
}

#define YUV_TO_RGB1(cb1, cr1)\
{\
    cb = (cb1) - 128;\
    cr = (cr1) - 128;\
    r_add = FIX(1.40200) * cr + ONE_HALF;\
    g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
    b_add = FIX(1.77200) * cb + ONE_HALF;\
}
1184 1185 1186

#define YUV_TO_RGB2(r, g, b, y1)\
{\
1187 1188 1189 1190
    y = (y1) << SCALEBITS;\
    r = cm[(y + r_add) >> SCALEBITS];\
    g = cm[(y + g_add) >> SCALEBITS];\
    b = cm[(y + b_add) >> SCALEBITS];\
1191 1192
}

1193
#define Y_CCIR_TO_JPEG(y)\
1194 1195
 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]

1196
#define Y_JPEG_TO_CCIR(y)\
1197 1198
 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)

1199 1200 1201 1202
#define C_CCIR_TO_JPEG(y)\
 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]

/* NOTE: the clamp is really necessary! */
1203 1204 1205
static inline int C_JPEG_TO_CCIR(int y) {
    y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
    if (y < 16)
1206
        y = 16;
1207 1208 1209
    return y;
}

1210

1211 1212 1213 1214
#define RGB_TO_Y(r, g, b) \
((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
  FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)

1215
#define RGB_TO_U(r1, g1, b1, shift)\
1216
(((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +         \
1217
     FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1218

1219
#define RGB_TO_V(r1, g1, b1, shift)\
1220
(((FIX(0.50000) * r1 - FIX(0.41869) * g1 -           \
1221
   FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1222 1223 1224 1225 1226

#define RGB_TO_Y_CCIR(r, g, b) \
((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
  FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)

1227
#define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1228
(((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 +         \
1229
     FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1230

1231
#define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1232
(((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 -           \
1233
   FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1234

1235 1236 1237 1238 1239 1240 1241
static uint8_t y_ccir_to_jpeg[256];
static uint8_t y_jpeg_to_ccir[256];
static uint8_t c_ccir_to_jpeg[256];
static uint8_t c_jpeg_to_ccir[256];

/* init various conversion tables */
static void img_convert_init(void)
1242
{
1243
    int i;
1244
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1245

1246 1247 1248 1249 1250
    for(i = 0;i < 256; i++) {
        y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
        y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
        c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
        c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1251 1252 1253
    }
}

1254
/* apply to each pixel the given table */
1255
static void img_apply_table(uint8_t *dst, int dst_wrap,
1256 1257
                            const uint8_t *src, int src_wrap,
                            int width, int height, const uint8_t *table1)
1258 1259 1260 1261
{
    int n;
    const uint8_t *s;
    uint8_t *d;
1262
    const uint8_t *table;
1263

1264
    table = table1;
1265 1266 1267 1268 1269
    for(;height > 0; height--) {
        s = src;
        d = dst;
        n = width;
        while (n >= 4) {
1270 1271 1272 1273
            d[0] = table[s[0]];
            d[1] = table[s[1]];
            d[2] = table[s[2]];
            d[3] = table[s[3]];
1274 1275 1276 1277 1278
            d += 4;
            s += 4;
            n -= 4;
        }
        while (n > 0) {
1279
            d[0] = table[s[0]];
1280 1281 1282 1283 1284 1285 1286 1287 1288
            d++;
            s++;
            n--;
        }
        dst += dst_wrap;
        src += src_wrap;
    }
}

1289
/* XXX: use generic filter ? */
1290 1291 1292
/* XXX: in most cases, the sampling position is incorrect */

/* 4x1 -> 1x1 */
1293
static void shrink41(uint8_t *dst, int dst_wrap,
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
                     const uint8_t *src, int src_wrap,
                     int width, int height)
{
    int w;
    const uint8_t *s;
    uint8_t *d;

    for(;height > 0; height--) {
        s = src;
        d = dst;
        for(w = width;w > 0; w--) {
            d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
            s += 4;
            d++;
        }
        src += src_wrap;
        dst += dst_wrap;
    }
}

/* 2x1 -> 1x1 */
1315
static void shrink21(uint8_t *dst, int dst_wrap,
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
                     const uint8_t *src, int src_wrap,
                     int width, int height)
{
    int w;
    const uint8_t *s;
    uint8_t *d;

    for(;height > 0; height--) {
        s = src;
        d = dst;
        for(w = width;w > 0; w--) {
            d[0] = (s[0] + s[1]) >> 1;
            s += 2;
            d++;
        }
        src += src_wrap;
        dst += dst_wrap;
    }
}

1336
/* 1x2 -> 1x1 */
1337
static void shrink12(uint8_t *dst, int dst_wrap,
1338 1339
                     const uint8_t *src, int src_wrap,
                     int width, int height)
1340 1341
{
    int w;
1342 1343
    uint8_t *d;
    const uint8_t *s1, *s2;
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369

    for(;height > 0; height--) {
        s1 = src;
        s2 = s1 + src_wrap;
        d = dst;
        for(w = width;w >= 4; w-=4) {
            d[0] = (s1[0] + s2[0]) >> 1;
            d[1] = (s1[1] + s2[1]) >> 1;
            d[2] = (s1[2] + s2[2]) >> 1;
            d[3] = (s1[3] + s2[3]) >> 1;
            s1 += 4;
            s2 += 4;
            d += 4;
        }
        for(;w > 0; w--) {
            d[0] = (s1[0] + s2[0]) >> 1;
            s1++;
            s2++;
            d++;
        }
        src += 2 * src_wrap;
        dst += dst_wrap;
    }
}

/* 2x2 -> 1x1 */
1370
void ff_shrink22(uint8_t *dst, int dst_wrap,
1371
                     const uint8_t *src, int src_wrap,
1372 1373 1374
                     int width, int height)
{
    int w;
1375 1376
    const uint8_t *s1, *s2;
    uint8_t *d;
1377 1378 1379 1380 1381 1382

    for(;height > 0; height--) {
        s1 = src;
        s2 = s1 + src_wrap;
        d = dst;
        for(w = width;w >= 4; w-=4) {
1383 1384 1385 1386
            d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
            d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
            d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
            d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1387 1388 1389 1390 1391
            s1 += 8;
            s2 += 8;
            d += 4;
        }
        for(;w > 0; w--) {
1392
            d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1393 1394 1395 1396 1397 1398 1399 1400 1401
            s1 += 2;
            s2 += 2;
            d++;
        }
        src += 2 * src_wrap;
        dst += dst_wrap;
    }
}

1402
/* 4x4 -> 1x1 */
1403
void ff_shrink44(uint8_t *dst, int dst_wrap,
1404
                     const uint8_t *src, int src_wrap,
1405 1406 1407
                     int width, int height)
{
    int w;
1408 1409
    const uint8_t *s1, *s2, *s3, *s4;
    uint8_t *d;
1410 1411 1412

    for(;height > 0; height--) {
        s1 = src;
1413 1414 1415
        s2 = s1 + src_wrap;
        s3 = s2 + src_wrap;
        s4 = s3 + src_wrap;
1416
        d = dst;
1417 1418 1419 1420 1421 1422 1423 1424 1425
        for(w = width;w > 0; w--) {
            d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
                    s2[0] + s2[1] + s2[2] + s2[3] +
                    s3[0] + s3[1] + s3[2] + s3[3] +
                    s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
            s1 += 4;
            s2 += 4;
            s3 += 4;
            s4 += 4;
1426 1427
            d++;
        }
1428 1429 1430 1431 1432
        src += 4 * src_wrap;
        dst += dst_wrap;
    }
}

1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
/* 8x8 -> 1x1 */
void ff_shrink88(uint8_t *dst, int dst_wrap,
                     const uint8_t *src, int src_wrap,
                     int width, int height)
{
    int w, i;

    for(;height > 0; height--) {
        for(w = width;w > 0; w--) {
            int tmp=0;
            for(i=0; i<8; i++){
                tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
                src += src_wrap;
            }
            *(dst++) = (tmp + 32)>>6;
            src += 8 - 8*src_wrap;
        }
        src += 8*src_wrap - 8*width;
        dst += dst_wrap - width;
    }
}

1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
static void grow21_line(uint8_t *dst, const uint8_t *src,
                        int width)
{
    int w;
    const uint8_t *s1;
    uint8_t *d;

    s1 = src;
    d = dst;
    for(w = width;w >= 4; w-=4) {
        d[1] = d[0] = s1[0];
        d[3] = d[2] = s1[1];
        s1 += 2;
        d += 4;
    }
    for(;w >= 2; w -= 2) {
        d[1] = d[0] = s1[0];
        s1 ++;
        d += 2;
    }
    /* only needed if width is not a multiple of two */
    /* XXX: veryfy that */
    if (w) {
        d[0] = s1[0];
    }
}

static void grow41_line(uint8_t *dst, const uint8_t *src,
                        int width)
{
    int w, v;
    const uint8_t *s1;
    uint8_t *d;

    s1 = src;
    d = dst;
    for(w = width;w >= 4; w-=4) {
        v = s1[0];
        d[0] = v;
        d[1] = v;
        d[2] = v;
        d[3] = v;
        s1 ++;
        d += 4;
    }
}

/* 1x1 -> 2x1 */
static void grow21(uint8_t *dst, int dst_wrap,
                   const uint8_t *src, int src_wrap,
                   int width, int height)
{
    for(;height > 0; height--) {
        grow21_line(dst, src, width);
        src += src_wrap;
        dst += dst_wrap;
    }
}

/* 1x1 -> 2x2 */
static void grow22(uint8_t *dst, int dst_wrap,
                   const uint8_t *src, int src_wrap,
                   int width, int height)
{
    for(;height > 0; height--) {
        grow21_line(dst, src, width);
1521 1522 1523 1524 1525 1526
        if (height%2)
            src += src_wrap;
        dst += dst_wrap;
    }
}

1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
/* 1x1 -> 4x1 */
static void grow41(uint8_t *dst, int dst_wrap,
                   const uint8_t *src, int src_wrap,
                   int width, int height)
{
    for(;height > 0; height--) {
        grow41_line(dst, src, width);
        src += src_wrap;
        dst += dst_wrap;
    }
}

/* 1x1 -> 4x4 */
static void grow44(uint8_t *dst, int dst_wrap,
                   const uint8_t *src, int src_wrap,
                   int width, int height)
{
    for(;height > 0; height--) {
        grow41_line(dst, src, width);
        if ((height & 3) == 1)
            src += src_wrap;
        dst += dst_wrap;
    }
}

1552
/* 1x2 -> 2x1 */
1553
static void conv411(uint8_t *dst, int dst_wrap,
1554
                    const uint8_t *src, int src_wrap,
1555 1556 1557
                    int width, int height)
{
    int w, c;
1558 1559
    const uint8_t *s1, *s2;
    uint8_t *d;
1560

1561 1562
    width>>=1;

1563
    for(;height > 0; height--) {
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
        s1 = src;
        s2 = src + src_wrap;
        d = dst;
        for(w = width;w > 0; w--) {
            c = (s1[0] + s2[0]) >> 1;
            d[0] = c;
            d[1] = c;
            s1++;
            s2++;
            d += 2;
        }
        src += src_wrap * 2;
        dst += dst_wrap;
    }
}

1580 1581 1582 1583 1584 1585
/* XXX: add jpeg quantize code */

#define TRANSP_INDEX (6*6*6)

/* this is maybe slow, but allows for extensions */
static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1586
{
1587
    return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1588 1589
}

1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
static void build_rgb_palette(uint8_t *palette, int has_alpha)
{
    uint32_t *pal;
    static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
    int i, r, g, b;

    pal = (uint32_t *)palette;
    i = 0;
    for(r = 0; r < 6; r++) {
        for(g = 0; g < 6; g++) {
            for(b = 0; b < 6; b++) {
1601
                pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1602 1603 1604 1605 1606 1607 1608 1609
                    (pal_value[g] << 8) | pal_value[b];
            }
        }
    }
    if (has_alpha)
        pal[i++] = 0;
    while (i < 256)
        pal[i++] = 0xff000000;
1610 1611 1612 1613
}

/* copy bit n to bits 0 ... n - 1 */
static inline unsigned int bitcopy_n(unsigned int a, int n)
1614
{
1615 1616 1617 1618 1619 1620 1621
    int mask;
    mask = (1 << n) - 1;
    return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
}

/* rgb555 handling */

1622 1623
#define RGB_NAME rgb555

1624 1625
#define RGB_IN(r, g, b, s)\
{\
1626
    unsigned int v = ((const uint16_t *)(s))[0];\
1627 1628 1629 1630 1631
    r = bitcopy_n(v >> (10 - 3), 3);\
    g = bitcopy_n(v >> (5 - 3), 3);\
    b = bitcopy_n(v << 3, 3);\
}

1632

1633
#define RGB_OUT(d, r, g, b)\
1634
{\
1635
    ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1636 1637 1638 1639
}

#define BPP 2

1640
#include "imgconvert_template.h"
1641 1642 1643

/* rgb565 handling */

1644 1645
#define RGB_NAME rgb565

1646 1647
#define RGB_IN(r, g, b, s)\
{\
1648
    unsigned int v = ((const uint16_t *)(s))[0];\
1649 1650 1651 1652 1653 1654 1655
    r = bitcopy_n(v >> (11 - 3), 3);\
    g = bitcopy_n(v >> (5 - 2), 2);\
    b = bitcopy_n(v << 3, 3);\
}

#define RGB_OUT(d, r, g, b)\
{\
1656
    ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1657 1658 1659 1660
}

#define BPP 2

1661
#include "imgconvert_template.h"
1662 1663 1664

/* bgr24 handling */

1665 1666
#define RGB_NAME bgr24

1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
#define RGB_IN(r, g, b, s)\
{\
    b = (s)[0];\
    g = (s)[1];\
    r = (s)[2];\
}

#define RGB_OUT(d, r, g, b)\
{\
    (d)[0] = b;\
    (d)[1] = g;\
    (d)[2] = r;\
}

#define BPP 3

1683
#include "imgconvert_template.h"
1684 1685 1686 1687 1688 1689 1690

#undef RGB_IN
#undef RGB_OUT
#undef BPP

/* rgb24 handling */

1691 1692 1693
#define RGB_NAME rgb24
#define FMT_RGB24

1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
#define RGB_IN(r, g, b, s)\
{\
    r = (s)[0];\
    g = (s)[1];\
    b = (s)[2];\
}

#define RGB_OUT(d, r, g, b)\
{\
    (d)[0] = r;\
    (d)[1] = g;\
    (d)[2] = b;\
}

#define BPP 3

1710
#include "imgconvert_template.h"
1711

1712
/* rgb32 handling */
1713

1714 1715
#define RGB_NAME rgb32
#define FMT_RGB32
1716

1717 1718
#define RGB_IN(r, g, b, s)\
{\
1719
    unsigned int v = ((const uint32_t *)(s))[0];\
1720 1721 1722 1723 1724
    r = (v >> 16) & 0xff;\
    g = (v >> 8) & 0xff;\
    b = v & 0xff;\
}

1725
#define RGBA_IN(r, g, b, a, s)\
1726
{\
1727 1728 1729 1730 1731
    unsigned int v = ((const uint32_t *)(s))[0];\
    a = (v >> 24) & 0xff;\
    r = (v >> 16) & 0xff;\
    g = (v >> 8) & 0xff;\
    b = v & 0xff;\
1732 1733
}

1734 1735 1736
#define RGBA_OUT(d, r, g, b, a)\
{\
    ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1737 1738
}

1739
#define BPP 4
1740

1741
#include "imgconvert_template.h"
1742

1743
static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1744
                         int width, int height, int xor_mask)
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
{
    const unsigned char *p;
    unsigned char *q;
    int v, dst_wrap, src_wrap;
    int y, w;

    p = src->data[0];
    src_wrap = src->linesize[0] - ((width + 7) >> 3);

    q = dst->data[0];
1755
    dst_wrap = dst->linesize[0] - width;
1756
    for(y=0;y<height;y++) {
1757
        w = width;
1758
        while (w >= 8) {
1759 1760 1761 1762 1763 1764 1765 1766 1767
            v = *p++ ^ xor_mask;
            q[0] = -(v >> 7);
            q[1] = -((v >> 6) & 1);
            q[2] = -((v >> 5) & 1);
            q[3] = -((v >> 4) & 1);
            q[4] = -((v >> 3) & 1);
            q[5] = -((v >> 2) & 1);
            q[6] = -((v >> 1) & 1);
            q[7] = -((v >> 0) & 1);
1768
            w -= 8;
1769
            q += 8;
1770 1771
        }
        if (w > 0) {
1772
            v = *p++ ^ xor_mask;
1773
            do {
1774 1775
                q[0] = -((v >> 7) & 1);
                q++;
1776 1777
                v <<= 1;
            } while (--w);
1778
        }
1779 1780
        p += src_wrap;
        q += dst_wrap;
1781 1782 1783
    }
}

1784
static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1785 1786
                               int width, int height)
{
1787 1788
    mono_to_gray(dst, src, width, height, 0xff);
}
1789

1790
static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1791 1792 1793 1794
                               int width, int height)
{
    mono_to_gray(dst, src, width, height, 0x00);
}
1795

1796
static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1797 1798 1799
                         int width, int height, int xor_mask)
{
    int n;
1800 1801
    const uint8_t *s;
    uint8_t *d;
1802 1803 1804 1805 1806 1807 1808
    int j, b, v, n1, src_wrap, dst_wrap, y;

    s = src->data[0];
    src_wrap = src->linesize[0] - width;

    d = dst->data[0];
    dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1809 1810

    for(y=0;y<height;y++) {
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
        n = width;
        while (n >= 8) {
            v = 0;
            for(j=0;j<8;j++) {
                b = s[0];
                s++;
                v = (v << 1) | (b >> 7);
            }
            d[0] = v ^ xor_mask;
            d++;
            n -= 8;
1822
        }
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
        if (n > 0) {
            n1 = n;
            v = 0;
            while (n > 0) {
                b = s[0];
                s++;
                v = (v << 1) | (b >> 7);
                n--;
            }
            d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
            d++;
1834
        }
1835 1836
        s += src_wrap;
        d += dst_wrap;
1837 1838 1839
    }
}

1840
static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1841 1842 1843 1844 1845
                              int width, int height)
{
    gray_to_mono(dst, src, width, height, 0xff);
}

1846
static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1847 1848 1849 1850 1851
                              int width, int height)
{
    gray_to_mono(dst, src, width, height, 0x00);
}

1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
                              int width, int height)
{
    int x, y, src_wrap, dst_wrap;
    uint8_t *s, *d;
    s = src->data[0];
    src_wrap = src->linesize[0] - width;
    d = dst->data[0];
    dst_wrap = dst->linesize[0] - width * 2;
    for(y=0; y<height; y++){
        for(x=0; x<width; x++){
            *d++ = *s;
            *d++ = *s++;
        }
        s += src_wrap;
        d += dst_wrap;
    }
}

static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
                              int width, int height)
{
    int x, y, src_wrap, dst_wrap;
    uint8_t *s, *d;
    s = src->data[0];
    src_wrap = src->linesize[0] - width * 2;
    d = dst->data[0];
    dst_wrap = dst->linesize[0] - width;
    for(y=0; y<height; y++){
        for(x=0; x<width; x++){
            *d++ = *s;
            s += 2;
        }
        s += src_wrap;
        d += dst_wrap;
    }
}

static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
                              int width, int height)
{
    gray16_to_gray(dst, src, width, height);
}

static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
                              int width, int height)
{
    gray16_to_gray(dst, src + 1, width, height);
}

static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
                              int width, int height)
{
    int x, y, src_wrap, dst_wrap;
    uint16_t *s, *d;
    s = src->data[0];
    src_wrap = (src->linesize[0] - width * 2)/2;
    d = dst->data[0];
    dst_wrap = (dst->linesize[0] - width * 2)/2;
    for(y=0; y<height; y++){
        for(x=0; x<width; x++){
            *d++ = bswap_16(*s++);
        }
        s += src_wrap;
        d += dst_wrap;
    }
}


1921
typedef struct ConvertEntry {
1922
    void (*convert)(AVPicture *dst,
1923
                    const AVPicture *src, int width, int height);
1924 1925
} ConvertEntry;

1926 1927 1928 1929
/* Add each new convertion function in this table. In order to be able
   to convert from any format to any format, the following constraints
   must be satisfied:

1930
   - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1931 1932 1933

   - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8

1934
   - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1935

1936
   - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1937 1938 1939
     PIX_FMT_RGB24.

   - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1940 1941

   The other conversion functions are just optimisations for common cases.
1942
*/
1943
static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1944
    [PIX_FMT_YUV420P] = {
1945
        [PIX_FMT_YUYV422] = {
1946
            .convert = yuv420p_to_yuyv422,
1947
        },
1948
        [PIX_FMT_RGB555] = {
1949
            .convert = yuv420p_to_rgb555
1950
        },
1951
        [PIX_FMT_RGB565] = {
1952
            .convert = yuv420p_to_rgb565
1953
        },
1954
        [PIX_FMT_BGR24] = {
1955
            .convert = yuv420p_to_bgr24
1956
        },
1957
        [PIX_FMT_RGB24] = {
1958
            .convert = yuv420p_to_rgb24
1959
        },
1960
        [PIX_FMT_RGB32] = {
1961
            .convert = yuv420p_to_rgb32
1962
        },
1963
        [PIX_FMT_UYVY422] = {
1964 1965
            .convert = yuv420p_to_uyvy422,
        },
1966
    },
1967
    [PIX_FMT_YUV422P] = {
1968
        [PIX_FMT_YUYV422] = {
1969
            .convert = yuv422p_to_yuyv422,
1970
        },
1971
        [PIX_FMT_UYVY422] = {
1972 1973
            .convert = yuv422p_to_uyvy422,
        },
1974
    },
1975 1976
    [PIX_FMT_YUV444P] = {
        [PIX_FMT_RGB24] = {
1977 1978 1979 1980
            .convert = yuv444p_to_rgb24
        },
    },
    [PIX_FMT_YUVJ420P] = {
1981
        [PIX_FMT_RGB555] = {
1982
            .convert = yuvj420p_to_rgb555
1983
        },
1984
        [PIX_FMT_RGB565] = {
1985
            .convert = yuvj420p_to_rgb565
1986
        },
1987
        [PIX_FMT_BGR24] = {
1988
            .convert = yuvj420p_to_bgr24
1989
        },
1990
        [PIX_FMT_RGB24] = {
1991
            .convert = yuvj420p_to_rgb24
1992
        },
1993
        [PIX_FMT_RGB32] = {
1994
            .convert = yuvj420p_to_rgb32
1995 1996
        },
    },
1997 1998
    [PIX_FMT_YUVJ444P] = {
        [PIX_FMT_RGB24] = {
1999
            .convert = yuvj444p_to_rgb24
2000 2001
        },
    },
2002
    [PIX_FMT_YUYV422] = {
2003
        [PIX_FMT_YUV420P] = {
2004
            .convert = yuyv422_to_yuv420p,
2005
        },
2006
        [PIX_FMT_YUV422P] = {
2007
            .convert = yuyv422_to_yuv422p,
2008
        },
2009
    },
2010 2011
    [PIX_FMT_UYVY422] = {
        [PIX_FMT_YUV420P] = {
2012 2013
            .convert = uyvy422_to_yuv420p,
        },
2014
        [PIX_FMT_YUV422P] = {
2015 2016 2017
            .convert = uyvy422_to_yuv422p,
        },
    },
2018
    [PIX_FMT_RGB24] = {
2019
        [PIX_FMT_YUV420P] = {
2020
            .convert = rgb24_to_yuv420p
2021
        },
2022
        [PIX_FMT_RGB565] = {
2023
            .convert = rgb24_to_rgb565
2024
        },
2025
        [PIX_FMT_RGB555] = {
2026
            .convert = rgb24_to_rgb555
2027
        },
2028
        [PIX_FMT_RGB32] = {
2029
            .convert = rgb24_to_rgb32
2030
        },
2031
        [PIX_FMT_BGR24] = {
2032 2033
            .convert = rgb24_to_bgr24
        },
2034
        [PIX_FMT_GRAY8] = {
2035
            .convert = rgb24_to_gray
2036
        },
2037
        [PIX_FMT_PAL8] = {
2038 2039
            .convert = rgb24_to_pal8
        },
2040
        [PIX_FMT_YUV444P] = {
2041 2042
            .convert = rgb24_to_yuv444p
        },
2043
        [PIX_FMT_YUVJ420P] = {
2044 2045
            .convert = rgb24_to_yuvj420p
        },
2046
        [PIX_FMT_YUVJ444P] = {
2047 2048
            .convert = rgb24_to_yuvj444p
        },
2049
    },
2050
    [PIX_FMT_RGB32] = {
2051
        [PIX_FMT_RGB24] = {
2052
            .convert = rgb32_to_rgb24
2053
        },
2054
        [PIX_FMT_BGR24] = {
2055
            .convert = rgb32_to_bgr24
2056 2057
        },
        [PIX_FMT_RGB565] = {
2058
            .convert = rgb32_to_rgb565
2059
        },
2060
        [PIX_FMT_RGB555] = {
2061
            .convert = rgb32_to_rgb555
2062
        },
2063
        [PIX_FMT_PAL8] = {
2064
            .convert = rgb32_to_pal8
2065
        },
2066
        [PIX_FMT_YUV420P] = {
2067
            .convert = rgb32_to_yuv420p
2068
        },
2069
        [PIX_FMT_GRAY8] = {
2070
            .convert = rgb32_to_gray
2071
        },
2072 2073
    },
    [PIX_FMT_BGR24] = {
2074
        [PIX_FMT_RGB32] = {
2075
            .convert = bgr24_to_rgb32
2076
        },
2077
        [PIX_FMT_RGB24] = {
2078 2079
            .convert = bgr24_to_rgb24
        },
2080
        [PIX_FMT_YUV420P] = {
2081
            .convert = bgr24_to_yuv420p
2082
        },
2083
        [PIX_FMT_GRAY8] = {
2084 2085
            .convert = bgr24_to_gray
        },
2086 2087
    },
    [PIX_FMT_RGB555] = {
2088
        [PIX_FMT_RGB24] = {
2089 2090
            .convert = rgb555_to_rgb24
        },
2091
        [PIX_FMT_RGB32] = {
2092
            .convert = rgb555_to_rgb32
2093
        },
2094
        [PIX_FMT_YUV420P] = {
2095
            .convert = rgb555_to_yuv420p
2096
        },
2097
        [PIX_FMT_GRAY8] = {
2098 2099
            .convert = rgb555_to_gray
        },
2100 2101
    },
    [PIX_FMT_RGB565] = {
2102
        [PIX_FMT_RGB32] = {
2103
            .convert = rgb565_to_rgb32
2104
        },
2105
        [PIX_FMT_RGB24] = {
2106 2107
            .convert = rgb565_to_rgb24
        },
2108
        [PIX_FMT_YUV420P] = {
2109
            .convert = rgb565_to_yuv420p
2110
        },
2111
        [PIX_FMT_GRAY8] = {
2112 2113
            .convert = rgb565_to_gray
        },
2114
    },
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
    [PIX_FMT_GRAY16BE] = {
        [PIX_FMT_GRAY8] = {
            .convert = gray16be_to_gray
        },
        [PIX_FMT_GRAY16LE] = {
            .convert = gray16_to_gray16
        },
    },
    [PIX_FMT_GRAY16LE] = {
        [PIX_FMT_GRAY8] = {
            .convert = gray16le_to_gray
        },
        [PIX_FMT_GRAY16BE] = {
            .convert = gray16_to_gray16
        },
    },
2131
    [PIX_FMT_GRAY8] = {
2132
        [PIX_FMT_RGB555] = {
2133 2134
            .convert = gray_to_rgb555
        },
2135
        [PIX_FMT_RGB565] = {
2136 2137
            .convert = gray_to_rgb565
        },
2138
        [PIX_FMT_RGB24] = {
2139
            .convert = gray_to_rgb24
2140
        },
2141
        [PIX_FMT_BGR24] = {
2142 2143
            .convert = gray_to_bgr24
        },
2144
        [PIX_FMT_RGB32] = {
2145
            .convert = gray_to_rgb32
2146
        },
2147
        [PIX_FMT_MONOWHITE] = {
2148
            .convert = gray_to_monowhite
2149
        },
2150
        [PIX_FMT_MONOBLACK] = {
2151
            .convert = gray_to_monoblack
2152
        },
2153 2154 2155 2156 2157 2158
        [PIX_FMT_GRAY16LE] = {
            .convert = gray_to_gray16
        },
        [PIX_FMT_GRAY16BE] = {
            .convert = gray_to_gray16
        },
2159 2160
    },
    [PIX_FMT_MONOWHITE] = {
2161
        [PIX_FMT_GRAY8] = {
2162
            .convert = monowhite_to_gray
2163 2164 2165
        },
    },
    [PIX_FMT_MONOBLACK] = {
2166
        [PIX_FMT_GRAY8] = {
2167
            .convert = monoblack_to_gray
2168 2169
        },
    },
2170
    [PIX_FMT_PAL8] = {
2171
        [PIX_FMT_RGB555] = {
2172 2173
            .convert = pal8_to_rgb555
        },
2174
        [PIX_FMT_RGB565] = {
2175 2176
            .convert = pal8_to_rgb565
        },
2177
        [PIX_FMT_BGR24] = {
2178 2179
            .convert = pal8_to_bgr24
        },
2180
        [PIX_FMT_RGB24] = {
2181 2182
            .convert = pal8_to_rgb24
        },
2183
        [PIX_FMT_RGB32] = {
2184
            .convert = pal8_to_rgb32
2185 2186
        },
    },
2187
    [PIX_FMT_UYYVYY411] = {
2188
        [PIX_FMT_YUV411P] = {
2189
            .convert = uyyvyy411_to_yuv411p,
2190 2191 2192
        },
    },

2193 2194
};

Drew Hess's avatar
Drew Hess committed
2195
int avpicture_alloc(AVPicture *picture,
2196 2197
                           int pix_fmt, int width, int height)
{
2198
    int size;
2199 2200 2201
    void *ptr;

    size = avpicture_get_size(pix_fmt, width, height);
2202 2203
    if(size<0)
        goto fail;
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
    ptr = av_malloc(size);
    if (!ptr)
        goto fail;
    avpicture_fill(picture, ptr, pix_fmt, width, height);
    return 0;
 fail:
    memset(picture, 0, sizeof(AVPicture));
    return -1;
}

Drew Hess's avatar
Drew Hess committed
2214
void avpicture_free(AVPicture *picture)
2215
{
2216
    av_free(picture->data[0]);
2217 2218
}

2219
/* return true if yuv planar */
2220
static inline int is_yuv_planar(const PixFmtInfo *ps)
2221 2222
{
    return (ps->color_type == FF_COLOR_YUV ||
2223
            ps->color_type == FF_COLOR_YUV_JPEG) &&
2224
        ps->pixel_type == FF_PIXEL_PLANAR;
2225 2226
}

2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
/**
 * Crop image top and left side
 */
int img_crop(AVPicture *dst, const AVPicture *src,
              int pix_fmt, int top_band, int left_band)
{
    int y_shift;
    int x_shift;

    if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
        return -1;

    y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
    x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;

    dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
    dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
    dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);

    dst->linesize[0] = src->linesize[0];
    dst->linesize[1] = src->linesize[1];
    dst->linesize[2] = src->linesize[2];
    return 0;
}

2252 2253 2254
/**
 * Pad image
 */
Luca Barbato's avatar
Luca Barbato committed
2255 2256 2257
int img_pad(AVPicture *dst, const AVPicture *src, int height, int width,
            int pix_fmt, int padtop, int padbottom, int padleft, int padright,
            int *color)
2258
{
2259
    uint8_t *optr;
2260 2261 2262 2263 2264
    int y_shift;
    int x_shift;
    int yheight;
    int i, y;

Luca Barbato's avatar
Luca Barbato committed
2265 2266
    if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
        !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2267 2268 2269 2270 2271 2272

    for (i = 0; i < 3; i++) {
        x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
        y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;

        if (padtop || padleft) {
Luca Barbato's avatar
Luca Barbato committed
2273 2274
            memset(dst->data[i], color[i],
                dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2275 2276
        }

2277 2278 2279 2280 2281 2282 2283
        if (padleft || padright) {
            optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
                (dst->linesize[i] - (padright >> x_shift));
            yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
            for (y = 0; y < yheight; y++) {
                memset(optr, color[i], (padleft + padright) >> x_shift);
                optr += dst->linesize[i];
2284
            }
2285 2286 2287 2288 2289 2290 2291 2292
        }

        if (src) { /* first line */
            uint8_t *iptr = src->data[i];
            optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
                    (padleft >> x_shift);
            memcpy(optr, iptr, src->linesize[i]);
            iptr += src->linesize[i];
Luca Barbato's avatar
Luca Barbato committed
2293 2294
            optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
                (dst->linesize[i] - (padright >> x_shift));
2295 2296 2297
            yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
            for (y = 0; y < yheight; y++) {
                memset(optr, color[i], (padleft + padright) >> x_shift);
2298 2299 2300
                memcpy(optr + ((padleft + padright) >> x_shift), iptr,
                    src->linesize[i]);
                iptr += src->linesize[i];
2301 2302 2303 2304 2305
                optr += dst->linesize[i];
            }
        }

        if (padbottom || padright) {
Luca Barbato's avatar
Luca Barbato committed
2306 2307 2308 2309
            optr = dst->data[i] + dst->linesize[i] *
                ((height - padbottom) >> y_shift) - (padright >> x_shift);
            memset(optr, color[i],dst->linesize[i] *
                (padbottom >> y_shift) + (padright >> x_shift));
2310 2311 2312 2313 2314
        }
    }
    return 0;
}

2315
#ifndef CONFIG_SWSCALER
2316 2317
/* XXX: always use linesize. Return -1 if not supported */
int img_convert(AVPicture *dst, int dst_pix_fmt,
2318
                const AVPicture *src, int src_pix_fmt,
2319
                int src_width, int src_height)
2320
{
2321
    static int inited;
2322
    int i, ret, dst_width, dst_height, int_pix_fmt;
2323 2324
    const PixFmtInfo *src_pix, *dst_pix;
    const ConvertEntry *ce;
2325 2326 2327 2328 2329 2330 2331
    AVPicture tmp1, *tmp = &tmp1;

    if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
        dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
        return -1;
    if (src_width <= 0 || src_height <= 0)
        return 0;
2332

2333 2334 2335 2336 2337
    if (!inited) {
        inited = 1;
        img_convert_init();
    }

2338 2339
    dst_width = src_width;
    dst_height = src_height;
2340

2341 2342 2343
    dst_pix = &pix_fmt_info[dst_pix_fmt];
    src_pix = &pix_fmt_info[src_pix_fmt];
    if (src_pix_fmt == dst_pix_fmt) {
2344 2345
        /* no conversion needed: just copy */
        img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2346 2347 2348 2349 2350
        return 0;
    }

    ce = &convert_table[src_pix_fmt][dst_pix_fmt];
    if (ce->convert) {
2351
        /* specific conversion routine */
2352 2353 2354 2355 2356
        ce->convert(dst, src, dst_width, dst_height);
        return 0;
    }

    /* gray to YUV */
2357
    if (is_yuv_planar(dst_pix) &&
2358
        src_pix_fmt == PIX_FMT_GRAY8) {
2359 2360 2361
        int w, h, y;
        uint8_t *d;

2362
        if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2363
            ff_img_copy_plane(dst->data[0], dst->linesize[0],
2364 2365 2366
                     src->data[0], src->linesize[0],
                     dst_width, dst_height);
        } else {
2367 2368 2369 2370
            img_apply_table(dst->data[0], dst->linesize[0],
                            src->data[0], src->linesize[0],
                            dst_width, dst_height,
                            y_jpeg_to_ccir);
2371
        }
2372 2373 2374 2375 2376 2377 2378
        /* fill U and V with 128 */
        w = dst_width;
        h = dst_height;
        w >>= dst_pix->x_chroma_shift;
        h >>= dst_pix->y_chroma_shift;
        for(i = 1; i <= 2; i++) {
            d = dst->data[i];
2379 2380
            for(y = 0; y< h; y++) {
                memset(d, 128, w);
2381 2382
                d += dst->linesize[i];
            }
2383
        }
2384 2385 2386 2387
        return 0;
    }

    /* YUV to gray */
2388
    if (is_yuv_planar(src_pix) &&
2389 2390
        dst_pix_fmt == PIX_FMT_GRAY8) {
        if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2391
            ff_img_copy_plane(dst->data[0], dst->linesize[0],
2392 2393 2394
                     src->data[0], src->linesize[0],
                     dst_width, dst_height);
        } else {
2395 2396 2397 2398
            img_apply_table(dst->data[0], dst->linesize[0],
                            src->data[0], src->linesize[0],
                            dst_width, dst_height,
                            y_ccir_to_jpeg);
2399
        }
2400 2401 2402
        return 0;
    }

2403 2404
    /* YUV to YUV planar */
    if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2405
        int x_shift, y_shift, w, h, xy_shift;
2406
        void (*resize_func)(uint8_t *dst, int dst_wrap,
2407
                            const uint8_t *src, int src_wrap,
2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423
                            int width, int height);

        /* compute chroma size of the smallest dimensions */
        w = dst_width;
        h = dst_height;
        if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
            w >>= dst_pix->x_chroma_shift;
        else
            w >>= src_pix->x_chroma_shift;
        if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
            h >>= dst_pix->y_chroma_shift;
        else
            h >>= src_pix->y_chroma_shift;

        x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
        y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2424 2425 2426 2427 2428
        xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
        /* there must be filters for conversion at least from and to
           YUV444 format */
        switch(xy_shift) {
        case 0x00:
2429
            resize_func = ff_img_copy_plane;
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
            break;
        case 0x10:
            resize_func = shrink21;
            break;
        case 0x20:
            resize_func = shrink41;
            break;
        case 0x01:
            resize_func = shrink12;
            break;
        case 0x11:
2441
            resize_func = ff_shrink22;
2442 2443
            break;
        case 0x22:
2444
            resize_func = ff_shrink44;
2445 2446 2447 2448 2449 2450 2451 2452
            break;
        case 0xf0:
            resize_func = grow21;
            break;
        case 0xe0:
            resize_func = grow41;
            break;
        case 0xff:
2453
            resize_func = grow22;
2454 2455 2456 2457 2458
            break;
        case 0xee:
            resize_func = grow44;
            break;
        case 0xf1:
2459
            resize_func = conv411;
2460 2461
            break;
        default:
2462
            /* currently not handled */
2463
            goto no_chroma_filter;
2464
        }
2465

2466
        ff_img_copy_plane(dst->data[0], dst->linesize[0],
2467 2468
                       src->data[0], src->linesize[0],
                       dst_width, dst_height);
2469

2470
        for(i = 1;i <= 2; i++)
2471 2472
            resize_func(dst->data[i], dst->linesize[i],
                        src->data[i], src->linesize[i],
Michael Niedermayer's avatar
Michael Niedermayer committed
2473
                        dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
        /* if yuv color space conversion is needed, we do it here on
           the destination image */
        if (dst_pix->color_type != src_pix->color_type) {
            const uint8_t *y_table, *c_table;
            if (dst_pix->color_type == FF_COLOR_YUV) {
                y_table = y_jpeg_to_ccir;
                c_table = c_jpeg_to_ccir;
            } else {
                y_table = y_ccir_to_jpeg;
                c_table = c_ccir_to_jpeg;
            }
            img_apply_table(dst->data[0], dst->linesize[0],
                            dst->data[0], dst->linesize[0],
                            dst_width, dst_height,
                            y_table);

            for(i = 1;i <= 2; i++)
                img_apply_table(dst->data[i], dst->linesize[i],
                                dst->data[i], dst->linesize[i],
2493
                                dst_width>>dst_pix->x_chroma_shift,
2494 2495 2496 2497
                                dst_height>>dst_pix->y_chroma_shift,
                                c_table);
        }
        return 0;
2498
    }
2499
 no_chroma_filter:
2500

2501
    /* try to use an intermediate format */
2502 2503
    if (src_pix_fmt == PIX_FMT_YUYV422 ||
        dst_pix_fmt == PIX_FMT_YUYV422) {
2504 2505
        /* specific case: convert to YUV422P first */
        int_pix_fmt = PIX_FMT_YUV422P;
2506 2507 2508 2509
    } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
        dst_pix_fmt == PIX_FMT_UYVY422) {
        /* specific case: convert to YUV422P first */
        int_pix_fmt = PIX_FMT_YUV422P;
2510 2511
    } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
        dst_pix_fmt == PIX_FMT_UYYVYY411) {
2512 2513
        /* specific case: convert to YUV411P first */
        int_pix_fmt = PIX_FMT_YUV411P;
2514
    } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2515
                src_pix_fmt != PIX_FMT_GRAY8) ||
2516 2517 2518
               (dst_pix->color_type == FF_COLOR_GRAY &&
                dst_pix_fmt != PIX_FMT_GRAY8)) {
        /* gray8 is the normalized format */
2519
        int_pix_fmt = PIX_FMT_GRAY8;
2520
    } else if ((is_yuv_planar(src_pix) &&
2521 2522 2523 2524 2525 2526 2527
                src_pix_fmt != PIX_FMT_YUV444P &&
                src_pix_fmt != PIX_FMT_YUVJ444P)) {
        /* yuv444 is the normalized format */
        if (src_pix->color_type == FF_COLOR_YUV_JPEG)
            int_pix_fmt = PIX_FMT_YUVJ444P;
        else
            int_pix_fmt = PIX_FMT_YUV444P;
2528
    } else if ((is_yuv_planar(dst_pix) &&
2529 2530 2531 2532 2533 2534 2535
                dst_pix_fmt != PIX_FMT_YUV444P &&
                dst_pix_fmt != PIX_FMT_YUVJ444P)) {
        /* yuv444 is the normalized format */
        if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
            int_pix_fmt = PIX_FMT_YUVJ444P;
        else
            int_pix_fmt = PIX_FMT_YUV444P;
2536
    } else {
2537 2538
        /* the two formats are rgb or gray8 or yuv[j]444p */
        if (src_pix->is_alpha && dst_pix->is_alpha)
2539
            int_pix_fmt = PIX_FMT_RGB32;
2540 2541
        else
            int_pix_fmt = PIX_FMT_RGB24;
2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555
    }
    if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
        return -1;
    ret = -1;
    if (img_convert(tmp, int_pix_fmt,
                    src, src_pix_fmt, src_width, src_height) < 0)
        goto fail1;
    if (img_convert(dst, dst_pix_fmt,
                    tmp, int_pix_fmt, dst_width, dst_height) < 0)
        goto fail1;
    ret = 0;
 fail1:
    avpicture_free(tmp);
    return ret;
2556
}
2557
#endif
2558

2559
/* NOTE: we scan all the pixels to have an exact information */
2560
static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2561 2562 2563 2564 2565
{
    const unsigned char *p;
    int src_wrap, ret, x, y;
    unsigned int a;
    uint32_t *palette = (uint32_t *)src->data[1];
2566

2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
    p = src->data[0];
    src_wrap = src->linesize[0] - width;
    ret = 0;
    for(y=0;y<height;y++) {
        for(x=0;x<width;x++) {
            a = palette[p[0]] >> 24;
            if (a == 0x00) {
                ret |= FF_ALPHA_TRANSP;
            } else if (a != 0xff) {
                ret |= FF_ALPHA_SEMI_TRANSP;
            }
            p++;
        }
        p += src_wrap;
    }
    return ret;
}

/**
 * Tell if an image really has transparent alpha values.
 * @return ored mask of FF_ALPHA_xxx constants
 */
2589
int img_get_alpha_info(const AVPicture *src,
2590
                       int pix_fmt, int width, int height)
2591
{
2592
    const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2593 2594 2595 2596 2597 2598 2599
    int ret;

    pf = &pix_fmt_info[pix_fmt];
    /* no alpha can be represented in format */
    if (!pf->is_alpha)
        return 0;
    switch(pix_fmt) {
2600
    case PIX_FMT_RGB32:
2601
        ret = get_alpha_info_rgb32(src, width, height);
2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612
        break;
    case PIX_FMT_PAL8:
        ret = get_alpha_info_pal8(src, width, height);
        break;
    default:
        /* we do not know, so everything is indicated */
        ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
        break;
    }
    return ret;
}
Fred's avatar
Fred committed
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660

#ifdef HAVE_MMX
#define DEINT_INPLACE_LINE_LUM \
                    movd_m2r(lum_m4[0],mm0);\
                    movd_m2r(lum_m3[0],mm1);\
                    movd_m2r(lum_m2[0],mm2);\
                    movd_m2r(lum_m1[0],mm3);\
                    movd_m2r(lum[0],mm4);\
                    punpcklbw_r2r(mm7,mm0);\
                    movd_r2m(mm2,lum_m4[0]);\
                    punpcklbw_r2r(mm7,mm1);\
                    punpcklbw_r2r(mm7,mm2);\
                    punpcklbw_r2r(mm7,mm3);\
                    punpcklbw_r2r(mm7,mm4);\
                    paddw_r2r(mm3,mm1);\
                    psllw_i2r(1,mm2);\
                    paddw_r2r(mm4,mm0);\
                    psllw_i2r(2,mm1);\
                    paddw_r2r(mm6,mm2);\
                    paddw_r2r(mm2,mm1);\
                    psubusw_r2r(mm0,mm1);\
                    psrlw_i2r(3,mm1);\
                    packuswb_r2r(mm7,mm1);\
                    movd_r2m(mm1,lum_m2[0]);

#define DEINT_LINE_LUM \
                    movd_m2r(lum_m4[0],mm0);\
                    movd_m2r(lum_m3[0],mm1);\
                    movd_m2r(lum_m2[0],mm2);\
                    movd_m2r(lum_m1[0],mm3);\
                    movd_m2r(lum[0],mm4);\
                    punpcklbw_r2r(mm7,mm0);\
                    punpcklbw_r2r(mm7,mm1);\
                    punpcklbw_r2r(mm7,mm2);\
                    punpcklbw_r2r(mm7,mm3);\
                    punpcklbw_r2r(mm7,mm4);\
                    paddw_r2r(mm3,mm1);\
                    psllw_i2r(1,mm2);\
                    paddw_r2r(mm4,mm0);\
                    psllw_i2r(2,mm1);\
                    paddw_r2r(mm6,mm2);\
                    paddw_r2r(mm2,mm1);\
                    psubusw_r2r(mm0,mm1);\
                    psrlw_i2r(3,mm1);\
                    packuswb_r2r(mm7,mm1);\
                    movd_r2m(mm1,dst[0]);
#endif

2661
/* filter parameters: [-1 4 2 4 -1] // 8 */
2662
static void deinterlace_line(uint8_t *dst,
2663 2664 2665 2666
                             const uint8_t *lum_m4, const uint8_t *lum_m3,
                             const uint8_t *lum_m2, const uint8_t *lum_m1,
                             const uint8_t *lum,
                             int size)
2667
{
Fred's avatar
Fred committed
2668
#ifndef HAVE_MMX
2669
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2670 2671 2672
    int sum;

    for(;size > 0;size--) {
Fred's avatar
Fred committed
2673 2674 2675 2676 2677
        sum = -lum_m4[0];
        sum += lum_m3[0] << 2;
        sum += lum_m2[0] << 1;
        sum += lum_m1[0] << 2;
        sum += -lum[0];
2678
        dst[0] = cm[(sum + 4) >> 3];
Fred's avatar
Fred committed
2679 2680 2681 2682 2683
        lum_m4++;
        lum_m3++;
        lum_m2++;
        lum_m1++;
        lum++;
2684 2685
        dst++;
    }
Fred's avatar
Fred committed
2686 2687
#else

Michael Niedermayer's avatar
Michael Niedermayer committed
2688 2689 2690 2691 2692 2693 2694 2695 2696
    {
        mmx_t rounder;
        rounder.uw[0]=4;
        rounder.uw[1]=4;
        rounder.uw[2]=4;
        rounder.uw[3]=4;
        pxor_r2r(mm7,mm7);
        movq_m2r(rounder,mm6);
    }
Fred's avatar
Fred committed
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
    for (;size > 3; size-=4) {
        DEINT_LINE_LUM
        lum_m4+=4;
        lum_m3+=4;
        lum_m2+=4;
        lum_m1+=4;
        lum+=4;
        dst+=4;
    }
#endif
}
2708
static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
Fred's avatar
Fred committed
2709 2710 2711
                             int size)
{
#ifndef HAVE_MMX
2712
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
Fred's avatar
Fred committed
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
    int sum;

    for(;size > 0;size--) {
        sum = -lum_m4[0];
        sum += lum_m3[0] << 2;
        sum += lum_m2[0] << 1;
        lum_m4[0]=lum_m2[0];
        sum += lum_m1[0] << 2;
        sum += -lum[0];
        lum_m2[0] = cm[(sum + 4) >> 3];
        lum_m4++;
        lum_m3++;
        lum_m2++;
        lum_m1++;
        lum++;
    }
#else

Michael Niedermayer's avatar
Michael Niedermayer committed
2731 2732 2733 2734 2735 2736 2737 2738 2739
    {
        mmx_t rounder;
        rounder.uw[0]=4;
        rounder.uw[1]=4;
        rounder.uw[2]=4;
        rounder.uw[3]=4;
        pxor_r2r(mm7,mm7);
        movq_m2r(rounder,mm6);
    }
Fred's avatar
Fred committed
2740 2741 2742 2743 2744 2745 2746 2747 2748
    for (;size > 3; size-=4) {
        DEINT_INPLACE_LINE_LUM
        lum_m4+=4;
        lum_m3+=4;
        lum_m2+=4;
        lum_m1+=4;
        lum+=4;
    }
#endif
2749 2750 2751 2752 2753
}

/* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
   top field is copied as is, but the bottom field is deinterlaced
   against the top field. */
2754
static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2755
                                    const uint8_t *src1, int src_wrap,
Fred's avatar
Fred committed
2756
                                    int width, int height)
2757
{
2758
    const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
Fred's avatar
Fred committed
2759 2760 2761 2762 2763 2764 2765 2766 2767
    int y;

    src_m2 = src1;
    src_m1 = src1;
    src_0=&src_m1[src_wrap];
    src_p1=&src_0[src_wrap];
    src_p2=&src_p1[src_wrap];
    for(y=0;y<(height-2);y+=2) {
        memcpy(dst,src_m1,width);
2768
        dst += dst_wrap;
Fred's avatar
Fred committed
2769 2770 2771 2772 2773 2774
        deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
        src_m2 = src_0;
        src_m1 = src_p1;
        src_0 = src_p2;
        src_p1 += 2*src_wrap;
        src_p2 += 2*src_wrap;
2775 2776
        dst += dst_wrap;
    }
Fred's avatar
Fred committed
2777 2778 2779 2780 2781 2782
    memcpy(dst,src_m1,width);
    dst += dst_wrap;
    /* do last line */
    deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
}

2783
static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2784
                                             int width, int height)
Fred's avatar
Fred committed
2785
{
2786
    uint8_t *src_m1, *src_0, *src_p1, *src_p2;
Fred's avatar
Fred committed
2787
    int y;
2788 2789
    uint8_t *buf;
    buf = (uint8_t*)av_malloc(width);
Fred's avatar
Fred committed
2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804

    src_m1 = src1;
    memcpy(buf,src_m1,width);
    src_0=&src_m1[src_wrap];
    src_p1=&src_0[src_wrap];
    src_p2=&src_p1[src_wrap];
    for(y=0;y<(height-2);y+=2) {
        deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
        src_m1 = src_p1;
        src_0 = src_p2;
        src_p1 += 2*src_wrap;
        src_p2 += 2*src_wrap;
    }
    /* do last line */
    deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2805
    av_free(buf);
2806 2807 2808
}


Fred's avatar
Fred committed
2809
/* deinterlace - if not supported return -1 */
2810
int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
Fabrice Bellard's avatar
Fabrice Bellard committed
2811 2812
                          int pix_fmt, int width, int height)
{
2813 2814 2815 2816
    int i;

    if (pix_fmt != PIX_FMT_YUV420P &&
        pix_fmt != PIX_FMT_YUV422P &&
2817
        pix_fmt != PIX_FMT_YUV444P &&
2818
        pix_fmt != PIX_FMT_YUV411P)
2819
        return -1;
Fred's avatar
Fred committed
2820
    if ((width & 3) != 0 || (height & 3) != 0)
2821
        return -1;
Fred's avatar
Fred committed
2822

2823 2824 2825 2826 2827 2828 2829 2830 2831 2832
    for(i=0;i<3;i++) {
        if (i == 1) {
            switch(pix_fmt) {
            case PIX_FMT_YUV420P:
                width >>= 1;
                height >>= 1;
                break;
            case PIX_FMT_YUV422P:
                width >>= 1;
                break;
2833 2834 2835
            case PIX_FMT_YUV411P:
                width >>= 2;
                break;
2836 2837 2838 2839
            default:
                break;
            }
        }
Fred's avatar
Fred committed
2840
        if (src == dst) {
2841
            deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2842
                                 width, height);
Fred's avatar
Fred committed
2843 2844 2845 2846 2847
        } else {
            deinterlace_bottom_field(dst->data[i],dst->linesize[i],
                                        src->data[i], src->linesize[i],
                                        width, height);
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
2848
    }
Fred's avatar
Fred committed
2849 2850 2851
#ifdef HAVE_MMX
    emms();
#endif
2852
    return 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
2853
}
2854 2855

#undef FIX