Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(423004)

Side by Side Diff: examples/prefilter.c

Issue 547: Added filter prior to encoding giving BDR gains on certain sequences like KristenAndSara.
Patch Set: Added filter prior to encoding giving BDR gains on certain sequences like KristenAndSara. Created 4 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « examples/encoder_example.c ('k') | examples/prefilter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*Daala video codec
3 Copyright (c) 2006-2014 Daala project contributors. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 - Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
19 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <string.h>
31
32 static long long iabs(long long a) { return a > 0 ? a : -a; }
33 static int min(int a, int b) { return a < b ? a : b; }
34
35 typedef struct {
36 unsigned char *yf;
37 unsigned char *uf;
38 unsigned char *vf;
39 unsigned char *yc;
40 unsigned char *uc;
41 unsigned char *vc;
42 unsigned char *yp;
43 unsigned char *up;
44 unsigned char *vp;
45 int w;
46 int h;
47 int strength; /* 256 is a reasonable value */
48 } filter_data;
49
50
51 static int check(int s, unsigned char *filt, unsigned char *curr,
52 unsigned char *prev, int stride, int threshold, int strength) {
53 long long csd = 0;
54 int curr_avg = 0;
55 int prev_avg = 0;
56 int x;
57 int y;
58
59 for (y = 0; y < s; y++)
60 for (x = 0; x < s; x++) {
61 int diff = curr[y*stride + x] - prev[y*stride + x];
62 csd += iabs(diff*diff*diff);
63 curr_avg += curr[y*stride + x];
64 prev_avg += prev[y*stride + x];
65 if (filt) { /* Apply temporal filter */
66 int temporal_coeff = 3;
67 unsigned int w = min(iabs(curr[y*stride + x] - prev[y*stride + x]),
68 (1 << temporal_coeff) - 1) + 1;
69 filt[y*stride + x] = ((w * curr[y*stride + x]
70 + ((1 << temporal_coeff) - w) * filt[y*stride + x])
71 + (1 << (temporal_coeff - 1))) / (1 << temporal_coeff);
72 }
73 }
74
75 return (curr_avg > prev_avg ? curr_avg - prev_avg : prev_avg - curr_avg) <
76 s*s && csd < s*s*threshold*threshold*threshold*strength/256;
77 }
78
79 static void memcpy2d(unsigned char *dest, unsigned char *src,
80 int size, int stride) {
81 int y;
82 for (y = 0; y < size; y++) {
83 memcpy(dest + y*stride, src + y*stride, size);
84 }
85 }
86
87 static void filter_block(int size, filter_data *data,
88 int x, int y, int threshold) {
89 int yoff = y * data->w + x;
90 int uvoff = y/2 * data->w/2 + x/2;
91 unsigned char *yp = data->yp;
92 unsigned char *up = data->up;
93 unsigned char *vp = data->vp;
94
95 /* Static video? */
96 if (x + size < data->w && y + size < data->h &&
97 check(size, data->yf + yoff, data->yc + yoff, yp + yoff, data->w,
98 threshold, data->strength) &&
99 check(size/2, data->uf + uvoff, data->uc + uvoff, up + uvoff, data->w/2,
100 threshold, data->strength) &&
101 check(size/2, data->vf + uvoff, data->vc + uvoff, vp + uvoff, data->w/2,
102 threshold, data->strength)) {
103
104 /* Use temporally filtered video if diff is small */
105 if (check(size, 0, data->yc + yoff, data->yf + yoff, data->w,
106 threshold, data->strength) &&
107 check(size/2, 0, data->uc + uvoff, data->uf + uvoff, data->w/2,
108 threshold, data->strength) &&
109 check(size/2, 0, data->vc + uvoff, data->vf + uvoff, data->w/2,
110 threshold, data->strength)) {
111 yp = data->yf;
112 up = data->uf;
113 vp = data->vf;
114 }
115
116 /* Reuse previous (filtered) frame */
117 memcpy2d(data->yc + yoff, yp + yoff, size, data->w);
118 memcpy2d(data->uc + uvoff, up + uvoff, size/2, data->w/2);
119 memcpy2d(data->vc + uvoff, vp + uvoff, size/2, data->w/2);
120 return;
121 }
122
123 if (size > 2) {
124 int a;
125 int b;
126 for (a = 0; a < size; a += size >> 1) {
127 for (b = 0; b < size; b += size >> 1) {
128 filter_block(size >> 1, data, x + b, y + a, threshold);
129 }
130 }
131 }
132 }
133
134 /* Apply a filter helping the encoder to avoid spending bits on static video */
135 void pre_filter(unsigned char *yf, unsigned char *uf, unsigned char *vf,
136 unsigned char *yc, unsigned char *uc, unsigned char *vc,
137 unsigned char *yp, unsigned char *up, unsigned char *vp,
138 int w, int h, int strength) {
139 /* filter strength is modulated over time so that filtered output
140 eventually converges toward the input. */
141 static char gop[] = { 2, 4, 5, 4, 5, 4, 5, 4 };
142 static int frame = 0;
143 int threshold = gop[frame++ & 7];
144 int a;
145 int b;
146 filter_data data;
147
148 data.yf = yf;
149 data.uf = uf;
150 data.vf = vf;
151 data.yc = yc;
152 data.uc = uc;
153 data.vc = vc;
154 data.yp = yp;
155 data.up = up;
156 data.vp = vp;
157 data.w = w;
158 data.h = h;
159 data.strength = strength;
160
161 for (a = 0; a < h; a += 128) {
162 for (b = 0; b < w; b += 128) {
163 filter_block(128, &data, b, a, threshold);
164 }
165 }
166 }
OLDNEW
« no previous file with comments | « examples/encoder_example.c ('k') | examples/prefilter.h » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld