cocoa_common: refactor menu generation
[mplayer.git] / libmpcodecs / vf_vo.c
blob0f89d7312f080261033e192f642d11815a4a59c5
1 /*
2 * This file is part of MPlayer.
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.
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.
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdbool.h>
24 #include "config.h"
25 #include "mp_msg.h"
26 #include "options.h"
28 #include "mp_image.h"
29 #include "vf.h"
31 #include "libvo/video_out.h"
33 #include "sub/ass_mp.h"
34 #include "sub/sub.h"
36 extern float sub_delay;
38 struct vf_priv_s {
39 struct vo *vo;
40 #ifdef CONFIG_ASS
41 ASS_Renderer *renderer_realaspect;
42 ASS_Renderer *renderer_vsfilter;
43 bool prev_visibility;
44 double scale_ratio;
45 #endif
47 #define video_out (vf->priv->vo)
49 static int query_format(struct vf_instance *vf, unsigned int fmt);
50 static void draw_slice(struct vf_instance *vf, unsigned char **src,
51 int *stride, int w, int h, int x, int y);
53 static int config(struct vf_instance *vf,
54 int width, int height, int d_width, int d_height,
55 unsigned int flags, unsigned int outfmt)
58 if ((width <= 0) || (height <= 0) || (d_width <= 0) || (d_height <= 0)) {
59 mp_msg(MSGT_CPLAYER, MSGL_ERR, "VO: invalid dimensions!\n");
60 return 0;
63 const vo_info_t *info = video_out->driver->info;
64 mp_msg(MSGT_CPLAYER, MSGL_INFO, "VO: [%s] %dx%d => %dx%d %s %s%s%s%s\n",
65 info->short_name,
66 width, height,
67 d_width, d_height,
68 vo_format_name(outfmt),
69 (flags & VOFLAG_FULLSCREEN) ? " [fs]" : "",
70 (flags & VOFLAG_MODESWITCHING) ? " [vm]" : "",
71 (flags & VOFLAG_SWSCALE) ? " [zoom]" : "",
72 (flags & VOFLAG_FLIPPING) ? " [flip]" : "");
73 mp_msg(MSGT_CPLAYER, MSGL_V, "VO: Description: %s\n", info->name);
74 mp_msg(MSGT_CPLAYER, MSGL_V, "VO: Author: %s\n", info->author);
75 if (info->comment && strlen(info->comment) > 0)
76 mp_msg(MSGT_CPLAYER, MSGL_V, "VO: Comment: %s\n", info->comment);
78 // save vo's stride capability for the wanted colorspace:
79 vf->default_caps = query_format(vf, outfmt);
80 vf->draw_slice = (vf->default_caps & VOCAP_NOSLICES) ? NULL : draw_slice;
82 if (vo_config(video_out, width, height, d_width, d_height, flags, outfmt))
83 return 0;
85 #ifdef CONFIG_ASS
86 vf->priv->scale_ratio = (double) d_width / d_height * height / width;
88 if (vf->priv->renderer_realaspect) {
89 mp_ass_configure(vf->priv->renderer_realaspect, vf->opts, width, height,
90 vf->default_caps & VFCAP_EOSD_UNSCALED);
91 mp_ass_configure(vf->priv->renderer_vsfilter, vf->opts, width, height,
92 vf->default_caps & VFCAP_EOSD_UNSCALED);
95 // force EOSD change detection reset
96 vf->priv->prev_visibility = false;
97 #endif
99 return 1;
102 static int control(struct vf_instance *vf, int request, void *data)
104 switch (request) {
105 case VFCTRL_GET_DEINTERLACE:
106 if (!video_out)
107 return CONTROL_FALSE; // vo not configured?
108 return vo_control(video_out, VOCTRL_GET_DEINTERLACE, data) == VO_TRUE;
109 case VFCTRL_SET_DEINTERLACE:
110 if (!video_out)
111 return CONTROL_FALSE; // vo not configured?
112 return vo_control(video_out, VOCTRL_SET_DEINTERLACE, data) == VO_TRUE;
113 case VFCTRL_GET_YUV_COLORSPACE:
114 return vo_control(video_out, VOCTRL_GET_YUV_COLORSPACE, data) == true;
115 case VFCTRL_SET_YUV_COLORSPACE:
116 return vo_control(video_out, VOCTRL_SET_YUV_COLORSPACE, data) == true;
117 case VFCTRL_DRAW_OSD:
118 if (!video_out->config_ok)
119 return CONTROL_FALSE; // vo not configured?
120 vo_draw_osd(video_out, data);
121 return CONTROL_TRUE;
122 case VFCTRL_SET_EQUALIZER: {
123 vf_equalizer_t *eq = data;
124 if (!video_out->config_ok)
125 return CONTROL_FALSE; // vo not configured?
126 struct voctrl_set_equalizer_args param = {
127 eq->item, eq->value
129 return vo_control(video_out, VOCTRL_SET_EQUALIZER, &param) == VO_TRUE;
131 case VFCTRL_GET_EQUALIZER: {
132 vf_equalizer_t *eq = data;
133 if (!video_out->config_ok)
134 return CONTROL_FALSE; // vo not configured?
135 struct voctrl_get_equalizer_args param = {
136 eq->item, &eq->value
138 return vo_control(video_out, VOCTRL_GET_EQUALIZER, &param) == VO_TRUE;
140 #ifdef CONFIG_ASS
141 case VFCTRL_INIT_EOSD: {
142 vf->priv->renderer_realaspect = ass_renderer_init(data);
143 if (!vf->priv->renderer_realaspect)
144 return CONTROL_FALSE;
145 vf->priv->renderer_vsfilter = ass_renderer_init(data);
146 if (!vf->priv->renderer_vsfilter) {
147 ass_renderer_done(vf->priv->renderer_realaspect);
148 return CONTROL_FALSE;
150 mp_ass_configure_fonts(vf->priv->renderer_realaspect);
151 mp_ass_configure_fonts(vf->priv->renderer_vsfilter);
152 vf->priv->prev_visibility = false;
153 return CONTROL_TRUE;
155 case VFCTRL_DRAW_EOSD: {
156 struct osd_state *osd = data;
157 mp_eosd_images_t images = { NULL, 2 };
158 ASS_Renderer *renderer;
159 double scale;
160 if (osd->vsfilter_aspect && vf->opts->ass_vsfilter_aspect_compat) {
161 renderer = vf->priv->renderer_vsfilter;
162 scale = vf->priv->scale_ratio;
163 } else {
164 renderer = vf->priv->renderer_realaspect;
165 scale = 1;
167 if (!video_out->config_ok || !renderer)
168 return CONTROL_FALSE;
169 if (osd->ass_track_changed)
170 vf->priv->prev_visibility = false;
171 osd->ass_track_changed = false;
172 if (sub_visibility && osd->ass_track && (osd->pts != MP_NOPTS_VALUE)) {
173 struct mp_eosd_res res = { 0 };
174 if (vo_control(video_out, VOCTRL_GET_EOSD_RES, &res) == VO_TRUE) {
175 ass_set_frame_size(renderer, res.w, res.h);
176 ass_set_margins(renderer, res.mt, res.mb, res.ml, res.mr);
177 ass_set_aspect_ratio(renderer, scale, 1);
180 if (osd->ass_force_reload) {
181 mp_ass_reload_options(vf->priv->renderer_realaspect, vf->opts);
182 mp_ass_reload_options(vf->priv->renderer_vsfilter, vf->opts);
184 images.imgs = ass_render_frame(renderer, osd->ass_track,
185 (osd->pts + sub_delay) * 1000 + .5,
186 &images.changed);
187 if (!vf->priv->prev_visibility || osd->ass_force_reload)
188 images.changed = 2;
189 osd->ass_force_reload = false;
190 vf->priv->prev_visibility = true;
191 } else
192 vf->priv->prev_visibility = false;
193 return vo_control(video_out, VOCTRL_DRAW_EOSD, &images) == VO_TRUE;
195 #endif
197 return CONTROL_UNKNOWN;
200 static int query_format(struct vf_instance *vf, unsigned int fmt)
202 int flags = vo_control(video_out, VOCTRL_QUERY_FORMAT, &fmt);
203 // draw_slice() accepts stride, draw_frame() doesn't:
204 if (flags)
205 if (fmt == IMGFMT_YV12 || fmt == IMGFMT_I420 || fmt == IMGFMT_IYUV)
206 flags |= VFCAP_ACCEPT_STRIDE;
207 return flags;
210 static void get_image(struct vf_instance *vf,
211 mp_image_t *mpi)
213 if (!video_out->config_ok)
214 return;
215 // GET_IMAGE is required for hardware-accelerated formats
216 if (vo_directrendering || IMGFMT_IS_HWACCEL(mpi->imgfmt))
217 vo_control(video_out, VOCTRL_GET_IMAGE, mpi);
220 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
222 if (!video_out->config_ok)
223 return 0;
224 // first check, maybe the vo/vf plugin implements draw_image using mpi:
225 if (vo_draw_image(video_out, mpi, pts) >= 0)
226 return 1;
227 // nope, fallback to old draw_frame/draw_slice:
228 if (!(mpi->flags & (MP_IMGFLAG_DIRECT | MP_IMGFLAG_DRAW_CALLBACK))) {
229 // blit frame:
230 if (vf->default_caps & VFCAP_ACCEPT_STRIDE)
231 vo_draw_slice(video_out, mpi->planes, mpi->stride, mpi->w, mpi->h,
232 mpi->x, mpi->y);
233 else
234 vo_draw_frame(video_out, mpi->planes);
236 return 1;
239 static void start_slice(struct vf_instance *vf, mp_image_t *mpi)
241 if (!video_out->config_ok)
242 return;
243 vo_control(video_out, VOCTRL_START_SLICE, mpi);
246 static void draw_slice(struct vf_instance *vf, unsigned char **src,
247 int *stride, int w, int h, int x, int y)
249 if (!video_out->config_ok)
250 return;
251 vo_draw_slice(video_out, src, stride, w, h, x, y);
254 static void uninit(struct vf_instance *vf)
256 if (vf->priv) {
257 /* Allow VO (which may live on to work with another instance of vf_vo)
258 * to get rid of numbered-mpi references that will now be invalid. */
259 vo_seek_reset(video_out);
260 #ifdef CONFIG_ASS
261 if (vf->priv->renderer_realaspect) {
262 ass_renderer_done(vf->priv->renderer_realaspect);
263 ass_renderer_done(vf->priv->renderer_vsfilter);
265 #endif
266 free(vf->priv);
270 static int vf_open(vf_instance_t *vf, char *args)
272 vf->config = config;
273 vf->control = control;
274 vf->query_format = query_format;
275 vf->get_image = get_image;
276 vf->put_image = put_image;
277 vf->draw_slice = draw_slice;
278 vf->start_slice = start_slice;
279 vf->uninit = uninit;
280 vf->priv = calloc(1, sizeof(struct vf_priv_s));
281 vf->priv->vo = (struct vo *)args;
282 if (!video_out)
283 return 0;
284 return 1;
287 const vf_info_t vf_info_vo = {
288 "libvo wrapper",
289 "vo",
290 "A'rpi",
291 "for internal use",
292 vf_open,
293 NULL