CVideoHandler.cpp 19 KB

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