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
);
154 int get_current_video_decoder_lag(sh_video_t
*sh_video
)
156 const struct vd_functions
*vd
= sh_video
->vd_driver
;
159 int ret
= vd
->control(sh_video
, VDCTRL_QUERY_UNSEEN_FRAMES
, NULL
);
165 void uninit_video(sh_video_t
*sh_video
)
167 if (!sh_video
->initialized
)
169 mp_tmsg(MSGT_DECVIDEO
, MSGL_V
, "Uninit video: %s\n", sh_video
->codec
->drv
);
170 sh_video
->vd_driver
->uninit(sh_video
);
171 #ifdef CONFIG_DYNAMIC_PLUGINS
172 if (sh_video
->dec_handle
)
173 dlclose(sh_video
->dec_handle
);
175 vf_uninit_filter_chain(sh_video
->vfilter
);
176 sh_video
->initialized
= 0;
182 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "Available (compiled-in) video codec families/drivers:\n");
183 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_DRIVERS\n");
184 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, " vfm: info: (comment)\n");
185 for (i
= 0; mpcodecs_vd_drivers
[i
] != NULL
; i
++)
186 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, "%8s %s (%s)\n",
187 mpcodecs_vd_drivers
[i
]->info
->short_name
,
188 mpcodecs_vd_drivers
[i
]->info
->name
,
189 mpcodecs_vd_drivers
[i
]->info
->comment
);
192 static int init_video(sh_video_t
*sh_video
, char *codecname
, char *vfm
,
193 int status
, stringset_t
*selected
)
196 unsigned int orig_fourcc
=
197 sh_video
->bih
? sh_video
->bih
->biCompression
: 0;
198 sh_video
->codec
= NULL
;
199 sh_video
->vf_initialized
= 0;
200 if (codecname
&& codecname
[0] == '+') {
201 codecname
= &codecname
[1];
208 // restore original fourcc:
210 sh_video
->bih
->biCompression
= orig_fourcc
;
213 find_video_codec(sh_video
->format
,
214 sh_video
->bih
? ((unsigned int *) &sh_video
->
215 bih
->biCompression
) : NULL
,
216 sh_video
->codec
, force
)))
218 // ok we found one codec
219 if (stringset_test(selected
, sh_video
->codec
->name
))
220 continue; // already tried & failed
221 if (codecname
&& strcmp(sh_video
->codec
->name
, codecname
))
223 if (vfm
&& strcmp(sh_video
->codec
->drv
, vfm
))
224 continue; // vfm doesn't match
225 if (!force
&& sh_video
->codec
->status
< status
)
226 continue; // too unstable
227 stringset_add(selected
, sh_video
->codec
->name
); // tagging it
228 // ok, it matches all rules, let's find the driver!
229 for (i
= 0; mpcodecs_vd_drivers
[i
] != NULL
; i
++)
230 if (!strcmp(mpcodecs_vd_drivers
[i
]->info
->short_name
,
231 sh_video
->codec
->drv
))
233 sh_video
->vd_driver
= mpcodecs_vd_drivers
[i
];
234 #ifdef CONFIG_DYNAMIC_PLUGINS
235 if (!sh_video
->vd_driver
) {
236 /* try to open shared decoder plugin */
239 vd_functions_t
*funcs_sym
;
243 strlen(MPLAYER_LIBDIR
) + strlen(sh_video
->codec
->drv
) + 16;
244 buf
= malloc(buf_len
);
247 snprintf(buf
, buf_len
, "%s/mplayer/vd_%s.so", MPLAYER_LIBDIR
,
248 sh_video
->codec
->drv
);
249 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
,
250 "Trying to open external plugin: %s\n", buf
);
251 sh_video
->dec_handle
= dlopen(buf
, RTLD_LAZY
);
252 if (!sh_video
->dec_handle
)
254 snprintf(buf
, buf_len
, "mpcodecs_vd_%s", sh_video
->codec
->drv
);
255 funcs_sym
= dlsym(sh_video
->dec_handle
, buf
);
256 if (!funcs_sym
|| !funcs_sym
->info
|| !funcs_sym
->init
257 || !funcs_sym
->uninit
|| !funcs_sym
->control
258 || !funcs_sym
->decode
)
260 info_sym
= funcs_sym
->info
;
261 if (strcmp(info_sym
->short_name
, sh_video
->codec
->drv
))
264 sh_video
->vd_driver
= funcs_sym
;
265 mp_msg(MSGT_DECVIDEO
, MSGL_V
,
266 "Using external decoder plugin (%s/mplayer/vd_%s.so)!\n",
267 MPLAYER_LIBDIR
, sh_video
->codec
->drv
);
270 if (!sh_video
->vd_driver
) { // driver not available (==compiled in)
271 mp_tmsg(MSGT_DECVIDEO
, MSGL_WARN
,
272 _("Requested video codec family [%s] (vfm=%s) not available.\nEnable it at compilation.\n"),
273 sh_video
->codec
->name
, sh_video
->codec
->drv
);
276 orig_w
= sh_video
->bih
? sh_video
->bih
->biWidth
: sh_video
->disp_w
;
277 orig_h
= sh_video
->bih
? sh_video
->bih
->biHeight
: sh_video
->disp_h
;
278 sh_video
->disp_w
= orig_w
;
279 sh_video
->disp_h
= orig_h
;
280 // it's available, let's try to init!
281 if (sh_video
->codec
->flags
& CODECS_FLAG_ALIGN16
) {
282 // align width/height to n*16
283 sh_video
->disp_w
= (sh_video
->disp_w
+ 15) & (~15);
284 sh_video
->disp_h
= (sh_video
->disp_h
+ 15) & (~15);
287 sh_video
->bih
->biWidth
= sh_video
->disp_w
;
288 sh_video
->bih
->biHeight
= sh_video
->disp_h
;
292 const struct vd_functions
*vd
= sh_video
->vd_driver
;
293 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "Opening video decoder: [%s] %s\n",
294 vd
->info
->short_name
, vd
->info
->name
);
295 // clear vf init error, it is no longer relevant
296 if (sh_video
->vf_initialized
< 0)
297 sh_video
->vf_initialized
= 0;
298 if (!vd
->init(sh_video
)) {
299 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "VDecoder init failed :(\n");
300 sh_video
->disp_w
= orig_w
;
301 sh_video
->disp_h
= orig_h
;
303 sh_video
->bih
->biWidth
= sh_video
->disp_w
;
304 sh_video
->bih
->biHeight
= sh_video
->disp_h
;
306 continue; // try next...
309 sh_video
->initialized
= 1;
315 int init_best_video_codec(sh_video_t
*sh_video
, char **video_codec_list
,
316 char **video_fm_list
)
318 char *vc_l_default
[2] = { "", (char *) NULL
};
319 stringset_t selected
;
321 if (!video_codec_list
)
322 video_codec_list
= vc_l_default
;
323 // Go through the codec.conf and find the best codec...
324 sh_video
->initialized
= 0;
325 stringset_init(&selected
);
326 while (!sh_video
->initialized
&& *video_codec_list
) {
327 char *video_codec
= *(video_codec_list
++);
328 if (video_codec
[0]) {
329 if (video_codec
[0] == '-') {
330 // disable this codec:
331 stringset_add(&selected
, video_codec
+ 1);
333 // forced codec by name:
334 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "Forced video codec: %s\n",
336 init_video(sh_video
, video_codec
, NULL
, -1, &selected
);
340 // try in stability order: UNTESTED, WORKING, BUGGY. never try CRASHING.
342 char **fmlist
= video_fm_list
;
343 // try first the preferred codec families:
344 while (!sh_video
->initialized
&& *fmlist
) {
345 char *video_fm
= *(fmlist
++);
346 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "Trying to force video codec driver family %s...\n",
348 for (status
= CODECS_STATUS__MAX
;
349 status
>= CODECS_STATUS__MIN
; --status
)
351 (sh_video
, NULL
, video_fm
, status
, &selected
))
355 if (!sh_video
->initialized
)
356 for (status
= CODECS_STATUS__MAX
; status
>= CODECS_STATUS__MIN
;
358 if (init_video(sh_video
, NULL
, NULL
, status
, &selected
))
362 stringset_free(&selected
);
364 if (!sh_video
->initialized
) {
365 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "Cannot find codec matching selected -vo and video format 0x%X.\n",
370 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "Selected video codec: [%s] vfm: %s (%s)\n",
371 sh_video
->codec
->name
, sh_video
->codec
->drv
, sh_video
->codec
->info
);
375 void *decode_video(sh_video_t
*sh_video
, unsigned char *start
, int in_size
,
376 int drop_frame
, double pts
)
378 mp_image_t
*mpi
= NULL
;
379 unsigned int t
= GetTimer();
382 struct MPOpts
*opts
= sh_video
->opts
;
384 if (opts
->correct_pts
&& pts
!= MP_NOPTS_VALUE
) {
385 int delay
= get_current_video_decoder_lag(sh_video
);
387 if (delay
> sh_video
->num_buffered_pts
)
389 // this is disabled because vd_ffmpeg reports the same lag
390 // after seek even when there are no buffered frames,
391 // leading to incorrect error messages
392 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Not enough buffered pts\n");
397 sh_video
->num_buffered_pts
= delay
;
399 if (sh_video
->num_buffered_pts
==
400 sizeof(sh_video
->buffered_pts
) / sizeof(double))
401 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Too many buffered pts\n");
404 for (i
= 0; i
< sh_video
->num_buffered_pts
; i
++)
405 if (sh_video
->buffered_pts
[i
] < pts
)
407 for (j
= sh_video
->num_buffered_pts
; j
> i
; j
--)
408 sh_video
->buffered_pts
[j
] = sh_video
->buffered_pts
[j
- 1];
409 sh_video
->buffered_pts
[i
] = pts
;
410 sh_video
->num_buffered_pts
++;
414 mpi
= sh_video
->vd_driver
->decode(sh_video
, start
, in_size
, drop_frame
);
416 //------------------------ frame decoded. --------------------
419 // some codecs are broken, and doesn't restore MMX state :(
420 // it happens usually with broken/damaged files.
421 if (gCpuCaps
.has3DNow
) {
422 __asm__
volatile("femms\n\t":::"memory");
423 } else if (gCpuCaps
.hasMMX
) {
424 __asm__
volatile("emms\n\t":::"memory");
431 video_time_usage
+= tt
;
433 if (!mpi
|| drop_frame
)
434 return NULL
; // error / skipped frame
436 if (field_dominance
== 0)
437 mpi
->fields
|= MP_IMGFIELD_TOP_FIRST
;
438 else if (field_dominance
== 1)
439 mpi
->fields
&= ~MP_IMGFIELD_TOP_FIRST
;
441 if (opts
->correct_pts
) {
442 if (sh_video
->num_buffered_pts
) {
443 sh_video
->num_buffered_pts
--;
444 sh_video
->pts
= sh_video
->buffered_pts
[sh_video
->num_buffered_pts
];
446 mp_msg(MSGT_CPLAYER
, MSGL_ERR
,
447 "No pts value from demuxer to " "use for frame!\n");
448 sh_video
->pts
= MP_NOPTS_VALUE
;
454 int filter_video(sh_video_t
*sh_video
, void *frame
, double pts
)
456 mp_image_t
*mpi
= frame
;
457 unsigned int t2
= GetTimer();
458 vf_instance_t
*vf
= sh_video
->vfilter
;
459 // apply video filters and call the leaf vo/ve
460 int ret
= vf
->put_image(vf
, mpi
, pts
);
462 t2
= GetTimer() - t2
;
463 vout_time_usage
+= t2
* 0.000001;