mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / sub / find_sub.c
blob22643cda444bddd6055c8c015d9064f47ffed459
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 "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 find_sub(struct MPContext *mpctx, sub_data* subd,int key){
41 subtitle *subs;
42 subtitle *new_sub = NULL;
43 int i,j;
45 if ( !subd || subd->sub_num == 0) return;
46 subs = subd->subtitles;
48 if (last_sub_data != subd) {
49 // Sub data changed, reset nosub range.
50 last_sub_data = subd;
51 nosub_range_start = -1;
52 nosub_range_end = -1;
55 if(vo_sub){
56 if(key>=vo_sub->start && key<=vo_sub->end) return; // OK!
57 } else {
58 if(key>nosub_range_start && key<nosub_range_end) return; // OK!
60 // sub changed!
62 /* Tell the OSD subsystem that the OSD contents will change soon */
63 vo_osd_changed(OSDTYPE_SUBTITLE);
65 if(key<=0){
66 // no sub here
67 goto update;
70 // printf("\r---- sub changed ----\n");
72 // check next sub.
73 if(current_sub>=0 && current_sub+1 < subd->sub_num){
74 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
75 // no sub
76 nosub_range_start=subs[current_sub].end;
77 nosub_range_end=subs[current_sub+1].start;
78 goto update;
80 // next sub?
81 ++current_sub;
82 new_sub=&subs[current_sub];
83 if(key>=new_sub->start && key<=new_sub->end) goto update; // OK!
86 // printf("\r---- sub log search... ----\n");
88 // use logarithmic search:
89 i=0;
90 j = subd->sub_num - 1;
91 // printf("Searching %d in %d..%d\n",key,subs[i].start,subs[j].end);
92 while(j>=i){
93 current_sub=(i+j+1)/2;
94 new_sub=&subs[current_sub];
95 if(key<new_sub->start) j=current_sub-1;
96 else if(key>new_sub->end) i=current_sub+1;
97 else goto update; // found!
99 // if(key>=new_sub->start && key<=new_sub->end) return; // OK!
101 // check where are we...
102 if(key<new_sub->start){
103 if(current_sub<=0){
104 // before the first sub
105 nosub_range_start=key-1; // tricky
106 nosub_range_end=new_sub->start;
107 // printf("FIRST... key=%d end=%d \n",key,new_sub->start);
108 new_sub=NULL;
109 goto update;
111 --current_sub;
112 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
113 // no sub
114 nosub_range_start=subs[current_sub].end;
115 nosub_range_end=subs[current_sub+1].start;
116 // printf("No sub... 1 \n");
117 new_sub=NULL;
118 goto update;
120 printf("HEH???? ");
121 } else {
122 if(key<=new_sub->end) printf("JAJJ! "); else
123 if(current_sub+1 >= subd->sub_num){
124 // at the end?
125 nosub_range_start=new_sub->end;
126 nosub_range_end=0x7FFFFFFF; // MAXINT
127 // printf("END!?\n");
128 new_sub=NULL;
129 goto update;
130 } else
131 if(key>subs[current_sub].end && key<subs[current_sub+1].start){
132 // no sub
133 nosub_range_start=subs[current_sub].end;
134 nosub_range_end=subs[current_sub+1].start;
135 // printf("No sub... 2 \n");
136 new_sub=NULL;
137 goto update;
141 mp_msg(MSGT_FIXME,MSGL_FIXME,"SUB ERROR: %d ? %d --- %d [%d] \n",key,(int)new_sub->start,(int)new_sub->end,current_sub);
143 new_sub=NULL; // no sub here
144 update:
145 set_osd_subtitle(mpctx, new_sub);