Dernière modification
16-08-2009 08:49:28
Magun
petit update du machin
SVideo.h SVideo.cpp CGUIVideo.h CGUIVideo.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 #ifndef _SVIDEO_H_
#define _SVIDEO_H_
/** * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* created by Ovan or Magun *
* compile with : lavformat,lavcodec,lavutil *
* and other library defined last *
* *
* conctact me : *
* www.irrlicht.fr *
* www.immortal-galaxy.com *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * **/
#include <stdint.h>
#if defined WIN32 || defined WIN64 || defined _WIN32_ || defined _WIN64_
#define WINDOWS
#endif
struct AVFormatContext;
struct AVCodecContext;
struct AVCodec;
struct AVPicture;
struct AVFrame;
namespace interne
{
namespace _interface { class CGUIVideo; }
struct SVideo
{
public :
SVideo();
SVideo(char*file,bool sound = false);
~SVideo();
bool open( char *file, bool sound);
void play( bool p = true ), stop(), setLoop(bool b = false);
bool restart(), isPlay(), isLoop();
int getWindowFPS(), getVideoFPS();
double getOriginalFPS(); //! get video file fps info
int getFrame(), getTotalFrame();
int getTime(), getTotalTime();//!
void goToFrame(int frm);
void goToTime(int sec = 0, int min = 0, int h = 0);
//! file info ... update ffmpeg for use this
char *getAuthor(), *getAlbum();
char *getTitle(), *getCopyright();
char *getComment(), *getGenre();
int getYear(), getTrack();
bool makeFrame();
signed int width, height;
private :
friend class _interface::CGUIVideo;
char *file;
AVFrame *Frame,*FrameRGB;
AVFormatContext *FormatCtx;
AVCodecContext *videoCodecCtx ,*audioCodecCtx ,*dataCodecCtx;
AVCodec *videoCodec ,*audioCodec ,*dataCodec;
uint8_t *buffer;
double videoFPS;
int nFrm, frame, numBytes, windowFPS;
int videoStream, audioStream, dataStream;
bool drawFrame, Sound, Play, Replay, writeConsol;
};
}
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 #include "SVideo.h"
/** * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* created by Ovan or Magun *
* compile with : lavformat,lavcodec,lavutil *
* and other library defined last *
* *
* conctact me : *
* www.irrlicht.fr *
* www.immortal-galaxy.com *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * **/
#if defined WIN32 || defined WIN64 || defined _WIN32_ || defined _WIN64_
extern "C"
{
#include <ffmpeg/avcodec.h>
#include <ffmpeg/avformat.h>
#include <ffmpeg/swscale.h>
}
#else
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
#endif
#if LIBAVCODEC_VERSION_MAJOR > 51
int img_convert(AVPicture* dst, PixelFormat dst_pix_fmt, AVPicture* src, PixelFormat pix_fmt, int width, int height)
{
int av_log = av_log_get_level();
av_log_set_level(AV_LOG_QUIET);
SwsContext *img_convert_ctx = sws_getContext(width, height, pix_fmt, width, height, dst_pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
int result = sws_scale(img_convert_ctx, src->data, src->linesize, 0, height, dst->data, dst->linesize);
sws_freeContext(img_convert_ctx);
av_log_set_level(av_log);
return result;
}
#endif
namespace interne
{
SVideo::SVideo()
{
av_register_all();
}
SVideo::SVideo(char*file,bool sound)
{
av_register_all();
open(file,sound);
}
SVideo::~SVideo()
{
if(buffer != NULL) av_free(buffer);
if(FrameRGB != NULL) av_free(FrameRGB);
if(Frame != NULL) av_free(Frame);
if(videoCodecCtx != NULL) avcodec_close(videoCodecCtx);
if(FormatCtx != NULL) av_close_input_file(FormatCtx);
}
bool SVideo::open(char *f,bool sound)
{
file = f;
Sound = sound;
if(av_open_input_file(&FormatCtx, file, NULL, 0, NULL)!=0)
{
Play = false;
return true;
}
if(av_find_stream_info(FormatCtx)<0)
{
Play = false;
return true;
}
dump_format(FormatCtx, 0, file, 0);
videoStream=-1;
audioStream=-1;
dataStream=-1;
for(nFrm=0; nFrm<FormatCtx->nb_streams; nFrm++)
{
if(FormatCtx->streams[nFrm]->codec->codec_type == CODEC_TYPE_VIDEO)
{
videoStream = nFrm;
}
if(FormatCtx->streams[nFrm]->codec->codec_type == CODEC_TYPE_AUDIO)
{
audioStream = nFrm;
}
if(FormatCtx->streams[nFrm]->codec->codec_type == CODEC_TYPE_DATA)
{
dataStream = nFrm;
}
}
if(dataStream > -1)
dataCodecCtx = FormatCtx->streams[dataStream]->codec;
if(audioStream > -1 && sound)
{
audioCodecCtx = FormatCtx->streams[audioStream]->codec;
audioCodec = avcodec_find_decoder(audioCodecCtx->codec_id);
avcodec_open(audioCodecCtx, audioCodec);
}
if(videoStream > -1)
{
videoCodecCtx = FormatCtx->streams[videoStream]->codec;
videoCodec = avcodec_find_decoder(videoCodecCtx->codec_id);
avcodec_open(videoCodecCtx, videoCodec);
videoFPS = (double)FormatCtx->streams[videoStream]->r_frame_rate.den / FormatCtx->streams[videoStream]->r_frame_rate.num;
Frame = avcodec_alloc_frame();
FrameRGB = avcodec_alloc_frame();
}
#if defined IRRLICHT_SUPPORT
numBytes = avpicture_get_size(PIX_FMT_RGB555, videoCodecCtx->width,videoCodecCtx->height);
buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
avpicture_fill((AVPicture *)FrameRGB, buffer, PIX_FMT_RGB555, videoCodecCtx->width, videoCodecCtx->height);
#else
numBytes = avpicture_get_size(PIX_FMT_RGB32, videoCodecCtx->width,videoCodecCtx->height);
buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
avpicture_fill((AVPicture *)FrameRGB, buffer, PIX_FMT_RGB32, videoCodecCtx->width, videoCodecCtx->height);
#endif
nFrm = 0;
width = videoCodecCtx->width;
height = videoCodecCtx->height;
return true;
}
bool SVideo::makeFrame()
{
if(Play)
{
AVPacket packet;
if(av_read_frame(FormatCtx, &packet) >= 0)
{
if(packet.stream_index == videoStream)
{
avcodec_decode_video(videoCodecCtx, Frame, &frame, packet.data, packet.size);
if(frame)
{
#if defined SDL_SUPPORT
#elif defined IRRLICHT_SUPPORT
img_convert((AVPicture *)FrameRGB, PIX_FMT_RGB555, (AVPicture*)Frame, videoCodecCtx->pix_fmt,width,height);
#else
img_convert((AVPicture *)FrameRGB, PIX_FMT_RGB32 , (AVPicture*)Frame, videoCodecCtx->pix_fmt,width,height);
#endif
nFrm++;
av_free_packet(&packet);
}
}
else if(packet.stream_index == audioStream);
else av_free_packet(&packet);
}
else
{
if(Replay == true) restart();
else
{
if(writeConsol) printf("\
> END <\
\
");
play(false);
return false;
}
}
drawFrame = true;
}
return true;
}
void SVideo::play(bool b) { Play = b; }
void SVideo::stop() { restart(); Play = false; }
void SVideo::setLoop(bool b){ Replay = b; }
/******************************/
bool SVideo::isPlay(){ return Play; }
bool SVideo::isLoop(){ return Replay; }
void SVideo::goToTime( int sec, int min, int h ) { sec += ((h*60)+min)*60; goToFrame((int)(sec/videoFPS)); }
void SVideo::goToFrame(int frm)
{
if( frm > nFrm) av_seek_frame(FormatCtx, videoStream, frm, AVSEEK_FLAG_BACKWARD);
else
{
restart();
av_seek_frame(FormatCtx, videoStream, frm, AVSEEK_FLAG_BACKWARD);
}
nFrm = frm;
}
bool SVideo::restart()
{
if (FormatCtx)
{
if (av_open_input_file(&FormatCtx, file, NULL, 0, NULL) != 0)
return false;
nFrm = 0;
if(writeConsol) printf("\
> RESTART <\
\
");
return true;
}
}
/********** FILE INFO *********/
char *SVideo::getAuthor() { return FormatCtx->author; }
char *SVideo::getAlbum() { return FormatCtx->album; }
char *SVideo::getTitle() { return FormatCtx->title; }
char *SVideo::getCopyright(){ return FormatCtx->copyright; }
char *SVideo::getComment() { return FormatCtx->comment; }
char *SVideo::getGenre(){ return FormatCtx->genre; }
int SVideo::getYear() { return FormatCtx->year; }
int SVideo::getTrack() { return FormatCtx->track; }
int SVideo::getVideoFPS() { return (int)videoFPS; }
double SVideo::getOriginalFPS(){ return (double)FormatCtx->streams[videoStream]->r_frame_rate.den / FormatCtx->streams[videoStream]->r_frame_rate.num; }
int SVideo::getFrame() { return nFrm; }
int SVideo::getTime() { return (int)(nFrm/videoFPS)/60; }
int SVideo::getTotalFrame() { return FormatCtx->streams[videoStream]->nb_frames; }
int SVideo::getTotalTime() { return (int)(FormatCtx->streams[videoStream]->nb_frames/videoFPS); }
int SVideo::getWindowFPS() { return windowFPS; }
/******************************/
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 #ifndef _CGUI_VIDEO_H_
#define _CGUI_VIDEO_H_
#include "../SVideo.h"
//!compatible graphique library
//#define SDL_SUPPORT
#define IRRLICHT_SUPPORT
//!futur compatible graphique labrary ?
//////////////#define DIRECTX_SUPPORT
//////////////#define OPENGL_SUPPORT
//////////////#define OGRE_SUPPORT
//!futur compatible Sound labrary ?
//////////////#define FMOD_SUPPORT
//////////////#define IRRKLANG_SUPPORT
//////////////#define SDL_AUDIO_SUPPORT
//////////////#define DIRECT_SOUND_SUPPORT
#if defined SDL_SUPPORT || SDL_AUDIO_SUPPORT
#include <SDL.h>
#include <SDL_audio.h>
#undef FMOD_SUPPORT
#undef IRRKLANG_SUPPORT
#define SDL_AUDIO_SUPPORT
#elif defined IRRLICHT_SUPPORT
#include <irrlicht/irrlicht.h>
//#elif defined OPENGL_SUPPORT
// #include <GL/gl.h>
// #include <GL/glu.h>
//#elif ( defined DIRECTX_SUPPORT || defined DIRECT_SOUND_SUPPORT ) && defined WINDOWS
// #include <d3d9.h>
// #include <dsound.h>
#endif
/** * * * * * * * * * * * * * * * * * * * * * * * * * **/
/** * * **/
/** * created by Ovan or Magun * **/
/** * compile with : lavformat,lavcodec,lavutil * **/
/** * and other library defined last * **/
/** * * **/
/** * conctact me : * **/
/** * www.irrlicht.fr * **/
/** * www.immortal-galaxy.com * **/
/** * * **/
/** * * * * * * * * * * * * * * * * * * * * * * * * * **/
namespace interne
{
namespace _interface
{
class CGUIVideo
{
public:
CGUIVideo(SVideo*,bool run = true,bool loop = false);
virtual ~CGUIVideo();//free memory class
virtual bool refresh(),// no limite refresh video
refreshByTime(),//limite refresh by time
refreshByFPS();//sync video fps and window fps for slow computer or slow render window ... no finished
virtual bool creatFrameScreenshot(char *name);
virtual void setFPS( double fps );//set the video decoding frame rate, if 0 fps = video.fps
virtual void setConsole( bool b = false );//!sdl note suported
#if defined SDL_SUPPORT
virtual bool draw(SDL_Surface *screen, SDL_Rect rect);
#elif defined IRRLICHT_SUPPORT
virtual irr::video::ITexture *draw();
virtual void setVideoDriver(irr::video::IVideoDriver*drive, irr::ITimer *time);
virtual irr::video::IImage *getImage();
virtual irr::video::ITexture *getTexture();
#endif
SVideo *getVideoData() { return data; }
protected:
SVideo *data;
bool writeConsol;
private:
int lastTime;
#if defined SDL_SUPPORT
SDL_Overlay *bmp;
#elif defined OGRE_SUPPORT
#elif defined IRRLICHT_SUPPORT
irr::ITimer *Timer;
irr::video::IImage *VImage;
irr::video::ITexture *VTexture;
irr::video::IVideoDriver*driver;
#endif
#if defined SDL_AUDIO_SUPPORT
SDL_AudioSpec wanted_spec, spec;
#endif
void WindowFPS();
};
}
}
#endif // CGUIVideo_H
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 #include "CGUIVideo.h"
using namespace std;
#if defined WIN32 || defined WIN64 || defined _WIN32_ || defined _WIN64_
extern "C"
{
#include <ffmpeg/avcodec.h>
}
#else
extern "C"
{
#include <libavcodec/avcodec.h>
}
#endif
/** * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* created by Ovan or Magun *
* compile with : lavformat,lavcodec,lavutil *
* and other library defined last *
* *
* conctact me : *
* www.irrlicht.fr *
* www.immortal-galaxy.com *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * **/
namespace interne
{
namespace _interface
{
CGUIVideo::CGUIVideo( SVideo *d, bool run, bool loop )
{
data = d;
data->Play = data->Replay = writeConsol = data->drawFrame = false;
#if defined SDL_SUPPORT
bmp = false;
#elif defined IRRLICHT_SUPPORT
VTexture = false;
VImage = false;
#endif
data->Play = run;
data->Replay = loop;
writeConsol = data->writeConsol = false;
lastTime = 0;
}
CGUIVideo::~CGUIVideo(){ }
void CGUIVideo::setConsole(bool b){ data->writeConsol = writeConsol = b; }
/********** VIDEO VIEW *********/
void CGUIVideo::setFPS(double fps) { data->videoFPS = fps; }
bool CGUIVideo::creatFrameScreenshot(char *name){}
void CGUIVideo::WindowFPS()
{
#if defined SDL_SUPPORT
static int time, frame,i;
i+=1;
if(SDL_GetTicks() - time > 1000)
{
windowFPS = i - frame;
frame = i;
time = SDL_GetTicks();
}
#elif defined IRRLICHT_SUPPORT
data->windowFPS = driver->getFPS();
#elif SFML_SUPPORT
static double time, frame,i;
i+=1;
if(timer.GetElapsedTime() - time)
{
windowFPS = i - frame;
frame = i;
time = timer.GetElapsedTime();
}
#endif
}
bool CGUIVideo::refreshByTime()
{
bool rf;
if(data->Play
#if defined SDL_SUPPORT
&& SDL_GetTicks() - lastTime > videoFPS*1000
#elif defined SFML_SUPPORT
&& timer.GetElapsedTime() > videoFPS
#elif defined IRRLICHT_SUPPORT
&& Timer->getRealTime() - lastTime > data->videoFPS*1000
#endif
)
{
#if defined SDL_SUPPORT
lastTime = SDL_GetTicks();
#elif defined SFML_SUPPORT
timer.Reset();
#elif defined IRRLICHT_SUPPORT
lastTime = Timer->getRealTime();
#endif
return refresh();
}
}
bool CGUIVideo::refreshByFPS()
{
WindowFPS();
static int i = (int)(data->getTotalFrame() / data->videoFPS), io = 0;
io = io+i;
data->goToFrame((int)io);
}
bool CGUIVideo::refresh() { return data->makeFrame(); }
#if defined SDL_SUPPORT
bool CGUIVideo::draw(SDL_Surface *screen, SDL_Rect rect)
{
WindowFPS();
if(Play && frame)
{
if(!bmp)
bmp = SDL_CreateYUVOverlay(data->width,data->height, SDL_YV12_OVERLAY,screen);
SDL_LockYUVOverlay(bmp);
pict.data[0] = bmp->pixels[0];
pict.data[1] = bmp->pixels[2];
pict.data[2] = bmp->pixels[1];
pict.linesize[0] = bmp->pitches[0];
pict.linesize[1] = bmp->pitches[2];
pict.linesize[2] = bmp->pitches[1];
img_convert(&pict, PIX_FMT_YUVJ420P,(AVPicture *)Frame,videoCodecCtx->pix_fmt,width,height);
SDL_UnlockYUVOverlay(bmp);
SDL_DisplayYUVOverlay(bmp, &rect);
}
}
#elif defined IRRLICHT_SUPPORT
irr::video::IImage* CGUIVideo::getImage() { return VImage; }
irr::video::ITexture* CGUIVideo::getTexture() { return VTexture; }
void CGUIVideo::setVideoDriver(irr::video::IVideoDriver*drive,irr::ITimer *time)
{
driver = drive; Timer = time;
}
irr::video::ITexture* CGUIVideo::draw()
{
WindowFPS();//temporaire, wait to dev syncronisation video
if(data->Play && data->frame && data->drawFrame)
{
if(!VImage) VImage = driver->createImageFromData(irr::video::ECF_A1R5G5B5,irr::core::dimension2d<irr::u32>(data->width,data->height),data->FrameRGB->data[0],true);
if(!VTexture) VTexture = driver->addTexture("Movie", VImage);
irr::u32 *tBits = (irr::u32*)VTexture->lock();
for(irr::u32 j=0; j<data->height; ++j)
for(irr::u32 i=0; i<data->width; ++i)
tBits[j*data->width+i] = VImage->getPixel(i,j).color;
VTexture->unlock();
data->drawFrame = false;
return VTexture;
}
}
#endif
}
}
Si vous ne pouvez pas activer javacsript, clique ici
avec un code d'exemple pour les fégniants comme moi :
GameIntro.h
Code c++ : #ifndef _GAME_INTRO_H_
#define _GAME_INTRO_H_
#include "CGUIVideo.h"
namespace interne
{
namespace _interface
{
class GameIntro
{
public:
GameIntro(irr::IrrlichtDevice*,char*,bool rsz = false);
virtual ~GameIntro();
virtual bool externalEvent(irr::SEvent event);
bool draw();
SVideo vi;
CGUIVideo *intro;
protected:
bool resize_for_render;
irr::IrrlichtDevice *device;
irr::core::dimension2d<irr::u32> current_render_size;
};
}
}
#endif // _GAME_INTRO_H_GameIntro.cpp
Code c++ : #include "GameIntro.h"
using namespace irr;
using namespace video;
using namespace core;
namespace interne
{
namespace _interface
{
GameIntro::GameIntro(irr::IrrlichtDevice*dev,char*video_file,bool rsz) : device(dev)
{
current_render_size = device->getVideoDriver()->getScreenSize();
resize_for_render = rsz;
vi.open(video_file,true); vi.play(true);
intro = new _interface::CGUIVideo(&vi);
intro->setVideoDriver(device->getVideoDriver(),device->getTimer());
device->getVideoDriver()->setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, false);
if(resize_for_render) device->getVideoDriver()->OnResize(dimension2d<u32>(vi.width,vi.height));
}
GameIntro::~GameIntro()
{
vi.stop();
//delete vi;
delete intro;
if(resize_for_render) device->getVideoDriver()->OnResize(current_render_size);
device->getVideoDriver()->setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, true);
}
bool GameIntro::externalEvent(SEvent event)
{
if(event.EventType == EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP && intro)
{ return true; }
if (event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown == false && intro)
{
switch(event.KeyInput.Key)
{
case KEY_ESCAPE: case KEY_DELETE: case KEY_RETURN: case KEY_SPACE:
return true; break; default: break;
}
}
return false;
}
bool GameIntro::draw()
{
if(intro && intro->refreshByTime() && device->run())
{
device->getVideoDriver()->draw2DImage(intro->draw(), position2d<s32>(0,0));
return false;
}
return true;
}
}
}Code c++ : device->getCursorControl()->setVisible(false);
intro = new _interface::GameIntro(device,"data/intro.data",true);
while(device->run() && intro)
{
driver->beginScene(true, true, SColor(0,0,0,0));
if(intro->draw()) break;
driver->endScene();
}
if(intro) delete intro; //if is deleted with event manager
intro = NULL;
device->getCursorControl()->setVisible(true);pour utiliser les evenements sans changer de "callback ?" passer par GameIntro->externalEvent(event); ( event = le "const SEvent &" du OnEvent pour les nulls ), biensur ses coder pour mon projet donc a modifier a vos besoin ^^
je croix qu'il y a une erreur dans le code, il date un peut il me semble, sa sorte de ma clées usb .... si quelqu'un teste et que sa marche pas trop, dite j'editerais
dsl je poste pas d'archive pour tester, et euh oui j'ai constater que sa plantais a la convertion de certain type de video avec irrlicht ...
merci en passant a la personne qui a fait "int img_convert..." !, a demande j'ai un makefile pour linux de cette même personne
ps : pas de correcteur orthographique, sorry
ps : avoir une geforce 6150 LE mini, avec un petit gain de 9fps
Création du message
12-06-2009 18:49:41
Magun
petit update du machin
SVideo.h SVideo.cpp CGUIVideo.h CGUIVideo.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 #ifndef _SVIDEO_H_
#define _SVIDEO_H_
/** * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* created by Ovan or Magun *
* compile with : lavformat,lavcodec,lavutil *
* and other library defined last *
* *
* conctact me : *
* www.irrlicht.fr *
* www.immortal-galaxy.com *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * **/
#include <stdint.h>
#if defined WIN32 || defined WIN64 || defined _WIN32_ || defined _WIN64_
#define WINDOWS
#endif
struct AVFormatContext;
struct AVCodecContext;
struct AVCodec;
struct AVPicture;
struct AVFrame;
namespace interne
{
namespace _interface { class CGUIVideo; }
struct SVideo
{
public :
SVideo();
SVideo(char*file,bool sound = false);
~SVideo();
bool open( char *file, bool sound);
void play( bool p = true ), stop(), setLoop(bool b = false);
bool restart(), isPlay(), isLoop();
int getWindowFPS(), getVideoFPS();
double getOriginalFPS(); //! get video file fps info
int getFrame(), getTotalFrame();
int getTime(), getTotalTime();//!
void goToFrame(int frm);
void goToTime(int sec = 0, int min = 0, int h = 0);
//! file info ... update ffmpeg for use this
char *getAuthor(), *getAlbum();
char *getTitle(), *getCopyright();
char *getComment(), *getGenre();
int getYear(), getTrack();
bool makeFrame();
signed int width, height;
private :
friend class _interface::CGUIVideo;
char *file;
AVFrame *Frame,*FrameRGB;
AVFormatContext *FormatCtx;
AVCodecContext *videoCodecCtx ,*audioCodecCtx ,*dataCodecCtx;
AVCodec *videoCodec ,*audioCodec ,*dataCodec;
uint8_t *buffer;
double videoFPS;
int nFrm, frame, numBytes, windowFPS;
int videoStream, audioStream, dataStream;
bool drawFrame, Sound, Play, Replay, writeConsol;
};
}
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 #include "SVideo.h"
/** * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* created by Ovan or Magun *
* compile with : lavformat,lavcodec,lavutil *
* and other library defined last *
* *
* conctact me : *
* www.irrlicht.fr *
* www.immortal-galaxy.com *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * **/
#if defined WIN32 || defined WIN64 || defined _WIN32_ || defined _WIN64_
extern "C"
{
#include <ffmpeg/avcodec.h>
#include <ffmpeg/avformat.h>
#include <ffmpeg/swscale.h>
}
#else
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
#endif
#if LIBAVCODEC_VERSION_MAJOR > 51
int img_convert(AVPicture* dst, PixelFormat dst_pix_fmt, AVPicture* src, PixelFormat pix_fmt, int width, int height)
{
int av_log = av_log_get_level();
av_log_set_level(AV_LOG_QUIET);
SwsContext *img_convert_ctx = sws_getContext(width, height, pix_fmt, width, height, dst_pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
int result = sws_scale(img_convert_ctx, src->data, src->linesize, 0, height, dst->data, dst->linesize);
sws_freeContext(img_convert_ctx);
av_log_set_level(av_log);
return result;
}
#endif
namespace interne
{
SVideo::SVideo()
{
av_register_all();
}
SVideo::SVideo(char*file,bool sound)
{
av_register_all();
open(file,sound);
}
SVideo::~SVideo()
{
if(buffer != NULL) av_free(buffer);
if(FrameRGB != NULL) av_free(FrameRGB);
if(Frame != NULL) av_free(Frame);
if(videoCodecCtx != NULL) avcodec_close(videoCodecCtx);
if(FormatCtx != NULL) av_close_input_file(FormatCtx);
}
bool SVideo::open(char *f,bool sound)
{
file = f;
Sound = sound;
if(av_open_input_file(&FormatCtx, file, NULL, 0, NULL)!=0)
{
Play = false;
return true;
}
if(av_find_stream_info(FormatCtx)<0)
{
Play = false;
return true;
}
dump_format(FormatCtx, 0, file, 0);
videoStream=-1;
audioStream=-1;
dataStream=-1;
for(nFrm=0; nFrm<FormatCtx->nb_streams; nFrm++)
{
if(FormatCtx->streams[nFrm]->codec->codec_type == CODEC_TYPE_VIDEO)
{
videoStream = nFrm;
}
if(FormatCtx->streams[nFrm]->codec->codec_type == CODEC_TYPE_AUDIO)
{
audioStream = nFrm;
}
if(FormatCtx->streams[nFrm]->codec->codec_type == CODEC_TYPE_DATA)
{
dataStream = nFrm;
}
}
if(dataStream > -1)
dataCodecCtx = FormatCtx->streams[dataStream]->codec;
if(audioStream > -1 && sound)
{
audioCodecCtx = FormatCtx->streams[audioStream]->codec;
audioCodec = avcodec_find_decoder(audioCodecCtx->codec_id);
avcodec_open(audioCodecCtx, audioCodec);
}
if(videoStream > -1)
{
videoCodecCtx = FormatCtx->streams[videoStream]->codec;
videoCodec = avcodec_find_decoder(videoCodecCtx->codec_id);
avcodec_open(videoCodecCtx, videoCodec);
videoFPS = (double)FormatCtx->streams[videoStream]->r_frame_rate.den / FormatCtx->streams[videoStream]->r_frame_rate.num;
Frame = avcodec_alloc_frame();
FrameRGB = avcodec_alloc_frame();
}
#if defined IRRLICHT_SUPPORT
numBytes = avpicture_get_size(PIX_FMT_RGB555, videoCodecCtx->width,videoCodecCtx->height);
buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
avpicture_fill((AVPicture *)FrameRGB, buffer, PIX_FMT_RGB555, videoCodecCtx->width, videoCodecCtx->height);
#else
numBytes = avpicture_get_size(PIX_FMT_RGB32, videoCodecCtx->width,videoCodecCtx->height);
buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
avpicture_fill((AVPicture *)FrameRGB, buffer, PIX_FMT_RGB32, videoCodecCtx->width, videoCodecCtx->height);
#endif
nFrm = 0;
width = videoCodecCtx->width;
height = videoCodecCtx->height;
return true;
}
bool SVideo::makeFrame()
{
if(Play)
{
AVPacket packet;
if(av_read_frame(FormatCtx, &packet) >= 0)
{
if(packet.stream_index == videoStream)
{
avcodec_decode_video(videoCodecCtx, Frame, &frame, packet.data, packet.size);
if(frame)
{
#if defined SDL_SUPPORT
#elif defined IRRLICHT_SUPPORT
img_convert((AVPicture *)FrameRGB, PIX_FMT_RGB555, (AVPicture*)Frame, videoCodecCtx->pix_fmt,width,height);
#else
img_convert((AVPicture *)FrameRGB, PIX_FMT_RGB32 , (AVPicture*)Frame, videoCodecCtx->pix_fmt,width,height);
#endif
nFrm++;
av_free_packet(&packet);
}
}
else if(packet.stream_index == audioStream);
else av_free_packet(&packet);
}
else
{
if(Replay == true) restart();
else
{
if(writeConsol) printf("\
> END <\
\
");
play(false);
return false;
}
}
drawFrame = true;
}
return true;
}
void SVideo::play(bool b) { Play = b; }
void SVideo::stop() { restart(); Play = false; }
void SVideo::setLoop(bool b){ Replay = b; }
/******************************/
bool SVideo::isPlay(){ return Play; }
bool SVideo::isLoop(){ return Replay; }
void SVideo::goToTime( int sec, int min, int h ) { sec += ((h*60)+min)*60; goToFrame((int)(sec/videoFPS)); }
void SVideo::goToFrame(int frm)
{
if( frm > nFrm) av_seek_frame(FormatCtx, videoStream, frm, AVSEEK_FLAG_BACKWARD);
else
{
restart();
av_seek_frame(FormatCtx, videoStream, frm, AVSEEK_FLAG_BACKWARD);
}
nFrm = frm;
}
bool SVideo::restart()
{
if (FormatCtx)
{
if (av_open_input_file(&FormatCtx, file, NULL, 0, NULL) != 0)
return false;
nFrm = 0;
if(writeConsol) printf("\
> RESTART <\
\
");
return true;
}
}
/********** FILE INFO *********/
char *SVideo::getAuthor() { return FormatCtx->author; }
char *SVideo::getAlbum() { return FormatCtx->album; }
char *SVideo::getTitle() { return FormatCtx->title; }
char *SVideo::getCopyright(){ return FormatCtx->copyright; }
char *SVideo::getComment() { return FormatCtx->comment; }
char *SVideo::getGenre(){ return FormatCtx->genre; }
int SVideo::getYear() { return FormatCtx->year; }
int SVideo::getTrack() { return FormatCtx->track; }
int SVideo::getVideoFPS() { return (int)videoFPS; }
double SVideo::getOriginalFPS(){ return (double)FormatCtx->streams[videoStream]->r_frame_rate.den / FormatCtx->streams[videoStream]->r_frame_rate.num; }
int SVideo::getFrame() { return nFrm; }
int SVideo::getTime() { return (int)(nFrm/videoFPS)/60; }
int SVideo::getTotalFrame() { return FormatCtx->streams[videoStream]->nb_frames; }
int SVideo::getTotalTime() { return (int)(FormatCtx->streams[videoStream]->nb_frames/videoFPS); }
int SVideo::getWindowFPS() { return windowFPS; }
/******************************/
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 #ifndef _CGUI_VIDEO_H_
#define _CGUI_VIDEO_H_
#include "../SVideo.h"
//!compatible graphique library
//#define SDL_SUPPORT
#define IRRLICHT_SUPPORT
//!futur compatible graphique labrary ?
//////////////#define DIRECTX_SUPPORT
//////////////#define OPENGL_SUPPORT
//////////////#define OGRE_SUPPORT
//!futur compatible Sound labrary ?
//////////////#define FMOD_SUPPORT
//////////////#define IRRKLANG_SUPPORT
//////////////#define SDL_AUDIO_SUPPORT
//////////////#define DIRECT_SOUND_SUPPORT
#if defined SDL_SUPPORT || SDL_AUDIO_SUPPORT
#include <SDL.h>
#include <SDL_audio.h>
#undef FMOD_SUPPORT
#undef IRRKLANG_SUPPORT
#define SDL_AUDIO_SUPPORT
#elif defined IRRLICHT_SUPPORT
#include <irrlicht/irrlicht.h>
//#elif defined OPENGL_SUPPORT
// #include <GL/gl.h>
// #include <GL/glu.h>
//#elif ( defined DIRECTX_SUPPORT || defined DIRECT_SOUND_SUPPORT ) && defined WINDOWS
// #include <d3d9.h>
// #include <dsound.h>
#endif
/** * * * * * * * * * * * * * * * * * * * * * * * * * **/
/** * * **/
/** * created by Ovan or Magun * **/
/** * compile with : lavformat,lavcodec,lavutil * **/
/** * and other library defined last * **/
/** * * **/
/** * conctact me : * **/
/** * www.irrlicht.fr * **/
/** * www.immortal-galaxy.com * **/
/** * * **/
/** * * * * * * * * * * * * * * * * * * * * * * * * * **/
namespace interne
{
namespace _interface
{
class CGUIVideo
{
public:
CGUIVideo(SVideo*,bool run = true,bool loop = false);
virtual ~CGUIVideo();//free memory class
virtual bool refresh(),// no limite refresh video
refreshByTime(),//limite refresh by time
refreshByFPS();//sync video fps and window fps for slow computer or slow render window ... no finished
virtual bool creatFrameScreenshot(char *name);
virtual void setFPS( double fps );//set the video decoding frame rate, if 0 fps = video.fps
virtual void setConsole( bool b = false );//!sdl note suported
#if defined SDL_SUPPORT
virtual bool draw(SDL_Surface *screen, SDL_Rect rect);
#elif defined IRRLICHT_SUPPORT
virtual irr::video::ITexture *draw();
virtual void setVideoDriver(irr::video::IVideoDriver*drive, irr::ITimer *time);
virtual irr::video::IImage *getImage();
virtual irr::video::ITexture *getTexture();
#endif
SVideo *getVideoData() { return data; }
protected:
SVideo *data;
bool writeConsol;
private:
int lastTime;
#if defined SDL_SUPPORT
SDL_Overlay *bmp;
#elif defined OGRE_SUPPORT
#elif defined IRRLICHT_SUPPORT
irr::ITimer *Timer;
irr::video::IImage *VImage;
irr::video::ITexture *VTexture;
irr::video::IVideoDriver*driver;
#endif
#if defined SDL_AUDIO_SUPPORT
SDL_AudioSpec wanted_spec, spec;
#endif
void WindowFPS();
};
}
}
#endif // CGUIVideo_H
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 #include "CGUIVideo.h"
using namespace std;
#if defined WIN32 || defined WIN64 || defined _WIN32_ || defined _WIN64_
extern "C"
{
#include <ffmpeg/avcodec.h>
}
#else
extern "C"
{
#include <libavcodec/avcodec.h>
}
#endif
/** * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* created by Ovan or Magun *
* compile with : lavformat,lavcodec,lavutil *
* and other library defined last *
* *
* conctact me : *
* www.irrlicht.fr *
* www.immortal-galaxy.com *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * **/
namespace interne
{
namespace _interface
{
CGUIVideo::CGUIVideo( SVideo *d, bool run, bool loop )
{
data = d;
data->Play = data->Replay = writeConsol = data->drawFrame = false;
#if defined SDL_SUPPORT
bmp = false;
#elif defined IRRLICHT_SUPPORT
VTexture = false;
VImage = false;
#endif
data->Play = run;
data->Replay = loop;
writeConsol = data->writeConsol = false;
lastTime = 0;
}
CGUIVideo::~CGUIVideo(){ }
void CGUIVideo::setConsole(bool b){ data->writeConsol = writeConsol = b; }
/********** VIDEO VIEW *********/
void CGUIVideo::setFPS(double fps) { data->videoFPS = fps; }
bool CGUIVideo::creatFrameScreenshot(char *name){}
void CGUIVideo::WindowFPS()
{
#if defined SDL_SUPPORT
static int time, frame,i;
i+=1;
if(SDL_GetTicks() - time > 1000)
{
windowFPS = i - frame;
frame = i;
time = SDL_GetTicks();
}
#elif defined IRRLICHT_SUPPORT
data->windowFPS = driver->getFPS();
#elif SFML_SUPPORT
static double time, frame,i;
i+=1;
if(timer.GetElapsedTime() - time)
{
windowFPS = i - frame;
frame = i;
time = timer.GetElapsedTime();
}
#endif
}
bool CGUIVideo::refreshByTime()
{
bool rf;
if(data->Play
#if defined SDL_SUPPORT
&& SDL_GetTicks() - lastTime > videoFPS*1000
#elif defined SFML_SUPPORT
&& timer.GetElapsedTime() > videoFPS
#elif defined IRRLICHT_SUPPORT
&& Timer->getRealTime() - lastTime > data->videoFPS*1000
#endif
)
{
#if defined SDL_SUPPORT
lastTime = SDL_GetTicks();
#elif defined SFML_SUPPORT
timer.Reset();
#elif defined IRRLICHT_SUPPORT
lastTime = Timer->getRealTime();
#endif
return refresh();
}
}
bool CGUIVideo::refreshByFPS()
{
WindowFPS();
static int i = (int)(data->getTotalFrame() / data->videoFPS), io = 0;
io = io+i;
data->goToFrame((int)io);
}
bool CGUIVideo::refresh() { return data->makeFrame(); }
#if defined SDL_SUPPORT
bool CGUIVideo::draw(SDL_Surface *screen, SDL_Rect rect)
{
WindowFPS();
if(Play && frame)
{
if(!bmp)
bmp = SDL_CreateYUVOverlay(data->width,data->height, SDL_YV12_OVERLAY,screen);
SDL_LockYUVOverlay(bmp);
pict.data[0] = bmp->pixels[0];
pict.data[1] = bmp->pixels[2];
pict.data[2] = bmp->pixels[1];
pict.linesize[0] = bmp->pitches[0];
pict.linesize[1] = bmp->pitches[2];
pict.linesize[2] = bmp->pitches[1];
img_convert(&pict, PIX_FMT_YUVJ420P,(AVPicture *)Frame,videoCodecCtx->pix_fmt,width,height);
SDL_UnlockYUVOverlay(bmp);
SDL_DisplayYUVOverlay(bmp, &rect);
}
}
#elif defined IRRLICHT_SUPPORT
irr::video::IImage* CGUIVideo::getImage() { return VImage; }
irr::video::ITexture* CGUIVideo::getTexture() { return VTexture; }
void CGUIVideo::setVideoDriver(irr::video::IVideoDriver*drive,irr::ITimer *time)
{
driver = drive; Timer = time;
}
irr::video::ITexture* CGUIVideo::draw()
{
WindowFPS();//temporaire, wait to dev syncronisation video
if(data->Play && data->frame && data->drawFrame)
{
if(!VImage) VImage = driver->createImageFromData(irr::video::ECF_A1R5G5B5,irr::core::dimension2d<irr::u32>(data->width,data->height),data->FrameRGB->data[0],true);
if(!VTexture) VTexture = driver->addTexture("Movie", VImage);
irr::u32 *tBits = (irr::u32*)VTexture->lock();
for(irr::u32 j=0; j<data->height; ++j)
for(irr::u32 i=0; i<data->width; ++i)
tBits[j*data->width+i] = VImage->getPixel(i,j).color;
VTexture->unlock();
data->drawFrame = false;
return VTexture;
}
}
#endif
}
}
Si vous ne pouvez pas activer javacsript, clique ici
avec un code d'exemple pour les fégniants comme moi :
GameIntro.h
Code c++ : #ifndef _GAME_INTRO_H_
#define _GAME_INTRO_H_
#include "CGUIVideo.h"
namespace interne
{
namespace _interface
{
class GameIntro
{
public:
GameIntro(irr::IrrlichtDevice*,char*,bool rsz = false);
virtual ~GameIntro();
virtual bool externalEvent(irr::SEvent event);
bool draw();
SVideo vi;
CGUIVideo *intro;
protected:
bool resize_for_render;
irr::IrrlichtDevice *device;
irr::core::dimension2d<irr::u32> current_render_size;
};
}
}
#endif // _GAME_INTRO_H_GameIntro.cpp
Code c++ : #include "GameIntro.h"
using namespace irr;
using namespace video;
using namespace core;
namespace interne
{
namespace _interface
{
GameIntro::GameIntro(irr::IrrlichtDevice*dev,char*video_file,bool rsz) : device(dev)
{
current_render_size = device->getVideoDriver()->getScreenSize();
resize_for_render = rsz;
vi.open(video_file,true); vi.play(true);
intro = new _interface::CGUIVideo(&vi);
intro->setVideoDriver(device->getVideoDriver(),device->getTimer());
device->getVideoDriver()->setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, false);
if(resize_for_render) device->getVideoDriver()->OnResize(dimension2d<u32>(vi.width,vi.height));
}
GameIntro::~GameIntro()
{
vi.stop();
//delete vi;
delete intro;
if(resize_for_render) device->getVideoDriver()->OnResize(current_render_size);
device->getVideoDriver()->setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, true);
}
bool GameIntro::externalEvent(SEvent event)
{
if(event.EventType == EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP && intro)
{ return true; }
if (event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown == false && intro)
{
switch(event.KeyInput.Key)
{
case KEY_ESCAPE: case KEY_DELETE: case KEY_RETURN: case KEY_SPACE:
return true; break; default: break;
}
}
return false;
}
bool GameIntro::draw()
{
if(intro && intro->refreshByTime() && device->run())
{
device->getVideoDriver()->draw2DImage(intro->draw(), position2d<s32>(0,0));
return false;
}
return true;
}
}
}Code c++ : device->getCursorControl()->setVisible(false);
intro = new _interface::GameIntro(device,"data/intro.data",true);
while(device->run() && intro)
{
driver->beginScene(true, true, SColor(0,0,0,0));
if(intro->draw()) break;
driver->endScene();
}
if(intro) delete intro; //if is deleted with event manager
intro = NULL;
device->getCursorControl()->setVisible(true);pour utiliser les evenements sans changer de "callback ?" passer par GameIntro->externalEvent(event); ( event = le "const SEvent &" du OnEvent pour les nulls ), biensur ses coder pour mon projet donc a modifier a vos besoin ^^
je croix qu'il y a une erreur dans le code, il date un peut il me semble, sa sorte de ma clées usb .... si quelqu'un teste et que sa marche pas trop, dite j'editerais
dsl je poste pas d'archive pour tester, et euh oui j'ai constater que sa plantais a la convertion de certain type de video avec irrlicht ...
merci en passant a la personne qui a fait "int img_convert..." !, a demande j'ai un makefile pour linux de cette même personne
ps : pas de correcteur orthographique, sorry
ps : avoir une geforce 6150 LE mini, avec un petit gain de 9fps