Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vf_yadif.c @ 14e3b120

History | View | Annotate | Download (10.6 KB)

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

    
21
#include "libavutil/cpu.h"
22
#include "libavutil/common.h"
23
#include "avfilter.h"
24
#include "yadif.h"
25

    
26
#undef NDEBUG
27
#include <assert.h>
28

    
29
typedef struct {
30
    /**
31
     * 0: send 1 frame for each frame
32
     * 1: send 1 frame for each field
33
     * 2: like 0 but skips spatial interlacing check
34
     * 3: like 1 but skips spatial interlacing check
35
     */
36
    int mode;
37

    
38
    /**
39
     *  0: bottom field first
40
     *  1: top field first
41
     * -1: auto-detection
42
     */
43
    int parity;
44

    
45
    int frame_pending;
46

    
47
    AVFilterBufferRef *cur;
48
    AVFilterBufferRef *next;
49
    AVFilterBufferRef *prev;
50
    AVFilterBufferRef *out;
51
    void (*filter_line)(uint8_t *dst,
52
                        uint8_t *prev, uint8_t *cur, uint8_t *next,
53
                        int w, int prefs, int mrefs, int parity, int mode);
54
} YADIFContext;
55

    
56
static void filter_line_c(uint8_t *dst,
57
                          uint8_t *prev, uint8_t *cur, uint8_t *next,
58
                          int w, int prefs, int mrefs, int parity, int mode)
59
{
60
    int x;
61
    uint8_t *prev2 = parity ? prev : cur ;
62
    uint8_t *next2 = parity ? cur  : next;
63
    for (x = 0;  x < w; x++) {
64
        int c = cur[mrefs];
65
        int d = (prev2[0] + next2[0])>>1;
66
        int e = cur[prefs];
67
        int temporal_diff0 = FFABS(prev2[0] - next2[0]);
68
        int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1;
69
        int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1;
70
        int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2);
71
        int spatial_pred = (c+e)>>1;
72
        int spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e)
73
                          + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1;
74

    
75
#define CHECK(j)\
76
    {   int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
77
                  + FFABS(cur[mrefs  +(j)] - cur[prefs  -(j)])\
78
                  + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
79
        if (score < spatial_score) {\
80
            spatial_score= score;\
81
            spatial_pred= (cur[mrefs  +(j)] + cur[prefs  -(j)])>>1;\
82

    
83
        CHECK(-1) CHECK(-2) }} }}
84
        CHECK( 1) CHECK( 2) }} }}
85

    
86
        if (mode < 2) {
87
            int b = (prev2[2*mrefs] + next2[2*mrefs])>>1;
88
            int f = (prev2[2*prefs] + next2[2*prefs])>>1;
89
#if 0
90
            int a = cur[-3*refs];
91
            int g = cur[+3*refs];
92
            int max = FFMAX3(d-e, d-c, FFMIN3(FFMAX(b-c,f-e),FFMAX(b-c,b-a),FFMAX(f-g,f-e)) );
93
            int min = FFMIN3(d-e, d-c, FFMAX3(FFMIN(b-c,f-e),FFMIN(b-c,b-a),FFMIN(f-g,f-e)) );
94
#else
95
            int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e));
96
            int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e));
97
#endif
98

    
99
            diff = FFMAX3(diff, min, -max);
100
        }
101

    
102
        if (spatial_pred > d + diff)
103
           spatial_pred = d + diff;
104
        else if (spatial_pred < d - diff)
105
           spatial_pred = d - diff;
106

    
107
        dst[0] = spatial_pred;
108

    
109
        dst++;
110
        cur++;
111
        prev++;
112
        next++;
113
        prev2++;
114
        next2++;
115
    }
116
}
117

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

    
124
    for (i = 0; i < 3; i++) {
125
        int is_chroma = !!i;
126
        int w = dstpic->video->w >> is_chroma;
127
        int h = dstpic->video->h >> is_chroma;
128
        int refs = yadif->cur->linesize[i];
129

    
130
        for (y = 0; y < h; y++) {
131
            if ((y ^ parity) & 1) {
132
                uint8_t *prev = &yadif->prev->data[i][y*refs];
133
                uint8_t *cur  = &yadif->cur ->data[i][y*refs];
134
                uint8_t *next = &yadif->next->data[i][y*refs];
135
                uint8_t *dst  = &dstpic->data[i][y*dstpic->linesize[i]];
136
                int     mode  = y==1 || y+2==h ? 2 : yadif->mode;
137
                yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
138
            } else {
139
                memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
140
                       &yadif->cur->data[i][y*refs], w);
141
            }
142
        }
143
    }
144
#if HAVE_MMX
145
    __asm__ volatile("emms \n\t" : : : "memory");
146
#endif
147
}
148

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

    
156
    picref = avfilter_default_get_video_buffer(link, perms, width, height);
157

    
158
    picref->video->w = w;
159
    picref->video->h = h;
160

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

    
164
    return picref;
165
}
166

    
167
static void return_frame(AVFilterContext *ctx, int is_second)
168
{
169
    YADIFContext *yadif = ctx->priv;
170
    AVFilterLink *link= ctx->outputs[0];
171
    int tff;
172

    
173
    if (yadif->parity == -1) {
174
        tff = yadif->cur->video->interlaced ?
175
            yadif->cur->video->top_field_first : 1;
176
    } else {
177
        tff = yadif->parity^1;
178
    }
179

    
180
    if (is_second)
181
        yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
182
                                               AV_PERM_REUSE, link->w, link->h);
