CVideoHandler.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. #include "StdInc.h"
  2. #include <SDL.h>
  3. #include "CVideoHandler.h"
  4. #include "UIFramework/CGuiHandler.h"
  5. #include "UIFramework/SDL_Extensions.h"
  6. #include "CPlayerInterface.h"
  7. #include "../lib/Filesystem/CResourceLoader.h"
  8. extern CGuiHandler GH; //global gui handler
  9. //reads events and returns true on key down
  10. static bool keyDown()
  11. {
  12. SDL_Event ev;
  13. while(SDL_PollEvent(&ev))
  14. {
  15. if(ev.type == SDL_KEYDOWN || ev.type == SDL_MOUSEBUTTONDOWN)
  16. return true;
  17. }
  18. return false;
  19. }
  20. #ifdef _WIN32
  21. #include <boost/algorithm/string/predicate.hpp>
  22. void checkForError(bool throwing = true)
  23. {
  24. int error = GetLastError();
  25. if(!error)
  26. return;
  27. tlog1 << "Error " << error << " encountered!\n";
  28. std::string msg;
  29. char* pTemp = NULL;
  30. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  31. NULL, error, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), (LPSTR)&pTemp, 1, NULL );
  32. tlog1 << "Error: " << pTemp << std::endl;
  33. msg = pTemp;
  34. LocalFree( pTemp );
  35. pTemp = NULL;
  36. if(throwing)
  37. throw std::runtime_error(msg);
  38. }
  39. void blitBuffer(char *buffer, int x, int y, int w, int h, SDL_Surface *dst)
  40. {
  41. const int bpp = dst->format->BytesPerPixel;
  42. char *dest;
  43. for(int i = h; i > 0; i--)
  44. {
  45. dest = (char*)dst->pixels + dst->pitch*(y+h-i) + x*dst->format->BytesPerPixel;
  46. memcpy(dest, buffer, bpp*w);
  47. buffer += bpp*w;
  48. }
  49. }
  50. void DLLHandler::Instantiate(const char *filename)
  51. {
  52. name = filename;
  53. dll = LoadLibraryA(filename);
  54. if(!dll)
  55. {
  56. tlog1 << "Failed loading " << filename << std::endl;
  57. checkForError(true);
  58. }
  59. }
  60. void *DLLHandler::FindAddress(const char *symbol)
  61. {
  62. void *ret;
  63. if(!dll)
  64. {
  65. tlog1 << "Cannot look for " << symbol << " because DLL hasn't been appropriately loaded!\n";
  66. return NULL;
  67. }
  68. ret = (void*) GetProcAddress(dll,symbol);
  69. if(!ret)
  70. {
  71. tlog1 << "Failed to find " << symbol << " in " << name << std::endl;
  72. checkForError();
  73. }
  74. return ret;
  75. }
  76. DLLHandler::~DLLHandler()
  77. {
  78. if(dll)
  79. {
  80. if(!FreeLibrary(dll))
  81. {
  82. tlog1 << "Failed to free " << name << std::endl;
  83. checkForError();
  84. }
  85. }
  86. }
  87. DLLHandler::DLLHandler()
  88. {
  89. dll = NULL;
  90. }
  91. CBIKHandler::CBIKHandler()
  92. {
  93. Instantiate("BINKW32.DLL");
  94. //binkGetError = FindAddress("_BinkGetError@0");
  95. binkOpen = (BinkOpen)FindAddress("_BinkOpen@8");
  96. binkSetSoundSystem = (BinkSetSoundSystem)FindAddress("_BinkSetSoundSystem@8");
  97. //getPalette = (BinkGetPalette)FindAddress("_BinkGetPalette@4");
  98. binkNextFrame = (BinkNextFrame)FindAddress("_BinkNextFrame@4");
  99. binkDoFrame = (BinkDoFrame)FindAddress("_BinkDoFrame@4");
  100. binkCopyToBuffer = (BinkCopyToBuffer)FindAddress("_BinkCopyToBuffer@28");
  101. binkWait = (BinkWait)FindAddress("_BinkWait@4");
  102. binkClose = (BinkClose)FindAddress("_BinkClose@4");
  103. hBinkFile = NULL;
  104. hBink = NULL;
  105. buffer = NULL;
  106. bufferSize = 0;
  107. }
  108. bool CBIKHandler::open(std::string name)
  109. {
  110. hBinkFile = CreateFileA
  111. (
  112. name.c_str(), // file name
  113. GENERIC_READ, // access mode
  114. FILE_SHARE_READ, // share mode
  115. NULL, // Security Descriptor
  116. OPEN_EXISTING, // how to create
  117. FILE_ATTRIBUTE_NORMAL,//FILE_FLAG_SEQUENTIAL_SCAN, // file attributes
  118. 0 // handle to template file
  119. );
  120. if(hBinkFile == INVALID_HANDLE_VALUE)
  121. {
  122. tlog1 << "BIK handler: failed to open " << name << std::endl;
  123. goto checkErrorAndClean;
  124. }
  125. //GCC wants scope of waveout to dont cross labels/swith/goto
  126. {
  127. void *waveout = GetProcAddress(dll,"_BinkOpenWaveOut@4");
  128. if(waveout)
  129. binkSetSoundSystem(waveout,NULL);
  130. }
  131. hBink = binkOpen(hBinkFile, 0x8a800000);
  132. if(!hBink)
  133. {
  134. tlog1 << "bink failed to open " << name << std::endl;
  135. goto checkErrorAndClean;
  136. }
  137. allocBuffer();
  138. return true;
  139. checkErrorAndClean:
  140. CloseHandle(hBinkFile);
  141. hBinkFile = NULL;
  142. checkForError();
  143. throw std::runtime_error("BIK failed opening video!");
  144. }
  145. void CBIKHandler::show( int x, int y, bool update )
  146. {
  147. const int w = hBink->width,
  148. h = hBink->height;
  149. binkDoFrame(hBink);
  150. binkCopyToBuffer(hBink, buffer, w*4, h, 0, 0, 1);
  151. //blitBuffer(buffer, x, y, w, h, dst);
  152. //if(update)
  153. // SDL_UpdateRect(dst, x, y, w, h);
  154. }
  155. bool CBIKHandler::nextFrame()
  156. {
  157. binkNextFrame(hBink);
  158. return true;
  159. }
  160. void CBIKHandler::close()
  161. {
  162. binkClose(hBink);
  163. hBink = NULL;
  164. CloseHandle(hBinkFile);
  165. hBinkFile = NULL;
  166. delete [] buffer;
  167. buffer = NULL;
  168. bufferSize = 0;
  169. }
  170. bool CBIKHandler::wait()
  171. {
  172. return binkWait(hBink);
  173. }
  174. int CBIKHandler::curFrame() const
  175. {
  176. return hBink->currentFrame;
  177. }
  178. int CBIKHandler::frameCount() const
  179. {
  180. return hBink->frameCount;
  181. }
  182. void CBIKHandler::redraw( int x, int y, bool update)
  183. {
  184. int w = hBink->width, h = hBink->height;
  185. //blitBuffer(buffer, x, y, w, h, dst);
  186. //if(update)
  187. // SDL_UpdateRect(dst, x, y, w, h);
  188. }
  189. void CBIKHandler::allocBuffer(int Bpp)
  190. {
  191. //* if(!Bpp) Bpp = screen->format->BytesPerPixel;
  192. bufferSize = hBink->width * hBink->height * Bpp;
  193. buffer = new char[bufferSize];
  194. }
  195. void CBIKHandler::freeBuffer()
  196. {
  197. delete [] buffer;
  198. buffer = NULL;
  199. bufferSize = 0;
  200. }
  201. bool CSmackPlayer::nextFrame()
  202. {
  203. ptrSmackNextFrame(data);
  204. return true;
  205. }
  206. bool CSmackPlayer::wait()
  207. {
  208. return ptrSmackWait(data);
  209. }
  210. CSmackPlayer::CSmackPlayer() : data(NULL)
  211. {
  212. Instantiate("smackw32.dll");
  213. ptrSmackNextFrame = (SmackNextFrame)FindAddress("_SmackNextFrame@4");
  214. ptrSmackWait = (SmackWait)FindAddress("_SmackWait@4");
  215. ptrSmackDoFrame = (SmackDoFrame)FindAddress("_SmackDoFrame@4");
  216. ptrSmackToBuffer = (SmackToBuffer)FindAddress("_SmackToBuffer@28");
  217. ptrSmackOpen = (SmackOpen)FindAddress("_SmackOpen@12");
  218. ptrSmackSoundOnOff = (SmackSoundOnOff)FindAddress("_SmackSoundOnOff@8");
  219. ptrSmackClose = (SmackClose)FindAddress("_SmackClose@4");
  220. ptrVolumePan = (SmackVolumePan)FindAddress("_SmackVolumePan@16");
  221. }
  222. CSmackPlayer::~CSmackPlayer()
  223. {
  224. if(data)
  225. close();
  226. }
  227. void CSmackPlayer::close()
  228. {
  229. ptrSmackClose(data);
  230. data = NULL;
  231. }
  232. bool CSmackPlayer::open( std::string name )
  233. {
  234. Uint32 flags[2] = {0xff400, 0xfe400};
  235. data = ptrSmackOpen( (void*)name.c_str(), flags[1], -1);
  236. if (!data)
  237. {
  238. tlog1 << "Smack cannot open " << name << std::endl;
  239. checkForError();
  240. throw std::runtime_error("SMACK failed opening video");
  241. }
  242. buffer = new char[data->width*data->height*2];
  243. buf = buffer+data->width*(data->height-1)*2; // adjust pointer position for later use by 'SmackToBuffer'
  244. //ptrVolumePan(data, 0xfe000, 3640 * GDefaultOptions.musicVolume / 11, 0x8000); //set volume
  245. return true;
  246. }
  247. void CSmackPlayer::show( int x, int y, bool update)
  248. {
  249. int w = data->width;
  250. int stripe = (-w*2) & (~3);
  251. //put frame to the buffer
  252. ptrSmackToBuffer(data, 0, 0, stripe, w, buf, 0x80000000);
  253. ptrSmackDoFrame(data);
  254. redraw(x, y, update);
  255. }
  256. int CSmackPlayer::curFrame() const
  257. {
  258. return data->currentFrame;
  259. }
  260. int CSmackPlayer::frameCount() const
  261. {
  262. return data->frameCount;
  263. }
  264. void CSmackPlayer::redraw( int x, int y, bool update )
  265. {
  266. //int w = std::min<int>(data->width, dst->w - x), h = std::min<int>(data->height, dst->h - y);
  267. int w = data->width, h = data->height;
  268. // draw the frame
  269. Uint16* addr = (Uint16*) (buffer+w*(h-1)*2-2);
  270. /*
  271. for( int j=0; j<h-1; j++) // why -1 ?
  272. {
  273. for ( int i=w-1; i>=0; i--)
  274. {
  275. Uint16 pixel = *addr;
  276. Uint8 *p = (Uint8 *)dst->pixels + (j+y) * dst->pitch + (i + x) * dst->format->BytesPerPixel;
  277. p[2] = ((pixel & 0x7c00) >> 10) * 8;
  278. p[1] = ((pixel & 0x3e0) >> 5) * 8;
  279. p[0] = ((pixel & 0x1F)) * 8;
  280. addr--;
  281. }
  282. }
  283. */
  284. //if(update)
  285. // SDL_UpdateRect(dst, x, y, w, h);
  286. }
  287. CVideoPlayer::CVideoPlayer()
  288. {
  289. current = NULL;
  290. }
  291. CVideoPlayer::~CVideoPlayer()
  292. {
  293. }
  294. bool CVideoPlayer::open(std::string name)
  295. {
  296. fname = name;
  297. first = true;
  298. try
  299. {
  300. // Extract video from video.vid so we can play it.
  301. // We can handle only videos in form of single file, no archive support yet.
  302. {
  303. ResourceID videoID = ResourceID("VIDEO/" + name, EResType::VIDEO);
  304. std::string realVideoFilename = CResourceHandler::get()->getResourceName(videoID);
  305. if(boost::algorithm::iends_with(realVideoFilename, ".BIK"))
  306. current = &bikPlayer;
  307. else
  308. current = &smkPlayer;
  309. auto myVideo = CResourceHandler::get()->load(videoID);
  310. unique_ptr<char[]> data = unique_ptr<char[]>(new char[myVideo->getSize()]);
  311. myVideo->read((ui8*)data.get(), myVideo->getSize());
  312. std::ofstream out(name, std::ofstream::binary);
  313. out.exceptions(std::ifstream::failbit | std::ifstream::badbit);
  314. out.write(data.get(), myVideo->getSize());
  315. }
  316. current->open(name);
  317. return true;
  318. }
  319. catch(std::exception &e)
  320. {
  321. current = nullptr;
  322. tlog3 << "Failed to open video file " << name << ": " << e.what() << std::endl;
  323. }
  324. return false;
  325. }
  326. void CVideoPlayer::close()
  327. {
  328. if(!current)
  329. {
  330. tlog2 << "Closing no opened player...?" << std::endl;
  331. return;
  332. }
  333. current->close();
  334. current = NULL;
  335. if(!DeleteFileA(fname.c_str()))
  336. {
  337. tlog1 << "Cannot remove temporarily extracted video file: " << fname;
  338. checkForError(false);
  339. }
  340. fname.clear();
  341. }
  342. bool CVideoPlayer::nextFrame()
  343. {
  344. if(current)
  345. {
  346. current->nextFrame();
  347. return true;
  348. }
  349. else
  350. return false;
  351. }
  352. void CVideoPlayer::show(int x, int y, bool update)
  353. {
  354. if(current)
  355. current->show(x, y, update);
  356. }
  357. bool CVideoPlayer::wait()
  358. {
  359. if(current)
  360. return current->wait();
  361. else
  362. return false;
  363. }
  364. int CVideoPlayer::curFrame() const
  365. {
  366. if(current)
  367. return current->curFrame();
  368. else
  369. return -1;
  370. }
  371. int CVideoPlayer::frameCount() const
  372. {
  373. if(current)
  374. return current->frameCount();
  375. else
  376. return -1;
  377. }
  378. bool CVideoPlayer::openAndPlayVideo(std::string name, int x, int y, bool stopOnKey)
  379. {
  380. if(!open(name))
  381. return false;
  382. bool ret = playVideo(x, y, stopOnKey);
  383. close();
  384. return ret;
  385. }
  386. void CVideoPlayer::update( int x, int y, bool forceRedraw, bool update )
  387. {
  388. if(!current)
  389. return;
  390. bool w = false;
  391. if(!first)
  392. {
  393. w = wait(); //check if should keep current frame
  394. if(!w)
  395. nextFrame();
  396. }
  397. else
  398. {
  399. first = false;
  400. }
  401. if(!w)
  402. {
  403. show(x, y, update);
  404. }
  405. else if (forceRedraw)
  406. {
  407. redraw(x, y, update);
  408. }
  409. }
  410. void CVideoPlayer::redraw( int x, int y, bool update)
  411. {
  412. if(current)
  413. current->redraw(x, y, update);
  414. }
  415. bool CVideoPlayer::playVideo(int x, int y, bool stopOnKey)
  416. {
  417. if(!current)
  418. return false;
  419. int frame = 0;
  420. while(frame < frameCount()) //play all frames
  421. {
  422. if(stopOnKey && keyDown())
  423. return false;
  424. if(!wait())
  425. {
  426. show(x, y);
  427. nextFrame();
  428. frame++;
  429. }
  430. SDL_Delay(20);
  431. }
  432. return true;
  433. }
  434. #else
  435. #ifndef DISABLE_VIDEO
  436. // Define a set of functions to read data
  437. static int lodRead(void* opaque, uint8_t* buf, int size)
  438. {
  439. auto video = reinterpret_cast<CVideoPlayer *>(opaque);
  440. return video->data->read(buf, size);
  441. }
  442. static si64 lodSeek(void * opaque, si64 pos, int whence)
  443. {
  444. auto video = reinterpret_cast<CVideoPlayer *>(opaque);
  445. if (whence & AVSEEK_SIZE)
  446. return video->data->getSize();
  447. return video->data->seek(pos);
  448. }
  449. CVideoPlayer::CVideoPlayer()
  450. {
  451. format = NULL;
  452. frame = NULL;
  453. codec = NULL;
  454. sws = NULL;
  455. overlay = NULL;
  456. dest = NULL;
  457. context = nullptr;
  458. // Register codecs. TODO: May be overkill. Should call a
  459. // combination of av_register_input_format() /
  460. // av_register_output_format() / av_register_protocol() instead.
  461. av_register_all();
  462. }
  463. bool CVideoPlayer::open(std::string fname)
  464. {
  465. return open(fname, true, false);
  466. }
  467. // loop = to loop through the video
  468. // useOverlay = directly write to the screen.
  469. bool CVideoPlayer::open(std::string fname, bool loop, bool useOverlay)
  470. {
  471. close();
  472. this->fname = fname;
  473. refreshWait = 3;
  474. refreshCount = -1;
  475. doLoop = loop;
  476. ResourceID resource(std::string("Video/") + fname, EResType::VIDEO);
  477. if (!CResourceHandler::get()->existsResource(resource))
  478. {
  479. tlog0 << "Error: video " << resource.getName() << " was not found\n";
  480. return false;
  481. }
  482. data = CResourceHandler::get()->load(resource);
  483. static const int BUFFER_SIZE = 4096;
  484. unsigned char * buffer = (unsigned char *)av_malloc(BUFFER_SIZE);// will be freed by ffmpeg
  485. context = avio_alloc_context( buffer, BUFFER_SIZE, 0, (void *)this, lodRead, NULL, lodSeek);
  486. format = avformat_alloc_context();
  487. format->pb = context;
  488. // filename is not needed - file was already open and stored in this->data;
  489. int avfopen = avformat_open_input(&format, "dummyFilename", nullptr, nullptr);
  490. if (avfopen != 0)
  491. {
  492. return false;
  493. }
  494. // Retrieve stream information
  495. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 17, 0)
  496. if (av_find_stream_info(format) < 0)
  497. #else
  498. if (avformat_find_stream_info(format, NULL) < 0)
  499. #endif
  500. return false;
  501. // Find the first video stream
  502. stream = -1;
  503. for(ui32 i=0; i<format->nb_streams; i++)
  504. {
  505. if (format->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
  506. {
  507. stream = i;
  508. break;
  509. }
  510. }
  511. if (stream < 0)
  512. // No video stream in that file
  513. return false;
  514. // Get a pointer to the codec context for the video stream
  515. codecContext = format->streams[stream]->codec;
  516. // Find the decoder for the video stream
  517. codec = avcodec_find_decoder(codecContext->codec_id);
  518. if (codec == NULL)
  519. {
  520. // Unsupported codec
  521. return false;
  522. }
  523. // Open codec
  524. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 6, 0)
  525. if ( avcodec_open(codecContext, codec) < 0 )
  526. #else
  527. if ( avcodec_open2(codecContext, codec, NULL) < 0 )
  528. #endif
  529. {
  530. // Could not open codec
  531. codec = NULL;
  532. return false;
  533. }
  534. // Allocate video frame
  535. frame = avcodec_alloc_frame();
  536. // Allocate a place to put our YUV image on that screen
  537. if (useOverlay)
  538. {
  539. overlay = SDL_CreateYUVOverlay(codecContext->width, codecContext->height,
  540. SDL_YV12_OVERLAY, screen);
  541. }
  542. else
  543. {
  544. dest = CSDL_Ext::newSurface(codecContext->width, codecContext->height);
  545. destRect.x = destRect.y = 0;
  546. destRect.w = codecContext->width;
  547. destRect.h = codecContext->height;
  548. }
  549. if (overlay == NULL && dest == NULL)
  550. return false;
  551. // Convert the image into YUV format that SDL uses
  552. if (overlay)
  553. {
  554. sws = sws_getContext(codecContext->width, codecContext->height,
  555. codecContext->pix_fmt, codecContext->width, codecContext->height,
  556. PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
  557. }
  558. else
  559. {
  560. PixelFormat screenFormat = PIX_FMT_NONE;
  561. if (screen->format->Bshift > screen->format->Rshift)
  562. {
  563. // this a BGR surface
  564. switch (screen->format->BytesPerPixel)
  565. {
  566. case 2: screenFormat = PIX_FMT_BGR565; break;
  567. case 3: screenFormat = PIX_FMT_BGR24; break;
  568. case 4: screenFormat = PIX_FMT_BGR32; break;
  569. default: return false;
  570. }
  571. }
  572. else
  573. {
  574. // this a RGB surface
  575. switch (screen->format->BytesPerPixel)
  576. {
  577. case 2: screenFormat = PIX_FMT_RGB565; break;
  578. case 3: screenFormat = PIX_FMT_RGB24; break;
  579. case 4: screenFormat = PIX_FMT_RGB32; break;
  580. default: return false;
  581. }
  582. }
  583. sws = sws_getContext(codecContext->width, codecContext->height,
  584. codecContext->pix_fmt, codecContext->width, codecContext->height,
  585. screenFormat, SWS_BICUBIC, NULL, NULL, NULL);
  586. }
  587. if (sws == NULL)
  588. return false;
  589. pos.w = codecContext->width;
  590. pos.h = codecContext->height;
  591. return true;
  592. }
  593. // Read the next frame. Return false on error/end of file.
  594. bool CVideoPlayer::nextFrame()
  595. {
  596. AVPacket packet;
  597. int frameFinished = 0;
  598. bool gotError = false;
  599. if (sws == NULL)
  600. return false;
  601. while(!frameFinished)
  602. {
  603. int ret = av_read_frame(format, &packet);
  604. if (ret < 0)
  605. {
  606. // Error. It's probably an end of file.
  607. if (doLoop && !gotError)
  608. {
  609. // Rewind
  610. if (av_seek_frame(format, stream, 0, 0) < 0)
  611. break;
  612. gotError = true;
  613. }
  614. else
  615. {
  616. break;
  617. }
  618. }
  619. else
  620. {
  621. // Is this a packet from the video stream?
  622. if (packet.stream_index == stream)
  623. {
  624. // Decode video frame
  625. avcodec_decode_video2(codecContext, frame, &frameFinished, &packet);
  626. // Did we get a video frame?
  627. if (frameFinished)
  628. {
  629. AVPicture pict;
  630. if (overlay) {
  631. SDL_LockYUVOverlay(overlay);
  632. pict.data[0] = overlay->pixels[0];
  633. pict.data[1] = overlay->pixels[2];
  634. pict.data[2] = overlay->pixels[1];
  635. pict.linesize[0] = overlay->pitches[0];
  636. pict.linesize[1] = overlay->pitches[2];
  637. pict.linesize[2] = overlay->pitches[1];
  638. sws_scale(sws, frame->data, frame->linesize,
  639. 0, codecContext->height, pict.data, pict.linesize);
  640. SDL_UnlockYUVOverlay(overlay);
  641. }
  642. else
  643. {
  644. pict.data[0] = (ui8 *)dest->pixels;
  645. pict.linesize[0] = dest->pitch;
  646. sws_scale(sws, frame->data, frame->linesize,
  647. 0, codecContext->height, pict.data, pict.linesize);
  648. }
  649. }
  650. }
  651. av_free_packet(&packet);
  652. }
  653. }
  654. return frameFinished != 0;
  655. }
  656. void CVideoPlayer::show( int x, int y, SDL_Surface *dst, bool update )
  657. {
  658. if (sws == NULL)
  659. return;
  660. pos.x = x;
  661. pos.y = y;
  662. CSDL_Ext::blitSurface(dest, &destRect, dst, &pos);
  663. if (update)
  664. SDL_UpdateRect(dst, pos.x, pos.y, pos.w, pos.h);
  665. }
  666. void CVideoPlayer::redraw( int x, int y, SDL_Surface *dst, bool update )
  667. {
  668. show(x, y, dst, update);
  669. }
  670. void CVideoPlayer::update( int x, int y, SDL_Surface *dst, bool forceRedraw, bool update )
  671. {
  672. if (sws == NULL)
  673. return;
  674. if (refreshCount <= 0)
  675. {
  676. refreshCount = refreshWait;
  677. if (nextFrame())
  678. show(x,y,dst,update);
  679. else
  680. {
  681. open(fname);
  682. nextFrame();
  683. // The y position is wrong at the first frame.
  684. // Note: either the windows player or the linux player is
  685. // broken. Compensate here until the bug is found.
  686. show(x, y--, dst, update);
  687. }
  688. }
  689. else
  690. {
  691. redraw(x, y, dst, update);
  692. }
  693. refreshCount --;
  694. }
  695. void CVideoPlayer::close()
  696. {
  697. fname = "";
  698. if (sws)
  699. {
  700. sws_freeContext(sws);
  701. sws = NULL;
  702. }
  703. if (overlay)
  704. {
  705. SDL_FreeYUVOverlay(overlay);
  706. overlay = NULL;
  707. }
  708. if (dest)
  709. {
  710. SDL_FreeSurface(dest);
  711. dest = NULL;
  712. }
  713. if (frame)
  714. {
  715. av_free(frame);
  716. frame = NULL;
  717. }
  718. if (codec)
  719. {
  720. avcodec_close(codecContext);
  721. codec = NULL;
  722. codecContext = NULL;
  723. }
  724. if (format)
  725. {
  726. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 17, 0)
  727. av_close_input_file(format);
  728. format = NULL;
  729. #else
  730. avformat_close_input(&format);
  731. #endif
  732. }
  733. if (context)
  734. {
  735. av_free(context);
  736. context = nullptr;
  737. }
  738. }
  739. // Plays a video. Only works for overlays.
  740. bool CVideoPlayer::playVideo(int x, int y, SDL_Surface *dst, bool stopOnKey)
  741. {
  742. // Note: either the windows player or the linux player is
  743. // broken. Compensate here until the bug is found.
  744. y--;
  745. pos.x = x;
  746. pos.y = y;
  747. while(nextFrame())
  748. {
  749. if(stopOnKey && keyDown())
  750. return false;
  751. SDL_DisplayYUVOverlay(overlay, &pos);
  752. // Wait 3 frames
  753. GH.mainFPSmng->framerateDelay();
  754. GH.mainFPSmng->framerateDelay();
  755. GH.mainFPSmng->framerateDelay();
  756. }
  757. return true;
  758. }
  759. bool CVideoPlayer::openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey)
  760. {
  761. open(name, false, true);
  762. bool ret = playVideo(x, y, dst, stopOnKey);
  763. close();
  764. return ret;
  765. }
  766. CVideoPlayer::~CVideoPlayer()
  767. {
  768. close();
  769. }
  770. #endif
  771. #endif