Statistics
| Branch: | Revision:

ffmpeg / libavcodec / imgconvert.c @ a80f74c5

History | View | Annotate | Download (31 KB)

1
/*
2
 * Misc image conversion routines
3
 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
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
20
 */
21

    
22
/**
23
 * @file
24
 * misc image conversion routines
25
 */
26

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

    
33
#include "avcodec.h"
34
#include "dsputil.h"
35
#include "internal.h"
36
#include "imgconvert.h"
37
#include "libavutil/colorspace.h"
38
#include "libavutil/pixdesc.h"
39
#include "libavutil/imgutils.h"
40

    
41
#if HAVE_MMX && HAVE_YASM
42
#include "x86/dsputil_mmx.h"
43
#endif
44

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

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

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

    
57
#if HAVE_MMX && HAVE_YASM
58
#define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
59
#define deinterlace_line         ff_deinterlace_line_mmx
60
#else
61
#define deinterlace_line_inplace deinterlace_line_inplace_c
62
#define deinterlace_line         deinterlace_line_c
63
#endif
64

    
65
typedef struct PixFmtInfo {
66
    uint8_t nb_channels;     /**< number of channels (including alpha) */
67
    uint8_t color_type;      /**< color type (see FF_COLOR_xxx constants) */
68
    uint8_t pixel_type;      /**< pixel storage type (see FF_PIXEL_xxx constants) */
69
    uint8_t is_alpha : 1;    /**< true if alpha can be specified */
70
    uint8_t depth;           /**< bit depth of the color components */
71
} PixFmtInfo;
72

    
73
/* this table gives more information about formats */
74
static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
75
    /* YUV formats */
76
    [PIX_FMT_YUV420P] = {
77
        .nb_channels = 3,
78
        .color_type = FF_COLOR_YUV,
79
        .pixel_type = FF_PIXEL_PLANAR,
80
        .depth = 8,
81
    },
82
    [PIX_FMT_YUV422P] = {
83
        .nb_channels = 3,
84
        .color_type = FF_COLOR_YUV,
85
        .pixel_type = FF_PIXEL_PLANAR,
86
        .depth = 8,
87
    },
88
    [PIX_FMT_YUV444P] = {
89
        .nb_channels = 3,
90
        .color_type = FF_COLOR_YUV,
91
        .pixel_type = FF_PIXEL_PLANAR,
92
        .depth = 8,
93
    },
94
    [PIX_FMT_YUYV422] = {
95
        .nb_channels = 1,
96
        .color_type = FF_COLOR_YUV,
97
        .pixel_type = FF_PIXEL_PACKED,
98
        .depth = 8,
99
    },
100
    [PIX_FMT_UYVY422] = {
101
        .nb_channels = 1,
102
        .color_type = FF_COLOR_YUV,
103
        .pixel_type = FF_PIXEL_PACKED,
104
        .depth = 8,
105
    },
106
    [PIX_FMT_YUV410P] = {
107
        .nb_channels = 3,
108
        .color_type = FF_COLOR_YUV,
109
        .pixel_type = FF_PIXEL_PLANAR,
110
        .depth = 8,
111
    },
112
    [PIX_FMT_YUV411P] = {
113
        .nb_channels = 3,
114
        .color_type = FF_COLOR_YUV,
115
        .pixel_type = FF_PIXEL_PLANAR,
116
        .depth = 8,
117
    },
118
    [PIX_FMT_YUV440P] = {
119
        .nb_channels = 3,
120
        .color_type = FF_COLOR_YUV,
121
        .pixel_type = FF_PIXEL_PLANAR,
122
        .depth = 8,
123
    },
124
    [PIX_FMT_YUV420P16LE] = {
125
        .nb_channels = 3,
126
        .color_type = FF_COLOR_YUV,
127
        .pixel_type = FF_PIXEL_PLANAR,
128
        .depth = 16,
129
    },
130
    [PIX_FMT_YUV422P16LE] = {
131
        .nb_channels = 3,
132
        .color_type = FF_COLOR_YUV,
133
        .pixel_type = FF_PIXEL_PLANAR,
134
        .depth = 16,
135
    },
136
    [PIX_FMT_YUV444P16LE] = {
137
        .nb_channels = 3,
138
        .color_type = FF_COLOR_YUV,
139
        .pixel_type = FF_PIXEL_PLANAR,
140
        .depth = 16,
141
    },
142
    [PIX_FMT_YUV420P16BE] = {
143
        .nb_channels = 3,
144
        .color_type = FF_COLOR_YUV,
145
        .pixel_type = FF_PIXEL_PLANAR,
146
        .depth = 16,
147
    },
