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

📄 mpegvideo.c

📁 Trolltech公司发布的图形界面操作系统。可在qt-embedded-2.3.7平台上编译为嵌入式图形界面操作系统。
💻 C
📖 第 1 页 / 共 5 页
字号:
    if (s->out_format != FMT_MJPEG) {        convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,                        s->intra_matrix, s->intra_quant_bias, 1, 31);        convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,                        s->inter_matrix, s->inter_quant_bias, 1, 31);    }    if(ff_rate_control_init(s) < 0)        return -1;    s->picture_number = 0;    s->picture_in_gop_number = 0;    s->fake_picture_number = 0;    /* motion detector init */    s->f_code = 1;    s->b_code = 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);          return 0;}/* 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 *buf, int wrap, int width, int height, int w){    UINT8 *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 */    }}/* generic function for encode/decode called before a frame is coded/decoded */int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx){    int i;    UINT8 *tmp;    s->mb_skiped = 0;    avctx->mbskip_table= s->mbskip_table;    if(avctx->flags&CODEC_FLAG_DR1){        if(avctx->get_buffer_callback(avctx, s->width, s->height, s->pict_type) < 0){            fprintf(stderr, "get_buffer() failed\n");            return -1;        }        s->linesize  = avctx->dr_stride;        s->uvlinesize= avctx->dr_uvstride;        s->ip_buffer_count= avctx->dr_ip_buffer_count;    }    avctx->dr_ip_buffer_count= s->ip_buffer_count;        if (s->pict_type == B_TYPE) {        for(i=0;i<3;i++) {            if(avctx->flags&CODEC_FLAG_DR1)                s->aux_picture[i]= avctx->dr_buffer[i];                        //FIXME the following should never be needed, the decoder should drop b frames if no reference is available            if(s->next_picture[i]==NULL)                s->next_picture[i]= s->aux_picture[i];            if(s->last_picture[i]==NULL)                s->last_picture[i]= s->next_picture[i];            s->current_picture[i] = s->aux_picture[i];        }        s->avctx->display_qscale_table=        s->avctx->current_qscale_table=         s->qscale_table= s->aux_qscale_table;    } else {        for(i=0;i<3;i++) {            /* swap next and last */            if(avctx->flags&CODEC_FLAG_DR1)                tmp= avctx->dr_buffer[i];            else                tmp = s->last_picture[i];            s->last_picture[i] = s->next_picture[i];            s->next_picture[i] = tmp;            s->current_picture[i] = tmp;            if(s->last_picture[i]==NULL)                s->last_picture[i]= s->next_picture[i];            s->last_dr_opaque= s->next_dr_opaque;            s->next_dr_opaque= avctx->dr_opaque_frame;            if(s->has_b_frames && s->last_dr_opaque && s->codec_id!=CODEC_ID_SVQ1)                avctx->dr_opaque_frame= s->last_dr_opaque;            else                avctx->dr_opaque_frame= s->next_dr_opaque;        }        s->avctx->current_qscale_table= s->qscale_table      = s->last_qscale_table;        s->avctx->display_qscale_table= s->last_qscale_table = s->next_qscale_table;        s->next_qscale_table= s->qscale_table;    }    /* set dequantizer, we cant do it during init as it might change for mpeg4       and we cant do it in the header decode as init isnt called for mpeg4 there yet */    if(s->out_format == FMT_H263){        if(s->mpeg_quant)            s->dct_unquantize = s->dct_unquantize_mpeg2;        else            s->dct_unquantize = s->dct_unquantize_h263;    }else         s->dct_unquantize = s->dct_unquantize_mpeg1;    return 0;}/* generic function for encode/decode called after a frame has been coded/decoded */void MPV_frame_end(MpegEncContext *s){    s->avctx->key_frame   = (s->pict_type == I_TYPE);    s->avctx->pict_type   = s->pict_type;    /* draw edge for correct motion prediction if outside */    if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {        draw_edges(s->current_picture[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );        draw_edges(s->current_picture[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);        draw_edges(s->current_picture[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);    }    emms_c();        s->last_pict_type    = s->pict_type;    if(s->pict_type!=B_TYPE){        s->last_non_b_pict_type= s->pict_type;        s->num_available_buffers++;        if(s->num_available_buffers>2) s->num_available_buffers= 2;    }}/* reorder input for encoding */void reorder_input(MpegEncContext *s, AVPicture *pict){    int i, j, index;                if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;//        delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)    for(j=0; j<REORDER_BUFFER_SIZE-1; j++){        s->coded_order[j]= s->coded_order[j+1];    }    s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers    s->coded_order[j].pict_type=0;    switch(s->input_pict_type){    default:     case I_TYPE:    case S_TYPE:    case P_TYPE:        index= s->max_b_frames - s->b_frames_since_non_b;        s->b_frames_since_non_b=0;        break;                case B_TYPE:        index= s->max_b_frames + 1;        s->b_frames_since_non_b++;        break;              }//printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);    if(   (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))       && pict->linesize[0] == s->linesize       && pict->linesize[1] == s->uvlinesize       && pict->linesize[2] == s->uvlinesize){//printf("ptr\n");        for(i=0; i<3; i++){            s->coded_order[index].picture[i]= pict->data[i];        }    }else{//printf("copy\n");        for(i=0; i<3; i++){            uint8_t *src = pict->data[i];            uint8_t *dest;            int src_wrap = pict->linesize[i];            int dest_wrap = s->linesize;            int w = s->width;            int h = s->height;            if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering            else         dest= s->picture_buffer[s->picture_buffer_index][i];            if (i >= 1) {                dest_wrap >>= 1;                w >>= 1;                h >>= 1;            }            s->coded_order[index].picture[i]= dest;            for(j=0;j<h;j++) {                memcpy(dest, src, w);                dest += dest_wrap;                src += src_wrap;            }        }        if(index!=0){            s->picture_buffer_index++;            if(s->picture_buffer_index >= REORDER_BUFFER_SIZE) s->picture_buffer_index=0;        }    }    s->coded_order[index].pict_type = s->input_pict_type;    s->coded_order[index].qscale    = s->input_qscale;    s->coded_order[index].force_type= s->force_input_type;    s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;    s->coded_order[index].picture_number= s->input_picture_number;    for(i=0; i<3; i++){        s->new_picture[i]= s->coded_order[0].picture[i];    }}int MPV_encode_picture(AVCodecContext *avctx,                       unsigned char *buf, int buf_size, void *data){    MpegEncContext *s = avctx->priv_data;    AVPicture *pict = data;    s->input_qscale = avctx->quality;    init_put_bits(&s->pb, buf, buf_size, NULL, NULL);    if(avctx->force_type){        s->input_pict_type=        s->force_input_type= avctx->force_type;    }else if(s->flags&CODEC_FLAG_PASS2){        s->input_pict_type=        s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;    }else{        s->force_input_type=0;        if (!s->intra_only) {            /* first picture of GOP is intra */            if (s->input_picture_in_gop_number % s->gop_size==0){                s->input_pict_type = I_TYPE;            }else if(s->max_b_frames==0){                s->input_pict_type = P_TYPE;            }else{                if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ                    s->input_pict_type = B_TYPE;                else                    s->input_pict_type = P_TYPE;            }        } else {            s->input_pict_type = I_TYPE;        }    }    if(s->input_pict_type==I_TYPE)        s->input_picture_in_gop_number=0;        reorder_input(s, pict);        /* output? */    if(s->coded_order[0].picture[0]){        s->pict_type= s->coded_order[0].pict_type;        if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */            s->qscale= s->coded_order[0].qscale;        s->force_type= s->coded_order[0].force_type;        s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;        s->picture_number= s->coded_order[0].picture_number;        MPV_frame_start(s, avctx);        encode_picture(s, s->picture_number);                avctx->real_pict_num  = s->picture_number;        avctx->header_bits = s->header_bits;        avctx->mv_bits     = s->mv_bits;        avctx->misc_bits   = s->misc_bits;        avctx->i_tex_bits  = s->i_tex_bits;        avctx->p_tex_bits  = s->p_tex_bits;        avctx->i_count     = s->i_count;        avctx->p_count     = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx        avctx->skip_count  = s->skip_count;        MPV_frame_end(s);        if (s->out_format == FMT_MJPEG)            mjpeg_picture_trailer(s);        if(!s->fixed_qscale)            avctx->quality = s->qscale;                if(s->flags&CODEC_FLAG_PASS1)            ff_write_pass1_stats(s);        }    s->input_picture_number++;    s->input_picture_in_gop_number++;    flush_put_bits(&s->pb);    s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;        s->total_bits += s->frame_bits;    avctx->frame_bits  = s->frame_bits;//printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", //s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits);#if 0 //dump some stats to stats.txt for testing/debugingif(s->max_b_frames==0){    static FILE *f=NULL;    if(!f) f= fopen("stats.txt", "wb");    get_psnr(pict->data, s->current_picture,             pict->linesize, s->linesize, avctx);    fprintf(f, "%7d, %7d, %2.4f\n", pbBufPtr(&s->pb) - s->pb.buf, s->qscale, avctx->psnr_y);}#endif    if (avctx->get_psnr) {        /* At this point pict->data should have the original frame   */        /* an s->current_picture should have the coded/decoded frame */        get_psnr(pict->data, s->current_picture,                 pict->linesize, s->linesize, avctx);//        printf("%f\n", avctx->psnr_y);    }    return pbBufPtr(&s->pb) - s->pb.buf;}static inline void gmc1_motion(MpegEncContext *s,                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,                               int dest_offset,                               UINT8 **ref_picture, int src_offset){    UINT8 *ptr;    int offset, src_x, src_y, linesize, uvlinesize;    int motion_x, motion_y;    int emu=0;    motion_x= s->sprite_offset[0][0];    motion_y= s->sprite_offset[0][1];    src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));    src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));    motion_x<<=(3-s->sprite_warping_accuracy);    motion_y<<=(3-s->sprite_warping_accuracy);    src_x = clip(src_x, -16, s->width);    if (src_x == s->width)        motion_x =0;    src_y = clip(src_y, -16, s->height);    if (src_y == s->height)

⌨️ 快捷键说明

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