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

📄 mpegvideo.c

📁 arm平台下的H264编码和解码源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
        break;    case CODEC_ID_FLV1:        s->out_format = FMT_H263;        s->h263_flv = 2; /* format = 1; 11-bit codes */        s->unrestricted_mv = 1;        s->rtp_mode=0; /* don't allow GOB */        avctx->delay=0;        s->low_delay=1;        break;    case CODEC_ID_RV10:        s->out_format = FMT_H263;        avctx->delay=0;        s->low_delay=1;        break;    case CODEC_ID_MPEG4:        s->out_format = FMT_H263;        s->h263_pred = 1;        s->unrestricted_mv = 1;        s->low_delay= s->max_b_frames ? 0 : 1;        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);        break;    case CODEC_ID_MSMPEG4V1:        s->out_format = FMT_H263;        s->h263_msmpeg4 = 1;        s->h263_pred = 1;        s->unrestricted_mv = 1;        s->msmpeg4_version= 1;        avctx->delay=0;        s->low_delay=1;        break;    case CODEC_ID_MSMPEG4V2:        s->out_format = FMT_H263;        s->h263_msmpeg4 = 1;        s->h263_pred = 1;        s->unrestricted_mv = 1;        s->msmpeg4_version= 2;        avctx->delay=0;        s->low_delay=1;        break;    case CODEC_ID_MSMPEG4V3:        s->out_format = FMT_H263;        s->h263_msmpeg4 = 1;        s->h263_pred = 1;        s->unrestricted_mv = 1;        s->msmpeg4_version= 3;        s->flipflop_rounding=1;        avctx->delay=0;        s->low_delay=1;        break;    case CODEC_ID_WMV1:        s->out_format = FMT_H263;        s->h263_msmpeg4 = 1;        s->h263_pred = 1;        s->unrestricted_mv = 1;        s->msmpeg4_version= 4;        s->flipflop_rounding=1;        avctx->delay=0;        s->low_delay=1;        break;    case CODEC_ID_WMV2:        s->out_format = FMT_H263;        s->h263_msmpeg4 = 1;        s->h263_pred = 1;        s->unrestricted_mv = 1;        s->msmpeg4_version= 5;        s->flipflop_rounding=1;        avctx->delay=0;        s->low_delay=1;        break;#endif    default:        return -1;    }    s->encoding = 1;    /* init */    if (MPV_common_init(s) < 0)        return -1;    if(s->modified_quant)        s->chroma_qscale_table= ff_h263_chroma_qscale_table;    s->progressive_frame=     s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME));    s->quant_precision=5;        ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);        ff_init_me(s);#ifdef CONFIG_ENCODERS#ifdef CONFIG_RISKY    if (s->out_format == FMT_H263)        h263_encode_init(s);    if(s->msmpeg4_version)        ff_msmpeg4_encode_init(s);#endif    if (s->out_format == FMT_MPEG1)        ff_mpeg1_encode_init(s);#endif    /* init q matrix */    for(i=0;i<64;i++) {        int j= s->dsp.idct_permutation[i];#ifdef CONFIG_RISKY        if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];        }else if(s->out_format == FMT_H263){            s->intra_matrix[j] =            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];        }else#endif        { /* mpeg1/2 */            s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];        }        if(s->avctx->intra_matrix)            s->intra_matrix[j] = s->avctx->intra_matrix[i];        if(s->avctx->inter_matrix)            s->inter_matrix[j] = s->avctx->inter_matrix[i];    }    /* precompute matrix */    /* for mjpeg, we do include qscale in the matrix */    if (s->out_format != FMT_MJPEG) {        convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,                        s->intra_matrix, s->intra_quant_bias, 1, 31);        convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,                        s->inter_matrix, s->inter_quant_bias, 1, 31);    }    if(ff_rate_control_init(s) < 0)        return -1;        return 0;}int MPV_encode_end(AVCodecContext *avctx){    MpegEncContext *s = avctx->priv_data;#ifdef STATS    print_stats();#endif    ff_rate_control_uninit(s);    MPV_common_end(s);    if (s->out_format == FMT_MJPEG)        mjpeg_close(s);    av_freep(&avctx->extradata);          return 0;}#endif //CONFIG_ENCODERS