148
    [PIX_FMT_YUV422P16BE] = {
149
        .nb_channels = 3,
150
        .color_type = FF_COLOR_YUV,
151
        .pixel_type = FF_PIXEL_PLANAR,
152
        .depth = 16,
153
    },
154
    [PIX_FMT_YUV444P16BE] = {
155
        .nb_channels = 3,
156
        .color_type = FF_COLOR_YUV,
157
        .pixel_type = FF_PIXEL_PLANAR,
158
        .depth = 16,
159
    },
160

    
161

    
162
    /* YUV formats with alpha plane */
163
    [PIX_FMT_YUVA420P] = {
164
        .nb_channels = 4,
165
        .color_type = FF_COLOR_YUV,
166
        .pixel_type = FF_PIXEL_PLANAR,
167
        .depth = 8,
168
    },
169

    
170
    /* JPEG YUV */
171
    [PIX_FMT_YUVJ420P] = {
172
        .nb_channels = 3,
173
        .color_type = FF_COLOR_YUV_JPEG,
174
        .pixel_type = FF_PIXEL_PLANAR,
175
        .depth = 8,
176
    },
177
    [PIX_FMT_YUVJ422P] = {
178
        .nb_channels = 3,
179
        .color_type = FF_COLOR_YUV_JPEG,
180
        .pixel_type = FF_PIXEL_PLANAR,
181
        .depth = 8,
182
    },
183
    [PIX_FMT_YUVJ444P] = {
184
        .nb_channels = 3,
185
        .color_type = FF_COLOR_YUV_JPEG,
186
        .pixel_type = FF_PIXEL_PLANAR,
187
        .depth = 8,
188
    },
189
    [PIX_FMT_YUVJ440P] = {
190
        .nb_channels = 3,
191
        .color_type = FF_COLOR_YUV_JPEG,
192
        .pixel_type = FF_PIXEL_PLANAR,
193
        .depth = 8,
194
    },
195

    
196
    /* RGB formats */
197
    [PIX_FMT_RGB24] = {
198
        .nb_channels = 3,
199
        .color_type = FF_COLOR_RGB,
200
        .pixel_type = FF_PIXEL_PACKED,
201
        .depth = 8,
202
    },
203
    [PIX_FMT_BGR24] = {
204
        .nb_channels = 3,
205
        .color_type = FF_COLOR_RGB,
206
        .pixel_type = FF_PIXEL_PACKED,
207
        .depth = 8,
208
    },
209
    [PIX_FMT_ARGB] = {
210
        .nb_channels = 4, .is_alpha = 1,
211
        .color_type = FF_COLOR_RGB,
212
        .pixel_type = FF_PIXEL_PACKED,
213
        .depth = 8,
214
    },
215
    [PIX_FMT_RGB48BE] = {
216
        .nb_channels = 3,
217
        .color_type = FF_COLOR_RGB,
218
        .pixel_type = FF_PIXEL_PACKED,
219
        .depth = 16,
220
    },
221
    [PIX_FMT_RGB48LE] = {
222
        .nb_channels = 3,
223
        .color_type = FF_COLOR_RGB,
224
        .pixel_type = FF_PIXEL_PACKED,
225
        .depth = 16,
226
    },
227
    [PIX_FMT_RGB565BE] = {
228
        .nb_channels = 3,
229
        .color_type = FF_COLOR_RGB,
230
        .pixel_type = FF_PIXEL_PACKED,
231
        .depth = 5,
232
    },
233
    [PIX_FMT_RGB565LE] = {
234
        .nb_channels = 3,
235
        .color_type = FF_COLOR_RGB,
236
        .pixel_type = FF_PIXEL_PACKED,
237
        .depth = 5,
238
    },
239
    [PIX_FMT_RGB555BE] = {
240
        .nb_channels = 3,
241
        .color_type = FF_COLOR_RGB,
242
        .pixel_type = FF_PIXEL_PACKED,
243
        .depth = 5,
244
    },
245
    [PIX_FMT_RGB555LE] = {
246
        .nb_channels = 3,
247
        .color_type = FF_COLOR_RGB,
248
        .pixel_type = FF_PIXEL_PACKED,
249
        .depth = 5,
250
    },
251
    [PIX_FMT_RGB444BE] = {
252
        .nb_channels = 3,
253
        .color_type = FF_COLOR_RGB,
254
        .pixel_type = FF_PIXEL_PACKED,
255
        .depth = 4,
256
    },
