imgconvert.c 78 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1
/*
Diego Biurrun's avatar
Diego Biurrun committed
2
 * Misc image conversion 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
Diego Biurrun's avatar
Diego Biurrun committed
24
 * misc image conversion routines
Michael Niedermayer's avatar
Michael Niedermayer committed
25 26
 */

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"
35
#include "colorspace.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
36

Fred's avatar
Fred committed
37 38 39
#ifdef HAVE_MMX
#include "i386/mmx.h"
#endif
40

41 42 43
#define xglue(x, y) x ## y
#define glue(x, y) xglue(x, y)

44 45 46 47
#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 */
48

49 50 51
#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 */
52

53 54
typedef struct PixFmtInfo {
    const char *name;
55 56 57 58 59 60 61
    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) */
    uint8_t is_alpha : 1;    /**< true if alpha can be specified */
    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 */
62 63 64
} PixFmtInfo;

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

132 133 134 135 136 137 138 139 140 141
    /* YUV formats with alpha plane */
    [PIX_FMT_YUVA420P] = {
        .name = "yuva420p",
        .nb_channels = 4,
        .color_type = FF_COLOR_YUV,
        .pixel_type = FF_PIXEL_PLANAR,
        .depth = 8,
        .x_chroma_shift = 1, .y_chroma_shift = 1,
    },

142 143 144
    /* JPEG YUV */
    [PIX_FMT_YUVJ420P] = {
        .name = "yuvj420p",
145
        .nb_channels = 3,
146
        .color_type = FF_COLOR_YUV_JPEG,
147
        .pixel_type = FF_PIXEL_PLANAR,
148
        .depth = 8,
149
        .x_chroma_shift = 1, .y_chroma_shift = 1,
150 151 152
    },
    [PIX_FMT_YUVJ422P] = {
        .name = "yuvj422p",
153
        .nb_channels = 3,
154
        .color_type = FF_COLOR_YUV_JPEG,
155
        .pixel_type = FF_PIXEL_PLANAR,
156
        .depth = 8,
157
        .x_chroma_shift = 1, .y_chroma_shift = 0,
158 159 160
    },
    [PIX_FMT_YUVJ444P] = {
        .name = "yuvj444p",
161
        .nb_channels = 3,
162
        .color_type = FF_COLOR_YUV_JPEG,
163
        .pixel_type = FF_PIXEL_PLANAR,
164
        .depth = 8,
165
        .x_chroma_shift = 0, .y_chroma_shift = 0,
166
    },
167 168 169 170 171 172 173 174
    [PIX_FMT_YUVJ440P] = {
        .name = "yuvj440p",
        .nb_channels = 3,
        .color_type = FF_COLOR_YUV_JPEG,
        .pixel_type = FF_PIXEL_PLANAR,
        .depth = 8,
        .x_chroma_shift = 0, .y_chroma_shift = 1,
    },
175

176 177
    /* RGB formats */
    [PIX_FMT_RGB24] = {
178
        .name = "rgb24",
179
        .nb_channels = 3,
180
        .color_type = FF_COLOR_RGB,
181
        .pixel_type = FF_PIXEL_PACKED,
182
        .depth = 8,
Michael Niedermayer's avatar
Michael Niedermayer committed
183
        .x_chroma_shift = 0, .y_chroma_shift = 0,
184 185
    },
    [PIX_FMT_BGR24] = {
186
        .name = "bgr24",
187
        .nb_channels = 3,
188
        .color_type = FF_COLOR_RGB,
189
        .pixel_type = FF_PIXEL_PACKED,
190
        .depth = 8,
Michael Niedermayer's avatar
Michael Niedermayer committed
191
        .x_chroma_shift = 0, .y_chroma_shift = 0,
192
    },
193 194
    [PIX_FMT_RGB32] = {
        .name = "rgb32",
195
        .nb_channels = 4, .is_alpha = 1,
196
        .color_type = FF_COLOR_RGB,
197
        .pixel_type = FF_PIXEL_PACKED,
198
        .depth = 8,
Michael Niedermayer's avatar
Michael Niedermayer committed
199
        .x_chroma_shift = 0, .y_chroma_shift = 0,
200 201
    },
    [PIX_FMT_RGB565] = {
202
        .name = "rgb565",
203
        .nb_channels = 3,
204
        .color_type = FF_COLOR_RGB,
205
        .pixel_type = FF_PIXEL_PACKED,
206
        .depth = 5,
Michael Niedermayer's avatar
Michael Niedermayer committed
207
        .x_chroma_shift = 0, .y_chroma_shift = 0,
208 209
    },
    [PIX_FMT_RGB555] = {
210
        .name = "rgb555",
211
        .nb_channels = 3,
212
        .color_type = FF_COLOR_RGB,
213
        .pixel_type = FF_PIXEL_PACKED,
214
        .depth = 5,
Michael Niedermayer's avatar
Michael Niedermayer committed
215
        .x_chroma_shift = 0, .y_chroma_shift = 0,
216 217 218
    },

    /* gray / mono formats */
219 220 221 222 223 224 225 226 227 228 229 230 231 232
    [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,
    },
233
    [PIX_FMT_GRAY8] = {
234
        .name = "gray",
235
        .nb_channels = 1,
236
        .color_type = FF_COLOR_GRAY,
237
        .pixel_type = FF_PIXEL_PLANAR,
238
        .depth = 8,
239 240
    },
    [PIX_FMT_MONOWHITE] = {
241
        .name = "monow",
242
        .nb_channels = 1,
243
        .color_type = FF_COLOR_GRAY,
244
        .pixel_type = FF_PIXEL_PLANAR,
245
        .depth = 1,
246 247
    },
    [PIX_FMT_MONOBLACK] = {
248
        .name = "monob",
249
        .nb_channels = 1,
250
        .color_type = FF_COLOR_GRAY,
251
        .pixel_type = FF_PIXEL_PLANAR,
252
        .depth = 1,
253
    },
254 255 256 257

    /* paletted formats */
    [PIX_FMT_PAL8] = {
        .name = "pal8",
258
        .nb_channels = 4, .is_alpha = 1,
259
        .color_type = FF_COLOR_RGB,
260
        .pixel_type = FF_PIXEL_PALETTE,
261
        .depth = 8,
262
    },
263 264 265 266 267 268
    [PIX_FMT_XVMC_MPEG2_MC] = {
        .name = "xvmcmc",
    },
    [PIX_FMT_XVMC_MPEG2_IDCT] = {
        .name = "xvmcidct",
    },
269 270
    [PIX_FMT_UYYVYY411] = {
        .name = "uyyvyy411",
271 272 273 274 275 276
        .nb_channels = 1,
        .color_type = FF_COLOR_YUV,
        .pixel_type = FF_PIXEL_PACKED,
        .depth = 8,
        .x_chroma_shift = 2, .y_chroma_shift = 0,
    },
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    [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",
295
        .nb_channels = 3,
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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
        .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,
    },
382 383 384 385
};

void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
{
386 387
    *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
    *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
388 389 390 391 392 393 394 395 396 397
}

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;
}

