Make AVI demuxer more resilient against broken or incomplete files.
[mplayer/glamo.git] / libmpcodecs / vf_crop.c
blobc9bd11c6c77140d45f6e17e8a21e593863a143b7
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "config.h"
6 #include "mp_msg.h"
7 #include "help_mp.h"
9 #include "img_format.h"
10 #include "mp_image.h"
11 #include "vf.h"
13 #include "m_option.h"
14 #include "m_struct.h"
16 static const struct vf_priv_s {
17 int crop_w,crop_h;
18 int crop_x,crop_y;
19 } vf_priv_dflt = {
20 -1,-1,
21 -1,-1
24 extern int opt_screen_size_x;
25 extern int opt_screen_size_y;
27 //===========================================================================//
29 static int config(struct vf_instance_s* vf,
30 int width, int height, int d_width, int d_height,
31 unsigned int flags, unsigned int outfmt){
32 // calculate the missing parameters:
33 if(vf->priv->crop_w<=0 || vf->priv->crop_w>width) vf->priv->crop_w=width;
34 if(vf->priv->crop_h<=0 || vf->priv->crop_h>height) vf->priv->crop_h=height;
35 if(vf->priv->crop_x<0) vf->priv->crop_x=(width-vf->priv->crop_w)/2;
36 if(vf->priv->crop_y<0) vf->priv->crop_y=(height-vf->priv->crop_h)/2;
37 // rounding:
38 if(!IMGFMT_IS_RGB(outfmt) && !IMGFMT_IS_BGR(outfmt)){
39 switch(outfmt){
40 case IMGFMT_444P:
41 case IMGFMT_Y800:
42 case IMGFMT_Y8:
43 break;
44 case IMGFMT_YVU9:
45 case IMGFMT_IF09:
46 vf->priv->crop_y&=~3;
47 case IMGFMT_411P:
48 vf->priv->crop_x&=~3;
49 break;
50 case IMGFMT_YV12:
51 case IMGFMT_I420:
52 case IMGFMT_IYUV:
53 vf->priv->crop_y&=~1;
54 default:
55 vf->priv->crop_x&=~1;
58 // check:
59 if(vf->priv->crop_w+vf->priv->crop_x>width ||
60 vf->priv->crop_h+vf->priv->crop_y>height){
61 mp_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_CropBadPositionWidthHeight);
62 return 0;
64 if(!opt_screen_size_x && !opt_screen_size_y){
65 d_width=d_width*vf->priv->crop_w/width;
66 d_height=d_height*vf->priv->crop_h/height;
68 return vf_next_config(vf,vf->priv->crop_w,vf->priv->crop_h,d_width,d_height,flags,outfmt);
71 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
72 mp_image_t *dmpi;
73 if (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
74 return vf_next_put_image(vf,vf->dmpi, pts);
75 dmpi=vf_get_image(vf->next,mpi->imgfmt,
76 MP_IMGTYPE_EXPORT, 0,
77 vf->priv->crop_w, vf->priv->crop_h);
78 if(mpi->flags&MP_IMGFLAG_PLANAR){
79 dmpi->planes[0]=mpi->planes[0]+
80 vf->priv->crop_y*mpi->stride[0]+vf->priv->crop_x;
81 dmpi->planes[1]=mpi->planes[1]+
82 (vf->priv->crop_y>>mpi->chroma_y_shift)*mpi->stride[1]+(vf->priv->crop_x>>mpi->chroma_x_shift);
83 dmpi->planes[2]=mpi->planes[2]+
84 (vf->priv->crop_y>>mpi->chroma_y_shift)*mpi->stride[2]+(vf->priv->crop_x>>mpi->chroma_x_shift);
85 dmpi->stride[1]=mpi->stride[1];
86 dmpi->stride[2]=mpi->stride[2];
87 } else {
88 dmpi->planes[0]=mpi->planes[0]+
89 vf->priv->crop_y*mpi->stride[0]+
90 vf->priv->crop_x*(mpi->bpp/8);
91 dmpi->planes[1]=mpi->planes[1]; // passthrough rgb8 palette
93 dmpi->stride[0]=mpi->stride[0];
94 dmpi->width=mpi->width;
95 return vf_next_put_image(vf,dmpi, pts);
98 static void start_slice(struct vf_instance_s* vf, mp_image_t *mpi){
99 vf->dmpi = vf_get_image(vf->next, mpi->imgfmt, mpi->type, mpi->flags,
100 vf->priv->crop_w, vf->priv->crop_h);
103 static void draw_slice(struct vf_instance_s* vf,
104 unsigned char** src, int* stride, int w,int h, int x, int y){
105 unsigned char *src2[3];
106 src2[0] = src[0];
107 if (vf->dmpi->flags & MP_IMGFLAG_PLANAR) {
108 src2[1] = src[1];
109 src2[2] = src[2];
111 //mp_msg(MSGT_VFILTER, MSGL_V, "crop slice %d %d %d %d ->", w,h,x,y);
112 if ((x -= vf->priv->crop_x) < 0) {
113 x = -x;
114 src2[0] += x;
115 if (vf->dmpi->flags & MP_IMGFLAG_PLANAR) {
116 src2[1] += x>>vf->dmpi->chroma_x_shift;
117 src2[2] += x>>vf->dmpi->chroma_x_shift;
119 w -= x;
120 x = 0;
122 if ((y -= vf->priv->crop_y) < 0) {
123 y = -y;
124 src2[0] += y*stride[0];
125 if (vf->dmpi->flags & MP_IMGFLAG_PLANAR) {
126 src2[1] += (y>>vf->dmpi->chroma_y_shift)*stride[1];
127 src2[2] += (y>>vf->dmpi->chroma_y_shift)*stride[2];
129 h -= y;
130 y = 0;
132 if (x+w > vf->priv->crop_w) w = vf->priv->crop_w-x;
133 if (y+h > vf->priv->crop_h) h = vf->priv->crop_h-y;
134 //mp_msg(MSGT_VFILTER, MSGL_V, "%d %d %d %d\n", w,h,x,y);
135 if ((w < 0) || (h < 0)) return;
136 vf_next_draw_slice(vf,src2,stride,w,h,x,y);
139 //===========================================================================//
141 static int open(vf_instance_t *vf, char* args){
142 vf->config=config;
143 vf->put_image=put_image;
144 vf->start_slice=start_slice;
145 vf->draw_slice=draw_slice;
146 vf->default_reqs=VFCAP_ACCEPT_STRIDE;
147 if(!vf->priv) {
148 vf->priv=malloc(sizeof(struct vf_priv_s));
149 // TODO: parse args ->
150 vf->priv->crop_x=
151 vf->priv->crop_y=
152 vf->priv->crop_w=
153 vf->priv->crop_h=-1;
154 } //if(!vf->priv)
155 if(args) sscanf(args, "%d:%d:%d:%d",
156 &vf->priv->crop_w,
157 &vf->priv->crop_h,
158 &vf->priv->crop_x,
159 &vf->priv->crop_y);
160 mp_msg(MSGT_VFILTER, MSGL_INFO, "Crop: %d x %d, %d ; %d\n",
161 vf->priv->crop_w,
162 vf->priv->crop_h,
163 vf->priv->crop_x,
164 vf->priv->crop_y);
165 return 1;
168 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
169 static const m_option_t vf_opts_fields[] = {
170 {"w", ST_OFF(crop_w), CONF_TYPE_INT, M_OPT_MIN,0 ,0, NULL},
171 {"h", ST_OFF(crop_h), CONF_TYPE_INT, M_OPT_MIN,0 ,0, NULL},
172 {"x", ST_OFF(crop_x), CONF_TYPE_INT, M_OPT_MIN,-1 ,0, NULL},
173 {"y", ST_OFF(crop_y), CONF_TYPE_INT, M_OPT_MIN,-1 ,0, NULL},
174 { NULL, NULL, 0, 0, 0, 0, NULL }
177 static const m_struct_t vf_opts = {
178 "crop",
179 sizeof(struct vf_priv_s),
180 &vf_priv_dflt,
181 vf_opts_fields
184 const vf_info_t vf_info_crop = {
185 "cropping",
186 "crop",
187 "A'rpi",
189 open,
190 &vf_opts
193 //===========================================================================//