257
    [PIX_FMT_RGB444LE] = {
258
        .nb_channels = 3,
259
        .color_type = FF_COLOR_RGB,
260
        .pixel_type = FF_PIXEL_PACKED,
261
        .depth = 4,
262
    },
263

    
264
    /* gray / mono formats */
265
    [PIX_FMT_GRAY16BE] = {
266
        .nb_channels = 1,
267
        .color_type = FF_COLOR_GRAY,
268
        .pixel_type = FF_PIXEL_PLANAR,
269
        .depth = 16,
270
    },
271
    [PIX_FMT_GRAY16LE] = {
272
        .nb_channels = 1,
273
        .color_type = FF_COLOR_GRAY,
274
        .pixel_type = FF_PIXEL_PLANAR,
275
        .depth = 16,
276
    },
277
    [PIX_FMT_GRAY8] = {
278
        .nb_channels = 1,
279
        .color_type = FF_COLOR_GRAY,
280
        .pixel_type = FF_PIXEL_PLANAR,
281
        .depth = 8,
282
    },
283
    [PIX_FMT_MONOWHITE] = {
284
        .nb_channels = 1,
285
        .color_type = FF_COLOR_GRAY,
286
        .pixel_type = FF_PIXEL_PLANAR,
287
        .depth = 1,
288
    },
289
    [PIX_FMT_MONOBLACK] = {
290
        .nb_channels = 1,
291
        .color_type = FF_COLOR_GRAY,
292
        .pixel_type = FF_PIXEL_PLANAR,
293
        .depth = 1,
294
    },
295

    
296
    /* paletted formats */
297
    [PIX_FMT_PAL8] = {
298
        .nb_channels = 4, .is_alpha = 1,
299
        .color_type = FF_COLOR_RGB,
300
        .pixel_type = FF_PIXEL_PALETTE,
301
        .depth = 8,
302
    },
303
    [PIX_FMT_UYYVYY411] = {
304
        .nb_channels = 1,
305
        .color_type = FF_COLOR_YUV,
306
        .pixel_type = FF_PIXEL_PACKED,
307
        .depth = 8,
308
    },
309
    [PIX_FMT_ABGR] = {
310
        .nb_channels = 4, .is_alpha = 1,
311
        .color_type = FF_COLOR_RGB,
312
        .pixel_type = FF_PIXEL_PACKED,
313
        .depth = 8,
314
    },
315
    [PIX_FMT_BGR565BE] = {
316
        .nb_channels = 3,
317
        .color_type = FF_COLOR_RGB,
318
        .pixel_type = FF_PIXEL_PACKED,
319
        .depth = 5,
320
    },
321
    [PIX_FMT_BGR565LE] = {
322
        .nb_channels = 3,
323
        .color_type = FF_COLOR_RGB,
324
        .pixel_type = FF_PIXEL_PACKED,
325
        .depth = 5,
326
    },
327
    [PIX_FMT_BGR555BE] = {
328
        .nb_channels = 3,
329
        .color_type = FF_COLOR_RGB,
330
        .pixel_type = FF_PIXEL_PACKED,
331
        .depth = 5,
332
    },
333
    [PIX_FMT_BGR555LE] = {
334
        .nb_channels = 3,
335
        .color_type = FF_COLOR_RGB,
336
        .pixel_type = FF_PIXEL_PACKED,
337
        .depth = 5,
338
    },
339
    [PIX_FMT_BGR444BE] = {
340
        .nb_channels = 3,
341
        .color_type = FF_COLOR_RGB,
342
        .pixel_type = FF_PIXEL_PACKED,
343
        .depth = 4,
344
    },
345
    [PIX_FMT_BGR444LE] = {
346
        .nb_channels = 3,
347
        .color_type = FF_COLOR_RGB,
348
        .pixel_type = FF_PIXEL_PACKED,
349
        .depth = 4,
350
    },
351
    [PIX_FMT_RGB8] = {
352
        .nb_channels = 1,
353
        .color_type = FF_COLOR_RGB,
354
        .pixel_type = FF_PIXEL_PACKED,
355
        .depth = 8,
356
    },
357
    [PIX_FMT_RGB4] = {
358
        .nb_channels = 1,
359
        .color_type = FF_COLOR_RGB,
360
        .pixel_type = FF_PIXEL_PACKED,
361
        .depth = 4,
362
    },
363
    [PIX_FMT_RGB4_BYTE] = {
364
        .nb_channels = 1,
365
        .color_type = FF_COLOR_RGB,
366
        .pixel_type = FF_PIXEL_PACKED,
367
        .depth = 8,
368
    },
