~ubuntu-branches/ubuntu/utopic/libav/utopic

« back to all changes in this revision

Viewing changes to libavcodec/imgconvert.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-12-21 15:32:13 UTC
  • mto: (1.2.18)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: package-import@ubuntu.com-20121221153213-fudzrugjzivtv0wp
Tags: upstream-9~beta3
ImportĀ upstreamĀ versionĀ 9~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
#include "x86/dsputil_mmx.h"
43
43
#endif
44
44
 
45
 
#define FF_COLOR_RGB      0 /**< RGB color space */
46
 
#define FF_COLOR_GRAY     1 /**< gray color space */
47
 
#define FF_COLOR_YUV      2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
48
 
#define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
49
 
 
50
 
#define FF_PIXEL_PLANAR   0 /**< each channel has one component in AVPicture */
51
 
#define FF_PIXEL_PACKED   1 /**< only one components containing all the channels */
52
 
#define FF_PIXEL_PALETTE  2  /**< one components containing indexes for a palette */
53
 
 
54
45
#if HAVE_MMX_EXTERNAL
55
46
#define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
56
47
#define deinterlace_line         ff_deinterlace_line_mmx
59
50
#define deinterlace_line         deinterlace_line_c
60
51
#endif
61
52
 
62
 
typedef struct PixFmtInfo {
63
 
    uint8_t nb_channels;     /**< number of channels (including alpha) */
64
 
    uint8_t color_type;      /**< color type (see FF_COLOR_xxx constants) */
65
 
    uint8_t pixel_type;      /**< pixel storage type (see FF_PIXEL_xxx constants) */
66
 
    uint8_t is_alpha : 1;    /**< true if alpha can be specified */
67
 
    uint8_t depth;           /**< bit depth of the color components */
68
 
} PixFmtInfo;
69
 
 
70
 
/* this table gives more information about formats */
71
 