398 399
enum PixelFormat avcodec_get_pix_fmt(const char* name)
{
400 401
    int i;

402 403
    for (i=0; i < PIX_FMT_NB; i++)
         if (!strcmp(pix_fmt_info[i].name, name))
404
             break;
405 406 407
    return i;
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
{
    PixFmtInfo info= pix_fmt_info[pix_fmt];

    char is_alpha_char= info.is_alpha ? 'y' : 'n';

    /* print header */
    if (pix_fmt < 0)
        snprintf (buf, buf_size,
                  "name      " " nb_channels" " depth" " is_alpha"
            );
    else
        snprintf (buf, buf_size,
                  "%-10s" "      %1d     " "   %2d " "     %c   ",
                  info.name,
                  info.nb_channels,
                  info.depth,
                  is_alpha_char
            );
}

429
int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
430
{
431
    int w2;
432
    const PixFmtInfo *pinfo;
433

434
    memset(picture->linesize, 0, sizeof(picture->linesize));
435

436
    pinfo = &pix_fmt_info[pix_fmt];
437 438
    switch(pix_fmt) {
    case PIX_FMT_YUV420P:
439 440 441 442
    case PIX_FMT_YUV422P:
    case PIX_FMT_YUV444P:
    case PIX_FMT_YUV410P:
    case PIX_FMT_YUV411P:
443
    case PIX_FMT_YUV440P:
444 445 446
    case PIX_FMT_YUVJ420P:
    case PIX_FMT_YUVJ422P:
    case PIX_FMT_YUVJ444P:
447
    case PIX_FMT_YUVJ440P:
448
        w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
449
        picture->linesize[0] = width;
450 451
        picture->linesize[1] = w2;
        picture->linesize[2] = w2;
452
        break;
453 454 455 456 457 458
    case PIX_FMT_YUVA420P:
        w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
        picture->linesize[0] = width;
        picture->linesize[1] = w2;
        picture->linesize[2] = w2;
        picture->linesize[3] = width;
459
        break;
460 461 462 463 464
    case PIX_FMT_NV12:
    case PIX_FMT_NV21:
        w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
        picture->linesize[0] = width;
        picture->linesize[1] = w2;
465
        break;
466 467 468
    case PIX_FMT_RGB24:
    case PIX_FMT_BGR24:
        picture->linesize[0] = width * 3;
469
        break;
470
    case PIX_FMT_RGB32:
471 472 473
    case PIX_FMT_BGR32:
    case PIX_FMT_RGB32_1:
    case PIX_FMT_BGR32_1:
474
        picture->linesize[0] = width * 4;
475
        break;
476 477
    case PIX_FMT_GRAY16BE:
    case PIX_FMT_GRAY16LE:
478 479
    case PIX_FMT_BGR555:
    case PIX_FMT_BGR565:
480 481
    case PIX_FMT_RGB555:
    case PIX_FMT_RGB565:
482
    case PIX_FMT_YUYV422:
483
        picture->linesize[0] = width * 2;
484
        break;
485 486
    case PIX_FMT_UYVY422:
        picture->linesize[0] = width * 2;
487
        break;
488
    case PIX_FMT_UYYVYY411:
489
        picture->linesize[0] = width + width/2;
490
        break;
491 492 493 494
    case PIX_FMT_RGB8:
    case PIX_FMT_BGR8:
    case PIX_FMT_RGB4_BYTE:
    case PIX_FMT_BGR4_BYTE:
495 496
    case PIX_FMT_GRAY8:
        picture->linesize[0] = width;
497
        break;
498 499
    case PIX_FMT_RGB4:
    case PIX_FMT_BGR4:
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
        picture->linesize[0] = width / 2;
        break;
    case PIX_FMT_MONOWHITE:
    case PIX_FMT_MONOBLACK:
        picture->linesize[0] = (width + 7) >> 3;
        break;
    case PIX_FMT_PAL8:
        picture->linesize[0] = width;
        picture->linesize[1] = 4;
        break;
    default:
        return -1;
    }
    return 0;
}

int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
                    int height)
{
    int size, h2, size2;
    const PixFmtInfo *pinfo;

    pinfo = &pix_fmt_info[pix_fmt];
    size = picture->linesize[0] * height;
    switch(pix_fmt) {
    case PIX_FMT_YUV420P:
    case PIX_FMT_YUV422P:
    case PIX_FMT_YUV444P:
    case PIX_FMT_YUV410P:
    case PIX_FMT_YUV411P:
    case PIX_FMT_YUV440P:
    case PIX_FMT_YUVJ420P:
    case PIX_FMT_YUVJ422P:
    case PIX_FMT_YUVJ444P:
    case PIX_FMT_YUVJ440P:
        h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
        size2 = picture->linesize[1] * h2;
537
        picture->data[0] = ptr;
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
        picture->data[1] = picture->data[0] + size;
        picture->data[2] = picture->data[1] + size2;
        picture->data[3] = NULL;
        return size + 2 * size2;
    case PIX_FMT_YUVA420P:
        h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
        size2 = picture->linesize[1] * h2;
        picture->data[0] = ptr;
        picture->data[1] = picture->data[0] + size;
        picture->data[2] = picture->data[1] + size2;
        picture->data[3] = picture->data[1] + size2 + size2;
        return 2 * size + 2 * size2;
    case PIX_FMT_NV12:
    case PIX_FMT_NV21:
        h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
        size2 = picture->linesize[1] * h2 * 2;
        picture->data[0] = ptr;
        picture->data[1] = picture->data[0] + size;
556
        picture->data[2] = NULL;
557
        picture->data[3] = NULL;
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
        return size + 2 * size2;
    case PIX_FMT_RGB24:
    case PIX_FMT_BGR24:
    case PIX_FMT_RGB32:
    case PIX_FMT_BGR32:
    case PIX_FMT_RGB32_1:
    case PIX_FMT_BGR32_1:
    case PIX_FMT_GRAY16BE:
    case PIX_FMT_GRAY16LE:
    case PIX_FMT_BGR555:
    case PIX_FMT_BGR565:
    case PIX_FMT_RGB555:
    case PIX_FMT_RGB565:
    case PIX_FMT_YUYV422:
    case PIX_FMT_UYVY422:
    case PIX_FMT_UYYVYY411:
    case PIX_FMT_RGB8:
    case PIX_FMT_BGR8:
    case PIX_FMT_RGB4_BYTE:
    case PIX_FMT_BGR4_BYTE:
    case PIX_FMT_GRAY8:
    case PIX_FMT_RGB4:
    case PIX_FMT_BGR4:
581 582 583 584 585
    case PIX_FMT_MONOWHITE:
    case PIX_FMT_MONOBLACK:
        picture->data[0] = ptr;
        picture->data[1] = NULL;
        picture->data[2] = NULL;
586
        picture->data[3] = NULL;
587
        return size;
588 589 590 591 592
    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;
593
        picture->data[3] = NULL;
594
        return size2 + 256 * 4;
595 596 597 598
    default:
        picture->data[0] = NULL;
        picture->data[1] = NULL;
        picture->data[2] = NULL;
599
        picture->data[3] = NULL;
600 601 602 603
        return -1;
    }
}

604 605 606 607 608 609 610
int avpicture_fill(AVPicture *picture, uint8_t *ptr,
                   int pix_fmt, int width, int height)
{

    if(avcodec_check_dimensions(NULL, width, height))
        return -1;

611
    if (ff_fill_linesize(picture, pix_fmt, width))
612 613
        return -1;

614
    return ff_fill_pointer(picture, ptr, pix_fmt, height);
615 616
}

