2010-01-30 16:57:40 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2002-04-06 22:05:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "mp_msg.h"
|
2008-04-24 04:23:15 +00:00
|
|
|
#include "options.h"
|
2002-04-06 22:05:01 +00:00
|
|
|
|
2003-01-05 15:45:55 +00:00
|
|
|
#include "img_format.h"
|
2002-04-13 19:14:34 +00:00
|
|
|
#include "mp_image.h"
|
2002-04-06 22:05:01 +00:00
|
|
|
#include "vf.h"
|
|
|
|
|
2003-03-15 20:45:03 +00:00
|
|
|
#include "m_option.h"
|
|
|
|
#include "m_struct.h"
|
|
|
|
|
2007-11-09 06:58:32 +00:00
|
|
|
static const struct vf_priv_s {
|
2002-04-06 22:05:01 +00:00
|
|
|
int crop_w,crop_h;
|
|
|
|
int crop_x,crop_y;
|
2007-11-09 06:58:32 +00:00
|
|
|
} vf_priv_dflt = {
|
2003-03-15 20:45:03 +00:00
|
|
|
-1,-1,
|
|
|
|
-1,-1
|
2002-04-06 22:05:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
2010-05-29 14:15:55 +00:00
|
|
|
static int config(struct vf_instance *vf,
|
2002-04-06 22:05:01 +00:00
|
|
|
int width, int height, int d_width, int d_height,
|
|
|
|
unsigned int flags, unsigned int outfmt){
|
2008-04-24 04:23:15 +00:00
|
|
|
struct MPOpts *opts = vf->opts;
|
2002-04-06 22:05:01 +00:00
|
|
|
// calculate the missing parameters:
|
|
|
|
if(vf->priv->crop_w<=0 || vf->priv->crop_w>width) vf->priv->crop_w=width;
|
|
|
|
if(vf->priv->crop_h<=0 || vf->priv->crop_h>height) vf->priv->crop_h=height;
|
|
|
|
if(vf->priv->crop_x<0) vf->priv->crop_x=(width-vf->priv->crop_w)/2;
|
|
|
|
if(vf->priv->crop_y<0) vf->priv->crop_y=(height-vf->priv->crop_h)/2;
|
2003-01-05 15:39:52 +00:00
|
|
|
// rounding:
|
|
|
|
if(!IMGFMT_IS_RGB(outfmt) && !IMGFMT_IS_BGR(outfmt)){
|
|
|
|
switch(outfmt){
|
|
|
|
case IMGFMT_444P:
|
|
|
|
case IMGFMT_Y800:
|
|
|
|
case IMGFMT_Y8:
|
|
|
|
break;
|
|
|
|
case IMGFMT_YVU9:
|
|
|
|
case IMGFMT_IF09:
|
|
|
|
vf->priv->crop_y&=~3;
|
|
|
|
case IMGFMT_411P:
|
|
|
|
vf->priv->crop_x&=~3;
|
|
|
|
break;
|
|
|
|
case IMGFMT_YV12:
|
|
|
|
case IMGFMT_I420:
|
|
|
|
case IMGFMT_IYUV:
|
|
|
|
vf->priv->crop_y&=~1;
|
|
|
|
default:
|
|
|
|
vf->priv->crop_x&=~1;
|
|
|
|
}
|
|
|
|
}
|
2002-04-06 22:05:01 +00:00
|
|
|
// check:
|
2002-05-12 19:06:15 +00:00
|
|
|
if(vf->priv->crop_w+vf->priv->crop_x>width ||
|
|
|
|
vf->priv->crop_h+vf->priv->crop_y>height){
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_VFILTER, MSGL_WARN, "[CROP] Bad position/width/height - cropped area outside of the original!\n");
|
2002-05-12 19:06:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-04-24 04:23:15 +00:00
|
|
|
if(!opts->screen_size_x && !opts->screen_size_y){
|
2002-05-12 19:06:15 +00:00
|
|
|
d_width=d_width*vf->priv->crop_w/width;
|
|
|
|
d_height=d_height*vf->priv->crop_h/height;
|
|
|
|
}
|
|
|
|
return vf_next_config(vf,vf->priv->crop_w,vf->priv->crop_h,d_width,d_height,flags,outfmt);
|
2002-04-06 22:05:01 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 14:15:55 +00:00
|
|
|
static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
|
2003-04-28 02:23:57 +00:00
|
|
|
mp_image_t *dmpi;
|
|
|
|
if (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
|
2006-03-21 21:26:42 +00:00
|
|
|
return vf_next_put_image(vf,vf->dmpi, pts);
|
2003-04-28 02:23:57 +00:00
|
|
|
dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
2002-04-06 22:05:01 +00:00
|
|
|
MP_IMGTYPE_EXPORT, 0,
|
|
|
|
vf->priv->crop_w, vf->priv->crop_h);
|
|
|
|
if(mpi->flags&MP_IMGFLAG_PLANAR){
|
|
|
|
dmpi->planes[0]=mpi->planes[0]+
|
|
|
|
vf->priv->crop_y*mpi->stride[0]+vf->priv->crop_x;
|
|
|
|
dmpi->planes[1]=mpi->planes[1]+
|
2003-01-05 15:39:52 +00:00
|
|
|
(vf->priv->crop_y>>mpi->chroma_y_shift)*mpi->stride[1]+(vf->priv->crop_x>>mpi->chroma_x_shift);
|
2002-04-06 22:05:01 +00:00
|
|
|
dmpi->planes[2]=mpi->planes[2]+
|
2003-01-05 15:39:52 +00:00
|
|
|
(vf->priv->crop_y>>mpi->chroma_y_shift)*mpi->stride[2]+(vf->priv->crop_x>>mpi->chroma_x_shift);
|
2002-04-06 22:05:01 +00:00
|
|
|
dmpi->stride[1]=mpi->stride[1];
|
|
|
|
dmpi->stride[2]=mpi->stride[2];
|
|
|
|
} else {
|
|
|
|
dmpi->planes[0]=mpi->planes[0]+
|
|
|
|
vf->priv->crop_y*mpi->stride[0]+
|
|
|
|
vf->priv->crop_x*(mpi->bpp/8);
|
2003-02-04 18:53:33 +00:00
|
|
|
dmpi->planes[1]=mpi->planes[1]; // passthrough rgb8 palette
|
2002-04-06 22:05:01 +00:00
|
|
|
}
|
2002-04-14 02:07:29 +00:00
|
|
|
dmpi->stride[0]=mpi->stride[0];
|
|
|
|
dmpi->width=mpi->width;
|
2006-03-21 21:26:42 +00:00
|
|
|
return vf_next_put_image(vf,dmpi, pts);
|
2002-04-06 22:05:01 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 14:15:55 +00:00
|
|
|
static void start_slice(struct vf_instance *vf, mp_image_t *mpi){
|
2003-05-20 17:42:33 +00:00
|
|
|
vf->dmpi = vf_get_image(vf->next, mpi->imgfmt, mpi->type, mpi->flags,
|
2003-04-27 21:04:00 +00:00
|
|
|
vf->priv->crop_w, vf->priv->crop_h);
|
|
|
|
}
|
|
|
|
|
2010-05-29 14:15:55 +00:00
|
|
|
static void draw_slice(struct vf_instance *vf,
|
2003-04-27 21:04:00 +00:00
|
|
|
unsigned char** src, int* stride, int w,int h, int x, int y){
|
2003-04-28 02:23:57 +00:00
|
|
|
unsigned char *src2[3];
|
2003-04-30 18:24:04 +00:00
|
|
|
src2[0] = src[0];
|
2003-05-20 17:42:33 +00:00
|
|
|
if (vf->dmpi->flags & MP_IMGFLAG_PLANAR) {
|
2003-04-30 18:24:04 +00:00
|
|
|
src2[1] = src[1];
|
|
|
|
src2[2] = src[2];
|
2003-04-28 02:23:57 +00:00
|
|
|
}
|
2003-04-27 21:04:00 +00:00
|
|
|
//mp_msg(MSGT_VFILTER, MSGL_V, "crop slice %d %d %d %d ->", w,h,x,y);
|
|
|
|
if ((x -= vf->priv->crop_x) < 0) {
|
2003-04-30 18:24:04 +00:00
|
|
|
x = -x;
|
|
|
|
src2[0] += x;
|
2003-05-20 17:42:33 +00:00
|
|
|
if (vf->dmpi->flags & MP_IMGFLAG_PLANAR) {
|
|
|
|
src2[1] += x>>vf->dmpi->chroma_x_shift;
|
|
|
|
src2[2] += x>>vf->dmpi->chroma_x_shift;
|
2003-04-30 18:24:04 +00:00
|
|
|
}
|
|
|
|
w -= x;
|
2003-04-27 21:04:00 +00:00
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
if ((y -= vf->priv->crop_y) < 0) {
|
2003-04-30 18:24:04 +00:00
|
|
|
y = -y;
|
|
|
|
src2[0] += y*stride[0];
|
2003-05-20 17:42:33 +00:00
|
|
|
if (vf->dmpi->flags & MP_IMGFLAG_PLANAR) {
|
|
|
|
src2[1] += (y>>vf->dmpi->chroma_y_shift)*stride[1];
|
|
|
|
src2[2] += (y>>vf->dmpi->chroma_y_shift)*stride[2];
|
2003-04-30 18:24:04 +00:00
|
|
|
}
|
|
|
|
h -= y;
|
2003-04-27 21:04:00 +00:00
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
if (x+w > vf->priv->crop_w) w = vf->priv->crop_w-x;
|
|
|
|
if (y+h > vf->priv->crop_h) h = vf->priv->crop_h-y;
|
|
|
|
//mp_msg(MSGT_VFILTER, MSGL_V, "%d %d %d %d\n", w,h,x,y);
|
2010-01-08 19:21:14 +00:00
|
|
|
if (w <= 0 || h <= 0) return;
|
2003-04-28 02:23:57 +00:00
|
|
|
vf_next_draw_slice(vf,src2,stride,w,h,x,y);
|
2003-04-27 21:04:00 +00:00
|
|
|
}
|
|
|
|
|
2002-04-06 22:05:01 +00:00
|
|
|
//===========================================================================//
|
|
|
|
|
2010-02-21 13:40:49 +00:00
|
|
|
static int vf_open(vf_instance_t *vf, char *args){
|
2002-04-06 22:05:01 +00:00
|
|
|
vf->config=config;
|
|
|
|
vf->put_image=put_image;
|
2003-04-27 21:04:00 +00:00
|
|
|
vf->start_slice=start_slice;
|
|
|
|
vf->draw_slice=draw_slice;
|
2002-04-11 20:56:17 +00:00
|
|
|
vf->default_reqs=VFCAP_ACCEPT_STRIDE;
|
2003-01-10 10:43:01 +00:00
|
|
|
mp_msg(MSGT_VFILTER, MSGL_INFO, "Crop: %d x %d, %d ; %d\n",
|
2002-04-06 22:05:01 +00:00
|
|
|
vf->priv->crop_w,
|
|
|
|
vf->priv->crop_h,
|
|
|
|
vf->priv->crop_x,
|
|
|
|
vf->priv->crop_y);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-03-15 20:45:03 +00:00
|
|
|
#define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
|
2007-11-09 06:58:32 +00:00
|
|
|
static const m_option_t vf_opts_fields[] = {
|
2003-03-15 20:55:33 +00:00
|
|
|
{"w", ST_OFF(crop_w), CONF_TYPE_INT, M_OPT_MIN,0 ,0, NULL},
|
|
|
|
{"h", ST_OFF(crop_h), CONF_TYPE_INT, M_OPT_MIN,0 ,0, NULL},
|
|
|
|
{"x", ST_OFF(crop_x), CONF_TYPE_INT, M_OPT_MIN,-1 ,0, NULL},
|
|
|
|
{"y", ST_OFF(crop_y), CONF_TYPE_INT, M_OPT_MIN,-1 ,0, NULL},
|
2003-03-15 20:45:03 +00:00
|
|
|
{ NULL, NULL, 0, 0, 0, 0, NULL }
|
|
|
|
};
|
|
|
|
|
2007-11-09 06:58:32 +00:00
|
|
|
static const m_struct_t vf_opts = {
|
2003-03-15 20:45:03 +00:00
|
|
|
"crop",
|
|
|
|
sizeof(struct vf_priv_s),
|
|
|
|
&vf_priv_dflt,
|
|
|
|
vf_opts_fields
|
|
|
|
};
|
|
|
|
|
2007-11-09 06:58:32 +00:00
|
|
|
const vf_info_t vf_info_crop = {
|
2002-04-06 22:05:01 +00:00
|
|
|
"cropping",
|
|
|
|
"crop",
|
|
|
|
"A'rpi",
|
|
|
|
"",
|
2010-02-21 13:40:49 +00:00
|
|
|
vf_open,
|
2003-03-15 20:45:03 +00:00
|
|
|
&vf_opts
|
2002-04-06 22:05:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//===========================================================================//
|