sync with en/mplayer.1 rev. 30822
[mplayer.git] / find_sub.c
blobdcd045eb60ac45551604d94aaa5422510a1ba4e0
1 /*
2 * .SUB
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "config.h"
23 #include <stdio.h>
25 #include "libvo/video_out.h"
26 #include "libvo/sub.h"
27 #include "subreader.h"
29 #include "mp_msg.h"
30 #include "help_mp.h"
31 #include "mpcommon.h"
33 static int current_sub=0;
35 //static subtitle* subtitles=NULL;
36 static int nosub_range_start=-1;
37 static int nosub_range_end=-1;
38 static const sub_data *last_sub_data = NULL;
40 extern float sub_delay;
41 extern float sub_fps;
43 void step_sub(sub_data *subd, float pts, int movement) {
44 subtitle *subs;
45 int key;
47 if (subd == NULL) return;
48 subs = subd->subtitles;
49 key = (pts+sub_delay) * (subd->sub_uses_time ? 100 : sub_fps);
51 /* Tell the OSD subsystem that the OSD contents will change soon */
52 vo_osd_changed(OSDTYPE_SUBTITLE);
54 /* If we are moving forward, don't count the next (current) subtitle
55 * if we haven't displayed it yet. Same when moving other direction.
57 if (movement > 0 && key < subs[current_sub].start)
58 movement--;
59 if (movement < 0 && key >= subs[current_sub].end)
60 movement++;
62 /* Never move beyond first or last subtitle. */
63 if (current_sub+movement < 0)
64 movement = 0-current_sub;
65 if (current_sub+movement >= subd->sub_num)
66 movement = subd->sub_num - current_sub - 1;
68 current_sub += movement;
69 sub_delay = subs[current_sub].start / (subd->sub_uses_time ? 100 : sub_fps) - pts;
72 void find_sub(sub_data* subd,int key){
73 subtitle *subs;
74 subtitle *new_sub = NULL;
75 int i,j;
77 if ( !subd || subd->sub_num == 0) return;
78 subs = subd->subtitles;
80 if (last_sub_data != subd) {
81 // Sub data changed, reset nosub range.
82 last_sub_data = subd;
83 nosub_range_start = -1;
84 nosub_range_end = -1;
87 if(vo_sub){
88 if(key>=vo_sub->start && key<=vo_sub->end) return; // OK!
89 } else {
90 if(key>nosub_range_start && key<nosub_range_end) return; // OK!
92 // sub changed!
94 /* Tell the OSD subsystem that the OSD contents will change soon */
95 vo_osd_changed(OSDTYPE_SUBTITLE);
97 if(key<=0){
98 // no sub here
99 goto update;
102 // printf("\r---- sub changed ----\n");
104 // check next sub.
105 if(current_sub>=0 && current_sub+1 < subd->sub_num){
106 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
107 // no sub
108 nosub_range_start=subs[current_sub].end;
109 nosub_range_end=subs[current_sub+1].start;
110 goto update;
112 // next sub?
113 ++current_sub;
114 new_sub=&subs[current_sub];
115 if(key>=new_sub->start && key<=new_sub->end) goto update; // OK!
118 // printf("\r---- sub log search... ----\n");
120 // use logarithmic search:
121 i=0;
122 j = subd->sub_num - 1;
123 // printf("Searching %d in %d..%d\n",key,subs[i].start,subs[j].end);
124 while(j>=i){
125 current_sub=(i+j+1)/2;
126 new_sub=&subs[current_sub];
127 if(key<new_sub->start) j=current_sub-1;
128 else if(key>new_sub->end) i=current_sub+1;
129 else goto update; // found!
131 // if(key>=new_sub->start && key<=new_sub->end) return; // OK!
133 // check where are we...
134 if(key<new_sub->start){
135 if(current_sub<=0){
136 // before the first sub
137 nosub_range_start=key-1; // tricky
138 nosub_range_end=new_sub->start;
139 // printf("FIRST... key=%d end=%d \n",key,new_sub->start);
140 new_sub=NULL;
141 goto update;
143 --current_sub;
144 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
145 // no sub
146 nosub_range_start=subs[current_sub].end;
147 nosub_range_end=subs[current_sub+1].start;
148 // printf("No sub... 1 \n");
149 new_sub=NULL;
150 goto update;
152 printf("HEH???? ");
153 } else {
154 if(key<=new_sub->end) printf("JAJJ! "); else
155 if(current_sub+1 >= subd->sub_num){
156 // at the end?
157 nosub_range_start=new_sub->end;
158 nosub_range_end=0x7FFFFFFF; // MAXINT
159 // printf("END!?\n");
160 new_sub=NULL;
161 goto update;
162 } else
163 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
164 // no sub
165 nosub_range_start=subs[current_sub].end;
166 nosub_range_end=subs[current_sub+1].start;
167 // printf("No sub... 2 \n");
168 new_sub=NULL;
169 goto update;
173 mp_msg(MSGT_FIXME,MSGL_FIXME,"SUB ERROR: %d ? %d --- %d [%d] \n",key,(int)new_sub->start,(int)new_sub->end,current_sub);
175 new_sub=NULL; // no sub here
176 update:
177 set_osd_subtitle(new_sub);