617
int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
618 619
                     unsigned char *dest, int dest_size)
{
620
    const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
621
    int i, j, w, h, data_planes;
622
    const unsigned char* s;
623 624
    int size = avpicture_get_size(pix_fmt, width, height);

625
    if (size > dest_size || size < 0)
626 627
        return -1;

628
    if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
629
        if (pix_fmt == PIX_FMT_YUYV422 ||
630
            pix_fmt == PIX_FMT_UYVY422 ||
631
            pix_fmt == PIX_FMT_BGR565 ||
632
            pix_fmt == PIX_FMT_BGR555 ||
633 634 635
            pix_fmt == PIX_FMT_RGB565 ||
            pix_fmt == PIX_FMT_RGB555)
            w = width * 2;
636
        else if (pix_fmt == PIX_FMT_UYYVYY411)
637 638 639 640 641 642 643 644
          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;
645 646
    } else {
        data_planes = pf->nb_channels;
647 648
        w = (width*pf->depth + 7)/8;
        h = height;
649
    }
650

651 652
    for (i=0; i<data_planes; i++) {
         if (i == 1) {
653 654 655
             w = width >> pf->x_chroma_shift;
             h = height >> pf->y_chroma_shift;
         }
656
         s = src->data[i];
657 658 659 660 661
         for(j=0; j<h; j++) {
             memcpy(dest, s, w);
             dest += w;
             s += src->linesize[i];
         }
662
    }
663

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

667 668 669
    return size;
}

670 671 672 673 674 675
int avpicture_get_size(int pix_fmt, int width, int height)
{
    AVPicture dummy_pict;
    return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
}

676 677 678 679 680 681 682 683 684 685 686 687
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];
688 689
    if (pf->depth < ps->depth ||
        (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
690
        loss |= FF_LOSS_DEPTH;
691 692
    if (pf->x_chroma_shift > ps->x_chroma_shift ||
        pf->y_chroma_shift > ps->y_chroma_shift)
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
        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 &&
710
            ps->color_type != FF_COLOR_YUV &&
711
            ps->color_type != FF_COLOR_GRAY)
712 713 714 715 716 717 718 719 720 721 722 723 724
            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;
725
    if (pf->pixel_type == FF_PIXEL_PALETTE &&
726
        (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
727 728 729 730 731 732 733 734 735 736
        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];
737 738
    switch(pf->pixel_type) {
    case FF_PIXEL_PACKED:
739
        switch(pix_fmt) {
740
        case PIX_FMT_YUYV422:
741
        case PIX_FMT_UYVY422:
742 743
        case PIX_FMT_RGB565:
        case PIX_FMT_RGB555:
744 745
        case PIX_FMT_BGR565:
        case PIX_FMT_BGR555:
746 747
            bits = 16;
            break;
748
        case PIX_FMT_UYYVYY411:
749 750
            bits = 12;
            break;
751
        default:
752
            bits = pf->depth * pf->nb_channels;
753 754
            break;
        }
755 756 757 758 759
        break;
    case FF_PIXEL_PLANAR:
        if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
            bits = pf->depth * pf->nb_channels;
        } else {
760
            bits = pf->depth + ((2 * pf->depth) >>
761 762 763 764 765 766 767 768 769
                                (pf->x_chroma_shift + pf->y_chroma_shift));
        }
        break;
    case FF_PIXEL_PALETTE:
        bits = 8;
        break;
    default:
        bits = -1;
        break;
770 771 772 773
    }
    return bits;
}

774
static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
                                      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;
}

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++];
817
        dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
818 819 820 821 822 823 824 825 826 827 828 829 830
                                                 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;
}

831
void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
832 833
                           const uint8_t *src, int src_wrap,
                           int width, int height)
834
{
835
    if((!dst) || (!src))
Michael Niedermayer's avatar
Michael Niedermayer committed
836
        return;
837 838 839 840 841 842 843
    for(;height > 0; height--) {
        memcpy(dst, src, width);
        dst += dst_wrap;
        src += src_wrap;
    }
}

844
int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
845
{
846
    int bits;
847
    const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
848

849 850 851 852
    pf = &pix_fmt_info[pix_fmt];
    switch(pf->pixel_type) {
    case FF_PIXEL_PACKED:
        switch(pix_fmt) {
853
        case PIX_FMT_YUYV422:
854
        case PIX_FMT_UYVY422:
855 856
        case PIX_FMT_RGB565:
        case PIX_FMT_RGB555:
857 858
        case PIX_FMT_BGR565:
        case PIX_FMT_BGR555:
859 860
            bits = 16;
            break;
861
        case PIX_FMT_UYYVYY411:
862 863
            bits = 12;
            break;
864 865 866 867
        default:
            bits = pf->depth * pf->nb_channels;
            break;
        }
868 869 870 871 872 873 874 875 876 877 878
        return (width * bits + 7) >> 3;
        break;
    case FF_PIXEL_PLANAR:
            if (plane == 1 || plane == 2)
                width >>= pf->x_chroma_shift;

            return (width * pf->depth + 7) >> 3;
        break;
    case FF_PIXEL_PALETTE:
        if (plane == 0)
            return width;
879
        break;
880 881 882 883 884 885 886 887 888 889 890 891 892 893
    }

    return -1;
}

void av_picture_copy(AVPicture *dst, const AVPicture *src,
              int pix_fmt, int width, int height)
{
    int i;
    const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];

    pf = &pix_fmt_info[pix_fmt];
    switch(pf->pixel_type) {
    case FF_PIXEL_PACKED:
894 895 896
    case FF_PIXEL_PLANAR:
        for(i = 0; i < pf->nb_channels; i++) {
            int w, h;
897
            int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
898 899 900 901 902 903
            w = width;
            h = height;
            if (i == 1 || i == 2) {
                w >>= pf->x_chroma_shift;
                h >>= pf->y_chroma_shift;
            }
904
            ff_img_copy_plane(dst->data[i], dst->linesize[i],
905 906 907 908 909
                           src->data[i], src->linesize[i],
                           bwidth, h);
        }
        break;
    case FF_PIXEL_PALETTE:
910
        ff_img_copy_plane(dst->data[0], dst->linesize[0],
911 912 913
                       src->data[0], src->linesize[0],
                       width, height);
        /* copy the palette */
914
        ff_img_copy_plane(dst->data[1], dst->linesize[1],
915 916 917 918 919
                       src->data[1], src->linesize[1],
                       4, 256);
        break;
    }
}
920

Fabrice Bellard's avatar
Fabrice Bellard committed
921 922
/* XXX: totally non optimized */

923
static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
924
                              int width, int height)
Fabrice Bellard's avatar
Fabrice Bellard committed
925
{
926 927
    const uint8_t *p, *p1;
    uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
928
    int w;
929

930 931
    p1 = src->data[0];
    lum1 = dst->data[0];
932 933
    cb1 = dst->data[1];
    cr1 = dst->data[2];
934

935
    for(;height >= 1; height -= 2) {
936 937 938 939
        p = p1;
        lum = lum1;
        cb = cb1;
        cr = cr1;
940
        for(w = width; w >= 2; w -= 2) {
941 942 943 944
            lum[0] = p[0];
            cb[0] = p[1];
            lum[1] = p[2];
            cr[0] = p[3];
Fabrice Bellard's avatar
Fabrice Bellard committed
945 946 947 948 949
            p += 4;
            lum += 2;
            cb++;
            cr++;
        }
950
        if (w) {
951
            lum[0] = p[0];
952 953 954 955
            cb[0] = p[1];
            cr[0] = p[3];
            cb++;
            cr++;
Fabrice Bellard's avatar
Fabrice Bellard committed
956
        }
957 958
        p1 += src->linesize[0];
        lum1 += dst->linesize[0];
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
        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];
        }
974 975 976 977 978
        cb1 += dst->linesize[1];
        cr1 += dst->linesize[2];
    }
}

