Remove HAVE_MATRIXVIEW references
[mplayer/kovensky.git] / libmpcodecs / vf_expand.c
blob5d2bf06f963cc06c69c51bce784d38bab6c3d651
1 #define OSD_SUPPORT
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <stdbool.h>
8 #include "config.h"
9 #include "mp_msg.h"
10 #include "help_mp.h"
11 #include "options.h"
13 #include "img_format.h"
14 #include "mp_image.h"
15 #include "vf.h"
17 #include "libvo/fastmemcpy.h"
18 #include "libavutil/avutil.h"
20 #ifdef OSD_SUPPORT
21 #include "libvo/sub.h"
22 #include "libvo/osd.h"
23 #endif
25 #include "m_option.h"
26 #include "m_struct.h"
28 static struct vf_priv_s {
29 // These four values are a backup of the values parsed from the command line.
30 // This is necessary so that we do not get a mess upon filter reinit due to
31 // e.g. aspect changes and with only aspect specified on the command line,
32 // where we would otherwise use the values calculated for a different aspect
33 // instead of recalculating them again.
34 int cfg_exp_w, cfg_exp_h;
35 int cfg_exp_x, cfg_exp_y;
36 int exp_w,exp_h;
37 int exp_x,exp_y;
38 int osd_enabled;
39 double aspect;
40 int round;
41 unsigned char* fb_ptr;
42 int passthrough;
43 int first_slice;
44 struct osd_state *osd;
45 } const vf_priv_dflt = {
46 -1,-1,
47 -1,-1,
48 -1,-1,
49 -1,-1,
51 0.,
53 NULL,
58 //===========================================================================//
59 #ifdef OSD_SUPPORT
61 static struct vf_instance* vf=NULL; // fixme (needs sub.c changes)
62 static int orig_w,orig_h;
64 static void remove_func_2(int x0,int y0, int w,int h){
65 // TODO: let's cleanup the place
66 //printf("OSD clear: %d;%d %dx%d \n",x0,y0,w,h);
67 vf_mpi_clear(vf->dmpi,x0,y0,w,h);
70 static void remove_func(int x0,int y0, int w,int h){
71 if(!vo_osd_changed_flag) return;
72 // split it to 4 parts:
73 if(y0<vf->priv->exp_y){
74 // it has parts above the image:
75 int y=y0+h;
76 if(y>vf->priv->exp_y) y=vf->priv->exp_y;
77 remove_func_2(x0,y0,w,y-y0);
78 if(y0+h<=vf->priv->exp_y) return;
79 h-=y-y0;y0=y;
81 if(y0+h>vf->priv->exp_y+orig_h){
82 // it has parts under the image:
83 int y=y0;
84 if(y<vf->priv->exp_y+orig_h) y=vf->priv->exp_y+orig_h;
85 remove_func_2(x0,y,w,y0+h-y);
86 if(y0>=vf->priv->exp_y+orig_h) return;
87 h=y-y0;
89 if(x0<vf->priv->exp_x){
90 // it has parts on the left side of the image:
91 int x=x0+w;
92 if(x>vf->priv->exp_x) x=vf->priv->exp_x;
93 remove_func_2(x0,y0,x-x0,h);
94 if(x0+w<=vf->priv->exp_x) return;
95 w-=x-x0;x0=x;
97 if(x0+w>vf->priv->exp_x+orig_w){
98 // it has parts on the right side of the image:
99 int x=x0;
100 if(x<vf->priv->exp_x+orig_w) x=vf->priv->exp_x+orig_w;
101 remove_func_2(x,y0,x0+w-x,h);
102 if(x0>=vf->priv->exp_x+orig_w) return;
103 w=x-x0;
107 static void draw_func(void *ctx, int x0,int y0, int w,int h,unsigned char* src, unsigned char *srca, int stride){
108 unsigned char* dst;
109 if(!vo_osd_changed_flag && vf->dmpi->planes[0]==vf->priv->fb_ptr){
110 // ok, enough to update the area inside the video, leave the black bands
111 // untouched!
112 if(x0<vf->priv->exp_x){
113 int tmp=vf->priv->exp_x-x0;
114 w-=tmp; src+=tmp; srca+=tmp; x0+=tmp;
116 if(y0<vf->priv->exp_y){
117 int tmp=vf->priv->exp_y-y0;
118 h-=tmp; src+=tmp*stride; srca+=tmp*stride; y0+=tmp;
120 if(x0+w>vf->priv->exp_x+orig_w){
121 w=vf->priv->exp_x+orig_w-x0;
123 if(y0+h>vf->priv->exp_y+orig_h){
124 h=vf->priv->exp_y+orig_h-y0;
127 if(w<=0 || h<=0) return; // nothing to do...
128 // printf("OSD redraw: %d;%d %dx%d \n",x0,y0,w,h);
129 dst=vf->dmpi->planes[0]+
130 vf->dmpi->stride[0]*y0+
131 (vf->dmpi->bpp>>3)*x0;
132 switch(vf->dmpi->imgfmt){
133 case IMGFMT_BGR15:
134 case IMGFMT_RGB15:
135 vo_draw_alpha_rgb15(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
136 break;
137 case IMGFMT_BGR16:
138 case IMGFMT_RGB16:
139 vo_draw_alpha_rgb16(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
140 break;
141 case IMGFMT_BGR24:
142 case IMGFMT_RGB24:
143 vo_draw_alpha_rgb24(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
144 break;
145 case IMGFMT_BGR32:
146 case IMGFMT_RGB32:
147 vo_draw_alpha_rgb32(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
148 break;
149 case IMGFMT_YV12:
150 case IMGFMT_I420:
151 case IMGFMT_IYUV:
152 case IMGFMT_YVU9:
153 case IMGFMT_IF09:
154 case IMGFMT_Y800:
155 case IMGFMT_Y8:
156 vo_draw_alpha_yv12(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
157 break;
158 case IMGFMT_YUY2:
159 vo_draw_alpha_yuy2(w,h,src,srca,stride,dst,vf->dmpi->stride[0]);
160 break;
161 case IMGFMT_UYVY:
162 vo_draw_alpha_yuy2(w,h,src,srca,stride,dst+1,vf->dmpi->stride[0]);
163 break;
167 static void draw_osd(struct vf_instance* vf_,int w,int h){
168 vf=vf_;orig_w=w;orig_h=h;
169 // printf("======================================\n");
170 if(vf->priv->exp_w!=w || vf->priv->exp_h!=h ||
171 vf->priv->exp_x || vf->priv->exp_y){
172 // yep, we're expanding image, not just copy.
173 if(vf->dmpi->planes[0]!=vf->priv->fb_ptr){
174 // double buffering, so we need full clear :(
175 if (vf->priv->exp_y > 0)
176 remove_func_2(0,0,vf->priv->exp_w,vf->priv->exp_y);
177 if (vf->priv->exp_y+h < vf->priv->exp_h)
178 remove_func_2(0,vf->priv->exp_y+h,vf->priv->exp_w,vf->priv->exp_h-h-vf->priv->exp_y);
179 if (vf->priv->exp_x > 0)
180 remove_func_2(0,vf->priv->exp_y,vf->priv->exp_x,h);
181 if (vf->priv->exp_x+w < vf->priv->exp_w)
182 remove_func_2(vf->priv->exp_x+w,vf->priv->exp_y,vf->priv->exp_w-w-vf->priv->exp_x,h);
183 } else {
184 // partial clear:
185 osd_remove_text(vf->priv->osd, vf->priv->exp_w,vf->priv->exp_h,remove_func);
188 osd_draw_text(vf->priv->osd, vf->priv->exp_w,vf->priv->exp_h,draw_func, NULL);
189 // save buffer pointer for double buffering detection - yes, i know it's
190 // ugly method, but note that codecs with DR support does the same...
191 if(vf->dmpi)
192 vf->priv->fb_ptr=vf->dmpi->planes[0];
195 #endif
196 //===========================================================================//
198 static int config(struct vf_instance* vf,
199 int width, int height, int d_width, int d_height,
200 unsigned int flags, unsigned int outfmt)
202 struct MPOpts *opts = vf->opts;
203 if(outfmt == IMGFMT_MPEGPES) {
204 vf->priv->passthrough = 1;
205 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
207 if (outfmt == IMGFMT_IF09) return 0;
208 vf->priv->exp_x = vf->priv->cfg_exp_x;
209 vf->priv->exp_y = vf->priv->cfg_exp_y;
210 vf->priv->exp_w = vf->priv->cfg_exp_w;
211 vf->priv->exp_h = vf->priv->cfg_exp_h;
212 // calculate the missing parameters:
213 #if 0
214 if(vf->priv->exp_w<width) vf->priv->exp_w=width;
215 if(vf->priv->exp_h<height) vf->priv->exp_h=height;
216 #else
217 if ( vf->priv->exp_w == -1 ) vf->priv->exp_w=width;
218 else if (vf->priv->exp_w < -1 ) vf->priv->exp_w=width - vf->priv->exp_w;
219 else if ( vf->priv->exp_w<width ) vf->priv->exp_w=width;
220 if ( vf->priv->exp_h == -1 ) vf->priv->exp_h=height;
221 else if ( vf->priv->exp_h < -1 ) vf->priv->exp_h=height - vf->priv->exp_h;
222 else if( vf->priv->exp_h<height ) vf->priv->exp_h=height;
223 #endif
224 if (vf->priv->aspect) {
225 float adjusted_aspect = vf->priv->aspect;
226 adjusted_aspect *= ((double)width/height) / ((double)d_width/d_height);
227 if (vf->priv->exp_h < vf->priv->exp_w / adjusted_aspect) {
228 vf->priv->exp_h = vf->priv->exp_w / adjusted_aspect + 0.5;
229 } else {
230 vf->priv->exp_w = vf->priv->exp_h * adjusted_aspect + 0.5;
233 if (vf->priv->round > 1) { // round up.
234 vf->priv->exp_w = (1 + (vf->priv->exp_w - 1) / vf->priv->round) * vf->priv->round;
235 vf->priv->exp_h = (1 + (vf->priv->exp_h - 1) / vf->priv->round) * vf->priv->round;
238 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;
239 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;
240 vf->priv->fb_ptr=NULL;
242 if(!opts->screen_size_x && !opts->screen_size_y){
243 d_width=d_width*vf->priv->exp_w/width;
244 d_height=d_height*vf->priv->exp_h/height;
246 return vf_next_config(vf,vf->priv->exp_w,vf->priv->exp_h,d_width,d_height,flags,outfmt);
249 // there are 4 cases:
250 // codec --DR--> expand --DR--> vo
251 // codec --DR--> expand -copy-> vo
252 // codec -copy-> expand --DR--> vo
253 // codec -copy-> expand -copy-> vo (worst case)
255 static void get_image(struct vf_instance* vf, mp_image_t *mpi){
256 // if(mpi->type==MP_IMGTYPE_IPB) return; // not yet working
257 #ifdef OSD_SUPPORT
258 if(vf->priv->osd_enabled && (mpi->flags&MP_IMGFLAG_PRESERVE)){
259 // check if we have to render osd!
260 osd_update(vf->priv->osd, vf->priv->exp_w, vf->priv->exp_h);
261 if(vo_osd_check_range_update(vf->priv->exp_x,vf->priv->exp_y,
262 vf->priv->exp_x+mpi->w,vf->priv->exp_y+mpi->h)) return;
264 #endif
265 if(vf->priv->exp_w==mpi->width ||
266 (mpi->flags&(MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_ACCEPT_WIDTH)) ){
267 // try full DR !
268 mpi->priv=vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
269 mpi->type, mpi->flags,
270 FFMAX(vf->priv->exp_w, mpi->width +vf->priv->exp_x),
271 FFMAX(vf->priv->exp_h, mpi->height+vf->priv->exp_y));
272 #if 1
273 if((vf->dmpi->flags & MP_IMGFLAG_DRAW_CALLBACK) &&
274 !(vf->dmpi->flags & MP_IMGFLAG_DIRECT)){
275 mp_tmsg(MSGT_VFILTER, MSGL_INFO, "Full DR not possible, trying SLICES instead!\n");
276 return;
278 #endif
279 // set up mpi as a cropped-down image of dmpi:
280 if(mpi->flags&MP_IMGFLAG_PLANAR){
281 mpi->planes[0]=vf->dmpi->planes[0]+
282 vf->priv->exp_y*vf->dmpi->stride[0]+vf->priv->exp_x;
283 mpi->planes[1]=vf->dmpi->planes[1]+
284 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[1]+(vf->priv->exp_x>>mpi->chroma_x_shift);
285 mpi->planes[2]=vf->dmpi->planes[2]+
286 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[2]+(vf->priv->exp_x>>mpi->chroma_x_shift);
287 mpi->stride[1]=vf->dmpi->stride[1];
288 mpi->stride[2]=vf->dmpi->stride[2];
289 } else {
290 mpi->planes[0]=vf->dmpi->planes[0]+
291 vf->priv->exp_y*vf->dmpi->stride[0]+
292 vf->priv->exp_x*(vf->dmpi->bpp/8);
294 mpi->stride[0]=vf->dmpi->stride[0];
295 mpi->width=vf->dmpi->width;
296 mpi->flags|=MP_IMGFLAG_DIRECT;
297 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
298 // vf->dmpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
302 static void start_slice(struct vf_instance* vf, mp_image_t *mpi){
303 // printf("start_slice called! flag=%d\n",mpi->flags&MP_IMGFLAG_DRAW_CALLBACK);
304 if(!vf->next->draw_slice){
305 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
306 return;
308 // they want slices!!! allocate the buffer.
309 if(!mpi->priv)
310 mpi->priv=vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
311 // MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
312 MP_IMGTYPE_TEMP, mpi->flags,
313 FFMAX(vf->priv->exp_w, mpi->width +vf->priv->exp_x),
314 FFMAX(vf->priv->exp_h, mpi->height+vf->priv->exp_y));
315 if(!(vf->dmpi->flags&MP_IMGFLAG_DRAW_CALLBACK))
316 mp_tmsg(MSGT_VFILTER, MSGL_WARN, "WARNING! Next filter doesn't support SLICES, get ready for sig11...\n"); // shouldn't happen.
317 vf->priv->first_slice = 1;
320 static void draw_top_blackbar_slice(struct vf_instance* vf,
321 unsigned char** src, int* stride, int w,int h, int x, int y){
322 if(vf->priv->exp_y>0 && y == 0) {
323 vf_next_draw_slice(vf, vf->dmpi->planes, vf->dmpi->stride,
324 vf->dmpi->w,vf->priv->exp_y,0,0);
329 static void draw_bottom_blackbar_slice(struct vf_instance* vf,
330 unsigned char** src, int* stride, int w,int h, int x, int y){
331 if(vf->priv->exp_y+vf->h<vf->dmpi->h && y+h == vf->h) {
332 unsigned char *src2[MP_MAX_PLANES];
333 src2[0] = vf->dmpi->planes[0]
334 + (vf->priv->exp_y+vf->h)*vf->dmpi->stride[0];
335 if(vf->dmpi->flags&MP_IMGFLAG_PLANAR){
336 src2[1] = vf->dmpi->planes[1]
337 + ((vf->priv->exp_y+vf->h)>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1];
338 src2[2] = vf->dmpi->planes[2]
339 + ((vf->priv->exp_y+vf->h)>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2];
340 } else {
341 src2[1] = vf->dmpi->planes[1]; // passthrough rgb8 palette
343 vf_next_draw_slice(vf, src2, vf->dmpi->stride,
344 vf->dmpi->w,vf->dmpi->h-(vf->priv->exp_y+vf->h),
345 0,vf->priv->exp_y+vf->h);
349 static void draw_slice(struct vf_instance* vf,
350 unsigned char** src, int* stride, int w,int h, int x, int y){
351 // printf("draw_slice() called %d at %d\n",h,y);
353 if (y == 0 && y+h == vf->h) {
354 // special case - only one slice
355 draw_top_blackbar_slice(vf, src, stride, w, h, x, y);
356 vf_next_draw_slice(vf,src,stride,w,h,x+vf->priv->exp_x,y+vf->priv->exp_y);
357 draw_bottom_blackbar_slice(vf, src, stride, w, h, x, y);
358 return;
360 if (vf->priv->first_slice) {
361 draw_top_blackbar_slice(vf, src, stride, w, h, x, y);
362 draw_bottom_blackbar_slice(vf, src, stride, w, h, x, y);
364 vf_next_draw_slice(vf,src,stride,w,h,x+vf->priv->exp_x,y+vf->priv->exp_y);
365 if (!vf->priv->first_slice) {
366 draw_top_blackbar_slice(vf, src, stride, w, h, x, y);
367 draw_bottom_blackbar_slice(vf, src, stride, w, h, x, y);
369 vf->priv->first_slice = 0;
372 static int put_image(struct vf_instance* vf, mp_image_t *mpi, double pts){
373 if (vf->priv->passthrough) {
374 mp_image_t *dmpi = vf_get_image(vf->next, IMGFMT_MPEGPES,
375 MP_IMGTYPE_EXPORT, 0, mpi->w, mpi->h);
376 dmpi->planes[0]=mpi->planes[0];
377 return vf_next_put_image(vf,dmpi, pts);
380 if(mpi->flags&MP_IMGFLAG_DIRECT || mpi->flags&MP_IMGFLAG_DRAW_CALLBACK){
381 vf->dmpi=mpi->priv;
382 if(!vf->dmpi) { mp_tmsg(MSGT_VFILTER, MSGL_WARN, "Why do we get NULL??\n"); return 0; }
383 mpi->priv=NULL;
384 #ifdef OSD_SUPPORT
385 if(vf->priv->osd_enabled) draw_osd(vf,mpi->w,mpi->h);
386 #endif
387 // we've used DR, so we're ready...
388 if(!(mpi->flags&MP_IMGFLAG_PLANAR))
389 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
390 return vf_next_put_image(vf,vf->dmpi, pts);
393 // hope we'll get DR buffer:
394 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
395 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
396 vf->priv->exp_w, vf->priv->exp_h);
398 // copy mpi->dmpi...
399 if(mpi->flags&MP_IMGFLAG_PLANAR){
400 memcpy_pic(vf->dmpi->planes[0]+
401 vf->priv->exp_y*vf->dmpi->stride[0]+vf->priv->exp_x,
402 mpi->planes[0], mpi->w, mpi->h,
403 vf->dmpi->stride[0],mpi->stride[0]);
404 memcpy_pic(vf->dmpi->planes[1]+
405 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[1]+(vf->priv->exp_x>>mpi->chroma_x_shift),
406 mpi->planes[1], (mpi->w>>mpi->chroma_x_shift), (mpi->h>>mpi->chroma_y_shift),
407 vf->dmpi->stride[1],mpi->stride[1]);
408 memcpy_pic(vf->dmpi->planes[2]+
409 (vf->priv->exp_y>>mpi->chroma_y_shift)*vf->dmpi->stride[2]+(vf->priv->exp_x>>mpi->chroma_x_shift),
410 mpi->planes[2], (mpi->w>>mpi->chroma_x_shift), (mpi->h>>mpi->chroma_y_shift),
411 vf->dmpi->stride[2],mpi->stride[2]);
412 } else {
413 memcpy_pic(vf->dmpi->planes[0]+
414 vf->priv->exp_y*vf->dmpi->stride[0]+vf->priv->exp_x*(vf->dmpi->bpp/8),
415 mpi->planes[0], mpi->w*(vf->dmpi->bpp/8), mpi->h,
416 vf->dmpi->stride[0],mpi->stride[0]);
417 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
419 #ifdef OSD_SUPPORT
420 if(vf->priv->osd_enabled) draw_osd(vf,mpi->w,mpi->h);
421 #endif
422 return vf_next_put_image(vf,vf->dmpi, pts);
425 //===========================================================================//
427 static int control(struct vf_instance* vf, int request, void* data){
428 #ifdef OSD_SUPPORT
429 switch(request){
430 case VFCTRL_SET_OSD_OBJ:
431 vf->priv->osd = data;
432 break;
433 case VFCTRL_DRAW_OSD:
434 if(vf->priv->osd_enabled) return CONTROL_TRUE;
435 break;
436 case VFCTRL_REDRAW_OSD:
437 if (vf->priv->osd_enabled)
438 return false;
439 break;
441 #endif
442 return vf_next_control(vf,request,data);
445 static int query_format(struct vf_instance* vf, unsigned int fmt){
446 return vf_next_query_format(vf,fmt);
449 static int open(vf_instance_t *vf, char* args){
450 vf->config=config;
451 vf->control=control;
452 vf->query_format=query_format;
453 vf->start_slice=start_slice;
454 vf->draw_slice=draw_slice;
455 vf->get_image=get_image;
456 vf->put_image=put_image;
457 mp_msg(MSGT_VFILTER, MSGL_INFO, "Expand: %d x %d, %d ; %d, osd: %d, aspect: %f, round: %d\n",
458 vf->priv->cfg_exp_w,
459 vf->priv->cfg_exp_h,
460 vf->priv->cfg_exp_x,
461 vf->priv->cfg_exp_y,
462 vf->priv->osd_enabled,
463 vf->priv->aspect,
464 vf->priv->round);
465 return 1;
468 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
469 static m_option_t vf_opts_fields[] = {
470 {"w", ST_OFF(cfg_exp_w), CONF_TYPE_INT, 0, 0 ,0, NULL},
471 {"h", ST_OFF(cfg_exp_h), CONF_TYPE_INT, 0, 0 ,0, NULL},
472 {"x", ST_OFF(cfg_exp_x), CONF_TYPE_INT, M_OPT_MIN, -1, 0, NULL},
473 {"y", ST_OFF(cfg_exp_y), CONF_TYPE_INT, M_OPT_MIN, -1, 0, NULL},
474 {"osd", ST_OFF(osd_enabled), CONF_TYPE_FLAG, 0 , 0, 1, NULL},
475 {"aspect", ST_OFF(aspect), CONF_TYPE_DOUBLE, M_OPT_MIN, 0, 0, NULL},
476 {"round", ST_OFF(round), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL},
477 { NULL, NULL, 0, 0, 0, 0, NULL }
480 static const m_struct_t vf_opts = {
481 "expand",
482 sizeof(struct vf_priv_s),
483 &vf_priv_dflt,
484 vf_opts_fields
489 const vf_info_t vf_info_expand = {
490 #ifdef OSD_SUPPORT
491 "expanding & osd",
492 #else
493 "expanding",
494 #endif
495 "expand",
496 "A'rpi",
498 open,
499 &vf_opts
502 //===========================================================================//