Merge svn changes up to r29412
[mplayer.git] / libmpcodecs / vd.c
blob62a3fcbddaf17cd2ebef4bee3a02e0590e2cbbd7
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_tmsg(MSGT_DECVIDEO, MSGL_INFO, "VDec: vo config request - %d x %d (preferred colorspace: %s)\n", 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_tmsg(MSGT_DECVIDEO, MSGL_INFO, "Could not find matching colorspace - retrying with -vf scale...\n");
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_tmsg(MSGT_CPLAYER, MSGL_WARN,
212 "The selected video_out device is incompatible with this codec.\n"\
213 "Try appending the scale filter to your filter list,\n"\
214 "e.g. -vf spp,scale instead of -vf spp.\n");
215 sh->vf_initialized = -1;
216 return 0; // failed
218 out_fmt = sh->codec->outfmt[j];
219 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "VDec: using %s as output csp (no %d)\n",
220 vo_format_name(out_fmt), j);
221 sh->outfmtidx = j;
222 sh->vfilter = vf;
224 // autodetect flipping
225 if (opts->flip == -1) {
226 opts->flip = 0;
227 if (sh->codec->outflags[j] & CODECS_FLAG_FLIP)
228 if (!(sh->codec->outflags[j] & CODECS_FLAG_NOFLIP))
229 opts->flip = 1;
231 if (sh->output_flags & VFCAP_FLIPPED)
232 opts->flip ^= 1;
233 if (opts->flip && !(sh->output_flags & VFCAP_FLIP)) {
234 // we need to flip, but no flipping filter avail.
235 vf_add_before_vo(&vf, "flip", NULL);
236 sh->vfilter = vf;
238 // time to do aspect ratio corrections...
240 if (opts->movie_aspect > -1.0)
241 sh->aspect = opts->movie_aspect; // cmdline overrides autodetect
242 else if (sh->stream_aspect != 0.0)
243 sh->aspect = sh->stream_aspect;
245 if (opts->screen_size_x || opts->screen_size_y) {
246 screen_size_x = opts->screen_size_x;
247 screen_size_y = opts->screen_size_y;
248 if (!opts->vidmode) {
249 if (!screen_size_x)
250 screen_size_x = 1;
251 if (!screen_size_y)
252 screen_size_y = 1;
253 if (screen_size_x <= 8)
254 screen_size_x *= sh->disp_w;
255 if (screen_size_y <= 8)
256 screen_size_y *= sh->disp_h;
258 } else {
259 // check source format aspect, calculate prescale ::atmos
260 screen_size_x = sh->disp_w;
261 screen_size_y = sh->disp_h;
262 if (opts->screen_size_xy >= 0.001) {
263 if (opts->screen_size_xy <= 8) {
264 // -xy means x+y scale
265 screen_size_x *= opts->screen_size_xy;
266 screen_size_y *= opts->screen_size_xy;
267 } else {
268 // -xy means forced width while keeping correct aspect
269 screen_size_x = opts->screen_size_xy;
270 screen_size_y = opts->screen_size_xy * sh->disp_h / sh->disp_w;
273 if (sh->aspect > 0.01) {
274 int w;
275 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Movie-Aspect is %.2f:1 - prescaling to correct movie aspect.\n",
276 sh->aspect);
277 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ASPECT=%1.4f\n",
278 sh->aspect);
279 w = (int) ((float) screen_size_y * sh->aspect);
280 w += w % 2; // round
281 // we don't like horizontal downscale || user forced width:
282 if (w < screen_size_x || opts->screen_size_xy > 8) {
283 screen_size_y =
284 (int) ((float) screen_size_x * (1.0 / sh->aspect));
285 screen_size_y += screen_size_y % 2; // round
286 } else
287 screen_size_x = w; // keep new width
288 } else {
289 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Movie-Aspect is undefined - no prescaling applied.\n");
293 vocfg_flags = (opts->fullscreen ? VOFLAG_FULLSCREEN : 0)
294 | (opts->vidmode ? VOFLAG_MODESWITCHING : 0)
295 | (opts->softzoom ? VOFLAG_SWSCALE : 0)
296 | (opts->flip ? VOFLAG_FLIPPING : 0);
298 // Time to config libvo!
299 mp_msg(MSGT_CPLAYER, MSGL_V,
300 "VO Config (%dx%d->%dx%d,flags=%d,'%s',0x%X)\n", sh->disp_w,
301 sh->disp_h, screen_size_x, screen_size_y, vocfg_flags, "MPlayer",
302 out_fmt);
304 vf->w = sh->disp_w;
305 vf->h = sh->disp_h;
306 if (vf_config_wrapper
307 (vf, sh->disp_w, sh->disp_h, screen_size_x, screen_size_y, vocfg_flags,
308 out_fmt) == 0) {
309 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "FATAL: Cannot initialize video driver.\n");
310 sh->vf_initialized = -1;
311 return 0;
314 sh->vf_initialized = 1;
316 if (opts->vo_gamma_gamma != 1000)
317 set_video_colors(sh, "gamma", opts->vo_gamma_gamma);
318 if (opts->vo_gamma_brightness != 1000)
319 set_video_colors(sh, "brightness", opts->vo_gamma_brightness);
320 if (opts->vo_gamma_contrast != 1000)
321 set_video_colors(sh, "contrast", opts->vo_gamma_contrast);
322 if (opts->vo_gamma_saturation != 1000)
323 set_video_colors(sh, "saturation", opts->vo_gamma_saturation);
324 if (opts->vo_gamma_hue != 1000)
325 set_video_colors(sh, "hue", opts->vo_gamma_hue);
327 return 1;
330 // mp_imgtype: buffering type, see mp_image.h
331 // mp_imgflag: buffer requirements (read/write, preserve, stride limits), see mp_image.h
332 // returns NULL or allocated mp_image_t*
333 // Note: buffer allocation may be moved to mpcodecs_config_vo() later...
334 mp_image_t *mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag,
335 int w, int h)
337 mp_image_t *mpi =
338 vf_get_image(sh->vfilter, sh->codec->outfmt[sh->outfmtidx], mp_imgtype,
339 mp_imgflag, w, h);
340 if (mpi)
341 mpi->x = mpi->y = 0;
342 return mpi;
345 void mpcodecs_draw_slice(sh_video_t *sh, unsigned char **src, int *stride,
346 int w, int h, int x, int y)
348 struct vf_instance *vf = sh->vfilter;
350 if (vf->draw_slice)
351 vf->draw_slice(vf, src, stride, w, h, x, y);