summaryrefslogtreecommitdiffstats
path: root/libmpcodecs/vf_expand.c
blob: 3c5eb2904f6da9a4ac850f140744615193dea492 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../config.h"
#include "../mp_msg.h"

#include "mp_image.h"
#include "vf.h"

#include "../libvo/fastmemcpy.h"

struct vf_priv_s {
    int exp_w,exp_h;
    int exp_x,exp_y;
    mp_image_t *dmpi;
};

//===========================================================================//

static int config(struct vf_instance_s* vf,
        int width, int height, int d_width, int d_height,
	unsigned int flags, unsigned int outfmt){
    int ret;
    // calculate the missing parameters:
    if(vf->priv->exp_w<width) vf->priv->exp_w=width;
    if(vf->priv->exp_h<height) vf->priv->exp_h=height;
    if(vf->priv->exp_x<0) vf->priv->exp_x=(vf->priv->exp_w-width)/2;
    if(vf->priv->exp_y<0) vf->priv->exp_y=(vf->priv->exp_h-height)/2;
    // check:
//    if(vf->priv->exp_w+vf->priv->exp_x>width) return 0; // bad width
//    if(vf->priv->exp_h+vf->priv->exp_y>height) return 0; // bad height
    ret=vf_next_config(vf,vf->priv->exp_w,vf->priv->exp_h,d_width,d_height,flags,outfmt);
    return ret;
}

// there are 4 cases:
// codec --DR--> expand --DR--> vo
// codec --DR--> expand -copy-> vo
// codec -copy-> expand --DR--> vo
// codec -copy-> expand -copy-> vo (worst case)

static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
    if(vf->priv->exp_w==mpi->width ||
       (mpi->flags&(MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_ACCEPT_WIDTH)) ){
	// try full DR !
	vf->priv->dmpi=vf_get_image(vf->next,mpi->imgfmt,
	    mpi->type, mpi->flags, vf->priv->exp_w, vf->priv->exp_h);
	// set up mpi as a cropped-down image of dmpi:
	if(mpi->flags&MP_IMGFLAG_PLANAR){
	    mpi->planes[0]=vf->priv->dmpi->planes[0]+
		vf->priv->exp_y*vf->priv->dmpi->stride[0]+vf->priv->exp_x;
	    mpi->planes[1]=vf->priv->dmpi->planes[1]+
		(vf->priv->exp_y>>1)*vf->priv->dmpi->stride[1]+(vf->priv->exp_x>>1);
	    mpi->planes[2]=vf->priv->dmpi->planes[2]+
		(vf->priv->exp_y>>1)*vf->priv->dmpi->stride[2]+(vf->priv->exp_x>>1);
	    mpi->stride[1]=vf->priv->dmpi->stride[1];
	    mpi->stride[2]=vf->priv->dmpi->stride[2];
	} else {
	    mpi->planes[0]=vf->priv->dmpi->planes[0]+
		vf->priv->exp_y*vf->priv->dmpi->stride[0]+
		vf->priv->exp_x*(vf->priv->dmpi->bpp/8);
	}
	mpi->stride[0]=vf->priv->dmpi->stride[0];
	mpi->width=vf->priv->dmpi->width;
	mpi->flags|=MP_IMGFLAG_DIRECT;
    }
}

static void put_image(struct vf_instance_s* vf, mp_image_t *mpi){
    if(mpi->flags&MP_IMGFLAG_DIRECT){
	vf_next_put_image(vf,vf->priv->dmpi);
	return; // we've used DR, so we're ready...
    }

    // hope we'll get DR buffer:
    vf->priv->dmpi=vf_get_image(vf->next,mpi->imgfmt,
	MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
	vf->priv->exp_w, vf->priv->exp_h);
    
    // copy mpi->dmpi...
    if(mpi->flags&MP_IMGFLAG_PLANAR){
	memcpy_pic(vf->priv->dmpi->planes[0]+
	        vf->priv->exp_y*vf->priv->dmpi->stride[0]+vf->priv->exp_x,
		mpi->planes[0], mpi->w, mpi->h,
		vf->priv->dmpi->stride[0],mpi->stride[0]);
	memcpy_pic(vf->priv->dmpi->planes[1]+
		(vf->priv->exp_y>>1)*vf->priv->dmpi->stride[1]+(vf->priv->exp_x>>1),
		mpi->planes[1], mpi->w>>1, mpi->h>>1,
		vf->priv->dmpi->stride[1],mpi->stride[1]);
	memcpy_pic(vf->priv->dmpi->planes[2]+
		(vf->priv->exp_y>>1)*vf->priv->dmpi->stride[2]+(vf->priv->exp_x>>1),
		mpi->planes[2], mpi->w>>1, mpi->h>>1,
		vf->priv->dmpi->stride[2],mpi->stride[2]);
    } else {
	memcpy_pic(vf->priv->dmpi->planes[0]+
	        vf->priv->exp_y*vf->priv->dmpi->stride[0]+vf->priv->exp_x*(vf->priv->dmpi->bpp/8),
		mpi->planes[0], mpi->w*(vf->priv->dmpi->bpp/8), mpi->h,
		vf->priv->dmpi->stride[0],mpi->stride[0]);
    }
    vf_next_put_image(vf,vf->priv->dmpi);
}

//===========================================================================//

static int open(vf_instance_t *vf, char* args){
    vf->config=config;
    vf->get_image=get_image;
    vf->put_image=put_image;
    vf->priv=malloc(sizeof(struct vf_priv_s));
    // TODO: parse args ->
    vf->priv->exp_x=
    vf->priv->exp_y=
    vf->priv->exp_w=
    vf->priv->exp_h=-1;
    if(args) sscanf(args, "%d:%d:%d:%d", 
    &vf->priv->exp_w,
    &vf->priv->exp_h,
    &vf->priv->exp_x,
    &vf->priv->exp_y);
    printf("Expand: %d x %d, %d ; %d\n",
    vf->priv->exp_w,
    vf->priv->exp_h,
    vf->priv->exp_x,
    vf->priv->exp_y);
    return 1;
}

vf_info_t vf_info_expand = {
    "expanding",
    "expand",
    "A'rpi",
    "",
    open
};

//===========================================================================//