static const PixFmtInfo pix_fmt_info[AV_PIX_FMT_NB] = {
72
 
    /* YUV formats */
73
 
    [AV_PIX_FMT_YUV420P] = {
74
 
        .nb_channels = 3,
75
 
        .color_type = FF_COLOR_YUV,
76
 
        .pixel_type = FF_PIXEL_PLANAR,
77
 
        .depth = 8,
78
 
    },
79
 
    [AV_PIX_FMT_YUV422P] = {
80
 
        .nb_channels = 3,
81
 
        .color_type = FF_COLOR_YUV,
82
 
        .pixel_type = FF_PIXEL_PLANAR,
83
 
        .depth = 8,
84
 
    },
85
 
    [AV_PIX_FMT_YUV444P] = {
86
 
        .nb_channels = 3,
87
 
        .color_type = FF_COLOR_YUV,
88
 
        .pixel_type = FF_PIXEL_PLANAR,
89
 
        .depth = 8,
90
 
    },
91
 
    [AV_PIX_FMT_YUYV422] = {
92
 
        .nb_channels = 1,
93
 
        .color_type = FF_COLOR_YUV,
94
 
        .pixel_type = FF_PIXEL_PACKED,
95
 
        .depth = 8,
96
 
    },
97
 
    [AV_PIX_FMT_UYVY422] = {
98
 
        .nb_channels = 1,
99
 
        .color_type = FF_COLOR_YUV,
100
 
        .pixel_type = FF_PIXEL_PACKED,
101
 
        .depth = 8,
102
 
    },
103
 
    [AV_PIX_FMT_YUV410P] = {
104
 
        .nb_channels = 3,
105
 
        .color_type = FF_COLOR_YUV,
106
 
        .pixel_type = FF_PIXEL_PLANAR,
107
 
        .depth = 8,
108
 
    },
109
 
    [AV_PIX_FMT_YUV411P] = {
110
 
        .nb_channels = 3,
111
 
        .color_type = FF_COLOR_YUV,
112
 
        .pixel_type = FF_PIXEL_PLANAR,
113
 
        .depth = 8,
114
 
    },
115
 
    [AV_PIX_FMT_YUV440P] = {
116
 
        .nb_channels = 3,
117
 
        .color_type = FF_COLOR_YUV,
118
 
        .pixel_type = FF_PIXEL_PLANAR,
119
 
        .depth = 8,
120
 
    },
121
 
    [AV_PIX_FMT_YUV420P16LE] = {
122
 
        .nb_channels = 3,
123
 
        .color_type = FF_COLOR_YUV,
124
 
        .pixel_type = FF_PIXEL_PLANAR,
125
 
        .depth = 16,
126
 
    },
127
 
    [AV_PIX_FMT_YUV422P16LE] = {
128
 
        .nb_channels = 3,
129
 
        .color_type = FF_COLOR_YUV,
130
 
        .pixel_type = FF_PIXEL_PLANAR,
131
 
        .depth = 16,
132
 
    },
133
 
    [AV_PIX_FMT_YUV444P16LE] = {
134
 
        .nb_channels = 3,
135
 
        .color_type = FF_COLOR_YUV,
136
 
        .pixel_type = FF_PIXEL_PLANAR,
137
 
        .depth = 16,
138
 
    },
139
 
    [AV_PIX_FMT_YUV420P16BE] = {
140
 
        .nb_channels = 3,
141
 
        .color_type = FF_COLOR_YUV,
142
 
        .pixel_type = FF_PIXEL_PLANAR,
143
 
        .depth = 16,
144
 
    },
145
 
    [AV_PIX_FMT_YUV422P16BE] = {
146
 
        .nb_channels = 3,
147
 
        .color_type = FF_COLOR_YUV,
148
 
        .pixel_type = FF_PIXEL_PLANAR,
149
 
        .depth = 16,
150
 
    },
151
 
    [AV_PIX_FMT_YUV444P16BE] = {
152
 
        .nb_channels = 3,
153
 
        .color_type = FF_COLOR_YUV,
154
 
        .pixel_type = FF_PIXEL_PLANAR,
155
 
        .depth = 16,
156
 
    },
157
 
 
158
 
 
159
 
    /* YUV formats with alpha plane */
160
 
    [AV_PIX_FMT_YUVA420P] = {
161
 
        .nb_channels = 4,
162
 
        .color_type = FF_COLOR_YUV,
163
 
        .pixel_type = FF_PIXEL_PLANAR,
164
 
        .depth = 8,
165
 
    },
166
 
 
167
 
    /* JPEG YUV */
168
 
    [AV_PIX_FMT_YUVJ420P] = {
169
 
        .nb_channels = 3,
170
 
        .color_type = FF_COLOR_YUV_JPEG,
171
 
        .pixel_type = FF_PIXEL_PLANAR,
172
 
        .depth = 8,
173
 
    },
174
 
    [AV_PIX_FMT_YUVJ422P] = {
175
 
        .nb_channels = 3,
176
 
        .color_type = FF_COLOR_YUV_JPEG,
177
 
        .pixel_type = FF_PIXEL_PLANAR,
178
 
        .depth = 8,
179
 
    },
180
 
    [AV_PIX_FMT_YUVJ444P] = {
181
 
        .nb_channels = 3,
182
 
        .color_type = FF_COLOR_YUV_JPEG,
183
 
        .pixel_type = FF_PIXEL_PLANAR,
184
 
        .depth = 8,
185
 
    },
186
 
    [AV_PIX_FMT_YUVJ440P] = {
187
 
        .nb_channels = 3,
188
 
        .color_type = FF_COLOR_YUV_JPEG,
189
 
        .pixel_type = FF_PIXEL_PLANAR,
190
 
        .depth = 8,
191
 
    },
192
 
 
193
 
    /* RGB formats */
194
 
    [AV_PIX_FMT_RGB24] = {
195
 
        .nb_channels = 3,
196
 
        .color_type = FF_COLOR_RGB,
197
 
        .pixel_type = FF_PIXEL_PACKED,
198
 
        .depth = 8,
199
 
    },
200
 
    [AV_PIX_FMT_BGR24] = {
201
 
        .nb_channels = 3,
202
 
        .color_type = FF_COLOR_RGB,
203
 
        .pixel_type = FF_PIXEL_PACKED,
204
 
        .depth = 8,
205
 
    },
206
 
    [AV_PIX_FMT_ARGB] = {
207
 
        .nb_channels = 4, .is_alpha = 1,
208
 
        .color_type = FF_COLOR_RGB,
209
 
        .pixel_type = FF_PIXEL_PACKED,
210
 
        .depth = 8,
211
 
    },
212
 
    [AV_PIX_FMT_RGB48BE] = {
213
 
        .nb_channels = 3,
214
 
        .color_type = FF_COLOR_RGB,
215
 
        .pixel_type = FF_PIXEL_PACKED,
216
 
        .depth = 16,
217
 
    },
218
 
    [AV_PIX_FMT_RGB48LE] = {
219
 
        .nb_channels = 3,
220
 
        .color_type = FF_COLOR_RGB,
221
 
        .pixel_type = FF_PIXEL_PACKED,
222
 
        .depth = 16,
223
 
    },
224
 
    [AV_PIX_FMT_RGB565BE] = {
225
 
        .nb_channels = 3,
226
 
        .color_type = FF_COLOR_RGB,
227
 
        .pixel_type = FF_PIXEL_PACKED,
228
 
        .depth = 5,
229
 
    },
230
 
    [AV_PIX_FMT_RGB565LE] = {
231
 
        .nb_channels = 3,
232
 
        .color_type = FF_COLOR_RGB,
233
 
        .pixel_type = FF_PIXEL_PACKED,
234
 
        .depth = 5,
235
 
    },
236
 
    [AV_PIX_FMT_RGB555BE] = {
237
 
        .nb_channels = 3,
238
 
        .color_type = FF_COLOR_RGB,
239
 
        .pixel_type = FF_PIXEL_PACKED,
240
 
        .depth = 5,
241
 
    },
242
 
    [AV_PIX_FMT_RGB555LE] = {
243
 
        .nb_channels = 3,
244
 
        .color_type = FF_COLOR_RGB,
245
 
        .pixel_type = FF_PIXEL_PACKED,
246
 
        .depth = 5,
247
 
    },
248
 
    [AV_PIX_FMT_RGB444BE] = {
249
 
        .nb_channels = 3,
250
 
        .color_type = FF_COLOR_RGB,
251
 
        .pixel_type = FF_PIXEL_PACKED,
252
 
        .depth = 4,
253
 
    },
254
 
    [AV_PIX_FMT_RGB444LE] = {
255
 
        .nb_channels = 3,
256
 
        .color_type = FF_COLOR_RGB,
257
 
        .pixel_type = FF_PIXEL_PACKED,
258
 
        .depth = 4,
259
 
    },
260
 
 
261
 
    /* gray / mono formats */
262
 
    [AV_PIX_FMT_GRAY16BE] = {
263
 
        .nb_channels = 1,
264
 
        .color_type = FF_COLOR_GRAY,
265
 
        .pixel_type = FF_PIXEL_PLANAR,
266
 
        .depth = 16,
267
 
    },
268
 
    [AV_PIX_FMT_GRAY16LE] = {
269
 
        .nb_channels = 1,
270
 
        .color_type = FF_COLOR_GRAY,
271
 
        .pixel_type = FF_PIXEL_PLANAR,
272
 
        .depth = 16,
273
 
    },
274
 
    [AV_PIX_FMT_GRAY8] = {
275
 
        .nb_channels = 1,
276
 
        .color_type = FF_COLOR_GRAY,
277
 
        .pixel_type = FF_PIXEL_PLANAR,
278
 
        .depth = 8,
279
 
    },
280
 
    [AV_PIX_FMT_MONOWHITE] = {
281
 
        .nb_channels = 1,
282
 
        .color_type = FF_COLOR_GRAY,
283
 
        .pixel_type = FF_PIXEL_PLANAR,
284
 
        .depth = 1,
285
 
    },
286
 
    [AV_PIX_FMT_MONOBLACK] = {
287
 
        .nb_channels = 1,
288
 
        .color_type = FF_COLOR_GRAY,
289
 
        .pixel_type = FF_PIXEL_PLANAR,
290
 
        .depth = 1,
291
 
    },
292
 
 
293
 
    /* paletted formats */
294
 
    [AV_PIX_FMT_PAL8] = {
295
 
        .nb_channels = 4, .is_alpha = 1,
296
 
        .color_type = FF_COLOR_RGB,
297
 
        .pixel_type = FF_PIXEL_PALETTE,
298
 
        .depth = 8,
299
 
    },
300
 
    [AV_PIX_FMT_UYYVYY411] = {
301
 
        .nb_channels = 1,
302
 
        .color_type = FF_COLOR_YUV,
303
 
        .pixel_type = FF_PIXEL_PACKED,
304
 
        .depth = 8,
305
 
    },
306
 
    [AV_PIX_FMT_ABGR] = {
307
 
        .nb_channels = 4, .is_alpha = 1,
308
 
        .color_type = FF_COLOR_RGB,
309
 
        .pixel_type = FF_PIXEL_PACKED,
310
 
        .depth = 8,
311
 
    },
312
 
    [AV_PIX_FMT_BGR565BE] = {
313
 
        .nb_channels = 3,
314
 
        .color_type = FF_COLOR_RGB,
315
 
        .pixel_type = FF_PIXEL_PACKED,
316
 
        .depth = 5,
317
 
    },
318
 
    [AV_PIX_FMT_BGR565LE] = {
319
 
        .nb_channels = 3,
320
 
        .color_type = FF_COLOR_RGB,
321
 
        .pixel_type = FF_PIXEL_PACKED,
322
 
        .depth = 5,
323
 
    },
324
 
    [AV_PIX_FMT_BGR555BE] = {
325
 
        .nb_channels = 3,
326
 
        .color_type = FF_COLOR_RGB,
327
 
        .pixel_type = FF_PIXEL_PACKED,
328
 
        .depth = 5,
329
 
    },
330
 
    [AV_PIX_FMT_BGR555LE] = {
331
 
        .nb_channels = 3,
332
 
        .color_type = FF_COLOR_RGB,
333
 
        .pixel_type = FF_PIXEL_PACKED,
334
 
        .depth = 5,
335
 
    },
336
 
    [AV_PIX_FMT_BGR444BE] = {
337
 
        .nb_channels = 3,
338
 
        .color_type = FF_COLOR_RGB,
339
 
        .pixel_type = FF_PIXEL_PACKED,
340
 
        .depth = 4,
341
 
    },
342
 
    [AV_PIX_FMT_BGR444LE] = {
343
 
        .nb_channels = 3,
344
 
        .color_type = FF_COLOR_RGB,
345
 
        .pixel_type = FF_PIXEL_PACKED,
346
 
        .depth = 4,
347
 
    },
348
 
    [AV_PIX_FMT_RGB8] = {
349
 
        .nb_channels = 1,
350
 
        .color_type = FF_COLOR_RGB,
351
 
        .pixel_type = FF_PIXEL_PACKED,
352
 
        .depth = 8,
353
 
    },
354
 
    [AV_PIX_FMT_RGB4] = {
355
 
        .nb_channels = 1,
356
 
        .color_type = FF_COLOR_RGB,
357
 
        .pixel_type = FF_PIXEL_PACKED,
358
 
        .depth = 4,
359
 
    },
360
 
    [AV_PIX_FMT_RGB4_BYTE] = {
361
 
        .nb_channels = 1,
362
 
        .color_type = FF_COLOR_RGB,
363
 
        .pixel_type = FF_PIXEL_PACKED,
364
 
        .depth = 8,
365
 
    },
366
 
    [AV_PIX_FMT_BGR8] = {
367
 
        .nb_channels = 1,
368
 
        .color_type = FF_COLOR_RGB,
369
 
        .pixel_type = FF_PIXEL_PACKED,
370
 
        .depth = 8,
371
 
    },
372
 
    [AV_PIX_FMT_BGR4] = {
373
 
        .nb_channels = 1,
374
 
        .color_type = FF_COLOR_RGB,
375
 
        .pixel_type = FF_PIXEL_PACKED,
376
 
        .depth = 4,
377
 
    },
378
 
    [AV_PIX_FMT_BGR4_BYTE] = {
379
 
        .nb_channels = 1,
380
 
        .color_type = FF_COLOR_RGB,
381
 
        .pixel_type = FF_PIXEL_PACKED,
382
 
        .depth = 8,
383
 
    },
384
 
    [AV_PIX_FMT_NV12] = {
385
 
        .nb_channels = 2,
386
 
        .color_type = FF_COLOR_YUV,
387
 
        .pixel_type = FF_PIXEL_PLANAR,
388
 
        .depth = 8,
389
 
    },
390
 
    [AV_PIX_FMT_NV21] = {
391
 
        .nb_channels = 2,
392
 
        .color_type = FF_COLOR_YUV,
393
 
        .pixel_type = FF_PIXEL_PLANAR,
394
 
        .depth = 8,
395
 
    },
396
 
 
397
 
    [AV_PIX_FMT_BGRA] = {
398
 
        .nb_channels = 4, .is_alpha = 1,
399
 
        .color_type = FF_COLOR_RGB,
400
 
        .pixel_type = FF_PIXEL_PACKED,
401
 
        .depth = 8,
402
 
    },
403
 
    [AV_PIX_FMT_RGBA] = {
404
 
        .nb_channels = 4, .is_alpha = 1,
405
 
        .color_type = FF_COLOR_RGB,
406
 
        .pixel_type = FF_PIXEL_PACKED,
407
 
        .depth = 8,
408
 
    },
409
 
};
410
 
 
411
53
void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
412
54
{
413
55
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
415
57
    *v_shift = desc->log2_chroma_h;
416
58
}
417
59
 
