Enable the read-based forward seek fallback also when CONFIG_NETWORK is
[mplayer/greg.git] / find_sub.c
blobc5081a519db6aa726ddf2e6a18fdcbcc9f0508b5
1 //**************************************************************************//
2 // .SUB
3 //**************************************************************************//
5 #include "config.h"
7 #include <stdio.h>
9 #include "libvo/video_out.h"
10 #include "libvo/sub.h"
11 #include "subreader.h"
13 #include "mp_msg.h"
14 #include "help_mp.h"
15 #include "mpcommon.h"
17 static int current_sub=0;
19 //static subtitle* subtitles=NULL;
20 static int nosub_range_start=-1;
21 static int nosub_range_end=-1;
22 static const sub_data *last_sub_data = NULL;
24 extern float sub_delay;
25 extern float sub_fps;
27 void step_sub(sub_data *subd, float pts, int movement) {
28 subtitle *subs;
29 int key;
31 if (subd == NULL) return;
32 subs = subd->subtitles;
33 key = (pts+sub_delay) * (subd->sub_uses_time ? 100 : sub_fps);
35 /* Tell the OSD subsystem that the OSD contents will change soon */
36 vo_osd_changed(OSDTYPE_SUBTITLE);
38 /* If we are moving forward, don't count the next (current) subtitle
39 * if we haven't displayed it yet. Same when moving other direction.
41 if (movement > 0 && key < subs[current_sub].start)
42 movement--;
43 if (movement < 0 && key >= subs[current_sub].end)
44 movement++;
46 /* Never move beyond first or last subtitle. */
47 if (current_sub+movement < 0)
48 movement = 0-current_sub;
49 if (current_sub+movement >= subd->sub_num)
50 movement = subd->sub_num - current_sub - 1;
52 current_sub += movement;
53 sub_delay = subs[current_sub].start / (subd->sub_uses_time ? 100 : sub_fps) - pts;
56 void find_sub(sub_data* subd,int key){
57 subtitle *subs;
58 subtitle *new_sub = NULL;
59 int i,j;
61 if ( !subd || subd->sub_num == 0) return;
62 subs = subd->subtitles;
64 if (last_sub_data != subd) {
65 // Sub data changed, reset nosub range.
66 last_sub_data = subd;
67 nosub_range_start = -1;
68 nosub_range_end = -1;
71 if(vo_sub){
72 if(key>=vo_sub->start && key<=vo_sub->end) return; // OK!
73 } else {
74 if(key>nosub_range_start && key<nosub_range_end) return; // OK!
76 // sub changed!
78 /* Tell the OSD subsystem that the OSD contents will change soon */
79 vo_osd_changed(OSDTYPE_SUBTITLE);
81 if(key<=0){
82 // no sub here
83 goto update;
86 // printf("\r---- sub changed ----\n");
88 // check next sub.
89 if(current_sub>=0 && current_sub+1 < subd->sub_num){
90 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
91 // no sub
92 nosub_range_start=subs[current_sub].end;
93 nosub_range_end=subs[current_sub+1].start;
94 goto update;
96 // next sub?
97 ++current_sub;
98 new_sub=&subs[current_sub];
99 if(key>=new_sub->start && key<=new_sub->end) goto update; // OK!
102 // printf("\r---- sub log search... ----\n");
104 // use logarithmic search:
105 i=0;
106 j = subd->sub_num - 1;
107 // printf("Searching %d in %d..%d\n",key,subs[i].start,subs[j].end);
108 while(j>=i){
109 current_sub=(i+j+1)/2;
110 new_sub=&subs[current_sub];
111 if(key<new_sub->start) j=current_sub-1;
112 else if(key>new_sub->end) i=current_sub+1;
113 else goto update; // found!
115 // if(key>=new_sub->start && key<=new_sub->end) return; // OK!
117 // check where are we...
118 if(key<new_sub->start){
119 if(current_sub<=0){
120 // before the first sub
121 nosub_range_start=key-1; // tricky
122 nosub_range_end=new_sub->start;
123 // printf("FIRST... key=%d end=%d \n",key,new_sub->start);
124 new_sub=NULL;
125 goto update;
127 --current_sub;
128 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
129 // no sub
130 nosub_range_start=subs[current_sub].end;
131 nosub_range_end=subs[current_sub+1].start;
132 // printf("No sub... 1 \n");
133 new_sub=NULL;
134 goto update;
136 printf("HEH???? ");
137 } else {
138 if(key<=new_sub->end) printf("JAJJ! "); else
139 if(current_sub+1 >= subd->sub_num){
140 // at the end?
141 nosub_range_start=new_sub->end;
142 nosub_range_end=0x7FFFFFFF; // MAXINT
143 // printf("END!?\n");
144 new_sub=NULL;
145 goto update;
146 } else
147 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
148 // no sub
149 nosub_range_start=subs[current_sub].end;
150 nosub_range_end=subs[current_sub+1].start;
151 // printf("No sub... 2 \n");
152 new_sub=NULL;
153 goto update;
157 mp_msg(MSGT_FIXME,MSGL_FIXME,"SUB ERROR: %d ? %d --- %d [%d] \n",key,(int)new_sub->start,(int)new_sub->end,current_sub);
159 new_sub=NULL; // no sub here
160 update:
161 set_osd_subtitle(new_sub);