vf_dsize, vf_scale: fix behavior on multiple config() calls
[mplayer.git] / libmpcodecs / vf_scale.c
blob585ef4d9a11d5c67b56d8e8b2a3dbd42b8cdb2a0
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
24 #include "config.h"
25 #include "mp_msg.h"
26 #include "cpudetect.h"
27 #include "options.h"
29 #include "img_format.h"
30 #include "mp_image.h"
31 #include "vf.h"
32 #include "fmt-conversion.h"
33 #include "mpbswap.h"
35 #include "libswscale/swscale.h"
36 #include "vf_scale.h"
38 #include "libvo/csputils.h"
39 // VOFLAG_SWSCALE
40 #include "libvo/video_out.h"
42 #include "m_option.h"
43 #include "m_struct.h"
45 static struct vf_priv_s {
46 int w,h;
47 int cfg_w, cfg_h;
48 int v_chr_drop;
49 double param[2];
50 unsigned int fmt;
51 struct SwsContext *ctx;
52 struct SwsContext *ctx2; //for interlaced slices only
53 unsigned char* palette;
54 int interlaced;
55 int noup;
56 int accurate_rnd;
57 struct mp_csp_details colorspace;
58 } const vf_priv_dflt = {
59 0, 0,
60 -1,-1,
62 {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT},
64 NULL,
65 NULL,
66 NULL
69 //===========================================================================//
71 void sws_getFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam);
73 static const unsigned int outfmt_list[]={
74 // YUV:
75 IMGFMT_444P,
76 IMGFMT_444P16_LE,
77 IMGFMT_444P16_BE,
78 IMGFMT_444P10_LE,
79 IMGFMT_444P10_BE,
80 IMGFMT_444P9_LE,
81 IMGFMT_444P9_BE,
82 IMGFMT_422P,
83 IMGFMT_422P16_LE,
84 IMGFMT_422P16_BE,
85 IMGFMT_422P10_LE,
86 IMGFMT_422P10_BE,
87 IMGFMT_YV12,
88 IMGFMT_I420,
89 IMGFMT_420P16_LE,
90 IMGFMT_420P16_BE,
91 IMGFMT_420P10_LE,
92 IMGFMT_420P10_BE,
93 IMGFMT_420P9_LE,
94 IMGFMT_420P9_BE,
95 IMGFMT_420A,
96 IMGFMT_IYUV,
97 IMGFMT_YVU9,
98 IMGFMT_IF09,
99 IMGFMT_411P,
100 IMGFMT_NV12,
101 IMGFMT_NV21,
102 IMGFMT_YUY2,
103 IMGFMT_UYVY,
104 IMGFMT_440P,
105 // RGB and grayscale (Y8 and Y800):
106 IMGFMT_BGR32,
107 IMGFMT_RGB32,
108 IMGFMT_BGR24,
109 IMGFMT_RGB24,
110 IMGFMT_RGB48LE,
111 IMGFMT_RGB48BE,
112 IMGFMT_BGR16,
113 IMGFMT_RGB16,
114 IMGFMT_BGR15,
115 IMGFMT_RGB15,
116 IMGFMT_BGR12,
117 IMGFMT_RGB12,
118 IMGFMT_Y800,
119 IMGFMT_Y8,
120 IMGFMT_BGR8,
121 IMGFMT_RGB8,
122 IMGFMT_BGR4,
123 IMGFMT_RGB4,
124 IMGFMT_BG4B,
125 IMGFMT_RG4B,
126 IMGFMT_BGR1,
127 IMGFMT_RGB1,
132 * A list of preferred conversions, in order of preference.
133 * This should be used for conversions that e.g. involve no scaling
134 * or to stop vf_scale from choosing a conversion that has no
135 * fast assembler implementation.
137 static int preferred_conversions[][2] = {
138 {IMGFMT_YUY2, IMGFMT_UYVY},
139 {IMGFMT_YUY2, IMGFMT_422P},
140 {IMGFMT_UYVY, IMGFMT_YUY2},
141 {IMGFMT_UYVY, IMGFMT_422P},
142 {IMGFMT_422P, IMGFMT_YUY2},
143 {IMGFMT_422P, IMGFMT_UYVY},
144 {0, 0}
147 static unsigned int find_best_out(vf_instance_t *vf, int in_format){
148 unsigned int best=0;
149 int i = -1;
150 int j = -1;
151 int format = 0;
153 // find the best outfmt:
154 while (1) {
155 int ret;
156 if (j < 0) {
157 format = in_format;
158 j = 0;
159 } else if (i < 0) {
160 while (preferred_conversions[j][0] &&
161 preferred_conversions[j][0] != in_format)
162 j++;
163 format = preferred_conversions[j++][1];
164 // switch to standard list
165 if (!format)
166 i = 0;
168 if (i >= 0)
169 format = outfmt_list[i++];
170 if (!format)
171 break;
172 ret = vf_next_query_format(vf, format);
174 mp_msg(MSGT_VFILTER,MSGL_DBG2,"scale: query(%s) -> %d\n",vo_format_name(format),ret&3);
175 if(ret&VFCAP_CSP_SUPPORTED_BY_HW){
176 best=format; // no conversion -> bingo!
177 break;
179 if(ret&VFCAP_CSP_SUPPORTED && !best)
180 best=format; // best with conversion
182 return best;
185 static int config(struct vf_instance *vf,
186 int width, int height, int d_width, int d_height,
187 unsigned int flags, unsigned int outfmt){
188 struct MPOpts *opts = vf->opts;
189 unsigned int best=find_best_out(vf, outfmt);
190 int vo_flags;
191 int int_sws_flags=0;
192 int round_w=0, round_h=0;
193 int i;
194 SwsFilter *srcFilter, *dstFilter;
195 enum PixelFormat dfmt, sfmt;
197 vf->priv->colorspace = (struct mp_csp_details) {0};
199 if(!best){
200 mp_msg(MSGT_VFILTER,MSGL_WARN,"SwScale: no supported outfmt found :(\n");
201 return 0;
203 sfmt = imgfmt2pixfmt(outfmt);
204 if (outfmt == IMGFMT_RGB8 || outfmt == IMGFMT_BGR8) sfmt = PIX_FMT_PAL8;
205 dfmt = imgfmt2pixfmt(best);
207 vo_flags=vf->next->query_format(vf->next,best);
209 vf->priv->w = vf->priv->cfg_w;
210 vf->priv->h = vf->priv->cfg_h;
212 // scaling to dwidth*d_height, if all these TRUE:
213 // - option -zoom
214 // - no other sw/hw up/down scaling avail.
215 // - we're after postproc
216 // - user didn't set w:h
217 if(!(vo_flags&VFCAP_POSTPROC) && (flags&VOFLAG_SWSCALE) &&
218 vf->priv->w<0 && vf->priv->h<0){ // -zoom
219 int x=(vo_flags&VFCAP_SWSCALE) ? 0 : 1;
220 if(d_width<width || d_height<height){
221 // downscale!
222 if(vo_flags&VFCAP_HWSCALE_DOWN) x=0;
223 } else {
224 // upscale:
225 if(vo_flags&VFCAP_HWSCALE_UP) x=0;
227 if(x){
228 // user wants sw scaling! (-zoom)
229 vf->priv->w=d_width;
230 vf->priv->h=d_height;
234 if(vf->priv->noup){
235 if((vf->priv->w > width) + (vf->priv->h > height) >= vf->priv->noup){
236 vf->priv->w= width;
237 vf->priv->h= height;
241 if (vf->priv->w <= -8) {
242 vf->priv->w += 8;
243 round_w = 1;
245 if (vf->priv->h <= -8) {
246 vf->priv->h += 8;
247 round_h = 1;
250 if (vf->priv->w < -3 || vf->priv->h < -3 ||
251 (vf->priv->w < -1 && vf->priv->h < -1)) {
252 // TODO: establish a direct connection to the user's brain
253 // and find out what the heck he thinks MPlayer should do
254 // with this nonsense.
255 mp_msg(MSGT_VFILTER, MSGL_ERR, "SwScale: EUSERBROKEN Check your parameters, they make no sense!\n");
256 return 0;
259 if (vf->priv->w == -1)
260 vf->priv->w = width;
261 if (vf->priv->w == 0)
262 vf->priv->w = d_width;
264 if (vf->priv->h == -1)
265 vf->priv->h = height;
266 if (vf->priv->h == 0)
267 vf->priv->h = d_height;
269 if (vf->priv->w == -3)
270 vf->priv->w = vf->priv->h * width / height;
271 if (vf->priv->w == -2)
272 vf->priv->w = vf->priv->h * d_width / d_height;
274 if (vf->priv->h == -3)
275 vf->priv->h = vf->priv->w * height / width;
276 if (vf->priv->h == -2)
277 vf->priv->h = vf->priv->w * d_height / d_width;
279 if (round_w)
280 vf->priv->w = ((vf->priv->w + 8) / 16) * 16;
281 if (round_h)
282 vf->priv->h = ((vf->priv->h + 8) / 16) * 16;
284 // calculate the missing parameters:
285 switch(best) {
286 case IMGFMT_YV12: /* YV12 needs w & h rounded to 2 */
287 case IMGFMT_I420:
288 case IMGFMT_IYUV:
289 case IMGFMT_NV12:
290 case IMGFMT_NV21:
291 vf->priv->h = (vf->priv->h + 1) & ~1;
292 case IMGFMT_YUY2: /* YUY2 needs w rounded to 2 */
293 case IMGFMT_UYVY:
294 vf->priv->w = (vf->priv->w + 1) & ~1;
297 mp_msg(MSGT_VFILTER,MSGL_DBG2,"SwScale: scaling %dx%d %s to %dx%d %s \n",
298 width,height,vo_format_name(outfmt),
299 vf->priv->w,vf->priv->h,vo_format_name(best));
301 // free old ctx:
302 if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
303 if(vf->priv->ctx2)sws_freeContext(vf->priv->ctx2);
305 // new swscaler:
306 sws_getFlagsAndFilterFromCmdLine(&int_sws_flags, &srcFilter, &dstFilter);
307 int_sws_flags|= vf->priv->v_chr_drop << SWS_SRC_V_CHR_DROP_SHIFT;
308 int_sws_flags|= vf->priv->accurate_rnd * SWS_ACCURATE_RND;
309 vf->priv->ctx=sws_getContext(width, height >> vf->priv->interlaced,
310 sfmt,
311 vf->priv->w, vf->priv->h >> vf->priv->interlaced,
312 dfmt,
313 int_sws_flags | get_sws_cpuflags(), srcFilter, dstFilter, vf->priv->param);
314 if(vf->priv->interlaced){
315 vf->priv->ctx2=sws_getContext(width, height >> 1,
316 sfmt,
317 vf->priv->w, vf->priv->h >> 1,
318 dfmt,
319 int_sws_flags | get_sws_cpuflags(), srcFilter, dstFilter, vf->priv->param);
321 if(!vf->priv->ctx){
322 // error...
323 mp_msg(MSGT_VFILTER,MSGL_WARN,"Couldn't init SwScaler for this setup\n");
324 return 0;
326 vf->priv->fmt=best;
328 free(vf->priv->palette);
329 vf->priv->palette=NULL;
330 switch(best){
331 case IMGFMT_RGB8: {
332 /* set 332 palette for 8 bpp */
333 vf->priv->palette=malloc(4*256);
334 for(i=0; i<256; i++){
335 vf->priv->palette[4*i+0]=4*(i>>6)*21;
336 vf->priv->palette[4*i+1]=4*((i>>3)&7)*9;
337 vf->priv->palette[4*i+2]=4*((i&7)&7)*9;
338 vf->priv->palette[4*i+3]=0;
340 break; }
341 case IMGFMT_BGR8: {
342 /* set 332 palette for 8 bpp */
343 vf->priv->palette=malloc(4*256);
344 for(i=0; i<256; i++){
345 vf->priv->palette[4*i+0]=4*(i&3)*21;
346 vf->priv->palette[4*i+1]=4*((i>>2)&7)*9;
347 vf->priv->palette[4*i+2]=4*((i>>5)&7)*9;
348 vf->priv->palette[4*i+3]=0;
350 break; }
351 case IMGFMT_BGR4:
352 case IMGFMT_BG4B: {
353 vf->priv->palette=malloc(4*16);
354 for(i=0; i<16; i++){
355 vf->priv->palette[4*i+0]=4*(i&1)*63;
356 vf->priv->palette[4*i+1]=4*((i>>1)&3)*21;
357 vf->priv->palette[4*i+2]=4*((i>>3)&1)*63;
358 vf->priv->palette[4*i+3]=0;
360 break; }
361 case IMGFMT_RGB4:
362 case IMGFMT_RG4B: {
363 vf->priv->palette=malloc(4*16);
364 for(i=0; i<16; i++){
365 vf->priv->palette[4*i+0]=4*(i>>3)*63;
366 vf->priv->palette[4*i+1]=4*((i>>1)&3)*21;
367 vf->priv->palette[4*i+2]=4*((i&1)&1)*63;
368 vf->priv->palette[4*i+3]=0;
370 break; }
373 if (!opts->screen_size_x && !opts->screen_size_y
374 && !(opts->screen_size_xy >= 0.001)) {
375 // Compute new d_width and d_height, preserving aspect
376 // while ensuring that both are >= output size in pixels.
377 if (vf->priv->h * d_width > vf->priv->w * d_height) {
378 d_width = vf->priv->h * d_width / d_height;
379 d_height = vf->priv->h;
380 } else {
381 d_height = vf->priv->w * d_height / d_width;
382 d_width = vf->priv->w;
384 //d_width=d_width*vf->priv->w/width;
385 //d_height=d_height*vf->priv->h/height;
387 return vf_next_config(vf,vf->priv->w,vf->priv->h,d_width,d_height,flags,best);
390 static void start_slice(struct vf_instance *vf, mp_image_t *mpi){
391 // printf("start_slice called! flag=%d\n",mpi->flags&MP_IMGFLAG_DRAW_CALLBACK);
392 if(!(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)) return; // shouldn't happen
393 // they want slices!!! allocate the buffer.
394 mpi->priv=vf->dmpi=vf_get_image(vf->next,vf->priv->fmt,
395 // mpi->type, mpi->flags & (~MP_IMGFLAG_DRAW_CALLBACK),
396 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
397 vf->priv->w, vf->priv->h);
400 static void scale(struct SwsContext *sws1, struct SwsContext *sws2, uint8_t *src[MP_MAX_PLANES], int src_stride[MP_MAX_PLANES],
401 int y, int h, uint8_t *dst[MP_MAX_PLANES], int dst_stride[MP_MAX_PLANES], int interlaced){
402 const uint8_t *src2[MP_MAX_PLANES]={src[0], src[1], src[2], src[3]};
403 #if HAVE_BIGENDIAN
404 uint32_t pal2[256];
405 if (src[1] && !src[2]){
406 int i;
407 for(i=0; i<256; i++)
408 pal2[i]= bswap_32(((uint32_t*)src[1])[i]);
409 src2[1]= pal2;
411 #endif
413 if(interlaced){
414 int i;
415 uint8_t *dst2[MP_MAX_PLANES]={dst[0], dst[1], dst[2], dst[3]};
416 int src_stride2[MP_MAX_PLANES]={2*src_stride[0], 2*src_stride[1], 2*src_stride[2], 2*src_stride[3]};
417 int dst_stride2[MP_MAX_PLANES]={2*dst_stride[0], 2*dst_stride[1], 2*dst_stride[2], 2*dst_stride[3]};
419 sws_scale(sws1, src2, src_stride2, y>>1, h>>1, dst2, dst_stride2);
420 for(i=0; i<MP_MAX_PLANES; i++){
421 src2[i] += src_stride[i];
422 dst2[i] += dst_stride[i];
424 sws_scale(sws2, src2, src_stride2, y>>1, h>>1, dst2, dst_stride2);
425 }else{
426 sws_scale(sws1, src2, src_stride, y, h, dst, dst_stride);
430 static void draw_slice(struct vf_instance *vf,
431 unsigned char** src, int* stride, int w,int h, int x, int y){
432 mp_image_t *dmpi=vf->dmpi;
433 if(!dmpi){
434 mp_msg(MSGT_VFILTER,MSGL_FATAL,"vf_scale: draw_slice() called with dmpi=NULL (no get_image?)\n");
435 return;
437 // printf("vf_scale::draw_slice() y=%d h=%d\n",y,h);
438 scale(vf->priv->ctx, vf->priv->ctx2, src, stride, y, h, dmpi->planes, dmpi->stride, vf->priv->interlaced);
441 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
442 mp_image_t *dmpi=mpi->priv;
444 // printf("vf_scale::put_image(): processing whole frame! dmpi=%p flag=%d\n",
445 // dmpi, (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK));
447 if(!(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK && dmpi)){
449 // hope we'll get DR buffer:
450 dmpi=vf_get_image(vf->next,vf->priv->fmt,
451 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
452 vf->priv->w, vf->priv->h);
454 scale(vf->priv->ctx, vf->priv->ctx, mpi->planes,mpi->stride,0,mpi->h,dmpi->planes,dmpi->stride, vf->priv->interlaced);
457 if(vf->priv->w==mpi->w && vf->priv->h==mpi->h){
458 // just conversion, no scaling -> keep postprocessing data
459 // this way we can apply pp filter to non-yv12 source using scaler
460 vf_clone_mpi_attributes(dmpi, mpi);
463 if(vf->priv->palette) dmpi->planes[1]=vf->priv->palette; // export palette!
465 return vf_next_put_image(vf,dmpi, pts);
468 static int control(struct vf_instance *vf, int request, void* data){
469 int *table;
470 int *inv_table;
471 int r;
472 int brightness, contrast, saturation, srcRange, dstRange;
473 vf_equalizer_t *eq;
475 if(vf->priv->ctx)
476 switch(request){
477 case VFCTRL_GET_EQUALIZER:
478 r= sws_getColorspaceDetails(vf->priv->ctx, &inv_table, &srcRange, &table, &dstRange, &brightness, &contrast, &saturation);
479 if(r<0) break;
481 eq = data;
482 if (!strcmp(eq->item,"brightness")) {
483 eq->value = ((brightness*100) + (1<<15))>>16;
485 else if (!strcmp(eq->item,"contrast")) {
486 eq->value = (((contrast *100) + (1<<15))>>16) - 100;
488 else if (!strcmp(eq->item,"saturation")) {
489 eq->value = (((saturation*100) + (1<<15))>>16) - 100;
491 else
492 break;
493 return CONTROL_TRUE;
494 case VFCTRL_SET_EQUALIZER:
495 r= sws_getColorspaceDetails(vf->priv->ctx, &inv_table, &srcRange, &table, &dstRange, &brightness, &contrast, &saturation);
496 if(r<0) break;
497 //printf("set %f %f %f\n", brightness/(float)(1<<16), contrast/(float)(1<<16), saturation/(float)(1<<16));
498 eq = data;
500 if (!strcmp(eq->item,"brightness")) {
501 brightness = (( eq->value <<16) + 50)/100;
503 else if (!strcmp(eq->item,"contrast")) {
504 contrast = (((eq->value+100)<<16) + 50)/100;
506 else if (!strcmp(eq->item,"saturation")) {
507 saturation = (((eq->value+100)<<16) + 50)/100;
509 else
510 break;
512 r= sws_setColorspaceDetails(vf->priv->ctx, inv_table, srcRange, table, dstRange, brightness, contrast, saturation);
513 if(r<0) break;
514 if(vf->priv->ctx2){
515 r= sws_setColorspaceDetails(vf->priv->ctx2, inv_table, srcRange, table, dstRange, brightness, contrast, saturation);
516 if(r<0) break;
519 return CONTROL_TRUE;
520 case VFCTRL_SET_YUV_COLORSPACE: {
521 struct mp_csp_details colorspace = *(struct mp_csp_details *)data;
522 if (mp_sws_set_colorspace(vf->priv->ctx, &colorspace) >= 0) {
523 if (vf->priv->ctx2)
524 mp_sws_set_colorspace(vf->priv->ctx2, &colorspace);
525 vf->priv->colorspace = colorspace;
526 return 1;
528 break;
530 case VFCTRL_GET_YUV_COLORSPACE: {
531 /* This scale filter should never react to colorspace commands if it
532 * doesn't do YUV->RGB conversion. But because finding out whether this
533 * is really YUV->RGB (and not YUV->YUV or anything else) is hard,
534 * react only if the colorspace has been set explicitly before. The
535 * trick is that mp_sws_set_colorspace does not succeed for YUV->YUV
536 * and RGB->YUV conversions, which makes this code correct in "most"
537 * cases. (This would be trivial to do correctly if libswscale exposed
538 * functionality like isYUV()).
540 if (vf->priv->colorspace.format) {
541 *(struct mp_csp_details *)data = vf->priv->colorspace;
542 return CONTROL_TRUE;
544 break;
546 default:
547 break;
550 return vf_next_control(vf,request,data);
553 static const int mp_csp_to_swscale[MP_CSP_COUNT] = {
554 [MP_CSP_BT_601] = SWS_CS_ITU601,
555 [MP_CSP_BT_709] = SWS_CS_ITU709,
556 [MP_CSP_SMPTE_240M] = SWS_CS_SMPTE240M,
559 // Adjust the colorspace used for YUV->RGB conversion. On other conversions,
560 // do nothing or return an error.
561 // The csp argument is set to the supported values.
562 // Return 0 on success and -1 on error.
563 int mp_sws_set_colorspace(struct SwsContext *sws, struct mp_csp_details *csp)
565 int *table, *inv_table;
566 int brightness, contrast, saturation, srcRange, dstRange;
568 csp->levels_out = MP_CSP_LEVELS_PC;
570 // NOTE: returns an error if the destination format is YUV
571 if (sws_getColorspaceDetails(sws, &inv_table, &srcRange, &table, &dstRange,
572 &brightness, &contrast, &saturation) == -1)
573 goto error_out;
575 int sws_csp = mp_csp_to_swscale[csp->format];
576 if (sws_csp == 0) {
577 // colorspace not supported, go with a reasonable default
578 csp->format = SWS_CS_ITU601;
579 sws_csp = MP_CSP_BT_601;
582 /* The swscale API for these is hardly documented.
583 * Apparently table/range only apply to YUV. Thus dstRange has no effect
584 * for YUV->RGB conversions, and conversions to limited-range RGB are
585 * not supported.
587 srcRange = csp->levels_in == MP_CSP_LEVELS_PC;
588 const int *new_inv_table = sws_getCoefficients(sws_csp);
590 if (sws_setColorspaceDetails(sws, new_inv_table, srcRange, table, dstRange,
591 brightness, contrast, saturation) == -1)
592 goto error_out;
594 return 0;
596 error_out:
597 *csp = (struct mp_csp_details){0};
598 return -1;
601 //===========================================================================//
603 // supported Input formats: YV12, I420, IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8, Y800
605 static int query_format(struct vf_instance *vf, unsigned int fmt){
606 if (!IMGFMT_IS_HWACCEL(fmt) && imgfmt2pixfmt(fmt) != PIX_FMT_NONE) {
607 unsigned int best=find_best_out(vf, fmt);
608 int flags;
609 if(!best) return 0; // no matching out-fmt
610 flags=vf_next_query_format(vf,best);
611 if(!(flags&(VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW))) return 0; // huh?
612 if(fmt!=best) flags&=~VFCAP_CSP_SUPPORTED_BY_HW;
613 // do not allow scaling, if we are before the PP fliter!
614 if(!(flags&VFCAP_POSTPROC)) flags|=VFCAP_SWSCALE;
615 return flags;
617 return 0; // nomatching in-fmt
620 static void uninit(struct vf_instance *vf){
621 if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
622 if(vf->priv->ctx2) sws_freeContext(vf->priv->ctx2);
623 free(vf->priv->palette);
624 free(vf->priv);
627 static int vf_open(vf_instance_t *vf, char *args){
628 vf->config=config;
629 vf->start_slice=start_slice;
630 vf->draw_slice=draw_slice;
631 vf->put_image=put_image;
632 vf->query_format=query_format;
633 vf->control= control;
634 vf->uninit=uninit;
635 mp_msg(MSGT_VFILTER,MSGL_V,"SwScale params: %d x %d (-1=no scaling)\n",
636 vf->priv->cfg_w,
637 vf->priv->cfg_h);
639 return 1;
642 //global sws_flags from the command line
643 int sws_flags=2;
645 //global srcFilter
646 static SwsFilter *src_filter= NULL;
648 float sws_lum_gblur= 0.0;
649 float sws_chr_gblur= 0.0;
650 int sws_chr_vshift= 0;
651 int sws_chr_hshift= 0;
652 float sws_chr_sharpen= 0.0;
653 float sws_lum_sharpen= 0.0;
655 int get_sws_cpuflags(void){
656 return
657 (gCpuCaps.hasMMX ? SWS_CPU_CAPS_MMX : 0)
658 | (gCpuCaps.hasMMX2 ? SWS_CPU_CAPS_MMX2 : 0)
659 | (gCpuCaps.has3DNow ? SWS_CPU_CAPS_3DNOW : 0)
660 | (gCpuCaps.hasAltiVec ? SWS_CPU_CAPS_ALTIVEC : 0);
663 void sws_getFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam)
665 static int firstTime=1;
666 *flags=0;
668 #if ARCH_X86
669 if(gCpuCaps.hasMMX)
670 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
671 #endif
672 if(firstTime)
674 firstTime=0;
675 *flags= SWS_PRINT_INFO;
677 else if( mp_msg_test(MSGT_VFILTER,MSGL_DBG2) ) *flags= SWS_PRINT_INFO;
679 if(src_filter) sws_freeFilter(src_filter);
681 src_filter= sws_getDefaultFilter(
682 sws_lum_gblur, sws_chr_gblur,
683 sws_lum_sharpen, sws_chr_sharpen,
684 sws_chr_hshift, sws_chr_vshift, verbose>1);
686 switch(sws_flags)
688 case 0: *flags|= SWS_FAST_BILINEAR; break;
689 case 1: *flags|= SWS_BILINEAR; break;
690 case 2: *flags|= SWS_BICUBIC; break;
691 case 3: *flags|= SWS_X; break;
692 case 4: *flags|= SWS_POINT; break;
693 case 5: *flags|= SWS_AREA; break;
694 case 6: *flags|= SWS_BICUBLIN; break;
695 case 7: *flags|= SWS_GAUSS; break;
696 case 8: *flags|= SWS_SINC; break;
697 case 9: *flags|= SWS_LANCZOS; break;
698 case 10:*flags|= SWS_SPLINE; break;
699 default:*flags|= SWS_BILINEAR; break;
702 *srcFilterParam= src_filter;
703 *dstFilterParam= NULL;
706 // will use sws_flags & src_filter (from cmd line)
707 struct SwsContext *sws_getContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
709 int flags;
710 SwsFilter *dstFilterParam, *srcFilterParam;
711 enum PixelFormat dfmt, sfmt;
713 dfmt = imgfmt2pixfmt(dstFormat);
714 sfmt = imgfmt2pixfmt(srcFormat);
715 if (srcFormat == IMGFMT_RGB8 || srcFormat == IMGFMT_BGR8) sfmt = PIX_FMT_PAL8;
716 sws_getFlagsAndFilterFromCmdLine(&flags, &srcFilterParam, &dstFilterParam);
718 return sws_getContext(srcW, srcH, sfmt, dstW, dstH, dfmt, flags | get_sws_cpuflags(), srcFilterParam, dstFilterParam, NULL);
721 /// An example of presets usage
722 static const struct size_preset {
723 char* name;
724 int w, h;
725 } vf_size_presets_defs[] = {
726 // TODO add more 'standard' resolutions
727 { "qntsc", 352, 240 },
728 { "qpal", 352, 288 },
729 { "ntsc", 720, 480 },
730 { "pal", 720, 576 },
731 { "sntsc", 640, 480 },
732 { "spal", 768, 576 },
733 { NULL, 0, 0}
736 #define ST_OFF(f) M_ST_OFF(struct size_preset,f)
737 static const m_option_t vf_size_preset_fields[] = {
738 {"w", ST_OFF(w), CONF_TYPE_INT, M_OPT_MIN,1 ,0, NULL},
739 {"h", ST_OFF(h), CONF_TYPE_INT, M_OPT_MIN,1 ,0, NULL},
740 { NULL, NULL, 0, 0, 0, 0, NULL }
743 static const m_struct_t vf_size_preset = {
744 "scale_size_preset",
745 sizeof(struct size_preset),
746 NULL,
747 vf_size_preset_fields
750 static const m_struct_t vf_opts;
751 static const m_obj_presets_t size_preset = {
752 &vf_size_preset, // Input struct desc
753 &vf_opts, // Output struct desc
754 vf_size_presets_defs, // The list of presets
755 ST_OFF(name) // At wich offset is the name field in the preset struct
758 /// Now the options
759 #undef ST_OFF
760 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
761 static const m_option_t vf_opts_fields[] = {
762 {"w", ST_OFF(cfg_w), CONF_TYPE_INT, M_OPT_MIN,-11,0, NULL},
763 {"h", ST_OFF(cfg_h), CONF_TYPE_INT, M_OPT_MIN,-11,0, NULL},
764 {"interlaced", ST_OFF(interlaced), CONF_TYPE_INT, M_OPT_RANGE, 0, 1, NULL},
765 {"chr-drop", ST_OFF(v_chr_drop), CONF_TYPE_INT, M_OPT_RANGE, 0, 3, NULL},
766 {"param" , ST_OFF(param[0]), CONF_TYPE_DOUBLE, M_OPT_RANGE, 0.0, 100.0, NULL},
767 {"param2", ST_OFF(param[1]), CONF_TYPE_DOUBLE, M_OPT_RANGE, 0.0, 100.0, NULL},
768 // Note that here the 2 field is NULL (ie 0)
769 // As we want this option to act on the option struct itself
770 {"presize", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0, (void *)&size_preset},
771 {"noup", ST_OFF(noup), CONF_TYPE_INT, M_OPT_RANGE, 0, 2, NULL},
772 {"arnd", ST_OFF(accurate_rnd), CONF_TYPE_FLAG, 0, 0, 1, NULL},
773 { NULL, NULL, 0, 0, 0, 0, NULL }
776 static const m_struct_t vf_opts = {
777 "scale",
778 sizeof(struct vf_priv_s),
779 &vf_priv_dflt,
780 vf_opts_fields
783 const vf_info_t vf_info_scale = {
784 "software scaling",
785 "scale",
786 "A'rpi",
788 vf_open,
789 &vf_opts
792 //===========================================================================//