FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_pp7.c
Date: 2024-04-23 16:28:37
Exec Total Coverage
Lines: 137 177 77.4%
Functions: 8 10 80.0%
Branches: 58 91 63.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2014 Arwa Arif <arwaarif1994@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 /**
23 * @file
24 * Postprocessing filter - 7
25 *
26 * Originally written by Michael Niedermayer for the MPlayer
27 * project, and ported by Arwa Arif for FFmpeg.
28 */
29
30 #include "libavutil/emms.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/mem_internal.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36 #include "internal.h"
37 #include "qp_table.h"
38 #include "vf_pp7.h"
39 #include "video.h"
40
41 enum mode {
42 MODE_HARD,
43 MODE_SOFT,
44 MODE_MEDIUM
45 };
46
47 #define OFFSET(x) offsetof(PP7Context, x)
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49 static const AVOption pp7_options[] = {
50 { "qp", "force a constant quantizer parameter", OFFSET(qp), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 64, FLAGS },
51 { "mode", "set thresholding mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_MEDIUM}, 0, 2, FLAGS, .unit = "mode" },
52 { "hard", "hard thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_HARD}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
53 { "soft", "soft thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_SOFT}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
54 { "medium", "medium thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_MEDIUM}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
55 { NULL }
56 };
57
58 AVFILTER_DEFINE_CLASS(pp7);
59
60 DECLARE_ALIGNED(8, static const uint8_t, dither)[8][8] = {
61 { 0, 48, 12, 60, 3, 51, 15, 63, },
62 { 32, 16, 44, 28, 35, 19, 47, 31, },
63 { 8, 56, 4, 52, 11, 59, 7, 55, },
64 { 40, 24, 36, 20, 43, 27, 39, 23, },
65 { 2, 50, 14, 62, 1, 49, 13, 61, },
66 { 34, 18, 46, 30, 33, 17, 45, 29, },
67 { 10, 58, 6, 54, 9, 57, 5, 53, },
68 { 42, 26, 38, 22, 41, 25, 37, 21, },
69 };
70
71 #define N0 4
72 #define N1 5
73 #define N2 10
74 #define SN0 2
75 #define SN1 2.2360679775
76 #define SN2 3.16227766017
77 #define N (1 << 16)
78
79 static const int factor[16] = {
80 N / (N0 * N0), N / (N0 * N1), N / (N0 * N0), N / (N0 * N2),
81 N / (N1 * N0), N / (N1 * N1), N / (N1 * N0), N / (N1 * N2),
82 N / (N0 * N0), N / (N0 * N1), N / (N0 * N0), N / (N0 * N2),
83 N / (N2 * N0), N / (N2 * N1), N / (N2 * N0), N / (N2 * N2),
84 };
85
86 1 static void init_thres2(PP7Context *p)
87 {
88 int qp, i;
89 1 int bias = 0; //FIXME
90
91
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 1 times.
100 for (qp = 0; qp < 99; qp++) {
92
2/2
✓ Branch 0 taken 1584 times.
✓ Branch 1 taken 99 times.
1683 for (i = 0; i < 16; i++) {
93
6/6
✓ Branch 0 taken 792 times.
✓ Branch 1 taken 792 times.
✓ Branch 2 taken 792 times.
✓ Branch 3 taken 792 times.
✓ Branch 4 taken 1568 times.
✓ Branch 5 taken 16 times.
1584 p->thres2[qp][i] = ((i&1) ? SN2 : SN0) * ((i&4) ? SN2 : SN0) * FFMAX(1, qp) * (1<<2) - 1 - bias;
94 }
95 }
96 1 }
97
98 195840 static inline void dctA_c(int16_t *dst, uint8_t *src, int stride)
99 {
100 int i;
101
102
2/2
✓ Branch 0 taken 783360 times.
✓ Branch 1 taken 195840 times.
979200 for (i = 0; i < 4; i++) {
103 783360 int s0 = src[0 * stride] + src[6 * stride];
104 783360 int s1 = src[1 * stride] + src[5 * stride];
105 783360 int s2 = src[2 * stride] + src[4 * stride];
106 783360 int s3 = src[3 * stride];
107 783360 int s = s3 + s3;
108 783360 s3 = s - s0;
109 783360 s0 = s + s0;
110 783360 s = s2 + s1;
111 783360 s2 = s2 - s1;
112 783360 dst[0] = s0 + s;
113 783360 dst[2] = s0 - s;
114 783360 dst[1] = 2 * s3 + s2;
115 783360 dst[3] = s3 - 2 * s2;
116 783360 src++;
117 783360 dst += 4;
118 }
119 195840 }
120
121 760320 static void dctB_c(int16_t *dst, int16_t *src)
122 {
123 int i;
124
125
2/2
✓ Branch 0 taken 3041280 times.
✓ Branch 1 taken 760320 times.
3801600 for (i = 0; i < 4; i++) {
126 3041280 int s0 = src[0 * 4] + src[6 * 4];
127 3041280 int s1 = src[1 * 4] + src[5 * 4];
128 3041280 int s2 = src[2 * 4] + src[4 * 4];
129 3041280 int s3 = src[3 * 4];
130 3041280 int s = s3 + s3;
131 3041280 s3 = s - s0;
132 3041280 s0 = s + s0;
133 3041280 s = s2 + s1;
134 3041280 s2 = s2 - s1;
135 3041280 dst[0 * 4] = s0 + s;
136 3041280 dst[2 * 4] = s0 - s;
137 3041280 dst[1 * 4] = 2 * s3 + s2;
138 3041280 dst[3 * 4] = s3 - 2 * s2;
139 3041280 src++;
140 3041280 dst++;
141 }
142 760320 }
143
144 static int hardthresh_c(PP7Context *p, int16_t *src, int qp)
145 {
146 int i;
147 int a;
148
149 a = src[0] * factor[0];
150 for (i = 1; i < 16; i++) {
151 unsigned int threshold1 = p->thres2[qp][i];
152 unsigned int threshold2 = threshold1 << 1;
153 int level = src[i];
154 if (((unsigned)(level + threshold1)) > threshold2)
155 a += level * factor[i];
156 }
157 return (a + (1 << 11)) >> 12;
158 }
159
160 760320 static int mediumthresh_c(PP7Context *p, int16_t *src, int qp)
161 {
162 int i;
163 int a;
164
165 760320 a = src[0] * factor[0];
166
2/2
✓ Branch 0 taken 11404800 times.
✓ Branch 1 taken 760320 times.
12165120 for (i = 1; i < 16; i++) {
167 11404800 unsigned int threshold1 = p->thres2[qp][i];
168 11404800 unsigned int threshold2 = threshold1 << 1;
169 11404800 int level = src[i];
170
2/2
✓ Branch 0 taken 5589798 times.
✓ Branch 1 taken 5815002 times.
11404800 if (((unsigned)(level + threshold1)) > threshold2) {
171
2/2
✓ Branch 0 taken 3897600 times.
✓ Branch 1 taken 1692198 times.
5589798 if (((unsigned)(level + 2 * threshold1)) > 2 * threshold2)
172 3897600 a += level * factor[i];
173 else {
174
2/2
✓ Branch 0 taken 845122 times.
✓ Branch 1 taken 847076 times.
1692198 if (level > 0)
175 845122 a += 2 * (level - (int)threshold1) * factor[i];
176 else
177 847076 a += 2 * (level + (int)threshold1) * factor[i];
178 }
179 }
180 }
181 760320 return (a + (1 << 11)) >> 12;
182 }
183
184 static int softthresh_c(PP7Context *p, int16_t *src, int qp)
185 {
186 int i;
187 int a;
188
189 a = src[0] * factor[0];
190 for (i = 1; i < 16; i++) {
191 unsigned int threshold1 = p->thres2[qp][i];
192 unsigned int threshold2 = threshold1 << 1;
193 int level = src[i];
194 if (((unsigned)(level + threshold1)) > threshold2) {
195 if (level > 0)
196 a += (level - (int)threshold1) * factor[i];
197 else
198 a += (level + (int)threshold1) * factor[i];
199 }
200 }
201 return (a + (1 << 11)) >> 12;
202 }
203
204 15 static void filter(PP7Context *p, uint8_t *dst, uint8_t *src,
205 int dst_stride, int src_stride,
206 int width, int height,
207 uint8_t *qp_store, int qp_stride, int is_luma)
208 {
209 int x, y;
210
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 10 times.
15 const int stride = is_luma ? p->temp_stride : ((width + 16 + 15) & (~15));
211 15 uint8_t *p_src = p->src + 8 * stride;
212 15 int16_t *block = (int16_t *)p->src;
213 15 int16_t *temp = (int16_t *)(p->src + 32);
214
215
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
15 if (!src || !dst) return;
216
2/2
✓ Branch 0 taken 2880 times.
✓ Branch 1 taken 15 times.
2895 for (y = 0; y < height; y++) {
217 2880 int index = 8 + 8 * stride + y * stride;
218 2880 memcpy(p_src + index, src + y * src_stride, width);
219
2/2
✓ Branch 0 taken 23040 times.
✓ Branch 1 taken 2880 times.
25920 for (x = 0; x < 8; x++) {
220 23040 p_src[index - x - 1]= p_src[index + x ];
221 23040 p_src[index + width + x ]= p_src[index + width - x - 1];
222 }
223 }
224
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 15 times.
135 for (y = 0; y < 8; y++) {
225 120 memcpy(p_src + ( 7 - y ) * stride, p_src + ( y + 8 ) * stride, stride);
226 120 memcpy(p_src + (height + 8 + y) * stride, p_src + (height - y + 7) * stride, stride);
227 }
228 //FIXME (try edge emu)
229
230
2/2
✓ Branch 0 taken 2880 times.
✓ Branch 1 taken 15 times.
2895 for (y = 0; y < height; y++) {
231
2/2
✓ Branch 0 taken 5760 times.
✓ Branch 1 taken 2880 times.
8640 for (x = -8; x < 0; x += 4) {
232 5760 const int index = x + y * stride + (8 - 3) * (1 + stride) + 8; //FIXME silly offset
233 5760 uint8_t *src = p_src + index;
234 5760 int16_t *tp = temp + 4 * x;
235
236 5760 dctA_c(tp + 4 * 8, src, stride);
237 }
238
2/2
✓ Branch 0 taken 95040 times.
✓ Branch 1 taken 2880 times.
97920 for (x = 0; x < width; ) {
239 95040 const int qps = 3 + is_luma;
240 int qp;
241
1/2
✓ Branch 0 taken 95040 times.
✗ Branch 1 not taken.
95040 int end = FFMIN(x + 8, width);
242
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95040 times.
95040 if (p->qp)
244 qp = p->qp;
245 else {
246
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 95040 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 95040 times.
95040 qp = qp_store[ (FFMIN(x, width - 1) >> qps) + (FFMIN(y, height - 1) >> qps) * qp_stride];
247 95040 qp = ff_norm_qscale(qp, p->qscale_type);
248 }
249
2/2
✓ Branch 0 taken 760320 times.
✓ Branch 1 taken 95040 times.
855360 for (; x < end; x++) {
250 760320 const int index = x + y * stride + (8 - 3) * (1 + stride) + 8; //FIXME silly offset
251 760320 uint8_t *src = p_src + index;
252 760320 int16_t *tp = temp + 4 * x;
253 int v;
254
255
2/2
✓ Branch 0 taken 190080 times.
✓ Branch 1 taken 570240 times.
760320 if ((x & 3) == 0)
256 190080 dctA_c(tp + 4 * 8, src, stride);
257
258 760320 p->dctB(block, tp);
259
260 760320 v = p->requantize(p, block, qp);
261 760320 v = (v + dither[y & 7][x & 7]) >> 6;
262
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 760311 times.
760320 if ((unsigned)v > 255)
263 9 v = (-v) >> 31;
264 760320 dst[x + y * dst_stride] = v;
265 }
266 }
267 }
268 }
269
270 static const enum AVPixelFormat pix_fmts[] = {
271 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
272 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
273 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
274 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
275 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
276 AV_PIX_FMT_GBRP,
277 AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
278 };
279
280 1 static int config_input(AVFilterLink *inlink)
281 {
282 1 AVFilterContext *ctx = inlink->dst;
283 1 PP7Context *pp7 = ctx->priv;
284 1 const int h = FFALIGN(inlink->h + 16, 16);
285 1 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
286
287 1 pp7->hsub = desc->log2_chroma_w;
288 1 pp7->vsub = desc->log2_chroma_h;
289
290 1 pp7->temp_stride = FFALIGN(inlink->w + 16, 16);
291 1 pp7->src = av_malloc_array(pp7->temp_stride, (h + 8) * sizeof(uint8_t));
292
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!pp7->src)
294 return AVERROR(ENOMEM);
295
296 1 init_thres2(pp7);
297
298
1/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 switch (pp7->mode) {
299 case 0: pp7->requantize = hardthresh_c; break;
300 case 1: pp7->requantize = softthresh_c; break;
301 1 default:
302 1 case 2: pp7->requantize = mediumthresh_c; break;
303 }
304
305 1 pp7->dctB = dctB_c;
306
307 #if ARCH_X86
308 1 ff_pp7_init_x86(pp7);
309 #endif
310
311 1 return 0;
312 }
313
314 5 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
315 {
316 5 AVFilterContext *ctx = inlink->dst;
317 5 PP7Context *pp7 = ctx->priv;
318 5 AVFilterLink *outlink = ctx->outputs[0];
319 5 AVFrame *out = in;
320
321 5 int qp_stride = 0;
322 5 int8_t *qp_table = NULL;
323
324
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (!pp7->qp) {
325 5 int ret = ff_qp_table_extract(in, &qp_table, &qp_stride, NULL, &pp7->qscale_type);
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (ret < 0) {
327 av_frame_free(&in);
328 return ret;
329 }
330 }
331
332
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (!ctx->is_disabled) {
333 5 const int cw = AV_CEIL_RSHIFT(inlink->w, pp7->hsub);
334 5 const int ch = AV_CEIL_RSHIFT(inlink->h, pp7->vsub);
335
336 /* get a new frame if in-place is not possible or if the dimensions
337 * are not multiple of 8 */
338
3/6
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
5 if (!av_frame_is_writable(in) || (inlink->w & 7) || (inlink->h & 7)) {
339 const int aligned_w = FFALIGN(inlink->w, 8);
340 const int aligned_h = FFALIGN(inlink->h, 8);
341
342 out = ff_get_video_buffer(outlink, aligned_w, aligned_h);
343 if (!out) {
344 av_frame_free(&in);
345 av_freep(&qp_table);
346 return AVERROR(ENOMEM);
347 }
348 av_frame_copy_props(out, in);
349 out->width = in->width;
350 out->height = in->height;
351 }
352
353
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5 if (qp_table || pp7->qp) {
354
355 5 filter(pp7, out->data[0], in->data[0], out->linesize[0], in->linesize[0],
356 inlink->w, inlink->h, qp_table, qp_stride, 1);
357 5 filter(pp7, out->data[1], in->data[1], out->linesize[1], in->linesize[1],
358 cw, ch, qp_table, qp_stride, 0);
359 5 filter(pp7, out->data[2], in->data[2], out->linesize[2], in->linesize[2],
360 cw, ch, qp_table, qp_stride, 0);
361 5 emms_c();
362 }
363 }
364
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (in != out) {
366 if (in->data[3])
367 av_image_copy_plane(out->data[3], out->linesize[3],
368 in ->data[3], in ->linesize[3],
369 inlink->w, inlink->h);
370 av_frame_free(&in);
371 }
372 5 av_freep(&qp_table);
373 5 return ff_filter_frame(outlink, out);
374 }
375
376 2 static av_cold void uninit(AVFilterContext *ctx)
377 {
378 2 PP7Context *pp7 = ctx->priv;
379 2 av_freep(&pp7->src);
380 2 }
381
382 static const AVFilterPad pp7_inputs[] = {
383 {
384 .name = "default",
385 .type = AVMEDIA_TYPE_VIDEO,
386 .config_props = config_input,
387 .filter_frame = filter_frame,
388 },
389 };
390
391 const AVFilter ff_vf_pp7 = {
392 .name = "pp7",
393 .description = NULL_IF_CONFIG_SMALL("Apply Postprocessing 7 filter."),
394 .priv_size = sizeof(PP7Context),
395 .uninit = uninit,
396 FILTER_INPUTS(pp7_inputs),
397 FILTER_OUTPUTS(ff_video_default_filterpad),
398 FILTER_PIXFMTS_ARRAY(pix_fmts),
399 .priv_class = &pp7_class,
400 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
401 };
402