369
    [PIX_FMT_BGR8] = {
370
        .nb_channels = 1,
371
        .color_type = FF_COLOR_RGB,
372
        .pixel_type = FF_PIXEL_PACKED,
373
        .depth = 8,
374
    },
375
    [PIX_FMT_BGR4] = {
376
        .nb_channels = 1,
377
        .color_type = FF_COLOR_RGB,
378
        .pixel_type = FF_PIXEL_PACKED,
379
        .depth = 4,
380
    },
381
    [PIX_FMT_BGR4_BYTE] = {
382
        .nb_channels = 1,
383
        .color_type = FF_COLOR_RGB,
384
        .pixel_type = FF_PIXEL_PACKED,
385
        .depth = 8,
386
    },
387
    [PIX_FMT_NV12] = {
388
        .nb_channels = 2,
389
        .color_type = FF_COLOR_YUV,
390
        .pixel_type = FF_PIXEL_PLANAR,
391
        .depth = 8,
392
    },
393
    [PIX_FMT_NV21] = {
394
        .nb_channels = 2,
395
        .color_type = FF_COLOR_YUV,
396
        .pixel_type = FF_PIXEL_PLANAR,
397
        .depth = 8,
398
    },
399

    
400
    [PIX_FMT_BGRA] = {
401
        .nb_channels = 4, .is_alpha = 1,
402
        .color_type = FF_COLOR_RGB,
403
        .pixel_type = FF_PIXEL_PACKED,
404
        .depth = 8,
405
    },
406
    [PIX_FMT_RGBA] = {
407
        .nb_channels = 4, .is_alpha = 1,
408
        .color_type = FF_COLOR_RGB,
409
        .pixel_type = FF_PIXEL_PACKED,
410
        .depth = 8,
411
    },
412
};
413

    
414
void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
415
{
416
    *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
417
    *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
418
}
419

    
420
const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
421
{
422
    if ((unsigned)pix_fmt >= PIX_FMT_NB)
423
        return NULL;
424
    else
425
        return av_pix_fmt_descriptors[pix_fmt].name;
426
}
427

    
428
int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
429
{
430
    return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
431
}
432

    
433
int avpicture_fill(AVPicture *picture, uint8_t *ptr,
434
                   enum PixelFormat pix_fmt, int width, int height)
435
{
436
    int ret;
437

    
438
    if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
439
        return ret;
440

    
441
    if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
442
        return ret;
443

    
444
    return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
445
}
446

    
447
int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
448
                     unsigned char *dest, int dest_size)
449
{
450
    int i, j, nb_planes = 0, linesizes[4];
451
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
452
    int size = avpicture_get_size(pix_fmt, width, height);
453

    
454
    if (size > dest_size || size < 0)
455
        return AVERROR(EINVAL);
456

    
457
    for (i = 0; i < desc->nb_components; i++)
458
        nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
459
    nb_planes++;
460

    
461
    av_image_fill_linesizes(linesizes, pix_fmt, width);
462
    for (i = 0; i < nb_planes; i++) {
463
        int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
464
        const unsigned char *s = src->data[i];
465
        h = (height + (1 << shift) - 1) >> shift;
466

    
467
        for (j = 0; j < h; j++) {
468
            memcpy(dest, s, linesizes[i]);
469
            dest += linesizes[i];
470
            s += src->linesize[i];
471
        }
472
    }
473

    
474
    if (desc->flags & PIX_FMT_PAL)
475
        memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
476

    
477
    return size;
478
}
479

    
480
int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
481
{
482
    AVPicture dummy_pict;
483
    if(av_image_check_size(width, height, 0, NULL))
484
        return -1;
485
    switch (pix_fmt) {
486
    case PIX_FMT_RGB8:
487
    case PIX_FMT_BGR8:
488
    case PIX_FMT_RGB4_BYTE:
489
    case PIX_FMT_BGR4_BYTE:
490
    case PIX_FMT_GRAY8:
491
        // do not include palette for these pseudo-paletted formats
492
        return width * height;
493
    }
494
    return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
495
}
496

    
497
int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
498
                             int has_alpha)
