Presentation is loading. Please wait.

Presentation is loading. Please wait.

H.264 code tracing Group7 9417053 徐君潔.

Similar presentations


Presentation on theme: "H.264 code tracing Group7 9417053 徐君潔."— Presentation transcript:

1 H.264 code tracing Group 徐君潔

2 Trace 關係圖 encode_one_slice write_one_macroblock terminate_macroblock
proceed2nextMacroblock terminate_slice writeMBLayer writeMB_typeInfo writeMB_skip_flagInfo_CABAC writeB8_typeInfo writeMotionInfo2NAL writeCBPandLumaCoeff

3 write_one_macroblock()
Target passes the chosen syntax elements to the NAL Input eos_bit (end of slice) /*0:write the Top MB data to the bitstream 1:write the Bottom MB data to the bitstream */ Output none In which file macroblock.c Caller encode_one_slice()

4 UseConstrainedIntraPred
Flow chart(1) Macroblock* currMB = &img->mb_data[img-current_mb_nr]; int* bitCount = currMB->bitcounter; img->SumFrameQP += currMB->qp; UseConstrainedIntraPred: 0: Inter MB are allowed for intra prediction 1: Not allowed #define IS_INTRA(MB) ((MB)->mb_type==I4MB || (MB)->mb_type==I16MB || (MB)->mb_type==I8MB || (MB)->mb_type==IPCM Initial Accumulate FrameQP UseConstrainedIntraPred & (P_SLICE || B_SLICE) yes intra_block[]= IS_INTRA()

5 write_terminating_bit()
Flow chart(2) Intras: record intra block numbers eos_bit = 1: write the bottom MB to bitstream write_terminating_bit () : write flag = 1 biari_encode_symbol_final(): Arithmetic encoding for last symbol before termination current_mb_nr = 0 yes Intras = 0 IS_INTRA() yes Intras++ CABAC&!first MB&eos_bit yes write_terminating_bit()

6 bitCount[BITS_TOTAL_MB]
Flow chart(3) cabac_encoding: use in Arithmatic coding writeMBLayer(0,&i):Codes macroblock header set_last_dquant(): Set global last_dquant according to macroblock delta qp Rate Control: img->NumberofMBHeaderBits img->NumberofMBTextureBits generic_RC->NumberofMBHeaderBits generic_RC->NumberofMBTextureBits if(img->BasicUnit < img->FrameSizeInMbs) generic_RC->NumberofBasicUnitHeaderBits generic_RC->NumberofBasicUnitTextureBits BasicUnit:picture, slice,macroblock raw… cabac_encoding = 1 writeMBLayer() set_last_dquant() Set total bit counter bitCount[BITS_TOTAL_MB] Rate Control RCEnable record total # of MBs cabac_encoding = 0

7 terminate_macroblock()
Target terminates processing of the current macroblock depending on the chosen slice mode Input end_of_slice /* returns true for last macroblock of a slice, otherwise false*/ recode_macroblock /* returns true if max. slice size is exceeded an macroblock must be recoded in next slice*/ Output none In which file macroblock.c Caller encode_one_slice()