979 980 981 982 983 984
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;
985

986
    p1 = src->data[0];
987

988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 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 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    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];
    }
}


1071
static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
1072 1073 1074 1075 1076 1077 1078 1079
                              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];
1080 1081 1082
    cb1 = dst->data[1];
    cr1 = dst->data[2];
    for(;height > 0; height--) {
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
        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
1101 1102 1103
    }
}

1104
static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1105 1106 1107 1108 1109 1110 1111 1112
                              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];
1113 1114 1115
    cb1 = src->data[1];
    cr1 = src->data[2];
    for(;height > 0; height--) {
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
        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++;
        }
1130 1131 1132 1133
        p1 += dst->linesize[0];
        lum1 += src->linesize[0];
        cb1 += src->linesize[1];
        cr1 += src->linesize[2];
1134 1135 1136
    }
}

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
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];
    }
}

1170
static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
                              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];
1188
            lum[0] = p[1];
1189 1190
            lum[1] = p[2];
            cr[0] = p[3];
1191 1192
            lum[2] = p[4];
            lum[3] = p[5];
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
            p += 6;
            lum += 4;
            cb++;
            cr++;
        }
        p1 += src->linesize[0];
        lum1 += dst->linesize[0];
        cb1 += dst->linesize[1];
        cr1 += dst->linesize[2];
    }
}
1204 1205


1206
static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1207 1208 1209 1210 1211 1212 1213
                              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];
1214

1215 1216 1217
    for(h = height / 2; h--;) {
        line1 = linesrc;
        line2 = linesrc + dst->linesize[0];
1218

1219 1220
        lum1 = lumsrc;
        lum2 = lumsrc + src->linesize[0];
1221

1222 1223
        cb1 = cb2;
        cr1 = cr2;
1224

1225
        for(w = width / 2; w--;) {
1226 1227 1228
                *line1++ = *lum1++; *line2++ = *lum2++;
                *line1++ =          *line2++ = *cb1++;
                *line1++ = *lum1++; *line2++ = *lum2++;
1229 1230
                *line1++ =          *line2++ = *cr1++;
        }
1231

1232 1233 1234 1235 1236 1237 1238
        linesrc += dst->linesize[0] * 2;
        lumsrc += src->linesize[0] * 2;
        cb2 += src->linesize[1];
        cr2 += src->linesize[2];
    }
}

1239 1240 1241 1242 1243 1244 1245 1246
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];
1247

1248 1249 1250
    for(h = height / 2; h--;) {
        line1 = linesrc;
        line2 = linesrc + dst->linesize[0];
1251

1252 1253
        lum1 = lumsrc;
        lum2 = lumsrc + src->linesize[0];
1254

1255 1256
        cb1 = cb2;
        cr1 = cr2;
1257

1258
        for(w = width / 2; w--;) {
1259 1260
                *line1++ =          *line2++ = *cb1++;
                *line1++ = *lum1++; *line2++ = *lum2++;
1261
                *line1++ =          *line2++ = *cr1++;
1262
                *line1++ = *lum1++; *line2++ = *lum2++;
1263
        }
1264

1265 1266 1267 1268 1269 1270 1271
        linesrc += dst->linesize[0] * 2;
        lumsrc += src->linesize[0] * 2;
        cb2 += src->linesize[1];
        cr2 += src->linesize[2];
    }
}

1272 1273 1274 1275 1276 1277 1278
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)
1279
{
1280
    int i;
1281
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1282

1283 1284 1285 1286 1287
    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);
1288 1289 1290
    }
}

1291
/* apply to each pixel the given table */
1292
static void img_apply_table(uint8_t *dst, int dst_wrap,
1293 1294
                            const uint8_t *src, int src_wrap,
                            int width, int height, const uint8_t *table1)
1295 1296 1297 1298
{
    int n;
    const uint8_t *s;
    uint8_t *d;
1299
    const uint8_t *table;
1300

1301
    table = table1;
1302 1303 1304 1305 1306
    for(;height > 0; height--) {
        s = src;
        d = dst;
        n = width;
        while (n >= 4) {
1307 1308 1309 1310
            d[0] = table[s[0]];
            d[1] = table[s[1]];
            d[2] = table[s[2]];
            d[3] = table[s[3]];
1311 1312 1313 1314 1315
            d += 4;
            s += 4;
            n -= 4;
        }
        while (n > 0) {
1316
            d[0] = table[s[0]];
1317 1318 1319 1320 1321 1322 1323 1324 1325
            d++;
            s++;
            n--;
        }
        dst += dst_wrap;
        src += src_wrap;
    }
}

1326
/* XXX: use generic filter ? */
1327 1328 1329
/* XXX: in most cases, the sampling position is incorrect */

/* 4x1 -> 1x1 */
1330
static void shrink41(uint8_t *dst, int dst_wrap,
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
                     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 */
1352
static void shrink21(uint8_t *dst, int dst_wrap,
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
                     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;
    }
}

1373
/* 1x2 -> 1x1 */
1374
static void shrink12(uint8_t *dst, int dst_wrap,
1375 1376
                     const uint8_t *src, int src_wrap,
                     int width, int height)
1377 1378
{
    int w;
1379 1380
    uint8_t *d;
    const uint8_t *s1, *s2;
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406

    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 */
1407
void ff_shrink22(uint8_t *dst, int dst_wrap,
1408
                     const uint8_t *src, int src_wrap,
1409 1410 1411
                     int width, int height)
{
    int w;
1412 1413
    const uint8_t *s1, *s2;
    uint8_t *d;
1414 1415 1416 1417 1418 1419

    for(;height > 0; height--) {
        s1 = src;
        s2 = s1 + src_wrap;
        d = dst;
        for(w = width;w >= 4; w-=4) {
1420 1421 1422 1423
            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;
1424 1425 1426 1427 1428
            s1 += 8;
            s2 += 8;
            d += 4;
        }
        for(;w > 0; w--) {
1429
            d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1430 1431 1432 1433 1434 1435 1436 1437 1438
            s1 += 2;
            s2 += 2;
            d++;
        }
        src += 2 * src_wrap;
        dst += dst_wrap;
    }
}

1439
/* 4x4 -> 1x1 */
1440
void ff_shrink44(uint8_t *dst, int dst_wrap,
1441
                     const uint8_t *src, int src_wrap,
1442 1443 1444
                     int width, int height)
{
    int w;
1445 1446
    const uint8_t *s1, *s2, *s3, *s4;
    uint8_t *d;
1447 1448 1449

    for(;height > 0; height--) {
        s1 = src;
1450 1451 1452
        s2 = s1 + src_wrap;
        s3 = s2 + src_wrap;
        s4 = s3 + src_wrap;
1453
        d = dst;
1454 1455 1456 1457 1458 1459 1460 1461 1462
        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;
1463 1464
            d++;
        }
1465 1466 1467 1468 1469
        src += 4 * src_wrap;
        dst += dst_wrap;
    }
}

1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
/* 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;
    }
}

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 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
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;
    }
}

1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
/* 1x1 -> 1x2 */
static void grow12(uint8_t *dst, int dst_wrap,
                   const uint8_t *src, int src_wrap,
                   int width, int height)
{
    for(;height > 0; height-=2) {
        memcpy(dst, src, width);
        dst += dst_wrap;
        memcpy(dst, src, width);
        dst += dst_wrap;
        src += src_wrap;
    }
}

