]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_pp7.c
lavfi/vf_spp: convert to the video_enc_params API
[ffmpeg] / libavfilter / vf_pp7.c
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/avassert.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/mem_internal.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "internal.h"
36 #include "vf_pp7.h"
37
38 enum mode {
39     MODE_HARD,
40     MODE_SOFT,
41     MODE_MEDIUM
42 };
43
44 #define OFFSET(x) offsetof(PP7Context, x)
45 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
46 static const AVOption pp7_options[] = {
47     { "qp", "force a constant quantizer parameter", OFFSET(qp), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 64, FLAGS },
48     { "mode", "set thresholding mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_MEDIUM}, 0, 2, FLAGS, "mode" },
49         { "hard",   "hard thresholding",   0, AV_OPT_TYPE_CONST, {.i64 = MODE_HARD},   INT_MIN, INT_MAX, FLAGS, "mode" },
50         { "soft",   "soft thresholding",   0, AV_OPT_TYPE_CONST, {.i64 = MODE_SOFT},   INT_MIN, INT_MAX, FLAGS, "mode" },
51         { "medium", "medium thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_MEDIUM}, INT_MIN, INT_MAX, FLAGS, "mode" },
52     { NULL }
53 };
54
55 AVFILTER_DEFINE_CLASS(pp7);
56
57 DECLARE_ALIGNED(8, static const uint8_t, dither)[8][8] = {
58     {  0,  48,  12,  60,   3,  51,  15,  63, },
59     { 32,  16,  44,  28,  35,  19,  47,  31, },
60     {  8,  56,   4,  52,  11,  59,   7,  55, },
61     { 40,  24,  36,  20,  43,  27,  39,  23, },
62     {  2,  50,  14,  62,   1,  49,  13,  61, },
63     { 34,  18,  46,  30,  33,  17,  45,  29, },
64     { 10,  58,   6,  54,   9,  57,   5,  53, },
65     { 42,  26,  38,  22,  41,  25,  37,  21, },
66 };
67
68 #define N0 4
69 #define N1 5
70 #define N2 10
71 #define SN0 2
72 #define SN1 2.2360679775
73 #define SN2 3.16227766017
74 #define N (1 << 16)
75
76 static const int factor[16] = {
77     N / (N0 * N0), N / (N0 * N1), N / (N0 * N0), N / (N0 * N2),
78     N / (N1 * N0), N / (N1 * N1), N / (N1 * N0), N / (N1 * N2),
79     N / (N0 * N0), N / (N0 * N1), N / (N0 * N0), N / (N0 * N2),
80     N / (N2 * N0), N / (N2 * N1), N / (N2 * N0), N / (N2 * N2),
81 };
82
83 static void init_thres2(PP7Context *p)
84 {
85     int qp, i;
86     int bias = 0; //FIXME
87
88     for (qp = 0; qp < 99; qp++) {
89         for (i = 0; i < 16; i++) {
90             p->thres2[qp][i] = ((i&1) ? SN2 : SN0) * ((i&4) ? SN2 : SN0) * FFMAX(1, qp) * (1<<2) - 1 - bias;
91         }
92     }
93 }
94
95 static inline void dctA_c(int16_t *dst, uint8_t *src, int stride)
96 {
97     int i;
98
99     for (i = 0; i < 4; i++) {
100         int s0 = src[0 * stride] + src[6 * stride];
101         int s1 = src[1 * stride] + src[5 * stride];
102         int s2 = src[2 * stride] + src[4 * stride];
103         int s3 = src[3 * stride];
104         int s = s3 + s3;
105         s3 = s  - s0;
106         s0 = s  + s0;
107         s  = s2 + s1;
108         s2 = s2 - s1;
109         dst[0] = s0 + s;
110         dst[2] = s0 - s;
111         dst[1] = 2 * s3 +     s2;
112         dst[3] =     s3 - 2 * s2;
113         src++;
114         dst += 4;
115     }
116 }
117
118 static void dctB_c(int16_t *dst, int16_t *src)
119 {
120     int i;
121
122     for (i = 0; i < 4; i++) {
123         int s0 = src[0 * 4] + src[6 * 4];
124         int s1 = src[1 * 4] + src[5 * 4];
125         int s2 = src[2 * 4] + src[4 * 4];
126         int s3 = src[3 * 4];
127         int s = s3 + s3;
128         s3 = s  - s0;
129         s0 = s  + s0;
130         s  = s2 + s1;
131         s2 = s2 - s1;
132         dst[0 * 4] = s0 + s;
133         dst[2 * 4] = s0 - s;
134         dst[1 * 4] = 2 * s3 +     s2;
135         dst[3 * 4] =     s3 - 2 * s2;
136         src++;
137         dst++;
138     }
139 }
140
141 static int hardthresh_c(PP7Context *p, int16_t *src, int qp)
142 {
143     int i;
144     int a;
145
146     a = src[0] * factor[0];
147     for (i = 1; i < 16; i++) {
148         unsigned int threshold1 = p->thres2[qp][i];
149         unsigned int threshold2 = threshold1 << 1;
150         int level = src[i];
151         if (((unsigned)(level + threshold1)) > threshold2)
152             a += level * factor[i];
153     }
154     return (a + (1 << 11)) >> 12;
155 }
156
157 static int mediumthresh_c(PP7Context *p, int16_t *src, int qp)
158 {
159     int i;
160     int a;
161
162     a = src[0] * factor[0];
163     for (i = 1; i < 16; i++) {
164         unsigned int threshold1 = p->thres2[qp][i];
165         unsigned int threshold2 = threshold1 << 1;
166         int level = src[i];
167         if (((unsigned)(level + threshold1)) > threshold2) {
168             if (((unsigned)(level + 2 * threshold1)) > 2 * threshold2)
169                 a += level * factor[i];
170             else {
171                 if (level > 0)
172                     a += 2 * (level - (int)threshold1) * factor[i];
173                 else
174                     a += 2 * (level + (int)threshold1) * factor[i];
175             }
176         }
177     }
178     return (a + (1 << 11)) >> 12;
179 }
180
181 static int softthresh_c(PP7Context *p, int16_t *src, int qp)
182 {
183     int i;
184     int a;
185
186     a = src[0] * factor[0];
187     for (i = 1; i < 16; i++) {
188         unsigned int threshold1 = p->thres2[qp][i];
189         unsigned int threshold2 = threshold1 << 1;
190         int level = src[i];
191         if (((unsigned)(level + threshold1)) > threshold2) {
192             if (level > 0)
193                 a += (level - (int)threshold1) * factor[i];
194             else
195                 a += (level + (int)threshold1) * factor[i];
196         }
197     }
198     return (a + (1 << 11)) >> 12;
199 }
200
201 static void filter(PP7Context *p, uint8_t *dst, uint8_t *src,
202                    int dst_stride, int src_stride,
203                    int width, int height,
204                    uint8_t *qp_store, int qp_stride, int is_luma)
205 {
206     int x, y;
207     const int stride = is_luma ? p->temp_stride : ((width + 16 + 15) & (~15));
208     uint8_t *p_src = p->src + 8 * stride;
209     int16_t *block = (int16_t *)p->src;
210     int16_t *temp  = (int16_t *)(p->src + 32);
211
212     if (!src || !dst) return;
213     for (y = 0; y < height; y++) {
214         int index = 8 + 8 * stride + y * stride;
215         memcpy(p_src + index, src + y * src_stride, width);
216         for (x = 0; x < 8; x++) {
217             p_src[index         - x - 1]= p_src[index +         x    ];
218             p_src[index + width + x    ]= p_src[index + width - x - 1];
219         }
220     }
221     for (y = 0; y < 8; y++) {
222         memcpy(p_src + (    7 - y     ) * stride, p_src + (    y + 8     ) * stride, stride);
223         memcpy(p_src + (height + 8 + y) * stride, p_src + (height - y + 7) * stride, stride);
224     }
225     //FIXME (try edge emu)
226
227     for (y = 0; y < height; y++) {
228         for (x = -8; x < 0; x += 4) {
229             const int index = x + y * stride + (8 - 3) * (1 + stride) + 8; //FIXME silly offset
230             uint8_t *src  = p_src + index;
231             int16_t *tp   = temp + 4 * x;
232
233             dctA_c(tp + 4 * 8, src, stride);
234         }
235         for (x = 0; x < width; ) {
236             const int qps = 3 + is_luma;
237             int qp;
238             int end = FFMIN(x + 8, width);
239
240             if (p->qp)
241                 qp = p->qp;
242             else {
243                 qp = qp_store[ (FFMIN(x, width - 1) >> qps) + (FFMIN(y, height - 1) >> qps) * qp_stride];
244                 qp = ff_norm_qscale(qp, p->qscale_type);
245             }
246             for (; x < end; x++) {
247                 const int index = x + y * stride + (8 - 3) * (1 + stride) + 8; //FIXME silly offset
248                 uint8_t *src = p_src + index;
249                 int16_t *tp  = temp + 4 * x;
250                 int v;
251
252                 if ((x & 3) == 0)
253                     dctA_c(tp + 4 * 8, src, stride);
254
255                 p->dctB(block, tp);
256
257                 v = p->requantize(p, block, qp);
258                 v = (v + dither[y & 7][x & 7]) >> 6;
259                 if ((unsigned)v > 255)
260                     v = (-v) >> 31;
261                 dst[x + y * dst_stride] = v;
262             }
263         }
264     }
265 }
266
267 static int query_formats(AVFilterContext *ctx)
268 {
269     static const enum AVPixelFormat pix_fmts[] = {
270         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,
271         AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUV411P,
272         AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUV440P,
273         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
274         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
275         AV_PIX_FMT_GBRP,
276         AV_PIX_FMT_GRAY8,    AV_PIX_FMT_NONE
277     };
278
279     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
280     if (!fmts_list)
281         return AVERROR(ENOMEM);
282     return ff_set_common_formats(ctx, fmts_list);
283 }
284
285 static int config_input(AVFilterLink *inlink)
286 {
287     AVFilterContext *ctx = inlink->dst;
288     PP7Context *pp7 = ctx->priv;
289     const int h = FFALIGN(inlink->h + 16, 16);
290     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
291
292     pp7->hsub = desc->log2_chroma_w;
293     pp7->vsub = desc->log2_chroma_h;
294
295     pp7->temp_stride = FFALIGN(inlink->w + 16, 16);
296     pp7->src = av_malloc_array(pp7->temp_stride,  (h + 8) * sizeof(uint8_t));
297
298     if (!pp7->src)
299         return AVERROR(ENOMEM);
300
301     init_thres2(pp7);
302
303     switch (pp7->mode) {
304         case 0: pp7->requantize = hardthresh_c; break;
305         case 1: pp7->requantize = softthresh_c; break;
306         default:
307         case 2: pp7->requantize = mediumthresh_c; break;
308     }
309
310     pp7->dctB = dctB_c;
311
312     if (ARCH_X86)
313         ff_pp7_init_x86(pp7);
314
315     return 0;
316 }
317
318 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
319 {
320     AVFilterContext *ctx = inlink->dst;
321     PP7Context *pp7 = ctx->priv;
322     AVFilterLink *outlink = ctx->outputs[0];
323     AVFrame *out = in;
324
325     int qp_stride = 0;
326     uint8_t *qp_table = NULL;
327
328     if (!pp7->qp)
329         qp_table = av_frame_get_qp_table(in, &qp_stride, &pp7->qscale_type);
330
331     if (!ctx->is_disabled) {
332         const int cw = AV_CEIL_RSHIFT(inlink->w, pp7->hsub);
333         const int ch = AV_CEIL_RSHIFT(inlink->h, pp7->vsub);
334
335         /* get a new frame if in-place is not possible or if the dimensions
336         * are not multiple of 8 */
337         if (!av_frame_is_writable(in) || (inlink->w & 7) || (inlink->h & 7)) {
338             const int aligned_w = FFALIGN(inlink->w, 8);
339             const int aligned_h = FFALIGN(inlink->h, 8);
340
341             out = ff_get_video_buffer(outlink, aligned_w, aligned_h);
342             if (!out) {
343                 av_frame_free(&in);
344                 return AVERROR(ENOMEM);
345             }
346             av_frame_copy_props(out, in);
347             out->width = in->width;
348             out->height = in->height;
349         }
350
351         if (qp_table || pp7->qp) {
352
353             filter(pp7, out->data[0], in->data[0], out->linesize[0], in->linesize[0],
354                    inlink->w, inlink->h, qp_table, qp_stride, 1);
355             filter(pp7, out->data[1], in->data[1], out->linesize[1], in->linesize[1],
356                    cw,        ch,        qp_table, qp_stride, 0);
357             filter(pp7, out->data[2], in->data[2], out->linesize[2], in->linesize[2],
358                    cw,        ch,        qp_table, qp_stride, 0);
359             emms_c();
360         }
361     }
362
363     if (in != out) {
364         if (in->data[3])
365             av_image_copy_plane(out->data[3], out->linesize[3],
366                                 in ->data[3], in ->linesize[3],
367                                 inlink->w, inlink->h);
368         av_frame_free(&in);
369     }
370     return ff_filter_frame(outlink, out);
371 }
372
373 static av_cold void uninit(AVFilterContext *ctx)
374 {
375     PP7Context *pp7 = ctx->priv;
376     av_freep(&pp7->src);
377 }
378
379 static const AVFilterPad pp7_inputs[] = {
380     {
381         .name         = "default",
382         .type         = AVMEDIA_TYPE_VIDEO,
383         .config_props = config_input,
384         .filter_frame = filter_frame,
385     },
386     { NULL }
387 };
388
389 static const AVFilterPad pp7_outputs[] = {
390     {
391         .name = "default",
392         .type = AVMEDIA_TYPE_VIDEO,
393     },
394     { NULL }
395 };
396
397 AVFilter ff_vf_pp7 = {
398     .name            = "pp7",
399     .description     = NULL_IF_CONFIG_SMALL("Apply Postprocessing 7 filter."),
400     .priv_size       = sizeof(PP7Context),
401     .uninit          = uninit,
402     .query_formats   = query_formats,
403     .inputs          = pp7_inputs,
404     .outputs         = pp7_outputs,
405     .priv_class      = &pp7_class,
406     .flags           = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
407 };