Debian Codegraph @

src/filter/d90stairsteppingfix/d90stairsteppingfix.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2010 Simon Andreas Eugster ([email protected])
3 * This file is a Frei0r plugin.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /**
21 * Videos recorded with the Nikon D90 and Firmware 1.0 (which might never
22 * be updated) show nasty steps in slanting lines.
23 *
24 * Lee Wilson's post about how to fix this was the first solution I found
25 * in the web:
26 * http://www.dvxuser.com/V6/showthread.php?t=149663
27 * Thank you very much.
28 *
29 * Mike Martucci used some C code to remove the steps together with
30 * ffmpeg, to be found here: http://www.epsilonic.com/d90fix/.
31 *
32 * The «magic numbers», called slices here, have been developed by
33 * buildyo and cgipro, also in the dvxuser forum.
34 *
35 *
36 * This plugin «skips» the scaling step by using linear interpolation
37 * on the whole image directly.
38 *
39 * More accurate:
40 * The PROBLEM is that Nikon did not scale the videos directly but
41 * simply used some pixel rows and skipped others, as it seems.
42 * In regular distances the number of skipped rows is higher than
43 * usual, creating a gap that leads to steps on sloped lines.
44 *
45 * The SLICES describe rows belonging together, i.e. not having a
46 * big gap between.
47 *
48 * The IDEA behind fixing the stair steps is to insert
49 * a new line between two slices (there are a total of 82 slices),
50 * filling the line by interpolation, and scaling the image
51 * (which is now 720+81 pixels high) down to a height of 720 pixels
52 * again. (Only rows are affected, columns are not, mystically.)
53 *
54 *
55 * This filling followed by scaling can also be done
56 * WITHOUT SCALING.
57 *
58 * Terminology:
59 * H original height
60 * N number of slice lines (#slices-1)
61 *
62 * 1 Create a MESH containing points representing the position of each
63 * row. Initially it is just [0 1 2 3 4 ...].
64 * Length: H
65 *
66 * 2 Insert the SLICE LINES into this mesh after each slice. If e.g.
67 * the first slice is of size 3, then we get [0 1 2 2.5 3 4 ...].
68 * The new slice line is located between the 3rd and 4th line
69 * (i.e. line 2 and line 3). 2.5 will be interpolated using
70 * LINEAR INTERPOLATION:
71 * 0.5 * the values of line 2
72 * 0.5 * the values of line 3
73 * Analogous would a value of 2.1 mean
74 * 0.9 * the value of line 2
75 * 0.1 * the value of line 3
76 * Length: H+N
77 *
78 * 3 SCALE the mesh BACK to size H again.
79 * The mesh might now look like that:
80 * [0.08 1.25 2.2 2.8 3.75 4.9 ...]
81 * It gets denser around the inserted slice line and looser in the
82 * middle of the slices.
83 * Length: H
84 *
85 * This mesh can be reused now for every frame. What needs to be done
86 * for each frame is
87 * 4 Interpolate the color values using the mesh values. Continuing
88 * the example above, this e.g. means for the pixels in the first
89 * row (line) of the target frame:
90 * For each color, the new color is
91 * 0.92 * color of line 0
92 * 0.08 * color of line 1
93 * And, for the second row:
94 * For each color, the new color value is
95 * 0.75 * color of line 1
96 * 0.25 * color of line 2
97 * Note that for a mesh value a.b,
98 * a is the lower line number and
99 * (1-b) is the factor for the lower line.
100 *
101 */
102
103 #include "frei0r.hpp"cxx . . $ `<file>/include/frei0r.hpp`/
104
105 #include <stdio.h>
106 #include <math.h>
107
108 static int slices720pcxx . . $ slices720p.[] = {7,9,9,8,9,9,9,9,9,8,9,9,9,9,8,9,9,9,9,9,8,9,9,9,9,
109 9,8,9,9,9,9,9,8,9,9,9,9,9,8,9,9,9,9,8,9,9,9,9,9,8,
110 9,9,9,9,9,8,9,9,9,9,9,8,9,9,9,9,9,8,9,9,9,9,8,9,9,
111 9,9,9,8,9,9,7};
112
113
114 class D90StairsteppingFixcxx . . $ D90StairsteppingFix# : public frei0rcxx . . $ frei0r/::filtercxx . . $ frei0r/filter#
115 {
116
117 public:
118 D90StairsteppingFixcxx . . $ D90StairsteppingFix#D90StairsteppingFix(b4d55b1286ff73e8).(unsigned int width, unsigned int height)
119 {
120 m_meshcxx . . $ D90StairsteppingFix#m_mesh. = new float[height];
121
122 if (height == 720) {
123
124 /** Number of newly inserted lines: always between two slices (so #slices-1) */
125 int sliceLinesNumber = (sizeof slices720pcxx . . $ slices720p.)/(sizeof slices720pcxx . . $ slices720p.[0]) - 1;
126
127 /** The height of the image after inserting the slice lines */
128 int newHeight = height + sliceLinesNumber;
129
130 // printf("%d slice lines, %d total new lines\n", sliceLinesNumber, newHeight);
131
132 /**
133 * The position of each line including slice lines.
134 * Slice lines are inserted between two lines (e.g. between 6 and 7)
135 * and therefore get the number (line1+line2)/2, here 6.5.
136 * This positions will later be used for interpolation.
137 */
138 float *filled = (float *) malloc(newHeight * sizeof(float));
139
140 int count = 0;
141 int index = 0;
142 for (int i = 0; i < sliceLinesNumber+1; i++) {
143 for (int j = 0; j < slices720pcxx . . $ slices720p.[i]; j++) {
144
145 filled[index] = count;
146 count++;
147 index++;
148
149 }
150 if (count < newHeight) {
151 filled[index] = count - 0.5;
152 index++;
153 }
154 }
155 // for (int i = 0; i < newHeight; i++) {
156 // printf("inserted Lines: %f at %d\n", filled[i], i);
157 // }
158
159 /**
160 * Calculate scaling numbers to scale the full height matrix
161 * with the slice lines down to the original height (720p).
162 */
163 float *downScaling = (float *) malloc(height * sizeof(float));
164
165 float scaleFactor = (float) newHeight/height;
166 // printf("scale factor: %f\n", scaleFactor);
167 for (unsigned int i = 0; i < height; i++) {
168 downScaling[i] = (float) (((2*i+1)*scaleFactor)-1)/2;
169 // printf("scaled: %f at %d\n", downScaling[i], i);
170 }
171
172
173 /**
174 * Finish the mesh by scaling the H+N sized mesh to size H,
175 * using linear interpolation and the previously
176 * calculated scaling numbers.
177 */
178 float offset;
179 for (unsigned int i = 0; i < height; i++) {
180
181 index = floorcxx . . $ floor(fbcf3211dfe1bfd1).(downScaling[i]);
182 offset = downScaling[i] - index;
183
184 m_meshcxx . . $ D90StairsteppingFix#m_mesh.[i] = (1-offset)*filled[index] + offset*filled[index+1];
185 // printf("%f at %d with weights %f and %f\n", m_mesh[i], i, (1-offset)*downScaling[i], offset*downScaling[i+1]);
186 }
187
188 free(downScaling);
189 free(filled);
190
191 } else {
192 // Not a 720p file.
193 }
194 }
195
196 ~cxx . . $ D90StairsteppingFix#`~D90StairsteppingFix`(49f6e7a06ebc5aa8).D90StairsteppingFixcxx . . $ D90StairsteppingFix#()
197 {
198 delete[] m_meshcxx . . $ D90StairsteppingFix#m_mesh.;
199 }
200
201 virtual void updatecxx . . $ D90StairsteppingFix#update(da7b4a17ceb4e697).(double time,
202 uint32_t* out,
203 const uint32_t* in)
204 {
205
206 if (heightcxx . . $ frei0r/fx#height. == 720) {
207 // printf("Converting.\n");
208
209 float factor;
210 int index;
211 unsigned char *cvA, *cvB, *cvOut;
212
213
214 for (unsigned int line = 0; line < heightcxx . . $ frei0r/fx#height.; line++) {
215 index = floorcxx . . $ floor(fbcf3211dfe1bfd1).(m_meshcxx . . $ D90StairsteppingFix#m_mesh.[line]);
216 factor = (float) m_meshcxx . . $ D90StairsteppingFix#m_mesh.[line] - index;
217
218 cvA = (unsigned char*) &in[widthcxx . . $ frei0r/fx#width.*index];
219 cvB = (unsigned char*) &in[widthcxx . . $ frei0r/fx#width.*(index+1)];
220 cvOut = (unsigned char*) &out[widthcxx . . $ frei0r/fx#width.*line];
221
222 for (unsigned int pixel = 0; pixel < widthcxx . . $ frei0r/fx#width.*4; pixel++) {
223 // Use linear interpolation on the colours
224
225 // Use pointer arithmetic. Colour values are stored
226 // as AABBGGRR in the uint32_t values.
227 // Convert each colour separately.
228
229 *cvOut = floorcxx . . $ floor(fbcf3211dfe1bfd1).((float)(1-factor)*(*cvA) + (factor)*(*cvB));
230 cvA++;
231 cvB++;
232 cvOut++;
233 }
234 }
235 std::copycxx . . $ std/copy(88304b602929f6f4).(in + widthcxx . . $ frei0r/fx#width.*(heightcxx . . $ frei0r/fx#height.-1), in+widthcxx . . $ frei0r/fx#width.*heightcxx . . $ frei0r/fx#height., out + widthcxx . . $ frei0r/fx#width.*(heightcxx . . $ frei0r/fx#height.-1));
236 } else {
237 // Not a 720p file -> Cannot work, do nothing.
238 // printf("Just copying. Height is %d.\n", height);
239 std::copycxx . . $ std/copy(88304b602929f6f4).(in, in + widthcxx . . $ frei0r/fx#width.*heightcxx . . $ frei0r/fx#height., out);
240 }
241 }
242
243 private:
244 float *m_meshcxx . . $ D90StairsteppingFix#m_mesh.;
245
246 };
247
248
249
250 frei0rcxx . . $ frei0r/::constructcxx . . $ frei0r/construct#<D90StairsteppingFixcxx . . $ D90StairsteppingFix#> plugincxx . . $ plugin.("Nikon D90 Stairstepping fix"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
251 "Removes the Stairstepping from Nikon D90 videos (720p only) by interpolation"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
252 "Simon A. Eugster (Granjow)"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
253 0,2,
254 F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!);