mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / libmpcodecs / vf_dsize.c
blobd46d22ebb2c6c2821af6d20eab040729202c650a
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"
27 #include "img_format.h"
28 #include "mp_image.h"
29 #include "vf.h"
31 struct vf_priv_s {
32 int w, h;
33 int method; // aspect method, 0 -> downscale, 1-> upscale. +2 -> original aspect.
34 int round;
35 float aspect;
38 static int config(struct vf_instance *vf,
39 int width, int height, int d_width, int d_height,
40 unsigned int flags, unsigned int outfmt)
42 int w = vf->priv->w;
43 int h = vf->priv->h;
44 if (vf->priv->aspect < 0.001) { // did the user input aspect or w,h params
45 if (w == 0) w = d_width;
46 if (h == 0) h = d_height;
47 if (w == -1) w = width;
48 if (h == -1) h = height;
49 if (w == -2) w = h * (double)d_width / d_height;
50 if (w == -3) w = h * (double)width / height;
51 if (h == -2) h = w * (double)d_height / d_width;
52 if (h == -3) h = w * (double)height / width;
53 if (vf->priv->method > -1) {
54 double aspect = (vf->priv->method & 2) ? ((double)height / width) : ((double)d_height / d_width);
55 if ((h > w * aspect) ^ (vf->priv->method & 1)) {
56 h = w * aspect;
57 } else {
58 w = h / aspect;
61 if (vf->priv->round > 1) { // round up
62 w += (vf->priv->round - 1 - (w - 1) % vf->priv->round);
63 h += (vf->priv->round - 1 - (h - 1) % vf->priv->round);
65 d_width = w;
66 d_height = h;
67 } else {
68 if (vf->priv->aspect * height > width) {
69 d_width = height * vf->priv->aspect + .5;
70 d_height = height;
71 } else {
72 d_height = width / vf->priv->aspect + .5;
73 d_width = width;
76 return vf_next_config(vf, width, height, d_width, d_height, flags, outfmt);
79 static void uninit(vf_instance_t *vf) {
80 free(vf->priv);
81 vf->priv = NULL;
84 static int vf_open(vf_instance_t *vf, char *args)
86 vf->config = config;
87 vf->draw_slice = vf_next_draw_slice;
88 vf->uninit = uninit;
89 //vf->default_caps = 0;
90 vf->priv = calloc(sizeof(struct vf_priv_s), 1);
91 vf->priv->aspect = 0.;
92 vf->priv->w = -1;
93 vf->priv->h = -1;
94 vf->priv->method = -1;
95 vf->priv->round = 1;
96 if (args) {
97 if (strchr(args, '/')) {
98 int w, h;
99 sscanf(args, "%d/%d", &w, &h);
100 vf->priv->aspect = (float)w/h;
101 } else if (strchr(args, '.')) {
102 sscanf(args, "%f", &vf->priv->aspect);
103 } else {
104 sscanf(args, "%d:%d:%d:%d", &vf->priv->w, &vf->priv->h, &vf->priv->method, &vf->priv->round);
107 if ((vf->priv->aspect < 0.) || (vf->priv->w < -3) || (vf->priv->h < -3) ||
108 ((vf->priv->w < -1) && (vf->priv->h < -1)) ||
109 (vf->priv->method < -1) || (vf->priv->method > 3) ||
110 (vf->priv->round < 0)) {
111 mp_msg(MSGT_VFILTER, MSGL_ERR, "[dsize] Illegal value(s): aspect: %f w: %d h: %d aspect_method: %d round: %d\n", vf->priv->aspect, vf->priv->w, vf->priv->h, vf->priv->method, vf->priv->round);
112 free(vf->priv); vf->priv = NULL;
113 return -1;
115 return 1;
118 const vf_info_t vf_info_dsize = {
119 "reset displaysize/aspect",
120 "dsize",
121 "Rich Felker",
123 vf_open,
124 NULL