# X264 源码解析之帧内预测

## Intra_4x4 预测模式

x264 中对 4x4 的预测模式如下：

Intra4x4PredMode[luma4x4BlkIdx] Name of Intra4x4PredMode[luma4x4BlkIdx]
0 Intra_4x4_Vertical(prediction mode)
1 Intra_4x4_Horizontal(prediction mode)
2 Intra_4x4_DC(prediction mode)
3 Intra_4x4_Diagonal_Down_Left(prediction mode)
4 Intra_4x4_Diagonal_Down_Right(prediction mode)
5 Intra_4x4_Vertical_Right(prediction mode)
6 Intra_4x4_Horizontal_Down(prediction mode)
7 Intra_4x4_Vertical_Left(prediction mode)
8 Intra_4x4_Horizontal_Up(prediction mode)

### Intra_4x4_Vertical 预测模式

This Intra_4x4 prediction mode is invoked when Intra4x4PredMode[ luma4x4BlkIdx ] is equal to 0.
This mode shall be used only when the samples p[ x, -1 ] with x = 0..3 are marked as "available for Intra_4x4 prediction".
The values of the prediction samples pred4x4L[ x, y ], with x, y = 0..3, are derived by
$pred4x4_L[x,y]=p[x,-1], with x,y=0..3$

x264 中关于模式 Intra_4x4_Vertical 的代码如下：

### Intra_4x4_Horizontal 预测模式

This Intra_4x4 prediction mode is invoked when Intra4x4PredMode[ luma4x4BlkIdx ] is equal to 1.
This mode shall be used only when the samples p[ −1, y ], with y = 0..3, are marked as "available for Intra_4x4 prediction".
The values of the prediction samples pred4x4L[ x, y ], with x, y = 0..3, are derived by
$pred4x4_L[x,y]=p[-1, y], with x,y=0..3$

x264 中关于模式 Intra_4x4_Vertical 的代码如下：

### Intra_4x4_DC 预测模式

This Intra_4x4 prediction mode is invoked when Intra4x4PredMode[ luma4x4BlkIdx ] is equal to 2.
The values of the prediction samples $pred4x4_L[ x, y ]$, with x, y = 0..3, are derived as follows:

If all samples p[ x, −1 ], with x = 0..3, and p[ −1, y ], with y = 0..3, are marked as "available for Intra_4x4 prediction", the values of the prediction samples $pred4x4_L$[ x, y ], with x, y = 0..3, are derived by
$pred4x4_L[x,y]$ = (p[0,-1]+p[1,-1]+p[2,-1]+p[3,-1]+p[-1,0]+p[-1,1]+p[-1,2]+p[-1,3]+4)>>3

Otherwise, if any samples p[ x, .1 ], with x = 0..3, are marked as "not available for Intra_4x4 prediction" and all samples p[ .1, y ], with y = 0..3, are marked as "available for Intra_4x4 prediction", the values of the prediction samples $pred4x4_L$[ x, y ], with x, y = 0..3, are derived by
$pred4x4_L[x,y] = (p[-1,0]+p[-1,1]+p[-1,2]+p[-1,3]+2)>>2$

Otherwise, if any samples p[ .1, y ], with y = 0..3, are marked as "not available for Intra_4x4 prediction" and all samples p[ x, .1 ], with x = 0 .. 3, are marked as "available for Intra_4x4 prediction", the values of the prediction samples $pred4x4_L$[ x, y ], with x, y = 0 .. 3, are derived by
$pred4x4_L[x,y] = (p[0,-1]+p[1,-1]+p[2,-1]+p[3,-1]+2)>>2$

Otherwise (some samples p[ x, .1 ], with x = 0..3, and some samples p[ .1, y ], with y = 0..3, are marked as "not available for Intra_4x4 prediction"), the values of the prediction samples $pred4x4_L$[ x, y ], with x, y = 0..3, are derived by
$pred4x4_L[x,y]=(1<<(BitDepth_Y-1))$

