Professional Documents
Culture Documents
264/AVC
Analysis of H.264/AVC Encoder Reference Software
H.264/AVC
MCCB-2006-002
2006.07.17
1.
2.
............................................................................................................ 12
1.1.
................................................................................................................. 12
1.2.
H.264/AVC ......................................................................................... 12
1.3.
JM ....................................................................................... 12
1.4.
..................................................................................................... 12
1.5.
................................................................................................................. 12
............................................................................ 14
2.1.
................................................................................................. 14
2.2.
JM .................................................................................. 14
2.2.1.
.......................................................................................... 14
2.2.2.
.......................................................................................... 15
2.3.
.............................................................................................. 17
2.4.
JM .................................................................................. 17
2.5.
3.
2.4.1.
.......................................................................................... 17
2.4.2.
............................................................................ 18
2.4.3.
.......................................................................................... 19
..................................................................................................... 20
.............................................................................................. 21
3.1.
......................................................................................... 21
3.2.
..................................................................................................... 26
3.2.1.
................................................................................. 26
3.2.2.
............................................................................ 30
3.2.3.
............................................................................................. 31
3.3.
.............................................................................................. 35
3.4.
.......................................................................................... 40
3.4.1.
............................................................................................. 42
3.4.1.1.
............................................................................ 42
3.4.1.2.
........................................................................ 44
3.4.2.
............................................................................................. 49
3.4.2.1.
..................................................... 50
3.4.2.2.
4.
5.
3.4.3.
Residual .......................................................................................... 55
3.4.4.
.......................................................................................... 60
................................................................................................. 63
4.1.
4x4 .......................................................................... 63
4.2.
16x16 ...................................................................... 77
4.3.
.................................................................................. 81
................................................................................................. 86
5.1.
........................................................................................................ 86
5.1.1.
..................................................................................... 86
5.1.2.
..................................................................................... 90
5.1.3.
............................................................ 92
5.1.3.1.
........................................................................................ 92
5.1.3.2.
6 .................................................................................... 101
5.1.3.3.
6 ........................................................................... 101
5.1.3.4.
5.1.4.
5.2.
...................................................................................... 102
5.1.4.2.
6 .................................................................................... 105
5.1.4.3.
6 ........................................................................... 105
................................................................. 105
....................................................................................... 107
.......................................................................... 109
6.1.1.
................................................... 109
6.1.1.1.
6.1.1.2.
6.1.2.
7.
.......................................................... 102
5.1.4.1.
5.1.5.
6.
............................................. 52
................................................... 124
......................................................................................... 131
7.1.
................................................................................... 131
7.1.2.
8.
................................................................................... 132
7.1.2.1.
................................................ 134
7.1.2.2.
................................................ 135
7.1.3.
..................................................................... 136
7.1.4.
....................................................................... 140
7.1.5.
.............................................................. 143
7.1.6.
..................................................................... 144
7.1.7.
............................................................................... 145
7.1.8.
....................................................................... 146
.................................................................................................... 161
1 H.264/AVC ............................................................................. 21
2 main() ................................................................................. 25
3 encode_one_frame() .................................................................... 29
4 frame_picture() field_picture() ...................................... 30
5 code_a_picture() ........................................................................ 33
6 encode_one_slice() .................................................................... 36
7 encode_one_macroblock() .......................................................... 41
8 encode_one_macroblock() ......................... 43
9 encode_one_macroblock() ..................... 46
10 RDCost_for_macroblocks() ...................................................... 48
11 encode_one_macroblock() ............................... 49
12 encode_one_macroblock() MB ............ 50
13 encode_one_macroblock() Sub-MB ....... 52
14 submacroblock_mode_decision() ........................................... 54
15 encode_one_macroblock() Residual ......................... 55
16 Residual ...................................................................................... 56
17 ................................................................... 56
18 LumaResidualCoding() .............................................................. 58
19 ChromaResidualCoding() .......................................................... 59
20 writeMBLayer() ........................................................................... 61
21 4x4 ......................................................................... 63
22 Mode_Decision_for_4x4IntraBlocks() ................................. 64
23 ................................................ 65
24 4x4 .................................................... 66
25 intrapred_luma() ....................... 72
26 16x16 Plane .......................................................... 79
27 find_sad_16x16() ...................................................................... 80
28 16x16 SAD ............................................ 81
29 IntraChromaPrediction() ........................................................ 82
30 PartitionMotionSearch() ........................................................ 87
31 BlockMotionSearch() ................................................................ 88
32 SetMotionVectorPredictor() ................................................. 90
33 .................................................................................................... 91
34 FastFullPelBlockMotionSearch() ......................................... 93
35 ............................................................................... 93
36 (0,0) ................................................................................................... 94
37 SetupFastFullPelSearch() ...................................................... 97
38 .................................................................... 99
39 SAD ............................................................ 99
40 ............................................................................. 100
41 4x4 SAD ............................................................ 101
42 ........................................................ 102
43 ................................. 103
44 SAD .................................................................................. 105
45 FindSkipModeMotionVector() ............................................... 106
46 ......................................................................... 106
47 dct_luma() ................................................................................. 110
48 QP MF ..................................................................................... 113
49 4x4 ............................................. 115
50 QP V ....................................................................................... 116
51 dct_luma_16x16() .................................................................... 119
52 dct_chroma() ............................................................................. 125
53 2x2 DC ........................................................................... 126
54 writeMBLayer() .......................... 136
55 writeCoeff4x4_CAVLC() .......................................................... 147
56 0 Trailing Ones .......................................... 152
1 ........................................................................................... 14
2 ............................................................................................. 15
3 ............................................................................................. 17
4 ........................................................................................... 18
5 ............................................................................................. 18
6 ................................................................................................. 18
7 ............................................................................................. 20
8 .................................................................................................. 22
9 start_sequence() ................................................................ 22
10 (nal_reference_idc) ........................... 23
11 .................................................................................... 23
12 ................................... 24
13 SetImgType() .......................................... 24
14 .............................................................................................. 26
15 .................................................................................... 26
16 ................................................................................................ 27
17 (frame_no) (img->number) ...................... 27
18 FmoGenerateMapUnitToSliceGroupMap() ............................... 32
19 ................................................................................................ 32
20 ......................................................................................... 34
21 / ............................................ 37
22 ................................... 39
23 RD .............................................................................................. 40
24 .............................. 44
25 ................................................................... 49
26 ................................................................. 51
27 ........................................................ 53
28 ..................................................... 54
29 ........................................................................ 57
30 ................................................... 60
31 , , CBP, ...................... 61
32 4x4 ............................................................... 65
33 4x4 9 ............................................... 66
34 4x4 Cost .......................................................... 67
71 ............................................................................... 130
72 symbol2vlc() ............................................................................. 132
73 .............................................................................................................. 133
74 ............................................................................. 133
75 ............................................................................................ 134
76 ........................................................................ 134
77 .................................................................... 135
78 ........................................................................ 135
79 .................................................................... 136
80 I .............................................................. 137
81 CAVLC ......................................................................................... 137
82 CAVLC ............................................................... 138
83 CAVLC .................................................................... 139
84 .............................. 139
85 ......................................................................... 140
86 CBP ........................................ 141
87 ............................................................................................ 141
88 4x4 ................................................ 143
89 4x4 ..................................................... 143
90 .................................................................... 144
91 ........................................................................ 144
92 ...................................................................... 145
93 ......................................................................... 146
94 ..................................................................................... 148
95 Trailing Ones ..................................... 149
96 0 .............................................................. 150
97 0 ............................................................. 150
98 0 Trailing Ones .................. 151
99 0 Trailing Ones ....................................... 151
100 writeSyntaxElement_NumCoeffTrailingOnes() .............. 153
101 Trailing One ...................................................................... 154
102 0 ............................................................. 155
103 0 0 ................ 157
104 writeSyntaxElement_TotalZeros() ................................... 157
105 DC TotalZeros ................. 158
106 0 ............................................... 159
10
11
1.
1.1.
H.264/AVC JM 10.2 (encoder module)
.
1.2. H.264/AVC
H.264 2003 ITU-T(International Tele-communication
Union-Telecommunication Standardization Sector) .
AVC(Advanced Video Coding) MPEG-4 Part 10 H.264
. ITU-T
MPEG ISO/IEC JTC1 JVT(Joint Video Team)
. H.264 AVC
H.264/AVC .
1.3. JM
JM 10.2 2005 HHI (Heinrich-Hertz-Institute)
H.264/AVC . JM 10.2 H.264/AVC (encoder)
(decoder) .
: http://iphome.hhi.de/suehring/tml/index.htm
1.4.
2 JM10.2 , 3
. , 4, 5 6 (Intra
Prediction) (Inter Prediction) Residual
. 7 . H.264/AVC
(Baseline Profile) .
1.5.
CABAC
CAVLC
CBR
DPB
FRExt
12
HRD
IDR
MB
: Macroblock
MBAFF
NAL
VBR
13
2.
2.1.
JM10.2 tml.dsw Visual Studio 6 (workspace)
. :
lencod
ldecod
rtpdump
, Debug Release .
, bin lencod.exe ldecod.exe .
rtpdump.exe rtpdump .
2.2. JM
JM .
2.2.1.
(encoder syntax) .
lencod [-h] [-d defenc.cfg] {[-f curenc1.cfg]...[-f curencN.cfg]}
{[-p EncParam1=EncValue1]...[-p EncParamM=EncValueM]}
1
Options:
-h
-d
-f
-p
.
defenc.cfg .
( , encoder.cfg .
curencM.cfg .
.
EncParamM EncValueM .
.
4:0:0, 4:2:0, 4:2:2, 4:4:4 YUV RGB . yuv
rgb . .
14
2
lencod.exe
lencod.exe -h
lencod.exe d default.cfg
lencod.exe f curenc1.cfg
lencod.exe f curenc1.cfg -p InputFile="e:\data\container_qcif_30.yuv"
-p SourceWidth=176 -p SourceHeight=144
lencod.exe -f curenc1.cfg -p FramesToBeEncoded=30 \
-p QPFirstFrame=28 -p QPRemainingFrame=28 -p QPBPicture=30
2.2.2.
, Rate/Distortion
. .
Parsing Configfile encoder.cfg...........................
--------------------------- JM 10.1 (FRExt) ------------------------------Input YUV file
: e:\data\foreman_176x144_30p.yuv
Output H.264 bitstream
: test.264
Output YUV file
: test_rec.yuv
YUV Format
: YUV 4:2:0
Frames to be encoded I-P/B
: 10/9
PicInterlace / MbInterlace
: 0/0
Transform8x8Mode
: 0
--------------------------------------------------------------------------Frame Bit/pic
QP
SnrY
SnrU
SnrV
Time(ms) MET(ms) Frm/Fld Ref
--------------------------------------------------------------------------0000(NVB)
176
0000(IDR)
24688
28 37.311 39.857 42.367
250
0
FRM
1
0002(P)
4512
28 36.630 39.770 42.112
172
62
FRM
1
0001(B)
1000
30 36.342 39.721 42.099
359
188
FRM
0
0004(P)
4904
28 36.446 39.479 41.409
235
96
FRM
1
0003(B)
1112
30 36.147 39.487 41.360
390
188
FRM
0
--------------------------------------------------------------------------Total Frames: 5 (3)
Leaky BucketRateFile does not have valid entries.
Using rate calculated from avg. rate
Number Leaky Buckets: 8
Rmin
Bmin
Fmin
108645
24688
24688
135795
24688
24688
162945
24688
24688
190095
24688
24688
217245
24688
24688
244395
24688
24688
271545
24688
24688
298695
24688
24688
---------------------------------------------------------------------------
15
. Verbose Normal
Detailed , Normal , Detailed
.
16
Frame
%04d($Type)
Bit/pic
Verbose
Normal/Detailed
%8d
Normal/Detailed
WP
%1d
Normal/Detailed
QP
%2d
Normal/Detailed
SnrY
%7.3f
Y PSNR
Normal/Detailed
SnrU
%7.3f
U PSNR
Normal/Detailed
SnrV
%7.3f
V PSNR
Normal/Detailed
Time(ms)
%7d
Normal/Detailed
MET(ms)
%5d
Normal/Detailed
Frm/Fld
FLD|FRM
(Frame Field)
Normal/Detailed
%3d
(Intra)
Detailed
%1d
(direct_spatial_mv_pred_flag)
Detailed
L0
%2d
List0
Detailed
L1
%2d
List1
Detailed
RDP
%d
Detailed
Ref
%d
Indicator (nal_reference_idc)
Normal/Detailed
2.3.
< >
2.4. JM
JM .
2.4.1.
(decoder syntax) .
ldecod [-h] {[defdec.cfg] | {[-i bitstream.264]...[-o output.yuv]
[-r reference.yuv] [-uv]}}
17
4
Options:
-h
[defdec.cfg]
(Configure file) .
bitstream.264 .
-i
test.264 .
output.yuv .
-o
test_dec.yuv .
PSNR reference.yuv .
-r
test_rec.yuv .
-uv
4:2:0 YUV ,
4:0:0 YUV Gray Chroma (, 128 ) .
.
5
ldecod.exe
ldecod.exe -h
ldecod.exe default.cfg
ldecod.exe i bitstream.264
ldecod.exe i bitstream.264 o output.yuv r reference.yuv
ldecod.exe i bitstream420.264 -uv
2.4.2.
.
6
Decoder Parameters:
bistream.264
H.26L
output.yuv
RAW .
SPS .
input.yuv
SNR
10
( .
SPS num_ref_frames
Profile/Level )
18
SNR
( 0 ,
PSNR )
500000
Rate (HRD )
104000
73000
leakybucketparam.cfg
LeakyBucket Params
2.4.3.
, Rate/Distortion
. .
----------------------------- JM 9.2 (FRExt) ----------------------------Decoder config file
: decoder.cfg
-------------------------------------------------------------------------Input H.264 bitstream
: test.264
Output decoded YUV
: test_dec.yuv
Output status file
: log.dec
Input reference file
: test_rec.yuv
-------------------------------------------------------------------------POC must = frame# or field# for SNRs to be correct
-------------------------------------------------------------------------Frame
POC
Pic#
QP
SnrY
SnrU
SnrV
Y:U:V Time(ms)
-------------------------------------------------------------------------0000(I)
0
0
28 0.0000 0.0000 0.0000 4:2:0
16
0006(P)
12
1
28 0.0000 0.0000 0.0000 4:2:0
0
0004(RB)
8
2
28 0.0000 0.0000 0.0000 4:2:0
15
0002(RB)
4
3
28 0.0000 0.0000 0.0000 4:2:0
16
-------------------- Average SNR all frames -----------------------------SNR Y(dB)
: 0.00
SNR U(dB)
: 0.00
SNR V(dB)
: 0.00
Total decoding time : 0.062 sec
-------------------------------------------------------------------------Exit JM 9 (FRExt) decoder, ver 9.2
19
Frame
%3d($Type)
POC
%3d
Pic#
%3d
Frame_num
QP
%5d
Y PSNR
SnrY
%7.4f
0.000 ,
.
U PSNR
SnrU
%7.4f
0.000 ,
.
V PSNR
SnrV
%7.4f
Y:U:V
X:Y:Z
Time(ms)
%5d
0.000 ,
.
(Color format)
2.5.
< >
20
3.
3.1.
- (Network Abstraction
Layer) (Video Coding Layer) ,
(Data Partitioning) ([1] ).
1 H.264/AVC
H.264/AVC main()
, , NAL DPL/VCL .
21
Init_QMatrix()
Init_QOffsetMatrix()
Q offset
AllocNalPayloadBuffer()
init_poc()
GenerateParameterSets()
SetLevelIndices
(img->LevelIndex)
init_img()
malloc_picture()
init_rdopt()
RD
init_dpb()
init_stats()
(successive_Bframe SNR )
init_global_buffers()
create_context_memory()
Context
Init_Motion_Search_Module()
Motion Search
PatchInputNoFrames()
, start_sequence() GenerateSeq_
, (VCL) . ,
22
(nal_reference_idc)
Picture Order Counts (framepoc, toppoc, bottompoc)
(frame_num)
(nal_reference_idc) 0 , NAL
(SPS), (PPS), ,
. (nal_reference_idc)
. intra_period I (Random
Access period, IntraPeriod) . IGOP
(NumberOfFrameInSecondIGOP==0) 1 , Disposable P
(DisposableP ==0) .
10 (nal_reference_idc)
if (input->intra_period)
img->nal_reference_idc
= (img->number % input->intra_period) ? (img->number + 1)% 2 : 1;
else
img->nal_reference_idc
= (img->number) ? (img->number + 1)% 2 : 1;
(frame_num) .
IGOP (NumberOfFrameInSecondIGOP==0)
. , log2_max_frame_num_minus4 (MaxFrameNum)
.
MaxFrameNum 2
log2_max_frame_num_minus4+4
( 1)
11
if (input->intra_period && input->idr_enable)
{
img->frame_num = ((img->number - primary_disp)%input->intra_period)
% (1 << (log2_max_frame_num_minus4 + 4));
if (img->number % input->intra_period
{
img->frame_num = 0;
primary_disp
= 0;
}
}
1
IGOP 2 .
23
== 0)
else
img->frame_num = (img->number - primary_disp)
% (1 << (log2_max_frame_num_minus4 + 4));
if (img->nal_reference_idc == 0)
primary_disp ++;
I (IntraPeriod) 0 ,
(nal_reference_idc) (frame_num)
. (frame_num)
,
(nal_
reference_idc) POC(framepoc) .
12
img->number
nal_reference_idc
frame_num
, SetImgType() I (intra
_period) I P
.
13 SetImgType()
if (input->intra_period)
{
if(img->number %input->intra_period)
img->type = P_SLICE;
else
img->type = I_SLICE;
}
else
{
if(img->number)
img->type = P_SLICE;
else
img->type = I_SLICE;
}
24
, I (intra_period) 0
(IPPP) , 4 (IPPPIPPP) .
Start
Initialization
start_sequence()
Start Sequence
(Sequence Header)
Loop for Frames
SetImgType()
encode_one_frame()
Frame Encoding
(I/P frames)
Last Frame?
N
img->number++
Y
terminate_sequence()
Terminate Sequence
End
2 main()
(nal_reference_idc) , encode_one_
frame() . encode_one_frame()
, .
, terminate_sequence()
.
25
3.2.
3.2.1.
encode_one_frame()
.
H.264/AVC 4 . , (frame
encoding), (field encoding), / (picture level field/
frame adaptive encoding), / (macroblock level
field/frame adaptive encoding) .
PicInterace MbInterace
. , /
, PicInterace=2 MbInterace=2 ([3] 4-19~20 ).
14
Options : PicInterace
0
15
Options : MbInterace
0
/ ,
. .
J Distortion Rate
( 2)
Lagrangian ,
Rate
.
, encode_one_frame()
.
26
, .
.
, , .
.
16
put_buffer_frame()
init_frame()
CalculateFrameNumber()
IGOP (, NumberOfFrameInSecondIGOP==0),
(Absolute Frame Number) CalculateFrameNumber()
.
frame_no = img->number * (input->jumpd + 1)
( 3)
frame_no
(FrameSkip==1 )
, ReadOneFrame()
. , , PaddAutoCrop
Borders() .
,
. PicInterace FIELD_CODING field_picture()
, frame_picture() .
, PicInterace ADAPTIVE_CODING
27
. picture_structure_decision()
. fld_flag=0
, fld_flag=1 .
NAL SNR
, writeout_
picture() NAL .
-Y, U V PSNR ,
(DPB: Decoded Picture Buffer) .
, (DPB) ,
. ,
,
. (I/P )
(B ) .
,
. (DPB)
.
28
Start
Initialization
ReadOneFrame()
Read a Frame
PaddAutoCropBorders()
Border Padding
Frame Coding?
Y
frame_picture()
Adaptive Coding?
Y
Adaptive Coding
field_picture()
writeout_picture()
find_snr()
store_picture_in_dpb()
field_picture()
picture_structure_decision()
Find SNR
End
3 encode_one_frame()
29
3.2.2.
(Progressive Scanning) ,
(Interlace Scanning) .
frame_picture() field_picture() .
.
frame_picture()
. code_a_picture()
. Distortion find_distortion()
. (a) .
field_picture() .
Top Bottom code_a_picture()
. Distortion distortion_
fld() . (b) .
Start
code_a_picture()
find_distortion()
Start
code_a_picture()
Find Distortion
code_a_picture()
distortion_fld()
End
Find Distortion
End
(a)
(b)
4 frame_picture() field_picture()
30
code_a_
picture() . ,
code_a_picture() .
3.2.3.
code_a_picture()
. ,
ASO(Arbitrary Slice Order) FMO(Flexible Macroblock Ordering)
.
MPEG-2 MPEG-4 (Raster Scan)
, H.264/AVC (ASO) .
H.264/AVC (FMO) 7 .
.
.
FMO
. .
RandomIntraNewPicture()
(Intra Coding) .
(pseudo-random) .
(configure file) RandomIntraMBRefresh
. 0 ,
.
FMO
31
18 FmoGenerateMapUnitToSliceGroupMap()
switch (pps->slice_group_map_type)
{
case 0:
FmoGenerateType0MapUnitMap (img, pps);
break;
case 1:
FmoGenerateType1MapUnitMap (img, pps);
break;
case 2:
FmoGenerateType2MapUnitMap (img, pps);
break;
case 3:
FmoGenerateType3MapUnitMap (img, pps);
break;
case 4:
FmoGenerateType4MapUnitMap (img, pps);
break;
case 5:
FmoGenerateType5MapUnitMap (img, pps);
break;
case 6:
FmoGenerateType6MapUnitMap (img, pps);
break;
default:
exit (-1);
}
.
19
Interleave
Dispersed
Foreground
Box-out
Raster Scan
Wipe
Explicit
( )
( )
32
Start
Allocate
forced Intra MBs
RandomIntraNewPicture()
FmoInit()
FmoStartPicture()
Initialize FMO
CalculateQuantParam()
CalculateOffsetParam()
Set Quantization
Parameters
Loop for Slices
Slice Encoding
encode_one_slice()
Last Slice?
N
Slice++
Y
Last
Slice Group?
N
SliceGroup++
FmoEndPicture()
Terminate Slices
DeblockFrame()
End
5 code_a_picture()
, FmoGenerateMBAmap() MapUnitToSliceGroupMap
(MBAmap) . MBAmap 1 ,
(scan order) ID(slice_group_id)
.
33
FmoStartPicture() FMO .
, FmoStartPicture()
FirstMBInSlice[] .
CalculateQuantParam() CalculateOffset
Param() . CalculateQuantParam()
, CalculateOffsetParam()
.
, encode_one_slice()
. FmoSetLastMacroblockInSlice()
FirstMBInSlice[] . FmoSliceGroupCompletelyCoded()
,
.
(NumberOfCodedMBs) (PicSizeInMbs)
.
.
20
while (NumberOfCodedMBs < img->PicSizeInMbs)
{
// Encode one Slice Group
while (!FmoSliceGroupCompletelyCoded (SliceGroup))
{
// Encode the current slice
NumberOfCodedMBs
+= encode_one_slice (SliceGroup, pic, NumberOfCodedMBs);
FmoSetLastMacroblockInSlice (img->current_mb_nr);
// Proceed to next slice
img->current_slice_nr++;
( omitted )
}
// Proceed to next SliceGroup
SliceGroup++;
}
34
FmoEndPicture()
. DeblockFrame()
.
(Deblocking Filter) .
3.3.
encode_one_slice() ,
. ,
, , /
. .
FmoGetFirstMacroblockInSlice() ,
(FirstMBInSlice[SliceGroup])
(CurrentMbAddr) . init_slice()
,
SetLagrangianMultipliers() Lagrangian Multiplier . ,
Lagrangian Multiplier(lambda_md) Lagrangian
Multiplier(lambda_mf) .
, start_slice() .
start_slice() SliceHeader()
, CAVLC CAVLC_init()
CAVLC (img->nz_coeff) .
, CalculateOffsetParam()
. , MbInterlace ,
FRAME_CODING . 1 MbInterlace ADAPTIVE_
CODING , MbInterlace FIELD_CODING
. , MbInterlace
.
img->MbaffFrameFlag. img->MbaffFrame
Flag frame_picture() field_picture() MbInterlace . MbInter
lace FRAME_CODING img->MbaffFrameFlag FALSE .
35
Start
Initialization
start_slice()
Start Slice
Frame MB
Coding?
Y
N
Adaptive MB
Coding?
Y
start_macroblock()
Start Macroblock
Start MB pair
Start MB pair
Macroblock Encoding
Encode
MB pair as Frame MBs
Encode
Macroblock
encode_one_macroblock()
Encode
MB pair as Field MBs
RD Decision
for Frame/Field MBs
write_one_macroblock()
Write Macroblock
terminate_macroblock()
Terminate Macroblock
N
Next Macroblock
terminate_slice()
Terminate Slice
End
6 encode_one_slice()
36
Encode
MB pair as Field MBs
. start_macroblock() .
, (img->mb_x, img->mb_y )
(currMB->slice_nr) . start_macroblock()
CheckAvailabilityOfNeighbors()
(availability) .
, encode_one_macroblock() .
, (macroblock pair)
RD Cost . RD Cost encode_
one_macroblock() . , img->field_mode MBPairIs
Field .
. RD Cost
, .
21 /
if((input->MbInterlace==ADAPTIVE_CODING)&&(FrameRDCost<FieldRDCost))
{
img->field_mode = 0;
MBPairIsField = 0;
}
else
{
img->field_mode = 1;
MBPairIsField = 1;
}
NAL
write_one_macroblock()
NAL . write_one_macroblock()
writeMBLayer() CAVLC CABAC
. , terminate_macroblock()
. writeMBLayer()
3.4.4 .
. recode_macroblock
FALSE , TRUE
terminate_macroblock() .
37
recode_macroblock
, FmoGetNextMBNr()
CurrentMbAddr . proceed2nextMacroblock()
.
, FmoGetPreviousMBNr()
img->current_mb_nr .
Top
Bottom . ,
Top Bottom .
,
encode_one_
macroblock() start_macroblock()
. copy_rdopt_data()
,
write_one_macroblock() terminate_macroblock()
.
, NAL
. ,
NAL , .
38
22
// Top MB in the MB pair
start_macroblock (CurrentMbAddr, img->field_mode);
rdopt = img->field_mode ? &rddata_top_field_mb : &rddata_top_frame_mb;
copy_rdopt_data (0);
write_one_macroblock (1);
terminate_macroblock (&end_of_slice, &recode_macroblock);
if (recode_macroblock == FALSE)
{
CurrentMbAddr = FmoGetNextMBNr (CurrentMbAddr);
if (CurrentMbAddr == -1)
end_of_slice = TRUE;
NumberOfCodedMBs++;
proceed2nextMacroblock ();
// Bottom MB in the MB pair
start_macroblock (CurrentMbAddr, img->field_mode);
rdopt = img->field_mode ? &rddata_bot_field_mb:&rddata_bot_frame_mb;
copy_rdopt_data(1);
write_one_macroblock (0);
terminate_macroblock (&end_of_slice, &recode_macroblock);
if (recode_macroblock == FALSE)
{
CurrentMbAddr = FmoGetNextMBNr (CurrentMbAddr);
if (CurrentMbAddr == -1)
end_of_slice = TRUE;
NumberOfCodedMBs++;
proceed2nextMacroblock ();
}
else
{
img->current_mb_nr = FmoGetPreviousMBNr(img->current_mb_nr);
if(img->current_mb_nr == -1 )
}
}
else
{
img->current_mb_nr = FmoGetPreviousMBNr(img->current_mb_nr);
if(img->current_mb_nr == -1 )
39
3.4.
encode_one_macroblock() .
,
. RD (RD optimization)
.
3 . ,
RD Cost .
, RD
Residual .
, RandomIntra()
Refresh (pseudo-random) .
init_enc_mb_params()
(validity) .
I
, P B ,
. , I
Cost .
, I (intra) TRUE
.
(Inter Prediction)
(intra) FALSE , P
B , Cost .
RD Cost , (sub-macroblock)
RD Cost .
RD
RD (RDOptimization) ,
. RD
.
23 RD
Options : RDOptimization
0
40
(RDOptimization==0) ,
(RDOptimization==1 or 2) .
Start
RandomIntra()
init_enc_mb_params()
Initialization
Choose Best Mode
Only Intra?
(I slice)
Inter Prediction
High Complexity
Mode?
N
Low Complextiy
Intra Prediction
High Complexity
Intra Prediction
SetModesAndRefframeForBlocks()
SetCoeffAndReconstruction8x8()
Set Modes
& Reference Frames
Set MB Parameters
set_stored_macroblock_parameters()
Set Coefficient
& Reconstruction for 8x8
Residual Coding
End
7 encode_one_macroblock()
41
. RD , SetModesAndRefframe
ForBlocks()
. , 8x8 (P8x8) , SetCoeffAnd
Reconstruction8x8() 8x8
. , , , .
RD , set_stored_macroblock_parameters()
.
Residual
, RD (Low
3.4.1.
encode_one_macroblock()
(Low Complexity Intra Prediction)
(High Complexity Intra Prediction) .
3.4.1.1.
(Low Complexity Intra Prediction) 4
. , .
, 4x4 16x16 RD Cost
. Cost .
, H.264/AVC 4x4 16x16
. 8x8 (Transform8x8Mode)
8x8 , 8x8
.
42
, IntraChroma
Prediction() 8x8
(DC_PRED_8), (HOR_PRED_8), (VERT_PRED_8)
(PLANE_8) .
currMB->c_ipred_mode .
.
Start
IntraChromaPrediction()
Mode_Decision_for_Intra4x4Macroblock()
intrapred_luma_16x16()
find_sad_16x16()
End
8 encode_one_macroblock()
4x4 , Mode_Decision_for_Intra4x4Macroblock()
4x4 . , 9
Cost . Cost Cost(min_cost)
, Cost Cost I4MB .
16x16
4x4 , intrapred_luma_16x16()
4 16x16 . find_sad_16x16()
16x16 ,
Cost- SAD(Sum of Absolute Difference) . Cost Cost
(min_cost) , I16MB dct_luma_16x16()
16x16 DCT . dct_luma_16x16()
6 .
43
4x4 16x16
Cost (best_mode) .
.
24
IntraChromaPrediction(NULL, NULL, NULL);
if (enc_mb.valid[I4MB]) // check INTRA4x4
{
Mode_Decision_for_Intra4x4Macroblock(enc_mb.lambda_md,&cost);
if (cost <= min_cost)
{
min_cost
= cost;
best_mode = I4MB;
}
}
if (enc_mb.valid[I16MB]) // check INTRA16x16
{
intrapred_luma_16x16 ();
cost = find_sad_16x16 (&i16mode);
if (cost < min_cost)
{
best_mode
= I16MB;
dct_luma_16x16 (i16mode);
}
}
3.4.1.2.
(High Complexity Intra Prediction)
2 . ,
.
RD Cost .
.
, IntraChroma
Prediction() 8x8
(DC_PRED_8), (HOR_PRED_8), (VERT_PRED_8)
44
(PLANE_8) .
currMB->c_ipred_mode .
.
RD Cost
Cost .
RDCost_for_macroblocks()
RD . ,
RD Cost RDCost_for_macroblocks()
. .
SetModesAndRefframeForBlocks()
.
compute_mode_RD_cost()
,
RD Cost .
mode, c_ipred_mode
.
45
Start
IntraChromaPrediction()
compute_mode_RD_cost()
Set Modes
& Reference Frames
SetModesAndRefframeForBlocks()
RDCost_for_macroblocks()
store_macroblock_parameters()
Store MB Parameters
N
mode++
N
c_ipred_mode++
Y
Decide Best Mode
End
9 encode_one_macroblock()
46
Residual RD Cost ,
Cost Residual .
Distortion , writeMBLayer()
NAL Rate . Distortion Rate
RD Cost . lambda RD Cost
Lagrangian Multiplier.
rdcost = distortion + lambda * max(0.5,rate)
( 4)
,
. , 16x16
(I16MB) Intra16x16_Mode_Decision()
intrapred_luma_16x16() find_
sad_16x16() , 16x16 DCT dct_luma_16x16()
. .
, 4x4 Residual
. 4
.
47
Start
Set Modes
& Reference Frames
SetModesAndRefframeForBlocks()
mode==1~3
(16x16/16x8/8x16)
Y
Luma Residual Coding
LumaResidualCoding()
N
mode==
P8x8
Y
Set Coefficient
& Reconstruction for 8x8
SetCoeffAndReconstruction8x8()
N
mode==
I4MB
Y
4x4 Intra Mode Decision
Mode_Decision_for_Intra4x4Macroblock()
mode==
I16MB
Y
Intra16x16_Mode_Decision()
ChromaResidualCoding()
Get Distortion
Get Rate
& Write MB Header
writeMBLayer()
Get RD Cost
Get RD Cost
End
10 RDCost_for_macroblocks()
48
3.4.2.
P B .
,
.
25
I
LIST 0
LIST 1
(Baseline Profile) B , P
.
.
, - 16x16, 16x8, 8x16
Cost . - 8x8, 8x4, 4x8, 4x4
Cost .
, (Tree-structured Motion Compensation)
.
, .
Start
Mode Decision
for MB Type
(16x16/16x8/8x16/Skip)
Mode Decision
for Sub-MB Type
(8x8/8x4/4x8/4x4/Skip)
End
11 encode_one_macroblock()
49
3.4.2.1.
16x16, 16x8 8x16 .
Cost .
Cost LIST
. .
LIST
, Partition
MotionSearch() .
list_prediction_cost() LIST0
Cost bmcost[LIST_0] .
bmcost[LIST_0] Cost .
Start
PartitionMotionSearch()
list_prediction_cost()
All Blocks
Encoded?
Y
assign_enc_picture_params()
SetRefAndMotionVectors()
N
block++
Set Parameters
Assign
Picture Parameters
All MB Types
Checked?
N
mode++
Y
Decide Best Mode
End
12 encode_one_macroblock() MB
50
, assign_enc_picture_params() . 16x8
8x16 , SetRefAndMotionVectors()
.
51
{
best_mode = mode;
min_cost
= cost;
best_transform_flag = currMB->luma_transform_size_8x8_flag;
}
}
}
3.4.2.2.
8x8, 8x4, 4x8 4x4 .
Cost
. .
Start
submacroblock_mode_decision()
Mode Decision
for Sub-MB
Y
best_mode = P8x8
End
13 encode_one_macroblock() Sub-MB
,
submacroblock_mode_decision() . 8x8, 8x4, 4x8
4x4 Cost
. Cost Cost(min_cost)
(P8x8) .
.
52
27
if ((!inter_skip) && enc_mb.valid[P8x8])
{
(...omitted...)
tr4x4.cost8x8 = 0;
// Check 8x8, 8x4, 4x8 and 4x4 partitions with transform size 4x4
for (block=0; block<4; block++)
{
submacroblock_mode_decision(enc_mb, &tr4x4, currMB,
cofAC8x8[block],&have_direct,bslice,block,&cost_direct,
&cost,&cost8x8_direct,0);
(...omitted...)
}
(...omitted...)
//check cost for P8x8 for non-rdopt mode
if (!input->rdopt&&(tr4x4.cost8x8<min_cost))
{
best_mode = P8x8;
min_cost = tr4x4.cost8x8;
currMB->luma_transform_size_8x8_flag=0;
}
}
submacroblock_mode_decision()
.
.
LIST
, Partition
MotionSearch() .
list_prediction_cost() LIST0
Cost bmcost[LIST_0] .
bmcost[LIST_0] Cost .
LIST , Cost Cost Cost
Cost . ,
.
53
Start
PartitionMotionSearch()
list_prediction_cost()
N
index++
Y
Decide Best Mode
Residual Coding
for 8x8 Luma Block
LumaResidualCoding8x8()
SetRefAndMotionVectors()
reset_coding_state()
End
14 submacroblock_mode_decision()
Cost , Cost
. .
28
if (!input->rdopt && *cost < min_cost8x8)
{
min_cost8x8 = *cost;
dataTr->part8x8mode [block] = mode;
dataTr->part8x8pdir [block] = best_pdir;
dataTr->part8x8fwref[block] = best_ref[LIST_0];
dataTr->part8x8bwref[block] = best_ref[LIST_1];
}
54
, LumaResidualCoding8x8() 8x8
Residual , , .
CBP(Coded Block Pattern) , SetRefAndMotionVectors()
. reset_coding_state()
,
.
3.4.3. Residual
, RD (Low Complexity Mode)
,
Residual . Residual .
Start
Intra?
N
LumaResidualCoding()
IntraChromaPrediction()
ChromaResidualCoding()
End
15 encode_one_macroblock() Residual
, (I4MB,I8MB,I16MB) Residual
.
Residual .
55
Residual (High
Complexity Mode) . ,
Cost
. ,
.
Residual
, Residual
LumaResidualCoding() .
16 4x4 4x4 Residual .
LumaResidualCoding()
.
LumaPrediction4x4()
LumaResidualCoding8x8()
16 Residual
, 4x4 .
16
17
20
21
18
19
22
23
12
13
10
11
14
15
Cr
Cb
17
56
Residual
IntraChromaPrediction()
8x8 .
, ChromaResidualCoding() Residual .
ChromaResidualCoding() 8x8 4 4x4
4x4 Residual .
. IntraChromaPrediction()
4.3 .
LumaResidualCoding()
LumaResidualCoding() SetModesAndRefframe()
8x8 , LumaResidualCoding8x8()
8x8 Residual . 8x8
4 4x4 Residual . 4x4
Residual Residual
.
, SetModesAndRefframe() ,
8x8 . B
, .
29
*fw_ref =enc_picture->ref_idx[LIST_0][img->block_y+j][img->block_x+i];
*bw_ref = 0;
*fw_mode
= currMB->b8mode[b8];
*bw_mode
= 0;
DCT/ 4x4 ,
(DFD, Displaced Frame Difference) img->m7[y][x] . ,
dct_luma() DCT (quantization) , IDCT,
(reconstruction) . dct_luma() 6
.
57
Start
SetModesAndRefframe()
LumaPrediction4x4()
dct_luma()
LumaResidualCoding8x8()
DCT/Quantization/
Inverse Quant./IDCT
Reconstruction
N
block4++
Y
All 8x8 Blocks
Checked?
N
block8++
End
18 LumaResidualCoding()
ChromaResidualCoding()
ChromaResidualCoding()
.
dct_chroma() . , Chroma
ResidualCoding() .
4x4 SetModesAndRefframe()
4x4 , ChromaPrediction
4x4() 4x4 .
IntraChromaPrediction4x4() 4x4 ,
OneComponentChromaPrediction4x4() LIST
4x4 . img->mpr[y][x] .
58
8x8
Cb Cr 8x8 ,
img->m7[y][x] . (imgUV_org[uv][y]
[x]) (img->mpr[y][x]) .
(CBP) (*cr_cbp) 16
(CBP, Coded Block Pattern) img->mb_data[].cbp
.
img->mb_data[].cbp += (*cr_cbp)<<4
( 5)
Start
SetModesAndRefframe()
ChromaPrediction4x4()
N
block4++
Y
Get Displaced Frame
Differences (DFD)
dct_chroma()
DCT/Quantization/
Inverse Quant./IDCT
Reconstruction
Cb & Cr
Checked?
N
uv++
Update cr_cbp
End
19 ChromaResidualCoding()
59
3.4.4.
encode_one_macroblock() ,
, write_one_macroblock()
NAL . , writeMBLayer()
, CALVC CABAC
. (Entropy Coding) DCT
. writeMBLayer()
.
30
Macroblock Type
(Prediction Mode)
(CBP)
(QP)
Quantization Parameter
writeMBLayer() .
.
, (Macroblock Type) .
(currMB->mb_type) (Unsigned
Exponential Golomb Coding) .
I
, RLC (Run-Length Coding)
. , ,
.
7 .
, , CBP,
, CBP, ,
.
60
31 , , CBP,
no_bits += writeIntra4x4Modes(-1);
no_bits += writeChromaIntraPredMode();
if (currMB->mb_type !=0 && currMB->mb_type !=P8x8)
no_bits
+= writeMotionInfo2NAL();
if (currMB->mb_type!=0)
{
*coeff_rate = writeCBPandLumaCoeff();
*coeff_rate += writeChromaCoeff();
no_bits
+= *coeff_rate;
Start
Write MB Type
ue_linfo()
writeIntra4x4Modes()
writeChromaIntraPredMode()
Skip Mode?
Y
Write Motion Information
writeMotionInfo2NAL()
writeCBPandLumaCoeff()
writeChromaCoeff()
End
20 writeMBLayer()
61
, writeIntra4x4Modes() write
ChromaIntraPredMode() .
, (CBP), (QP), (Residual Data)
(Motion Information) .
(CBP), writeCBPandLuma
Coeff() , writeChromaCoeff()
. , writeMotionInfo2NAL()
. 7 .
62
4.
4.1. 4x4
4x4 Mode_Decision_for_Intra4x4Macroblock()
. 4 8x8
Cost . 8x8
Mode_Decision_for_8x8IntraBlocks()
.
8x8 4 4x4
Cost . 4x4
Mode_Decision_for_4x4IntraBlocks() .
.
Mode_Decision_for_8x8IntraBlocks()
Mode_Decision_for_4x4IntraBlocks()
21 4x4
4x4 Mode_Decision_for_4x4IntraBlocks()
.
63
Start
Get Neighboring
4x4 Luma Blocks
getLuma4x4Neighbour()
intrapred_luma()
Available Mode?
Compute Cost
High Complexity
Mode?
N
Compute Cost
for Low Complexity Mode
SATD()
N
ipmode++
Compute Cost
for High Complexity Mode
RDCost_for_4x4IntraBlocks()
All Modes
Checked?
Y
High Complexity
Mode?
Set Variables
N
dct_luma()
Residual Coding
End
22 Mode_Decision_for_4x4IntraBlocks()
64
getLuma4x4Neighbour()
(A ) (B ) .
, leftMode upMode .
leftMode upMode ,
(mostProbableMode) . .
23
32 4x4
getLuma4x4Neighbour(img->current_mb_nr, block_x4, block_y4, -1,
0,
&left_block);
getLuma4x4Neighbour(img->current_mb_nr, block_x4, block_y4,
0, -1,
&top_block);
upMode= top_block.available ?
img->ipredmode[top_block.pos_y ][top_block.pos_x ] : -1;
leftMode = left_block.available ?
img->ipredmode[left_block.pos_y][left_block.pos_x] : -1;
mostProbableMode
4x4 , intrapred_luma() 9
4x4 .
.
65
24 4x4
, A~Q 4x4
a~p . PredPel[] ,
img->mprr[ipmode][x][y] . 4x4
9 .
33 4x4 9
VERT_PRED
Vertical
HOR_PRED
Horizontal
DC_PRED
DC
DIAG_DOWN_LEFT_PRED
DIAG_DOWN_RIGHT_PRED
VERT_RIGHT_PRED
Vertical Right
HOR_DOWN_PRED
Horizontal Down
VERT_LEFT_PRED
Vertical Left
HOR_UP_PRED
Horizontal Up
4x4 intrapred_luma()
.
, Cost Cost
. , ,
.
DC (DC_PRED)
(B ) , VERT_PRED, VERT_LEFT_PRED, DIAG_DOWN_
LEFT_PRED
(A ) , HOR_PRED HOR_UP_PRED
(A ), (B ) (D )
66
= cost;
, Cost . , Cost
Lagrangian Multiplier . Lagrangian Multiplier enc_mb->
lambda_md encode_one_slice() SetLagrangianMultip
liers() , encode_one_macroblock()
init_enc_mb_params() . RD
.
, QPISlice QPPSlice I P
28 . DispPQPOffset Disposable P
0 . ,
(QP2QUANT) enc_mb->lambda_md 5 , fixedcost
20 .
fixedcost = floor(4 * QP2QUANT)
( 6)
67
35 Lagrangian Multiplier
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
};
SHIFT_QP = 12;
if (img->type == I_SLICE)
img->qp = QPISlice;
else
img->qp = QPPSlice + (img->nal_reference_idc? 0 :
DispPQPOffset);
enc_mb->lambda_md = QP2QUANT[max(0,img->qp-SHIFT_QP)];
, (mostProbableMode) Cost
.
cost = SATD
( 7)
Cost .
cost = SATD + fixedcost
( 8)
TH 2 , D i, j i, j
SATD
1 3 3
TH D i, j
2 i 0 j0
1
1
H
1
( 9)
H .
1
1
1
1
1
1
1
1
1
1
( 10)
68
69
37 RDCost_for_4x4IntraBlocks()
*nonzero = dct_luma(block_x, block_y, &dummy, 1);
for (y=0; y<4; y++)
{
for (x=pic_pix_x; x<pic_pix_x+4; x++)
distortion += img->quad [imgY_org[pic_opix_y+y][x]
- imgY[pic_pix_y+y][x]];
}
writeSyntaxElement_Intra4x4PredictionMode(currSE, dataPart);
rate = currSE->len;
rate
( 11)
Cost
, Cost (best_ipmode) , img->
ipredmode[y][x] . Cost Cost .
4x4
. 4x4
(most
ProbableMode) . (mostProbableMode)
, , TRUE
. ,
FALSE , .
.
38
img->ipredmode[pic_block_y][pic_block_x] = best_ipmode;
if(mostProbableMode == best_ipmode)
intra_pred_modes = -1;
else if(mostProbableMode > best_ipmode)
intra_pred_modes = best_ipmode;
else
intra_pred_modes = best_ipmode-1;
70
(mostProbableMode) (best_
ipmode) (mostProbableMode)
1 .
Residual
, Cost
Residual Residual .
Cost dct_luma()
Residual .
intrapred_luma()
Mode_Decision_for_4x4IntraBlocks() 4x4
. 4x4
intrapred_luma() , .
.
, A~Q , get
Neighbour() .
A~Q .
.
, img->dc_
pred_value . init_img() .
img->dc_pred_value = 1<<(img->bitdepth_luma - 1)
( 12)
, img->bitdepth_luma (BitDepth
Luma) 8 . , img->dc_pred_value 128
.
, (C ) (block_avail
able_up_right==0) (B )
(P_D) .
71
Start
Available
Up-Block?
(Block B)
getNeighbour()
Y
(A~D) = LumaValue
Available
Up-Right-Block?
(Block C)
(A~D) = DC_Pred_Value
Y
(E~H) = LumaValue
Available
Left-Block?
(Block A)
(E~H) = D
Y
(I~L) = LumaValue
Available
Up-Left-Block?
(Block D)
(I~L) = DC_Pred_Value
Y
Q = LumaValue
Q = DC_Pred_Value
End
25 intrapred_luma()
72
DC
, . DC
( 2) , DC . DC
(B ) (A ) ,
4x4 .
- , img->
dc_pred_value 4x4 .
39 4x4 DC
ABCD
AllPixels round ( A B C D)
4
AllPixels round ( I J K L)
4
I
J
K
L
ABCD
I
J
K
L
Horizontal Vertical
AllPixels round ( A B C D I J K L)
8
, Horizontal ( 1) Vertical ( 0)
. , Vertical (VERT_PRED)
(P_A ~ P_D) . Horizontal
(HOR_PRED) (P_I ~ P_L)
. ,
(img->mprr[][0][0]) -1 .
73
ABCD
Vertical
( 0)
a, e, i, m A
(1 column)
b, f , j, n B
(2
st
nd
column)
c, g , k , o C
(3 column)
d , h, l , p D
(4 column)
rd
th
( B) ,
a, b, c, d I
(1 row)
e, f , g , h J
Horizontal
( 1)l
i, j , k , l K
I
J
K
L
a 1
st
(2
nd
row)
rd
(3 row)
m, n, o, p L
th
(4 row)
( A) ,
a 1
Diagonal Down Left Vertical Left Diagonal Down Left ( 3) Vertical Left
( 7) .
, (B ) .
41 4x4 Diagonal Down Left Vertical Left
ABCDEF GH
a round A 2B C / 4
b, e round B 2C D / 4
c, f , i round C 2D E / 4
Diagonal
d , g , j, m round D 2E F / 4
Down
Left
h, k , n round E 2F G / 4
( 3)
l , o round F 2G H / 4
p round G 3H / 4
ABCDEF GH
Vertical
Left
( 7)
a round A B / 2
e round A 2B C / 4
b, i round B C / 2
f , m round B 2C D / 4
c, j round C D / 2
74
g , n round C 2D E / 4
d , k round D E / 2
h, o round D 2E F / 4
l round E F / 2
p round E 2F G / 4
Horizontal Up Horizontal Up ( 8) .
(A ) , (B )
.
42 4x4 Horizontal Up
a round I J / 2
Horizontal
Up
( 8)
I
J
K
L
b round I 2 J K / 4
c, e round J K / 2
d , f round J 2K L / 4
g , i round K L / 2
h, j round K 3L / 4
k , l , m, n, o, p L
Diagonal Down Right, Vertical Right Horizontal Down Diagonal Down Right
( 4), Vertical Right( 5) Horizontal Down ( 6)
. (B ), (A ) (D )
, .
75
ABCDEF GH
Diagonal
Down
m round J 2K L / 4
i, n round I 2 J K / 4
I
J
K
L
e, j, o round Q 2I J / 4
a, f , k , p round I 2Q A / 4
Right
b, g , l round Q 2 A B / 4
( 4)
c, h round A 2B C / 4
d round B 2C D / 4
ABCDEF GH
m round I 2 J K / 2
i round Q 2I J / 4
I
J
K
L
e, n round I 2Q A / 2
a, j round Q A / 2
f , o round Q 2 A B / 2
Vertical
Right
b, k round A B / 2
( 5)
g , p round A 2B C / 4
c, l round B C / 2
h round B 2C D / 4
d round C D / 2
ABCDEF GH
Horizontal
Down
( 6)
I
J
K
L
m round K L / 2
n round J 2K L / 4
i, o round J K / 2
j, p round I 2 J K / 4
e, k round I J / 2
f , l round Q 2I J / 4
a, g round I Q / 2
76
b, h round I 2Q A / 4
c round Q 2 A B / 4
d round A 2B C / 4
4.2. 16x16
16x16 intrapred_luma_16x16() find_sad_16x16()
. intrapred_luma_16x16() 16x16
, find_sad_16x16() 16x16
, SAD(Sum of Absolute Differences) .
(Low Complexity Mode) (High Complexity Mode) 16x16
. 16x16 intrapred_
luma_16x16() find_sad_16x16() .
intrapred_luma_16x16()
intrapred_luma_16x16() 16x16 .
intrapred_luma() . getNeighbour()
, 4 . 4x4
9 , 16x16
4 . ,
img->mprr_2[ipmode][y][x] .
44 16x16 4
DC_PRED_16
DC
VERT_PRED_16
Vertical
HOR_PRED_16
Horizontal
PLANE_16
Plane
, , getNei
ghbour() . ,
, . up[0]~up[15],
left[1]~left[16], left[0] .
77
DC
, . DC
( 0) , DC . DC
,
16x16 .
- , img-> dc_pred_
value 4x4 .
Horizontal Vertical
, Horizontal ( 1) Vertical ( 2)
. , Vertical (VERT_PRED_16)
(up[0]~up[15])
Horizontal (HOR_PRED_16)
(left[1]~left[16]) . DC , Horizontal Vertical
4x4 , 4x4 .
Plane Plane ( 3) . ,
,
a , b , c . a
b c x y .
,
.
7
h i up 7 x up 7 x 8 up 15 left 0
i 1
8
( 13)
v i left 8 y left 8 y
( 14)
a 16 up 15 left 16
( 15)
i 1
b round 6 5 h
2
c round 6 5 v
2
( 16)
( 17)
pred x, y round 5 a x 7 b y 7 c
2
( 18)
(MAX)
img->max_imgpel_value
img() .
78
( 19)
main()
init_
img->max_imgpel_value = (1<<img->bitdepth_luma) 1
( 20)
, img->bitdepth_luma (BitDepth
Luma) 8 . , img->max_imgpel_value
255 .
26 16x16 Plane
find_sad_16x16()
find_sad_16x16() 16x16
SAD . getNeighbour()
. SAD .
4x4 ,
DC SAD
. SAD .
.
, , getNei
ghbour() . ,
, . intrapred_
luma_16x16() .
SAD
SAD
. ,
.
79
, , 4x4
. , 4 4x4
. , 4x4 DC 1/4 DC
4x4 . ,
SAD .
SAD , SAD
(intra_mode) .
Start
Get Neighboring
16x16 Luma Blocks
getNeighbour()
Is There
Neighbours?
Y
Calculate Differences
Hadamard Transform
of 4x4 Blocks
Hadamard Transform
of DC Coefficients
All Modes
Checked?
N
ipmode++
End
27 find_sad_16x16()
80
Hadamard Transform
of DC Coefficients
Hadamard Transform
of 16x16 Macroblock
28 16x16 SAD
4.3.
IntraChromaPrediction() , 8x8
. 16x16 4
.
45 8x8 4
DC_PRED_8
DC
VERT_PRED_8
Vertical
HOR_PRED_8
Horizontal
PLANE_8
Plane
.
Cb Cr ,
img->mprr_c[uv]
[ipmode][y][x] .
, 4x4 ,
. 4x4
16
2 .
81
IntraChromaPrediction()
.
Start
Get Neighboring
8x8 Luma Blocks
getNeighbour()
Chroma Intra Prediction
DC Prediction
Vertical Prediction
Horizontal Prediction
Plane Prediction
Cb & Cr
Checked?
N
uv++
High Complexity
Mode?
N
N
ipmode++
SATD()
All Modes
Checked?
Y
End
29 IntraChromaPrediction()
82
, , getNei
ghbour() . ,
, . up[0]~up[7],
left[1]~left[8], left[0] .
DC
, . DC
( 0) , DC . DC
.
, img->dc_pred_value .
, 8x8 8x8 4x4
. 8x8 8x8 . H.264/AVC
4:2:0 8x8 4x4
, 8x8 4x4
. 8x8
. .
4x4 DC DC
. DC
. DC , 8x8
4x4
.
.
.
46 8x8 DC
U1
L1
L2
U2
U1
L1
B0 _ AllPixels round U1 L1 / 8
U1
83
U1
U2
U2
L1
U2
L1
L2
U1
U2
L2
B2 _ AllPixels round L2 / 4
L1
L2
L2
U1
U1
U2
U2
L2
B3 _ AllPixels round U 2 L2 / 8
L1
L2
U2
B3 _ AllPixels round L2 / 4
L2
B3 _ AllPixels round U 2 / 4
, U1, U2, L1 L2 4 ,
. ,
.
Horizontal Vertical
, Horizontal ( 1) Vertical ( 2)
. , Vertical (VERT_PRED_8)
(hline[0]~hline[7]) .
Horizontal (HOR_PRED_8)
(vline[0]~vline[7]) . Horizontal Vertical
4x4 , 4x4 .
84
Plane Plane ( 3) ,
, . ,
a , b , c . a b c
x y .
,
.
( 21)
v i vline 4 y vline 4 y
( 22)
a 16 hline 7 vline 7
( 23)
i 1
b round 5 17 h
2
c round 5 17 v
2
( 24)
( 25)
pred x, y round 5 a x 3 b y 3 c
2
( 26)
(MAX)
img->max_imgpel_value
( 27)
main()
init_
img() .
16x16 .
85
5.
(16x16, 16x8, 8x16) (8x8, 8x4, 4x8,
4x4) .
PartitionMotionSearch() list_prediction_cost() . PartitionMoti
onSearch() ,
list_prediction_cost() Cost
. .
5.1.
PartitionMotionSearch()
. , ,
- , , (full-pel) ,
(sub-pel) .
5.1.1.
PartitionMotionSearch()
. , LIST
. LIST ,
BlockMotionSearch() .
LIST
, LIST LIST
86
Start
All Blocks
Checked?
BlockMotionSearch()
N
block++
Y
N
ref++
All References
Checked?
Y
N
list++
All Lists
Checked?
Y
End
30 PartitionMotionSearch()
(Search Range)
. (RestrictSearchRange)
(SearchRange) ,
(RestrictSearchRange==2) 16 (SearchRange==16).
47
if (RestrictSearchRange == 2)
search_range = SearchRange;
else if (RestrictSearchRange == 1)
search_range = SearchRange /
(min(ref,1)+1);
else
search_range = SearchRange / ((min(ref,1)+1) * min(2,blocktype));
(RestrictSearchRange) 2
, 1 , 0
.
87
, BlockMotionSearch()
Cost
. Cost Cost .
BlockMotionSearch()
BlockMotionSearch()
Cost , .
Start
SetMotionVectorPredictor()
Integer-Pel Search
FastFullPelBlockMotionSearch()
Sub-Pel Search
High Complexity
Mode?
SubPelBlockMotionSearch()
N
16x16
in P Slice?
Y
FindSkipModeMotionVector()
End
31 BlockMotionSearch()
orig_pic[y][x] . , SetMotionVectorPredictor()
(motion vector predictor) .
.
88
FullPelBlockMotionSearch()
FastFullPelBlockMotionSearch()
FastIntegerPelBlockMotionSearch()
6
EPZS
simplified_FastIntegerPelBlockMotionSearch()
EPZSPelBlockMotionSearch()
H.264/AVC 1/4
.
6 FIR 1/2 ,
2 1/4 .
, 6 , 6 .
. 5.1.4
.
49
6
6
SubPelBlockMotionSearch()
: SubPelBlockMotionSearch()
: FastSubPelBlockMotionSearch()
16x16 : simplified_FastFullSubPelBlockMotionSearch()
: simplified_FastSubPelBlockMotionSearch()
89
16x16 P SP ,
Cost . FindSkipMode
MotionVector() , Cost GetSkipCostMB()
. 5.1.5 .
cost = GetSkipCostMB(lambda_factor) round(lambda_factor/213)
( 28)
5.1.2.
SetMotionVectorPredictor() .
- , ( A), ( B), (
C) . SetMotionVectorPredictor()
.
Start
getLuma4x4Neighbour()
End
32 SetMotionVectorPredictor()
, getLuma4x4Neighbour()
.
, , . ,
, .
90
mv_b
mv_a
mv_c
pred_vec
Current Block
33
,
, (mvPredType)
. (MVPRED_MEDIAN) ,
.
,
.
( A) , mvPredType = MVPRED_L
( B) , mvPredType = MVPRED_U
( C) , mvPredType = MVPRED_UR
16x8 8x16 ,
. ,
.
8x16 , mvPredType = MVPRED_L
8x16 , mvPredType = MVPRED_UR
16x8 , mvPredType = MVPRED_U
16x8 , mvPredType = MVPRED_L
- ,
, .
,
. (mvPredType) MVPRED_MEDIAN ,
.
pred_vec = mv_a+mv_b+mv_c - min(mv_a, min(mv_b,mv_c))
-max(mv_a, max(mv_b,mv_c))
91
( 29)
MVPRED_L ( A) , MVPRED_U
( B) , MVPRED_UR ( C)
.
5.1.3.
(UseFME)
, (Full Search), 6 (Hexagon Search), 6 (Simplified Hexagon
Search), EPZS (Enhanced Predictive Zonal Search) .
.
5.1.3.1.
(full search) (fast full search) (general full search)
. .
(_FAST_FULL_ME_) , Fast
FullPelBlockMotionSearch() . FastFullPelBlockMotion
Search() .
, SetupFastFullPelSearch()
SAD .
.
SetupFastFullPelSearch() Cost
, (search_center_x, search_center_y)
. (offset_x, offset
_y) . , (img->opix_x, img->opix_y)
.
offset_x = search_center_x[list][ref] - img->opix_x
( 30)
( 31)
.
,
. .
92
Start
Set up
Fast Full Integer Search
SetupFastFullPelSearch()
High Complexity
Mode?
N
Get Cost
for (0,0) Motion Vector
MV_COST()
MV_COST()
All Positions
Checked?
N
pos++
Y
Decide Best Motion Vector
End
34 FastFullPelBlockMotionSearch()
(search_center_x, search_center_y)
(offset_x, offset_y)
Search Range
(img->opix_x, img->opix_y)
Block
35
93
(0,0) Cost
,
. ,
(0,0) , SetupFast
FullPelSearch() pos_00[list][ref] .
Search Range
Offset Center
New Search Center
= Original Center
36 (0,0)
( 32)
( 33)
( 34)
block_sad[pos] = BlockSAD[list][ref][blocktype][block_index][pos]
94
50 mvbits[] 1
i
mvbits[i]
2~3
4~7
8 ~ 15
16 ~ 31
11
32 ~ 63
13
64 ~ 127
15
128 ~ 255
17
256 ~ 511
19
Cost
(0,0) Cost ,
Cost .
mcost = block_sad[pos] + MV_COST
( 35)
( 36)
( 37)
( 38)
( 39)
Cost , Cost
(best_pos) .
.
*mv_x = offset_x + spiral_search_x[best_pos]
( 40)
( 41)
(MaxMvBits) 19 ,
.
SubPelPosNum
4 2 SearchRange
3
MaxMvBits 3 2 ceil log 2 SubPelPosNum 1 e10
, SubPelPosNum . SearchRange
16 . , SubPelPosNum 140, MaxMvBits 19 :
95
FastFullPelBlockMotionSearch()
.
51 FastFullPelBlockMotionSearch()
SetupFastFullPelSearch (ref, list);
offset_x = search_center_x[list][ref] - img->opix_x;
offset_y = search_center_y[list][ref] - img->opix_y;
//===== cost for (0,0)-vector =====
if (!input->rdopt)
{
mcost = block_sad[pos_00[list][ref]]
+ MV_COST (lambda_factor, 2, 0, 0, pred_mv_x, pred_mv_y);
if (mcost < min_mcost)
{
min_mcost = mcost;
best_pos
= pos_00[list][ref];
}
}
//===== loop over all search positions =====
for (pos=0; pos<max_pos; pos++, block_sad++)
{
if (*block_sad < min_mcost)
{
cand_x = offset_x + spiral_search_x[pos];
cand_y = offset_y + spiral_search_y[pos];
mcost
= *block_sad;
= pos;
}
}
}
//===== set best motion vector =====
*mv_x = offset_x + spiral_search_x[best_pos];
*mv_y = offset_y + spiral_search_y[best_pos];
96
SetupFastFullPelSearch()
SetupFastFullPelSearch()
SAD . .
Start
Search Range
inside Image?
SetMotionVectorPredictor()
Y
range_partly_outside = 0
range_partly_outside = 1
All Positions
Checked?
N
pos++
Y
Combine SAD
for Larger Block Types
SetupLargerBlocks()
End
37 SetupFastFullPelSearch()
SetMotionVector
97
52
search_center_x[list][ref] = pmv[0] / 4;
search_center_y[list][ref] = pmv[1] / 4;
if (!input->rdopt)
{
//--- correct center so that (0,0) vector is inside --search_center_x[list][ref] =
max(-SearchRange, min(SearchRange, search_center_x[list][ref]));
search_center_y[list][ref] =
max(-SearchRange, min(SearchRange, search_center_y[list][ref]));
}
search_center_x[list][ref] += img->opix_x;
search_center_y[list][ref] += img->opix_y;
orgptr . ,
range_partly_outside FALSE .
, SAD
. , (abs_x,
abs_y) SAD .
53
max_width = ref_picture->size_x - 17;
max_height = ref_picture->size_y - 17;
if (range_partly_outside)
{
if (abs_y >= 0 && abs_y <= max_height &&
abs_x >= 0 && abs_x <= max_width)
PelYline_11 = FastLine16Y_11;
else
PelYline_11 = UMVLine16Y_11;
}
else
PelYline_11 = FastLine16Y_11
98
PelYline_11 SAD ,
(FastLine16Y_11) (x,y)
. ,
(UMVLine16Y_11) ,
.
Original
Block
38
max_height max_width 17
,
(FastLine16Y_11) . SAD 16x16
,
. , .
Search Range
(max_width - SearchRange,
max_height - SearchRange)
(max_width, max_height)
39 SAD
99
(0,0)
(0,0) .
,
, (0,0)
. (0,0) .
54 (0,0)
if (!input->rdopt)
{
ref_x = img->opix_x - offset_x;
ref_y = img->opix_y - offset_y;
for (pos = 0; pos < max_pos; pos++)
{
if (ref_x == spiral_search_x[pos] &&
ref_y == spiral_search_y[pos])
{
pos_00[list][ref] = pos;
break;
}
}
}
4x4 SAD
SAD .
.
Init_Motion_Search_Module() (spiral_search_x[pos], spi
ral_search_y[pos]) .
12
11
10
9
24
13
2
1
8
23
14
3
0
7
22
15
4
5
6
21
16
17
18
19
20
(a)
15 9 11 13 16
17 3 1 4 18
19 5 0 6 20
21 7 2 8 22
23 10 12 14 24
(b)
40
100
( 42)
y 0 x 0
4x4 SAD
SAD[0]
SAD[1]
SAD[2]
SAD[3]
SAD[4]
SAD[5]
SAD[6]
SAD[7]
blockSAD = SAD[0] + + SAD[7]
41 4x4 SAD
5.1.3.2. 6
5.1.3.3. 6
5.1.3.4. EPZS
101
5.1.4.
(Full Search), 6 (Hexagon
Search), 6 (Simplified Hexagon Search) . EPZS
, .
.
5.1.4.1.
SubPelBlockMotionSearch()
. 1/2 , 1/4
. .
1/2
Padded Picture
Original Picture
FastLine4X
UMVLine4X
Integer-Pel
Motion-compensated block
16
16
42
, .
(IMG_PAD_SIZE) 4 . ,
(x,y)
.
102
, get_line = UMVLine4X
, ( MaxPosX 4 , MaxPosY 4 )
. get_line
SAD ,
(FastLine4X) (x,y)
. , (UMVLine
4X) ,
.
55
y < 0 , ypos = y%4
0 y height4 , ypos = y
y > height4 , ypos = height4 + (y%4)
x < 0 , xpos = x%4
0 x width4 , xpos = x
x > width4 , xpos = width4 + (x%4)
, (xpos,ypos) (x,y) .
43211234
57322375
29511592
01577510
Original
37288273
Block
43
1/2 Cost
( 43)
103
, MV_COST Cost . ,
(pred_mv_x, pred_mv_y) 1/2
. (cand_mv_x, cand_mv_y)
.
MV_COST = (lambda_factor * bits) >> 16
( 44)
( 45)
( 46)
( 47)
( 48)
104
44 SAD
1/4 Cost
1/2
, 1/4 Cost .
UnifiedOneForthPix() 1/2 1/4
2 1/2
.
1,1 / 2
( 49)
5.1.4.2. 6
5.1.4.3. 6
5.1.5.
16x16 P SP
. FindSkipModeMotionVector()
, GetSkipCostMB() Cost .
, FindSkipModeMotionVector()
.
105
Start
Above/Left
Zero Motion?
getLuma4x4Neighbour()
Y
Get Motion Vector
Predictor
Motion Vector = 0
SetMotionVectorPredictor()
End
45 FindSkipModeMotionVector()
, SetMotionVectorPredictor()
. ,
.
SetMotionVectorPredictor() 5.1.2 .
, ( A),
( B), ( C) ,
.
mv_b
mv_a
mv_c
pred_vec
Current Block
46
106
( A) ( B) 0
, 0 .
Cost
, GetSkipCost
i 4x4 ( x, y) org ( x, y) ,
mpr ( x, y) , SAD
.
3
( 50)
y 0 x 0
(UseHadamard==1),
SATD . 4x4 SAD , SAD
Cost .
15
Cost BlockSAD i
( 51)
i 0
5.2.
, PartitionMotionSearch() LIST
. list_prediction_cost() LIST
Cost Cost . LIST0
LIST1 , Cost .
( 52)
:
,
( 53)
Lagrangian Multiplier,
( 54)
107
, encode_one_macroblock() init_enc_
mb_params() .
Ref_Cost 0 .
56 refbits[]
ref
refbits[ref]
1~2
3~6
7 ~ 14
15 ~ 30
108
6. Residual
Residual DCT (Discrete Cosine Transform) (Quantization) . H.264/AVC 4x4
, 16x16 DC (Hadamard
Transform) . DCT
, . CAVLC
CABAC , CAVLC .
CABAC .
Residual DCT
.
6.1.
6.1.1.
dct_
luma() dct_luma_16x16() . 4x4
dct_luma() , 16x16 DC
dct_luma_16x16() .
LumaResidualCoding()
, dct_luma() .
- 4x4
dct_luma() 16x16 dct_luma_16x16()
.
6.1.1.1. 4x4
4x4 dct_luma() . DCT,
, , IDCT . 4x4
, dct_luma()
.
dct_luma() DCT, , , IDCT
.
109
Start
DCT
Quantization
Inverse Quantization
IDCT
Reconstruction
End
47 dct_luma()
X ij
(i, j ) , Yxy Y ( x, y)
2 / N .
. Ci i 0 1/ N , 0
N 1 N 1
2 j 1 y cos 2i 1 x
i 0 j 0
2N
2N
( 55)
Y = AXAT ,
Aij Ci cos
2 j 1 i
( 56)
2N
4x4 DCT .
110
a
a a b
a
c
a a
b c c b a c a b
X
Y = AXA T
a a a a a c a b
c b b c a b a c
,
1
1
1
3
, b
cos
cos , c
2
2
2
8
8
( 57)
a b .
Y = CXCT
1
E
1
d
1
1
1 1 1
1 d a2
d d 1 1 d 1 1 ab
X
1 1 1 1 d 1 1 a 2
1 1 d 1 1 1 d ab
a2
ab
a2
ab
ab
b2
ab
b2
ab
b2
ab
b 2
( 58)
, C
d .
c
0.414 0.5
b
( 59)
a b .
2
1
1
, b
, d
5
2
2
Y = C f XCTf
( 60)
C E .
1 1 1 1 1
2 1 1 2 1
Ef
X
1 1 1 1 1
1 2 2 1 1
2
a
2 1 1 ab
1 1 2 2
1 1 2 2
a
2 1 1
ab
2
ab
2
b2
4
ab
2
b2
4
a2
ab
2
a2
ab
2
( 61)
111
ab
2
b2
4
ab
2
b2
4
, .
Y d
.
C f XCTf JM . , img->m7[y][x]
4x4 DCT m4[y][x] .
57 DCT
//Horizontal transform
for (j=0; j < BLOCK_SIZE; j++)
{
m5[0] = img->m7[j][0]+img->m7[j][3];
m5[1] = img->m7[j][1]+img->m7[j][2];
m5[2] = img->m7[j][1]-img->m7[j][2];
m5[3] = img->m7[j][0]-img->m7[j][3];
m4[j][0] = m5[0] + m5[1];
m4[j][2] = m5[0] - m5[1];
m4[j][1] = m5[3]*2 + m5[2];
m4[j][3] = m5[3] - m5[2]*2;
}
//Vertical transform
for (i=0; i < BLOCK_SIZE; i++)
{
m5[0] = m4[0][i] + m4[3][i];
m5[1] = m4[1][i] + m4[2][i];
m5[2] = m4[1][i] - m4[2][i];
m5[3] = m4[0][i] - m4[3][i];
m4[0][i] = m5[0] + m5[1];
m4[2][i] = m5[0] - m5[1];
m4[1][i] = m5[3]*2 + m5[2];
m4[3][i] = m5[3] - m5[2]*2;
}
H.264/AVC
E f Ei . .
Yij
Zij round
Qstep
( 62)
112
Z ij . 52 Qstep ,
,
QP .
E f , . ,
PFij Wij post- E f (i, j ) .
PFij
Qstep
( 63)
,
.
MFij
( 64)
MFij
2
qbits
PFij
( 65)
Qstep
qbits 15 floor QP / 6
( 66)
. % .
1 3 1 0 7 8 0 6 6 1 3 1 0 7 8 0 6 6 1 1 9 1 6 7 4 9 0 11 9 1 26 67 54 59 40 110000882 6 5 5 4
8 0 6 6 5 2 4 3 8 0 6 6 5 2 4 3 7 4 9 0 4 6 6 0 7 4 9 0 4 6 6 0 66555544 44119944
1 3 1 0 7 8 0 6 6 1 3 1 0 7 8 0 6 6 1 1 9 1 6 7 4 9 0 11 9 1 26 67554
4 9 010082
1 0 0 6554
8
8066 5243 8066 5243 7490 4660 7490 4660 6554 4194 6554 4194
QP%6 = 0
9362
5825
9362
5825
5825
3647
5825
3647
9362
5825
9362
5825
QP%6 = 3
QP%6 = 1
5825 8192
3647 5243
5825 8192
3647 5243
5243
3355
5243
3355
8192
5243
8192
5243
QP%6 = 2
5243 7282
3355 4559
5243 7282
3355 4559
QP%6 = 4
48 QP MF
113
4559
2893
4559
2893
7282
4559
7282
4559
QP%6 = 5
4559
2893
4559
2893
qbits
Z ij . , f
/ 3 , 2qbits / 6 .
( 67)
( 68)
( 69)
JM
. , currMB->qp
leveloffset[i][j]
QP , levelscale[i][j] MFij ,
f .
58
qp_per = (currMB->qp)/6;
qp_rem = (currMB->qp)%6;
q_bits
= Q_BITS+qp_per;
levelscale
= LevelScale4x4Luma[intra][qp_rem];
leveloffset
= LevelOffset4x4Luma[intra][qp_per];
run=-1;
scan_pos=0;
run=-1;
level=sign(level, m4[j][i]);
}
}
114
, LevelScale4x4Luma[][] code_a_picture()
CalculateQuantParam() .
LevelScale4x4Luma[][]
quant_coef[][][] ,
MFij .
49 4x4
, img->cofAC Run-Level
. img->cofAC[b8][b4][0] Level , img->cofAC[b8][b4][1]
Run .
Z ij DCT Yij
, .
( 70)
,
. pre-
Ei .
( 71)
Vij QP .
115
10
13
10
13
13
16
13
16
13 11
16 14
13 11
16 14
10
13
10
13
QP%6 = 0
14
18
14
18
18
23
18
23
11
14
11
14
14 13
18 16
14 13
18 16
QP%6 = 1
18 16
23 20
18 16
23 20
14
18
14
18
14
18
14
18
QP%6 = 3
20
25
20
25
16
20
16
20
16
20
16
20
13
16
13
16
16
20
16
20
QP%6 = 2
20 18
25 23
20 18
25 23
QP%6 = 4
23
29
23
29
18
23
18
23
23
29
23
29
QP%6 = 5
50 QP V
JM
.
59
qp_per = (currMB->qp)/6;
qp_rem = (currMB->qp)%6;
scan_pos=0;
invlevelscale = InvLevelScale4x4Luma[intra][qp_rem];
for (coeff_ctr=0;coeff_ctr < 16;coeff_ctr++)
{
i=pos_scan[coeff_ctr][0];
j=pos_scan[coeff_ctr][1];
++scan_pos;
level = img->cofAC[b8][b4][0][scan_pos];
if (level != 0)
ilev=((((level*invlevelscale[i][j])<< qp_per) + 8 ) >> 4);
m4[j][i]=ilev;
}
, level
invlevelscale[i][j]
Z ij , invlevelscale[i][j] Vij . ,
InvLevelScale4x4Luma[][]
code
_a_picture() CalculateQuantParam() ,
Vij dequant_coef[][][]
. m4[y][x] .
116
IDCT
, (IDCT) H.264/AVC . ,
pre-
Ei , C C 1/ 2
T
.
. ,
T-1 T X = X .
1 1
1 1
2
X = CiT Y Ei Ci
1 1
2
1 1
1
2
a2
1 1
ab
Y
a2
1 1
ab
1
1
2
1
ab a ab
1
b 2 ab b 2
ab a 2 ab 1
b 2 ab b 2 1
2
2
1
1
1
1
2
2
1 1
1
1
2
( 72)
Ei , CiT WCi
T
. Ci WCi JM
pre-
.
60 IDCT
//horizontal transform
for (j=0; j < BLOCK_SIZE; j++)
{
m6[0]=(m4[j][0] + m4[j][2]);
m6[1]=(m4[j][0] - m4[j][2]);
m6[2]=(m4[j][1]>>1) - m4[j][3];
m6[3]= m4[j][1] + (m4[j][3]>>1);
m4[j][0] = m6[0] + m6[3];
m4[j][1] = m6[1] + m6[2];
m4[j][2] = m6[1] - m6[2];
m4[j][3] = m6[0] - m6[3];
}
//vertical transform
for (i=0; i < BLOCK_SIZE; i++)
{
m6[0]=(m4[0][i] + m4[2][i]);
m6[1]=(m4[0][i] - m4[2][i]);
m6[2]=(m4[1][i]>>1) - m4[3][i];
m6[3]= m4[1][i] + (m4[3][i]>>1);
117
IDCT ,
. , img->m7[y][x] , img->mpr
[y][x] , enc_picture->imgY[y][x]
.
61
for (j=0; j < BLOCK_SIZE; j++)
{
pix_y = img->pix_y + block_y + j;
pix_x = img->pix_x + block_x;
for (i=0; i < BLOCK_SIZE; i++)
enc_picture->imgY[pix_y][pix_x + i]=
min(img->max_imgpel_value, max(0, (img->m7[j][i]+
(img->mpr[j + block_y][i + block_x]<<6)+32)>>6) );
}
6.1.1.2. 16x16
16x16 dct_luma_16x16() .
DCT, , , IDCT , DC
. 16x16
, dct_luma_16x16() . dct_
luma_16x16() .
DCT
, 16x16 ,
DCT .
post-
E f C f XCTf .
118
Start
Get Displaced
Frame Differences
DCT
DC Coefficients
DC Hadamard Transform
DC Quantization
DC Inverse
Hadamard Transform
DC Inverse Quantization
AC Coefficients
AC Quantization
AC Inverse Quantization
IDCT
Reconstruction
End
51 dct_luma_16x16()
DC
. 4x4 . ,
WD 4x4 DC , YD .
119
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
YD
WD
/ 2
1
1
1
1
1
1 1 1 1
1
1
1
( 73)
DC JM . , 16
DC M4[j][i] .
62 DC
for (j=0;j<4;j++)
{
for (i=0;i<4;i++)
M4[j][i]= M0[j][i][0][0];
}
//horizontal transform
for (j=0;j<4;j++)
{
M5[0] = M4[j][0]+M4[j][3];
M5[1] = M4[j][1]+M4[j][2];
M5[2] = M4[j][1]-M4[j][2];
M5[3] = M4[j][0]-M4[j][3];
M4[j][0] = M5[0]+M5[1];
M4[j][2] = M5[0]-M5[1];
M4[j][1] = M5[3]+M5[2];
M4[j][3] = M5[3]-M5[2];
}
//vertical transform
for (i=0;i<4;i++)
{
M5[0] = M4[0][i]+M4[3][i];
M5[1] = M4[1][i]+M4[2][i];
M5[2] = M4[1][i]-M4[2][i];
M5[3] = M4[0][i]-M4[3][i];
M4[0][i]=(M5[0]+M5[1])>>1;
M4[2][i]=(M5[0]-M5[1])>>1;
M4[1][i]=(M5[3]+M5[2])>>1;
M4[3][i]=(M5[3]-M5[2])>>1;
}
120
DC DC YD
( 74)
( 75)
DC JM .
63 DC
run=-1;
scan_pos=0;
for (coeff_ctr=0;coeff_ctr<16;coeff_ctr++)
{
i=pos_scan[coeff_ctr][0];
j=pos_scan[coeff_ctr][1];
run++;
level= (absm(M4[j][i]) * levelscale[0][0]
+ (leveloffset[0][0]<<1)) >> (q_bits+1);
if (level != 0)
{
img->cofDC[0][0][scan_pos] = sign(level,M4[j][i]);
img->cofDC[0][1][scan_pos] = run;
++scan_pos;
run=-1;
}
M4[j][i]=sign(level,M4[j][i]);
}
, pos_scan[] 4x4
4x4 . , DCLevel[scan_pos]
DCRun[scan_pos] .
DC , img->cofDC
Run-Level . img->cofDC[0][0] Level , img->cofDC[0][1]
Run .
121
DC 4x4 DC ,
. DC
. ,
WQD
Z D
WQD DC .
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
ZD
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
( 76)
DC M4[j][i] M0[j][i][0]
[0] .
DC DC ,
. ,
(0, 0) .
( QP / 6 6 )
( 77)
( QP / 6 6 )
( 78)
64 DC
for (i=0 ; i<4 ; i++)
{
if(qp_per<6) {
M0[0][i][0][0] =
(M0[0][i][0][0]*invlevelscale[0][0]+(1<<(5-qp_per)))>>(6-qp_per);
M0[1][i][0][0] =
(M0[1][i][0][0]*invlevelscale[0][0]+(1<<(5-qp_per)))>>(6-qp_per);
M0[2][i][0][0] =
(M0[2][i][0][0]*invlevelscale[0][0]+(1<<(5-qp_per)))>>(6-qp_per);
M0[3][i][0][0] =
(M0[3][i][0][0]*invlevelscale[0][0]+(1<<(5-qp_per)))>>(6-qp_per);
}
else {
M0[0][i][0][0] = (M0[0][i][0][0]*invlevelscale[0][0])<<(qp_per-6);
M0[1][i][0][0] = (M0[1][i][0][0]*invlevelscale[0][0])<<(qp_per-6);
M0[2][i][0][0] = (M0[2][i][0][0]*invlevelscale[0][0])<<(qp_per-6);
M0[3][i][0][0] = (M0[3][i][0][0]*invlevelscale[0][0])<<(qp_per-6);
}
}
122
AC
DC , AC
. AC 16x16 4x4 ,
4x4 .
, img->cofAC Run-Level .
, 16x16 AC 4x4
.
( QP / 6 4 )
( 79)
( QP / 6 4 )
( 80)
65 16x16 AC
qp_per = qp_per_matrix[currMB->qp] = currMB->qp / 6;
qp_rem = qp_rem_matrix[currMB->qp] = currMB->qp % 6;
invlevelscale = InvLevelScale4x4Luma[1][qp_rem];
for (jj=0;jj<4;jj++)
{
for (ii=0;ii<4;ii++)
{
scan_pos =
0;
123
Ei , CiT WCi
. M1[j*4 + y][i*4 + x]
.
IDCT ,
. , M1[j][i] , img->mprr_2
[mode][y][x] , enc_picture->imgY[y][x]
.
66 16x16
for (j=0;j<16;j++)
{
for (i=0;i<16;i++)
{
enc_picture->imgY[img->pix_y + j][img->pix_x + i] =
clip1a((M1[j][i]+(img->mprr_2[new_intra_mode][j][i]<<6)+32)>>6);
}
}
6.1.2.
8x8 dct_chroma()
. DCT, , , IDCT
. , 2x2 DC
.
dct_chroma()
.
124
Start
DCT
DC Coefficients
DC Hadamard Transform
DC Quantization
DC Inverse
Hadamard Transform
DC Inverse Quantization
AC Coefficients
AC Quantization
AC Inverse Quantization
IDCT
Reconstruction
End
52 dct_chroma()
DC
, 4 4x4 DC ,
WD , YD ,
m1[] .
1 1
1 1
YD
WD
1 1
1 1
( 81)
125
67 DC
m1[0]=(img->m7[0][0] + img->m7[0][4] + img->m7[4][0] + img->m7[4][4]);
m1[1]=(img->m7[0][0] - img->m7[0][4] + img->m7[4][0] - img->m7[4][4]);
m1[2]=(img->m7[0][0] + img->m7[0][4] - img->m7[4][0] - img->m7[4][4]);
m1[3]=(img->m7[0][0] - img->m7[0][4] - img->m7[4][0] + img->m7[4][4]);
DC
DC ,
. ,
MF00
quant_coef[][][]
(0, 0) . f
qbits
2
/ 3 , 2qbits / 6 . qbits
QP qbits 15 floor QP / 6 .
( 82)
( 83)
img->chroma_qp_offset[uv]
init_img()
QP (CbQPOffset CrQPOffset)
. , QP 0 .
QP 0~51 , QP_SCALE_CR[] .
LevelScale4x4Chroma[][] code_a_picture() Calculate
QuantParam() . LevelScale4x4Chroma [][] quant_coef
[][][] ,
MFij . LevelOffset
4x4Chroma[][]
code_a_picture()
CalculateOffset
Param[][] .
, img->cofDC Run-Level
.
img->cofDC[uv+1][0] Level ,
img->cofDC[uv+1][1]
Run . , 2x2 DC .
53 2x2 DC
126
68 DC
const byte QP_SCALE_CR[52]=
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,
12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
28,29,29,30,31,32,32,33,34,34,35,35,36,36,37,37,
37,38,38,38,39,39,39,39
};
qp_c = currMB->qp + img->chroma_qp_offset[uv];
qp_c = Clip3(0,51,qp_c);
qp_c = (qp_c < 0)? qp_c : QP_SCALE_CR[qp_c];
qp_per = qp_per_matrix[qp_c] = qp_c / 6;
qp_rem = qp_rem_matrix[qp_c] = qp_c % 6;
q_bits
= 15 + qp_per;
levelscale = LevelScale4x4Chroma[uv][intra];
leveloffset = LevelOffset4x4Chroma[uv][intra];
run=-1;
scan_pos=0;
run=-1;
ilev=sign(level, m1[coeff_ctr]);
}
m1[coeff_ctr]=ilev;
}
DC
, DC
, . DC
.
1 1 1 1
WQD
ZD
1 1 1 1
( 84)
127
, . ,
0, 0
( QP / 6 5 )
( 85)
( QP / 6 5 )
( 86)
69 DC
m5[0]=(m1[0] + m1[1] + m1[2] + m1[3]);
m5[1]=(m1[0] - m1[1] + m1[2] - m1[3]);
m5[2]=(m1[0] + m1[1] - m1[2] - m1[3]);
m5[3]=(m1[0] - m1[1] - m1[2] + m1[3]);
if(qp_per<5)
{
for(i=0; i<4; i++)
m1[i]=(m5[i] * invlevelscale[qp_rem][0][0])>>(5-qp_per);
}
else
{
for(i=0; i<4; i++)
m1[i]=(m5[i] * invlevelscale[qp_rem][0][0])<<(qp_per-5);
}
img->m7[0][0] = m1[0];
img->m7[0][4] = m1[1];
img->m7[4][0] = m1[2];
img->m7[4][4] = m1[3];
, DC m1[]
, 8x8 (img->m7[y][x]) DC .
AC
AC 8x8
4x4 , 16x16
. 4x4 AC 4x4 AC
. , img->cofAC Run-Level
. , img->cofAC[4+b8+uv_scale][b4][0][scan_pos] Level , img
->cofAC[4+b8+uv_scale][b4][1][scan_pos] Run .
AC 16x16
.
128
( QP / 6 4 )
( 87)
( QP / 6 4 )
( 88)
AC JM .
70 AC
coeff_cost=0;
cr_cbp_tmp=0;
scan_pos=0;
!= 0)
{
cr_cbp_tmp=2;
img->cofAC[4+b8+uv_scale][b4][0][scan_pos] =
sign(level,img->m7[j][i]);
img->cofAC[4+b8+uv_scale][b4][1][scan_pos] = run;
++scan_pos;
run=-1;
//Dequantization
level=sign(level, img->m7[j][i]);
if(qp_per<4)
ilev=(level*invlevelscale[qp_rem][i][j]
+(1<<(3-qp_per)))>>(4-qp_per);
else
ilev=(level*invlevelscale[qp_rem][i][j])<<(qp_per-4);
}
img->m7[j][i]=ilev;
}
ACLevel[scan_pos] = 0;
}
}
129
IDCT
DC AC
Ei
CiT WCi .
img->m7[y][x] .
IDCT ,
. , img->m7[y][x] ,
img->mpr[y][x] , enc_picture->imgUV[uv][y]
[x] .
71
for (j=0; j < 8; j++)
{
for (i=0; i < 8; i++)
{
enc_picture->imgUV[uv][img->pix_c_y + j][img->pix_c_x + i] =
min(255,max(0,(img->m7[j][i]+(img->mpr[j][i]<<6)+32)>>6));
}
}
130
7.
(entropy)
. ,
, - , . ,
. ,
.
H.264/AVC CAVLC(Context-based Adaptive Variable Length Coding) CABAC(Contextbased Adaptive Binary Arithmetic Coding) .
CAVLC CABAC (context)
, CAVLC (Variable Length Coding) CABAC
(Arithmetic Coding). , CABAC (main profile)
. CABAC
.
CAVLC CABAC
.
7.1. CAVLC
CAVLC ,
. ,
(Exponential Bolomb Coding) .
CAVLC , (CBP), ,
(residual data) ,
.
7.1.1.
CAVLC (VLC, Variable Length Coding)
. .
x
m 1 xm 2 x0
( 89)
131
symbol2vlc() VLC
, . ,
(sym->len) (sym->inf) ,
.
72 symbol2vlc()
int symbol2vlc(SyntaxElement *sym)
{
int info_len = sym->len;
sym->bitpattern = 0;
while(--info_len >= 0)
{
sym->bitpattern <<= 1;
sym->bitpattern |= (0x01 & (sym->inf >> info_len));
}
}
7.1.2.
(Exponential Golomb Code) .
00 01 xn1 xn2 x0
n zeros
( 90)
Info
[ xn1 xn2 x0 ] , n - .
m 2n 1 . ,
( CodeNum ) ,
n Info .
( 91)
Info CodeNum 1 2
( 92)
132
73
ue
ue_linfo()
te
se
se_linfo()
, delta QP
me
cbp_linfo_intra()
cbp_linfo_inter()
CBP
.
.
m Info , symbol2uvlc()
.
74
int symbol2uvlc(SyntaxElement *sym)
{
int suffix_len=sym->len/2;
sym->bitpattern = (1<<suffix_len)|(sym->inf&((1<<suffix_len)-1));
return 0;
}
, sym->len
(codeword) .
. ,
0
m Info ,
( CodeNum ) .
133
75
code_num
Codeword
010
011
00100
00101
00110
00111
0001000
0001001
0001010
7.1.2.1.
(Unsigned Exponential Golomb Code) ue_linfo()
. ( CodeNum )
.
CodeNum k
( 93)
76
CodeNum
. ,
ue,
134
77
int i,nn;
nn=(ue+1)/2;
for (i=0; i < 16 && nn != 0; i++)
nn /= 2;
*len= 2*i + 1;
*info=ue+1-(int)pow(2,i);
7.1.2.2.
(Signed Exponential Golomb Code) se_linfo()
. ( CodeNum )
CodeNum 2 k
CodeNum 2 k 1
(k
0)
(k
( 94)
0)
( 95)
78
CodeNum
-1
-2
6
k
-3
k 1
ceil k / 2
. ,
se,
135
79
int i,n,sign,nn;
sign=0;
if (se <= 0)
sign=1;
n=absm(se) << 1;
nn=n/2;
for (i=0; i < 16 && nn != 0; i++)
nn /= 2;
*len=i*2 + 1;
*info=n - (int)pow(2,i) + sign;
7.1.3.
(Macroblock Type) writeMBLayer() .
(ue_linfo)
. .
Start
I Slice?
Skip Mode?
Y
ue_linfo()
Write MB Type
P8x8?
N
Y
Write Block Type
for Sub-MB
ue_linfo()
End
54 writeMBLayer()
136
, I ,
(macroblcok type) .
80 I
currSE->value1
= MBType2Value (currMB);
currSE->value2
= 0;
currSE->type
= SE_MBTYPE;
currSE->mapping = ue_linfo;
dataPart->writeSyntaxElement( currSE, dataPart);
no_bits += currSE->len;
, MBType2Value() (currMB->mb_
type) . ,
SE_MBTYPE . , (input->symbol_mode)
CAVLC ue_linfo()
(Unsigned Exponential Golomb Coding) . ue_linfo()
.
dataPart->writeSyntaxElement ,
CAVLC init_slice() writeSyntaxElement_
UVLC() .
writeSyntaxElement_UVLC() . ,
(ue_linfo) (se_linfo) (se->mapp
ing) , symbol2uvlc() (Exponential Golomb
Coding) . writeUVLC2buffer()
. symbol2
uvlc() .
81 CAVLC
se->mapping(se->value1,se->value2,&(se->len),&(se->inf));
symbol2uvlc(se);
writeUVLC2buffer(se, this_dataPart->bitstream);
, (no_bits)
. .
137
I ,
= img->cod_counter;
currSE->value2
= 0;
currSE->mapping = ue_linfo;
currSE->type = SE_MBTYPE;
dataPart->writeSyntaxElement( currSE, dataPart);
no_bits += currSE->len;
img->cod_counter = 0;
currSE->value1 = MBType2Value (currMB);
currSE->value1--;
currSE->mapping = ue_linfo;
currSE->type = SE_MBTYPE;
currSE->value2 = 0;
dataPart->writeSyntaxElement(currSE, dataPart);
no_bits += currSE->len;
I ,
FmoGetNextMBNr()
-1 .
138
83 CAVLC
img->cod_counter++;
if(FmoGetNextMBNr(img->current_mb_nr) == -1 && img->cod_counter>0)
{
currSE->value1
= img->cod_counter;
currSE->value2
= 0;
currSE->mapping = ue_linfo;
currSE->type = SE_MBTYPE;
dataPart->writeSyntaxElement(currSE, dataPart);
no_bits += currSE->len;
img->cod_counter = 0;
}
(P8x8),
.
84
for (i=0; i<4; i++)
{
currSE->mapping = ue_linfo;
currSE->value1 = B8Mode2Value(currMB->b8mode[i],currMB->b8pdir[i]);
currSE->value2 = 0;
currSE->type = SE_MBTYPE;
dataPart->writeSyntaxElement (currSE, dataPart);
no_bits += currSE->len;
}
, B8Mode2Value() 8x8
(currMB->b8mode[]) . B ,
.
AssignedValue B8Mode 4
( 96)
, 8x8 (ue_linfo)
.
139
7.1.4.
(CBP, Coded Block Pattern) (Mapped
Exponential Golomb Coding) . ,
4x4 .
4x4 writeCBPandLumaCoeff() cbp_
linfo_intra() , cbp_linfo_inter()
.
.
85
if ((currMB->mb_type!=I16MB) && (currMB->mb_type!=IPCM))
{
currSE->value1 = cbp;
if (currMB->mb_type == I4MB || currMB->mb_type == SI4MB)
{
currSE->mapping = cbp_linfo_intra;
currSE->type = SE_CBP_INTRA;
}
else
{
currSE->mapping = cbp_linfo_inter;
currSE->type = SE_CBP_INTER;
}
dataPart->writeSyntaxElement(currSE, dataPart);
}
cbp_linfo_intra() cbp_linfo_inter()
. ,
cbp,
140
86 CBP
void cbp_linfo_intra(int cbp, int dummy, int *len,int *info)
{
extern const unsigned char NCBP[2][48][2];
ue_linfo(NCBP[1][cbp][0], dummy, len, info);
}
void cbp_linfo_inter(int cbp, int dummy, int *len,int *info)
{
extern const unsigned char NCBP[2][48][2];
ue_linfo(NCBP[1][cbp][1], dummy, len, info);
}
, (CBP) ( CodeNum )
NCBP[][][] .
87
CBP
CodeNum
4x4
47
31
16
15
23
27
29
32
30
11
10
10
13
12
11
14
15
12
39
47
13
43
14
45
11
15
46
13
16
16
14
141
17
18
19
10
31
20
12
35
21
19
37
22
21
42
23
26
44
24
28
33
25
35
34
26
37
36
27
42
40
28
44
39
29
43
30
45
31
46
32
17
33
17
18
34
18
20
35
20
24
36
24
19
37
21
38
26
39
22
28
40
25
23
41
32
27
42
33
29
43
34
30
44
36
22
45
40
25
46
38
38
47
41
41
142
7.1.5.
writeIntra4x4Modes() ,
writeChromaIntraPredMode() .
.
4x4
, 4x4 ,
= ipred_array[i];
currSE->value2
= 0;
currSE->type = SE_INTRAPREDMODE;
writeSyntaxElement_Intra4x4PredictionMode(currSE, dataPart);
}
, writeSyntaxElement_Intra4x4PredictionMode()
4x4 . , ,
m 4, Info .
89 4x4
if (currSE->value1 == -1)
{
currSE->len = 1;
currSE->inf = 1;
}
else
{
currSE->len = 4;
currSE->inf = currSE->value1;
}
currSE->bitpattern = currSE->inf;
writeUVLC2buffer(currSE, dataPart->bitstream);
143
writeChroma
IntraPredMode() . ,
(currMB->c_ipred_mode) (ue_linfo)
.
90
currSE->mapping = ue_linfo;
currSE->value1 = currMB->c_ipred_mode;
currSE->value2 = 0;
currSE->type = SE_INTRAPREDMODE;
dataPart->writeSyntaxElement (currSE, dataPart);
7.1.6.
(Quantization Parameter) writeCBPandLumaCoeff()
. , (currMB->delta_qp)
(se_linfo) ( CodeNum ) . ,
SE_DELTA_QUANT_INTRA,
SE_DELTA_QUANT_INTER .
91
if (cbp!=0 || (currMB->mb_type==I16MB) || (currMB->mb_type!=IPCM))
{
currSE->value1 = currMB->delta_qp;
currSE->mapping = se_linfo;
if(currMB->mb_type!=I4MB
&& currMB->mb_type!=I16MB
&& currMB->mb_type!=IPCM)
currSE->type = SE_DELTA_QUANT_INTER;
else
currSE->type = SE_DELTA_QUANT_INTRA;
dataPart->writeSyntaxElement(currSE, dataPart);
}
144
7.1.7.
(Motion Information)
, writeMotionInfo2NAL() writeRefer
enceFrame() . , writeMotionVector
8x8() .
, writeReferenceFrame()
, .
currMB->b8mode[k], enc_picture->ref_idx[LIST_
0][y][x] .
92
currSE->value1 = ref;
currSE->value2
currSE->type
= 0;
= SE_REFFRAME;
num_ref = listXsize[LIST_0]
if(num_ref == 1)
rate = 0;
if(num_ref != 1)
{
if(num_ref == 2)
{
currSE->bitpattern = 1 - currSE->value1;
currSE->len = 1;
writeSyntaxElement2Buf_Fixed(currSE, dataPart->bitstream);
}
else
{
currSE->mapping = ue_linfo;
dataPart->writeSyntaxElement (currSE, dataPart);
}
rate += currSE->len;
}
, listXsize[] . ,
, . ,
2 , writeSyntaxElement2Buf_Fixed() 1
145
. 1 3 ,
(ue_linfo) .
, writeMotionVector8x8() ,
.
, (all_mv)
(pred_mv) . .
, (se_linfo)
( CodeNum ) . SE_MVD .
93
curr_mvd = all_mv[j][i][list_idx][refindex][mv_mode][k]
- pred_mv[j][i][list_idx][refindex][mv_mode][k];
currSE->value1 = curr_mvd;
currSE->value2 = 0;
currSE->type = SE_MVD;
currSE->mapping = se_linfo;
dataPart->writeSyntaxElement (currSE, dataPart);
7.1.8.
(Residual Data) writeCBP
andLumaCoeff() writeLumaCoeff8x8() ,
writeCoeff4x4_CAVLC() .
4x4 . ,
8x8 writeLumaCoeff8x8() , 8x8
4x4 writeCoeff4x4_CAVLC() .
, writeCoeff4x4_CAVLC() 4x4
.
, writeCoeff4x4_CAVLC()
, .
writeSyntaxElement2Buf_Fixed() writeUVLC2buffer()
.
146
writeCoeff4x4_CAVLC()
writeCoeff4x4_CAVLC() 4x4 (Residual Data)
. .
Start
Set CAVLC
of 4x4 Coefficients
Calculate
NumCoeff & TrailingOnes
predict_nnz()
predict_nnz_chroma()
Write
NumCoeff & TrailingOnes
NumCoeff == 0
writeSyntaxElement_NumCoeffTrailingOnes()
writeSyntaxElement_NumCoeffTrailingOnesChromaDC()
N
Write Signs
of Trailing Ones
Write Levels
of Non-zero Coefficients
writeSyntaxElement_VLC()
writeSyntaxElement_Level_VLC1()
writeSyntaxElement_Level_VLCN()
writeSyntaxElement_TotalZeros()
writeSyntaxElement_TotalZerosChromaDC()
Write Run
before Each Coefficient
writeSyntaxElement_Run()
End
55 writeCoeff4x4_CAVLC()
147
, ,
. 5- , 4x4
(LUMA), 16x16 DC (LUMA_INTRA16x16DC), 16x16 AC
(LUMA_INTRA16x16AC), DC (CHROMA_DC), AC
(CHROMA_AC) .
, (max_coeff_num) 0 (numcoeff)
. , (bitcounttype)
. BITS_COEFF_Y_MB
, BITS_COEFF_UV_MB
.
(pLevel) (pRun)
img->cofDC img->cofAC
. (dptype) DC AC ,
, .
94
max_coeff_num
bitcounttype
pLevel
pRun
dptype
LUMA
16
BITS_COEFF_
Y_MB
img->cofAC
[b8][b4][0]
img->cofAC
[b8][b4][1]
:
SE_LUM_AC_INTRA
:
SE_LUM_AC_INTER
LUMA_INTRA
16x16DC
16
BITS_COEFF_
Y_MB
img->cofDC
[0][0]
img->cofDC
[0][1]
SE_LUM_DC_INTRA
LUMA_INTRA
16x16AC
15
BITS_COEFF_
Y_MB
img->cofAC
[b8][b4][0]
img->cofAC
[b8][b4][1]
SE_LUM_AC_INTRA
CHROMA_DC
4
(cdc=1)
BITS_COEFF_
UV_MB
img->cofDC
[param+1][0]
img->cofDC
[param+1][1]
:
SE_CHR_DC_INTRA
:
SE_CHR_DC_INTER
CHROMA_AC
15
(cac=1)
BITS_COEFF_
UV_MB
img->cofAC
[b8][b4][0]
img->cofAC
[b8][b4][1]
:
SE_CHR_AC_INTRA
:
SE_CHR_AC_INTER
148
1 . Trailing One 3
// clip to 3
}
else
numtrailingones = 0;
numcoeff ++;
lastcoeff = k;
}
}
0 (numcoeff) Trailing
149
, (subblock_x, subblock_y) ,
(CHROMA_DC CHROMA_AC) predict_nnz() 0
(nnz) . AC , predict
_nnz_chroma() 0 (nnz) .
96 0
if (!cdc)
{
if (!cac)
{
//luma
(...Omitted)
nnz = predict_nnz(subblock_x,subblock_y);
}
else
{
//chroma AC
(...Omitted)
nnz = predict_nnz_chroma(subblock_x,subblock_y);
}
img->nz_coeff[img->current_mb_nr][subblock_x][subblock_y]
= numcoeff;
}
predict_nnz() , getLuma4x4Neighbour()
.
97 0
nnz round nU nL / 2
nnz nL
nnz nU
nnz 0
150
, 0
nL , 0
nU . predict_nnz_chroma() getChroma4x4Neighbour()
0
.
DC 0
, 0 (numcoeff) img->nz_coeff .
ingones) , DC
(numcoeff_vlc) .
98 0 Trailing Ones
0
(nnz)
(numcoeff_vlc)
DC
0, 1
2, 3
4, 5, 6, 7
0 Trailing Ones
, 0 (num
= dptype;
currSE->value1 = numcoeff;
currSE->value2 = numtrailingones;
currSE->len = numcoeff_vlc;
if(!cdc)
writeSyntaxElement_NumCoeffTrailingOnes(currSE, dataPart);
else
writeSyntaxElement_NumCoeffTrailingOnesChromaDC(currSE, dataPart);
151
writeSyntaxElement_NumCoeffTrailingOnes()
. , 0 (numcoeff) Trailing Ones (numtrailing
ones) lentab[][][] (currSE->len) , cod
tab[][][] (currSE->inf) .
Number of
Non-Zero Coefficients
(numcoeff)
Length Table
(lentab[][][])
Code Length
(currSE->len)
Number of
Trailing Ones
(numtrailingones)
Information Table
(codtab[][][])
Code Information
(currSE->inf)
56 0 Trailing Ones
lentab[][][] codtab[][][] .
(numcoeff_vlc) 2 , 0 Trailing Ones
. 3 - , 0
(nnz) 8 (currSE->len)
6 , 0 (currSE->inf) 3,
((numcoeff-1)<<2)|numtrailingones) .
, symbol2vlc()
VLC , writeUVLC2buffer()
.
152
100 writeSyntaxElement_NumCoeffTrailingOnes()
static const int lentab[3][4][17] =
{
{
{ 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
{ 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
{ 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
{ 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16},
},
{
{ 2, 6, 6, 7, 8, 8, 9,11,11,12,12,12,13,13,13,14,14},
{ 0, 2, 5, 6, 6, 7, 8, 9,11,11,12,12,13,13,14,14,14},
{ 0, 0, 3, 6, 6, 7, 8, 9,11,11,12,12,13,13,13,14,14},
{ 0, 0, 0, 4, 4, 5, 6, 6, 7, 9,11,11,12,13,13,13,14},
},
{
{ 4, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9,10,10,10,10},
{ 0, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8, 9, 9, 9,10,10,10},
{ 0, 0, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,10},
{ 0, 0, 0, 4, 4, 4, 4, 4, 5, 6, 7, 8, 8, 9,10,10,10},
},
};
static const int codtab[3][4][17] =
{
{
{ 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7,4},
{ 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10,6},
{ 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9,5},
{ 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12,8},
},
{
{ 3,11, 7, 7, 7, 4, 7,15,11,15,11, 8,15,11, 7, 9,7},
{ 0, 2, 7,10, 6, 6, 6, 6,14,10,14,10,14,10,11, 8,6},
{ 0, 0, 3, 9, 5, 5, 5, 5,13, 9,13, 9,13, 9, 6,10,5},
{ 0, 0, 0, 5, 4, 6, 8, 4, 4, 4,12, 8,12,12, 8, 1,4},
},
{
{15,15,11, 8,15,11, 9, 8,15,11,15,11, 8,13, 9, 5,1},
{ 0,14,15,12,10, 8,14,10,14,14,10,14,10, 7,12, 8,4},
{ 0, 0,13,14,11, 9,13, 9,13,10,13, 9,13, 9,11, 7,3},
{ 0, 0, 0,12,11,10, 9, 8,13,12,12,12, 8,12,10, 6,2},
},
};
153
vlcnum = currSE->len;
if (vlcnum == 3)
{
currSE->len = 6;
if (currSE->value1 > 0)
currSE->inf = ((currSE->value1-1) << 2) | currSE->value2;
else
currSE->inf = 3;
}
else
{
currSE->len = lentab[vlcnum][currSE->value2][currSE->value1];
currSE->inf = codtab[vlcnum][currSE->value2][currSE->value1];
}
symbol2vlc(currSE);
writeUVLC2buffer(currSE, dataPart->bitstream);
exit(-1);
code <<= 1;
if (level < 0)
code |= 0x1;
}
if (numtrailingones)
{
currSE->type
= dptype;
currSE->value2 = numtrailingones;
currSE->value1 = code;
writeSyntaxElement_VLC (currSE, dataPart);
}
}
154
, Trailing One .
1 .
0 , 0 - , (level)
. Prefix Suffix , Suffix 0
0~6 .
.
. ,
0 11 Tailing Ones 2 Suffix
(vlcnum) 1 , 0 .
, 0 DC . 0
(numcoeff) 3 Trailing Ones (numtrailing
ones) 3 , 0
1 , 0 1
.
Suffix (vlcnum) 0 writeSyntaxElement_Level_VLC1()
, writeSyntaxElement_Level_VLCN()
.
0 , Suffix (vlcnum)
. , Suffix (vlcnum) (incVlc
[vlcnum]) Suffix (vlcnum) . ,
0 3 Suffix
(vlcnum) 2 .
102 0
static int incVlc[] = {0,3,6,12,24,48,32768};
if (numcoeff)
{
level_two_or_higher = 1;
if (numcoeff > 3 && numtrailingones == 3)
level_two_or_higher = 0;
if (numcoeff > 10 && numtrailingones < 3)
vlcnum = 1;
else
vlcnum = 0;
155
= dptype;
if (level_two_or_higher)
{
if (currSE->value1 > 0)
currSE->value1 --;
else
currSE->value1 ++;
level_two_or_higher = 0;
}
//encode level
if (vlcnum == 0)
writeSyntaxElement_Level_VLC1(currSE, dataPart);
else
writeSyntaxElement_Level_VLCN(currSE, vlcnum, dataPart);
//update VLC table
if (absm(level)>incVlc[vlcnum])
vlcnum++;
if (k == lastcoeff - numtrailingones && absm(level)>3)
vlcnum = 2;
currSE++;
currMB->currSEnr++;
}
}
0 0
, 0
(numcoeff) (max_coeff_num) , 0
0 (totzeros) . ,
0 (numcoeff) (currSE->len) .
, DC writeSyntaxElement_TotalZerosChroma
DC() VLC , writeSyntaxElement_
TotalZeros() .
156
103 0 0
if (numcoeff < max_coeff_num)
{
currSE->type
= dptype;
currSE->value1 = totzeros;
vlcnum = numcoeff-1;
currSE->len = vlcnum;
if (!cdc)
writeSyntaxElement_TotalZeros(currSE, dataPart);
else
writeSyntaxElement_TotalZerosChromaDC(currSE, dataPart);
}
, writeSyntaxElement_TotalZeros() .
(currSE->len) (lentab[][])
(currSE->len) , (codtab[][])
(currSE->inf) . , symbol2
vlc() VLC , writeUVLC2buffer()
.
104 writeSyntaxElement_TotalZeros()
static const int lentab[TOTRUN_NUM][16] =
{
{ 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
{ 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
{ 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
{ 5,3,4,4,3,3,3,4,3,4,5,5,5},
{ 4,4,4,3,3,3,3,3,4,5,4,5},
{ 6,5,3,3,3,3,3,3,4,3,6},
{ 6,5,3,3,3,2,3,4,3,6},
{ 6,4,5,3,2,2,3,3,6},
{ 6,6,4,2,2,3,2,5},
{ 5,5,3,2,2,2,4},
{ 4,4,3,3,1,3},
{ 4,4,2,1,3},
{ 3,3,1,2},
{ 2,2,1},
{ 1,1},
};
157
writeSyntaxElement_TotalZerosChromaDC()
VLC . , (lentab[][])
(codtab[][]) . 4:2:0 , .
105 DC TotalZeros
static const int lentab[3][TOTRUN_NUM][16] =
{
{{ 1,2,3,3},
{ 1,2,2},
{ 1,1}}
};
static const int codtab[3][TOTRUN_NUM][16] =
{
{{ 1,1,1,0},
{ 1,1,0},
{ 1,0}}
};
158
, 0 -
, (Run) . 0
. VLC
Info , 0
0 0 - 0 1
(vlcnum) (currSE->len) . 0 8
(currSE->len) 6 . , VLC
writeSyntaxElement_Run() .
, 0 (numcoeff) 1 0
.
106 0
RUNBEFORE_NUM = 7;
zerosleft = totzeros;
numcoef = numcoeff;
for (k = lastcoeff; k >= 0; k--)
{
run = pRun[k];
currSE->value1 = run;
currSE->type
= dptype;
, writeSyntaxElement_Run() .
(currSE->len) (lentab[][])
(currSE->len) , (codtab[][])
159
(currSE->inf) . , symbol2vlc()
VLC , writeUVLC2buffer()
.
107 writeSyntaxElement_Run()
static const int lentab[TOTRUN_NUM][16] =
{
{1,1},
{1,2,2},
{2,2,2,2},
{2,2,2,3,3},
{2,2,3,3,3,3},
{2,3,3,3,3,3,3},
{3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
};
static const int codtab[TOTRUN_NUM][16] =
{
{1,0},
{1,1,0},
{3,2,1,0},
{3,2,1,1,0},
{3,2,3,2,1,0},
{3,0,1,3,2,5,4},
{7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
};
currSE->len = lentab[currSE->len][currSE->value1];
currSE->inf = codtab[currSE->len][currSE->value1];
if (currSE->len != 0)
{
symbol2vlc(currSE);
writeUVLC2buffer(currSE, this_dataPart->bitstream);
}
160
8.
[1] Thomas Wiegand, Gary J. Sullivan, Gisle Bjontegaard, and Ajay Luthra, "Overview of the
H.264/AVC Video Coding Standard", IEEE Transactions on Circuits and Systems for Video
Technology, Vol. 13, No. 7, pp. 560-576, July 2003
[2] , H.264/AVC , , 2005
[3] Revised H.264/MPEG-4 AVC Reference Software Manual, in in Joint Video Team (JVT) of
ISO/IEC MPEG and ITU-T VCEG, JVT-Q042, 2005
[4] Draft ITU-T recommendation and final draft international standard of joint video specification
(ITU-T Rec. H.264|ISO/IEC 14496-10 AVC), in Joint Video Team (JVT) of ISO/IEC MPEG and
ITU-T VCEG, JVTG050r1, 2003
161