core: Set mpctx->chapters to NULL at uninit
[mplayer.git] / libmpcodecs / vf_expand.c
blobdec0c524b6704910a69ef9aa27045e1d15e26572
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"
19 #ifdef OSD_SUPPORT
20 #include "libvo/sub.h"
21 #include "libvo/osd.h"
22 #endif
24 #include "m_option.h"
25 #include "m_struct.h"
27 #define MAX(a,b) ((a) > (b) ? (a) : (b))
29 static struct vf_priv_s {
30 int exp_w,exp_h;
31 int exp_x,exp_y;
32 int osd_enabled;
33 double aspect;
34 int round;
35 unsigned char* fb_ptr;
36 int passthrough;
37 int first_slice;
38 struct osd_state *osd;
39 } const vf_priv_dflt = {
40 -1,-1,
41 -1,-1,
43 0.,
45 NULL,
50 //===========================================================================//
51 #ifdef OSD_SUPPORT
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:
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(void *ctx, 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* 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 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...
183 if(vf->dmpi)
184 vf->priv->fb_ptr=vf->dmpi->planes[0];
187 #endif
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:
201 #if 0
202 if(vf->priv->exp_w<width) vf->priv->exp_w=width;
203 if(vf->priv->exp_h<height) vf->priv->exp_h=height;
204 #else
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;
211 #endif
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;
217 } else {
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
245 #ifdef OSD_SUPPORT
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;
252 #endif
253 if(vf->priv->exp_w==mpi->width ||
254 (mpi->flags&(MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_ACCEPT_WIDTH)) ){
255 // try full DR !
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));
260 #if 1
261 if((vf->dmpi->flags & MP_IMGFLAG_DRAW_CALLBACK) &&
262 !(vf->dmpi->flags & MP_IMGFLAG_DIRECT)){
263 mp_msg(MSGT_VFILTER, MSGL_INFO, MSGTR_MPCODECS_FullDRNotPossible);
264 return;
266 #endif
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];
277 } else {
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;
294 return;
296 // they want slices!!! allocate the buffer.
297 if(!mpi->priv)
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_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_WarnNextFilterDoesntSupportSlices); // 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];
328 } else {
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);
346 return;
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){
369 vf->dmpi=mpi->priv;
370 if(!vf->dmpi) { mp_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_FunWhydowegetNULL); return 0; }
371 mpi->priv=NULL;
372 #ifdef OSD_SUPPORT
373 if(vf->priv->osd_enabled) draw_osd(vf,mpi->w,mpi->h);
374 #endif
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);
386 // copy mpi->dmpi...
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]);
400 } else {
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
407 #ifdef OSD_SUPPORT
408 if(vf->priv->osd_enabled) draw_osd(vf,mpi->w,mpi->h);
409 #endif
410 return vf_next_put_image(vf,vf->dmpi, pts);
413 //===========================================================================//
415 static int control(struct vf_instance* vf, int request, void* data){
416 #ifdef OSD_SUPPORT
417 switch(request){
418 case VFCTRL_SET_OSD_OBJ:
419 vf->priv->osd = data;
420 break;
421 case VFCTRL_DRAW_OSD:
422 if(vf->priv->osd_enabled) return CONTROL_TRUE;
423 break;
424 case VFCTRL_REDRAW_OSD:
425 if (vf->priv->osd_enabled)
426 return false;
427 break;
429 #endif
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){
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: %lf, round: %d\n",
446 vf->priv->exp_w,
447 vf->priv->exp_h,
448 vf->priv->exp_x,
449 vf->priv->exp_y,
450 vf->priv->osd_enabled,
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 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 = {
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 //===========================================================================//