]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/filter/scalable_yadif/scalable_yadif.cpp
aa35e0aa44a2e2044495acb1c6a243e2acfb7259
[casparcg] / modules / ffmpeg / producer / filter / scalable_yadif / scalable_yadif.cpp
1 #include "../../../StdAfx.h"\r
2 \r
3 #include "scalable_yadif.h"\r
4 \r
5 \r
6 /*
7  * Copyright (C) 2006-2010 Michael Niedermayer <michaelni@gmx.at>
8  *               2010      James Darnley <james.darnley@gmail.com>
9  *
10  * FFmpeg is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 \r
25 #pragma warning(push, 0)
26
27
28 #undef NDEBUG
29 #include <assert.h>
30
31 typedef struct {
32     /**
33      * 0: send 1 frame for each frame
34      * 1: send 1 frame for each field
35      * 2: like 0 but skips spatial interlacing check
36      * 3: like 1 but skips spatial interlacing check
37      */
38     int mode;
39
40     /**
41      *  0: bottom field first
42      *  1: top field first
43      * -1: auto-detection
44      */
45     int parity;
46
47     int frame_pending;
48
49     /**
50      *  0: deinterlace all frames
51      *  1: only deinterlace frames marked as interlaced
52      */
53     int auto_enable;
54
55     AVFilterBufferRef *cur;
56     AVFilterBufferRef *next;
57     AVFilterBufferRef *prev;
58     AVFilterBufferRef *out;
59     void (*filter_line)(uint8_t *dst,
60                         uint8_t *prev, uint8_t *cur, uint8_t *next,
61                         int w, int prefs, int mrefs, int parity, int mode);
62
63     const AVPixFmtDescriptor *csp;
64 } YADIFContext;
65
66 #define CHECK(j)\
67     {   int score = FFABS(cur[mrefs-1+(j)] - cur[prefs-1-(j)])\
68                   + FFABS(cur[mrefs  +(j)] - cur[prefs  -(j)])\
69                   + FFABS(cur[mrefs+1+(j)] - cur[prefs+1-(j)]);\
70         if (score < spatial_score) {\
71             spatial_score= score;\
72             spatial_pred= (cur[mrefs  +(j)] + cur[prefs  -(j)])>>1;\
73
74 #define FILTER \
75     for (x = 0;  x < w; x++) { \
76         int c = cur[mrefs]; \
77         int d = (prev2[0] + next2[0])>>1; \
78         int e = cur[prefs]; \
79         int temporal_diff0 = FFABS(prev2[0] - next2[0]); \
80         int temporal_diff1 =(FFABS(prev[mrefs] - c) + FFABS(prev[prefs] - e) )>>1; \
81         int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
82         int diff = FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2); \
83         int spatial_pred = (c+e)>>1; \
84         int spatial_score = FFABS(cur[mrefs-1] - cur[prefs-1]) + FFABS(c-e) \
85                           + FFABS(cur[mrefs+1] - cur[prefs+1]) - 1; \
86  \
87         CHECK(-1) CHECK(-2) }} }} \
88         CHECK( 1) CHECK( 2) }} }} \
89  \
90         if (mode < 2) { \
91             int b = (prev2[2*mrefs] + next2[2*mrefs])>>1; \
92             int f = (prev2[2*prefs] + next2[2*prefs])>>1; \
93             int max = FFMAX3(d-e, d-c, FFMIN(b-c, f-e)); \
94             int min = FFMIN3(d-e, d-c, FFMAX(b-c, f-e)); \
95  \
96             diff = FFMAX3(diff, min, -max); \
97         } \
98  \
99         if (spatial_pred > d + diff) \
100            spatial_pred = d + diff; \
101         else if (spatial_pred < d - diff) \
102            spatial_pred = d - diff; \
103  \
104         dst[0] = spatial_pred; \
105  \
106         dst++; \
107         cur++; \
108         prev++; \
109         next++; \
110         prev2++; \
111         next2++; \
112     }
113
114 void filter_line_c(uint8_t *dst,
115                           uint8_t *prev, uint8_t *cur, uint8_t *next,
116                           int w, int prefs, int mrefs, int parity, int mode)
117 {
118     int x;
119     uint8_t *prev2 = parity ? prev : cur ;
120     uint8_t *next2 = parity ? cur  : next;
121
122     FILTER
123 }
124
125 void filter_line_c_16bit(uint16_t *dst,
126                                 uint16_t *prev, uint16_t *cur, uint16_t *next,
127                                 int w, int prefs, int mrefs, int parity, int mode)
128 {
129     int x;
130     uint16_t *prev2 = parity ? prev : cur ;
131     uint16_t *next2 = parity ? cur  : next;
132     mrefs /= 2;
133     prefs /= 2;
134
135     FILTER
136 }
137
138 void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
139                    int parity, int tff)
140 {
141     YADIFContext *yadif = (YADIFContext*)ctx->priv;
142     int y, i;
143
144     for (i = 0; i < yadif->csp->nb_components; i++) {
145         int w = dstpic->video->w;
146         int h = dstpic->video->h;
147         int refs = yadif->cur->linesize[i];
148         int df = (yadif->csp->comp[i].depth_minus1+1) / 8;
149
150         if (i) {
151         /* Why is this not part of the per-plane description thing? */
152             w >>= yadif->csp->log2_chroma_w;
153             h >>= yadif->csp->log2_chroma_h;
154         }
155
156         for (y = 0; y < h; y++) {
157             if ((y ^ parity) & 1) {
158                 uint8_t *prev = &yadif->prev->data[i][y*refs];
159                 uint8_t *cur  = &yadif->cur ->data[i][y*refs];
160                 uint8_t *next = &yadif->next->data[i][y*refs];
161                 uint8_t *dst  = &dstpic->data[i][y*dstpic->linesize[i]];
162                 int     mode  = y==1 || y+2==h ? 2 : yadif->mode;
163                 yadif->filter_line(dst, prev, cur, next, w, y+1<h ? refs : -refs, y ? -refs : refs, parity ^ tff, mode);
164             } else {
165                 memcpy(&dstpic->data[i][y*dstpic->linesize[i]],
166                        &yadif->cur->data[i][y*refs], w*df);
167             }
168         }
169     }
170     //__asm__ volatile("emms \n\t" : : : "memory");
171 }
172
173 AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
174 {
175     AVFilterBufferRef *picref;
176     int width = FFALIGN(w, 32);
177     int height= FFALIGN(h+2, 32);
178     int i;
179
180     picref = avfilter_default_get_video_buffer(link, perms, width, height);
181
182     picref->video->w = w;
183     picref->video->h = h;
184
185     for (i = 0; i < 3; i++)
186         picref->data[i] += picref->linesize[i];
187
188     return picref;
189 }
190
191 void return_frame(AVFilterContext *ctx, int is_second)
192 {
193     YADIFContext *yadif = (YADIFContext*)ctx->priv;
194     AVFilterLink *link= ctx->outputs[0];
195     int tff;
196
197     if (yadif->parity == -1) {
198         tff = yadif->cur->video->interlaced ?
199             yadif->cur->video->top_field_first : 1;
200     } else {
201         tff = yadif->parity^1;
202     }
203
204     if (is_second)
205         yadif->out = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
206                                                AV_PERM_REUSE, link->w, link->h);
207
208     if (!yadif->csp)
209         yadif->csp = &av_pix_fmt_descriptors[link->format];
210     if (yadif->csp->comp[0].depth_minus1 == 15)
211         yadif->filter_line = (decltype(yadif->filter_line))filter_line_c_16bit;
212
213     filter(ctx, yadif->out, tff ^ !is_second, tff);
214
215     if (is_second) {
216         if (yadif->next->pts != AV_NOPTS_VALUE &&
217             yadif->cur->pts != AV_NOPTS_VALUE) {
218             yadif->out->pts =
219                 (yadif->next->pts&yadif->cur->pts) +
220                 ((yadif->next->pts^yadif->cur->pts)>>1);
221         } else {
222             yadif->out->pts = AV_NOPTS_VALUE;
223         }
224         avfilter_start_frame(ctx->outputs[0], yadif->out);
225     }
226     avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
227     avfilter_end_frame(ctx->outputs[0]);
228
229     yadif->frame_pending = (yadif->mode&1) && !is_second;
230 }
231
232 void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
233 {
234     AVFilterContext *ctx = link->dst;
235     YADIFContext *yadif = (YADIFContext*)ctx->priv;
236
237     if (yadif->frame_pending)
238         return_frame(ctx, 1);
239
240     if (yadif->prev)
241         avfilter_unref_buffer(yadif->prev);
242     yadif->prev = yadif->cur;
243     yadif->cur  = yadif->next;
244     yadif->next = picref;
245
246     if (!yadif->cur)
247         return;
248
249     if (yadif->auto_enable && !yadif->cur->video->interlaced) {
250         yadif->out  = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
251         avfilter_unref_buffer(yadif->prev);
252         yadif->prev = NULL;
253         avfilter_start_frame(ctx->outputs[0], yadif->out);
254         return;
255     }
256
257     if (!yadif->prev)
258         yadif->prev = avfilter_ref_buffer(yadif->cur, AV_PERM_READ);
259
260     yadif->out = avfilter_get_video_buffer(ctx->outputs[0], AV_PERM_WRITE | AV_PERM_PRESERVE |
261                                        AV_PERM_REUSE, link->w, link->h);
262
263     avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
264     yadif->out->video->interlaced = 0;
265     avfilter_start_frame(ctx->outputs[0], yadif->out);
266 }
267
268 void end_frame(AVFilterLink *link)
269 {
270     AVFilterContext *ctx = link->dst;
271     YADIFContext *yadif = (YADIFContext*)ctx->priv;
272
273     if (!yadif->out)
274         return;
275
276     if (yadif->auto_enable && !yadif->cur->video->interlaced) {
277         avfilter_draw_slice(ctx->outputs[0], 0, link->h, 1);
278         avfilter_end_frame(ctx->outputs[0]);
279         return;
280     }
281
282     return_frame(ctx, 0);
283 }
284
285 int request_frame(AVFilterLink *link)
286 {
287     AVFilterContext *ctx = link->src;
288     YADIFContext *yadif = (YADIFContext*)ctx->priv;
289
290     if (yadif->frame_pending) {
291         return_frame(ctx, 1);
292         return 0;
293     }
294
295     do {
296         int ret;
297                 ret = avfilter_request_frame(link->src->inputs[0]);
298         if (ret)
299             return ret;
300     } while (!yadif->cur);
301
302     return 0;
303 }
304
305 int poll_frame(AVFilterLink *link)
306 {
307     YADIFContext *yadif = (YADIFContext*)link->src->priv;
308     int ret, val;
309
310     if (yadif->frame_pending)
311         return 1;
312
313     val = avfilter_poll_frame(link->src->inputs[0]);
314
315     if (val==1 && !yadif->next) { //FIXME change API to not requre this red tape
316         if ((ret = avfilter_request_frame(link->src->inputs[0])) < 0)
317             return ret;
318         val = avfilter_poll_frame(link->src->inputs[0]);
319     }
320     assert(yadif->next || !val);
321
322     if (yadif->auto_enable && yadif->next && !yadif->next->video->interlaced)
323         return val;
324
325     return val * ((yadif->mode&1)+1);
326 }
327
328 void uninit(AVFilterContext *ctx)
329 {
330     YADIFContext *yadif = (YADIFContext*)ctx->priv;
331
332     if (yadif->prev) avfilter_unref_buffer(yadif->prev);
333     if (yadif->cur ) avfilter_unref_buffer(yadif->cur );
334     if (yadif->next) avfilter_unref_buffer(yadif->next);
335 }
336
337 int query_formats(AVFilterContext *ctx)
338 {
339     static const enum PixelFormat pix_fmts[] = {
340         PIX_FMT_YUV420P,
341         PIX_FMT_YUV422P,
342         PIX_FMT_YUV444P,
343         PIX_FMT_YUV410P,
344         PIX_FMT_YUV411P,
345         PIX_FMT_GRAY8,
346         PIX_FMT_YUVJ420P,
347         PIX_FMT_YUVJ422P,
348         PIX_FMT_YUVJ444P,
349         AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
350         PIX_FMT_YUV440P,
351         PIX_FMT_YUVJ440P,
352         AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
353         AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
354         AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
355         PIX_FMT_NONE
356     };
357
358     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list((const int*)pix_fmts));
359
360     return 0;
361 }
362
363 int init(AVFilterContext *ctx, const char *args, void *opaque)
364 {
365     YADIFContext *yadif = (YADIFContext*)ctx->priv;
366     av_unused int cpu_flags = av_get_cpu_flags();
367
368     yadif->mode = 0;
369     yadif->parity = -1;
370     yadif->auto_enable = 0;
371     yadif->csp = NULL;
372
373     if (args) sscanf(args, "%d:%d:%d", &yadif->mode, &yadif->parity, &yadif->auto_enable);
374
375         \r
376         auto module = LoadLibrary(L"avfilter-2.dll");\r
377         static std::shared_ptr<void> lib(module, FreeLibrary);\r
378     yadif->filter_line = (decltype(yadif->filter_line))(GetProcAddress(module, "ff_yadif_filter_line_ssse3"));
379
380     //yadif->filter_line = filter_line_c;
381     //if (HAVE_SSSE3 && cpu_flags & AV_CPU_FLAG_SSSE3)
382     //    yadif->filter_line = ff_yadif_filter_line_ssse3;
383     //else if (HAVE_SSE && cpu_flags & AV_CPU_FLAG_SSE2)
384     //    yadif->filter_line = ff_yadif_filter_line_sse2;
385     //else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX)
386     //    yadif->filter_line = ff_yadif_filter_line_mmx;
387
388     av_log(ctx, AV_LOG_INFO, "mode:%d parity:%d auto_enable:%d\n", yadif->mode, yadif->parity, yadif->auto_enable);
389
390     return 0;
391 }
392
393 void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
394
395 void register_scalable_yadif()
396 {
397         static AVFilter avfilter_vf_syadif;
398     avfilter_vf_syadif.name          = "syadif";
399     avfilter_vf_syadif .description   = "Deinterlace the input image";
400
401     avfilter_vf_syadif.priv_size     = sizeof(YADIFContext);
402     avfilter_vf_syadif.init          = init;
403     avfilter_vf_syadif.uninit        = uninit;
404     avfilter_vf_syadif.query_formats = query_formats;
405
406         static AVFilterPad inputs[2];
407
408         inputs[0].name                          = "default";         
409         inputs[0].type                          = AVMEDIA_TYPE_VIDEO,
410     inputs[0].start_frame               = start_frame;
411     inputs[0].get_video_buffer  = get_video_buffer;
412     inputs[0].draw_slice                = null_draw_slice;
413     inputs[0].end_frame                 = end_frame;
414         inputs[1].name                          = nullptr;
415         
416         avfilter_vf_syadif.inputs = inputs;
417
418         static AVFilterPad outputs[2];
419
420         outputs[0].name                         = "default";
421         outputs[0].type                         = AVMEDIA_TYPE_VIDEO;
422         outputs[0].poll_frame           = poll_frame;
423         outputs[0].request_frame        = request_frame;
424         outputs[1].name                         = nullptr;
425         
426         avfilter_vf_syadif.outputs = outputs;
427         
428         avfilter_register(&avfilter_vf_syadif);
429 };