Update to MPlayer SVN rev 34578.
[vaapi:athaifas-mplayer.git] / libmpcodecs / vf_scale.c
1 /*
2  * This file is part of MPlayer.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
23
24 #include "config.h"
25 #include "mp_msg.h"
26 #include "cpudetect.h"
27
28 #include "img_format.h"
29 #include "mp_image.h"
30 #include "vd.h"
31 #include "vf.h"
32 #include "fmt-conversion.h"
33 #include "mpbswap.h"
34
35 #include "libswscale/swscale.h"
36 #include "vf_scale.h"
37
38 #include "m_option.h"
39 #include "m_struct.h"
40
41 static struct vf_priv_s {
42     int w,h;
43     int v_chr_drop;
44     double param[2];
45     unsigned int fmt;
46     struct SwsContext *ctx;
47     struct SwsContext *ctx2; //for interlaced slices only
48     unsigned char* palette;
49     int interlaced;
50     int noup;
51     int accurate_rnd;
52 } const vf_priv_dflt = {
53   -1,-1,
54   0,
55   {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT},
56   0,
57   NULL,
58   NULL,
59   NULL
60 };
61
62 //===========================================================================//
63
64 void sws_getFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam);
65
66 static const unsigned int outfmt_list[]={
67 // YUV:
68     IMGFMT_444P,
69     IMGFMT_444P16_LE,
70     IMGFMT_444P16_BE,
71     IMGFMT_444P10_LE,
72     IMGFMT_444P10_BE,
73     IMGFMT_444P9_LE,
74     IMGFMT_444P9_BE,
75     IMGFMT_422P,
76     IMGFMT_422P16_LE,
77     IMGFMT_422P16_BE,
78     IMGFMT_422P10_LE,
79     IMGFMT_422P10_BE,
80     IMGFMT_422P9_LE,
81     IMGFMT_422P9_BE,
82     IMGFMT_YV12,
83     IMGFMT_I420,
84     IMGFMT_420P16_LE,
85     IMGFMT_420P16_BE,
86     IMGFMT_420P10_LE,
87     IMGFMT_420P10_BE,
88     IMGFMT_420P9_LE,
89     IMGFMT_420P9_BE,
90     IMGFMT_420A,
91     IMGFMT_IYUV,
92     IMGFMT_YVU9,
93     IMGFMT_IF09,
94     IMGFMT_411P,
95     IMGFMT_NV12,
96     IMGFMT_NV21,
97     IMGFMT_YUY2,
98     IMGFMT_UYVY,
99     IMGFMT_440P,
100 // RGB and grayscale (Y8 and Y800):
101     IMGFMT_BGR32,
102     IMGFMT_RGB32,
103     IMGFMT_BGR24,
104     IMGFMT_RGB24,
105     IMGFMT_GBR24P,
106     IMGFMT_RGB48LE,
107     IMGFMT_RGB48BE,
108     IMGFMT_BGR16,
109     IMGFMT_RGB16,
110     IMGFMT_BGR15,
111     IMGFMT_RGB15,
112     IMGFMT_BGR12,
113     IMGFMT_RGB12,
114     IMGFMT_Y800,
115     IMGFMT_Y8,
116     IMGFMT_BGR8,
117     IMGFMT_RGB8,
118     IMGFMT_BGR4,
119     IMGFMT_RGB4,
120     IMGFMT_BG4B,
121     IMGFMT_RG4B,
122     IMGFMT_BGR1,
123     IMGFMT_RGB1,
124     0
125 };
126
127 /**
128  * A list of preferred conversions, in order of preference.
129  * This should be used for conversions that e.g. involve no scaling
130  * or to stop vf_scale from choosing a conversion that has no
131  * fast assembler implementation.
132  */
133 static int preferred_conversions[][2] = {
134     {IMGFMT_YUY2, IMGFMT_UYVY},
135     {IMGFMT_YUY2, IMGFMT_422P},
136     {IMGFMT_UYVY, IMGFMT_YUY2},
137     {IMGFMT_UYVY, IMGFMT_422P},
138     {IMGFMT_422P, IMGFMT_YUY2},
139     {IMGFMT_422P, IMGFMT_UYVY},
140     {IMGFMT_GBR24P, IMGFMT_BGR24},
141     {IMGFMT_GBR24P, IMGFMT_RGB24},
142     {IMGFMT_GBR24P, IMGFMT_BGR32},
143     {IMGFMT_GBR24P, IMGFMT_RGB32},
144     {0, 0}
145 };
146
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;
152
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;
167         }
168         if (i >= 0)
169             format = outfmt_list[i++];
170         if (!format)
171             break;
172         ret = vf_next_query_format(vf, format);
173
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;
178         }
179         if(ret&VFCAP_CSP_SUPPORTED && !best)
180             best=format; // best with conversion
181     }
182     return best;
183 }
184
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     unsigned int best=find_best_out(vf, outfmt);
189     int vo_flags;
190     int int_sws_flags=0;
191     int round_w=0, round_h=0;
192     int i;
193     SwsFilter *srcFilter, *dstFilter;
194     enum PixelFormat dfmt, sfmt;
195
196     if(!best){
197         mp_msg(MSGT_VFILTER,MSGL_WARN,"SwScale: no supported outfmt found :(\n");
198         return 0;
199     }
200     sfmt = imgfmt2pixfmt(outfmt);
201     if (outfmt == IMGFMT_RGB8 || outfmt == IMGFMT_BGR8) sfmt = PIX_FMT_PAL8;
202     dfmt = imgfmt2pixfmt(best);
203
204     vo_flags=vf->next->query_format(vf->next,best);
205
206     // scaling to dwidth*d_height, if all these TRUE:
207     // - option -zoom
208     // - no other sw/hw up/down scaling avail.
209     // - we're after postproc
210     // - user didn't set w:h
211     if(!(vo_flags&VFCAP_POSTPROC) && (flags&4) &&
212             vf->priv->w<0 && vf->priv->h<0){        // -zoom
213         int x=(vo_flags&VFCAP_SWSCALE) ? 0 : 1;
214         if(d_width<width || d_height<height){
215             // downscale!
216             if(vo_flags&VFCAP_HWSCALE_DOWN) x=0;
217         } else {
218             // upscale:
219             if(vo_flags&VFCAP_HWSCALE_UP) x=0;
220         }
221         if(x){
222             // user wants sw scaling! (-zoom)
223             vf->priv->w=d_width;
224             vf->priv->h=d_height;
225         }
226     }
227
228     if(vf->priv->noup){
229         if((vf->priv->w > width) + (vf->priv->h > height) >= vf->priv->noup){
230             vf->priv->w= width;
231             vf->priv->h= height;
232         }
233     }
234
235     if (vf->priv->w <= -8) {
236       vf->priv->w += 8;
237       round_w = 1;
238     }
239     if (vf->priv->h <= -8) {
240       vf->priv->h += 8;
241       round_h = 1;
242     }
243
244     if (vf->priv->w < -3 || vf->priv->h < -3 ||
245          (vf->priv->w < -1 && vf->priv->h < -1)) {
246       // TODO: establish a direct connection to the user's brain
247       // and find out what the heck he thinks MPlayer should do
248       // with this nonsense.
249       mp_msg(MSGT_VFILTER, MSGL_ERR, "SwScale: EUSERBROKEN Check your parameters, they make no sense!\n");
250       return 0;
251     }
252
253     if (vf->priv->w == -1)
254       vf->priv->w = width;
255     if (vf->priv->w == 0)
256       vf->priv->w = d_width;
257
258     if (vf->priv->h == -1)
259       vf->priv->h = height;
260     if (vf->priv->h == 0)
261       vf->priv->h = d_height;
262
263     if (vf->priv->w == -3)
264       vf->priv->w = vf->priv->h * width / height;
265     if (vf->priv->w == -2)
266       vf->priv->w = vf->priv->h * d_width / d_height;
267
268     if (vf->priv->h == -3)
269       vf->priv->h = vf->priv->w * height / width;
270     if (vf->priv->h == -2)
271       vf->priv->h = vf->priv->w * d_height / d_width;
272
273     if (round_w)
274       vf->priv->w = ((vf->priv->w + 8) / 16) * 16;
275     if (round_h)
276       vf->priv->h = ((vf->priv->h + 8) / 16) * 16;
277
278     // calculate the missing parameters:
279     switch(best) {
280     case IMGFMT_YV12:                /* YV12 needs w & h rounded to 2 */
281     case IMGFMT_I420:
282     case IMGFMT_IYUV:
283     case IMGFMT_NV12:
284     case IMGFMT_NV21:
285       vf->priv->h = (vf->priv->h + 1) & ~1;
286     case IMGFMT_YUY2:                /* YUY2 needs w rounded to 2 */
287     case IMGFMT_UYVY:
288       vf->priv->w = (vf->priv->w + 1) & ~1;
289     }
290
291     mp_msg(MSGT_VFILTER,MSGL_DBG2,"SwScale: scaling %dx%d %s to %dx%d %s  \n",
292         width,height,vo_format_name(outfmt),
293         vf->priv->w,vf->priv->h,vo_format_name(best));
294
295     // free old ctx:
296     if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
297     if(vf->priv->ctx2)sws_freeContext(vf->priv->ctx2);
298
299     // new swscaler:
300     sws_getFlagsAndFilterFromCmdLine(&int_sws_flags, &srcFilter, &dstFilter);
301     int_sws_flags|= vf->priv->v_chr_drop << SWS_SRC_V_CHR_DROP_SHIFT;
302     int_sws_flags|= vf->priv->accurate_rnd * SWS_ACCURATE_RND;
303     vf->priv->ctx=sws_getContext(width, height >> vf->priv->interlaced,
304             sfmt,
305                   vf->priv->w, vf->priv->h >> vf->priv->interlaced,
306             dfmt,
307             int_sws_flags, srcFilter, dstFilter, vf->priv->param);
308     if(vf->priv->interlaced){
309         vf->priv->ctx2=sws_getContext(width, height >> 1,
310             sfmt,
311                   vf->priv->w, vf->priv->h >> 1,
312             dfmt,
313             int_sws_flags, srcFilter, dstFilter, vf->priv->param);
314     }
315     if(!vf->priv->ctx){
316         // error...
317         mp_msg(MSGT_VFILTER,MSGL_WARN,"Couldn't init SwScaler for this setup\n");
318         return 0;
319     }
320     vf->priv->fmt=best;
321
322     free(vf->priv->palette);
323     vf->priv->palette=NULL;
324     switch(best){
325     case IMGFMT_RGB8: {
326       /* set 332 palette for 8 bpp */
327         vf->priv->palette=malloc(4*256);
328         for(i=0; i<256; i++){
329             vf->priv->palette[4*i+0]=4*(i>>6)*21;
330             vf->priv->palette[4*i+1]=4*((i>>3)&7)*9;
331             vf->priv->palette[4*i+2]=4*((i&7)&7)*9;
332             vf->priv->palette[4*i+3]=0;
333         }
334         break; }
335     case IMGFMT_BGR8: {
336       /* set 332 palette for 8 bpp */
337         vf->priv->palette=malloc(4*256);
338         for(i=0; i<256; i++){
339             vf->priv->palette[4*i+0]=4*(i&3)*21;
340             vf->priv->palette[4*i+1]=4*((i>>2)&7)*9;
341             vf->priv->palette[4*i+2]=4*((i>>5)&7)*9;
342             vf->priv->palette[4*i+3]=0;
343         }
344         break; }
345     case IMGFMT_BGR4:
346     case IMGFMT_BG4B: {
347         vf->priv->palette=malloc(4*16);
348         for(i=0; i<16; i++){
349             vf->priv->palette[4*i+0]=4*(i&1)*63;
350             vf->priv->palette[4*i+1]=4*((i>>1)&3)*21;
351             vf->priv->palette[4*i+2]=4*((i>>3)&1)*63;
352             vf->priv->palette[4*i+3]=0;
353         }
354         break; }
355     case IMGFMT_RGB4:
356     case IMGFMT_RG4B: {
357         vf->priv->palette=malloc(4*16);
358         for(i=0; i<16; i++){
359             vf->priv->palette[4*i+0]=4*(i>>3)*63;
360             vf->priv->palette[4*i+1]=4*((i>>1)&3)*21;
361             vf->priv->palette[4*i+2]=4*((i&1)&1)*63;
362             vf->priv->palette[4*i+3]=0;
363         }
364         break; }
365     }
366
367     if(!opt_screen_size_x && !opt_screen_size_y && !(screen_size_xy >= 0.001)){
368         // Compute new d_width and d_height, preserving aspect
369         // while ensuring that both are >= output size in pixels.
370         if (vf->priv->h * d_width > vf->priv->w * d_height) {
371                 d_width = vf->priv->h * d_width / d_height;
372                 d_height = vf->priv->h;
373         } else {
374                 d_height = vf->priv->w * d_height / d_width;
375                 d_width = vf->priv->w;
376         }
377         //d_width=d_width*vf->priv->w/width;
378         //d_height=d_height*vf->priv->h/height;
379     }
380     return vf_next_config(vf,vf->priv->w,vf->priv->h,d_width,d_height,flags,best);
381 }
382
383 static void start_slice(struct vf_instance *vf, mp_image_t *mpi){
384 //  printf("start_slice called! flag=%d\n",mpi->flags&MP_IMGFLAG_DRAW_CALLBACK);
385     if(!(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)) return; // shouldn't happen
386     // they want slices!!! allocate the buffer.
387     mpi->priv=vf->dmpi=vf_get_image(vf->next,vf->priv->fmt,
388 //      mpi->type, mpi->flags & (~MP_IMGFLAG_DRAW_CALLBACK),
389         MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
390         vf->priv->w, vf->priv->h);
391 }
392
393 static void scale(struct SwsContext *sws1, struct SwsContext *sws2, uint8_t *src[MP_MAX_PLANES], int src_stride[MP_MAX_PLANES],
394                   int y, int h,  uint8_t *dst[MP_MAX_PLANES], int dst_stride[MP_MAX_PLANES], int interlaced){
395     uint8_t *src2[MP_MAX_PLANES]={src[0], src[1], src[2], src[3]};
396 #if HAVE_BIGENDIAN
397     uint32_t pal2[256];
398     if (src[1] && !src[2]){
399         int i;
400         for(i=0; i<256; i++)
401             pal2[i]= bswap_32(((uint32_t*)src[1])[i]);
402         src2[1]= pal2;
403     }
404 #endif
405
406     if(interlaced){
407         int i;
408         uint8_t *dst2[MP_MAX_PLANES]={dst[0], dst[1], dst[2], dst[3]};
409         int src_stride2[MP_MAX_PLANES]={2*src_stride[0], 2*src_stride[1], 2*src_stride[2], 2*src_stride[3]};
410         int dst_stride2[MP_MAX_PLANES]={2*dst_stride[0], 2*dst_stride[1], 2*dst_stride[2], 2*dst_stride[3]};
411
412         sws_scale(sws1, src2, src_stride2, y>>1, h>>1, dst2, dst_stride2);
413         for(i=0; i<MP_MAX_PLANES; i++){
414             src2[i] += src_stride[i];
415             dst2[i] += dst_stride[i];
416         }
417         sws_scale(sws2, src2, src_stride2, y>>1, h>>1, dst2, dst_stride2);
418     }else{
419         sws_scale(sws1, src2, src_stride, y, h, dst, dst_stride);
420     }
421 }
422
423 static void draw_slice(struct vf_instance *vf,
424         unsigned char** src, int* stride, int w,int h, int x, int y){
425     mp_image_t *dmpi=vf->dmpi;
426     if(!dmpi){
427         mp_msg(MSGT_VFILTER,MSGL_FATAL,"vf_scale: draw_slice() called with dmpi=NULL (no get_image?)\n");
428         return;
429     }
430 //  printf("vf_scale::draw_slice() y=%d h=%d\n",y,h);
431     scale(vf->priv->ctx, vf->priv->ctx2, src, stride, y, h, dmpi->planes, dmpi->stride, vf->priv->interlaced);
432 }
433
434 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
435     mp_image_t *dmpi=mpi->priv;
436
437 //  printf("vf_scale::put_image(): processing whole frame! dmpi=%p flag=%d\n",
438 //      dmpi, (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK));
439
440   if(!(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK && dmpi)){
441
442     // hope we'll get DR buffer:
443     dmpi=vf_get_image(vf->next,vf->priv->fmt,
444         MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
445         vf->priv->w, vf->priv->h);
446
447       scale(vf->priv->ctx, vf->priv->ctx, mpi->planes,mpi->stride,0,mpi->h,dmpi->planes,dmpi->stride, vf->priv->interlaced);
448   }
449
450     if(vf->priv->w==mpi->w && vf->priv->h==mpi->h){
451         // just conversion, no scaling -> keep postprocessing data
452         // this way we can apply pp filter to non-yv12 source using scaler
453         vf_clone_mpi_attributes(dmpi, mpi);
454     }
455
456     if(vf->priv->palette) dmpi->planes[1]=vf->priv->palette; // export palette!
457
458     return vf_next_put_image(vf,dmpi, pts);
459 }
460
461 static int control(struct vf_instance *vf, int request, void* data){
462     int *table;
463     int *inv_table;
464     int r;
465     int brightness, contrast, saturation, srcRange, dstRange;
466     vf_equalizer_t *eq;
467
468   if(vf->priv->ctx)
469     switch(request){
470     case VFCTRL_GET_EQUALIZER:
471         r= sws_getColorspaceDetails(vf->priv->ctx, &inv_table, &srcRange, &table, &dstRange, &brightness, &contrast, &saturation);
472         if(r<0) break;
473
474         eq = data;
475         if (!strcmp(eq->item,"brightness")) {
476                 eq->value =  ((brightness*100) + (1<<15))>>16;
477         }
478         else if (!strcmp(eq->item,"contrast")) {
479                 eq->value = (((contrast  *100) + (1<<15))>>16) - 100;
480         }
481         else if (!strcmp(eq->item,"saturation")) {
482                 eq->value = (((saturation*100) + (1<<15))>>16) - 100;
483         }
484         else
485                 break;
486         return CONTROL_TRUE;
487     case VFCTRL_SET_EQUALIZER:
488         r= sws_getColorspaceDetails(vf->priv->ctx, &inv_table, &srcRange, &table, &dstRange, &brightness, &contrast, &saturation);
489         if(r<0) break;
490 //printf("set %f %f %f\n", brightness/(float)(1<<16), contrast/(float)(1<<16), saturation/(float)(1<<16));
491         eq = data;
492
493         if (!strcmp(eq->item,"brightness")) {
494                 brightness = (( eq->value     <<16) + 50)/100;
495         }
496         else if (!strcmp(eq->item,"contrast")) {
497                 contrast   = (((eq->value+100)<<16) + 50)/100;
498         }
499         else if (!strcmp(eq->item,"saturation")) {
500                 saturation = (((eq->value+100)<<16) + 50)/100;
501         }
502         else
503                 break;
504
505         r= sws_setColorspaceDetails(vf->priv->ctx, inv_table, srcRange, table, dstRange, brightness, contrast, saturation);
506         if(r<0) break;
507         if(vf->priv->ctx2){
508             r= sws_setColorspaceDetails(vf->priv->ctx2, inv_table, srcRange, table, dstRange, brightness, contrast, saturation);
509             if(r<0) break;
510         }
511
512         return CONTROL_TRUE;
513     default:
514         break;
515     }
516
517     return vf_next_control(vf,request,data);
518 }
519
520 //===========================================================================//
521
522 //  supported Input formats: YV12, I420, IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8, Y800
523
524 static int query_format(struct vf_instance *vf, unsigned int fmt){
525     if (!IMGFMT_IS_HWACCEL(fmt) && imgfmt2pixfmt(fmt) != PIX_FMT_NONE) {
526         unsigned int best=find_best_out(vf, fmt);
527         int flags;
528         if(!best) return 0;         // no matching out-fmt
529         flags=vf_next_query_format(vf,best);
530         if(!(flags&(VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW))) return 0; // huh?
531         if(fmt!=best) flags&=~VFCAP_CSP_SUPPORTED_BY_HW;
532         // do not allow scaling, if we are before the PP fliter!
533         if(!(flags&VFCAP_POSTPROC)) flags|=VFCAP_SWSCALE;
534         return flags;
535     }
536     return 0;        // nomatching in-fmt
537 }
538
539 static void uninit(struct vf_instance *vf){
540     if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
541     if(vf->priv->ctx2) sws_freeContext(vf->priv->ctx2);
542     free(vf->priv->palette);
543     free(vf->priv);
544 }
545
546 static int vf_open(vf_instance_t *vf, char *args){
547     vf->config=config;
548     vf->start_slice=start_slice;
549     vf->draw_slice=draw_slice;
550     vf->put_image=put_image;
551     vf->query_format=query_format;
552     vf->control= control;
553     vf->uninit=uninit;
554     mp_msg(MSGT_VFILTER,MSGL_V,"SwScale params: %d x %d (-1=no scaling)\n",
555     vf->priv->w,
556     vf->priv->h);
557
558     return 1;
559 }
560
561 //global sws_flags from the command line
562 int sws_flags=2;
563
564 //global srcFilter
565 static SwsFilter *src_filter= NULL;
566
567 float sws_lum_gblur= 0.0;
568 float sws_chr_gblur= 0.0;
569 int sws_chr_vshift= 0;
570 int sws_chr_hshift= 0;
571 float sws_chr_sharpen= 0.0;
572 float sws_lum_sharpen= 0.0;
573
574
575 void sws_getFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam)
576 {
577         static int firstTime=1;
578         *flags=0;
579
580 #if ARCH_X86
581         if(gCpuCaps.hasMMX)
582                 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
583 #endif
584         if(firstTime)
585         {
586                 firstTime=0;
587                 *flags= SWS_PRINT_INFO;
588         }
589         else if( mp_msg_test(MSGT_VFILTER,MSGL_DBG2) ) *flags= SWS_PRINT_INFO;
590
591         if(src_filter) sws_freeFilter(src_filter);
592
593         src_filter= sws_getDefaultFilter(
594                 sws_lum_gblur, sws_chr_gblur,
595                 sws_lum_sharpen, sws_chr_sharpen,
596                 sws_chr_hshift, sws_chr_vshift, verbose>1);
597
598         switch(sws_flags)
599         {
600                 case 0: *flags|= SWS_FAST_BILINEAR; break;
601                 case 1: *flags|= SWS_BILINEAR; break;
602                 case 2: *flags|= SWS_BICUBIC; break;
603                 case 3: *flags|= SWS_X; break;
604                 case 4: *flags|= SWS_POINT; break;
605                 case 5: *flags|= SWS_AREA; break;
606                 case 6: *flags|= SWS_BICUBLIN; break;
607                 case 7: *flags|= SWS_GAUSS; break;
608                 case 8: *flags|= SWS_SINC; break;
609                 case 9: *flags|= SWS_LANCZOS; break;
610                 case 10:*flags|= SWS_SPLINE; break;
611                 default:*flags|= SWS_BILINEAR; break;
612         }
613
614         *srcFilterParam= src_filter;
615         *dstFilterParam= NULL;
616 }
617
618 // will use sws_flags & src_filter (from cmd line)
619 struct SwsContext *sws_getContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
620 {
621         int flags;
622         SwsFilter *dstFilterParam, *srcFilterParam;
623         enum PixelFormat dfmt, sfmt;
624
625         dfmt = imgfmt2pixfmt(dstFormat);
626         sfmt = imgfmt2pixfmt(srcFormat);
627         if (srcFormat == IMGFMT_RGB8 || srcFormat == IMGFMT_BGR8) sfmt = PIX_FMT_PAL8;
628         sws_getFlagsAndFilterFromCmdLine(&flags, &srcFilterParam, &dstFilterParam);
629
630         return sws_getContext(srcW, srcH, sfmt, dstW, dstH, dfmt, flags, srcFilterParam, dstFilterParam, NULL);
631 }
632
633 /// An example of presets usage
634 static const struct size_preset {
635   char* name;
636   int w, h;
637 } vf_size_presets_defs[] = {
638   // TODO add more 'standard' resolutions
639   { "qntsc", 352, 240 },
640   { "qpal", 352, 288 },
641   { "ntsc", 720, 480 },
642   { "pal", 720, 576 },
643   { "sntsc", 640, 480 },
644   { "spal", 768, 576 },
645   { NULL, 0, 0}
646 };
647
648 #define ST_OFF(f) M_ST_OFF(struct size_preset,f)
649 static const m_option_t vf_size_preset_fields[] = {
650   {"w", ST_OFF(w), CONF_TYPE_INT, M_OPT_MIN,1 ,0, NULL},
651   {"h", ST_OFF(h), CONF_TYPE_INT, M_OPT_MIN,1 ,0, NULL},
652   { NULL, NULL, 0, 0, 0, 0,  NULL }
653 };
654
655 static const m_struct_t vf_size_preset = {
656   "scale_size_preset",
657   sizeof(struct size_preset),
658   NULL,
659   vf_size_preset_fields
660 };
661
662 static const m_struct_t vf_opts;
663 static const m_obj_presets_t size_preset = {
664   &vf_size_preset, // Input struct desc
665   &vf_opts, // Output struct desc
666   vf_size_presets_defs, // The list of presets
667   ST_OFF(name) // At wich offset is the name field in the preset struct
668 };
669
670 /// Now the options
671 #undef ST_OFF
672 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
673 static const m_option_t vf_opts_fields[] = {
674   {"w", ST_OFF(w), CONF_TYPE_INT, M_OPT_MIN,-11,0, NULL},
675   {"h", ST_OFF(h), CONF_TYPE_INT, M_OPT_MIN,-11,0, NULL},
676   {"interlaced", ST_OFF(interlaced), CONF_TYPE_INT, M_OPT_RANGE, 0, 1, NULL},
677   {"chr-drop", ST_OFF(v_chr_drop), CONF_TYPE_INT, M_OPT_RANGE, 0, 3, NULL},
678   {"param" , ST_OFF(param[0]), CONF_TYPE_DOUBLE, M_OPT_RANGE, 0.0, 100.0, NULL},
679   {"param2", ST_OFF(param[1]), CONF_TYPE_DOUBLE, M_OPT_RANGE, 0.0, 100.0, NULL},
680   // Note that here the 2 field is NULL (ie 0)
681   // As we want this option to act on the option struct itself
682   {"presize", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0, &size_preset},
683   {"noup", ST_OFF(noup), CONF_TYPE_INT, M_OPT_RANGE, 0, 2, NULL},
684   {"arnd", ST_OFF(accurate_rnd), CONF_TYPE_FLAG, 0, 0, 1, NULL},
685   { NULL, NULL, 0, 0, 0, 0,  NULL }
686 };
687
688 static const m_struct_t vf_opts = {
689   "scale",
690   sizeof(struct vf_priv_s),
691   &vf_priv_dflt,
692   vf_opts_fields
693 };
694
695 const vf_info_t vf_info_scale = {
696     "software scaling",
697     "scale",
698     "A'rpi",
699     "",
700     vf_open,
701     &vf_opts
702 };
703
704 //===========================================================================//