vo_gl: add new vo name "gl_sdl" to make SDL+GL mode available
[mplayer.git] / libmpcodecs / vf_scale.c
blob8cd0a37c33c8bdde50a5b1dfcddabb8a57b349bc
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"
40 #include "m_option.h"
41 #include "m_struct.h"
43 static struct vf_priv_s {
44 int w,h;
45 int v_chr_drop;
46 double param[2];
47 unsigned int fmt;
48 struct SwsContext *ctx;
49 struct SwsContext *ctx2; //for interlaced slices only
50 unsigned char* palette;
51 int interlaced;
52 int noup;
53 int accurate_rnd;
54 struct mp_csp_details colorspace;
55 } const vf_priv_dflt = {
56 -1,-1,
58 {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT},
60 NULL,
61 NULL,
62 NULL
65 //===========================================================================//
67 void sws_getFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam);
69 static const unsigned int outfmt_list[]={
70 // YUV:
71 IMGFMT_444P,
72 IMGFMT_444P16_LE,
73 IMGFMT_444P16_BE,
74 IMGFMT_444P10_LE,
75 IMGFMT_444P10_BE,
76 IMGFMT_444P9_LE,
77 IMGFMT_444P9_BE,
78 IMGFMT_422P,
79 IMGFMT_422P16_LE,
80 IMGFMT_422P16_BE,
81 IMGFMT_422P10_LE,
82 IMGFMT_422P10_BE,
83 IMGFMT_YV12,
84 IMGFMT_I420,
85 IMGFMT_420P16_LE,
86 IMGFMT_420P16_BE,
87 IMGFMT_420P10_LE,
88 IMGFMT_420P10_BE,
89 IMGFMT_420P9_LE,
90 IMGFMT_420P9_BE,
91 IMGFMT_420A,
92 IMGFMT_IYUV,
93 IMGFMT_YVU9,
94 IMGFMT_IF09,
95 IMGFMT_411P,
96 IMGFMT_NV12,
97 IMGFMT_NV21,
98 IMGFMT_YUY2,
99 IMGFMT_UYVY,
100 IMGFMT_440P,
101 // RGB and grayscale (Y8 and Y800):
102 IMGFMT_BGR32,
103 IMGFMT_RGB32,
104 IMGFMT_BGR24,
105 IMGFMT_RGB24,
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,
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.
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 {0, 0}
143 static unsigned int find_best_out(vf_instance_t *vf, int in_format){
144 unsigned int best=0;
145 int i = -1;
146 int j = -1;
147 int format = 0;
149 // find the best outfmt:
150 while (1) {
151 int ret;
152 if (j < 0) {
153 format = in_format;
154 j = 0;
155 } else if (i < 0) {
156 while (preferred_conversions[j][0] &&
157 preferred_conversions[j][0] != in_format)
158 j++;
159 format = preferred_conversions[j++][1];
160 // switch to standard list
161 if (!format)
162 i = 0;
164 if (i >= 0)
165 format = outfmt_list[i++];
166 if (!format)
167 break;
168 ret = vf_next_query_format(vf, format);
170 mp_msg(MSGT_VFILTER,MSGL_DBG2,"scale: query(%s) -> %d\n",vo_format_name(format),ret&3);
171 if(ret&VFCAP_CSP_SUPPORTED_BY_HW){
172 best=format; // no conversion -> bingo!
173 break;
175 if(ret&VFCAP_CSP_SUPPORTED && !best)
176 best=format; // best with conversion
178 return best;
181 static int config(struct vf_instance *vf,
182 int width, int height, int d_width, int d_height,
183 unsigned int flags, unsigned int outfmt){
184 struct MPOpts *opts = vf->opts;
185 unsigned int best=find_best_out(vf, outfmt);
186 int vo_flags;
187 int int_sws_flags=0;
188 int round_w=0, round_h=0;
189 int i;
190 SwsFilter *srcFilter, *dstFilter;
191 enum PixelFormat dfmt, sfmt;
193 vf->priv->colorspace = (struct mp_csp_details) {0};
195 if(!best){
196 mp_msg(MSGT_VFILTER,MSGL_WARN,"SwScale: no supported outfmt found :(\n");
197 return 0;
199 sfmt = imgfmt2pixfmt(outfmt);
200 if (outfmt == IMGFMT_RGB8 || outfmt == IMGFMT_BGR8) sfmt = PIX_FMT_PAL8;
201 dfmt = imgfmt2pixfmt(best);
203 vo_flags=vf->next->query_format(vf->next,best);
205 // scaling to dwidth*d_height, if all these TRUE:
206 // - option -zoom
207 // - no other sw/hw up/down scaling avail.
208 // - we're after postproc
209 // - user didn't set w:h
210 if(!(vo_flags&VFCAP_POSTPROC) && (flags&4) &&
211 vf->priv->w<0 && vf->priv->h<0){ // -zoom
212 int x=(vo_flags&VFCAP_SWSCALE) ? 0 : 1;
213 if(d_width<width || d_height<height){
214 // downscale!
215 if(vo_flags&VFCAP_HWSCALE_DOWN) x=0;
216 } else {
217 // upscale:
218 if(vo_flags&VFCAP_HWSCALE_UP) x=0;
220 if(x){
221 // user wants sw scaling! (-zoom)
222 vf->priv->w=d_width;
223 vf->priv->h=d_height;
227 if(vf->priv->noup){
228 if((vf->priv->w > width) + (vf->priv->h > height) >= vf->priv->noup){
229 vf->priv->w= width;
230 vf->priv->h= height;
234 if (vf->priv->w <= -8) {
235 vf->priv->w += 8;
236 round_w = 1;
238 if (vf->priv->h <= -8) {
239 vf->priv->h += 8;
240 round_h = 1;
243 if (vf->priv->w < -3 || vf->priv->h < -3 ||
244 (vf->priv->w < -1 && vf->priv->h < -1)) {
245 // TODO: establish a direct connection to the user's brain
246 // and find out what the heck he thinks MPlayer should do
247 // with this nonsense.
248 mp_msg(MSGT_VFILTER, MSGL_ERR, "SwScale: EUSERBROKEN Check your parameters, they make no sense!\n");
249 return 0;
252 if (vf->priv->w == -1)
253 vf->priv->w = width;
254 if (vf->priv->w == 0)
255 vf->priv->w = d_width;
257 if (vf->priv->h == -1)
258 vf->priv->h = height;
259 if (vf->priv->h == 0)
260 vf->priv->h = d_height;
262 if (vf->priv->w == -3)
263 vf->priv->w = vf->priv->h * width / height;
264 if (vf->priv->w == -2)
265 vf->priv->w = vf->priv->h * d_width / d_height;
267 if (vf->priv->h == -3)
268 vf->priv->h = vf->priv->w * height / width;
269 if (vf->priv->h == -2)
270 vf->priv->h = vf->priv->w * d_height / d_width;
272 if (round_w)
273 vf->priv->w = ((vf->priv->w + 8) / 16) * 16;
274 if (round_h)
275 vf->priv->h = ((vf->priv->h + 8) / 16) * 16;
277 // calculate the missing parameters:
278 switch(best) {
279 case IMGFMT_YV12: /* YV12 needs w & h rounded to 2 */
280 case IMGFMT_I420:
281 case IMGFMT_IYUV:
282 case IMGFMT_NV12:
283 case IMGFMT_NV21:
284 vf->priv->h = (vf->priv->h + 1) & ~1;
285 case IMGFMT_YUY2: /* YUY2 needs w rounded to 2 */
286 case IMGFMT_UYVY:
287 vf->priv->w = (vf->priv->w + 1) & ~1;
290 mp_msg(MSGT_VFILTER,MSGL_DBG2,"SwScale: scaling %dx%d %s to %dx%d %s \n",
291 width,height,vo_format_name(outfmt),
292 vf->priv->w,vf->priv->h,vo_format_name(best));
294 // free old ctx:
295 if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
296 if(vf->priv->ctx2)sws_freeContext(vf->priv->ctx2);
298 // new swscaler:
299 sws_getFlagsAndFilterFromCmdLine(&int_sws_flags, &srcFilter, &dstFilter);
300 int_sws_flags|= vf->priv->v_chr_drop << SWS_SRC_V_CHR_DROP_SHIFT;
301 int_sws_flags|= vf->priv->accurate_rnd * SWS_ACCURATE_RND;
302 vf->priv->ctx=sws_getContext(width, height >> vf->priv->interlaced,
303 sfmt,
304 vf->priv->w, vf->priv->h >> vf->priv->interlaced,
305 dfmt,
306 int_sws_flags | get_sws_cpuflags(), srcFilter, dstFilter, vf->priv->param);
307 if(vf->priv->interlaced){
308 vf->priv->ctx2=sws_getContext(width, height >> 1,
309 sfmt,
310 vf->priv->w, vf->priv->h >> 1,
311 dfmt,
312 int_sws_flags | get_sws_cpuflags(), srcFilter, dstFilter, vf->priv->param);
314 if(!vf->priv->ctx){
315 // error...
316 mp_msg(MSGT_VFILTER,MSGL_WARN,"Couldn't init SwScaler for this setup\n");
317 return 0;
319 vf->priv->fmt=best;
321 free(vf->priv->palette);
322 vf->priv->palette=NULL;
323 switch(best){
324 case IMGFMT_RGB8: {
325 /* set 332 palette for 8 bpp */
326 vf->priv->palette=malloc(4*256);
327 for(i=0; i<256; i++){
328 vf->priv->palette[4*i+0]=4*(i>>6)*21;
329 vf->priv->palette[4*i+1]=4*((i>>3)&7)*9;
330 vf->priv->palette[4*i+2]=4*((i&7)&7)*9;
331 vf->priv->palette[4*i+3]=0;
333 break; }
334 case IMGFMT_BGR8: {
335 /* set 332 palette for 8 bpp */
336 vf->priv->palette=malloc(4*256);
337 for(i=0; i<256; i++){
338 vf->priv->palette[4*i+0]=4*(i&3)*21;
339 vf->priv->palette[4*i+1]=4*((i>>2)&7)*9;
340 vf->priv->palette[4*i+2]=4*((i>>5)&7)*9;
341 vf->priv->palette[4*i+3]=0;
343 break; }
344 case IMGFMT_BGR4:
345 case IMGFMT_BG4B: {
346 vf->priv->palette=malloc(4*16);
347 for(i=0; i<16; i++){
348 vf->priv->palette[4*i+0]=4*(i&1)*63;
349 vf->priv->palette[4*i+1]=4*((i>>1)&3)*21;
350 vf->priv->palette[4*i+2]=4*((i>>3)&1)*63;
351 vf->priv->palette[4*i+3]=0;
353 break; }
354 case IMGFMT_RGB4:
355 case IMGFMT_RG4B: {
356 vf->priv->palette=malloc(4*16);
357 for(i=0; i<16; i++){
358 vf->priv->palette[4*i+0]=4*(i>>3)*63;
359 vf->priv->palette[4*i+1]=4*((i>>1)&3)*21;
360 vf->priv->palette[4*i+2]=4*((i&1)&1)*63;
361 vf->priv->palette[4*i+3]=0;
363 break; }
366 if (!opts->screen_size_x && !opts->screen_size_y
367 && !(opts->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;
377 //d_width=d_width*vf->priv->w/width;
378 //d_height=d_height*vf->priv->h/height;
380 return vf_next_config(vf,vf->priv->w,vf->priv->h,d_width,d_height,flags,best);
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);
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 const 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;
404 #endif
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]};
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];
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);
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;
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);
434 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
435 mp_image_t *dmpi=mpi->priv;
437 // printf("vf_scale::put_image(): processing whole frame! dmpi=%p flag=%d\n",
438 // dmpi, (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK));
440 if(!(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK && dmpi)){
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);
447 scale(vf->priv->ctx, vf->priv->ctx, mpi->planes,mpi->stride,0,mpi->h,dmpi->planes,dmpi->stride, vf->priv->interlaced);
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);
456 if(vf->priv->palette) dmpi->planes[1]=vf->priv->palette; // export palette!
458 return vf_next_put_image(vf,dmpi, pts);
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;
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;
474 eq = data;
475 if (!strcmp(eq->item,"brightness")) {
476 eq->value = ((brightness*100) + (1<<15))>>16;
478 else if (!strcmp(eq->item,"contrast")) {
479 eq->value = (((contrast *100) + (1<<15))>>16) - 100;
481 else if (!strcmp(eq->item,"saturation")) {
482 eq->value = (((saturation*100) + (1<<15))>>16) - 100;
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;
493 if (!strcmp(eq->item,"brightness")) {
494 brightness = (( eq->value <<16) + 50)/100;
496 else if (!strcmp(eq->item,"contrast")) {
497 contrast = (((eq->value+100)<<16) + 50)/100;
499 else if (!strcmp(eq->item,"saturation")) {
500 saturation = (((eq->value+100)<<16) + 50)/100;
502 else
503 break;
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;
512 return CONTROL_TRUE;
513 case VFCTRL_SET_YUV_COLORSPACE: {
514 struct mp_csp_details colorspace = *(struct mp_csp_details *)data;
515 if (mp_sws_set_colorspace(vf->priv->ctx, &colorspace) >= 0) {
516 if (vf->priv->ctx2)
517 mp_sws_set_colorspace(vf->priv->ctx2, &colorspace);
518 vf->priv->colorspace = colorspace;
519 return 1;
521 break;
523 case VFCTRL_GET_YUV_COLORSPACE: {
524 /* This scale filter should never react to colorspace commands if it
525 * doesn't do YUV->RGB conversion. But because finding out whether this
526 * is really YUV->RGB (and not YUV->YUV or anything else) is hard,
527 * react only if the colorspace has been set explicitly before. The
528 * trick is that mp_sws_set_colorspace does not succeed for YUV->YUV
529 * and RGB->YUV conversions, which makes this code correct in "most"
530 * cases. (This would be trivial to do correctly if libswscale exposed
531 * functionality like isYUV()).
533 if (vf->priv->colorspace.format) {
534 *(struct mp_csp_details *)data = vf->priv->colorspace;
535 return CONTROL_TRUE;
537 break;
539 default:
540 break;
543 return vf_next_control(vf,request,data);
546 static const int mp_csp_to_swscale[MP_CSP_COUNT] = {
547 [MP_CSP_BT_601] = SWS_CS_ITU601,
548 [MP_CSP_BT_709] = SWS_CS_ITU709,
549 [MP_CSP_SMPTE_240M] = SWS_CS_SMPTE240M,
552 // Adjust the colorspace used for YUV->RGB conversion. On other conversions,
553 // do nothing or return an error.
554 // The csp argument is set to the supported values.
555 // Return 0 on success and -1 on error.
556 int mp_sws_set_colorspace(struct SwsContext *sws, struct mp_csp_details *csp)
558 int *table, *inv_table;
559 int brightness, contrast, saturation, srcRange, dstRange;
561 csp->levels_out = MP_CSP_LEVELS_PC;
563 // NOTE: returns an error if the destination format is YUV
564 if (sws_getColorspaceDetails(sws, &inv_table, &srcRange, &table, &dstRange,
565 &brightness, &contrast, &saturation) == -1)
566 goto error_out;
568 int sws_csp = mp_csp_to_swscale[csp->format];
569 if (sws_csp == 0) {
570 // colorspace not supported, go with a reasonable default
571 csp->format = SWS_CS_ITU601;
572 sws_csp = MP_CSP_BT_601;
575 /* The swscale API for these is hardly documented.
576 * Apparently table/range only apply to YUV. Thus dstRange has no effect
577 * for YUV->RGB conversions, and conversions to limited-range RGB are
578 * not supported.
580 srcRange = csp->levels_in == MP_CSP_LEVELS_PC;
581 const int *new_inv_table = sws_getCoefficients(sws_csp);
583 if (sws_setColorspaceDetails(sws, new_inv_table, srcRange, table, dstRange,
584 brightness, contrast, saturation) == -1)
585 goto error_out;
587 return 0;
589 error_out:
590 *csp = (struct mp_csp_details){0};
591 return -1;
594 //===========================================================================//
596 // supported Input formats: YV12, I420, IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8, Y800
598 static int query_format(struct vf_instance *vf, unsigned int fmt){
599 if (!IMGFMT_IS_HWACCEL(fmt) && imgfmt2pixfmt(fmt) != PIX_FMT_NONE) {
600 unsigned int best=find_best_out(vf, fmt);
601 int flags;
602 if(!best) return 0; // no matching out-fmt
603 flags=vf_next_query_format(vf,best);
604 if(!(flags&(VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW))) return 0; // huh?
605 if(fmt!=best) flags&=~VFCAP_CSP_SUPPORTED_BY_HW;
606 // do not allow scaling, if we are before the PP fliter!
607 if(!(flags&VFCAP_POSTPROC)) flags|=VFCAP_SWSCALE;
608 return flags;
610 return 0; // nomatching in-fmt
613 static void uninit(struct vf_instance *vf){
614 if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
615 if(vf->priv->ctx2) sws_freeContext(vf->priv->ctx2);
616 free(vf->priv->palette);
617 free(vf->priv);
620 static int vf_open(vf_instance_t *vf, char *args){
621 vf->config=config;
622 vf->start_slice=start_slice;
623 vf->draw_slice=draw_slice;
624 vf->put_image=put_image;
625 vf->query_format=query_format;
626 vf->control= control;
627 vf->uninit=uninit;
628 mp_msg(MSGT_VFILTER,MSGL_V,"SwScale params: %d x %d (-1=no scaling)\n",
629 vf->priv->w,
630 vf->priv->h);
632 return 1;
635 //global sws_flags from the command line
636 int sws_flags=2;
638 //global srcFilter
639 static SwsFilter *src_filter= NULL;
641 float sws_lum_gblur= 0.0;
642 float sws_chr_gblur= 0.0;
643 int sws_chr_vshift= 0;
644 int sws_chr_hshift= 0;
645 float sws_chr_sharpen= 0.0;
646 float sws_lum_sharpen= 0.0;
648 int get_sws_cpuflags(void){
649 return
650 (gCpuCaps.hasMMX ? SWS_CPU_CAPS_MMX : 0)
651 | (gCpuCaps.hasMMX2 ? SWS_CPU_CAPS_MMX2 : 0)
652 | (gCpuCaps.has3DNow ? SWS_CPU_CAPS_3DNOW : 0)
653 | (gCpuCaps.hasAltiVec ? SWS_CPU_CAPS_ALTIVEC : 0);
656 void sws_getFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam)
658 static int firstTime=1;
659 *flags=0;
661 #if ARCH_X86
662 if(gCpuCaps.hasMMX)
663 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
664 #endif
665 if(firstTime)
667 firstTime=0;
668 *flags= SWS_PRINT_INFO;
670 else if( mp_msg_test(MSGT_VFILTER,MSGL_DBG2) ) *flags= SWS_PRINT_INFO;
672 if(src_filter) sws_freeFilter(src_filter);
674 src_filter= sws_getDefaultFilter(
675 sws_lum_gblur, sws_chr_gblur,
676 sws_lum_sharpen, sws_chr_sharpen,
677 sws_chr_hshift, sws_chr_vshift, verbose>1);
679 switch(sws_flags)
681 case 0: *flags|= SWS_FAST_BILINEAR; break;
682 case 1: *flags|= SWS_BILINEAR; break;
683 case 2: *flags|= SWS_BICUBIC; break;
684 case 3: *flags|= SWS_X; break;
685 case 4: *flags|= SWS_POINT; break;
686 case 5: *flags|= SWS_AREA; break;
687 case 6: *flags|= SWS_BICUBLIN; break;
688 case 7: *flags|= SWS_GAUSS; break;
689 case 8: *flags|= SWS_SINC; break;
690 case 9: *flags|= SWS_LANCZOS; break;
691 case 10:*flags|= SWS_SPLINE; break;
692 default:*flags|= SWS_BILINEAR; break;
695 *srcFilterParam= src_filter;
696 *dstFilterParam= NULL;
699 // will use sws_flags & src_filter (from cmd line)
700 struct SwsContext *sws_getContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
702 int flags;
703 SwsFilter *dstFilterParam, *srcFilterParam;
704 enum PixelFormat dfmt, sfmt;
706 dfmt = imgfmt2pixfmt(dstFormat);
707 sfmt = imgfmt2pixfmt(srcFormat);
708 if (srcFormat == IMGFMT_RGB8 || srcFormat == IMGFMT_BGR8) sfmt = PIX_FMT_PAL8;
709 sws_getFlagsAndFilterFromCmdLine(&flags, &srcFilterParam, &dstFilterParam);
711 return sws_getContext(srcW, srcH, sfmt, dstW, dstH, dfmt, flags | get_sws_cpuflags(), srcFilterParam, dstFilterParam, NULL);
714 /// An example of presets usage
715 static const struct size_preset {
716 char* name;
717 int w, h;
718 } vf_size_presets_defs[] = {
719 // TODO add more 'standard' resolutions
720 { "qntsc", 352, 240 },
721 { "qpal", 352, 288 },
722 { "ntsc", 720, 480 },
723 { "pal", 720, 576 },
724 { "sntsc", 640, 480 },
725 { "spal", 768, 576 },
726 { NULL, 0, 0}
729 #define ST_OFF(f) M_ST_OFF(struct size_preset,f)
730 static const m_option_t vf_size_preset_fields[] = {
731 {"w", ST_OFF(w), CONF_TYPE_INT, M_OPT_MIN,1 ,0, NULL},
732 {"h", ST_OFF(h), CONF_TYPE_INT, M_OPT_MIN,1 ,0, NULL},
733 { NULL, NULL, 0, 0, 0, 0, NULL }
736 static const m_struct_t vf_size_preset = {
737 "scale_size_preset",
738 sizeof(struct size_preset),
739 NULL,
740 vf_size_preset_fields
743 static const m_struct_t vf_opts;
744 static const m_obj_presets_t size_preset = {
745 &vf_size_preset, // Input struct desc
746 &vf_opts, // Output struct desc
747 vf_size_presets_defs, // The list of presets
748 ST_OFF(name) // At wich offset is the name field in the preset struct
751 /// Now the options
752 #undef ST_OFF
753 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
754 static const m_option_t vf_opts_fields[] = {
755 {"w", ST_OFF(w), CONF_TYPE_INT, M_OPT_MIN,-11,0, NULL},
756 {"h", ST_OFF(h), CONF_TYPE_INT, M_OPT_MIN,-11,0, NULL},
757 {"interlaced", ST_OFF(interlaced), CONF_TYPE_INT, M_OPT_RANGE, 0, 1, NULL},
758 {"chr-drop", ST_OFF(v_chr_drop), CONF_TYPE_INT, M_OPT_RANGE, 0, 3, NULL},
759 {"param" , ST_OFF(param[0]), CONF_TYPE_DOUBLE, M_OPT_RANGE, 0.0, 100.0, NULL},
760 {"param2", ST_OFF(param[1]), CONF_TYPE_DOUBLE, M_OPT_RANGE, 0.0, 100.0, NULL},
761 // Note that here the 2 field is NULL (ie 0)
762 // As we want this option to act on the option struct itself
763 {"presize", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0, (void *)&size_preset},
764 {"noup", ST_OFF(noup), CONF_TYPE_INT, M_OPT_RANGE, 0, 2, NULL},
765 {"arnd", ST_OFF(accurate_rnd), CONF_TYPE_FLAG, 0, 0, 1, NULL},
766 { NULL, NULL, 0, 0, 0, 0, NULL }
769 static const m_struct_t vf_opts = {
770 "scale",
771 sizeof(struct vf_priv_s),
772 &vf_priv_dflt,
773 vf_opts_fields
776 const vf_info_t vf_info_scale = {
777 "software scaling",
778 "scale",
779 "A'rpi",
781 vf_open,
782 &vf_opts
785 //===========================================================================//