vo_glamo: sub.h was moved to sub directory in c9026cb3210205b07e2e068467a18ee40f9259a3
[mplayer/glamo.git] / libmpcodecs / vd.c
blobcd0c913418af1ae7e39af2d4e59ac600dfabb0df
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"
39 extern const vd_functions_t mpcodecs_vd_null;
40 extern const vd_functions_t mpcodecs_vd_ffmpeg;
41 extern const vd_functions_t mpcodecs_vd_theora;
42 extern const vd_functions_t mpcodecs_vd_dshow;
43 extern const vd_functions_t mpcodecs_vd_dmo;
44 extern const vd_functions_t mpcodecs_vd_vfw;
45 extern const vd_functions_t mpcodecs_vd_vfwex;
46 extern const vd_functions_t mpcodecs_vd_raw;
47 extern const vd_functions_t mpcodecs_vd_hmblck;
48 extern const vd_functions_t mpcodecs_vd_xanim;
49 extern const vd_functions_t mpcodecs_vd_mpng;
50 extern const vd_functions_t mpcodecs_vd_ijpg;
51 extern const vd_functions_t mpcodecs_vd_mtga;
52 extern const vd_functions_t mpcodecs_vd_sgi;
53 extern const vd_functions_t mpcodecs_vd_libmpeg2;
54 extern const vd_functions_t mpcodecs_vd_mpegpes;
55 extern const vd_functions_t mpcodecs_vd_hwmp4;
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_FFMPEG
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 &mpcodecs_vd_hwmp4,
100 #ifdef CONFIG_ZR
101 &mpcodecs_vd_zrmjpeg,
102 #endif
103 #ifdef CONFIG_REALCODECS
104 &mpcodecs_vd_realvid,
105 #endif
106 #ifdef CONFIG_XVID4
107 &mpcodecs_vd_xvid,
108 #endif
109 #ifdef CONFIG_LIBDV095
110 &mpcodecs_vd_libdv,
111 #endif
112 #ifdef CONFIG_QTX_CODECS
113 &mpcodecs_vd_qtvideo,
114 #endif
115 /* Please do not add any new decoders here. If you want to implement a new
116 * decoder, add it to libavcodec, except for wrappers around external
117 * libraries and decoders requiring binary support. */
118 NULL
121 #include "libvo/video_out.h"
123 int mpcodecs_config_vo(sh_video_t *sh, int w, int h,
124 unsigned int preferred_outfmt)
126 struct MPOpts *opts = sh->opts;
127 int i, j;
128 unsigned int out_fmt = 0;
129 int screen_size_x = 0;
130 int screen_size_y = 0;
131 vf_instance_t *vf = sh->vfilter, *sc = NULL;
132 int palette = 0;
133 int vocfg_flags = 0;
135 if (w)
136 sh->disp_w = w;
137 if (h)
138 sh->disp_h = h;
140 if (!sh->disp_w || !sh->disp_h)
141 return 0;
143 mp_msg(MSGT_DECVIDEO, MSGL_V,
144 "VDec: vo config request - %d x %d (preferred colorspace: %s)\n",
145 w, h, vo_format_name(preferred_outfmt));
147 if (get_video_quality_max(sh) <= 0 && divx_quality) {
148 // user wants postprocess but no pp filter yet:
149 sh->vfilter = vf = vf_open_filter(opts, vf, "pp", NULL);
151 // check if libvo and codec has common outfmt (no conversion):
152 csp_again:
154 if (mp_msg_test(MSGT_DECVIDEO, MSGL_V)) {
155 vf_instance_t *f = vf;
156 mp_msg(MSGT_DECVIDEO, MSGL_V, "Trying filter chain:");
157 for (f = vf; f; f = f->next)
158 mp_msg(MSGT_DECVIDEO, MSGL_V, " %s", f->info->name);
159 mp_msg(MSGT_DECVIDEO, MSGL_V, "\n");
162 j = -1;
163 for (i = 0; i < CODECS_MAX_OUTFMT; i++) {
164 int flags;
165 out_fmt = sh->codec->outfmt[i];
166 if (out_fmt == (unsigned int) 0xFFFFFFFF)
167 continue;
168 flags = vf->query_format(vf, out_fmt);
169 mp_msg(MSGT_CPLAYER, MSGL_DBG2,
170 "vo_debug: query(%s) returned 0x%X (i=%d) \n",
171 vo_format_name(out_fmt), flags, i);
172 if ((flags & VFCAP_CSP_SUPPORTED_BY_HW)
173 || (flags & VFCAP_CSP_SUPPORTED && j < 0)) {
174 // check (query) if codec really support this outfmt...
175 sh->outfmtidx = j; // pass index to the control() function this way
176 if (sh->vd_driver->control(sh, VDCTRL_QUERY_FORMAT, &out_fmt) ==
177 CONTROL_FALSE) {
178 mp_msg(MSGT_CPLAYER, MSGL_DBG2,
179 "vo_debug: codec query_format(%s) returned FALSE\n",
180 vo_format_name(out_fmt));
181 continue;
183 j = i;
184 sh->output_flags = flags;
185 if (flags & VFCAP_CSP_SUPPORTED_BY_HW)
186 break;
187 } else if (!palette
188 && !(flags &
189 (VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_CSP_SUPPORTED))
190 && (out_fmt == IMGFMT_RGB8 || out_fmt == IMGFMT_BGR8)) {
191 sh->outfmtidx = j; // pass index to the control() function this way
192 if (sh->vd_driver->control(sh, VDCTRL_QUERY_FORMAT, &out_fmt) !=
193 CONTROL_FALSE)
194 palette = 1;
197 if (j < 0) {
198 // TODO: no match - we should use conversion...
199 if (strcmp(vf->info->name, "scale") && palette != -1) {
200 mp_tmsg(MSGT_DECVIDEO, MSGL_INFO, "Could not find matching colorspace - retrying with -vf scale...\n");
201 sc = vf = vf_open_filter(opts, vf, "scale", NULL);
202 goto csp_again;
203 } else if (palette == 1) {
204 mp_msg(MSGT_DECVIDEO, MSGL_V, "vd: Trying -vf palette...\n");
205 palette = -1;
206 vf = vf_open_filter(opts, vf, "palette", NULL);
207 goto csp_again;
208 } else {
209 // sws failed, if the last filter (vf_vo) support MPEGPES try
210 // to append vf_lavc
211 vf_instance_t *vo, *vp = NULL, *ve, *vpp = NULL;
212 // Remove the scale filter if we added it ourselves
213 if (vf == sc) {
214 ve = vf;
215 vf = vf->next;
216 vf_uninit_filter(ve);
218 // Find the last filter (vf_vo)
219 for (vo = vf; vo->next; vo = vo->next) {
220 vpp = vp;
221 vp = vo;
223 if (vo->query_format(vo, IMGFMT_MPEGPES)
224 && (!vp || (vp && strcmp(vp->info->name, "lavc")))) {
225 ve = vf_open_filter(opts, vo, "lavc", NULL);
226 if (vp)
227 vp->next = ve;
228 else
229 vf = ve;
230 goto csp_again;
232 if (vp && !strcmp(vp->info->name,"lavc")) {
233 if (vpp)
234 vpp->next = vo;
235 else
236 vf = vo;
237 vf_uninit_filter(vp);
240 mp_tmsg(MSGT_CPLAYER, MSGL_WARN,
241 "The selected video_out device is incompatible with this codec.\n"\
242 "Try appending the scale filter to your filter list,\n"\
243 "e.g. -vf spp,scale instead of -vf spp.\n");
244 sh->vf_initialized = -1;
245 return 0; // failed
247 out_fmt = sh->codec->outfmt[j];
248 mp_msg(MSGT_CPLAYER, MSGL_V, "VDec: using %s as output csp (no %d)\n",
249 vo_format_name(out_fmt), j);
250 sh->outfmtidx = j;
251 sh->vfilter = vf;
253 // autodetect flipping
254 if (opts->flip == -1) {
255 opts->flip = 0;
256 if (sh->codec->outflags[j] & CODECS_FLAG_FLIP)
257 if (!(sh->codec->outflags[j] & CODECS_FLAG_NOFLIP))
258 opts->flip = 1;
260 if (sh->output_flags & VFCAP_FLIPPED)
261 opts->flip ^= 1;
262 if (opts->flip && !(sh->output_flags & VFCAP_FLIP)) {
263 // we need to flip, but no flipping filter avail.
264 vf_add_before_vo(&vf, "flip", NULL);
265 sh->vfilter = vf;
267 // time to do aspect ratio corrections...
269 if (opts->movie_aspect > -1.0)
270 sh->aspect = opts->movie_aspect; // cmdline overrides autodetect
271 else if (sh->stream_aspect != 0.0)
272 sh->aspect = sh->stream_aspect;
274 if (opts->screen_size_x || opts->screen_size_y) {
275 screen_size_x = opts->screen_size_x;
276 screen_size_y = opts->screen_size_y;
277 if (!opts->vidmode) {
278 if (!screen_size_x)
279 screen_size_x = 1;
280 if (!screen_size_y)
281 screen_size_y = 1;
282 if (screen_size_x <= 8)
283 screen_size_x *= sh->disp_w;
284 if (screen_size_y <= 8)
285 screen_size_y *= sh->disp_h;
287 } else {
288 // check source format aspect, calculate prescale ::atmos
289 screen_size_x = sh->disp_w;
290 screen_size_y = sh->disp_h;
291 if (opts->screen_size_xy >= 0.001) {
292 if (opts->screen_size_xy <= 8) {
293 // -xy means x+y scale
294 screen_size_x *= opts->screen_size_xy;
295 screen_size_y *= opts->screen_size_xy;
296 } else {
297 // -xy means forced width while keeping correct aspect
298 screen_size_x = opts->screen_size_xy;
299 screen_size_y = opts->screen_size_xy * sh->disp_h / sh->disp_w;
302 if (sh->aspect > 0.01) {
303 int w;
304 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Movie-Aspect is %.2f:1 - prescaling to correct movie aspect.\n",
305 sh->aspect);
306 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ASPECT=%1.4f\n",
307 sh->aspect);
308 w = (int) ((float) screen_size_y * sh->aspect);
309 w += w % 2; // round
310 // we don't like horizontal downscale || user forced width:
311 if (w < screen_size_x || opts->screen_size_xy > 8) {
312 screen_size_y =
313 (int) ((float) screen_size_x * (1.0 / sh->aspect));
314 screen_size_y += screen_size_y % 2; // round
315 } else
316 screen_size_x = w; // keep new width
317 } else {
318 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Movie-Aspect is undefined - no prescaling applied.\n");
322 vocfg_flags = (opts->fullscreen ? VOFLAG_FULLSCREEN : 0)
323 | (opts->vidmode ? VOFLAG_MODESWITCHING : 0)
324 | (opts->softzoom ? VOFLAG_SWSCALE : 0)
325 | (opts->flip ? VOFLAG_FLIPPING : 0);
327 // Time to config libvo!
328 mp_msg(MSGT_CPLAYER, MSGL_V,
329 "VO Config (%dx%d->%dx%d,flags=%d,'%s',0x%X)\n", sh->disp_w,
330 sh->disp_h, screen_size_x, screen_size_y, vocfg_flags, "MPlayer",
331 out_fmt);
333 vf->w = sh->disp_w;
334 vf->h = sh->disp_h;
335 if (vf_config_wrapper
336 (vf, sh->disp_w, sh->disp_h, screen_size_x, screen_size_y, vocfg_flags,
337 out_fmt) == 0) {
338 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "FATAL: Cannot initialize video driver.\n");
339 sh->vf_initialized = -1;
340 return 0;
343 sh->vf_initialized = 1;
345 if (opts->vo_gamma_gamma != 1000)
346 set_video_colors(sh, "gamma", opts->vo_gamma_gamma);
347 if (opts->vo_gamma_brightness != 1000)
348 set_video_colors(sh, "brightness", opts->vo_gamma_brightness);
349 if (opts->vo_gamma_contrast != 1000)
350 set_video_colors(sh, "contrast", opts->vo_gamma_contrast);
351 if (opts->vo_gamma_saturation != 1000)
352 set_video_colors(sh, "saturation", opts->vo_gamma_saturation);
353 if (opts->vo_gamma_hue != 1000)
354 set_video_colors(sh, "hue", opts->vo_gamma_hue);
356 return 1;
359 // mp_imgtype: buffering type, see mp_image.h
360 // mp_imgflag: buffer requirements (read/write, preserve, stride limits), see mp_image.h
361 // returns NULL or allocated mp_image_t*
362 // Note: buffer allocation may be moved to mpcodecs_config_vo() later...
363 mp_image_t *mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag,
364 int w, int h)
366 mp_image_t *mpi =
367 vf_get_image(sh->vfilter, sh->codec->outfmt[sh->outfmtidx], mp_imgtype,
368 mp_imgflag, w, h);
369 if (mpi)
370 mpi->x = mpi->y = 0;
371 return mpi;
374 void mpcodecs_draw_slice(sh_video_t *sh, unsigned char **src, int *stride,
375 int w, int h, int x, int y)
377 struct vf_instance *vf = sh->vfilter;
379 if (vf->draw_slice)
380 vf->draw_slice(vf, src, stride, w, h, x, y);