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

📄 h264.c

📁 arm平台下的H264编码和解码源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
static inline void write_back_motion(H264Context *h, int mb_type){    MpegEncContext * const s = &h->s;    const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;    const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;    int list;    for(list=0; list<2; list++){        int y;        if((!IS_8X8(mb_type)) && !USES_LIST(mb_type, list)){            if(1){ //FIXME skip or never read if mb_type doesnt use it                for(y=0; y<4; y++){                    *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]=                    *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= 0;                }                if( h->pps.cabac ) {                    /* FIXME needed ? */                    for(y=0; y<4; y++){                        *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]=                        *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= 0;                    }                }                for(y=0; y<2; y++){                    *(uint16_t*)s->current_picture.motion_val[list][b8_xy + y*h->b8_stride]= (LIST_NOT_USED&0xFF)*0x0101;                }            }            continue; //FIXME direct mode ...        }                for(y=0; y<4; y++){            *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+0 + 8*y];            *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+2 + 8*y];        }        if( h->pps.cabac ) {            for(y=0; y<4; y++){                *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];                *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];            }        }        for(y=0; y<2; y++){            s->current_picture.ref_index[list][b8_xy + 0 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+0 + 16*y];            s->current_picture.ref_index[list][b8_xy + 1 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+2 + 16*y];        }    }}/** * Decodes a network abstraction layer unit. * @param consumed is the number of bytes used as input * @param length is the length of the array * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp ttailing? * @returns decoded bytes, might be src+1 if no escapes  */static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){    int i, si, di;    uint8_t *dst;//    src[0]&0x80;		//forbidden bit    h->nal_ref_idc= src[0]>>5;    h->nal_unit_type= src[0]&0x1F;    src++; length--;#if 0        for(i=0; i<length; i++)        printf("%2X ", src[i]);#endif    for(i=0; i+1<length; i+=2){        if(src[i]) continue;        if(i>0 && src[i-1]==0) i--;        if(i+2<length && src[i+1]==0 && src[i+2]<=3){            if(src[i+2]!=3){                /* startcode, so we must be past the end */                length=i;            }            break;        }    }    if(i>=length-1){ //no escaped 0        *dst_length= length;        *consumed= length+1; //+1 for the header        return src;     }    h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);    dst= h->rbsp_buffer;//printf("deoding esc\n");    si=di=0;    while(si<length){         //remove escapes (very rare 1:2^22)        if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){            if(src[si+2]==3){ //escape                dst[di++]= 0;                dst[di++]= 0;                si+=3;            }else //next start code                break;        }        dst[di++]= src[si++];    }    *dst_length= di;    *consumed= si + 1;//+1 for the header//FIXME store exact number of bits in the getbitcontext (its needed for decoding)    return dst;}#if 0/** * @param src the data which should be escaped * @param dst the target buffer, dst+1 == src is allowed as a special case * @param length the length of the src data * @param dst_length the length of the dst array * @returns length of escaped data in bytes or -1 if an error occured */static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){    int i, escape_count, si, di;    uint8_t *temp;        assert(length>=0);    assert(dst_length>0);        dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;    if(length==0) return 1;    escape_count= 0;    for(i=0; i<length; i+=2){        if(src[i]) continue;        if(i>0 && src[i-1]==0)             i--;        if(i+2<length && src[i+1]==0 && src[i+2]<=3){            escape_count++;            i+=2;        }    }        if(escape_count==0){         if(dst+1 != src)            memcpy(dst+1, src, length);        return length + 1;    }        if(length + escape_count + 1> dst_length)        return -1;    //this should be damn rare (hopefully)    h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);    temp= h->rbsp_buffer;//printf("encoding esc\n");        si= 0;    di= 0;    while(si < length){        if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){            temp[di++]= 0; si++;            temp[di++]= 0; si++;            temp[di++]= 3;             temp[di++]= src[si++];        }        else            temp[di++]= src[si++];    }    memcpy(dst+1, temp, length+escape_count);        assert(di == length+escape_count);        return di + 1;}/** * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4 */static void encode_rbsp_trailing(PutBitContext *pb){    int length;    put_bits(pb, 1, 1);    length= (-put_bits_count(pb))&7;    if(length) put_bits(pb, length, 0);}#endif/** * identifies the exact end of the bitstream * @return the length of the trailing, or 0 if damaged */static int decode_rbsp_trailing(uint8_t *src){    int v= *src;    int r;    tprintf("rbsp trailing %X\n", v);    for(r=1; r<9; r++){        if(v&1) return r;        v>>=1;    }    return 0;}/** * idct tranforms the 16 dc values and dequantize them. * @param qp quantization parameter */static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp){    const int qmul= dequant_coeff[qp][0];#define stride 16    int i;    int temp[16]; //FIXME check if this is a good idea    static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};    static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};//memset(block, 64, 2*256);//return;    for(i=0; i<4; i++){        const int offset= y_offset[i];        const int z0= block[offset+stride*0] + block[offset+stride*4];        const int z1= block[offset+stride*0] - block[offset+stride*4];        const int z2= block[offset+stride*1] - block[offset+stride*5];        const int z3= block[offset+stride*1] + block[offset+stride*5];        temp[4*i+0]= z0+z3;        temp[4*i+1]= z1+z2;        temp[4*i+2]= z1-z2;        temp[4*i+3]= z0-z3;    }    for(i=0; i<4; i++){        const int offset= x_offset[i];        const int z0= temp[4*0+i] + temp[4*2+i];        const int z1= temp[4*0+i] - temp[4*2+i];        const int z2= temp[4*1+i] - temp[4*3+i];        const int z3= temp[4*1+i] + temp[4*3+i];        block[stride*0 +offset]= ((z0 + z3)*qmul + 2)>>2; //FIXME think about merging this into decode_resdual        block[stride*2 +offset]= ((z1 + z2)*qmul + 2)>>2;        block[stride*8 +offset]= ((z1 - z2)*qmul + 2)>>2;        block[stride*10+offset]= ((z0 - z3)*qmul + 2)>>2;    }}#if 0/** * dct tranforms the 16 dc values. * @param qp quantization parameter ??? FIXME */static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){//    const int qmul= dequant_coeff[qp][0];    int i;    int temp[16]; //FIXME check if this is a good idea    static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};    static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};    for(i=0; i<4; i++){        const int offset= y_offset[i];        const int z0= block[offset+stride*0] + block[offset+stride*4];        const int z1= block[offset+stride*0] - block[offset+stride*4];        const int z2= block[offset+stride*1] - block[offset+stride*5];        const int z3= block[offset+stride*1] + block[offset+stride*5];        temp[4*i+0]= z0+z3;        temp[4*i+1]= z1+z2;        temp[4*i+2]= z1-z2;        temp[4*i+3]= z0-z3;    }    for(i=0; i<4; i++){        const int offset= x_offset[i];        const int z0= temp[4*0+i] + temp[4*2+i];        const int z1= temp[4*0+i] - temp[4*2+i];        const int z2= temp[4*1+i] - temp[4*3+i];        const int z3= temp[4*1+i] + temp[4*3+i];        block[stride*0 +offset]= (z0 + z3)>>1;        block[stride*2 +offset]= (z1 + z2)>>1;        block[stride*8 +offset]= (z1 - z2)>>1;        block[stride*10+offset]= (z0 - z3)>>1;    }}#endif#undef xStride#undef stridestatic void chroma_dc_dequant_idct_c(DCTELEM *block, int qp){    const int qmul= dequant_coeff[qp][0];    const int stride= 16*2;    const int xStride= 16;    int a,b,c,d,e;    a= block[stride*0 + xStride*0];    b= block[stride*0 + xStride*1];    c= block[stride*1 + xStride*0];    d= block[stride*1 + xStride*1];    e= a-b;    a= a+b;    b= c-d;    c= c+d;    block[stride*0 + xStride*0]= ((a+c)*qmul + 0)>>1;    block[stride*0 + xStride*1]= ((e+b)*qmul + 0)>>1;    block[stride*1 + xStride*0]= ((a-c)*qmul + 0)>>1;    block[stride*1 + xStride*1]= ((e-b)*qmul + 0)>>1;}#if 0static void chroma_dc_dct_c(DCTELEM *block){    const int stride= 16*2;    const int xStride= 16;    int a,b,c,d,e;    a= block[stride*0 + xStride*0];    b= block[stride*0 + xStride*1];    c= block[stride*1 + xStride*0];    d= block[stride*1 + xStride*1];    e= a-b;    a= a+b;    b= c-d;    c= c+d;    block[stride*0 + xStride*0]= (a+c);    block[stride*0 + xStride*1]= (e+b);    block[stride*1 + xStride*0]= (a-c);    block[stride*1 + xStride*1]= (e-b);}#endif/** * gets the chroma qp. */static inline int get_chroma_qp(H264Context *h, int qscale){        return chroma_qp[clip(qscale + h->pps.chroma_qp_index_offset, 0, 51)];}/** * */static void h264_add_idct_c(uint8_t *dst, DCTELEM *block, int stride){    int i;    uint8_t *cm = cropTbl + MAX_NEG_CROP;    block[0] += 32;    for(i=0; i<4; i++){        const int z0=  block[0 + 4*i]     +  block[2 + 4*i];        const int z1=  block[0 + 4*i]     -  block[2 + 4*i];        const int z2= (block[1 + 4*i]>>1) -  block[3 + 4*i];        const int z3=  block[1 + 4*i]     + (block[3 + 4*i]>>1);        block[0 + 4*i]= z0 + z3;        block[1 + 4*i]= z1 + z2;        block[2 + 4*i]= z1 - z2;        block[3 + 4*i]= z0 - z3;    }    for(i=0; i<4; i++){        const int z0=  block[i + 4*0]     +  block[i + 4*2];        const int z1=  block[i + 4*0]     -  block[i + 4*2];        const int z2= (block[i + 4*1]>>1) -  block[i + 4*3];        const int z3=  block[i + 4*1]     + (block[i + 4*3]>>1);        dst[i + 0*stride]= cm[ dst[i + 0*stride] + ((z0 + z3) >> 6) ];        dst[i + 1*stride]= cm[ dst[i + 1*stride] + ((z1 + z2) >> 6) ];        dst[i + 2*stride]= cm[ dst[i + 2*stride] + ((z1 - z2) >> 6) ];

⌨️ 快捷键说明

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