CVideoHandler.cpp 10 KB

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