⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 h263.c

📁 mpeg4 video codec mpeg4 video codec
💻 C
📖 第 1 页 / 共 5 页
字号:
        const int index= scantable[j];        int level= block[index];        if(level){            level+= 64;            if((level&(~127)) == 0){                if(j<block_last_index) rate+= s->intra_ac_vlc_length     [UNI_AC_ENC_INDEX(j-last-1, level)];                else                   rate+= s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j-last-1, level)];            }else                rate += s->ac_esc_length;            level-= 64;            last= j;        }    }        return rate;}static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6]){    int score= 0;    int i, n;    int8_t * const qscale_table= s->current_picture.qscale_table;    memcpy(zigzag_last_index, s->block_last_index, sizeof(int)*6);        for(n=0; n<6; n++){        int16_t *ac_val, *ac_val1;        score -= get_block_rate(s, block[n], s->block_last_index[n], s->intra_scantable.permutated);        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;        ac_val1= ac_val;        if(dir[n]){            const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;            /* top prediction */            ac_val-= s->block_wrap[n]*16;            if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){                /* same qscale */                for(i=1; i<8; i++){                    const int level= block[n][s->dsp.idct_permutation[i   ]];                    block[n][s->dsp.idct_permutation[i   ]] = level - ac_val[i+8];                    ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];                    ac_val1[i+8]= level;                }            }else{                /* different qscale, we must rescale */                for(i=1; i<8; i++){                    const int level= block[n][s->dsp.idct_permutation[i   ]];                    block[n][s->dsp.idct_permutation[i   ]] = level - ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);                    ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];                    ac_val1[i+8]= level;                }            }            st[n]= s->intra_h_scantable.permutated;        }else{            const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;            /* left prediction */            ac_val-= 16;            if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){                /* same qscale */                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 - ac_val[i];                    ac_val1[i  ]= level;                    ac_val1[i+8]=    block[n][s->dsp.idct_permutation[i   ]];                }            }else{                /* 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;    }    if(s->codec_id != CODEC_ID_H263P){        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]&CANDIDATE_MB_TYPE_INTER4V)){                s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_INTER4V;                s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;            }        }    }}/** * 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);        if(s->pict_type== B_TYPE){        int odd=0;        /* ok, come on, this isn't funny anymore, there's 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]&CANDIDATE_MB_TYPE_DIRECT)){                s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_DIRECT;                s->mb_type[mb_xy]|= CANDIDATE_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];    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->next_picture.motion_val[0][xy][0]*time_pb/time_pp + mx;            s->mv[0][i][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp + my;            s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->next_picture.motion_val[0][xy][0]                                : s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;            s->mv[1][i][1] = my ? s->mv[0][i][1] - s->next_picture.motion_val[0][xy][1]                                 : s->next_picture.motion_val[0][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++){            int field_select= s->next_picture.ref_index[0][s->block_index[2*i]];            if(s->top_field_first){                time_pp= s->pp_field_time - field_select + i;                time_pb= s->pb_field_time - field_select + i;            }else{                time_pp= s->pp_field_time + field_select - i;                time_pb= s->pb_field_time + field_select - i;            }            s->mv[0][i][0] = s->p_field_mv_table[i][0][mb_index][0]*time_pb/time_pp + mx;            s->mv[0][i][1] = s->p_field_mv_table[i][0][mb_index][1]*time_pb/time_pp + my;            s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->p_field_mv_table[i][0][mb_index][0]                                : s->p_field_mv_table[i][0][mb_index][0]*(time_pb - time_pp)/time_pp;            s->mv[1][i][1] = my ? s->mv[0][i][1] - s->p_field_mv_table[i][0][mb_index][1]                                 : s->p_field_mv_table[i][0][mb_index][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->next_picture.motion_val[0][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->next_picture.motion_val[0][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->next_picture.motion_val[0][xy][0]                            : s->next_picture.motion_val[0][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->next_picture.motion_val[0][xy][1]                             : s->next_picture.motion_val[0][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 that is only needed for !low_delay    const int wrap = s->b8_stride;    const int xy = s->block_index[0];        s->current_picture.mbskip_table[mb_xy]= s->mb_skipped;     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->p_field_mv_table[i][0][mb_xy][0]= s->mv[0][i][0];                s->p_field_mv_table[i][0][mb_xy][1]= s->mv[0][i][1];            }            s->current_picture.ref_index[0][xy           ]=            s->current_picture.ref_index[0][xy        + 1]= s->field_select[0][0];            s->current_picture.ref_index[0][xy + wrap    ]=            s->current_picture.ref_index[0][xy + wrap + 1]= s->field_select[0][1];        }                /* no update if 8X8 because it has been done during parsing */        s->current_picture.motion_val[0][xy][0] = motion_x;        s->current_picture.motion_val[0][xy][1] = motion_y;        s->current_picture.motion_val[0][xy + 1][0] = motion_x;        s->current_picture.motion_val[0][xy + 1][1] = motion_y;        s->current_picture.motion_val[0][xy + wrap][0] = motion_x;        s->current_picture.motion_val[0][xy + wrap][1] = motion_y;        s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;        s->current_picture.motion_val[0][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 if(s->mb_intra)            s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA;        else            s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_16x16;    }}#ifdef CONFIG_ENCODERSstatic inline int h263_get_motion_length(MpegEncContext * s, int val, int f_code){    int l, bit_size, code;    if (val == 0) {        return mvtab[0][1];    } else {        bit_size = f_code - 1;        /* modulo encoding */        l= INT_BIT - 6 - bit_size;        val = (val<<l)>>l;        val--;        code = (val >> bit_size) + 1;        return mvtab[code][1] + 1 + bit_size;    }}static inline void ff_h263_encode_motion_vector(MpegEncContext * s, int x, int y, int f_code){    if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){        skip_put_bits(&s->pb,             h263_get_motion_length(s, x, f_code)           +h263_get_motion_length(s, y, f_code));    }else{        ff_h263_encode_motion(s, x, f_code);        ff_h263_encode_motion(s, y, f_code);    }}static 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;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -