11 #include "img_format.h"
14 #include "fmt-conversion.h"
17 #include "libswscale/swscale.h"
23 static struct vf_priv_s
{
28 struct SwsContext
*ctx
;
29 struct SwsContext
*ctx2
; //for interlaced slices only
30 unsigned char* palette
;
34 int query_format_cache
[64];
35 } const vf_priv_dflt
= {
38 {SWS_PARAM_DEFAULT
, SWS_PARAM_DEFAULT
},
45 //===========================================================================//
47 void sws_getFlagsAndFilterFromCmdLine(int *flags
, SwsFilter
**srcFilterParam
, SwsFilter
**dstFilterParam
);
49 static const unsigned int outfmt_list
[]={
63 // RGB and grayscale (Y8 and Y800):
85 static unsigned int find_best_out(vf_instance_t
*vf
){
89 // find the best outfmt:
90 for(i
=0; i
<sizeof(outfmt_list
)/sizeof(int)-1; i
++){
91 const int format
= outfmt_list
[i
];
92 int ret
= vf
->priv
->query_format_cache
[i
]-1;
94 ret
= vf_next_query_format(vf
, outfmt_list
[i
]);
95 vf
->priv
->query_format_cache
[i
]= ret
+1;
98 mp_msg(MSGT_VFILTER
,MSGL_DBG2
,"scale: query(%s) -> %d\n",vo_format_name(format
),ret
&3);
99 if(ret
&VFCAP_CSP_SUPPORTED_BY_HW
){
100 best
=format
; // no conversion -> bingo!
103 if(ret
&VFCAP_CSP_SUPPORTED
&& !best
)
104 best
=format
; // best with conversion
109 static int config(struct vf_instance
* vf
,
110 int width
, int height
, int d_width
, int d_height
,
111 unsigned int flags
, unsigned int outfmt
){
112 struct MPOpts
*opts
= vf
->opts
;
113 unsigned int best
=find_best_out(vf
);
116 int round_w
=0, round_h
=0;
118 SwsFilter
*srcFilter
, *dstFilter
;
119 enum PixelFormat dfmt
, sfmt
;
122 mp_msg(MSGT_VFILTER
,MSGL_WARN
,"SwScale: no supported outfmt found :(\n");
125 sfmt
= imgfmt2pixfmt(outfmt
);
126 if (outfmt
== IMGFMT_RGB8
|| outfmt
== IMGFMT_BGR8
) sfmt
= PIX_FMT_PAL8
;
127 dfmt
= imgfmt2pixfmt(best
);
129 vo_flags
=vf
->next
->query_format(vf
->next
,best
);
131 // scaling to dwidth*d_height, if all these TRUE:
133 // - no other sw/hw up/down scaling avail.
134 // - we're after postproc
135 // - user didn't set w:h
136 if(!(vo_flags
&VFCAP_POSTPROC
) && (flags
&4) &&
137 vf
->priv
->w
<0 && vf
->priv
->h
<0){ // -zoom
138 int x
=(vo_flags
&VFCAP_SWSCALE
) ? 0 : 1;
139 if(d_width
<width
|| d_height
<height
){
141 if(vo_flags
&VFCAP_HWSCALE_DOWN
) x
=0;
144 if(vo_flags
&VFCAP_HWSCALE_UP
) x
=0;
147 // user wants sw scaling! (-zoom)
149 vf
->priv
->h
=d_height
;
154 if((vf
->priv
->w
> width
) + (vf
->priv
->h
> height
) >= vf
->priv
->noup
){
160 if (vf
->priv
->w
<= -8) {
164 if (vf
->priv
->h
<= -8) {
169 if (vf
->priv
->w
< -3 || vf
->priv
->h
< -3 ||
170 (vf
->priv
->w
< -1 && vf
->priv
->h
< -1)) {
171 // TODO: establish a direct connection to the user's brain
172 // and find out what the heck he thinks MPlayer should do
173 // with this nonsense.
174 mp_msg(MSGT_VFILTER
, MSGL_ERR
, "SwScale: EUSERBROKEN Check your parameters, they make no sense!\n");
178 if (vf
->priv
->w
== -1)
180 if (vf
->priv
->w
== 0)
181 vf
->priv
->w
= d_width
;
183 if (vf
->priv
->h
== -1)
184 vf
->priv
->h
= height
;
185 if (vf
->priv
->h
== 0)
186 vf
->priv
->h
= d_height
;
188 if (vf
->priv
->w
== -3)
189 vf
->priv
->w
= vf
->priv
->h
* width
/ height
;
190 if (vf
->priv
->w
== -2)
191 vf
->priv
->w
= vf
->priv
->h
* d_width
/ d_height
;
193 if (vf
->priv
->h
== -3)
194 vf
->priv
->h
= vf
->priv
->w
* height
/ width
;
195 if (vf
->priv
->h
== -2)
196 vf
->priv
->h
= vf
->priv
->w
* d_height
/ d_width
;
199 vf
->priv
->w
= ((vf
->priv
->w
+ 8) / 16) * 16;
201 vf
->priv
->h
= ((vf
->priv
->h
+ 8) / 16) * 16;
203 // calculate the missing parameters:
205 case IMGFMT_YV12
: /* YV12 needs w & h rounded to 2 */
210 vf
->priv
->h
= (vf
->priv
->h
+ 1) & ~1;
211 case IMGFMT_YUY2
: /* YUY2 needs w rounded to 2 */
213 vf
->priv
->w
= (vf
->priv
->w
+ 1) & ~1;
216 mp_msg(MSGT_VFILTER
,MSGL_DBG2
,"SwScale: scaling %dx%d %s to %dx%d %s \n",
217 width
,height
,vo_format_name(outfmt
),
218 vf
->priv
->w
,vf
->priv
->h
,vo_format_name(best
));
221 if(vf
->priv
->ctx
) sws_freeContext(vf
->priv
->ctx
);
222 if(vf
->priv
->ctx2
)sws_freeContext(vf
->priv
->ctx2
);
225 sws_getFlagsAndFilterFromCmdLine(&int_sws_flags
, &srcFilter
, &dstFilter
);
226 int_sws_flags
|= vf
->priv
->v_chr_drop
<< SWS_SRC_V_CHR_DROP_SHIFT
;
227 int_sws_flags
|= vf
->priv
->accurate_rnd
* SWS_ACCURATE_RND
;
228 vf
->priv
->ctx
=sws_getContext(width
, height
>> vf
->priv
->interlaced
,
230 vf
->priv
->w
, vf
->priv
->h
>> vf
->priv
->interlaced
,
232 int_sws_flags
| get_sws_cpuflags(), srcFilter
, dstFilter
, vf
->priv
->param
);
233 if(vf
->priv
->interlaced
){
234 vf
->priv
->ctx2
=sws_getContext(width
, height
>> 1,
236 vf
->priv
->w
, vf
->priv
->h
>> 1,
238 int_sws_flags
| get_sws_cpuflags(), srcFilter
, dstFilter
, vf
->priv
->param
);
242 mp_msg(MSGT_VFILTER
,MSGL_WARN
,"Couldn't init SwScaler for this setup\n");
247 if(vf
->priv
->palette
){
248 free(vf
->priv
->palette
);
249 vf
->priv
->palette
=NULL
;
253 /* set 332 palette for 8 bpp */
254 vf
->priv
->palette
=malloc(4*256);
255 for(i
=0; i
<256; i
++){
256 vf
->priv
->palette
[4*i
+0]=4*(i
>>6)*21;
257 vf
->priv
->palette
[4*i
+1]=4*((i
>>3)&7)*9;
258 vf
->priv
->palette
[4*i
+2]=4*((i
&7)&7)*9;
259 vf
->priv
->palette
[4*i
+3]=0;
263 /* set 332 palette for 8 bpp */
264 vf
->priv
->palette
=malloc(4*256);
265 for(i
=0; i
<256; i
++){
266 vf
->priv
->palette
[4*i
+0]=4*(i
&3)*21;
267 vf
->priv
->palette
[4*i
+1]=4*((i
>>2)&7)*9;
268 vf
->priv
->palette
[4*i
+2]=4*((i
>>5)&7)*9;
269 vf
->priv
->palette
[4*i
+3]=0;
274 vf
->priv
->palette
=malloc(4*16);
276 vf
->priv
->palette
[4*i
+0]=4*(i
&1)*63;
277 vf
->priv
->palette
[4*i
+1]=4*((i
>>1)&3)*21;
278 vf
->priv
->palette
[4*i
+2]=4*((i
>>3)&1)*63;
279 vf
->priv
->palette
[4*i
+3]=0;
284 vf
->priv
->palette
=malloc(4*16);
286 vf
->priv
->palette
[4*i
+0]=4*(i
>>3)*63;
287 vf
->priv
->palette
[4*i
+1]=4*((i
>>1)&3)*21;
288 vf
->priv
->palette
[4*i
+2]=4*((i
&1)&1)*63;
289 vf
->priv
->palette
[4*i
+3]=0;
294 if (!opts
->screen_size_x
&& !opts
->screen_size_y
295 && !(opts
->screen_size_xy
>= 0.001)) {
296 // Compute new d_width and d_height, preserving aspect
297 // while ensuring that both are >= output size in pixels.
298 if (vf
->priv
->h
* d_width
> vf
->priv
->w
* d_height
) {
299 d_width
= vf
->priv
->h
* d_width
/ d_height
;
300 d_height
= vf
->priv
->h
;
302 d_height
= vf
->priv
->w
* d_height
/ d_width
;
303 d_width
= vf
->priv
->w
;
305 //d_width=d_width*vf->priv->w/width;
306 //d_height=d_height*vf->priv->h/height;
308 return vf_next_config(vf
,vf
->priv
->w
,vf
->priv
->h
,d_width
,d_height
,flags
,best
);
311 static void start_slice(struct vf_instance
* vf
, mp_image_t
*mpi
){
312 // printf("start_slice called! flag=%d\n",mpi->flags&MP_IMGFLAG_DRAW_CALLBACK);
313 if(!(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
)) return; // shouldn't happen
314 // they want slices!!! allocate the buffer.
315 mpi
->priv
=vf
->dmpi
=vf_get_image(vf
->next
,vf
->priv
->fmt
,
316 // mpi->type, mpi->flags & (~MP_IMGFLAG_DRAW_CALLBACK),
317 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_PREFER_ALIGNED_STRIDE
,
318 vf
->priv
->w
, vf
->priv
->h
);
321 static void scale(struct SwsContext
*sws1
, struct SwsContext
*sws2
, uint8_t *src
[3], int src_stride
[3], int y
, int h
,
322 uint8_t *dst
[3], int dst_stride
[3], int interlaced
){
323 uint8_t *src2
[3]={src
[0], src
[1], src
[2]};
324 #ifdef WORDS_BIGENDIAN
326 if (src
[1] && !src
[2]){
329 pal2
[i
]= bswap_32(((uint32_t*)src
[1])[i
]);
336 uint8_t *dst2
[3]={dst
[0], dst
[1], dst
[2]};
337 int src_stride2
[3]={2*src_stride
[0], 2*src_stride
[1], 2*src_stride
[2]};
338 int dst_stride2
[3]={2*dst_stride
[0], 2*dst_stride
[1], 2*dst_stride
[2]};
340 sws_scale_ordered(sws1
, src2
, src_stride2
, y
>>1, h
>>1, dst2
, dst_stride2
);
342 src2
[i
] += src_stride
[i
];
343 dst2
[i
] += dst_stride
[i
];
345 sws_scale_ordered(sws2
, src2
, src_stride2
, y
>>1, h
>>1, dst2
, dst_stride2
);
347 sws_scale_ordered(sws1
, src2
, src_stride
, y
, h
, dst
, dst_stride
);
351 static void draw_slice(struct vf_instance
* vf
,
352 unsigned char** src
, int* stride
, int w
,int h
, int x
, int y
){
353 mp_image_t
*dmpi
=vf
->dmpi
;
355 mp_msg(MSGT_VFILTER
,MSGL_FATAL
,"vf_scale: draw_slice() called with dmpi=NULL (no get_image?)\n");
358 // printf("vf_scale::draw_slice() y=%d h=%d\n",y,h);
359 scale(vf
->priv
->ctx
, vf
->priv
->ctx2
, src
, stride
, y
, h
, dmpi
->planes
, dmpi
->stride
, vf
->priv
->interlaced
);
362 static int put_image(struct vf_instance
* vf
, mp_image_t
*mpi
, double pts
){
363 mp_image_t
*dmpi
=mpi
->priv
;
365 // printf("vf_scale::put_image(): processing whole frame! dmpi=%p flag=%d\n",
366 // dmpi, (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK));
368 if(!(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
&& dmpi
)){
370 // hope we'll get DR buffer:
371 dmpi
=vf_get_image(vf
->next
,vf
->priv
->fmt
,
372 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_PREFER_ALIGNED_STRIDE
,
373 vf
->priv
->w
, vf
->priv
->h
);
375 scale(vf
->priv
->ctx
, vf
->priv
->ctx
, mpi
->planes
,mpi
->stride
,0,mpi
->h
,dmpi
->planes
,dmpi
->stride
, vf
->priv
->interlaced
);
378 if(vf
->priv
->w
==mpi
->w
&& vf
->priv
->h
==mpi
->h
){
379 // just conversion, no scaling -> keep postprocessing data
380 // this way we can apply pp filter to non-yv12 source using scaler
381 vf_clone_mpi_attributes(dmpi
, mpi
);
384 if(vf
->priv
->palette
) dmpi
->planes
[1]=vf
->priv
->palette
; // export palette!
386 return vf_next_put_image(vf
,dmpi
, pts
);
389 static int control(struct vf_instance
* vf
, int request
, void* data
){
393 int brightness
, contrast
, saturation
, srcRange
, dstRange
;
398 case VFCTRL_GET_EQUALIZER
:
399 r
= sws_getColorspaceDetails(vf
->priv
->ctx
, &inv_table
, &srcRange
, &table
, &dstRange
, &brightness
, &contrast
, &saturation
);
403 if (!strcmp(eq
->item
,"brightness")) {
404 eq
->value
= ((brightness
*100) + (1<<15))>>16;
406 else if (!strcmp(eq
->item
,"contrast")) {
407 eq
->value
= (((contrast
*100) + (1<<15))>>16) - 100;
409 else if (!strcmp(eq
->item
,"saturation")) {
410 eq
->value
= (((saturation
*100) + (1<<15))>>16) - 100;
415 case VFCTRL_SET_EQUALIZER
:
416 r
= sws_getColorspaceDetails(vf
->priv
->ctx
, &inv_table
, &srcRange
, &table
, &dstRange
, &brightness
, &contrast
, &saturation
);
418 //printf("set %f %f %f\n", brightness/(float)(1<<16), contrast/(float)(1<<16), saturation/(float)(1<<16));
421 if (!strcmp(eq
->item
,"brightness")) {
422 brightness
= (( eq
->value
<<16) + 50)/100;
424 else if (!strcmp(eq
->item
,"contrast")) {
425 contrast
= (((eq
->value
+100)<<16) + 50)/100;
427 else if (!strcmp(eq
->item
,"saturation")) {
428 saturation
= (((eq
->value
+100)<<16) + 50)/100;
433 r
= sws_setColorspaceDetails(vf
->priv
->ctx
, inv_table
, srcRange
, table
, dstRange
, brightness
, contrast
, saturation
);
436 r
= sws_setColorspaceDetails(vf
->priv
->ctx2
, inv_table
, srcRange
, table
, dstRange
, brightness
, contrast
, saturation
);
445 return vf_next_control(vf
,request
,data
);
448 //===========================================================================//
450 // supported Input formats: YV12, I420, IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8, Y800
452 static int query_format(struct vf_instance
* vf
, unsigned int fmt
){
477 unsigned int best
=find_best_out(vf
);
479 if(!best
) return 0; // no matching out-fmt
480 flags
=vf_next_query_format(vf
,best
);
481 if(!(flags
&(VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
))) return 0; // huh?
482 if(fmt
!=best
) flags
&=~VFCAP_CSP_SUPPORTED_BY_HW
;
483 // do not allow scaling, if we are before the PP fliter!
484 if(!(flags
&VFCAP_POSTPROC
)) flags
|=VFCAP_SWSCALE
;
488 return 0; // nomatching in-fmt
491 static void uninit(struct vf_instance
*vf
){
492 if(vf
->priv
->ctx
) sws_freeContext(vf
->priv
->ctx
);
493 if(vf
->priv
->ctx2
) sws_freeContext(vf
->priv
->ctx2
);
494 if(vf
->priv
->palette
) free(vf
->priv
->palette
);
498 static int open(vf_instance_t
*vf
, char* args
){
500 vf
->start_slice
=start_slice
;
501 vf
->draw_slice
=draw_slice
;
502 vf
->put_image
=put_image
;
503 vf
->query_format
=query_format
;
504 vf
->control
= control
;
507 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
508 // TODO: parse args ->
513 vf
->priv
->v_chr_drop
=0;
514 vf
->priv
->accurate_rnd
=0;
516 vf
->priv
->param
[1]=SWS_PARAM_DEFAULT
;
517 vf
->priv
->palette
=NULL
;
519 if(args
) sscanf(args
, "%d:%d:%d:%lf:%lf",
522 &vf
->priv
->v_chr_drop
,
524 &vf
->priv
->param
[1]);
525 mp_msg(MSGT_VFILTER
,MSGL_V
,"SwScale params: %d x %d (-1=no scaling)\n",
532 //global sws_flags from the command line
536 static SwsFilter
*src_filter
= NULL
;
538 float sws_lum_gblur
= 0.0;
539 float sws_chr_gblur
= 0.0;
540 int sws_chr_vshift
= 0;
541 int sws_chr_hshift
= 0;
542 float sws_chr_sharpen
= 0.0;
543 float sws_lum_sharpen
= 0.0;
545 int get_sws_cpuflags(void){
547 (gCpuCaps
.hasMMX
? SWS_CPU_CAPS_MMX
: 0)
548 | (gCpuCaps
.hasMMX2
? SWS_CPU_CAPS_MMX2
: 0)
549 | (gCpuCaps
.has3DNow
? SWS_CPU_CAPS_3DNOW
: 0)
550 | (gCpuCaps
.hasAltiVec
? SWS_CPU_CAPS_ALTIVEC
: 0);
553 void sws_getFlagsAndFilterFromCmdLine(int *flags
, SwsFilter
**srcFilterParam
, SwsFilter
**dstFilterParam
)
555 static int firstTime
=1;
560 __asm__
volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
565 *flags
= SWS_PRINT_INFO
;
567 else if( mp_msg_test(MSGT_VFILTER
,MSGL_DBG2
) ) *flags
= SWS_PRINT_INFO
;
569 if(src_filter
) sws_freeFilter(src_filter
);
571 src_filter
= sws_getDefaultFilter(
572 sws_lum_gblur
, sws_chr_gblur
,
573 sws_lum_sharpen
, sws_chr_sharpen
,
574 sws_chr_hshift
, sws_chr_vshift
, verbose
>1);
578 case 0: *flags
|= SWS_FAST_BILINEAR
; break;
579 case 1: *flags
|= SWS_BILINEAR
; break;
580 case 2: *flags
|= SWS_BICUBIC
; break;
581 case 3: *flags
|= SWS_X
; break;
582 case 4: *flags
|= SWS_POINT
; break;
583 case 5: *flags
|= SWS_AREA
; break;
584 case 6: *flags
|= SWS_BICUBLIN
; break;
585 case 7: *flags
|= SWS_GAUSS
; break;
586 case 8: *flags
|= SWS_SINC
; break;
587 case 9: *flags
|= SWS_LANCZOS
; break;
588 case 10:*flags
|= SWS_SPLINE
; break;
589 default:*flags
|= SWS_BILINEAR
; break;
592 *srcFilterParam
= src_filter
;
593 *dstFilterParam
= NULL
;
596 // will use sws_flags & src_filter (from cmd line)
597 struct SwsContext
*sws_getContextFromCmdLine(int srcW
, int srcH
, int srcFormat
, int dstW
, int dstH
, int dstFormat
)
600 SwsFilter
*dstFilterParam
, *srcFilterParam
;
601 enum PixelFormat dfmt
, sfmt
;
603 dfmt
= imgfmt2pixfmt(dstFormat
);
604 sfmt
= imgfmt2pixfmt(srcFormat
);
605 if (srcFormat
== IMGFMT_RGB8
|| srcFormat
== IMGFMT_BGR8
) sfmt
= PIX_FMT_PAL8
;
606 sws_getFlagsAndFilterFromCmdLine(&flags
, &srcFilterParam
, &dstFilterParam
);
608 return sws_getContext(srcW
, srcH
, sfmt
, dstW
, dstH
, dfmt
, flags
| get_sws_cpuflags(), srcFilterParam
, dstFilterParam
, NULL
);
611 /// An example of presets usage
612 static const struct size_preset
{
615 } vf_size_presets_defs
[] = {
616 // TODO add more 'standard' resolutions
617 { "qntsc", 352, 240 },
618 { "qpal", 352, 288 },
619 { "ntsc", 720, 480 },
621 { "sntsc", 640, 480 },
622 { "spal", 768, 576 },
626 #define ST_OFF(f) M_ST_OFF(struct size_preset,f)
627 static const m_option_t vf_size_preset_fields
[] = {
628 {"w", ST_OFF(w
), CONF_TYPE_INT
, M_OPT_MIN
,1 ,0, NULL
},
629 {"h", ST_OFF(h
), CONF_TYPE_INT
, M_OPT_MIN
,1 ,0, NULL
},
630 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
633 static const m_struct_t vf_size_preset
= {
635 sizeof(struct size_preset
),
637 vf_size_preset_fields
640 static const m_struct_t vf_opts
;
641 static const m_obj_presets_t size_preset
= {
642 &vf_size_preset
, // Input struct desc
643 &vf_opts
, // Output struct desc
644 vf_size_presets_defs
, // The list of presets
645 ST_OFF(name
) // At wich offset is the name field in the preset struct
650 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
651 static const m_option_t vf_opts_fields
[] = {
652 {"w", ST_OFF(w
), CONF_TYPE_INT
, M_OPT_MIN
,-11,0, NULL
},
653 {"h", ST_OFF(h
), CONF_TYPE_INT
, M_OPT_MIN
,-11,0, NULL
},
654 {"interlaced", ST_OFF(interlaced
), CONF_TYPE_INT
, M_OPT_RANGE
, 0, 1, NULL
},
655 {"chr-drop", ST_OFF(v_chr_drop
), CONF_TYPE_INT
, M_OPT_RANGE
, 0, 3, NULL
},
656 {"param" , ST_OFF(param
[0]), CONF_TYPE_DOUBLE
, M_OPT_RANGE
, 0.0, 100.0, NULL
},
657 {"param2", ST_OFF(param
[1]), CONF_TYPE_DOUBLE
, M_OPT_RANGE
, 0.0, 100.0, NULL
},
658 // Note that here the 2 field is NULL (ie 0)
659 // As we want this option to act on the option struct itself
660 {"presize", 0, CONF_TYPE_OBJ_PRESETS
, 0, 0, 0, &size_preset
},
661 {"noup", ST_OFF(noup
), CONF_TYPE_INT
, M_OPT_RANGE
, 0, 2, NULL
},
662 {"arnd", ST_OFF(accurate_rnd
), CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
663 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
666 static const m_struct_t vf_opts
= {
668 sizeof(struct vf_priv_s
),
673 const vf_info_t vf_info_scale
= {
682 //===========================================================================//