13 #include "img_format.h"
17 #include "libvo/fastmemcpy.h"
20 #include "libvo/sub.h"
21 #include "libvo/osd.h"
27 #define MAX(a,b) ((a) > (b) ? (a) : (b))
29 static struct vf_priv_s
{
35 unsigned char* fb_ptr
;
38 struct osd_state
*osd
;
39 } const vf_priv_dflt
= {
50 //===========================================================================//
53 static struct vf_instance
* 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:
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;
73 if(y0
+h
>vf
->priv
->exp_y
+orig_h
){
74 // it has parts under the image:
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;
81 if(x0
<vf
->priv
->exp_x
){
82 // it has parts on the left side of the image:
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;
89 if(x0
+w
>vf
->priv
->exp_x
+orig_w
){
90 // it has parts on the right side of the image:
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;
99 static void draw_func(void *ctx
, int x0
,int y0
, int w
,int h
,unsigned char* src
, unsigned char *srca
, int stride
){
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
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
){
127 vo_draw_alpha_rgb15(w
,h
,src
,srca
,stride
,dst
,vf
->dmpi
->stride
[0]);
131 vo_draw_alpha_rgb16(w
,h
,src
,srca
,stride
,dst
,vf
->dmpi
->stride
[0]);
135 vo_draw_alpha_rgb24(w
,h
,src
,srca
,stride
,dst
,vf
->dmpi
->stride
[0]);
139 vo_draw_alpha_rgb32(w
,h
,src
,srca
,stride
,dst
,vf
->dmpi
->stride
[0]);
148 vo_draw_alpha_yv12(w
,h
,src
,srca
,stride
,dst
,vf
->dmpi
->stride
[0]);
151 vo_draw_alpha_yuy2(w
,h
,src
,srca
,stride
,dst
,vf
->dmpi
->stride
[0]);
154 vo_draw_alpha_yuy2(w
,h
,src
,srca
,stride
,dst
+1,vf
->dmpi
->stride
[0]);
159 static void draw_osd(struct vf_instance
* 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
);
177 osd_remove_text(vf
->priv
->osd
, vf
->priv
->exp_w
,vf
->priv
->exp_h
,remove_func
);
180 osd_draw_text(vf
->priv
->osd
, vf
->priv
->exp_w
,vf
->priv
->exp_h
,draw_func
, NULL
);
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...
184 vf
->priv
->fb_ptr
=vf
->dmpi
->planes
[0];
188 //===========================================================================//
190 static int config(struct vf_instance
* vf
,
191 int width
, int height
, int d_width
, int d_height
,
192 unsigned int flags
, unsigned int outfmt
)
194 struct MPOpts
*opts
= vf
->opts
;
195 if(outfmt
== IMGFMT_MPEGPES
) {
196 vf
->priv
->passthrough
= 1;
197 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
199 if (outfmt
== IMGFMT_IF09
) return 0;
200 // calculate the missing parameters:
202 if(vf
->priv
->exp_w
<width
) vf
->priv
->exp_w
=width
;
203 if(vf
->priv
->exp_h
<height
) vf
->priv
->exp_h
=height
;
205 if ( vf
->priv
->exp_w
== -1 ) vf
->priv
->exp_w
=width
;
206 else if (vf
->priv
->exp_w
< -1 ) vf
->priv
->exp_w
=width
- vf
->priv
->exp_w
;
207 else if ( vf
->priv
->exp_w
<width
) vf
->priv
->exp_w
=width
;
208 if ( vf
->priv
->exp_h
== -1 ) vf
->priv
->exp_h
=height
;
209 else if ( vf
->priv
->exp_h
< -1 ) vf
->priv
->exp_h
=height
- vf
->priv
->exp_h
;
210 else if( vf
->priv
->exp_h
<height
) vf
->priv
->exp_h
=height
;
212 if (vf
->priv
->aspect
) {
213 float adjusted_aspect
= vf
->priv
->aspect
;
214 adjusted_aspect
*= ((double)width
/height
) / ((double)d_width
/d_height
);
215 if (vf
->priv
->exp_h
< vf
->priv
->exp_w
/ adjusted_aspect
) {
216 vf
->priv
->exp_h
= vf
->priv
->exp_w
/ adjusted_aspect
+ 0.5;
218 vf
->priv
->exp_w
= vf
->priv
->exp_h
* adjusted_aspect
+ 0.5;
221 if (vf
->priv
->round
> 1) { // round up.
222 vf
->priv
->exp_w
= (1 + (vf
->priv
->exp_w
- 1) / vf
->priv
->round
) * vf
->priv
->round
;
223 vf
->priv
->exp_h
= (1 + (vf
->priv
->exp_h
- 1) / vf
->priv
->round
) * vf
->priv
->round
;
226 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;
227 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;
228 vf
->priv
->fb_ptr
=NULL
;
230 if(!opts
->screen_size_x
&& !opts
->screen_size_y
){
231 d_width
=d_width
*vf
->priv
->exp_w
/width
;
232 d_height
=d_height
*vf
->priv
->exp_h
/height
;
234 return vf_next_config(vf
,vf
->priv
->exp_w
,vf
->priv
->exp_h
,d_width
,d_height
,flags
,outfmt
);
237 // there are 4 cases:
238 // codec --DR--> expand --DR--> vo
239 // codec --DR--> expand -copy-> vo
240 // codec -copy-> expand --DR--> vo
241 // codec -copy-> expand -copy-> vo (worst case)
243 static void get_image(struct vf_instance
* vf
, mp_image_t
*mpi
){
244 // if(mpi->type==MP_IMGTYPE_IPB) return; // not yet working
246 if(vf
->priv
->osd_enabled
&& (mpi
->flags
&MP_IMGFLAG_PRESERVE
)){
247 // check if we have to render osd!
248 osd_update(vf
->priv
->osd
, vf
->priv
->exp_w
, vf
->priv
->exp_h
);
249 if(vo_osd_check_range_update(vf
->priv
->exp_x
,vf
->priv
->exp_y
,
250 vf
->priv
->exp_x
+mpi
->w
,vf
->priv
->exp_y
+mpi
->h
)) return;
253 if(vf
->priv
->exp_w
==mpi
->width
||
254 (mpi
->flags
&(MP_IMGFLAG_ACCEPT_STRIDE
|MP_IMGFLAG_ACCEPT_WIDTH
)) ){
256 mpi
->priv
=vf
->dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
257 mpi
->type
, mpi
->flags
,
258 MAX(vf
->priv
->exp_w
, mpi
->width
+vf
->priv
->exp_x
),
259 MAX(vf
->priv
->exp_h
, mpi
->height
+vf
->priv
->exp_y
));
261 if((vf
->dmpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
) &&
262 !(vf
->dmpi
->flags
& MP_IMGFLAG_DIRECT
)){
263 mp_tmsg(MSGT_VFILTER
, MSGL_INFO
, "Full DR not possible, trying SLICES instead!\n");
267 // set up mpi as a cropped-down image of dmpi:
268 if(mpi
->flags
&MP_IMGFLAG_PLANAR
){
269 mpi
->planes
[0]=vf
->dmpi
->planes
[0]+
270 vf
->priv
->exp_y
*vf
->dmpi
->stride
[0]+vf
->priv
->exp_x
;
271 mpi
->planes
[1]=vf
->dmpi
->planes
[1]+
272 (vf
->priv
->exp_y
>>mpi
->chroma_y_shift
)*vf
->dmpi
->stride
[1]+(vf
->priv
->exp_x
>>mpi
->chroma_x_shift
);
273 mpi
->planes
[2]=vf
->dmpi
->planes
[2]+
274 (vf
->priv
->exp_y
>>mpi
->chroma_y_shift
)*vf
->dmpi
->stride
[2]+(vf
->priv
->exp_x
>>mpi
->chroma_x_shift
);
275 mpi
->stride
[1]=vf
->dmpi
->stride
[1];
276 mpi
->stride
[2]=vf
->dmpi
->stride
[2];
278 mpi
->planes
[0]=vf
->dmpi
->planes
[0]+
279 vf
->priv
->exp_y
*vf
->dmpi
->stride
[0]+
280 vf
->priv
->exp_x
*(vf
->dmpi
->bpp
/8);
282 mpi
->stride
[0]=vf
->dmpi
->stride
[0];
283 mpi
->width
=vf
->dmpi
->width
;
284 mpi
->flags
|=MP_IMGFLAG_DIRECT
;
285 mpi
->flags
&=~MP_IMGFLAG_DRAW_CALLBACK
;
286 // vf->dmpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
290 static void start_slice(struct vf_instance
* vf
, mp_image_t
*mpi
){
291 // printf("start_slice called! flag=%d\n",mpi->flags&MP_IMGFLAG_DRAW_CALLBACK);
292 if(!vf
->next
->draw_slice
){
293 mpi
->flags
&=~MP_IMGFLAG_DRAW_CALLBACK
;
296 // they want slices!!! allocate the buffer.
298 mpi
->priv
=vf
->dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
299 // MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
300 MP_IMGTYPE_TEMP
, mpi
->flags
,
301 MAX(vf
->priv
->exp_w
, mpi
->width
+vf
->priv
->exp_x
),
302 MAX(vf
->priv
->exp_h
, mpi
->height
+vf
->priv
->exp_y
));
303 if(!(vf
->dmpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
))
304 mp_tmsg(MSGT_VFILTER
, MSGL_WARN
, "WARNING! Next filter doesn't support SLICES, get ready for sig11...\n"); // shouldn't happen.
305 vf
->priv
->first_slice
= 1;
308 static void draw_top_blackbar_slice(struct vf_instance
* vf
,
309 unsigned char** src
, int* stride
, int w
,int h
, int x
, int y
){
310 if(vf
->priv
->exp_y
>0 && y
== 0) {
311 vf_next_draw_slice(vf
, vf
->dmpi
->planes
, vf
->dmpi
->stride
,
312 vf
->dmpi
->w
,vf
->priv
->exp_y
,0,0);
317 static void draw_bottom_blackbar_slice(struct vf_instance
* vf
,
318 unsigned char** src
, int* stride
, int w
,int h
, int x
, int y
){
319 if(vf
->priv
->exp_y
+vf
->h
<vf
->dmpi
->h
&& y
+h
== vf
->h
) {
320 unsigned char *src2
[MP_MAX_PLANES
];
321 src2
[0] = vf
->dmpi
->planes
[0]
322 + (vf
->priv
->exp_y
+vf
->h
)*vf
->dmpi
->stride
[0];
323 if(vf
->dmpi
->flags
&MP_IMGFLAG_PLANAR
){
324 src2
[1] = vf
->dmpi
->planes
[1]
325 + ((vf
->priv
->exp_y
+vf
->h
)>>vf
->dmpi
->chroma_y_shift
)*vf
->dmpi
->stride
[1];
326 src2
[2] = vf
->dmpi
->planes
[2]
327 + ((vf
->priv
->exp_y
+vf
->h
)>>vf
->dmpi
->chroma_y_shift
)*vf
->dmpi
->stride
[2];
329 src2
[1] = vf
->dmpi
->planes
[1]; // passthrough rgb8 palette
331 vf_next_draw_slice(vf
, src2
, vf
->dmpi
->stride
,
332 vf
->dmpi
->w
,vf
->dmpi
->h
-(vf
->priv
->exp_y
+vf
->h
),
333 0,vf
->priv
->exp_y
+vf
->h
);
337 static void draw_slice(struct vf_instance
* vf
,
338 unsigned char** src
, int* stride
, int w
,int h
, int x
, int y
){
339 // printf("draw_slice() called %d at %d\n",h,y);
341 if (y
== 0 && y
+h
== vf
->h
) {
342 // special case - only one slice
343 draw_top_blackbar_slice(vf
, src
, stride
, w
, h
, x
, y
);
344 vf_next_draw_slice(vf
,src
,stride
,w
,h
,x
+vf
->priv
->exp_x
,y
+vf
->priv
->exp_y
);
345 draw_bottom_blackbar_slice(vf
, src
, stride
, w
, h
, x
, y
);
348 if (vf
->priv
->first_slice
) {
349 draw_top_blackbar_slice(vf
, src
, stride
, w
, h
, x
, y
);
350 draw_bottom_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 if (!vf
->priv
->first_slice
) {
354 draw_top_blackbar_slice(vf
, src
, stride
, w
, h
, x
, y
);
355 draw_bottom_blackbar_slice(vf
, src
, stride
, w
, h
, x
, y
);
357 vf
->priv
->first_slice
= 0;
360 static int put_image(struct vf_instance
* vf
, mp_image_t
*mpi
, double pts
){
361 if (vf
->priv
->passthrough
) {
362 mp_image_t
*dmpi
= vf_get_image(vf
->next
, IMGFMT_MPEGPES
,
363 MP_IMGTYPE_EXPORT
, 0, mpi
->w
, mpi
->h
);
364 dmpi
->planes
[0]=mpi
->planes
[0];
365 return vf_next_put_image(vf
,dmpi
, pts
);
368 if(mpi
->flags
&MP_IMGFLAG_DIRECT
|| mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
){
370 if(!vf
->dmpi
) { mp_tmsg(MSGT_VFILTER
, MSGL_WARN
, "Why do we get NULL??\n"); return 0; }
373 if(vf
->priv
->osd_enabled
) draw_osd(vf
,mpi
->w
,mpi
->h
);
375 // we've used DR, so we're ready...
376 if(!(mpi
->flags
&MP_IMGFLAG_PLANAR
))
377 vf
->dmpi
->planes
[1] = mpi
->planes
[1]; // passthrough rgb8 palette
378 return vf_next_put_image(vf
,vf
->dmpi
, pts
);
381 // hope we'll get DR buffer:
382 vf
->dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
383 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
384 vf
->priv
->exp_w
, vf
->priv
->exp_h
);
387 if(mpi
->flags
&MP_IMGFLAG_PLANAR
){
388 memcpy_pic(vf
->dmpi
->planes
[0]+
389 vf
->priv
->exp_y
*vf
->dmpi
->stride
[0]+vf
->priv
->exp_x
,
390 mpi
->planes
[0], mpi
->w
, mpi
->h
,
391 vf
->dmpi
->stride
[0],mpi
->stride
[0]);
392 memcpy_pic(vf
->dmpi
->planes
[1]+
393 (vf
->priv
->exp_y
>>mpi
->chroma_y_shift
)*vf
->dmpi
->stride
[1]+(vf
->priv
->exp_x
>>mpi
->chroma_x_shift
),
394 mpi
->planes
[1], (mpi
->w
>>mpi
->chroma_x_shift
), (mpi
->h
>>mpi
->chroma_y_shift
),
395 vf
->dmpi
->stride
[1],mpi
->stride
[1]);
396 memcpy_pic(vf
->dmpi
->planes
[2]+
397 (vf
->priv
->exp_y
>>mpi
->chroma_y_shift
)*vf
->dmpi
->stride
[2]+(vf
->priv
->exp_x
>>mpi
->chroma_x_shift
),
398 mpi
->planes
[2], (mpi
->w
>>mpi
->chroma_x_shift
), (mpi
->h
>>mpi
->chroma_y_shift
),
399 vf
->dmpi
->stride
[2],mpi
->stride
[2]);
401 memcpy_pic(vf
->dmpi
->planes
[0]+
402 vf
->priv
->exp_y
*vf
->dmpi
->stride
[0]+vf
->priv
->exp_x
*(vf
->dmpi
->bpp
/8),
403 mpi
->planes
[0], mpi
->w
*(vf
->dmpi
->bpp
/8), mpi
->h
,
404 vf
->dmpi
->stride
[0],mpi
->stride
[0]);
405 vf
->dmpi
->planes
[1] = mpi
->planes
[1]; // passthrough rgb8 palette
408 if(vf
->priv
->osd_enabled
) draw_osd(vf
,mpi
->w
,mpi
->h
);
410 return vf_next_put_image(vf
,vf
->dmpi
, pts
);
413 //===========================================================================//
415 static int control(struct vf_instance
* vf
, int request
, void* data
){
418 case VFCTRL_SET_OSD_OBJ
:
419 vf
->priv
->osd
= data
;
421 case VFCTRL_DRAW_OSD
:
422 if(vf
->priv
->osd_enabled
) return CONTROL_TRUE
;
424 case VFCTRL_REDRAW_OSD
:
425 if (vf
->priv
->osd_enabled
)
430 return vf_next_control(vf
,request
,data
);
433 static int query_format(struct vf_instance
* vf
, unsigned int fmt
){
434 return vf_next_query_format(vf
,fmt
);
437 static int open(vf_instance_t
*vf
, char* args
){
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: %lf, round: %d\n",
450 vf
->priv
->osd_enabled
,
456 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
457 static const m_option_t vf_opts_fields
[] = {
458 {"w", ST_OFF(exp_w
), CONF_TYPE_INT
, 0, 0 ,0, NULL
},
459 {"h", ST_OFF(exp_h
), CONF_TYPE_INT
, 0, 0 ,0, NULL
},
460 {"x", ST_OFF(exp_x
), CONF_TYPE_INT
, M_OPT_MIN
, -1, 0, NULL
},
461 {"y", ST_OFF(exp_y
), CONF_TYPE_INT
, M_OPT_MIN
, -1, 0, NULL
},
462 {"osd", ST_OFF(osd_enabled
), 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 const m_struct_t vf_opts
= {
470 sizeof(struct vf_priv_s
),
477 const vf_info_t vf_info_expand
= {
490 //===========================================================================//