418
 
int ff_is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
419
 
{
420
 
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
421
 
    return desc->flags & PIX_FMT_HWACCEL;
422
 
}
423
 
 
424
 
int avpicture_fill(AVPicture *picture, uint8_t *ptr,
425
 
                   enum AVPixelFormat pix_fmt, int width, int height)
426
 
{
427
 
    int ret;
428
 
 
429
 
    if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
430
 
        return ret;
431
 
 
432
 
    if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
433
 
        return ret;
434
 
 
435
 
    return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
436
 
}
437
 
 
438
 
int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt, int width, int height,
439
 
                     unsigned char *dest, int dest_size)
440
 
{
441
 
    int i, j, nb_planes = 0, linesizes[4];
442
 
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
443
 
    int size = avpicture_get_size(pix_fmt, width, height);
444
 
 
445
 
    if (size > dest_size || size < 0)
446
 
        return AVERROR(EINVAL);
447
 
 
448
 
    for (i = 0; i < desc->nb_components; i++)
449
 
        nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
450
 
    nb_planes++;
451
 
 
452
 
    av_image_fill_linesizes(linesizes, pix_fmt, width);
453
 
    for (i = 0; i < nb_planes; i++) {
454
 
        int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
455
 
        const unsigned char *s = src->data[i];
456
 
        h = (height + (1 << shift) - 1) >> shift;
457
 
 
458
 
        for (j = 0; j < h; j++) {
459
 
            memcpy(dest, s, linesizes[i]);
460
 
            dest += linesizes[i];
461
 
            s += src->linesize[i];
462
 
        }
463
 
    }
464
 
 
465
 
    if (desc->flags & PIX_FMT_PAL)
466
 
        memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
467
 
 
468
 
    return size;
469
 
}
470
 
 
471
 
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
472
 
