CVideoHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. #include "StdInc.h"
  2. #include <SDL.h>
  3. #include "CVideoHandler.h"
  4. #include "gui/CGuiHandler.h"
  5. #include "gui/SDL_Extensions.h"
  6. #include "CPlayerInterface.h"
  7. #include "../lib/filesystem/Filesystem.h"
  8. extern CGuiHandler GH; //global gui handler
  9. #ifndef DISABLE_VIDEO
  10. //reads events and returns true on key down
  11. static bool keyDown()
  12. {
  13. SDL_Event ev;
  14. while(SDL_PollEvent(&ev))
  15. {
  16. if(ev.type == SDL_KEYDOWN || ev.type == SDL_MOUSEBUTTONDOWN)
  17. return true;
  18. }
  19. return false;
  20. }
  21. #ifdef _MSC_VER
  22. #pragma comment(lib, "avcodec.lib")
  23. #pragma comment(lib, "avutil.lib")
  24. #pragma comment(lib, "avformat.lib")
  25. #pragma comment(lib, "swscale.lib")
  26. #endif // _MSC_VER
  27. // Define a set of functions to read data
  28. static int lodRead(void* opaque, uint8_t* buf, int size)
  29. {
  30. auto video = reinterpret_cast<CVideoPlayer *>(opaque);
  31. return video->data->read(buf, size);
  32. }
  33. static si64 lodSeek(void * opaque, si64 pos, int whence)
  34. {
  35. auto video = reinterpret_cast<CVideoPlayer *>(opaque);
  36. if (whence & AVSEEK_SIZE)
  37. return video->data->getSize();
  38. return video->data->seek(pos);
  39. }
  40. CVideoPlayer::CVideoPlayer()
  41. {
  42. format = nullptr;
  43. frame = nullptr;
  44. codec = nullptr;
  45. sws = nullptr;
  46. #ifdef VCMI_SDL1
  47. overlay = nullptr;
  48. #else
  49. texture = nullptr;
  50. #endif
  51. dest = nullptr;
  52. context = nullptr;
  53. // Register codecs. TODO: May be overkill. Should call a
  54. // combination of av_register_input_format() /
  55. // av_register_output_format() / av_register_protocol() instead.
  56. av_register_all();
  57. }
  58. bool CVideoPlayer::open(std::string fname, bool scale/* = false*/)
  59. {
  60. return open(fname, true, false);
  61. }
  62. // loop = to loop through the video
  63. // useOverlay = directly write to the screen.
  64. bool CVideoPlayer::open(std::string fname, bool loop, bool useOverlay, bool scale /*= false*/)
  65. {
  66. close();
  67. this->fname = fname;
  68. refreshWait = 3;
  69. refreshCount = -1;
  70. doLoop = loop;
  71. ResourceID resource(std::string("Video/") + fname, EResType::VIDEO);
  72. if (!CResourceHandler::get()->existsResource(resource))
  73. {
  74. logGlobal->errorStream() << "Error: video " << resource.getName() << " was not found";
  75. return false;
  76. }
  77. data = CResourceHandler::get()->load(resource);
  78. static const int BUFFER_SIZE = 4096;
  79. unsigned char * buffer = (unsigned char *)av_malloc(BUFFER_SIZE);// will be freed by ffmpeg
  80. context = avio_alloc_context( buffer, BUFFER_SIZE, 0, (void *)this, lodRead, nullptr, lodSeek);
  81. format = avformat_alloc_context();
  82. format->pb = context;
  83. // filename is not needed - file was already open and stored in this->data;
  84. int avfopen = avformat_open_input(&format, "dummyFilename", nullptr, nullptr);
  85. if (avfopen != 0)
  86. {
  87. return false;
  88. }
  89. // Retrieve stream information
  90. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 17, 0)
  91. if (av_find_stream_info(format) < 0)
  92. #else
  93. if (avformat_find_stream_info(format, nullptr) < 0)
  94. #endif
  95. return false;
  96. // Find the first video stream
  97. stream = -1;
  98. for(ui32 i=0; i<format->nb_streams; i++)
  99. {
  100. if (format->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
  101. {
  102. stream = i;
  103. break;
  104. }
  105. }
  106. if (stream < 0)
  107. // No video stream in that file
  108. return false;
  109. // Get a pointer to the codec context for the video stream
  110. codecContext = format->streams[stream]->codec;
  111. // Find the decoder for the video stream
  112. codec = avcodec_find_decoder(codecContext->codec_id);
  113. if (codec == nullptr)
  114. {
  115. // Unsupported codec
  116. return false;
  117. }
  118. // Open codec
  119. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 6, 0)
  120. if ( avcodec_open(codecContext, codec) < 0 )
  121. #else
  122. if ( avcodec_open2(codecContext, codec, nullptr) < 0 )
  123. #endif
  124. {
  125. // Could not open codec
  126. codec = nullptr;
  127. return false;
  128. }
  129. // Allocate video frame
  130. frame = avcodec_alloc_frame();
  131. //setup scaling
  132. if(scale)
  133. {
  134. pos.w = screen->w;
  135. pos.h = screen->h;
  136. }
  137. else
  138. {
  139. pos.w = codecContext->width;
  140. pos.h = codecContext->height;
  141. }
  142. // Allocate a place to put our YUV image on that screen
  143. if (useOverlay)
  144. {
  145. #ifdef VCMI_SDL1
  146. overlay = SDL_CreateYUVOverlay(pos.w, pos.h,
  147. SDL_YV12_OVERLAY, screen);
  148. #else
  149. texture = SDL_CreateTexture( mainRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STATIC, pos.w, pos.h);
  150. #endif
  151. }
  152. else
  153. {
  154. dest = CSDL_Ext::newSurface(pos.w, pos.h);
  155. destRect.x = destRect.y = 0;
  156. destRect.w = pos.w;
  157. destRect.h = pos.h;
  158. }
  159. #ifdef VCMI_SDL1
  160. if (overlay == nullptr && dest == nullptr)
  161. return false;
  162. if (overlay)
  163. #else
  164. if (texture == nullptr && dest == nullptr)
  165. return false;
  166. if (texture)
  167. #endif
  168. { // Convert the image into YUV format that SDL uses
  169. sws = sws_getContext(codecContext->width, codecContext->height, codecContext->pix_fmt,
  170. pos.w, pos.h, PIX_FMT_YUV420P,
  171. SWS_BICUBIC, nullptr, nullptr, nullptr);
  172. }
  173. else
  174. {
  175. PixelFormat screenFormat = PIX_FMT_NONE;
  176. if (screen->format->Bshift > screen->format->Rshift)
  177. {
  178. // this a BGR surface
  179. switch (screen->format->BytesPerPixel)
  180. {
  181. case 2: screenFormat = PIX_FMT_BGR565; break;
  182. case 3: screenFormat = PIX_FMT_BGR24; break;
  183. case 4: screenFormat = PIX_FMT_BGR32; break;
  184. default: return false;
  185. }
  186. }
  187. else
  188. {
  189. // this a RGB surface
  190. switch (screen->format->BytesPerPixel)
  191. {
  192. case 2: screenFormat = PIX_FMT_RGB565; break;
  193. case 3: screenFormat = PIX_FMT_RGB24; break;
  194. case 4: screenFormat = PIX_FMT_RGB32; break;
  195. default: return false;
  196. }
  197. }
  198. sws = sws_getContext(codecContext->width, codecContext->height, codecContext->pix_fmt,
  199. pos.w, pos.h, screenFormat,
  200. SWS_BICUBIC, nullptr, nullptr, nullptr);
  201. }
  202. if (sws == nullptr)
  203. return false;
  204. return true;
  205. }
  206. // Read the next frame. Return false on error/end of file.
  207. bool CVideoPlayer::nextFrame()
  208. {
  209. AVPacket packet;
  210. int frameFinished = 0;
  211. bool gotError = false;
  212. if (sws == nullptr)
  213. return false;
  214. while(!frameFinished)
  215. {
  216. int ret = av_read_frame(format, &packet);
  217. if (ret < 0)
  218. {
  219. // Error. It's probably an end of file.
  220. if (doLoop && !gotError)
  221. {
  222. // Rewind
  223. if (av_seek_frame(format, stream, 0, AVSEEK_FLAG_BYTE) < 0)
  224. break;
  225. gotError = true;
  226. }
  227. else
  228. {
  229. break;
  230. }
  231. }
  232. else
  233. {
  234. // Is this a packet from the video stream?
  235. if (packet.stream_index == stream)
  236. {
  237. // Decode video frame
  238. avcodec_decode_video2(codecContext, frame, &frameFinished, &packet);
  239. // Did we get a video frame?
  240. if (frameFinished)
  241. {
  242. AVPicture pict;
  243. #ifdef VCMI_SDL1
  244. if (overlay) {
  245. SDL_LockYUVOverlay(overlay);
  246. pict.data[0] = overlay->pixels[0];
  247. pict.data[1] = overlay->pixels[2];
  248. pict.data[2] = overlay->pixels[1];
  249. pict.linesize[0] = overlay->pitches[0];
  250. pict.linesize[1] = overlay->pitches[2];
  251. pict.linesize[2] = overlay->pitches[1];
  252. sws_scale(sws, frame->data, frame->linesize,
  253. 0, codecContext->height, pict.data, pict.linesize);
  254. SDL_UnlockYUVOverlay(overlay);
  255. #else
  256. if (texture) {
  257. avpicture_alloc(&pict, AV_PIX_FMT_YUV420P, pos.w, pos.h);
  258. sws_scale(sws, frame->data, frame->linesize,
  259. 0, codecContext->height, pict.data, pict.linesize);
  260. SDL_UpdateYUVTexture(texture, NULL, pict.data[0], pict.linesize[0],
  261. pict.data[1], pict.linesize[1],
  262. pict.data[2], pict.linesize[2]);
  263. avpicture_free(&pict);
  264. #endif
  265. }
  266. else
  267. {
  268. pict.data[0] = (ui8 *)dest->pixels;
  269. pict.linesize[0] = dest->pitch;
  270. sws_scale(sws, frame->data, frame->linesize,
  271. 0, codecContext->height, pict.data, pict.linesize);
  272. }
  273. }
  274. }
  275. av_free_packet(&packet);
  276. }
  277. }
  278. return frameFinished != 0;
  279. }
  280. void CVideoPlayer::show( int x, int y, SDL_Surface *dst, bool update )
  281. {
  282. if (sws == nullptr)
  283. return;
  284. pos.x = x;
  285. pos.y = y;
  286. CSDL_Ext::blitSurface(dest, &destRect, dst, &pos);
  287. if (update)
  288. SDL_UpdateRect(dst, pos.x, pos.y, pos.w, pos.h);
  289. }
  290. void CVideoPlayer::redraw( int x, int y, SDL_Surface *dst, bool update )
  291. {
  292. show(x, y, dst, update);
  293. }
  294. void CVideoPlayer::update( int x, int y, SDL_Surface *dst, bool forceRedraw, bool update )
  295. {
  296. if (sws == nullptr)
  297. return;
  298. if (refreshCount <= 0)
  299. {
  300. refreshCount = refreshWait;
  301. if (nextFrame())
  302. show(x,y,dst,update);
  303. else
  304. {
  305. open(fname);
  306. nextFrame();
  307. // The y position is wrong at the first frame.
  308. // Note: either the windows player or the linux player is
  309. // broken. Compensate here until the bug is found.
  310. show(x, y--, dst, update);
  311. }
  312. }
  313. else
  314. {
  315. redraw(x, y, dst, update);
  316. }
  317. refreshCount --;
  318. }
  319. void CVideoPlayer::close()
  320. {
  321. fname = "";
  322. if (sws)
  323. {
  324. sws_freeContext(sws);
  325. sws = nullptr;
  326. }
  327. #ifdef VCMI_SDL1
  328. if (overlay)
  329. {
  330. SDL_FreeYUVOverlay(overlay);
  331. overlay = nullptr;
  332. }
  333. #else
  334. if (texture)
  335. {
  336. SDL_DestroyTexture(texture);
  337. texture = nullptr;
  338. }
  339. #endif
  340. if (dest)
  341. {
  342. SDL_FreeSurface(dest);
  343. dest = nullptr;
  344. }
  345. if (frame)
  346. {
  347. av_free(frame);
  348. frame = nullptr;
  349. }
  350. if (codec)
  351. {
  352. avcodec_close(codecContext);
  353. codec = nullptr;
  354. codecContext = nullptr;
  355. }
  356. if (format)
  357. {
  358. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 17, 0)
  359. av_close_input_file(format);
  360. format = nullptr;
  361. #else
  362. avformat_close_input(&format);
  363. #endif
  364. }
  365. if (context)
  366. {
  367. av_free(context);
  368. context = nullptr;
  369. }
  370. }
  371. // Plays a video. Only works for overlays.
  372. bool CVideoPlayer::playVideo(int x, int y, SDL_Surface *dst, bool stopOnKey)
  373. {
  374. // Note: either the windows player or the linux player is
  375. // broken. Compensate here until the bug is found.
  376. y--;
  377. pos.x = x;
  378. pos.y = y;
  379. while(nextFrame())
  380. {
  381. if(stopOnKey && keyDown())
  382. return false;
  383. #ifdef VCMI_SDL1
  384. SDL_DisplayYUVOverlay(overlay, &pos);
  385. #else
  386. SDL_RenderCopy(mainRenderer, texture, NULL, &pos);
  387. SDL_RenderPresent(mainRenderer);
  388. #endif
  389. // Wait 3 frames
  390. GH.mainFPSmng->framerateDelay();
  391. GH.mainFPSmng->framerateDelay();
  392. GH.mainFPSmng->framerateDelay();
  393. }
  394. return true;
  395. }
  396. bool CVideoPlayer::openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey, bool scale/* = false*/)
  397. {
  398. open(name, false, true, scale);
  399. bool ret = playVideo(x, y, dst, stopOnKey);
  400. close();
  401. return ret;
  402. }
  403. CVideoPlayer::~CVideoPlayer()
  404. {
  405. close();
  406. }
  407. #endif