📄 h263.c
字号:
/* different qscale, we must rescale */ for(i=1; i<8; i++){ const int level= block[n][s->dsp.idct_permutation[i<<3]]; block[n][s->dsp.idct_permutation[i<<3]]= level - ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale); ac_val1[i ]= level; ac_val1[i+8]= block[n][s->dsp.idct_permutation[i ]]; } } st[n]= s->intra_v_scantable.permutated; } for(i=63; i>0; i--) //FIXME optimize if(block[n][ st[n][i] ]) break; s->block_last_index[n]= i; score += get_block_rate(s, block[n], s->block_last_index[n], st[n]); } return score < 0;}static inline void restore_ac_coeffs(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6]){ int i, n; memcpy(s->block_last_index, zigzag_last_index, sizeof(int)*6); for(n=0; n<6; n++){ int16_t *ac_val = s->ac_val[0][0] + s->block_index[n] * 16; st[n]= s->intra_scantable.permutated; if(dir[n]){ /* top prediction */ for(i=1; i<8; i++){ block[n][s->dsp.idct_permutation[i ]] = ac_val[i+8]; } }else{ /* left prediction */ for(i=1; i<8; i++){ block[n][s->dsp.idct_permutation[i<<3]]= ac_val[i ]; } } }}/** * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2) */void ff_clean_h263_qscales(MpegEncContext *s){ int i; int8_t * const qscale_table= s->current_picture.qscale_table; for(i=1; i<s->mb_num; i++){ if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2) qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2; } for(i=s->mb_num-2; i>=0; i--){ if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2) qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2; }}/** * modify mb_type & qscale so that encoding is acually possible in mpeg4 */void ff_clean_mpeg4_qscales(MpegEncContext *s){ int i; int8_t * const qscale_table= s->current_picture.qscale_table; ff_clean_h263_qscales(s); for(i=1; i<s->mb_num; i++){ int mb_xy= s->mb_index2xy[i]; if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&MB_TYPE_INTER4V)){ s->mb_type[mb_xy]&= ~MB_TYPE_INTER4V; s->mb_type[mb_xy]|= MB_TYPE_INTER; } } if(s->pict_type== B_TYPE){ int odd=0; /* ok, come on, this isnt funny anymore, theres more code for handling this mpeg4 mess than for the actual adaptive quantization */ for(i=0; i<s->mb_num; i++){ int mb_xy= s->mb_index2xy[i]; odd += qscale_table[mb_xy]&1; } if(2*odd > s->mb_num) odd=1; else odd=0; for(i=0; i<s->mb_num; i++){ int mb_xy= s->mb_index2xy[i]; if((qscale_table[mb_xy]&1) != odd) qscale_table[mb_xy]++; if(qscale_table[mb_xy] > 31) qscale_table[mb_xy]= 31; } for(i=1; i<s->mb_num; i++){ int mb_xy= s->mb_index2xy[i]; if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&MB_TYPE_DIRECT)){ s->mb_type[mb_xy]&= ~MB_TYPE_DIRECT; s->mb_type[mb_xy]|= MB_TYPE_BIDIR; } } }}#endif //CONFIG_ENCODERS/** * * @return the mb_type */int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){ const int mb_index= s->mb_x + s->mb_y*s->mb_stride; const int colocated_mb_type= s->next_picture.mb_type[mb_index]; //FIXME or next? int xy= s->block_index[0]; uint16_t time_pp= s->pp_time; uint16_t time_pb= s->pb_time; int i; //FIXME avoid divides if(IS_8X8(colocated_mb_type)){ s->mv_type = MV_TYPE_8X8; for(i=0; i<4; i++){ xy= s->block_index[i]; s->mv[0][i][0] = s->motion_val[xy][0]*time_pb/time_pp + mx; s->mv[0][i][1] = s->motion_val[xy][1]*time_pb/time_pp + my; s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->motion_val[xy][0] : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp; s->mv[1][i][1] = my ? s->mv[0][i][1] - s->motion_val[xy][1] : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp; } return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1; } else if(IS_INTERLACED(colocated_mb_type)){ s->mv_type = MV_TYPE_FIELD; for(i=0; i<2; i++){ if(s->top_field_first){ time_pp= s->pp_field_time - s->field_select_table[mb_index][i] + i; time_pb= s->pb_field_time - s->field_select_table[mb_index][i] + i; }else{ time_pp= s->pp_field_time + s->field_select_table[mb_index][i] - i; time_pb= s->pb_field_time + s->field_select_table[mb_index][i] - i; } s->mv[0][i][0] = s->field_mv_table[mb_index][i][0]*time_pb/time_pp + mx; s->mv[0][i][1] = s->field_mv_table[mb_index][i][1]*time_pb/time_pp + my; s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->field_mv_table[mb_index][i][0] : s->field_mv_table[mb_index][i][0]*(time_pb - time_pp)/time_pp; s->mv[1][i][1] = my ? s->mv[0][i][1] - s->field_mv_table[mb_index][i][1] : s->field_mv_table[mb_index][i][1]*(time_pb - time_pp)/time_pp; } return MB_TYPE_DIRECT2 | MB_TYPE_16x8 | MB_TYPE_L0L1 | MB_TYPE_INTERLACED; }else{ s->mv[0][0][0] = s->mv[0][1][0] = s->mv[0][2][0] = s->mv[0][3][0] = s->motion_val[xy][0]*time_pb/time_pp + mx; s->mv[0][0][1] = s->mv[0][1][1] = s->mv[0][2][1] = s->mv[0][3][1] = s->motion_val[xy][1]*time_pb/time_pp + my; s->mv[1][0][0] = s->mv[1][1][0] = s->mv[1][2][0] = s->mv[1][3][0] = mx ? s->mv[0][0][0] - s->motion_val[xy][0] : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp; s->mv[1][0][1] = s->mv[1][1][1] = s->mv[1][2][1] = s->mv[1][3][1] = my ? s->mv[0][0][1] - s->motion_val[xy][1] : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp; if((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) || !s->quarter_sample) s->mv_type= MV_TYPE_16X16; else s->mv_type= MV_TYPE_8X8; return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_L0L1; //Note see prev line }}void ff_h263_update_motion_val(MpegEncContext * s){ const int mb_xy = s->mb_y * s->mb_stride + s->mb_x; //FIXME a lot of thet is only needed for !low_delay const int wrap = s->block_wrap[0]; const int xy = s->block_index[0]; s->current_picture.mbskip_table[mb_xy]= s->mb_skiped; if(s->mv_type != MV_TYPE_8X8){ int motion_x, motion_y; if (s->mb_intra) { motion_x = 0; motion_y = 0; } else if (s->mv_type == MV_TYPE_16X16) { motion_x = s->mv[0][0][0]; motion_y = s->mv[0][0][1]; } else /*if (s->mv_type == MV_TYPE_FIELD)*/ { int i; motion_x = s->mv[0][0][0] + s->mv[0][1][0]; motion_y = s->mv[0][0][1] + s->mv[0][1][1]; motion_x = (motion_x>>1) | (motion_x&1); for(i=0; i<2; i++){ s->field_mv_table[mb_xy][i][0]= s->mv[0][i][0]; s->field_mv_table[mb_xy][i][1]= s->mv[0][i][1]; s->field_select_table[mb_xy][i]= s->field_select[0][i]; } } /* no update if 8X8 because it has been done during parsing */ s->motion_val[xy][0] = motion_x; s->motion_val[xy][1] = motion_y; s->motion_val[xy + 1][0] = motion_x; s->motion_val[xy + 1][1] = motion_y; s->motion_val[xy + wrap][0] = motion_x; s->motion_val[xy + wrap][1] = motion_y; s->motion_val[xy + 1 + wrap][0] = motion_x; s->motion_val[xy + 1 + wrap][1] = motion_y; } if(s->encoding){ //FIXME encoding MUST be cleaned up if (s->mv_type == MV_TYPE_8X8) s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_8x8; else s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_16x16; }}#ifdef CONFIG_ENCODERSstatic inline int get_p_cbp(MpegEncContext * s, DCTELEM block[6][64], int motion_x, int motion_y){ int cbp, i; if(s->flags & CODEC_FLAG_CBP_RD){ int best_cbpy_score= INT_MAX; int best_cbpc_score= INT_MAX; int cbpc = (-1), cbpy= (-1); const int offset= (s->mv_type==MV_TYPE_16X16 ? 0 : 16) + (s->dquant ? 8 : 0); const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6); for(i=0; i<4; i++){ int score= inter_MCBPC_bits[i + offset] * lambda; if(i&1) score += s->coded_score[5]; if(i&2) score += s->coded_score[4]; if(score < best_cbpc_score){ best_cbpc_score= score; cbpc= i; } } for(i=0; i<16; i++){ int score= cbpy_tab[i ^ 0xF][1] * lambda; if(i&1) score += s->coded_score[3]; if(i&2) score += s->coded_score[2]; if(i&4) score += s->coded_score[1]; if(i&8) score += s->coded_score[0]; if(score < best_cbpy_score){ best_cbpy_score= score; cbpy= i; } } cbp= cbpc + 4*cbpy; if ((motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16){ if(best_cbpy_score + best_cbpc_score + 2*lambda >= 0) cbp= 0; } for (i = 0; i < 6; i++) { if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){ s->block_last_index[i]= -1; memset(s->block[i], 0, sizeof(DCTELEM)*64); } } }else{ cbp= 0; for (i = 0; i < 6; i++) { if (s->block_last_index[i] >= 0) cbp |= 1 << (5 - i); } } return cbp;}static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64], int motion_x, int motion_y, int mb_type){ int cbp=0, i; if(s->flags & CODEC_FLAG_CBP_RD){ int score=0; const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6); for(i=0; i<6; i++){ if(s->coded_score[i] < 0){ score += s->coded_score[i]; cbp |= 1 << (5 - i); } } if(cbp){ int zero_score= -6; if ((motion_x | motion_y | s->dquant | mb_type) == 0){ zero_score-= 4; //2*MV + mb_type + cbp bit } zero_score*= lambda; if(zero_score <= score){ cbp=0; } } for (i = 0; i < 6; i++) { if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){ s->block_last_index[i]= -1; memset(s->block[i], 0, sizeof(DCTELEM)*64); } } }else{ for (i = 0; i < 6; i++) { if (s->block_last_index[i] >= 0) cbp |= 1 << (5 - i); } } return cbp;}void mpeg4_encode_mb(MpegEncContext * s, DCTELEM block[6][64], int motion_x, int motion_y){ int cbpc, cbpy, pred_x, pred_y; PutBitContext * const pb2 = s->data_partitioning ? &s->pb2 : &s->pb; PutBitContext * const tex_pb = s->data_partitioning && s->pict_type!=B_TYPE ? &s->tex_pb : &s->pb; PutBitContext * const dc_pb = s->data_partitioning && s->pict_type!=I_TYPE ? &s->pb2 : &s->pb; const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0; const int dquant_code[5]= {1,0,9,2,3}; // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); if (!s->mb_intra) { /* compute cbp */ int i, cbp; if(s->pict_type==B_TYPE){ static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */ int mb_type= mb_type_table[s->mv_dir]; if(s->mb_x==0){ s->last_mv[0][0][0]= s->last_mv[0][0][1]= s->last_mv[1][0][0]= s->last_mv[1][0][1]= 0; } assert(s->dquant>=-2 && s->dquant<=2); assert((s->dquant&1)==0); assert(mb_type>=0); /* nothing to do if this MB was skiped in the next P Frame */ if(s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]){ //FIXME avoid DCT & ... s->skip_count++; s->mv[0][0][0]= s->mv[0][0][1]= s->mv[1][0][0]= s->mv[1][0][1]= 0; s->mv_dir= MV_DIR_FORWARD; //doesnt matter s->qscale -= s->dquant;// s->mb_skiped=1; return; } cbp= get_b_cbp(s, block, motion_x, motion_y, mb_type); if ((cbp | motion_x | motion_y | mb_type) ==0) { /* direct MB with MV={0,0} */ assert(s->dquant==0); put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */ if(interleaved_stats){ s->misc_bits++; s->last_bits++; } s->skip_count++; return; } put_bits(&s->pb, 1, 0); /* mb coded modb1=0 */ put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we dont need it :) if(cbp) put_bits(&s->pb, 6, cbp); if(cbp && mb_type){ if(s->dquant) put_bits(&s->pb, 2, (s->dquant>>2)+3); else put_bits(&s->pb, 1, 0); }else s->qscale -= s->dquant;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -