Add support for VDPAU video out, including hardware decoding.
[mplayer/glamo.git] / libmpcodecs / vd.c
blob67f6b3e7e030edbcc34b62bd66f8e71c47f6a06a
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"
9 #ifdef HAVE_MALLOC_H
10 #include <malloc.h>
11 #endif
13 #include "codec-cfg.h"
14 //#include "mp_image.h"
16 #include "img_format.h"
18 #include "stream/stream.h"
19 #include "libmpdemux/demuxer.h"
20 #include "libmpdemux/stheader.h"
21 #include "dec_video.h"
23 #include "vd.h"
24 #include "vf.h"
26 //#include "vd_internal.h"
28 extern vd_functions_t mpcodecs_vd_null;
29 extern vd_functions_t mpcodecs_vd_ffmpeg;
30 extern vd_functions_t mpcodecs_vd_theora;
31 extern vd_functions_t mpcodecs_vd_dshow;
32 extern vd_functions_t mpcodecs_vd_dmo;
33 extern vd_functions_t mpcodecs_vd_vfw;
34 extern vd_functions_t mpcodecs_vd_vfwex;
35 extern vd_functions_t mpcodecs_vd_raw;
36 extern vd_functions_t mpcodecs_vd_hmblck;
37 extern vd_functions_t mpcodecs_vd_xanim;
38 extern vd_functions_t mpcodecs_vd_nuv;
39 extern vd_functions_t mpcodecs_vd_mpng;
40 extern vd_functions_t mpcodecs_vd_ijpg;
41 extern vd_functions_t mpcodecs_vd_mtga;
42 extern vd_functions_t mpcodecs_vd_sgi;
43 extern vd_functions_t mpcodecs_vd_libmpeg2;
44 extern vd_functions_t mpcodecs_vd_mpegpes;
45 extern vd_functions_t mpcodecs_vd_zrmjpeg;
46 extern vd_functions_t mpcodecs_vd_realvid;
47 extern vd_functions_t mpcodecs_vd_xvid;
48 extern vd_functions_t mpcodecs_vd_libdv;
49 extern vd_functions_t mpcodecs_vd_lzo;
50 extern vd_functions_t mpcodecs_vd_qtvideo;
52 /* Please do not add any new decoders here. If you want to implement a new
53 * decoder, add it to libavcodec, except for wrappers around external
54 * libraries and decoders requiring binary support. */
56 vd_functions_t* mpcodecs_vd_drivers[] = {
57 &mpcodecs_vd_null,
58 #ifdef CONFIG_LIBAVCODEC
59 &mpcodecs_vd_ffmpeg,
60 #endif
61 #ifdef CONFIG_OGGTHEORA
62 &mpcodecs_vd_theora,
63 #endif
64 #ifdef CONFIG_WIN32DLL
65 &mpcodecs_vd_dshow,
66 &mpcodecs_vd_dmo,
67 &mpcodecs_vd_vfw,
68 &mpcodecs_vd_vfwex,
69 #endif
70 &mpcodecs_vd_lzo,
71 &mpcodecs_vd_raw,
72 &mpcodecs_vd_hmblck,
73 &mpcodecs_vd_nuv,
74 #ifdef CONFIG_XANIM
75 &mpcodecs_vd_xanim,
76 #endif
77 #ifdef CONFIG_PNG
78 &mpcodecs_vd_mpng,
79 #endif
80 #ifdef CONFIG_JPEG
81 &mpcodecs_vd_ijpg,
82 #endif
83 &mpcodecs_vd_mtga,
84 &mpcodecs_vd_sgi,
85 #ifdef CONFIG_LIBMPEG2
86 &mpcodecs_vd_libmpeg2,
87 #endif
88 &mpcodecs_vd_mpegpes,
89 #ifdef CONFIG_ZR
90 &mpcodecs_vd_zrmjpeg,
91 #endif
92 #ifdef CONFIG_REALCODECS
93 &mpcodecs_vd_realvid,
94 #endif
95 #ifdef CONFIG_XVID4
96 &mpcodecs_vd_xvid,
97 #endif
98 #ifdef CONFIG_LIBDV095
99 &mpcodecs_vd_libdv,
100 #endif
101 #ifdef CONFIG_QTX_CODECS
102 &mpcodecs_vd_qtvideo,
103 #endif
104 /* Please do not add any new decoders here. If you want to implement a new
105 * decoder, add it to libavcodec, except for wrappers around external
106 * libraries and decoders requiring binary support. */
107 NULL
110 #include "libvo/video_out.h"
112 // libvo opts:
113 int fullscreen=0;
114 int vidmode=0;
115 int softzoom=0;
116 int flip=-1;
117 int opt_screen_size_x=0;
118 int opt_screen_size_y=0;
119 float screen_size_xy=0;
120 float movie_aspect=-1.0;
121 int vo_flags=0;
122 int vd_use_slices=1;
124 /** global variables for gamma, brightness, contrast, saturation and hue
125 modified by mplayer.c and gui/mplayer/gtk/eq.c:
126 ranges -100 - 100
127 1000 if the vo default should be used
129 int vo_gamma_gamma = 1000;
130 int vo_gamma_brightness = 1000;
131 int vo_gamma_contrast = 1000;
132 int vo_gamma_saturation = 1000;
133 int vo_gamma_hue = 1000;
135 extern vd_functions_t* mpvdec; // FIXME!
137 #define SCREEN_SIZE_X 1
138 #define SCREEN_SIZE_Y 1
140 int mpcodecs_config_vo(sh_video_t *sh, int w, int h, unsigned int preferred_outfmt){
141 int i,j;
142 unsigned int out_fmt=0;
143 int screen_size_x=0;//SCREEN_SIZE_X;
144 int screen_size_y=0;//SCREEN_SIZE_Y;
145 vf_instance_t* vf=sh->vfilter,*sc=NULL;
146 int palette=0;
147 int vocfg_flags=0;
149 if(w)
150 sh->disp_w=w;
151 if(h)
152 sh->disp_h=h;
154 if(!sh->disp_w || !sh->disp_h)
155 return 0;
157 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_VoConfigRequest,w,h,vo_format_name(preferred_outfmt));
159 // if(!vf) return 1; // temp hack
161 if(get_video_quality_max(sh)<=0 && divx_quality){
162 // user wants postprocess but no pp filter yet:
163 sh->vfilter=vf=vf_open_filter(vf,"pp",NULL);
166 // check if libvo and codec has common outfmt (no conversion):
167 csp_again:
169 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ){
170 vf_instance_t* f=vf;
171 mp_msg(MSGT_DECVIDEO,MSGL_V,"Trying filter chain:");
172 for(f = vf ; f ; f = f->next)
173 mp_msg(MSGT_DECVIDEO,MSGL_V," %s",f->info->name);
174 mp_msg(MSGT_DECVIDEO,MSGL_V,"\n");
177 j=-1;
178 for(i=0;i<CODECS_MAX_OUTFMT;i++){
179 int flags;
180 out_fmt=sh->codec->outfmt[i];
181 if(out_fmt==(unsigned int)0xFFFFFFFF) continue;
182 flags=vf->query_format(vf,out_fmt);
183 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"vo_debug: query(%s) returned 0x%X (i=%d) \n",vo_format_name(out_fmt),flags,i);
184 if((flags&VFCAP_CSP_SUPPORTED_BY_HW) || (flags&VFCAP_CSP_SUPPORTED && j<0)){
185 // check (query) if codec really support this outfmt...
186 sh->outfmtidx=j; // pass index to the control() function this way
187 if(mpvdec->control(sh,VDCTRL_QUERY_FORMAT,&out_fmt)==CONTROL_FALSE){
188 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"vo_debug: codec query_format(%s) returned FALSE\n",vo_format_name(out_fmt));
189 continue;
191 j=i; vo_flags=flags; if(flags&VFCAP_CSP_SUPPORTED_BY_HW) break;
192 } else
193 if(!palette && !(flags&(VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_CSP_SUPPORTED)) && (out_fmt==IMGFMT_RGB8||out_fmt==IMGFMT_BGR8)){
194 sh->outfmtidx=j; // pass index to the control() function this way
195 if(mpvdec->control(sh,VDCTRL_QUERY_FORMAT,&out_fmt)!=CONTROL_FALSE)
196 palette=1;
199 if(j<0){
200 // TODO: no match - we should use conversion...
201 if(strcmp(vf->info->name,"scale") && palette!=-1){
202 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_CouldNotFindColorspace);
203 sc=vf=vf_open_filter(vf,"scale",NULL);
204 goto csp_again;
205 } else
206 if(palette==1){
207 mp_msg(MSGT_DECVIDEO,MSGL_V,"vd: Trying -vf palette...\n");
208 palette=-1;
209 vf=vf_open_filter(vf,"palette",NULL);
210 goto csp_again;
211 } else
212 { // sws failed, if the last filter (vf_vo) support MPEGPES try to append vf_lavc
213 vf_instance_t* vo, *vp = NULL, *ve;
214 // Remove the scale filter if we added it ourself
215 if(vf == sc) {
216 ve = vf;
217 vf = vf->next;
218 vf_uninit_filter(ve);
220 // Find the last filter (vf_vo)
221 for(vo = vf ; vo->next ; vo = vo->next)
222 vp = vo;
223 if(vo->query_format(vo,IMGFMT_MPEGPES) && (!vp || (vp && strcmp(vp->info->name,"lavc")))) {
224 ve = vf_open_filter(vo,"lavc",NULL);
225 if(vp) vp->next = ve;
226 else vf = ve;
227 goto csp_again;
230 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_VOincompCodec);
231 sh->vf_initialized=-1;
232 return 0; // failed
234 out_fmt=sh->codec->outfmt[j];
235 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_UsingXAsOutputCspNoY,vo_format_name(out_fmt),j);
236 sh->outfmtidx=j;
237 sh->vfilter=vf;
239 // autodetect flipping
240 if(flip==-1){
241 flip=0;
242 if(sh->codec->outflags[j]&CODECS_FLAG_FLIP)
243 if(!(sh->codec->outflags[j]&CODECS_FLAG_NOFLIP))
244 flip=1;
246 if(vo_flags&VFCAP_FLIPPED) flip^=1;
247 if(flip && !(vo_flags&VFCAP_FLIP)){
248 // we need to flip, but no flipping filter avail.
249 vf_add_before_vo(&vf, "flip", NULL);
250 sh->vfilter = vf;
253 // time to do aspect ratio corrections...
255 if(movie_aspect>-1.0) sh->aspect = movie_aspect; // cmdline overrides autodetect
256 else if(sh->stream_aspect!=0.0) sh->aspect = sh->stream_aspect;
257 // if(!sh->aspect) sh->aspect=1.0;
259 if(opt_screen_size_x||opt_screen_size_y){
260 screen_size_x = opt_screen_size_x;
261 screen_size_y = opt_screen_size_y;
262 if(!vidmode){
263 if(!screen_size_x) screen_size_x=SCREEN_SIZE_X;
264 if(!screen_size_y) screen_size_y=SCREEN_SIZE_Y;
265 if(screen_size_x<=8) screen_size_x*=sh->disp_w;
266 if(screen_size_y<=8) screen_size_y*=sh->disp_h;
268 } else {
269 // check source format aspect, calculate prescale ::atmos
270 screen_size_x=sh->disp_w;
271 screen_size_y=sh->disp_h;
272 if(screen_size_xy>=0.001){
273 if(screen_size_xy<=8){
274 // -xy means x+y scale
275 screen_size_x*=screen_size_xy;
276 screen_size_y*=screen_size_xy;
277 } else {
278 // -xy means forced width while keeping correct aspect
279 screen_size_x=screen_size_xy;
280 screen_size_y=screen_size_xy*sh->disp_h/sh->disp_w;
283 if(sh->aspect>0.01){
284 int w;
285 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MovieAspectIsSet,sh->aspect);
286 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ASPECT=%1.4f\n", sh->aspect);
287 w=(int)((float)screen_size_y*sh->aspect); w+=w%2; // round
288 // we don't like horizontal downscale || user forced width:
289 if(w<screen_size_x || screen_size_xy>8){
290 screen_size_y=(int)((float)screen_size_x*(1.0/sh->aspect));
291 screen_size_y+=screen_size_y%2; // round
292 } else screen_size_x=w; // keep new width
293 } else {
294 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MovieAspectUndefined);
298 vocfg_flags = (fullscreen ? VOFLAG_FULLSCREEN:0)
299 | (vidmode ? VOFLAG_MODESWITCHING:0)
300 | (softzoom ? VOFLAG_SWSCALE:0)
301 | (flip ? VOFLAG_FLIPPING:0);
303 // Time to config libvo!
304 mp_msg(MSGT_CPLAYER,MSGL_V,"VO Config (%dx%d->%dx%d,flags=%d,'%s',0x%X)\n",
305 sh->disp_w,sh->disp_h,
306 screen_size_x,screen_size_y,
307 vocfg_flags,
308 "MPlayer",out_fmt);
310 vf->w = sh->disp_w;
311 vf->h = sh->disp_h;
312 if(vf_config_wrapper(vf,sh->disp_w,sh->disp_h,
313 screen_size_x,screen_size_y,
314 vocfg_flags,
315 out_fmt)==0){
316 // "MPlayer",out_fmt)){
317 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_CannotInitVO);
318 sh->vf_initialized=-1;
319 return 0;
322 sh->vf_initialized=1;
324 if (vo_gamma_gamma != 1000)
325 set_video_colors(sh, "gamma", vo_gamma_gamma);
326 if (vo_gamma_brightness != 1000)
327 set_video_colors(sh, "brightness", vo_gamma_brightness);
328 if (vo_gamma_contrast != 1000)
329 set_video_colors(sh, "contrast", vo_gamma_contrast);
330 if (vo_gamma_saturation != 1000)
331 set_video_colors(sh, "saturation", vo_gamma_saturation);
332 if (vo_gamma_hue != 1000)
333 set_video_colors(sh, "hue", vo_gamma_hue);
335 return 1;
338 // mp_imgtype: buffering type, see mp_image.h
339 // mp_imgflag: buffer requirements (read/write, preserve, stride limits), see mp_image.h
340 // returns NULL or allocated mp_image_t*
341 // Note: buffer allocation may be moved to mpcodecs_config_vo() later...
342 mp_image_t* mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag, int w, int h){
343 mp_image_t* mpi=vf_get_image(sh->vfilter,sh->codec->outfmt[sh->outfmtidx],mp_imgtype,mp_imgflag,w,h);
344 mpi->x=mpi->y=0;
345 return mpi;
348 void mpcodecs_draw_slice(sh_video_t *sh, unsigned char** src, int* stride, int w,int h, int x, int y) {
349 struct vf_instance_s* vf = sh->vfilter;
351 if(vf->draw_slice)
352 vf->draw_slice(vf,src,stride,w,h,x,y);