1 //**************************************************************************//
3 //**************************************************************************//
9 #include "libvo/video_out.h"
10 #include "libvo/sub.h"
11 #include "subreader.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
;
27 void step_sub(sub_data
*subd
, float pts
, int movement
) {
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
)
43 if (movement
< 0 && key
>= subs
[current_sub
].end
)
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(struct MPContext
*mpctx
, sub_data
* subd
,int key
){
58 subtitle
*new_sub
= NULL
;
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.
67 nosub_range_start
= -1;
72 if(key
>=vo_sub
->start
&& key
<=vo_sub
->end
) return; // OK!
74 if(key
>nosub_range_start
&& key
<nosub_range_end
) return; // OK!
78 /* Tell the OSD subsystem that the OSD contents will change soon */
79 vo_osd_changed(OSDTYPE_SUBTITLE
);
86 // printf("\r---- sub changed ----\n");
89 if(current_sub
>=0 && current_sub
+1 < subd
->sub_num
){
90 if(key
>subs
[current_sub
].end
&& key
<subs
[current_sub
+1].start
){
92 nosub_range_start
=subs
[current_sub
].end
;
93 nosub_range_end
=subs
[current_sub
+1].start
;
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:
106 j
= subd
->sub_num
- 1;
107 // printf("Searching %d in %d..%d\n",key,subs[i].start,subs[j].end);
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
){
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);
128 if(key
>subs
[current_sub
].end
&& key
<subs
[current_sub
+1].start
){
130 nosub_range_start
=subs
[current_sub
].end
;
131 nosub_range_end
=subs
[current_sub
+1].start
;
132 // printf("No sub... 1 \n");
138 if(key
<=new_sub
->end
) printf("JAJJ! "); else
139 if(current_sub
+1 >= subd
->sub_num
){
141 nosub_range_start
=new_sub
->end
;
142 nosub_range_end
=0x7FFFFFFF; // MAXINT
143 // printf("END!?\n");
147 if(key
>subs
[current_sub
].end
&& key
<subs
[current_sub
+1].start
){
149 nosub_range_start
=subs
[current_sub
].end
;
150 nosub_range_end
=subs
[current_sub
+1].start
;
151 // printf("No sub... 2 \n");
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
161 set_osd_subtitle(mpctx
, new_sub
);