comparison VideoPlayer.h @ 2142:ca548138d6aa

some code cleaning in video playing, loops play but there is a memory leak
author zipi
date Thu, 02 Jan 2014 18:25:42 +0000
parents d24ee391fd1f
children 0b2eea6f80c9
comparison
equal deleted inserted replaced
2141:1283eedcc028 2142:ca548138d6aa
520 520
521 template<int NUM_PRECACHED_FRAMES> 521 template<int NUM_PRECACHED_FRAMES>
522 class MovieCached 522 class MovieCached
523 { 523 {
524 public: 524 public:
525 bool Stopped() const { return stopped; } 525 bool Stopped() { return stopped; }
526 526 int GetWidth() { return width; }
527 int GetHeight() { return height; }
528 inline ~MovieCached()
529 {
530 Release();
531 }
527 protected: 532 protected:
528 friend class MultimediaPlayer; 533 friend class MultimediaPlayer;
529 inline MovieCached(OpenALSoundProvider *sound_provider) 534 inline MovieCached(OpenALSoundProvider *sound_provider)
530 { 535 {
531 this->format_ctx = nullptr; 536 this->format_ctx = nullptr;
539 544
540 this->audio_stream_idx = -1; 545 this->audio_stream_idx = -1;
541 this->audio_stream = nullptr; 546 this->audio_stream = nullptr;
542 this->audio_stream_dec = nullptr; 547 this->audio_stream_dec = nullptr;
543 this->audio_stream_dec_ctx = nullptr; 548 this->audio_stream_dec_ctx = nullptr;
549
550 packet = nullptr;
551 ioBuffer = nullptr;
552 format_ctx = nullptr;
553 avioContext = nullptr;
554 }
555
556 bool LoadFromLOD(HANDLE h, int readFunction(void*, uint8_t*, int), int64_t seekFunction(void*, int64_t, int), int width, int height)
557 {
558 if (!ioBuffer)
559 ioBuffer = (unsigned char *)av_malloc(16384 + FF_INPUT_BUFFER_PADDING_SIZE); // can get av_free()ed by libav
560 if (!avioContext)
561 avioContext = avio_alloc_context(ioBuffer, 16384, 0, h, &readFunction, NULL, &seekFunction);
562 if (!format_ctx)
563 format_ctx = avformat_alloc_context();
564 format_ctx->pb = avioContext;
565 return Load("dummyFilename", width, height);
544 } 566 }
545 567
546 bool Load(const char *video_filename, int width, int height) 568 bool Load(const char *video_filename, int width, int height)
547 { 569 {
548 this->width = width; 570 this->width = width;
549 this->height = height; 571 this->height = height;
550 572
551 /* for (uint i = 0; i < pVideoPlayer->uNumMagicVideoHeaders; ++i)
552 if (!_stricmp(video_filename, pVideoPlayer->pMagicVideoHeaders[i].pVideoName))
553 {
554 SetFilePointer(pVideoPlayer->hMagicVid, pVideoPlayer->pMagicVideoHeaders[i].uFileOffset, 0, FILE_BEGIN);
555 break;
556 }
557 */ packet = nullptr;
558 int ioBufferSize = 32768;
559 void *src;
560
561 unsigned char * ioBuffer = (unsigned char *)av_malloc(ioBufferSize + FF_INPUT_BUFFER_PADDING_SIZE); // can get av_free()ed by libav
562 if (pVideoPlayer->uVidFile)
563 {
564 src = (void*)pVideoPlayer->hMagicVid;
565 }
566 else
567 {
568 src = (void*)pVideoPlayer->hMightVid;
569 }
570
571 avioContext = avio_alloc_context(ioBuffer, ioBufferSize, 0, src, &readFunction, NULL, &seekFunction);
572 //offset = SetFilePointer(pVideoPlayer->hMagicVid,0,0,FILE_CURRENT);
573 LARGE_INTEGER li, li2;
574 PLARGE_INTEGER pli = &li2;
575 li.QuadPart = 0;
576 SetFilePointerEx(pVideoPlayer->hMagicVid, li, pli, FILE_CURRENT);
577 offset = pli->QuadPart;
578
579 format_ctx = avformat_alloc_context();
580 format_ctx->pb = avioContext;
581 if (avformat_open_input(&format_ctx, video_filename, nullptr, nullptr) >= 0) 573 if (avformat_open_input(&format_ctx, video_filename, nullptr, nullptr) >= 0)
582 { 574 {
583 if (avformat_find_stream_info(format_ctx, nullptr) >= 0) 575 if (avformat_find_stream_info(format_ctx, nullptr) >= 0)
584 { 576 {
585 av_dump_format(format_ctx, 0, video_filename, 0); 577 av_dump_format(format_ctx, 0, video_filename, 0);
586 578
587 video_stream_idx = OpenStream(AVMEDIA_TYPE_VIDEO, &video_stream, &video_stream_dec, &video_stream_dec_ctx); 579 video_stream_idx = OpenStream(AVMEDIA_TYPE_VIDEO, &video_stream, &video_stream_dec, &video_stream_dec_ctx);
588 if (video_stream_idx < 0) 580 if (video_stream_idx < 0)
589 return Release(), false; 581 return Release(), false;
590 582
591 audio_stream_idx = OpenStream(AVMEDIA_TYPE_AUDIO, &audio_stream, &audio_stream_dec, &audio_stream_dec_ctx); 583 audio_stream_idx = OpenStream(AVMEDIA_TYPE_AUDIO, &audio_stream, &audio_stream_dec, &audio_stream_dec_ctx);
601 return Release(), false; 593 return Release(), false;
602 } 594 }
603 595
604 bool Release() 596 bool Release()
605 { 597 {
606 if (format_ctx)
607 {
608 avformat_free_context(format_ctx);
609 format_ctx = nullptr;
610 }
611 if (packet) 598 if (packet)
612 { 599 {
613 av_free_packet(packet); 600 av_free_packet(packet);
614 delete packet; 601 delete packet;
615 packet = nullptr; 602 packet = nullptr;
628 { 615 {
629 audio_stream_idx = -1; 616 audio_stream_idx = -1;
630 audio_stream = nullptr; 617 audio_stream = nullptr;
631 audio_stream_dec = nullptr; 618 audio_stream_dec = nullptr;
632 avcodec_close(audio_stream_dec_ctx); 619 avcodec_close(audio_stream_dec_ctx);
620
621 }
622 if (avioContext)
623 {
624 av_free(avioContext);
625 avioContext = nullptr;
626 }
627 if (format_ctx)
628 {
629 avformat_free_context(format_ctx);
630 format_ctx = nullptr;
633 } 631 }
634 return true; 632 return true;
635 } 633 }
636 634
637 635
653 *out_dec_ctx = dec_ctx; 651 *out_dec_ctx = dec_ctx;
654 return stream_idx; 652 return stream_idx;
655 } 653 }
656 } 654 }
657 return -1; 655 return -1;
658 }
659
660 void Rewind()
661 {
662 //auto i=avio_seek(avioContext, pVideoPlayer->uOffset, 0);
663 //auto i = av_seek_frame(format_ctx, -1, 0, AVSEEK_FLAG_BYTE);
664 //i = av_seek_frame(format_ctx, audio_stream_idx, 0, AVSEEK_FLAG_BYTE);
665
666 int64_t timestamp = 9;
667 int ret, i;
668 if (format_ctx->start_time != AV_NOPTS_VALUE)
669 timestamp += format_ctx->start_time;
670 ret = av_seek_frame(format_ctx, -1, format_ctx->start_time, AVSEEK_FLAG_BYTE);
671 if (ret < 0) {
672 // av_log(ctx, AV_LOG_ERROR, "Unable to loop: %s\n", av_err2str(ret));
673 //loop_count = 1; /* do not try again */
674 return;// ret;
675 }
676
677
678 stopped = false;
679 } 656 }
680 657
681 MultimediaFrame::Ptr GetNextFrame() 658 MultimediaFrame::Ptr GetNextFrame()
682 { 659 {
683 packet->data = nullptr; 660 packet->data = nullptr;
720 697
721 int audio_stream_idx; 698 int audio_stream_idx;
722 AVStream *audio_stream; 699 AVStream *audio_stream;
723 AVCodec *audio_stream_dec; 700 AVCodec *audio_stream_dec;
724 AVCodecContext *audio_stream_dec_ctx; 701 AVCodecContext *audio_stream_dec_ctx;
725 AVIOContext * avioContext; 702 unsigned char * ioBuffer;
726 int64_t offset; 703 AVIOContext *avioContext;
727 }; 704 };
728 typedef MovieCached<10> Movie; 705 typedef MovieCached<10> Movie;
729 706
730 707
731 708
752 sound_provider->Initialize(); 729 sound_provider->Initialize();
753 730
754 return true; 731 return true;
755 } 732 }
756 733
734 Movie *LoadMovieFromLOD(HANDLE h, int readFunction(void*, uint8_t*, int), int64_t seekFunction(void*, int64_t, int), int width, int height)
735 {
736 auto movie = new Movie(sound_provider);
737 if (movie)
738 {
739 if (movie->LoadFromLOD(h, readFunction, seekFunction, width, height))
740 {
741 current_movie_width = width;
742 current_movie_height = height;
743 return current_movie = movie;
744 }
745 delete movie;
746 }
747 return nullptr;
748 }
757 749
758 Movie *LoadMovie(const char *filename, int width, int height) 750 Movie *LoadMovie(const char *filename, int width, int height)
759 { 751 {
760 auto movie = new Movie(sound_provider); 752 auto movie = new Movie(sound_provider);
761 if (movie) 753 if (movie)
767 return current_movie = movie; 759 return current_movie = movie;
768 } 760 }
769 delete movie; 761 delete movie;
770 } 762 }
771 return nullptr; 763 return nullptr;
772 }
773
774 inline void Rewind()
775 {
776 current_movie->Rewind();
777 } 764 }
778 765
779 inline char *DoFrame() 766 inline char *DoFrame()
780 { 767 {
781 if (!current_movie) 768 if (!current_movie)
863 void _4BF5B2(); 850 void _4BF5B2();
864 void SelectMovieType();//0x4BF73A 851 void SelectMovieType();//0x4BF73A
865 _BINKBUF *CreateBinkBuffer(unsigned int uWidth, unsigned int uHeight, char a4); 852 _BINKBUF *CreateBinkBuffer(unsigned int uWidth, unsigned int uHeight, char a4);
866 void _inlined_in_463149(); 853 void _inlined_in_463149();
867 854
868 static void MovieLoop(const char *pMovieName, int a2, int a3, int a4); 855 void MovieLoop(const char *pMovieName, int a2, int a3, int a4);
869 856
870 857
871 RGBTexture pVideoFrame; 858 RGBTexture pVideoFrame;
872 struct _SMACK *pSmackerMovie; 859 //struct _SMACK *pSmackerMovie;
873 struct _SMACKBUF *pSmackerBuffer; 860 //struct _SMACKBUF *pSmackerBuffer;
874 char *pSomeSmackerBuffer; 861 //char *pSomeSmackerBuffer;
875 int field_34; 862 int field_34;
876 MovieHeader *pMightVideoHeaders; 863 MovieHeader *pMightVideoHeaders;
877 MovieHeader *pMagicVideoHeaders; 864 MovieHeader *pMagicVideoHeaders;
878 int pResetflag; 865 int pResetflag;
879 int field_44; 866 int field_44;
890 //HWND hWindow; 877 //HWND hWindow;
891 OSWindow *window; 878 OSWindow *window;
892 struct _SMACKBLIT *pSmackMovieBlit; 879 struct _SMACKBLIT *pSmackMovieBlit;
893 HANDLE hMightVid; 880 HANDLE hMightVid;
894 HANDLE hMagicVid; 881 HANDLE hMagicVid;
895 _BINK *pBinkMovie; 882 //_BINK *pBinkMovie;
896 _BINKBUF *pBinkBuffer; 883 //_BINKBUF *pBinkBuffer;
897 char field_88[20]; 884 char field_88[20];
898 unsigned int uMovieFormat; 885 unsigned int uMovieFormat;
899 int uMovieFormatSwapped; 886 int uMovieFormatSwapped;
900 char pCurrentMovieName[64]; 887 char pCurrentMovieName[64];
901 char pVideoFrameTextureFilename[32]; 888 char pVideoFrameTextureFilename[32];
902 int field_104; 889 int field_104;
903 MultimediaPlayer *mPlayer; 890 MultimediaPlayer *pPlayer;
904 Movie *mMovie; 891 Movie *pMovie;
905 int uVidFile; 892 HANDLE hVidFile;
906 int uSize; 893 int uSize;
907 int uOffset; 894 int uOffset;
895 void UpdatePalette();
896 static int readFunction(void *, uint8_t *, int);
897 static int64_t seekFunction(void *, int64_t, int);
898 void LoadMovie(const char *);
908 }; 899 };
909 #pragma pack(pop) 900 #pragma pack(pop)
910 901
911 902
912 903