core: Set mpctx->chapters to NULL at uninit
[mplayer.git] / libmpcodecs / dec_video.c
blob95297b2117bcac3021352e373dfa8966eb2e225a
1 #include "config.h"
2 #include "options.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdbool.h>
7 #include <unistd.h>
9 #include "mp_msg.h"
10 #include "help_mp.h"
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"
24 #include "vd.h"
25 #include "vf.h"
27 #include "dec_video.h"
29 #ifdef CONFIG_DYNAMIC_PLUGINS
30 #include <dlfcn.h>
31 #endif
33 // ===================================================================
35 extern double video_time_usage;
36 extern double vout_time_usage;
38 #include "cpudetect.h"
40 int field_dominance = -1;
42 int divx_quality = 0;
44 int get_video_quality_max(sh_video_t *sh_video)
46 vf_instance_t *vf = sh_video->vfilter;
47 if (vf) {
48 int ret = vf->control(vf, VFCTRL_QUERY_MAX_PP_LEVEL, NULL);
49 if (ret > 0) {
50 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_UsingExternalPP, ret);
51 return ret;
54 const struct vd_functions *vd = sh_video->vd_driver;
55 if (vd) {
56 int ret = vd->control(sh_video, VDCTRL_QUERY_MAX_PP_LEVEL, NULL);
57 if (ret > 0) {
58 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_UsingCodecPP, ret);
59 return ret;
62 return 0;
65 void set_video_quality(sh_video_t *sh_video, int quality)
67 vf_instance_t *vf = sh_video->vfilter;
68 if (vf) {
69 int ret = vf->control(vf, VFCTRL_SET_PP_LEVEL, (void *) (&quality));
70 if (ret == CONTROL_TRUE)
71 return; // success
73 const struct vd_functions *vd = sh_video->vd_driver;
74 if (vd)
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;
81 vf_equalizer_t data;
83 data.item = item;
84 data.value = value;
86 mp_dbg(MSGT_DECVIDEO, MSGL_V, "set video colors %s=%d \n", item, value);
87 if (vf) {
88 int ret = vf->control(vf, VFCTRL_SET_EQUALIZER, &data);
89 if (ret == CONTROL_TRUE)
90 return 1;
92 /* try software control */
93 const struct vd_functions *vd = sh_video->vd_driver;
94 if (vd &&
95 vd->control(sh_video, VDCTRL_SET_EQUALIZER, item, (int *) value)
96 == CONTROL_OK)
97 return 1;
98 mp_msg(MSGT_DECVIDEO, MSGL_V, MSGTR_VideoAttributeNotSupportedByVO_VD,
99 item);
100 return 0;
103 int get_video_colors(sh_video_t *sh_video, const char *item, int *value)
105 vf_instance_t *vf = sh_video->vfilter;
106 vf_equalizer_t data;
108 data.item = item;
110 mp_dbg(MSGT_DECVIDEO, MSGL_V, "get video colors %s \n", item);
111 if (vf) {
112 int ret = vf->control(vf, VFCTRL_GET_EQUALIZER, &data);
113 if (ret == CONTROL_TRUE) {
114 *value = data.value;
115 return 1;
118 /* try software control */
119 const struct vd_functions *vd = sh_video->vd_driver;
120 if (vd)
121 return vd->control(sh_video, VDCTRL_GET_EQUALIZER, item, value);
122 return 0;
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");
131 if (vf) {
132 int ret = vf->control(vf, VFCTRL_CHANGE_RECTANGLE, data);
133 if (ret)
134 return 1;
136 return 0;
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)
143 return 0;
144 return -1;
147 void resync_video_stream(sh_video_t *sh_video)
149 const struct vd_functions *vd = sh_video->vd_driver;
150 if (vd)
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;
157 if (!vd)
158 return -1;
159 int ret = vd->control(sh_video, VDCTRL_QUERY_UNSEEN_FRAMES, NULL);
160 if (ret >= 10)
161 return ret - 10;
162 return -1;
165 void uninit_video(sh_video_t *sh_video)
167 if (!sh_video->initialized)
168 return;
169 mp_msg(MSGT_DECVIDEO, MSGL_V, MSGTR_UninitVideoStr, 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);
174 #endif
175 vf_uninit_filter_chain(sh_video->vfilter);
176 sh_video->initialized = 0;
179 void vfm_help(void)
181 int i;
182 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_AvailableVideoFm);
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)
195 int force = 0;
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];
202 force = 1;
205 while (1) {
206 int i;
207 int orig_w, orig_h;
208 // restore original fourcc:
209 if (sh_video->bih)
210 sh_video->bih->biCompression = orig_fourcc;
211 if (!
212 (sh_video->codec =
213 find_video_codec(sh_video->format,
214 sh_video->bih ? ((unsigned int *) &sh_video->
215 bih->biCompression) : NULL,
216 sh_video->codec, force)))
217 break;
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))
222 continue; // -vc
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))
232 break;
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 */
237 int buf_len;
238 char *buf;
239 vd_functions_t *funcs_sym;
240 vd_info_t *info_sym;
242 buf_len =
243 strlen(MPLAYER_LIBDIR) + strlen(sh_video->codec->drv) + 16;
244 buf = malloc(buf_len);
245 if (!buf)
246 break;
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)
253 break;
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)
259 break;
260 info_sym = funcs_sym->info;
261 if (strcmp(info_sym->short_name, sh_video->codec->drv))
262 break;
263 free(buf);
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);
269 #endif
270 if (!sh_video->vd_driver) { // driver not available (==compiled in)
271 mp_msg(MSGT_DECVIDEO, MSGL_WARN,
272 MSGTR_VideoCodecFamilyNotAvailableStr,
273 sh_video->codec->name, sh_video->codec->drv);
274 continue;
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);
286 if (sh_video->bih) {
287 sh_video->bih->biWidth = sh_video->disp_w;
288 sh_video->bih->biHeight = sh_video->disp_h;
291 // init()
292 const struct vd_functions *vd = sh_video->vd_driver;
293 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_OpeningVideoDecoder,
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_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_VDecoderInitFailed);
300 sh_video->disp_w = orig_w;
301 sh_video->disp_h = orig_h;
302 if (sh_video->bih) {
303 sh_video->bih->biWidth = sh_video->disp_w;
304 sh_video->bih->biHeight = sh_video->disp_h;
306 continue; // try next...
308 // Yeah! We got it!
309 sh_video->initialized = 1;
310 return 1;
312 return 0;
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;
320 // hack:
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);
332 } else {
333 // forced codec by name:
334 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_ForcedVideoCodec,
335 video_codec);
336 init_video(sh_video, video_codec, NULL, -1, &selected);
338 } else {
339 int status;
340 // try in stability order: UNTESTED, WORKING, BUGGY. never try CRASHING.
341 if (video_fm_list) {
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_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_TryForceVideoFmtStr,
347 video_fm);
348 for (status = CODECS_STATUS__MAX;
349 status >= CODECS_STATUS__MIN; --status)
350 if (init_video
351 (sh_video, NULL, video_fm, status, &selected))
352 break;
355 if (!sh_video->initialized)
356 for (status = CODECS_STATUS__MAX; status >= CODECS_STATUS__MIN;
357 --status)
358 if (init_video(sh_video, NULL, NULL, status, &selected))
359 break;
362 stringset_free(&selected);
364 if (!sh_video->initialized) {
365 mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_CantFindVideoCodec,
366 sh_video->format);
367 mp_msg(MSGT_DECAUDIO, MSGL_HINT, MSGTR_RTFMCodecs);
368 return 0; // failed
371 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_SelectedVideoCodec,
372 sh_video->codec->name, sh_video->codec->drv, sh_video->codec->info);
373 return 1; // success
376 void *decode_video(sh_video_t *sh_video, unsigned char *start, int in_size,
377 int drop_frame, double pts)
379 mp_image_t *mpi = NULL;
380 unsigned int t = GetTimer();
381 unsigned int t2;
382 double tt;
383 struct MPOpts *opts = sh_video->opts;
385 if (opts->correct_pts && pts != MP_NOPTS_VALUE) {
386 int delay = get_current_video_decoder_lag(sh_video);
387 if (delay >= 0) {
388 if (delay > sh_video->num_buffered_pts)
389 #if 0
390 // this is disabled because vd_ffmpeg reports the same lag
391 // after seek even when there are no buffered frames,
392 // leading to incorrect error messages
393 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Not enough buffered pts\n");
394 #else
396 #endif
397 else
398 sh_video->num_buffered_pts = delay;
400 if (sh_video->num_buffered_pts ==
401 sizeof(sh_video->buffered_pts) / sizeof(double))
402 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Too many buffered pts\n");
403 else {
404 int i, j;
405 for (i = 0; i < sh_video->num_buffered_pts; i++)
406 if (sh_video->buffered_pts[i] < pts)
407 break;
408 for (j = sh_video->num_buffered_pts; j > i; j--)
409 sh_video->buffered_pts[j] = sh_video->buffered_pts[j - 1];
410 sh_video->buffered_pts[i] = pts;
411 sh_video->num_buffered_pts++;
415 mpi = sh_video->vd_driver->decode(sh_video, start, in_size, drop_frame);
417 //------------------------ frame decoded. --------------------
419 #if HAVE_MMX
420 // some codecs are broken, and doesn't restore MMX state :(
421 // it happens usually with broken/damaged files.
422 if (gCpuCaps.has3DNow) {
423 __asm__ volatile("femms\n\t":::"memory");
424 } else if (gCpuCaps.hasMMX) {
425 __asm__ volatile("emms\n\t":::"memory");
427 #endif
429 t2 = GetTimer();
430 t = t2 - t;
431 tt = t * 0.000001f;
432 video_time_usage += tt;
434 if (!mpi || drop_frame)
435 return NULL; // error / skipped frame
437 if (field_dominance == 0)
438 mpi->fields |= MP_IMGFIELD_TOP_FIRST;
439 else if (field_dominance == 1)
440 mpi->fields &= ~MP_IMGFIELD_TOP_FIRST;
442 if (opts->correct_pts) {
443 if (sh_video->num_buffered_pts) {
444 sh_video->num_buffered_pts--;
445 sh_video->pts = sh_video->buffered_pts[sh_video->num_buffered_pts];
446 } else {
447 mp_msg(MSGT_CPLAYER, MSGL_ERR,
448 "No pts value from demuxer to " "use for frame!\n");
449 sh_video->pts = MP_NOPTS_VALUE;
452 return mpi;
455 int filter_video(sh_video_t *sh_video, void *frame, double pts)
457 mp_image_t *mpi = frame;
458 unsigned int t2 = GetTimer();
459 vf_instance_t *vf = sh_video->vfilter;
460 // apply video filters and call the leaf vo/ve
461 int ret = vf->put_image(vf, mpi, pts);
463 t2 = GetTimer() - t2;
464 vout_time_usage += t2 * 0.000001;
466 return ret;