10 #include "img_format.h"
14 #include "libvo/fastmemcpy.h"
15 #include "postproc/swscale.h"
21 static struct vf_priv_s
{
26 struct SwsContext
*ctx
;
27 struct SwsContext
*ctx2
; //for interlaced slices only
28 unsigned char* palette
;
31 int query_format_cache
[64];
35 {SWS_PARAM_DEFAULT
, SWS_PARAM_DEFAULT
},
42 extern int opt_screen_size_x
;
43 extern int opt_screen_size_y
;
44 extern float screen_size_xy
;
46 //===========================================================================//
48 void sws_getFlagsAndFilterFromCmdLine(int *flags
, SwsFilter
**srcFilterParam
, SwsFilter
**dstFilterParam
);
50 static unsigned int outfmt_list
[]={
64 // RGB and grayscale (Y8 and Y800):
86 static unsigned int find_best_out(vf_instance_t
*vf
){
90 // find the best outfmt:
91 for(i
=0; i
<sizeof(outfmt_list
)/sizeof(int)-1; i
++){
92 const int format
= outfmt_list
[i
];
93 int ret
= vf
->priv
->query_format_cache
[i
]-1;
95 ret
= vf_next_query_format(vf
, outfmt_list
[i
]);
96 vf
->priv
->query_format_cache
[i
]= ret
+1;
99 mp_msg(MSGT_VFILTER
,MSGL_DBG2
,"scale: query(%s) -> %d\n",vo_format_name(format
),ret
&3);
100 if(ret
&VFCAP_CSP_SUPPORTED_BY_HW
){
101 best
=format
; // no conversion -> bingo!
104 if(ret
&VFCAP_CSP_SUPPORTED
&& !best
)
105 best
=format
; // best with conversion
110 static int config(struct vf_instance_s
* vf
,
111 int width
, int height
, int d_width
, int d_height
,
112 unsigned int flags
, unsigned int outfmt
){
113 unsigned int best
=find_best_out(vf
);
116 int round_w
=0, round_h
=0;
117 SwsFilter
*srcFilter
, *dstFilter
;
120 mp_msg(MSGT_VFILTER
,MSGL_WARN
,"SwScale: no supported outfmt found :(\n");
124 vo_flags
=vf
->next
->query_format(vf
->next
,best
);
126 // scaling to dwidth*d_height, if all these TRUE:
128 // - no other sw/hw up/down scaling avail.
129 // - we're after postproc
130 // - user didn't set w:h
131 if(!(vo_flags
&VFCAP_POSTPROC
) && (flags
&4) &&
132 vf
->priv
->w
<0 && vf
->priv
->h
<0){ // -zoom
133 int x
=(vo_flags
&VFCAP_SWSCALE
) ? 0 : 1;
134 if(d_width
<width
|| d_height
<height
){
136 if(vo_flags
&VFCAP_HWSCALE_DOWN
) x
=0;
139 if(vo_flags
&VFCAP_HWSCALE_UP
) x
=0;
142 // user wants sw scaling! (-zoom)
144 vf
->priv
->h
=d_height
;
149 if((vf
->priv
->w
> width
) + (vf
->priv
->h
> height
) >= vf
->priv
->noup
){
155 if (vf
->priv
->w
<= -8) {
159 if (vf
->priv
->h
<= -8) {
164 if (vf
->priv
->w
< -3 || vf
->priv
->h
< -3 ||
165 (vf
->priv
->w
< -1 && vf
->priv
->h
< -1)) {
166 // TODO: establish a direct connection to the user's brain
167 // and find out what the heck he thinks MPlayer should do
168 // with this nonsense.
169 mp_msg(MSGT_VFILTER
, MSGL_ERR
, "SwScale: EUSERBROKEN Check your parameters, they make no sense!\n");
173 if (vf
->priv
->w
== -1)
175 if (vf
->priv
->w
== 0)
176 vf
->priv
->w
= d_width
;
178 if (vf
->priv
->h
== -1)
179 vf
->priv
->h
= height
;
180 if (vf
->priv
->h
== 0)
181 vf
->priv
->h
= d_height
;
183 if (vf
->priv
->w
== -3)
184 vf
->priv
->w
= vf
->priv
->h
* width
/ height
;
185 if (vf
->priv
->w
== -2)
186 vf
->priv
->w
= vf
->priv
->h
* d_width
/ d_height
;
188 if (vf
->priv
->h
== -3)
189 vf
->priv
->h
= vf
->priv
->w
* height
/ width
;
190 if (vf
->priv
->h
== -2)
191 vf
->priv
->h
= vf
->priv
->w
* d_height
/ d_width
;
194 vf
->priv
->w
= ((vf
->priv
->w
+ 8) / 16) * 16;
196 vf
->priv
->h
= ((vf
->priv
->h
+ 8) / 16) * 16;
198 // calculate the missing parameters:
200 case IMGFMT_YV12
: /* YV12 needs w & h rounded to 2 */
205 vf
->priv
->h
= (vf
->priv
->h
+ 1) & ~1;
206 case IMGFMT_YUY2
: /* YUY2 needs w rounded to 2 */
208 vf
->priv
->w
= (vf
->priv
->w
+ 1) & ~1;
211 mp_msg(MSGT_VFILTER
,MSGL_DBG2
,"SwScale: scaling %dx%d %s to %dx%d %s \n",
212 width
,height
,vo_format_name(outfmt
),
213 vf
->priv
->w
,vf
->priv
->h
,vo_format_name(best
));
216 if(vf
->priv
->ctx
) sws_freeContext(vf
->priv
->ctx
);
217 if(vf
->priv
->ctx2
)sws_freeContext(vf
->priv
->ctx2
);
220 sws_getFlagsAndFilterFromCmdLine(&int_sws_flags
, &srcFilter
, &dstFilter
);
221 int_sws_flags
|= vf
->priv
->v_chr_drop
<< SWS_SRC_V_CHR_DROP_SHIFT
;
222 vf
->priv
->ctx
=sws_getContext(width
, height
>> vf
->priv
->interlaced
,
224 vf
->priv
->w
, vf
->priv
->h
>> vf
->priv
->interlaced
,
226 int_sws_flags
| get_sws_cpuflags(), srcFilter
, dstFilter
, vf
->priv
->param
);
227 if(vf
->priv
->interlaced
){
228 vf
->priv
->ctx2
=sws_getContext(width
, height
>> 1,
230 vf
->priv
->w
, vf
->priv
->h
>> 1,
232 int_sws_flags
| get_sws_cpuflags(), srcFilter
, dstFilter
, vf
->priv
->param
);
236 mp_msg(MSGT_VFILTER
,MSGL_WARN
,"Couldn't init SwScaler for this setup\n");
241 if(vf
->priv
->palette
){
242 free(vf
->priv
->palette
);
243 vf
->priv
->palette
=NULL
;
247 /* set 332 palette for 8 bpp */
249 vf
->priv
->palette
=malloc(4*256);
250 for(i
=0; i
<256; i
++){
251 vf
->priv
->palette
[4*i
+0]=4*(i
&3)*21;
252 vf
->priv
->palette
[4*i
+1]=4*((i
>>2)&7)*9;
253 vf
->priv
->palette
[4*i
+2]=4*((i
>>5)&7)*9;
259 vf
->priv
->palette
=malloc(4*16);
261 vf
->priv
->palette
[4*i
+0]=4*(i
&1)*63;
262 vf
->priv
->palette
[4*i
+1]=4*((i
>>1)&3)*21;
263 vf
->priv
->palette
[4*i
+2]=4*((i
>>3)&1)*63;
268 if(!opt_screen_size_x
&& !opt_screen_size_y
&& !(screen_size_xy
>= 0.001)){
269 // Compute new d_width and d_height, preserving aspect
270 // while ensuring that both are >= output size in pixels.
271 if (vf
->priv
->h
* d_width
> vf
->priv
->w
* d_height
) {
272 d_width
= vf
->priv
->h
* d_width
/ d_height
;
273 d_height
= vf
->priv
->h
;
275 d_height
= vf
->priv
->w
* d_height
/ d_width
;
276 d_width
= vf
->priv
->w
;
278 //d_width=d_width*vf->priv->w/width;
279 //d_height=d_height*vf->priv->h/height;
281 return vf_next_config(vf
,vf
->priv
->w
,vf
->priv
->h
,d_width
,d_height
,flags
,best
);
284 static void start_slice(struct vf_instance_s
* vf
, mp_image_t
*mpi
){
285 // printf("start_slice called! flag=%d\n",mpi->flags&MP_IMGFLAG_DRAW_CALLBACK);
286 if(!(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
)) return; // shouldn't happen
287 // they want slices!!! allocate the buffer.
288 mpi
->priv
=vf
->dmpi
=vf_get_image(vf
->next
,vf
->priv
->fmt
,
289 // mpi->type, mpi->flags & (~MP_IMGFLAG_DRAW_CALLBACK),
290 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_PREFER_ALIGNED_STRIDE
,
291 vf
->priv
->w
, vf
->priv
->h
);
294 static void scale(struct SwsContext
*sws1
, struct SwsContext
*sws2
, uint8_t *src
[3], int src_stride
[3], int y
, int h
,
295 uint8_t *dst
[3], int dst_stride
[3], int interlaced
){
298 uint8_t *src2
[3]={src
[0], src
[1], src
[2]};
299 uint8_t *dst2
[3]={dst
[0], dst
[1], dst
[2]};
300 int src_stride2
[3]={2*src_stride
[0], 2*src_stride
[1], 2*src_stride
[2]};
301 int dst_stride2
[3]={2*dst_stride
[0], 2*dst_stride
[1], 2*dst_stride
[2]};
303 sws_scale_ordered(sws1
, src2
, src_stride2
, y
>>1, h
>>1, dst2
, dst_stride2
);
305 src2
[i
] += src_stride
[i
];
306 dst2
[i
] += dst_stride
[i
];
308 sws_scale_ordered(sws2
, src2
, src_stride2
, y
>>1, h
>>1, dst2
, dst_stride2
);
310 sws_scale_ordered(sws1
, src
, src_stride
, y
, h
, dst
, dst_stride
);
314 static void draw_slice(struct vf_instance_s
* vf
,
315 unsigned char** src
, int* stride
, int w
,int h
, int x
, int y
){
316 mp_image_t
*dmpi
=vf
->dmpi
;
318 mp_msg(MSGT_VFILTER
,MSGL_FATAL
,"vf_scale: draw_slice() called with dmpi=NULL (no get_image??)\n");
321 // printf("vf_scale::draw_slice() y=%d h=%d\n",y,h);
322 scale(vf
->priv
->ctx
, vf
->priv
->ctx2
, src
, stride
, y
, h
, dmpi
->planes
, dmpi
->stride
, vf
->priv
->interlaced
);
325 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
326 mp_image_t
*dmpi
=mpi
->priv
;
328 // printf("vf_scale::put_image(): processing whole frame! dmpi=%p flag=%d\n",
329 // dmpi, (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK));
331 if(!(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
&& dmpi
)){
333 // hope we'll get DR buffer:
334 dmpi
=vf_get_image(vf
->next
,vf
->priv
->fmt
,
335 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_PREFER_ALIGNED_STRIDE
,
336 vf
->priv
->w
, vf
->priv
->h
);
338 scale(vf
->priv
->ctx
, vf
->priv
->ctx
, mpi
->planes
,mpi
->stride
,0,mpi
->h
,dmpi
->planes
,dmpi
->stride
, vf
->priv
->interlaced
);
341 if(vf
->priv
->w
==mpi
->w
&& vf
->priv
->h
==mpi
->h
){
342 // just conversion, no scaling -> keep postprocessing data
343 // this way we can apply pp filter to non-yv12 source using scaler
344 vf_clone_mpi_attributes(dmpi
, mpi
);
347 if(vf
->priv
->palette
) dmpi
->planes
[1]=vf
->priv
->palette
; // export palette!
349 return vf_next_put_image(vf
,dmpi
, pts
);
352 static int control(struct vf_instance_s
* vf
, int request
, void* data
){
356 int brightness
, contrast
, saturation
, srcRange
, dstRange
;
361 case VFCTRL_GET_EQUALIZER
:
362 r
= sws_getColorspaceDetails(vf
->priv
->ctx
, &inv_table
, &srcRange
, &table
, &dstRange
, &brightness
, &contrast
, &saturation
);
366 if (!strcmp(eq
->item
,"brightness")) {
367 eq
->value
= ((brightness
*100) + (1<<15))>>16;
369 else if (!strcmp(eq
->item
,"contrast")) {
370 eq
->value
= (((contrast
*100) + (1<<15))>>16) - 100;
372 else if (!strcmp(eq
->item
,"saturation")) {
373 eq
->value
= (((saturation
*100) + (1<<15))>>16) - 100;
378 case VFCTRL_SET_EQUALIZER
:
379 r
= sws_getColorspaceDetails(vf
->priv
->ctx
, &inv_table
, &srcRange
, &table
, &dstRange
, &brightness
, &contrast
, &saturation
);
381 //printf("set %f %f %f\n", brightness/(float)(1<<16), contrast/(float)(1<<16), saturation/(float)(1<<16));
384 if (!strcmp(eq
->item
,"brightness")) {
385 brightness
= (( eq
->value
<<16) + 50)/100;
387 else if (!strcmp(eq
->item
,"contrast")) {
388 contrast
= (((eq
->value
+100)<<16) + 50)/100;
390 else if (!strcmp(eq
->item
,"saturation")) {
391 saturation
= (((eq
->value
+100)<<16) + 50)/100;
396 r
= sws_setColorspaceDetails(vf
->priv
->ctx
, inv_table
, srcRange
, table
, dstRange
, brightness
, contrast
, saturation
);
399 r
= sws_setColorspaceDetails(vf
->priv
->ctx2
, inv_table
, srcRange
, table
, dstRange
, brightness
, contrast
, saturation
);
408 return vf_next_control(vf
,request
,data
);
411 //===========================================================================//
413 // supported Input formats: YV12, I420, IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8, Y800
415 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
436 unsigned int best
=find_best_out(vf
);
438 if(!best
) return 0; // no matching out-fmt
439 flags
=vf_next_query_format(vf
,best
);
440 if(!(flags
&(VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
))) return 0; // huh?
441 if(fmt
!=best
) flags
&=~VFCAP_CSP_SUPPORTED_BY_HW
;
442 // do not allow scaling, if we are before the PP fliter!
443 if(!(flags
&VFCAP_POSTPROC
)) flags
|=VFCAP_SWSCALE
;
447 return 0; // nomatching in-fmt
450 static void uninit(struct vf_instance_s
*vf
){
451 if(vf
->priv
->ctx
) sws_freeContext(vf
->priv
->ctx
);
452 if(vf
->priv
->ctx2
) sws_freeContext(vf
->priv
->ctx2
);
453 if(vf
->priv
->palette
) free(vf
->priv
->palette
);
457 static int open(vf_instance_t
*vf
, char* args
){
459 vf
->start_slice
=start_slice
;
460 vf
->draw_slice
=draw_slice
;
461 vf
->put_image
=put_image
;
462 vf
->query_format
=query_format
;
463 vf
->control
= control
;
466 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
467 // TODO: parse args ->
472 vf
->priv
->v_chr_drop
=0;
474 vf
->priv
->param
[1]=SWS_PARAM_DEFAULT
;
475 vf
->priv
->palette
=NULL
;
477 if(args
) sscanf(args
, "%d:%d:%d:%lf:%lf",
480 &vf
->priv
->v_chr_drop
,
482 &vf
->priv
->param
[1]);
483 mp_msg(MSGT_VFILTER
,MSGL_V
,"SwScale params: %d x %d (-1=no scaling)\n",
490 //global sws_flags from the command line
494 static SwsFilter
*src_filter
= NULL
;
496 float sws_lum_gblur
= 0.0;
497 float sws_chr_gblur
= 0.0;
498 int sws_chr_vshift
= 0;
499 int sws_chr_hshift
= 0;
500 float sws_chr_sharpen
= 0.0;
501 float sws_lum_sharpen
= 0.0;
503 int get_sws_cpuflags(void){
505 (gCpuCaps
.hasMMX
? SWS_CPU_CAPS_MMX
: 0)
506 | (gCpuCaps
.hasMMX2
? SWS_CPU_CAPS_MMX2
: 0)
507 | (gCpuCaps
.has3DNow
? SWS_CPU_CAPS_3DNOW
: 0)
508 | (gCpuCaps
.hasAltiVec
? SWS_CPU_CAPS_ALTIVEC
: 0);
511 void sws_getFlagsAndFilterFromCmdLine(int *flags
, SwsFilter
**srcFilterParam
, SwsFilter
**dstFilterParam
)
513 static int firstTime
=1;
518 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
523 *flags
= SWS_PRINT_INFO
;
525 else if( mp_msg_test(MSGT_VFILTER
,MSGL_DBG2
) ) *flags
= SWS_PRINT_INFO
;
527 if(src_filter
) sws_freeFilter(src_filter
);
529 src_filter
= sws_getDefaultFilter(
530 sws_lum_gblur
, sws_chr_gblur
,
531 sws_lum_sharpen
, sws_chr_sharpen
,
532 sws_chr_hshift
, sws_chr_vshift
, verbose
>1);
536 case 0: *flags
|= SWS_FAST_BILINEAR
; break;
537 case 1: *flags
|= SWS_BILINEAR
; break;
538 case 2: *flags
|= SWS_BICUBIC
; break;
539 case 3: *flags
|= SWS_X
; break;
540 case 4: *flags
|= SWS_POINT
; break;
541 case 5: *flags
|= SWS_AREA
; break;
542 case 6: *flags
|= SWS_BICUBLIN
; break;
543 case 7: *flags
|= SWS_GAUSS
; break;
544 case 8: *flags
|= SWS_SINC
; break;
545 case 9: *flags
|= SWS_LANCZOS
; break;
546 case 10:*flags
|= SWS_SPLINE
; break;
547 default:*flags
|= SWS_BILINEAR
; break;
550 *srcFilterParam
= src_filter
;
551 *dstFilterParam
= NULL
;
554 // will use sws_flags & src_filter (from cmd line)
555 struct SwsContext
*sws_getContextFromCmdLine(int srcW
, int srcH
, int srcFormat
, int dstW
, int dstH
, int dstFormat
)
558 SwsFilter
*dstFilterParam
, *srcFilterParam
;
559 sws_getFlagsAndFilterFromCmdLine(&flags
, &srcFilterParam
, &dstFilterParam
);
561 return sws_getContext(srcW
, srcH
, srcFormat
, dstW
, dstH
, dstFormat
, flags
| get_sws_cpuflags(), srcFilterParam
, dstFilterParam
, NULL
);
564 /// An example of presets usage
565 static struct size_preset
{
568 } vf_size_presets_defs
[] = {
569 // TODO add more 'standard' resolutions
570 { "qntsc", 352, 240 },
571 { "qpal", 352, 288 },
572 { "ntsc", 720, 480 },
574 { "sntsc", 640, 480 },
575 { "spal", 768, 576 },
579 #define ST_OFF(f) M_ST_OFF(struct size_preset,f)
580 static m_option_t vf_size_preset_fields
[] = {
581 {"w", ST_OFF(w
), CONF_TYPE_INT
, M_OPT_MIN
,1 ,0, NULL
},
582 {"h", ST_OFF(h
), CONF_TYPE_INT
, M_OPT_MIN
,1 ,0, NULL
},
583 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
586 static m_struct_t vf_size_preset
= {
588 sizeof(struct size_preset
),
590 vf_size_preset_fields
593 static m_struct_t vf_opts
;
594 static m_obj_presets_t size_preset
= {
595 &vf_size_preset
, // Input struct desc
596 &vf_opts
, // Output struct desc
597 vf_size_presets_defs
, // The list of presets
598 ST_OFF(name
) // At wich offset is the name field in the preset struct
603 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
604 static m_option_t vf_opts_fields
[] = {
605 {"w", ST_OFF(w
), CONF_TYPE_INT
, M_OPT_MIN
,-11,0, NULL
},
606 {"h", ST_OFF(h
), CONF_TYPE_INT
, M_OPT_MIN
,-11,0, NULL
},
607 {"interlaced", ST_OFF(interlaced
), CONF_TYPE_INT
, M_OPT_RANGE
, 0, 1, NULL
},
608 {"chr-drop", ST_OFF(v_chr_drop
), CONF_TYPE_INT
, M_OPT_RANGE
, 0, 3, NULL
},
609 {"param" , ST_OFF(param
[0]), CONF_TYPE_DOUBLE
, M_OPT_RANGE
, 0.0, 100.0, NULL
},
610 {"param2", ST_OFF(param
[1]), CONF_TYPE_DOUBLE
, M_OPT_RANGE
, 0.0, 100.0, NULL
},
611 // Note that here the 2 field is NULL (ie 0)
612 // As we want this option to act on the option struct itself
613 {"presize", 0, CONF_TYPE_OBJ_PRESETS
, 0, 0, 0, &size_preset
},
614 {"noup", ST_OFF(noup
), CONF_TYPE_INT
, M_OPT_RANGE
, 0, 1, NULL
},
615 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
618 static m_struct_t vf_opts
= {
620 sizeof(struct vf_priv_s
),
625 vf_info_t vf_info_scale
= {
634 //===========================================================================//