2
2
* H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3
3
* Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5
* This library is free software; you can redistribute it and/or
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
6
8
* modify it under the terms of the GNU Lesser General Public
7
9
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
12
* FFmpeg is distributed in the hope that it will be useful,
11
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
15
* Lesser General Public License for more details.
15
17
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
18
* License along with FFmpeg; if not, write to the Free Software
17
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5692
5698
if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
5695
if( !get_cabac( &h->cabac, &h->cabac_state[27+ctx] ) )
5701
if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
5696
5702
return 0; /* B_Direct_16x16 */
5698
if( !get_cabac( &h->cabac, &h->cabac_state[27+3] ) ) {
5699
return 1 + get_cabac( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
5704
if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
5705
return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
5702
bits = get_cabac( &h->cabac, &h->cabac_state[27+4] ) << 3;
5703
bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 2;
5704
bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 1;
5705
bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] );
5708
bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
5709
bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
5710
bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
5711
bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
5707
5713
return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
5708
5714
else if( bits == 13 ) {
6029
6034
return ctx + 4 * cat;
6037
static const __attribute((used)) uint8_t last_coeff_flag_offset_8x8[63] = {
6038
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
6039
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
6040
3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
6041
5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
6032
6044
static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
6033
6045
const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
6034
6046
static const int significant_coeff_flag_offset[2][6] = {
6042
6054
static const int coeff_abs_level_m1_offset[6] = {
6043
6055
227+0, 227+10, 227+20, 227+30, 227+39, 426
6045
static const int significant_coeff_flag_offset_8x8[2][63] = {
6057
static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
6046
6058
{ 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
6047
6059
4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
6048
6060
7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
6052
6064
9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
6053
6065
9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
6055
static const int last_coeff_flag_offset_8x8[63] = {
6056
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
6057
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
6058
3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
6059
5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
6065
6071
int coeff_count = 0;
6067
6073
int abslevel1 = 1;
6102
6126
#define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
6103
6127
for(last= 0; last < coefs; last++) { \
6104
6128
uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
6105
if( get_cabac( &h->cabac, sig_ctx )) { \
6129
if( get_cabac( CC, sig_ctx )) { \
6106
6130
uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
6107
6131
index[coeff_count++] = last; \
6108
if( get_cabac( &h->cabac, last_ctx ) ) { \
6132
if( get_cabac( CC, last_ctx ) ) { \
6109
6133
last= max_coeff; \
6138
if( last == max_coeff -1 ) {\
6139
index[coeff_count++] = last;\
6114
const int *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
6141
const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
6143
coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
6145
coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
6115
6147
DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
6117
6149
DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
6119
if( last == max_coeff -1 ) {
6120
index[coeff_count++] = last;
6122
6152
assert(coeff_count > 0);
6134
6164
fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
6137
for( i = coeff_count - 1; i >= 0; i-- ) {
6167
for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
6138
6168
uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
6139
int j= scantable[index[i]];
6169
int j= scantable[index[coeff_count]];
6141
if( get_cabac( &h->cabac, ctx ) == 0 ) {
6171
if( get_cabac( CC, ctx ) == 0 ) {
6143
if( get_cabac_bypass( &h->cabac ) ) block[j] = -1;
6173
block[j] = get_cabac_bypass_sign( CC, -1);
6146
if( get_cabac_bypass( &h->cabac ) ) block[j] = (-qmul[j] + 32) >> 6;
6147
else block[j] = ( qmul[j] + 32) >> 6;
6175
block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
6152
6180
int coeff_abs = 2;
6153
6181
ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
6154
while( coeff_abs < 15 && get_cabac( &h->cabac, ctx ) ) {
6182
while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
6158
6186
if( coeff_abs >= 15 ) {
6160
while( get_cabac_bypass( &h->cabac ) ) {
6161
coeff_abs += 1 << j;
6188
while( get_cabac_bypass( CC ) ) {
6166
if( get_cabac_bypass( &h->cabac ) )
6167
coeff_abs += 1 << j ;
6194
coeff_abs += coeff_abs + get_cabac_bypass( CC );
6172
if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs;
6200
if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
6173
6201
else block[j] = coeff_abs;
6175
if( get_cabac_bypass( &h->cabac ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
6203
if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
6176
6204
else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
6210
#ifdef CABAC_ON_STACK
6211
h->cabac.range = cc.range ;
6212
h->cabac.low = cc.low ;
6213
h->cabac.bytestream= cc.bytestream;
7044
static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
7045
MpegEncContext * const s = &h->s;
7047
int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
7049
if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
7050
filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
7053
assert(!FRAME_MBAFF);
7055
mb_xy = mb_x + mb_y*s->mb_stride;
7056
mb_type = s->current_picture.mb_type[mb_xy];
7057
qp = s->current_picture.qscale_table[mb_xy];
7058
qp0 = s->current_picture.qscale_table[mb_xy-1];
7059
qp1 = s->current_picture.qscale_table[h->top_mb_xy];
7060
qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
7061
qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
7062
qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
7063
qp0 = (qp + qp0 + 1) >> 1;
7064
qp1 = (qp + qp1 + 1) >> 1;
7065
qpc0 = (qpc + qpc0 + 1) >> 1;
7066
qpc1 = (qpc + qpc1 + 1) >> 1;
7067
qp_thresh = 15 - h->slice_alpha_c0_offset;
7068
if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
7069
qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
7072
if( IS_INTRA(mb_type) ) {
7073
int16_t bS4[4] = {4,4,4,4};
7074
int16_t bS3[4] = {3,3,3,3};
7075
if( IS_8x8DCT(mb_type) ) {
7076
filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
7077
filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
7078
filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
7079
filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
7081
filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
7082
filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
7083
filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
7084
filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
7085
filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
7086
filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
7087
filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
7088
filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
7090
filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
7091
filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
7092
filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
7093
filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
7094
filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
7095
filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
7096
filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
7097
filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
7100
DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
7101
uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
7103
if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
7105
bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
7107
int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
7108
(mb_type & MB_TYPE_16x8) ? 1 : 0;
7109
int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
7110
&& (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
7112
int step = IS_8x8DCT(mb_type) ? 2 : 1;
7113
edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
7114
s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
7115
(h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
7117
if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
7118
bSv[0][0] = 0x0004000400040004ULL;
7119
if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
7120
bSv[1][0] = 0x0004000400040004ULL;
7122
#define FILTER(hv,dir,edge)\
7123
if(bSv[dir][edge]) {\
7124
filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
7126
filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
7127
filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
7133
} else if( IS_8x8DCT(mb_type) ) {
7009
7152
static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
7010
7153
MpegEncContext * const s = &h->s;
7011
7154
const int mb_xy= mb_x + mb_y*s->mb_stride;