summaryrefslogtreecommitdiffstats
path: root/video/out/aspect.c
blob: f85b75778b20539fc8259cd535579d50ef7c5048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* Stuff for correct aspect scaling. */
#include "aspect.h"
#include "vo.h"
#include "core/mp_msg.h"
#include "core/options.h"

#include "vo.h"

void aspect_save_videores(struct vo *vo, int w, int h, int d_w, int d_h)
{
    vo->aspdat.orgw = w;
    vo->aspdat.orgh = h;
    vo->aspdat.prew = d_w;
    vo->aspdat.preh = d_h;
    vo->aspdat.par = (double)d_w / d_h * h / w;
}

void aspect_save_screenres(struct vo *vo, int scrw, int scrh)
{
    mp_msg(MSGT_VO, MSGL_DBG2, "aspect_save_screenres %dx%d\n", scrw, scrh);
    struct mp_vo_opts *opts = vo->opts;
    if (scrw <= 0 && scrh <= 0)
        scrw = 1024;
    if (scrh <= 0)
        scrh = (scrw * 3 + 3) / 4;
    if (scrw <= 0)
        scrw = (scrh * 4 + 2) / 3;
    if (opts->force_monitor_aspect)
        vo->aspdat.monitor_par = opts->force_monitor_aspect * scrh / scrw;
    else
        vo->aspdat.monitor_par = 1.0 / opts->monitor_pixel_aspect;
}

void aspect_calc_monitor(struct vo *vo, int *w, int *h)
{
    float pixelaspect = vo->aspdat.monitor_par;

    if (pixelaspect < 1) {
        *h /= pixelaspect;
    } else {
        *w *= pixelaspect;
    }
}

static void aspect_calc(struct vo *vo, int *srcw, int *srch)
{
    struct aspect_data *aspdat = &vo->aspdat;
    float pixelaspect = aspdat->monitor_par;

    int fitw = vo->dwidth;
    int fith = vo->dheight;

    mp_msg(MSGT_VO, MSGL_DBG2, "aspect(0) fitin: %dx%d monitor_par: %.2f\n",
           fitw, fith, aspdat->monitor_par);
    *srcw = fitw;
    *srch = (float)fitw / aspdat->prew * aspdat->preh / pixelaspect;
    mp_msg(MSGT_VO, MSGL_DBG2, "aspect(1) wh: %dx%d (org: %dx%d)\n",
           *srcw, *srch, aspdat->prew, aspdat->preh);
    if (*srch > fith || *srch < aspdat->orgh) {
        int tmpw = (float)fith / aspdat->preh * aspdat->prew * pixelaspect;
        if (tmpw <= fitw) {
            *srch = fith;
            *srcw = tmpw;
        } else if (*srch > fith) {
            mp_tmsg(MSGT_VO, MSGL_WARN,
                    "[ASPECT] Warning: No suitable new res found!\n");
        }
    }
    aspdat->asp = *srcw / (float)*srch;
    mp_msg(MSGT_VO, MSGL_DBG2, "aspect(2) wh: %dx%d (org: %dx%d)\n",
           *srcw, *srch, aspdat->prew, aspdat->preh);
}

void aspect_calc_panscan(struct vo *vo, int *out_w, int *out_h)
{
    struct mp_vo_opts *opts = vo->opts;
    int fwidth, fheight;
    aspect_calc(vo, &fwidth, &fheight);

    int vo_panscan_area;
    if (opts->panscanrange > 0) {
        vo_panscan_area = vo->dheight - fheight;
        if (!vo_panscan_area)
            vo_panscan_area = vo->dwidth - fwidth;
        vo_panscan_area *= opts->panscanrange;
    } else
        vo_panscan_area = -opts->panscanrange * vo->dheight;

    *out_w = fwidth + vo_panscan_area * opts->panscan * vo->aspdat.asp;
    *out_h = fheight + vo_panscan_area * opts->panscan;
}