void init_rl(RLTable *rl){    int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];    uint8_t index_run[MAX_RUN+1];    int last, run, level, start, end, i;    /* compute max_level[], max_run[] and index_run[] */    for(last=0;last<2;last++) {        if (last == 0) {            start = 0;            end = rl->last;        } else {            start = rl->last;            end = rl->n;        }        memset(max_level, 0, MAX_RUN + 1);        memset(max_run, 0, MAX_LEVEL + 1);        memset(index_run, rl->n, MAX_RUN + 1);        for(i=start;i<end;i++) {            run = rl->table_run[i];            level = rl->table_level[i];            if (index_run[run] == rl->n)                index_run[run] = i;            if (level > max_level[run])                max_level[run] = level;            if (run > max_run[level])                max_run[level] = run;        }        rl->max_level[last] = av_malloc(MAX_RUN + 1);        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);        rl->max_run[last] = av_malloc(MAX_LEVEL + 1);        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);        rl->index_run[last] = av_malloc(MAX_RUN + 1);        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);    }}/* draw the edges of width 'w' of an image of size width, height *///FIXME check that this is ok for mpeg4 interlacedstatic void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w){    uint8_t *ptr, *last_line;    int i;    last_line = buf + (height - 1) * wrap;    for(i=0;i<w;i++) {        /* top and bottom */        memcpy(buf - (i + 1) * wrap, buf, width);        memcpy(last_line + (i + 1) * wrap, last_line, width);    }    /* left and right */    ptr = buf;    for(i=0;i<height;i++) {        memset(ptr - w, ptr[0], w);        memset(ptr + width, ptr[width-1], w);        ptr += wrap;    }    /* corners */    for(i=0;i<w;i++) {        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */    }}int ff_find_unused_picture(MpegEncContext *s, int shared){    int i;        if(shared){        for(i=0; i<MAX_PICTURE_COUNT; i++){            if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;        }    }else{        for(i=0; i<MAX_PICTURE_COUNT; i++){            if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME        }        for(i=0; i<MAX_PICTURE_COUNT; i++){            if(s->picture[i].data[0]==NULL) return i;        }    }#ifndef WINCE //assert    assert(0);
#endif    return -1;}static void update_noise_reduction(MpegEncContext *s){    int intra, i;    for(intra=0; intra<2; intra++){        if(s->dct_count[intra] > (1<<16)){            for(i=0; i<64; i++){                s->dct_error_sum[intra][i] >>=1;            }            s->dct_count[intra] >>= 1;        }                for(i=0; i<64; i++){            s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);        }    }}/** * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded */int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx){    int i;    AVFrame *pic;    s->mb_skiped = 0;#ifndef WINCE //assert    assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);#endif    /* mark&release old frames */    if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr->data[0]) {        avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);        /* release forgotten pictures */        /* if(mpeg124/h263) */        if(!s->encoding){            for(i=0; i<MAX_PICTURE_COUNT; i++){                if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){                    av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");                    avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);                                }            }        }    }alloc:    if(!s->encoding){        /* release non refernce frames */        for(i=0; i<MAX_PICTURE_COUNT; i++){            if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){                s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);            }        }        if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)            pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)        else{            i= ff_find_unused_picture(s, 0);            pic= (AVFrame*)&s->picture[i];        }        pic->reference= s->pict_type != B_TYPE ? 3 : 0;        pic->coded_picture_number= s->coded_picture_number++;                if( alloc_picture(s, (Picture*)pic, 0) < 0)            return -1;        s->current_picture_ptr= (Picture*)pic;        s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic        s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;    }    s->current_picture_ptr->pict_type= s->pict_type;//    if(s->flags && CODEC_FLAG_QSCALE)   //      s->current_picture_ptr->quality= s->new_picture_ptr->quality;    s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;    copy_picture(&s->current_picture, s->current_picture_ptr);    if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){    if (s->pict_type != B_TYPE) {        s->last_picture_ptr= s->next_picture_ptr;        s->next_picture_ptr= s->current_picture_ptr;    }        if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);    if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);        if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){        av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");#ifndef WINCE //assert
        assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference#endif
		goto alloc;    }#ifndef WINCE //assert    assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));#endif    if(s->picture_structure!=PICT_FRAME){        int i;        for(i=0; i<4; i++){            if(s->picture_structure == PICT_BOTTOM_FIELD){                 s->current_picture.data[i] += s->current_picture.linesize[i];            }             s->current_picture.linesize[i] *= 2;            s->last_picture.linesize[i] *=2;            s->next_picture.linesize[i] *=2;        }    }  }

⌨️ 快捷键说明

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