cleanup: remove mp_image_t.x/y fields
[mplayer.git] / libmpcodecs / vd.c
blobe756c2d53ed52f97063392ea91c7fade323c7641
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>
23 #include "config.h"
24 #include "mp_msg.h"
25 #include "options.h"
27 #include "codec-cfg.h"
29 #include "img_format.h"
31 #include "stream/stream.h"
32 #include "libmpdemux/demuxer.h"
33 #include "libmpdemux/stheader.h"
34 #include "dec_video.h"
36 #include "vd.h"
37 #include "vf.h"
38 #include "libvo/video_out.h"
40 extern const vd_functions_t mpcodecs_vd_null;
41 extern const vd_functions_t mpcodecs_vd_ffmpeg;
42 extern const vd_functions_t mpcodecs_vd_theora;
43 extern const vd_functions_t mpcodecs_vd_dshow;
44 extern const vd_functions_t mpcodecs_vd_dmo;
45 extern const vd_functions_t mpcodecs_vd_vfw;
46 extern const vd_functions_t mpcodecs_vd_vfwex;
47 extern const vd_functions_t mpcodecs_vd_raw;
48 extern const vd_functions_t mpcodecs_vd_hmblck;
49 extern const vd_functions_t mpcodecs_vd_xanim;
50 extern const vd_functions_t mpcodecs_vd_mpng;
51 extern const vd_functions_t mpcodecs_vd_ijpg;
52 extern const vd_functions_t mpcodecs_vd_mtga;
53 extern const vd_functions_t mpcodecs_vd_sgi;
54 extern const vd_functions_t mpcodecs_vd_mpegpes;
55 extern const vd_functions_t mpcodecs_vd_realvid;
56 extern const vd_functions_t mpcodecs_vd_xvid;
57 extern const vd_functions_t mpcodecs_vd_libdv;
58 extern const vd_functions_t mpcodecs_vd_lzo;
59 extern const vd_functions_t mpcodecs_vd_qtvideo;
61 /* Please do not add any new decoders here. If you want to implement a new
62 * decoder, add it to libavcodec, except for wrappers around external
63 * libraries and decoders requiring binary support. */
65 const vd_functions_t * const mpcodecs_vd_drivers[] = {
66 &mpcodecs_vd_null,
67 &mpcodecs_vd_ffmpeg,
68 #ifdef CONFIG_OGGTHEORA
69 &mpcodecs_vd_theora,
70 #endif
71 #ifdef CONFIG_WIN32DLL
72 &mpcodecs_vd_dshow,
73 &mpcodecs_vd_dmo,
74 &mpcodecs_vd_vfw,
75 &mpcodecs_vd_vfwex,
76 #endif
77 &mpcodecs_vd_lzo,
78 &mpcodecs_vd_raw,
79 &mpcodecs_vd_hmblck,
80 #ifdef CONFIG_XANIM
81 &mpcodecs_vd_xanim,
82 #endif
83 #ifdef CONFIG_PNG
84 &mpcodecs_vd_mpng,
85 #endif
86 #ifdef CONFIG_JPEG
87 &mpcodecs_vd_ijpg,
88 #endif
89 &mpcodecs_vd_mtga,
90 &mpcodecs_vd_sgi,
91 &mpcodecs_vd_mpegpes,
92 #ifdef CONFIG_REALCODECS
93 &mpcodecs_vd_realvid,
94 #endif
95 #ifdef CONFIG_XVID4
96 &mpcodecs_vd_xvid,
97 #endif
98 #ifdef CONFIG_LIBDV095
99 &mpcodecs_vd_libdv,
100 #endif
101 #ifdef CONFIG_QTX_CODECS
102 &mpcodecs_vd_qtvideo,
103 #endif
104 /* Please do not add any new decoders here. If you want to implement a new
105 * decoder, add it to libavcodec, except for wrappers around external
106 * libraries and decoders requiring binary support. */
107 NULL
110 int mpcodecs_config_vo2(sh_video_t *sh, int w, int h,
111 const unsigned int *outfmts,
112 unsigned int preferred_outfmt)
114 struct MPOpts *opts = sh->opts;
115 int j;
116 unsigned int out_fmt = 0;
117 int screen_size_x = 0;
118 int screen_size_y = 0;
119 vf_instance_t *vf = sh->vfilter, *sc = NULL;
120 int palette = 0;
121 int vocfg_flags = 0;
123 if (w)
124 sh->disp_w = w;
125 if (h)
126 sh->disp_h = h;
128 mp_msg(MSGT_DECVIDEO, MSGL_INFO,
129 "VIDEO: %dx%d %5.3f fps %5.1f kbps (%4.1f kB/s)\n",
130 sh->disp_w, sh->disp_h, sh->fps, sh->i_bps * 0.008,
131 sh->i_bps / 1000.0);
133 if (!sh->disp_w || !sh->disp_h)
134 return 0;
136 mp_msg(MSGT_DECVIDEO, MSGL_V,
137 "VDec: vo config request - %d x %d (preferred colorspace: %s)\n",
138 w, h, vo_format_name(preferred_outfmt));
140 if (get_video_quality_max(sh) <= 0 && divx_quality) {
141 // user wants postprocess but no pp filter yet:
142 sh->vfilter = vf = vf_open_filter(opts, vf, "pp", NULL);
145 if (!outfmts || sh->codec->outfmt[0] != 0xffffffff)
146 outfmts = sh->codec->outfmt;
148 // check if libvo and codec has common outfmt (no conversion):
149 csp_again:
151 if (mp_msg_test(MSGT_DECVIDEO, MSGL_V)) {
152 vf_instance_t *f = vf;
153 mp_msg(MSGT_DECVIDEO, MSGL_V, "Trying filter chain:");
154 for (f = vf; f; f = f->next)
155 mp_msg(MSGT_DECVIDEO, MSGL_V, " %s", f->info->name);
156 mp_msg(MSGT_DECVIDEO, MSGL_V, "\n");
159 j = -1;
160 for (int i = 0; i < CODECS_MAX_OUTFMT; i++) {
161 int flags;
162 out_fmt = outfmts[i];
163 if (out_fmt == (unsigned int) 0xFFFFFFFF)
164 break;
165 flags = vf->query_format(vf, out_fmt);
166 mp_msg(MSGT_CPLAYER, MSGL_DBG2,
167 "vo_debug: query(%s) returned 0x%X (i=%d) \n",
168 vo_format_name(out_fmt), flags, i);
169 if ((flags & VFCAP_CSP_SUPPORTED_BY_HW)
170 || (flags & VFCAP_CSP_SUPPORTED && j < 0)) {
171 // check (query) if codec really support this outfmt...
172 sh->outfmtidx = j; // pass index to the control() function this way
173 if (sh->vd_driver->control(sh, VDCTRL_QUERY_FORMAT, &out_fmt) ==
174 CONTROL_FALSE) {
175 mp_msg(MSGT_CPLAYER, MSGL_DBG2,
176 "vo_debug: codec query_format(%s) returned FALSE\n",
177 vo_format_name(out_fmt));
178 continue;
180 j = i;
181 sh->output_flags = flags;
182 if (flags & VFCAP_CSP_SUPPORTED_BY_HW)
183 break;
184 } else if (!palette
185 && !(flags &
186 (VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_CSP_SUPPORTED))
187 && (out_fmt == IMGFMT_RGB8 || out_fmt == IMGFMT_BGR8)) {
188 sh->outfmtidx = j; // pass index to the control() function this way
189 if (sh->vd_driver->control(sh, VDCTRL_QUERY_FORMAT, &out_fmt) !=
190 CONTROL_FALSE)
191 palette = 1;
194 if (j < 0) {
195 // TODO: no match - we should use conversion...
196 if (strcmp(vf->info->name, "scale") && palette != -1) {
197 mp_tmsg(MSGT_DECVIDEO, MSGL_INFO, "Could not find matching colorspace - retrying with -vf scale...\n");
198 sc = vf = vf_open_filter(opts, vf, "scale", NULL);
199 goto csp_again;
200 } else if (palette == 1) {
201 mp_msg(MSGT_DECVIDEO, MSGL_V, "vd: Trying -vf palette...\n");
202 palette = -1;
203 vf = vf_open_filter(opts, vf, "palette", NULL);
204 goto csp_again;
205 } else {
206 // sws failed, if the last filter (vf_vo) support MPEGPES try
207 // to append vf_lavc
208 vf_instance_t *vo, *vp = NULL, *ve, *vpp = NULL;
209 // Remove the scale filter if we added it ourselves
210 if (vf == sc) {
211 ve = vf;
212 vf = vf->next;
213 vf_uninit_filter(ve);
215 // Find the last filter (vf_vo)
216 for (vo = vf; vo->next; vo = vo->next) {
217 vpp = vp;
218 vp = vo;
220 if (vo->query_format(vo, IMGFMT_MPEGPES)
221 && (!vp || (vp && strcmp(vp->info->name, "lavc")))) {
222 ve = vf_open_filter(opts, vo, "lavc", NULL);
223 if (vp)
224 vp->next = ve;
225 else
226 vf = ve;
227 goto csp_again;
229 if (vp && !strcmp(vp->info->name,"lavc")) {
230 if (vpp)
231 vpp->next = vo;
232 else
233 vf = vo;
234 vf_uninit_filter(vp);
237 mp_tmsg(MSGT_CPLAYER, MSGL_WARN,
238 "The selected video_out device is incompatible with this codec.\n"\
239 "Try appending the scale filter to your filter list,\n"\
240 "e.g. -vf spp,scale instead of -vf spp.\n");
241 sh->vf_initialized = -1;
242 return 0; // failed
244 out_fmt = outfmts[j];
245 sh->outfmt = out_fmt;
246 mp_msg(MSGT_CPLAYER, MSGL_V, "VDec: using %s as output csp (no %d)\n",
247 vo_format_name(out_fmt), j);
248 sh->outfmtidx = j;
249 sh->vfilter = vf;
251 // autodetect flipping
252 if (opts->flip == -1) {
253 opts->flip = 0;
254 if (sh->codec->outflags[j] & CODECS_FLAG_FLIP)
255 if (!(sh->codec->outflags[j] & CODECS_FLAG_NOFLIP))
256 opts->flip = 1;
258 if (sh->output_flags & VFCAP_FLIPPED)
259 opts->flip ^= 1;
260 if (opts->flip && !(sh->output_flags & VFCAP_FLIP)) {
261 // we need to flip, but no flipping filter avail.
262 vf_add_before_vo(&vf, "flip", NULL);
263 sh->vfilter = vf;
265 // time to do aspect ratio corrections...
267 if (opts->movie_aspect > -1.0)
268 sh->aspect = opts->movie_aspect; // cmdline overrides autodetect
269 else if (sh->stream_aspect != 0.0)
270 sh->aspect = sh->stream_aspect;
272 if (opts->screen_size_x || opts->screen_size_y) {
273 screen_size_x = opts->screen_size_x;
274 screen_size_y = opts->screen_size_y;
275 if (!opts->vidmode) {
276 if (!screen_size_x)
277 screen_size_x = 1;
278 if (!screen_size_y)
279 screen_size_y = 1;
280 if (screen_size_x <= 8)
281 screen_size_x *= sh->disp_w;
282 if (screen_size_y <= 8)
283 screen_size_y *= sh->disp_h;
285 } else {
286 // check source format aspect, calculate prescale ::atmos
287 screen_size_x = sh->disp_w;
288 screen_size_y = sh->disp_h;
289 if (opts->screen_size_xy >= 0.001) {
290 if (opts->screen_size_xy <= 8) {
291 // -xy means x+y scale
292 screen_size_x *= opts->screen_size_xy;
293 screen_size_y *= opts->screen_size_xy;
294 } else {
295 // -xy means forced width while keeping correct aspect
296 screen_size_x = opts->screen_size_xy;
297 screen_size_y = opts->screen_size_xy * sh->disp_h / sh->disp_w;
300 if (sh->aspect > 0.01) {
301 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ASPECT=%1.4f\n",
302 sh->aspect);
303 int w = screen_size_y * sh->aspect;
304 int h = screen_size_y;
305 // we don't like horizontal downscale || user forced width:
306 if (w < screen_size_x || opts->screen_size_xy > 8) {
307 w = screen_size_x;
308 h = screen_size_x / sh->aspect;
310 if (abs(screen_size_x - w) >= 4 || abs(screen_size_y - h) >= 4) {
311 screen_size_x = w;
312 screen_size_y = h;
313 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Aspect ratio is %.2f:1 - "
314 "scaling to correct movie aspect.\n", sh->aspect);
316 } else {
317 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Movie-Aspect is undefined - no prescaling applied.\n");
321 vocfg_flags = (opts->fullscreen ? VOFLAG_FULLSCREEN : 0)
322 | (opts->vidmode ? VOFLAG_MODESWITCHING : 0)
323 | (opts->softzoom ? VOFLAG_SWSCALE : 0)
324 | (opts->flip ? VOFLAG_FLIPPING : 0);
326 // Time to config libvo!
327 mp_msg(MSGT_CPLAYER, MSGL_V,
328 "VO Config (%dx%d->%dx%d,flags=%d,0x%X)\n", sh->disp_w,
329 sh->disp_h, screen_size_x, screen_size_y, vocfg_flags, out_fmt);
331 vf->w = sh->disp_w;
332 vf->h = sh->disp_h;
334 if (vf_config_wrapper
335 (vf, sh->disp_w, sh->disp_h, screen_size_x, screen_size_y, vocfg_flags,
336 out_fmt) == 0) {
337 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "FATAL: Cannot initialize video driver.\n");
338 sh->vf_initialized = -1;
339 return 0;
342 sh->vf_initialized = 1;
344 set_video_colorspace(sh);
346 if (opts->vo_gamma_gamma != 1000)
347 set_video_colors(sh, "gamma", opts->vo_gamma_gamma);
348 if (opts->vo_gamma_brightness != 1000)
349 set_video_colors(sh, "brightness", opts->vo_gamma_brightness);
350 if (opts->vo_gamma_contrast != 1000)
351 set_video_colors(sh, "contrast", opts->vo_gamma_contrast);
352 if (opts->vo_gamma_saturation != 1000)
353 set_video_colors(sh, "saturation", opts->vo_gamma_saturation);
354 if (opts->vo_gamma_hue != 1000)
355 set_video_colors(sh, "hue", opts->vo_gamma_hue);
357 return 1;
360 // mp_imgtype: buffering type, see mp_image.h
361 // mp_imgflag: buffer requirements (read/write, preserve, stride limits), see mp_image.h
362 // returns NULL or allocated mp_image_t*
363 // Note: buffer allocation may be moved to mpcodecs_config_vo() later...
364 mp_image_t *mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag,
365 int w, int h)
367 return vf_get_image(sh->vfilter, sh->outfmt, mp_imgtype, mp_imgflag, w, h);
370 void mpcodecs_draw_slice(sh_video_t *sh, unsigned char **src, int *stride,
371 int w, int h, int x, int y)
373 struct vf_instance *vf = sh->vfilter;
375 if (vf->draw_slice)
376 vf->draw_slice(vf, src, stride, w, h, x, y);