SubtitlesMicroDVD.cpp
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:4k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. /**************************************************************************************
  2.  *                                                                                    *
  3.  *                                                                                    *
  4.  **************************************************************************************/
  5. #include "SubtitlesMicroDVD.h"
  6. MediaSubtitlerMicroDVD::MediaSubtitlerMicroDVD()
  7. {
  8. this->inputFile   = new MediaInput();
  9. this->inputBuffer = new MediaBuffer();
  10. }
  11. MediaSubtitlerMicroDVD::~MediaSubtitlerMicroDVD()
  12. {
  13. delete this->inputFile;
  14. delete this->inputBuffer;
  15. }
  16. media_type_t  MediaSubtitlerMicroDVD::GetType()
  17. {
  18. return MEDIA_TYPE_SUBTITLER;
  19. }
  20. char         *MediaSubtitlerMicroDVD::GetName()
  21. {
  22. return "MicroDVD Subtitle Reader";
  23. }
  24. MP_RESULT     MediaSubtitlerMicroDVD::Connect(MediaItem *item)
  25. {
  26. return MP_RESULT_OK;
  27. }
  28. MP_RESULT     MediaSubtitlerMicroDVD::ReleaseConnections()
  29. {
  30. return MP_RESULT_OK;
  31. }
  32. DWORD         MediaSubtitlerMicroDVD::GetCaps()
  33. {
  34. return 0;
  35. }
  36. MP_RESULT     MediaSubtitlerMicroDVD::Configure(HINSTANCE hInstance, HWND hwnd)
  37. {
  38. return MP_RESULT_ERROR;
  39. }
  40. MP_RESULT MediaSubtitlerMicroDVD::ParseSubtitle()
  41. {
  42. char text[512];
  43. DWORD i, j, count;
  44. if( sscanf((char *) this->inputBuffer->GetData(), "{%d}{%d}", &this->firstFrame, &this->lastFrame) != 2)
  45. return MP_RESULT_ERROR;
  46. strcpy(text, strrchr((char *) this->inputBuffer->GetData(), '}') + 1);
  47. i     = 0;
  48. count = 0;
  49. for(j=0; j < strlen(text); j++) {
  50. if(text[j] == '|') {
  51. this->subtitles.subtitlesText[i][count] = '';
  52. i++;
  53. count = 0;
  54. }
  55. else {
  56. if(text[j] == 'n') {
  57. this->subtitles.subtitlesText[i][count] = '';
  58. break;
  59. }
  60. else {
  61. this->subtitles.subtitlesText[i][count] = text[j];
  62. count++;
  63. }
  64. }
  65. }
  66. this->subtitles.nbSubtitles = i + 1;
  67. return MP_RESULT_OK;
  68. }
  69. MP_RESULT MediaSubtitlerMicroDVD::SeekToFrame(DWORD frameNumber)
  70. {
  71. if(frameNumber > this->lastFrame) {
  72. do {
  73. this->inputFile->GetLine(this->inputBuffer);
  74. this->previousLastFrame = this->lastFrame;
  75. if(sscanf((char *) this->inputBuffer->GetData(), "{%d}{%d}", &this->firstFrame, &this->lastFrame) != 2) {
  76. return MP_RESULT_ERROR;
  77. }
  78. }
  79. while(this->firstFrame < frameNumber);
  80. this->ParseSubtitle();
  81. }
  82. else {
  83. this->inputFile->Seek(0, INPUT_SEEK_SET);
  84. this->firstFrame = 0;
  85. this->lastFrame  = 0;
  86. this->previousLastFrame = 0;
  87. SeekToFrame(frameNumber);
  88. }
  89. return MP_RESULT_OK;
  90. }
  91. MP_RESULT    MediaSubtitlerMicroDVD::Open(char *lpFilename)
  92. {
  93. if(lpFilename) {
  94. this->firstFrame         = 0;
  95. this->lastFrame          = 0;
  96. this->firstSubtitleFrame = 0;
  97. this->previousLastFrame  = 0;
  98. if(this->inputFile->Open(lpFilename, INPUT_OPEN_ASCII) == MP_RESULT_OK) {
  99. this->inputBuffer->Alloc(SUB_INPUT_BUFFER_SIZE);
  100. this->subtitles.subtitlesText[0] = (char *) new char[256];
  101. this->subtitles.subtitlesText[1] = (char *) new char[256];
  102. this->subtitles.subtitlesText[2] = (char *) new char[256];
  103. this->subtitles.subtitlesText[3] = (char *) new char[256];
  104. this->subtitles.nbSubtitles = 0;
  105. this->inputFile->GetLine(this->inputBuffer);
  106. if(this->ParseSubtitle() == MP_RESULT_OK) {
  107. this->firstSubtitleFrame = this->firstFrame;
  108. this->previousLastFrame  = 0;
  109. return MP_RESULT_OK;
  110. }
  111. }
  112. }
  113. return MP_RESULT_ERROR;
  114. }
  115. subtitles_t *MediaSubtitlerMicroDVD::GetSubtitles(DWORD frameNumber)
  116. {
  117. if(frameNumber < this->firstSubtitleFrame) {
  118. return NULL;
  119. }
  120. if(frameNumber < this->firstFrame && frameNumber > this->previousLastFrame) {
  121. return NULL;
  122. }
  123. if(frameNumber >= this->firstFrame && frameNumber < this->lastFrame) {
  124. return &this->subtitles;
  125. }
  126. else {
  127. this->SeekToFrame(frameNumber);
  128. return NULL;
  129. }
  130. return NULL;
  131. }
  132. MP_RESULT    MediaSubtitlerMicroDVD::Close()
  133. {
  134. if(this->inputFile) {
  135. this->inputFile->Close();
  136. }
  137. return MP_RESULT_ERROR;
  138. }