vf_yadif.c 13.2 KB
Newer Older
1 2
/*
 * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
James Darnley's avatar
James Darnley committed
3
 *               2010      James Darnley <james.darnley@gmail.com>
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or modify
8 9 10 11
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
18
 * with Libav; if not, write to the Free Software Foundation, Inc.,
19 20 21 22 23
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "libavutil/cpu.h"
#include "libavutil/common.h"
24
#include "libavutil/pixdesc.h"
25
#include "avfilter.h"
26
#include "formats.h"
27
#include "internal.h"
28
#include "video.h"
29 30 31 32 33
#include "yadif.h"

#undef NDEBUG
#include <assert.h>

Luca Barbato's avatar
Luca Barbato committed
34 35
#define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE

36
#define CHECK(j)\
37 38 39
    {   int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
                  + FFABS(cur[mrefs  +(j)] - cur[prefs  -(j)])\
                  + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
40 41
        if (score < spatial_score) {\
            spatial_score= score;\
42
            spatial_pred= (cur[mrefs  +(j)] + cur[prefs  -(j)])>>1;\
43

James Darnley's avatar
James Darnley committed
44 45 46 47 48 49 50 51
#define FILTER \
    for (x = 0;  x < w; x++) { \
        int c = cur[mrefs]; \
        int d = (prev2[0] + next2[0])>>1; \
        int e = cur[prefs]; \
        int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
        int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
        int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
Luca Barbato's avatar
Luca Barbato committed
52 53 54 55
        int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
        int spatial_pred = (c+e) >> 1; \
        int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
                          + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
James Darnley's avatar
James Darnley committed
56 57 58 59 60
 \
        CHECK(-1) CHECK(-2) }} }} \
        CHECK( 1) CHECK( 2) }} }} \
 \
        if (mode < 2) { \
Luca Barbato's avatar
Luca Barbato committed
61 62 63 64
            int b = (prev2[2 * mrefs] + next2[2 * mrefs])>>1; \
            int f = (prev2[2 * prefs] + next2[2 * prefs])>>1; \
            int max = FFMAX3(d - e, d - c, FFMIN(b - c, f - e)); \
            int min = FFMIN3(d - e, d - c, FFMAX(b - c, f - e)); \
James Darnley's avatar
James Darnley committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
 \
            diff = FFMAX3(diff, min, -max); \
        } \
 \
        if (spatial_pred > d + diff) \
           spatial_pred = d + diff; \
        else if (spatial_pred < d - diff) \
           spatial_pred = d - diff; \
 \
        dst[0] = spatial_pred; \
 \
        dst++; \
        cur++; \
        prev++; \
        next++; \
        prev2++; \
        next2++; \
    }
83

James Darnley's avatar
James Darnley committed
84 85 86 87 88 89 90
static void filter_line_c(uint8_t *dst,
                          uint8_t *prev, uint8_t *cur, uint8_t *next,
                          int w, int prefs, int mrefs, int parity, int mode)
{
    int x;
    uint8_t *prev2 = parity ? prev : cur ;
    uint8_t *next2 = parity ? cur  : next;
91

James Darnley's avatar
James Darnley committed
92 93
    FILTER
}
94

James Darnley's avatar
James Darnley committed
95 96
static void filter_line_c_16bit(uint16_t *dst,
                                uint16_t *prev, uint16_t *cur, uint16_t *next,
Luca Barbato's avatar
Luca Barbato committed
97 98
                                int w, int prefs, int mrefs, int parity,
                                int mode)
James Darnley's avatar
James Darnley committed
99 100 101 102 103 104
{
    int x;
    uint16_t *prev2 = parity ? prev : cur ;
    uint16_t *next2 = parity ? cur  : next;
    mrefs /= 2;
    prefs /= 2;
105

James Darnley's avatar
James Darnley committed
106
    FILTER
107 108 109 110 111 112 113 114
}

static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
                   int parity, int tff)
{
    YADIFContext *yadif = ctx->priv;
    int y, i;

115 116 117
    for (i = 0; i < yadif->csp->nb_components; i++) {
        int w = dstpic->video->w;
        int h = dstpic->video->h;
118
        int refs = yadif->cur->linesize[i];
Luca Barbato's avatar
Luca Barbato committed
119
        int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
120

121
        if (i == 1 || i == 2) {
122 123 124 125 126
        /* Why is this not part of the per-plane description thing? */
            w >>= yadif->csp->log2_chroma_w;
            h >>= yadif->csp->log2_chroma_h;
        }

