subreader: fix unsafe sscanf calls with "%["
[mplayer.git] / libvo / aspect.c
blob8a26a5ac0093f080894dcb4c216db0fa42730c8b
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 /* Stuff for correct aspect scaling. */
20 #include "aspect.h"
21 #include "geometry.h"
22 #include "video_out.h"
23 #include "mp_msg.h"
24 #include "options.h"
26 #include "video_out.h"
28 void aspect_save_orig(struct vo *vo, int orgw, int orgh)
30 mp_msg(MSGT_VO, MSGL_DBG2, "aspect_save_orig %dx%d\n", orgw, orgh);
31 vo->aspdat.orgw = orgw;
32 vo->aspdat.orgh = orgh;
35 void aspect_save_prescale(struct vo *vo, int prew, int preh)
37 mp_msg(MSGT_VO, MSGL_DBG2, "aspect_save_prescale %dx%d\n", prew, preh);
38 vo->aspdat.prew = prew;
39 vo->aspdat.preh = preh;
42 void aspect_save_screenres(struct vo *vo, int scrw, int scrh)
44 mp_msg(MSGT_VO, MSGL_DBG2, "aspect_save_screenres %dx%d\n", scrw, scrh);
45 struct MPOpts *opts = vo->opts;
46 if (scrw <= 0 && scrh <= 0)
47 scrw = 1024;
48 if (scrh <= 0)
49 scrh = (scrw * 3 + 3) / 4;
50 if (scrw <= 0)
51 scrw = (scrh * 4 + 2) / 3;
52 vo->aspdat.scrw = scrw;
53 vo->aspdat.scrh = scrh;
54 if (opts->force_monitor_aspect)
55 vo->monitor_aspect = opts->force_monitor_aspect;
56 else
57 vo->monitor_aspect = opts->monitor_pixel_aspect * scrw / scrh;
60 /* aspect is called with the source resolution and the
61 * resolution, that the scaled image should fit into
64 void aspect_fit(struct vo *vo, int *srcw, int *srch, int fitw, int fith)
66 struct aspect_data *aspdat = &vo->aspdat;
67 float pixelaspect = vo->monitor_aspect * aspdat->scrh / aspdat->scrw;
69 mp_msg(MSGT_VO, MSGL_DBG2, "aspect(0) fitin: %dx%d screenaspect: %.2f\n",
70 fitw, fith, vo->monitor_aspect);
71 *srcw = fitw;
72 *srch = (float)fitw / aspdat->prew * aspdat->preh * pixelaspect;
73 *srch += *srch % 2; // round
74 mp_msg(MSGT_VO, MSGL_DBG2, "aspect(1) wh: %dx%d (org: %dx%d)\n",
75 *srcw, *srch, aspdat->prew, aspdat->preh);
76 if (*srch > fith || *srch < aspdat->orgh) {
77 int tmpw = (float)fith / aspdat->preh * aspdat->prew / pixelaspect;
78 tmpw += tmpw % 2; // round
79 if (tmpw <= fitw) {
80 *srch = fith;
81 *srcw = tmpw;
82 } else if (*srch > fith) {
83 mp_tmsg(MSGT_VO, MSGL_WARN,
84 "[ASPECT] Warning: No suitable new res found!\n");
87 aspdat->asp = *srcw / (float)*srch;
88 mp_msg(MSGT_VO, MSGL_DBG2, "aspect(2) wh: %dx%d (org: %dx%d)\n",
89 *srcw, *srch, aspdat->prew, aspdat->preh);
92 static void get_max_dims(struct vo *vo, int *w, int *h, int zoom)
94 struct aspect_data *aspdat = &vo->aspdat;
95 *w = zoom ? aspdat->scrw : aspdat->prew;
96 *h = zoom ? aspdat->scrh : aspdat->preh;
97 if (zoom && WinID >= 0)
98 zoom = A_WINZOOM;
99 if (zoom == A_WINZOOM) {
100 *w = vo->dwidth;
101 *h = vo->dheight;
105 void aspect(struct vo *vo, int *srcw, int *srch, int zoom)
107 int fitw;
108 int fith;
109 get_max_dims(vo, &fitw, &fith, zoom);
110 if (!zoom && geometry_wh_changed) {
111 mp_msg(MSGT_VO, MSGL_DBG2, "aspect(0) no aspect forced!\n");
112 return; // the user doesn't want to fix aspect
114 aspect_fit(vo, srcw, srch, fitw, fith);
117 void panscan_init(struct vo *vo)
119 vo->panscan_x = 0;
120 vo->panscan_y = 0;
121 vo->panscan_amount = 0.0f;
124 static void panscan_calc_internal(struct vo *vo, int zoom)
126 int fwidth, fheight;
127 int vo_panscan_area;
128 int max_w, max_h;
129 get_max_dims(vo, &max_w, &max_h, zoom);
130 struct MPOpts *opts = vo->opts;
132 if (opts->vo_panscanrange > 0) {
133 aspect(vo, &fwidth, &fheight, zoom);
134 vo_panscan_area = max_h - fheight;
135 if (!vo_panscan_area)
136 vo_panscan_area = max_w - fwidth;
137 vo_panscan_area *= opts->vo_panscanrange;
138 } else
139 vo_panscan_area = -opts->vo_panscanrange * max_h;
141 vo->panscan_amount = vo_fs || zoom == A_WINZOOM ? vo_panscan : 0;
142 vo->panscan_x = vo_panscan_area * vo->panscan_amount * vo->aspdat.asp;
143 vo->panscan_y = vo_panscan_area * vo->panscan_amount;
147 * vos that set vo_dwidth and v_dheight correctly should call this to update
148 * vo_panscan_x and vo_panscan_y
150 void panscan_calc_windowed(struct vo *vo)
152 panscan_calc_internal(vo, A_WINZOOM);