Merge svn changes up to r30605
[mplayer/kovensky.git] / mpcommon.c
bloba816e32b3d27a9e2306f8f911671da1749e9b553
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdlib.h>
20 #include "mpcommon.h"
21 #include "options.h"
22 #include "stream/stream.h"
23 #include "libmpdemux/demuxer.h"
24 #include "libmpdemux/stheader.h"
25 #include "mplayer.h"
26 #include "libvo/sub.h"
27 #include "libvo/video_out.h"
28 #include "cpudetect.h"
29 #include "help_mp.h"
30 #include "mp_msg.h"
31 #include "spudec.h"
32 #include "version.h"
33 #include "vobsub.h"
34 #include "libmpcodecs/dec_teletext.h"
35 #include "ffmpeg_files/intreadwrite.h"
36 #include "m_option.h"
38 double sub_last_pts = -303;
40 #ifdef CONFIG_ASS
41 #include "ass_mp.h"
42 ASS_Track *ass_track = 0; // current track to render
43 #endif
45 sub_data* subdata = NULL;
46 subtitle* vo_sub_last = NULL;
49 void print_version(const char* name)
51 mp_msg(MSGT_CPLAYER, MSGL_INFO, MP_TITLE, name);
53 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
54 GetCpuCaps(&gCpuCaps);
55 #if ARCH_X86
56 mp_msg(MSGT_CPLAYER, MSGL_V,
57 "CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNowExt: %d SSE: %d SSE2: %d SSSE3: %d\n",
58 gCpuCaps.hasMMX, gCpuCaps.hasMMX2,
59 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
60 gCpuCaps.hasSSE, gCpuCaps.hasSSE2, gCpuCaps.hasSSSE3);
61 #if CONFIG_RUNTIME_CPUDETECT
62 mp_tmsg(MSGT_CPLAYER,MSGL_V, "Compiled with runtime CPU detection.\n");
63 #else
64 mp_tmsg(MSGT_CPLAYER,MSGL_V, "Compiled for x86 CPU with extensions:");
65 if (HAVE_MMX)
66 mp_msg(MSGT_CPLAYER,MSGL_V," MMX");
67 if (HAVE_MMX2)
68 mp_msg(MSGT_CPLAYER,MSGL_V," MMX2");
69 if (HAVE_AMD3DNOW)
70 mp_msg(MSGT_CPLAYER,MSGL_V," 3DNow");
71 if (HAVE_AMD3DNOWEXT)
72 mp_msg(MSGT_CPLAYER,MSGL_V," 3DNowExt");
73 if (HAVE_SSE)
74 mp_msg(MSGT_CPLAYER,MSGL_V," SSE");
75 if (HAVE_SSE2)
76 mp_msg(MSGT_CPLAYER,MSGL_V," SSE2");
77 if (HAVE_SSSE3)
78 mp_msg(MSGT_CPLAYER,MSGL_V," SSSE3");
79 if (HAVE_CMOV)
80 mp_msg(MSGT_CPLAYER,MSGL_V," CMOV");
81 mp_msg(MSGT_CPLAYER,MSGL_V,"\n");
82 #endif /* CONFIG_RUNTIME_CPUDETECT */
83 #endif /* ARCH_X86 */
87 void update_subtitles(struct MPContext *mpctx, struct MPOpts *opts,
88 sh_video_t *sh_video, double refpts, double sub_offset,
89 demux_stream_t *d_dvdsub, int reset)
91 unsigned char *packet=NULL;
92 int len;
93 char type = d_dvdsub->sh ? ((sh_sub_t *)d_dvdsub->sh)->type : 'v';
94 static subtitle subs;
95 if (reset) {
96 sub_clear_text(&subs, MP_NOPTS_VALUE);
97 if (vo_sub) {
98 set_osd_subtitle(mpctx, NULL);
100 if (vo_spudec) {
101 spudec_reset(vo_spudec);
102 vo_osd_changed(OSDTYPE_SPU);
104 return;
106 // find sub
107 if (subdata) {
108 if (sub_fps==0) sub_fps = sh_video ? sh_video->fps : 25;
109 current_module = "find_sub";
110 if (refpts > sub_last_pts || refpts < sub_last_pts-1.0) {
111 find_sub(mpctx, subdata, (refpts+sub_delay) *
112 (subdata->sub_uses_time ? 100. : sub_fps));
113 if (vo_sub) vo_sub_last = vo_sub;
114 // FIXME! frame counter...
115 sub_last_pts = refpts;
119 // DVD sub:
120 if (vo_spudec && (vobsub_id >= 0 || (opts->sub_id >= 0 && type == 'v'))) {
121 int timestamp;
122 current_module = "spudec";
123 spudec_heartbeat(vo_spudec, 90000*sh_video->timer);
124 /* Get a sub packet from the DVD or a vobsub and make a timestamp
125 * relative to sh_video->timer */
126 while(1) {
127 // Vobsub
128 len = 0;
129 if (vo_vobsub) {
130 if (refpts+sub_delay >= 0) {
131 len = vobsub_get_packet(vo_vobsub, refpts+sub_delay,
132 (void**)&packet, &timestamp);
133 if (len > 0) {
134 timestamp -= (refpts + sub_delay - sh_video->timer)*90000;
135 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,refpts,sh_video->timer,timestamp / 90000.0,timestamp);
138 } else {
139 // DVD sub
140 len = ds_get_packet_sub(d_dvdsub, (unsigned char**)&packet);
141 if (len > 0) {
142 // XXX This is wrong, sh_video->pts can be arbitrarily
143 // much behind demuxing position. Unfortunately using
144 // d_video->pts which would have been the simplest
145 // improvement doesn't work because mpeg specific hacks
146 // in video.c set d_video->pts to 0.
147 float x = d_dvdsub->pts - refpts;
148 if (x > -20 && x < 20) // prevent missing subs on pts reset
149 timestamp = 90000*(sh_video->timer + d_dvdsub->pts
150 + sub_delay - refpts);
151 else timestamp = 90000*(sh_video->timer + sub_delay);
152 mp_dbg(MSGT_CPLAYER, MSGL_V, "\rDVD sub: len=%d "
153 "v_pts=%5.3f s_pts=%5.3f ts=%d \n", len,
154 refpts, d_dvdsub->pts, timestamp);
157 if (len<=0 || !packet) break;
158 if (vo_vobsub || timestamp >= 0)
159 spudec_assemble(vo_spudec, packet, len, timestamp);
162 if (spudec_changed(vo_spudec))
163 vo_osd_changed(OSDTYPE_SPU);
164 } else if (opts->sub_id >= 0
165 && (type == 't' || type == 'm' || type == 'a' || type == 'd')) {
166 double curpts = refpts + sub_delay;
167 double endpts;
168 if (type == 'd' && !d_dvdsub->demuxer->teletext) {
169 tt_stream_props tsp = {0};
170 void *ptr = &tsp;
171 if (teletext_control(NULL, TV_VBI_CONTROL_START, &ptr) == VBI_CONTROL_TRUE)
172 d_dvdsub->demuxer->teletext = ptr;
174 if (d_dvdsub->non_interleaved)
175 ds_get_next_pts(d_dvdsub);
176 while (d_dvdsub->first) {
177 double subpts = ds_get_next_pts(d_dvdsub) + sub_offset;
178 if (subpts > curpts) {
179 // Libass handled subs can be fed to it in advance
180 if (!opts->ass_enabled || type == 'd')
181 break;
182 // Try to avoid demuxing whole file at once
183 if (d_dvdsub->non_interleaved && subpts > curpts + 1)
184 break;
186 endpts = d_dvdsub->first->endpts + sub_offset;
187 len = ds_get_packet_sub(d_dvdsub, &packet);
188 if (type == 'm') {
189 if (len < 2) continue;
190 len = FFMIN(len - 2, AV_RB16(packet));
191 packet += 2;
193 if (type == 'd') {
194 if (d_dvdsub->demuxer->teletext) {
195 uint8_t *p = packet;
196 p++;
197 len--;
198 while (len >= 46) {
199 int sublen = p[1];
200 if (p[0] == 2 || p[0] == 3)
201 teletext_control(d_dvdsub->demuxer->teletext,
202 TV_VBI_CONTROL_DECODE_DVB, p + 2);
203 p += sublen + 2;
204 len -= sublen + 2;
207 continue;
209 #ifdef CONFIG_ASS
210 if (opts->ass_enabled) {
211 sh_sub_t* sh = d_dvdsub->sh;
212 ass_track = sh ? sh->ass_track : NULL;
213 if (!ass_track) continue;
214 if (type == 'a') { // ssa/ass subs with libass
215 ass_process_chunk(ass_track, packet, len,
216 (long long)(subpts*1000 + 0.5),
217 (long long)((endpts-subpts)*1000 + 0.5));
218 } else { // plaintext subs with libass
219 if (subpts != MP_NOPTS_VALUE) {
220 subtitle tmp_subs = {0};
221 if (endpts == MP_NOPTS_VALUE) endpts = subpts + 3;
222 sub_add_text(&tmp_subs, packet, len, endpts);
223 tmp_subs.start = subpts * 100;
224 tmp_subs.end = endpts * 100;
225 ass_process_subtitle(ass_track, &tmp_subs);
226 sub_clear_text(&tmp_subs, MP_NOPTS_VALUE);
229 continue;
231 #endif
232 if (subpts != MP_NOPTS_VALUE) {
233 if (endpts == MP_NOPTS_VALUE)
234 sub_clear_text(&subs, MP_NOPTS_VALUE);
235 if (type == 'a') { // ssa/ass subs without libass => convert to plaintext
236 int i;
237 unsigned char* p = packet;
238 for (i=0; i < 8 && *p != '\0'; p++)
239 if (*p == ',')
240 i++;
241 if (*p == '\0') /* Broken line? */
242 continue;
243 len -= p - packet;
244 packet = p;
246 sub_add_text(&subs, packet, len, endpts);
247 set_osd_subtitle(mpctx, &subs);
249 if (d_dvdsub->non_interleaved)
250 ds_get_next_pts(d_dvdsub);
252 if (!opts->ass_enabled)
253 if (sub_clear_text(&subs, curpts))
254 set_osd_subtitle(mpctx, &subs);
256 current_module=NULL;
259 void update_teletext(sh_video_t *sh_video, demuxer_t *demuxer, int reset)
261 int page_changed;
263 if (!demuxer->teletext)
264 return;
266 //Also forcing page update when such ioctl is not supported or call error occured
267 if(teletext_control(demuxer->teletext,TV_VBI_CONTROL_IS_CHANGED,&page_changed)!=VBI_CONTROL_TRUE)
268 page_changed=1;
270 if(!page_changed)
271 return;
273 if(teletext_control(demuxer->teletext,TV_VBI_CONTROL_GET_VBIPAGE,&vo_osd_teletext_page)!=VBI_CONTROL_TRUE)
274 vo_osd_teletext_page=NULL;
275 if(teletext_control(demuxer->teletext,TV_VBI_CONTROL_GET_HALF_PAGE,&vo_osd_teletext_half)!=VBI_CONTROL_TRUE)
276 vo_osd_teletext_half=0;
277 if(teletext_control(demuxer->teletext,TV_VBI_CONTROL_GET_MODE,&vo_osd_teletext_mode)!=VBI_CONTROL_TRUE)
278 vo_osd_teletext_mode=0;
279 if(teletext_control(demuxer->teletext,TV_VBI_CONTROL_GET_FORMAT,&vo_osd_teletext_format)!=VBI_CONTROL_TRUE)
280 vo_osd_teletext_format=0;
281 vo_osd_changed(OSDTYPE_TELETEXT);
283 teletext_control(demuxer->teletext,TV_VBI_CONTROL_MARK_UNCHANGED,NULL);
286 int select_audio(demuxer_t* demuxer, int audio_id, char* audio_lang)
288 if (audio_id == -1 && audio_lang)
289 audio_id = demuxer_audio_track_by_lang(demuxer, audio_lang);
290 if (audio_id == -1)
291 audio_id = demuxer_default_audio_track(demuxer);
292 if (audio_id != -1) // -1 (automatic) is the default behaviour of demuxers
293 demuxer_switch_audio(demuxer, audio_id);
294 if (audio_id == -2) { // some demuxers don't yet know how to switch to no sound
295 demuxer->audio->id = -2;
296 demuxer->audio->sh = NULL;
298 return demuxer->audio->id;
301 /* Parse -noconfig common to both programs */
302 int disable_system_conf=0;
303 int disable_user_conf=0;
305 /* Disable all configuration files */
306 static void noconfig_all(void)
308 disable_system_conf = 1;
309 disable_user_conf = 1;
312 const m_option_t noconfig_opts[] = {
313 {"all", noconfig_all, CONF_TYPE_FUNC, CONF_GLOBAL|CONF_NOCFG|CONF_PRE_PARSE, 0, 0, NULL},
314 {"system", &disable_system_conf, CONF_TYPE_FLAG, CONF_GLOBAL|CONF_NOCFG|CONF_PRE_PARSE, 0, 1, NULL},
315 {"user", &disable_user_conf, CONF_TYPE_FLAG, CONF_GLOBAL|CONF_NOCFG|CONF_PRE_PARSE, 0, 1, NULL},
316 {NULL, NULL, 0, 0, 0, 0, NULL}