127 128
        for (y = 0; y < h; y++) {
            if ((y ^ parity) & 1) {
Luca Barbato's avatar
Luca Barbato committed
129 130 131 132 133 134 135 136 137
                uint8_t *prev = &yadif->prev->data[i][y * refs];
                uint8_t *cur  = &yadif->cur ->data[i][y * refs];
                uint8_t *next = &yadif->next->data[i][y * refs];
                uint8_t *dst  = &dstpic->data[i][y * dstpic->linesize[i]];
                int     mode  = y == 1 || y + 2 == h ? 2 : yadif->mode;
                yadif->filter_line(dst, prev, cur, next, w,
                                   y + 1 < h ? refs : -refs,
                                   y ? -refs : refs,
                                   parity ^ tff, mode);
138
            } else {
Luca Barbato's avatar
Luca Barbato committed
139 140
                memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
                       &yadif->cur->data[i][y * refs], w * df);
141 142 143
            }
        }
    }
144 145

    emms_c();
146 147
}

Luca Barbato's avatar
Luca Barbato committed
148 149
static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms,
                                           int w, int h)
150 151
{
    AVFilterBufferRef *picref;
Luca Barbato's avatar
Luca Barbato committed
152 153
    int width  = FFALIGN(w, 32);
    int height = FFALIGN(h + 2, 32);
154 155
    int i;

156
    picref = ff_default_get_video_buffer(link, perms, width, height);
157 158 159 160 161

    picref->video->w = w;
    picref->video->h = h;

    for (i = 0; i < 3; i++)
162
        picref->data[i] += picref->linesize[i];
163 164 165 166

    return picref;
}

167
static int return_frame(AVFilterContext *ctx, int is_second)
Stefano Sabatini's avatar
Stefano Sabatini committed
168
{
169
    YADIFContext *yadif = ctx->priv;
Luca Barbato's avatar
Luca Barbato committed
170
    AVFilterLink *link  = ctx->outputs[0];
171
    int tff, ret;
172 173 174

    if (yadif->parity == -1) {
        tff = yadif->cur->video->interlaced ?
Luca Barbato's avatar
Luca Barbato committed
175
              yadif->cur->video->top_field_first : 1;
176
    } else {
Luca Barbato's avatar
Luca Barbato committed
177
        tff = yadif->parity ^ 1;
178
    }
179

180
    if (is_second) {
Luca Barbato's avatar
Luca Barbato committed
181
        yadif->out = ff_get_video_buffer(link, PERM_RWP, link->w, link->h);
182 183 184
        if (!yadif->out)
            return AVERROR(ENOMEM);

185 186 187
        avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
        yadif->out->video->interlaced = 0;
    }
188

189
    if (!yadif->csp)
190
        yadif->csp = av_pix_fmt_desc_get(link->format);
Luca Barbato's avatar
Luca Barbato committed
191
    if (yadif->csp->comp[0].depth_minus1 / 8 == 1)
James Darnley's avatar
James Darnley committed
192
        yadif->filter_line = filter_line_c_16bit;
193

194 195
    filter(ctx, yadif->out, tff ^ !is_second, tff);

196
    if (is_second) {
Robert Nagy's avatar
Robert Nagy committed
197 198 199 200 201
        int64_t cur_pts  = yadif->cur->pts;
        int64_t next_pts = yadif->next->pts;

        if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
            yadif->out->pts = cur_pts + next_pts;
202 203 204
        } else {
            yadif->out->pts = AV_NOPTS_VALUE;
        }
205 206 207
        ret = ff_start_frame(ctx->outputs[0], yadif->out);
        if (ret < 0)
            return ret;
208
    }
209 210 211
    if ((ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1)) < 0 ||
        (ret = ff_end_frame(ctx->outputs[0])) < 0)
        return ret;
212

Stefano Sabatini's avatar
Stefano Sabatini committed
213
    yadif->frame_pending = (yadif->mode&1) && !is_second;
214
    return 0;
215 216
}

