subtitles/demux: store duration instead of endpts in demux packets
[mplayer/glamo.git] / find_sub.c
blob7eeefda9d12db26a07cc3c1e8669a4fcb9602d10
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"
31 #include "mplayer.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 void step_sub(sub_data *subd, float pts, int movement) {
41 subtitle *subs;
42 int key;
44 if (subd == NULL) return;
45 subs = subd->subtitles;
46 key = (pts+sub_delay) * (subd->sub_uses_time ? 100 : sub_fps);
48 /* Tell the OSD subsystem that the OSD contents will change soon */
49 vo_osd_changed(OSDTYPE_SUBTITLE);
51 /* If we are moving forward, don't count the next (current) subtitle
52 * if we haven't displayed it yet. Same when moving other direction.
54 if (movement > 0 && key < subs[current_sub].start)
55 movement--;
56 if (movement < 0 && key >= subs[current_sub].end)
57 movement++;
59 /* Never move beyond first or last subtitle. */
60 if (current_sub+movement < 0)
61 movement = 0-current_sub;
62 if (current_sub+movement >= subd->sub_num)
63 movement = subd->sub_num - current_sub - 1;
65 current_sub += movement;
66 sub_delay = subs[current_sub].start / (subd->sub_uses_time ? 100 : sub_fps) - pts;
69 void find_sub(struct MPContext *mpctx, sub_data* subd,int key){
70 subtitle *subs;
71 subtitle *new_sub = NULL;
72 int i,j;
74 if ( !subd || subd->sub_num == 0) return;
75 subs = subd->subtitles;
77 if (last_sub_data != subd) {
78 // Sub data changed, reset nosub range.
79 last_sub_data = subd;
80 nosub_range_start = -1;
81 nosub_range_end = -1;
84 if(vo_sub){
85 if(key>=vo_sub->start && key<=vo_sub->end) return; // OK!
86 } else {
87 if(key>nosub_range_start && key<nosub_range_end) return; // OK!
89 // sub changed!
91 /* Tell the OSD subsystem that the OSD contents will change soon */
92 vo_osd_changed(OSDTYPE_SUBTITLE);
94 if(key<=0){
95 // no sub here
96 goto update;
99 // printf("\r---- sub changed ----\n");
101 // check next sub.
102 if(current_sub>=0 && current_sub+1 < subd->sub_num){
103 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
104 // no sub
105 nosub_range_start=subs[current_sub].end;
106 nosub_range_end=subs[current_sub+1].start;
107 goto update;
109 // next sub?
110 ++current_sub;
111 new_sub=&subs[current_sub];
112 if(key>=new_sub->start && key<=new_sub->end) goto update; // OK!
115 // printf("\r---- sub log search... ----\n");
117 // use logarithmic search:
118 i=0;
119 j = subd->sub_num - 1;
120 // printf("Searching %d in %d..%d\n",key,subs[i].start,subs[j].end);
121 while(j>=i){
122 current_sub=(i+j+1)/2;
123 new_sub=&subs[current_sub];
124 if(key<new_sub->start) j=current_sub-1;
125 else if(key>new_sub->end) i=current_sub+1;
126 else goto update; // found!
128 // if(key>=new_sub->start && key<=new_sub->end) return; // OK!
130 // check where are we...
131 if(key<new_sub->start){
132 if(current_sub<=0){
133 // before the first sub
134 nosub_range_start=key-1; // tricky
135 nosub_range_end=new_sub->start;
136 // printf("FIRST... key=%d end=%d \n",key,new_sub->start);
137 new_sub=NULL;
138 goto update;
140 --current_sub;
141 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
142 // no sub
143 nosub_range_start=subs[current_sub].end;
144 nosub_range_end=subs[current_sub+1].start;
145 // printf("No sub... 1 \n");
146 new_sub=NULL;
147 goto update;
149 printf("HEH???? ");
150 } else {
151 if(key<=new_sub->end) printf("JAJJ! "); else
152 if(current_sub+1 >= subd->sub_num){
153 // at the end?
154 nosub_range_start=new_sub->end;
155 nosub_range_end=0x7FFFFFFF; // MAXINT
156 // printf("END!?\n");
157 new_sub=NULL;
158 goto update;
159 } else
160 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
161 // no sub
162 nosub_range_start=subs[current_sub].end;
163 nosub_range_end=subs[current_sub+1].start;
164 // printf("No sub... 2 \n");
165 new_sub=NULL;
166 goto update;
170 mp_msg(MSGT_FIXME,MSGL_FIXME,"SUB ERROR: %d ? %d --- %d [%d] \n",key,(int)new_sub->start,(int)new_sub->end,current_sub);
172 new_sub=NULL; // no sub here
173 update:
174 set_osd_subtitle(mpctx, new_sub);