x264 中关于模式 Intra_4x4_DC 的代码如下：

### Intra_4x4_Diagonal_Down_Left 预测模式

This Intra_4x4 prediction mode is invoked when Intra4x4PredMode[ luma4x4BlkIdx ] is equal to 3.
This mode shall be used only when the samples p[ x, −1 ] with x = 0..7 are marked as "available for Intra_4x4 prediction".
The values of the prediction samples $pred4x4_L[ x, y ]$, with x, y = 0..3, are derived as follows:

If x is equal to 3 and y is equal to 3,
$pred4x4_L[ x, y ] = ( p[ 6, .1 ] + 3 * p[ 7, .1 ] + 2 ) >> 2$

Otherwise (x is not equal to 3 or y is not equal to 3),
$pred4x4_L[ x, y ] = ( p[ x + y, −1 ] + 2 * p[ x + y + 1, −1 ] + p[ x + y + 2, −1 ] + 2 ) >> 2$

x264 中关于模式 Intra_4x4_DC 的代码如下：

### Intra_4x4_Diagonal_Down_Right 预测模式

This Intra_4x4 prediction mode is invoked when Intra4x4PredMode[ luma4x4BlkIdx ] is equal to 4.
This mode shall be used only when the samples p[ x, .1 ] with x = 0..3 and p[ .1, y ] with y = .1..3 are marked as "available for Intra_4x4 prediction".
The values of the prediction samples $pred4x4_L[ x, y ]$, with x, y = 0..3, are derived as follows:
If x is greater than y,
$pred4x4_L[ x, y ] = ( p[ x − y − 2, −1] + 2 * p[ x − y − 1, −1 ] + p[ x − y, −1 ] + 2 ) >> 2$
Otherwise if x is less than y,
$pred4x4_L[ x, y ] = ( p[ −1, y − x − 2 ] + 2 * p[ −1, y − x − 1 ] + p[ −1, y − x ] + 2 ) >> 2$
Otherwise (x is equal to y),
$pred4x4_L[ x, y ] = ( p[ 0, .1 ] + 2 * p[ .1, .1 ] + p[ .1, 0 ] + 2 ) >> 2$

x264 中关于模式 Intra_4x4_Diagonal_Down_Right 的代码如下：

### Intra_4x4_Vertical_Right 预测模式

This Intra_4x4 prediction mode is invoked when Intra4x4PredMode[ luma4x4BlkIdx ] is equal to 5.
This mode shall be used only when the samples p[ x, −1 ] with x = 0..3 and p[ −1, y ] with y = −1..3 are marked as "available for Intra_4x4 prediction".
Let the variable zVR be set equal to 2 * x − y.
The values of the prediction samples $pred4x4_L[ x, y ]$, with x, y = 0..3, are derived as follows:

If zVR is equal to 0,2,4 or 6,
$pred_L[x,y]=(p[x-(y>>1)-1,-1]+p[x-(y>>1),-1]+1)>>1$
Otherwise, if zVR is equal to 1, 3, or 5,
$pred4x4_L[x,y]=(p[x-(y>>1)-2,-1]+2*p[x-(y>>1)-1,-1]+p[x-(y>>1),-1]+2)>>2$
Otherwise, if zVR is equal to −1,
$pred4x4_L[ x, y ] = (p[-1, 0 ] + 2 * p[ -1, -1 ] + p[ 0, -1 ] + 2 ) >> 2$
Otherwise (zVR is equal to -2 or -3),
$pred4x4_L[ x, y ] = ( p[ −1, y − 1 ] + 2 * p[ −1, y − 2 ] + p[ −1, y − 3 ] + 2 ) >> 2$

x264 中关于模式 Intra_4x4_Vertical_Right 的代码如下：

### Intra_4x4_Horizontal_Down 预测模式

This Intra_4x4 prediction mode is invoked when Intra4x4PredMode[ luma4x4BlkIdx ] is equal to 6.
This mode shall be used only when the samples p[ x, -1 ] with x = 0..3 and p[ -1, y ] with y = .1..3 are marked as "available for Intra_4x4 prediction".
Let the variable zHD be set equal to 2 * y - x.
The values of the prediction samples $pred4x4_L[ x, y ]$, with x, y = 0..3, are derived as follows:

