CVideoHandler.cpp 10 KB

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