12 #include "osdep/timer.h"
13 #include "osdep/shmem.h"
15 #include "stream/stream.h"
16 #include "libmpdemux/demuxer.h"
17 #include "libmpdemux/parse_es.h"
19 #include "codec-cfg.h"
21 #include "libvo/video_out.h"
23 #include "libmpdemux/stheader.h"
27 #include "dec_video.h"
29 #ifdef CONFIG_DYNAMIC_PLUGINS
33 // ===================================================================
35 extern double video_time_usage
;
36 extern double vout_time_usage
;
38 #include "cpudetect.h"
40 int field_dominance
= -1;
44 int get_video_quality_max(sh_video_t
*sh_video
)
46 vf_instance_t
*vf
= sh_video
->vfilter
;
48 int ret
= vf
->control(vf
, VFCTRL_QUERY_MAX_PP_LEVEL
, NULL
);
50 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "[PP] Using external postprocessing filter, max q = %d.\n", ret
);
54 const struct vd_functions
*vd
= sh_video
->vd_driver
;
56 int ret
= vd
->control(sh_video
, VDCTRL_QUERY_MAX_PP_LEVEL
, NULL
);
58 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "[PP] Using codec's postprocessing, max q = %d.\n", ret
);
65 void set_video_quality(sh_video_t
*sh_video
, int quality
)
67 vf_instance_t
*vf
= sh_video
->vfilter
;
69 int ret
= vf
->control(vf
, VFCTRL_SET_PP_LEVEL
, (void *) (&quality
));
70 if (ret
== CONTROL_TRUE
)
73 const struct vd_functions
*vd
= sh_video
->vd_driver
;
75 vd
->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
);
88 int ret
= vf
->control(vf
, VFCTRL_SET_EQUALIZER
, &data
);
89 if (ret
== CONTROL_TRUE
)
92 /* try software control */
93 const struct vd_functions
*vd
= sh_video
->vd_driver
;
95 vd
->control(sh_video
, VDCTRL_SET_EQUALIZER
, item
, (int *) value
)
98 mp_tmsg(MSGT_DECVIDEO
, MSGL_V
, "Video attribute '%s' is not supported by selected vo & vd.\n",
103 int get_video_colors(sh_video_t
*sh_video
, const char *item
, int *value
)
105 vf_instance_t
*vf
= sh_video
->vfilter
;
110 mp_dbg(MSGT_DECVIDEO
, MSGL_V
, "get video colors %s \n", item
);
112 int ret
= vf
->control(vf
, VFCTRL_GET_EQUALIZER
, &data
);
113 if (ret
== CONTROL_TRUE
) {
118 /* try software control */
119 const struct vd_functions
*vd
= sh_video
->vd_driver
;
121 return vd
->control(sh_video
, VDCTRL_GET_EQUALIZER
, item
, value
);
125 int set_rectangle(sh_video_t
*sh_video
, int param
, int value
)
127 vf_instance_t
*vf
= sh_video
->vfilter
;
128 int data
[] = { param
, value
};
130 mp_dbg(MSGT_DECVIDEO
, MSGL_V
, "set rectangle \n");
132 int ret
= vf
->control(vf
, VFCTRL_CHANGE_RECTANGLE
, data
);
139 int redraw_osd(struct sh_video
*sh_video
, struct osd_state
*osd
)
141 struct vf_instance
*vf
= sh_video
->vfilter
;
142 if (vf
->control(vf
, VFCTRL_REDRAW_OSD
, osd
) == true)
147 void resync_video_stream(sh_video_t
*sh_video
)
149 const struct vd_functions
*vd
= sh_video
->vd_driver
;
151 vd
->control(sh_video
, VDCTRL_RESYNC_STREAM
, NULL
);
152 sh_video
->prev_codec_reordered_pts
= MP_NOPTS_VALUE
;
153 sh_video
->prev_sorted_pts
= MP_NOPTS_VALUE
;
156 int get_current_video_decoder_lag(sh_video_t
*sh_video
)
158 const struct vd_functions
*vd
= sh_video
->vd_driver
;
161 int ret
= vd
->control(sh_video
, VDCTRL_QUERY_UNSEEN_FRAMES
, NULL
);
167 void uninit_video(sh_video_t
*sh_video
)
169 if (!sh_video
->initialized
)
171 mp_tmsg(MSGT_DECVIDEO
, MSGL_V
, "Uninit video: %s\n", sh_video
->codec
->drv
);
172 sh_video
->vd_driver
->uninit(sh_video
);
173 #ifdef CONFIG_DYNAMIC_PLUGINS
174 if (sh_video
->dec_handle
)
175 dlclose(sh_video
->dec_handle
);
177 vf_uninit_filter_chain(sh_video
->vfilter
);
178 sh_video
->initialized
= 0;
184 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "Available (compiled-in) video codec families/drivers:\n");
185 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_DRIVERS\n");
186 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, " vfm: info: (comment)\n");
187 for (i
= 0; mpcodecs_vd_drivers
[i
] != NULL
; i
++)
188 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, "%8s %s (%s)\n",
189 mpcodecs_vd_drivers
[i
]->info
->short_name
,
190 mpcodecs_vd_drivers
[i
]->info
->name
,
191 mpcodecs_vd_drivers
[i
]->info
->comment
);
194 static int init_video(sh_video_t
*sh_video
, char *codecname
, char *vfm
,
195 int status
, stringset_t
*selected
)
198 unsigned int orig_fourcc
=
199 sh_video
->bih
? sh_video
->bih
->biCompression
: 0;
200 sh_video
->codec
= NULL
;
201 sh_video
->vf_initialized
= 0;
202 if (codecname
&& codecname
[0] == '+') {
203 codecname
= &codecname
[1];
210 // restore original fourcc:
212 sh_video
->bih
->biCompression
= orig_fourcc
;
215 find_video_codec(sh_video
->format
,
216 sh_video
->bih
? ((unsigned int *) &sh_video
->
217 bih
->biCompression
) : NULL
,
218 sh_video
->codec
, force
)))
220 // ok we found one codec
221 if (stringset_test(selected
, sh_video
->codec
->name
))
222 continue; // already tried & failed
223 if (codecname
&& strcmp(sh_video
->codec
->name
, codecname
))
225 if (vfm
&& strcmp(sh_video
->codec
->drv
, vfm
))
226 continue; // vfm doesn't match
227 if (!force
&& sh_video
->codec
->status
< status
)
228 continue; // too unstable
229 stringset_add(selected
, sh_video
->codec
->name
); // tagging it
230 // ok, it matches all rules, let's find the driver!
231 for (i
= 0; mpcodecs_vd_drivers
[i
] != NULL
; i
++)
232 if (!strcmp(mpcodecs_vd_drivers
[i
]->info
->short_name
,
233 sh_video
->codec
->drv
))
235 sh_video
->vd_driver
= mpcodecs_vd_drivers
[i
];
236 #ifdef CONFIG_DYNAMIC_PLUGINS
237 if (!sh_video
->vd_driver
) {
238 /* try to open shared decoder plugin */
241 vd_functions_t
*funcs_sym
;
245 strlen(MPLAYER_LIBDIR
) + strlen(sh_video
->codec
->drv
) + 16;
246 buf
= malloc(buf_len
);
249 snprintf(buf
, buf_len
, "%s/mplayer/vd_%s.so", MPLAYER_LIBDIR
,
250 sh_video
->codec
->drv
);
251 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
,
252 "Trying to open external plugin: %s\n", buf
);
253 sh_video
->dec_handle
= dlopen(buf
, RTLD_LAZY
);
254 if (!sh_video
->dec_handle
)
256 snprintf(buf
, buf_len
, "mpcodecs_vd_%s", sh_video
->codec
->drv
);
257 funcs_sym
= dlsym(sh_video
->dec_handle
, buf
);
258 if (!funcs_sym
|| !funcs_sym
->info
|| !funcs_sym
->init
259 || !funcs_sym
->uninit
|| !funcs_sym
->control
260 || !funcs_sym
->decode
)
262 info_sym
= funcs_sym
->info
;
263 if (strcmp(info_sym
->short_name
, sh_video
->codec
->drv
))
266 sh_video
->vd_driver
= funcs_sym
;
267 mp_msg(MSGT_DECVIDEO
, MSGL_V
,
268 "Using external decoder plugin (%s/mplayer/vd_%s.so)!\n",
269 MPLAYER_LIBDIR
, sh_video
->codec
->drv
);
272 if (!sh_video
->vd_driver
) { // driver not available (==compiled in)
273 mp_tmsg(MSGT_DECVIDEO
, MSGL_WARN
,
274 _("Requested video codec family [%s] (vfm=%s) not available.\nEnable it at compilation.\n"),
275 sh_video
->codec
->name
, sh_video
->codec
->drv
);
278 orig_w
= sh_video
->bih
? sh_video
->bih
->biWidth
: sh_video
->disp_w
;
279 orig_h
= sh_video
->bih
? sh_video
->bih
->biHeight
: sh_video
->disp_h
;
280 sh_video
->disp_w
= orig_w
;
281 sh_video
->disp_h
= orig_h
;
282 // it's available, let's try to init!
283 if (sh_video
->codec
->flags
& CODECS_FLAG_ALIGN16
) {
284 // align width/height to n*16
285 sh_video
->disp_w
= (sh_video
->disp_w
+ 15) & (~15);
286 sh_video
->disp_h
= (sh_video
->disp_h
+ 15) & (~15);
289 sh_video
->bih
->biWidth
= sh_video
->disp_w
;
290 sh_video
->bih
->biHeight
= sh_video
->disp_h
;
294 const struct vd_functions
*vd
= sh_video
->vd_driver
;
295 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "Opening video decoder: [%s] %s\n",
296 vd
->info
->short_name
, vd
->info
->name
);
297 // clear vf init error, it is no longer relevant
298 if (sh_video
->vf_initialized
< 0)
299 sh_video
->vf_initialized
= 0;
300 if (!vd
->init(sh_video
)) {
301 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "VDecoder init failed :(\n");
302 sh_video
->disp_w
= orig_w
;
303 sh_video
->disp_h
= orig_h
;
305 sh_video
->bih
->biWidth
= sh_video
->disp_w
;
306 sh_video
->bih
->biHeight
= sh_video
->disp_h
;
308 continue; // try next...
311 sh_video
->initialized
= 1;
312 sh_video
->prev_codec_reordered_pts
= MP_NOPTS_VALUE
;
313 sh_video
->prev_sorted_pts
= MP_NOPTS_VALUE
;
319 int init_best_video_codec(sh_video_t
*sh_video
, char **video_codec_list
,
320 char **video_fm_list
)
322 char *vc_l_default
[2] = { "", (char *) NULL
};
323 stringset_t selected
;
325 if (!video_codec_list
)
326 video_codec_list
= vc_l_default
;
327 // Go through the codec.conf and find the best codec...
328 sh_video
->initialized
= 0;
329 stringset_init(&selected
);
330 while (!sh_video
->initialized
&& *video_codec_list
) {
331 char *video_codec
= *(video_codec_list
++);
332 if (video_codec
[0]) {
333 if (video_codec
[0] == '-') {
334 // disable this codec:
335 stringset_add(&selected
, video_codec
+ 1);
337 // forced codec by name:
338 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "Forced video codec: %s\n",
340 init_video(sh_video
, video_codec
, NULL
, -1, &selected
);
344 // try in stability order: UNTESTED, WORKING, BUGGY. never try CRASHING.
346 char **fmlist
= video_fm_list
;
347 // try first the preferred codec families:
348 while (!sh_video
->initialized
&& *fmlist
) {
349 char *video_fm
= *(fmlist
++);
350 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "Trying to force video codec driver family %s...\n",
352 for (status
= CODECS_STATUS__MAX
;
353 status
>= CODECS_STATUS__MIN
; --status
)
355 (sh_video
, NULL
, video_fm
, status
, &selected
))
359 if (!sh_video
->initialized
)
360 for (status
= CODECS_STATUS__MAX
; status
>= CODECS_STATUS__MIN
;
362 if (init_video(sh_video
, NULL
, NULL
, status
, &selected
))
366 stringset_free(&selected
);
368 if (!sh_video
->initialized
) {
369 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "Cannot find codec matching selected -vo and video format 0x%X.\n",
374 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "Selected video codec: [%s] vfm: %s (%s)\n",
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 if (sh_video
->vd_driver
->decode2
) {
419 mpi
= sh_video
->vd_driver
->decode2(sh_video
, start
, in_size
,
422 mpi
= sh_video
->vd_driver
->decode(sh_video
, start
, in_size
,
424 pts
= MP_NOPTS_VALUE
;
427 //------------------------ frame decoded. --------------------
430 // some codecs are broken, and doesn't restore MMX state :(
431 // it happens usually with broken/damaged files.
432 if (gCpuCaps
.has3DNow
) {
433 __asm__
volatile("femms\n\t":::"memory");
434 } else if (gCpuCaps
.hasMMX
) {
435 __asm__
volatile("emms\n\t":::"memory");
442 video_time_usage
+= tt
;
444 if (!mpi
|| drop_frame
)
445 return NULL
; // error / skipped frame
447 if (field_dominance
== 0)
448 mpi
->fields
|= MP_IMGFIELD_TOP_FIRST
;
449 else if (field_dominance
== 1)
450 mpi
->fields
&= ~MP_IMGFIELD_TOP_FIRST
;
452 double prevpts
= sh_video
->codec_reordered_pts
;
453 sh_video
->prev_codec_reordered_pts
= prevpts
;
454 sh_video
->codec_reordered_pts
= pts
;
455 if (prevpts
!= MP_NOPTS_VALUE
&& pts
<= prevpts
456 || pts
== MP_NOPTS_VALUE
)
457 sh_video
->num_reordered_pts_problems
++;
458 prevpts
= sh_video
->sorted_pts
;
459 if (opts
->correct_pts
) {
460 if (sh_video
->num_buffered_pts
) {
461 sh_video
->num_buffered_pts
--;
462 sh_video
->sorted_pts
=
463 sh_video
->buffered_pts
[sh_video
->num_buffered_pts
];
465 mp_msg(MSGT_CPLAYER
, MSGL_ERR
,
466 "No pts value from demuxer to " "use for frame!\n");
467 sh_video
->sorted_pts
= MP_NOPTS_VALUE
;
470 pts
= sh_video
->sorted_pts
;
471 if (prevpts
!= MP_NOPTS_VALUE
&& pts
<= prevpts
472 || pts
== MP_NOPTS_VALUE
)
473 sh_video
->num_sorted_pts_problems
++;
477 int filter_video(sh_video_t
*sh_video
, void *frame
, double pts
)
479 mp_image_t
*mpi
= frame
;
480 unsigned int t2
= GetTimer();
481 vf_instance_t
*vf
= sh_video
->vfilter
;
482 // apply video filters and call the leaf vo/ve
483 int ret
= vf
->put_image(vf
, mpi
, pts
);
485 t2
= GetTimer() - t2
;
486 vout_time_usage
+= t2
* 0.000001;