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