183

    
184
    filter(ctx, yadif->out, tff ^ !is_second, tff);
185

    
186
    if (is_second) {
187
        if (yadif->next->pts != AV_NOPTS_VALUE &&
188
            yadif->cur->pts != AV_NOPTS_VALUE) {
189
            yadif->out->pts =
190
                (yadif->next->pts&yadif->cur->pts) +
191
                ((yadif->next->pts^yadif->cur->pts)>>1);
192
        } else {
193
            yadif->out->pts = AV_NOPTS_VALUE;
194
        }
195
        avfilter_start_frame(ctx->outputs[0], yadif->out);
196
    }
197
    avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
198
    avfilter_end_frame(ctx->outputs[0]);
199

    
200
    yadif->frame_pending = (yadif->mode&1) && !is_second;
201
}
202

    
203
static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
204
{
205
    AVFilterContext *ctx = link->dst;
206
    YADIFContext *yadif = ctx->priv;
207

    
208
    if (yadif->frame_pending)
209
        return_frame(ctx, 1);
210

    
211
    if (yadif->prev)
212
        avfilter_unref_buffer(yadif->prev);
213
    yadif->prev = yadif->cur;
214
    yadif->cur  = yadif->next;
215
    yadif->next = picref;
216

    
217
    if (!yadif->cur)
218
        return;
219

    
220
    if (!yadif->prev)
221
        yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
222

    
223
    yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
224
                                       AV_PERM_REUSE, link->w, link->h);
225

    
226
    avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
227
    yadif->out->video->interlaced = 0;
228
    avfilter_start_frame(ctx->outputs[0], yadif->out);
229
}
230

    
231
static void end_frame(AVFilterLink *link)
232
{
233
    AVFilterContext *ctx = link->dst;
234
    YADIFContext *yadif = ctx->priv;
235

    
236
    if (!yadif->out)
237
        return;
238

    
239
    return_frame(ctx, 0);
240
}
241

    
242
static int request_frame(AVFilterLink *link)
243
{
244
    AVFilterContext *ctx = link->src;
245
    YADIFContext *yadif = ctx->priv;
246

    
247
    if (yadif->frame_pending) {
248
        return_frame(ctx, 1);
249
        return 0;
250
    }
251

    
252
    do {
253
        int ret;
254

    
255
        if ((ret = avfilter_request_frame(link->src->inputs[0])))
256
            return ret;
257
    } while (!yadif->cur);
258

    
259
    return 0;
260
}
261

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

    
267
    if (yadif->frame_pending)
268
        return 1;
269

    
270
    val = avfilter_poll_frame(link->src->inputs[0]);
271

    
272
    if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
273
        if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
274
            return ret;
275
        val = avfilter_poll_frame(link->src->inputs[0]);
276
    }
277
    assert(yadif->next);
278

    
279
    return val * ((yadif->mode&1)+1);
280
}
281

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

    
286
    if (yadif->prev) avfilter_unref_buffer(yadif->prev);
287
    if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
288
    if (yadif->next) avfilter_unref_buffer(yadif->next);
289
}
290

    
291
static int query_formats(AVFilterContext *ctx)
292
{
293
    static const enum PixelFormat pix_fmts[] = {
294
        PIX_FMT_YUV420P,
295
        PIX_FMT_GRAY8,
296
        PIX_FMT_NONE
297
    };
298

    
299
    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
300

    
301
    return 0;
302
}
303

    
304
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
305
{
306
    YADIFContext *yadif = ctx->priv;
307
    av_unused int cpu_flags = av_get_cpu_flags();
308

    
309
    yadif->mode = 0;
310
    yadif->parity = -1;
311

    
312
    if (args) sscanf(args, "%d:%d", &yadif->mode, &yadif->parity);
313

    
314
    yadif->filter_line = filter_line_c;
315
    if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
316
        yadif->filter_line = ff_yadif_filter_line_ssse3;
317
    else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
318
        yadif->filter_line = ff_yadif_filter_line_sse2;
319
    else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
320
        yadif->filter_line = ff_yadif_filter_line_mmx;
321

    
322
    av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d\n", yadif->mode, yadif->parity);
323

    
324
    return 0;
325
}
326

    
327
static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
328

    
329
AVFilter avfilter_vf_yadif = {
330
    .name          = "yadif",
331
    .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
332

    
333
    .priv_size     = sizeof(YADIFContext),
334
    .init          = init,
335
    .uninit        = uninit,
336
    .query_formats = query_formats,
337

    
338
    .inputs    = (AVFilterPad[]) {{ .name             = "default",
339
                                    .type             = AVMEDIA_TYPE_VIDEO,
340
                                    .start_frame      = start_frame,
341
                                    .get_video_buffer = get_video_buffer,
342
                                    .draw_slice       = null_draw_slice,
343
                                    .end_frame        = end_frame, },
344
                                  { .name = NULL}},
345

    
346
    .outputs   = (AVFilterPad[]) {{ .name             = "default",
347
                                    .type             = AVMEDIA_TYPE_VIDEO,
348
                                    .poll_frame       = poll_frame,
349
                                    .request_frame    = request_frame, },
350
                                  { .name = NULL}},
351
};