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.
30 #include "img_format.h"
34 #include "libvo/fastmemcpy.h"
35 #include "libavutil/avutil.h"
39 #include "libvo/osd.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
;
60 struct osd_state
*osd
;
61 } const vf_priv_dflt
= {
73 //===========================================================================//
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
;
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
){
87 vo_draw_alpha_rgb12(w
, h
, src
, srca
, stride
, dst
, vf
->dmpi
->stride
[0]);
91 vo_draw_alpha_rgb15(w
,h
,src
,srca
,stride
,dst
,vf
->dmpi
->stride
[0]);
95 vo_draw_alpha_rgb16(w
,h
,src
,srca
,stride
,dst
,vf
->dmpi
->stride
[0]);
99 vo_draw_alpha_rgb24(w
,h
,src
,srca
,stride
,dst
,vf
->dmpi
->stride
[0]);
103 vo_draw_alpha_rgb32(w
,h
,src
,srca
,stride
,dst
,vf
->dmpi
->stride
[0]);
112 vo_draw_alpha_yv12(w
,h
,src
,srca
,stride
,dst
,vf
->dmpi
->stride
[0]);
115 vo_draw_alpha_yuy2(w
,h
,src
,srca
,stride
,dst
,vf
->dmpi
->stride
[0]);
118 vo_draw_alpha_yuy2(w
,h
,src
,srca
,stride
,dst
+1,vf
->dmpi
->stride
[0]);
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
);
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:
146 if(vf
->priv
->exp_w
<width
) vf
->priv
->exp_w
=width
;
147 if(vf
->priv
->exp_h
<height
) vf
->priv
->exp_h
=height
;
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
;
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;
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
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;
196 if(vf
->priv
->exp_w
==mpi
->width
||
197 (mpi
->flags
&(MP_IMGFLAG_ACCEPT_STRIDE
|MP_IMGFLAG_ACCEPT_WIDTH
)) ){
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");
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];
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
;
237 // they want slices!!! allocate the buffer.
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];
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
);
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
);
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
));
310 vf_mpi_clear(vf
->dmpi
, 0, vf
->priv
->exp_y
, vf
->priv
->exp_x
, h
);
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
){
326 if(!vf
->dmpi
) { mp_tmsg(MSGT_VFILTER
, MSGL_WARN
, "Why do we get NULL??\n"); return 0; }
328 clear_borders(vf
,mpi
->w
,mpi
->h
);
330 if(vf
->priv
->osd_enabled
) draw_osd(vf
,mpi
->w
,mpi
->h
);
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
);
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]);
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
);
366 if(vf
->priv
->osd_enabled
) draw_osd(vf
,mpi
->w
,mpi
->h
);
368 return vf_next_put_image(vf
,vf
->dmpi
, pts
);
371 //===========================================================================//
373 static int control(struct vf_instance
*vf
, int request
, void* data
){
376 case VFCTRL_SET_OSD_OBJ
:
377 vf
->priv
->osd
= data
;
379 case VFCTRL_DRAW_OSD
:
380 if(vf
->priv
->osd_enabled
) return CONTROL_TRUE
;
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
){
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",
404 vf
->priv
->osd_enabled
,
407 if (vf
->priv
->osd_enabled
)
408 vf
->default_caps
= VFCAP_OSD_FILTER
;
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
= {
426 sizeof(struct vf_priv_s
),
433 const vf_info_t vf_info_expand
= {
446 //===========================================================================//