imgconvert.c 15.3 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

/**
23
 * @file
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 "imgconvert.h"
35
#include "internal.h"
36
#include "mathops.h"
37
#include "libavutil/avassert.h"
38
#include "libavutil/colorspace.h"
39
#include "libavutil/common.h"
40
#include "libavutil/pixdesc.h"
41
#include "libavutil/imgutils.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
42

43
void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
44
{
45
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
46
    av_assert0(desc);
47 48
    *h_shift = desc->log2_chroma_w;
    *v_shift = desc->log2_chroma_h;
49 50
}

51 52 53 54
int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
                             enum AVPixelFormat src_pix_fmt,
                             int has_alpha)
{
55
    return av_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
56 57
}

58 59
enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
                                            enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
60
{
61
    return av_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
62 63
}

64
#if AV_HAVE_INCOMPATIBLE_LIBAV_ABI
65
enum AVPixelFormat avcodec_find_best_pix_fmt2(const enum AVPixelFormat *pix_fmt_list,
66
                                            enum AVPixelFormat src_pix_fmt,
67 68 69 70
                                            int has_alpha, int *loss_ptr){
    return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
}
#else
71 72
enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
                                            enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
73 74 75
{
    return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
}
76
#endif
77

78
enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(const enum AVPixelFormat *pix_fmt_list,
79
                                            enum AVPixelFormat src_pix_fmt,
80 81 82
                                            int has_alpha, int *loss_ptr){
    int i;

83
    enum AVPixelFormat best = AV_PIX_FMT_NONE;
84

85
    for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
86
        best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
87 88 89 90

    return best;
}

91
/* 2x2 -> 1x1 */
92
void ff_shrink22(uint8_t *dst, int dst_wrap,
93
                     const uint8_t *src, int src_wrap,
94 95 96
                     int width, int height)
{
    int w;
97 98
    const uint8_t *s1, *s2;
    uint8_t *d;
99 100 101 102 103 104

    for(;height > 0; height--) {
        s1 = src;
        s2 = s1 + src_wrap;
        d = dst;
        for(w = width;w >= 4; w-=4) {
105 106 107 108
            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;
109 110 111 112 113
            s1 += 8;
            s2 += 8;
            d += 4;
        }
        for(;w > 0; w--) {
114
            d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
115 116 117 118 119 120 121 122 123
            s1 += 2;
            s2 += 2;
            d++;
        }
        src += 2 * src_wrap;
        dst += dst_wrap;
    }
}

124
/* 4x4 -> 1x1 */
125
void ff_shrink44(uint8_t *dst, int dst_wrap,
126
                     const uint8_t *src, int src_wrap,
127 128 129
                     int width, int height)
{
    int w;
130 131
    const uint8_t *s1, *s2, *s3, *s4;
    uint8_t *d;
132 133 134

    for(;height > 0; height--) {
        s1 = src;
135 136 137
        s2 = s1 + src_wrap;
        s3 = s2 + src_wrap;
        s4 = s3 + src_wrap;
138
        d = dst;
139 140 141 142 143 144 145 146 147
        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;
148 149
            d++;
        }
150 151 152 153 154
        src += 4 * src_wrap;
        dst += dst_wrap;
    }
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/* 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;
    }
}

177
/* return true if yuv planar */
178
static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
179
{
180 181 182
    int i;
    int planes[4] = { 0 };

183 184
    if (     desc->flags & AV_PIX_FMT_FLAG_RGB
        || !(desc->flags & AV_PIX_FMT_FLAG_PLANAR))
185 186 187 188 189 190 191 192 193 194 195
        return 0;

    /* set the used planes */
    for (i = 0; i < desc->nb_components; i++)
        planes[desc->comp[i].plane] = 1;

    /* if there is an unused plane, the format is not planar */
    for (i = 0; i < desc->nb_components; i++)
        if (!planes[i])
            return 0;
    return 1;
196 197
}