x264 中关于模式 Intra_4x4_Horizontal_Down 的代码如下：

### Intra_4x4_Vertical_Left 预测模式

This Intra_4x4 prediction mode is invoked when Intra4x4PredMode[ luma4x4BlkIdx ] is equal to 7.
This mode shall be used only when the samples p[ x, −1 ] with x = 0..7 are marked as "available for Intra_4x4 prediction".
The values of the prediction samples $pred4x4_L[ x, y ]$, with x, y = 0..3, are derived as follows:

x264 中关于模式 Intra_4x4_Vertical_Left 的代码如下：

### Intra_4x4_Horizontal_Up 预测模式

This Intra_4x4 prediction mode is invoked when Intra4x4PredMode[ luma4x4BlkIdx ] is equal to 8.
This mode shall be used only when the samples p[ −1, y ] with y = 0..3 are marked as "available for Intra_4x4 prediction".
Let the variable zHU be set equal to x + 2 * y.
The values of the prediction samples $pred4x4_L[ x, y ]$, with x, y = 0..3, are derived as follows:

x264 中关于模式 Intra_4x4_Horizontal_Up 的代码如下：

## Intra_8x8 预测模式

x264 中对 8x8 的预测模式如下：

intra8x8Predmodei[luma8x8BlkIdx] Name of Intra8x8PredMode[luma8x8BlkIdx]
0 Intra_8x8_Vertical(prediction mode)
1 Intra_8x8_Horizontal(prediction mode)
2 Intra_8x8_DC(prediction mode)
3 Intra_8x8_Diagonal_Down_Left(prediction mode)
4 Intra_8x8_Diagonal_Down_Right(prediction mode)
5 Intra_8x8_Vertical_Right(prediction mode)
6 Intra_8x8_Horizontal_Down(prediction mode)
7 Intra_8x8_Vertical_Left(prediction mode)
8 Intra_8x8_Horizontal_Up(prediction mode)

### Intra_8x8_Vertical 预测模式

This Intra_8x8 prediction mode is invoked when Intra8x8PredMode[ luma8x8BlkIdx ] is equal to 0.
This mode shall be used only when the samples p[ x, -1 ] with x = 0..7 are marked as "available for Intra_8x8 prediction".
The values of the prediction samples $pred8x8_L[ x, y ]$, with x, y = 0..7, are derived by
$pred8x8_L[x,y]=p'[x,-1],with x,y=0..7$

X264 中关于模式 Intra_8x8_Vertical 的代码如下：

### Intra_8x8_Horizontal 预测模式

This Intra_8x8 prediction mode is invoked when Intra8x8PredMode[ luma8x8BlkIdx ] is equal to 1.
This mode shall be used only when the samples p[ −1, y ], with y = 0..7, are marked as "available for Intra_8x8 prediction".
The values of the prediction samples $pred8x8_L[ x, y ]$, with x, y = 0..7, are derived by
$pred8x8_L[x,y]=p'[-1,y], with x,y=0..7$

x264 中对 Intra_8x8_Horizontal 的代码如下：

### Intra_8x8_DC 预测模式

This Intra_8x8 prediction mode is invoked when Intra8x8PredMode[ luma8x8BlkIdx ] is equal to 2.
The values of the prediction samples $pred8x8_L[ x, y ]$, with x, y = 0..7, are derived as follows:

x264 中对 Intra_8x8_DC 模式的代码如下：

### Intra_8x8_Diagonal_Down_Left 预测模式

