pull _convert_ctx out of loop

This commit is contained in:
David Rose 2011-11-11 19:28:41 +00:00
parent 2584e3d57d
commit 67dc3fabfa
2 changed files with 38 additions and 21 deletions

View File

@ -52,6 +52,7 @@ FfmpegVideoCursor() :
_packet1(NULL),
_format_ctx(NULL),
_video_ctx(NULL),
_convert_ctx(NULL),
_video_index(-1),
_frame(NULL),
_frame_out(NULL),
@ -133,7 +134,6 @@ init_from(FfmpegVideo *source) {
cleanup();
return;
}
}
_size_x = _video_ctx->width;
_size_y = _video_ctx->height;
@ -142,6 +142,13 @@ init_from(FfmpegVideo *source) {
_can_seek = true;
_can_seek_fast = true;
#ifdef HAVE_SWSCALE
_convert_ctx = sws_getContext(_size_x, _size_y,
_video_ctx->pix_fmt, _size_x, _size_y,
PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
#endif // HAVE_SWSCALE
}
_frame = avcodec_alloc_frame();
_frame_out = avcodec_alloc_frame();
if ((_frame == 0)||(_frame_out == 0)) {
@ -181,6 +188,7 @@ FfmpegVideoCursor(FfmpegVideo *src) :
_packet1(NULL),
_format_ctx(NULL),
_video_ctx(NULL),
_convert_ctx(NULL),
_video_index(-1),
_frame(NULL),
_frame_out(NULL),
@ -493,8 +501,17 @@ cleanup() {
_packet1 = NULL;
}
if ((_video_ctx)&&(_video_ctx->codec)) {
{
MutexHolder av_holder(_av_lock);
#ifdef HAVE_SWSCALE
if (_convert_ctx != NULL) {
sws_freeContext(_convert_ctx);
}
_convert_ctx = NULL;
#endif // HAVE_SWSCALE
if ((_video_ctx)&&(_video_ctx->codec)) {
avcodec_close(_video_ctx);
}
_video_ctx = NULL;
@ -503,6 +520,7 @@ cleanup() {
_ffvfile.close();
_format_ctx = NULL;
}
}
_video_index = -1;
}
@ -927,13 +945,9 @@ export_frame(MovieVideoCursor::Buffer *buffer) {
buffer->_begin_time = _begin_time;
buffer->_end_time = _end_time;
#ifdef HAVE_SWSCALE
struct SwsContext *convert_ctx = sws_getContext(_size_x, _size_y,
_video_ctx->pix_fmt, _size_x, _size_y,
PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
nassertv(convert_ctx != NULL);
sws_scale(convert_ctx, _frame->data, _frame->linesize,
nassertv(_convert_ctx != NULL);
sws_scale(_convert_ctx, _frame->data, _frame->linesize,
0, _size_y, _frame_out->data, _frame_out->linesize);
sws_freeContext(convert_ctx);
#else
img_convert((AVPicture *)_frame_out, PIX_FMT_BGR24,
(AVPicture *)_frame, _video_ctx->pix_fmt, _size_x, _size_y);

View File

@ -33,6 +33,7 @@ struct AVCodecContext;
struct AVStream;
struct AVPacket;
struct AVFrame;
struct SwsContext;
////////////////////////////////////////////////////////////////////
// Class : FfmpegVideoCursor
@ -116,6 +117,8 @@ private:
double _packet_time;
AVFormatContext *_format_ctx;
AVCodecContext *_video_ctx;
SwsContext *_convert_ctx;
FfmpegVirtualFile _ffvfile;
int _video_index;
double _video_timebase;