{
473
 
    AVPicture dummy_pict;
474
 
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
475
 
 
476
 
    if(av_image_check_size(width, height, 0, NULL))
477
 
        return -1;
478
 
    if (desc->flags & PIX_FMT_PSEUDOPAL)
479
 
        // do not include palette for these pseudo-paletted formats
480
 
        return width * height;
481
 
    return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
482
 
}
483
 
 
484
 
int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt, enum AVPixelFormat src_pix_fmt,
 
60
static int is_gray(const AVPixFmtDescriptor *desc)
 
61
{
 
62
    return desc->nb_components - (desc->flags & PIX_FMT_ALPHA) == 1;
 
63
}
 
64
 
 
65
int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
 
66
                             enum AVPixelFormat src_pix_fmt,
485
67
                             int has_alpha)
486
68
{
487
 
    const PixFmtInfo *pf, *ps;
488
69
    const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
489
70
    const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
490
 
    int loss;
491
 
 
492
 
    ps = &pix_fmt_info[src_pix_fmt];
 
71
    int loss, i, nb_components = FFMIN(src_desc->nb_components,
 
72
                                       dst_desc->nb_components);
493
73
 
494
74
    /* compute loss */
495
75
    loss = 0;
496
 
    pf = &pix_fmt_info[dst_pix_fmt];
497
 
    if (pf->depth < ps->depth ||
498
 
        ((dst_pix_fmt == AV_PIX_FMT_RGB555BE || dst_pix_fmt == AV_PIX_FMT_RGB555LE ||
499
 
          dst_pix_fmt == AV_PIX_FMT_BGR555BE || dst_pix_fmt == AV_PIX_FMT_BGR555LE) &&
500
 
         (src_pix_fmt == AV_PIX_FMT_RGB565BE || src_pix_fmt == AV_PIX_FMT_RGB565LE ||
501
 
          src_pix_fmt == AV_PIX_FMT_BGR565BE || src_pix_fmt == AV_PIX_FMT_BGR565LE)))
502
 
        loss |= FF_LOSS_DEPTH;
 
76
 
 
77
    if (dst_pix_fmt == src_pix_fmt)
 
78
        return 0;
 
79
 
 
80
    for (i = 0; i < nb_components; i++)
 
81
        if (src_desc->comp[i].depth_minus1 > dst_desc->comp[i].depth_minus1)
 
82
            loss |= FF_LOSS_DEPTH;
 
83
 
503
84
    if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
504
85
        dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
505
86
        loss |= FF_LOSS_RESOLUTION;
506
 
    switch(pf->color_type) {
507
 
    case FF_COLOR_RGB:
508
 
        if (ps->color_type != FF_COLOR_RGB &&
509
 
            ps->color_type != FF_COLOR_GRAY)
510
 
            loss |= FF_LOSS_COLORSPACE;
511
 
        break;
512
 
    case FF_COLOR_GRAY:
513
 
        if (ps->color_type != FF_COLOR_GRAY)
514
 
            loss |= FF_LOSS_COLORSPACE;
515
 
        break;
516
 
    case FF_COLOR_YUV:
517
 
        if (ps->color_type != FF_COLOR_YUV)
518
 
            loss |= FF_LOSS_COLORSPACE;
519
 
        break;
520
 
    case FF_COLOR_YUV_JPEG:
521
 
        if (ps->color_type != FF_COLOR_YUV_JPEG &&
522
 
            ps->color_type != FF_COLOR_YUV &&
523
 
            ps->color_type != FF_COLOR_GRAY)
524
 
            loss |= FF_LOSS_COLORSPACE;
525
 
        break;
526
 
    default:
527
 
        /* fail safe test */
528
 
        if (ps->color_type != pf->color_type)
529
 
            loss |= FF_LOSS_COLORSPACE;
530
 
        break;
531
 
    }
532
 
    if (pf->color_type == FF_COLOR_GRAY &&
533
 
        ps->color_type != FF_COLOR_GRAY)
534
 
        loss |= FF_LOSS_CHROMA;
535
 
    if (!pf->is_alpha && (ps->is_alpha && has_alpha))
 
87
 
 
88
    if ((src_desc->flags & PIX_FMT_RGB) != (dst_desc->flags & PIX_FMT_RGB))
 
89
        loss |= FF_LOSS_COLORSPACE;
 
90
 
 
91
    if (has_alpha && !(dst_desc->flags & PIX_FMT_ALPHA) &&
 
92
         (dst_desc->flags & PIX_FMT_ALPHA))
536
93
        loss |= FF_LOSS_ALPHA;
537
 
    if (pf->pixel_type == FF_PIXEL_PALETTE &&
538
 
        (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
539
 
        loss |= FF_LOSS_COLORQUANT;
 
94
 
 
95
    if (dst_pix_fmt == AV_PIX_FMT_PAL8 && !is_gray(src_desc))
 
96
        return loss | FF_LOSS_COLORQUANT;
 
97
 
 
98
    if (src_desc->nb_components > dst_desc->nb_components)
 
99
        if (is_gray(dst_desc))
 
100
            loss |= FF_LOSS_CHROMA;
 
101
 
540
102
    return loss;
541
103
}
542
104
 
543
 
static int avg_bits_per_pixel(enum AVPixelFormat pix_fmt)
544
 
{
545
 
    int bits;
546
 
    const PixFmtInfo *pf;
547
 
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
548
 
 
549
 
    pf = &pix_fmt_info[pix_fmt];
550
 
    switch(pf->pixel_type) {
551
 
    case FF_PIXEL_PACKED:
552
 
        switch(pix_fmt) {
553
 
        case AV_PIX_FMT_YUYV422:
554
 
        case AV_PIX_FMT_UYVY422:
555
 
        case AV_PIX_FMT_RGB565BE:
556
 
        case AV_PIX_FMT_RGB565LE:
557
 
        case AV_PIX_FMT_RGB555BE:
558
 
        case AV_PIX_FMT_RGB555LE:
559
 
        case AV_PIX_FMT_RGB444BE:
560
 
        case AV_PIX_FMT_RGB444LE:
561
 
        case AV_PIX_FMT_BGR565BE:
562
 
        case AV_PIX_FMT_BGR565LE:
563
 
        case AV_PIX_FMT_BGR555BE:
564
 
        case AV_PIX_FMT_BGR555LE:
565
 
        case AV_PIX_FMT_BGR444BE:
566
 
        case AV_PIX_FMT_BGR444LE:
567
 
            bits = 16;
568
 
            break;
569
 
        case AV_PIX_FMT_UYYVYY411:
570
 
            bits = 12;
571
 
            break;
572
 
        default:
573
 
            bits = pf->depth * pf->nb_channels;
574
 
            break;
575
 
        }
576
 
        break;
577
 
    case FF_PIXEL_PLANAR:
578
 
        if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
579
 
            bits = pf->depth * pf->nb_channels;
580
 
        } else {
581
 
            bits = pf->depth + ((2 * pf->depth) >>
582
 
                                (desc->log2_chroma_w + desc->log2_chroma_h));
583
 
        }
584
 
        break;
585
 
    case FF_PIXEL_PALETTE:
586
 
        bits = 8;
587
 
        break;
588
 
    default:
589
 
        bits = -1;
590
 
        break;
591
 
    }
592
 
    return bits;
593
 
}
594
 
 
595
105
static enum AVPixelFormat avcodec_find_best_pix_fmt1(enum AVPixelFormat *pix_fmt_list,
596
106
                                      enum AVPixelFormat src_pix_fmt,
597
107
                                      int has_alpha,
615
125
 
616
126
        loss = avcodec_get_pix_fmt_loss(pix_fmt, src_pix_fmt, has_alpha) & loss_mask;
617
127
        if (loss == 0) {
618
 
            dist = avg_bits_per_pixel(pix_fmt);
 
128
            dist = av_get_bits_per_pixel(av_pix_fmt_desc_get(pix_fmt));
619
129
            if (dist < min_dist) {
620
130
                min_dist = dist;
621
131
                dst_pix_fmt = pix_fmt;
678
188
    return dst_pix_fmt;
679
189
}
680
190
 
681
 
void av_picture_copy(AVPicture *dst, const AVPicture *src,
682
 
                     enum AVPixelFormat pix_fmt, int width, int height)
683
 
{
684
 
    av_image_copy(dst->data, dst->linesize, src->data,
685
 
                  src->linesize, pix_fmt, width, height);
686
 
}
687
 
 
688
191
/* 2x2 -> 1x1 */
689
192
void ff_shrink22(uint8_t *dst, int dst_wrap,
690
193
                     const uint8_t *src, int src_wrap,
771
274
    }
772
275
}
773
276
 
774
 
 
775
 
int avpicture_alloc(AVPicture *picture,
776
 
                    enum AVPixelFormat pix_fmt, int width, int height)
777
 
{
778
 
    int ret;
779
 
 
780
 
    if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
781
 
        memset(picture, 0, sizeof(AVPicture));
782
 
        return ret;
783
 
    }
784
 
 
785
 
    return 0;
786
 
}
787
 
 
788
 
void avpicture_free(AVPicture *picture)
789
 
{
790
 
    av_free(picture->data[0]);
791
 
}
792
 
 
793
277
/* return true if yuv planar */
794
 
static inline int is_yuv_planar(const PixFmtInfo *ps)
 
278
static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
795
279
{
796
 
    return (ps->color_type == FF_COLOR_YUV ||
797
 
            ps->color_type == FF_COLOR_YUV_JPEG) &&
798
 
        ps->pixel_type == FF_PIXEL_PLANAR;
 
280
    return (!(desc->flags & PIX_FMT_RGB) &&
 
281
             (desc->flags & PIX_FMT_PLANAR));
799
282
}
800
283
 
801
284
int av_picture_crop(AVPicture *dst, const AVPicture *src,
805
288
    int y_shift;
806
289
    int x_shift;
807
290
 
808
 
    if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
 
291
    if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(desc))
809
292
        return -1;
810
293
 
811
294
    y_shift = desc->log2_chroma_h;
833
316
    int i, y;
834
317
 
835
318
    if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
836
 
        !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
 
319
        !is_yuv_planar(desc)) return -1;
837
320
 
838
321
    for (i = 0; i < 3; i++) {
839
322
        x_shift = i ? desc->log2_chroma_w : 0;