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"
29 #include "dec_video.h"
31 #ifdef DYNAMIC_PLUGINS
35 // ===================================================================
37 extern double video_time_usage
;
38 extern double vout_time_usage
;
40 #include "cpudetect.h"
42 int field_dominance
=-1;
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
;
51 int ret
=vf
->control(vf
,VFCTRL_QUERY_MAX_PP_LEVEL
,NULL
);
53 mp_msg(MSGT_DECVIDEO
,MSGL_INFO
,MSGTR_UsingExternalPP
,ret
);
58 int ret
=mpvdec
->control(sh_video
,VDCTRL_QUERY_MAX_PP_LEVEL
,NULL
);
60 mp_msg(MSGT_DECVIDEO
,MSGL_INFO
,MSGTR_UsingCodecPP
,ret
);
64 // mp_msg(MSGT_DECVIDEO,MSGL_INFO,"[PP] Sorry, postprocessing is not available\n");
68 void set_video_quality(sh_video_t
*sh_video
,int quality
){
69 vf_instance_t
* vf
=sh_video
->vfilter
;
71 int ret
=vf
->control(vf
,VFCTRL_SET_PP_LEVEL
, (void*)(&quality
));
72 if(ret
==CONTROL_TRUE
) return; // success
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
;
86 mp_dbg(MSGT_DECVIDEO
,MSGL_V
,"set video colors %s=%d \n", item
, value
);
89 int ret
= vf
->control(vf
, VFCTRL_SET_EQUALIZER
, &data
);
90 if (ret
== CONTROL_TRUE
)
93 /* try software control */
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
);
101 int get_video_colors(sh_video_t
*sh_video
,const char *item
,int *value
)
103 vf_instance_t
* vf
=sh_video
->vfilter
;
108 mp_dbg(MSGT_DECVIDEO
,MSGL_V
,"get video colors %s \n", item
);
111 int ret
= vf
->control(vf
, VFCTRL_GET_EQUALIZER
, &data
);
112 if (ret
== CONTROL_TRUE
){
117 /* try software control */
118 if(mpvdec
) return mpvdec
->control(sh_video
,VDCTRL_GET_EQUALIZER
, item
, value
);
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");
130 int ret
= vf
->control(vf
, VFCTRL_CHANGE_RECTANGLE
, data
);
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
)
148 ret
= mpvdec
->control(sh_video
, VDCTRL_QUERY_UNSEEN_FRAMES
, NULL
);
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
);
162 vf_uninit_filter_chain(sh_video
->vfilter
);
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
){
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];
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
210 /* try to open shared decoder plugin */
213 vd_functions_t
*funcs_sym
;
216 buf_len
= strlen(MPLAYER_LIBDIR
)+strlen(sh_video
->codec
->drv
)+16;
217 buf
= malloc(buf_len
);
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
)
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
)
230 info_sym
= funcs_sym
->info
;
231 if (strcmp(info_sym
->short_name
, sh_video
->codec
->drv
))
235 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "Using external decoder plugin (%s/mplayer/vd_%s.so)!\n",
236 MPLAYER_LIBDIR
, sh_video
->codec
->drv
);
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
);
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);
255 sh_video
->bih
->biWidth
= sh_video
->disp_w
;
256 sh_video
->bih
->biHeight
= sh_video
->disp_h
;
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
;
268 sh_video
->bih
->biWidth
= sh_video
->disp_w
;
269 sh_video
->bih
->biHeight
= sh_video
->disp_h
;
271 continue; // try next...
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
};
283 if(!video_codec_list
) video_codec_list
=vc_l_default
;
284 // Go through the codec.conf and find the best codec...
286 codecs_reset_selection(0);
287 while(!sh_video
->inited
&& *video_codec_list
){
288 char* video_codec
=*(video_codec_list
++);
290 if(video_codec
[0]=='-'){
291 // disable this codec:
292 select_codec(video_codec
+1,0);
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);
300 // try in stability order: UNTESTED, WORKING, BUGGY. never try CRASHING.
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
);
323 mp_msg(MSGT_DECVIDEO
,MSGL_INFO
,MSGTR_SelectedVideoCodec
,
324 sh_video
->codec
->name
,sh_video
->codec
->drv
,sh_video
->codec
->info
);
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();
336 if (correct_pts
&& pts
!= MP_NOPTS_VALUE
) {
337 int delay
= get_current_video_decoder_lag(sh_video
);
339 if (delay
> sh_video
->num_buffered_pts
)
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");
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");
356 for (i
= 0; i
< sh_video
->num_buffered_pts
; i
++)
357 if (sh_video
->buffered_pts
[i
] < pts
)
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. --------------------
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");
381 t2
= GetTimer(); t
= t2
-t
;
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
;
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
];
399 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "No pts value from demuxer to "
401 sh_video
->pts
= MP_NOPTS_VALUE
;
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
);
415 vf
->control(vf
, VFCTRL_DRAW_OSD
, NULL
);
417 vf
->control(vf
, VFCTRL_DRAW_EOSD
, NULL
);
422 vout_time_usage
+= t2
*0.000001;