2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
15 #include "./vpx_dsp_rtcd.h"
16 #include "./vpx_config.h"
17 #include "./vpx_scale_rtcd.h"
18 #include "./vp9_rtcd.h"
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"
27 #include "vp9/common/vp9_onyxc_int.h"
28 #include "vp9/common/vp9_postproc.h"
29 #include "vp9/common/vp9_textblit.h"
31 #if CONFIG_VP9_POSTPROC
32 static const int16_t kernel5[] = {
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[];
40 #if CONFIG_VP9_HIGHBITDEPTH
41 void vp9_highbd_post_proc_down_and_across_c(const uint16_t *src_ptr,
43 int src_pixels_per_line,
44 int dst_pixels_per_line,
48 uint16_t const *p_src;
50 int row, col, i, v, kernel;
51 int pitch = src_pixels_per_line;
54 for (row = 0; row < rows; row++) {
55 // post_proc_down for one row.
59 for (col = 0; col < cols; col++) {
63 for (i = -2; i <= 2; i++) {
64 if (abs(v - p_src[col + i * pitch]) > flimit)
65 goto down_skip_convolve;
67 kernel += kernel5[2 + i] * p_src[col + i * pitch];
76 /* now post_proc_across */
80 for (i = 0; i < 8; i++)
83 for (col = 0; col < cols; col++) {
89 for (i = -2; i <= 2; i++) {
90 if (abs(v - p_src[col + i]) > flimit)
91 goto across_skip_convolve;
93 kernel += kernel5[2 + i] * p_src[col + i];
96 d[col & 7] = (kernel >> 3);
100 p_dst[col - 2] = d[(col - 2) & 7];
103 /* handle the last two pixels */
104 p_dst[col - 2] = d[(col - 2) & 7];
105 p_dst[col - 1] = d[(col - 1) & 7];
110 dst_ptr += dst_pixels_per_line;
113 #endif // CONFIG_VP9_HIGHBITDEPTH
115 static int q2mbl(int x) {
118 x = 50 + (x - 50) * 10 / 8;
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) {
131 for (r = 0; r < rows; r++) {
135 for (i = -8; i <= 6; i++) {
136 sumsq += s[i] * s[i];
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];
150 if (sumsq * 15 - sum * sum < flimit) {
151 d[c & 15] = (8 + sum + s[c]) >> 4;
154 s[c - 8] = d[(c - 8) & 15];
160 #endif // CONFIG_VP9_HIGHBITDEPTH
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) {
167 const int16_t *rv3 = &vpx_rv[63 & rand()]; // NOLINT
169 for (c = 0; c < cols; c++) {
170 uint16_t *s = &dst[c];
174 const int16_t *rv2 = rv3 + ((c * 17) & 127);
176 for (i = -8; i <= 6; i++) {
177 sumsq += s[i * pitch] * s[i * pitch];
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];
186 if (sumsq * 15 - sum * sum < flimit) {
187 d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4;
190 s[-8 * pitch] = d[(r - 8) & 15];
195 #endif // CONFIG_VP9_HIGHBITDEPTH
197 static void deblock_and_de_macro_block(YV12_BUFFER_CONFIG *source,
198 YV12_BUFFER_CONFIG *post,
203 (void) low_var_thresh;
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,
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));
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));
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,
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,
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
242 #endif // CONFIG_VP9_HIGHBITDEPTH
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
249 #if CONFIG_VP9_HIGHBITDEPTH
250 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
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};
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);
267 #endif // CONFIG_VP9_HIGHBITDEPTH
269 const int mb_rows = src->y_height / 16;
270 const int mb_cols = src->y_width / 16;
272 memset(limits, (unsigned char) ppl, 16 * mb_cols);
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);
288 #if CONFIG_VP9_HIGHBITDEPTH
290 #endif // CONFIG_VP9_HIGHBITDEPTH
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);
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;
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;
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;
316 if (!cm->frame_to_show)
320 *dest = *cm->frame_to_show;
324 vpx_clear_system_state();
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;
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) {
337 ppstate->prev_mi = ppstate->prev_mip + cm->mi_stride + 1;
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);
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");
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);
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,
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");
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));
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) {
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);
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);
401 vp8_yv12_copy_frame(&cm->post_proc_buffer_int, ppbuf);
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);
410 vp8_yv12_copy_frame(cm->frame_to_show, ppbuf);
413 ppstate->last_base_qindex = cm->base_qindex;
414 ppstate->last_frame_valid = 1;
415 if (flags & VP9D_ADDNOISE) {
416 const int noise_level = ppflags->noise_level;
417 if (ppstate->last_q != q ||
418 ppstate->last_noise != noise_level) {
420 vpx_clear_system_state();
421 sigma = noise_level + .5 + .6 * q / 63.0;
422 ppstate->clamp = vpx_setup_noise(sigma, ppstate->noise,
423 sizeof(ppstate->noise));
425 ppstate->last_noise = noise_level;
427 vpx_plane_add_noise(ppbuf->y_buffer, ppstate->noise, ppstate->clamp,
428 ppstate->clamp, ppbuf->y_width, ppbuf->y_height,
434 /* handle problem with extending borders */
435 dest->y_width = cm->width;
436 dest->y_height = cm->height;
437 dest->uv_width = dest->y_width >> cm->subsampling_x;
438 dest->uv_height = dest->y_height >> cm->subsampling_y;
440 if (flags & VP9D_MFQE)
441 swap_mi_and_prev_mi(cm);
444 #endif // CONFIG_VP9_POSTPROC