1565 1566 1567 1568 1569 1570 1571
/* 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);
1572 1573 1574 1575 1576 1577
        if (height%2)
            src += src_wrap;
        dst += dst_wrap;
    }
}

1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
/* 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;
    }
}

1603
/* 1x2 -> 2x1 */
1604
static void conv411(uint8_t *dst, int dst_wrap,
1605
                    const uint8_t *src, int src_wrap,
1606 1607 1608
                    int width, int height)
{
    int w, c;
1609 1610
    const uint8_t *s1, *s2;
    uint8_t *d;
1611

1612 1613
    width>>=1;

1614
    for(;height > 0; height--) {
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
        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;
    }
}

1631 1632 1633 1634 1635 1636
/* 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)
1637
{
1638
    return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1639 1640
}

1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
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++) {
1652
                pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1653 1654 1655 1656 1657 1658 1659 1660
                    (pal_value[g] << 8) | pal_value[b];
            }
        }
    }
    if (has_alpha)
        pal[i++] = 0;
    while (i < 256)
        pal[i++] = 0xff000000;
1661 1662 1663 1664
}

/* copy bit n to bits 0 ... n - 1 */
static inline unsigned int bitcopy_n(unsigned int a, int n)
1665
{
1666 1667 1668 1669 1670 1671 1672
    int mask;
    mask = (1 << n) - 1;
    return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
}

/* rgb555 handling */

1673 1674
#define RGB_NAME rgb555

1675 1676
#define RGB_IN(r, g, b, s)\
{\
1677
    unsigned int v = ((const uint16_t *)(s))[0];\
1678 1679 1680 1681 1682
    r = bitcopy_n(v >> (10 - 3), 3);\
    g = bitcopy_n(v >> (5 - 3), 3);\
    b = bitcopy_n(v << 3, 3);\
}

1683

1684
#define RGB_OUT(d, r, g, b)\
1685
{\
1686
    ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1687 1688 1689 1690
}

#define BPP 2

1691
#include "imgconvert_template.h"
1692 1693 1694

/* rgb565 handling */

1695 1696
#define RGB_NAME rgb565

1697 1698
#define RGB_IN(r, g, b, s)\
{\
1699
    unsigned int v = ((const uint16_t *)(s))[0];\
1700 1701 1702 1703 1704 1705 1706
    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)\
{\
1707
    ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1708 1709 1710 1711
}

#define BPP 2

1712
#include "imgconvert_template.h"
1713 1714 1715

/* bgr24 handling */

1716 1717
#define RGB_NAME bgr24

1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
#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

1734
#include "imgconvert_template.h"
1735 1736 1737 1738 1739 1740 1741

#undef RGB_IN
#undef RGB_OUT
#undef BPP

/* rgb24 handling */

1742 1743 1744
#define RGB_NAME rgb24
#define FMT_RGB24

1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
#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

1761
#include "imgconvert_template.h"
1762

1763
/* rgb32 handling */
1764

1765 1766
#define RGB_NAME rgb32
#define FMT_RGB32
1767

1768 1769
#define RGB_IN(r, g, b, s)\
{\
1770
    unsigned int v = ((const uint32_t *)(s))[0];\
1771 1772 1773 1774 1775
    r = (v >> 16) & 0xff;\
    g = (v >> 8) & 0xff;\
    b = v & 0xff;\
}

1776
#define RGBA_IN(r, g, b, a, s)\
1777
{\
1778 1779 1780 1781 1782
    unsigned int v = ((const uint32_t *)(s))[0];\
    a = (v >> 24) & 0xff;\
    r = (v >> 16) & 0xff;\
    g = (v >> 8) & 0xff;\
    b = v & 0xff;\
1783 1784
}

1785 1786 1787
#define RGBA_OUT(d, r, g, b, a)\
{\
    ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1788 1789
}

1790
#define BPP 4
1791

1792
#include "imgconvert_template.h"
1793

1794
static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1795
                         int width, int height, int xor_mask)
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
{
    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];
1806
    dst_wrap = dst->linesize[0] - width;
1807
    for(y=0;y<height;y++) {
1808
        w = width;
1809
        while (w >= 8) {
1810 1811 1812 1813 1814 1815 1816 1817 1818
            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);
1819
            w -= 8;
1820
            q += 8;
1821 1822
        }
        if (w > 0) {
1823
            v = *p++ ^ xor_mask;
1824
            do {
1825 1826
                q[0] = -((v >> 7) & 1);
                q++;
1827 1828
                v <<= 1;
            } while (--w);
1829
        }
1830 1831
        p += src_wrap;
        q += dst_wrap;
1832 1833 1834
    }
}

1835
static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1836 1837
                               int width, int height)
{
1838 1839
    mono_to_gray(dst, src, width, height, 0xff);
}
1840

1841
static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1842 1843 1844 1845
                               int width, int height)
{
    mono_to_gray(dst, src, width, height, 0x00);
}
1846

1847
static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1848 1849 1850
                         int width, int height, int xor_mask)
{
    int n;
1851 1852
    const uint8_t *s;
    uint8_t *d;
1853 1854 1855 1856 1857 1858 1859
    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);
1860 1861

    for(y=0;y<height;y++) {
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
        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;
1873
        }
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
        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++;
1885
        }
1886 1887
        s += src_wrap;
        d += dst_wrap;
1888 1889 1890
    }
}

1891
static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1892 1893 1894 1895 1896
                              int width, int height)
{
    gray_to_mono(dst, src, width, height, 0xff);
}

1897
static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1898 1899 1900 1901 1902
                              int width, int height)
{
    gray_to_mono(dst, src, width, height, 0x00);
}

1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
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)
{
1950 1951 1952
    AVPicture tmpsrc = *src;
    tmpsrc.data[0]++;
    gray16_to_gray(dst, &tmpsrc, width, height);
1953 1954 1955 1956 1957 1958 1959
}

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;
1960
    s = (uint16_t*)src->data[0];
1961
    src_wrap = (src->linesize[0] - width * 2)/2;
1962
    d = (uint16_t*)dst->data[0];
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
    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;
    }
}


1974
typedef struct ConvertEntry {
1975
    void (*convert)(AVPicture *dst,
1976
                    const AVPicture *src, int width, int height);
1977 1978
} ConvertEntry;

Diego Biurrun's avatar
Diego Biurrun committed
1979
/* Add each new conversion function in this table. In order to be able
1980 1981 1982
   to convert from any format to any format, the following constraints
   must be satisfied:

1983
   - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1984 1985 1986

   - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8

1987
   - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1988

1989
   - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1990 1991 1992
     PIX_FMT_RGB24.

   - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1993

Vitor Sessak's avatar
Vitor Sessak committed
1994
   The other conversion functions are just optimizations for common cases.
1995
*/
1996
static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1997
    [PIX_FMT_YUV420P] = {
1998
        [PIX_FMT_YUYV422] = {
1999
            .convert = yuv420p_to_yuyv422,
2000
        },
2001
        [PIX_FMT_RGB555] = {
2002
            .convert = yuv420p_to_rgb555
2003
        },
2004
        [PIX_FMT_RGB565] = {
2005
            .convert = yuv420p_to_rgb565
2006
        },
2007
        [PIX_FMT_BGR24] = {
2008
            .convert = yuv420p_to_bgr24
2009
        },
2010
        [PIX_FMT_RGB24] = {
2011
            .convert = yuv420p_to_rgb24
2012
        },
2013
        [PIX_FMT_RGB32] = {
2014
            .convert = yuv420p_to_rgb32
2015
        },
2016
        [PIX_FMT_UYVY422] = {
2017 2018
            .convert = yuv420p_to_uyvy422,
        },
2019
    },
