Update to MPlayer SVN rev 31722 and FFmpeg SVN rev 24213.
[vaapi:mplayer.git] / libmpcodecs / vf_vo.c
1 /*
2  * This file is part of MPlayer.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "config.h"
24 #include "mp_msg.h"
25 #include "mpcommon.h"
26 #include "mp_image.h"
27 #include "vf.h"
28
29 #include "libvo/sub.h"
30 #include "libvo/video_out.h"
31
32 #ifdef CONFIG_ASS
33 #include "libass/ass_mp.h"
34 #endif
35
36 //===========================================================================//
37
38 struct vf_priv_s {
39     double pts;
40     const vo_functions_t *vo;
41 #ifdef CONFIG_ASS
42     ass_renderer_t* ass_priv;
43     int prev_visibility;
44 #endif
45 };
46 #define video_out (vf->priv->vo)
47
48 static int query_format(struct vf_instance *vf, unsigned int fmt); /* forward declaration */
49 static void draw_slice(struct vf_instance *vf, unsigned char** src, int* stride, int w,int h, int x, int y);
50
51 static int config(struct vf_instance *vf,
52         int width, int height, int d_width, int d_height,
53         unsigned int flags, unsigned int outfmt){
54
55     if ((width <= 0) || (height <= 0) || (d_width <= 0) || (d_height <= 0))
56     {
57         mp_msg(MSGT_CPLAYER, MSGL_ERR, "VO: invalid dimensions!\n");
58         return 0;
59     }
60
61   if(video_out->info)
62   { const vo_info_t *info = video_out->info;
63     mp_msg(MSGT_CPLAYER,MSGL_INFO,"VO: [%s] %dx%d => %dx%d %s %s%s%s%s\n",info->short_name,
64          width, height,
65          d_width, d_height,
66          vo_format_name(outfmt),
67          (flags&VOFLAG_FULLSCREEN)?" [fs]":"",
68          (flags&VOFLAG_MODESWITCHING)?" [vm]":"",
69          (flags&VOFLAG_SWSCALE)?" [zoom]":"",
70          (flags&VOFLAG_FLIPPING)?" [flip]":"");
71     mp_msg(MSGT_CPLAYER,MSGL_V,"VO: Description: %s\n",info->name);
72     mp_msg(MSGT_CPLAYER,MSGL_V,"VO: Author: %s\n", info->author);
73     if(info->comment && strlen(info->comment) > 0)
74         mp_msg(MSGT_CPLAYER,MSGL_V,"VO: Comment: %s\n", info->comment);
75   }
76
77     // save vo's stride capability for the wanted colorspace:
78     vf->default_caps=query_format(vf,outfmt);
79     vf->draw_slice = (vf->default_caps & VOCAP_NOSLICES) ? NULL : draw_slice;
80
81     if(config_video_out(video_out,width,height,d_width,d_height,flags,"MPlayer",outfmt))
82         return 0;
83
84 #ifdef CONFIG_ASS
85     if (vf->priv->ass_priv)
86         ass_configure(vf->priv->ass_priv, width, height, !!(vf->default_caps & VFCAP_EOSD_UNSCALED));
87 #endif
88
89     ++vo_config_count;
90     return 1;
91 }
92
93 static int control(struct vf_instance *vf, int request, void* data)
94 {
95     switch(request){
96     case VFCTRL_GET_DEINTERLACE:
97     {
98         if(!video_out) return CONTROL_FALSE; // vo not configured?
99         return(video_out->control(VOCTRL_GET_DEINTERLACE, data)
100                 == VO_TRUE) ? CONTROL_TRUE : CONTROL_FALSE;
101     }
102     case VFCTRL_SET_DEINTERLACE:
103     {
104         if(!video_out) return CONTROL_FALSE; // vo not configured?
105         return(video_out->control(VOCTRL_SET_DEINTERLACE, data)
106                 == VO_TRUE) ? CONTROL_TRUE : CONTROL_FALSE;
107     }
108     case VFCTRL_DRAW_OSD:
109         if(!vo_config_count) return CONTROL_FALSE; // vo not configured?
110         video_out->draw_osd();
111         return CONTROL_TRUE;
112     case VFCTRL_FLIP_PAGE:
113     {
114         if(!vo_config_count) return CONTROL_FALSE; // vo not configured?
115         video_out->flip_page();
116         return CONTROL_TRUE;
117     }
118     case VFCTRL_SET_EQUALIZER:
119     {
120         vf_equalizer_t *eq=data;
121         if(!vo_config_count) return CONTROL_FALSE; // vo not configured?
122         return (video_out->control(VOCTRL_SET_EQUALIZER, eq->item, eq->value) == VO_TRUE) ? CONTROL_TRUE : CONTROL_FALSE;
123     }
124     case VFCTRL_GET_EQUALIZER:
125     {
126         vf_equalizer_t *eq=data;
127         if(!vo_config_count) return CONTROL_FALSE; // vo not configured?
128         return (video_out->control(VOCTRL_GET_EQUALIZER, eq->item, &eq->value) == VO_TRUE) ? CONTROL_TRUE : CONTROL_FALSE;
129     }
130 #ifdef CONFIG_ASS
131     case VFCTRL_INIT_EOSD:
132     {
133         vf->priv->ass_priv = ass_renderer_init((ass_library_t*)data);
134         if (!vf->priv->ass_priv) return CONTROL_FALSE;
135         ass_configure_fonts(vf->priv->ass_priv);
136         vf->priv->prev_visibility = 0;
137         return CONTROL_TRUE;
138     }
139     case VFCTRL_DRAW_EOSD:
140     {
141         mp_eosd_images_t images = {NULL, 2};
142         double pts = vf->priv->pts;
143         if (!vo_config_count || !vf->priv->ass_priv) return CONTROL_FALSE;
144         if (sub_visibility && vf->priv->ass_priv && ass_track && (pts != MP_NOPTS_VALUE)) {
145             mp_eosd_res_t res;
146             memset(&res, 0, sizeof(res));
147             if (video_out->control(VOCTRL_GET_EOSD_RES, &res) == VO_TRUE) {
148                 double dar = (double) (res.w - res.ml - res.mr) / (res.h - res.mt - res.mb);
149                 ass_set_frame_size(vf->priv->ass_priv, res.w, res.h);
150                 ass_set_margins(vf->priv->ass_priv, res.mt, res.mb, res.ml, res.mr);
151 #if defined(LIBASS_VERSION) && LIBASS_VERSION >= 0x00907010
152                 ass_set_aspect_ratio(vf->priv->ass_priv, dar, (double)res.srcw/res.srch);
153 #else
154                 ass_set_aspect_ratio(vf->priv->ass_priv, (double)res.w / res.h);
155 #endif
156             }
157
158             images.imgs = ass_mp_render_frame(vf->priv->ass_priv, ass_track, (pts+sub_delay) * 1000 + .5, &images.changed);
159             if (!vf->priv->prev_visibility)
160                 images.changed = 2;
161             vf->priv->prev_visibility = 1;
162         } else
163             vf->priv->prev_visibility = 0;
164         vf->priv->prev_visibility = sub_visibility;
165         return (video_out->control(VOCTRL_DRAW_EOSD, &images) == VO_TRUE) ? CONTROL_TRUE : CONTROL_FALSE;
166     }
167 #endif
168     case VFCTRL_GET_PTS:
169     {
170         *(double *)data = vf->priv->pts;
171         return CONTROL_TRUE;
172     }
173     }
174     // return video_out->control(request,data);
175     return CONTROL_UNKNOWN;
176 }
177
178 static int query_format(struct vf_instance *vf, unsigned int fmt){
179     int flags=video_out->control(VOCTRL_QUERY_FORMAT,&fmt);
180     // draw_slice() accepts stride, draw_frame() doesn't:
181     if(flags)
182         if(fmt==IMGFMT_YV12 || fmt==IMGFMT_I420 || fmt==IMGFMT_IYUV)
183             flags|=VFCAP_ACCEPT_STRIDE;
184     return flags;
185 }
186
187 static void get_image(struct vf_instance *vf,
188         mp_image_t *mpi){
189     if(!vo_config_count) return;
190     // GET_IMAGE is required for hardware-accelerated formats
191     if(vo_directrendering ||
192        IMGFMT_IS_XVMC(mpi->imgfmt) || IMGFMT_IS_VDPAU(mpi->imgfmt))
193         video_out->control(VOCTRL_GET_IMAGE,mpi);
194 }
195
196 static int put_image(struct vf_instance *vf,
197         mp_image_t *mpi, double pts){
198   if(!vo_config_count) return 0; // vo not configured?
199   // record pts (potentially modified by filters) for main loop
200   vf->priv->pts = pts;
201   // first check, maybe the vo/vf plugin implements draw_image using mpi:
202   if(video_out->control(VOCTRL_DRAW_IMAGE,mpi)==VO_TRUE) return 1; // done.
203   // nope, fallback to old draw_frame/draw_slice:
204   if(!(mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK))){
205     // blit frame:
206 //    if(mpi->flags&MP_IMGFLAG_PLANAR)
207     if(vf->default_caps&VFCAP_ACCEPT_STRIDE)
208         video_out->draw_slice(mpi->planes,mpi->stride,mpi->w,mpi->h,mpi->x,mpi->y);
209     else
210         video_out->draw_frame(mpi->planes);
211   }
212   return 1;
213 }
214
215 static void start_slice(struct vf_instance *vf,
216                        mp_image_t *mpi) {
217     if(!vo_config_count) return; // vo not configured?
218     video_out->control(VOCTRL_START_SLICE,mpi);
219 }
220
221 static void draw_slice(struct vf_instance *vf,
222         unsigned char** src, int* stride, int w,int h, int x, int y){
223     if(!vo_config_count) return; // vo not configured?
224     video_out->draw_slice(src,stride,w,h,x,y);
225 }
226
227 static void uninit(struct vf_instance *vf)
228 {
229     if (vf->priv) {
230 #ifdef CONFIG_ASS
231         if (vf->priv->ass_priv)
232             ass_renderer_done(vf->priv->ass_priv);
233 #endif
234         free(vf->priv);
235     }
236 }
237 //===========================================================================//
238
239 static int vf_open(vf_instance_t *vf, char *args){
240     vf->config=config;
241     vf->control=control;
242     vf->query_format=query_format;
243     vf->get_image=get_image;
244     vf->put_image=put_image;
245     vf->draw_slice=draw_slice;
246     vf->start_slice=start_slice;
247     vf->uninit=uninit;
248     vf->priv=calloc(1, sizeof(struct vf_priv_s));
249     vf->priv->vo = (const vo_functions_t *)args;
250     if(!video_out) return 0; // no vo ?
251     return 1;
252 }
253
254 const vf_info_t vf_info_vo = {
255     "libvo wrapper",
256     "vo",
257     "A'rpi",
258     "for internal use",
259     vf_open,
260     NULL
261 };
262
263 //===========================================================================//