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