499
{
500
    const PixFmtInfo *pf, *ps;
501
    const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
502
    const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
503
    int loss;
504

    
505
    ps = &pix_fmt_info[src_pix_fmt];
506

    
507
    /* compute loss */
508
    loss = 0;
509
    pf = &pix_fmt_info[dst_pix_fmt];
510
    if (pf->depth < ps->depth ||
511
        ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
512
          dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
513
         (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
514
          src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
515
        loss |= FF_LOSS_DEPTH;
516
    if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
517
        dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
518
        loss |= FF_LOSS_RESOLUTION;
519
    switch(pf->color_type) {
520
    case FF_COLOR_RGB:
521
        if (ps->color_type != FF_COLOR_RGB &&
522
            ps->color_type != FF_COLOR_GRAY)
523
            loss |= FF_LOSS_COLORSPACE;
524
        break;
525
    case FF_COLOR_GRAY:
526
        if (ps->color_type != FF_COLOR_GRAY)
527
            loss |= FF_LOSS_COLORSPACE;
528
        break;
529
    case FF_COLOR_YUV:
530
        if (ps->color_type != FF_COLOR_YUV)
531
            loss |= FF_LOSS_COLORSPACE;
532
        break;
533
    case FF_COLOR_YUV_JPEG:
534
        if (ps->color_type != FF_COLOR_YUV_JPEG &&
535
            ps->color_type != FF_COLOR_YUV &&
536
            ps->color_type != FF_COLOR_GRAY)
537
            loss |= FF_LOSS_COLORSPACE;
538
        break;
539
    default:
540
        /* fail safe test */
541
        if (ps->color_type != pf->color_type)
542
            loss |= FF_LOSS_COLORSPACE;
543
        break;
544
    }
545
    if (pf->color_type == FF_COLOR_GRAY &&
546
        ps->color_type != FF_COLOR_GRAY)
547
        loss |= FF_LOSS_CHROMA;
548
    if (!pf->is_alpha && (ps->is_alpha && has_alpha))
549
        loss |= FF_LOSS_ALPHA;
550
    if (pf->pixel_type == FF_PIXEL_PALETTE &&
551
        (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
552
        loss |= FF_LOSS_COLORQUANT;
553
    return loss;
554
}
555

    
556
static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
557
{
558
    int bits;
559
    const PixFmtInfo *pf;
560
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
561

    
562
    pf = &pix_fmt_info[pix_fmt];
563
    switch(pf->pixel_type) {
564
    case FF_PIXEL_PACKED:
565
        switch(pix_fmt) {
566
        case PIX_FMT_YUYV422:
567
        case PIX_FMT_UYVY422:
568
        case PIX_FMT_RGB565BE:
569
        case PIX_FMT_RGB565LE:
570
        case PIX_FMT_RGB555BE:
571
        case PIX_FMT_RGB555LE:
572
        case PIX_FMT_RGB444BE:
573
        case PIX_FMT_RGB444LE:
574
        case PIX_FMT_BGR565BE:
575
        case PIX_FMT_BGR565LE:
576
        case PIX_FMT_BGR555BE:
577
        case PIX_FMT_BGR555LE:
578
        case PIX_FMT_BGR444BE:
579
        case PIX_FMT_BGR444LE:
580
            bits = 16;
581
            break;
582
        case PIX_FMT_UYYVYY411:
583
            bits = 12;
584
            break;
585
        default:
586
            bits = pf->depth * pf->nb_channels;
587
            break;
588
        }
589
        break;
590
    case FF_PIXEL_PLANAR:
591
        if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
592
            bits = pf->depth * pf->nb_channels;
593
        } else {
594
            bits = pf->depth + ((2 * pf->depth) >>
595
                                (desc->log2_chroma_w + desc->log2_chroma_h));
596
        }
597
        break;
598
    case FF_PIXEL_PALETTE:
599
        bits = 8;
600
        break;
601
    default:
602
        bits = -1;
603
        break;
604
    }
605
    return bits;
606
}
607

    
608
static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
609
                                      enum PixelFormat src_pix_fmt,
610
                                      int has_alpha,
611
                                      int loss_mask)
612
{
613
    int dist, i, loss, min_dist;
614
    enum PixelFormat dst_pix_fmt;
615

    
616
    /* find exact color match with smallest size */
617
    dst_pix_fmt = PIX_FMT_NONE;
618
    min_dist = 0x7fffffff;
619
    for(i = 0;i < PIX_FMT_NB; i++) {
620
        if (pix_fmt_mask & (1ULL << i)) {
621
            loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
622
            if (loss == 0) {
623
                dist = avg_bits_per_pixel(i);
624
                if (dist < min_dist) {
625
                    min_dist = dist;
626
                    dst_pix_fmt = i;
627
                }
628
            }
629
        }
630
    }
631
    return dst_pix_fmt;
632
}
633

    
634
enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
635
                              int has_alpha, int *loss_ptr)
636
{
637
    enum PixelFormat dst_pix_fmt;
638
    int loss_mask, i;
639
    static const int loss_mask_order[] = {
640
        ~0, /* no loss first */
641
        ~FF_LOSS_ALPHA,
642
        ~FF_LOSS_RESOLUTION,
643
        ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
644
        ~FF_LOSS_COLORQUANT,
645
        ~FF_LOSS_DEPTH,
646
        0,
647
    };
648

    
649
    /* try with successive loss */
650
    i = 0;
651
    for(;;) {
652
        loss_mask = loss_mask_order[i++];
653
        dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
654
                                                 has_alpha, loss_mask);
655
        if (dst_pix_fmt >= 0)
656
            goto found;
657
        if (loss_mask == 0)
658
            break;
659
    }
660
    return PIX_FMT_NONE;
661
 found:
662
    if (loss_ptr)
663
        *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
664
    return dst_pix_fmt;
665
}
666

    
667
void av_picture_copy(AVPicture *dst, const AVPicture *src,
668
                     enum PixelFormat pix_fmt, int width, int height)
