/src/ffmpeg/libavcodec/vp9lpf.c
Line | Count | Source |
1 | | /* |
2 | | * VP9 compatible video decoder |
3 | | * |
4 | | * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com> |
5 | | * Copyright (C) 2013 Clément Bœsch <u pkh me> |
6 | | * |
7 | | * This file is part of FFmpeg. |
8 | | * |
9 | | * FFmpeg is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public |
11 | | * License as published by the Free Software Foundation; either |
12 | | * version 2.1 of the License, or (at your option) any later version. |
13 | | * |
14 | | * FFmpeg is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | | * Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public |
20 | | * License along with FFmpeg; if not, write to the Free Software |
21 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | | */ |
23 | | |
24 | | #include "avcodec.h" |
25 | | #include "vp9dec.h" |
26 | | |
27 | | static av_always_inline void filter_plane_cols(VP9Context *s, int col, int ss_h, int ss_v, |
28 | | uint8_t *lvl, uint8_t (*mask)[4], |
29 | | uint8_t *dst, ptrdiff_t ls) |
30 | 12.7M | { |
31 | 12.7M | int y, x, bytesperpixel = s->bytesperpixel; |
32 | | |
33 | | // filter edges between columns (e.g. block1 | block2) |
34 | 55.9M | for (y = 0; y < 8; y += 2 << ss_v, dst += 16 * ls, lvl += 16 << ss_v) { |
35 | 43.1M | uint8_t *ptr = dst, *l = lvl, *hmask1 = mask[y], *hmask2 = mask[y + 1 + ss_v]; |
36 | 43.1M | unsigned hm1 = hmask1[0] | hmask1[1] | hmask1[2], hm13 = hmask1[3]; |
37 | 43.1M | unsigned hm2 = hmask2[1] | hmask2[2], hm23 = hmask2[3]; |
38 | 43.1M | unsigned hm = hm1 | hm2 | hm13 | hm23; |
39 | | |
40 | 166M | for (x = 1; hm & ~(x - 1); x <<= 1, ptr += 8 * bytesperpixel >> ss_h) { |
41 | 123M | if (col || x > 1) { |
42 | 108M | if (hm1 & x) { |
43 | 90.0M | int L = *l, H = L >> 4; |
44 | 90.0M | int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L]; |
45 | | |
46 | 90.0M | if (hmask1[0] & x) { |
47 | 11.6M | if (hmask2[0] & x) { |
48 | 11.2M | av_assert2(l[8 << ss_v] == L); |
49 | 11.2M | s->dsp.loop_filter_16[0](ptr, ls, E, I, H); |
50 | 11.2M | } else { |
51 | 353k | s->dsp.loop_filter_8[2][0](ptr, ls, E, I, H); |
52 | 353k | } |
53 | 78.4M | } else if (hm2 & x) { |
54 | 63.7M | L = l[8 << ss_v]; |
55 | 63.7M | H |= (L >> 4) << 8; |
56 | 63.7M | E |= s->filter_lut.mblim_lut[L] << 8; |
57 | 63.7M | I |= s->filter_lut.lim_lut[L] << 8; |
58 | 63.7M | s->dsp.loop_filter_mix2[!!(hmask1[1] & x)] |
59 | 63.7M | [!!(hmask2[1] & x)] |
60 | 63.7M | [0](ptr, ls, E, I, H); |
61 | 63.7M | } else { |
62 | 14.6M | s->dsp.loop_filter_8[!!(hmask1[1] & x)] |
63 | 14.6M | [0](ptr, ls, E, I, H); |
64 | 14.6M | } |
65 | 90.0M | } else if (hm2 & x) { |
66 | 466k | int L = l[8 << ss_v], H = L >> 4; |
67 | 466k | int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L]; |
68 | | |
69 | 466k | s->dsp.loop_filter_8[!!(hmask2[1] & x)] |
70 | 466k | [0](ptr + 8 * ls, ls, E, I, H); |
71 | 466k | } |
72 | 108M | } |
73 | 123M | if (ss_h) { |
74 | 24.3M | if (x & 0xAA) |
75 | 8.88M | l += 2; |
76 | 99.0M | } else { |
77 | 99.0M | if (hm13 & x) { |
78 | 50.2M | int L = *l, H = L >> 4; |
79 | 50.2M | int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L]; |
80 | | |
81 | 50.2M | if (hm23 & x) { |
82 | 40.8M | L = l[8 << ss_v]; |
83 | 40.8M | H |= (L >> 4) << 8; |
84 | 40.8M | E |= s->filter_lut.mblim_lut[L] << 8; |
85 | 40.8M | I |= s->filter_lut.lim_lut[L] << 8; |
86 | 40.8M | s->dsp.loop_filter_mix2[0][0][0](ptr + 4 * bytesperpixel, ls, E, I, H); |
87 | 40.8M | } else { |
88 | 9.36M | s->dsp.loop_filter_8[0][0](ptr + 4 * bytesperpixel, ls, E, I, H); |
89 | 9.36M | } |
90 | 50.2M | } else if (hm23 & x) { |
91 | 2.73M | int L = l[8 << ss_v], H = L >> 4; |
92 | 2.73M | int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L]; |
93 | | |
94 | 2.73M | s->dsp.loop_filter_8[0][0](ptr + 8 * ls + 4 * bytesperpixel, ls, E, I, H); |
95 | 2.73M | } |
96 | 99.0M | l++; |
97 | 99.0M | } |
98 | 123M | } |
99 | 43.1M | } |
100 | 12.7M | } |
101 | | |
102 | | static av_always_inline void filter_plane_rows(VP9Context *s, int row, int ss_h, int ss_v, |
103 | | uint8_t *lvl, uint8_t (*mask)[4], |
104 | | uint8_t *dst, ptrdiff_t ls) |
105 | 12.7M | { |
106 | 12.7M | int y, x, bytesperpixel = s->bytesperpixel; |
107 | | |
108 | | // block1 |
109 | | // filter edges between rows (e.g. ------) |
110 | | // block2 |
111 | 115M | for (y = 0; y < 8; y++, dst += 8 * ls >> ss_v) { |
112 | 102M | uint8_t *ptr = dst, *l = lvl, *vmask = mask[y]; |
113 | 102M | unsigned vm = vmask[0] | vmask[1] | vmask[2], vm3 = vmask[3]; |
114 | | |
115 | 217M | for (x = 1; vm & ~(x - 1); x <<= (2 << ss_h), ptr += 16 * bytesperpixel, l += 2 << ss_h) { |
116 | 115M | if (row || y) { |
117 | 103M | if (vm & x) { |
118 | 99.7M | int L = *l, H = L >> 4; |
119 | 99.7M | int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L]; |
120 | | |
121 | 99.7M | if (vmask[0] & x) { |
122 | 12.9M | if (vmask[0] & (x << (1 + ss_h))) { |
123 | 11.3M | av_assert2(l[1 + ss_h] == L); |
124 | 11.3M | s->dsp.loop_filter_16[1](ptr, ls, E, I, H); |
125 | 11.3M | } else { |
126 | 1.55M | s->dsp.loop_filter_8[2][1](ptr, ls, E, I, H); |
127 | 1.55M | } |
128 | 86.8M | } else if (vm & (x << (1 + ss_h))) { |
129 | 66.0M | L = l[1 + ss_h]; |
130 | 66.0M | H |= (L >> 4) << 8; |
131 | 66.0M | E |= s->filter_lut.mblim_lut[L] << 8; |
132 | 66.0M | I |= s->filter_lut.lim_lut[L] << 8; |
133 | 66.0M | s->dsp.loop_filter_mix2[!!(vmask[1] & x)] |
134 | 66.0M | [!!(vmask[1] & (x << (1 + ss_h)))] |
135 | 66.0M | [1](ptr, ls, E, I, H); |
136 | 66.0M | } else { |
137 | 20.8M | s->dsp.loop_filter_8[!!(vmask[1] & x)] |
138 | 20.8M | [1](ptr, ls, E, I, H); |
139 | 20.8M | } |
140 | 99.7M | } else if (vm & (x << (1 + ss_h))) { |
141 | 367k | int L = l[1 + ss_h], H = L >> 4; |
142 | 367k | int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L]; |
143 | | |
144 | 367k | s->dsp.loop_filter_8[!!(vmask[1] & (x << (1 + ss_h)))] |
145 | 367k | [1](ptr + 8 * bytesperpixel, ls, E, I, H); |
146 | 367k | } |
147 | 103M | } |
148 | 115M | if (!ss_v) { |
149 | 85.8M | if (vm3 & x) { |
150 | 46.7M | int L = *l, H = L >> 4; |
151 | 46.7M | int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L]; |
152 | | |
153 | 46.7M | if (vm3 & (x << (1 + ss_h))) { |
154 | 35.9M | L = l[1 + ss_h]; |
155 | 35.9M | H |= (L >> 4) << 8; |
156 | 35.9M | E |= s->filter_lut.mblim_lut[L] << 8; |
157 | 35.9M | I |= s->filter_lut.lim_lut[L] << 8; |
158 | 35.9M | s->dsp.loop_filter_mix2[0][0][1](ptr + ls * 4, ls, E, I, H); |
159 | 35.9M | } else { |
160 | 10.7M | s->dsp.loop_filter_8[0][1](ptr + ls * 4, ls, E, I, H); |
161 | 10.7M | } |
162 | 46.7M | } else if (vm3 & (x << (1 + ss_h))) { |
163 | 2.46M | int L = l[1 + ss_h], H = L >> 4; |
164 | 2.46M | int E = s->filter_lut.mblim_lut[L], I = s->filter_lut.lim_lut[L]; |
165 | | |
166 | 2.46M | s->dsp.loop_filter_8[0][1](ptr + ls * 4 + 8 * bytesperpixel, ls, E, I, H); |
167 | 2.46M | } |
168 | 85.8M | } |
169 | 115M | } |
170 | 102M | if (ss_v) { |
171 | 31.7M | if (y & 1) |
172 | 15.8M | lvl += 16; |
173 | 70.5M | } else { |
174 | 70.5M | lvl += 8; |
175 | 70.5M | } |
176 | 102M | } |
177 | 12.7M | } |
178 | | |
179 | | void ff_vp9_loopfilter_sb(AVCodecContext *avctx, VP9Filter *lflvl, |
180 | | int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff) |
181 | 4.25M | { |
182 | 4.25M | VP9Context *s = avctx->priv_data; |
183 | 4.25M | AVFrame *f = s->s.frames[CUR_FRAME].tf.f; |
184 | 4.25M | uint8_t *dst = f->data[0] + yoff; |
185 | 4.25M | ptrdiff_t ls_y = f->linesize[0], ls_uv = f->linesize[1]; |
186 | 4.25M | uint8_t (*uv_masks)[8][4] = lflvl->mask[s->ss_h | s->ss_v]; |
187 | 4.25M | int p; |
188 | | |
189 | | /* FIXME: In how far can we interleave the v/h loopfilter calls? E.g. |
190 | | * if you think of them as acting on a 8x8 block max, we can interleave |
191 | | * each v/h within the single x loop, but that only works if we work on |
192 | | * 8 pixel blocks, and we won't always do that (we want at least 16px |
193 | | * to use SSE2 optimizations, perhaps 32 for AVX2) */ |
194 | | |
195 | 4.25M | filter_plane_cols(s, col, 0, 0, lflvl->level, lflvl->mask[0][0], dst, ls_y); |
196 | 4.25M | filter_plane_rows(s, row, 0, 0, lflvl->level, lflvl->mask[0][1], dst, ls_y); |
197 | | |
198 | 12.7M | for (p = 0; p < 2; p++) { |
199 | 8.51M | dst = f->data[1 + p] + uvoff; |
200 | 8.51M | filter_plane_cols(s, col, s->ss_h, s->ss_v, lflvl->level, uv_masks[0], dst, ls_uv); |
201 | 8.51M | filter_plane_rows(s, row, s->ss_h, s->ss_v, lflvl->level, uv_masks[1], dst, ls_uv); |
202 | 8.51M | } |
203 | 4.25M | } |