10 #include "img_format.h"
13 #include "fmt-conversion.h"
16 #include "libswscale/swscale.h"
22 static struct vf_priv_s
{
27 struct SwsContext
*ctx
;
28 struct SwsContext
*ctx2
; //for interlaced slices only
29 unsigned char* palette
;
33 int query_format_cache
[64];
34 } const vf_priv_dflt
= {
37 {SWS_PARAM_DEFAULT
, SWS_PARAM_DEFAULT
},
44 extern int opt_screen_size_x
;
45 extern int opt_screen_size_y
;
46 extern float screen_size_xy
;
48 //===========================================================================//
50 void sws_getFlagsAndFilterFromCmdLine(int *flags
, SwsFilter
**srcFilterParam
, SwsFilter
**dstFilterParam
);
52 static unsigned int outfmt_list
[]={
66 // RGB and grayscale (Y8 and Y800):
88 static unsigned int find_best_out(vf_instance_t
*vf
){
92 // find the best outfmt:
93 for(i
=0; i
<sizeof(outfmt_list
)/sizeof(int)-1; i
++){
94 const int format
= outfmt_list
[i
];
95 int ret
= vf
->priv
->query_format_cache
[i
]-1;
97 ret
= vf_next_query_format(vf
, outfmt_list
[i
]);
98 vf
->priv
->query_format_cache
[i
]= ret
+1;
101 mp_msg(MSGT_VFILTER
,MSGL_DBG2
,"scale: query(%s) -> %d\n",vo_format_name(format
),ret
&3);
102 if(ret
&VFCAP_CSP_SUPPORTED_BY_HW
){
103 best
=format
; // no conversion -> bingo!
106 if(ret
&VFCAP_CSP_SUPPORTED
&& !best
)
107 best
=format
; // best with conversion
112 static int config(struct vf_instance_s
* vf
,
113 int width
, int height
, int d_width
, int d_height
,
114 unsigned int flags
, unsigned int outfmt
){
115 unsigned int best
=find_best_out(vf
);
118 int round_w
=0, round_h
=0;
119 SwsFilter
*srcFilter
, *dstFilter
;
120 enum PixelFormat dfmt
, sfmt
;
123 mp_msg(MSGT_VFILTER
,MSGL_WARN
,"SwScale: no supported outfmt found :(\n");
126 sfmt
= imgfmt2pixfmt(outfmt
);
127 if (outfmt
== IMGFMT_RGB8
|| outfmt
== IMGFMT_BGR8
) sfmt
= PIX_FMT_PAL8
;
128 dfmt
= imgfmt2pixfmt(best
);
130 vo_flags
=vf
->next
->query_format(vf
->next
,best
);
132 // scaling to dwidth*d_height, if all these TRUE:
134 // - no other sw/hw up/down scaling avail.
135 // - we're after postproc
136 // - user didn't set w:h
137 if(!(vo_flags
&VFCAP_POSTPROC
) && (flags
&4) &&
138 vf
->priv
->w
<0 && vf
->priv
->h
<0){ // -zoom
139 int x
=(vo_flags
&VFCAP_SWSCALE
) ? 0 : 1;
140 if(d_width
<width
|| d_height
<height
){
142 if(vo_flags
&VFCAP_HWSCALE_DOWN
) x
=0;
145 if(vo_flags
&VFCAP_HWSCALE_UP
) x
=0;
148 // user wants sw scaling! (-zoom)
150 vf
->priv
->h
=d_height
;
155 if((vf
->priv
->w
> width
) + (vf
->priv
->h
> height
) >= vf
->priv
->noup
){
161 if (vf
->priv
->w
<= -8) {
165 if (vf
->priv
->h
<= -8) {
170 if (vf
->priv
->w
< -3 || vf
->priv
->h
< -3 ||
171 (vf
->priv
->w
< -1 && vf
->priv
->h
< -1)) {
172 // TODO: establish a direct connection to the user's brain
173 // and find out what the heck he thinks MPlayer should do
174 // with this nonsense.
175 mp_msg(MSGT_VFILTER
, MSGL_ERR
, "SwScale: EUSERBROKEN Check your parameters, they make no sense!\n");
179 if (vf
->priv
->w
== -1)
181 if (vf
->priv
->w
== 0)
182 vf
->priv
->w
= d_width
;
184 if (vf
->priv
->h
== -1)
185 vf
->priv
->h
= height
;
186 if (vf
->priv
->h
== 0)
187 vf
->priv
->h
= d_height
;
189 if (vf
->priv
->w
== -3)
190 vf
->priv
->w
= vf
->priv
->h
* width
/ height
;
191 if (vf
->priv
->w
== -2)
192 vf
->priv
->w
= vf
->priv
->h
* d_width
/ d_height
;
194 if (vf
->priv
->h
== -3)
195 vf
->priv
->h
= vf
->priv
->w
* height
/ width
;
196 if (vf
->priv
->h
== -2)
197 vf
->priv
->h
= vf
->priv
->w
* d_height
/ d_width
;
200 vf
->priv
->w
= ((vf
->priv
->w
+ 8) / 16) * 16;
202 vf
->priv
->h
= ((vf
->priv
->h
+ 8) / 16) * 16;
204 // calculate the missing parameters:
206 case IMGFMT_YV12
: /* YV12 needs w & h rounded to 2 */
211 vf
->priv
->h
= (vf
->priv
->h
+ 1) & ~1;
212 case IMGFMT_YUY2
: /* YUY2 needs w rounded to 2 */
214 vf
->priv
->w
= (vf
->priv
->w
+ 1) & ~1;
217 mp_msg(MSGT_VFILTER
,MSGL_DBG2
,"SwScale: scaling %dx%d %s to %dx%d %s \n",
218 width
,height
,vo_format_name(outfmt
),
219 vf
->priv
->w
,vf
->priv
->h
,vo_format_name(best
));
222 if(vf
->priv
->ctx
) sws_freeContext(vf
->priv
->ctx
);
223 if(vf
->priv
->ctx2
)sws_freeContext(vf
->priv
->ctx2
);
226 sws_getFlagsAndFilterFromCmdLine(&int_sws_flags
, &srcFilter
, &dstFilter
);
227 int_sws_flags
|= vf
->priv
->v_chr_drop
<< SWS_SRC_V_CHR_DROP_SHIFT
;
228 int_sws_flags
|= vf
->priv
->accurate_rnd
* SWS_ACCURATE_RND
;
229 vf
->priv
->ctx
=sws_getContext(width
, height
>> vf
->priv
->interlaced
,
231 vf
->priv
->w
, vf
->priv
->h
>> vf
->priv
->interlaced
,
233 int_sws_flags
| get_sws_cpuflags(), srcFilter
, dstFilter
, vf
->priv
->param
);
234 if(vf
->priv
->interlaced
){
235 vf
->priv
->ctx2
=sws_getContext(width
, height
>> 1,
237 vf
->priv
->w
, vf
->priv
->h
>> 1,
239 int_sws_flags
| get_sws_cpuflags(), srcFilter
, dstFilter
, vf
->priv
->param
);
243 mp_msg(MSGT_VFILTER
,MSGL_WARN
,"Couldn't init SwScaler for this setup\n");
248 if(vf
->priv
->palette
){
249 free(vf
->priv
->palette
);
250 vf
->priv
->palette
=NULL
;
254 /* set 332 palette for 8 bpp */
256 vf
->priv
->palette
=malloc(4*256);
257 for(i
=0; i
<256; i
++){
258 vf
->priv
->palette
[4*i
+0]=4*(i
>>6)*21;
259 vf
->priv
->palette
[4*i
+1]=4*((i
>>3)&7)*9;
260 vf
->priv
->palette
[4*i
+2]=4*((i
&7)&7)*9;
261 vf
->priv
->palette
[4*i
+3]=0;
265 /* set 332 palette for 8 bpp */
267 vf
->priv
->palette
=malloc(4*256);
268 for(i
=0; i
<256; i
++){
269 vf
->priv
->palette
[4*i
+0]=4*(i
&3)*21;
270 vf
->priv
->palette
[4*i
+1]=4*((i
>>2)&7)*9;
271 vf
->priv
->palette
[4*i
+2]=4*((i
>>5)&7)*9;
272 vf
->priv
->palette
[4*i
+3]=0;
278 vf
->priv
->palette
=malloc(4*16);
280 vf
->priv
->palette
[4*i
+0]=4*(i
&1)*63;
281 vf
->priv
->palette
[4*i
+1]=4*((i
>>1)&3)*21;
282 vf
->priv
->palette
[4*i
+2]=4*((i
>>3)&1)*63;
283 vf
->priv
->palette
[4*i
+3]=0;
289 vf
->priv
->palette
=malloc(4*16);
291 vf
->priv
->palette
[4*i
+0]=4*(i
>>3)*63;
292 vf
->priv
->palette
[4*i
+1]=4*((i
>>1)&3)*21;
293 vf
->priv
->palette
[4*i
+2]=4*((i
&1)&1)*63;
294 vf
->priv
->palette
[4*i
+3]=0;
299 if(!opt_screen_size_x
&& !opt_screen_size_y
&& !(screen_size_xy
>= 0.001)){
300 // Compute new d_width and d_height, preserving aspect
301 // while ensuring that both are >= output size in pixels.
302 if (vf
->priv
->h
* d_width
> vf
->priv
->w
* d_height
) {
303 d_width
= vf
->priv
->h
* d_width
/ d_height
;
304 d_height
= vf
->priv
->h
;
306 d_height
= vf
->priv
->w
* d_height
/ d_width
;
307 d_width
= vf
->priv
->w
;
309 //d_width=d_width*vf->priv->w/width;
310 //d_height=d_height*vf->priv->h/height;
312 return vf_next_config(vf
,vf
->priv
->w
,vf
->priv
->h
,d_width
,d_height
,flags
,best
);
315 static void start_slice(struct vf_instance_s
* vf
, mp_image_t
*mpi
){
316 // printf("start_slice called! flag=%d\n",mpi->flags&MP_IMGFLAG_DRAW_CALLBACK);
317 if(!(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
)) return; // shouldn't happen
318 // they want slices!!! allocate the buffer.
319 mpi
->priv
=vf
->dmpi
=vf_get_image(vf
->next
,vf
->priv
->fmt
,
320 // mpi->type, mpi->flags & (~MP_IMGFLAG_DRAW_CALLBACK),
321 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_PREFER_ALIGNED_STRIDE
,
322 vf
->priv
->w
, vf
->priv
->h
);
325 static void scale(struct SwsContext
*sws1
, struct SwsContext
*sws2
, uint8_t *src
[3], int src_stride
[3], int y
, int h
,
326 uint8_t *dst
[3], int dst_stride
[3], int interlaced
){
327 uint8_t *src2
[3]={src
[0], src
[1], src
[2]};
328 #ifdef WORDS_BIGENDIAN
330 if (src
[1] && !src
[2]){
333 pal2
[i
]= bswap_32(((uint32_t*)src
[1])[i
]);
340 uint8_t *dst2
[3]={dst
[0], dst
[1], dst
[2]};
341 int src_stride2
[3]={2*src_stride
[0], 2*src_stride
[1], 2*src_stride
[2]};
342 int dst_stride2
[3]={2*dst_stride
[0], 2*dst_stride
[1], 2*dst_stride
[2]};
344 sws_scale_ordered(sws1
, src2
, src_stride2
, y
>>1, h
>>1, dst2
, dst_stride2
);
346 src2
[i
] += src_stride
[i
];
347 dst2
[i
] += dst_stride
[i
];
349 sws_scale_ordered(sws2
, src2
, src_stride2
, y
>>1, h
>>1, dst2
, dst_stride2
);
351 sws_scale_ordered(sws1
, src2
, src_stride
, y
, h
, dst
, dst_stride
);
355 static void draw_slice(struct vf_instance_s
* vf
,
356 unsigned char** src
, int* stride
, int w
,int h
, int x
, int y
){
357 mp_image_t
*dmpi
=vf
->dmpi
;
359 mp_msg(MSGT_VFILTER
,MSGL_FATAL
,"vf_scale: draw_slice() called with dmpi=NULL (no get_image?)\n");
362 // printf("vf_scale::draw_slice() y=%d h=%d\n",y,h);
363 scale(vf
->priv
->ctx
, vf
->priv
->ctx2
, src
, stride
, y
, h
, dmpi
->planes
, dmpi
->stride
, vf
->priv
->interlaced
);
366 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
367 mp_image_t
*dmpi
=mpi
->priv
;
369 // printf("vf_scale::put_image(): processing whole frame! dmpi=%p flag=%d\n",
370 // dmpi, (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK));
372 if(!(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
&& dmpi
)){
374 // hope we'll get DR buffer:
375 dmpi
=vf_get_image(vf
->next
,vf
->priv
->fmt
,
376 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_PREFER_ALIGNED_STRIDE
,
377 vf
->priv
->w
, vf
->priv
->h
);
379 scale(vf
->priv
->ctx
, vf
->priv
->ctx
, mpi
->planes
,mpi
->stride
,0,mpi
->h
,dmpi
->planes
,dmpi
->stride
, vf
->priv
->interlaced
);
382 if(vf
->priv
->w
==mpi
->w
&& vf
->priv
->h
==mpi
->h
){
383 // just conversion, no scaling -> keep postprocessing data
384 // this way we can apply pp filter to non-yv12 source using scaler
385 vf_clone_mpi_attributes(dmpi
, mpi
);
388 if(vf
->priv
->palette
) dmpi
->planes
[1]=vf
->priv
->palette
; // export palette!
390 return vf_next_put_image(vf
,dmpi
, pts
);
393 static int control(struct vf_instance_s
* vf
, int request
, void* data
){
397 int brightness
, contrast
, saturation
, srcRange
, dstRange
;
402 case VFCTRL_GET_EQUALIZER
:
403 r
= sws_getColorspaceDetails(vf
->priv
->ctx
, &inv_table
, &srcRange
, &table
, &dstRange
, &brightness
, &contrast
, &saturation
);
407 if (!strcmp(eq
->item
,"brightness")) {
408 eq
->value
= ((brightness
*100) + (1<<15))>>16;
410 else if (!strcmp(eq
->item
,"contrast")) {
411 eq
->value
= (((contrast
*100) + (1<<15))>>16) - 100;
413 else if (!strcmp(eq
->item
,"saturation")) {
414 eq
->value
= (((saturation
*100) + (1<<15))>>16) - 100;
419 case VFCTRL_SET_EQUALIZER
:
420 r
= sws_getColorspaceDetails(vf
->priv
->ctx
, &inv_table
, &srcRange
, &table
, &dstRange
, &brightness
, &contrast
, &saturation
);
422 //printf("set %f %f %f\n", brightness/(float)(1<<16), contrast/(float)(1<<16), saturation/(float)(1<<16));
425 if (!strcmp(eq
->item
,"brightness")) {
426 brightness
= (( eq
->value
<<16) + 50)/100;
428 else if (!strcmp(eq
->item
,"contrast")) {
429 contrast
= (((eq
->value
+100)<<16) + 50)/100;
431 else if (!strcmp(eq
->item
,"saturation")) {
432 saturation
= (((eq
->value
+100)<<16) + 50)/100;
437 r
= sws_setColorspaceDetails(vf
->priv
->ctx
, inv_table
, srcRange
, table
, dstRange
, brightness
, contrast
, saturation
);
440 r
= sws_setColorspaceDetails(vf
->priv
->ctx2
, inv_table
, srcRange
, table
, dstRange
, brightness
, contrast
, saturation
);
449 return vf_next_control(vf
,request
,data
);
452 //===========================================================================//
454 // supported Input formats: YV12, I420, IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8, Y800
456 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
481 unsigned int best
=find_best_out(vf
);
483 if(!best
) return 0; // no matching out-fmt
484 flags
=vf_next_query_format(vf
,best
);
485 if(!(flags
&(VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
))) return 0; // huh?
486 if(fmt
!=best
) flags
&=~VFCAP_CSP_SUPPORTED_BY_HW
;
487 // do not allow scaling, if we are before the PP fliter!
488 if(!(flags
&VFCAP_POSTPROC
)) flags
|=VFCAP_SWSCALE
;
492 return 0; // nomatching in-fmt
495 static void uninit(struct vf_instance_s
*vf
){
496 if(vf
->priv
->ctx
) sws_freeContext(vf
->priv
->ctx
);
497 if(vf
->priv
->ctx2
) sws_freeContext(vf
->priv
->ctx2
);
498 if(vf
->priv
->palette
) free(vf
->priv
->palette
);
502 static int open(vf_instance_t
*vf
, char* args
){
504 vf
->start_slice
=start_slice
;
505 vf
->draw_slice
=draw_slice
;
506 vf
->put_image
=put_image
;
507 vf
->query_format
=query_format
;
508 vf
->control
= control
;
511 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
512 // TODO: parse args ->
517 vf
->priv
->v_chr_drop
=0;
518 vf
->priv
->accurate_rnd
=0;
520 vf
->priv
->param
[1]=SWS_PARAM_DEFAULT
;
521 vf
->priv
->palette
=NULL
;
523 if(args
) sscanf(args
, "%d:%d:%d:%lf:%lf",
526 &vf
->priv
->v_chr_drop
,
528 &vf
->priv
->param
[1]);
529 mp_msg(MSGT_VFILTER
,MSGL_V
,"SwScale params: %d x %d (-1=no scaling)\n",
536 //global sws_flags from the command line
540 static SwsFilter
*src_filter
= NULL
;
542 float sws_lum_gblur
= 0.0;
543 float sws_chr_gblur
= 0.0;
544 int sws_chr_vshift
= 0;
545 int sws_chr_hshift
= 0;
546 float sws_chr_sharpen
= 0.0;
547 float sws_lum_sharpen
= 0.0;
549 int get_sws_cpuflags(void){
551 (gCpuCaps
.hasMMX
? SWS_CPU_CAPS_MMX
: 0)
552 | (gCpuCaps
.hasMMX2
? SWS_CPU_CAPS_MMX2
: 0)
553 | (gCpuCaps
.has3DNow
? SWS_CPU_CAPS_3DNOW
: 0)
554 | (gCpuCaps
.hasAltiVec
? SWS_CPU_CAPS_ALTIVEC
: 0);
557 void sws_getFlagsAndFilterFromCmdLine(int *flags
, SwsFilter
**srcFilterParam
, SwsFilter
**dstFilterParam
)
559 static int firstTime
=1;
564 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
569 *flags
= SWS_PRINT_INFO
;
571 else if( mp_msg_test(MSGT_VFILTER
,MSGL_DBG2
) ) *flags
= SWS_PRINT_INFO
;
573 if(src_filter
) sws_freeFilter(src_filter
);
575 src_filter
= sws_getDefaultFilter(
576 sws_lum_gblur
, sws_chr_gblur
,
577 sws_lum_sharpen
, sws_chr_sharpen
,
578 sws_chr_hshift
, sws_chr_vshift
, verbose
>1);
582 case 0: *flags
|= SWS_FAST_BILINEAR
; break;
583 case 1: *flags
|= SWS_BILINEAR
; break;
584 case 2: *flags
|= SWS_BICUBIC
; break;
585 case 3: *flags
|= SWS_X
; break;
586 case 4: *flags
|= SWS_POINT
; break;
587 case 5: *flags
|= SWS_AREA
; break;
588 case 6: *flags
|= SWS_BICUBLIN
; break;
589 case 7: *flags
|= SWS_GAUSS
; break;
590 case 8: *flags
|= SWS_SINC
; break;
591 case 9: *flags
|= SWS_LANCZOS
; break;
592 case 10:*flags
|= SWS_SPLINE
; break;
593 default:*flags
|= SWS_BILINEAR
; break;
596 *srcFilterParam
= src_filter
;
597 *dstFilterParam
= NULL
;
600 // will use sws_flags & src_filter (from cmd line)
601 struct SwsContext
*sws_getContextFromCmdLine(int srcW
, int srcH
, int srcFormat
, int dstW
, int dstH
, int dstFormat
)
604 SwsFilter
*dstFilterParam
, *srcFilterParam
;
605 enum PixelFormat dfmt
, sfmt
;
607 dfmt
= imgfmt2pixfmt(dstFormat
);
608 sfmt
= imgfmt2pixfmt(srcFormat
);
609 if (srcFormat
== IMGFMT_RGB8
|| srcFormat
== IMGFMT_BGR8
) sfmt
= PIX_FMT_PAL8
;
610 sws_getFlagsAndFilterFromCmdLine(&flags
, &srcFilterParam
, &dstFilterParam
);
612 return sws_getContext(srcW
, srcH
, sfmt
, dstW
, dstH
, dfmt
, flags
| get_sws_cpuflags(), srcFilterParam
, dstFilterParam
, NULL
);
615 /// An example of presets usage
616 static struct size_preset
{
619 } vf_size_presets_defs
[] = {
620 // TODO add more 'standard' resolutions
621 { "qntsc", 352, 240 },
622 { "qpal", 352, 288 },
623 { "ntsc", 720, 480 },
625 { "sntsc", 640, 480 },
626 { "spal", 768, 576 },
630 #define ST_OFF(f) M_ST_OFF(struct size_preset,f)
631 static m_option_t vf_size_preset_fields
[] = {
632 {"w", ST_OFF(w
), CONF_TYPE_INT
, M_OPT_MIN
,1 ,0, NULL
},
633 {"h", ST_OFF(h
), CONF_TYPE_INT
, M_OPT_MIN
,1 ,0, NULL
},
634 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
637 static m_struct_t vf_size_preset
= {
639 sizeof(struct size_preset
),
641 vf_size_preset_fields
644 static m_struct_t vf_opts
;
645 static m_obj_presets_t size_preset
= {
646 &vf_size_preset
, // Input struct desc
647 &vf_opts
, // Output struct desc
648 vf_size_presets_defs
, // The list of presets
649 ST_OFF(name
) // At wich offset is the name field in the preset struct
654 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
655 static m_option_t vf_opts_fields
[] = {
656 {"w", ST_OFF(w
), CONF_TYPE_INT
, M_OPT_MIN
,-11,0, NULL
},
657 {"h", ST_OFF(h
), CONF_TYPE_INT
, M_OPT_MIN
,-11,0, NULL
},
658 {"interlaced", ST_OFF(interlaced
), CONF_TYPE_INT
, M_OPT_RANGE
, 0, 1, NULL
},
659 {"chr-drop", ST_OFF(v_chr_drop
), CONF_TYPE_INT
, M_OPT_RANGE
, 0, 3, NULL
},
660 {"param" , ST_OFF(param
[0]), CONF_TYPE_DOUBLE
, M_OPT_RANGE
, 0.0, 100.0, NULL
},
661 {"param2", ST_OFF(param
[1]), CONF_TYPE_DOUBLE
, M_OPT_RANGE
, 0.0, 100.0, NULL
},
662 // Note that here the 2 field is NULL (ie 0)
663 // As we want this option to act on the option struct itself
664 {"presize", 0, CONF_TYPE_OBJ_PRESETS
, 0, 0, 0, &size_preset
},
665 {"noup", ST_OFF(noup
), CONF_TYPE_INT
, M_OPT_RANGE
, 0, 2, NULL
},
666 {"arnd", ST_OFF(accurate_rnd
), CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
667 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
670 static m_struct_t vf_opts
= {
672 sizeof(struct vf_priv_s
),
677 const vf_info_t vf_info_scale
= {
686 //===========================================================================//