2 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2014 Arwa Arif <arwaarif1994@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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.
24 * Postprocessing filter - 7
26 * Originally written by Michael Niedermayer for the MPlayer
27 * project, and ported by Arwa Arif for FFmpeg.
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"
45 #define OFFSET(x) offsetof(PP7Context, x)
46 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
47 static const AVOption pp7_options[] = {
48 { "qp", "force a constant quantizer parameter", OFFSET(qp), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 64, FLAGS },
49 { "mode", "set thresholding mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_MEDIUM}, 0, 2, FLAGS, "mode" },
50 { "hard", "hard thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_HARD}, INT_MIN, INT_MAX, FLAGS, "mode" },
51 { "soft", "soft thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_SOFT}, INT_MIN, INT_MAX, FLAGS, "mode" },
52 { "medium", "medium thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_MEDIUM}, INT_MIN, INT_MAX, FLAGS, "mode" },
56 AVFILTER_DEFINE_CLASS(pp7);
58 DECLARE_ALIGNED(8, static const uint8_t, dither)[8][8] = {
59 { 0, 48, 12, 60, 3, 51, 15, 63, },
60 { 32, 16, 44, 28, 35, 19, 47, 31, },
61 { 8, 56, 4, 52, 11, 59, 7, 55, },
62 { 40, 24, 36, 20, 43, 27, 39, 23, },
63 { 2, 50, 14, 62, 1, 49, 13, 61, },
64 { 34, 18, 46, 30, 33, 17, 45, 29, },
65 { 10, 58, 6, 54, 9, 57, 5, 53, },
66 { 42, 26, 38, 22, 41, 25, 37, 21, },
73 #define SN1 2.2360679775
74 #define SN2 3.16227766017
77 static const int factor[16] = {
78 N / (N0 * N0), N / (N0 * N1), N / (N0 * N0), N / (N0 * N2),
79 N / (N1 * N0), N / (N1 * N1), N / (N1 * N0), N / (N1 * N2),
80 N / (N0 * N0), N / (N0 * N1), N / (N0 * N0), N / (N0 * N2),
81 N / (N2 * N0), N / (N2 * N1), N / (N2 * N0), N / (N2 * N2),
84 static void init_thres2(PP7Context *p)
89 for (qp = 0; qp < 99; qp++) {
90 for (i = 0; i < 16; i++) {
91 p->thres2[qp][i] = ((i&1) ? SN2 : SN0) * ((i&4) ? SN2 : SN0) * FFMAX(1, qp) * (1<<2) - 1 - bias;
96 static inline void dctA_c(int16_t *dst, uint8_t *src, int stride)
100 for (i = 0; i < 4; i++) {
101 int s0 = src[0 * stride] + src[6 * stride];
102 int s1 = src[1 * stride] + src[5 * stride];
103 int s2 = src[2 * stride] + src[4 * stride];
104 int s3 = src[3 * stride];
112 dst[1] = 2 * s3 + s2;
113 dst[3] = s3 - 2 * s2;
119 static void dctB_c(int16_t *dst, int16_t *src)
123 for (i = 0; i < 4; i++) {
124 int s0 = src[0 * 4] + src[6 * 4];
125 int s1 = src[1 * 4] + src[5 * 4];
126 int s2 = src[2 * 4] + src[4 * 4];
135 dst[1 * 4] = 2 * s3 + s2;
136 dst[3 * 4] = s3 - 2 * s2;
142 static int hardthresh_c(PP7Context *p, int16_t *src, int qp)
147 a = src[0] * factor[0];
148 for (i = 1; i < 16; i++) {
149 unsigned int threshold1 = p->thres2[qp][i];
150 unsigned int threshold2 = threshold1 << 1;
152 if (((unsigned)(level + threshold1)) > threshold2)
153 a += level * factor[i];
155 return (a + (1 << 11)) >> 12;
158 static int mediumthresh_c(PP7Context *p, int16_t *src, int qp)
163 a = src[0] * factor[0];
164 for (i = 1; i < 16; i++) {
165 unsigned int threshold1 = p->thres2[qp][i];
166 unsigned int threshold2 = threshold1 << 1;
168 if (((unsigned)(level + threshold1)) > threshold2) {
169 if (((unsigned)(level + 2 * threshold1)) > 2 * threshold2)
170 a += level * factor[i];
173 a += 2 * (level - (int)threshold1) * factor[i];
175 a += 2 * (level + (int)threshold1) * factor[i];
179 return (a + (1 << 11)) >> 12;
182 static int softthresh_c(PP7Context *p, int16_t *src, int qp)
187 a = src[0] * factor[0];
188 for (i = 1; i < 16; i++) {
189 unsigned int threshold1 = p->thres2[qp][i];
190 unsigned int threshold2 = threshold1 << 1;
192 if (((unsigned)(level + threshold1)) > threshold2) {
194 a += (level - (int)threshold1) * factor[i];
196 a += (level + (int)threshold1) * factor[i];
199 return (a + (1 << 11)) >> 12;
202 static void filter(PP7Context *p, uint8_t *dst, uint8_t *src,
203 int dst_stride, int src_stride,
204 int width, int height,
205 uint8_t *qp_store, int qp_stride, int is_luma)
208 const int stride = is_luma ? p->temp_stride : ((width + 16 + 15) & (~15));
209 uint8_t *p_src = p->src + 8 * stride;
210 int16_t *block = (int16_t *)p->src;
211 int16_t *temp = (int16_t *)(p->src + 32);
213 if (!src || !dst) return;
214 for (y = 0; y < height; y++) {
215 int index = 8 + 8 * stride + y * stride;
216 memcpy(p_src + index, src + y * src_stride, width);
217 for (x = 0; x < 8; x++) {
218 p_src[index - x - 1]= p_src[index + x ];
219 p_src[index + width + x ]= p_src[index + width - x - 1];
222 for (y = 0; y < 8; y++) {
223 memcpy(p_src + ( 7 - y ) * stride, p_src + ( y + 8 ) * stride, stride);
224 memcpy(p_src + (height + 8 + y) * stride, p_src + (height - y + 7) * stride, stride);
226 //FIXME (try edge emu)
228 for (y = 0; y < height; y++) {
229 for (x = -8; x < 0; x += 4) {
230 const int index = x + y * stride + (8 - 3) * (1 + stride) + 8; //FIXME silly offset
231 uint8_t *src = p_src + index;
232 int16_t *tp = temp + 4 * x;
234 dctA_c(tp + 4 * 8, src, stride);
236 for (x = 0; x < width; ) {
237 const int qps = 3 + is_luma;
239 int end = FFMIN(x + 8, width);
244 qp = qp_store[ (FFMIN(x, width - 1) >> qps) + (FFMIN(y, height - 1) >> qps) * qp_stride];
245 qp = ff_norm_qscale(qp, p->qscale_type);
247 for (; x < end; x++) {
248 const int index = x + y * stride + (8 - 3) * (1 + stride) + 8; //FIXME silly offset
249 uint8_t *src = p_src + index;
250 int16_t *tp = temp + 4 * x;
254 dctA_c(tp + 4 * 8, src, stride);
258 v = p->requantize(p, block, qp);
259 v = (v + dither[y & 7][x & 7]) >> 6;
260 if ((unsigned)v > 255)
262 dst[x + y * dst_stride] = v;
268 static int query_formats(AVFilterContext *ctx)
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,
277 AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
280 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
282 return AVERROR(ENOMEM);
283 return ff_set_common_formats(ctx, fmts_list);
286 static int config_input(AVFilterLink *inlink)
288 AVFilterContext *ctx = inlink->dst;
289 PP7Context *pp7 = ctx->priv;
290 const int h = FFALIGN(inlink->h + 16, 16);
291 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
293 pp7->hsub = desc->log2_chroma_w;
294 pp7->vsub = desc->log2_chroma_h;
296 pp7->temp_stride = FFALIGN(inlink->w + 16, 16);
297 pp7->src = av_malloc_array(pp7->temp_stride, (h + 8) * sizeof(uint8_t));
300 return AVERROR(ENOMEM);
305 case 0: pp7->requantize = hardthresh_c; break;
306 case 1: pp7->requantize = softthresh_c; break;
308 case 2: pp7->requantize = mediumthresh_c; break;
314 ff_pp7_init_x86(pp7);
319 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
321 AVFilterContext *ctx = inlink->dst;
322 PP7Context *pp7 = ctx->priv;
323 AVFilterLink *outlink = ctx->outputs[0];
327 int8_t *qp_table = NULL;
330 int ret = ff_qp_table_extract(in, &qp_table, &qp_stride, NULL, &pp7->qscale_type);
337 if (!ctx->is_disabled) {
338 const int cw = AV_CEIL_RSHIFT(inlink->w, pp7->hsub);
339 const int ch = AV_CEIL_RSHIFT(inlink->h, pp7->vsub);
341 /* get a new frame if in-place is not possible or if the dimensions
342 * are not multiple of 8 */
343 if (!av_frame_is_writable(in) || (inlink->w & 7) || (inlink->h & 7)) {
344 const int aligned_w = FFALIGN(inlink->w, 8);
345 const int aligned_h = FFALIGN(inlink->h, 8);
347 out = ff_get_video_buffer(outlink, aligned_w, aligned_h);
351 return AVERROR(ENOMEM);
353 av_frame_copy_props(out, in);
354 out->width = in->width;
355 out->height = in->height;
358 if (qp_table || pp7->qp) {
360 filter(pp7, out->data[0], in->data[0], out->linesize[0], in->linesize[0],
361 inlink->w, inlink->h, qp_table, qp_stride, 1);
362 filter(pp7, out->data[1], in->data[1], out->linesize[1], in->linesize[1],
363 cw, ch, qp_table, qp_stride, 0);
364 filter(pp7, out->data[2], in->data[2], out->linesize[2], in->linesize[2],
365 cw, ch, qp_table, qp_stride, 0);
372 av_image_copy_plane(out->data[3], out->linesize[3],
373 in ->data[3], in ->linesize[3],
374 inlink->w, inlink->h);
378 return ff_filter_frame(outlink, out);
381 static av_cold void uninit(AVFilterContext *ctx)
383 PP7Context *pp7 = ctx->priv;
387 static const AVFilterPad pp7_inputs[] = {
390 .type = AVMEDIA_TYPE_VIDEO,
391 .config_props = config_input,
392 .filter_frame = filter_frame,
397 static const AVFilterPad pp7_outputs[] = {
400 .type = AVMEDIA_TYPE_VIDEO,
405 const AVFilter ff_vf_pp7 = {
407 .description = NULL_IF_CONFIG_SMALL("Apply Postprocessing 7 filter."),
408 .priv_size = sizeof(PP7Context),
410 .query_formats = query_formats,
411 .inputs = pp7_inputs,
412 .outputs = pp7_outputs,
413 .priv_class = &pp7_class,
414 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,