Configure needs AS to be set for the Makefiles.
[mplayer/glamo.git] / libmpcodecs / vf_dsize.c
blob324ddf169aaf356a337049132d797b3bab00ea1d
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
6 #include "config.h"
7 #include "mp_msg.h"
9 #include "img_format.h"
10 #include "mp_image.h"
11 #include "vf.h"
13 struct vf_priv_s {
14 int w, h;
15 int method; // aspect method, 0 -> downscale, 1-> upscale. +2 -> original aspect.
16 int round;
17 float aspect;
20 static int config(struct vf_instance_s* vf,
21 int width, int height, int d_width, int d_height,
22 unsigned int flags, unsigned int outfmt)
24 if (vf->priv->aspect < 0.001) { // did the user input aspect or w,h params
25 if (vf->priv->w == 0) vf->priv->w = d_width;
26 if (vf->priv->h == 0) vf->priv->h = d_height;
27 if (vf->priv->w == -1) vf->priv->w = width;
28 if (vf->priv->h == -1) vf->priv->h = height;
29 if (vf->priv->w == -2) vf->priv->w = vf->priv->h * (double)d_width / d_height;
30 if (vf->priv->w == -3) vf->priv->w = vf->priv->h * (double)width / height;
31 if (vf->priv->h == -2) vf->priv->h = vf->priv->w * (double)d_height / d_width;
32 if (vf->priv->h == -3) vf->priv->h = vf->priv->w * (double)height / width;
33 if (vf->priv->method > -1) {
34 double aspect = (vf->priv->method & 2) ? ((double)height / width) : ((double)d_height / d_width);
35 if ((vf->priv->h > vf->priv->w * aspect) ^ (vf->priv->method & 1)) {
36 vf->priv->h = vf->priv->w * aspect;
37 } else {
38 vf->priv->w = vf->priv->h / aspect;
41 if (vf->priv->round > 1) { // round up
42 vf->priv->w += (vf->priv->round - 1 - (vf->priv->w - 1) % vf->priv->round);
43 vf->priv->h += (vf->priv->round - 1 - (vf->priv->h - 1) % vf->priv->round);
45 d_width = vf->priv->w;
46 d_height = vf->priv->h;
47 } else {
48 if (vf->priv->aspect * height > width) {
49 d_width = height * vf->priv->aspect + .5;
50 d_height = height;
51 } else {
52 d_height = width / vf->priv->aspect + .5;
53 d_width = width;
56 return vf_next_config(vf, width, height, d_width, d_height, flags, outfmt);
59 static void uninit(vf_instance_t *vf) {
60 free(vf->priv);
61 vf->priv = NULL;
64 static int open(vf_instance_t *vf, char* args)
66 vf->config = config;
67 vf->draw_slice = vf_next_draw_slice;
68 vf->uninit = uninit;
69 //vf->default_caps = 0;
70 vf->priv = calloc(sizeof(struct vf_priv_s), 1);
71 vf->priv->aspect = 0.;
72 vf->priv->w = -1;
73 vf->priv->h = -1;
74 vf->priv->method = -1;
75 vf->priv->round = 1;
76 if (args) {
77 if (strchr(args, '/')) {
78 int w, h;
79 sscanf(args, "%d/%d", &w, &h);
80 vf->priv->aspect = (float)w/h;
81 } else if (strchr(args, '.')) {
82 sscanf(args, "%f", &vf->priv->aspect);
83 } else {
84 sscanf(args, "%d:%d:%d:%d", &vf->priv->w, &vf->priv->h, &vf->priv->method, &vf->priv->round);
87 if ((vf->priv->aspect < 0.) || (vf->priv->w < -3) || (vf->priv->h < -3) ||
88 ((vf->priv->w < -1) && (vf->priv->h < -1)) ||
89 (vf->priv->method < -1) || (vf->priv->method > 3) ||
90 (vf->priv->round < 0)) {
91 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);
92 free(vf->priv); vf->priv = NULL;
93 return -1;
95 return 1;
98 const vf_info_t vf_info_dsize = {
99 "reset displaysize/aspect",
100 "dsize",
101 "Rich Felker",
103 open,
104 NULL