Add missing multiple inclusion guards.
[mplayer/glamo.git] / libmpcodecs / vf_expand.c
blob0f25766f098d68924dba828346ca53438612db24
1 #define OSD_SUPPORT
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #include "config.h"
8 #include "mp_msg.h"
9 #include "help_mp.h"
11 #include "img_format.h"
12 #include "mp_image.h"
13 #include "vf.h"
15 #include "libvo/fastmemcpy.h"
17 #ifdef OSD_SUPPORT
18 #include "libvo/sub.h"
19 #include "libvo/osd.h"
20 #endif
22 #include "m_option.h"
23 #include "m_struct.h"
25 #define MAX(a,b) ((a) > (b) ? (a) : (b))
27 static struct vf_priv_s {
28 int exp_w,exp_h;
29 int exp_x,exp_y;
30 int osd;
31 double aspect;
32 int round;
33 unsigned char* fb_ptr;
34 int passthrough;
35 int first_slice;
36 } const vf_priv_dflt = {
37 -1,-1,
38 -1,-1,
40 0.,
42 NULL,
47 extern int opt_screen_size_x;
48 extern int opt_screen_size_y;
50 //===========================================================================//
51 #ifdef OSD_SUPPORT
53 static struct vf_instance_s* vf=NULL; // fixme (needs sub.c changes)
54 static int orig_w,orig_h;
56 static void remove_func_2(int x0,int y0, int w,int h){
57 // TODO: let's cleanup the place
58 //printf("OSD clear: %d;%d %dx%d \n",x0,y0,w,h);
59 vf_mpi_clear(vf->dmpi,x0,y0,w,h);
62 static void remove_func(int x0,int y0, int w,int h){
63 if(!vo_osd_changed_flag) return;
64 // split it to 4 parts:
65 if(y0<vf->priv->exp_y){
66 // it has parts above the image:
67 int y=y0+h;
68 if(y>vf->priv->exp_y) y=vf->priv->exp_y;
69 remove_func_2(x0,y0,w,y-y0);
70 if(y0+h<=vf->priv->exp_y) return;
71 h-=y-y0;y0=y;
73 if(y0+h>vf->priv->exp_y+orig_h){
74 // it has parts under the image:
75 int y=y0;
76 if(y<vf->priv->exp_y+orig_h) y=vf->priv->exp_y+orig_h;
77 remove_func_2(x0,y,w,y0+h-y);
78 if(y0>=vf->priv->exp_y+orig_h) return;
79 h=y-y0;
81 if(x0<vf->priv->exp_x){
82 // it has parts on the left side of the image:
83 int x=x0+w;
84 if(x>vf->priv->exp_x) x=vf->priv->exp_x;
85 remove_func_2(x0,y0,x-x0,h);
86 if(x0+w<=vf->priv->exp_x) return;
87 w-=x-x0;x0=x;
89 if(x0+w>vf->priv->exp_x+orig_w){
90 // it has parts on the right side of the image:
91 int x=x0;
92 if(x<vf->priv->exp_x+orig_w) x=vf->priv->exp_x+orig_w;
93 remove_func_2(x,y0,x0+w-x,h);
94 if(x0>=vf->priv->exp_x+orig_w) return;
95 w=x-x0;
99 static void draw_func(int x0,int y0, int w,int h,unsigned char* src, unsigned char *srca, int stride){
100 unsigned char* dst;
101 if(!vo_osd_changed_flag && vf->dmpi->planes[0]==vf->priv->fb_ptr){
102 // ok, enough to update the area inside the video, leave the black bands
103 // untouched!
104 if(x0<vf->priv->exp_x){
105 int tmp=vf->priv->exp_x-x0;
106 w-=tmp; src+=tmp; srca+=tmp; x0+=tmp;
108 if(y0<vf->priv->exp_y){
109 int tmp=vf->priv->exp_y-y0;
110 h-=tmp; src+=tmp*stride; srca+=tmp*stride; y0+=tmp;
112 if(x0+w>vf->priv->exp_x+orig_w){
113 w=vf->priv->exp_x+orig_w-x0;
115 if(y0+h>vf->priv->exp_y+orig_h){
116 h=vf->priv->exp_y+orig_h-y0;
119 if(w<=0 || h<=0) return; // nothing to do...
120 // printf("OSD redraw: %d;%d %dx%d \n",x0,y0,w,h);
121 dst=vf->dmpi->planes[0]+
122 vf->dmpi->stride[0]*y0+
123 (vf->dmpi->bpp>>3)*x0;
124 switch(vf->dmpi->imgfmt){
125 case IMGFMT_BGR15:
126 case IMGFMT_RGB15:
127 vo_draw_alpha_rgb15(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
128 break;
129 case IMGFMT_BGR16:
130 case IMGFMT_RGB16:
131 vo_draw_alpha_rgb16(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
132 break;
133 case IMGFMT_BGR24:
134 case IMGFMT_RGB24:
135 vo_draw_alpha_rgb24(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
136 break;
137 case IMGFMT_BGR32:
138 case IMGFMT_RGB32:
139 vo_draw_alpha_rgb32(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
140 break;
141 case IMGFMT_YV12:
142 case IMGFMT_I420:
143 case IMGFMT_IYUV:
144 case IMGFMT_YVU9:
145 case IMGFMT_IF09:
146 case IMGFMT_Y800:
147 case IMGFMT_Y8:
148 vo_draw_alpha_yv12(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
149 break;
150 case IMGFMT_YUY2:
151 vo_draw_alpha_yuy2(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
152 break;
153 case IMGFMT_UYVY:
154 vo_draw_alpha_yuy2(w,h,src,srca,stride,dst+1,vf->dmpi->stride[0]);
155 break;
159 static void draw_osd(struct vf_instance_s* vf_,int w,int h){
160 vf=vf_;orig_w=w;orig_h=h;
161 // printf("======================================\n");
162 if(vf->priv->exp_w!=w || vf->priv->exp_h!=h ||
163 vf->priv->exp_x || vf->priv->exp_y){
164 // yep, we're expanding image, not just copy.
165 if(vf->dmpi->planes[0]!=vf->priv->fb_ptr){
166 // double buffering, so we need full clear :(
167 if (vf->priv->exp_y > 0)
168 remove_func_2(0,0,vf->priv->exp_w,vf->priv->exp_y);
169 if (vf->priv->exp_y+h < vf->priv->exp_h)
170 remove_func_2(0,vf->priv->exp_y+h,vf->priv->exp_w,vf->priv->exp_h-h-vf->priv->exp_y);
171 if (vf->priv->exp_x > 0)
172 remove_func_2(0,vf->priv->exp_y,vf->priv->exp_x,h);
173 if (vf->priv->exp_x+w < vf->priv->exp_w)
174 remove_func_2(vf->priv->exp_x+w,vf->priv->exp_y,vf->priv->exp_w-w-vf->priv->exp_x,h);
175 } else {
176 // partial clear:
177 vo_remove_text(vf->priv->exp_w,vf->priv->exp_h,remove_func);
180 vo_draw_text(vf->priv->exp_w,vf->priv->exp_h,draw_func);
181 // save buffer pointer for double buffering detection - yes, i know it's
182 // ugly method, but note that codecs with DR support does the same...
183 if(vf->dmpi)
184 vf->priv->fb_ptr=vf->dmpi->planes[0];
187 #endif
188 //===========================================================================//
190 static int config(struct vf_instance_s* vf,
191 int width, int height, int d_width, int d_height,
192 unsigned int flags, unsigned int outfmt){
193 if(outfmt == IMGFMT_MPEGPES) {
194 vf->priv->passthrough = 1;
195 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
197 if (outfmt == IMGFMT_IF09) return 0;
198 // calculate the missing parameters:
199 #if 0
200 if(vf->priv->exp_w<width) vf->priv->exp_w=width;
201 if(vf->priv->exp_h<height) vf->priv->exp_h=height;
202 #else
203 if ( vf->priv->exp_w == -1 ) vf->priv->exp_w=width;
204 else if (vf->priv->exp_w < -1 ) vf->priv->exp_w=width - vf->priv->exp_w;
205 else if ( vf->priv->exp_w<width ) vf->priv->exp_w=width;
206 if ( vf->priv->exp_h == -1 ) vf->priv->exp_h=height;
207 else if ( vf->priv->exp_h < -1 ) vf->priv->exp_h=height - vf->priv->exp_h;
208 else if( vf->priv->exp_h<height ) vf->priv->exp_h=height;
209 #endif
210 if (vf->priv->aspect) {
211 float adjusted_aspect = vf->priv->aspect;
212 adjusted_aspect *= ((double)width/height) / ((double)d_width/d_height);
213 if (vf->priv->exp_h < vf->priv->exp_w / adjusted_aspect) {
214 vf->priv->exp_h = vf->priv->exp_w / adjusted_aspect + 0.5;
215 } else {
216 vf->priv->exp_w = vf->priv->exp_h * adjusted_aspect + 0.5;
219 if (vf->priv->round > 1) { // round up.
220 vf->priv->exp_w = (1 + (vf->priv->exp_w - 1) / vf->priv->round) * vf->priv->round;
221 vf->priv->exp_h = (1 + (vf->priv->exp_h - 1) / vf->priv->round) * vf->priv->round;
224 if(vf->priv->exp_x<0 || vf->priv->exp_x+width>vf->priv->exp_w) vf->priv->exp_x=(vf->priv->exp_w-width)/2;
225 if(vf->priv->exp_y<0 || vf->priv->exp_y+height>vf->priv->exp_h) vf->priv->exp_y=(vf->priv->exp_h-height)/2;
226 vf->priv->fb_ptr=NULL;
228 if(!opt_screen_size_x && !opt_screen_size_y){
229 d_width=d_width*vf->priv->exp_w/width;
230 d_height=d_height*vf->priv->exp_h/height;
232 return vf_next_config(vf,vf->priv->exp_w,vf->priv->exp_h,d_width,d_height,flags,outfmt);
235 // there are 4 cases:
236 // codec --DR--> expand --DR--> vo
237 // codec --DR--> expand -copy-> vo
238 // codec -copy-> expand --DR--> vo
239 // codec -copy-> expand -copy-> vo (worst case)
241 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
242 // if(mpi->type==MP_IMGTYPE_IPB) return; // not yet working
243 #ifdef OSD_SUPPORT
244 if(vf->priv->osd && (mpi->flags&MP_IMGFLAG_PRESERVE)){
245 // check if we have to render osd!
246 vo_update_osd(vf->priv->exp_w, vf->priv->exp_h);
247 if(vo_osd_check_range_update(vf->priv->exp_x,vf->priv->exp_y,
248 vf->priv->exp_x+mpi->w,vf->priv->exp_y+mpi->h)) return;
250 #endif
251 if(vf->priv->exp_w==mpi->width ||
252 (mpi->flags&(MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_ACCEPT_WIDTH)) ){
253 // try full DR !
254 mpi->priv=vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
255 mpi->type, mpi->flags,
256 MAX(vf->priv->exp_w, mpi->width +vf->priv->exp_x),
257 MAX(vf->priv->exp_h, mpi->height+vf->priv->exp_y));
258 #if 1
259 if((vf->dmpi->flags & MP_IMGFLAG_DRAW_CALLBACK) &&
260 !(vf->dmpi->flags & MP_IMGFLAG_DIRECT)){
261 mp_msg(MSGT_VFILTER, MSGL_INFO, MSGTR_MPCODECS_FullDRNotPossible);
262 return;
264 #endif
265 // set up mpi as a cropped-down image of dmpi:
266 if(mpi->flags&MP_IMGFLAG_PLANAR){
267 mpi->planes[0]=vf->dmpi->planes[0]+
268 vf->priv->exp_y*vf->dmpi->stride[0]+vf->priv->exp_x;
269 mpi->planes[1]=vf->dmpi->planes[1]+
270 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[1]+(vf->priv->exp_x>>mpi->chroma_x_shift);
271 mpi->planes[2]=vf->dmpi->planes[2]+
272 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[2]+(vf->priv->exp_x>>mpi->chroma_x_shift);
273 mpi->stride[1]=vf->dmpi->stride[1];
274 mpi->stride[2]=vf->dmpi->stride[2];
275 } else {
276 mpi->planes[0]=vf->dmpi->planes[0]+
277 vf->priv->exp_y*vf->dmpi->stride[0]+
278 vf->priv->exp_x*(vf->dmpi->bpp/8);
280 mpi->stride[0]=vf->dmpi->stride[0];
281 mpi->width=vf->dmpi->width;
282 mpi->flags|=MP_IMGFLAG_DIRECT;
283 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
284 // vf->dmpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
288 static void start_slice(struct vf_instance_s* vf, mp_image_t *mpi){
289 // printf("start_slice called! flag=%d\n",mpi->flags&MP_IMGFLAG_DRAW_CALLBACK);
290 if(!vf->next->draw_slice){
291 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
292 return;
294 // they want slices!!! allocate the buffer.
295 if(!mpi->priv)
296 mpi->priv=vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
297 // MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
298 MP_IMGTYPE_TEMP, mpi->flags,
299 MAX(vf->priv->exp_w, mpi->width +vf->priv->exp_x),
300 MAX(vf->priv->exp_h, mpi->height+vf->priv->exp_y));
301 if(!(vf->dmpi->flags&MP_IMGFLAG_DRAW_CALLBACK))
302 mp_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_WarnNextFilterDoesntSupportSlices); // shouldn't happen.
303 vf->priv->first_slice = 1;
306 static void draw_top_blackbar_slice(struct vf_instance_s* vf,
307 unsigned char** src, int* stride, int w,int h, int x, int y){
308 if(vf->priv->exp_y>0 && y == 0) {
309 vf_next_draw_slice(vf, vf->dmpi->planes, vf->dmpi->stride,
310 vf->dmpi->w,vf->priv->exp_y,0,0);
315 static void draw_bottom_blackbar_slice(struct vf_instance_s* vf,
316 unsigned char** src, int* stride, int w,int h, int x, int y){
317 if(vf->priv->exp_y+vf->h<vf->dmpi->h && y+h == vf->h) {
318 unsigned char *src2[MP_MAX_PLANES];
319 src2[0] = vf->dmpi->planes[0]
320 + (vf->priv->exp_y+vf->h)*vf->dmpi->stride[0];
321 if(vf->dmpi->flags&MP_IMGFLAG_PLANAR){
322 src2[1] = vf->dmpi->planes[1]
323 + ((vf->priv->exp_y+vf->h)>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1];
324 src2[2] = vf->dmpi->planes[2]
325 + ((vf->priv->exp_y+vf->h)>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2];
326 } else {
327 src2[1] = vf->dmpi->planes[1]; // passthrough rgb8 palette
329 vf_next_draw_slice(vf, src2, vf->dmpi->stride,
330 vf->dmpi->w,vf->dmpi->h-(vf->priv->exp_y+vf->h),
331 0,vf->priv->exp_y+vf->h);
335 static void draw_slice(struct vf_instance_s* vf,
336 unsigned char** src, int* stride, int w,int h, int x, int y){
337 // printf("draw_slice() called %d at %d\n",h,y);
339 if (y == 0 && y+h == vf->h) {
340 // special case - only one slice
341 draw_top_blackbar_slice(vf, src, stride, w, h, x, y);
342 vf_next_draw_slice(vf,src,stride,w,h,x+vf->priv->exp_x,y+vf->priv->exp_y);
343 draw_bottom_blackbar_slice(vf, src, stride, w, h, x, y);
344 return;
346 if (vf->priv->first_slice) {
347 draw_top_blackbar_slice(vf, src, stride, w, h, x, y);
348 draw_bottom_blackbar_slice(vf, src, stride, w, h, x, y);
350 vf_next_draw_slice(vf,src,stride,w,h,x+vf->priv->exp_x,y+vf->priv->exp_y);
351 if (!vf->priv->first_slice) {
352 draw_top_blackbar_slice(vf, src, stride, w, h, x, y);
353 draw_bottom_blackbar_slice(vf, src, stride, w, h, x, y);
355 vf->priv->first_slice = 0;
358 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
359 if (vf->priv->passthrough) {
360 mp_image_t *dmpi = vf_get_image(vf->next, IMGFMT_MPEGPES,
361 MP_IMGTYPE_EXPORT, 0, mpi->w, mpi->h);
362 dmpi->planes[0]=mpi->planes[0];
363 return vf_next_put_image(vf,dmpi, pts);
366 if(mpi->flags&MP_IMGFLAG_DIRECT || mpi->flags&MP_IMGFLAG_DRAW_CALLBACK){
367 vf->dmpi=mpi->priv;
368 if(!vf->dmpi) { mp_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_FunWhydowegetNULL); return 0; }
369 mpi->priv=NULL;
370 #ifdef OSD_SUPPORT
371 if(vf->priv->osd) draw_osd(vf,mpi->w,mpi->h);
372 #endif
373 // we've used DR, so we're ready...
374 if(!(mpi->flags&MP_IMGFLAG_PLANAR))
375 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
376 return vf_next_put_image(vf,vf->dmpi, pts);
379 // hope we'll get DR buffer:
380 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
381 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
382 vf->priv->exp_w, vf->priv->exp_h);
384 // copy mpi->dmpi...
385 if(mpi->flags&MP_IMGFLAG_PLANAR){
386 memcpy_pic(vf->dmpi->planes[0]+
387 vf->priv->exp_y*vf->dmpi->stride[0]+vf->priv->exp_x,
388 mpi->planes[0], mpi->w, mpi->h,
389 vf->dmpi->stride[0],mpi->stride[0]);
390 memcpy_pic(vf->dmpi->planes[1]+
391 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[1]+(vf->priv->exp_x>>mpi->chroma_x_shift),
392 mpi->planes[1], (mpi->w>>mpi->chroma_x_shift), (mpi->h>>mpi->chroma_y_shift),
393 vf->dmpi->stride[1],mpi->stride[1]);
394 memcpy_pic(vf->dmpi->planes[2]+
395 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[2]+(vf->priv->exp_x>>mpi->chroma_x_shift),
396 mpi->planes[2], (mpi->w>>mpi->chroma_x_shift), (mpi->h>>mpi->chroma_y_shift),
397 vf->dmpi->stride[2],mpi->stride[2]);
398 } else {
399 memcpy_pic(vf->dmpi->planes[0]+
400 vf->priv->exp_y*vf->dmpi->stride[0]+vf->priv->exp_x*(vf->dmpi->bpp/8),
401 mpi->planes[0], mpi->w*(vf->dmpi->bpp/8), mpi->h,
402 vf->dmpi->stride[0],mpi->stride[0]);
403 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
405 #ifdef OSD_SUPPORT
406 if(vf->priv->osd) draw_osd(vf,mpi->w,mpi->h);
407 #endif
408 return vf_next_put_image(vf,vf->dmpi, pts);
411 //===========================================================================//
413 static int control(struct vf_instance_s* vf, int request, void* data){
414 #ifdef OSD_SUPPORT
415 switch(request){
416 case VFCTRL_DRAW_OSD:
417 if(vf->priv->osd) return CONTROL_TRUE;
419 #endif
420 return vf_next_control(vf,request,data);
423 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
424 return vf_next_query_format(vf,fmt);
427 static int open(vf_instance_t *vf, char* args){
428 vf->config=config;
429 vf->control=control;
430 vf->query_format=query_format;
431 vf->start_slice=start_slice;
432 vf->draw_slice=draw_slice;
433 vf->get_image=get_image;
434 vf->put_image=put_image;
435 mp_msg(MSGT_VFILTER, MSGL_INFO, "Expand: %d x %d, %d ; %d, osd: %d, aspect: %lf, round: %d\n",
436 vf->priv->exp_w,
437 vf->priv->exp_h,
438 vf->priv->exp_x,
439 vf->priv->exp_y,
440 vf->priv->osd,
441 vf->priv->aspect,
442 vf->priv->round);
443 return 1;
446 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
447 static m_option_t vf_opts_fields[] = {
448 {"w", ST_OFF(exp_w), CONF_TYPE_INT, 0, 0 ,0, NULL},
449 {"h", ST_OFF(exp_h), CONF_TYPE_INT, 0, 0 ,0, NULL},
450 {"x", ST_OFF(exp_x), CONF_TYPE_INT, M_OPT_MIN, -1, 0, NULL},
451 {"y", ST_OFF(exp_y), CONF_TYPE_INT, M_OPT_MIN, -1, 0, NULL},
452 {"osd", ST_OFF(osd), CONF_TYPE_FLAG, 0 , 0, 1, NULL},
453 {"aspect", ST_OFF(aspect), CONF_TYPE_DOUBLE, M_OPT_MIN, 0, 0, NULL},
454 {"round", ST_OFF(round), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL},
455 { NULL, NULL, 0, 0, 0, 0, NULL }
458 static m_struct_t vf_opts = {
459 "expand",
460 sizeof(struct vf_priv_s),
461 &vf_priv_dflt,
462 vf_opts_fields
467 const vf_info_t vf_info_expand = {
468 #ifdef OSD_SUPPORT
469 "expanding & osd",
470 #else
471 "expanding",
472 #endif
473 "expand",
474 "A'rpi",
476 open,
477 &vf_opts
480 //===========================================================================//