📄 slice.c
字号:
/*!
**************************************************************************************
* \file
* slice.c
* \brief
* generate the slice header, setup the bit buffer for slices,
* and generates the slice NALU(s)
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Thomas Stockhammer <stockhammer@ei.tum.de>
* - Detlev Marpe <marpe@hhi.de>
* - Stephan Wenger <stewe@cs.tu-berlin.de>
* - Alexis Michael Tourapis <alexismt@ieee.org>
***************************************************************************************
*/
#include "contributors.h"
#include <math.h>
#include <float.h>
#include "global.h"
#include "header.h"
#include "nal.h"
#include "rtp.h"
#include "fmo.h"
#include "vlc.h"
#include "image.h"
#include "cabac.h"
#include "elements.h"
#include "macroblock.h"
#include "symbol.h"
#include "context_ini.h"
#include "enc_statistics.h"
#include "ratectl.h"
#include "me_epzs.h"
#include "wp.h"
#include "slice.h"
#include "rdoq.h"
#include "wp_mcprec.h"
#include "q_offsets.h"
#include "conformance.h"
#include "quant4x4.h"
#include "quant8x8.h"
#include "quantChroma.h"
// Local declarations
static Slice *malloc_slice();
static void free_slice(Slice *slice);
static void set_ref_pic_num();
extern ColocatedParams *Co_located;
extern StorablePicture **listX[6];
//! convert from H.263 QP to H.264 quant given by: quant=pow(2,QP/6)
const int QP2QUANT[40]=
{
1, 1, 1, 1, 2, 2, 2, 2,
3, 3, 3, 4, 4, 4, 5, 6,
6, 7, 8, 9,10,11,13,14,
16,18,20,23,25,29,32,36,
40,45,51,57,64,72,81,91
};
/*!
************************************************************************
* \brief
* init_ref_pic_list_reordering initializations should go here
************************************************************************
*/
void init_ref_pic_list_reordering(Slice* currSlice)
{
currSlice->ref_pic_list_reordering_flag_l0 = 0;
currSlice->ref_pic_list_reordering_flag_l1 = 0;
}
/*!
************************************************************************
* \brief
* This function generates the slice (and partition) header(s)
*
* \return number of bits used for the slice (and partition) header(s)
*
* \par Side effects:
* Adds slice/partition header symbols to the symbol buffer
* increments Picture->no_slices, allocates memory for the
* slice, sets img->currSlice
************************************************************************
*/
static int start_slice(Slice *currSlice, StatParameters *cur_stats)
{
EncodingEnvironmentPtr eep;
Bitstream *currStream;
int header_len = 0;
int i;
int NumberOfPartitions = (params->partition_mode == PAR_DP_1?1:3);
//one partition for IDR img
if(img->currentPicture->idr_flag)
{
NumberOfPartitions = 1;
}
RTPUpdateTimestamp (img->tr); // this has no side effects, just leave it for all NALs
for (i=0; i<NumberOfPartitions; i++)
{
currStream = (currSlice->partArr[i]).bitstream;
currStream->write_flag = 0;
if (i==0) // First partition
header_len += SliceHeader ();
else // Second/Third partition
header_len += Partition_BC_Header(i);
//! Initialize CABAC
if (currSlice->symbol_mode == CABAC)
{
eep = &((currSlice->partArr[i]).ee_cabac);
if (currStream->bits_to_go != 8)
header_len+=currStream->bits_to_go;
writeVlcByteAlign(currStream, cur_stats);
arienco_start_encoding(eep, currStream->streamBuffer, &(currStream->byte_pos));
arienco_reset_EC(eep);
}
else
{
// Initialize CA-VLC
CAVLC_init();
}
}
if(currSlice->symbol_mode == CABAC)
{
init_contexts();
}
return header_len;
}
/*!
************************************************************************
* \brief
* This creates a NAL unit structures for all data partition of the slice
*
************************************************************************
*/
void create_slice_nalus(Slice *currSlice)
{
// KS: this is approx. max. allowed code picture size
const int buffer_size = 500 + img->FrameSizeInMbs * (128 + 256 * img->bitdepth_luma + 512 * img->bitdepth_chroma);
int part;
NALU_t *nalu;
for (part=0; part< currSlice->max_part_nr; part++)
{
if (currSlice->partArr[part].bitstream->write_flag)
{
nalu = AllocNALU(buffer_size);
currSlice->partArr[part].nal_unit = nalu;
nalu->startcodeprefix_len = 1+ (currSlice->start_mb_nr == 0 && part == 0 ?ZEROBYTES_SHORTSTARTCODE+1:ZEROBYTES_SHORTSTARTCODE);
nalu->forbidden_bit = 0;
if (img->currentPicture->idr_flag)
{
nalu->nal_unit_type = NALU_TYPE_IDR;
nalu->nal_reference_idc = NALU_PRIORITY_HIGHEST;
}
else
{
//different nal header for different partitions
if(params->partition_mode == 0)
{
nalu->nal_unit_type = NALU_TYPE_SLICE;
}
else
{
nalu->nal_unit_type = (NaluType) (NALU_TYPE_DPA + part);
}
if (img->nal_reference_idc !=0)
{
nalu->nal_reference_idc = NALU_PRIORITY_HIGH;
}
else
{
nalu->nal_reference_idc = NALU_PRIORITY_DISPOSABLE;
}
}
}
else
{
currSlice->partArr[part].nal_unit = NULL;
}
}
}
/*!
************************************************************************
* \brief
* This function terminates a slice (but doesn't write it out),
* the old terminate_slice (0)
* \return
* 0 if OK, \n
* 1 in case of error
*
************************************************************************
*/
static int terminate_slice(Slice *currSlice, int lastslice, StatParameters *cur_stats )
{
Bitstream *currStream;
NALU_t *currNalu;
EncodingEnvironmentPtr eep;
int part;
int tmp_stuffingbits = img->mb_data[img->current_mb_nr].bitcounter[BITS_STUFFING];
if (currSlice->symbol_mode == CABAC)
write_terminating_bit (1); // only once, not for all partitions
create_slice_nalus(currSlice);
for (part=0; part<currSlice->max_part_nr; part++)
{
currStream = (currSlice->partArr[part]).bitstream;
currNalu = (currSlice->partArr[part]).nal_unit;
if (currStream->write_flag)
{
if (currSlice->symbol_mode == CAVLC)
{
SODBtoRBSP(currStream);
currNalu->len = RBSPtoEBSP(currNalu->buf, currStream->streamBuffer, currStream->byte_pos);
}
else // CABAC
{
eep = &((currSlice->partArr[part]).ee_cabac);
// terminate the arithmetic code
arienco_done_encoding(eep);
set_pic_bin_count(eep);
currStream->bits_to_go = eep->Ebits_to_go;
currStream->byte_buf = 0;
currNalu->len = RBSPtoEBSP(currNalu->buf, currStream->streamBuffer, currStream->byte_pos);
// NumBytesInNALunit is: payload length + 1 byte header
img->bytes_in_picture += currNalu->len + 1;
if (lastslice && (part==(currSlice->max_part_nr-1)))
{
addCabacZeroWords(currNalu, cur_stats);
}
} // CABAC
}
} // partition loop
if( currSlice->symbol_mode == CABAC )
{
store_contexts();
}
cur_stats->bit_use_stuffingBits[img->type] += img->mb_data[img->current_mb_nr].bitcounter[BITS_STUFFING] - tmp_stuffingbits;
if (img->type != I_SLICE && img->type != SI_SLICE)
free_ref_pic_list_reordering_buffer (currSlice);
return 0;
}
/*!
************************************************************************
* \brief
* Encodes one slice
* \par
* returns the number of coded MBs in the SLice
************************************************************************
*/
int encode_one_slice (int SliceGroupId, Picture *pic, int TotalCodedMBs)
{
Boolean end_of_slice = FALSE;
Boolean recode_macroblock;
Boolean prev_recode_mb = FALSE;
int len;
int NumberOfCodedMBs = 0;
Macroblock* currMB = NULL;
int CurrentMbAddr;
double FrameRDCost = DBL_MAX, FieldRDCost = DBL_MAX;
StatParameters *cur_stats = &enc_picture->stats;
Slice *currSlice = NULL;
Motion_Selected = 0;
if( IS_INDEPENDENT(params) )
{
change_plane_JV( img->colour_plane_id );
}
img->cod_counter = 0;
CurrentMbAddr = FmoGetFirstMacroblockInSlice (SliceGroupId);
// printf ("\n\nEncode_one_slice: PictureID %d SliceGroupId %d SliceID %d FirstMB %d \n", img->tr, SliceGroupId, img->current_slice_nr, CurrentMbInScanOrder);
init_slice (&currSlice, CurrentMbAddr);
// Initialize quantization functions based on rounding/quantization method
// Done here since we may wish to disable adaptive rounding on occasional intervals (even at a frame or gop level).
init_quant_4x4(params, img, currSlice);
init_quant_8x8(params, img, currSlice);
init_quant_Chroma(params, img, currSlice);
SetLagrangianMultipliers();
if (currSlice->symbol_mode == CABAC)
{
SetCtxModelNumber ();
}
img->checkref = (params->rdopt && params->RestrictRef && (img->type==P_SLICE || img->type==SP_SLICE));
len = start_slice (currSlice, cur_stats);
// Rate control
if (params->RCEnable)
rc_store_slice_header_bits( len );
// Update statistics
stats->bit_slice += len;
cur_stats->bit_use_header[img->type] += len;
while (end_of_slice == FALSE) // loop over macroblocks
{
if (img->AdaptiveRounding && params->AdaptRndPeriod && (img->current_mb_nr % params->AdaptRndPeriod == 0))
{
CalculateOffsetParam();
if(params->Transform8x8Mode)
{
CalculateOffset8Param();
}
}
//sw paff
if (!img->MbaffFrameFlag)
{
recode_macroblock = FALSE;
if(params->UseRDOQuant) // This needs revisit
rdopt = &rddata_trellis_curr;
else
rdopt = &rddata_top_frame_mb; // store data in top frame MB
start_macroblock (currSlice, &currMB, CurrentMbAddr, FALSE);
if(params->UseRDOQuant)
{
trellis_coding(currMB, CurrentMbAddr, prev_recode_mb);
}
else
{
img->masterQP = img->qp;
encode_one_macroblock (currMB);
write_one_macroblock (currMB, 1, prev_recode_mb);
}
terminate_macroblock (currSlice, currMB, &end_of_slice, &recode_macroblock);
prev_recode_mb = recode_macroblock;
// printf ("encode_one_slice: mb %d, slice %d, bitbuf bytepos %d EOS %d\n",
// img->current_mb_nr, img->current_slice_nr,
// currSlice->partArr[0].bitstream->byte_pos, end_of_slice);
if (recode_macroblock == FALSE) // The final processing of the macroblock has been done
{
img->SumFrameQP += currMB->qp;
CurrentMbAddr = FmoGetNextMBNr (CurrentMbAddr);
if (CurrentMbAddr == -1) // end of slice
{
// printf ("FMO End of Slice Group detected, current MBs %d, force end of slice\n", NumberOfCodedMBs+1);
end_of_slice = TRUE;
}
NumberOfCodedMBs++; // only here we are sure that the coded MB is actually included in the slice
proceed2nextMacroblock (currMB);
}
else
{
//!Go back to the previous MB to recode it
img->current_mb_nr = FmoGetPreviousMBNr(img->current_mb_nr);
img->NumberofCodedMacroBlocks--;
if(img->current_mb_nr == -1 ) // The first MB of the slice group is too big,
// which means it's impossible to encode picture using current slice bits restriction
{
snprintf (errortext, ET_SIZE, "Error encoding first MB with specified parameter, bits of current MB may be too big");
error (errortext, 300);
}
}
}
else // TBD -- Addition of FMO
{
//! This following ugly code breaks slices, at least for a slice mode that accumulates a certain
//! number of bits into one slice.
//! The suggested algorithm is as follows:
//!
//! SaveState (Bitstream, stats, etc. etc.);
//! BitsForThisMBPairInFrameMode = CodeMB (Upper, FRAME_MODE) + CodeMB (Lower, FRAME_MODE);
//! DistortionForThisMBPairInFrameMode = CalculateDistortion(Upper) + CalculateDistortion (Lower);
//! RestoreState();
//! BitsForThisMBPairInFieldMode = CodeMB (Upper, FIELD_MODE) + CodeMB (Lower, FIELD_MODE);
//! DistortionForThisMBPairInFrameMode = CalculateDistortion(Upper) + CalculateDistortion (Lower);
//! FrameFieldMode = Decision (...)
//! RestoreState()
//! if (FrameFieldMode == FRAME) {
//! CodeMB (Upper, FRAME); CodeMB (Lower, FRAME);
//! } else {
//! CodeMB (Upper FIELD); CodeMB (Lower, FIELD);
//! }
//!
//! Open questions/issues:
//! 1. CABAC/CA-VLC state: It seems that the CABAC/CA_VLC states are changed during the
//! dummy encoding processes (for the R-D based selection), but that they are never
//! reset, once the selection is made. I believe that this breaks the MB-adaptive
//! frame/field coding. The necessary code for the state saves is readily available
//! in macroblock.c, start_macroblock() and terminate_macroblock() (this code needs
//! to be double checked that it works with CA-VLC as well
//! 2. would it be an option to allocate Bitstreams with zero data in them (or copy the
//! already generated bitstream) for the "test coding"?
img->write_macroblock = FALSE;
if (params->MbInterlace == ADAPTIVE_CODING || params->MbInterlace == FRAME_MB_PAIR_CODING)
{
//================ code MB pair as frame MB ================
//----------------------------------------------------------
recode_macroblock = FALSE;
//set mv limits to frame type
update_mv_limits(img, FALSE);
img->field_mode = FALSE; // MB coded as frame
img->top_field = FALSE; // Set top field to 0
//Rate control
img->write_macroblock = FALSE;
img->bot_MB = FALSE;
// save RC state only when it is going to change
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -