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 CONFIG_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
->initialized
) return;
156 mp_msg(MSGT_DECVIDEO
,MSGL_V
,MSGTR_UninitVideoStr
,sh_video
->codec
->drv
);
157 mpvdec
->uninit(sh_video
);
158 #ifdef CONFIG_DYNAMIC_PLUGINS
159 if (sh_video
->dec_handle
)
160 dlclose(sh_video
->dec_handle
);
162 vf_uninit_filter_chain(sh_video
->vfilter
);
163 sh_video
->initialized
=0;
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 stringset_t
*selected
){
181 unsigned int orig_fourcc
=sh_video
->bih
?sh_video
->bih
->biCompression
:0;
182 sh_video
->codec
=NULL
;
183 sh_video
->vf_initialized
=0;
184 if (codecname
&& codecname
[0] == '+') {
185 codecname
= &codecname
[1];
192 // restore original fourcc:
193 if(sh_video
->bih
) sh_video
->bih
->biCompression
=orig_fourcc
;
194 if(!(sh_video
->codec
=find_video_codec(sh_video
->format
,
195 sh_video
->bih
?((unsigned int*) &sh_video
->bih
->biCompression
):NULL
,
196 sh_video
->codec
,force
) )) break;
197 // ok we found one codec
198 if(stringset_test(selected
, sh_video
->codec
->name
)) continue; // already tried & failed
199 if(codecname
&& strcmp(sh_video
->codec
->name
,codecname
)) continue; // -vc
200 if(vfm
&& strcmp(sh_video
->codec
->drv
,vfm
)) continue; // vfm doesn't match
201 if(!force
&& sh_video
->codec
->status
<status
) continue; // too unstable
202 stringset_add(selected
, sh_video
->codec
->name
); // tagging it
203 // ok, it matches all rules, let's find the driver!
204 for (i
=0; mpcodecs_vd_drivers
[i
] != NULL
; i
++)
205 // if(mpcodecs_vd_drivers[i]->info->id==sh_video->codec->driver) break;
206 if(!strcmp(mpcodecs_vd_drivers
[i
]->info
->short_name
,sh_video
->codec
->drv
)) break;
207 mpvdec
=mpcodecs_vd_drivers
[i
];
208 #ifdef CONFIG_DYNAMIC_PLUGINS
211 /* try to open shared decoder plugin */
214 vd_functions_t
*funcs_sym
;
217 buf_len
= strlen(MPLAYER_LIBDIR
)+strlen(sh_video
->codec
->drv
)+16;
218 buf
= malloc(buf_len
);
221 snprintf(buf
, buf_len
, "%s/mplayer/vd_%s.so", MPLAYER_LIBDIR
, sh_video
->codec
->drv
);
222 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
, "Trying to open external plugin: %s\n", buf
);
223 sh_video
->dec_handle
= dlopen(buf
, RTLD_LAZY
);
224 if (!sh_video
->dec_handle
)
226 snprintf(buf
, buf_len
, "mpcodecs_vd_%s", sh_video
->codec
->drv
);
227 funcs_sym
= dlsym(sh_video
->dec_handle
, buf
);
228 if (!funcs_sym
|| !funcs_sym
->info
|| !funcs_sym
->init
||
229 !funcs_sym
->uninit
|| !funcs_sym
->control
|| !funcs_sym
->decode
)
231 info_sym
= funcs_sym
->info
;
232 if (strcmp(info_sym
->short_name
, sh_video
->codec
->drv
))
236 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "Using external decoder plugin (%s/mplayer/vd_%s.so)!\n",
237 MPLAYER_LIBDIR
, sh_video
->codec
->drv
);
240 if(!mpvdec
){ // driver not available (==compiled in)
241 mp_msg(MSGT_DECVIDEO
,MSGL_WARN
,MSGTR_VideoCodecFamilyNotAvailableStr
,
242 sh_video
->codec
->name
, sh_video
->codec
->drv
);
245 orig_w
= sh_video
->bih
? sh_video
->bih
->biWidth
: sh_video
->disp_w
;
246 orig_h
= sh_video
->bih
? sh_video
->bih
->biHeight
: sh_video
->disp_h
;
247 sh_video
->disp_w
= orig_w
;
248 sh_video
->disp_h
= orig_h
;
249 // it's available, let's try to init!
250 if(sh_video
->codec
->flags
& CODECS_FLAG_ALIGN16
){
251 // align width/height to n*16
252 sh_video
->disp_w
=(sh_video
->disp_w
+15)&(~15);
253 sh_video
->disp_h
=(sh_video
->disp_h
+15)&(~15);
256 sh_video
->bih
->biWidth
= sh_video
->disp_w
;
257 sh_video
->bih
->biHeight
= sh_video
->disp_h
;
260 mp_msg(MSGT_DECVIDEO
,MSGL_INFO
,MSGTR_OpeningVideoDecoder
,mpvdec
->info
->short_name
,mpvdec
->info
->name
);
261 // clear vf init error, it is no longer relevant
262 if (sh_video
->vf_initialized
< 0)
263 sh_video
->vf_initialized
= 0;
264 if(!mpvdec
->init(sh_video
)){
265 mp_msg(MSGT_DECVIDEO
,MSGL_INFO
,MSGTR_VDecoderInitFailed
);
266 sh_video
->disp_w
=orig_w
;
267 sh_video
->disp_h
=orig_h
;
269 sh_video
->bih
->biWidth
= sh_video
->disp_w
;
270 sh_video
->bih
->biHeight
= sh_video
->disp_h
;
272 continue; // try next...
275 sh_video
->initialized
=1;
281 int init_best_video_codec(sh_video_t
*sh_video
,char** video_codec_list
,char** video_fm_list
){
282 char* vc_l_default
[2]={"",(char*)NULL
};
283 stringset_t selected
;
285 if(!video_codec_list
) video_codec_list
=vc_l_default
;
286 // Go through the codec.conf and find the best codec...
287 sh_video
->initialized
=0;
288 stringset_init(&selected
);
289 while(!sh_video
->initialized
&& *video_codec_list
){
290 char* video_codec
=*(video_codec_list
++);
292 if(video_codec
[0]=='-'){
293 // disable this codec:
294 stringset_add(&selected
, video_codec
+1);
296 // forced codec by name:
297 mp_msg(MSGT_DECVIDEO
,MSGL_INFO
,MSGTR_ForcedVideoCodec
,video_codec
);
298 init_video(sh_video
,video_codec
,NULL
,-1, &selected
);
302 // try in stability order: UNTESTED, WORKING, BUGGY. never try CRASHING.
304 char** fmlist
=video_fm_list
;
305 // try first the preferred codec families:
306 while(!sh_video
->initialized
&& *fmlist
){
307 char* video_fm
=*(fmlist
++);
308 mp_msg(MSGT_DECVIDEO
,MSGL_INFO
,MSGTR_TryForceVideoFmtStr
,video_fm
);
309 for(status
=CODECS_STATUS__MAX
;status
>=CODECS_STATUS__MIN
;--status
)
310 if(init_video(sh_video
,NULL
,video_fm
,status
, &selected
)) break;
313 if(!sh_video
->initialized
)
314 for(status
=CODECS_STATUS__MAX
;status
>=CODECS_STATUS__MIN
;--status
)
315 if(init_video(sh_video
,NULL
,NULL
,status
, &selected
)) break;
318 stringset_free(&selected
);
320 if(!sh_video
->initialized
){
321 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,MSGTR_CantFindVideoCodec
,sh_video
->format
);
322 mp_msg(MSGT_DECAUDIO
,MSGL_HINT
, MSGTR_RTFMCodecs
);
326 mp_msg(MSGT_DECVIDEO
,MSGL_INFO
,MSGTR_SelectedVideoCodec
,
327 sh_video
->codec
->name
,sh_video
->codec
->drv
,sh_video
->codec
->info
);
331 void *decode_video(sh_video_t
*sh_video
, unsigned char *start
, int in_size
,
332 int drop_frame
, double pts
)
334 mp_image_t
*mpi
= NULL
;
335 unsigned int t
= GetTimer();
339 if (correct_pts
&& pts
!= MP_NOPTS_VALUE
) {
340 int delay
= get_current_video_decoder_lag(sh_video
);
342 if (delay
> sh_video
->num_buffered_pts
)
344 // this is disabled because vd_ffmpeg reports the same lag
345 // after seek even when there are no buffered frames,
346 // leading to incorrect error messages
347 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Not enough buffered pts\n");
352 sh_video
->num_buffered_pts
= delay
;
354 if (sh_video
->num_buffered_pts
==
355 sizeof(sh_video
->buffered_pts
)/sizeof(double))
356 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Too many buffered pts\n");
359 for (i
= 0; i
< sh_video
->num_buffered_pts
; i
++)
360 if (sh_video
->buffered_pts
[i
] < pts
)
362 for (j
= sh_video
->num_buffered_pts
; j
> i
; j
--)
363 sh_video
->buffered_pts
[j
] = sh_video
->buffered_pts
[j
-1];
364 sh_video
->buffered_pts
[i
] = pts
;
365 sh_video
->num_buffered_pts
++;
369 mpi
= mpvdec
->decode(sh_video
, start
, in_size
, drop_frame
);
371 //------------------------ frame decoded. --------------------
374 // some codecs are broken, and doesn't restore MMX state :(
375 // it happens usually with broken/damaged files.
376 if (gCpuCaps
.has3DNow
) {
377 __asm__
volatile ("femms\n\t":::"memory");
379 else if (gCpuCaps
.hasMMX
) {
380 __asm__
volatile ("emms\n\t":::"memory");
384 t2
= GetTimer(); t
= t2
-t
;
386 video_time_usage
+= tt
;
388 if (!mpi
|| drop_frame
)
389 return NULL
; // error / skipped frame
391 if (field_dominance
== 0)
392 mpi
->fields
|= MP_IMGFIELD_TOP_FIRST
;
393 else if (field_dominance
== 1)
394 mpi
->fields
&= ~MP_IMGFIELD_TOP_FIRST
;
397 if (sh_video
->num_buffered_pts
) {
398 sh_video
->num_buffered_pts
--;
399 sh_video
->pts
= sh_video
->buffered_pts
[sh_video
->num_buffered_pts
];
402 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "No pts value from demuxer to "
404 sh_video
->pts
= MP_NOPTS_VALUE
;
410 int filter_video(sh_video_t
*sh_video
, void *frame
, double pts
)
412 mp_image_t
*mpi
= frame
;
413 unsigned int t2
= GetTimer();
414 vf_instance_t
*vf
= sh_video
->vfilter
;
415 // apply video filters and call the leaf vo/ve
416 int ret
= vf
->put_image(vf
, mpi
, pts
);
418 // draw EOSD first so it ends up below the OSD.
419 // Note that changing this is will not work right with vf_ass and the
420 // vos currently always draw the EOSD first in paused mode.
422 vf
->control(vf
, VFCTRL_DRAW_EOSD
, NULL
);
424 vf
->control(vf
, VFCTRL_DRAW_OSD
, NULL
);
428 vout_time_usage
+= t2
*0.000001;