]> granicus.if.org Git - libvpx/blob - vp9/common/vp9_postproc.c
Merge "postproc - move filling of noise buffer to vpx_dsp."
[libvpx] / vp9 / common / vp9_postproc.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <math.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14
15 #include "./vpx_dsp_rtcd.h"
16 #include "./vpx_config.h"
17 #include "./vpx_scale_rtcd.h"
18 #include "./vp9_rtcd.h"
19
20 #include "vpx_dsp/vpx_dsp_common.h"
21 #include "vpx_dsp/postproc.h"
22 #include "vpx_ports/mem.h"
23 #include "vpx_ports/system_state.h"
24 #include "vpx_scale/vpx_scale.h"
25 #include "vpx_scale/yv12config.h"
26
27 #include "vp9/common/vp9_onyxc_int.h"
28 #include "vp9/common/vp9_postproc.h"
29 #include "vp9/common/vp9_textblit.h"
30
31 #if CONFIG_VP9_POSTPROC
32 static const int16_t kernel5[] = {
33   1, 1, 4, 1, 1
34 };
35
36 static const uint8_t q_diff_thresh = 20;
37 static const uint8_t last_q_thresh = 170;
38 extern const int16_t vpx_rv[];
39
40 #if CONFIG_VP9_HIGHBITDEPTH
41 void vp9_highbd_post_proc_down_and_across_c(const uint16_t *src_ptr,
42                                             uint16_t *dst_ptr,
43                                             int src_pixels_per_line,
44                                             int dst_pixels_per_line,
45                                             int rows,
46                                             int cols,
47                                             int flimit) {
48   uint16_t const *p_src;
49   uint16_t *p_dst;
50   int row, col, i, v, kernel;
51   int pitch = src_pixels_per_line;
52   uint16_t d[8];
53
54   for (row = 0; row < rows; row++) {
55     // post_proc_down for one row.
56     p_src = src_ptr;
57     p_dst = dst_ptr;
58
59     for (col = 0; col < cols; col++) {
60       kernel = 4;
61       v = p_src[col];
62
63       for (i = -2; i <= 2; i++) {
64         if (abs(v - p_src[col + i * pitch]) > flimit)
65           goto down_skip_convolve;
66
67         kernel += kernel5[2 + i] * p_src[col + i * pitch];
68       }
69
70       v = (kernel >> 3);
71
72     down_skip_convolve:
73       p_dst[col] = v;
74     }
75
76     /* now post_proc_across */
77     p_src = dst_ptr;
78     p_dst = dst_ptr;
79
80     for (i = 0; i < 8; i++)
81       d[i] = p_src[i];
82
83     for (col = 0; col < cols; col++) {
84       kernel = 4;
85       v = p_src[col];
86
87       d[col & 7] = v;
88
89       for (i = -2; i <= 2; i++) {
90         if (abs(v - p_src[col + i]) > flimit)
91           goto across_skip_convolve;
92
93         kernel += kernel5[2 + i] * p_src[col + i];
94       }
95
96       d[col & 7] = (kernel >> 3);
97
98     across_skip_convolve:
99       if (col >= 2)
100         p_dst[col - 2] = d[(col - 2) & 7];
101     }
102
103     /* handle the last two pixels */
104     p_dst[col - 2] = d[(col - 2) & 7];
105     p_dst[col - 1] = d[(col - 1) & 7];
106
107
108     /* next row */
109     src_ptr += pitch;
110     dst_ptr += dst_pixels_per_line;
111   }
112 }
113 #endif  // CONFIG_VP9_HIGHBITDEPTH
114
115 static int q2mbl(int x) {
116   if (x < 20) x = 20;
117
118   x = 50 + (x - 50) * 10 / 8;
119   return x * x / 3;
120 }
121
122 #if CONFIG_VP9_HIGHBITDEPTH
123 void vp9_highbd_mbpost_proc_across_ip_c(uint16_t *src, int pitch,
124                                         int rows, int cols, int flimit) {
125   int r, c, i;
126
127   uint16_t *s = src;
128   uint16_t d[16];
129
130
131   for (r = 0; r < rows; r++) {
132     int sumsq = 0;
133     int sum   = 0;
134
135     for (i = -8; i <= 6; i++) {
136       sumsq += s[i] * s[i];
137       sum   += s[i];
138       d[i + 8] = 0;
139     }
140
141     for (c = 0; c < cols + 8; c++) {
142       int x = s[c + 7] - s[c - 8];
143       int y = s[c + 7] + s[c - 8];
144
145       sum  += x;
146       sumsq += x * y;
147
148       d[c & 15] = s[c];
149
150       if (sumsq * 15 - sum * sum < flimit) {
151         d[c & 15] = (8 + sum + s[c]) >> 4;
152       }
153
154       s[c - 8] = d[(c - 8) & 15];
155     }
156
157     s += pitch;
158   }
159 }
160 #endif  // CONFIG_VP9_HIGHBITDEPTH
161
162
163 #if CONFIG_VP9_HIGHBITDEPTH
164 void vp9_highbd_mbpost_proc_down_c(uint16_t *dst, int pitch,
165                                    int rows, int cols, int flimit) {
166   int r, c, i;
167   const int16_t *rv3 = &vpx_rv[63 & rand()];  // NOLINT
168
169   for (c = 0; c < cols; c++) {
170     uint16_t *s = &dst[c];
171     int sumsq = 0;
172     int sum = 0;
173     uint16_t d[16];
174     const int16_t *rv2 = rv3 + ((c * 17) & 127);
175
176     for (i = -8; i <= 6; i++) {
177       sumsq += s[i * pitch] * s[i * pitch];
178       sum += s[i * pitch];
179     }
180
181     for (r = 0; r < rows + 8; r++) {
182       sumsq += s[7 * pitch] * s[ 7 * pitch] - s[-8 * pitch] * s[-8 * pitch];
183       sum += s[7 * pitch] - s[-8 * pitch];
184       d[r & 15] = s[0];
185
186       if (sumsq * 15 - sum * sum < flimit) {
187         d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4;
188       }
189
190       s[-8 * pitch] = d[(r - 8) & 15];
191       s += pitch;
192     }
193   }
194 }
195 #endif  // CONFIG_VP9_HIGHBITDEPTH
196
197 static void deblock_and_de_macro_block(YV12_BUFFER_CONFIG   *source,
198                                        YV12_BUFFER_CONFIG   *post,
199                                        int                   q,
200                                        int                   low_var_thresh,
201                                        int                   flag,
202                                        uint8_t              *limits) {
203   (void) low_var_thresh;
204   (void) flag;
205 #if CONFIG_VP9_HIGHBITDEPTH
206   if (source->flags & YV12_FLAG_HIGHBITDEPTH) {
207     double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
208     int ppl = (int)(level + .5);
209     vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(source->y_buffer),
210                                          CONVERT_TO_SHORTPTR(post->y_buffer),
211                                          source->y_stride, post->y_stride,
212                                          source->y_height, source->y_width,
213                                          ppl);
214
215     vp9_highbd_mbpost_proc_across_ip(CONVERT_TO_SHORTPTR(post->y_buffer),
216                                      post->y_stride, post->y_height,
217                                      post->y_width, q2mbl(q));
218
219     vp9_highbd_mbpost_proc_down(CONVERT_TO_SHORTPTR(post->y_buffer),
220                                 post->y_stride, post->y_height,
221                                 post->y_width, q2mbl(q));
222
223     vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(source->u_buffer),
224                                          CONVERT_TO_SHORTPTR(post->u_buffer),
225                                          source->uv_stride, post->uv_stride,
226                                          source->uv_height, source->uv_width,
227                                          ppl);
228     vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(source->v_buffer),
229                                          CONVERT_TO_SHORTPTR(post->v_buffer),
230                                          source->uv_stride, post->uv_stride,
231                                          source->uv_height, source->uv_width,
232                                          ppl);
233   } else {
234 #endif  // CONFIG_VP9_HIGHBITDEPTH
235     vp9_deblock(source, post, q, limits);
236     vpx_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
237                               post->y_width, q2mbl(q));
238     vpx_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
239                          post->y_width, q2mbl(q));
240 #if CONFIG_VP9_HIGHBITDEPTH
241   }
242 #endif  // CONFIG_VP9_HIGHBITDEPTH
243 }
244
245 void vp9_deblock(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
246                  int q, uint8_t *limits) {
247   const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
248                         + 0.0065 + 0.5);
249 #if CONFIG_VP9_HIGHBITDEPTH
250   if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
251     int i;
252     const uint8_t * const srcs[3] =
253         {src->y_buffer, src->u_buffer, src->v_buffer};
254     const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
255     const int src_widths[3] = {src->y_width, src->uv_width, src->uv_width};
256     const int src_heights[3] = {src->y_height, src->uv_height, src->uv_height};
257
258     uint8_t * const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
259     const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
260     for (i = 0; i < MAX_MB_PLANE; ++i) {
261       vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(srcs[i]),
262                                            CONVERT_TO_SHORTPTR(dsts[i]),
263                                            src_strides[i], dst_strides[i],
264                                            src_heights[i], src_widths[i], ppl);
265     }
266   } else {
267 #endif  // CONFIG_VP9_HIGHBITDEPTH
268     int mbr;
269     const int mb_rows = src->y_height / 16;
270     const int mb_cols = src->y_width / 16;
271
272     memset(limits, (unsigned char) ppl, 16 * mb_cols);
273
274     for (mbr = 0; mbr < mb_rows; mbr++) {
275       vpx_post_proc_down_and_across_mb_row(
276           src->y_buffer + 16 * mbr * src->y_stride,
277           dst->y_buffer + 16 * mbr * dst->y_stride, src->y_stride,
278           dst->y_stride, src->y_width, limits, 16);
279       vpx_post_proc_down_and_across_mb_row(
280           src->u_buffer + 8 * mbr * src->uv_stride,
281           dst->u_buffer + 8 * mbr * dst->uv_stride, src->uv_stride,
282           dst->uv_stride, src->uv_width, limits, 8);
283       vpx_post_proc_down_and_across_mb_row(
284           src->v_buffer + 8 * mbr * src->uv_stride,
285           dst->v_buffer + 8 * mbr * dst->uv_stride, src->uv_stride,
286           dst->uv_stride, src->uv_width, limits, 8);
287     }
288 #if CONFIG_VP9_HIGHBITDEPTH
289   }
290 #endif  // CONFIG_VP9_HIGHBITDEPTH
291 }
292
293 void vp9_denoise(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
294                  int q, uint8_t *limits) {
295   vp9_deblock(src, dst, q, limits);
296 }
297
298 static void swap_mi_and_prev_mi(VP9_COMMON *cm) {
299   // Current mip will be the prev_mip for the next frame.
300   MODE_INFO *temp = cm->postproc_state.prev_mip;
301   cm->postproc_state.prev_mip = cm->mip;
302   cm->mip = temp;
303
304   // Update the upper left visible macroblock ptrs.
305   cm->mi = cm->mip + cm->mi_stride + 1;
306   cm->postproc_state.prev_mi = cm->postproc_state.prev_mip + cm->mi_stride + 1;
307 }
308
309 int vp9_post_proc_frame(struct VP9Common *cm,
310                         YV12_BUFFER_CONFIG *dest, vp9_ppflags_t *ppflags) {
311   const int q = VPXMIN(105, cm->lf.filter_level * 2);
312   const int flags = ppflags->post_proc_flag;
313   YV12_BUFFER_CONFIG *const ppbuf = &cm->post_proc_buffer;
314   struct postproc_state *const ppstate = &cm->postproc_state;
315
316   if (!cm->frame_to_show)
317     return -1;
318
319   if (!flags) {
320     *dest = *cm->frame_to_show;
321     return 0;
322   }
323
324   vpx_clear_system_state();
325
326   // Alloc memory for prev_mip in the first frame.
327   if (cm->current_video_frame == 1) {
328     ppstate->last_base_qindex = cm->base_qindex;
329     ppstate->last_frame_valid = 1;
330   }
331
332   if ((flags & VP9D_MFQE) && ppstate->prev_mip == NULL) {
333     ppstate->prev_mip = vpx_calloc(cm->mi_alloc_size, sizeof(*cm->mip));
334     if (!ppstate->prev_mip) {
335       return 1;
336     }
337     ppstate->prev_mi = ppstate->prev_mip + cm->mi_stride + 1;
338   }
339
340   // Allocate post_proc_buffer_int if needed.
341   if ((flags & VP9D_MFQE) && !cm->post_proc_buffer_int.buffer_alloc) {
342     if ((flags & VP9D_DEMACROBLOCK) || (flags & VP9D_DEBLOCK)) {
343       const int width = ALIGN_POWER_OF_TWO(cm->width, 4);
344       const int height = ALIGN_POWER_OF_TWO(cm->height, 4);
345
346       if (vpx_alloc_frame_buffer(&cm->post_proc_buffer_int, width, height,
347                                  cm->subsampling_x, cm->subsampling_y,
348 #if CONFIG_VP9_HIGHBITDEPTH
349                                  cm->use_highbitdepth,
350 #endif  // CONFIG_VP9_HIGHBITDEPTH
351                                  VP9_ENC_BORDER_IN_PIXELS,
352                                  cm->byte_alignment) < 0) {
353         vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
354                            "Failed to allocate MFQE framebuffer");
355       }
356
357       // Ensure that postproc is set to all 0s so that post proc
358       // doesn't pull random data in from edge.
359       memset(cm->post_proc_buffer_int.buffer_alloc, 128,
360              cm->post_proc_buffer.frame_size);
361     }
362   }
363
364   if (vpx_realloc_frame_buffer(&cm->post_proc_buffer, cm->width, cm->height,
365                                cm->subsampling_x, cm->subsampling_y,
366 #if CONFIG_VP9_HIGHBITDEPTH
367                                cm->use_highbitdepth,
368 #endif
369                                VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
370                                NULL, NULL, NULL) < 0)
371     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
372                        "Failed to allocate post-processing buffer");
373
374   if (flags & (VP9D_DEMACROBLOCK | VP9D_DEBLOCK)) {
375     if (!cm->postproc_state.limits) {
376       cm->postproc_state.limits = vpx_calloc(
377           cm->width, sizeof(*cm->postproc_state.limits));
378     }
379   }
380
381
382   if ((flags & VP9D_MFQE) && cm->current_video_frame >= 2 &&
383       ppstate->last_frame_valid && cm->bit_depth == 8 &&
384       ppstate->last_base_qindex <= last_q_thresh &&
385       cm->base_qindex - ppstate->last_base_qindex >= q_diff_thresh) {
386     vp9_mfqe(cm);
387     // TODO(jackychen): Consider whether enable deblocking by default
388     // if mfqe is enabled. Need to take both the quality and the speed
389     // into consideration.
390     if ((flags & VP9D_DEMACROBLOCK) || (flags & VP9D_DEBLOCK)) {
391       vp8_yv12_copy_frame(ppbuf, &cm->post_proc_buffer_int);
392     }
393     if ((flags & VP9D_DEMACROBLOCK) && cm->post_proc_buffer_int.buffer_alloc) {
394       deblock_and_de_macro_block(&cm->post_proc_buffer_int, ppbuf,
395                                  q + (ppflags->deblocking_level - 5) * 10,
396                                  1, 0, cm->postproc_state.limits);
397     } else if (flags & VP9D_DEBLOCK) {
398       vp9_deblock(&cm->post_proc_buffer_int, ppbuf, q,
399                   cm->postproc_state.limits);
400     } else {
401       vp8_yv12_copy_frame(&cm->post_proc_buffer_int, ppbuf);
402     }
403   } else if (flags & VP9D_DEMACROBLOCK) {
404     deblock_and_de_macro_block(cm->frame_to_show, ppbuf,
405                                q + (ppflags->deblocking_level - 5) * 10, 1, 0,
406                                cm->postproc_state.limits);
407   } else if (flags & VP9D_DEBLOCK) {
408     vp9_deblock(cm->frame_to_show, ppbuf, q, cm->postproc_state.limits);
409   } else {
410     vp8_yv12_copy_frame(cm->frame_to_show, ppbuf);
411   }
412
413   ppstate->last_base_qindex = cm->base_qindex;
414   ppstate->last_frame_valid = 1;
415
416   if (flags & VP9D_ADDNOISE) {
417     const int noise_level = ppflags->noise_level;
418     if (ppstate->last_q != q ||
419         ppstate->last_noise != noise_level) {
420       double sigma;
421       int clamp, i;
422       vpx_clear_system_state();
423       sigma = noise_level + .5 + .6 * q / 63.0;
424       clamp = vpx_setup_noise(sizeof(ppstate->noise), sigma,
425                               ppstate->noise);
426
427       for (i = 0; i < 16; i++) {
428         ppstate->blackclamp[i] = clamp;
429         ppstate->whiteclamp[i] = clamp;
430         ppstate->bothclamp[i] = 2 * clamp;
431       }
432       ppstate->last_q = q;
433       ppstate->last_noise = noise_level;
434     }
435     vpx_plane_add_noise(ppbuf->y_buffer, ppstate->noise, ppstate->blackclamp,
436                         ppstate->whiteclamp, ppstate->bothclamp,
437                         ppbuf->y_width, ppbuf->y_height, ppbuf->y_stride);
438   }
439
440   *dest = *ppbuf;
441
442   /* handle problem with extending borders */
443   dest->y_width = cm->width;
444   dest->y_height = cm->height;
445   dest->uv_width = dest->y_width >> cm->subsampling_x;
446   dest->uv_height = dest->y_height >> cm->subsampling_y;
447
448   if (flags & VP9D_MFQE)
449     swap_mi_and_prev_mi(cm);
450   return 0;
451 }
452 #endif  // CONFIG_VP9_POSTPROC