CVideoHandler.cpp 10 KB

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