Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vf_yadif.c @ 48f7f29f

History | View | Annotate | Download (9.81 KB)

1
/*
2
 * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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/x86_cpu.h"
23
#include "libavutil/common.h"
24
#include "avfilter.h"
25
#include "yadif.h"
26

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

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

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

    
46
    int frame_pending;
47

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

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

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

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

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

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

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

    
108
        dst[0] = spatial_pred;
109

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

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

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

    
131
        for (y = 0; y < h; y++) {
132
            if ((y ^ parity) & 1) {
133
                uint8_t *prev = &yadif->prev->data[i][y*refs];
134
                uint8_t *cur  = &yadif->cur ->data[i][y*refs];
135
                uint8_t *next = &yadif->next->data[i][y*refs];
136
                uint8_t *dst  = &dstpic->data[i][y*dstpic->linesize[i]];
137
                yadif->filter_line(dst, prev, cur, next, w, refs, parity ^ tff, yadif->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+6, 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] += 3 * picref->linesize[i];
163

    
164
    return picref;
165
}
166

    
167
static void return_frame(AVFilterContext *ctx, int is_second){
168
    YADIFContext *yadif = ctx->priv;
169
    AVFilterLink *link= ctx->outputs[0];
170
    int tff = yadif->parity == -1 ? yadif->cur->video->top_field_first : (yadif->parity^1);
171

    
172
    if(is_second)
173
        yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
174
                                       AV_PERM_REUSE, link->w, link->h);
175

    
176
    filter(ctx, yadif->out, tff ^ !is_second, tff);
177

    
178
    if(is_second)
179
        avfilter_start_frame(ctx->outputs[0], yadif->out);
180
    avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
181
    avfilter_end_frame(ctx->outputs[0]);
182

    
183
    yadif->frame_pending= (yadif->mode&1) && !is_second;
184
}
185

    
186
static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
187
{
188
    AVFilterContext *ctx = link->dst;
189
    YADIFContext *yadif = ctx->priv;
190

    
191
    if(yadif->frame_pending)
192
        return_frame(ctx, 1);
193

    
194
    if (yadif->prev)
195
        avfilter_unref_buffer(yadif->prev);
196
    yadif->prev = yadif->cur;
197
    yadif->cur  = yadif->next;
198
    yadif->next = picref;
199

    
200
    if(!yadif->cur)
201
        return;
202

    
203
    if (!yadif->prev)
204
        yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
205

    
206
    yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
207
                                       AV_PERM_REUSE, link->w, link->h);
208

    
209
    yadif->out->pts = yadif->cur->pts;
210
    avfilter_start_frame(ctx->outputs[0], yadif->out);
211
}
212

    
213
static void end_frame(AVFilterLink *link)
214
{
215
    AVFilterContext *ctx = link->dst;
216
    YADIFContext *yadif = ctx->priv;
217

    
218
    if(!yadif->out)
219
        return;
220

    
221
    return_frame(ctx, 0);
222
}
223

    
224
static int request_frame(AVFilterLink *link)
225
{
226
    AVFilterContext *ctx = link->src;
227
    YADIFContext *yadif = ctx->priv;
228

    
229
    if(yadif->frame_pending){
230
        return_frame(ctx, 1);
231
        return 0;
232
    }
233

    
234
    do{
235
        int ret;
236

    
237
        if ((ret = avfilter_request_frame(link->src->inputs[0])))
238
            return ret;
239
    }while(!yadif->cur);
240

    
241
    return 0;
242
}
243

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

    
249
    if(yadif->frame_pending)
250
        return 1;
251

    
252
    val= avfilter_poll_frame(link->src->inputs[0]);
253

    
254
    if(val==1 && !yadif->next){ //FIXME change API to not requre this red tape
255
        if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
256
            return ret;
257
        val = avfilter_poll_frame(link->src->inputs[0]);
258
    }
259
    assert(yadif->next);
260

    
261
    return val * ((yadif->mode&1)+1);
262
}
263

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

    
268
    if (yadif->prev) avfilter_unref_buffer(yadif->prev);
269
    if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
270
    if (yadif->next) avfilter_unref_buffer(yadif->next);
271
}
272

    
273
static int query_formats(AVFilterContext *ctx)
274
{
275
    static const enum PixelFormat pix_fmts[] = {
276
        PIX_FMT_YUV420P,
277
        PIX_FMT_GRAY8,
278
        PIX_FMT_NONE
279
    };
280

    
281
    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
282

    
283
    return 0;
284
}
285

    
286
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
287
{
288
    YADIFContext *yadif = ctx->priv;
289
    av_unused int cpu_flags = av_get_cpu_flags();
290

    
291
    yadif->mode = 0;
292
    yadif->parity = -1;
293

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

    
296
    yadif->filter_line = filter_line_c;
297
#if HAVE_MMX
298
    if (cpu_flags & AV_CPU_FLAG_MMX)
299
        yadif->filter_line = ff_yadif_filter_line_mmx;
300
#endif
301

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

    
304
    return 0;
305
}
306

    
307
static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
308

    
309
AVFilter avfilter_vf_yadif = {
310
    .name          = "yadif",
311
    .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image"),
312

    
313
    .priv_size     = sizeof(YADIFContext),
314
    .init          = init,
315
    .uninit        = uninit,
316
    .query_formats = query_formats,
317

    
318
    .inputs    = (AVFilterPad[]) {{ .name             = "default",
319
                                    .type             = AVMEDIA_TYPE_VIDEO,
320
                                    .start_frame      = start_frame,
321
                                    .get_video_buffer = get_video_buffer,
322
                                    .draw_slice       = null_draw_slice,
323
                                    .end_frame        = end_frame, },
324
                                  { .name = NULL}},
325

    
326
    .outputs   = (AVFilterPad[]) {{ .name             = "default",
327
                                    .type             = AVMEDIA_TYPE_VIDEO,
328
                                    .poll_frame       = poll_frame,
329
                                    .request_frame    = request_frame, },
330
                                  { .name = NULL}},
331
};