2020
    [PIX_FMT_YUV422P] = {
2021
        [PIX_FMT_YUYV422] = {
2022
            .convert = yuv422p_to_yuyv422,
2023
        },
2024
        [PIX_FMT_UYVY422] = {
2025 2026
            .convert = yuv422p_to_uyvy422,
        },
2027
    },
2028 2029
    [PIX_FMT_YUV444P] = {
        [PIX_FMT_RGB24] = {
2030 2031 2032 2033
            .convert = yuv444p_to_rgb24
        },
    },
    [PIX_FMT_YUVJ420P] = {
2034
        [PIX_FMT_RGB555] = {
2035
            .convert = yuvj420p_to_rgb555
2036
        },
2037
        [PIX_FMT_RGB565] = {
2038
            .convert = yuvj420p_to_rgb565
2039
        },
2040
        [PIX_FMT_BGR24] = {
2041
            .convert = yuvj420p_to_bgr24
2042
        },
2043
        [PIX_FMT_RGB24] = {
2044
            .convert = yuvj420p_to_rgb24
2045
        },
2046
        [PIX_FMT_RGB32] = {
2047
            .convert = yuvj420p_to_rgb32
2048 2049
        },
    },
2050 2051
    [PIX_FMT_YUVJ444P] = {
        [PIX_FMT_RGB24] = {
2052
            .convert = yuvj444p_to_rgb24
2053 2054
        },
    },
2055
    [PIX_FMT_YUYV422] = {
2056
        [PIX_FMT_YUV420P] = {
2057
            .convert = yuyv422_to_yuv420p,
2058
        },
2059
        [PIX_FMT_YUV422P] = {
2060
            .convert = yuyv422_to_yuv422p,
2061
        },
2062
    },
2063 2064
    [PIX_FMT_UYVY422] = {
        [PIX_FMT_YUV420P] = {
2065 2066
            .convert = uyvy422_to_yuv420p,
        },
2067
        [PIX_FMT_YUV422P] = {
2068 2069 2070
            .convert = uyvy422_to_yuv422p,
        },
    },
2071
    [PIX_FMT_RGB24] = {
2072
        [PIX_FMT_YUV420P] = {
2073
            .convert = rgb24_to_yuv420p
2074
        },
2075
        [PIX_FMT_RGB565] = {
2076
            .convert = rgb24_to_rgb565
2077
        },
2078
        [PIX_FMT_RGB555] = {
2079
            .convert = rgb24_to_rgb555
2080
        },
2081
        [PIX_FMT_RGB32] = {
2082
            .convert = rgb24_to_rgb32
2083
        },
2084
        [PIX_FMT_BGR24] = {
2085 2086
            .convert = rgb24_to_bgr24
        },
2087
        [PIX_FMT_GRAY8] = {
2088
            .convert = rgb24_to_gray
2089
        },
2090
        [PIX_FMT_PAL8] = {
2091 2092
            .convert = rgb24_to_pal8
        },
2093
        [PIX_FMT_YUV444P] = {
2094 2095
            .convert = rgb24_to_yuv444p
        },
2096
        [PIX_FMT_YUVJ420P] = {
2097 2098
            .convert = rgb24_to_yuvj420p
        },
2099
        [PIX_FMT_YUVJ444P] = {
2100 2101
            .convert = rgb24_to_yuvj444p
        },
2102
    },
2103
    [PIX_FMT_RGB32] = {
2104
        [PIX_FMT_RGB24] = {
2105
            .convert = rgb32_to_rgb24
2106
        },
2107
        [PIX_FMT_BGR24] = {
2108
            .convert = rgb32_to_bgr24
2109 2110
        },
        [PIX_FMT_RGB565] = {
2111
            .convert = rgb32_to_rgb565
2112
        },
2113
        [PIX_FMT_RGB555] = {
2114
            .convert = rgb32_to_rgb555
2115
        },
2116
        [PIX_FMT_PAL8] = {
2117
            .convert = rgb32_to_pal8
2118
        },
2119
        [PIX_FMT_YUV420P] = {
2120
            .convert = rgb32_to_yuv420p
2121
        },
2122
        [PIX_FMT_GRAY8] = {
2123
            .convert = rgb32_to_gray
2124
        },
2125 2126
    },
    [PIX_FMT_BGR24] = {
2127
        [PIX_FMT_RGB32] = {
2128
            .convert = bgr24_to_rgb32
2129
        },
2130
        [PIX_FMT_RGB24] = {
2131 2132
            .convert = bgr24_to_rgb24
        },
2133
        [PIX_FMT_YUV420P] = {
2134
            .convert = bgr24_to_yuv420p
2135
        },
2136
        [PIX_FMT_GRAY8] = {
2137 2138
            .convert = bgr24_to_gray
        },
2139 2140
    },
    [PIX_FMT_RGB555] = {
2141
        [PIX_FMT_RGB24] = {
2142 2143
            .convert = rgb555_to_rgb24
        },
2144
        [PIX_FMT_RGB32] = {
2145
            .convert = rgb555_to_rgb32
2146
        },
2147
        [PIX_FMT_YUV420P] = {
2148
            .convert = rgb555_to_yuv420p
2149
        },
2150
        [PIX_FMT_GRAY8] = {
2151 2152
            .convert = rgb555_to_gray
        },
2153 2154
    },
    [PIX_FMT_RGB565] = {
2155
        [PIX_FMT_RGB32] = {
2156
            .convert = rgb565_to_rgb32
2157
        },
2158
        [PIX_FMT_RGB24] = {
2159 2160
            .convert = rgb565_to_rgb24
        },
2161
        [PIX_FMT_YUV420P] = {
2162
            .convert = rgb565_to_yuv420p
2163
        },
2164
        [PIX_FMT_GRAY8] = {
2165 2166
            .convert = rgb565_to_gray
        },
2167
    },
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
    [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
        },
    },
2184
    [PIX_FMT_GRAY8] = {
2185
        [PIX_FMT_RGB555] = {
2186 2187
            .convert = gray_to_rgb555
        },
2188
        [PIX_FMT_RGB565] = {
2189 2190
            .convert = gray_to_rgb565
        },
2191
        [PIX_FMT_RGB24] = {
2192
            .convert = gray_to_rgb24
2193
        },
2194
        [PIX_FMT_BGR24] = {
2195 2196
            .convert = gray_to_bgr24
        },
2197
        [PIX_FMT_RGB32] = {
2198
            .convert = gray_to_rgb32
2199
        },
2200
        [PIX_FMT_MONOWHITE] = {
2201
            .convert = gray_to_monowhite
2202
        },
2203
        [PIX_FMT_MONOBLACK] = {
2204
            .convert = gray_to_monoblack
2205
        },
2206 2207 2208 2209 2210 2211
        [PIX_FMT_GRAY16LE] = {
            .convert = gray_to_gray16
        },
        [PIX_FMT_GRAY16BE] = {
            .convert = gray_to_gray16
        },
2212 2213
    },
    [PIX_FMT_MONOWHITE] = {
2214
        [PIX_FMT_GRAY8] = {
2215
            .convert = monowhite_to_gray
2216 2217 2218
        },
    },
    [PIX_FMT_MONOBLACK] = {
2219
        [PIX_FMT_GRAY8] = {
2220
            .convert = monoblack_to_gray
2221 2222
        },
    },