669
{
670
    av_image_copy(dst->data, dst->linesize, src->data,
671
                  src->linesize, pix_fmt, width, height);
672
}
673

    
674
/* 2x2 -> 1x1 */
675
void ff_shrink22(uint8_t *dst, int dst_wrap,
676
                     const uint8_t *src, int src_wrap,
677
                     int width, int height)
678
{
679
    int w;
680
    const uint8_t *s1, *s2;
681
    uint8_t *d;
682

    
683
    for(;height > 0; height--) {
684
        s1 = src;
685
        s2 = s1 + src_wrap;
686
        d = dst;
687
        for(w = width;w >= 4; w-=4) {
688
            d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
689
            d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
690
            d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
691
            d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
692
            s1 += 8;
693
            s2 += 8;
694
            d += 4;
695
        }
696
        for(;w > 0; w--) {
697
            d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
698
            s1 += 2;
699
            s2 += 2;
700
            d++;
701
        }
702
        src += 2 * src_wrap;
703
        dst += dst_wrap;
704
    }
705
}
706

    
707
/* 4x4 -> 1x1 */
708
void ff_shrink44(uint8_t *dst, int dst_wrap,
709
                     const uint8_t *src, int src_wrap,
710
                     int width, int height)
711
{
712
    int w;
713
    const uint8_t *s1, *s2, *s3, *s4;
714
    uint8_t *d;
715

    
716
    for(;height > 0; height--) {
717
        s1 = src;
718
        s2 = s1 + src_wrap;
719
        s3 = s2 + src_wrap;
720
        s4 = s3 + src_wrap;
721
        d = dst;
722
        for(w = width;w > 0; w--) {
723
            d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
724
                    s2[0] + s2[1] + s2[2] + s2[3] +
725
                    s3[0] + s3[1] + s3[2] + s3[3] +
726
                    s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
727
            s1 += 4;
728
            s2 += 4;
729
            s3 += 4;
730
            s4 += 4;
731
            d++;
732
        }
733
        src += 4 * src_wrap;
734
        dst += dst_wrap;
735
    }
736
}
737

    
738
/* 8x8 -> 1x1 */
739
void ff_shrink88(uint8_t *dst, int dst_wrap,
740
                     const uint8_t *src, int src_wrap,
741
                     int width, int height)
742
{
743
    int w, i;
744

    
745
    for(;height > 0; height--) {
746
        for(w = width;w > 0; w--) {
747
            int tmp=0;
748
            for(i=0; i<8; i++){
749
                tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
750
                src += src_wrap;
751
            }
752
            *(dst++) = (tmp + 32)>>6;
753
            src += 8 - 8*src_wrap;
754
        }
755
        src += 8*src_wrap - 8*width;
756
        dst += dst_wrap - width;
757
    }
758
}
759

    
760

    
761
int avpicture_alloc(AVPicture *picture,
762
                    enum PixelFormat pix_fmt, int width, int height)
763
{
764
    int ret;
765

    
766
    if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
767
        memset(picture, 0, sizeof(AVPicture));
768
        return ret;
769
    }
770

    
771
    return 0;
772
}
773

    
774
void avpicture_free(AVPicture *picture)
775
{
776
    av_free(picture->data[0]);
777
}
778

    
779
/* return true if yuv planar */
780
static inline int is_yuv_planar(const PixFmtInfo *ps)
781
{
782
    return (ps->color_type == FF_COLOR_YUV ||
783
            ps->color_type == FF_COLOR_YUV_JPEG) &&
784
        ps->pixel_type == FF_PIXEL_PLANAR;
785
}
786

    
787
int av_picture_crop(AVPicture *dst, const AVPicture *src,
788
                    enum PixelFormat pix_fmt, int top_band, int left_band)
