vf.c

来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C语言 代码 · 共 744 行 · 第 1/2 页

C
744
字号
	  {	     mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8+	    				mpi->chroma_width*mpi->chroma_height);	     /* export delta table */	     mpi->planes[3]=mpi->planes[0]+(mpi->width*mpi->height)+2*(mpi->chroma_width*mpi->chroma_height);	  }	  else	     mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8);	  if(mpi->flags&MP_IMGFLAG_PLANAR){	      // YV12/I420/YVU9/IF09. feel free to add other planar formats here...	      //if(!mpi->stride[0]) 	      mpi->stride[0]=mpi->width;	      //if(!mpi->stride[1]) 	      if(mpi->num_planes > 2){	      mpi->stride[1]=mpi->stride[2]=mpi->chroma_width;	      if(mpi->flags&MP_IMGFLAG_SWAPPED){	          // I420/IYUV  (Y,U,V)	          mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;	          mpi->planes[2]=mpi->planes[1]+mpi->chroma_width*mpi->chroma_height;	      } else {	          // YV12,YVU9,IF09  (Y,V,U)	          mpi->planes[2]=mpi->planes[0]+mpi->width*mpi->height;	          mpi->planes[1]=mpi->planes[2]+mpi->chroma_width*mpi->chroma_height;	      }	      } else {	          // NV12/NV21	          mpi->stride[1]=mpi->chroma_width;	          mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;	      }	  } else {	      //if(!mpi->stride[0]) 	      mpi->stride[0]=mpi->width*mpi->bpp/8;	  }//	  printf("clearing img!\n");	  vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);	  mpi->flags|=MP_IMGFLAG_ALLOCATED;        }    }    if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)	if(vf->start_slice) vf->start_slice(vf,mpi);    if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){	    mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",		  vf->info->name,		  (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":	          ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),	          (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",	          mpi->width,mpi->height,mpi->bpp,		  (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),		  (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",	          mpi->bpp*mpi->width*mpi->height/8);	    mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"(imgfmt: %x, planes: %p,%p,%p strides: %d,%d,%d, chroma: %dx%d, shift: h:%d,v:%d)\n",		mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],		mpi->stride[0], mpi->stride[1], mpi->stride[2],		mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);	    mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;    }  mpi->qscale = NULL;  }//    printf("\rVF_MPI: %p %p %p %d %d %d    \n",//	mpi->planes[0],mpi->planes[1],mpi->planes[2],//	mpi->stride[0],mpi->stride[1],mpi->stride[2]);  return mpi;}//============================================================================// By default vf doesn't accept MPEGPESstatic int vf_default_query_format(struct vf_instance_s* vf, unsigned int fmt){  if(fmt == IMGFMT_MPEGPES) return 0;  return vf_next_query_format(vf,fmt);}vf_instance_t* vf_open_plugin(vf_info_t** filter_list, vf_instance_t* next, const char *name, char **args){    vf_instance_t* vf;    int i;    for(i=0;;i++){	if(!filter_list[i]){	    mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CouldNotFindVideoFilter,name);	    return NULL; // no such filter!	}	if(!strcmp(filter_list[i]->name,name)) break;    }    vf=malloc(sizeof(vf_instance_t));    memset(vf,0,sizeof(vf_instance_t));    vf->info=filter_list[i];    vf->next=next;    vf->config=vf_next_config;    vf->control=vf_next_control;    vf->query_format=vf_default_query_format;    vf->put_image=vf_next_put_image;    vf->default_caps=VFCAP_ACCEPT_STRIDE;    vf->default_reqs=0;    if(vf->info->opts) { // vf_vo get some special argument      m_struct_t* st = vf->info->opts;      void* vf_priv = m_struct_alloc(st);      int n;      for(n = 0 ; args && args[2*n] ; n++)	m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);      vf->priv = vf_priv;      args = NULL;    } else // Otherwise we should have the '_oldargs_'      if(args && !strcmp(args[0],"_oldargs_"))	args = (char**)args[1];      else	args = NULL;    if(vf->info->open(vf,(char*)args)>0) return vf; // Success!    free(vf);    mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CouldNotOpenVideoFilter,name);    return NULL;}vf_instance_t* vf_open_filter(vf_instance_t* next, const char *name, char **args){  if(args && strcmp(args[0],"_oldargs_")) {    int i,l = 0;    for(i = 0 ; args && args[2*i] ; i++)      l += 1 + strlen(args[2*i]) + 1 + strlen(args[2*i+1]);    l += strlen(name);    {      char str[l+1];      char* p = str;      p += sprintf(str,"%s",name);      for(i = 0 ; args && args[2*i] ; i++)	p += sprintf(p," %s=%s",args[2*i],args[2*i+1]);      mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter "[%s]\n",str);    }  } else if(strcmp(name,"vo")) {    if(args && strcmp(args[0],"_oldargs_") == 0)      mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter	     "[%s=%s]\n", name,args[1]);    else      mp_msg(MSGT_VFILTER,MSGL_INFO,MSGTR_OpeningVideoFilter	     "[%s]\n", name);  }  return vf_open_plugin(filter_list,next,name,args);}/** * \brief adds a filter before the last one (which should be the vo filter). * \param vf start of the filter chain. * \param name name of the filter to add. * \param args argument list for the filter. * \return pointer to the filter instance that was created. */vf_instance_t* vf_add_before_vo(vf_instance_t **vf, char *name, char **args) {  vf_instance_t *vo, *prev = NULL, *new;  // Find the last filter (should be vf_vo)  for (vo = *vf; vo->next; vo = vo->next)    prev = vo;  new = vf_open_filter(vo, name, args);  if (prev)    prev->next = new;  else    *vf = new;  return new;}//============================================================================unsigned int vf_match_csp(vf_instance_t** vfp,unsigned int* list,unsigned int preferred){    vf_instance_t* vf=*vfp;    unsigned int* p;    unsigned int best=0;    int ret;    if((p=list)) while(*p){	ret=vf->query_format(vf,*p);	mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);	if(ret&2){ best=*p; break;} // no conversion -> bingo!	if(ret&1 && !best) best=*p; // best with conversion	++p;    }    if(best) return best; // bingo, they have common csp!    // ok, then try with scale:    if(vf->info == &vf_info_scale) return 0; // avoid infinite recursion!    vf=vf_open_filter(vf,"scale",NULL);    if(!vf) return 0; // failed to init "scale"    // try the preferred csp first:    if(preferred && vf->query_format(vf,preferred)) best=preferred; else    // try the list again, now with "scaler" :    if((p=list)) while(*p){	ret=vf->query_format(vf,*p);	mp_msg(MSGT_VFILTER,MSGL_V,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);	if(ret&2){ best=*p; break;} // no conversion -> bingo!	if(ret&1 && !best) best=*p; // best with conversion	++p;    }    if(best) *vfp=vf; // else uninit vf  !FIXME!    return best;}void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){    dst->pict_type= src->pict_type;    dst->fields = src->fields;    dst->qscale_type= src->qscale_type;    if(dst->width == src->width && dst->height == src->height){	dst->qstride= src->qstride;	dst->qscale= src->qscale;    }}void vf_queue_frame(vf_instance_t *vf, int (*func)(vf_instance_t *)){    vf->continue_buffered_image = func;}// Output the next buffered image (if any) from the filter chain.// The queue could be kept as a simple stack/list instead avoiding the// looping here, but there's currently no good context variable where// that could be stored so this was easier to implement.int vf_output_queued_frame(vf_instance_t *vf){    while (1) {	int ret;	vf_instance_t *current;	vf_instance_t *last=NULL;	int (*tmp)(vf_instance_t *);	for (current = vf; current; current = current->next)	    if (current->continue_buffered_image)		last = current;	if (!last)	    return 0;	tmp = last->continue_buffered_image;	last->continue_buffered_image = NULL;	ret = tmp(last);	if (ret)	    return ret;    }}/** * \brief Video config() function wrapper * * Blocks config() calls with different size or format for filters * with VFCAP_CONSTANT * * First call is redirected to vf->config. * * In following calls, it verifies that the configuration parameters * are unchanged, and returns either success or error. **/int vf_config_wrapper(struct vf_instance_s* vf,		    int width, int height, int d_width, int d_height,		    unsigned int flags, unsigned int outfmt){    int r;    if ((vf->default_caps&VFCAP_CONSTANT) && vf->fmt.have_configured) {        if ((vf->fmt.orig_width != width)	    || (vf->fmt.orig_height != height)	    || (vf->fmt.orig_fmt != outfmt)) {            mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_ResolutionDoesntMatch);            return 0;        }        return 1;    }    vf->fmt.have_configured = 1;    vf->fmt.orig_height = height;    vf->fmt.orig_width = width;    vf->fmt.orig_fmt = outfmt;    r = vf->config(vf, width, height, d_width, d_height, flags, outfmt);    if (!r) vf->fmt.have_configured = 0;    return r;}int vf_next_config(struct vf_instance_s* vf,        int width, int height, int d_width, int d_height,	unsigned int voflags, unsigned int outfmt){    int miss;    int flags=vf->next->query_format(vf->next,outfmt);    if(!flags){	// hmm. colorspace mismatch!!!	// let's insert the 'scale' filter, it does the job for us:	vf_instance_t* vf2;	if(vf->next->info==&vf_info_scale) return 0; // scale->scale	vf2=vf_open_filter(vf->next,"scale",NULL);	if(!vf2) return 0; // shouldn't happen!	vf->next=vf2;	flags=vf->next->query_format(vf->next,outfmt);	if(!flags){	    mp_msg(MSGT_VFILTER,MSGL_ERR,MSGTR_CannotFindColorspace);	    return 0; // FAIL	}    }    mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X  req=0x%X  \n",flags,vf->default_reqs);    miss=vf->default_reqs - (flags&vf->default_reqs);    if(miss&VFCAP_ACCEPT_STRIDE){	// vf requires stride support but vf->next doesn't support it!	// let's insert the 'expand' filter, it does the job for us:	vf_instance_t* vf2=vf_open_filter(vf->next,"expand",NULL);	if(!vf2) return 0; // shouldn't happen!	vf->next=vf2;    }    vf->next->w = width; vf->next->h = height;    return vf_config_wrapper(vf->next,width,height,d_width,d_height,voflags,outfmt);}int vf_next_control(struct vf_instance_s* vf, int request, void* data){    return vf->next->control(vf->next,request,data);}int vf_next_query_format(struct vf_instance_s* vf, unsigned int fmt){    int flags=vf->next->query_format(vf->next,fmt);    if(flags) flags|=vf->default_caps;    return flags;}int vf_next_put_image(struct vf_instance_s* vf,mp_image_t *mpi, double pts){    return vf->next->put_image(vf->next,mpi, pts);}void vf_next_draw_slice(struct vf_instance_s* vf,unsigned char** src, int * stride,int w, int h, int x, int y){    if (vf->next->draw_slice) {	vf->next->draw_slice(vf->next,src,stride,w,h,x,y);	return;    }    if (!vf->dmpi) {	mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);	return;    }    if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {	memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,	    src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);	return;    }    memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],	w, h, vf->dmpi->stride[0], stride[0]);    memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),	src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);    memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),	src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);}//============================================================================vf_instance_t* append_filters(vf_instance_t* last){  vf_instance_t* vf;  int i;   if(vf_settings) {    // We want to add them in the 'right order'    for(i = 0 ; vf_settings[i].name ; i++)      /* NOP */;    for(i-- ; i >= 0 ; i--) {      //printf("Open filter %s\n",vf_settings[i].name);      vf = vf_open_filter(last,vf_settings[i].name,vf_settings[i].attribs);      if(vf) last=vf;    }  }  return last;}//============================================================================void vf_uninit_filter(vf_instance_t* vf){    if(vf->uninit) vf->uninit(vf);    free_mp_image(vf->imgctx.static_images[0]);    free_mp_image(vf->imgctx.static_images[1]);    free_mp_image(vf->imgctx.temp_images[0]);    free_mp_image(vf->imgctx.export_images[0]);    free(vf);}void vf_uninit_filter_chain(vf_instance_t* vf){    while(vf){	vf_instance_t* next=vf->next;	vf_uninit_filter(vf);	vf=next;    }}

⌨️ 快捷键说明

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