This source file includes following definitions.
- new_option
- start
- compute_rdo_costs
- find_best_rdo_index
- return_best_rdo_node
- begin
- end
#include "libde265/encoder/algo/coding-options.h"
#include "libde265/encoder/encoder-context.h"
template <class node>
CodingOptions<node>::CodingOptions(encoder_context* ectx, node* _node, context_model_table& tab)
{
mInputNode = _node;
mContextModelInput = &tab;
mBestRDO=-1;
mECtx = ectx;
}
template <class node>
CodingOptions<node>::~CodingOptions()
{
}
template <class node>
CodingOption<node> CodingOptions<node>::new_option(bool active)
{
if (!active) {
return CodingOption<node>();
}
CodingOptionData opt;
bool firstOption = mOptions.empty();
if (firstOption) {
opt.mNode = mInputNode;
}
else {
opt.mNode = new node(*mInputNode);
}
opt.context = *mContextModelInput;
opt.computed = false;
CodingOption<node> option(this, mOptions.size());
mOptions.push_back( std::move(opt) );
return option;
}
template <class node>
void CodingOptions<node>::start(enum RateEstimationMethod rateMethod)
{
mContextModelInput->release();
bool adaptiveContext;
switch (rateMethod) {
case Rate_Default:
adaptiveContext = mECtx->use_adaptive_context;
break;
case Rate_FixedContext:
adaptiveContext = false;
break;
case Rate_AdaptiveContext:
adaptiveContext = true;
break;
}
if (adaptiveContext) {
for (auto& option : mOptions) {
option.context.decouple();
}
cabac = &cabac_adaptive;
}
else {
cabac = &cabac_constant;
}
}
template <class node>
void CodingOptions<node>::compute_rdo_costs()
{
for (int i=0;i<mOptions.size();i++) {
if (mOptions[i].computed) {
mOptions[i].rdoCost = mOptions[i].mNode->distortion + mECtx->lambda * mOptions[i].mNode->rate;
}
}
}
template <class node>
int CodingOptions<node>::find_best_rdo_index()
{
assert(mOptions.size()>0);
float bestRDOCost = 0;
bool first=true;
int bestRDO=-1;
for (int i=0;i<mOptions.size();i++) {
if (mOptions[i].computed) {
float cost = mOptions[i].rdoCost;
if (first || cost < bestRDOCost) {
bestRDOCost = cost;
first = false;
bestRDO = i;
}
}
}
return bestRDO;
}
template <class node>
node* CodingOptions<node>::return_best_rdo_node()
{
int bestRDO = find_best_rdo_index();
assert(bestRDO>=0);
*mContextModelInput = mOptions[bestRDO].context;
for (int i=0;i<mOptions.size();i++) {
if (i != bestRDO)
{
delete mOptions[i].mNode;
mOptions[i].mNode = NULL;
}
}
return mOptions[bestRDO].mNode;
}
template <class node>
void CodingOption<node>::begin()
{
assert(mParent);
assert(mParent->cabac);
mParent->cabac->reset();
mParent->cabac->set_context_models( &get_context() );
mParent->mOptions[mOptionIdx].computed = true;
node* n = get_node();
*(n->downPtr) = n;
}
template <class node>
void CodingOption<node>::end()
{
}
template class CodingOptions<enc_tb>;
template class CodingOptions<enc_cb>;
template class CodingOption<enc_tb>;
template class CodingOption<enc_cb>;