📄 mpegvideo.c
字号:
}}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; } } assert(0); 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; assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3); /* 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; if(s->current_picture_ptr) //FIXME broken, we need a coded_picture_number in MpegEncContext pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1; if( alloc_picture(s, (Picture*)pic, 0) < 0) return -1; s->current_picture_ptr= (Picture*)pic; } 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"); assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference goto alloc; } assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0])); 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; } } } s->hurry_up= s->avctx->hurry_up; s->error_resilience= avctx->error_resilience; /* 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->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO) s->dct_unquantize = s->dct_unquantize_mpeg2; else if(s->out_format == FMT_H263) s->dct_unquantize = s->dct_unquantize_h263; else s->dct_unquantize = s->dct_unquantize_mpeg1; if(s->dct_error_sum){ assert(s->avctx->noise_reduction && s->encoding); update_noise_reduction(s); } #ifdef HAVE_XVMC if(s->avctx->xvmc_acceleration) return XVMC_field_start(s, avctx);#endif return 0;}/* generic function for encode/decode called after a frame has been coded/decoded */void MPV_frame_end(MpegEncContext *s){ int i; /* draw edge for correct motion prediction if outside */#ifdef HAVE_XVMC//just to make sure that all data is rendered. if(s->avctx->xvmc_acceleration){ XVMC_field_end(s); }else#endif if(s->unrestricted_mv && s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) { draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH ); draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); draw_edges(s->current_picture.data[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; }#if 0 /* copy back current_picture variables */ for(i=0; i<MAX_PICTURE_COUNT; i++){ if(s->picture[i].data[0] == s->current_picture.data[0]){ s->picture[i]= s->current_picture; break; } } assert(i<MAX_PICTURE_COUNT);#endif 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]); } } } // clear copies, to avoid confusion#if 0 memset(&s->last_picture, 0, sizeof(Picture)); memset(&s->next_picture, 0, sizeof(Picture)); memset(&s->current_picture, 0, sizeof(Picture));#endif}/** * draws an line from (ex, ey) -> (sx, sy). * @param w width of the image * @param h height of the image * @param stride stride/linesize of the image * @param color color of the arrow */static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){ int t, x, y, f; sx= clip(sx, 0, w-1); sy= clip(sy, 0, h-1); ex= clip(ex, 0, w-1); ey= clip(ey, 0, h-1); buf[sy*stride + sx]+= color; if(ABS(ex - sx) > ABS(ey - sy)){ if(sx > ex){ t=sx; sx=ex; ex=t; t=sy; sy=ey; ey=t; } buf+= sx + sy*stride; ex-= sx; f= ((ey-sy)<<16)/ex; for(x= 0; x <= ex; x++){ y= ((x*f) + (1<<15))>>16; buf[y*stride + x]+= color; } }else{ if(sy > ey){ t=sx; sx=ex; ex=t; t=sy; sy=ey; ey=t; } buf+= sx + sy*stride; ey-= sy; if(ey) f= ((ex-sx)<<16)/ey; else f= 0; for(y= 0; y <= ey; y++){ x= ((y*f) + (1<<15))>>16; buf[y*stride + x]+= color; } }}/** * draws an arrow from (ex, ey) -> (sx, sy). * @param w width of the image * @param h height of the image * @param stride stride/linesize of the image * @param color color of the arrow */static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){ int dx,dy; sx= clip(sx, -100, w+100); sy= clip(sy, -100, h+100); ex= clip(ex, -100, w+100); ey= clip(ey, -100, h+100); dx= ex - sx; dy= ey - sy; if(dx*dx + dy*dy > 3*3){ int rx= dx + dy; int ry= -dx + dy; int length= ff_sqrt((rx*rx + ry*ry)<<8); //FIXME subpixel accuracy rx= ROUNDED_DIV(rx*3<<4, length); ry= ROUNDED_DIV(ry*3<<4, length); draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color); draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color); } draw_line(buf, sx, sy, ex, ey, w, h, stride, color);}/** * prints debuging info for the given picture. */void ff_print_debug_info(MpegEncContext *s, Picture *pict){ if(!pict || !pict->mb_type) return; if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){ int x,y; for(y=0; y<s->mb_height; y++){ for(x=0; x<s->mb_width; x++){ if(s->avctx->debug&FF_DEBUG_SKIP){ int count= s->mbskip_table[x + y*s->mb_stride]; if(count>9) count=9; av_log(s->avctx, AV_LOG_DEBUG, "%1d", count); } if(s->avctx->debug&FF_DEBUG_QP){ av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]); } if(s->avctx->debug&FF_DEBUG_MB_TYPE){ int mb_type= pict->mb_type[x + y*s->mb_stride]; //Type & MV direction if(IS_PCM(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "P"); else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "A"); else if(IS_INTRA4x4(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "i"); else if(IS_INTRA16x16(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "I"); else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "d"); else if(IS_DIRECT(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "D"); else if(IS_GMC(mb_type) && IS_SKIP(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "g"); else if(IS_GMC(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "G"); else if(IS_SKIP(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "S"); else if(!USES_LIST(mb_type, 1)) av_log(s->avctx, AV_LOG_DEBUG, ">"); else if(!USES_LIST(mb_type, 0)) av_log(s->avctx, AV_LOG_DEBUG, "<"); else{ assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1)); av_log(s->avctx, AV_LOG_DEBUG, "X"); } //segmentation if(IS_8X8(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "+"); else if(IS_16X8(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "-"); else if(IS_8X16(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -