Merge svn changes up to r30463
[mplayer/glamo.git] / libmpcodecs / vd.c
blob19ac82bf837df575de7fd8c86409fe665a88edca
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 "help_mp.h"
26 #include "options.h"
28 #include "codec-cfg.h"
30 #include "img_format.h"
32 #include "stream/stream.h"
33 #include "libmpdemux/demuxer.h"
34 #include "libmpdemux/stheader.h"
35 #include "dec_video.h"
37 #include "vd.h"
38 #include "vf.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_libmpeg2;
55 extern const vd_functions_t mpcodecs_vd_mpegpes;
56 extern const vd_functions_t mpcodecs_vd_zrmjpeg;
57 extern const vd_functions_t mpcodecs_vd_realvid;
58 extern const vd_functions_t mpcodecs_vd_xvid;
59 extern const vd_functions_t mpcodecs_vd_libdv;
60 extern const vd_functions_t mpcodecs_vd_lzo;
61 extern const vd_functions_t mpcodecs_vd_qtvideo;
63 /* Please do not add any new decoders here. If you want to implement a new
64 * decoder, add it to libavcodec, except for wrappers around external
65 * libraries and decoders requiring binary support. */
67 const vd_functions_t * const mpcodecs_vd_drivers[] = {
68 &mpcodecs_vd_null,
69 #ifdef CONFIG_LIBAVCODEC
70 &mpcodecs_vd_ffmpeg,
71 #endif
72 #ifdef CONFIG_OGGTHEORA
73 &mpcodecs_vd_theora,
74 #endif
75 #ifdef CONFIG_WIN32DLL
76 &mpcodecs_vd_dshow,
77 &mpcodecs_vd_dmo,
78 &mpcodecs_vd_vfw,
79 &mpcodecs_vd_vfwex,
80 #endif
81 &mpcodecs_vd_lzo,
82 &mpcodecs_vd_raw,
83 &mpcodecs_vd_hmblck,
84 #ifdef CONFIG_XANIM
85 &mpcodecs_vd_xanim,
86 #endif
87 #ifdef CONFIG_PNG
88 &mpcodecs_vd_mpng,
89 #endif
90 #ifdef CONFIG_JPEG
91 &mpcodecs_vd_ijpg,
92 #endif
93 &mpcodecs_vd_mtga,
94 &mpcodecs_vd_sgi,
95 #ifdef CONFIG_LIBMPEG2
96 &mpcodecs_vd_libmpeg2,
97 #endif
98 &mpcodecs_vd_mpegpes,
99 #ifdef CONFIG_ZR
100 &mpcodecs_vd_zrmjpeg,
101 #endif
102 #ifdef CONFIG_REALCODECS
103 &mpcodecs_vd_realvid,
104 #endif
105 #ifdef CONFIG_XVID4
106 &mpcodecs_vd_xvid,
107 #endif
108 #ifdef CONFIG_LIBDV095
109 &mpcodecs_vd_libdv,
110 #endif
111 #ifdef CONFIG_QTX_CODECS
112 &mpcodecs_vd_qtvideo,
113 #endif
114 /* Please do not add any new decoders here. If you want to implement a new
115 * decoder, add it to libavcodec, except for wrappers around external
116 * libraries and decoders requiring binary support. */
117 NULL
120 #include "libvo/video_out.h"
122 int mpcodecs_config_vo(sh_video_t *sh, int w, int h,
123 unsigned int preferred_outfmt)
125 struct MPOpts *opts = sh->opts;
126 int i, j;
127 unsigned int out_fmt = 0;
128 int screen_size_x = 0;
129 int screen_size_y = 0;
130 vf_instance_t *vf = sh->vfilter, *sc = NULL;
131 int palette = 0;
132 int vocfg_flags = 0;
134 if (w)
135 sh->disp_w = w;
136 if (h)
137 sh->disp_h = h;
139 if (!sh->disp_w || !sh->disp_h)
140 return 0;
142 mp_msg(MSGT_DECVIDEO, MSGL_V,
143 "VDec: vo config request - %d x %d (preferred colorspace: %s)\n",
144 w, h, vo_format_name(preferred_outfmt));
146 if (get_video_quality_max(sh) <= 0 && divx_quality) {
147 // user wants postprocess but no pp filter yet:
148 sh->vfilter = vf = vf_open_filter(opts, vf, "pp", NULL);
150 // check if libvo and codec has common outfmt (no conversion):
151 csp_again:
153 if (mp_msg_test(MSGT_DECVIDEO, MSGL_V)) {
154 vf_instance_t *f = vf;
155 mp_msg(MSGT_DECVIDEO, MSGL_V, "Trying filter chain:");
156 for (f = vf; f; f = f->next)
157 mp_msg(MSGT_DECVIDEO, MSGL_V, " %s", f->info->name);
158 mp_msg(MSGT_DECVIDEO, MSGL_V, "\n");
161 j = -1;
162 for (i = 0; i < CODECS_MAX_OUTFMT; i++) {
163 int flags;
164 out_fmt = sh->codec->outfmt[i];
165 if (out_fmt == (unsigned int) 0xFFFFFFFF)
166 continue;
167 flags = vf->query_format(vf, out_fmt);
168 mp_msg(MSGT_CPLAYER, MSGL_DBG2,
169 "vo_debug: query(%s) returned 0x%X (i=%d) \n",
170 vo_format_name(out_fmt), flags, i);
171 if ((flags & VFCAP_CSP_SUPPORTED_BY_HW)
172 || (flags & VFCAP_CSP_SUPPORTED && j < 0)) {
173 // check (query) if codec really support this outfmt...
174 sh->outfmtidx = j; // pass index to the control() function this way
175 if (sh->vd_driver->control(sh, VDCTRL_QUERY_FORMAT, &out_fmt) ==
176 CONTROL_FALSE) {
177 mp_msg(MSGT_CPLAYER, MSGL_DBG2,
178 "vo_debug: codec query_format(%s) returned FALSE\n",
179 vo_format_name(out_fmt));
180 continue;
182 j = i;
183 sh->output_flags = flags;
184 if (flags & VFCAP_CSP_SUPPORTED_BY_HW)
185 break;
186 } else if (!palette
187 && !(flags &
188 (VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_CSP_SUPPORTED))
189 && (out_fmt == IMGFMT_RGB8 || out_fmt == IMGFMT_BGR8)) {
190 sh->outfmtidx = j; // pass index to the control() function this way
191 if (sh->vd_driver->control(sh, VDCTRL_QUERY_FORMAT, &out_fmt) !=
192 CONTROL_FALSE)
193 palette = 1;
196 if (j < 0) {
197 // TODO: no match - we should use conversion...
198 if (strcmp(vf->info->name, "scale") && palette != -1) {
199 mp_tmsg(MSGT_DECVIDEO, MSGL_INFO, "Could not find matching colorspace - retrying with -vf scale...\n");
200 sc = vf = vf_open_filter(opts, vf, "scale", NULL);
201 goto csp_again;
202 } else if (palette == 1) {
203 mp_msg(MSGT_DECVIDEO, MSGL_V, "vd: Trying -vf palette...\n");
204 palette = -1;
205 vf = vf_open_filter(opts, vf, "palette", NULL);
206 goto csp_again;
207 } else {
208 // sws failed, if the last filter (vf_vo) support MPEGPES try
209 // to append vf_lavc
210 vf_instance_t *vo, *vp = NULL, *ve, *vpp = NULL;
211 // Remove the scale filter if we added it ourselves
212 if (vf == sc) {
213 ve = vf;
214 vf = vf->next;
215 vf_uninit_filter(ve);
217 // Find the last filter (vf_vo)
218 for (vo = vf; vo->next; vo = vo->next) {
219 vpp = vp;
220 vp = vo;
222 if (vo->query_format(vo, IMGFMT_MPEGPES)
223 && (!vp || (vp && strcmp(vp->info->name, "lavc")))) {
224 ve = vf_open_filter(opts, vo, "lavc", NULL);
225 if (vp)
226 vp->next = ve;
227 else
228 vf = ve;
229 goto csp_again;
231 if (vp && !strcmp(vp->info->name,"lavc")) {
232 if (vpp)
233 vpp->next = vo;
234 else
235 vf = vo;
236 vf_uninit_filter(vp);
239 mp_tmsg(MSGT_CPLAYER, MSGL_WARN,
240 "The selected video_out device is incompatible with this codec.\n"\
241 "Try appending the scale filter to your filter list,\n"\
242 "e.g. -vf spp,scale instead of -vf spp.\n");
243 sh->vf_initialized = -1;
244 return 0; // failed
246 out_fmt = sh->codec->outfmt[j];
247 mp_msg(MSGT_CPLAYER, MSGL_V, "VDec: using %s as output csp (no %d)\n",
248 vo_format_name(out_fmt), j);
249 sh->outfmtidx = j;
250 sh->vfilter = vf;
252 // autodetect flipping
253 if (opts->flip == -1) {
254 opts->flip = 0;
255 if (sh->codec->outflags[j] & CODECS_FLAG_FLIP)
256 if (!(sh->codec->outflags[j] & CODECS_FLAG_NOFLIP))
257 opts->flip = 1;
259 if (sh->output_flags & VFCAP_FLIPPED)
260 opts->flip ^= 1;
261 if (opts->flip && !(sh->output_flags & VFCAP_FLIP)) {
262 // we need to flip, but no flipping filter avail.
263 vf_add_before_vo(&vf, "flip", NULL);
264 sh->vfilter = vf;
266 // time to do aspect ratio corrections...
268 if (opts->movie_aspect > -1.0)
269 sh->aspect = opts->movie_aspect; // cmdline overrides autodetect
270 else if (sh->stream_aspect != 0.0)
271 sh->aspect = sh->stream_aspect;
273 if (opts->screen_size_x || opts->screen_size_y) {
274 screen_size_x = opts->screen_size_x;
275 screen_size_y = opts->screen_size_y;
276 if (!opts->vidmode) {
277 if (!screen_size_x)
278 screen_size_x = 1;
279 if (!screen_size_y)
280 screen_size_y = 1;
281 if (screen_size_x <= 8)
282 screen_size_x *= sh->disp_w;
283 if (screen_size_y <= 8)
284 screen_size_y *= sh->disp_h;
286 } else {
287 // check source format aspect, calculate prescale ::atmos
288 screen_size_x = sh->disp_w;
289 screen_size_y = sh->disp_h;
290 if (opts->screen_size_xy >= 0.001) {
291 if (opts->screen_size_xy <= 8) {
292 // -xy means x+y scale
293 screen_size_x *= opts->screen_size_xy;
294 screen_size_y *= opts->screen_size_xy;
295 } else {
296 // -xy means forced width while keeping correct aspect
297 screen_size_x = opts->screen_size_xy;
298 screen_size_y = opts->screen_size_xy * sh->disp_h / sh->disp_w;
301 if (sh->aspect > 0.01) {
302 int w;
303 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Movie-Aspect is %.2f:1 - prescaling to correct movie aspect.\n",
304 sh->aspect);
305 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ASPECT=%1.4f\n",
306 sh->aspect);
307 w = (int) ((float) screen_size_y * sh->aspect);
308 w += w % 2; // round
309 // we don't like horizontal downscale || user forced width:
310 if (w < screen_size_x || opts->screen_size_xy > 8) {
311 screen_size_y =
312 (int) ((float) screen_size_x * (1.0 / sh->aspect));
313 screen_size_y += screen_size_y % 2; // round
314 } else
315 screen_size_x = w; // keep new width
316 } else {
317 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "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,'%s',0x%X)\n", sh->disp_w,
329 sh->disp_h, screen_size_x, screen_size_y, vocfg_flags, "MPlayer",
330 out_fmt);
332 vf->w = sh->disp_w;
333 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 if (opts->vo_gamma_gamma != 1000)
345 set_video_colors(sh, "gamma", opts->vo_gamma_gamma);
346 if (opts->vo_gamma_brightness != 1000)
347 set_video_colors(sh, "brightness", opts->vo_gamma_brightness);
348 if (opts->vo_gamma_contrast != 1000)
349 set_video_colors(sh, "contrast", opts->vo_gamma_contrast);
350 if (opts->vo_gamma_saturation != 1000)
351 set_video_colors(sh, "saturation", opts->vo_gamma_saturation);
352 if (opts->vo_gamma_hue != 1000)
353 set_video_colors(sh, "hue", opts->vo_gamma_hue);
355 return 1;
358 // mp_imgtype: buffering type, see mp_image.h
359 // mp_imgflag: buffer requirements (read/write, preserve, stride limits), see mp_image.h
360 // returns NULL or allocated mp_image_t*
361 // Note: buffer allocation may be moved to mpcodecs_config_vo() later...
362 mp_image_t *mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag,
363 int w, int h)
365 mp_image_t *mpi =
366 vf_get_image(sh->vfilter, sh->codec->outfmt[sh->outfmtidx], mp_imgtype,
367 mp_imgflag, w, h);
368 if (mpi)
369 mpi->x = mpi->y = 0;
370 return mpi;
373 void mpcodecs_draw_slice(sh_video_t *sh, unsigned char **src, int *stride,
374 int w, int h, int x, int y)
376 struct vf_instance *vf = sh->vfilter;
378 if (vf->draw_slice)
379 vf->draw_slice(vf, src, stride, w, h, x, y);