slice.c
来自「the newest JM software by h.264 JVT offi」· C语言 代码 · 共 1,870 行 · 第 1/4 页
C
1,870 行
/*!
**************************************************************************************
* \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 "biariencode.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->frame_no); // 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 (currSlice);
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 buffer_size = currSlice->partArr[0].bitstream->buffer_size;
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 (currSlice, 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;
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->frame_no, 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();
}
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 (img, currSlice, &currMB, CurrentMbAddr, FALSE);
if(params->UseRDOQuant)
{
trellis_coding(currSlice, currMB, CurrentMbAddr, prev_recode_mb);
}
else
{
img->masterQP = img->qp;
encode_one_macroblock (currSlice, currMB);
write_macroblock (img, currSlice, currMB, 1, prev_recode_mb);
}
end_macroblock (img, 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
next_macroblock (img, 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);
}
}
}
if ((params->WPIterMC) && (img->frameOffsetAvail == 0) && img->nal_reference_idc)
{
compute_offset();
}
terminate_slice (currSlice, (NumberOfCodedMBs + TotalCodedMBs >= (int)img->PicSizeInMbs), cur_stats );
return NumberOfCodedMBs;
}
/*!
************************************************************************
* \brief
* Encodes one slice (MBAFF Frame)
* \par
* returns the number of coded MBs in the SLice
************************************************************************
*/
int encode_one_slice_MBAFF (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->frame_no, 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
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?