FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_spp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (c) 2013 Clément Bœsch <u pkh me>
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  * Simple post processing filter
25  *
26  * This implementation is based on an algorithm described in
27  * "Aria Nosratinia Embedded Post-Processing for
28  * Enhancement of Compressed Images (1999)"
29  *
30  * Originally written by Michael Niedermayer for the MPlayer project, and
31  * ported by Clément Bœsch for FFmpeg.
32  */
33 
34 #include "libavutil/avassert.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "internal.h"
39 #include "vf_spp.h"
40 
41 enum mode {
45 };
46 
47 #define OFFSET(x) offsetof(SPPContext, x)
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49 static const AVOption spp_options[] = {
50  { "quality", "set quality", OFFSET(log2_count), AV_OPT_TYPE_INT, {.i64 = 3}, 0, MAX_LEVEL, FLAGS },
51  { "qp", "force a constant quantizer parameter", OFFSET(qp), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 63, FLAGS },
52  { "mode", "set thresholding mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_HARD}, 0, NB_MODES - 1, FLAGS, "mode" },
53  { "hard", "hard thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_HARD}, INT_MIN, INT_MAX, FLAGS, "mode" },
54  { "soft", "soft thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_SOFT}, INT_MIN, INT_MAX, FLAGS, "mode" },
55  { "use_bframe_qp", "use B-frames' QP", OFFSET(use_bframe_qp), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
56  { NULL }
57 };
58 
60 
61 // XXX: share between filters?
62 DECLARE_ALIGNED(8, static const uint8_t, ldither)[8][8] = {
63  { 0, 48, 12, 60, 3, 51, 15, 63 },
64  { 32, 16, 44, 28, 35, 19, 47, 31 },
65  { 8, 56, 4, 52, 11, 59, 7, 55 },
66  { 40, 24, 36, 20, 43, 27, 39, 23 },
67  { 2, 50, 14, 62, 1, 49, 13, 61 },
68  { 34, 18, 46, 30, 33, 17, 45, 29 },
69  { 10, 58, 6, 54, 9, 57, 5, 53 },
70  { 42, 26, 38, 22, 41, 25, 37, 21 },
71 };
72 
73 static const uint8_t offset[127][2] = {
74  {0,0},
75  {0,0}, {4,4}, // quality = 1
76  {0,0}, {2,2}, {6,4}, {4,6}, // quality = 2
77  {0,0}, {5,1}, {2,2}, {7,3}, {4,4}, {1,5}, {6,6}, {3,7}, // quality = 3
78 
79  {0,0}, {4,0}, {1,1}, {5,1}, {3,2}, {7,2}, {2,3}, {6,3}, // quality = 4
80  {0,4}, {4,4}, {1,5}, {5,5}, {3,6}, {7,6}, {2,7}, {6,7},
81 
82  {0,0}, {0,2}, {0,4}, {0,6}, {1,1}, {1,3}, {1,5}, {1,7}, // quality = 5
83  {2,0}, {2,2}, {2,4}, {2,6}, {3,1}, {3,3}, {3,5}, {3,7},
84  {4,0}, {4,2}, {4,4}, {4,6}, {5,1}, {5,3}, {5,5}, {5,7},
85  {6,0}, {6,2}, {6,4}, {6,6}, {7,1}, {7,3}, {7,5}, {7,7},
86 
87  {0,0}, {4,4}, {0,4}, {4,0}, {2,2}, {6,6}, {2,6}, {6,2}, // quality = 6
88  {0,2}, {4,6}, {0,6}, {4,2}, {2,0}, {6,4}, {2,4}, {6,0},
89  {1,1}, {5,5}, {1,5}, {5,1}, {3,3}, {7,7}, {3,7}, {7,3},
90  {1,3}, {5,7}, {1,7}, {5,3}, {3,1}, {7,5}, {3,5}, {7,1},
91  {0,1}, {4,5}, {0,5}, {4,1}, {2,3}, {6,7}, {2,7}, {6,3},
92  {0,3}, {4,7}, {0,7}, {4,3}, {2,1}, {6,5}, {2,5}, {6,1},
93  {1,0}, {5,4}, {1,4}, {5,0}, {3,2}, {7,6}, {3,6}, {7,2},
94  {1,2}, {5,6}, {1,6}, {5,2}, {3,0}, {7,4}, {3,4}, {7,0},
95 };
96 
97 static void hardthresh_c(int16_t dst[64], const int16_t src[64],
98  int qp, const uint8_t *permutation)
99 {
100  int i;
101  int bias = 0; // FIXME
102 
103  unsigned threshold1 = qp * ((1<<4) - bias) - 1;
104  unsigned threshold2 = threshold1 << 1;
105 
106  memset(dst, 0, 64 * sizeof(dst[0]));
107  dst[0] = (src[0] + 4) >> 3;
108 
109  for (i = 1; i < 64; i++) {
110  int level = src[i];
111  if (((unsigned)(level + threshold1)) > threshold2) {
112  const int j = permutation[i];
113  dst[j] = (level + 4) >> 3;
114  }
115  }
116 }
117 
118 static void softthresh_c(int16_t dst[64], const int16_t src[64],
119  int qp, const uint8_t *permutation)
120 {
121  int i;
122  int bias = 0; //FIXME
123 
124  unsigned threshold1 = qp * ((1<<4) - bias) - 1;
125  unsigned threshold2 = threshold1 << 1;
126 
127  memset(dst, 0, 64 * sizeof(dst[0]));
128  dst[0] = (src[0] + 4) >> 3;
129 
130  for (i = 1; i < 64; i++) {
131  int level = src[i];
132  if (((unsigned)(level + threshold1)) > threshold2) {
133  const int j = permutation[i];
134  if (level > 0) dst[j] = (level - threshold1 + 4) >> 3;
135  else dst[j] = (level + threshold1 + 4) >> 3;
136  }
137  }
138 }
139 
140 static void store_slice_c(uint8_t *dst, const int16_t *src,
141  int dst_linesize, int src_linesize,
142  int width, int height, int log2_scale,
143  const uint8_t dither[8][8])
144 {
145  int y, x;
146 
147 #define STORE(pos) do { \
148  temp = ((src[x + y*src_linesize + pos] << log2_scale) + d[pos]) >> 6; \
149  if (temp & 0x100) \
150  temp = ~(temp >> 31); \
151  dst[x + y*dst_linesize + pos] = temp; \
152 } while (0)
153 
154  for (y = 0; y < height; y++) {
155  const uint8_t *d = dither[y];
156  for (x = 0; x < width; x += 8) {
157  int temp;
158  STORE(0);
159  STORE(1);
160  STORE(2);
161  STORE(3);
162  STORE(4);
163  STORE(5);
164  STORE(6);
165  STORE(7);
166  }
167  }
168 }
169 
170 static inline void add_block(int16_t *dst, int linesize, const int16_t block[64])
171 {
172  int y;
173 
174  for (y = 0; y < 8; y++) {
175  *(uint32_t *)&dst[0 + y*linesize] += *(uint32_t *)&block[0 + y*8];
176  *(uint32_t *)&dst[2 + y*linesize] += *(uint32_t *)&block[2 + y*8];
177  *(uint32_t *)&dst[4 + y*linesize] += *(uint32_t *)&block[4 + y*8];
178  *(uint32_t *)&dst[6 + y*linesize] += *(uint32_t *)&block[6 + y*8];
179  }
180 }
181 
182 // XXX: export the function?
183 static inline int norm_qscale(int qscale, int type)
184 {
185  switch (type) {
186  case FF_QSCALE_TYPE_MPEG1: return qscale;
187  case FF_QSCALE_TYPE_MPEG2: return qscale >> 1;
188  case FF_QSCALE_TYPE_H264: return qscale >> 2;
189  case FF_QSCALE_TYPE_VP56: return (63 - qscale + 2) >> 2;
190  }
191  return qscale;
192 }
193 
194 static void filter(SPPContext *p, uint8_t *dst, uint8_t *src,
195  int dst_linesize, int src_linesize, int width, int height,
196  const uint8_t *qp_table, int qp_stride, int is_luma)
197 {
198  int x, y, i;
199  const int count = 1 << p->log2_count;
200  const int linesize = is_luma ? p->temp_linesize : FFALIGN(width+16, 16);
201  DECLARE_ALIGNED(16, uint64_t, block_align)[32];
202  int16_t *block = (int16_t *)block_align;
203  int16_t *block2 = (int16_t *)(block_align + 16);
204 
205  for (y = 0; y < height; y++) {
206  int index = 8 + 8*linesize + y*linesize;
207  memcpy(p->src + index, src + y*src_linesize, width);
208  for (x = 0; x < 8; x++) {
209  p->src[index - x - 1] = p->src[index + x ];
210  p->src[index + width + x ] = p->src[index + width - x - 1];
211  }
212  }
213  for (y = 0; y < 8; y++) {
214  memcpy(p->src + ( 7-y)*linesize, p->src + ( y+8)*linesize, linesize);
215  memcpy(p->src + (height+8+y)*linesize, p->src + (height-y+7)*linesize, linesize);
216  }
217 
218  for (y = 0; y < height + 8; y += 8) {
219  memset(p->temp + (8 + y) * linesize, 0, 8 * linesize * sizeof(*p->temp));
220  for (x = 0; x < width + 8; x += 8) {
221  int qp;
222 
223  if (p->qp) {
224  qp = p->qp;
225  } else{
226  const int qps = 3 + is_luma;
227  qp = qp_table[(FFMIN(x, width - 1) >> qps) + (FFMIN(y, height - 1) >> qps) * qp_stride];
228  qp = FFMAX(1, norm_qscale(qp, p->qscale_type));
229  }
230  for (i = 0; i < count; i++) {
231  const int x1 = x + offset[i + count - 1][0];
232  const int y1 = y + offset[i + count - 1][1];
233  const int index = x1 + y1*linesize;
234  p->pdsp.get_pixels(block, p->src + index, linesize);
235  p->fdsp.fdct(block);
236  p->requantize(block2, block, qp, p->idsp.idct_permutation);
237  p->idsp.idct(block2);
238  add_block(p->temp + index, linesize, block2);
239  }
240  }
241  if (y)
242  p->store_slice(dst + (y - 8) * dst_linesize, p->temp + 8 + y*linesize,
243  dst_linesize, linesize, width,
244  FFMIN(8, height + 8 - y), MAX_LEVEL - p->log2_count,
245  ldither);
246  }
247 }
248 
250 {
251  static const enum PixelFormat pix_fmts[] = {
258  };
260  return 0;
261 }
262 
263 static int config_input(AVFilterLink *inlink)
264 {
265  SPPContext *spp = inlink->dst->priv;
266  const int h = FFALIGN(inlink->h + 16, 16);
267  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
268 
269  spp->hsub = desc->log2_chroma_w;
270  spp->vsub = desc->log2_chroma_h;
271  spp->temp_linesize = FFALIGN(inlink->w + 16, 16);
272  spp->temp = av_malloc_array(spp->temp_linesize, h * sizeof(*spp->temp));
273  spp->src = av_malloc_array(spp->temp_linesize, h * sizeof(*spp->src));
274  if (!spp->use_bframe_qp) {
275  /* we are assuming here the qp blocks will not be smaller that 16x16 */
276  spp->non_b_qp_alloc_size = FF_CEIL_RSHIFT(inlink->w, 4) * FF_CEIL_RSHIFT(inlink->h, 4);
277  spp->non_b_qp_table = av_calloc(spp->non_b_qp_alloc_size, sizeof(*spp->non_b_qp_table));
278  if (!spp->non_b_qp_table)
279  return AVERROR(ENOMEM);
280  }
281  if (!spp->temp || !spp->src)
282  return AVERROR(ENOMEM);
283  return 0;
284 }
285 
286 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
287 {
288  AVFilterContext *ctx = inlink->dst;
289  SPPContext *spp = ctx->priv;
290  AVFilterLink *outlink = ctx->outputs[0];
291  AVFrame *out = in;
292  int qp_stride = 0;
293  const int8_t *qp_table = NULL;
294 
295  /* if we are not in a constant user quantizer mode and we don't want to use
296  * the quantizers from the B-frames (B-frames often have a higher QP), we
297  * need to save the qp table from the last non B-frame; this is what the
298  * following code block does */
299  if (!spp->qp) {
300  qp_table = av_frame_get_qp_table(in, &qp_stride, &spp->qscale_type);
301 
302  if (qp_table && !spp->use_bframe_qp && in->pict_type != AV_PICTURE_TYPE_B) {
303  int w, h;
304 
305  /* if the qp stride is not set, it means the QP are only defined on
306  * a line basis */
307  if (!qp_stride) {
308  w = FF_CEIL_RSHIFT(inlink->w, 4);
309  h = 1;
310  } else {
311  w = FF_CEIL_RSHIFT(qp_stride, 4);
312  h = FF_CEIL_RSHIFT(inlink->h, 4);
313  }
314  av_assert0(w * h <= spp->non_b_qp_alloc_size);
315  memcpy(spp->non_b_qp_table, qp_table, w * h);
316  }
317  }
318 
319  if (spp->log2_count && !ctx->is_disabled) {
320  if (!spp->use_bframe_qp && spp->non_b_qp_table)
321  qp_table = spp->non_b_qp_table;
322 
323  if (qp_table || spp->qp) {
324  const int cw = FF_CEIL_RSHIFT(inlink->w, spp->hsub);
325  const int ch = FF_CEIL_RSHIFT(inlink->h, spp->vsub);
326 
327  /* get a new frame if in-place is not possible or if the dimensions
328  * are not multiple of 8 */
329  if (!av_frame_is_writable(in) || (inlink->w & 7) || (inlink->h & 7)) {
330  const int aligned_w = FFALIGN(inlink->w, 8);
331  const int aligned_h = FFALIGN(inlink->h, 8);
332 
333  out = ff_get_video_buffer(outlink, aligned_w, aligned_h);
334  if (!out) {
335  av_frame_free(&in);
336  return AVERROR(ENOMEM);
337  }
338  av_frame_copy_props(out, in);
339  out->width = in->width;
340  out->height = in->height;
341  }
342 
343  filter(spp, out->data[0], in->data[0], out->linesize[0], in->linesize[0], inlink->w, inlink->h, qp_table, qp_stride, 1);
344  filter(spp, out->data[1], in->data[1], out->linesize[1], in->linesize[1], cw, ch, qp_table, qp_stride, 0);
345  filter(spp, out->data[2], in->data[2], out->linesize[2], in->linesize[2], cw, ch, qp_table, qp_stride, 0);
346  emms_c();
347  }
348  }
349 
350  if (in != out) {
351  if (in->data[3])
352  av_image_copy_plane(out->data[3], out->linesize[3],
353  in ->data[3], in ->linesize[3],
354  inlink->w, inlink->h);
355  av_frame_free(&in);
356  }
357  return ff_filter_frame(outlink, out);
358 }
359 
360 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
361  char *res, int res_len, int flags)
362 {
363  SPPContext *spp = ctx->priv;
364 
365  if (!strcmp(cmd, "level")) {
366  if (!strcmp(args, "max"))
367  spp->log2_count = MAX_LEVEL;
368  else
369  spp->log2_count = av_clip(strtol(args, NULL, 10), 0, MAX_LEVEL);
370  return 0;
371  }
372  return AVERROR(ENOSYS);
373 }
374 
375 static av_cold int init(AVFilterContext *ctx)
376 {
377  SPPContext *spp = ctx->priv;
378 
379  spp->avctx = avcodec_alloc_context3(NULL);
380  if (!spp->avctx)
381  return AVERROR(ENOMEM);
382  ff_idctdsp_init(&spp->idsp, spp->avctx);
383  ff_fdctdsp_init(&spp->fdsp, spp->avctx);
384  ff_pixblockdsp_init(&spp->pdsp, spp->avctx);
385  spp->store_slice = store_slice_c;
386  switch (spp->mode) {
387  case MODE_HARD: spp->requantize = hardthresh_c; break;
388  case MODE_SOFT: spp->requantize = softthresh_c; break;
389  }
390  if (ARCH_X86)
391  ff_spp_init_x86(spp);
392  return 0;
393 }
394 
395 static av_cold void uninit(AVFilterContext *ctx)
396 {
397  SPPContext *spp = ctx->priv;
398 
399  av_freep(&spp->temp);
400  av_freep(&spp->src);
401  if (spp->avctx) {
402  avcodec_close(spp->avctx);
403  av_freep(&spp->avctx);
404  }
405  av_freep(&spp->non_b_qp_table);
406 }
407 
408 static const AVFilterPad spp_inputs[] = {
409  {
410  .name = "default",
411  .type = AVMEDIA_TYPE_VIDEO,
412  .config_props = config_input,
413  .filter_frame = filter_frame,
414  },
415  { NULL }
416 };
417 
418 static const AVFilterPad spp_outputs[] = {
419  {
420  .name = "default",
421  .type = AVMEDIA_TYPE_VIDEO,
422  },
423  { NULL }
424 };
425 
427  .name = "spp",
428  .description = NULL_IF_CONFIG_SMALL("Apply a simple post processing filter."),
429  .priv_size = sizeof(SPPContext),
430  .init = init,
431  .uninit = uninit,
433  .inputs = spp_inputs,
434  .outputs = spp_outputs,
436  .priv_class = &spp_class,
438 };