mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / libmpcodecs / vf_scale.c
bloba9663abdaf2d59928b192c9c34c1b53376f10072
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_422P9_LE,
88 IMGFMT_422P9_BE,
89 IMGFMT_YV12,
90 IMGFMT_I420,
91 IMGFMT_420P16_LE,
92 IMGFMT_420P16_BE,
93 IMGFMT_420P10_LE,
94 IMGFMT_420P10_BE,
95 IMGFMT_420P9_LE,
96 IMGFMT_420P9_BE,
97 IMGFMT_420A,
98 IMGFMT_IYUV,
99 IMGFMT_YVU9,
100 IMGFMT_IF09,
101 IMGFMT_411P,
102 IMGFMT_NV12,
103 IMGFMT_NV21,
104 IMGFMT_YUY2,
105 IMGFMT_UYVY,
106 IMGFMT_440P,
107 // RGB and grayscale (Y8 and Y800):
108 IMGFMT_BGR32,
109 IMGFMT_RGB32,
110 IMGFMT_BGR24,
111 IMGFMT_RGB24,
112 IMGFMT_GBRP,
113 IMGFMT_GBRP10,
114 IMGFMT_GBRP9,
115 IMGFMT_RGB48LE,
116 IMGFMT_RGB48BE,
117 IMGFMT_BGR16,
118 IMGFMT_RGB16,
119 IMGFMT_BGR15,
120 IMGFMT_RGB15,
121 IMGFMT_BGR12,
122 IMGFMT_RGB12,
123 IMGFMT_Y800,
124 IMGFMT_Y8,
125 IMGFMT_BGR8,
126 IMGFMT_RGB8,
127 IMGFMT_BGR4,
128 IMGFMT_RGB4,
129 IMGFMT_BG4B,
130 IMGFMT_RG4B,
131 IMGFMT_BGR1,
132 IMGFMT_RGB1,
137 * A list of preferred conversions, in order of preference.
138 * This should be used for conversions that e.g. involve no scaling
139 * or to stop vf_scale from choosing a conversion that has no
140 * fast assembler implementation.
142 static int preferred_conversions[][2] = {
143 {IMGFMT_YUY2, IMGFMT_UYVY},
144 {IMGFMT_YUY2, IMGFMT_422P},
145 {IMGFMT_UYVY, IMGFMT_YUY2},
146 {IMGFMT_UYVY, IMGFMT_422P},
147 {IMGFMT_422P, IMGFMT_YUY2},
148 {IMGFMT_422P, IMGFMT_UYVY},
149 {IMGFMT_GBRP, IMGFMT_BGR24},
150 {IMGFMT_GBRP, IMGFMT_RGB24},
151 {IMGFMT_GBRP, IMGFMT_BGR32},
152 {IMGFMT_GBRP, IMGFMT_RGB32},
153 {0, 0}
156 static unsigned int find_best_out(vf_instance_t *vf, int in_format){
157 unsigned int best=0;
158 int i = -1;
159 int j = -1;
160 int format = 0;
162 // find the best outfmt:
163 while (1) {
164 int ret;
165 if (j < 0) {
166 format = in_format;
167 j = 0;
168 } else if (i < 0) {
169 while (preferred_conversions[j][0] &&
170 preferred_conversions[j][0] != in_format)
171 j++;
172 format = preferred_conversions[j++][1];
173 // switch to standard list
174 if (!format)
175 i = 0;
177 if (i >= 0)
178 format = outfmt_list[i++];
179 if (!format)
180 break;
181 ret = vf_next_query_format(vf, format);
183 mp_msg(MSGT_VFILTER,MSGL_DBG2,"scale: query(%s) -> %d\n",vo_format_name(format),ret&3);
184 if(ret&VFCAP_CSP_SUPPORTED_BY_HW){
185 best=format; // no conversion -> bingo!
186 break;
188 if(ret&VFCAP_CSP_SUPPORTED && !best)
189 best=format; // best with conversion
191 return best;
194 static int config(struct vf_instance *vf,
195 int width, int height, int d_width, int d_height,
196 unsigned int flags, unsigned int outfmt){
197 struct MPOpts *opts = vf->opts;
198 unsigned int best=find_best_out(vf, outfmt);
199 int vo_flags;
200 int int_sws_flags=0;
201 int round_w=0, round_h=0;
202 int i;
203 SwsFilter *srcFilter, *dstFilter;
204 enum PixelFormat dfmt, sfmt;
206 vf->priv->colorspace = (struct mp_csp_details) {0};
208 if(!best){
209 mp_msg(MSGT_VFILTER,MSGL_WARN,"SwScale: no supported outfmt found :(\n");
210 return 0;
212 sfmt = imgfmt2pixfmt(outfmt);
213 if (outfmt == IMGFMT_RGB8 || outfmt == IMGFMT_BGR8) sfmt = PIX_FMT_PAL8;
214 dfmt = imgfmt2pixfmt(best);
216 vo_flags=vf->next->query_format(vf->next,best);
218 vf->priv->w = vf->priv->cfg_w;
219 vf->priv->h = vf->priv->cfg_h;
221 // scaling to dwidth*d_height, if all these TRUE:
222 // - option -zoom
223 // - no other sw/hw up/down scaling avail.
224 // - we're after postproc
225 // - user didn't set w:h
226 if(!(vo_flags&VFCAP_POSTPROC) && (flags&VOFLAG_SWSCALE) &&
227 vf->priv->w<0 && vf->priv->h<0){ // -zoom
228 int x=(vo_flags&VFCAP_SWSCALE) ? 0 : 1;
229 if(d_width<width || d_height<height){
230 // downscale!
231 if(vo_flags&VFCAP_HWSCALE_DOWN) x=0;
232 } else {
233 // upscale:
234 if(vo_flags&VFCAP_HWSCALE_UP) x=0;
236 if(x){
237 // user wants sw scaling! (-zoom)
238 vf->priv->w=d_width;
239 vf->priv->h=d_height;
243 if(vf->priv->noup){
244 if((vf->priv->w > width) + (vf->priv->h > height) >= vf->priv->noup){
245 vf->priv->w= width;
246 vf->priv->h= height;
250 if (vf->priv->w <= -8) {
251 vf->priv->w += 8;
252 round_w = 1;
254 if (vf->priv->h <= -8) {
255 vf->priv->h += 8;
256 round_h = 1;
259 if (vf->priv->w < -3 || vf->priv->h < -3 ||
260 (vf->priv->w < -1 && vf->priv->h < -1)) {
261 // TODO: establish a direct connection to the user's brain
262 // and find out what the heck he thinks MPlayer should do
263 // with this nonsense.
264 mp_msg(MSGT_VFILTER, MSGL_ERR, "SwScale: EUSERBROKEN Check your parameters, they make no sense!\n");
265 return 0;
268 if (vf->priv->w == -1)
269 vf->priv->w = width;
270 if (vf->priv->w == 0)
271 vf->priv->w = d_width;
273 if (vf->priv->h == -1)
274 vf->priv->h = height;
275 if (vf->priv->h == 0)
276 vf->priv->h = d_height;
278 if (vf->priv->w == -3)
279 vf->priv->w = vf->priv->h * width / height;
280 if (vf->priv->w == -2)
281 vf->priv->w = vf->priv->h * d_width / d_height;
283 if (vf->priv->h == -3)
284 vf->priv->h = vf->priv->w * height / width;
285 if (vf->priv->h == -2)
286 vf->priv->h = vf->priv->w * d_height / d_width;
288 if (round_w)
289 vf->priv->w = ((vf->priv->w + 8) / 16) * 16;
290 if (round_h)
291 vf->priv->h = ((vf->priv->h + 8) / 16) * 16;
293 // calculate the missing parameters:
294 switch(best) {
295 case IMGFMT_YV12: /* YV12 needs w & h rounded to 2 */
296 case IMGFMT_I420:
297 case IMGFMT_IYUV:
298 case IMGFMT_NV12:
299 case IMGFMT_NV21:
300 vf->priv->h = (vf->priv->h + 1) & ~1;
301 case IMGFMT_YUY2: /* YUY2 needs w rounded to 2 */
302 case IMGFMT_UYVY:
303 vf->priv->w = (vf->priv->w + 1) & ~1;
306 mp_msg(MSGT_VFILTER,MSGL_DBG2,"SwScale: scaling %dx%d %s to %dx%d %s \n",
307 width,height,vo_format_name(outfmt),
308 vf->priv->w,vf->priv->h,vo_format_name(best));
310 // free old ctx:
311 if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
312 if(vf->priv->ctx2)sws_freeContext(vf->priv->ctx2);
314 // new swscaler:
315 sws_getFlagsAndFilterFromCmdLine(&int_sws_flags, &srcFilter, &dstFilter);
316 int_sws_flags|= vf->priv->v_chr_drop << SWS_SRC_V_CHR_DROP_SHIFT;
317 int_sws_flags|= vf->priv->accurate_rnd * SWS_ACCURATE_RND;
318 vf->priv->ctx=sws_getContext(width, height >> vf->priv->interlaced,
319 sfmt,
320 vf->priv->w, vf->priv->h >> vf->priv->interlaced,
321 dfmt,
322 int_sws_flags | get_sws_cpuflags(), srcFilter, dstFilter, vf->priv->param);
323 if(vf->priv->interlaced){
324 vf->priv->ctx2=sws_getContext(width, height >> 1,
325 sfmt,
326 vf->priv->w, vf->priv->h >> 1,
327 dfmt,
328 int_sws_flags | get_sws_cpuflags(), srcFilter, dstFilter, vf->priv->param);
330 if(!vf->priv->ctx){
331 // error...
332 mp_msg(MSGT_VFILTER,MSGL_WARN,"Couldn't init SwScaler for this setup\n");
333 return 0;
335 vf->priv->fmt=best;
337 free(vf->priv->palette);
338 vf->priv->palette=NULL;
339 switch(best){
340 case IMGFMT_RGB8: {
341 /* set 332 palette for 8 bpp */
342 vf->priv->palette=malloc(4*256);
343 for(i=0; i<256; i++){
344 vf->priv->palette[4*i+0]=4*(i>>6)*21;
345 vf->priv->palette[4*i+1]=4*((i>>3)&7)*9;
346 vf->priv->palette[4*i+2]=4*((i&7)&7)*9;
347 vf->priv->palette[4*i+3]=0;
349 break; }
350 case IMGFMT_BGR8: {
351 /* set 332 palette for 8 bpp */
352 vf->priv->palette=malloc(4*256);
353 for(i=0; i<256; i++){
354 vf->priv->palette[4*i+0]=4*(i&3)*21;
355 vf->priv->palette[4*i+1]=4*((i>>2)&7)*9;
356 vf->priv->palette[4*i+2]=4*((i>>5)&7)*9;
357 vf->priv->palette[4*i+3]=0;
359 break; }
360 case IMGFMT_BGR4:
361 case IMGFMT_BG4B: {
362 vf->priv->palette=malloc(4*16);
363 for(i=0; i<16; i++){
364 vf->priv->palette[4*i+0]=4*(i&1)*63;
365 vf->priv->palette[4*i+1]=4*((i>>1)&3)*21;
366 vf->priv->palette[4*i+2]=4*((i>>3)&1)*63;
367 vf->priv->palette[4*i+3]=0;
369 break; }
370 case IMGFMT_RGB4:
371 case IMGFMT_RG4B: {
372 vf->priv->palette=malloc(4*16);
373 for(i=0; i<16; i++){
374 vf->priv->palette[4*i+0]=4*(i>>3)*63;
375 vf->priv->palette[4*i+1]=4*((i>>1)&3)*21;
376 vf->priv->palette[4*i+2]=4*((i&1)&1)*63;
377 vf->priv->palette[4*i+3]=0;
379 break; }
382 if (!opts->screen_size_x && !opts->screen_size_y
383 && !(opts->screen_size_xy >= 0.001)) {
384 // Compute new d_width and d_height, preserving aspect
385 // while ensuring that both are >= output size in pixels.
386 if (vf->priv->h * d_width > vf->priv->w * d_height) {
387 d_width = vf->priv->h * d_width / d_height;
388 d_height = vf->priv->h;
389 } else {
390 d_height = vf->priv->w * d_height / d_width;
391 d_width = vf->priv->w;
393 //d_width=d_width*vf->priv->w/width;
394 //d_height=d_height*vf->priv->h/height;
396 return vf_next_config(vf,vf->priv->w,vf->priv->h,d_width,d_height,flags,best);
399 static void start_slice(struct vf_instance *vf, mp_image_t *mpi){
400 // printf("start_slice called! flag=%d\n",mpi->flags&MP_IMGFLAG_DRAW_CALLBACK);
401 if(!(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)) return; // shouldn't happen
402 // they want slices!!! allocate the buffer.
403 mpi->priv=vf->dmpi=vf_get_image(vf->next,vf->priv->fmt,
404 // mpi->type, mpi->flags & (~MP_IMGFLAG_DRAW_CALLBACK),
405 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
406 vf->priv->w, vf->priv->h);
409 static void scale(struct SwsContext *sws1, struct SwsContext *sws2, uint8_t *src[MP_MAX_PLANES], int src_stride[MP_MAX_PLANES],
410 int y, int h, uint8_t *dst[MP_MAX_PLANES], int dst_stride[MP_MAX_PLANES], int interlaced){
411 const uint8_t *src2[MP_MAX_PLANES]={src[0], src[1], src[2], src[3]};
412 #if HAVE_BIGENDIAN
413 uint32_t pal2[256];
414 if (src[1] && !src[2]){
415 int i;
416 for(i=0; i<256; i++)
417 pal2[i]= bswap_32(((uint32_t*)src[1])[i]);
418 src2[1]= pal2;
420 #endif
422 if(interlaced){
423 int i;
424 uint8_t *dst2[MP_MAX_PLANES]={dst[0], dst[1], dst[2], dst[3]};
425 int src_stride2[MP_MAX_PLANES]={2*src_stride[0], 2*src_stride[1], 2*src_stride[2], 2*src_stride[3]};
426 int dst_stride2[MP_MAX_PLANES]={2*dst_stride[0], 2*dst_stride[1], 2*dst_stride[2], 2*dst_stride[3]};
428 sws_scale(sws1, src2, src_stride2, y>>1, h>>1, dst2, dst_stride2);
429 for(i=0; i<MP_MAX_PLANES; i++){
430 src2[i] += src_stride[i];
431 dst2[i] += dst_stride[i];
433 sws_scale(sws2, src2, src_stride2, y>>1, h>>1, dst2, dst_stride2);
434 }else{
435 sws_scale(sws1, src2, src_stride, y, h, dst, dst_stride);
439 static void draw_slice(struct vf_instance *vf,
440 unsigned char** src, int* stride, int w,int h, int x, int y){
441 mp_image_t *dmpi=vf->dmpi;
442 if(!dmpi){
443 mp_msg(MSGT_VFILTER,MSGL_FATAL,"vf_scale: draw_slice() called with dmpi=NULL (no get_image?)\n");
444 return;
446 // printf("vf_scale::draw_slice() y=%d h=%d\n",y,h);
447 scale(vf->priv->ctx, vf->priv->ctx2, src, stride, y, h, dmpi->planes, dmpi->stride, vf->priv->interlaced);
450 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
451 mp_image_t *dmpi=mpi->priv;
453 // printf("vf_scale::put_image(): processing whole frame! dmpi=%p flag=%d\n",
454 // dmpi, (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK));
456 if(!(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK && dmpi)){
458 // hope we'll get DR buffer:
459 dmpi=vf_get_image(vf->next,vf->priv->fmt,
460 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
461 vf->priv->w, vf->priv->h);
463 scale(vf->priv->ctx, vf->priv->ctx, mpi->planes,mpi->stride,0,mpi->h,dmpi->planes,dmpi->stride, vf->priv->interlaced);
466 if(vf->priv->w==mpi->w && vf->priv->h==mpi->h){
467 // just conversion, no scaling -> keep postprocessing data
468 // this way we can apply pp filter to non-yv12 source using scaler
469 vf_clone_mpi_attributes(dmpi, mpi);
472 if(vf->priv->palette) dmpi->planes[1]=vf->priv->palette; // export palette!
474 return vf_next_put_image(vf,dmpi, pts);
477 static int control(struct vf_instance *vf, int request, void* data){
478 int *table;
479 int *inv_table;
480 int r;
481 int brightness, contrast, saturation, srcRange, dstRange;
482 vf_equalizer_t *eq;
484 if(vf->priv->ctx)
485 switch(request){
486 case VFCTRL_GET_EQUALIZER:
487 r= sws_getColorspaceDetails(vf->priv->ctx, &inv_table, &srcRange, &table, &dstRange, &brightness, &contrast, &saturation);
488 if(r<0) break;
490 eq = data;
491 if (!strcmp(eq->item,"brightness")) {
492 eq->value = ((brightness*100) + (1<<15))>>16;
494 else if (!strcmp(eq->item,"contrast")) {
495 eq->value = (((contrast *100) + (1<<15))>>16) - 100;
497 else if (!strcmp(eq->item,"saturation")) {
498 eq->value = (((saturation*100) + (1<<15))>>16) - 100;
500 else
501 break;
502 return CONTROL_TRUE;
503 case VFCTRL_SET_EQUALIZER:
504 r= sws_getColorspaceDetails(vf->priv->ctx, &inv_table, &srcRange, &table, &dstRange, &brightness, &contrast, &saturation);
505 if(r<0) break;
506 //printf("set %f %f %f\n", brightness/(float)(1<<16), contrast/(float)(1<<16), saturation/(float)(1<<16));
507 eq = data;
509 if (!strcmp(eq->item,"brightness")) {
510 brightness = (( eq->value <<16) + 50)/100;
512 else if (!strcmp(eq->item,"contrast")) {
513 contrast = (((eq->value+100)<<16) + 50)/100;
515 else if (!strcmp(eq->item,"saturation")) {
516 saturation = (((eq->value+100)<<16) + 50)/100;
518 else
519 break;
521 r= sws_setColorspaceDetails(vf->priv->ctx, inv_table, srcRange, table, dstRange, brightness, contrast, saturation);
522 if(r<0) break;
523 if(vf->priv->ctx2){
524 r= sws_setColorspaceDetails(vf->priv->ctx2, inv_table, srcRange, table, dstRange, brightness, contrast, saturation);
525 if(r<0) break;
528 return CONTROL_TRUE;
529 case VFCTRL_SET_YUV_COLORSPACE: {
530 struct mp_csp_details colorspace = *(struct mp_csp_details *)data;
531 if (mp_sws_set_colorspace(vf->priv->ctx, &colorspace) >= 0) {
532 if (vf->priv->ctx2)
533 mp_sws_set_colorspace(vf->priv->ctx2, &colorspace);
534 vf->priv->colorspace = colorspace;
535 return 1;
537 break;
539 case VFCTRL_GET_YUV_COLORSPACE: {
540 /* This scale filter should never react to colorspace commands if it
541 * doesn't do YUV->RGB conversion. But because finding out whether this
542 * is really YUV->RGB (and not YUV->YUV or anything else) is hard,
543 * react only if the colorspace has been set explicitly before. The
544 * trick is that mp_sws_set_colorspace does not succeed for YUV->YUV
545 * and RGB->YUV conversions, which makes this code correct in "most"
546 * cases. (This would be trivial to do correctly if libswscale exposed
547 * functionality like isYUV()).
549 if (vf->priv->colorspace.format) {
550 *(struct mp_csp_details *)data = vf->priv->colorspace;
551 return CONTROL_TRUE;
553 break;
555 default:
556 break;
559 return vf_next_control(vf,request,data);
562 static const int mp_csp_to_swscale[MP_CSP_COUNT] = {
563 [MP_CSP_BT_601] = SWS_CS_ITU601,
564 [MP_CSP_BT_709] = SWS_CS_ITU709,
565 [MP_CSP_SMPTE_240M] = SWS_CS_SMPTE240M,
568 // Adjust the colorspace used for YUV->RGB conversion. On other conversions,
569 // do nothing or return an error.
570 // The csp argument is set to the supported values.
571 // Return 0 on success and -1 on error.
572 int mp_sws_set_colorspace(struct SwsContext *sws, struct mp_csp_details *csp)
574 int *table, *inv_table;
575 int brightness, contrast, saturation, srcRange, dstRange;
577 csp->levels_out = MP_CSP_LEVELS_PC;
579 // NOTE: returns an error if the destination format is YUV
580 if (sws_getColorspaceDetails(sws, &inv_table, &srcRange, &table, &dstRange,
581 &brightness, &contrast, &saturation) == -1)
582 goto error_out;
584 int sws_csp = mp_csp_to_swscale[csp->format];
585 if (sws_csp == 0) {
586 // colorspace not supported, go with a reasonable default
587 csp->format = SWS_CS_ITU601;
588 sws_csp = MP_CSP_BT_601;
591 /* The swscale API for these is hardly documented.
592 * Apparently table/range only apply to YUV. Thus dstRange has no effect
593 * for YUV->RGB conversions, and conversions to limited-range RGB are
594 * not supported.
596 srcRange = csp->levels_in == MP_CSP_LEVELS_PC;
597 const int *new_inv_table = sws_getCoefficients(sws_csp);
599 if (sws_setColorspaceDetails(sws, new_inv_table, srcRange, table, dstRange,
600 brightness, contrast, saturation) == -1)
601 goto error_out;
603 return 0;
605 error_out:
606 *csp = (struct mp_csp_details){0};
607 return -1;
610 //===========================================================================//
612 // supported Input formats: YV12, I420, IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8, Y800
614 static int query_format(struct vf_instance *vf, unsigned int fmt){
615 if (!IMGFMT_IS_HWACCEL(fmt) && imgfmt2pixfmt(fmt) != PIX_FMT_NONE) {
616 unsigned int best=find_best_out(vf, fmt);
617 int flags;
618 if(!best) return 0; // no matching out-fmt
619 flags=vf_next_query_format(vf,best);
620 if(!(flags&(VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW))) return 0; // huh?
621 if(fmt!=best) flags&=~VFCAP_CSP_SUPPORTED_BY_HW;
622 // do not allow scaling, if we are before the PP fliter!
623 if(!(flags&VFCAP_POSTPROC)) flags|=VFCAP_SWSCALE;
624 return flags;
626 return 0; // nomatching in-fmt
629 static void uninit(struct vf_instance *vf){
630 if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
631 if(vf->priv->ctx2) sws_freeContext(vf->priv->ctx2);
632 free(vf->priv->palette);
633 free(vf->priv);
636 static int vf_open(vf_instance_t *vf, char *args){
637 vf->config=config;
638 vf->start_slice=start_slice;
639 vf->draw_slice=draw_slice;
640 vf->put_image=put_image;
641 vf->query_format=query_format;
642 vf->control= control;
643 vf->uninit=uninit;
644 mp_msg(MSGT_VFILTER,MSGL_V,"SwScale params: %d x %d (-1=no scaling)\n",
645 vf->priv->cfg_w,
646 vf->priv->cfg_h);
648 return 1;
651 //global sws_flags from the command line
652 int sws_flags=2;
654 //global srcFilter
655 static SwsFilter *src_filter= NULL;
657 float sws_lum_gblur= 0.0;
658 float sws_chr_gblur= 0.0;
659 int sws_chr_vshift= 0;
660 int sws_chr_hshift= 0;
661 float sws_chr_sharpen= 0.0;
662 float sws_lum_sharpen= 0.0;
664 int get_sws_cpuflags(void){
665 return
666 (gCpuCaps.hasMMX ? SWS_CPU_CAPS_MMX : 0)
667 | (gCpuCaps.hasMMX2 ? SWS_CPU_CAPS_MMX2 : 0)
668 | (gCpuCaps.has3DNow ? SWS_CPU_CAPS_3DNOW : 0)
669 | (gCpuCaps.hasAltiVec ? SWS_CPU_CAPS_ALTIVEC : 0);
672 void sws_getFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam)
674 static int firstTime=1;
675 *flags=0;
677 #if ARCH_X86
678 if(gCpuCaps.hasMMX)
679 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
680 #endif
681 if(firstTime)
683 firstTime=0;
684 *flags= SWS_PRINT_INFO;
686 else if( mp_msg_test(MSGT_VFILTER,MSGL_DBG2) ) *flags= SWS_PRINT_INFO;
688 if(src_filter) sws_freeFilter(src_filter);
690 src_filter= sws_getDefaultFilter(
691 sws_lum_gblur, sws_chr_gblur,
692 sws_lum_sharpen, sws_chr_sharpen,
693 sws_chr_hshift, sws_chr_vshift, verbose>1);
695 switch(sws_flags)
697 case 0: *flags|= SWS_FAST_BILINEAR; break;
698 case 1: *flags|= SWS_BILINEAR; break;
699 case 2: *flags|= SWS_BICUBIC; break;
700 case 3: *flags|= SWS_X; break;
701 case 4: *flags|= SWS_POINT; break;
702 case 5: *flags|= SWS_AREA; break;
703 case 6: *flags|= SWS_BICUBLIN; break;
704 case 7: *flags|= SWS_GAUSS; break;
705 case 8: *flags|= SWS_SINC; break;
706 case 9: *flags|= SWS_LANCZOS; break;
707 case 10:*flags|= SWS_SPLINE; break;
708 default:*flags|= SWS_BILINEAR; break;
711 *srcFilterParam= src_filter;
712 *dstFilterParam= NULL;
715 // will use sws_flags & src_filter (from cmd line)
716 static struct SwsContext *sws_getContextFromCmdLine2(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int extraflags)
718 int flags;
719 SwsFilter *dstFilterParam, *srcFilterParam;
720 enum PixelFormat dfmt, sfmt;
722 dfmt = imgfmt2pixfmt(dstFormat);
723 sfmt = imgfmt2pixfmt(srcFormat);
724 if (srcFormat == IMGFMT_RGB8 || srcFormat == IMGFMT_BGR8) sfmt = PIX_FMT_PAL8;
725 sws_getFlagsAndFilterFromCmdLine(&flags, &srcFilterParam, &dstFilterParam);
727 return sws_getContext(srcW, srcH, sfmt, dstW, dstH, dfmt, flags | extraflags | get_sws_cpuflags(), srcFilterParam, dstFilterParam, NULL);
730 struct SwsContext *sws_getContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
732 return sws_getContextFromCmdLine2(srcW, srcH, srcFormat, dstW, dstH, dstFormat, 0);
735 /* These extra flags improve the image visibly in some cases, but also
736 * make conversion a lot slower. For some reason SWS_FULL_CHR_H_INT
737 * also seems to make the overall image slightly brighter.
739 struct SwsContext *sws_getContextFromCmdLine_hq(int srcW, int srcH,
740 int srcFormat, int dstW, int dstH, int dstFormat)
742 return sws_getContextFromCmdLine2(srcW, srcH, srcFormat,
743 dstW, dstH, dstFormat,
744 SWS_FULL_CHR_H_INT | SWS_ACCURATE_RND);
747 /// An example of presets usage
748 static const struct size_preset {
749 char* name;
750 int w, h;
751 } vf_size_presets_defs[] = {
752 // TODO add more 'standard' resolutions
753 { "qntsc", 352, 240 },
754 { "qpal", 352, 288 },
755 { "ntsc", 720, 480 },
756 { "pal", 720, 576 },
757 { "sntsc", 640, 480 },
758 { "spal", 768, 576 },
759 { NULL, 0, 0}
762 #define ST_OFF(f) M_ST_OFF(struct size_preset,f)
763 static const m_option_t vf_size_preset_fields[] = {
764 {"w", ST_OFF(w), CONF_TYPE_INT, M_OPT_MIN,1 ,0, NULL},
765 {"h", ST_OFF(h), CONF_TYPE_INT, M_OPT_MIN,1 ,0, NULL},
766 { NULL, NULL, 0, 0, 0, 0, NULL }
769 static const m_struct_t vf_size_preset = {
770 "scale_size_preset",
771 sizeof(struct size_preset),
772 NULL,
773 vf_size_preset_fields
776 static const m_struct_t vf_opts;
777 static const m_obj_presets_t size_preset = {
778 &vf_size_preset, // Input struct desc
779 &vf_opts, // Output struct desc
780 vf_size_presets_defs, // The list of presets
781 ST_OFF(name) // At wich offset is the name field in the preset struct
784 /// Now the options
785 #undef ST_OFF
786 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
787 static const m_option_t vf_opts_fields[] = {
788 {"w", ST_OFF(cfg_w), CONF_TYPE_INT, M_OPT_MIN,-11,0, NULL},
789 {"h", ST_OFF(cfg_h), CONF_TYPE_INT, M_OPT_MIN,-11,0, NULL},
790 {"interlaced", ST_OFF(interlaced), CONF_TYPE_INT, M_OPT_RANGE, 0, 1, NULL},
791 {"chr-drop", ST_OFF(v_chr_drop), CONF_TYPE_INT, M_OPT_RANGE, 0, 3, NULL},
792 {"param" , ST_OFF(param[0]), CONF_TYPE_DOUBLE, M_OPT_RANGE, 0.0, 100.0, NULL},
793 {"param2", ST_OFF(param[1]), CONF_TYPE_DOUBLE, M_OPT_RANGE, 0.0, 100.0, NULL},
794 // Note that here the 2 field is NULL (ie 0)
795 // As we want this option to act on the option struct itself
796 {"presize", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0, (void *)&size_preset},
797 {"noup", ST_OFF(noup), CONF_TYPE_INT, M_OPT_RANGE, 0, 2, NULL},
798 {"arnd", ST_OFF(accurate_rnd), CONF_TYPE_FLAG, 0, 0, 1, NULL},
799 { NULL, NULL, 0, 0, 0, 0, NULL }
802 static const m_struct_t vf_opts = {
803 "scale",
804 sizeof(struct vf_priv_s),
805 &vf_priv_dflt,
806 vf_opts_fields
809 const vf_info_t vf_info_scale = {
810 "software scaling",
811 "scale",
812 "A'rpi",
814 vf_open,
815 &vf_opts
818 //===========================================================================//