1
0

vid_subs.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "main.h"
  2. #include "vid_subs.h"
  3. Subtitles::Subtitles(const char *filename)
  4. {
  5. m_frame_based=0;
  6. m_last_sub=-1;
  7. m_font_size_mod=0;
  8. #ifdef SUBTITLES_READER
  9. if(!filename) return;
  10. IDataReader *file_reader=CreateReader((char *)filename);
  11. if(file_reader) {
  12. char *text=NULL;
  13. int textpos=0;
  14. int allocsize=0;
  15. char buf[1024] = {0};
  16. unsigned aborttime=GetTickCount()+20000;
  17. for (;;)
  18. {
  19. int l=file_reader->read(buf,1024);
  20. if (l <= 0)
  21. {
  22. if (file_reader->iseof()) break;
  23. if (file_reader->geterror() || GetTickCount() > aborttime)
  24. {
  25. free(text);
  26. return;
  27. }
  28. Sleep(100);
  29. }
  30. else
  31. {
  32. if (textpos+l+1 >= allocsize)
  33. {
  34. allocsize = textpos+l+1+8192;
  35. text=(char*)realloc(text,allocsize);
  36. }
  37. memcpy(text+textpos,buf,l);
  38. textpos+=l;
  39. }
  40. }
  41. if (text) {
  42. text[textpos]=0;
  43. //fucko: check for case
  44. if(strstr(filename,".srt")) decodeSrtFile(text);
  45. else if(strstr(filename,".sub")) decodeSubFile(text);
  46. free(text);
  47. }
  48. }
  49. delete(file_reader);
  50. #endif
  51. }
  52. #ifdef SUBTITLES_READER
  53. void Subtitles::decodeSrtFile(char *text) {
  54. // parse subtitle file (.srt format)
  55. char *p=text;
  56. //for(int i=0;;i++) {
  57. while(1) {
  58. unsigned int time_start,time_end;
  59. // parse title nb
  60. char *p2=p;
  61. while(p2 && *p2 && *p2!='\n') p2++;
  62. *p2++=0;
  63. //if(atoi(p)!=i+1) break;
  64. if(atoi(p)<=0) break;
  65. // parse start time
  66. p=p2;
  67. while(p2 && *p2 && *p2!=' ') p2++;
  68. *p2++=0;
  69. time_start=getTimeFromSrtText(p);
  70. // parse "-->"
  71. while(p2 && *p2 && *p2!=' ') p2++;
  72. p2++;
  73. // parse end time
  74. p=p2;
  75. while(p2 && *p2 && *p2!='\n') p2++;
  76. *p2++=0;
  77. time_end=getTimeFromSrtText(p);
  78. // parse text
  79. p=p2;
  80. while(p2 && *p2 && !(*p2=='\r' || *p=='\n')) {
  81. while(p2 && *p2 && *p2!='\n') p2++;
  82. p2++;
  83. }
  84. *p2++=0;
  85. //remove trailing CR
  86. {
  87. int l=lstrlen(p);
  88. if(l) {
  89. if(p[l-1]=='\r' || p[l-1]=='\n') p[l-1]=0;
  90. }
  91. }
  92. m_subs.put(new SubsItem(time_start,time_end,p));
  93. if(*p2=='\n') p2++;
  94. p=p2;
  95. }
  96. m_frame_based=0;
  97. }
  98. unsigned int Subtitles::getTimeFromSrtText(const char *text) {
  99. int hours,mins,secs,mills;
  100. const char *p=text;
  101. hours=atoi(p);
  102. while(p && *p && *p!=':') p++;
  103. if (p) p++;
  104. mins=atoi(p);
  105. while(p && *p && *p!=':') p++;
  106. if (p) p++;
  107. secs=atoi(p);
  108. while(p && *p && *p!=',') p++;
  109. if (p) p++;
  110. mills=atoi(p);
  111. return mills+(secs*1000)+(mins*60000)+(hours*60*60000);
  112. }
  113. void Subtitles::decodeSubFile(char *text)
  114. {
  115. char *p=text;
  116. while(p && *p && *p=='{') {
  117. int framestart,frameend;
  118. p++;
  119. char *p2=p;
  120. while(p2 && *p2 && *p2!='}') p2++;
  121. if (p2) *p2++=0;
  122. framestart=atoi(p);
  123. p2+=1;
  124. p=p2;
  125. while(p2 && *p2 && *p2!='}') p2++;
  126. if (p2) *p2++=0;
  127. frameend=atoi(p);
  128. p=p2;
  129. while(p2 && *p2 && *p2!='\r' && *p2!='\n') {
  130. //replace pipes with CR
  131. if(*p2=='|') *p2='\n';
  132. p2++;
  133. }
  134. *p2++=0;
  135. m_subs.put(new SubsItem(framestart,frameend,p));
  136. if(*p2=='\n') p2++;
  137. p=p2;
  138. }
  139. m_frame_based=1;
  140. }
  141. #endif
  142. SubsItem *Subtitles::getSubtitle(unsigned int time, unsigned int frame)
  143. {
  144. //FUCKO
  145. #if 0
  146. unsigned int ref=m_frame_based?frame:time;
  147. //check with lastsub
  148. if(m_last_sub!=-1) {
  149. SubsItem *item=m_subs.get(m_last_sub);
  150. if(ref>=item->timestart && ref<=item->timeend)
  151. {
  152. item->fontSize=item->origFontSize+m_font_size_mod;
  153. return item;
  154. }
  155. SubsItem *item2=m_subs.get(m_last_sub+1);
  156. if(item2) {
  157. if(ref>=item->timeend && ref<=item2->timestart) return NULL;
  158. if(ref>=item2->timestart && ref<=item2->timeend) {
  159. m_last_sub++;
  160. item2->fontSize=item2->origFontSize+m_font_size_mod;
  161. return item2;
  162. }
  163. }
  164. }
  165. int l=m_subs.getlen();
  166. for(int i=0;i<l;i++) {
  167. SubsItem *item=m_subs.get(i);
  168. if(ref<item->timestart) break;
  169. if(ref>=item->timestart && ref<=item->timeend) {
  170. m_last_sub=i;
  171. item->fontSize=item->origFontSize+m_font_size_mod;
  172. return item;
  173. }
  174. }
  175. m_last_sub=-1;
  176. #endif
  177. return NULL;
  178. }
  179. void Subtitles::addSubtitlePacket(SUBTITLE_INFO *sti)
  180. {
  181. //FUCKO
  182. #if 0
  183. m_frame_based=1; //FUCKO: put this in subsitem struct
  184. SubsItem *i=new SubsItem(sti->start_frame,sti->end_frame,sti->utf8_text);
  185. i->xPos=sti->xPos;
  186. i->yPos=sti->yPos;
  187. i->colorBlue=sti->colorBlue;
  188. i->colorGreen=sti->colorGreen;
  189. i->colorRed=sti->colorRed;
  190. i->extraDataSize=sti->extraDataSize;
  191. i->origFontSize=sti->fontSize;
  192. if(sti->extraDataSize) {
  193. i->extraData=malloc(sti->extraDataSize);
  194. memcpy((void *)i->extraData,sti->extraData,sti->extraDataSize);
  195. }
  196. i->muxed_subtitle=1;
  197. m_subs.put(i);
  198. #endif
  199. }