ao: fix crash after ao init failure (from recent 3a5fd15fa2)
[mplayer/greg.git] / libmpcodecs / vd.c
blobc54ae5260bc54e4f6ad2b2af0fa2df36354535b3
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_mpegpes;
54 extern const vd_functions_t mpcodecs_vd_realvid;
55 extern const vd_functions_t mpcodecs_vd_xvid;
56 extern const vd_functions_t mpcodecs_vd_libdv;
57 extern const vd_functions_t mpcodecs_vd_lzo;
58 extern const vd_functions_t mpcodecs_vd_qtvideo;
60 /* Please do not add any new decoders here. If you want to implement a new
61 * decoder, add it to libavcodec, except for wrappers around external
62 * libraries and decoders requiring binary support. */
64 const vd_functions_t * const mpcodecs_vd_drivers[] = {
65 &mpcodecs_vd_null,
66 #ifdef CONFIG_FFMPEG
67 &mpcodecs_vd_ffmpeg,
68 #endif
69 #ifdef CONFIG_OGGTHEORA
70 &mpcodecs_vd_theora,
71 #endif
72 #ifdef CONFIG_WIN32DLL
73 &mpcodecs_vd_dshow,
74 &mpcodecs_vd_dmo,
75 &mpcodecs_vd_vfw,
76 &mpcodecs_vd_vfwex,
77 #endif
78 &mpcodecs_vd_lzo,
79 &mpcodecs_vd_raw,
80 &mpcodecs_vd_hmblck,
81 #ifdef CONFIG_XANIM
82 &mpcodecs_vd_xanim,
83 #endif
84 #ifdef CONFIG_PNG
85 &mpcodecs_vd_mpng,
86 #endif
87 #ifdef CONFIG_JPEG
88 &mpcodecs_vd_ijpg,
89 #endif
90 &mpcodecs_vd_mtga,
91 &mpcodecs_vd_sgi,
92 &mpcodecs_vd_mpegpes,
93 #ifdef CONFIG_REALCODECS
94 &mpcodecs_vd_realvid,
95 #endif
96 #ifdef CONFIG_XVID4
97 &mpcodecs_vd_xvid,
98 #endif
99 #ifdef CONFIG_LIBDV095
100 &mpcodecs_vd_libdv,
101 #endif
102 #ifdef CONFIG_QTX_CODECS
103 &mpcodecs_vd_qtvideo,
104 #endif
105 /* Please do not add any new decoders here. If you want to implement a new
106 * decoder, add it to libavcodec, except for wrappers around external
107 * libraries and decoders requiring binary support. */
108 NULL
111 #include "libvo/video_out.h"
113 int mpcodecs_config_vo(sh_video_t *sh, int w, int h,
114 unsigned int preferred_outfmt)
116 struct MPOpts *opts = sh->opts;
117 int i, j;
118 unsigned int out_fmt = 0;
119 int screen_size_x = 0;
120 int screen_size_y = 0;
121 vf_instance_t *vf = sh->vfilter, *sc = NULL;
122 int palette = 0;
123 int vocfg_flags = 0;
125 if (w)
126 sh->disp_w = w;
127 if (h)
128 sh->disp_h = h;
130 if (!sh->disp_w || !sh->disp_h)
131 return 0;
133 mp_msg(MSGT_DECVIDEO, MSGL_V,
134 "VDec: vo config request - %d x %d (preferred colorspace: %s)\n",
135 w, h, vo_format_name(preferred_outfmt));
137 if (get_video_quality_max(sh) <= 0 && divx_quality) {
138 // user wants postprocess but no pp filter yet:
139 sh->vfilter = vf = vf_open_filter(opts, vf, "pp", NULL);
141 // check if libvo and codec has common outfmt (no conversion):
142 csp_again:
144 if (mp_msg_test(MSGT_DECVIDEO, MSGL_V)) {
145 vf_instance_t *f = vf;
146 mp_msg(MSGT_DECVIDEO, MSGL_V, "Trying filter chain:");
147 for (f = vf; f; f = f->next)
148 mp_msg(MSGT_DECVIDEO, MSGL_V, " %s", f->info->name);
149 mp_msg(MSGT_DECVIDEO, MSGL_V, "\n");
152 j = -1;
153 for (i = 0; i < CODECS_MAX_OUTFMT; i++) {
154 int flags;
155 out_fmt = sh->codec->outfmt[i];
156 if (out_fmt == (unsigned int) 0xFFFFFFFF)
157 continue;
158 flags = vf->query_format(vf, out_fmt);
159 mp_msg(MSGT_CPLAYER, MSGL_DBG2,
160 "vo_debug: query(%s) returned 0x%X (i=%d) \n",
161 vo_format_name(out_fmt), flags, i);
162 if ((flags & VFCAP_CSP_SUPPORTED_BY_HW)
163 || (flags & VFCAP_CSP_SUPPORTED && j < 0)) {
164 // check (query) if codec really support this outfmt...
165 sh->outfmtidx = j; // pass index to the control() function this way
166 if (sh->vd_driver->control(sh, VDCTRL_QUERY_FORMAT, &out_fmt) ==
167 CONTROL_FALSE) {
168 mp_msg(MSGT_CPLAYER, MSGL_DBG2,
169 "vo_debug: codec query_format(%s) returned FALSE\n",
170 vo_format_name(out_fmt));
171 continue;
173 j = i;
174 sh->output_flags = flags;
175 if (flags & VFCAP_CSP_SUPPORTED_BY_HW)
176 break;
177 } else if (!palette
178 && !(flags &
179 (VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_CSP_SUPPORTED))
180 && (out_fmt == IMGFMT_RGB8 || out_fmt == IMGFMT_BGR8)) {
181 sh->outfmtidx = j; // pass index to the control() function this way
182 if (sh->vd_driver->control(sh, VDCTRL_QUERY_FORMAT, &out_fmt) !=
183 CONTROL_FALSE)
184 palette = 1;
187 if (j < 0) {
188 // TODO: no match - we should use conversion...
189 if (strcmp(vf->info->name, "scale") && palette != -1) {
190 mp_tmsg(MSGT_DECVIDEO, MSGL_INFO, "Could not find matching colorspace - retrying with -vf scale...\n");
191 sc = vf = vf_open_filter(opts, vf, "scale", NULL);
192 goto csp_again;
193 } else if (palette == 1) {
194 mp_msg(MSGT_DECVIDEO, MSGL_V, "vd: Trying -vf palette...\n");
195 palette = -1;
196 vf = vf_open_filter(opts, vf, "palette", NULL);
197 goto csp_again;
198 } else {
199 // sws failed, if the last filter (vf_vo) support MPEGPES try
200 // to append vf_lavc
201 vf_instance_t *vo, *vp = NULL, *ve, *vpp = NULL;
202 // Remove the scale filter if we added it ourselves
203 if (vf == sc) {
204 ve = vf;
205 vf = vf->next;
206 vf_uninit_filter(ve);
208 // Find the last filter (vf_vo)
209 for (vo = vf; vo->next; vo = vo->next) {
210 vpp = vp;
211 vp = vo;
213 if (vo->query_format(vo, IMGFMT_MPEGPES)
214 && (!vp || (vp && strcmp(vp->info->name, "lavc")))) {
215 ve = vf_open_filter(opts, vo, "lavc", NULL);
216 if (vp)
217 vp->next = ve;
218 else
219 vf = ve;
220 goto csp_again;
222 if (vp && !strcmp(vp->info->name,"lavc")) {
223 if (vpp)
224 vpp->next = vo;
225 else
226 vf = vo;
227 vf_uninit_filter(vp);
230 mp_tmsg(MSGT_CPLAYER, MSGL_WARN,
231 "The selected video_out device is incompatible with this codec.\n"\
232 "Try appending the scale filter to your filter list,\n"\
233 "e.g. -vf spp,scale instead of -vf spp.\n");
234 sh->vf_initialized = -1;
235 return 0; // failed
237 out_fmt = sh->codec->outfmt[j];
238 mp_msg(MSGT_CPLAYER, MSGL_V, "VDec: using %s as output csp (no %d)\n",
239 vo_format_name(out_fmt), j);
240 sh->outfmtidx = j;
241 sh->vfilter = vf;
243 // autodetect flipping
244 if (opts->flip == -1) {
245 opts->flip = 0;
246 if (sh->codec->outflags[j] & CODECS_FLAG_FLIP)
247 if (!(sh->codec->outflags[j] & CODECS_FLAG_NOFLIP))
248 opts->flip = 1;
250 if (sh->output_flags & VFCAP_FLIPPED)
251 opts->flip ^= 1;
252 if (opts->flip && !(sh->output_flags & VFCAP_FLIP)) {
253 // we need to flip, but no flipping filter avail.
254 vf_add_before_vo(&vf, "flip", NULL);
255 sh->vfilter = vf;
257 // time to do aspect ratio corrections...
259 if (opts->movie_aspect > -1.0)
260 sh->aspect = opts->movie_aspect; // cmdline overrides autodetect
261 else if (sh->stream_aspect != 0.0)
262 sh->aspect = sh->stream_aspect;
264 if (opts->screen_size_x || opts->screen_size_y) {
265 screen_size_x = opts->screen_size_x;
266 screen_size_y = opts->screen_size_y;
267 if (!opts->vidmode) {
268 if (!screen_size_x)
269 screen_size_x = 1;
270 if (!screen_size_y)
271 screen_size_y = 1;
272 if (screen_size_x <= 8)
273 screen_size_x *= sh->disp_w;
274 if (screen_size_y <= 8)
275 screen_size_y *= sh->disp_h;
277 } else {
278 // check source format aspect, calculate prescale ::atmos
279 screen_size_x = sh->disp_w;
280 screen_size_y = sh->disp_h;
281 if (opts->screen_size_xy >= 0.001) {
282 if (opts->screen_size_xy <= 8) {
283 // -xy means x+y scale
284 screen_size_x *= opts->screen_size_xy;
285 screen_size_y *= opts->screen_size_xy;
286 } else {
287 // -xy means forced width while keeping correct aspect
288 screen_size_x = opts->screen_size_xy;
289 screen_size_y = opts->screen_size_xy * sh->disp_h / sh->disp_w;
292 if (sh->aspect > 0.01) {
293 int w;
294 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Movie-Aspect is %.2f:1 - prescaling to correct movie aspect.\n",
295 sh->aspect);
296 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ASPECT=%1.4f\n",
297 sh->aspect);
298 w = (int) ((float) screen_size_y * sh->aspect);
299 w += w % 2; // round
300 // we don't like horizontal downscale || user forced width:
301 if (w < screen_size_x || opts->screen_size_xy > 8) {
302 screen_size_y =
303 (int) ((float) screen_size_x * (1.0 / sh->aspect));
304 screen_size_y += screen_size_y % 2; // round
305 } else
306 screen_size_x = w; // keep new width
307 } else {
308 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Movie-Aspect is undefined - no prescaling applied.\n");
312 vocfg_flags = (opts->fullscreen ? VOFLAG_FULLSCREEN : 0)
313 | (opts->vidmode ? VOFLAG_MODESWITCHING : 0)
314 | (opts->softzoom ? VOFLAG_SWSCALE : 0)
315 | (opts->flip ? VOFLAG_FLIPPING : 0);
317 // Time to config libvo!
318 mp_msg(MSGT_CPLAYER, MSGL_V,
319 "VO Config (%dx%d->%dx%d,flags=%d,'%s',0x%X)\n", sh->disp_w,
320 sh->disp_h, screen_size_x, screen_size_y, vocfg_flags, "MPlayer",
321 out_fmt);
323 vf->w = sh->disp_w;
324 vf->h = sh->disp_h;
325 if (vf_config_wrapper
326 (vf, sh->disp_w, sh->disp_h, screen_size_x, screen_size_y, vocfg_flags,
327 out_fmt) == 0) {
328 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "FATAL: Cannot initialize video driver.\n");
329 sh->vf_initialized = -1;
330 return 0;
333 sh->vf_initialized = 1;
335 if (opts->vo_gamma_gamma != 1000)
336 set_video_colors(sh, "gamma", opts->vo_gamma_gamma);
337 if (opts->vo_gamma_brightness != 1000)
338 set_video_colors(sh, "brightness", opts->vo_gamma_brightness);
339 if (opts->vo_gamma_contrast != 1000)
340 set_video_colors(sh, "contrast", opts->vo_gamma_contrast);
341 if (opts->vo_gamma_saturation != 1000)
342 set_video_colors(sh, "saturation", opts->vo_gamma_saturation);
343 if (opts->vo_gamma_hue != 1000)
344 set_video_colors(sh, "hue", opts->vo_gamma_hue);
346 return 1;
349 // mp_imgtype: buffering type, see mp_image.h
350 // mp_imgflag: buffer requirements (read/write, preserve, stride limits), see mp_image.h
351 // returns NULL or allocated mp_image_t*
352 // Note: buffer allocation may be moved to mpcodecs_config_vo() later...
353 mp_image_t *mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag,
354 int w, int h)
356 mp_image_t *mpi =
357 vf_get_image(sh->vfilter, sh->codec->outfmt[sh->outfmtidx], mp_imgtype,
358 mp_imgflag, w, h);
359 if (mpi)
360 mpi->x = mpi->y = 0;
361 return mpi;
364 void mpcodecs_draw_slice(sh_video_t *sh, unsigned char **src, int *stride,
365 int w, int h, int x, int y)
367 struct vf_instance *vf = sh->vfilter;
369 if (vf->draw_slice)
370 vf->draw_slice(vf, src, stride, w, h, x, y);