789
{
790
    int y_shift;
791
    int x_shift;
792

    
793
    if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
794
        return -1;
795

    
796
    y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
797
    x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
798

    
799
    if (is_yuv_planar(&pix_fmt_info[pix_fmt])) {
800
    dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
801
    dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
802
    dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
803
    } else{
804
        if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
805
            return -1;
806
        if(left_band) //FIXME add support for this too
807
            return -1;
808
        dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
809
    }
810

    
811
    dst->linesize[0] = src->linesize[0];
812
    dst->linesize[1] = src->linesize[1];
813
    dst->linesize[2] = src->linesize[2];
814
    return 0;
815
}
816

    
817
int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
818
                   enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
819
            int *color)
820
{
821
    uint8_t *optr;
822
    int y_shift;
823
    int x_shift;
824
    int yheight;
825
    int i, y;
826

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

    
830
    for (i = 0; i < 3; i++) {
831
        x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
832
        y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
833

    
834
        if (padtop || padleft) {
835
            memset(dst->data[i], color[i],
836
                dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
837
        }
838

    
839
        if (padleft || padright) {
840
            optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
841
                (dst->linesize[i] - (padright >> x_shift));
842
            yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
843
            for (y = 0; y < yheight; y++) {
844
                memset(optr, color[i], (padleft + padright) >> x_shift);
845
                optr += dst->linesize[i];
846
            }
847
        }
848

    
849
        if (src) { /* first line */
850
            uint8_t *iptr = src->data[i];
851
            optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
852
                    (padleft >> x_shift);
853
            memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
854
            iptr += src->linesize[i];
855
            optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
856
                (dst->linesize[i] - (padright >> x_shift));
857
            yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
858
            for (y = 0; y < yheight; y++) {
859
                memset(optr, color[i], (padleft + padright) >> x_shift);
860
                memcpy(optr + ((padleft + padright) >> x_shift), iptr,
861
                       (width - padleft - padright) >> x_shift);
862
                iptr += src->linesize[i];
863
                optr += dst->linesize[i];
864
            }
865
        }
866

    
867
        if (padbottom || padright) {
868
            optr = dst->data[i] + dst->linesize[i] *
869
                ((height - padbottom) >> y_shift) - (padright >> x_shift);
870
            memset(optr, color[i],dst->linesize[i] *
871
                (padbottom >> y_shift) + (padright >> x_shift));
872
        }
873
    }
874
    return 0;
875
}
876

    
877
/* NOTE: we scan all the pixels to have an exact information */
878
static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
879
{
880
    const unsigned char *p;
881
    int src_wrap, ret, x, y;
882
    unsigned int a;
883
    uint32_t *palette = (uint32_t *)src->data[1];
884

    
885
    p = src->data[0];
886
    src_wrap = src->linesize[0] - width;
887
    ret = 0;
888
    for(y=0;y<height;y++) {
889
        for(x=0;x<width;x++) {
890
            a = palette[p[0]] >> 24;
891
            if (a == 0x00) {
892
                ret |= FF_ALPHA_TRANSP;
893
            } else if (a != 0xff) {
894
                ret |= FF_ALPHA_SEMI_TRANSP;
895
            }
896
            p++;
897
        }
898
        p += src_wrap;
899
    }
900
    return ret;
901
}
902

    
903
int img_get_alpha_info(const AVPicture *src,
904
                       enum PixelFormat pix_fmt, int width, int height)
905
{
906
    const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
907
    int ret;
908

    
909
    /* no alpha can be represented in format */
910
    if (!pf->is_alpha)
911
        return 0;
912
    switch(pix_fmt) {
913
    case PIX_FMT_PAL8:
914
        ret = get_alpha_info_pal8(src, width, height);
915
        break;
916
    default:
917
        /* we do not know, so everything is indicated */
918
        ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
919
        break;
920
    }
921
    return ret;
922
}
923

    
924
#if !(HAVE_MMX && HAVE_YASM)
925
/* filter parameters: [-1 4 2 4 -1] // 8 */
926
static void deinterlace_line_c(uint8_t *dst,
927
                             const uint8_t *lum_m4, const uint8_t *lum_m3,
928
                             const uint8_t *lum_m2, const uint8_t *lum_m1,
929
                             const uint8_t *lum,
930
                             int size)
931
{
932
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
933
    int sum;
934

    
935
    for(;size > 0;size--) {
936
        sum = -lum_m4[0];
937
        sum += lum_m3[0] << 2;
938
        sum += lum_m2[0] << 1;
939
        sum += lum_m1[0] << 2;
940
        sum += -lum[0];
941
        dst[0] = cm[(sum + 4) >> 3];
942
        lum_m4++;
943
        lum_m3++;
944
        lum_m2++;
945
        lum_m1++;
946
        lum++;
947
        dst++;
948
    }
949
}
950

    
951
static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
952
                                       uint8_t *lum_m2, uint8_t *lum_m1,
953
                                       uint8_t *lum, int size)
