Merge remote branch 'mplayer/master'
[mplayer/glamo.git] / libmpcodecs / vf_vo.c
blobb15e48896176a90e23afb452eccc3bcc5dc18f6d
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdbool.h>
6 #include "config.h"
7 #include "mp_msg.h"
9 #include "mp_image.h"
10 #include "vf.h"
12 #include "libvo/video_out.h"
14 #ifdef CONFIG_ASS
15 #include "ass_mp.h"
16 extern ASS_Track *ass_track;
17 #endif
19 //===========================================================================//
21 extern int sub_visibility;
22 extern float sub_delay;
24 struct vf_priv_s {
25 struct vo *vo;
26 #ifdef CONFIG_ASS
27 ASS_Renderer *ass_priv;
28 int prev_visibility;
29 double scale_ratio;
30 #endif
32 #define video_out (vf->priv->vo)
34 static int query_format(struct vf_instance* vf, unsigned int fmt); /* forward declaration */
35 static void draw_slice(struct vf_instance *vf, unsigned char **src, int *stride, int w,int h, int x, int y);
37 static int config(struct vf_instance* vf,
38 int width, int height, int d_width, int d_height,
39 unsigned int flags, unsigned int outfmt){
41 if ((width <= 0) || (height <= 0) || (d_width <= 0) || (d_height <= 0))
43 mp_msg(MSGT_CPLAYER, MSGL_ERR, "VO: invalid dimensions!\n");
44 return 0;
47 const vo_info_t *info = video_out->driver->info;
48 mp_msg(MSGT_CPLAYER,MSGL_INFO,"VO: [%s] %dx%d => %dx%d %s %s%s%s%s\n",info->short_name,
49 width, height,
50 d_width, d_height,
51 vo_format_name(outfmt),
52 (flags&VOFLAG_FULLSCREEN)?" [fs]":"",
53 (flags&VOFLAG_MODESWITCHING)?" [vm]":"",
54 (flags&VOFLAG_SWSCALE)?" [zoom]":"",
55 (flags&VOFLAG_FLIPPING)?" [flip]":"");
56 mp_msg(MSGT_CPLAYER,MSGL_V,"VO: Description: %s\n",info->name);
57 mp_msg(MSGT_CPLAYER,MSGL_V,"VO: Author: %s\n", info->author);
58 if(info->comment && strlen(info->comment) > 0)
59 mp_msg(MSGT_CPLAYER,MSGL_V,"VO: Comment: %s\n", info->comment);
61 // save vo's stride capability for the wanted colorspace:
62 vf->default_caps=query_format(vf,outfmt);
63 vf->draw_slice = (vf->default_caps & VOCAP_NOSLICES) ? NULL : draw_slice;
65 if (vo_config(video_out, width, height, d_width, d_height, flags, "MPlayer", outfmt))
66 return 0;
68 #ifdef CONFIG_ASS
69 vf->priv->scale_ratio = (double) d_width / d_height * height / width;
71 if (vf->priv->ass_priv)
72 ass_configure(vf->priv->ass_priv, width, height, !!(vf->default_caps & VFCAP_EOSD_UNSCALED));
73 #endif
75 return 1;
78 static int control(struct vf_instance* vf, int request, void* data)
80 switch(request){
81 case VFCTRL_GET_DEINTERLACE:
83 if(!video_out) return CONTROL_FALSE; // vo not configured?
84 return vo_control(video_out, VOCTRL_GET_DEINTERLACE, data) == VO_TRUE;
86 case VFCTRL_SET_DEINTERLACE:
88 if(!video_out) return CONTROL_FALSE; // vo not configured?
89 return vo_control(video_out, VOCTRL_SET_DEINTERLACE, data) == VO_TRUE;
91 case VFCTRL_GET_YUV_COLORSPACE:
92 return vo_control(video_out, VOCTRL_GET_YUV_COLORSPACE, data) == true;
93 case VFCTRL_SET_YUV_COLORSPACE:
94 return vo_control(video_out, VOCTRL_SET_YUV_COLORSPACE, data) == true;
95 case VFCTRL_DRAW_OSD:
96 if(!video_out->config_ok) return CONTROL_FALSE; // vo not configured?
97 vo_draw_osd(video_out, data);
98 return CONTROL_TRUE;
99 case VFCTRL_REDRAW_OSD:
100 return vo_control(video_out, VOCTRL_REDRAW_OSD, data) == true;
101 case VFCTRL_SET_EQUALIZER:
103 vf_equalizer_t *eq=data;
104 if(!video_out->config_ok) return CONTROL_FALSE; // vo not configured?
105 struct voctrl_set_equalizer_args param = {eq->item, eq->value};
106 return vo_control(video_out, VOCTRL_SET_EQUALIZER, &param) == VO_TRUE;
108 case VFCTRL_GET_EQUALIZER:
110 vf_equalizer_t *eq=data;
111 if(!video_out->config_ok) return CONTROL_FALSE; // vo not configured?
112 struct voctrl_get_equalizer_args param = {eq->item, &eq->value};
113 return vo_control(video_out, VOCTRL_GET_EQUALIZER, &param) == VO_TRUE;
115 #ifdef CONFIG_ASS
116 case VFCTRL_INIT_EOSD:
118 vf->priv->ass_priv = ass_renderer_init((ASS_Library*)data);
119 if (!vf->priv->ass_priv) return CONTROL_FALSE;
120 ass_configure_fonts(vf->priv->ass_priv);
121 vf->priv->prev_visibility = 0;
122 return CONTROL_TRUE;
124 case VFCTRL_DRAW_EOSD:
126 mp_eosd_images_t images = {NULL, 2};
127 double pts = video_out->next_pts;
128 if (!video_out->config_ok || !vf->priv->ass_priv) return CONTROL_FALSE;
129 if (sub_visibility && vf->priv->ass_priv && ass_track && (pts != MP_NOPTS_VALUE)) {
130 mp_eosd_res_t res;
131 memset(&res, 0, sizeof(res));
132 if (vo_control(video_out, VOCTRL_GET_EOSD_RES, &res) == VO_TRUE) {
133 ass_set_frame_size(vf->priv->ass_priv, res.w, res.h);
134 ass_set_margins(vf->priv->ass_priv, res.mt, res.mb, res.ml, res.mr);
135 ass_set_aspect_ratio(vf->priv->ass_priv, vf->priv->scale_ratio, 1);
138 images.imgs = ass_mp_render_frame(vf->priv->ass_priv, ass_track, (pts+sub_delay) * 1000 + .5, &images.changed);
139 if (!vf->priv->prev_visibility)
140 images.changed = 2;
141 vf->priv->prev_visibility = 1;
142 } else
143 vf->priv->prev_visibility = 0;
144 vf->priv->prev_visibility = sub_visibility;
145 return vo_control(video_out, VOCTRL_DRAW_EOSD, &images) == VO_TRUE;
147 #endif
149 return CONTROL_UNKNOWN;
152 static int query_format(struct vf_instance* vf, unsigned int fmt){
153 int flags = vo_control(video_out, VOCTRL_QUERY_FORMAT, &fmt);
154 // draw_slice() accepts stride, draw_frame() doesn't:
155 if(flags)
156 if(fmt==IMGFMT_YV12 || fmt==IMGFMT_I420 || fmt==IMGFMT_IYUV)
157 flags|=VFCAP_ACCEPT_STRIDE;
158 return flags;
161 static void get_image(struct vf_instance* vf,
162 mp_image_t *mpi){
163 if (!video_out->config_ok)
164 return;
165 // GET_IMAGE is required for hardware-accelerated formats
166 if(vo_directrendering ||
167 IMGFMT_IS_XVMC(mpi->imgfmt) || IMGFMT_IS_VDPAU(mpi->imgfmt))
168 vo_control(video_out, VOCTRL_GET_IMAGE, mpi);
171 static int put_image(struct vf_instance* vf,
172 mp_image_t *mpi, double pts){
173 if(!video_out->config_ok) return 0; // vo not configured?
174 // first check, maybe the vo/vf plugin implements draw_image using mpi:
175 if (vo_draw_image(video_out, mpi, pts) >= 0)
176 return 1;
177 // nope, fallback to old draw_frame/draw_slice:
178 if(!(mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK))){
179 // blit frame:
180 // if(mpi->flags&MP_IMGFLAG_PLANAR)
181 if(vf->default_caps&VFCAP_ACCEPT_STRIDE)
182 vo_draw_slice(video_out, mpi->planes,mpi->stride,mpi->w,mpi->h,mpi->x,mpi->y);
183 else
184 vo_draw_frame(video_out, mpi->planes);
186 return 1;
189 static void start_slice(struct vf_instance* vf,
190 mp_image_t *mpi) {
191 if(!video_out->config_ok) return; // vo not configured?
192 vo_control(video_out, VOCTRL_START_SLICE,mpi);
195 static void draw_slice(struct vf_instance* vf,
196 unsigned char** src, int* stride, int w,int h, int x, int y){
197 if(!video_out->config_ok) return; // vo not configured?
198 vo_draw_slice(video_out, src,stride,w,h,x,y);
201 static void uninit(struct vf_instance* vf)
203 if (vf->priv) {
204 /* Allow VO (which may live on to work with another instance of vf_vo)
205 * to get rid of numbered-mpi references that will now be invalid. */
206 vo_seek_reset(video_out);
207 #ifdef CONFIG_ASS
208 if (vf->priv->ass_priv)
209 ass_renderer_done(vf->priv->ass_priv);
210 #endif
211 free(vf->priv);
214 //===========================================================================//
216 static int open(vf_instance_t *vf, char* args){
217 vf->config=config;
218 vf->control=control;
219 vf->query_format=query_format;
220 vf->get_image=get_image;
221 vf->put_image=put_image;
222 vf->draw_slice=draw_slice;
223 vf->start_slice=start_slice;
224 vf->uninit=uninit;
225 vf->priv=calloc(1, sizeof(struct vf_priv_s));
226 vf->priv->vo = (struct vo *)args;
227 if(!video_out) return 0; // no vo ?
228 return 1;
231 const vf_info_t vf_info_vo = {
232 "libvo wrapper",
233 "vo",
234 "A'rpi",
235 "for internal use",
236 open,
237 NULL
240 //===========================================================================//