Remove HAVE_MATRIXVIEW references
[mplayer/kovensky.git] / libmpcodecs / dec_video.c
blobe010715c4ae3f58b870c0b5cd843d08018809db3
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_tmsg(MSGT_DECVIDEO, MSGL_INFO, "[PP] Using external postprocessing filter, max q = %d.\n", 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_tmsg(MSGT_DECVIDEO, MSGL_INFO, "[PP] Using codec's postprocessing, max q = %d.\n", 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_tmsg(MSGT_DECVIDEO, MSGL_V, "Video attribute '%s' is not supported by selected vo & vd.\n",
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);
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;
159 if (!vd)
160 return -1;
161 int ret = vd->control(sh_video, VDCTRL_QUERY_UNSEEN_FRAMES, NULL);
162 if (ret >= 10)
163 return ret - 10;
164 return -1;
167 void uninit_video(sh_video_t *sh_video)
169 if (!sh_video->initialized)
170 return;
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);
176 #endif
177 vf_uninit_filter_chain(sh_video->vfilter);
178 sh_video->initialized = 0;
181 void vfm_help(void)
183 int i;
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)
197 int force = 0;
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];
204 force = 1;
207 while (1) {
208 int i;
209 int orig_w, orig_h;
210 // restore original fourcc:
211 if (sh_video->bih)
212 sh_video->bih->biCompression = orig_fourcc;
213 if (!
214 (sh_video->codec =
215 find_video_codec(sh_video->format,
216 sh_video->bih ? ((unsigned int *) &sh_video->
217 bih->biCompression) : NULL,
218 sh_video->codec, force)))
219 break;
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))
224 continue; // -vc
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))
234 break;
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 */
239 int buf_len;
240 char *buf;
241 vd_functions_t *funcs_sym;
242 vd_info_t *info_sym;
244 buf_len =
245 strlen(MPLAYER_LIBDIR) + strlen(sh_video->codec->drv) + 16;
246 buf = malloc(buf_len);
247 if (!buf)
248 break;
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)
255 break;
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)
261 break;
262 info_sym = funcs_sym->info;
263 if (strcmp(info_sym->short_name, sh_video->codec->drv))
264 break;
265 free(buf);
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);
271 #endif
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);
276 continue;
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);
288 if (sh_video->bih) {
289 sh_video->bih->biWidth = sh_video->disp_w;
290 sh_video->bih->biHeight = sh_video->disp_h;
293 // init()
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;
304 if (sh_video->bih) {
305 sh_video->bih->biWidth = sh_video->disp_w;
306 sh_video->bih->biHeight = sh_video->disp_h;
308 continue; // try next...
310 // Yeah! We got it!
311 sh_video->initialized = 1;
312 sh_video->prev_codec_reordered_pts = MP_NOPTS_VALUE;
313 sh_video->prev_sorted_pts = MP_NOPTS_VALUE;
314 return 1;
316 return 0;
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;
324 // hack:
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);
336 } else {
337 // forced codec by name:
338 mp_tmsg(MSGT_DECVIDEO, MSGL_INFO, "Forced video codec: %s\n",
339 video_codec);
340 init_video(sh_video, video_codec, NULL, -1, &selected);
342 } else {
343 int status;
344 // try in stability order: UNTESTED, WORKING, BUGGY. never try CRASHING.
345 if (video_fm_list) {
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",
351 video_fm);
352 for (status = CODECS_STATUS__MAX;
353 status >= CODECS_STATUS__MIN; --status)
354 if (init_video
355 (sh_video, NULL, video_fm, status, &selected))
356 break;
359 if (!sh_video->initialized)
360 for (status = CODECS_STATUS__MAX; status >= CODECS_STATUS__MIN;
361 --status)
362 if (init_video(sh_video, NULL, NULL, status, &selected))
363 break;
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",
370 sh_video->format);
371 return 0; // failed
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);
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 if (sh_video->vd_driver->decode2) {
419 mpi = sh_video->vd_driver->decode2(sh_video, start, in_size,
420 drop_frame, &pts);
421 } else {
422 mpi = sh_video->vd_driver->decode(sh_video, start, in_size,
423 drop_frame);
424 pts = MP_NOPTS_VALUE;
427 //------------------------ frame decoded. --------------------
429 #if HAVE_MMX
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");
437 #endif
439 t2 = GetTimer();
440 t = t2 - t;
441 tt = t * 0.000001f;
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];
464 } else {
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++;
474 return mpi;
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;
488 return ret;