An error occurred while loading the file. Please try again.
-
Yunqing Wang authored
The example encoder down-samples the input video frames a number of times with a down-sampling factor, and then encodes and outputs bitstreams with different resolutions. Support arbitrary down-sampling factor, and down-sampling factor can be different for each encoding level. For example, the encoder can be tested as follows. 1. Configure with multi-resolution encoding enabled: ../libvpx/configure --target=x86-linux-gcc --disable-codecs --enable-vp8 --enable-runtime_cpu_detect --enable-debug --disable-install-docs --enable-error-concealment --enable-multi-res-encoding 2. Run make 3. Encode: If input video is 1280x720, run: ./vp8_multi_resolution_encoder 1280 720 input.yuv 1.ivf 2.ivf 3.ivf 1 (output: 1.ivf(1280x720); 2.ivf(640x360); 3.ivf(320x180). The last parameter is set to 1/0 to show/not show PSNR.) 4. Decode: ./simple_decoder 1.ivf 1.yuv ./simple_decoder 2.ivf 2.yuv ./simple_decoder 3.ivf 3.yuv 5. View video: mplayer 1.yuv -demuxer rawvideo -rawvideo w=1280:h=720 -loop 0 -fps 30 mplayer 2.yuv -demuxer rawvideo -rawvideo w=640:h=360 -loop 0 -fps 30 mplayer 3.yuv -demuxer rawvideo -rawvideo w=320:h=180 -loop 0 -fps 30 The encoding parameters can be modified in vp8_multi_resolution_encoder.c, for example, target bitrate, frame rate... Modified API. John helped a lot with that. Thanks! Change-Id: I03be9a51167eddf94399f92d269599fb3f3d54f5
aa7335e6
vp9_bitstream.c 69.36 KiB
/*
* Copyright (c) 2010 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 "vp9/common/vp9_header.h"
#include "vp9/encoder/vp9_encodemv.h"
#include "vp9/common/vp9_entropymode.h"
#include "vp9/common/vp9_entropymv.h"
#include "vp9/common/vp9_findnearmv.h"
#include "vp9/encoder/vp9_mcomp.h"
#include "vp9/common/vp9_systemdependent.h"
#include <assert.h>
#include <stdio.h>
#include <limits.h>
#include "vp9/common/vp9_pragmas.h"
#include "vpx/vpx_encoder.h"
#include "vpx_mem/vpx_mem.h"
#include "vp9/encoder/vp9_bitstream.h"
#include "vp9/encoder/vp9_segmentation.h"
#include "vp9/common/vp9_seg_common.h"
#include "vp9/common/vp9_pred_common.h"
#include "vp9/common/vp9_entropy.h"
#include "vp9/encoder/vp9_encodemv.h"
#include "vp9/common/vp9_entropymv.h"
#include "vp9/common/vp9_mvref_common.h"
#include "vp9/common/vp9_treecoder.h"
#if defined(SECTIONBITS_OUTPUT)
unsigned __int64 Sectionbits[500];
#endif
#ifdef ENTROPY_STATS
int intra_mode_stats[VP9_KF_BINTRAMODES]
[VP9_KF_BINTRAMODES]
[VP9_KF_BINTRAMODES];
vp9_coeff_stats tree_update_hist_4x4[BLOCK_TYPES_4X4];
vp9_coeff_stats hybrid_tree_update_hist_4x4[BLOCK_TYPES_4X4];
vp9_coeff_stats tree_update_hist_8x8[BLOCK_TYPES_8X8];
vp9_coeff_stats hybrid_tree_update_hist_8x8[BLOCK_TYPES_8X8];
vp9_coeff_stats tree_update_hist_16x16[BLOCK_TYPES_16X16];
vp9_coeff_stats hybrid_tree_update_hist_16x16[BLOCK_TYPES_16X16];
vp9_coeff_stats tree_update_hist_32x32[BLOCK_TYPES_32X32];
extern unsigned int active_section;
#endif
#ifdef MODE_STATS
int count_mb_seg[4] = { 0, 0, 0, 0 };
#endif
#define vp9_cost_upd ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)) >> 8)
#define vp9_cost_upd256 ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)))
#define SEARCH_NEWP
static int update_bits[255];
static void compute_update_table() {
int i;
for (i = 0; i < 255; i++)
update_bits[i] = vp9_count_term_subexp(i, SUBEXP_PARAM, 255);
}
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
static int split_index(int i, int n, int modulus) {
int max1 = (n - 1 - modulus / 2) / modulus + 1;
if (i % modulus == modulus / 2) i = i / modulus;
else i = max1 + i - (i + modulus - modulus / 2) / modulus;
return i;
}
static int remap_prob(int v, int m) {
const int n = 256;
const int modulus = MODULUS_PARAM;
int i;
if ((m << 1) <= n)
i = vp9_recenter_nonneg(v, m) - 1;
else
i = vp9_recenter_nonneg(n - 1 - v, n - 1 - m) - 1;
i = split_index(i, n - 1, modulus);
return i;
}
static void write_prob_diff_update(vp9_writer *const bc,
vp9_prob newp, vp9_prob oldp) {
int delp = remap_prob(newp, oldp);
vp9_encode_term_subexp(bc, delp, SUBEXP_PARAM, 255);
}
static int prob_diff_update_cost(vp9_prob newp, vp9_prob oldp) {
int delp = remap_prob(newp, oldp);
return update_bits[delp] * 256;
}
static void update_mode(
vp9_writer *const bc,
int n,
vp9_token tok [/* n */],
vp9_tree tree,
vp9_prob Pnew [/* n-1 */],
vp9_prob Pcur [/* n-1 */],
unsigned int bct [/* n-1 */] [2],
const unsigned int num_events[/* n */]
) {
unsigned int new_b = 0, old_b = 0;
int i = 0;
vp9_tree_probs_from_distribution(n--, tok, tree,
Pnew, bct, num_events);
do {
new_b += cost_branch(bct[i], Pnew[i]);
old_b += cost_branch(bct[i], Pcur[i]);
} while (++i < n);
if (new_b + (n << 8) < old_b) {
int i = 0;
vp9_write_bit(bc, 1);
do {
const vp9_prob p = Pnew[i];
vp9_write_literal(bc, Pcur[i] = p ? p : 1, 8);
} while (++i < n);
} else
vp9_write_bit(bc, 0);
}
static void update_mbintra_mode_probs(VP9_COMP* const cpi,
vp9_writer* const bc) {
VP9_COMMON *const cm = &cpi->common;
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
{
vp9_prob Pnew [VP9_YMODES - 1];
unsigned int bct [VP9_YMODES - 1] [2];
update_mode(
bc, VP9_YMODES, vp9_ymode_encodings, vp9_ymode_tree,
Pnew, cm->fc.ymode_prob, bct, (unsigned int *)cpi->ymode_count
);
update_mode(bc, VP9_I32X32_MODES, vp9_sb_ymode_encodings,
vp9_sb_ymode_tree, Pnew, cm->fc.sb_ymode_prob, bct,
(unsigned int *)cpi->sb_ymode_count);
}
}
void vp9_update_skip_probs(VP9_COMP *cpi) {
VP9_COMMON *const pc = &cpi->common;
int k;
for (k = 0; k < MBSKIP_CONTEXTS; ++k) {
pc->mbskip_pred_probs[k] = get_binary_prob(cpi->skip_false_count[k],
cpi->skip_true_count[k]);
}
}
static void update_switchable_interp_probs(VP9_COMP *cpi,
vp9_writer* const bc) {
VP9_COMMON *const pc = &cpi->common;
unsigned int branch_ct[32][2];
int i, j;
for (j = 0; j <= VP9_SWITCHABLE_FILTERS; ++j) {
vp9_tree_probs_from_distribution(
VP9_SWITCHABLE_FILTERS,
vp9_switchable_interp_encodings, vp9_switchable_interp_tree,
pc->fc.switchable_interp_prob[j], branch_ct,
cpi->switchable_interp_count[j]);
for (i = 0; i < VP9_SWITCHABLE_FILTERS - 1; ++i) {
if (pc->fc.switchable_interp_prob[j][i] < 1)
pc->fc.switchable_interp_prob[j][i] = 1;
vp9_write_literal(bc, pc->fc.switchable_interp_prob[j][i], 8);
}
}
}
// This function updates the reference frame prediction stats
static void update_refpred_stats(VP9_COMP *cpi) {
VP9_COMMON *const cm = &cpi->common;
int i;
vp9_prob new_pred_probs[PREDICTION_PROBS];
int old_cost, new_cost;
// Set the prediction probability structures to defaults
if (cm->frame_type == KEY_FRAME) {
// Set the prediction probabilities to defaults
cm->ref_pred_probs[0] = 120;
cm->ref_pred_probs[1] = 80;
cm->ref_pred_probs[2] = 40;
vpx_memset(cpi->ref_pred_probs_update, 0,
sizeof(cpi->ref_pred_probs_update));
} else {
// From the prediction counts set the probabilities for each context
for (i = 0; i < PREDICTION_PROBS; i++) {
new_pred_probs[i] = get_binary_prob(cpi->ref_pred_count[i][0],
cpi->ref_pred_count[i][1]);
// Decide whether or not to update the reference frame probs.
// Returned costs are in 1/256 bit units.
old_cost =
(cpi->ref_pred_count[i][0] * vp9_cost_zero(cm->ref_pred_probs[i])) +
(cpi->ref_pred_count[i][1] * vp9_cost_one(cm->ref_pred_probs[i]));
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
new_cost =
(cpi->ref_pred_count[i][0] * vp9_cost_zero(new_pred_probs[i])) +
(cpi->ref_pred_count[i][1] * vp9_cost_one(new_pred_probs[i]));
// Cost saving must be >= 8 bits (2048 in these units)
if ((old_cost - new_cost) >= 2048) {
cpi->ref_pred_probs_update[i] = 1;
cm->ref_pred_probs[i] = new_pred_probs[i];
} else
cpi->ref_pred_probs_update[i] = 0;
}
}
}
// This function is called to update the mode probability context used to encode
// inter modes. It assumes the branch counts table has already been populated
// prior to the actual packing of the bitstream (in rd stage or dummy pack)
//
// The branch counts table is re-populated during the actual pack stage and in
// the decoder to facilitate backwards update of the context.
static void update_mode_probs(VP9_COMMON *cm,
int mode_context[INTER_MODE_CONTEXTS][4]) {
int i, j;
unsigned int (*mv_ref_ct)[4][2];
vpx_memcpy(mode_context, cm->fc.vp9_mode_contexts,
sizeof(cm->fc.vp9_mode_contexts));
mv_ref_ct = cm->fc.mv_ref_ct;
for (i = 0; i < INTER_MODE_CONTEXTS; i++) {
for (j = 0; j < 4; j++) {
int new_prob, old_cost, new_cost;
// Work out cost of coding branches with the old and optimal probability
old_cost = cost_branch256(mv_ref_ct[i][j], mode_context[i][j]);
new_prob = get_binary_prob(mv_ref_ct[i][j][0], mv_ref_ct[i][j][1]);
new_cost = cost_branch256(mv_ref_ct[i][j], new_prob);
// If cost saving is >= 14 bits then update the mode probability.
// This is the approximate net cost of updating one probability given
// that the no update case ismuch more common than the update case.
if (new_cost <= (old_cost - (14 << 8))) {
mode_context[i][j] = new_prob;
}
}
}
}
#if CONFIG_NEW_MVREF
static void update_mv_ref_probs(VP9_COMP *cpi,
int mvref_probs[MAX_REF_FRAMES]
[MAX_MV_REF_CANDIDATES-1]) {
MACROBLOCKD *xd = &cpi->mb.e_mbd;
int rf; // Reference frame
int ref_c; // Motion reference candidate
int node; // Probability node index
for (rf = 0; rf < MAX_REF_FRAMES; ++rf) {
int count = 0;
// Skip the dummy entry for intra ref frame.
if (rf == INTRA_FRAME) {
continue;
}
// Sum the counts for all candidates
for (ref_c = 0; ref_c < MAX_MV_REF_CANDIDATES; ++ref_c) {
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
count += cpi->mb_mv_ref_count[rf][ref_c];
}
// Calculate the tree node probabilities
for (node = 0; node < MAX_MV_REF_CANDIDATES-1; ++node) {
int new_prob, old_cost, new_cost;
unsigned int branch_cnts[2];
// How many hits on each branch at this node
branch_cnts[0] = cpi->mb_mv_ref_count[rf][node];
branch_cnts[1] = count - cpi->mb_mv_ref_count[rf][node];
// Work out cost of coding branches with the old and optimal probability
old_cost = cost_branch256(branch_cnts, xd->mb_mv_ref_probs[rf][node]);
new_prob = get_prob(branch_cnts[0], count);
new_cost = cost_branch256(branch_cnts, new_prob);
// Take current 0 branch cases out of residual count
count -= cpi->mb_mv_ref_count[rf][node];
if ((new_cost + VP9_MV_REF_UPDATE_COST) <= old_cost) {
mvref_probs[rf][node] = new_prob;
} else {
mvref_probs[rf][node] = xd->mb_mv_ref_probs[rf][node];
}
}
}
}
#endif
static void write_ymode(vp9_writer *bc, int m, const vp9_prob *p) {
write_token(bc, vp9_ymode_tree, p, vp9_ymode_encodings + m);
}
static void kfwrite_ymode(vp9_writer *bc, int m, const vp9_prob *p) {
write_token(bc, vp9_kf_ymode_tree, p, vp9_kf_ymode_encodings + m);
}
static void write_sb_ymode(vp9_writer *bc, int m, const vp9_prob *p) {
write_token(bc, vp9_sb_ymode_tree, p, vp9_sb_ymode_encodings + m);
}
static void sb_kfwrite_ymode(vp9_writer *bc, int m, const vp9_prob *p) {
write_token(bc, vp9_uv_mode_tree, p, vp9_sb_kf_ymode_encodings + m);
}
static void write_i8x8_mode(vp9_writer *bc, int m, const vp9_prob *p) {
write_token(bc, vp9_i8x8_mode_tree, p, vp9_i8x8_mode_encodings + m);
}
static void write_uv_mode(vp9_writer *bc, int m, const vp9_prob *p) {
write_token(bc, vp9_uv_mode_tree, p, vp9_uv_mode_encodings + m);
}
static void write_bmode(vp9_writer *bc, int m, const vp9_prob *p) {
#if CONFIG_NEWBINTRAMODES
assert(m < B_CONTEXT_PRED - CONTEXT_PRED_REPLACEMENTS || m == B_CONTEXT_PRED);
if (m == B_CONTEXT_PRED) m -= CONTEXT_PRED_REPLACEMENTS;
#endif
write_token(bc, vp9_bmode_tree, p, vp9_bmode_encodings + m);
}
static void write_kf_bmode(vp9_writer *bc, int m, const vp9_prob *p) {
write_token(bc, vp9_kf_bmode_tree, p, vp9_kf_bmode_encodings + m);
}
static void write_split(vp9_writer *bc, int x, const vp9_prob *p) {
write_token(
bc, vp9_mbsplit_tree, p, vp9_mbsplit_encodings + x);
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
}
static int prob_update_savings(const unsigned int *ct,
const vp9_prob oldp, const vp9_prob newp,
const vp9_prob upd) {
const int old_b = cost_branch256(ct, oldp);
const int new_b = cost_branch256(ct, newp);
const int update_b = 2048 + vp9_cost_upd256;
return (old_b - new_b - update_b);
}
static int prob_diff_update_savings(const unsigned int *ct,
const vp9_prob oldp, const vp9_prob newp,
const vp9_prob upd) {
const int old_b = cost_branch256(ct, oldp);
const int new_b = cost_branch256(ct, newp);
const int update_b = (newp == oldp ? 0 :
prob_diff_update_cost(newp, oldp) + vp9_cost_upd256);
return (old_b - new_b - update_b);
}
static int prob_diff_update_savings_search(const unsigned int *ct,
const vp9_prob oldp, vp9_prob *bestp,
const vp9_prob upd) {
const int old_b = cost_branch256(ct, oldp);
int new_b, update_b, savings, bestsavings, step;
vp9_prob newp, bestnewp;
bestsavings = 0;
bestnewp = oldp;
step = (*bestp > oldp ? -1 : 1);
for (newp = *bestp; newp != oldp; newp += step) {
new_b = cost_branch256(ct, newp);
update_b = prob_diff_update_cost(newp, oldp) + vp9_cost_upd256;
savings = old_b - new_b - update_b;
if (savings > bestsavings) {
bestsavings = savings;
bestnewp = newp;
}
}
*bestp = bestnewp;
return bestsavings;
}
static void vp9_cond_prob_update(vp9_writer *bc, vp9_prob *oldp, vp9_prob upd,
unsigned int *ct) {
vp9_prob newp;
int savings;
newp = get_binary_prob(ct[0], ct[1]);
savings = prob_update_savings(ct, *oldp, newp, upd);
if (savings > 0) {
vp9_write(bc, 1, upd);
vp9_write_literal(bc, newp, 8);
*oldp = newp;
} else {
vp9_write(bc, 0, upd);
}
}
static void pack_mb_tokens(vp9_writer* const bc,
TOKENEXTRA **tp,
const TOKENEXTRA *const stop) {
TOKENEXTRA *p = *tp;
while (p < stop) {
const int t = p->Token;
vp9_token *const a = vp9_coef_encodings + t;
const vp9_extra_bit_struct *const b = vp9_extra_bits + t;
int i = 0;
421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
const unsigned char *pp = p->context_tree;
int v = a->value;
int n = a->Len;
if (t == EOSB_TOKEN)
{
++p;
break;
}
/* skip one or two nodes */
if (p->skip_eob_node) {
n -= p->skip_eob_node;
i = 2 * p->skip_eob_node;
}
do {
const int bb = (v >> --n) & 1;
encode_bool(bc, bb, pp[i >> 1]);
i = vp9_coef_tree[i + bb];
} while (n);
if (b->base_val) {
const int e = p->Extra, L = b->Len;
if (L) {
const unsigned char *pp = b->prob;
int v = e >> 1;
int n = L; /* number of bits in v, assumed nonzero */
int i = 0;
do {
const int bb = (v >> --n) & 1;
encode_bool(bc, bb, pp[i >> 1]);
i = b->tree[i + bb];
} while (n);
}
encode_bool(bc, e & 1, 128);
}
++p;
}
*tp = p;
}
static void write_partition_size(unsigned char *cx_data, int size) {
signed char csize;
csize = size & 0xff;
*cx_data = csize;
csize = (size >> 8) & 0xff;
*(cx_data + 1) = csize;
csize = (size >> 16) & 0xff;
*(cx_data + 2) = csize;
}
static void write_mv_ref
(
vp9_writer *bc, MB_PREDICTION_MODE m, const vp9_prob *p
) {
#if CONFIG_DEBUG
assert(NEARESTMV <= m && m <= SPLITMV);
#endif
write_token(bc, vp9_mv_ref_tree, p,
vp9_mv_ref_encoding_array - NEARESTMV + m);
}
491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
static void write_sb_mv_ref(vp9_writer *bc, MB_PREDICTION_MODE m,
const vp9_prob *p) {
#if CONFIG_DEBUG
assert(NEARESTMV <= m && m < SPLITMV);
#endif
write_token(bc, vp9_sb_mv_ref_tree, p,
vp9_sb_mv_ref_encoding_array - NEARESTMV + m);
}
static void write_sub_mv_ref
(
vp9_writer *bc, B_PREDICTION_MODE m, const vp9_prob *p
) {
#if CONFIG_DEBUG
assert(LEFT4X4 <= m && m <= NEW4X4);
#endif
write_token(bc, vp9_sub_mv_ref_tree, p,
vp9_sub_mv_ref_encoding_array - LEFT4X4 + m);
}
static void write_nmv(vp9_writer *bc, const MV *mv, const int_mv *ref,
const nmv_context *nmvc, int usehp) {
MV e;
e.row = mv->row - ref->as_mv.row;
e.col = mv->col - ref->as_mv.col;
vp9_encode_nmv(bc, &e, &ref->as_mv, nmvc);
vp9_encode_nmv_fp(bc, &e, &ref->as_mv, nmvc, usehp);
}
#if CONFIG_NEW_MVREF
static void vp9_write_mv_ref_id(vp9_writer *w,
vp9_prob * ref_id_probs,
int mv_ref_id) {
// Encode the index for the MV reference.
switch (mv_ref_id) {
case 0:
vp9_write(w, 0, ref_id_probs[0]);
break;
case 1:
vp9_write(w, 1, ref_id_probs[0]);
vp9_write(w, 0, ref_id_probs[1]);
break;
case 2:
vp9_write(w, 1, ref_id_probs[0]);
vp9_write(w, 1, ref_id_probs[1]);
vp9_write(w, 0, ref_id_probs[2]);
break;
case 3:
vp9_write(w, 1, ref_id_probs[0]);
vp9_write(w, 1, ref_id_probs[1]);
vp9_write(w, 1, ref_id_probs[2]);
break;
// TRAP.. This should not happen
default:
assert(0);
break;
}
}
#endif
// This function writes the current macro block's segnment id to the bitstream
// It should only be called if a segment map update is indicated.
static void write_mb_segid(vp9_writer *bc,
const MB_MODE_INFO *mi, const MACROBLOCKD *xd) {
// Encode the MB segment id.
int seg_id = mi->segment_id;
if (xd->segmentation_enabled && xd->update_mb_segmentation_map) {
561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
switch (seg_id) {
case 0:
vp9_write(bc, 0, xd->mb_segment_tree_probs[0]);
vp9_write(bc, 0, xd->mb_segment_tree_probs[1]);
break;
case 1:
vp9_write(bc, 0, xd->mb_segment_tree_probs[0]);
vp9_write(bc, 1, xd->mb_segment_tree_probs[1]);
break;
case 2:
vp9_write(bc, 1, xd->mb_segment_tree_probs[0]);
vp9_write(bc, 0, xd->mb_segment_tree_probs[2]);
break;
case 3:
vp9_write(bc, 1, xd->mb_segment_tree_probs[0]);
vp9_write(bc, 1, xd->mb_segment_tree_probs[2]);
break;
// TRAP.. This should not happen
default:
vp9_write(bc, 0, xd->mb_segment_tree_probs[0]);
vp9_write(bc, 0, xd->mb_segment_tree_probs[1]);
break;
}
}
}
// This function encodes the reference frame
static void encode_ref_frame(vp9_writer *const bc,
VP9_COMMON *const cm,
MACROBLOCKD *xd,
int segment_id,
MV_REFERENCE_FRAME rf) {
int seg_ref_active;
int seg_ref_count = 0;
seg_ref_active = vp9_segfeature_active(xd,
segment_id,
SEG_LVL_REF_FRAME);
if (seg_ref_active) {
seg_ref_count = vp9_check_segref(xd, segment_id, INTRA_FRAME) +
vp9_check_segref(xd, segment_id, LAST_FRAME) +
vp9_check_segref(xd, segment_id, GOLDEN_FRAME) +
vp9_check_segref(xd, segment_id, ALTREF_FRAME);
}
// If segment level coding of this signal is disabled...
// or the segment allows multiple reference frame options
if (!seg_ref_active || (seg_ref_count > 1)) {
// Values used in prediction model coding
unsigned char prediction_flag;
vp9_prob pred_prob;
MV_REFERENCE_FRAME pred_rf;
// Get the context probability the prediction flag
pred_prob = vp9_get_pred_prob(cm, xd, PRED_REF);
// Get the predicted value.
pred_rf = vp9_get_pred_ref(cm, xd);
// Did the chosen reference frame match its predicted value.
prediction_flag =
(xd->mode_info_context->mbmi.ref_frame == pred_rf);
vp9_set_pred_flag(xd, PRED_REF, prediction_flag);
vp9_write(bc, prediction_flag, pred_prob);
// If not predicted correctly then code value explicitly
if (!prediction_flag) {
vp9_prob mod_refprobs[PREDICTION_PROBS];
631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
vpx_memcpy(mod_refprobs,
cm->mod_refprobs[pred_rf], sizeof(mod_refprobs));
// If segment coding enabled blank out options that cant occur by
// setting the branch probability to 0.
if (seg_ref_active) {
mod_refprobs[INTRA_FRAME] *=
vp9_check_segref(xd, segment_id, INTRA_FRAME);
mod_refprobs[LAST_FRAME] *=
vp9_check_segref(xd, segment_id, LAST_FRAME);
mod_refprobs[GOLDEN_FRAME] *=
(vp9_check_segref(xd, segment_id, GOLDEN_FRAME) *
vp9_check_segref(xd, segment_id, ALTREF_FRAME));
}
if (mod_refprobs[0]) {
vp9_write(bc, (rf != INTRA_FRAME), mod_refprobs[0]);
}
// Inter coded
if (rf != INTRA_FRAME) {
if (mod_refprobs[1]) {
vp9_write(bc, (rf != LAST_FRAME), mod_refprobs[1]);
}
if (rf != LAST_FRAME) {
if (mod_refprobs[2]) {
vp9_write(bc, (rf != GOLDEN_FRAME), mod_refprobs[2]);
}
}
}
}
}
// if using the prediction mdoel we have nothing further to do because
// the reference frame is fully coded by the segment
}
// Update the probabilities used to encode reference frame data
static void update_ref_probs(VP9_COMP *const cpi) {
VP9_COMMON *const cm = &cpi->common;
const int *const rfct = cpi->count_mb_ref_frame_usage;
const int rf_intra = rfct[INTRA_FRAME];
const int rf_inter = rfct[LAST_FRAME] +
rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
cm->prob_intra_coded = get_binary_prob(rf_intra, rf_inter);
cm->prob_last_coded = get_prob(rfct[LAST_FRAME], rf_inter);
cm->prob_gf_coded = get_binary_prob(rfct[GOLDEN_FRAME], rfct[ALTREF_FRAME]);
// Compute a modified set of probabilities to use when prediction of the
// reference frame fails
vp9_compute_mod_refprobs(cm);
}
static void pack_inter_mode_mvs(VP9_COMP *cpi, MODE_INFO *m,
vp9_writer *bc,
int mb_rows_left, int mb_cols_left) {
VP9_COMMON *const pc = &cpi->common;
const nmv_context *nmvc = &pc->fc.nmvc;
MACROBLOCK *const x = &cpi->mb;
MACROBLOCKD *const xd = &x->e_mbd;
const int mis = pc->mode_info_stride;
MB_MODE_INFO *const mi = &m->mbmi;
const MV_REFERENCE_FRAME rf = mi->ref_frame;
const MB_PREDICTION_MODE mode = mi->mode;
const int segment_id = mi->segment_id;
const int mb_size = 1 << mi->sb_type;
701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
int skip_coeff;
int mb_row = pc->mb_rows - mb_rows_left;
int mb_col = pc->mb_cols - mb_cols_left;
xd->prev_mode_info_context = pc->prev_mi + (m - pc->mi);
x->partition_info = x->pi + (m - pc->mi);
// Distance of Mb to the various image edges.
// These specified to 8th pel as they are always compared to MV
// values that are in 1/8th pel units
xd->mb_to_left_edge = -((mb_col * 16) << 3);
xd->mb_to_top_edge = -((mb_row * 16)) << 3;
xd->mb_to_right_edge = ((pc->mb_cols - mb_size - mb_col) * 16) << 3;
xd->mb_to_bottom_edge = ((pc->mb_rows - mb_size - mb_row) * 16) << 3;
#ifdef ENTROPY_STATS
active_section = 9;
#endif
if (cpi->mb.e_mbd.update_mb_segmentation_map) {
// Is temporal coding of the segment map enabled
if (pc->temporal_update) {
unsigned char prediction_flag = vp9_get_pred_flag(xd, PRED_SEG_ID);
vp9_prob pred_prob = vp9_get_pred_prob(pc, xd, PRED_SEG_ID);
// Code the segment id prediction flag for this mb
vp9_write(bc, prediction_flag, pred_prob);
// If the mb segment id wasn't predicted code explicitly
if (!prediction_flag)
write_mb_segid(bc, mi, &cpi->mb.e_mbd);
} else {
// Normal unpredicted coding
write_mb_segid(bc, mi, &cpi->mb.e_mbd);
}
}
if (!pc->mb_no_coeff_skip) {
skip_coeff = 0;
} else if (vp9_segfeature_active(xd, segment_id, SEG_LVL_EOB) &&
vp9_get_segdata(xd, segment_id, SEG_LVL_EOB) == 0) {
skip_coeff = 1;
} else {
const int nmbs = mb_size;
const int xmbs = MIN(nmbs, mb_cols_left);
const int ymbs = MIN(nmbs, mb_rows_left);
int x, y;
skip_coeff = 1;
for (y = 0; y < ymbs; y++) {
for (x = 0; x < xmbs; x++) {
skip_coeff = skip_coeff && m[y * mis + x].mbmi.mb_skip_coeff;
}
}
vp9_write(bc, skip_coeff,
vp9_get_pred_prob(pc, xd, PRED_MBSKIP));
}
// Encode the reference frame.
if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_MODE)
|| vp9_get_segdata(xd, segment_id, SEG_LVL_MODE) >= NEARESTMV) {
encode_ref_frame(bc, pc, xd, segment_id, rf);
} else {
assert(rf == INTRA_FRAME);
}
if (rf == INTRA_FRAME) {
#ifdef ENTROPY_STATS
active_section = 6;
771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
#endif
if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_MODE)) {
if (m->mbmi.sb_type)
write_sb_ymode(bc, mode, pc->fc.sb_ymode_prob);
else
write_ymode(bc, mode, pc->fc.ymode_prob);
}
if (mode == B_PRED) {
int j = 0;
#if CONFIG_COMP_INTRA_PRED
int uses_second =
m->bmi[0].as_mode.second !=
(B_PREDICTION_MODE)(B_DC_PRED - 1);
vp9_write(bc, uses_second, DEFAULT_COMP_INTRA_PROB);
#endif
do {
#if CONFIG_COMP_INTRA_PRED
B_PREDICTION_MODE mode2 = m->bmi[j].as_mode.second;
#endif
write_bmode(bc, m->bmi[j].as_mode.first,
pc->fc.bmode_prob);
#if CONFIG_COMP_INTRA_PRED
if (uses_second) {
write_bmode(bc, mode2, pc->fc.bmode_prob);
}
#endif
} while (++j < 16);
}
if (mode == I8X8_PRED) {
write_i8x8_mode(bc, m->bmi[0].as_mode.first,
pc->fc.i8x8_mode_prob);
write_i8x8_mode(bc, m->bmi[2].as_mode.first,
pc->fc.i8x8_mode_prob);
write_i8x8_mode(bc, m->bmi[8].as_mode.first,
pc->fc.i8x8_mode_prob);
write_i8x8_mode(bc, m->bmi[10].as_mode.first,
pc->fc.i8x8_mode_prob);
} else {
write_uv_mode(bc, mi->uv_mode,
pc->fc.uv_mode_prob[mode]);
}
} else {
vp9_prob mv_ref_p[VP9_MVREFS - 1];
vp9_mv_ref_probs(&cpi->common, mv_ref_p, mi->mb_mode_context[rf]);
// #ifdef ENTROPY_STATS
#ifdef ENTROPY_STATS
accum_mv_refs(mode, ct);
active_section = 3;
#endif
// Is the segment coding of mode enabled
if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_MODE)) {
if (mi->sb_type) {
write_sb_mv_ref(bc, mode, mv_ref_p);
} else {
write_mv_ref(bc, mode, mv_ref_p);
}
vp9_accum_mv_refs(&cpi->common, mode, mi->mb_mode_context[rf]);
}
if (mode >= NEARESTMV && mode <= SPLITMV) {
if (cpi->common.mcomp_filter_type == SWITCHABLE) {
write_token(bc, vp9_switchable_interp_tree,
vp9_get_pred_probs(&cpi->common, xd,
PRED_SWITCHABLE_INTERP),
vp9_switchable_interp_encodings +
vp9_switchable_interp_map[mi->interp_filter]);
841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
} else {
assert(mi->interp_filter == cpi->common.mcomp_filter_type);
}
}
// does the feature use compound prediction or not
// (if not specified at the frame/segment level)
if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) {
vp9_write(bc, mi->second_ref_frame > INTRA_FRAME,
vp9_get_pred_prob(pc, xd, PRED_COMP));
}
#if CONFIG_COMP_INTERINTRA_PRED
if (cpi->common.use_interintra &&
mode >= NEARESTMV && mode < SPLITMV &&
mi->second_ref_frame <= INTRA_FRAME) {
vp9_write(bc, mi->second_ref_frame == INTRA_FRAME,
pc->fc.interintra_prob);
// if (!cpi->dummy_packing)
// printf("-- %d (%d)\n", mi->second_ref_frame == INTRA_FRAME,
// pc->fc.interintra_prob);
if (mi->second_ref_frame == INTRA_FRAME) {
// if (!cpi->dummy_packing)
// printf("** %d %d\n", mi->interintra_mode,
// mi->interintra_uv_mode);
write_ymode(bc, mi->interintra_mode, pc->fc.ymode_prob);
#if SEPARATE_INTERINTRA_UV
write_uv_mode(bc, mi->interintra_uv_mode,
pc->fc.uv_mode_prob[mi->interintra_mode]);
#endif
}
}
#endif
#if CONFIG_NEW_MVREF
// if ((mode == NEWMV) || (mode == SPLITMV)) {
if (mode == NEWMV) {
// Encode the index of the choice.
vp9_write_mv_ref_id(bc,
xd->mb_mv_ref_probs[rf], mi->best_index);
if (mi->second_ref_frame > 0) {
// Encode the index of the choice.
vp9_write_mv_ref_id(
bc, xd->mb_mv_ref_probs[mi->second_ref_frame],
mi->best_second_index);
}
}
#endif
switch (mode) { /* new, split require MVs */
case NEWMV:
#ifdef ENTROPY_STATS
active_section = 5;
#endif
write_nmv(bc, &mi->mv[0].as_mv, &mi->best_mv,
(const nmv_context*) nmvc,
xd->allow_high_precision_mv);
if (mi->second_ref_frame > 0) {
write_nmv(bc, &mi->mv[1].as_mv, &mi->best_second_mv,
(const nmv_context*) nmvc,
xd->allow_high_precision_mv);
}
break;
case SPLITMV: {
int j = 0;
#ifdef MODE_STATS
++count_mb_seg[mi->partitioning];
#endif
911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
write_split(bc, mi->partitioning, cpi->common.fc.mbsplit_prob);
cpi->mbsplit_count[mi->partitioning]++;
do {
B_PREDICTION_MODE blockmode;
int_mv blockmv;
const int *const L = vp9_mbsplits[mi->partitioning];
int k = -1; /* first block in subset j */
int mv_contz;
int_mv leftmv, abovemv;
blockmode = cpi->mb.partition_info->bmi[j].mode;
blockmv = cpi->mb.partition_info->bmi[j].mv;
#if CONFIG_DEBUG
while (j != L[++k])
if (k >= 16)
assert(0);
#else
while (j != L[++k]);
#endif
leftmv.as_int = left_block_mv(m, k);
abovemv.as_int = above_block_mv(m, k, mis);
mv_contz = vp9_mv_cont(&leftmv, &abovemv);
write_sub_mv_ref(bc, blockmode,
cpi->common.fc.sub_mv_ref_prob[mv_contz]);
cpi->sub_mv_ref_count[mv_contz][blockmode - LEFT4X4]++;
if (blockmode == NEW4X4) {
#ifdef ENTROPY_STATS
active_section = 11;
#endif
write_nmv(bc, &blockmv.as_mv, &mi->best_mv,
(const nmv_context*) nmvc,
xd->allow_high_precision_mv);
if (mi->second_ref_frame > 0) {
write_nmv(bc,
&cpi->mb.partition_info->bmi[j].second_mv.as_mv,
&mi->best_second_mv,
(const nmv_context*) nmvc,
xd->allow_high_precision_mv);
}
}
} while (++j < cpi->mb.partition_info->count);
break;
}
default:
break;
}
}
if (((rf == INTRA_FRAME && mode <= I8X8_PRED) ||
(rf != INTRA_FRAME && !(mode == SPLITMV &&
mi->partitioning == PARTITIONING_4X4))) &&
pc->txfm_mode == TX_MODE_SELECT &&
!((pc->mb_no_coeff_skip && skip_coeff) ||
(vp9_segfeature_active(xd, segment_id, SEG_LVL_EOB) &&
vp9_get_segdata(xd, segment_id, SEG_LVL_EOB) == 0))) {
TX_SIZE sz = mi->txfm_size;
// FIXME(rbultje) code ternary symbol once all experiments are merged
vp9_write(bc, sz != TX_4X4, pc->prob_tx[0]);
if (sz != TX_4X4 && mode != I8X8_PRED && mode != SPLITMV) {
vp9_write(bc, sz != TX_8X8, pc->prob_tx[1]);
#if CONFIG_TX32X32
if (mi->sb_type && sz != TX_8X8)
vp9_write(bc, sz != TX_16X16, pc->prob_tx[2]);
#endif
}
}
981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
}
static void write_mb_modes_kf(const VP9_COMP *cpi,
const MODE_INFO *m,
vp9_writer *bc,
int mb_rows_left, int mb_cols_left) {
const VP9_COMMON *const c = &cpi->common;
const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
const int mis = c->mode_info_stride;
const int ym = m->mbmi.mode;
const int segment_id = m->mbmi.segment_id;
int skip_coeff;
if (xd->update_mb_segmentation_map) {
write_mb_segid(bc, &m->mbmi, xd);
}
if (!c->mb_no_coeff_skip) {
skip_coeff = 0;
} else if (vp9_segfeature_active(xd, segment_id, SEG_LVL_EOB) &&
vp9_get_segdata(xd, segment_id, SEG_LVL_EOB) == 0) {
skip_coeff = 1;
} else {
const int nmbs = 1 << m->mbmi.sb_type;
const int xmbs = MIN(nmbs, mb_cols_left);
const int ymbs = MIN(nmbs, mb_rows_left);
int x, y;
skip_coeff = 1;
for (y = 0; y < ymbs; y++) {
for (x = 0; x < xmbs; x++) {
skip_coeff = skip_coeff && m[y * mis + x].mbmi.mb_skip_coeff;
}
}
vp9_write(bc, skip_coeff,
vp9_get_pred_prob(c, xd, PRED_MBSKIP));
}
if (m->mbmi.sb_type) {
sb_kfwrite_ymode(bc, ym,
c->sb_kf_ymode_prob[c->kf_ymode_probs_index]);
} else {
kfwrite_ymode(bc, ym,
c->kf_ymode_prob[c->kf_ymode_probs_index]);
}
if (ym == B_PRED) {
int i = 0;
#if CONFIG_COMP_INTRA_PRED
int uses_second =
m->bmi[0].as_mode.second !=
(B_PREDICTION_MODE)(B_DC_PRED - 1);
vp9_write(bc, uses_second, DEFAULT_COMP_INTRA_PROB);
#endif
do {
const B_PREDICTION_MODE A = above_block_mode(m, i, mis);
const B_PREDICTION_MODE L = left_block_mode(m, i);
const int bm = m->bmi[i].as_mode.first;
#if CONFIG_COMP_INTRA_PRED
const int bm2 = m->bmi[i].as_mode.second;
#endif
#ifdef ENTROPY_STATS
++intra_mode_stats [A] [L] [bm];
#endif
write_kf_bmode(bc, bm, c->kf_bmode_prob[A][L]);
#if CONFIG_COMP_INTRA_PRED
if (uses_second) {
1051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
write_kf_bmode(bc, bm2, c->kf_bmode_prob[A][L]);
}
#endif
} while (++i < 16);
}
if (ym == I8X8_PRED) {
write_i8x8_mode(bc, m->bmi[0].as_mode.first,
c->fc.i8x8_mode_prob);
// printf(" mode: %d\n", m->bmi[0].as_mode.first); fflush(stdout);
write_i8x8_mode(bc, m->bmi[2].as_mode.first,
c->fc.i8x8_mode_prob);
// printf(" mode: %d\n", m->bmi[2].as_mode.first); fflush(stdout);
write_i8x8_mode(bc, m->bmi[8].as_mode.first,
c->fc.i8x8_mode_prob);
// printf(" mode: %d\n", m->bmi[8].as_mode.first); fflush(stdout);
write_i8x8_mode(bc, m->bmi[10].as_mode.first,
c->fc.i8x8_mode_prob);
// printf(" mode: %d\n", m->bmi[10].as_mode.first); fflush(stdout);
} else
write_uv_mode(bc, m->mbmi.uv_mode, c->kf_uv_mode_prob[ym]);
if (ym <= I8X8_PRED && c->txfm_mode == TX_MODE_SELECT &&
!((c->mb_no_coeff_skip && skip_coeff) ||
(vp9_segfeature_active(xd, segment_id, SEG_LVL_EOB) &&
vp9_get_segdata(xd, segment_id, SEG_LVL_EOB) == 0))) {
TX_SIZE sz = m->mbmi.txfm_size;
// FIXME(rbultje) code ternary symbol once all experiments are merged
vp9_write(bc, sz != TX_4X4, c->prob_tx[0]);
if (sz != TX_4X4 && ym <= TM_PRED) {
vp9_write(bc, sz != TX_8X8, c->prob_tx[1]);
#if CONFIG_TX32X32
if (m->mbmi.sb_type && sz != TX_8X8)
vp9_write(bc, sz != TX_16X16, c->prob_tx[2]);
#endif
}
}
}
static void write_modes_b(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
TOKENEXTRA **tok, TOKENEXTRA *tok_end,
int mb_row, int mb_col) {
VP9_COMMON *const c = &cpi->common;
MACROBLOCKD *const xd = &cpi->mb.e_mbd;
xd->mode_info_context = m;
if (c->frame_type == KEY_FRAME) {
write_mb_modes_kf(cpi, m, bc,
c->mb_rows - mb_row, c->mb_cols - mb_col);
#ifdef ENTROPY_STATS
active_section = 8;
#endif
} else {
pack_inter_mode_mvs(cpi, m, bc,
c->mb_rows - mb_row, c->mb_cols - mb_col);
#ifdef ENTROPY_STATS
active_section = 1;
#endif
}
assert(*tok < tok_end);
pack_mb_tokens(bc, tok, tok_end);
}
static void write_modes(VP9_COMP *cpi, vp9_writer* const bc) {
VP9_COMMON *const c = &cpi->common;
const int mis = c->mode_info_stride;
MODE_INFO *m, *m_ptr = c->mi;
int i, mb_row, mb_col;
TOKENEXTRA *tok = cpi->tok;
TOKENEXTRA *tok_end = tok + cpi->tok_count;
1121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
for (mb_row = 0; mb_row < c->mb_rows; mb_row += 4, m_ptr += 4 * mis) {
m = m_ptr;
for (mb_col = 0; mb_col < c->mb_cols; mb_col += 4, m += 4) {
#if CONFIG_SUPERBLOCKS64
vp9_write(bc, m->mbmi.sb_type == BLOCK_SIZE_SB64X64, c->sb64_coded);
if (m->mbmi.sb_type == BLOCK_SIZE_SB64X64) {
write_modes_b(cpi, m, bc, &tok, tok_end, mb_row, mb_col);
} else
#endif
{
int j;
for (j = 0; j < 4; j++) {
const int x_idx_sb = (j & 1) << 1, y_idx_sb = j & 2;
MODE_INFO *sb_m = m + y_idx_sb * mis + x_idx_sb;
if (mb_col + x_idx_sb >= c->mb_cols ||
mb_row + y_idx_sb >= c->mb_rows)
continue;
vp9_write(bc, sb_m->mbmi.sb_type, c->sb32_coded);
if (sb_m->mbmi.sb_type) {
assert(sb_m->mbmi.sb_type == BLOCK_SIZE_SB32X32);
write_modes_b(cpi, sb_m, bc, &tok, tok_end,
mb_row + y_idx_sb, mb_col + x_idx_sb);
} else {
// Process the 4 MBs in the order:
// top-left, top-right, bottom-left, bottom-right
for (i = 0; i < 4; i++) {
const int x_idx = x_idx_sb + (i & 1), y_idx = y_idx_sb + (i >> 1);
MODE_INFO *mb_m = m + x_idx + y_idx * mis;
if (mb_row + y_idx >= c->mb_rows ||
mb_col + x_idx >= c->mb_cols) {
// MB lies outside frame, move on
continue;
}
assert(mb_m->mbmi.sb_type == BLOCK_SIZE_MB16X16);
write_modes_b(cpi, mb_m, bc, &tok, tok_end,
mb_row + y_idx, mb_col + x_idx);
}
}
}
}
}
}
}
/* This function is used for debugging probability trees. */
static void print_prob_tree(vp9_coeff_probs *coef_probs) {
/* print coef probability tree */
int i, j, k, l;
FILE *f = fopen("enc_tree_probs.txt", "a");
fprintf(f, "{\n");
for (i = 0; i < BLOCK_TYPES_4X4; i++) {
fprintf(f, " {\n");
for (j = 0; j < COEF_BANDS; j++) {
fprintf(f, " {\n");
for (k = 0; k < PREV_COEF_CONTEXTS; k++) {
fprintf(f, " {");
for (l = 0; l < ENTROPY_NODES; l++) {
fprintf(f, "%3u, ",
(unsigned int)(coef_probs [i][j][k][l]));
}
fprintf(f, " }\n");
}
fprintf(f, " }\n");
1191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
}
fprintf(f, " }\n");
}
fprintf(f, "}\n");
fclose(f);
}
static void build_tree_distribution(vp9_coeff_probs *coef_probs,
vp9_coeff_count *coef_counts,
#ifdef ENTROPY_STATS
VP9_COMP *cpi,
vp9_coeff_accum *context_counters,
#endif
vp9_coeff_stats *coef_branch_ct,
int block_types) {
int i = 0, j, k;
#ifdef ENTROPY_STATS
int t = 0;
#endif
for (i = 0; i < block_types; ++i) {
for (j = 0; j < COEF_BANDS; ++j) {
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
continue;
vp9_tree_probs_from_distribution(MAX_ENTROPY_TOKENS,
vp9_coef_encodings, vp9_coef_tree,
coef_probs[i][j][k],
coef_branch_ct[i][j][k],
coef_counts[i][j][k]);
#ifdef ENTROPY_STATS
if (!cpi->dummy_packing)
for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
context_counters[i][j][k][t] += coef_counts[i][j][k][t];
#endif
}
}
}
}
static void build_coeff_contexts(VP9_COMP *cpi) {
build_tree_distribution(cpi->frame_coef_probs_4x4,
cpi->coef_counts_4x4,
#ifdef ENTROPY_STATS
cpi, context_counters_4x4,
#endif
cpi->frame_branch_ct_4x4, BLOCK_TYPES_4X4);
build_tree_distribution(cpi->frame_hybrid_coef_probs_4x4,
cpi->hybrid_coef_counts_4x4,
#ifdef ENTROPY_STATS
cpi, hybrid_context_counters_4x4,
#endif
cpi->frame_hybrid_branch_ct_4x4, BLOCK_TYPES_4X4);
build_tree_distribution(cpi->frame_coef_probs_8x8,
cpi->coef_counts_8x8,
#ifdef ENTROPY_STATS
cpi, context_counters_8x8,
#endif
cpi->frame_branch_ct_8x8, BLOCK_TYPES_8X8);
build_tree_distribution(cpi->frame_hybrid_coef_probs_8x8,
cpi->hybrid_coef_counts_8x8,
#ifdef ENTROPY_STATS
cpi, hybrid_context_counters_8x8,
#endif
cpi->frame_hybrid_branch_ct_8x8, BLOCK_TYPES_8X8);
build_tree_distribution(cpi->frame_coef_probs_16x16,
cpi->coef_counts_16x16,
#ifdef ENTROPY_STATS
cpi, context_counters_16x16,
#endif
1261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
cpi->frame_branch_ct_16x16, BLOCK_TYPES_16X16);
build_tree_distribution(cpi->frame_hybrid_coef_probs_16x16,
cpi->hybrid_coef_counts_16x16,
#ifdef ENTROPY_STATS
cpi, hybrid_context_counters_16x16,
#endif
cpi->frame_hybrid_branch_ct_16x16, BLOCK_TYPES_16X16);
#if CONFIG_TX32X32
build_tree_distribution(cpi->frame_coef_probs_32x32,
cpi->coef_counts_32x32,
#ifdef ENTROPY_STATS
cpi, context_counters_32x32,
#endif
cpi->frame_branch_ct_32x32, BLOCK_TYPES_32X32);
#endif
}
static void update_coef_probs_common(vp9_writer* const bc,
#ifdef ENTROPY_STATS
VP9_COMP *cpi,
vp9_coeff_stats *tree_update_hist,
#endif
vp9_coeff_probs *new_frame_coef_probs,
vp9_coeff_probs *old_frame_coef_probs,
vp9_coeff_stats *frame_branch_ct,
int block_types) {
int i, j, k, t;
int update[2] = {0, 0};
int savings;
// vp9_prob bestupd = find_coef_update_prob(cpi);
/* dry run to see if there is any udpate at all needed */
savings = 0;
for (i = 0; i < block_types; ++i) {
for (j = !i; j < COEF_BANDS; ++j) {
int prev_coef_savings[ENTROPY_NODES] = {0};
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
for (t = 0; t < ENTROPY_NODES; ++t) {
vp9_prob newp = new_frame_coef_probs[i][j][k][t];
const vp9_prob oldp = old_frame_coef_probs[i][j][k][t];
const vp9_prob upd = COEF_UPDATE_PROB;
int s = prev_coef_savings[t];
int u = 0;
if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
continue;
#if defined(SEARCH_NEWP)
s = prob_diff_update_savings_search(
frame_branch_ct[i][j][k][t],
oldp, &newp, upd);
if (s > 0 && newp != oldp)
u = 1;
if (u)
savings += s - (int)(vp9_cost_zero(upd));
else
savings -= (int)(vp9_cost_zero(upd));
#else
s = prob_update_savings(
frame_branch_ct[i][j][k][t],
oldp, newp, upd);
if (s > 0)
u = 1;
if (u)
savings += s;
#endif
update[u]++;
}
}
}
}
1331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
// printf("Update %d %d, savings %d\n", update[0], update[1], savings);
/* Is coef updated at all */
if (update[1] == 0 || savings < 0) {
vp9_write_bit(bc, 0);
} else {
vp9_write_bit(bc, 1);
for (i = 0; i < block_types; ++i) {
for (j = !i; j < COEF_BANDS; ++j) {
int prev_coef_savings[ENTROPY_NODES] = {0};
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
// calc probs and branch cts for this frame only
for (t = 0; t < ENTROPY_NODES; ++t) {
vp9_prob newp = new_frame_coef_probs[i][j][k][t];
vp9_prob *oldp = old_frame_coef_probs[i][j][k] + t;
const vp9_prob upd = COEF_UPDATE_PROB;
int s = prev_coef_savings[t];
int u = 0;
if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
continue;
#if defined(SEARCH_NEWP)
s = prob_diff_update_savings_search(
frame_branch_ct[i][j][k][t],
*oldp, &newp, upd);
if (s > 0 && newp != *oldp)
u = 1;
#else
s = prob_update_savings(
frame_branch_ct[i][j][k][t],
*oldp, newp, upd);
if (s > 0)
u = 1;
#endif
vp9_write(bc, u, upd);
#ifdef ENTROPY_STATS
if (!cpi->dummy_packing)
++tree_update_hist[i][j][k][t][u];
#endif
if (u) {
/* send/use new probability */
write_prob_diff_update(bc, newp, *oldp);
*oldp = newp;
}
}
}
}
}
}
}
static void update_coef_probs(VP9_COMP* const cpi, vp9_writer* const bc) {
vp9_clear_system_state();
// Build the cofficient contexts based on counts collected in encode loop
build_coeff_contexts(cpi);
update_coef_probs_common(bc,
#ifdef ENTROPY_STATS
cpi,
tree_update_hist_4x4,
#endif
cpi->frame_coef_probs_4x4,
cpi->common.fc.coef_probs_4x4,
cpi->frame_branch_ct_4x4,
BLOCK_TYPES_4X4);
update_coef_probs_common(bc,
#ifdef ENTROPY_STATS
cpi,
1401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
hybrid_tree_update_hist_4x4,
#endif
cpi->frame_hybrid_coef_probs_4x4,
cpi->common.fc.hybrid_coef_probs_4x4,
cpi->frame_hybrid_branch_ct_4x4,
BLOCK_TYPES_4X4);
/* do not do this if not even allowed */
if (cpi->common.txfm_mode != ONLY_4X4) {
update_coef_probs_common(bc,
#ifdef ENTROPY_STATS
cpi,
tree_update_hist_8x8,
#endif
cpi->frame_coef_probs_8x8,
cpi->common.fc.coef_probs_8x8,
cpi->frame_branch_ct_8x8,
BLOCK_TYPES_8X8);
update_coef_probs_common(bc,
#ifdef ENTROPY_STATS
cpi,
hybrid_tree_update_hist_8x8,
#endif
cpi->frame_hybrid_coef_probs_8x8,
cpi->common.fc.hybrid_coef_probs_8x8,
cpi->frame_hybrid_branch_ct_8x8,
BLOCK_TYPES_8X8);
}
if (cpi->common.txfm_mode > ALLOW_8X8) {
update_coef_probs_common(bc,
#ifdef ENTROPY_STATS
cpi,
tree_update_hist_16x16,
#endif
cpi->frame_coef_probs_16x16,
cpi->common.fc.coef_probs_16x16,
cpi->frame_branch_ct_16x16,
BLOCK_TYPES_16X16);
update_coef_probs_common(bc,
#ifdef ENTROPY_STATS
cpi,
hybrid_tree_update_hist_16x16,
#endif
cpi->frame_hybrid_coef_probs_16x16,
cpi->common.fc.hybrid_coef_probs_16x16,
cpi->frame_hybrid_branch_ct_16x16,
BLOCK_TYPES_16X16);
}
#if CONFIG_TX32X32
if (cpi->common.txfm_mode > ALLOW_16X16) {
update_coef_probs_common(bc,
#ifdef ENTROPY_STATS
cpi,
tree_update_hist_32x32,
#endif
cpi->frame_coef_probs_32x32,
cpi->common.fc.coef_probs_32x32,
cpi->frame_branch_ct_32x32,
BLOCK_TYPES_32X32);
}
#endif
}
#ifdef PACKET_TESTING
FILE *vpxlogc = 0;
#endif
1471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
static void put_delta_q(vp9_writer *bc, int delta_q) {
if (delta_q != 0) {
vp9_write_bit(bc, 1);
vp9_write_literal(bc, abs(delta_q), 4);
if (delta_q < 0)
vp9_write_bit(bc, 1);
else
vp9_write_bit(bc, 0);
} else
vp9_write_bit(bc, 0);
}
static void decide_kf_ymode_entropy(VP9_COMP *cpi) {
int mode_cost[MB_MODE_COUNT];
int cost;
int bestcost = INT_MAX;
int bestindex = 0;
int i, j;
for (i = 0; i < 8; i++) {
vp9_cost_tokens(mode_cost, cpi->common.kf_ymode_prob[i], vp9_kf_ymode_tree);
cost = 0;
for (j = 0; j < VP9_YMODES; j++) {
cost += mode_cost[j] * cpi->ymode_count[j];
}
vp9_cost_tokens(mode_cost, cpi->common.sb_kf_ymode_prob[i],
vp9_sb_ymode_tree);
for (j = 0; j < VP9_I32X32_MODES; j++) {
cost += mode_cost[j] * cpi->sb_ymode_count[j];
}
if (cost < bestcost) {
bestindex = i;
bestcost = cost;
}
}
cpi->common.kf_ymode_probs_index = bestindex;
}
static void segment_reference_frames(VP9_COMP *cpi) {
VP9_COMMON *oci = &cpi->common;
MODE_INFO *mi = oci->mi;
int ref[MAX_MB_SEGMENTS] = {0};
int i, j;
int mb_index = 0;
MACROBLOCKD *const xd = &cpi->mb.e_mbd;
for (i = 0; i < oci->mb_rows; i++) {
for (j = 0; j < oci->mb_cols; j++, mb_index++) {
ref[mi[mb_index].mbmi.segment_id] |= (1 << mi[mb_index].mbmi.ref_frame);
}
mb_index++;
}
for (i = 0; i < MAX_MB_SEGMENTS; i++) {
vp9_enable_segfeature(xd, i, SEG_LVL_REF_FRAME);
vp9_set_segdata(xd, i, SEG_LVL_REF_FRAME, ref[i]);
}
}
void vp9_pack_bitstream(VP9_COMP *cpi, unsigned char *dest,
unsigned long *size) {
int i, j;
VP9_HEADER oh;
VP9_COMMON *const pc = &cpi->common;
vp9_writer header_bc, residual_bc;
MACROBLOCKD *const xd = &cpi->mb.e_mbd;
int extra_bytes_packed = 0;
unsigned char *cx_data = dest;
1541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
oh.show_frame = (int) pc->show_frame;
oh.type = (int)pc->frame_type;
oh.version = pc->version;
oh.first_partition_length_in_bytes = 0;
cx_data += 3;
#if defined(SECTIONBITS_OUTPUT)
Sectionbits[active_section = 1] += sizeof(VP9_HEADER) * 8 * 256;
#endif
compute_update_table();
/* vp9_kf_default_bmode_probs() is called in vp9_setup_key_frame() once
* for each K frame before encode frame. pc->kf_bmode_prob doesn't get
* changed anywhere else. No need to call it again here. --yw
* vp9_kf_default_bmode_probs( pc->kf_bmode_prob);
*/
/* every keyframe send startcode, width, height, scale factor, clamp
* and color type.
*/
if (oh.type == KEY_FRAME) {
int v;
// Start / synch code
cx_data[0] = 0x9D;
cx_data[1] = 0x01;
cx_data[2] = 0x2a;
v = (pc->horiz_scale << 14) | pc->Width;
cx_data[3] = v;
cx_data[4] = v >> 8;
v = (pc->vert_scale << 14) | pc->Height;
cx_data[5] = v;
cx_data[6] = v >> 8;
extra_bytes_packed = 7;
cx_data += extra_bytes_packed;
vp9_start_encode(&header_bc, cx_data);
// signal clr type
vp9_write_bit(&header_bc, pc->clr_type);
vp9_write_bit(&header_bc, pc->clamp_type);
} else {
vp9_start_encode(&header_bc, cx_data);
}
// Signal whether or not Segmentation is enabled
vp9_write_bit(&header_bc, (xd->segmentation_enabled) ? 1 : 0);
// Indicate which features are enabled
if (xd->segmentation_enabled) {
// Indicate whether or not the segmentation map is being updated.
vp9_write_bit(&header_bc, (xd->update_mb_segmentation_map) ? 1 : 0);
// If it is, then indicate the method that will be used.
if (xd->update_mb_segmentation_map) {
// Select the coding strategy (temporal or spatial)
vp9_choose_segmap_coding_method(cpi);
// Send the tree probabilities used to decode unpredicted
// macro-block segments
for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) {
int data = xd->mb_segment_tree_probs[i];
if (data != 255) {
1611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
vp9_write_bit(&header_bc, 1);
vp9_write_literal(&header_bc, data, 8);
} else {
vp9_write_bit(&header_bc, 0);
}
}
// Write out the chosen coding method.
vp9_write_bit(&header_bc, (pc->temporal_update) ? 1 : 0);
if (pc->temporal_update) {
for (i = 0; i < PREDICTION_PROBS; i++) {
int data = pc->segment_pred_probs[i];
if (data != 255) {
vp9_write_bit(&header_bc, 1);
vp9_write_literal(&header_bc, data, 8);
} else {
vp9_write_bit(&header_bc, 0);
}
}
}
}
vp9_write_bit(&header_bc, (xd->update_mb_segmentation_data) ? 1 : 0);
// segment_reference_frames(cpi);
if (xd->update_mb_segmentation_data) {
signed char Data;
vp9_write_bit(&header_bc, (xd->mb_segment_abs_delta) ? 1 : 0);
// For each segments id...
for (i = 0; i < MAX_MB_SEGMENTS; i++) {
// For each segmentation codable feature...
for (j = 0; j < SEG_LVL_MAX; j++) {
Data = vp9_get_segdata(xd, i, j);
// If the feature is enabled...
if (vp9_segfeature_active(xd, i, j)) {
vp9_write_bit(&header_bc, 1);
// Is the segment data signed..
if (vp9_is_segfeature_signed(j)) {
// Encode the relevant feature data
if (Data < 0) {
Data = - Data;
vp9_encode_unsigned_max(&header_bc, Data,
vp9_seg_feature_data_max(j));
vp9_write_bit(&header_bc, 1);
} else {
vp9_encode_unsigned_max(&header_bc, Data,
vp9_seg_feature_data_max(j));
vp9_write_bit(&header_bc, 0);
}
}
// Unsigned data element so no sign bit needed
else
vp9_encode_unsigned_max(&header_bc, Data,
vp9_seg_feature_data_max(j));
} else
vp9_write_bit(&header_bc, 0);
}
}
}
}
// Encode the common prediction model status flag probability updates for
// the reference frame
update_refpred_stats(cpi);