core: fix audio-only + framestep weird behavior
[mplayer/glamo.git] / find_sub.c
blobd0ea3c0016f56646e4a831cc96ba8709d1488330
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 "mpcommon.h"
32 static int current_sub=0;
34 //static subtitle* subtitles=NULL;
35 static int nosub_range_start=-1;
36 static int nosub_range_end=-1;
37 static const sub_data *last_sub_data = NULL;
39 extern float sub_delay;
40 extern float sub_fps;
42 void step_sub(sub_data *subd, float pts, int movement) {
43 subtitle *subs;
44 int key;
46 if (subd == NULL) return;
47 subs = subd->subtitles;
48 key = (pts+sub_delay) * (subd->sub_uses_time ? 100 : sub_fps);
50 /* Tell the OSD subsystem that the OSD contents will change soon */
51 vo_osd_changed(OSDTYPE_SUBTITLE);
53 /* If we are moving forward, don't count the next (current) subtitle
54 * if we haven't displayed it yet. Same when moving other direction.
56 if (movement > 0 && key < subs[current_sub].start)
57 movement--;
58 if (movement < 0 && key >= subs[current_sub].end)
59 movement++;
61 /* Never move beyond first or last subtitle. */
62 if (current_sub+movement < 0)
63 movement = 0-current_sub;
64 if (current_sub+movement >= subd->sub_num)
65 movement = subd->sub_num - current_sub - 1;
67 current_sub += movement;
68 sub_delay = subs[current_sub].start / (subd->sub_uses_time ? 100 : sub_fps) - pts;
71 void find_sub(struct MPContext *mpctx, sub_data* subd,int key){
72 subtitle *subs;
73 subtitle *new_sub = NULL;
74 int i,j;
76 if ( !subd || subd->sub_num == 0) return;
77 subs = subd->subtitles;
79 if (last_sub_data != subd) {
80 // Sub data changed, reset nosub range.
81 last_sub_data = subd;
82 nosub_range_start = -1;
83 nosub_range_end = -1;
86 if(vo_sub){
87 if(key>=vo_sub->start && key<=vo_sub->end) return; // OK!
88 } else {
89 if(key>nosub_range_start && key<nosub_range_end) return; // OK!
91 // sub changed!
93 /* Tell the OSD subsystem that the OSD contents will change soon */
94 vo_osd_changed(OSDTYPE_SUBTITLE);
96 if(key<=0){
97 // no sub here
98 goto update;
101 // printf("\r---- sub changed ----\n");
103 // check next sub.
104 if(current_sub>=0 && current_sub+1 < subd->sub_num){
105 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
106 // no sub
107 nosub_range_start=subs[current_sub].end;
108 nosub_range_end=subs[current_sub+1].start;
109 goto update;
111 // next sub?
112 ++current_sub;
113 new_sub=&subs[current_sub];
114 if(key>=new_sub->start && key<=new_sub->end) goto update; // OK!
117 // printf("\r---- sub log search... ----\n");
119 // use logarithmic search:
120 i=0;
121 j = subd->sub_num - 1;
122 // printf("Searching %d in %d..%d\n",key,subs[i].start,subs[j].end);
123 while(j>=i){
124 current_sub=(i+j+1)/2;
125 new_sub=&subs[current_sub];
126 if(key<new_sub->start) j=current_sub-1;
127 else if(key>new_sub->end) i=current_sub+1;
128 else goto update; // found!
130 // if(key>=new_sub->start && key<=new_sub->end) return; // OK!
132 // check where are we...
133 if(key<new_sub->start){
134 if(current_sub<=0){
135 // before the first sub
136 nosub_range_start=key-1; // tricky
137 nosub_range_end=new_sub->start;
138 // printf("FIRST... key=%d end=%d \n",key,new_sub->start);
139 new_sub=NULL;
140 goto update;
142 --current_sub;
143 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
144 // no sub
145 nosub_range_start=subs[current_sub].end;
146 nosub_range_end=subs[current_sub+1].start;
147 // printf("No sub... 1 \n");
148 new_sub=NULL;
149 goto update;
151 printf("HEH???? ");
152 } else {
153 if(key<=new_sub->end) printf("JAJJ! "); else
154 if(current_sub+1 >= subd->sub_num){
155 // at the end?
156 nosub_range_start=new_sub->end;
157 nosub_range_end=0x7FFFFFFF; // MAXINT
158 // printf("END!?\n");
159 new_sub=NULL;
160 goto update;
161 } else
162 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
163 // no sub
164 nosub_range_start=subs[current_sub].end;
165 nosub_range_end=subs[current_sub+1].start;
166 // printf("No sub... 2 \n");
167 new_sub=NULL;
168 goto update;
172 mp_msg(MSGT_FIXME,MSGL_FIXME,"SUB ERROR: %d ? %d --- %d [%d] \n",key,(int)new_sub->start,(int)new_sub->end,current_sub);
174 new_sub=NULL; // no sub here
175 update:
176 set_osd_subtitle(mpctx, new_sub);