8 SyntaxElement(SE) se.value1 = img->cod_counter; se.value2 = 0;
typedef struct syntaxelement { int type; //!< type of syntax element for data part. int value1; //!< numerical value of syntax element int value2; //!< for blocked symbols, e.g. run/level int len; //!< length of code int inf; //!< info part of UVLC code unsigned int bitpattern; //!< UVLC bitpattern int context; //!< CABAC context } SyntaxElement; Ex: SyntaxElement se; se.value1 = img->cod_counter; se.value2 = 0; se.type = SE_MBTYPE; writeSE_UVLC(&se, dataPart);

9 Flow chart(1) Slice *currSlice = img->currentSlice;
Macroblock *currMB = &img->mb_data[img->current_mb_nr]; int use_bitstream_backing = (input->slice_mode == FIXED_RATE || input->slice_mode == CALL_BACK); if ( (img->current_mb_nr==0) || (FmoGetPreviousMBNr(img->current_mb_nr)<0) ) else if( img->mb_data[FmoGetPreviousMBNr (img->current_mb_nr)].slice_nr != img->current_slice_nr ) Initial New slice yes new_slice = 1

10 Flow chart(2) slice_mode (1) NO_SLICES num_mb++ *recode_MB = FALSE
typedef enum { NO_SLICES, // 0=off FIXED_MB, // 1=fixed # mb in slice FIXED_RATE, // 2=fixed # bytes in slice CALL_BACK, // 3=use callback } SliceMode; slice_mode (1) NO_SLICES num_mb++ *recode_MB = FALSE num_mb++ *recode_MB = FALSE (2) max num of MBs|| end of slice group end of slice group|| max # of MBs in slice FIXED_MB yes yes *end_of_slice = TRUE *end_of_slice = TRUE

11 Flow chart(3) (3) FIXED_RATE cod_counter cod_counter : skipped MBs
currStream->bits_to_go currStream->byte_pos currStream->byte_buf yes (UVLC) write out cod_counter writeSE_UVLC() record rlc_bits save bitstream & restore bitstream slice_too_big(rlc_bits) || (!recode_MB&last MB) !new_slice& slice_too_big(rlc_bits) yes yes *end_of_slice =TRUE *recode_MB =TRUE

12 Flow chart(4) mb_nr>0& !new_slice& slice_too_big (4) CALL_BACK
yes *recode_MB =TRUE *end_of_slice =TRUE !recode_macroblock &end of slice default yes *end_of_slice =TRUE error msg

13 Flow chart(5) end_of_slice& *recode_macroblock cod_counter
yes yes restore bitstream writeSE_UVLC UVLC yes handle skip MBs writeSE_UVLC()

14 proceed2nextMacroblock()
Target updates StatParameters for the next macroblock Input none Output In which file macroblock.c Caller encode_one_slice()

15 StatParameters (4) (2) (3) (1) typedef struct {
int quant0; //!< quant for the first frame int quant1; //!< average quant for the remaining frames int b8_mode_0_use[NUM_PIC_TYPE][2]; int mode_use_transform_8x8[NUM_PIC_TYPE][MAXMODE]; int mode_use_transform_4x4[NUM_PIC_TYPE][MAXMODE]; int intra_chroma_mode[4]; int64 mode_use[NUM_PIC_TYPE][MAXMODE]; //!< Macroblock mode usage for Intra frames int64 bit_use_mode[NUM_PIC_TYPE][MAXMODE]; //!< statistics of bit usage int64 bit_use_mb_type[NUM_PIC_TYPE]; int64 tmp_bit_use_cbp[NUM_PIC_TYPE]; int64 bit_use_coeffY[NUM_PIC_TYPE]; int64 bit_use_coeffC[NUM_PIC_TYPE]; int64 bit_use_delta_quant[NUM_PIC_TYPE]; } StatParameters; StatParameters stats; (4) (2) (3) (1)

16 mode_use&b8_mode_0_use
Flow chart !I_SLICE&P8x8 bitCount > max_bitCount yes update mode_use&b8_mode_0_use mode_use_transform yes error msg mb_type&(cbp&15) update(1) bitcounter yes update mode_use_transform IS_INTRA update(3) P_SLICE || SP_SLICE yes update(2) mode yes update(4)

17 terminate_slice() Target terminates a slice (but doesn't write it out)
Input lastslice Output 0:if OK 1: in case of error In which file slice.c Caller encode_one_slice()

18 名詞解釋 SODP String of data bits (original data) RBSP
Raw byte sequence packet end with 1 and trailing 0 for alignment EBSP Encapsulate byte sequence payload in order to avoid start code conflict in NALU, add 0x03 after two 0x00

19 write_terminating_bit()
Flow chart(1) static int MbWidthC [4]= { 0, 8, 8, 16}; static int MbHeightC [4]= { 0, 8, 16, 16}; //calculate RawBits Slice *currSlice = img->currentSlice; Initial CABAC yes write_terminating_bit()

20 Flow chart(2) SODBtoRBSP():converts String Of Data Bits (SODB)
to Raw Byte Sequence Packet (RBSP) RBSPtoEBSP():coverts a RBSP payload to to an EBSP payload return size of streamBuffer after stuffing for this slice’s each partition UVLC UVLC yes SODBtoRBSP() RBSPtoEBSP() no record stuffing bits

21 arienco_done_encoding()
Flow chart(3) CABAC arienco_done_encoding() arienco_done_encoding():Terminates the arithmetic codeword, writes stop bit and stuffing bytes RawMbBits = 256 * img->bitdepth_luma +2 * MbWidthC[active_sps->chroma_format_idc] * MbHeightC[active_sps->chroma_format_idc] * img->bitdepth_chroma; min_num_bytes = ((96 * get_pic_bin_count()– (RawMbBits * (int)img->PicSizeInMbs *3)+ 1023) / 1024; RBSPtoEBSP():add stuffing_bytes lastslice& max_part_nr-1 yes calclulate min_num_bytes min_num_bytes >bytes_in_pic yes calculate stuffing_bytes RBSPtoEBSP()

22 free_ref_pic_list_recodering_buffer()
Flow chart(4) store_contexts(): store motion coding and texture coding context free_ref_pic_list_reordering_buffer(): Free memory for buffering of reference picture reordering commands CABAC yes store_context() !I_SLICE|| !SI_SLICE yes free_ref_pic_list_recodering_buffer()

23 write_MBLayer() Target Codes macroblock header Input rdopt coeff_rate
/* true for calls during RD-optimization */ coeff_rate /* bitrate of Luma and Chroma coeff */ Output no_bits /* number of bits for header coding */ In which file macroblock.c Caller write_one_macroblock()

24 Flow chart(1) int mb_nr = img->current_mb_nr; int prev_mb_nr =
FmoGetPreviousMBNr(img->current_mb_nr); Macroblock* currMB = &img->mb_data[mb_nr]; Macroblock* prevMB = mb_nr ? (&img-mb_data[prev_mb_nr]) : NULL; int* bitCount = currMB->bitcounter; Slice* currSlice = img->currentSlice; int skip = currMB->mb_type ? 0:((img->type == B_SLICE) ? !currMB->cbp:1); /* FmoGetNextMBBr: Returns the MB-Nr (in scan order) of the next MB in the (FMO) Slice, -1 if the SliceGroup is finished */ Initial

25 Flow chart(2) MbaffFrameFlag top MB was skipped mb_nr is even
one field cannot use the macroblocks in the other field of the same frame as a Reference for motion prediction in order to preserve as much spatial consistency as possible MbaffFrameFlag yes top MB was skipped mb_nr is even no yes yes WriteFrameFieldMBInHeader = 1 prevMbSkipped = 0 WriteFrameFieldMBInHeader = 1 topMB=&img->mb_data[prev_mb_nr] prevMbSkipped=topMB->skip_flag

26 Flow chart(3) (1) I_SLICE write mb_aff writeFieldModeInfo() I_SLICE
yes write mb_aff writeFieldModeInfo() I_SLICE write mb_type writeMB_typeInfo() no (2) not I_SLICE but CABAC write mb_skip_flag writeMB_skip_flagInfo_CABAC() CABAC yes write mb_aff writeFieldModeInfo() no write mb_type writeMB_typeInfo()

27 last MB &cod_counter>0
Flow chart(4) (3) VLC not intra mb_type!=0 || (B_SLICE&&cbp!=0) //UVLC yes writeSE_UVLC() (4) skipped MB no write mb_aff writeSE_Flag() last MB &cod_counter>0 yes write mb_type writeMB_typeInfo() writeSE_UVLC()

28 Flow chart(5) IPCM arienco_bits_written() arienco_done_encoding()
returns the number of currently written bits arienco_done_encoding(): terminates the arithmetic codeword, writes stop bit and stuffing bytes arienco_start_encoding(): Initializes the EncodingEnvironment for the arithmetic coder reset_pic_bin_count(): pic_bin_count = 0; IPCM arienco_bits_written() arienco_done_encoding() arienco_start_encoding() reset_pic_bin_count() yes CABAC yes no write mb_type writeSE_Fix() CAVLC yes

29 Flow chart(6) writeIntraModes() encode luma writeChromaIntraPredMode()
encode chroma mb_type writeMotionInfo2NAL() IS_P8x8 writeB8_typeInfo() writeMotionInfo2NAL() mb_type|| (B_SLICE&cbp) INTRA_4x4 INTRA_8x8 writeCBPandLumaCoeff() writeChromaCoeff()


Download ppt "H.264 code tracing Group7 9417053 徐君潔."

Similar presentations


Ads by Google