CVideoHandler.cpp 19 KB

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