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
|
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "videorecorder.h"
#ifdef XVIDEXPORT
#include "../libs/revel/revel.h"
#include "../events.h"
#include "../gameoptions.h"
class VideoRecorderInternals {
public:
VideoRecorderInternals() : open(false), frameCounterOut(0), frameCounterIn(0), lastFrame(NULL) {};
~VideoRecorderInternals() { if ( lastFrame ) delete[] lastFrame; };
int encoderHandle;
Revel_Params revParams;
bool open;
int framerate;
long lastTick;
ASCString filename;
int frameCounterOut; //<! counts all frames that where send to the videorecorder
int frameCounterIn; //<! counts all frames that where passed to the video encoder
int* lastFrame;
};
void checkErrors( const Revel_Error& err )
{
if (err != REVEL_ERR_NONE)
{
printf("Revel Error : %d\n", err);
}
}
VideoRecorder::VideoRecorder( const ASCString& filename, const SDL_Surface* surf, int framerate, int quality )
{
data = new VideoRecorderInternals();
data->framerate = framerate;
data->lastTick = getTicker();
data->filename = filename;
Revel_Error revError = Revel_CreateEncoder(&data->encoderHandle);
checkErrors( revError );
Revel_InitializeParams(&data->revParams);
data->revParams.width = surf->w;
data->revParams.height = surf->h;
data->revParams.frameRate = data->framerate;
float q = quality;
q /= 100.0f;
data->revParams.quality = q;
data->revParams.codec = REVEL_CD_XVID;
data->revParams.hasAudio = 0;
// Initiate encoding
revError = Revel_EncodeStart(data->encoderHandle, filename.c_str(), &data->revParams);
data->open = true;
}
void VideoRecorder::storeFrame( const SDL_Surface* surf )
{
if ( !surf )
surf = SDL_GetVideoSurface();
if ( !surf )
return;
Revel_VideoFrame frame;
frame.width = surf->w;
frame.height = surf->h;
frame.bytesPerPixel = 4;
bool directScreenRender = false;
/*
if ( surf->pitch == surf->w*4 && surf->format->BytesPerPixel == 4 ) {
if ( surf->format->Rshift == 0 && surf->format->Gshift == 8 && surf->format->Bshift == 16 ) {
frame.pixelFormat = REVEL_PF_RGBA;
directScreenRender = true;
}
if ( surf->format->Rshift == 24 && surf->format->Gshift == 16 && surf->format->Bshift == 8 ) {
frame.pixelFormat = REVEL_PF_ABGR;
directScreenRender = true;
}
}
*/
if ( directScreenRender ) {
int frameSize;
frame.pixels = surf->pixels;
Revel_Error revError = Revel_EncodeFrame(data->encoderHandle, &frame, &frameSize);
checkErrors( revError);
} else {
frame.pixelFormat = REVEL_PF_RGBA;
int* buf;
frame.pixels = buf = new int[frame.width*frame.height];
Uint32* pix = (Uint32*)frame.pixels;
bool diff = false;
int* lastBuf = data->lastFrame;
if ( !data->lastFrame )
diff = true;
for ( int y = 0; y < surf->h; ++y ) {
Uint32* src = ((Uint32*) surf->pixels) + (surf->pitch/4*y);
for ( int x = 0 ; x < surf->w; ++x ) {
Uint8 r,g,b,a;
SDL_GetRGBA( *src, surf->format, &r,&g,&b,&a);
*pix = r + (g<<8) + (b<<16) + (a<<24);
if ( lastBuf ) {
if ( *pix != *lastBuf )
diff = true;
++lastBuf;
}
++pix;
++src;
}
}
if ( diff ) {
int frameSize;
Revel_Error revError = Revel_EncodeFrame(data->encoderHandle, &frame, &frameSize);
checkErrors( revError);
++data->frameCounterIn;
}
if ( data->lastFrame )
delete[] data->lastFrame;
data->lastFrame = buf;
}
++data->frameCounterOut;
// we are limited the video to our framerate
while ( getTicker() < data->lastTick + 100/data->framerate )
releasetimeslice();
data->lastTick = getTicker();
}
void VideoRecorder::close()
{
if ( data->open ) {
int totalSize;
Revel_Error revError = Revel_EncodeEnd(data->encoderHandle, &totalSize);
checkErrors( revError );
Revel_DestroyEncoder(data->encoderHandle);
}
printf("recorded %d / %d frames\n", data->frameCounterOut, data->frameCounterIn );
}
const ASCString VideoRecorder::getFilename()
{
return data->filename;
}
VideoRecorder::~VideoRecorder()
{
close();
delete data;
}
#else
VideoRecorder::VideoRecorder( const ASCString& filename, const SDL_Surface* surf, int framerate, int quality ) {}
const ASCString VideoRecorder::getFilename() { return ""; }
void VideoRecorder::storeFrame( const SDL_Surface* surf ) {}
void VideoRecorder::close() {}
VideoRecorder::~VideoRecorder() {}
#endif
|