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>
|
|
|
|
|
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"
|
|
|
|
#include "vp9/common/vp9_systemdependent.h"
|
|
|
|
|
2014-02-03 13:47:01 +01:00
|
|
|
#define ENERGY_MIN (-1)
|
|
|
|
#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)
|
|
|
|
|
|
|
|
static double q_ratio[MAX_SEGMENTS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
|
|
|
|
static int segment_id[MAX_SEGMENTS] = { 5, 3, 1, 0, 2, 4, 6, 7 };
|
|
|
|
|
|
|
|
#define Q_RATIO(i) q_ratio[(i) - ENERGY_MIN]
|
|
|
|
#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);
|
2013-10-17 20:04:57 +02:00
|
|
|
|
2013-10-07 20:20:10 +02:00
|
|
|
return SEGMENT_ID(energy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vp9_vaq_init() {
|
|
|
|
int i;
|
2013-10-17 20:04:57 +02:00
|
|
|
double base_ratio;
|
2013-10-07 20:20:10 +02:00
|
|
|
|
|
|
|
assert(ENERGY_SPAN <= MAX_SEGMENTS);
|
|
|
|
|
2014-02-14 00:49:21 +01:00
|
|
|
vp9_clear_system_state();
|
2013-10-17 20:04:57 +02:00
|
|
|
|
2014-02-03 13:47:01 +01:00
|
|
|
base_ratio = 1.5;
|
2013-10-17 20:04:57 +02:00
|
|
|
|
2013-10-07 20:20:10 +02:00
|
|
|
for (i = ENERGY_MIN; i <= ENERGY_MAX; i++) {
|
|
|
|
Q_RATIO(i) = pow(base_ratio, i/3.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void vp9_vaq_frame_setup(VP9_COMP *cpi) {
|
|
|
|
VP9_COMMON *cm = &cpi->common;
|
|
|
|
struct segmentation *seg = &cm->seg;
|
2014-09-03 01:34:09 +02:00
|
|
|
const double base_q = vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth);
|
2013-10-07 20:20:10 +02:00
|
|
|
int i;
|
|
|
|
|
2014-02-03 13:47:01 +01:00
|
|
|
if (cm->frame_type == KEY_FRAME ||
|
|
|
|
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
|
|
|
|
2014-02-14 00:49:21 +01:00
|
|
|
vp9_clear_system_state();
|
2013-10-17 20:04:57 +02:00
|
|
|
|
2014-02-03 13:47:01 +01:00
|
|
|
for (i = ENERGY_MIN; i <= ENERGY_MAX; i++) {
|
2014-11-12 21:36:16 +01:00
|
|
|
int qindex_delta;
|
2013-10-07 20:20:10 +02:00
|
|
|
|
2014-02-03 13:47:01 +01:00
|
|
|
if (Q_RATIO(i) == 1) {
|
|
|
|
// No need to enable SEG_LVL_ALT_Q for this segment
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-07 20:20:10 +02:00
|
|
|
|
2014-09-03 01:34:09 +02:00
|
|
|
qindex_delta = vp9_compute_qdelta(&cpi->rc, base_q, base_q * Q_RATIO(i),
|
|
|
|
cm->bit_depth);
|
2014-02-03 13:47:01 +01:00
|
|
|
vp9_set_segdata(seg, SEGMENT_ID(i), SEG_LVL_ALT_Q, qindex_delta);
|
|
|
|
vp9_enable_segfeature(seg, SEGMENT_ID(i), SEG_LVL_ALT_Q);
|
|
|
|
}
|
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) {
|
2014-10-08 21:43:22 +02:00
|
|
|
highbd_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 {
|
|
|
|
variance(x->plane[0].src.buf, x->plane[0].src.stride,
|
|
|
|
vp9_64_zeros, 0, bw, bh, &sse, &avg);
|
|
|
|
}
|
|
|
|
#else
|
2013-10-07 20:20:10 +02:00
|
|
|
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));
|
|
|
|
return (256 * var) / (bw * bh);
|
|
|
|
} 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
|
2013-10-07 20:20:10 +02:00
|
|
|
return (256 * var) >> num_pels_log2_lookup[bs];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int vp9_block_energy(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) {
|
2013-10-17 20:04:57 +02:00
|
|
|
double energy;
|
|
|
|
unsigned int var = block_variance(cpi, x, bs);
|
|
|
|
|
2014-02-14 00:49:21 +01:00
|
|
|
vp9_clear_system_state();
|
2013-10-17 20:04:57 +02:00
|
|
|
|
2014-02-14 16:14:20 +01:00
|
|
|
energy = 0.9 * (log(var + 1.0) - 10.0);
|
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
|
|
|
}
|