217
static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
218 219 220 221
{
    AVFilterContext *ctx = link->dst;
    YADIFContext *yadif = ctx->priv;

Stefano Sabatini's avatar
Stefano Sabatini committed
222
    if (yadif->frame_pending)
223 224 225 226 227 228 229
        return_frame(ctx, 1);

    if (yadif->prev)
        avfilter_unref_buffer(yadif->prev);
    yadif->prev = yadif->cur;
    yadif->cur  = yadif->next;
    yadif->next = picref;
230
    link->cur_buf = NULL;
231

Stefano Sabatini's avatar
Stefano Sabatini committed
232
    if (!yadif->cur)
233
        return 0;
234

235 236
    if (yadif->auto_enable && !yadif->cur->video->interlaced) {
        yadif->out  = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
237 238 239
        if (!yadif->out)
            return AVERROR(ENOMEM);

240
        avfilter_unref_bufferp(&yadif->prev);
Robert Nagy's avatar
Robert Nagy committed
241 242
        if (yadif->out->pts != AV_NOPTS_VALUE)
            yadif->out->pts *= 2;
243
        return ff_start_frame(ctx->outputs[0], yadif->out);
244 245
    }

246 247 248
    if (!yadif->prev &&
        !(yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ)))
        return AVERROR(ENOMEM);
249

Luca Barbato's avatar
Luca Barbato committed
250 251
    yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
                                     link->w, link->h);
252 253
    if (!yadif->out)
        return AVERROR(ENOMEM);
254

255 256
    avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
    yadif->out->video->interlaced = 0;
Luca Barbato's avatar
Luca Barbato committed
257

Robert Nagy's avatar
Robert Nagy committed
258 259
    if (yadif->out->pts != AV_NOPTS_VALUE)
        yadif->out->pts *= 2;
Luca Barbato's avatar
Luca Barbato committed
260

261
    return ff_start_frame(ctx->outputs[0], yadif->out);
262 263
}

264
static int end_frame(AVFilterLink *link)
265 266 267 268
{
    AVFilterContext *ctx = link->dst;
    YADIFContext *yadif = ctx->priv;

Stefano Sabatini's avatar
Stefano Sabatini committed
269
    if (!yadif->out)
270
        return 0;
271

272
    if (yadif->auto_enable && !yadif->cur->video->interlaced) {
273 274 275 276
        int ret = ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
        if (ret >= 0)
            ret = ff_end_frame(ctx->outputs[0]);
        return ret;
277 278
    }

279
    return_frame(ctx, 0);
280
    return 0;
281 282 283 284 285 286 287
}

static int request_frame(AVFilterLink *link)
{
    AVFilterContext *ctx = link->src;
    YADIFContext *yadif = ctx->priv;

Stefano Sabatini's avatar
Stefano Sabatini committed
288
    if (yadif->frame_pending) {
289 290 291 292
        return_frame(ctx, 1);
        return 0;
    }

Stefano Sabatini's avatar
Stefano Sabatini committed
293
    do {
294 295
        int ret;

Robert Nagy's avatar
Robert Nagy committed
296 297 298
        if (yadif->eof)
            return AVERROR_EOF;

299
        ret  = ff_request_frame(link->src->inputs[0]);
Robert Nagy's avatar
Robert Nagy committed
300 301

        if (ret == AVERROR_EOF && yadif->next) {
Luca Barbato's avatar
Luca Barbato committed
302 303 304
            AVFilterBufferRef *next =
                avfilter_ref_buffer(yadif->next, AV_PERM_READ);

305 306 307
            if (!next)
                return AVERROR(ENOMEM);

Robert Nagy's avatar
Robert Nagy committed
308 309 310 311 312 313
            next->pts = yadif->next->pts * 2 - yadif->cur->pts;

            start_frame(link->src->inputs[0], next);
            end_frame(link->src->inputs[0]);
            yadif->eof = 1;
        } else if (ret < 0) {
314
            return ret;
Robert Nagy's avatar
Robert Nagy committed
315
        }
Stefano Sabatini's avatar
Stefano Sabatini committed
316
    } while (!yadif->cur);
317 318 319 320 321 322 323 324 325

    return 0;
}

