15 #include "osdep/timer.h"
16 #include "osdep/shmem.h"
18 #include "stream/stream.h"
19 #include "libmpdemux/demuxer.h"
20 #include "libmpdemux/parse_es.h"
22 #include "codec-cfg.h"
24 #include "libvo/video_out.h"
26 #include "libmpdemux/stheader.h"
30 #include "dec_video.h"
32 #ifdef CONFIG_DYNAMIC_PLUGINS
36 // ===================================================================
38 extern double video_time_usage
;
39 extern double vout_time_usage
;
41 #include "cpudetect.h"
43 int field_dominance
= -1;
47 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
);
57 const struct vd_functions
*vd
= sh_video
->vd_driver
;
59 int ret
= vd
->control(sh_video
, VDCTRL_QUERY_MAX_PP_LEVEL
, NULL
);
61 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_UsingCodecPP
, ret
);
68 void set_video_quality(sh_video_t
*sh_video
, int quality
)
70 vf_instance_t
*vf
= sh_video
->vfilter
;
72 int ret
= vf
->control(vf
, VFCTRL_SET_PP_LEVEL
, (void *) (&quality
));
73 if (ret
== CONTROL_TRUE
)
76 const struct vd_functions
*vd
= sh_video
->vd_driver
;
78 vd
->control(sh_video
, VDCTRL_SET_PP_LEVEL
, (void *) (&quality
));
81 int set_video_colors(sh_video_t
*sh_video
, const char *item
, int value
)
83 vf_instance_t
*vf
= sh_video
->vfilter
;
89 mp_dbg(MSGT_DECVIDEO
, MSGL_V
, "set video colors %s=%d \n", item
, value
);
91 int ret
= vf
->control(vf
, VFCTRL_SET_EQUALIZER
, &data
);
92 if (ret
== CONTROL_TRUE
)
95 /* try software control */
96 const struct vd_functions
*vd
= sh_video
->vd_driver
;
98 vd
->control(sh_video
, VDCTRL_SET_EQUALIZER
, item
, (int *) value
)
101 mp_msg(MSGT_DECVIDEO
, MSGL_V
, MSGTR_VideoAttributeNotSupportedByVO_VD
,
106 int get_video_colors(sh_video_t
*sh_video
, const char *item
, int *value
)
108 vf_instance_t
*vf
= sh_video
->vfilter
;
113 mp_dbg(MSGT_DECVIDEO
, MSGL_V
, "get video colors %s \n", item
);
115 int ret
= vf
->control(vf
, VFCTRL_GET_EQUALIZER
, &data
);
116 if (ret
== CONTROL_TRUE
) {
121 /* try software control */
122 const struct vd_functions
*vd
= sh_video
->vd_driver
;
124 return vd
->control(sh_video
, VDCTRL_GET_EQUALIZER
, item
, value
);
128 int set_rectangle(sh_video_t
*sh_video
, int param
, int value
)
130 vf_instance_t
*vf
= sh_video
->vfilter
;
131 int data
[] = { param
, value
};
133 mp_dbg(MSGT_DECVIDEO
, MSGL_V
, "set rectangle \n");
135 int ret
= vf
->control(vf
, VFCTRL_CHANGE_RECTANGLE
, data
);
142 int redraw_osd(struct sh_video
*sh_video
, struct osd_state
*osd
)
144 struct vf_instance
*vf
= sh_video
->vfilter
;
145 if (vf
->control(vf
, VFCTRL_REDRAW_OSD
, osd
) == true)
150 void resync_video_stream(sh_video_t
*sh_video
)
152 const struct vd_functions
*vd
= sh_video
->vd_driver
;
154 vd
->control(sh_video
, VDCTRL_RESYNC_STREAM
, NULL
);
157 int get_current_video_decoder_lag(sh_video_t
*sh_video
)
159 const struct vd_functions
*vd
= sh_video
->vd_driver
;
162 int ret
= vd
->control(sh_video
, VDCTRL_QUERY_UNSEEN_FRAMES
, NULL
);
168 void uninit_video(sh_video_t
*sh_video
)
170 if (!sh_video
->initialized
)
172 mp_msg(MSGT_DECVIDEO
, MSGL_V
, MSGTR_UninitVideoStr
, sh_video
->codec
->drv
);
173 sh_video
->vd_driver
->uninit(sh_video
);
174 #ifdef CONFIG_DYNAMIC_PLUGINS
175 if (sh_video
->dec_handle
)
176 dlclose(sh_video
->dec_handle
);
178 vf_uninit_filter_chain(sh_video
->vfilter
);
179 sh_video
->initialized
= 0;
185 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_AvailableVideoFm
);
186 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_DRIVERS\n");
187 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, " vfm: info: (comment)\n");
188 for (i
= 0; mpcodecs_vd_drivers
[i
] != NULL
; i
++)
189 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, "%8s %s (%s)\n",
190 mpcodecs_vd_drivers
[i
]->info
->short_name
,
191 mpcodecs_vd_drivers
[i
]->info
->name
,
192 mpcodecs_vd_drivers
[i
]->info
->comment
);
195 static int init_video(sh_video_t
*sh_video
, char *codecname
, char *vfm
,
196 int status
, stringset_t
*selected
)
199 unsigned int orig_fourcc
=
200 sh_video
->bih
? sh_video
->bih
->biCompression
: 0;
201 sh_video
->codec
= NULL
;
202 sh_video
->vf_initialized
= 0;
203 if (codecname
&& codecname
[0] == '+') {
204 codecname
= &codecname
[1];
211 // restore original fourcc:
213 sh_video
->bih
->biCompression
= orig_fourcc
;
216 find_video_codec(sh_video
->format
,
217 sh_video
->bih
? ((unsigned int *) &sh_video
->
218 bih
->biCompression
) : NULL
,
219 sh_video
->codec
, force
)))
221 // ok we found one codec
222 if (stringset_test(selected
, sh_video
->codec
->name
))
223 continue; // already tried & failed
224 if (codecname
&& strcmp(sh_video
->codec
->name
, codecname
))
226 if (vfm
&& strcmp(sh_video
->codec
->drv
, vfm
))
227 continue; // vfm doesn't match
228 if (!force
&& sh_video
->codec
->status
< status
)
229 continue; // too unstable
230 stringset_add(selected
, sh_video
->codec
->name
); // tagging it
231 // ok, it matches all rules, let's find the driver!
232 for (i
= 0; mpcodecs_vd_drivers
[i
] != NULL
; i
++)
233 if (!strcmp(mpcodecs_vd_drivers
[i
]->info
->short_name
,
234 sh_video
->codec
->drv
))
236 sh_video
->vd_driver
= mpcodecs_vd_drivers
[i
];
237 #ifdef CONFIG_DYNAMIC_PLUGINS
238 if (!sh_video
->vd_driver
) {
239 /* try to open shared decoder plugin */
242 vd_functions_t
*funcs_sym
;
246 strlen(MPLAYER_LIBDIR
) + strlen(sh_video
->codec
->drv
) + 16;
247 buf
= malloc(buf_len
);
250 snprintf(buf
, buf_len
, "%s/mplayer/vd_%s.so", MPLAYER_LIBDIR
,
251 sh_video
->codec
->drv
);
252 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
,
253 "Trying to open external plugin: %s\n", buf
);
254 sh_video
->dec_handle
= dlopen(buf
, RTLD_LAZY
);
255 if (!sh_video
->dec_handle
)
257 snprintf(buf
, buf_len
, "mpcodecs_vd_%s", sh_video
->codec
->drv
);
258 funcs_sym
= dlsym(sh_video
->dec_handle
, buf
);
259 if (!funcs_sym
|| !funcs_sym
->info
|| !funcs_sym
->init
260 || !funcs_sym
->uninit
|| !funcs_sym
->control
261 || !funcs_sym
->decode
)
263 info_sym
= funcs_sym
->info
;
264 if (strcmp(info_sym
->short_name
, sh_video
->codec
->drv
))
267 sh_video
->vd_driver
= funcs_sym
;
268 mp_msg(MSGT_DECVIDEO
, MSGL_V
,
269 "Using external decoder plugin (%s/mplayer/vd_%s.so)!\n",
270 MPLAYER_LIBDIR
, sh_video
->codec
->drv
);
273 if (!sh_video
->vd_driver
) { // driver not available (==compiled in)
274 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
,
275 MSGTR_VideoCodecFamilyNotAvailableStr
,
276 sh_video
->codec
->name
, sh_video
->codec
->drv
);
279 orig_w
= sh_video
->bih
? sh_video
->bih
->biWidth
: sh_video
->disp_w
;
280 orig_h
= sh_video
->bih
? sh_video
->bih
->biHeight
: sh_video
->disp_h
;
281 sh_video
->disp_w
= orig_w
;
282 sh_video
->disp_h
= orig_h
;
283 // it's available, let's try to init!
284 if (sh_video
->codec
->flags
& CODECS_FLAG_ALIGN16
) {
285 // align width/height to n*16
286 sh_video
->disp_w
= (sh_video
->disp_w
+ 15) & (~15);
287 sh_video
->disp_h
= (sh_video
->disp_h
+ 15) & (~15);
290 sh_video
->bih
->biWidth
= sh_video
->disp_w
;
291 sh_video
->bih
->biHeight
= sh_video
->disp_h
;
295 const struct vd_functions
*vd
= sh_video
->vd_driver
;
296 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_OpeningVideoDecoder
,
297 vd
->info
->short_name
, vd
->info
->name
);
298 // clear vf init error, it is no longer relevant
299 if (sh_video
->vf_initialized
< 0)
300 sh_video
->vf_initialized
= 0;
301 if (!vd
->init(sh_video
)) {
302 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_VDecoderInitFailed
);
303 sh_video
->disp_w
= orig_w
;
304 sh_video
->disp_h
= orig_h
;
306 sh_video
->bih
->biWidth
= sh_video
->disp_w
;
307 sh_video
->bih
->biHeight
= sh_video
->disp_h
;
309 continue; // try next...
312 sh_video
->initialized
= 1;
318 int init_best_video_codec(sh_video_t
*sh_video
, char **video_codec_list
,
319 char **video_fm_list
)
321 char *vc_l_default
[2] = { "", (char *) NULL
};
322 stringset_t selected
;
324 if (!video_codec_list
)
325 video_codec_list
= vc_l_default
;
326 // Go through the codec.conf and find the best codec...
327 sh_video
->initialized
= 0;
328 stringset_init(&selected
);
329 while (!sh_video
->initialized
&& *video_codec_list
) {
330 char *video_codec
= *(video_codec_list
++);
331 if (video_codec
[0]) {
332 if (video_codec
[0] == '-') {
333 // disable this codec:
334 stringset_add(&selected
, video_codec
+ 1);
336 // forced codec by name:
337 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_ForcedVideoCodec
,
339 init_video(sh_video
, video_codec
, NULL
, -1, &selected
);
343 // try in stability order: UNTESTED, WORKING, BUGGY. never try CRASHING.
345 char **fmlist
= video_fm_list
;
346 // try first the preferred codec families:
347 while (!sh_video
->initialized
&& *fmlist
) {
348 char *video_fm
= *(fmlist
++);
349 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_TryForceVideoFmtStr
,
351 for (status
= CODECS_STATUS__MAX
;
352 status
>= CODECS_STATUS__MIN
; --status
)
354 (sh_video
, NULL
, video_fm
, status
, &selected
))
358 if (!sh_video
->initialized
)
359 for (status
= CODECS_STATUS__MAX
; status
>= CODECS_STATUS__MIN
;
361 if (init_video(sh_video
, NULL
, NULL
, status
, &selected
))
365 stringset_free(&selected
);
367 if (!sh_video
->initialized
) {
368 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_CantFindVideoCodec
,
370 mp_msg(MSGT_DECAUDIO
, MSGL_HINT
, MSGTR_RTFMCodecs
);
374 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_SelectedVideoCodec
,
375 sh_video
->codec
->name
, sh_video
->codec
->drv
, sh_video
->codec
->info
);
379 void *decode_video(sh_video_t
*sh_video
, unsigned char *start
, int in_size
,
380 int drop_frame
, double pts
)
382 mp_image_t
*mpi
= NULL
;
383 unsigned int t
= GetTimer();
386 struct MPOpts
*opts
= sh_video
->opts
;
388 if (opts
->correct_pts
&& pts
!= MP_NOPTS_VALUE
) {
389 int delay
= get_current_video_decoder_lag(sh_video
);
391 if (delay
> sh_video
->num_buffered_pts
)
393 // this is disabled because vd_ffmpeg reports the same lag
394 // after seek even when there are no buffered frames,
395 // leading to incorrect error messages
396 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Not enough buffered pts\n");
401 sh_video
->num_buffered_pts
= delay
;
403 if (sh_video
->num_buffered_pts
==
404 sizeof(sh_video
->buffered_pts
) / sizeof(double))
405 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Too many buffered pts\n");
408 for (i
= 0; i
< sh_video
->num_buffered_pts
; i
++)
409 if (sh_video
->buffered_pts
[i
] < pts
)
411 for (j
= sh_video
->num_buffered_pts
; j
> i
; j
--)
412 sh_video
->buffered_pts
[j
] = sh_video
->buffered_pts
[j
- 1];
413 sh_video
->buffered_pts
[i
] = pts
;
414 sh_video
->num_buffered_pts
++;
418 mpi
= sh_video
->vd_driver
->decode(sh_video
, start
, in_size
, drop_frame
);
420 //------------------------ frame decoded. --------------------
423 // some codecs are broken, and doesn't restore MMX state :(
424 // it happens usually with broken/damaged files.
425 if (gCpuCaps
.has3DNow
) {
426 __asm__
volatile("femms\n\t":::"memory");
427 } else if (gCpuCaps
.hasMMX
) {
428 __asm__
volatile("emms\n\t":::"memory");
435 video_time_usage
+= tt
;
437 if (!mpi
|| drop_frame
)
438 return NULL
; // error / skipped frame
440 if (field_dominance
== 0)
441 mpi
->fields
|= MP_IMGFIELD_TOP_FIRST
;
442 else if (field_dominance
== 1)
443 mpi
->fields
&= ~MP_IMGFIELD_TOP_FIRST
;
445 if (opts
->correct_pts
) {
446 if (sh_video
->num_buffered_pts
) {
447 sh_video
->num_buffered_pts
--;
448 sh_video
->pts
= sh_video
->buffered_pts
[sh_video
->num_buffered_pts
];
450 mp_msg(MSGT_CPLAYER
, MSGL_ERR
,
451 "No pts value from demuxer to " "use for frame!\n");
452 sh_video
->pts
= MP_NOPTS_VALUE
;
458 int filter_video(sh_video_t
*sh_video
, void *frame
, double pts
)
460 mp_image_t
*mpi
= frame
;
461 unsigned int t2
= GetTimer();
462 vf_instance_t
*vf
= sh_video
->vfilter
;
463 // apply video filters and call the leaf vo/ve
464 int ret
= vf
->put_image(vf
, mpi
, pts
);
466 t2
= GetTimer() - t2
;
467 vout_time_usage
+= t2
* 0.000001;