198
int av_picture_crop(AVPicture *dst, const AVPicture *src,
199
                    enum AVPixelFormat pix_fmt, int top_band, int left_band)
200
{
201
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
202 203
    int y_shift;
    int x_shift;
204
    int max_step[4];
205

206
    if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
207 208
        return -1;

209 210
    y_shift = desc->log2_chroma_h;
    x_shift = desc->log2_chroma_w;
211
    av_image_fill_max_pixsteps(max_step, NULL, desc);
212

213
    if (is_yuv_planar(desc)) {
214 215 216
    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);
217 218 219
    } else{
        if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
            return -1;
220
        dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + (left_band * max_step[0]);
221
    }
222 223 224 225 226 227 228

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

229
int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
230
                   enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
Luca Barbato's avatar
Luca Barbato committed
231
            int *color)
232
{
233
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
234
    uint8_t *optr;
235 236 237 238 239
    int y_shift;
    int x_shift;
    int yheight;
    int i, y;

240
    if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
241
        !is_yuv_planar(desc)) return -1;
242 243

    for (i = 0; i < 3; i++) {
244 245
        x_shift = i ? desc->log2_chroma_w : 0;
        y_shift = i ? desc->log2_chroma_h : 0;
246 247

        if (padtop || padleft) {
Luca Barbato's avatar
Luca Barbato committed
248 249
            memset(dst->data[i], color[i],
                dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
250 251
        }

252 253 254 255 256 257 258
        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];
259
            }
260 261 262 263 264 265
        }

        if (src) { /* first line */
            uint8_t *iptr = src->data[i];
            optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
                    (padleft >> x_shift);
266
            memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
267
            iptr += src->linesize[i];
Luca Barbato's avatar
Luca Barbato committed
268 269
            optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
                (dst->linesize[i] - (padright >> x_shift));
270 271 272
            yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
            for (y = 0; y < yheight; y++) {
                memset(optr, color[i], (padleft + padright) >> x_shift);
273
                memcpy(optr + ((padleft + padright) >> x_shift), iptr,
274
                       (width - padleft - padright) >> x_shift);
275
                iptr += src->linesize[i];
276 277 278 279 280
                optr += dst->linesize[i];
            }
        }

        if (padbottom || padright) {
Luca Barbato's avatar
Luca Barbato committed
281 282 283 284
            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));
285 286 287 288 289
        }
    }
    return 0;
}

290 291
#if FF_API_DEINTERLACE

292 293 294 295 296 297 298
#if HAVE_MMX_EXTERNAL
#define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
#define deinterlace_line         ff_deinterlace_line_mmx
#else
#define deinterlace_line_inplace deinterlace_line_inplace_c
#define deinterlace_line         deinterlace_line_c

299
/* filter parameters: [-1 4 2 4 -1] // 8 */
300
static void deinterlace_line_c(uint8_t *dst,
301 302 303 304
                             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)
305
{
306
    const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
307 308 309
    int sum;

    for(;size > 0;size--) {
Fred's avatar
Fred committed
310 311 312 313 314
        sum = -lum_m4[0];
        sum += lum_m3[0] << 2;
        sum += lum_m2[0] << 1;
        sum += lum_m1[0] << 2;
        sum += -lum[0];
315
        dst[0] = cm[(sum + 4) >> 3];
Fred's avatar
Fred committed
316 317 318 319 320
        lum_m4++;
        lum_m3++;
        lum_m2++;
        lum_m1++;
        lum++;
321 322
        dst++;
    }
Fred's avatar
Fred committed
323
}
324 325 326 327

static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
                                       uint8_t *lum_m2, uint8_t *lum_m1,
                                       uint8_t *lum, int size)
Fred's avatar
Fred committed
328
{
329
    const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
Fred's avatar
Fred committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    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++;
    }
346
}
347
#endif /* !HAVE_MMX_EXTERNAL */
348 349 350 351

/* 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. */
352
static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
353
                                    const uint8_t *src1, int src_wrap,
