Only compile fastmemcpybench on x86.
[mplayer/glamo.git] / libmpcodecs / dec_video.c
blob4186bd5e728552889a7702175c55ce6a60f303cd
2 #include "config.h"
4 #include <stdio.h>
5 #if HAVE_MALLOC_H
6 #include <malloc.h>
7 #endif
8 #include <stdlib.h>
9 #include <unistd.h>
11 #include "mp_msg.h"
12 #include "help_mp.h"
14 #include "osdep/timer.h"
15 #include "osdep/shmem.h"
17 #include "stream/stream.h"
18 #include "libmpdemux/demuxer.h"
19 #include "libmpdemux/parse_es.h"
21 #include "codec-cfg.h"
23 #include "libvo/video_out.h"
25 #include "libmpdemux/stheader.h"
26 #include "vd.h"
27 #include "vf.h"
29 #include "dec_video.h"
31 #ifdef CONFIG_DYNAMIC_PLUGINS
32 #include <dlfcn.h>
33 #endif
35 // ===================================================================
37 extern double video_time_usage;
38 extern double vout_time_usage;
40 #include "cpudetect.h"
42 int field_dominance=-1;
44 int divx_quality=0;
46 vd_functions_t* mpvdec=NULL;
48 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 if(mpvdec){
58 int ret=mpvdec->control(sh_video,VDCTRL_QUERY_MAX_PP_LEVEL,NULL);
59 if(ret>0){
60 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_UsingCodecPP,ret);
61 return ret;
64 // mp_msg(MSGT_DECVIDEO,MSGL_INFO,"[PP] Sorry, postprocessing is not available\n");
65 return 0;
68 void set_video_quality(sh_video_t *sh_video,int quality){
69 vf_instance_t* vf=sh_video->vfilter;
70 if(vf){
71 int ret=vf->control(vf,VFCTRL_SET_PP_LEVEL, (void*)(&quality));
72 if(ret==CONTROL_TRUE) return; // success
74 if(mpvdec)
75 mpvdec->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)
89 int ret = vf->control(vf, VFCTRL_SET_EQUALIZER, &data);
90 if (ret == CONTROL_TRUE)
91 return 1;
93 /* try software control */
94 if(mpvdec)
95 if( mpvdec->control(sh_video,VDCTRL_SET_EQUALIZER, item, (int *)value)
96 == CONTROL_OK) return 1;
97 mp_msg(MSGT_DECVIDEO,MSGL_V,MSGTR_VideoAttributeNotSupportedByVO_VD,item);
98 return 0;
101 int get_video_colors(sh_video_t *sh_video,const char *item,int *value)
103 vf_instance_t* vf=sh_video->vfilter;
104 vf_equalizer_t data;
106 data.item = item;
108 mp_dbg(MSGT_DECVIDEO,MSGL_V,"get video colors %s \n", item);
109 if (vf)
111 int ret = vf->control(vf, VFCTRL_GET_EQUALIZER, &data);
112 if (ret == CONTROL_TRUE){
113 *value = data.value;
114 return 1;
117 /* try software control */
118 if(mpvdec) return mpvdec->control(sh_video,VDCTRL_GET_EQUALIZER, item, value);
119 return 0;
122 int set_rectangle(sh_video_t *sh_video,int param,int value)
124 vf_instance_t* vf=sh_video->vfilter;
125 int data[] = {param, value};
127 mp_dbg(MSGT_DECVIDEO,MSGL_V,"set rectangle \n");
128 if (vf)
130 int ret = vf->control(vf, VFCTRL_CHANGE_RECTANGLE, data);
131 if (ret)
132 return 1;
134 return 0;
137 void resync_video_stream(sh_video_t *sh_video)
139 if(mpvdec) mpvdec->control(sh_video, VDCTRL_RESYNC_STREAM, NULL);
142 int get_current_video_decoder_lag(sh_video_t *sh_video)
144 int ret;
146 if (!mpvdec)
147 return -1;
148 ret = mpvdec->control(sh_video, VDCTRL_QUERY_UNSEEN_FRAMES, NULL);
149 if (ret >= 10)
150 return ret-10;
151 return -1;
154 void uninit_video(sh_video_t *sh_video){
155 if(!sh_video->initialized) return;
156 mp_msg(MSGT_DECVIDEO,MSGL_V,MSGTR_UninitVideoStr,sh_video->codec->drv);
157 mpvdec->uninit(sh_video);
158 #ifdef CONFIG_DYNAMIC_PLUGINS
159 if (sh_video->dec_handle)
160 dlclose(sh_video->dec_handle);
161 #endif
162 vf_uninit_filter_chain(sh_video->vfilter);
163 sh_video->initialized=0;
166 void vfm_help(void){
167 int i;
168 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_AvailableVideoFm);
169 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_DRIVERS\n");
170 mp_msg(MSGT_DECVIDEO,MSGL_INFO," vfm: info: (comment)\n");
171 for (i=0; mpcodecs_vd_drivers[i] != NULL; i++)
172 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"%8s %s (%s)\n",
173 mpcodecs_vd_drivers[i]->info->short_name,
174 mpcodecs_vd_drivers[i]->info->name,
175 mpcodecs_vd_drivers[i]->info->comment);
178 static int init_video(sh_video_t *sh_video,char* codecname,char* vfm,int status,
179 stringset_t *selected){
180 int force = 0;
181 unsigned int orig_fourcc=sh_video->bih?sh_video->bih->biCompression:0;
182 sh_video->codec=NULL;
183 sh_video->vf_initialized=0;
184 if (codecname && codecname[0] == '+') {
185 codecname = &codecname[1];
186 force = 1;
189 while(1){
190 int i;
191 int orig_w, orig_h;
192 // restore original fourcc:
193 if(sh_video->bih) sh_video->bih->biCompression=orig_fourcc;
194 if(!(sh_video->codec=find_video_codec(sh_video->format,
195 sh_video->bih?((unsigned int*) &sh_video->bih->biCompression):NULL,
196 sh_video->codec,force) )) break;
197 // ok we found one codec
198 if(stringset_test(selected, sh_video->codec->name)) continue; // already tried & failed
199 if(codecname && strcmp(sh_video->codec->name,codecname)) continue; // -vc
200 if(vfm && strcmp(sh_video->codec->drv,vfm)) continue; // vfm doesn't match
201 if(!force && sh_video->codec->status<status) continue; // too unstable
202 stringset_add(selected, sh_video->codec->name); // tagging it
203 // ok, it matches all rules, let's find the driver!
204 for (i=0; mpcodecs_vd_drivers[i] != NULL; i++)
205 // if(mpcodecs_vd_drivers[i]->info->id==sh_video->codec->driver) break;
206 if(!strcmp(mpcodecs_vd_drivers[i]->info->short_name,sh_video->codec->drv)) break;
207 mpvdec=mpcodecs_vd_drivers[i];
208 #ifdef CONFIG_DYNAMIC_PLUGINS
209 if (!mpvdec)
211 /* try to open shared decoder plugin */
212 int buf_len;
213 char *buf;
214 vd_functions_t *funcs_sym;
215 vd_info_t *info_sym;
217 buf_len = strlen(MPLAYER_LIBDIR)+strlen(sh_video->codec->drv)+16;
218 buf = malloc(buf_len);
219 if (!buf)
220 break;
221 snprintf(buf, buf_len, "%s/mplayer/vd_%s.so", MPLAYER_LIBDIR, sh_video->codec->drv);
222 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "Trying to open external plugin: %s\n", buf);
223 sh_video->dec_handle = dlopen(buf, RTLD_LAZY);
224 if (!sh_video->dec_handle)
225 break;
226 snprintf(buf, buf_len, "mpcodecs_vd_%s", sh_video->codec->drv);
227 funcs_sym = dlsym(sh_video->dec_handle, buf);
228 if (!funcs_sym || !funcs_sym->info || !funcs_sym->init ||
229 !funcs_sym->uninit || !funcs_sym->control || !funcs_sym->decode)
230 break;
231 info_sym = funcs_sym->info;
232 if (strcmp(info_sym->short_name, sh_video->codec->drv))
233 break;
234 free(buf);
235 mpvdec = funcs_sym;
236 mp_msg(MSGT_DECVIDEO, MSGL_V, "Using external decoder plugin (%s/mplayer/vd_%s.so)!\n",
237 MPLAYER_LIBDIR, sh_video->codec->drv);
239 #endif
240 if(!mpvdec){ // driver not available (==compiled in)
241 mp_msg(MSGT_DECVIDEO,MSGL_WARN,MSGTR_VideoCodecFamilyNotAvailableStr,
242 sh_video->codec->name, sh_video->codec->drv);
243 continue;
245 orig_w = sh_video->bih ? sh_video->bih->biWidth : sh_video->disp_w;
246 orig_h = sh_video->bih ? sh_video->bih->biHeight : sh_video->disp_h;
247 sh_video->disp_w = orig_w;
248 sh_video->disp_h = orig_h;
249 // it's available, let's try to init!
250 if(sh_video->codec->flags & CODECS_FLAG_ALIGN16){
251 // align width/height to n*16
252 sh_video->disp_w=(sh_video->disp_w+15)&(~15);
253 sh_video->disp_h=(sh_video->disp_h+15)&(~15);
255 if (sh_video->bih) {
256 sh_video->bih->biWidth = sh_video->disp_w;
257 sh_video->bih->biHeight = sh_video->disp_h;
259 // init()
260 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_OpeningVideoDecoder,mpvdec->info->short_name,mpvdec->info->name);
261 // clear vf init error, it is no longer relevant
262 if (sh_video->vf_initialized < 0)
263 sh_video->vf_initialized = 0;
264 if(!mpvdec->init(sh_video)){
265 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_VDecoderInitFailed);
266 sh_video->disp_w=orig_w;
267 sh_video->disp_h=orig_h;
268 if (sh_video->bih) {
269 sh_video->bih->biWidth = sh_video->disp_w;
270 sh_video->bih->biHeight = sh_video->disp_h;
272 continue; // try next...
274 // Yeah! We got it!
275 sh_video->initialized=1;
276 return 1;
278 return 0;
281 int init_best_video_codec(sh_video_t *sh_video,char** video_codec_list,char** video_fm_list){
282 char* vc_l_default[2]={"",(char*)NULL};
283 stringset_t selected;
284 // hack:
285 if(!video_codec_list) video_codec_list=vc_l_default;
286 // Go through the codec.conf and find the best codec...
287 sh_video->initialized=0;
288 stringset_init(&selected);
289 while(!sh_video->initialized && *video_codec_list){
290 char* video_codec=*(video_codec_list++);
291 if(video_codec[0]){
292 if(video_codec[0]=='-'){
293 // disable this codec:
294 stringset_add(&selected, video_codec+1);
295 } else {
296 // forced codec by name:
297 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_ForcedVideoCodec,video_codec);
298 init_video(sh_video,video_codec,NULL,-1, &selected);
300 } else {
301 int status;
302 // try in stability order: UNTESTED, WORKING, BUGGY. never try CRASHING.
303 if(video_fm_list){
304 char** fmlist=video_fm_list;
305 // try first the preferred codec families:
306 while(!sh_video->initialized && *fmlist){
307 char* video_fm=*(fmlist++);
308 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_TryForceVideoFmtStr,video_fm);
309 for(status=CODECS_STATUS__MAX;status>=CODECS_STATUS__MIN;--status)
310 if(init_video(sh_video,NULL,video_fm,status, &selected)) break;
313 if(!sh_video->initialized)
314 for(status=CODECS_STATUS__MAX;status>=CODECS_STATUS__MIN;--status)
315 if(init_video(sh_video,NULL,NULL,status, &selected)) break;
318 stringset_free(&selected);
320 if(!sh_video->initialized){
321 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CantFindVideoCodec,sh_video->format);
322 mp_msg(MSGT_DECAUDIO,MSGL_HINT, MSGTR_RTFMCodecs);
323 return 0; // failed
326 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_SelectedVideoCodec,
327 sh_video->codec->name,sh_video->codec->drv,sh_video->codec->info);
328 return 1; // success
331 void *decode_video(sh_video_t *sh_video, unsigned char *start, int in_size,
332 int drop_frame, double pts)
334 mp_image_t *mpi = NULL;
335 unsigned int t = GetTimer();
336 unsigned int t2;
337 double tt;
339 if (correct_pts && pts != MP_NOPTS_VALUE) {
340 int delay = get_current_video_decoder_lag(sh_video);
341 if (delay >= 0) {
342 if (delay > sh_video->num_buffered_pts)
343 #if 0
344 // this is disabled because vd_ffmpeg reports the same lag
345 // after seek even when there are no buffered frames,
346 // leading to incorrect error messages
347 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Not enough buffered pts\n");
348 #else
350 #endif
351 else
352 sh_video->num_buffered_pts = delay;
354 if (sh_video->num_buffered_pts ==
355 sizeof(sh_video->buffered_pts)/sizeof(double))
356 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Too many buffered pts\n");
357 else {
358 int i, j;
359 for (i = 0; i < sh_video->num_buffered_pts; i++)
360 if (sh_video->buffered_pts[i] < pts)
361 break;
362 for (j = sh_video->num_buffered_pts; j > i; j--)
363 sh_video->buffered_pts[j] = sh_video->buffered_pts[j-1];
364 sh_video->buffered_pts[i] = pts;
365 sh_video->num_buffered_pts++;
369 mpi = mpvdec->decode(sh_video, start, in_size, drop_frame);
371 //------------------------ frame decoded. --------------------
373 #if HAVE_MMX
374 // some codecs are broken, and doesn't restore MMX state :(
375 // it happens usually with broken/damaged files.
376 if (gCpuCaps.has3DNow) {
377 __asm__ volatile ("femms\n\t":::"memory");
379 else if (gCpuCaps.hasMMX) {
380 __asm__ volatile ("emms\n\t":::"memory");
382 #endif
384 t2 = GetTimer(); t = t2-t;
385 tt = t*0.000001f;
386 video_time_usage += tt;
388 if (!mpi || drop_frame)
389 return NULL; // error / skipped frame
391 if (field_dominance == 0)
392 mpi->fields |= MP_IMGFIELD_TOP_FIRST;
393 else if (field_dominance == 1)
394 mpi->fields &= ~MP_IMGFIELD_TOP_FIRST;
396 if (correct_pts) {
397 if (sh_video->num_buffered_pts) {
398 sh_video->num_buffered_pts--;
399 sh_video->pts = sh_video->buffered_pts[sh_video->num_buffered_pts];
401 else {
402 mp_msg(MSGT_CPLAYER, MSGL_ERR, "No pts value from demuxer to "
403 "use for frame!\n");
404 sh_video->pts = MP_NOPTS_VALUE;
407 return mpi;
410 int filter_video(sh_video_t *sh_video, void *frame, double pts)
412 mp_image_t *mpi = frame;
413 unsigned int t2 = GetTimer();
414 vf_instance_t *vf = sh_video->vfilter;
415 // apply video filters and call the leaf vo/ve
416 int ret = vf->put_image(vf, mpi, pts);
417 if (ret > 0) {
418 // draw EOSD first so it ends up below the OSD.
419 // Note that changing this is will not work right with vf_ass and the
420 // vos currently always draw the EOSD first in paused mode.
421 #ifdef CONFIG_ASS
422 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
423 #endif
424 vf->control(vf, VFCTRL_DRAW_OSD, NULL);
427 t2 = GetTimer()-t2;
428 vout_time_usage += t2*0.000001;
430 return ret;