2223
    [PIX_FMT_PAL8] = {
2224
        [PIX_FMT_RGB555] = {
2225 2226
            .convert = pal8_to_rgb555
        },
2227
        [PIX_FMT_RGB565] = {
2228 2229
            .convert = pal8_to_rgb565
        },
2230
        [PIX_FMT_BGR24] = {
2231 2232
            .convert = pal8_to_bgr24
        },
2233
        [PIX_FMT_RGB24] = {
2234 2235
            .convert = pal8_to_rgb24
        },
2236
        [PIX_FMT_RGB32] = {
2237
            .convert = pal8_to_rgb32
2238 2239
        },
    },
2240
    [PIX_FMT_UYYVYY411] = {
2241
        [PIX_FMT_YUV411P] = {
2242
            .convert = uyyvyy411_to_yuv411p,
2243 2244 2245
        },
    },

2246 2247
};

Drew Hess's avatar
Drew Hess committed
2248
int avpicture_alloc(AVPicture *picture,
2249 2250
                           int pix_fmt, int width, int height)
{
2251
    int size;
2252 2253 2254
    void *ptr;

    size = avpicture_get_size(pix_fmt, width, height);
2255 2256
    if(size<0)
        goto fail;
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
    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
2267
void avpicture_free(AVPicture *picture)
2268
{
2269
    av_free(picture->data[0]);
2270 2271
}

2272
/* return true if yuv planar */
2273
static inline int is_yuv_planar(const PixFmtInfo *ps)
2274 2275
{
    return (ps->color_type == FF_COLOR_YUV ||
2276
            ps->color_type == FF_COLOR_YUV_JPEG) &&
2277
        ps->pixel_type == FF_PIXEL_PLANAR;
2278 2279
}

2280
int av_picture_crop(AVPicture *dst, const AVPicture *src,
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301
              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;
}

2302
int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
Luca Barbato's avatar
Luca Barbato committed
2303 2304
            int pix_fmt, int padtop, int padbottom, int padleft, int padright,
            int *color)
2305
{
2306
    uint8_t *optr;
2307 2308 2309 2310 2311
    int y_shift;
    int x_shift;
    int yheight;
    int i, y;

Luca Barbato's avatar
Luca Barbato committed
2312 2313
    if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
        !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2314 2315 2316 2317 2318 2319

    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
2320 2321
            memset(dst->data[i], color[i],
                dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2322 2323
        }

2324 2325 2326 2327 2328 2329 2330
        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];
2331
            }
2332 2333 2334 2335 2336 2337 2338 2339
        }

        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
2340 2341
            optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
                (dst->linesize[i] - (padright >> x_shift));
2342 2343 2344
            yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
            for (y = 0; y < yheight; y++) {
                memset(optr, color[i], (padleft + padright) >> x_shift);
2345 2346 2347
                memcpy(optr + ((padleft + padright) >> x_shift), iptr,
                    src->linesize[i]);
                iptr += src->linesize[i];
2348 2349 2350 2351 2352
                optr += dst->linesize[i];
            }
        }

        if (padbottom || padright) {
Luca Barbato's avatar
Luca Barbato committed
2353 2354 2355 2356
            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));
2357 2358 2359 2360 2361
        }
    }
    return 0;
}

2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
void img_copy(AVPicture *dst, const AVPicture *src,
              int pix_fmt, int width, int height)
{
    av_picture_copy(dst, src, pix_fmt, width, height);
}

int img_crop(AVPicture *dst, const AVPicture *src,
              int pix_fmt, int top_band, int left_band)
{
    return av_picture_crop(dst, src, pix_fmt, top_band, left_band);
}

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)
{
    return av_picture_pad(dst, src, height, width, pix_fmt, padtop, padbottom, padleft, padright, color);
}
#endif

2383
#ifndef CONFIG_SWSCALER
2384 2385
/* XXX: always use linesize. Return -1 if not supported */
int img_convert(AVPicture *dst, int dst_pix_fmt,
2386
                const AVPicture *src, int src_pix_fmt,
2387
                int src_width, int src_height)
2388
{
2389
    static int initialized;
2390
    int i, ret, dst_width, dst_height, int_pix_fmt;
2391 2392
    const PixFmtInfo *src_pix, *dst_pix;
    const ConvertEntry *ce;
2393 2394 2395 2396 2397 2398 2399
    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;
2400

2401 2402
    if (!initialized) {
        initialized = 1;
2403 2404 2405
        img_convert_init();
    }

2406 2407
    dst_width = src_width;
    dst_height = src_height;
2408

2409 2410 2411
    dst_pix = &pix_fmt_info[dst_pix_fmt];
    src_pix = &pix_fmt_info[src_pix_fmt];
    if (src_pix_fmt == dst_pix_fmt) {
2412
        /* no conversion needed: just copy */
2413
        av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2414 2415 2416 2417 2418
        return 0;
    }

    ce = &convert_table[src_pix_fmt][dst_pix_fmt];
    if (ce->convert) {
2419
        /* specific conversion routine */
2420 2421 2422 2423 2424
        ce->convert(dst, src, dst_width, dst_height);
        return 0;
    }

    /* gray to YUV */
2425
    if (is_yuv_planar(dst_pix) &&
2426
        src_pix_fmt == PIX_FMT_GRAY8) {
2427 2428 2429
        int w, h, y;
        uint8_t *d;

2430
        if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2431
            ff_img_copy_plane(dst->data[0], dst->linesize[0],
2432 2433 2434
                     src->data[0], src->linesize[0],
                     dst_width, dst_height);
        } else {
2435 2436 2437 2438
            img_apply_table(dst->data[0], dst->linesize[0],
                            src->data[0], src->linesize[0],
                            dst_width, dst_height,
                            y_jpeg_to_ccir);
2439
        }
2440 2441 2442 2443 2444 2445 2446
        /* 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];
2447 2448
            for(y = 0; y< h; y++) {
                memset(d, 128, w);
2449 2450
                d += dst->linesize[i];
            }
2451
        }
2452 2453 2454 2455
        return 0;
    }

    /* YUV to gray */
2456
    if (is_yuv_planar(src_pix) &&
2457 2458
        dst_pix_fmt == PIX_FMT_GRAY8) {
        if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2459
            ff_img_copy_plane(dst->data[0], dst->linesize[0],
2460 2461 2462
                     src->data[0], src->linesize[0],
                     dst_width, dst_height);
        } else {
2463 2464 2465 2466
            img_apply_table(dst->data[0], dst->linesize[0],
                            src->data[0], src->linesize[0],
                            dst_width, dst_height,
                            y_ccir_to_jpeg);
2467
        }
2468 2469 2470
        return 0;
    }

2471 2472
    /* YUV to YUV planar */
    if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2473
        int x_shift, y_shift, w, h, xy_shift;
2474
        void (*resize_func)(uint8_t *dst, int dst_wrap,
2475
                            const uint8_t *src, int src_wrap,
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
                            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);
2492 2493 2494 2495 2496
        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:
2497
            resize_func = ff_img_copy_plane;
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
            break;
        case 0x10:
            resize_func = shrink21;
            break;
        case 0x20:
            resize_func = shrink41;
            break;
        case 0x01:
            resize_func = shrink12;
            break;
        case 0x11:
2509
            resize_func = ff_shrink22;
2510 2511
            break;
        case 0x22:
2512
            resize_func = ff_shrink44;
2513 2514 2515 2516
            break;
        case 0xf0:
            resize_func = grow21;
            break;
2517 2518 2519
        case 0x0f:
            resize_func = grow12;
            break;
2520 2521 2522 2523
        case 0xe0:
            resize_func = grow41;
            break;
        case 0xff:
2524
            resize_func = grow22;
