2003-08-29 04:34:10 +00:00
|
|
|
#include "global.h"
|
2003-09-02 04:01:01 +00:00
|
|
|
#include "MovieTexture_FFMpeg.h"
|
2003-08-29 04:34:10 +00:00
|
|
|
|
|
|
|
|
#include "RageLog.h"
|
|
|
|
|
#include "RageTextureManager.h"
|
|
|
|
|
#include "RageUtil.h"
|
2003-09-02 07:47:37 +00:00
|
|
|
#include "RageTimer.h"
|
2003-08-29 04:34:10 +00:00
|
|
|
#include "SDL_utils.h"
|
|
|
|
|
#include "SDL_endian.h"
|
|
|
|
|
|
|
|
|
|
|
2003-09-02 04:01:01 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
#pragma comment(lib, "ffmpeg/lib/avcodec.lib")
|
|
|
|
|
#pragma comment(lib, "ffmpeg/lib/avformat.lib")
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-08-29 04:34:10 +00:00
|
|
|
struct AVPixelFormat_t
|
|
|
|
|
{
|
|
|
|
|
int bpp;
|
|
|
|
|
int masks[4];
|
|
|
|
|
avcodec::PixelFormat pf;
|
|
|
|
|
bool HighColor;
|
|
|
|
|
bool ByteSwapOnLittleEndian;
|
|
|
|
|
} AVPixelFormats[] = {
|
|
|
|
|
{
|
|
|
|
|
24,
|
|
|
|
|
{ 0xFF0000,
|
|
|
|
|
0x00FF00,
|
|
|
|
|
0x0000FF,
|
|
|
|
|
0x000000 },
|
|
|
|
|
avcodec::PIX_FMT_RGB24,
|
|
|
|
|
true,
|
|
|
|
|
true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
24,
|
|
|
|
|
{ 0x0000FF,
|
|
|
|
|
0x00FF00,
|
|
|
|
|
0xFF0000,
|
|
|
|
|
0x000000 },
|
|
|
|
|
avcodec::PIX_FMT_BGR24,
|
|
|
|
|
true,
|
|
|
|
|
true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
16,
|
|
|
|
|
{ 0x7C00,
|
|
|
|
|
0x03E0,
|
|
|
|
|
0x001F,
|
|
|
|
|
0x8000 },
|
|
|
|
|
avcodec::PIX_FMT_RGB555,
|
|
|
|
|
false,
|
|
|
|
|
false
|
|
|
|
|
},
|
|
|
|
|
{ 0, { 0,0,0,0 }, avcodec::PIX_FMT_NB, true }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void FixLilEndian()
|
|
|
|
|
{
|
|
|
|
|
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
|
|
|
|
|
static bool Initialized = false;
|
|
|
|
|
if( Initialized )
|
|
|
|
|
return;
|
|
|
|
|
Initialized = true;
|
|
|
|
|
|
|
|
|
|
for( int i = 0; i < AVPixelFormats[i].bpp; ++i )
|
|
|
|
|
{
|
|
|
|
|
AVPixelFormat_t &pf = AVPixelFormats[i];
|
|
|
|
|
|
|
|
|
|
if( !pf.ByteSwapOnLittleEndian )
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
for( int mask = 0; mask < 4; ++mask)
|
|
|
|
|
{
|
|
|
|
|
int m = pf.masks[mask];
|
|
|
|
|
switch( pf.bpp )
|
|
|
|
|
{
|
|
|
|
|
case 24: m = mySDL_Swap24(m); break;
|
|
|
|
|
case 32: m = SDL_Swap32(m); break;
|
|
|
|
|
default: ASSERT(0);
|
|
|
|
|
}
|
|
|
|
|
pf.masks[mask] = m;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int FindCompatibleAVFormat( PixelFormat &pixfmt, bool HighColor )
|
|
|
|
|
{
|
|
|
|
|
for( int i = 0; AVPixelFormats[i].bpp; ++i )
|
|
|
|
|
{
|
|
|
|
|
AVPixelFormat_t &fmt = AVPixelFormats[i];
|
|
|
|
|
if( fmt.HighColor != HighColor )
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
pixfmt = DISPLAY->FindPixelFormat( fmt.bpp,
|
|
|
|
|
fmt.masks[0],
|
|
|
|
|
fmt.masks[1],
|
|
|
|
|
fmt.masks[2],
|
|
|
|
|
fmt.masks[3] );
|
|
|
|
|
|
|
|
|
|
if( pixfmt == NUM_PIX_FORMATS )
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2003-09-03 02:18:39 +00:00
|
|
|
|
|
|
|
|
class FFMpeg_Helper
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
avcodec::AVFormatContext *m_fctx;
|
|
|
|
|
avcodec::AVStream *m_stream;
|
|
|
|
|
bool GetNextTimestamp;
|
|
|
|
|
float CurrentTimestamp, Last_IP_Timestamp;
|
|
|
|
|
|
|
|
|
|
float LastFrameDelay;
|
|
|
|
|
|
|
|
|
|
float pts, last_IP_pts;
|
|
|
|
|
|
|
|
|
|
avcodec::AVPacket pkt;
|
|
|
|
|
int current_packet_offset;
|
|
|
|
|
|
|
|
|
|
avcodec::AVFrame frame;
|
|
|
|
|
|
|
|
|
|
FFMpeg_Helper();
|
|
|
|
|
int GetFrame();
|
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int ReadPacket();
|
|
|
|
|
int DecodePacket();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FFMpeg_Helper::FFMpeg_Helper()
|
|
|
|
|
{
|
|
|
|
|
m_fctx=NULL;
|
|
|
|
|
m_stream=NULL;
|
|
|
|
|
current_packet_offset = -1;
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FFMpeg_Helper::Init()
|
|
|
|
|
{
|
|
|
|
|
GetNextTimestamp = true;
|
|
|
|
|
CurrentTimestamp = 0, Last_IP_Timestamp = 0;
|
|
|
|
|
LastFrameDelay = 0;
|
|
|
|
|
pts = 0, last_IP_pts = 0;
|
|
|
|
|
|
|
|
|
|
if( current_packet_offset != -1 )
|
|
|
|
|
{
|
|
|
|
|
avcodec::av_free_packet( &pkt );
|
|
|
|
|
current_packet_offset = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Read until we get a frame, EOF or error. Return -1 on error, 0 on EOF, 1 if we have a frame. */
|
|
|
|
|
int FFMpeg_Helper::GetFrame()
|
|
|
|
|
{
|
|
|
|
|
while( 1 )
|
|
|
|
|
{
|
|
|
|
|
int ret = DecodePacket();
|
|
|
|
|
if( ret == 1 )
|
|
|
|
|
return 1;
|
|
|
|
|
if( ret == -1 )
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
ASSERT( ret == 0 );
|
|
|
|
|
ret = ReadPacket();
|
|
|
|
|
if( ret <= 0 )
|
|
|
|
|
return ret; /* error or EOF */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Read a packet. Return -1 on error, 0 on EOF, 1 on OK. */
|
|
|
|
|
int FFMpeg_Helper::ReadPacket()
|
|
|
|
|
{
|
|
|
|
|
while( 1 )
|
|
|
|
|
{
|
|
|
|
|
CHECKPOINT;
|
|
|
|
|
if( current_packet_offset != -1 )
|
|
|
|
|
{
|
|
|
|
|
current_packet_offset = -1;
|
|
|
|
|
avcodec::av_free_packet( &pkt );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ret = avcodec::av_read_packet(m_fctx, &pkt);
|
|
|
|
|
if( ret == -1 )
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if( ret < 0 )
|
|
|
|
|
{
|
|
|
|
|
/* XXX ? */
|
|
|
|
|
// LOG->Warn("AVCodec: Error decoding %s: %i",
|
|
|
|
|
// GetID().filename.c_str(), ret );
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( pkt.stream_index == m_stream->index )
|
|
|
|
|
{
|
|
|
|
|
current_packet_offset = 0;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* It's not for the video stream; ignore it. */
|
|
|
|
|
avcodec::av_free_packet( &pkt );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Decode data from the current packet. Return -1 on error, 0 if the packet is finished,
|
|
|
|
|
* and 1 if we have a frame (we may have more data in the packet). */
|
|
|
|
|
int FFMpeg_Helper::DecodePacket()
|
|
|
|
|
{
|
|
|
|
|
if( current_packet_offset == -1 )
|
|
|
|
|
return 0; /* no packet */
|
|
|
|
|
|
|
|
|
|
while( current_packet_offset < pkt.size )
|
|
|
|
|
{
|
|
|
|
|
if ( GetNextTimestamp )
|
|
|
|
|
{
|
|
|
|
|
if (pkt.pts != AV_NOPTS_VALUE)
|
|
|
|
|
pts = (float)pkt.pts * m_fctx->pts_num / m_fctx->pts_den;
|
|
|
|
|
else
|
|
|
|
|
pts = -1;
|
|
|
|
|
GetNextTimestamp = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int got_frame;
|
|
|
|
|
CHECKPOINT;
|
|
|
|
|
int len = avcodec::avcodec_decode_video(
|
|
|
|
|
&m_stream->codec,
|
|
|
|
|
&frame, &got_frame,
|
|
|
|
|
pkt.data + current_packet_offset,
|
|
|
|
|
pkt.size - current_packet_offset );
|
|
|
|
|
CHECKPOINT;
|
|
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
|
{
|
|
|
|
|
LOG->Warn("avcodec_decode_video: %i", len);
|
|
|
|
|
return -1; // XXX
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_packet_offset += len;
|
|
|
|
|
|
|
|
|
|
if (!got_frame)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
GetNextTimestamp = true;
|
|
|
|
|
|
|
|
|
|
/* Length of this frame: */
|
|
|
|
|
LastFrameDelay = (float)m_stream->codec.frame_rate_base / m_stream->codec.frame_rate;
|
|
|
|
|
LastFrameDelay += frame.repeat_pict * (LastFrameDelay * 0.5f);
|
|
|
|
|
|
|
|
|
|
if ( m_stream->codec.has_b_frames &&
|
|
|
|
|
frame.pict_type != FF_B_TYPE )
|
|
|
|
|
{
|
|
|
|
|
swap( pts, last_IP_pts );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pts != -1)
|
|
|
|
|
{
|
|
|
|
|
CurrentTimestamp = pts;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* If the timestamp is zero, this frame is to be played at the
|
|
|
|
|
* time of the last frame plus the length of the last frame. */
|
|
|
|
|
CurrentTimestamp += LastFrameDelay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0; /* packet done */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MovieTexture_FFMpeg::ConvertFrame()
|
|
|
|
|
{
|
|
|
|
|
avcodec::AVPicture pict;
|
|
|
|
|
pict.data[0] = (unsigned char *)m_img->pixels;
|
|
|
|
|
pict.linesize[0] = m_img->pitch;
|
|
|
|
|
|
|
|
|
|
avcodec::img_convert(&pict, AVPixelFormats[m_AVTexfmt].pf,
|
|
|
|
|
(avcodec::AVPicture *) &decoder->frame, decoder->m_stream->codec.pix_fmt,
|
|
|
|
|
decoder->m_stream->codec.width, decoder->m_stream->codec.height);
|
|
|
|
|
}
|
|
|
|
|
|
2003-08-29 04:34:10 +00:00
|
|
|
static avcodec::AVStream *FindVideoStream( avcodec::AVFormatContext *m_fctx )
|
|
|
|
|
{
|
|
|
|
|
for( int stream = 0; stream < m_fctx->nb_streams; ++stream )
|
|
|
|
|
{
|
|
|
|
|
avcodec::AVStream *enc = m_fctx->streams[stream];
|
|
|
|
|
if( enc->codec.codec_type == avcodec::CODEC_TYPE_VIDEO )
|
|
|
|
|
return enc;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-02 04:01:01 +00:00
|
|
|
MovieTexture_FFMpeg::MovieTexture_FFMpeg( RageTextureID ID ):
|
2003-08-29 04:34:10 +00:00
|
|
|
RageMovieTexture( ID )
|
|
|
|
|
{
|
2003-09-02 19:12:45 +00:00
|
|
|
LOG->Trace( "MovieTexture_FFMpeg::MovieTexture_FFMpeg(%s)", ID.filename.c_str() );
|
|
|
|
|
|
2003-08-29 04:34:10 +00:00
|
|
|
FixLilEndian();
|
|
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
decoder = new FFMpeg_Helper;
|
|
|
|
|
|
2003-08-29 04:34:10 +00:00
|
|
|
m_uTexHandle = 0;
|
|
|
|
|
m_bLoop = true;
|
|
|
|
|
m_State = DECODER_QUIT; /* it's quit until we call StartThread */
|
|
|
|
|
m_bLoop = true;
|
|
|
|
|
m_img = NULL;
|
|
|
|
|
m_ImageWaiting = false;
|
|
|
|
|
m_Rate = 1;
|
2003-09-03 02:18:39 +00:00
|
|
|
m_bWantRewind = false;
|
2003-08-29 04:34:10 +00:00
|
|
|
|
|
|
|
|
m_BufferFinished = SDL_CreateSemaphore(0);
|
|
|
|
|
|
2003-09-02 06:08:02 +00:00
|
|
|
CreateDecoder();
|
|
|
|
|
|
|
|
|
|
LOG->Trace("Resolution: %ix%i (%ix%i, %ix%i)",
|
|
|
|
|
m_iSourceWidth, m_iSourceHeight,
|
|
|
|
|
m_iImageWidth, m_iImageHeight, m_iTextureWidth, m_iTextureHeight);
|
2003-09-03 02:18:39 +00:00
|
|
|
LOG->Trace("Bitrate: %i", decoder->m_stream->codec.bit_rate );
|
|
|
|
|
LOG->Trace("Codec pixel format: %i", decoder->m_stream->codec.pix_fmt );
|
2003-09-02 06:08:02 +00:00
|
|
|
|
|
|
|
|
CreateTexture();
|
2003-08-29 04:34:10 +00:00
|
|
|
CreateFrameRects();
|
2003-09-02 06:08:02 +00:00
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
/* Decode one frame, to guarantee that the texture is drawn when this function returns. */
|
|
|
|
|
int ret = decoder->GetFrame();
|
|
|
|
|
if( ret == -1 )
|
|
|
|
|
{
|
|
|
|
|
/* XXX */
|
|
|
|
|
LOG->Trace( "%s: error getting first frame", GetID().filename.c_str() );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if( ret == 0 )
|
|
|
|
|
{
|
|
|
|
|
/* There's nothing there. XXX */
|
|
|
|
|
LOG->Trace( "%s: EOF getting first frame", GetID().filename.c_str() );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConvertFrame();
|
|
|
|
|
UpdateFrame();
|
|
|
|
|
|
2003-09-02 06:08:02 +00:00
|
|
|
CHECKPOINT;
|
|
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
StartThread();
|
2003-09-02 06:08:02 +00:00
|
|
|
Play();
|
2003-08-29 04:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
2003-08-29 16:50:47 +00:00
|
|
|
static CString averr_ssprintf( int err, const char *fmt, ... )
|
|
|
|
|
{
|
|
|
|
|
ASSERT( err < 0 );
|
|
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
|
CString s = vssprintf( fmt, va );
|
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
|
|
CString Error;
|
|
|
|
|
switch( err )
|
|
|
|
|
{
|
|
|
|
|
case AVERROR_IO: Error = "I/O error"; break;
|
|
|
|
|
case AVERROR_NUMEXPECTED: Error = "number syntax expected in filename"; break;
|
|
|
|
|
case AVERROR_INVALIDDATA: Error = "invalid data found"; break;
|
|
|
|
|
case AVERROR_NOMEM: Error = "not enough memory"; break;
|
|
|
|
|
case AVERROR_NOFMT: Error = "unknown format"; break;
|
|
|
|
|
case AVERROR_UNKNOWN: Error = "unknown error"; break;
|
|
|
|
|
default: Error = ssprintf( "unknown error %i", err ); break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s + " (" + Error + ")";
|
|
|
|
|
}
|
|
|
|
|
|
2003-08-29 04:34:10 +00:00
|
|
|
|
2003-09-02 06:08:02 +00:00
|
|
|
void MovieTexture_FFMpeg::CreateDecoder()
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
|
|
|
|
RageTextureID actualID = GetID();
|
|
|
|
|
|
|
|
|
|
actualID.iAlphaBits = 0;
|
|
|
|
|
|
|
|
|
|
avcodec::av_register_all();
|
2003-09-03 02:18:39 +00:00
|
|
|
int ret = avcodec::av_open_input_file( &decoder->m_fctx, actualID.filename, NULL, 0, NULL );
|
2003-08-29 04:34:10 +00:00
|
|
|
if( ret < 0 )
|
2003-08-29 16:50:47 +00:00
|
|
|
RageException::Throw( averr_ssprintf(ret, "AVCodec: Couldn't open \"%s\"", actualID.filename.c_str()) );
|
2003-08-29 04:34:10 +00:00
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
ret = avcodec::av_find_stream_info( decoder->m_fctx );
|
2003-08-29 04:34:10 +00:00
|
|
|
if ( ret < 0 )
|
2003-09-03 02:18:39 +00:00
|
|
|
RageException::Throw( averr_ssprintf(ret, "AVCodec (%s): Couldn't find codec parameters", actualID.filename.c_str()) );
|
2003-08-29 04:34:10 +00:00
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
decoder->m_stream = FindVideoStream( decoder->m_fctx );
|
|
|
|
|
if ( decoder->m_stream == NULL )
|
|
|
|
|
RageException::Throw( "AVCodec (%s): Couldn't find any video streams", actualID.filename.c_str() );
|
2003-08-29 04:34:10 +00:00
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
avcodec::AVCodec *codec = avcodec::avcodec_find_decoder( decoder->m_stream->codec.codec_id );
|
2003-09-02 08:38:48 +00:00
|
|
|
if( codec == NULL )
|
2003-09-03 02:18:39 +00:00
|
|
|
RageException::Throw( "AVCodec (%s): Couldn't find decoder %i", actualID.filename.c_str(), decoder->m_stream->codec.codec_id );
|
2003-08-29 04:34:10 +00:00
|
|
|
|
2003-09-02 08:38:48 +00:00
|
|
|
LOG->Trace("Opening codec %s", codec->name );
|
2003-09-03 02:18:39 +00:00
|
|
|
ret = avcodec::avcodec_open( &decoder->m_stream->codec, codec );
|
2003-08-29 04:34:10 +00:00
|
|
|
if ( ret < 0 )
|
2003-09-03 02:18:39 +00:00
|
|
|
RageException::Throw( averr_ssprintf(ret, "AVCodec (%s): Couldn't open codec \"%s\"", actualID.filename.c_str(), codec->name) );
|
2003-08-29 04:34:10 +00:00
|
|
|
|
|
|
|
|
/* Cap the max texture size to the hardware max. */
|
|
|
|
|
actualID.iMaxSize = min( actualID.iMaxSize, DISPLAY->GetMaxTextureSize() );
|
|
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
m_iSourceWidth = decoder->m_stream->codec.width;
|
|
|
|
|
m_iSourceHeight = decoder->m_stream->codec.height;
|
2003-08-29 04:34:10 +00:00
|
|
|
|
|
|
|
|
/* image size cannot exceed max size */
|
|
|
|
|
m_iImageWidth = min( m_iSourceWidth, actualID.iMaxSize );
|
|
|
|
|
m_iImageHeight = min( m_iSourceHeight, actualID.iMaxSize );
|
|
|
|
|
|
|
|
|
|
/* Texture dimensions need to be a power of two; jump to the next. */
|
|
|
|
|
m_iTextureWidth = power_of_two(m_iImageWidth);
|
|
|
|
|
m_iTextureHeight = power_of_two(m_iImageHeight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-09-02 06:08:02 +00:00
|
|
|
/* Delete the decoder. The decoding thread must be stopped. */
|
|
|
|
|
void MovieTexture_FFMpeg::DestroyDecoder()
|
|
|
|
|
{
|
2003-09-03 02:18:39 +00:00
|
|
|
avcodec::avcodec_close( &decoder->m_stream->codec );
|
|
|
|
|
avcodec::av_close_input_file( decoder->m_fctx );
|
|
|
|
|
decoder->m_fctx = NULL;
|
|
|
|
|
decoder->m_stream = NULL;
|
2003-09-02 06:08:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Delete the surface and texture. The decoding thread must be stopped, and this
|
|
|
|
|
* is normally done after destroying the decoder. */
|
|
|
|
|
void MovieTexture_FFMpeg::DestroyTexture()
|
|
|
|
|
{
|
2003-08-29 04:34:10 +00:00
|
|
|
if( m_img )
|
|
|
|
|
{
|
|
|
|
|
SDL_FreeSurface( m_img );
|
|
|
|
|
m_img=NULL;
|
|
|
|
|
}
|
|
|
|
|
if(m_uTexHandle)
|
|
|
|
|
{
|
|
|
|
|
DISPLAY->DeleteTexture( m_uTexHandle );
|
|
|
|
|
m_uTexHandle = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-09-02 04:01:01 +00:00
|
|
|
void MovieTexture_FFMpeg::CreateTexture()
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
|
|
|
|
if( m_uTexHandle )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
CHECKPOINT;
|
|
|
|
|
|
|
|
|
|
/* If the movie is coming in RGB, we can potentially save a lot
|
|
|
|
|
* of time by simply sending frame updates to RageDisplay in the
|
|
|
|
|
* format we're receiving data. However, I think just about all
|
|
|
|
|
* formats output data in some form of YUV, so I havn't bothered
|
|
|
|
|
* with this yet.
|
|
|
|
|
*
|
|
|
|
|
* TODO: We could get a big speed bonus by doing the above if the
|
|
|
|
|
* hardware renderer can handle YUV textures. I think D3D can do
|
|
|
|
|
* this, as well as OpenGL on the Mac, but we don't have any infrastructure
|
2003-08-29 16:50:47 +00:00
|
|
|
* for this right now.
|
|
|
|
|
*
|
|
|
|
|
* A hint: http://oss.sgi.com/projects/performer/mail/info-performer/perf-01-06/0017.html */
|
2003-08-29 04:34:10 +00:00
|
|
|
PixelFormat pixfmt;
|
|
|
|
|
bool PreferHighColor = (TEXTUREMAN->GetMovieColorDepth() == 32);
|
|
|
|
|
m_AVTexfmt = FindCompatibleAVFormat( pixfmt, PreferHighColor );
|
|
|
|
|
|
|
|
|
|
if( m_AVTexfmt == -1 )
|
|
|
|
|
m_AVTexfmt = FindCompatibleAVFormat( pixfmt, !PreferHighColor );
|
|
|
|
|
|
|
|
|
|
if( m_AVTexfmt == -1 )
|
|
|
|
|
{
|
|
|
|
|
/* No dice. Use the first avcodec format of the preferred bit depth,
|
|
|
|
|
* and let the display system convert. */
|
|
|
|
|
for( m_AVTexfmt = 0; AVPixelFormats[m_AVTexfmt].bpp; ++m_AVTexfmt )
|
|
|
|
|
if( AVPixelFormats[m_AVTexfmt].HighColor == PreferHighColor )
|
|
|
|
|
break;
|
|
|
|
|
ASSERT( AVPixelFormats[m_AVTexfmt].bpp );
|
|
|
|
|
|
|
|
|
|
switch( TEXTUREMAN->GetMovieColorDepth() )
|
|
|
|
|
{
|
|
|
|
|
default:
|
|
|
|
|
ASSERT(0);
|
|
|
|
|
case 16:
|
|
|
|
|
if( DISPLAY->SupportsTextureFormat(FMT_RGB5) )
|
|
|
|
|
pixfmt = FMT_RGB5;
|
|
|
|
|
else
|
|
|
|
|
pixfmt = FMT_RGBA4; // everything supports RGBA4
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 32:
|
|
|
|
|
if( DISPLAY->SupportsTextureFormat(FMT_RGB8) )
|
|
|
|
|
pixfmt = FMT_RGB8;
|
|
|
|
|
else if( DISPLAY->SupportsTextureFormat(FMT_RGBA8) )
|
|
|
|
|
pixfmt = FMT_RGBA8;
|
|
|
|
|
else if( DISPLAY->SupportsTextureFormat(FMT_RGB5) )
|
|
|
|
|
pixfmt = FMT_RGB5;
|
|
|
|
|
else
|
|
|
|
|
pixfmt = FMT_RGBA4; // everything supports RGBA4
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( !m_img )
|
|
|
|
|
{
|
|
|
|
|
const AVPixelFormat_t *pfd = &AVPixelFormats[m_AVTexfmt];
|
|
|
|
|
|
|
|
|
|
LOG->Trace("format %i, %08x %08x %08x %08x",
|
|
|
|
|
pfd->bpp, pfd->masks[0], pfd->masks[1], pfd->masks[2], pfd->masks[3]);
|
|
|
|
|
|
|
|
|
|
m_img = SDL_CreateRGBSurfaceSane(SDL_SWSURFACE, m_iTextureWidth, m_iTextureHeight,
|
|
|
|
|
pfd->bpp, pfd->masks[0], pfd->masks[1], pfd->masks[2], pfd->masks[3]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_uTexHandle = DISPLAY->CreateTexture( pixfmt, m_img );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-09-02 04:01:01 +00:00
|
|
|
void MovieTexture_FFMpeg::DecoderThread()
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
2003-09-02 07:47:37 +00:00
|
|
|
RageTimer Timer;
|
2003-09-03 02:18:39 +00:00
|
|
|
float Clock = 0;
|
|
|
|
|
bool FrameSkipMode = false;
|
2003-09-02 05:31:47 +00:00
|
|
|
|
2003-08-29 04:34:10 +00:00
|
|
|
CHECKPOINT;
|
2003-09-02 07:47:37 +00:00
|
|
|
|
2003-08-29 04:34:10 +00:00
|
|
|
while( m_State != DECODER_QUIT )
|
|
|
|
|
{
|
|
|
|
|
if( m_State == PAUSE_DECODER )
|
|
|
|
|
{
|
|
|
|
|
SDL_Delay( 5 );
|
2003-09-02 00:38:01 +00:00
|
|
|
|
|
|
|
|
/* The video isn't running; skip time. */
|
2003-09-02 07:47:37 +00:00
|
|
|
Timer.GetDeltaTime();
|
2003-08-29 04:34:10 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CHECKPOINT;
|
|
|
|
|
|
2003-09-02 07:47:37 +00:00
|
|
|
/* We're playing. Update the clock. */
|
|
|
|
|
Clock += Timer.GetDeltaTime() * m_Rate;
|
2003-08-29 04:34:10 +00:00
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
/* Read a frame. */
|
|
|
|
|
int ret = decoder->GetFrame();
|
|
|
|
|
if( ret == -1 )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if( m_bWantRewind && decoder->CurrentTimestamp == 0 )
|
|
|
|
|
m_bWantRewind = false; /* ignore */
|
|
|
|
|
|
|
|
|
|
if( ret == 0 )
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
2003-09-03 02:18:39 +00:00
|
|
|
/* EOF. */
|
|
|
|
|
if( !m_bLoop )
|
2003-08-29 04:34:10 +00:00
|
|
|
return;
|
|
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
LOG->Trace( "File \"%s\" looping", GetID().filename.c_str() );
|
|
|
|
|
m_bWantRewind = true;
|
2003-08-29 04:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
if( m_bWantRewind )
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
2003-09-03 02:18:39 +00:00
|
|
|
m_bWantRewind = false;
|
2003-08-29 04:34:10 +00:00
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
/* Restart. */
|
|
|
|
|
DestroyDecoder();
|
|
|
|
|
CreateDecoder();
|
2003-08-29 04:34:10 +00:00
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
decoder->Init();
|
|
|
|
|
Clock = 0;
|
|
|
|
|
continue;
|
2003-08-29 04:34:10 +00:00
|
|
|
}
|
2003-09-03 02:18:39 +00:00
|
|
|
|
|
|
|
|
/* We got a frame. */
|
|
|
|
|
const float Offset = decoder->CurrentTimestamp - Clock;
|
|
|
|
|
|
|
|
|
|
/* If we're ahead, we're decoding too fast; delay. */
|
|
|
|
|
if( Offset > 0 )
|
|
|
|
|
{
|
|
|
|
|
SDL_Delay( int(1000*(decoder->CurrentTimestamp - Clock)) );
|
|
|
|
|
if( FrameSkipMode )
|
|
|
|
|
{
|
|
|
|
|
/* We're caught up; stop skipping frames. */
|
|
|
|
|
LOG->Trace( "stopped skipping frames" );
|
|
|
|
|
FrameSkipMode = false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* We're behind by -Offset seconds.
|
|
|
|
|
*
|
|
|
|
|
* If we're just slightly behind, don't worry about it; we'll simply
|
|
|
|
|
* not sleep, so we'll move as fast as we can to catch up.
|
|
|
|
|
*
|
|
|
|
|
* If we're far behind, we're short on CPU and we need to do something
|
|
|
|
|
* about it. We have at least two options:
|
|
|
|
|
*
|
|
|
|
|
* 1: We can skip texture updates. This is a big bottleneck on many
|
|
|
|
|
* systems.
|
|
|
|
|
*
|
|
|
|
|
* 2: If that's not enough, we can play with hurry_up.
|
|
|
|
|
*
|
|
|
|
|
* If we hit a threshold, start skipping frames via #1. If we do that,
|
|
|
|
|
* don't stop once we hit the threshold; keep doing it until we're fully
|
|
|
|
|
* caught up.
|
|
|
|
|
*
|
|
|
|
|
* I'm not sure when we should do #2. Also, we should try to notice if
|
|
|
|
|
* we simply don't have enough CPU for the video; it's better to just
|
|
|
|
|
* stay in frame skip mode than to enter and exit it constantly, but we
|
|
|
|
|
* don't want to do that due to a single timing glitch.
|
|
|
|
|
*
|
|
|
|
|
* XXX: is there a setting for hurry_up we can use when we're going to ignore
|
|
|
|
|
* a frame to make it take less time?
|
|
|
|
|
*/
|
|
|
|
|
const float FrameSkipThreshold = 0.5f;
|
|
|
|
|
|
|
|
|
|
if( -Offset >= FrameSkipThreshold && !FrameSkipMode )
|
|
|
|
|
{
|
|
|
|
|
LOG->Trace( "(%s) Time is %f, and the movie is at %f. Entering frame skip mode.",
|
|
|
|
|
GetID().filename.c_str(), decoder->CurrentTimestamp, Clock );
|
|
|
|
|
FrameSkipMode = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( FrameSkipMode && decoder->m_stream->codec.frame_number % 2 )
|
|
|
|
|
continue; /* skip */
|
|
|
|
|
|
|
|
|
|
/* Convert it. */
|
|
|
|
|
ConvertFrame();
|
|
|
|
|
|
|
|
|
|
/* Signal the main thread to update the image on the next Update. */
|
|
|
|
|
m_ImageWaiting=true;
|
|
|
|
|
|
|
|
|
|
CHECKPOINT;
|
|
|
|
|
SDL_SemWait( m_BufferFinished );
|
|
|
|
|
CHECKPOINT;
|
|
|
|
|
|
|
|
|
|
/* If the frame wasn't used, then we must be shutting down. */
|
|
|
|
|
ASSERT( !m_ImageWaiting || m_State == DECODER_QUIT );
|
2003-08-29 04:34:10 +00:00
|
|
|
}
|
|
|
|
|
CHECKPOINT;
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-02 04:01:01 +00:00
|
|
|
MovieTexture_FFMpeg::~MovieTexture_FFMpeg()
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
2003-09-02 06:08:02 +00:00
|
|
|
StopThread();
|
|
|
|
|
DestroyDecoder();
|
|
|
|
|
DestroyTexture();
|
2003-08-29 04:34:10 +00:00
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
delete decoder;
|
|
|
|
|
|
2003-08-29 04:34:10 +00:00
|
|
|
SDL_DestroySemaphore( m_BufferFinished );
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-02 04:01:01 +00:00
|
|
|
void MovieTexture_FFMpeg::Update(float fDeltaTime)
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
2003-09-03 02:18:39 +00:00
|
|
|
if( m_State != PLAYING )
|
|
|
|
|
return;
|
2003-08-29 04:34:10 +00:00
|
|
|
|
|
|
|
|
if( !m_ImageWaiting )
|
|
|
|
|
return;
|
|
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
CHECKPOINT;
|
|
|
|
|
|
|
|
|
|
UpdateFrame();
|
|
|
|
|
m_ImageWaiting = false;
|
|
|
|
|
SDL_SemPost(m_BufferFinished);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MovieTexture_FFMpeg::UpdateFrame()
|
|
|
|
|
{
|
2003-08-29 04:34:10 +00:00
|
|
|
/* Just in case we were invalidated: */
|
|
|
|
|
CreateTexture();
|
|
|
|
|
|
|
|
|
|
CHECKPOINT;
|
|
|
|
|
DISPLAY->UpdateTexture(
|
|
|
|
|
m_uTexHandle,
|
|
|
|
|
m_img,
|
|
|
|
|
0, 0,
|
|
|
|
|
m_iImageWidth, m_iImageHeight );
|
|
|
|
|
CHECKPOINT;
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-02 04:01:01 +00:00
|
|
|
void MovieTexture_FFMpeg::Reload()
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-02 04:01:01 +00:00
|
|
|
void MovieTexture_FFMpeg::StartThread()
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
2003-09-02 22:07:01 +00:00
|
|
|
/* If we had a frame waiting from a previous thread start, clear it. */
|
|
|
|
|
m_ImageWaiting = false;
|
|
|
|
|
|
2003-08-29 04:34:10 +00:00
|
|
|
ASSERT( m_State == DECODER_QUIT );
|
|
|
|
|
m_State = PAUSE_DECODER;
|
2003-09-02 04:01:01 +00:00
|
|
|
m_DecoderThread.SetName( ssprintf("MovieTexture_FFMpeg(%s)", GetID().filename.c_str()) );
|
2003-08-29 04:34:10 +00:00
|
|
|
m_DecoderThread.Create( DecoderThread_start, this );
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-02 04:01:01 +00:00
|
|
|
void MovieTexture_FFMpeg::StopThread()
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
|
|
|
|
LOG->Trace("Shutting down decoder thread ...");
|
|
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
if( !m_DecoderThread.IsCreated() )
|
2003-08-29 04:34:10 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_State = DECODER_QUIT;
|
|
|
|
|
|
|
|
|
|
/* Make sure we don't deadlock waiting for m_BufferFinished. */
|
|
|
|
|
SDL_SemPost(m_BufferFinished);
|
2003-08-29 05:39:47 +00:00
|
|
|
CHECKPOINT;
|
2003-08-29 04:34:10 +00:00
|
|
|
m_DecoderThread.Wait();
|
2003-08-29 05:39:47 +00:00
|
|
|
CHECKPOINT;
|
2003-08-29 04:34:10 +00:00
|
|
|
|
|
|
|
|
/* Clear the above post, if the thread didn't. */
|
|
|
|
|
SDL_SemTryWait(m_BufferFinished);
|
|
|
|
|
|
|
|
|
|
LOG->Trace("Decoder thread shut down.");
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-02 04:01:01 +00:00
|
|
|
void MovieTexture_FFMpeg::Play()
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
|
|
|
|
m_State = PLAYING;
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-02 04:01:01 +00:00
|
|
|
void MovieTexture_FFMpeg::Pause()
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
|
|
|
|
m_State = PAUSE_DECODER;
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-02 04:01:01 +00:00
|
|
|
void MovieTexture_FFMpeg::SetPosition( float fSeconds )
|
2003-08-29 04:34:10 +00:00
|
|
|
{
|
|
|
|
|
ASSERT( m_State != DECODER_QUIT );
|
|
|
|
|
|
|
|
|
|
/* We can reset to 0, but I don't think this API supports fast seeking
|
|
|
|
|
* yet. I don't think we ever actually seek except to 0 right now,
|
|
|
|
|
* anyway. XXX */
|
|
|
|
|
if( fSeconds != 0 )
|
|
|
|
|
{
|
2003-09-02 04:01:01 +00:00
|
|
|
LOG->Warn( "MovieTexture_FFMpeg::SetPosition(%f): non-0 seeking unsupported; ignored", fSeconds );
|
2003-08-29 04:34:10 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-03 02:18:39 +00:00
|
|
|
LOG->Trace( "Seek to %f (from %f)", fSeconds );
|
|
|
|
|
m_bWantRewind = true;
|
2003-08-29 04:34:10 +00:00
|
|
|
}
|
|
|
|
|
|