2013-10-07 20:20:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 The WebM project authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
2015-05-12 04:09:22 +02:00
|
|
|
#include "vpx_ports/mem.h"
|
2015-08-10 20:28:04 +02:00
|
|
|
#include "vpx_ports/system_state.h"
|
2015-05-12 04:09:22 +02:00
|
|
|
|
2014-03-21 22:31:53 +01:00
|
|
|
#include "vp9/encoder/vp9_aq_variance.h"
|
2013-10-07 20:20:10 +02:00
|
|
|
|
|
|
|
#include "vp9/common/vp9_seg_common.h"
|
|
|
|
|
|
|
|
#include "vp9/encoder/vp9_ratectrl.h"
|
2014-07-02 21:36:48 +02:00
|
|
|
#include "vp9/encoder/vp9_rd.h"
|
2013-10-07 20:20:10 +02:00
|
|
|
#include "vp9/encoder/vp9_segmentation.h"
|
|
|
|
|
2014-11-21 21:26:38 +01:00
|
|
|
#define ENERGY_MIN (-4)
|
2014-02-03 13:47:01 +01:00
|
|
|
#define ENERGY_MAX (1)
|
2013-10-07 20:20:10 +02:00
|
|
|
#define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN + 1)
|
|
|
|
#define ENERGY_IN_BOUNDS(energy)\
|
|
|
|
assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX)
|
|
|
|
|
2014-11-21 21:27:52 +01:00
|
|
|
static const double rate_ratio[MAX_SEGMENTS] =
|
2014-11-21 21:26:38 +01:00
|
|
|
{2.5, 2.0, 1.5, 1.0, 0.75, 1.0, 1.0, 1.0};
|
|
|
|
static const int segment_id[ENERGY_SPAN] = {0, 1, 1, 2, 3, 4};
|
2013-10-07 20:20:10 +02:00
|
|
|
|
|
|
|
#define SEGMENT_ID(i) segment_id[(i) - ENERGY_MIN]
|
|
|
|
|
|
|
|
DECLARE_ALIGNED(16, static const uint8_t, vp9_64_zeros[64]) = {0};
|
2014-09-24 15:36:34 +02:00
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
DECLARE_ALIGNED(16, static const uint16_t, vp9_highbd_64_zeros[64]) = {0};
|
|
|
|
#endif
|
2013-10-07 20:20:10 +02:00
|
|
|
|
|
|
|
unsigned int vp9_vaq_segment_id(int energy) {
|
|
|
|
ENERGY_IN_BOUNDS(energy);
|
|
|
|
return SEGMENT_ID(energy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vp9_vaq_frame_setup(VP9_COMP *cpi) {
|
|
|
|
VP9_COMMON *cm = &cpi->common;
|
|
|
|
struct segmentation *seg = &cm->seg;
|
|
|
|
int i;
|
|
|
|
|
2016-01-14 02:27:17 +01:00
|
|
|
if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
|
2014-02-03 13:47:01 +01:00
|
|
|
cpi->refresh_alt_ref_frame ||
|
|
|
|
(cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
|
2014-02-28 20:59:50 +01:00
|
|
|
vp9_enable_segmentation(seg);
|
2014-02-03 13:47:01 +01:00
|
|
|
vp9_clearall_segfeatures(seg);
|
2013-10-07 20:20:10 +02:00
|
|
|
|
2014-02-03 13:47:01 +01:00
|
|
|
seg->abs_delta = SEGMENT_DELTADATA;
|
2013-10-07 20:20:10 +02:00
|
|
|
|
2015-08-10 20:28:04 +02:00
|
|
|
vpx_clear_system_state();
|
2013-10-17 20:04:57 +02:00
|
|
|
|
2014-11-21 21:27:52 +01:00
|
|
|
for (i = 0; i < MAX_SEGMENTS; ++i) {
|
2014-11-21 02:28:21 +01:00
|
|
|
int qindex_delta =
|
|
|
|
vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, cm->base_qindex,
|
|
|
|
rate_ratio[i], cm->bit_depth);
|
|
|
|
|
2014-11-21 21:27:52 +01:00
|
|
|
// We don't allow qindex 0 in a segment if the base value is not 0.
|
|
|
|
// Q index 0 (lossless) implies 4x4 encoding only and in AQ mode a segment
|
2014-11-21 02:28:21 +01:00
|
|
|
// Q delta is sometimes applied without going back around the rd loop.
|
|
|
|
// This could lead to an illegal combination of partition size and q.
|
|
|
|
if ((cm->base_qindex != 0) && ((cm->base_qindex + qindex_delta) == 0)) {
|
|
|
|
qindex_delta = -cm->base_qindex + 1;
|
|
|
|
}
|
2013-10-07 20:20:10 +02:00
|
|
|
|
2014-11-21 21:27:52 +01:00
|
|
|
// No need to enable SEG_LVL_ALT_Q for this segment.
|
2014-11-21 02:28:21 +01:00
|
|
|
if (rate_ratio[i] == 1.0) {
|
2014-02-03 13:47:01 +01:00
|
|
|
continue;
|
|
|
|
}
|
2013-10-07 20:20:10 +02:00
|
|
|
|
2014-11-13 21:35:07 +01:00
|
|
|
vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, qindex_delta);
|
|
|
|
vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
|
2014-02-03 13:47:01 +01:00
|
|
|
}
|
2013-10-07 20:20:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 22:23:27 +02:00
|
|
|
/* TODO(agrange, paulwilkins): The block_variance calls the unoptimized versions
|
|
|
|
* of variance() and highbd_8_variance(). It should not.
|
|
|
|
*/
|
|
|
|
static void aq_variance(const uint8_t *a, int a_stride,
|
|
|
|
const uint8_t *b, int b_stride,
|
|
|
|
int w, int h, unsigned int *sse, int *sum) {
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
*sum = 0;
|
|
|
|
*sse = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < h; i++) {
|
|
|
|
for (j = 0; j < w; j++) {
|
|
|
|
const int diff = a[j] - b[j];
|
|
|
|
*sum += diff;
|
|
|
|
*sse += diff * diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
a += a_stride;
|
|
|
|
b += b_stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
static void aq_highbd_variance64(const uint8_t *a8, int a_stride,
|
|
|
|
const uint8_t *b8, int b_stride,
|
|
|
|
int w, int h, uint64_t *sse, uint64_t *sum) {
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
uint16_t *a = CONVERT_TO_SHORTPTR(a8);
|
|
|
|
uint16_t *b = CONVERT_TO_SHORTPTR(b8);
|
|
|
|
*sum = 0;
|
|
|
|
*sse = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < h; i++) {
|
|
|
|
for (j = 0; j < w; j++) {
|
|
|
|
const int diff = a[j] - b[j];
|
|
|
|
*sum += diff;
|
|
|
|
*sse += diff * diff;
|
|
|
|
}
|
|
|
|
a += a_stride;
|
|
|
|
b += b_stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void aq_highbd_8_variance(const uint8_t *a8, int a_stride,
|
|
|
|
const uint8_t *b8, int b_stride,
|
|
|
|
int w, int h, unsigned int *sse, int *sum) {
|
|
|
|
uint64_t sse_long = 0;
|
|
|
|
uint64_t sum_long = 0;
|
|
|
|
aq_highbd_variance64(a8, a_stride, b8, b_stride, w, h, &sse_long, &sum_long);
|
|
|
|
*sse = (unsigned int)sse_long;
|
|
|
|
*sum = (int)sum_long;
|
|
|
|
}
|
|
|
|
#endif // CONFIG_VP9_HIGHBITDEPTH
|
2013-10-07 20:20:10 +02:00
|
|
|
|
|
|
|
static unsigned int block_variance(VP9_COMP *cpi, MACROBLOCK *x,
|
|
|
|
BLOCK_SIZE bs) {
|
|
|
|
MACROBLOCKD *xd = &x->e_mbd;
|
|
|
|
unsigned int var, sse;
|
|
|
|
int right_overflow = (xd->mb_to_right_edge < 0) ?
|
|
|
|
((-xd->mb_to_right_edge) >> 3) : 0;
|
|
|
|
int bottom_overflow = (xd->mb_to_bottom_edge < 0) ?
|
|
|
|
((-xd->mb_to_bottom_edge) >> 3) : 0;
|
|
|
|
|
|
|
|
if (right_overflow || bottom_overflow) {
|
2013-11-06 22:34:23 +01:00
|
|
|
const int bw = 8 * num_8x8_blocks_wide_lookup[bs] - right_overflow;
|
|
|
|
const int bh = 8 * num_8x8_blocks_high_lookup[bs] - bottom_overflow;
|
2013-10-07 20:20:10 +02:00
|
|
|
int avg;
|
2014-09-24 15:36:34 +02:00
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
|
2015-06-03 22:23:27 +02:00
|
|
|
aq_highbd_8_variance(x->plane[0].src.buf, x->plane[0].src.stride,
|
|
|
|
CONVERT_TO_BYTEPTR(vp9_highbd_64_zeros), 0, bw, bh,
|
|
|
|
&sse, &avg);
|
2014-09-24 15:36:34 +02:00
|
|
|
sse >>= 2 * (xd->bd - 8);
|
|
|
|
avg >>= (xd->bd - 8);
|
|
|
|
} else {
|
2015-06-03 22:23:27 +02:00
|
|
|
aq_variance(x->plane[0].src.buf, x->plane[0].src.stride,
|
|
|
|
vp9_64_zeros, 0, bw, bh, &sse, &avg);
|
2014-09-24 15:36:34 +02:00
|
|
|
}
|
|
|
|
#else
|
2015-06-03 22:23:27 +02:00
|
|
|
aq_variance(x->plane[0].src.buf, x->plane[0].src.stride,
|
|
|
|
vp9_64_zeros, 0, bw, bh, &sse, &avg);
|
2014-09-24 15:36:34 +02:00
|
|
|
#endif // CONFIG_VP9_HIGHBITDEPTH
|
2013-10-07 20:20:10 +02:00
|
|
|
var = sse - (((int64_t)avg * avg) / (bw * bh));
|
2016-05-04 20:30:00 +02:00
|
|
|
return (unsigned int)(((uint64_t)256 * var) / (bw * bh));
|
2013-10-07 20:20:10 +02:00
|
|
|
} else {
|
2014-09-24 15:36:34 +02:00
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
|
|
|
|
var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf,
|
|
|
|
x->plane[0].src.stride,
|
|
|
|
CONVERT_TO_BYTEPTR(vp9_highbd_64_zeros),
|
|
|
|
0, &sse);
|
|
|
|
} else {
|
|
|
|
var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf,
|
|
|
|
x->plane[0].src.stride,
|
|
|
|
vp9_64_zeros, 0, &sse);
|
|
|
|
}
|
|
|
|
#else
|
2013-10-07 20:20:10 +02:00
|
|
|
var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf,
|
|
|
|
x->plane[0].src.stride,
|
|
|
|
vp9_64_zeros, 0, &sse);
|
2014-09-24 15:36:34 +02:00
|
|
|
#endif // CONFIG_VP9_HIGHBITDEPTH
|
2016-05-04 20:30:00 +02:00
|
|
|
return (unsigned int)(((uint64_t)256 * var) >> num_pels_log2_lookup[bs]);
|
2013-10-07 20:20:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-07 17:32:50 +01:00
|
|
|
double vp9_log_block_var(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) {
|
2013-10-17 20:04:57 +02:00
|
|
|
unsigned int var = block_variance(cpi, x, bs);
|
2015-08-10 20:28:04 +02:00
|
|
|
vpx_clear_system_state();
|
2014-11-07 17:32:50 +01:00
|
|
|
return log(var + 1.0);
|
|
|
|
}
|
2013-10-17 20:04:57 +02:00
|
|
|
|
2014-11-21 21:27:52 +01:00
|
|
|
#define DEFAULT_E_MIDPOINT 10.0
|
2014-11-07 17:32:50 +01:00
|
|
|
int vp9_block_energy(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) {
|
|
|
|
double energy;
|
2014-11-21 00:03:01 +01:00
|
|
|
double energy_midpoint;
|
2015-08-10 20:28:04 +02:00
|
|
|
vpx_clear_system_state();
|
2014-11-21 00:03:01 +01:00
|
|
|
energy_midpoint =
|
|
|
|
(cpi->oxcf.pass == 2) ? cpi->twopass.mb_av_energy : DEFAULT_E_MIDPOINT;
|
|
|
|
energy = vp9_log_block_var(cpi, x, bs) - energy_midpoint;
|
2014-02-19 03:26:42 +01:00
|
|
|
return clamp((int)round(energy), ENERGY_MIN, ENERGY_MAX);
|
2013-10-07 20:20:10 +02:00
|
|
|
}
|