Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / dec_video.c
blobb29c01aba4072926562ea847128657446e7e623f
2 #include "config.h"
4 #include <stdio.h>
5 #ifdef HAVE_MALLOC_H
6 #include <malloc.h>
7 #endif
8 #include <stdlib.h>
9 #include <unistd.h>
11 #include "mp_msg.h"
12 #include "help_mp.h"
14 #include "osdep/timer.h"
15 #include "osdep/shmem.h"
17 #include "stream/stream.h"
18 #include "libmpdemux/demuxer.h"
19 #include "libmpdemux/parse_es.h"
21 #include "codec-cfg.h"
23 #include "libvo/video_out.h"
25 #include "libmpdemux/stheader.h"
26 #include "vd.h"
27 #include "vf.h"
29 #include "dec_video.h"
31 #ifdef DYNAMIC_PLUGINS
32 #include <dlfcn.h>
33 #endif
35 // ===================================================================
37 extern double video_time_usage;
38 extern double vout_time_usage;
40 #include "cpudetect.h"
42 int field_dominance=-1;
44 int divx_quality=0;
46 vd_functions_t* mpvdec=NULL;
48 int get_video_quality_max(sh_video_t *sh_video){
49 vf_instance_t* vf=sh_video->vfilter;
50 if(vf){
51 int ret=vf->control(vf,VFCTRL_QUERY_MAX_PP_LEVEL,NULL);
52 if(ret>0){
53 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_UsingExternalPP,ret);
54 return ret;
57 if(mpvdec){
58 int ret=mpvdec->control(sh_video,VDCTRL_QUERY_MAX_PP_LEVEL,NULL);
59 if(ret>0){
60 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_UsingCodecPP,ret);
61 return ret;
64 // mp_msg(MSGT_DECVIDEO,MSGL_INFO,"[PP] Sorry, postprocessing is not available\n");
65 return 0;
68 void set_video_quality(sh_video_t *sh_video,int quality){
69 vf_instance_t* vf=sh_video->vfilter;
70 if(vf){
71 int ret=vf->control(vf,VFCTRL_SET_PP_LEVEL, (void*)(&quality));
72 if(ret==CONTROL_TRUE) return; // success
74 if(mpvdec)
75 mpvdec->control(sh_video,VDCTRL_SET_PP_LEVEL, (void*)(&quality));
78 int set_video_colors(sh_video_t *sh_video,const char *item,int value)
80 vf_instance_t* vf=sh_video->vfilter;
81 vf_equalizer_t data;
83 data.item = item;
84 data.value = value;
86 mp_dbg(MSGT_DECVIDEO,MSGL_V,"set video colors %s=%d \n", item, value);
87 if (vf)
89 int ret = vf->control(vf, VFCTRL_SET_EQUALIZER, &data);
90 if (ret == CONTROL_TRUE)
91 return(1);
93 /* try software control */
94 if(mpvdec)
95 if( mpvdec->control(sh_video,VDCTRL_SET_EQUALIZER, item, (int *)value)
96 == CONTROL_OK) return 1;
97 mp_msg(MSGT_DECVIDEO,MSGL_V,MSGTR_VideoAttributeNotSupportedByVO_VD,item);
98 return 0;
101 int get_video_colors(sh_video_t *sh_video,const char *item,int *value)
103 vf_instance_t* vf=sh_video->vfilter;
104 vf_equalizer_t data;
106 data.item = item;
108 mp_dbg(MSGT_DECVIDEO,MSGL_V,"get video colors %s \n", item);
109 if (vf)
111 int ret = vf->control(vf, VFCTRL_GET_EQUALIZER, &data);
112 if (ret == CONTROL_TRUE){
113 *value = data.value;
114 return(1);
117 /* try software control */
118 if(mpvdec) return mpvdec->control(sh_video,VDCTRL_GET_EQUALIZER, item, value);
119 return 0;
122 int set_rectangle(sh_video_t *sh_video,int param,int value)
124 vf_instance_t* vf=sh_video->vfilter;
125 int data[] = {param, value};
127 mp_dbg(MSGT_DECVIDEO,MSGL_V,"set rectangle \n");
128 if (vf)
130 int ret = vf->control(vf, VFCTRL_CHANGE_RECTANGLE, data);
131 if (ret)
132 return(1);
134 return 0;
137 void resync_video_stream(sh_video_t *sh_video)
139 if(mpvdec) mpvdec->control(sh_video, VDCTRL_RESYNC_STREAM, NULL);
142 int get_current_video_decoder_lag(sh_video_t *sh_video)
144 int ret;
146 if (!mpvdec)
147 return -1;
148 ret = mpvdec->control(sh_video, VDCTRL_QUERY_UNSEEN_FRAMES, NULL);
149 if (ret >= 10)
150 return ret-10;
151 return -1;
154 void uninit_video(sh_video_t *sh_video){
155 if(!sh_video->inited) return;
156 mp_msg(MSGT_DECVIDEO,MSGL_V,MSGTR_UninitVideoStr,sh_video->codec->drv);
157 mpvdec->uninit(sh_video);
158 #ifdef DYNAMIC_PLUGINS
159 if (sh_video->dec_handle)
160 dlclose(sh_video->dec_handle);
161 #endif
162 vf_uninit_filter_chain(sh_video->vfilter);
163 sh_video->inited=0;
166 void vfm_help(void){
167 int i;
168 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_AvailableVideoFm);
169 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_DRIVERS\n");
170 mp_msg(MSGT_DECVIDEO,MSGL_INFO," vfm: info: (comment)\n");
171 for (i=0; mpcodecs_vd_drivers[i] != NULL; i++)
172 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"%8s %s (%s)\n",
173 mpcodecs_vd_drivers[i]->info->short_name,
174 mpcodecs_vd_drivers[i]->info->name,
175 mpcodecs_vd_drivers[i]->info->comment);
178 static int init_video(sh_video_t *sh_video,char* codecname,char* vfm,int status){
179 int force = 0;
180 unsigned int orig_fourcc=sh_video->bih?sh_video->bih->biCompression:0;
181 sh_video->codec=NULL;
182 sh_video->vf_inited=0;
183 if (codecname && codecname[0] == '+') {
184 codecname = &codecname[1];
185 force = 1;
188 while(1){
189 int i;
190 int orig_w, orig_h;
191 // restore original fourcc:
192 if(sh_video->bih) sh_video->bih->biCompression=orig_fourcc;
193 if(!(sh_video->codec=find_video_codec(sh_video->format,
194 sh_video->bih?((unsigned int*) &sh_video->bih->biCompression):NULL,
195 sh_video->codec,force) )) break;
196 // ok we found one codec
197 if(sh_video->codec->flags&CODECS_FLAG_SELECTED) continue; // already tried & failed
198 if(codecname && strcmp(sh_video->codec->name,codecname)) continue; // -vc
199 if(vfm && strcmp(sh_video->codec->drv,vfm)) continue; // vfm doesn't match
200 if(!force && sh_video->codec->status<status) continue; // too unstable
201 sh_video->codec->flags|=CODECS_FLAG_SELECTED; // tagging it
202 // ok, it matches all rules, let's find the driver!
203 for (i=0; mpcodecs_vd_drivers[i] != NULL; i++)
204 // if(mpcodecs_vd_drivers[i]->info->id==sh_video->codec->driver) break;
205 if(!strcmp(mpcodecs_vd_drivers[i]->info->short_name,sh_video->codec->drv)) break;
206 mpvdec=mpcodecs_vd_drivers[i];
207 #ifdef DYNAMIC_PLUGINS
208 if (!mpvdec)
210 /* try to open shared decoder plugin */
211 int buf_len;
212 char *buf;
213 vd_functions_t *funcs_sym;
214 vd_info_t *info_sym;
216 buf_len = strlen(MPLAYER_LIBDIR)+strlen(sh_video->codec->drv)+16;
217 buf = malloc(buf_len);
218 if (!buf)
219 break;
220 snprintf(buf, buf_len, "%s/mplayer/vd_%s.so", MPLAYER_LIBDIR, sh_video->codec->drv);
221 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "Trying to open external plugin: %s\n", buf);
222 sh_video->dec_handle = dlopen(buf, RTLD_LAZY);
223 if (!sh_video->dec_handle)
224 break;
225 snprintf(buf, buf_len, "mpcodecs_vd_%s", sh_video->codec->drv);
226 funcs_sym = dlsym(sh_video->dec_handle, buf);
227 if (!funcs_sym || !funcs_sym->info || !funcs_sym->init ||
228 !funcs_sym->uninit || !funcs_sym->control || !funcs_sym->decode)
229 break;
230 info_sym = funcs_sym->info;
231 if (strcmp(info_sym->short_name, sh_video->codec->drv))
232 break;
233 free(buf);
234 mpvdec = funcs_sym;
235 mp_msg(MSGT_DECVIDEO, MSGL_V, "Using external decoder plugin (%s/mplayer/vd_%s.so)!\n",
236 MPLAYER_LIBDIR, sh_video->codec->drv);
238 #endif
239 if(!mpvdec){ // driver not available (==compiled in)
240 mp_msg(MSGT_DECVIDEO,MSGL_WARN,MSGTR_VideoCodecFamilyNotAvailableStr,
241 sh_video->codec->name, sh_video->codec->drv);
242 continue;
244 orig_w = sh_video->bih ? sh_video->bih->biWidth : sh_video->disp_w;
245 orig_h = sh_video->bih ? sh_video->bih->biHeight : sh_video->disp_h;
246 sh_video->disp_w = orig_w;
247 sh_video->disp_h = orig_h;
248 // it's available, let's try to init!
249 if(sh_video->codec->flags & CODECS_FLAG_ALIGN16){
250 // align width/height to n*16
251 sh_video->disp_w=(sh_video->disp_w+15)&(~15);
252 sh_video->disp_h=(sh_video->disp_h+15)&(~15);
254 if (sh_video->bih) {
255 sh_video->bih->biWidth = sh_video->disp_w;
256 sh_video->bih->biHeight = sh_video->disp_h;
258 // init()
259 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_OpeningVideoDecoder,mpvdec->info->short_name,mpvdec->info->name);
260 // clear vf init error, it is no longer relevant
261 if (sh_video->vf_inited < 0)
262 sh_video->vf_inited = 0;
263 if(!mpvdec->init(sh_video)){
264 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_VDecoderInitFailed);
265 sh_video->disp_w=orig_w;
266 sh_video->disp_h=orig_h;
267 if (sh_video->bih) {
268 sh_video->bih->biWidth = sh_video->disp_w;
269 sh_video->bih->biHeight = sh_video->disp_h;
271 continue; // try next...
273 // Yeah! We got it!
274 sh_video->inited=1;
275 return 1;
277 return 0;
280 int init_best_video_codec(sh_video_t *sh_video,char** video_codec_list,char** video_fm_list){
281 char* vc_l_default[2]={"",(char*)NULL};
282 // hack:
283 if(!video_codec_list) video_codec_list=vc_l_default;
284 // Go through the codec.conf and find the best codec...
285 sh_video->inited=0;
286 codecs_reset_selection(0);
287 while(!sh_video->inited && *video_codec_list){
288 char* video_codec=*(video_codec_list++);
289 if(video_codec[0]){
290 if(video_codec[0]=='-'){
291 // disable this codec:
292 select_codec(video_codec+1,0);
293 } else {
294 // forced codec by name:
295 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_ForcedVideoCodec,video_codec);
296 init_video(sh_video,video_codec,NULL,-1);
298 } else {
299 int status;
300 // try in stability order: UNTESTED, WORKING, BUGGY. never try CRASHING.
301 if(video_fm_list){
302 char** fmlist=video_fm_list;
303 // try first the preferred codec families:
304 while(!sh_video->inited && *fmlist){
305 char* video_fm=*(fmlist++);
306 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_TryForceVideoFmtStr,video_fm);
307 for(status=CODECS_STATUS__MAX;status>=CODECS_STATUS__MIN;--status)
308 if(init_video(sh_video,NULL,video_fm,status)) break;
311 if(!sh_video->inited)
312 for(status=CODECS_STATUS__MAX;status>=CODECS_STATUS__MIN;--status)
313 if(init_video(sh_video,NULL,NULL,status)) break;
317 if(!sh_video->inited){
318 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CantFindVideoCodec,sh_video->format);
319 mp_msg(MSGT_DECAUDIO,MSGL_HINT, MSGTR_RTFMCodecs);
320 return 0; // failed
323 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_SelectedVideoCodec,
324 sh_video->codec->name,sh_video->codec->drv,sh_video->codec->info);
325 return 1; // success
328 void *decode_video(sh_video_t *sh_video, unsigned char *start, int in_size,
329 int drop_frame, double pts)
331 mp_image_t *mpi = NULL;
332 unsigned int t = GetTimer();
333 unsigned int t2;
334 double tt;
336 if (correct_pts && pts != MP_NOPTS_VALUE) {
337 int delay = get_current_video_decoder_lag(sh_video);
338 if (delay >= 0) {
339 if (delay > sh_video->num_buffered_pts)
340 #if 0
341 // this is disabled because vd_ffmpeg reports the same lag
342 // after seek even when there are no buffered frames,
343 // leading to incorrect error messages
344 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Not enough buffered pts\n");
345 #else
347 #endif
348 else
349 sh_video->num_buffered_pts = delay;
351 if (sh_video->num_buffered_pts ==
352 sizeof(sh_video->buffered_pts)/sizeof(double))
353 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Too many buffered pts\n");
354 else {
355 int i, j;
356 for (i = 0; i < sh_video->num_buffered_pts; i++)
357 if (sh_video->buffered_pts[i] < pts)
358 break;
359 for (j = sh_video->num_buffered_pts; j > i; j--)
360 sh_video->buffered_pts[j] = sh_video->buffered_pts[j-1];
361 sh_video->buffered_pts[i] = pts;
362 sh_video->num_buffered_pts++;
366 mpi = mpvdec->decode(sh_video, start, in_size, drop_frame);
368 //------------------------ frame decoded. --------------------
370 #ifdef HAVE_MMX
371 // some codecs are broken, and doesn't restore MMX state :(
372 // it happens usually with broken/damaged files.
373 if (gCpuCaps.has3DNow) {
374 __asm __volatile ("femms\n\t":::"memory");
376 else if (gCpuCaps.hasMMX) {
377 __asm __volatile ("emms\n\t":::"memory");
379 #endif
381 t2 = GetTimer(); t = t2-t;
382 tt = t*0.000001f;
383 video_time_usage += tt;
385 if (!mpi || drop_frame)
386 return NULL; // error / skipped frame
388 if (field_dominance == 0)
389 mpi->fields |= MP_IMGFIELD_TOP_FIRST;
390 else if (field_dominance == 1)
391 mpi->fields &= ~MP_IMGFIELD_TOP_FIRST;
393 if (correct_pts) {
394 if (sh_video->num_buffered_pts) {
395 sh_video->num_buffered_pts--;
396 sh_video->pts = sh_video->buffered_pts[sh_video->num_buffered_pts];
398 else {
399 mp_msg(MSGT_CPLAYER, MSGL_ERR, "No pts value from demuxer to "
400 "use for frame!\n");
401 sh_video->pts = MP_NOPTS_VALUE;
404 return mpi;
407 int filter_video(sh_video_t *sh_video, void *frame, double pts)
409 mp_image_t *mpi = frame;
410 unsigned int t2 = GetTimer();
411 vf_instance_t *vf = sh_video->vfilter;
412 // apply video filters and call the leaf vo/ve
413 int ret = vf->put_image(vf, mpi, pts);
414 if (ret > 0) {
415 vf->control(vf, VFCTRL_DRAW_OSD, NULL);
416 #ifdef USE_ASS
417 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
418 #endif
421 t2 = GetTimer()-t2;
422 vout_time_usage += t2*0.000001;
424 return ret;