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