1
0

VideoThread.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "main.h"
  2. #include "VideoThread.h"
  3. #include "../Winamp/wa_ipc.h"
  4. VideoSample *video_sample=0;
  5. IVideoOutput *videoOutput=0;
  6. static bool video_reopen=false;
  7. static int height=0;
  8. static int width=0;
  9. static bool video_opened=false;
  10. static int consecutive_early_frames;
  11. HANDLE video_flush = 0, video_start_flushing=0, video_flush_done = 0, video_resume = 0;
  12. static HANDLE video_thread = 0;
  13. MP4SampleId nextVideoSampleId=1; // set in conjunction with video_flush
  14. static void OpenVideo()
  15. {
  16. if (!video_opened || video_reopen)
  17. {
  18. consecutive_early_frames = 0;
  19. if (!videoOutput)
  20. videoOutput = (IVideoOutput *)SendMessage(mod.hMainWindow, WM_WA_IPC, 0, IPC_GET_IVIDEOOUTPUT);
  21. int color_format;
  22. double aspect_ratio=1.0;
  23. if (video && video->GetOutputFormat(&width, &height, &color_format, &aspect_ratio) == MP4_VIDEO_SUCCESS)
  24. {
  25. videoOutput->extended(VIDUSER_SET_THREAD_SAFE, 1, 0);
  26. videoOutput->open(width, height, 0, 1.0/aspect_ratio, color_format);
  27. video_opened = true;
  28. video_reopen = false;
  29. }
  30. }
  31. }
  32. static DecodedVideoSample *GetNextPicture()
  33. {
  34. void *data, *decoder_data;
  35. MP4Timestamp timestamp=video_sample?(video_sample->timestamp):0;
  36. switch(video->GetPicture(&data, &decoder_data, &timestamp))
  37. {
  38. case MP4_VIDEO_OUTPUT_FORMAT_CHANGED:
  39. video_reopen=true;
  40. // fall through
  41. case MP4_VIDEO_SUCCESS:
  42. DecodedVideoSample *decoded = new DecodedVideoSample;
  43. decoded->decoder = video;
  44. decoded->decoder_data = decoder_data;
  45. decoded->timestamp = timestamp;
  46. decoded->output = data;
  47. return decoded;
  48. }
  49. return 0;
  50. }
  51. static void OutputPicture(DecodedVideoSample *decoded_video_sample)
  52. {
  53. if (decoded_video_sample)
  54. {
  55. int outputTime = (int)((decoded_video_sample->timestamp*1000ULL)/(uint64_t)m_video_timescale);
  56. again:
  57. MP4Duration realTime = GetClock();
  58. int time_diff = outputTime - realTime;
  59. if (time_diff > 12 && consecutive_early_frames) // plenty of time, go ahead and turn off frame dropping
  60. {
  61. if (--consecutive_early_frames == 0)
  62. video->HurryUp(0);
  63. }
  64. else if (time_diff < -50) // shit we're way late, start dropping frames
  65. {
  66. video->HurryUp(1);
  67. consecutive_early_frames += 3;
  68. }
  69. if (time_diff > 3)
  70. {
  71. HANDLE handles[] = {killEvent, video_start_flushing};
  72. if (WaitForMultipleObjects(2, handles, FALSE, outputTime-realTime) != WAIT_TIMEOUT)
  73. {
  74. delete decoded_video_sample;
  75. decoded_video_sample=0;
  76. return;
  77. }
  78. goto again; // TODO: handle paused state a little better than this
  79. }
  80. OpenVideo(); // open video if we havn't already
  81. videoOutput->draw(decoded_video_sample->output);
  82. delete decoded_video_sample;
  83. decoded_video_sample=0;
  84. /* TODO: probably want separate audio and video done flags
  85. if (temp->sampleId == numSamples) // done!
  86. done = true;
  87. */
  88. }
  89. }
  90. static bool ReadNextVideoSample()
  91. {
  92. while (nextVideoSampleId <= numVideoSamples)
  93. {
  94. VideoSample &sample=*video_sample;
  95. unsigned __int32 buffer_size = sample.inputSize;
  96. bool isSync=false;
  97. MP4Duration duration, offset;
  98. play_mp4_guard.Lock();
  99. bool sample_read=MP4ReadSample(MP4hFile, video_track, nextVideoSampleId++, (unsigned __int8 **)&sample.input, &buffer_size, &sample.timestamp, &duration, &offset, &isSync);
  100. play_mp4_guard.Unlock();
  101. if (sample_read)
  102. {
  103. // some buggy movies store signed int32 offsets, so let's deal with it
  104. offset = (uint32_t)offset;
  105. int32_t signed_offset = (int32_t)offset;
  106. sample.timestamp += signed_offset;
  107. //int outputTime = (int)((sample.timestamp*1000ULL) /(uint64_t)m_video_timescale);
  108. sample.inputValid = buffer_size;
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. static DWORD WINAPI VideoPlayThread(LPVOID parameter)
  115. {
  116. DWORD waitTime = 0;
  117. HANDLE handles[] = {killEvent, video_flush, video_start_flushing, video_resume};
  118. while (1)
  119. {
  120. int ret = WaitForMultipleObjects(4, handles, FALSE, waitTime);
  121. if (ret == WAIT_OBJECT_0) // kill
  122. break;
  123. else if (ret == WAIT_OBJECT_0+1) // flush
  124. {
  125. if (video)
  126. video->Flush();
  127. ResetEvent(video_flush);
  128. waitTime = 0;
  129. SetEvent(video_flush_done);
  130. }
  131. else if (ret == WAIT_OBJECT_0+2) // start flushing
  132. {
  133. waitTime = INFINITE; // this will stop us from decoding samples for a while
  134. ResetEvent(video_start_flushing);
  135. SetEvent(video_flush_done);
  136. }
  137. else if (ret == WAIT_OBJECT_0+3) // resume playback (like flush but don't flush the decoder)
  138. {
  139. ResetEvent(video_resume);
  140. waitTime = 0;
  141. SetEvent(video_flush_done);
  142. }
  143. else if (ret == WAIT_TIMEOUT)
  144. {
  145. if (ReadNextVideoSample())
  146. {
  147. int ret = video->DecodeSample(video_sample->input, video_sample->inputValid, video_sample->timestamp);
  148. if (ret == MP4_VIDEO_OUTPUT_FORMAT_CHANGED)
  149. video_reopen=true;
  150. if (ret == MP4_VIDEO_AGAIN)
  151. nextVideoSampleId--;
  152. DecodedVideoSample *picture = 0;
  153. while (picture = GetNextPicture())
  154. {
  155. OutputPicture(picture);
  156. }
  157. waitTime = 0;
  158. }
  159. else
  160. {
  161. // TODO: tell decoder end-of-file and get any buffers in queue
  162. if (!audio)
  163. PostMessage(mod.hMainWindow, WM_WA_MPEG_EOF, 0, 0);
  164. waitTime = INFINITE; // out of stuff to do, wait for kill or flush
  165. }
  166. }
  167. else // error
  168. break;
  169. }
  170. if (videoOutput)
  171. videoOutput->close();
  172. return 0;
  173. }
  174. void Video_Init()
  175. {
  176. width=0;
  177. height=0;
  178. video_reopen=false;
  179. video_opened=false;
  180. video_flush = CreateEvent(NULL, TRUE, FALSE, NULL);
  181. video_start_flushing = CreateEvent(NULL, TRUE, FALSE, NULL);
  182. video_flush_done = CreateEvent(NULL, FALSE, FALSE, NULL);
  183. video_resume = CreateEvent(NULL, TRUE, FALSE, NULL);
  184. video_thread = CreateThread(0, 0, VideoPlayThread, 0, 0, 0);
  185. }
  186. void Video_Close()
  187. {
  188. WaitForSingleObject(video_thread, INFINITE);
  189. CloseHandle(video_thread);
  190. video_thread = 0;
  191. CloseHandle(video_start_flushing);
  192. video_start_flushing=0;
  193. CloseHandle(video_flush);
  194. video_flush=0;
  195. CloseHandle(video_resume);
  196. video_resume=0;
  197. CloseHandle(video_flush_done);
  198. video_flush_done = 0;
  199. delete video_sample;
  200. video_sample=0;
  201. }