Use MSGT_DECVIDEO in a video decoder.
[mplayer/glamo.git] / libmpcodecs / vd.c
blob2aaeb65f653af8f921a7d735b0040479d60f9c7b
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 "help_mp.h"
27 #include "codec-cfg.h"
28 //#include "mp_image.h"
30 #include "img_format.h"
32 #include "stream/stream.h"
33 #include "libmpdemux/demuxer.h"
34 #include "libmpdemux/stheader.h"
35 #include "dec_video.h"
37 #include "vd.h"
38 #include "vf.h"
40 //#include "vd_internal.h"
42 extern const vd_functions_t mpcodecs_vd_null;
43 extern const vd_functions_t mpcodecs_vd_ffmpeg;
44 extern const vd_functions_t mpcodecs_vd_theora;
45 extern const vd_functions_t mpcodecs_vd_dshow;
46 extern const vd_functions_t mpcodecs_vd_dmo;
47 extern const vd_functions_t mpcodecs_vd_vfw;
48 extern const vd_functions_t mpcodecs_vd_vfwex;
49 extern const vd_functions_t mpcodecs_vd_raw;
50 extern const vd_functions_t mpcodecs_vd_hmblck;
51 extern const vd_functions_t mpcodecs_vd_xanim;
52 extern const vd_functions_t mpcodecs_vd_mpng;
53 extern const vd_functions_t mpcodecs_vd_ijpg;
54 extern const vd_functions_t mpcodecs_vd_mtga;
55 extern const vd_functions_t mpcodecs_vd_sgi;
56 extern const vd_functions_t mpcodecs_vd_libmpeg2;
57 extern const vd_functions_t mpcodecs_vd_mpegpes;
58 extern const vd_functions_t mpcodecs_vd_zrmjpeg;
59 extern const vd_functions_t mpcodecs_vd_realvid;
60 extern const vd_functions_t mpcodecs_vd_xvid;
61 extern const vd_functions_t mpcodecs_vd_libdv;
62 extern const vd_functions_t mpcodecs_vd_lzo;
63 extern const vd_functions_t mpcodecs_vd_qtvideo;
65 /* Please do not add any new decoders here. If you want to implement a new
66 * decoder, add it to libavcodec, except for wrappers around external
67 * libraries and decoders requiring binary support. */
69 const vd_functions_t * const mpcodecs_vd_drivers[] = {
70 &mpcodecs_vd_null,
71 #ifdef CONFIG_LIBAVCODEC
72 &mpcodecs_vd_ffmpeg,
73 #endif
74 #ifdef CONFIG_OGGTHEORA
75 &mpcodecs_vd_theora,
76 #endif
77 #ifdef CONFIG_WIN32DLL
78 &mpcodecs_vd_dshow,
79 &mpcodecs_vd_dmo,
80 &mpcodecs_vd_vfw,
81 &mpcodecs_vd_vfwex,
82 #endif
83 &mpcodecs_vd_lzo,
84 &mpcodecs_vd_raw,
85 &mpcodecs_vd_hmblck,
86 #ifdef CONFIG_XANIM
87 &mpcodecs_vd_xanim,
88 #endif
89 #ifdef CONFIG_PNG
90 &mpcodecs_vd_mpng,
91 #endif
92 #ifdef CONFIG_JPEG
93 &mpcodecs_vd_ijpg,
94 #endif
95 &mpcodecs_vd_mtga,
96 &mpcodecs_vd_sgi,
97 #ifdef CONFIG_LIBMPEG2
98 &mpcodecs_vd_libmpeg2,
99 #endif
100 &mpcodecs_vd_mpegpes,
101 #ifdef CONFIG_ZR
102 &mpcodecs_vd_zrmjpeg,
103 #endif
104 #ifdef CONFIG_REALCODECS
105 &mpcodecs_vd_realvid,
106 #endif
107 #ifdef CONFIG_XVID4
108 &mpcodecs_vd_xvid,
109 #endif
110 #ifdef CONFIG_LIBDV095
111 &mpcodecs_vd_libdv,
112 #endif
113 #ifdef CONFIG_QTX_CODECS
114 &mpcodecs_vd_qtvideo,
115 #endif
116 /* Please do not add any new decoders here. If you want to implement a new
117 * decoder, add it to libavcodec, except for wrappers around external
118 * libraries and decoders requiring binary support. */
119 NULL
122 #include "libvo/video_out.h"
124 // libvo opts:
125 int fullscreen = 0;
126 int vidmode = 0;
127 int softzoom = 0;
128 int flip = -1;
129 int opt_screen_size_x = 0;
130 int opt_screen_size_y = 0;
131 float screen_size_xy = 0;
132 float movie_aspect = -1.0;
133 int vo_flags = 0;
134 int vd_use_slices = 1;
136 /** global variables for gamma, brightness, contrast, saturation and hue
137 modified by mplayer.c and gui/mplayer/gtk/eq.c:
138 ranges -100 - 100
139 1000 if the vo default should be used
141 int vo_gamma_gamma = 1000;
142 int vo_gamma_brightness = 1000;
143 int vo_gamma_contrast = 1000;
144 int vo_gamma_saturation = 1000;
145 int vo_gamma_hue = 1000;
147 extern const vd_functions_t *mpvdec; // FIXME!
149 #define SCREEN_SIZE_X 1
150 #define SCREEN_SIZE_Y 1
152 int mpcodecs_config_vo(sh_video_t *sh, int w, int h,
153 unsigned int preferred_outfmt)
155 int i, j;
156 unsigned int out_fmt = 0;
157 int screen_size_x = 0; //SCREEN_SIZE_X;
158 int screen_size_y = 0; //SCREEN_SIZE_Y;
159 vf_instance_t *vf = sh->vfilter, *sc = NULL;
160 int palette = 0;
161 int vocfg_flags = 0;
163 if (w)
164 sh->disp_w = w;
165 if (h)
166 sh->disp_h = h;
168 if (!sh->disp_w || !sh->disp_h)
169 return 0;
171 mp_msg(MSGT_DECVIDEO, MSGL_V,
172 "VDec: vo config request - %d x %d (preferred colorspace: %s)\n", w,
173 h, vo_format_name(preferred_outfmt));
175 // if(!vf) return 1; // temp hack
177 if (get_video_quality_max(sh) <= 0 && divx_quality) {
178 // user wants postprocess but no pp filter yet:
179 sh->vfilter = vf = vf_open_filter(vf, "pp", NULL);
181 // check if libvo and codec has common outfmt (no conversion):
182 csp_again:
184 if (mp_msg_test(MSGT_DECVIDEO, MSGL_V)) {
185 vf_instance_t *f = vf;
186 mp_msg(MSGT_DECVIDEO, MSGL_V, "Trying filter chain:");
187 for (f = vf; f; f = f->next)
188 mp_msg(MSGT_DECVIDEO, MSGL_V, " %s", f->info->name);
189 mp_msg(MSGT_DECVIDEO, MSGL_V, "\n");
192 j = -1;
193 for (i = 0; i < CODECS_MAX_OUTFMT; i++) {
194 int flags;
195 out_fmt = sh->codec->outfmt[i];
196 if (out_fmt == (unsigned int) 0xFFFFFFFF)
197 continue;
198 flags = vf->query_format(vf, out_fmt);
199 mp_msg(MSGT_CPLAYER, MSGL_DBG2,
200 "vo_debug: query(%s) returned 0x%X (i=%d) \n",
201 vo_format_name(out_fmt), flags, i);
202 if ((flags & VFCAP_CSP_SUPPORTED_BY_HW)
203 || (flags & VFCAP_CSP_SUPPORTED && j < 0)) {
204 // check (query) if codec really support this outfmt...
205 sh->outfmtidx = j; // pass index to the control() function this way
206 if (mpvdec->control(sh, VDCTRL_QUERY_FORMAT, &out_fmt) ==
207 CONTROL_FALSE) {
208 mp_msg(MSGT_CPLAYER, MSGL_DBG2,
209 "vo_debug: codec query_format(%s) returned FALSE\n",
210 vo_format_name(out_fmt));
211 continue;
213 j = i;
214 vo_flags = flags;
215 if (flags & VFCAP_CSP_SUPPORTED_BY_HW)
216 break;
217 } else if (!palette
218 && !(flags &
219 (VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_CSP_SUPPORTED))
220 && (out_fmt == IMGFMT_RGB8 || out_fmt == IMGFMT_BGR8)) {
221 sh->outfmtidx = j; // pass index to the control() function this way
222 if (mpvdec->control(sh, VDCTRL_QUERY_FORMAT, &out_fmt) !=
223 CONTROL_FALSE)
224 palette = 1;
227 if (j < 0) {
228 // TODO: no match - we should use conversion...
229 if (strcmp(vf->info->name, "scale") && palette != -1) {
230 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_CouldNotFindColorspace);
231 sc = vf = vf_open_filter(vf, "scale", NULL);
232 goto csp_again;
233 } else if (palette == 1) {
234 mp_msg(MSGT_DECVIDEO, MSGL_V, "vd: Trying -vf palette...\n");
235 palette = -1;
236 vf = vf_open_filter(vf, "palette", NULL);
237 goto csp_again;
238 } else { // sws failed, if the last filter (vf_vo) support MPEGPES try to append vf_lavc
239 vf_instance_t *vo, *vp = NULL, *ve, *vpp = NULL;
240 // Remove the scale filter if we added it ourself
241 if (vf == sc) {
242 ve = vf;
243 vf = vf->next;
244 vf_uninit_filter(ve);
246 // Find the last filter (vf_vo)
247 for (vo = vf; vo->next; vo = vo->next) {
248 vpp = vp;
249 vp = vo;
251 if (vo->query_format(vo, IMGFMT_MPEGPES)
252 && (!vp || (vp && strcmp(vp->info->name, "lavc")))) {
253 ve = vf_open_filter(vo, "lavc", NULL);
254 if (vp)
255 vp->next = ve;
256 else
257 vf = ve;
258 goto csp_again;
260 if (vp && !strcmp(vp->info->name, "lavc")) {
261 if (vpp)
262 vpp->next = vo;
263 else
264 vf = vo;
265 vf_uninit_filter(vp);
268 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_VOincompCodec);
269 sh->vf_initialized = -1;
270 return 0; // failed
272 out_fmt = sh->codec->outfmt[j];
273 mp_msg(MSGT_CPLAYER, MSGL_V, "VDec: using %s as output csp (no %d)\n",
274 vo_format_name(out_fmt), j);
275 sh->outfmtidx = j;
276 sh->vfilter = vf;
278 // autodetect flipping
279 if (flip == -1) {
280 flip = 0;
281 if (sh->codec->outflags[j] & CODECS_FLAG_FLIP)
282 if (!(sh->codec->outflags[j] & CODECS_FLAG_NOFLIP))
283 flip = 1;
285 if (vo_flags & VFCAP_FLIPPED)
286 flip ^= 1;
287 if (flip && !(vo_flags & VFCAP_FLIP)) {
288 // we need to flip, but no flipping filter avail.
289 vf_add_before_vo(&vf, "flip", NULL);
290 sh->vfilter = vf;
292 // time to do aspect ratio corrections...
294 if (movie_aspect > -1.0)
295 sh->aspect = movie_aspect; // cmdline overrides autodetect
296 else if (sh->stream_aspect != 0.0)
297 sh->aspect = sh->stream_aspect;
298 // if(!sh->aspect) sh->aspect=1.0;
300 if (opt_screen_size_x || opt_screen_size_y) {
301 screen_size_x = opt_screen_size_x;
302 screen_size_y = opt_screen_size_y;
303 if (!vidmode) {
304 if (!screen_size_x)
305 screen_size_x = SCREEN_SIZE_X;
306 if (!screen_size_y)
307 screen_size_y = SCREEN_SIZE_Y;
308 if (screen_size_x <= 8)
309 screen_size_x *= sh->disp_w;
310 if (screen_size_y <= 8)
311 screen_size_y *= sh->disp_h;
313 } else {
314 // check source format aspect, calculate prescale ::atmos
315 screen_size_x = sh->disp_w;
316 screen_size_y = sh->disp_h;
317 if (screen_size_xy >= 0.001) {
318 if (screen_size_xy <= 8) {
319 // -xy means x+y scale
320 screen_size_x *= screen_size_xy;
321 screen_size_y *= screen_size_xy;
322 } else {
323 // -xy means forced width while keeping correct aspect
324 screen_size_x = screen_size_xy;
325 screen_size_y = screen_size_xy * sh->disp_h / sh->disp_w;
328 if (sh->aspect > 0.01) {
329 int w;
330 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_MovieAspectIsSet,
331 sh->aspect);
332 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ASPECT=%1.4f\n",
333 sh->aspect);
334 w = (int) ((float) screen_size_y * sh->aspect);
335 w += w % 2; // round
336 // we don't like horizontal downscale || user forced width:
337 if (w < screen_size_x || screen_size_xy > 8) {
338 screen_size_y =
339 (int) ((float) screen_size_x * (1.0 / sh->aspect));
340 screen_size_y += screen_size_y % 2; // round
341 } else
342 screen_size_x = w; // keep new width
343 } else {
344 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_MovieAspectUndefined);
348 vocfg_flags = (fullscreen ? VOFLAG_FULLSCREEN : 0)
349 | (vidmode ? VOFLAG_MODESWITCHING : 0)
350 | (softzoom ? VOFLAG_SWSCALE : 0)
351 | (flip ? VOFLAG_FLIPPING : 0);
353 // Time to config libvo!
354 mp_msg(MSGT_CPLAYER, MSGL_V,
355 "VO Config (%dx%d->%dx%d,flags=%d,'%s',0x%X)\n", sh->disp_w,
356 sh->disp_h, screen_size_x, screen_size_y, vocfg_flags, "MPlayer",
357 out_fmt);
359 vf->w = sh->disp_w;
360 vf->h = sh->disp_h;
361 if (vf_config_wrapper
362 (vf, sh->disp_w, sh->disp_h, screen_size_x, screen_size_y, vocfg_flags,
363 out_fmt) == 0) {
364 // "MPlayer",out_fmt)){
365 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_CannotInitVO);
366 sh->vf_initialized = -1;
367 return 0;
370 sh->vf_initialized = 1;
372 if (vo_gamma_gamma != 1000)
373 set_video_colors(sh, "gamma", vo_gamma_gamma);
374 if (vo_gamma_brightness != 1000)
375 set_video_colors(sh, "brightness", vo_gamma_brightness);
376 if (vo_gamma_contrast != 1000)
377 set_video_colors(sh, "contrast", vo_gamma_contrast);
378 if (vo_gamma_saturation != 1000)
379 set_video_colors(sh, "saturation", vo_gamma_saturation);
380 if (vo_gamma_hue != 1000)
381 set_video_colors(sh, "hue", vo_gamma_hue);
383 return 1;
386 // mp_imgtype: buffering type, see mp_image.h
387 // mp_imgflag: buffer requirements (read/write, preserve, stride limits), see mp_image.h
388 // returns NULL or allocated mp_image_t*
389 // Note: buffer allocation may be moved to mpcodecs_config_vo() later...
390 mp_image_t *mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag,
391 int w, int h)
393 mp_image_t *mpi =
394 vf_get_image(sh->vfilter, sh->codec->outfmt[sh->outfmtidx], mp_imgtype,
395 mp_imgflag, w, h);
396 if (mpi)
397 mpi->x = mpi->y = 0;
398 return mpi;
401 void mpcodecs_draw_slice(sh_video_t *sh, unsigned char **src, int *stride,
402 int w, int h, int x, int y)
404 struct vf_instance *vf = sh->vfilter;
406 if (vf->draw_slice)
407 vf->draw_slice(vf, src, stride, w, h, x, y);