vo_xv: Fix context Shminfo table size
[mplayer.git] / libmpcodecs / vd.c
blobe585d7507877c98eb4dc24155b6ce95ca25ac46a
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "config.h"
6 #include "mp_msg.h"
7 #include "help_mp.h"
8 #include "options.h"
10 #include "codec-cfg.h"
12 #include "img_format.h"
14 #include "stream/stream.h"
15 #include "libmpdemux/demuxer.h"
16 #include "libmpdemux/stheader.h"
17 #include "dec_video.h"
19 #include "vd.h"
20 #include "vf.h"
22 extern const vd_functions_t mpcodecs_vd_null;
23 extern const vd_functions_t mpcodecs_vd_ffmpeg;
24 extern const vd_functions_t mpcodecs_vd_theora;
25 extern const vd_functions_t mpcodecs_vd_dshow;
26 extern const vd_functions_t mpcodecs_vd_dmo;
27 extern const vd_functions_t mpcodecs_vd_vfw;
28 extern const vd_functions_t mpcodecs_vd_vfwex;
29 extern const vd_functions_t mpcodecs_vd_raw;
30 extern const vd_functions_t mpcodecs_vd_hmblck;
31 extern const vd_functions_t mpcodecs_vd_xanim;
32 extern const vd_functions_t mpcodecs_vd_mpng;
33 extern const vd_functions_t mpcodecs_vd_ijpg;
34 extern const vd_functions_t mpcodecs_vd_mtga;
35 extern const vd_functions_t mpcodecs_vd_sgi;
36 extern const vd_functions_t mpcodecs_vd_libmpeg2;
37 extern const vd_functions_t mpcodecs_vd_mpegpes;
38 extern const vd_functions_t mpcodecs_vd_zrmjpeg;
39 extern const vd_functions_t mpcodecs_vd_realvid;
40 extern const vd_functions_t mpcodecs_vd_xvid;
41 extern const vd_functions_t mpcodecs_vd_libdv;
42 extern const vd_functions_t mpcodecs_vd_lzo;
43 extern const vd_functions_t mpcodecs_vd_qtvideo;
45 /* Please do not add any new decoders here. If you want to implement a new
46 * decoder, add it to libavcodec, except for wrappers around external
47 * libraries and decoders requiring binary support. */
49 const vd_functions_t * const mpcodecs_vd_drivers[] = {
50 &mpcodecs_vd_null,
51 #ifdef CONFIG_LIBAVCODEC
52 &mpcodecs_vd_ffmpeg,
53 #endif
54 #ifdef CONFIG_OGGTHEORA
55 &mpcodecs_vd_theora,
56 #endif
57 #ifdef CONFIG_WIN32DLL
58 &mpcodecs_vd_dshow,
59 &mpcodecs_vd_dmo,
60 &mpcodecs_vd_vfw,
61 &mpcodecs_vd_vfwex,
62 #endif
63 &mpcodecs_vd_lzo,
64 &mpcodecs_vd_raw,
65 &mpcodecs_vd_hmblck,
66 #ifdef CONFIG_XANIM
67 &mpcodecs_vd_xanim,
68 #endif
69 #ifdef CONFIG_PNG
70 &mpcodecs_vd_mpng,
71 #endif
72 #ifdef CONFIG_JPEG
73 &mpcodecs_vd_ijpg,
74 #endif
75 &mpcodecs_vd_mtga,
76 &mpcodecs_vd_sgi,
77 #ifdef CONFIG_LIBMPEG2
78 &mpcodecs_vd_libmpeg2,
79 #endif
80 &mpcodecs_vd_mpegpes,
81 #ifdef CONFIG_ZR
82 &mpcodecs_vd_zrmjpeg,
83 #endif
84 #ifdef CONFIG_REALCODECS
85 &mpcodecs_vd_realvid,
86 #endif
87 #ifdef CONFIG_XVID4
88 &mpcodecs_vd_xvid,
89 #endif
90 #ifdef CONFIG_LIBDV095
91 &mpcodecs_vd_libdv,
92 #endif
93 #ifdef CONFIG_QTX_CODECS
94 &mpcodecs_vd_qtvideo,
95 #endif
96 /* Please do not add any new decoders here. If you want to implement a new
97 * decoder, add it to libavcodec, except for wrappers around external
98 * libraries and decoders requiring binary support. */
99 NULL
102 #include "libvo/video_out.h"
104 int mpcodecs_config_vo(sh_video_t *sh, int w, int h,
105 unsigned int preferred_outfmt)
107 struct MPOpts *opts = sh->opts;
108 int i, j;
109 unsigned int out_fmt = 0;
110 int screen_size_x = 0;
111 int screen_size_y = 0;
112 vf_instance_t *vf = sh->vfilter, *sc = NULL;
113 int palette = 0;
114 int vocfg_flags = 0;
116 if (w)
117 sh->disp_w = w;
118 if (h)
119 sh->disp_h = h;
121 if (!sh->disp_w || !sh->disp_h)
122 return 0;
124 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_VoConfigRequest, w, h,
125 vo_format_name(preferred_outfmt));
127 if (get_video_quality_max(sh) <= 0 && divx_quality) {
128 // user wants postprocess but no pp filter yet:
129 sh->vfilter = vf = vf_open_filter(opts, vf, "pp", NULL);
131 // check if libvo and codec has common outfmt (no conversion):
132 csp_again:
134 if (mp_msg_test(MSGT_DECVIDEO, MSGL_V)) {
135 vf_instance_t *f = vf;
136 mp_msg(MSGT_DECVIDEO, MSGL_V, "Trying filter chain:");
137 for (f = vf; f; f = f->next)
138 mp_msg(MSGT_DECVIDEO, MSGL_V, " %s", f->info->name);
139 mp_msg(MSGT_DECVIDEO, MSGL_V, "\n");
142 j = -1;
143 for (i = 0; i < CODECS_MAX_OUTFMT; i++) {
144 int flags;
145 out_fmt = sh->codec->outfmt[i];
146 if (out_fmt == (unsigned int) 0xFFFFFFFF)
147 continue;
148 flags = vf->query_format(vf, out_fmt);
149 mp_msg(MSGT_CPLAYER, MSGL_DBG2,
150 "vo_debug: query(%s) returned 0x%X (i=%d) \n",
151 vo_format_name(out_fmt), flags, i);
152 if ((flags & VFCAP_CSP_SUPPORTED_BY_HW)
153 || (flags & VFCAP_CSP_SUPPORTED && j < 0)) {
154 // check (query) if codec really support this outfmt...
155 sh->outfmtidx = j; // pass index to the control() function this way
156 if (sh->vd_driver->control(sh, VDCTRL_QUERY_FORMAT, &out_fmt) ==
157 CONTROL_FALSE) {
158 mp_msg(MSGT_CPLAYER, MSGL_DBG2,
159 "vo_debug: codec query_format(%s) returned FALSE\n",
160 vo_format_name(out_fmt));
161 continue;
163 j = i;
164 sh->output_flags = flags;
165 if (flags & VFCAP_CSP_SUPPORTED_BY_HW)
166 break;
167 } else if (!palette
168 && !(flags &
169 (VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_CSP_SUPPORTED))
170 && (out_fmt == IMGFMT_RGB8 || out_fmt == IMGFMT_BGR8)) {
171 sh->outfmtidx = j; // pass index to the control() function this way
172 if (sh->vd_driver->control(sh, VDCTRL_QUERY_FORMAT, &out_fmt) !=
173 CONTROL_FALSE)
174 palette = 1;
177 if (j < 0) {
178 // TODO: no match - we should use conversion...
179 if (strcmp(vf->info->name, "scale") && palette != -1) {
180 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_CouldNotFindColorspace);
181 sc = vf = vf_open_filter(opts, vf, "scale", NULL);
182 goto csp_again;
183 } else if (palette == 1) {
184 mp_msg(MSGT_DECVIDEO, MSGL_V, "vd: Trying -vf palette...\n");
185 palette = -1;
186 vf = vf_open_filter(opts, vf, "palette", NULL);
187 goto csp_again;
188 } else {
189 // sws failed, if the last filter (vf_vo) support MPEGPES try
190 // to append vf_lavc
191 vf_instance_t *vo, *vp = NULL, *ve;
192 // Remove the scale filter if we added it ourselves
193 if (vf == sc) {
194 ve = vf;
195 vf = vf->next;
196 vf_uninit_filter(ve);
198 // Find the last filter (vf_vo)
199 for (vo = vf; vo->next; vo = vo->next)
200 vp = vo;
201 if (vo->query_format(vo, IMGFMT_MPEGPES)
202 && (!vp || (vp && strcmp(vp->info->name, "lavc")))) {
203 ve = vf_open_filter(opts, vo, "lavc", NULL);
204 if (vp)
205 vp->next = ve;
206 else
207 vf = ve;
208 goto csp_again;
211 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_VOincompCodec);
212 sh->vf_initialized = -1;
213 return 0; // failed
215 out_fmt = sh->codec->outfmt[j];
216 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_UsingXAsOutputCspNoY,
217 vo_format_name(out_fmt), j);
218 sh->outfmtidx = j;
219 sh->vfilter = vf;
221 // autodetect flipping
222 if (opts->flip == -1) {
223 opts->flip = 0;
224 if (sh->codec->outflags[j] & CODECS_FLAG_FLIP)
225 if (!(sh->codec->outflags[j] & CODECS_FLAG_NOFLIP))
226 opts->flip = 1;
228 if (sh->output_flags & VFCAP_FLIPPED)
229 opts->flip ^= 1;
230 if (opts->flip && !(sh->output_flags & VFCAP_FLIP)) {
231 // we need to flip, but no flipping filter avail.
232 vf_add_before_vo(&vf, "flip", NULL);
233 sh->vfilter = vf;
235 // time to do aspect ratio corrections...
237 if (opts->movie_aspect > -1.0)
238 sh->aspect = opts->movie_aspect; // cmdline overrides autodetect
239 else if (sh->stream_aspect != 0.0)
240 sh->aspect = sh->stream_aspect;
242 if (opts->screen_size_x || opts->screen_size_y) {
243 screen_size_x = opts->screen_size_x;
244 screen_size_y = opts->screen_size_y;
245 if (!opts->vidmode) {
246 if (!screen_size_x)
247 screen_size_x = 1;
248 if (!screen_size_y)
249 screen_size_y = 1;
250 if (screen_size_x <= 8)
251 screen_size_x *= sh->disp_w;
252 if (screen_size_y <= 8)
253 screen_size_y *= sh->disp_h;
255 } else {
256 // check source format aspect, calculate prescale ::atmos
257 screen_size_x = sh->disp_w;
258 screen_size_y = sh->disp_h;
259 if (opts->screen_size_xy >= 0.001) {
260 if (opts->screen_size_xy <= 8) {
261 // -xy means x+y scale
262 screen_size_x *= opts->screen_size_xy;
263 screen_size_y *= opts->screen_size_xy;
264 } else {
265 // -xy means forced width while keeping correct aspect
266 screen_size_x = opts->screen_size_xy;
267 screen_size_y = opts->screen_size_xy * sh->disp_h / sh->disp_w;
270 if (sh->aspect > 0.01) {
271 int w;
272 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_MovieAspectIsSet,
273 sh->aspect);
274 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ASPECT=%1.4f\n",
275 sh->aspect);
276 w = (int) ((float) screen_size_y * sh->aspect);
277 w += w % 2; // round
278 // we don't like horizontal downscale || user forced width:
279 if (w < screen_size_x || opts->screen_size_xy > 8) {
280 screen_size_y =
281 (int) ((float) screen_size_x * (1.0 / sh->aspect));
282 screen_size_y += screen_size_y % 2; // round
283 } else
284 screen_size_x = w; // keep new width
285 } else {
286 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_MovieAspectUndefined);
290 vocfg_flags = (opts->fullscreen ? VOFLAG_FULLSCREEN : 0)
291 | (opts->vidmode ? VOFLAG_MODESWITCHING : 0)
292 | (opts->softzoom ? VOFLAG_SWSCALE : 0)
293 | (opts->flip ? VOFLAG_FLIPPING : 0);
295 // Time to config libvo!
296 mp_msg(MSGT_CPLAYER, MSGL_V,
297 "VO Config (%dx%d->%dx%d,flags=%d,'%s',0x%X)\n", sh->disp_w,
298 sh->disp_h, screen_size_x, screen_size_y, vocfg_flags, "MPlayer",
299 out_fmt);
301 vf->w = sh->disp_w;
302 vf->h = sh->disp_h;
303 if (vf_config_wrapper
304 (vf, sh->disp_w, sh->disp_h, screen_size_x, screen_size_y, vocfg_flags,
305 out_fmt) == 0) {
306 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_CannotInitVO);
307 sh->vf_initialized = -1;
308 return 0;
311 sh->vf_initialized = 1;
313 if (opts->vo_gamma_gamma != 1000)
314 set_video_colors(sh, "gamma", opts->vo_gamma_gamma);
315 if (opts->vo_gamma_brightness != 1000)
316 set_video_colors(sh, "brightness", opts->vo_gamma_brightness);
317 if (opts->vo_gamma_contrast != 1000)
318 set_video_colors(sh, "contrast", opts->vo_gamma_contrast);
319 if (opts->vo_gamma_saturation != 1000)
320 set_video_colors(sh, "saturation", opts->vo_gamma_saturation);
321 if (opts->vo_gamma_hue != 1000)
322 set_video_colors(sh, "hue", opts->vo_gamma_hue);
324 return 1;
327 // mp_imgtype: buffering type, see mp_image.h
328 // mp_imgflag: buffer requirements (read/write, preserve, stride limits), see mp_image.h
329 // returns NULL or allocated mp_image_t*
330 // Note: buffer allocation may be moved to mpcodecs_config_vo() later...
331 mp_image_t *mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag,
332 int w, int h)
334 mp_image_t *mpi =
335 vf_get_image(sh->vfilter, sh->codec->outfmt[sh->outfmtidx], mp_imgtype,
336 mp_imgflag, w, h);
337 if (mpi)
338 mpi->x = mpi->y = 0;
339 return mpi;
342 void mpcodecs_draw_slice(sh_video_t *sh, unsigned char **src, int *stride,
343 int w, int h, int x, int y)
345 struct vf_instance *vf = sh->vfilter;
347 if (vf->draw_slice)
348 vf->draw_slice(vf, src, stride, w, h, x, y);