Fred's avatar
Fred committed
354
                                    int width, int height)
355
{
356
    const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
Fred's avatar
Fred committed
357 358 359 360 361 362 363 364 365
    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);
366
        dst += dst_wrap;
Fred's avatar
Fred committed
367 368 369 370 371 372
        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;
373 374
        dst += dst_wrap;
    }
Fred's avatar
Fred committed
375 376 377 378 379 380
    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);
}

381 382
static int deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
                                            int width, int height)
Fred's avatar
Fred committed
383
{
384
    uint8_t *src_m1, *src_0, *src_p1, *src_p2;
Fred's avatar
Fred committed
385
    int y;
386
    uint8_t *buf;
387
    buf = av_malloc(width);
388 389
    if (!buf)
        return AVERROR(ENOMEM);
Fred's avatar
Fred committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

    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);
405
    av_free(buf);
406
    return 0;
407 408
}

409
int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
410
                          enum AVPixelFormat pix_fmt, int width, int height)
Fabrice Bellard's avatar
Fabrice Bellard committed
411
{
412
    int i, ret;
413

414 415 416 417 418 419 420
    if (pix_fmt != AV_PIX_FMT_YUV420P &&
        pix_fmt != AV_PIX_FMT_YUVJ420P &&
        pix_fmt != AV_PIX_FMT_YUV422P &&
        pix_fmt != AV_PIX_FMT_YUVJ422P &&
        pix_fmt != AV_PIX_FMT_YUV444P &&
        pix_fmt != AV_PIX_FMT_YUV411P &&
        pix_fmt != AV_PIX_FMT_GRAY8)
421
        return -1;
Fred's avatar
Fred committed
422
    if ((width & 3) != 0 || (height & 3) != 0)
423
        return -1;
Fred's avatar
Fred committed
424

425 426 427
    for(i=0;i<3;i++) {
        if (i == 1) {
            switch(pix_fmt) {
428 429
            case AV_PIX_FMT_YUVJ420P:
            case AV_PIX_FMT_YUV420P:
430 431 432
                width >>= 1;
                height >>= 1;
                break;
433 434
            case AV_PIX_FMT_YUV422P:
            case AV_PIX_FMT_YUVJ422P:
435 436
                width >>= 1;
                break;
437
            case AV_PIX_FMT_YUV411P:
438 439
                width >>= 2;
                break;
440 441 442
            default:
                break;
            }
443
            if (pix_fmt == AV_PIX_FMT_GRAY8) {
444 445
                break;
            }
446
        }
Fred's avatar
Fred committed
447
        if (src == dst) {
448 449 450 451 452
            ret = deinterlace_bottom_field_inplace(dst->data[i],
                                                   dst->linesize[i],
                                                   width, height);
            if (ret < 0)
                return ret;
Fred's avatar
Fred committed
453 454 455 456 457
        } 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
458
    }
459
    emms_c();
460
    return 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
461
}
462

463 464
#endif /* FF_API_DEINTERLACE */

465 466 467 468
#ifdef TEST

int main(void){
    int i;
469
    int err=0;
470
    int skip = 0;
471

472
    for (i=0; i<AV_PIX_FMT_NB*2; i++) {
473
        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
474 475
        if(!desc || !desc->name) {
            skip ++;
476
            continue;
477 478
        }
        if (skip) {
479
            av_log(NULL, AV_LOG_INFO, "%3d unused pixel format values\n", skip);
480 481
            skip = 0;
        }
482
        av_log(NULL, AV_LOG_INFO, "pix fmt %s yuv_plan:%d avg_bpp:%d\n", desc->name, is_yuv_planar(desc), av_get_padded_bits_per_pixel(desc));
483
        if ((!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)) != (desc->nb_components != 2 && desc->nb_components != 4)) {
484
            av_log(NULL, AV_LOG_ERROR, "Alpha flag mismatch\n");
485 486
            err = 1;
        }
487
    }
488
    return err;
489 490 491
}

#endif