Subtitles.cpp
上传用户:lusi_8715
上传日期:2007-01-08
资源大小:199k
文件大小:5k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /**************************************************************************************
  2.  *                                                                                    *
  3.  * This application contains code from OpenDivX and is released as a "Larger Work"    *
  4.  * under that license. Consistant with that license, this application is released     *
  5.  * under the GNU General Public License.                                              *
  6.  *                                                                                    *
  7.  * The OpenDivX license can be found at: http://www.projectmayo.com/opendivx/docs.php *
  8.  * The GPL can be found at: http://www.gnu.org/copyleft/gpl.html                      *
  9.  *                                                                                    *
  10.  * Copyright (c) 2001 - Project Mayo                                                  *
  11.  *                                                                                    *
  12.  * Authors: Damien Chavarria <adrc at projectmayo.com>                                *
  13.  *                                                                                    *
  14.  **************************************************************************************/
  15. #include "Subtitles.h"
  16. /*
  17.  * Subtitles class
  18.  */
  19. Subtitles::Subtitles() {
  20. this->input  = NULL;
  21. this->format = -1;
  22. }
  23. Subtitles::~Subtitles() {
  24. }
  25. int Subtitles::Open(char *aviFilename) {
  26. this->input = NULL;
  27. if(aviFilename) {
  28. char *subFilename;
  29. subFilename = (char *) malloc(strlen(aviFilename) - 2);
  30. strncpy(subFilename, aviFilename, strlen(aviFilename) - 3);
  31. subFilename[strlen(aviFilename) - 3]= '';
  32. strncat(subFilename, "sub", 3);
  33. /*
  34.  * only local subtitles are supported now
  35.  */
  36. this->input = new InputMedia();
  37. this->input->Open(subFilename, INPUT_OPEN_ASCII, INPUT_TYPE_FILE);
  38. if(!this->input->isOK())
  39. return 0;
  40. /*
  41.  * Now check the subtitles type
  42.  */
  43. char firstByte;
  44. this->input->Read(&firstByte, 1);
  45. this->input->Seek(0, INPUT_SEEK_SET);
  46. switch(firstByte) {
  47. case '[':
  48. /*
  49.  * SubViewer format (likely)
  50.  */
  51. return 0;
  52. break;
  53. case '{':
  54. /*
  55.  * Micro DVD format (likely)
  56.  */
  57. this->format = SUBTITLES_FORMAT_SUBVIEWER;
  58. break;
  59. default:
  60. return 0;
  61. }
  62. getNextSubtitle();
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. int Subtitles::getNextSubtitle() {
  68. if(this->input->isOK()) {
  69. switch(this->format) {
  70. case SUBTITLES_FORMAT_SUBVIEWER:
  71. /*
  72.  * Find the corresponding string
  73.  * and get the text to display
  74.  */
  75. /*
  76.  * Seek to the next '{'
  77.  * and look at frame number
  78.  *
  79.  */
  80. char  dummy;
  81. DWORD frame;
  82. DWORD i;
  83. this->input->Read(&dummy, 1);
  84. while(dummy != '{') {
  85. this->input->Read(&dummy, 1);
  86. };
  87. /*
  88.  * Now we can get the frame number
  89.  */
  90. this->input->ScanF("%d", &this->firstFrame);
  91. this->input->ScanF("%c%c", &text);
  92. this->input->ScanF("%d", &this->lastFrame);
  93. this->input->ScanF("%c", &text);
  94. /*
  95.  * now read until the end of line
  96.  */
  97. memset(text, 0, 255);
  98. i=0;
  99. while(text[i-1] != 'n' && i < 255) {
  100. this->input->Read(&text[i], 1);
  101. i++;
  102. }
  103. return 1;
  104. break;
  105. default:
  106. this->firstFrame = 0x100000000;
  107. return 0;
  108. }
  109. }
  110. return 0;
  111. }
  112. int Subtitles::Apply(HWND hwnd, long frameNumber, double frameRate, int fullscreen) {
  113. if(this->input->isOK()) {
  114. if(frameNumber > this->lastFrame) {
  115. do {
  116. this->getNextSubtitle();
  117. }
  118. while(this->firstFrame < frameNumber);
  119. }
  120. HDC    hdc;
  121. HBRUSH brush;
  122. char  *secondLine;
  123. RECT   rect;
  124. hdc = GetDC(hwnd);
  125. SetBkMode(hdc, TRANSPARENT);
  126. GetClientRect( hwnd, &rect );
  127. /*
  128.  * squeeze rect so that
  129.  * we have space for controls
  130.  */
  131. rect.left   += 7;
  132. rect.right  -= 8;
  133. rect.top     = rect.bottom - 115 - 50;
  134. rect.bottom -= 115;
  135. if(fullscreen) {
  136. return 0;
  137. }
  138. brush = CreateSolidBrush(0); 
  139. SelectObject(hdc, brush);
  140. Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom); 
  141. if(frameNumber >= this->firstFrame && frameNumber <= this->lastFrame) {
  142. /*
  143.  * We can draw it
  144.  *
  145.  */
  146. SetTextColor(hdc, 0x00FFFFFF);
  147. secondLine = strchr(this->text, '|');
  148. if(secondLine == NULL) {
  149. TextOut(hdc, rect.left + ((rect.right - rect.left)/2) - 3*strlen(text), 
  150.     rect.top + (rect.bottom - rect.top)/2 - 6, this->text, strlen(this->text) - 1);
  151. }
  152. else {
  153. TextOut(hdc, rect.left + ((rect.right - rect.left)/2) - 3*(strlen(this->text) - strlen(secondLine)), 
  154.     rect.top + (rect.bottom - rect.top)/2 - 15, this->text, strlen(this->text) - strlen(secondLine));
  155. TextOut(hdc, rect.left + ((rect.right - rect.left)/2) - 3*(strlen(secondLine) - 2), 
  156.     rect.top + (rect.bottom - rect.top)/2, secondLine + 1, strlen(secondLine) - 2);
  157. }
  158. }
  159. DeleteDC(hdc);
  160. }
  161. return 0;
  162. }
  163. int Subtitles::Close() {
  164. if(this->input) {
  165. this->input->Close();
  166. delete this->input;
  167. }
  168. return 0;
  169. }