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

📄 rv34.c

📁 mediastreamer2是开源的网络传输媒体流的库
💻 C
📖 第 1 页 / 共 3 页
字号:
        A[0] = s->current_picture_ptr->motion_val[0][mv_pos-1][0];        A[1] = s->current_picture_ptr->motion_val[0][mv_pos-1][1];    }    if(r->avail_cache[avail_index - 4]){        B[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][0];        B[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][1];    }else{        B[0] = A[0];        B[1] = A[1];    }    if(!r->avail_cache[avail_index - 4 + c_off]){        if(r->avail_cache[avail_index - 4] && (r->avail_cache[avail_index - 1] || r->rv30)){            C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][0];            C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][1];        }else{            C[0] = A[0];            C[1] = A[1];        }    }else{        C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][0];        C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][1];    }    mx = mid_pred(A[0], B[0], C[0]);    my = mid_pred(A[1], B[1], C[1]);    mx += r->dmv[dmv_no][0];    my += r->dmv[dmv_no][1];    for(j = 0; j < part_sizes_h[block_type]; j++){        for(i = 0; i < part_sizes_w[block_type]; i++){            s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;            s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;        }    }}/** * Calculate motion vector component that should be added for direct blocks. */static int calc_add_mv(MpegEncContext *s, int dir, int component){    int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;    int sum;    sum = (s->next_picture_ptr->motion_val[0][mv_pos][component] +           s->next_picture_ptr->motion_val[0][mv_pos + 1][component] +           s->next_picture_ptr->motion_val[0][mv_pos + s->b8_stride][component] +           s->next_picture_ptr->motion_val[0][mv_pos + s->b8_stride + 1][component]) >> 2;    return dir ? -(sum >> 1) : ((sum + 1) >> 1);}/** * Predict motion vector for B-frame macroblock. */static inline void rv34_pred_b_vector(int A[2], int B[2], int C[2],                                      int A_avail, int B_avail, int C_avail,                                      int *mx, int *my){    if(A_avail + B_avail + C_avail != 3){        *mx = A[0] + B[0] + C[0];        *my = A[1] + B[1] + C[1];        if(A_avail + B_avail + C_avail == 2){            *mx /= 2;            *my /= 2;        }    }else{        *mx = mid_pred(A[0], B[0], C[0]);        *my = mid_pred(A[1], B[1], C[1]);    }}/** * motion vector prediction for B-frames */static void rv34_pred_mv_b(RV34DecContext *r, int block_type, int dir){    MpegEncContext *s = &r->s;    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;    int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;    int A[2], B[2], C[2];    int has_A = 0, has_B = 0, has_C = 0;    int mx, my;    int i, j;    Picture *cur_pic = s->current_picture_ptr;    const int mask = dir ? MB_TYPE_L1 : MB_TYPE_L0;    int type = cur_pic->mb_type[mb_pos];    memset(A, 0, sizeof(A));    memset(B, 0, sizeof(B));    memset(C, 0, sizeof(C));    if((r->avail_cache[5-1] & type) & mask){        A[0] = cur_pic->motion_val[dir][mv_pos - 1][0];        A[1] = cur_pic->motion_val[dir][mv_pos - 1][1];        has_A = 1;    }    if((r->avail_cache[5-4] & type) & mask){        B[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][0];        B[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][1];        has_B = 1;    }    if((r->avail_cache[5-2] & type) & mask){        C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][0];        C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][1];        has_C = 1;    }else if((s->mb_x+1) == s->mb_width && (r->avail_cache[5-5] & type) & mask){        C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][0];        C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][1];        has_C = 1;    }    rv34_pred_b_vector(A, B, C, has_A, has_B, has_C, &mx, &my);    mx += r->dmv[dir][0];    my += r->dmv[dir][1];    if(block_type == RV34_MB_B_DIRECT){        mx += calc_add_mv(s, dir, 0);        my += calc_add_mv(s, dir, 1);    }    for(j = 0; j < 2; j++){        for(i = 0; i < 2; i++){            cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;            cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;        }    }    if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD)        fill_rectangle(cur_pic->motion_val[!dir][mv_pos], 2, 2, s->b8_stride, 0, 4);}static const int chroma_coeffs[3] = { 8, 5, 3 };/** * generic motion compensation function * * @param r decoder context * @param block_type type of the current block * @param xoff horizontal offset from the start of the current block * @param yoff vertical offset from the start of the current block * @param mv_off offset to the motion vector information * @param width width of the current partition in 8x8 blocks * @param height height of the current partition in 8x8 blocks */static inline void rv34_mc(RV34DecContext *r, const int block_type,                          const int xoff, const int yoff, int mv_off,                          const int width, const int height, int dir,                          const int thirdpel,                          qpel_mc_func (*qpel_mc)[16],                          h264_chroma_mc_func (*chroma_mc)){    MpegEncContext *s = &r->s;    uint8_t *Y, *U, *V, *srcY, *srcU, *srcV;    int dxy, mx, my, lx, ly, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;    int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride + mv_off;    int is16x16 = 1;    if(thirdpel){        mx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);        my = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);        lx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) % 3;        ly = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) % 3;        uvmx = chroma_coeffs[(3*(mx&1) + lx) >> 1];        uvmy = chroma_coeffs[(3*(my&1) + ly) >> 1];    }else{        mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] >> 2;        my = s->current_picture_ptr->motion_val[dir][mv_pos][1] >> 2;        lx = s->current_picture_ptr->motion_val[dir][mv_pos][0] & 3;        ly = s->current_picture_ptr->motion_val[dir][mv_pos][1] & 3;        uvmx = mx & 6;        uvmy = my & 6;    }    dxy = ly*4 + lx;    srcY = dir ? s->next_picture_ptr->data[0] : s->last_picture_ptr->data[0];    srcU = dir ? s->next_picture_ptr->data[1] : s->last_picture_ptr->data[1];    srcV = dir ? s->next_picture_ptr->data[2] : s->last_picture_ptr->data[2];    src_x = s->mb_x * 16 + xoff + mx;    src_y = s->mb_y * 16 + yoff + my;    uvsrc_x = s->mb_x * 8 + (xoff >> 1) + (mx >> 1);    uvsrc_y = s->mb_y * 8 + (yoff >> 1) + (my >> 1);    srcY += src_y * s->linesize + src_x;    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;    if(   (unsigned)(src_x - !!lx*2) > s->h_edge_pos - !!lx*2 - (width <<3) - 3       || (unsigned)(src_y - !!ly*2) > s->v_edge_pos - !!ly*2 - (height<<3) - 3){        uint8_t *uvbuf= s->edge_emu_buffer + 20 * s->linesize;        srcY -= 2 + 2*s->linesize;        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, (width<<3)+4, (height<<3)+4,                            src_x - 2, src_y - 2, s->h_edge_pos, s->v_edge_pos);        srcY = s->edge_emu_buffer + 2 + 2*s->linesize;        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, (width<<2)+1, (height<<2)+1,                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, (width<<2)+1, (height<<2)+1,                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);        srcU = uvbuf;        srcV = uvbuf + 16;    }    Y = s->dest[0] + xoff      + yoff     *s->linesize;    U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;    V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;    if(block_type == RV34_MB_P_16x8){        qpel_mc[1][dxy](Y, srcY, s->linesize);        Y    += 8;        srcY += 8;    }else if(block_type == RV34_MB_P_8x16){        qpel_mc[1][dxy](Y, srcY, s->linesize);        Y    += 8 * s->linesize;        srcY += 8 * s->linesize;    }    is16x16 = (block_type != RV34_MB_P_8x8) && (block_type != RV34_MB_P_16x8) && (block_type != RV34_MB_P_8x16);    qpel_mc[!is16x16][dxy](Y, srcY, s->linesize);    chroma_mc[2-width]   (U, srcU, s->uvlinesize, height*4, uvmx, uvmy);    chroma_mc[2-width]   (V, srcV, s->uvlinesize, height*4, uvmx, uvmy);}static void rv34_mc_1mv(RV34DecContext *r, const int block_type,                        const int xoff, const int yoff, int mv_off,                        const int width, const int height, int dir){    rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30,            r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab                    : r->s.dsp.put_h264_qpel_pixels_tab,            r->s.dsp.put_h264_chroma_pixels_tab);}static void rv34_mc_2mv(RV34DecContext *r, const int block_type){    rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30,            r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab                    : r->s.dsp.put_h264_qpel_pixels_tab,            r->s.dsp.put_h264_chroma_pixels_tab);    rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30,            r->rv30 ? r->s.dsp.avg_rv30_tpel_pixels_tab                    : r->s.dsp.avg_h264_qpel_pixels_tab,            r->s.dsp.avg_h264_chroma_pixels_tab);}/** number of motion vectors in each macroblock type */static const int num_mvs[RV34_MB_TYPES] = { 0, 0, 1, 4, 1, 1, 0, 0, 2, 2, 2, 1 };/** * Decode motion vector differences * and perform motion vector reconstruction and motion compensation. */static int rv34_decode_mv(RV34DecContext *r, int block_type){    MpegEncContext *s = &r->s;    GetBitContext *gb = &s->gb;    int i;    memset(r->dmv, 0, sizeof(r->dmv));    for(i = 0; i < num_mvs[block_type]; i++){        r->dmv[i][0] = svq3_get_se_golomb(gb);        r->dmv[i][1] = svq3_get_se_golomb(gb);    }    switch(block_type){    case RV34_MB_TYPE_INTRA:    case RV34_MB_TYPE_INTRA16x16:        fill_rectangle(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], 2, 2, s->b8_stride, 0, 4);        return 0;    case RV34_MB_SKIP:        if(s->pict_type == P_TYPE){            fill_rectangle(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], 2, 2, s->b8_stride, 0, 4);            rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);            break;        }    case RV34_MB_B_DIRECT:        rv34_pred_mv_b  (r, RV34_MB_B_DIRECT, 0);        rv34_pred_mv_b  (r, RV34_MB_B_DIRECT, 1);        rv34_mc_2mv     (r, RV34_MB_B_DIRECT);        break;    case RV34_MB_P_16x16:    case RV34_MB_P_MIX16x16:        rv34_pred_mv(r, block_type, 0, 0);        rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);        break;    case RV34_MB_B_FORWARD:    case RV34_MB_B_BACKWARD:        r->dmv[1][0] = r->dmv[0][0];        r->dmv[1][1] = r->dmv[0][1];        rv34_pred_mv_b  (r, block_type, block_type == RV34_MB_B_BACKWARD);        rv34_mc_1mv     (r, block_type, 0, 0, 0, 2, 2, block_type == RV34_MB_B_BACKWARD);        break;    case RV34_MB_P_16x8:    case RV34_MB_P_8x16:        rv34_pred_mv(r, block_type, 0, 0);        rv34_pred_mv(r, block_type, 1 + (block_type == RV34_MB_P_16x8), 1);        if(block_type == RV34_MB_P_16x8){            rv34_mc_1mv(r, block_type, 0, 0, 0,            2, 1, 0);            rv34_mc_1mv(r, block_type, 0, 8, s->b8_stride, 2, 1, 0);        }        if(block_type == RV34_MB_P_8x16){            rv34_mc_1mv(r, block_type, 0, 0, 0, 1, 2, 0);            rv34_mc_1mv(r, block_type, 8, 0, 1, 1, 2, 0);        }        break;    case RV34_MB_B_BIDIR:        rv34_pred_mv_b  (r, block_type, 0);        rv34_pred_mv_b  (r, block_type, 1);        rv34_mc_2mv     (r, block_type);        break;    case RV34_MB_P_8x8:        for(i=0;i< 4;i++){            rv34_pred_mv(r, block_type, i, i);            rv34_mc_1mv (r, block_type, (i&1)<<3, (i&2)<<2, (i&1)+(i>>1)*s->b8_stride, 1, 1, 0);        }        break;    }    return 0;}/** @} */ // mv group/** * @defgroup recons Macroblock reconstruction functions * @{ *//** mapping of RV30/40 intra prediction types to standard H.264 types */static const int ittrans[9] = { DC_PRED, VERT_PRED, HOR_PRED, DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_LEFT_PRED, VERT_RIGHT_PRED, VERT_LEFT_PRED, HOR_UP_PRED, HOR_DOWN_PRED,};/** mapping of RV30/40 intra 16x16 prediction types to standard H.264 types */static const int ittrans16[4] = { DC_PRED8x8, VERT_PRED8x8, HOR_PRED8x8, PLANE_PRED8x8,};/** * Perform 4x4 intra prediction. */static void rv34_pred_4x4_block(RV34DecContext *r, uint8_t *dst, int stride, int itype, int up, int left, int down, int right){    uint8_t *prev = dst - stride + 4;    uint32_t topleft;    if(!up && !left)        itype = DC_128_PRED;    else if(!up){        if(itype == VERT_PRED) itype = HOR_PRED;        if(itype == DC_PRED)   itype = LEFT_DC_PRED;    }else if(!left){        if(itype == HOR_PRED)  itype = VERT_PRED;        if(itype == DC_PRED)   itype = TOP_DC_PRED;        if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;    }    if(!down){        if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;        if(itype == HOR_UP_PRED) itype = HOR_UP_PRED_RV40_NODOWN;        if(itype == VERT_LEFT_PRED) itype = VERT_LEFT_PRED_RV40_NODOWN;    }    if(!right && up){        topleft = dst[-stride + 3] * 0x01010101;        prev = &topleft;    }    r->h.pred4x4[itype](dst, prev, stride);}/** add_pixels_clamped for 4x4 block */static void rv34_add_4x4_block(uint8_t *dst, int stride, DCTELEM block[64], int off){    int x, y;    for(y = 0; y < 4; y++)        for(x = 0; x < 4; x++)            dst[x + y*stride] = av_clip_uint8(dst[x + y*stride] + block[off + x+y*8]);}static inline int adjust_pred16(int itype, int up, int left){    if(!up && !left)        itype = DC_128_PRED8x8;    else if(!up){        if(itype == PLANE_PRED8x8)itype = HOR_PRED8x8;        if(itype == VERT_PRED8x8) itype = HOR_PRED8x8;        if(itype == DC_PRED8x8)   itype = LEFT_DC_PRED8x8;    }else if(!left){        if(itype == PLANE_PRED8x8)itype = VERT_PRED8x8;        if(itype == HOR_PRED8x8)  itype = VERT_PRED8x8;        if(itype == DC_PRED8x8)   itype = TOP_DC_PRED8x8;    }    return itype;}static void rv34_output_macroblock(RV34DecContext *r, int8_t *intra_types, int cbp, int is16){    MpegEncContext *s = &r->s;    DSPContext *dsp = &s->dsp;    int i, j;    uint8_t *Y, *U, *V;    int itype;    int avail[6*8] = {0};    int idx;    // Set neighbour information.    if(r->avail_cache[0])        avail[0] = 1;    if(r->avail_cache[1])        avail[1] = avail[2] = 1;    if(r->avail_cache[2])        avail[3] = avail[4] = 1;    if(r->avail_cache[3])        avail[5] = 1;    if(r->avail_cache[4])        avail[8] = avail[16] = 1;    if(r->avail_cache[8])        avail[24] = avail[32] = 1;    Y = s->dest[0];    U = s->dest[1];    V = s->dest[2];    if(!is16){        for(j = 0; j < 4; j++){            idx = 9 + j*8;            for(i = 0; i < 4; i++, cbp >>= 1, Y += 4, idx++){                rv34_pred_4x4_block(r, Y, s->linesize, ittrans[intra_types[i]], avail[idx-8], avail[idx-1], avail[idx+7], avail[idx-7]);                avail[idx] = 1;                if(cbp & 1)                    rv34_add_4x4_block(Y, s->linesize, s->block[(i>>1)+(j&2)], (i&1)*4+(j&1)*32);            }            Y += s->linesize * 4 - 4*4;            intra_types += s->b4_stride;        }        intra_types -= s->b4_stride * 4;        fill_rectangle(r->avail_cache + 5, 2, 2, 4, 0, 4);        for(j = 0; j < 2; j++){            idx = 5 + j*4;            for(i = 0; i < 2; i++, cbp >>= 1, idx++){                rv34_pred_4x4_block(r, U + i*4 + j*4*s->uvlinesize, s->uvlinesize, ittrans[intra_types[i*2+j*2*s->b4_stride]], r->avail_cache[idx-4], r->avail_cache[idx-1], !i && !j, r->avail_cache[idx-3]);                rv34_pred_4x4_block(r, V + i*4 + j*4*s->uvlinesize, s->uvlinesize, ittrans[intra_types[i*2+j*2*s->b4_stride]], r->avail_cache[idx-4], r->avail_cache[idx-1], !i && !j, r->avail_cache[idx-3]);                r->avail_cache[idx] = 1;                if(cbp & 0x01)                    rv34_add_4x4_block(U + i*4 + j*4*s->uvlinesize, s->uvlinesize, s->block[4], i*4+j*32);                if(cbp & 0x10)                    rv34_add_4x4_block(V + i*4 + j*4*s->uvlinesize, s->uvlinesize, s->block[5], i*4+j*32);            }        }

⌨️ 快捷键说明

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