This Intra_8x8 prediction mode is invoked when Intra8x8PredMode[ luma8x8BlkIdx ] is equal to 3.
This mode shall be used only when the samples p[ x, −1 ] with x = 0..15 are marked as "available for Intra_8x8 prediction".
The values of the prediction samples $pred8x8_L[ x, y ]$, with x, y = 0..7, are derived as follows:
If x is equal to 7 and y is equal to 7,
$pred8xx_L[x,y]=( p′[ 14, −1 ] + 3 * p′[ 15, −1 ] + 2 ) >> 2$
Otherwise (x is not equal to 7 or y is not equal to 7)
$pred8x8_L[ x, y ] = ( p′[ x + y, −1 ] + 2 * p′[ x + y + 1, −1 ] + p′[ x + y + 2, −1 ] + 2 ) >> 2$

X264 中关于模式 Intra_8x8_Diagonal_Down_Left 的代码如下：

### Intra_8x8_Diagonal_Down_Right 预测模式

This Intra_8x8 prediction mode is invoked when Intra8x8PredMode[ luma8x8BlkIdx ] is equal to 4.
This mode shall be used only when the samples p[ x, .1 ] with x = 0..7 and p[ .1, y ] with y = .1..7 are marked as
"available for Intra_8x8 prediction".
The values of the prediction samples $pred8x8_L[ x, y ]$, with x, y = 0..7, are derived as follows:
If x is greater than y,
$pred8x8_L[ x, y ] = ( p′[ x − y − 2, −1] + 2 * p′[ x − y − 1, −1 ] + p′[ x − y, −1 ] + 2 ) >> 2$
Otherwise if x is less than y,
$pred8x8_L[ x, y ] = ( p′[ .1, y . x . 2 ] + 2 * p′[ .1, y . x . 1 ] + p′[ .1, y . x ] + 2 ) >> 2$
Otherwise (x is equal to y),
$pred8x8_L[ x, y ] = ( p′[ 0, −1 ] + 2 * p′[ −1, −1 ] + p′[ −1, 0 ] + 2 ) >> 2$

X264 中关于模式 Intra_8x8_Diagonal_Down_Right 的代码如下：

### Intra_8x8_Vertical_Right 预测模式

This Intra_8x8 prediction mode is invoked when Intra8x8PredMode[ luma8x8BlkIdx ] is equal to 5.
This mode shall be used only when the samples p[ x, -1 ] with x = 0..7 and p[ -1, y ] with y = -1..7 are marked as "available for Intra_8x8 prediction".
Let the variable zVR be set equal to 2 * x . y.
The values of the prediction samples $pred8x8_L[ x, y ]$, with x, y = 0..7, are derived as follows:
If zVR is equal to 0, 2, 4, 6, 8, 10, 12, or 14
$pred8x8_L[x,y] = (p'[x-y(y>>1)-1,-1] + p'[x-(y>>1),-1]+1)>>1$
Otherwise, if zVR is equal to 1, 3, 5, 7, 9, 11, or 13
$pred8x8_L[x,y]=(p'[x-(y>>1)-2,-1]+2*p'[x-(y>>1)-1,-1]+p'[x-(y>>1),-1]+2)>>2$
Otherwise, if zVR is equal to −1,
$pred8x8_L[ x, y ] = ( p′[ −1, 0 ] + 2 * p′[ −1, −1 ] + p′[ 0, −1 ] + 2 ) >> 2$
Otherwise (zVR is equal to .2, .3, .4, .5, .6, or .7),
$pred8x8_L[ x, y ] = ( p′[ -1, y . 2*x - 1 ] + 2 * p′[ -1, y - 2*x - 2 ] + p′[ -1, y - 2*x - 3 ] + 2 ) >> 2$

X264 中关于模式 Intra_8x8_Vertical_Right 的代码如下：

### Intra_8x8_Horizontal_Down 预测模式

This Intra_8x8 prediction mode is invoked when Intra8x8PredMode[ luma8x8BlkIdx ] is equal to 6.
This mode shall be used only when the samples p[ x, −1 ] with x = 0..7 and p[ −1, y ] with y = −1..7 are marked as "available for Intra_8x8 prediction".
Let the variable zHD be set equal to 2 * y − x.
The values of the prediction samples $pred8x8_L[ x, y ]$, with x, y = 0..7, are derived as follows:
If zHD is equal to 0, 2, 4, 6, 8, 10, 12, or 14
$pred8x8_L[ x, y ] = ( p′[ −1, y − ( x >> 1 ) − 1 ] + p′[ −1, y − ( x >> 1 ) ] + 1 ) >> 1$
Otherwise, if zHD is equal to 1, 3, 5, 7, 9, 11, or 13
$pred8x8_L[x,y]=(p′[-1, y -(x>>1)-2]+2*p′[-1,y-(x>>1)-1]+p′[-1,y-(x>>1)]+2)>>2$
Otherwise, if zHD is equal to −1,
$pred8x8L[ x, y ] = ( p′[ −1, 0 ] + 2 * p′[ −1, −1 ] + p′[ 0, −1 ] + 2 ) >> 2$
Otherwise (zHD is equal to −2, −3, −4, −5, −6, −7),
$pred8x8L[ x, y ] = ( p′[ x − 2*y − 1, −1 ] + 2 * p′[ x − 2*y − 2, −1 ] + p′[ x − 2*y − 3, −1 ] + 2 ) >> 2$

X264 中关于模式 Intra_8x8_Horizontal_Down 的代码如下：

### Intra_8x8_Vertical_Left 预测模式

This Intra_8x8 prediction mode is invoked when Intra8x8PredMode[ luma8x8BlkIdx ] is equal to 7.
This mode shall be used only when the samples p[ x, −1 ] with x = 0..15 are marked as "available for Intra_8x8 prediction".
The values of the prediction samples $pred8x8_L[ x, y ]$, with x, y = 0..7, are derived as follows:
If y is equal to 0, 2, 4 or 6
$pred8x8_L[ x, y ] = ( p′[ x + ( y >> 1 ), .1 ] + p′[ x + ( y >> 1 ) + 1, .1 ] + 1) >> 1$
Otherwise (y is equal to 1, 3, 5, 7),
$pred8x8_L[ x, y ] = ( p′[ x + ( y >> 1 ), −1 ] + 2 * p′[ x + ( y >> 1 ) + 1, −1 ] + p′[ x + ( y >> 1 ) + 2, −1 ] + 2 ) >>2$

X264 中关于模式 Intra_8x8_Horizontal_Down 的代码如下：

### Intra_8x8_Horizontal_Up 预测模式

This Intra_8x8 prediction mode is invoked when Intra8x8PredMode[ luma8x8BlkIdx ] is equal to 8.
This mode shall be used only when the samples p[ -1, y ] with y = 0..7 are marked as "available for Intra_8x8 prediction".
Let the variable zHU be set equal to x + 2 * y.
The values of the prediction samples $pred8x8_L[ x, y ]$, with x, y = 0..7, are derived as follows:
If zHU is equal to 0, 2, 4, 6, 8, 10, or 12
$pred8x8_L[ x, y ] = ( p′[ −1, y + ( x >> 1 ) ] + p′[ −1, y + ( x >> 1 ) + 1 ] + 1 ) >> 1$
Otherwise, if zHU is equal to 1, 3, 5, 7, 9, or 11
$pred8x8_L[ x, y ] = ( p′[ −1, y + ( x >> 1 ) ] + 2 * p′[ −1, y + ( x >> 1 ) + 1 ] + p′[ −1, y + ( x >> 1 ) + 2 ] + 2 ) >>2$
Otherwise, if zHU is equal to 13,
$pred8x8_L[ x, y ] = ( p′[ −1, 6 ] + 3 * p′[ −1, 7 ] + 2 ) >> 2$
Otherwise (zHU is greater than 13),
$pred8x8_L[x,y]=p'[-1,7]$

X264 中关于模式 Intra_8x8_Horizontal_Up 的代码如下：

## Intra_16x16 预测模式

x264 中对 16x16 的预测模式如下：

intra16x16Predmode Name of Intra16x16PredMode Note
0 Intra_16x16_Vertical(prediction mode) 由上边像素推出相应像素值
1 Intra_16x16_Horicontal(prdiction mode) 由左边像素推出相应像素值
2 Intra_16x16_DC(prediction mode) 由上边和左边像素平均值推出相应像素值
3 Intra_16x16_Plane(prediction mode) 利用线性 plan 函数及左、上像素推出相应像素值，适用于亮度变化平缓区域

### Intra_16x16_Vertical 预测模式

This Intra_16x16 prediction mode shall be used only when the samples p[x, -1] with x=0...15 are marked as "available for Intra_16x16 prediction".The values of the prediction samples pred[x, y] with x,y=0...15, are derived by pred[x,y]=p[x,-1],with x,y=0...15

x264 中关于模式 Vertical 的C 代码如下：

### Intra_16x16_Horizontal 预测模式

This Intra_16x16 prediction mode shall be used only when the samples p[−1, y] with y = 0..15 are marked as "available
for Intra_16x16 prediction".
The values of the prediction samples predL[ x, y ], with x, y = 0..15, are derived by
predL[ x, y ] = p[ −1, y ], with x, y = 0..15

x264 中关于模式 Horizontal 的代码如下：

### Intra_16x16_DC 预测模式

This Intra_16x16 prediction mode operates, depending on whether the neighbouring samples are marked as "available for Intra_16x16 prediction", as follows:

If all neighbouring samples p[x,-1] ], with x = 0..15, and p[ −1, y ], with y = 0..15, are marked as "available for Intra_16x16 prediction", the prediction for all luma samples in the macroblock is given by: $pred_L[x,y]=(\sum_{x'=0}^{15}p[x',-1]+\sum_{y'=0}^{15}p[-1,y']+16)>>5, with x,y=0..15$

