]> git.sesse.net Git - movit/blob - deinterlace_effect.h
Use ryg's much faster fp16 conversion code.
[movit] / deinterlace_effect.h
1 #ifndef _MOVIT_DEINTERLACE_EFFECT_H
2 #define _MOVIT_DEINTERLACE_EFFECT_H 1
3
4 // YADIF deinterlacing filter (original by Michael Niedermayer, in MPlayer).
5 //
6 // Good deinterlacing is very hard. YADIF, despite its innocious-sounding
7 // name (Yet Another DeInterlacing Filter) is probably the most commonly
8 // used (non-trivial) deinterlacing filter in the open-source world.
9 // It works by trying to fill in the missing lines from neighboring ones
10 // (spatial interpolation), and then constrains that estimate within an
11 // interval found from previous and next frames (temporal interpolation).
12 // It's not very fast, even in GPU implementation, but 1080i60 -> 1080p60
13 // realtime conversion is well within range for a mid-range GPU.
14 //
15 // The inner workings of YADIF are poorly documented; implementation details
16 // are generally explained the .frag file. However, a few things should be
17 // mentioned here: YADIF has two modes, with and without a “spatial interlacing
18 // check” which basically allows more temporal change in areas of high detail.
19 // (The variant with the check corresponds to the original's modes 0 and 1, and
20 // the variant without to modes 2 and 3. The remaining difference is whether it
21 // is frame-doubling or not, which in Movit is up to the driver, not the
22 // filter.)
23 //
24 // Neither mode is perfect by any means. If the spatial check is off, the
25 // filter possesses the potentially nice quality that a static picture
26 // deinterlaces exactly to itself. (If it's on, there's some flickering
27 // on very fine vertical detail. The picture is nice and stable if no such
28 // detail is present, though.) But then, certain patterns, like horizontally
29 // scrolling text, leaves residues. Both have issues with diagonal lines at
30 // certain angles leaving stray pixels, although in practical applications,
31 // YADIF is pretty good.
32 //
33 // In general, having the spatial check on (the default) is the safe choice.
34 // However, if you are reasonably certain that the image comes from a video source
35 // (ie., no graphical overlays), or if the case of still images is particularly
36 // important for you (e.g., slides from a laptop), you could turn it off.
37 // It is slightly faster, although in practice, it does not mean all that much.
38 // You need to decide before finalize(), as the choice gets compiled into the shader.
39 //
40 // YADIF needs five fields as input; the previous two, the current one, and
41 // then the two next ones. (By convention, they come in that order, although if
42 // you reverse them, it doesn't matter, as the filter is symmetric. It _does_
43 // matter if you change the ordering in any other way, though.) They need to be
44 // of the same resolution, or the effect will assert-fail. If you cannot supply
45 // this, you could simply reuse the current field for previous/next as
46 // required; it won't be optimal in any way, but it also won't blow up on you.
47 //
48 // This requirement to “see the future” will mean you have an extra full frame
49 // of delay (33.3 ms at 60i, 40 ms at 50i). You will also need to tell the
50 // filter for each and every invocation if the current field (ie., the one in
51 // the middle input) is a top or bottom field (neighboring fields have opposite
52 // parity, so all the others are implicit).
53
54 #include <epoxy/gl.h>
55 #include <string>
56
57 #include "effect.h"
58
59 namespace movit {
60
61 class DeinterlaceEffect : public Effect {
62 public:
63         DeinterlaceEffect();
64         virtual std::string effect_type_id() const { return "DeinterlaceEffect"; }
65         std::string output_fragment_shader();
66
67         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
68
69         // First = before previous, second = previous, third = current,
70         // fourth = next, fifth = after next. These are treated symmetrically,
71         // though.
72         //
73         // Note that if you have interlaced _frames_ and not _fields_, you will
74         // need to pull them apart first, for instance with SliceEffect.
75         virtual unsigned num_inputs() const { return 5; }
76         virtual bool needs_texture_bounce() const { return true; }
77         virtual bool changes_output_size() const { return true; }
78
79         virtual AlphaHandling alpha_handling() const { return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK; }
80
81         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height);
82         virtual void get_output_size(unsigned *width, unsigned *height,
83                                      unsigned *virtual_width, unsigned *virtual_height) const;
84
85         enum FieldPosition { TOP = 0, BOTTOM = 1 };
86
87 private:
88         unsigned widths[5], heights[5];
89
90         // See file-level comment for explanation of this option.
91         bool enable_spatial_interlacing_check;
92
93         // Which field the current input (the middle one) is.
94         FieldPosition current_field_position;
95
96         // Offset for one pixel in the horizontal direction (1/width).
97         float inv_width;
98
99         // Vertical resolution of the output.
100         float num_lines;
101
102         // All of these offsets are vertical texel offsets; they are needed to adjust
103         // for the changed texel center as the number of lines double, and depend on
104         // <current_field_position>.
105
106         // For sampling unchanged lines from the current field.
107         float self_offset;
108
109         // For evaluating the low-pass filter (in the current field). Four taps.
110         float current_offset[2];
111
112         // For evaluating the high-pass filter (in the previous and next fields).
113         // Five taps, but evaluated twice since there are two fields.
114         float other_offset[3];
115 };
116
117 }  // namespace movit
118
119 #endif // !defined(_MOVIT_DEINTERLACE_EFFECT_H)