static int poll_frame(AVFilterLink *link)
{
    YADIFContext *yadif = link->src->priv;
    int ret, val;

Stefano Sabatini's avatar
Stefano Sabatini committed
326
    if (yadif->frame_pending)
327 328
        return 1;

329
    val = ff_poll_frame(link->src->inputs[0]);
330 331
    if (val <= 0)
        return val;
332

Luca Barbato's avatar
Luca Barbato committed
333 334
    //FIXME change API to not requre this red tape
    if (val == 1 && !yadif->next) {
335
        if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
336
            return ret;
337
        val = ff_poll_frame(link->src->inputs[0]);
338 339
        if (val <= 0)
            return val;
340
    }
341
    assert(yadif->next || !val);
342

343 344 345
    if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
        return val;

346 347 348 349 350 351 352
    return val * ((yadif->mode&1)+1);
}

static av_cold void uninit(AVFilterContext *ctx)
{
    YADIFContext *yadif = ctx->priv;

353 354 355
    if (yadif->prev) avfilter_unref_bufferp(&yadif->prev);
    if (yadif->cur ) avfilter_unref_bufferp(&yadif->cur );
    if (yadif->next) avfilter_unref_bufferp(&yadif->next);
356 357 358 359
}

static int query_formats(AVFilterContext *ctx)
{
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    static const enum AVPixelFormat pix_fmts[] = {
        AV_PIX_FMT_YUV420P,
        AV_PIX_FMT_YUV422P,
        AV_PIX_FMT_YUV444P,
        AV_PIX_FMT_YUV410P,
        AV_PIX_FMT_YUV411P,
        AV_PIX_FMT_GRAY8,
        AV_PIX_FMT_YUVJ420P,
        AV_PIX_FMT_YUVJ422P,
        AV_PIX_FMT_YUVJ444P,
        AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
        AV_PIX_FMT_YUV440P,
        AV_PIX_FMT_YUVJ440P,
        AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
        AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
        AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
        AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
        AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
        AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
        AV_PIX_FMT_YUVA420P,
        AV_PIX_FMT_NONE
381 382
    };

383
    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
384 385 386 387

    return 0;
}

388
static av_cold int init(AVFilterContext *ctx, const char *args)
389 390 391 392 393
{
    YADIFContext *yadif = ctx->priv;

    yadif->mode = 0;
    yadif->parity = -1;
394
    yadif->auto_enable = 0;
395
    yadif->csp = NULL;
396

Luca Barbato's avatar
Luca Barbato committed
397 398 399
    if (args)
        sscanf(args, "%d:%d:%d",
               &yadif->mode, &yadif->parity, &yadif->auto_enable);
400 401

    yadif->filter_line = filter_line_c;
402

403
    if (ARCH_X86)
404
        ff_yadif_init_x86(yadif);
405

Luca Barbato's avatar
Luca Barbato committed
406 407
    av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d auto_enable:%d\n",
           yadif->mode, yadif->parity, yadif->auto_enable);
408 409 410 411

    return 0;
}

412 413 414 415
static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
{
    return 0;
}
416

Robert Nagy's avatar
Robert Nagy committed
417 418 419 420 421 422 423 424 425 426
static int config_props(AVFilterLink *link)
{
    link->time_base.num = link->src->inputs[0]->time_base.num;
    link->time_base.den = link->src->inputs[0]->time_base.den * 2;
    link->w             = link->src->inputs[0]->w;
    link->h             = link->src->inputs[0]->h;

    return 0;
}

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
static const AVFilterPad avfilter_vf_yadif_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_VIDEO,
        .start_frame      = start_frame,
        .get_video_buffer = get_video_buffer,
        .draw_slice       = null_draw_slice,
        .end_frame        = end_frame,
    },
    { NULL }
};

static const AVFilterPad avfilter_vf_yadif_outputs[] = {
    {
        .name          = "default",
        .type          = AVMEDIA_TYPE_VIDEO,
        .poll_frame    = poll_frame,
        .request_frame = request_frame,
        .config_props  = config_props,
    },
    { NULL }
};

450 451 452 453 454 455 456 457 458
AVFilter avfilter_vf_yadif = {
    .name          = "yadif",
    .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),

    .priv_size     = sizeof(YADIFContext),
    .init          = init,
    .uninit        = uninit,
    .query_formats = query_formats,

459 460 461
    .inputs    = avfilter_vf_yadif_inputs,

    .outputs   = avfilter_vf_yadif_outputs,
462
};