win32: core: wake up more often to poll for input
[mplayer.git] / libmpcodecs / vf_expand.c
blob6cc7a810b6092b6f3a925f58a528b69ae69f4972
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #define OSD_SUPPORT
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdbool.h>
26 #include "config.h"
27 #include "mp_msg.h"
28 #include "options.h"
30 #include "img_format.h"
31 #include "mp_image.h"
32 #include "vf.h"
34 #include "libvo/fastmemcpy.h"
35 #include "libavutil/avutil.h"
37 #ifdef OSD_SUPPORT
38 #include "sub/sub.h"
39 #include "libvo/osd.h"
40 #endif
42 #include "m_option.h"
43 #include "m_struct.h"
45 static struct vf_priv_s {
46 // These four values are a backup of the values parsed from the command line.
47 // This is necessary so that we do not get a mess upon filter reinit due to
48 // e.g. aspect changes and with only aspect specified on the command line,
49 // where we would otherwise use the values calculated for a different aspect
50 // instead of recalculating them again.
51 int cfg_exp_w, cfg_exp_h;
52 int cfg_exp_x, cfg_exp_y;
53 int exp_w,exp_h;
54 int exp_x,exp_y;
55 int osd_enabled;
56 double aspect;
57 int round;
58 int passthrough;
59 int first_slice;
60 struct osd_state *osd;
61 } const vf_priv_dflt = {
62 -1,-1,
63 -1,-1,
64 -1,-1,
65 -1,-1,
67 0.,
73 //===========================================================================//
74 #ifdef OSD_SUPPORT
76 static void draw_func(void *ctx, int x0,int y0, int w,int h,unsigned char* src, unsigned char *srca, int stride){
77 struct vf_instance *vf = ctx;
78 unsigned char* dst;
79 if(w<=0 || h<=0) return; // nothing to do...
80 // printf("OSD redraw: %d;%d %dx%d \n",x0,y0,w,h);
81 dst=vf->dmpi->planes[0]+
82 vf->dmpi->stride[0]*y0+
83 (vf->dmpi->bpp>>3)*x0;
84 switch(vf->dmpi->imgfmt){
85 case IMGFMT_BGR12:
86 case IMGFMT_RGB12:
87 vo_draw_alpha_rgb12(w, h, src, srca, stride, dst, vf->dmpi->stride[0]);
88 break;
89 case IMGFMT_BGR15:
90 case IMGFMT_RGB15:
91 vo_draw_alpha_rgb15(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
92 break;
93 case IMGFMT_BGR16:
94 case IMGFMT_RGB16:
95 vo_draw_alpha_rgb16(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
96 break;
97 case IMGFMT_BGR24:
98 case IMGFMT_RGB24:
99 vo_draw_alpha_rgb24(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
100 break;
101 case IMGFMT_BGR32:
102 case IMGFMT_RGB32:
103 vo_draw_alpha_rgb32(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
104 break;
105 case IMGFMT_YV12:
106 case IMGFMT_I420:
107 case IMGFMT_IYUV:
108 case IMGFMT_YVU9:
109 case IMGFMT_IF09:
110 case IMGFMT_Y800:
111 case IMGFMT_Y8:
112 vo_draw_alpha_yv12(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
113 break;
114 case IMGFMT_YUY2:
115 vo_draw_alpha_yuy2(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
116 break;
117 case IMGFMT_UYVY:
118 vo_draw_alpha_yuy2(w,h,src,srca,stride,dst+1,vf->dmpi->stride[0]);
119 break;
123 static void draw_osd(struct vf_instance *vf,int w,int h){
124 osd_draw_text(vf->priv->osd, vf->priv->exp_w,vf->priv->exp_h,draw_func,vf);
127 #endif
128 //===========================================================================//
130 static int config(struct vf_instance *vf,
131 int width, int height, int d_width, int d_height,
132 unsigned int flags, unsigned int outfmt)
134 struct MPOpts *opts = vf->opts;
135 if(outfmt == IMGFMT_MPEGPES) {
136 vf->priv->passthrough = 1;
137 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
139 if (outfmt == IMGFMT_IF09) return 0;
140 vf->priv->exp_x = vf->priv->cfg_exp_x;
141 vf->priv->exp_y = vf->priv->cfg_exp_y;
142 vf->priv->exp_w = vf->priv->cfg_exp_w;
143 vf->priv->exp_h = vf->priv->cfg_exp_h;
144 // calculate the missing parameters:
145 #if 0
146 if(vf->priv->exp_w<width) vf->priv->exp_w=width;
147 if(vf->priv->exp_h<height) vf->priv->exp_h=height;
148 #else
149 if ( vf->priv->exp_w == -1 ) vf->priv->exp_w=width;
150 else if (vf->priv->exp_w < -1 ) vf->priv->exp_w=width - vf->priv->exp_w;
151 else if ( vf->priv->exp_w<width ) vf->priv->exp_w=width;
152 if ( vf->priv->exp_h == -1 ) vf->priv->exp_h=height;
153 else if ( vf->priv->exp_h < -1 ) vf->priv->exp_h=height - vf->priv->exp_h;
154 else if( vf->priv->exp_h<height ) vf->priv->exp_h=height;
155 #endif
156 if (vf->priv->aspect) {
157 float adjusted_aspect = vf->priv->aspect;
158 adjusted_aspect *= ((double)width/height) / ((double)d_width/d_height);
159 if (vf->priv->exp_h < vf->priv->exp_w / adjusted_aspect) {
160 vf->priv->exp_h = vf->priv->exp_w / adjusted_aspect + 0.5;
161 } else {
162 vf->priv->exp_w = vf->priv->exp_h * adjusted_aspect + 0.5;
165 if (vf->priv->round > 1) { // round up.
166 vf->priv->exp_w = (1 + (vf->priv->exp_w - 1) / vf->priv->round) * vf->priv->round;
167 vf->priv->exp_h = (1 + (vf->priv->exp_h - 1) / vf->priv->round) * vf->priv->round;
170 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;
171 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;
173 if(!opts->screen_size_x && !opts->screen_size_y){
174 d_width=d_width*vf->priv->exp_w/width;
175 d_height=d_height*vf->priv->exp_h/height;
177 return vf_next_config(vf,vf->priv->exp_w,vf->priv->exp_h,d_width,d_height,flags,outfmt);
180 // there are 4 cases:
181 // codec --DR--> expand --DR--> vo
182 // codec --DR--> expand -copy-> vo
183 // codec -copy-> expand --DR--> vo
184 // codec -copy-> expand -copy-> vo (worst case)
186 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
187 // if(mpi->type==MP_IMGTYPE_IPB) return; // not yet working
188 #ifdef OSD_SUPPORT
189 if(vf->priv->osd_enabled && (mpi->flags&MP_IMGFLAG_PRESERVE)){
190 // check if we have to render osd!
191 osd_update(vf->priv->osd, vf->priv->exp_w, vf->priv->exp_h);
192 if(vo_osd_check_range_update(vf->priv->exp_x,vf->priv->exp_y,
193 vf->priv->exp_x+mpi->w,vf->priv->exp_y+mpi->h)) return;
195 #endif
196 if(vf->priv->exp_w==mpi->width ||
197 (mpi->flags&(MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_ACCEPT_WIDTH)) ){
198 // try full DR !
199 mpi->priv=vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
200 mpi->type, mpi->flags,
201 FFMAX(vf->priv->exp_w, mpi->width +vf->priv->exp_x),
202 FFMAX(vf->priv->exp_h, mpi->height+vf->priv->exp_y));
203 if((vf->dmpi->flags & MP_IMGFLAG_DRAW_CALLBACK) &&
204 !(vf->dmpi->flags & MP_IMGFLAG_DIRECT)){
205 mp_tmsg(MSGT_VFILTER, MSGL_INFO, "Full DR not possible, trying SLICES instead!\n");
206 return;
208 // set up mpi as a cropped-down image of dmpi:
209 if(mpi->flags&MP_IMGFLAG_PLANAR){
210 mpi->planes[0]=vf->dmpi->planes[0]+
211 vf->priv->exp_y*vf->dmpi->stride[0]+vf->priv->exp_x;
212 mpi->planes[1]=vf->dmpi->planes[1]+
213 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[1]+(vf->priv->exp_x>>mpi->chroma_x_shift);
214 mpi->planes[2]=vf->dmpi->planes[2]+
215 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[2]+(vf->priv->exp_x>>mpi->chroma_x_shift);
216 mpi->stride[1]=vf->dmpi->stride[1];
217 mpi->stride[2]=vf->dmpi->stride[2];
218 } else {
219 mpi->planes[0]=vf->dmpi->planes[0]+
220 vf->priv->exp_y*vf->dmpi->stride[0]+
221 vf->priv->exp_x*(vf->dmpi->bpp/8);
223 mpi->stride[0]=vf->dmpi->stride[0];
224 mpi->width=vf->dmpi->width;
225 mpi->flags|=MP_IMGFLAG_DIRECT;
226 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
227 // vf->dmpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
231 static void start_slice(struct vf_instance *vf, mp_image_t *mpi){
232 // printf("start_slice called! flag=%d\n",mpi->flags&MP_IMGFLAG_DRAW_CALLBACK);
233 if(!vf->next->draw_slice){
234 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
235 return;
237 // they want slices!!! allocate the buffer.
238 if(!mpi->priv)
239 mpi->priv=vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
240 // MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
241 MP_IMGTYPE_TEMP, mpi->flags,
242 FFMAX(vf->priv->exp_w, mpi->width +vf->priv->exp_x),
243 FFMAX(vf->priv->exp_h, mpi->height+vf->priv->exp_y));
244 if(!(vf->dmpi->flags&MP_IMGFLAG_DRAW_CALLBACK))
245 mp_tmsg(MSGT_VFILTER, MSGL_WARN, "WARNING! Next filter doesn't support SLICES, get ready for sig11...\n"); // shouldn't happen.
246 vf->priv->first_slice = 1;
249 static void draw_top_blackbar_slice(struct vf_instance *vf,
250 unsigned char** src, int* stride, int w,int h, int x, int y){
251 if(vf->priv->exp_y>0 && y == 0) {
252 vf_next_draw_slice(vf, vf->dmpi->planes, vf->dmpi->stride,
253 vf->dmpi->w,vf->priv->exp_y,0,0);
258 static void draw_bottom_blackbar_slice(struct vf_instance *vf,
259 unsigned char** src, int* stride, int w,int h, int x, int y){
260 if(vf->priv->exp_y+vf->h<vf->dmpi->h && y+h == vf->h) {
261 unsigned char *src2[MP_MAX_PLANES];
262 src2[0] = vf->dmpi->planes[0]
263 + (vf->priv->exp_y+vf->h)*vf->dmpi->stride[0];
264 if(vf->dmpi->flags&MP_IMGFLAG_PLANAR){
265 src2[1] = vf->dmpi->planes[1]
266 + ((vf->priv->exp_y+vf->h)>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1];
267 src2[2] = vf->dmpi->planes[2]
268 + ((vf->priv->exp_y+vf->h)>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2];
269 } else {
270 src2[1] = vf->dmpi->planes[1]; // passthrough rgb8 palette
272 vf_next_draw_slice(vf, src2, vf->dmpi->stride,
273 vf->dmpi->w,vf->dmpi->h-(vf->priv->exp_y+vf->h),
274 0,vf->priv->exp_y+vf->h);
278 static void draw_slice(struct vf_instance *vf,
279 unsigned char** src, int* stride, int w,int h, int x, int y){
280 // printf("draw_slice() called %d at %d\n",h,y);
282 if (y == 0 && y+h == vf->h) {
283 // special case - only one slice
284 draw_top_blackbar_slice(vf, src, stride, w, h, x, y);
285 vf_next_draw_slice(vf,src,stride,w,h,x+vf->priv->exp_x,y+vf->priv->exp_y);
286 draw_bottom_blackbar_slice(vf, src, stride, w, h, x, y);
287 return;
289 if (vf->priv->first_slice) {
290 draw_top_blackbar_slice(vf, src, stride, w, h, x, y);
291 draw_bottom_blackbar_slice(vf, src, stride, w, h, x, y);
293 vf_next_draw_slice(vf,src,stride,w,h,x+vf->priv->exp_x,y+vf->priv->exp_y);
294 if (!vf->priv->first_slice) {
295 draw_top_blackbar_slice(vf, src, stride, w, h, x, y);
296 draw_bottom_blackbar_slice(vf, src, stride, w, h, x, y);
298 vf->priv->first_slice = 0;
301 // w, h = width and height of the actual video frame (located at exp_x/exp_y)
302 static void clear_borders(struct vf_instance *vf, int w, int h)
304 // upper border (over the full width)
305 vf_mpi_clear(vf->dmpi, 0, 0, vf->priv->exp_w, vf->priv->exp_y);
306 // lower border
307 vf_mpi_clear(vf->dmpi, 0, vf->priv->exp_y + h, vf->priv->exp_w,
308 vf->priv->exp_h - (vf->priv->exp_y + h));
309 // left
310 vf_mpi_clear(vf->dmpi, 0, vf->priv->exp_y, vf->priv->exp_x, h);
311 // right
312 vf_mpi_clear(vf->dmpi, vf->priv->exp_x + w, vf->priv->exp_y,
313 vf->priv->exp_w - (vf->priv->exp_x + w), h);
316 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
317 if (vf->priv->passthrough) {
318 mp_image_t *dmpi = vf_get_image(vf->next, IMGFMT_MPEGPES,
319 MP_IMGTYPE_EXPORT, 0, mpi->w, mpi->h);
320 dmpi->planes[0]=mpi->planes[0];
321 return vf_next_put_image(vf,dmpi, pts);
324 if(mpi->flags&MP_IMGFLAG_DIRECT || mpi->flags&MP_IMGFLAG_DRAW_CALLBACK){
325 vf->dmpi=mpi->priv;
326 if(!vf->dmpi) { mp_tmsg(MSGT_VFILTER, MSGL_WARN, "Why do we get NULL??\n"); return 0; }
327 mpi->priv=NULL;
328 clear_borders(vf,mpi->w,mpi->h);
329 #ifdef OSD_SUPPORT
330 if(vf->priv->osd_enabled) draw_osd(vf,mpi->w,mpi->h);
331 #endif
332 // we've used DR, so we're ready...
333 if(!(mpi->flags&MP_IMGFLAG_PLANAR))
334 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
335 return vf_next_put_image(vf,vf->dmpi, pts);
338 // hope we'll get DR buffer:
339 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
340 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
341 vf->priv->exp_w, vf->priv->exp_h);
343 // copy mpi->dmpi...
344 if(mpi->flags&MP_IMGFLAG_PLANAR){
345 memcpy_pic(vf->dmpi->planes[0]+
346 vf->priv->exp_y*vf->dmpi->stride[0]+vf->priv->exp_x,
347 mpi->planes[0], mpi->w, mpi->h,
348 vf->dmpi->stride[0],mpi->stride[0]);
349 memcpy_pic(vf->dmpi->planes[1]+
350 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[1]+(vf->priv->exp_x>>mpi->chroma_x_shift),
351 mpi->planes[1], (mpi->w>>mpi->chroma_x_shift), (mpi->h>>mpi->chroma_y_shift),
352 vf->dmpi->stride[1],mpi->stride[1]);
353 memcpy_pic(vf->dmpi->planes[2]+
354 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[2]+(vf->priv->exp_x>>mpi->chroma_x_shift),
355 mpi->planes[2], (mpi->w>>mpi->chroma_x_shift), (mpi->h>>mpi->chroma_y_shift),
356 vf->dmpi->stride[2],mpi->stride[2]);
357 } else {
358 memcpy_pic(vf->dmpi->planes[0]+
359 vf->priv->exp_y*vf->dmpi->stride[0]+vf->priv->exp_x*(vf->dmpi->bpp/8),
360 mpi->planes[0], mpi->w*(vf->dmpi->bpp/8), mpi->h,
361 vf->dmpi->stride[0],mpi->stride[0]);
362 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
364 clear_borders(vf,mpi->w,mpi->h);
365 #ifdef OSD_SUPPORT
366 if(vf->priv->osd_enabled) draw_osd(vf,mpi->w,mpi->h);
367 #endif
368 return vf_next_put_image(vf,vf->dmpi, pts);
371 //===========================================================================//
373 static int control(struct vf_instance *vf, int request, void* data){
374 #ifdef OSD_SUPPORT
375 switch(request){
376 case VFCTRL_SET_OSD_OBJ:
377 vf->priv->osd = data;
378 break;
379 case VFCTRL_DRAW_OSD:
380 if(vf->priv->osd_enabled) return CONTROL_TRUE;
381 break;
383 #endif
384 return vf_next_control(vf,request,data);
387 static int query_format(struct vf_instance *vf, unsigned int fmt){
388 return vf_next_query_format(vf,fmt);
391 static int vf_open(vf_instance_t *vf, char *args){
392 vf->config=config;
393 vf->control=control;
394 vf->query_format=query_format;
395 vf->start_slice=start_slice;
396 vf->draw_slice=draw_slice;
397 vf->get_image=get_image;
398 vf->put_image=put_image;
399 mp_msg(MSGT_VFILTER, MSGL_INFO, "Expand: %d x %d, %d ; %d, osd: %d, aspect: %f, round: %d\n",
400 vf->priv->cfg_exp_w,
401 vf->priv->cfg_exp_h,
402 vf->priv->cfg_exp_x,
403 vf->priv->cfg_exp_y,
404 vf->priv->osd_enabled,
405 vf->priv->aspect,
406 vf->priv->round);
407 if (vf->priv->osd_enabled)
408 vf->default_caps = VFCAP_OSD_FILTER;
409 return 1;
412 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
413 static m_option_t vf_opts_fields[] = {
414 {"w", ST_OFF(cfg_exp_w), CONF_TYPE_INT, 0, 0 ,0, NULL},
415 {"h", ST_OFF(cfg_exp_h), CONF_TYPE_INT, 0, 0 ,0, NULL},
416 {"x", ST_OFF(cfg_exp_x), CONF_TYPE_INT, M_OPT_MIN, -1, 0, NULL},
417 {"y", ST_OFF(cfg_exp_y), CONF_TYPE_INT, M_OPT_MIN, -1, 0, NULL},
418 {"osd", ST_OFF(osd_enabled), CONF_TYPE_FLAG, 0 , 0, 1, NULL},
419 {"aspect", ST_OFF(aspect), CONF_TYPE_DOUBLE, M_OPT_MIN, 0, 0, NULL},
420 {"round", ST_OFF(round), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL},
421 { NULL, NULL, 0, 0, 0, 0, NULL }
424 static const m_struct_t vf_opts = {
425 "expand",
426 sizeof(struct vf_priv_s),
427 &vf_priv_dflt,
428 vf_opts_fields
433 const vf_info_t vf_info_expand = {
434 #ifdef OSD_SUPPORT
435 "expanding & osd",
436 #else
437 "expanding",
438 #endif
439 "expand",
440 "A'rpi",
442 vf_open,
443 &vf_opts
446 //===========================================================================//