Improve VDPAU noforce-mixer documentation.
[mplayer.git] / libmpcodecs / vf_expand.c
blobbc3fb9c57dec62fbd64ba31a839f946550391da3
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"
16 #include "libavutil/avutil.h"
18 #ifdef OSD_SUPPORT
19 #include "libvo/sub.h"
20 #include "libvo/osd.h"
21 #endif
23 #include "m_option.h"
24 #include "m_struct.h"
26 static struct vf_priv_s {
27 // These four values are a backup of the values parsed from the command line.
28 // This is necessary so that we do not get a mess upon filter reinit due to
29 // e.g. aspect changes and with only aspect specified on the command line,
30 // where we would otherwise use the values calculated for a different aspect
31 // instead of recalculating them again.
32 int cfg_exp_w, cfg_exp_h;
33 int cfg_exp_x, cfg_exp_y;
34 int exp_w,exp_h;
35 int exp_x,exp_y;
36 int osd;
37 double aspect;
38 int round;
39 unsigned char* fb_ptr;
40 int passthrough;
41 int first_slice;
42 } const vf_priv_dflt = {
43 -1,-1,
44 -1,-1,
46 0.,
48 NULL,
53 extern int opt_screen_size_x;
54 extern int opt_screen_size_y;
56 //===========================================================================//
57 #ifdef OSD_SUPPORT
59 static struct vf_instance_s* vf=NULL; // fixme (needs sub.c changes)
60 static int orig_w,orig_h;
62 static void remove_func_2(int x0,int y0, int w,int h){
63 // TODO: let's cleanup the place
64 //printf("OSD clear: %d;%d %dx%d \n",x0,y0,w,h);
65 vf_mpi_clear(vf->dmpi,x0,y0,w,h);
68 static void remove_func(int x0,int y0, int w,int h){
69 if(!vo_osd_changed_flag) return;
70 // split it to 4 parts:
71 if(y0<vf->priv->exp_y){
72 // it has parts above the image:
73 int y=y0+h;
74 if(y>vf->priv->exp_y) y=vf->priv->exp_y;
75 remove_func_2(x0,y0,w,y-y0);
76 if(y0+h<=vf->priv->exp_y) return;
77 h-=y-y0;y0=y;
79 if(y0+h>vf->priv->exp_y+orig_h){
80 // it has parts under the image:
81 int y=y0;
82 if(y<vf->priv->exp_y+orig_h) y=vf->priv->exp_y+orig_h;
83 remove_func_2(x0,y,w,y0+h-y);
84 if(y0>=vf->priv->exp_y+orig_h) return;
85 h=y-y0;
87 if(x0<vf->priv->exp_x){
88 // it has parts on the left side of the image:
89 int x=x0+w;
90 if(x>vf->priv->exp_x) x=vf->priv->exp_x;
91 remove_func_2(x0,y0,x-x0,h);
92 if(x0+w<=vf->priv->exp_x) return;
93 w-=x-x0;x0=x;
95 if(x0+w>vf->priv->exp_x+orig_w){
96 // it has parts on the right side of the image:
97 int x=x0;
98 if(x<vf->priv->exp_x+orig_w) x=vf->priv->exp_x+orig_w;
99 remove_func_2(x,y0,x0+w-x,h);
100 if(x0>=vf->priv->exp_x+orig_w) return;
101 w=x-x0;
105 static void draw_func(int x0,int y0, int w,int h,unsigned char* src, unsigned char *srca, int stride){
106 unsigned char* dst;
107 if(!vo_osd_changed_flag && vf->dmpi->planes[0]==vf->priv->fb_ptr){
108 // ok, enough to update the area inside the video, leave the black bands
109 // untouched!
110 if(x0<vf->priv->exp_x){
111 int tmp=vf->priv->exp_x-x0;
112 w-=tmp; src+=tmp; srca+=tmp; x0+=tmp;
114 if(y0<vf->priv->exp_y){
115 int tmp=vf->priv->exp_y-y0;
116 h-=tmp; src+=tmp*stride; srca+=tmp*stride; y0+=tmp;
118 if(x0+w>vf->priv->exp_x+orig_w){
119 w=vf->priv->exp_x+orig_w-x0;
121 if(y0+h>vf->priv->exp_y+orig_h){
122 h=vf->priv->exp_y+orig_h-y0;
125 if(w<=0 || h<=0) return; // nothing to do...
126 // printf("OSD redraw: %d;%d %dx%d \n",x0,y0,w,h);
127 dst=vf->dmpi->planes[0]+
128 vf->dmpi->stride[0]*y0+
129 (vf->dmpi->bpp>>3)*x0;
130 switch(vf->dmpi->imgfmt){
131 case IMGFMT_BGR15:
132 case IMGFMT_RGB15:
133 vo_draw_alpha_rgb15(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
134 break;
135 case IMGFMT_BGR16:
136 case IMGFMT_RGB16:
137 vo_draw_alpha_rgb16(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
138 break;
139 case IMGFMT_BGR24:
140 case IMGFMT_RGB24:
141 vo_draw_alpha_rgb24(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
142 break;
143 case IMGFMT_BGR32:
144 case IMGFMT_RGB32:
145 vo_draw_alpha_rgb32(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
146 break;
147 case IMGFMT_YV12:
148 case IMGFMT_I420:
149 case IMGFMT_IYUV:
150 case IMGFMT_YVU9:
151 case IMGFMT_IF09:
152 case IMGFMT_Y800:
153 case IMGFMT_Y8:
154 vo_draw_alpha_yv12(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
155 break;
156 case IMGFMT_YUY2:
157 vo_draw_alpha_yuy2(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
158 break;
159 case IMGFMT_UYVY:
160 vo_draw_alpha_yuy2(w,h,src,srca,stride,dst+1,vf->dmpi->stride[0]);
161 break;
165 static void draw_osd(struct vf_instance_s* vf_,int w,int h){
166 vf=vf_;orig_w=w;orig_h=h;
167 // printf("======================================\n");
168 if(vf->priv->exp_w!=w || vf->priv->exp_h!=h ||
169 vf->priv->exp_x || vf->priv->exp_y){
170 // yep, we're expanding image, not just copy.
171 if(vf->dmpi->planes[0]!=vf->priv->fb_ptr){
172 // double buffering, so we need full clear :(
173 if (vf->priv->exp_y > 0)
174 remove_func_2(0,0,vf->priv->exp_w,vf->priv->exp_y);
175 if (vf->priv->exp_y+h < vf->priv->exp_h)
176 remove_func_2(0,vf->priv->exp_y+h,vf->priv->exp_w,vf->priv->exp_h-h-vf->priv->exp_y);
177 if (vf->priv->exp_x > 0)
178 remove_func_2(0,vf->priv->exp_y,vf->priv->exp_x,h);
179 if (vf->priv->exp_x+w < vf->priv->exp_w)
180 remove_func_2(vf->priv->exp_x+w,vf->priv->exp_y,vf->priv->exp_w-w-vf->priv->exp_x,h);
181 } else {
182 // partial clear:
183 vo_remove_text(vf->priv->exp_w,vf->priv->exp_h,remove_func);
186 vo_draw_text(vf->priv->exp_w,vf->priv->exp_h,draw_func);
187 // save buffer pointer for double buffering detection - yes, i know it's
188 // ugly method, but note that codecs with DR support does the same...
189 if(vf->dmpi)
190 vf->priv->fb_ptr=vf->dmpi->planes[0];
193 #endif
194 //===========================================================================//
196 static int config(struct vf_instance_s* vf,
197 int width, int height, int d_width, int d_height,
198 unsigned int flags, unsigned int outfmt){
199 if(outfmt == IMGFMT_MPEGPES) {
200 vf->priv->passthrough = 1;
201 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
203 if (outfmt == IMGFMT_IF09) return 0;
204 vf->priv->exp_x = vf->priv->cfg_exp_x;
205 vf->priv->exp_y = vf->priv->cfg_exp_y;
206 vf->priv->exp_w = vf->priv->cfg_exp_w;
207 vf->priv->exp_h = vf->priv->cfg_exp_h;
208 // calculate the missing parameters:
209 #if 0
210 if(vf->priv->exp_w<width) vf->priv->exp_w=width;
211 if(vf->priv->exp_h<height) vf->priv->exp_h=height;
212 #else
213 if ( vf->priv->exp_w == -1 ) vf->priv->exp_w=width;
214 else if (vf->priv->exp_w < -1 ) vf->priv->exp_w=width - vf->priv->exp_w;
215 else if ( vf->priv->exp_w<width ) vf->priv->exp_w=width;
216 if ( vf->priv->exp_h == -1 ) vf->priv->exp_h=height;
217 else if ( vf->priv->exp_h < -1 ) vf->priv->exp_h=height - vf->priv->exp_h;
218 else if( vf->priv->exp_h<height ) vf->priv->exp_h=height;
219 #endif
220 if (vf->priv->aspect) {
221 float adjusted_aspect = vf->priv->aspect;
222 adjusted_aspect *= ((double)width/height) / ((double)d_width/d_height);
223 if (vf->priv->exp_h < vf->priv->exp_w / adjusted_aspect) {
224 vf->priv->exp_h = vf->priv->exp_w / adjusted_aspect + 0.5;
225 } else {
226 vf->priv->exp_w = vf->priv->exp_h * adjusted_aspect + 0.5;
229 if (vf->priv->round > 1) { // round up.
230 vf->priv->exp_w = (1 + (vf->priv->exp_w - 1) / vf->priv->round) * vf->priv->round;
231 vf->priv->exp_h = (1 + (vf->priv->exp_h - 1) / vf->priv->round) * vf->priv->round;
234 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;
235 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;
236 vf->priv->fb_ptr=NULL;
238 if(!opt_screen_size_x && !opt_screen_size_y){
239 d_width=d_width*vf->priv->exp_w/width;
240 d_height=d_height*vf->priv->exp_h/height;
242 return vf_next_config(vf,vf->priv->exp_w,vf->priv->exp_h,d_width,d_height,flags,outfmt);
245 // there are 4 cases:
246 // codec --DR--> expand --DR--> vo
247 // codec --DR--> expand -copy-> vo
248 // codec -copy-> expand --DR--> vo
249 // codec -copy-> expand -copy-> vo (worst case)
251 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
252 // if(mpi->type==MP_IMGTYPE_IPB) return; // not yet working
253 #ifdef OSD_SUPPORT
254 if(vf->priv->osd && (mpi->flags&MP_IMGFLAG_PRESERVE)){
255 // check if we have to render osd!
256 vo_update_osd(vf->priv->exp_w, vf->priv->exp_h);
257 if(vo_osd_check_range_update(vf->priv->exp_x,vf->priv->exp_y,
258 vf->priv->exp_x+mpi->w,vf->priv->exp_y+mpi->h)) return;
260 #endif
261 if(vf->priv->exp_w==mpi->width ||
262 (mpi->flags&(MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_ACCEPT_WIDTH)) ){
263 // try full DR !
264 mpi->priv=vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
265 mpi->type, mpi->flags,
266 FFMAX(vf->priv->exp_w, mpi->width +vf->priv->exp_x),
267 FFMAX(vf->priv->exp_h, mpi->height+vf->priv->exp_y));
268 #if 1
269 if((vf->dmpi->flags & MP_IMGFLAG_DRAW_CALLBACK) &&
270 !(vf->dmpi->flags & MP_IMGFLAG_DIRECT)){
271 mp_msg(MSGT_VFILTER, MSGL_INFO, MSGTR_MPCODECS_FullDRNotPossible);
272 return;
274 #endif
275 // set up mpi as a cropped-down image of dmpi:
276 if(mpi->flags&MP_IMGFLAG_PLANAR){
277 mpi->planes[0]=vf->dmpi->planes[0]+
278 vf->priv->exp_y*vf->dmpi->stride[0]+vf->priv->exp_x;
279 mpi->planes[1]=vf->dmpi->planes[1]+
280 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[1]+(vf->priv->exp_x>>mpi->chroma_x_shift);
281 mpi->planes[2]=vf->dmpi->planes[2]+
282 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[2]+(vf->priv->exp_x>>mpi->chroma_x_shift);
283 mpi->stride[1]=vf->dmpi->stride[1];
284 mpi->stride[2]=vf->dmpi->stride[2];
285 } else {
286 mpi->planes[0]=vf->dmpi->planes[0]+
287 vf->priv->exp_y*vf->dmpi->stride[0]+
288 vf->priv->exp_x*(vf->dmpi->bpp/8);
290 mpi->stride[0]=vf->dmpi->stride[0];
291 mpi->width=vf->dmpi->width;
292 mpi->flags|=MP_IMGFLAG_DIRECT;
293 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
294 // vf->dmpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
298 static void start_slice(struct vf_instance_s* vf, mp_image_t *mpi){
299 // printf("start_slice called! flag=%d\n",mpi->flags&MP_IMGFLAG_DRAW_CALLBACK);
300 if(!vf->next->draw_slice){
301 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
302 return;
304 // they want slices!!! allocate the buffer.
305 if(!mpi->priv)
306 mpi->priv=vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
307 // MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
308 MP_IMGTYPE_TEMP, mpi->flags,
309 FFMAX(vf->priv->exp_w, mpi->width +vf->priv->exp_x),
310 FFMAX(vf->priv->exp_h, mpi->height+vf->priv->exp_y));
311 if(!(vf->dmpi->flags&MP_IMGFLAG_DRAW_CALLBACK))
312 mp_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_WarnNextFilterDoesntSupportSlices); // shouldn't happen.
313 vf->priv->first_slice = 1;
316 static void draw_top_blackbar_slice(struct vf_instance_s* vf,
317 unsigned char** src, int* stride, int w,int h, int x, int y){
318 if(vf->priv->exp_y>0 && y == 0) {
319 vf_next_draw_slice(vf, vf->dmpi->planes, vf->dmpi->stride,
320 vf->dmpi->w,vf->priv->exp_y,0,0);
325 static void draw_bottom_blackbar_slice(struct vf_instance_s* vf,
326 unsigned char** src, int* stride, int w,int h, int x, int y){
327 if(vf->priv->exp_y+vf->h<vf->dmpi->h && y+h == vf->h) {
328 unsigned char *src2[MP_MAX_PLANES];
329 src2[0] = vf->dmpi->planes[0]
330 + (vf->priv->exp_y+vf->h)*vf->dmpi->stride[0];
331 if(vf->dmpi->flags&MP_IMGFLAG_PLANAR){
332 src2[1] = vf->dmpi->planes[1]
333 + ((vf->priv->exp_y+vf->h)>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1];
334 src2[2] = vf->dmpi->planes[2]
335 + ((vf->priv->exp_y+vf->h)>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2];
336 } else {
337 src2[1] = vf->dmpi->planes[1]; // passthrough rgb8 palette
339 vf_next_draw_slice(vf, src2, vf->dmpi->stride,
340 vf->dmpi->w,vf->dmpi->h-(vf->priv->exp_y+vf->h),
341 0,vf->priv->exp_y+vf->h);
345 static void draw_slice(struct vf_instance_s* vf,
346 unsigned char** src, int* stride, int w,int h, int x, int y){
347 // printf("draw_slice() called %d at %d\n",h,y);
349 if (y == 0 && y+h == vf->h) {
350 // special case - only one slice
351 draw_top_blackbar_slice(vf, src, stride, w, h, x, y);
352 vf_next_draw_slice(vf,src,stride,w,h,x+vf->priv->exp_x,y+vf->priv->exp_y);
353 draw_bottom_blackbar_slice(vf, src, stride, w, h, x, y);
354 return;
356 if (vf->priv->first_slice) {
357 draw_top_blackbar_slice(vf, src, stride, w, h, x, y);
358 draw_bottom_blackbar_slice(vf, src, stride, w, h, x, y);
360 vf_next_draw_slice(vf,src,stride,w,h,x+vf->priv->exp_x,y+vf->priv->exp_y);
361 if (!vf->priv->first_slice) {
362 draw_top_blackbar_slice(vf, src, stride, w, h, x, y);
363 draw_bottom_blackbar_slice(vf, src, stride, w, h, x, y);
365 vf->priv->first_slice = 0;
368 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
369 if (vf->priv->passthrough) {
370 mp_image_t *dmpi = vf_get_image(vf->next, IMGFMT_MPEGPES,
371 MP_IMGTYPE_EXPORT, 0, mpi->w, mpi->h);
372 dmpi->planes[0]=mpi->planes[0];
373 return vf_next_put_image(vf,dmpi, pts);
376 if(mpi->flags&MP_IMGFLAG_DIRECT || mpi->flags&MP_IMGFLAG_DRAW_CALLBACK){
377 vf->dmpi=mpi->priv;
378 if(!vf->dmpi) { mp_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_FunWhydowegetNULL); return 0; }
379 mpi->priv=NULL;
380 #ifdef OSD_SUPPORT
381 if(vf->priv->osd) draw_osd(vf,mpi->w,mpi->h);
382 #endif
383 // we've used DR, so we're ready...
384 if(!(mpi->flags&MP_IMGFLAG_PLANAR))
385 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
386 return vf_next_put_image(vf,vf->dmpi, pts);
389 // hope we'll get DR buffer:
390 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
391 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
392 vf->priv->exp_w, vf->priv->exp_h);
394 // copy mpi->dmpi...
395 if(mpi->flags&MP_IMGFLAG_PLANAR){
396 memcpy_pic(vf->dmpi->planes[0]+
397 vf->priv->exp_y*vf->dmpi->stride[0]+vf->priv->exp_x,
398 mpi->planes[0], mpi->w, mpi->h,
399 vf->dmpi->stride[0],mpi->stride[0]);
400 memcpy_pic(vf->dmpi->planes[1]+
401 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[1]+(vf->priv->exp_x>>mpi->chroma_x_shift),
402 mpi->planes[1], (mpi->w>>mpi->chroma_x_shift), (mpi->h>>mpi->chroma_y_shift),
403 vf->dmpi->stride[1],mpi->stride[1]);
404 memcpy_pic(vf->dmpi->planes[2]+
405 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[2]+(vf->priv->exp_x>>mpi->chroma_x_shift),
406 mpi->planes[2], (mpi->w>>mpi->chroma_x_shift), (mpi->h>>mpi->chroma_y_shift),
407 vf->dmpi->stride[2],mpi->stride[2]);
408 } else {
409 memcpy_pic(vf->dmpi->planes[0]+
410 vf->priv->exp_y*vf->dmpi->stride[0]+vf->priv->exp_x*(vf->dmpi->bpp/8),
411 mpi->planes[0], mpi->w*(vf->dmpi->bpp/8), mpi->h,
412 vf->dmpi->stride[0],mpi->stride[0]);
413 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
415 #ifdef OSD_SUPPORT
416 if(vf->priv->osd) draw_osd(vf,mpi->w,mpi->h);
417 #endif
418 return vf_next_put_image(vf,vf->dmpi, pts);
421 //===========================================================================//
423 static int control(struct vf_instance_s* vf, int request, void* data){
424 #ifdef OSD_SUPPORT
425 switch(request){
426 case VFCTRL_DRAW_OSD:
427 if(vf->priv->osd) return CONTROL_TRUE;
429 #endif
430 return vf_next_control(vf,request,data);
433 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
434 return vf_next_query_format(vf,fmt);
437 static int open(vf_instance_t *vf, char* args){
438 vf->config=config;
439 vf->control=control;
440 vf->query_format=query_format;
441 vf->start_slice=start_slice;
442 vf->draw_slice=draw_slice;
443 vf->get_image=get_image;
444 vf->put_image=put_image;
445 mp_msg(MSGT_VFILTER, MSGL_INFO, "Expand: %d x %d, %d ; %d, osd: %d, aspect: %f, round: %d\n",
446 vf->priv->cfg_exp_w,
447 vf->priv->cfg_exp_h,
448 vf->priv->cfg_exp_x,
449 vf->priv->cfg_exp_y,
450 vf->priv->osd,
451 vf->priv->aspect,
452 vf->priv->round);
453 return 1;
456 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
457 static m_option_t vf_opts_fields[] = {
458 {"w", ST_OFF(cfg_exp_w), CONF_TYPE_INT, 0, 0 ,0, NULL},
459 {"h", ST_OFF(cfg_exp_h), CONF_TYPE_INT, 0, 0 ,0, NULL},
460 {"x", ST_OFF(cfg_exp_x), CONF_TYPE_INT, M_OPT_MIN, -1, 0, NULL},
461 {"y", ST_OFF(cfg_exp_y), CONF_TYPE_INT, M_OPT_MIN, -1, 0, NULL},
462 {"osd", ST_OFF(osd), CONF_TYPE_FLAG, 0 , 0, 1, NULL},
463 {"aspect", ST_OFF(aspect), CONF_TYPE_DOUBLE, M_OPT_MIN, 0, 0, NULL},
464 {"round", ST_OFF(round), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL},
465 { NULL, NULL, 0, 0, 0, 0, NULL }
468 static m_struct_t vf_opts = {
469 "expand",
470 sizeof(struct vf_priv_s),
471 &vf_priv_dflt,
472 vf_opts_fields
477 const vf_info_t vf_info_expand = {
478 #ifdef OSD_SUPPORT
479 "expanding & osd",
480 #else
481 "expanding",
482 #endif
483 "expand",
484 "A'rpi",
486 open,
487 &vf_opts
490 //===========================================================================//