Otherwise, if any of the neighbouring samples p[ x, .1 ], with x = 0..15, are marked as "not available for Intra_16x16 prediction" and all of the neighbouring samples p[ .1, y ], with y = 0..15, are marked as "available for Intra_16x16 prediction", the prediction for all luma samples in the macroblock is given by: $pred_L[x,y]=(\sum_{y'=0}^{15}p[-1,y']+8)>>4, with x,y=0..15$

Otherwise, if any of the neighbouring samples p[ −1, y ], with y = 0..15, are marked as "not available for Intra_16x16 prediction" and all of the neighbouring samples p[ x, −1 ], with x = 0..15, are marked as "available for Intra_16x16 prediction", the prediction for all luma samples in the macroblock is given by: $pred_L[x,y]=(\sum_{x'=0}^{15}p[x',-1]+8)>>4, with x,y=0..15$

Otherwise (some of the neighbouring samples p[ x, .1 ], with x = 0..15, and some of the neighbouring samples p[ .1, y ], with y = 0..15, are marked as "not available for Intra_16x16 prediction"), the prediction for all luma samples in the macroblock is given by: $pred_L[x,y]=(1<<(BitDepth_Y - 1)), with x,y=0..15$

x264 中关于模式 DC 的代码如下：

### Intra_16x16_Plane 预测模式

This Intra_16x16 prediction mode shall be used only when the samples p[ x, .1 ] with x = .1..15 and p[ .1, y ] with y = 0..15 are marked as "available for Intra_16x16 prediction".
The values of the prediction samples $pred_L[x,y]$,with x,y=0...15, are derived by
$pred_L[x,y]=Clip1_Y((a+b*(x-7)+c*(y-7)+16)>>5)$,with x,y=0...15, where
$a = 16 * ( p[ .1, 15 ] + p[ 15, .1 ] )$
$b = ( 5 * H + 32 ) >> 6$
$c = ( 5 * V + 32 ) >> 6$
and H and V are specified as
$H=\sum_{x'=0}^{7}(x'+1)*(p[8+x',-1]-p[6-x',-1])$
$V=\sum_{y'=0}^{7}(y'+1)*(p[-1,8+y']-p[-1,6-y'])$

x264 中关于模式 Plane 的代码如下：