core: Improve handling of bad timestamps
[mplayer.git] / mpcommon.c
blobd07e4eb27c7f8ee804dee94d773d1173c92a759d
1 #include <stdlib.h>
2 #include "mpcommon.h"
3 #include "options.h"
4 #include "stream/stream.h"
5 #include "libmpdemux/demuxer.h"
6 #include "libmpdemux/stheader.h"
7 #include "mplayer.h"
8 #include "libvo/sub.h"
9 #include "libvo/video_out.h"
10 #include "spudec.h"
11 #include "vobsub.h"
12 #ifdef CONFIG_TV_TELETEXT
13 #include "stream/tv.h"
14 #endif
15 #include "libavutil/intreadwrite.h"
16 #include "m_option.h"
18 double sub_last_pts = -303;
20 #ifdef CONFIG_ASS
21 #include "libass/ass.h"
22 #include "libass/ass_mp.h"
23 ass_track_t* ass_track = 0; // current track to render
24 #endif
26 sub_data* subdata = NULL;
27 subtitle* vo_sub_last = NULL;
29 void update_subtitles(sh_video_t *sh_video, demux_stream_t *d_dvdsub, int reset)
31 struct MPOpts *opts = sh_video->opts;
32 unsigned char *packet=NULL;
33 int len;
34 char type = d_dvdsub->sh ? ((sh_sub_t *)d_dvdsub->sh)->type : 'v';
35 static subtitle subs;
36 if (reset) {
37 sub_clear_text(&subs, MP_NOPTS_VALUE);
38 if (vo_sub) {
39 vo_sub = NULL;
40 vo_osd_changed(OSDTYPE_SUBTITLE);
42 if (vo_spudec) {
43 spudec_reset(vo_spudec);
44 vo_osd_changed(OSDTYPE_SPU);
47 // find sub
48 if (subdata) {
49 double pts = sh_video->pts;
50 if (sub_fps==0) sub_fps = sh_video->fps;
51 current_module = "find_sub";
52 if (pts > sub_last_pts || pts < sub_last_pts-1.0) {
53 find_sub(subdata, (pts+sub_delay) *
54 (subdata->sub_uses_time ? 100. : sub_fps));
55 if (vo_sub) vo_sub_last = vo_sub;
56 // FIXME! frame counter...
57 sub_last_pts = pts;
61 // DVD sub:
62 if (vo_spudec && (vobsub_id >= 0 || (opts->sub_id >= 0 && type == 'v'))) {
63 int timestamp;
64 current_module = "spudec";
65 spudec_heartbeat(vo_spudec, 90000*sh_video->timer);
66 /* Get a sub packet from the DVD or a vobsub and make a timestamp
67 * relative to sh_video->timer */
68 while(1) {
69 // Vobsub
70 len = 0;
71 if (vo_vobsub) {
72 if (sh_video->pts+sub_delay >= 0) {
73 len = vobsub_get_packet(vo_vobsub, sh_video->pts+sub_delay,
74 (void**)&packet, &timestamp);
75 if (len > 0) {
76 timestamp -= (sh_video->pts + sub_delay - sh_video->timer)*90000;
77 mp_dbg(MSGT_CPLAYER,MSGL_V,"\rVOB sub: len=%d v_pts=%5.3f v_timer=%5.3f sub=%5.3f ts=%d \n",len,sh_video->pts,sh_video->timer,timestamp / 90000.0,timestamp);
80 } else {
81 // DVD sub
82 len = ds_get_packet_sub(d_dvdsub, (unsigned char**)&packet);
83 if (len > 0) {
84 // XXX This is wrong, sh_video->pts can be arbitrarily
85 // much behind demuxing position. Unfortunately using
86 // d_video->pts which would have been the simplest
87 // improvement doesn't work because mpeg specific hacks
88 // in video.c set d_video->pts to 0.
89 float x = d_dvdsub->pts - sh_video->pts;
90 if (x > -20 && x < 20) // prevent missing subs on pts reset
91 timestamp = 90000*(sh_video->timer + d_dvdsub->pts
92 + sub_delay - sh_video->pts);
93 else timestamp = 90000*(sh_video->timer + sub_delay);
94 mp_dbg(MSGT_CPLAYER, MSGL_V, "\rDVD sub: len=%d "
95 "v_pts=%5.3f s_pts=%5.3f ts=%d \n", len,
96 sh_video->pts, d_dvdsub->pts, timestamp);
99 if (len<=0 || !packet) break;
100 if (vo_vobsub || timestamp >= 0)
101 spudec_assemble(vo_spudec, packet, len, timestamp);
104 if (spudec_changed(vo_spudec))
105 vo_osd_changed(OSDTYPE_SPU);
106 } else if (opts->sub_id >= 0
107 && (type == 't' || type == 'm' || type == 'a')) {
108 double curpts = sh_video->pts + sub_delay;
109 double endpts;
110 vo_sub = &subs;
111 while (d_dvdsub->first) {
112 double pts = ds_get_next_pts(d_dvdsub);
113 if (pts > curpts)
114 break;
115 endpts = d_dvdsub->first->endpts;
116 len = ds_get_packet_sub(d_dvdsub, &packet);
117 if (type == 'm') {
118 if (len < 2) continue;
119 len = FFMIN(len - 2, AV_RB16(packet));
120 packet += 2;
122 #ifdef CONFIG_ASS
123 if (ass_enabled) {
124 sh_sub_t* sh = d_dvdsub->sh;
125 ass_track = sh ? sh->ass_track : NULL;
126 if (!ass_track) continue;
127 if (type == 'a') { // ssa/ass subs with libass
128 ass_process_chunk(ass_track, packet, len,
129 (long long)(pts*1000 + 0.5),
130 (long long)((endpts-pts)*1000 + 0.5));
131 } else { // plaintext subs with libass
132 vo_sub = NULL;
133 if (pts != MP_NOPTS_VALUE) {
134 if (endpts == MP_NOPTS_VALUE) endpts = pts + 3;
135 sub_clear_text(&subs, MP_NOPTS_VALUE);
136 sub_add_text(&subs, packet, len, endpts);
137 subs.start = pts * 100;
138 subs.end = endpts * 100;
139 ass_process_subtitle(ass_track, &subs);
142 continue;
144 #endif
145 if (pts != MP_NOPTS_VALUE) {
146 if (endpts == MP_NOPTS_VALUE)
147 sub_clear_text(&subs, MP_NOPTS_VALUE);
148 if (type == 'a') { // ssa/ass subs without libass => convert to plaintext
149 int i;
150 unsigned char* p = packet;
151 for (i=0; i < 8 && *p != '\0'; p++)
152 if (*p == ',')
153 i++;
154 if (*p == '\0') /* Broken line? */
155 continue;
156 len -= p - packet;
157 packet = p;
159 sub_add_text(&subs, packet, len, endpts);
160 vo_osd_changed(OSDTYPE_SUBTITLE);
163 if (sub_clear_text(&subs, curpts))
164 vo_osd_changed(OSDTYPE_SUBTITLE);
166 current_module=NULL;
169 void update_teletext(sh_video_t *sh_video, demuxer_t *demuxer, int reset)
171 #ifdef CONFIG_TV_TELETEXT
172 tvi_handle_t* tvh=demuxer->priv;
173 int page_changed;
175 if (demuxer->type != DEMUXER_TYPE_TV || !tvh) return;
177 //Also forcing page update when such ioctl is not supported or call error occured
178 if(tvh->functions->control(tvh->priv,TV_VBI_CONTROL_IS_CHANGED,&page_changed)!=TVI_CONTROL_TRUE)
179 page_changed=1;
181 if(!page_changed)
182 return;
184 if(tvh->functions->control(tvh->priv,TV_VBI_CONTROL_GET_VBIPAGE,&vo_osd_teletext_page)!=TVI_CONTROL_TRUE)
185 vo_osd_teletext_page=NULL;
186 if(tvh->functions->control(tvh->priv,TV_VBI_CONTROL_GET_HALF_PAGE,&vo_osd_teletext_half)!=TVI_CONTROL_TRUE)
187 vo_osd_teletext_half=0;
188 if(tvh->functions->control(tvh->priv,TV_VBI_CONTROL_GET_MODE,&vo_osd_teletext_mode)!=TVI_CONTROL_TRUE)
189 vo_osd_teletext_mode=0;
190 if(tvh->functions->control(tvh->priv,TV_VBI_CONTROL_GET_FORMAT,&vo_osd_teletext_format)!=TVI_CONTROL_TRUE)
191 vo_osd_teletext_format=0;
192 vo_osd_changed(OSDTYPE_TELETEXT);
194 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_MARK_UNCHANGED,NULL);
195 #endif
198 int select_audio(demuxer_t* demuxer, int audio_id, char* audio_lang)
200 if (audio_id == -1 && audio_lang)
201 audio_id = demuxer_audio_track_by_lang(demuxer, audio_lang);
202 if (audio_id == -1)
203 audio_id = demuxer_default_audio_track(demuxer);
204 if (audio_id != -1) // -1 (automatic) is the default behaviour of demuxers
205 demuxer_switch_audio(demuxer, audio_id);
206 if (audio_id == -2) { // some demuxers don't yet know how to switch to no sound
207 demuxer->audio->id = -2;
208 demuxer->audio->sh = NULL;
210 return demuxer->audio->id;
213 /* Parse -noconfig common to both programs */
214 int disable_system_conf=0;
215 int disable_user_conf=0;
216 #ifdef CONFIG_GUI
217 int disable_gui_conf=0;
218 #endif /* CONFIG_GUI */
220 /* Disable all configuration files */
221 static void noconfig_all(void)
223 disable_system_conf = 1;
224 disable_user_conf = 1;
225 #ifdef CONFIG_GUI
226 disable_gui_conf = 1;
227 #endif /* CONFIG_GUI */
230 const m_option_t noconfig_opts[] = {
231 {"all", noconfig_all, CONF_TYPE_FUNC, CONF_GLOBAL|CONF_NOCFG|CONF_PRE_PARSE, 0, 0, NULL},
232 {"system", &disable_system_conf, CONF_TYPE_FLAG, CONF_GLOBAL|CONF_NOCFG|CONF_PRE_PARSE, 0, 1, NULL},
233 {"user", &disable_user_conf, CONF_TYPE_FLAG, CONF_GLOBAL|CONF_NOCFG|CONF_PRE_PARSE, 0, 1, NULL},
234 #ifdef CONFIG_GUI
235 {"gui", &disable_gui_conf, CONF_TYPE_FLAG, CONF_GLOBAL|CONF_NOCFG|CONF_PRE_PARSE, 0, 1, NULL},
236 #endif /* CONFIG_GUI */
237 {NULL, NULL, 0, 0, 0, 0, NULL}