Support chapter seeking with ordered chapters
[mplayer.git] / libmpcodecs / dec_video.c
blob7faf2ac39609eb3140a0cbfcaa52106b6e2b005d
1 #include "config.h"
2 #include "options.h"
4 #include <stdio.h>
5 #if HAVE_MALLOC_H
6 #include <malloc.h>
7 #endif
8 #include <stdlib.h>
9 #include <stdbool.h>
10 #include <unistd.h>
12 #include "mp_msg.h"
13 #include "help_mp.h"
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"
27 #include "vd.h"
28 #include "vf.h"
30 #include "dec_video.h"
32 #ifdef CONFIG_DYNAMIC_PLUGINS
33 #include <dlfcn.h>
34 #endif
36 // ===================================================================
38 extern double video_time_usage;
39 extern double vout_time_usage;
41 #include "cpudetect.h"
43 int field_dominance = -1;
45 int divx_quality = 0;
47 int get_video_quality_max(sh_video_t *sh_video)
49 vf_instance_t *vf = sh_video->vfilter;
50 if (vf) {
51 int ret = vf->control(vf, VFCTRL_QUERY_MAX_PP_LEVEL, NULL);
52 if (ret > 0) {
53 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_UsingExternalPP, ret);
54 return ret;
57 const struct vd_functions *vd = sh_video->vd_driver;
58 if (vd) {
59 int ret = vd->control(sh_video, VDCTRL_QUERY_MAX_PP_LEVEL, NULL);
60 if (ret > 0) {
61 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_UsingCodecPP, ret);
62 return ret;
65 return 0;
68 void set_video_quality(sh_video_t *sh_video, int quality)
70 vf_instance_t *vf = sh_video->vfilter;
71 if (vf) {
72 int ret = vf->control(vf, VFCTRL_SET_PP_LEVEL, (void *) (&quality));
73 if (ret == CONTROL_TRUE)
74 return; // success
76 const struct vd_functions *vd = sh_video->vd_driver;
77 if (vd)
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;
84 vf_equalizer_t data;
86 data.item = item;
87 data.value = value;
89 mp_dbg(MSGT_DECVIDEO, MSGL_V, "set video colors %s=%d \n", item, value);
90 if (vf) {
91 int ret = vf->control(vf, VFCTRL_SET_EQUALIZER, &data);
92 if (ret == CONTROL_TRUE)
93 return 1;
95 /* try software control */
96 const struct vd_functions *vd = sh_video->vd_driver;
97 if (vd &&
98 vd->control(sh_video, VDCTRL_SET_EQUALIZER, item, (int *) value)
99 == CONTROL_OK)
100 return 1;
101 mp_msg(MSGT_DECVIDEO, MSGL_V, MSGTR_VideoAttributeNotSupportedByVO_VD,
102 item);
103 return 0;
106 int get_video_colors(sh_video_t *sh_video, const char *item, int *value)
108 vf_instance_t *vf = sh_video->vfilter;
109 vf_equalizer_t data;
111 data.item = item;
113 mp_dbg(MSGT_DECVIDEO, MSGL_V, "get video colors %s \n", item);
114 if (vf) {
115 int ret = vf->control(vf, VFCTRL_GET_EQUALIZER, &data);
116 if (ret == CONTROL_TRUE) {
117 *value = data.value;
118 return 1;
121 /* try software control */
122 const struct vd_functions *vd = sh_video->vd_driver;
123 if (vd)
124 return vd->control(sh_video, VDCTRL_GET_EQUALIZER, item, value);
125 return 0;
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");
134 if (vf) {
135 int ret = vf->control(vf, VFCTRL_CHANGE_RECTANGLE, data);
136 if (ret)
137 return 1;
139 return 0;
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)
146 return 0;
147 return -1;
150 void resync_video_stream(sh_video_t *sh_video)
152 const struct vd_functions *vd = sh_video->vd_driver;
153 if (vd)
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;
160 if (!vd)
161 return -1;
162 int ret = vd->control(sh_video, VDCTRL_QUERY_UNSEEN_FRAMES, NULL);
163 if (ret >= 10)
164 return ret - 10;
165 return -1;
168 void uninit_video(sh_video_t *sh_video)
170 if (!sh_video->initialized)
171 return;
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);
177 #endif
178 vf_uninit_filter_chain(sh_video->vfilter);
179 sh_video->initialized = 0;
182 void vfm_help(void)
184 int i;
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)
198 int force = 0;
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];
205 force = 1;
208 while (1) {
209 int i;
210 int orig_w, orig_h;
211 // restore original fourcc:
212 if (sh_video->bih)
213 sh_video->bih->biCompression = orig_fourcc;
214 if (!
215 (sh_video->codec =
216 find_video_codec(sh_video->format,
217 sh_video->bih ? ((unsigned int *) &sh_video->
218 bih->biCompression) : NULL,
219 sh_video->codec, force)))
220 break;
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))
225 continue; // -vc
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))
235 break;
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 */
240 int buf_len;
241 char *buf;
242 vd_functions_t *funcs_sym;
243 vd_info_t *info_sym;
245 buf_len =
246 strlen(MPLAYER_LIBDIR) + strlen(sh_video->codec->drv) + 16;
247 buf = malloc(buf_len);
248 if (!buf)
249 break;
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)
256 break;
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)
262 break;
263 info_sym = funcs_sym->info;
264 if (strcmp(info_sym->short_name, sh_video->codec->drv))
265 break;
266 free(buf);
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);
272 #endif
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);
277 continue;
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);
289 if (sh_video->bih) {
290 sh_video->bih->biWidth = sh_video->disp_w;
291 sh_video->bih->biHeight = sh_video->disp_h;
294 // init()
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;
305 if (sh_video->bih) {
306 sh_video->bih->biWidth = sh_video->disp_w;
307 sh_video->bih->biHeight = sh_video->disp_h;
309 continue; // try next...
311 // Yeah! We got it!
312 sh_video->initialized = 1;
313 return 1;
315 return 0;
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;
323 // hack:
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);
335 } else {
336 // forced codec by name:
337 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_ForcedVideoCodec,
338 video_codec);
339 init_video(sh_video, video_codec, NULL, -1, &selected);
341 } else {
342 int status;
343 // try in stability order: UNTESTED, WORKING, BUGGY. never try CRASHING.
344 if (video_fm_list) {
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,
350 video_fm);
351 for (status = CODECS_STATUS__MAX;
352 status >= CODECS_STATUS__MIN; --status)
353 if (init_video
354 (sh_video, NULL, video_fm, status, &selected))
355 break;
358 if (!sh_video->initialized)
359 for (status = CODECS_STATUS__MAX; status >= CODECS_STATUS__MIN;
360 --status)
361 if (init_video(sh_video, NULL, NULL, status, &selected))
362 break;
365 stringset_free(&selected);
367 if (!sh_video->initialized) {
368 mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_CantFindVideoCodec,
369 sh_video->format);
370 mp_msg(MSGT_DECAUDIO, MSGL_HINT, MSGTR_RTFMCodecs);
371 return 0; // failed
374 mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_SelectedVideoCodec,
375 sh_video->codec->name, sh_video->codec->drv, sh_video->codec->info);
376 return 1; // success
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();
384 unsigned int t2;
385 double tt;
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);
390 if (delay >= 0) {
391 if (delay > sh_video->num_buffered_pts)
392 #if 0
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");
397 #else
399 #endif
400 else
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");
406 else {
407 int i, j;
408 for (i = 0; i < sh_video->num_buffered_pts; i++)
409 if (sh_video->buffered_pts[i] < pts)
410 break;
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. --------------------
422 #if HAVE_MMX
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");
430 #endif
432 t2 = GetTimer();
433 t = t2 - t;
434 tt = t * 0.000001f;
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];
449 } else {
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;
455 return mpi;
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;
469 return ret;