954
{
955
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
956
    int sum;
957

    
958
    for(;size > 0;size--) {
959
        sum = -lum_m4[0];
960
        sum += lum_m3[0] << 2;
961
        sum += lum_m2[0] << 1;
962
        lum_m4[0]=lum_m2[0];
963
        sum += lum_m1[0] << 2;
964
        sum += -lum[0];
965
        lum_m2[0] = cm[(sum + 4) >> 3];
966
        lum_m4++;
967
        lum_m3++;
968
        lum_m2++;
969
        lum_m1++;
970
        lum++;
971
    }
972
}
973
#endif
974

    
975
/* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
976
   top field is copied as is, but the bottom field is deinterlaced
977
   against the top field. */
978
static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
979
                                    const uint8_t *src1, int src_wrap,
980
                                    int width, int height)
981
{
982
    const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
983
    int y;
984

    
985
    src_m2 = src1;
986
    src_m1 = src1;
987
    src_0=&src_m1[src_wrap];
988
    src_p1=&src_0[src_wrap];
989
    src_p2=&src_p1[src_wrap];
990
    for(y=0;y<(height-2);y+=2) {
991
        memcpy(dst,src_m1,width);
992
        dst += dst_wrap;
993
        deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
994
        src_m2 = src_0;
995
        src_m1 = src_p1;
996
        src_0 = src_p2;
997
        src_p1 += 2*src_wrap;
998
        src_p2 += 2*src_wrap;
999
        dst += dst_wrap;
1000
    }
1001
    memcpy(dst,src_m1,width);
1002
    dst += dst_wrap;
1003
    /* do last line */
1004
    deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1005
}
1006

    
1007
static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1008
                                             int width, int height)
1009
{
1010
    uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1011
    int y;
1012
    uint8_t *buf;
1013
    buf = (uint8_t*)av_malloc(width);
1014

    
1015
    src_m1 = src1;
1016
    memcpy(buf,src_m1,width);
1017
    src_0=&src_m1[src_wrap];
1018
    src_p1=&src_0[src_wrap];
1019
    src_p2=&src_p1[src_wrap];
1020
    for(y=0;y<(height-2);y+=2) {
1021
        deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1022
        src_m1 = src_p1;
1023
        src_0 = src_p2;
1024
        src_p1 += 2*src_wrap;
1025
        src_p2 += 2*src_wrap;
1026
    }
1027
    /* do last line */
1028
    deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1029
    av_free(buf);
1030
}
1031

    
1032
int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1033
                          enum PixelFormat pix_fmt, int width, int height)
1034
{
1035
    int i;
1036

    
1037
    if (pix_fmt != PIX_FMT_YUV420P &&
1038
        pix_fmt != PIX_FMT_YUVJ420P &&
1039
        pix_fmt != PIX_FMT_YUV422P &&
1040
        pix_fmt != PIX_FMT_YUVJ422P &&
1041
        pix_fmt != PIX_FMT_YUV444P &&
1042
        pix_fmt != PIX_FMT_YUV411P &&
1043
        pix_fmt != PIX_FMT_GRAY8)
1044
        return -1;
1045
    if ((width & 3) != 0 || (height & 3) != 0)
1046
        return -1;
1047

    
1048
    for(i=0;i<3;i++) {
1049
        if (i == 1) {
1050
            switch(pix_fmt) {
1051
            case PIX_FMT_YUVJ420P:
1052
            case PIX_FMT_YUV420P:
1053
                width >>= 1;
1054
                height >>= 1;
1055
                break;
1056
            case PIX_FMT_YUV422P:
1057
            case PIX_FMT_YUVJ422P:
1058
                width >>= 1;
1059
                break;
1060
            case PIX_FMT_YUV411P:
1061
                width >>= 2;
1062
                break;
1063
            default:
1064
                break;
1065
            }
1066
            if (pix_fmt == PIX_FMT_GRAY8) {
1067
                break;
1068
            }
1069
        }
1070
        if (src == dst) {
1071
            deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1072
                                 width, height);
1073
        } else {
1074
            deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1075
                                        src->data[i], src->linesize[i],
1076
                                        width, height);
1077
        }
1078
    }
1079
    emms_c();
1080
    return 0;
1081
}
1082