directsound audio output plugin, patch by Gabor Szecsi <deje at miki.hu> some minor...
[mplayer.git] / find_sub.c
blobd391d285d1c216f2192d62e531db26825aea4e82
1 //**************************************************************************//
2 // .SUB
3 //**************************************************************************//
5 #include "config.h"
7 #ifdef USE_OSD
9 #include <stdio.h>
11 #include "libvo/video_out.h"
12 #include "libvo/sub.h"
13 #include "subreader.h"
15 static int current_sub=0;
17 //static subtitle* subtitles=NULL;
18 static int nosub_range_start=-1;
19 static int nosub_range_end=-1;
21 extern float sub_delay;
22 extern float sub_fps;
24 void step_sub(sub_data *subd, float pts, int movement) {
25 subtitle *subs;
26 int key;
28 if (subd == NULL) return;
29 subs = subd->subtitles;
30 key = (pts+sub_delay) * (subd->sub_uses_time ? 100 : sub_fps);
32 /* Tell the OSD subsystem that the OSD contents will change soon */
33 vo_osd_changed(OSDTYPE_SUBTITLE);
35 /* If we are moving forward, don't count the next (current) subtitle
36 * if we haven't displayed it yet. Same when moving other direction.
38 if (movement > 0 && key < subs[current_sub].start)
39 movement--;
40 if (movement < 0 && key >= subs[current_sub].end)
41 movement++;
43 /* Never move beyond first or last subtitle. */
44 if (current_sub+movement < 0)
45 movement = 0-current_sub;
46 if (current_sub+movement >= subd->sub_num)
47 movement = subd->sub_num - current_sub - 1;
49 current_sub += movement;
50 sub_delay = subs[current_sub].start / (subd->sub_uses_time ? 100 : sub_fps) - pts;
53 void find_sub(sub_data* subd,int key){
54 subtitle *subs;
55 int i,j;
57 if ( !subd || subd->sub_num == 0) return;
58 subs = subd->subtitles;
60 if(vo_sub){
61 if(key>=vo_sub->start && key<=vo_sub->end) return; // OK!
62 } else {
63 if(key>nosub_range_start && key<nosub_range_end) return; // OK!
65 // sub changed!
67 /* Tell the OSD subsystem that the OSD contents will change soon */
68 vo_osd_changed(OSDTYPE_SUBTITLE);
70 if(key<=0){
71 vo_sub=NULL; // no sub here
72 return;
75 // printf("\r---- sub changed ----\n");
77 // check next sub.
78 if(current_sub>=0 && current_sub+1 < subd->sub_num){
79 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
80 // no sub
81 nosub_range_start=subs[current_sub].end;
82 nosub_range_end=subs[current_sub+1].start;
83 vo_sub=NULL;
84 return;
86 // next sub?
87 ++current_sub;
88 vo_sub=&subs[current_sub];
89 if(key>=vo_sub->start && key<=vo_sub->end) return; // OK!
92 // printf("\r---- sub log search... ----\n");
94 // use logarithmic search:
95 i=0;
96 j = subd->sub_num - 1;
97 // printf("Searching %d in %d..%d\n",key,subs[i].start,subs[j].end);
98 while(j>=i){
99 current_sub=(i+j+1)/2;
100 vo_sub=&subs[current_sub];
101 if(key<vo_sub->start) j=current_sub-1;
102 else if(key>vo_sub->end) i=current_sub+1;
103 else return; // found!
105 // if(key>=vo_sub->start && key<=vo_sub->end) return; // OK!
107 // check where are we...
108 if(key<vo_sub->start){
109 if(current_sub<=0){
110 // before the first sub
111 nosub_range_start=key-1; // tricky
112 nosub_range_end=vo_sub->start;
113 // printf("FIRST... key=%d end=%d \n",key,vo_sub->start);
114 vo_sub=NULL;
115 return;
117 --current_sub;
118 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
119 // no sub
120 nosub_range_start=subs[current_sub].end;
121 nosub_range_end=subs[current_sub+1].start;
122 // printf("No sub... 1 \n");
123 vo_sub=NULL;
124 return;
126 printf("HEH???? ");
127 } else {
128 if(key<=vo_sub->end) printf("JAJJ! "); else
129 if(current_sub+1 >= subd->sub_num){
130 // at the end?
131 nosub_range_start=vo_sub->end;
132 nosub_range_end=0x7FFFFFFF; // MAXINT
133 // printf("END!?\n");
134 vo_sub=NULL;
135 return;
136 } else
137 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
138 // no sub
139 nosub_range_start=subs[current_sub].end;
140 nosub_range_end=subs[current_sub+1].start;
141 // printf("No sub... 2 \n");
142 vo_sub=NULL;
143 return;
147 printf("SUB ERROR: %d ? %d --- %d [%d] \n",key,(int)vo_sub->start,(int)vo_sub->end,current_sub);
149 vo_sub=NULL; // no sub here
152 #endif