2525 2526 2527 2528 2529
            break;
        case 0xee:
            resize_func = grow44;
            break;
        case 0xf1:
2530
            resize_func = conv411;
2531 2532
            break;
        default:
2533
            /* currently not handled */
2534
            goto no_chroma_filter;
2535
        }
2536

2537
        ff_img_copy_plane(dst->data[0], dst->linesize[0],
2538 2539
                       src->data[0], src->linesize[0],
                       dst_width, dst_height);
2540

2541
        for(i = 1;i <= 2; i++)
2542 2543
            resize_func(dst->data[i], dst->linesize[i],
                        src->data[i], src->linesize[i],
Michael Niedermayer's avatar
Michael Niedermayer committed
2544
                        dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
        /* 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],
2564
                                dst_width>>dst_pix->x_chroma_shift,
2565 2566 2567 2568
                                dst_height>>dst_pix->y_chroma_shift,
                                c_table);
        }
        return 0;
2569
    }
2570
 no_chroma_filter:
2571

2572
    /* try to use an intermediate format */
2573 2574
    if (src_pix_fmt == PIX_FMT_YUYV422 ||
        dst_pix_fmt == PIX_FMT_YUYV422) {
2575 2576
        /* specific case: convert to YUV422P first */
        int_pix_fmt = PIX_FMT_YUV422P;
2577 2578 2579 2580
    } 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;
2581 2582
    } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
        dst_pix_fmt == PIX_FMT_UYYVYY411) {
2583 2584
        /* specific case: convert to YUV411P first */
        int_pix_fmt = PIX_FMT_YUV411P;
2585
    } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2586
                src_pix_fmt != PIX_FMT_GRAY8) ||
2587 2588 2589
               (dst_pix->color_type == FF_COLOR_GRAY &&
                dst_pix_fmt != PIX_FMT_GRAY8)) {
        /* gray8 is the normalized format */
2590
        int_pix_fmt = PIX_FMT_GRAY8;
2591
    } else if ((is_yuv_planar(src_pix) &&
2592 2593 2594 2595 2596 2597 2598
                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;
2599
    } else if ((is_yuv_planar(dst_pix) &&
2600 2601 2602 2603 2604 2605 2606
                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;
2607
    } else {
2608 2609
        /* the two formats are rgb or gray8 or yuv[j]444p */
        if (src_pix->is_alpha && dst_pix->is_alpha)
2610
            int_pix_fmt = PIX_FMT_RGB32;
2611 2612
        else
            int_pix_fmt = PIX_FMT_RGB24;
2613
    }
2614 2615
    if (src_pix_fmt == int_pix_fmt)
        return -1;
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628
    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;
2629
}
2630
#endif
2631

2632
/* NOTE: we scan all the pixels to have an exact information */
2633
static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2634 2635 2636 2637 2638
{
    const unsigned char *p;
    int src_wrap, ret, x, y;
    unsigned int a;
    uint32_t *palette = (uint32_t *)src->data[1];
2639

2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657
    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;
}

2658
int img_get_alpha_info(const AVPicture *src,
2659
                       int pix_fmt, int width, int height)
2660
{
2661
    const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2662 2663 2664 2665 2666 2667 2668
    int ret;

    pf = &pix_fmt_info[pix_fmt];
    /* no alpha can be represented in format */
    if (!pf->is_alpha)
        return 0;
    switch(pix_fmt) {
2669
    case PIX_FMT_RGB32:
2670
        ret = get_alpha_info_rgb32(src, width, height);
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
        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
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729

#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

2730
/* filter parameters: [-1 4 2 4 -1] // 8 */
2731
static void deinterlace_line(uint8_t *dst,
2732 2733 2734 2735
                             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)
2736
{
Fred's avatar
Fred committed
2737
#ifndef HAVE_MMX
2738
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2739 2740 2741
    int sum;

    for(;size > 0;size--) {
Fred's avatar
Fred committed
2742 2743 2744 2745 2746
        sum = -lum_m4[0];
        sum += lum_m3[0] << 2;
        sum += lum_m2[0] << 1;
        sum += lum_m1[0] << 2;
        sum += -lum[0];
2747
        dst[0] = cm[(sum + 4) >> 3];
Fred's avatar
Fred committed
2748 2749 2750 2751 2752
        lum_m4++;
        lum_m3++;
        lum_m2++;
        lum_m1++;
        lum++;
2753 2754
        dst++;
    }
Fred's avatar
Fred committed
2755 2756
#else

Michael Niedermayer's avatar
Michael Niedermayer committed
2757 2758 2759 2760 2761 2762 2763 2764 2765
    {
        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
2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776
    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
}
2777
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
2778 2779 2780
                             int size)
{
#ifndef HAVE_MMX
2781
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
Fred's avatar
Fred committed
2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799
    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
2800 2801 2802 2803 2804 2805 2806 2807 2808
    {
        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
2809 2810 2811 2812 2813 2814 2815 2816 2817
    for (;size > 3; size-=4) {
        DEINT_INPLACE_LINE_LUM
        lum_m4+=4;
        lum_m3+=4;
        lum_m2+=4;
        lum_m1+=4;
        lum+=4;
    }
#endif
2818 2819 2820 2821 2822
}

/* 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. */
2823
static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2824
                                    const uint8_t *src1, int src_wrap,
Fred's avatar
Fred committed
2825
                                    int width, int height)
2826
{
2827
    const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
Fred's avatar
Fred committed
2828 2829 2830 2831 2832 2833 2834 2835 2836
    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);
2837
        dst += dst_wrap;
Fred's avatar
Fred committed
2838 2839 2840 2841 2842 2843
        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;
2844 2845
        dst += dst_wrap;
    }
Fred's avatar
Fred committed
2846 2847 2848 2849 2850 2851
    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);
}

2852
static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2853
                                             int width, int height)
Fred's avatar
Fred committed
2854
{
2855
    uint8_t *src_m1, *src_0, *src_p1, *src_p2;
Fred's avatar
Fred committed
2856
    int y;
2857 2858
    uint8_t *buf;
    buf = (uint8_t*)av_malloc(width);
Fred's avatar
Fred committed
2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873

    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);
2874
    av_free(buf);
2875 2876
}

2877
int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
Fabrice Bellard's avatar
Fabrice Bellard committed
2878 2879
                          int pix_fmt, int width, int height)
{
2880 2881 2882 2883
    int i;

    if (pix_fmt != PIX_FMT_YUV420P &&
        pix_fmt != PIX_FMT_YUV422P &&
2884
        pix_fmt != PIX_FMT_YUV444P &&
2885 2886
        pix_fmt != PIX_FMT_YUV411P &&
        pix_fmt != PIX_FMT_GRAY8)
2887
        return -1;
Fred's avatar
Fred committed
2888
    if ((width & 3) != 0 || (height & 3) != 0)
2889
        return -1;
Fred's avatar
Fred committed
2890

2891 2892 2893 2894 2895 2896 2897 2898 2899 2900
    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;
2901 2902 2903
            case PIX_FMT_YUV411P:
                width >>= 2;
                break;
2904 2905 2906
            default:
                break;
            }
2907 2908 2909
            if (pix_fmt == PIX_FMT_GRAY8) {
                break;
            }
2910
        }
Fred's avatar
Fred committed
2911
        if (src == dst) {
2912
            deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2913
                                 width, height);
Fred's avatar
Fred committed
2914 2915 2916 2917 2918
        } 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
2919
    }
2920
    emms_c();
2921
    return 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
2922
}
2923