mirror of https://github.com/odrling/Aegisub
Add support for externally controlled frame data to AegiVideoFrame, and take advantage of this in the FFMS2 video provider to eliminate a copy of the frame data. Net speedup for seeking from beginning to end of several test files is 10-20%.
Originally committed to SVN as r3729.
This commit is contained in:
parent
c4d07cb525
commit
4fc2b90399
|
@ -34,21 +34,17 @@
|
||||||
/// @ingroup video
|
/// @ingroup video
|
||||||
///
|
///
|
||||||
|
|
||||||
|
|
||||||
///////////
|
|
||||||
// Headers
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "video_frame.h"
|
#include "video_frame.h"
|
||||||
|
|
||||||
|
|
||||||
/// @brief Reset
|
/// @brief Reset values to the defaults
|
||||||
///
|
///
|
||||||
|
/// Note that this function DOES NOT unallocate memory.
|
||||||
|
/// Use Clear() for that
|
||||||
void AegiVideoFrame::Reset() {
|
void AegiVideoFrame::Reset() {
|
||||||
// Note that this function DOES NOT unallocate memory.
|
|
||||||
// Use Clear() for that
|
|
||||||
|
|
||||||
// Zero variables
|
// Zero variables
|
||||||
for (int i=0;i<4;i++) {
|
for (int i=0;i<4;i++) {
|
||||||
data[i] = NULL;
|
data[i] = NULL;
|
||||||
|
@ -62,25 +58,19 @@ void AegiVideoFrame::Reset() {
|
||||||
format = FORMAT_NONE;
|
format = FORMAT_NONE;
|
||||||
flipped = false;
|
flipped = false;
|
||||||
invertChannels = true;
|
invertChannels = true;
|
||||||
|
ownMem = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// @brief Constructor
|
/// @brief Constructor
|
||||||
///
|
|
||||||
AegiVideoFrame::AegiVideoFrame() {
|
AegiVideoFrame::AegiVideoFrame() {
|
||||||
Reset();
|
Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Create a solid black frame of the request size and format
|
||||||
|
|
||||||
/// @brief Create default
|
|
||||||
/// @param width
|
/// @param width
|
||||||
/// @param height
|
/// @param height
|
||||||
/// @param fmt
|
/// @param fmt
|
||||||
///
|
|
||||||
AegiVideoFrame::AegiVideoFrame(int width,int height,VideoFrameFormat fmt) {
|
AegiVideoFrame::AegiVideoFrame(int width,int height,VideoFrameFormat fmt) {
|
||||||
// Clear
|
|
||||||
Reset();
|
Reset();
|
||||||
|
|
||||||
// Set format
|
// Set format
|
||||||
|
@ -93,7 +83,6 @@ AegiVideoFrame::AegiVideoFrame(int width,int height,VideoFrameFormat fmt) {
|
||||||
pitch[2] = w/2;
|
pitch[2] = w/2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allocate
|
|
||||||
Allocate();
|
Allocate();
|
||||||
|
|
||||||
// Clear data
|
// Clear data
|
||||||
|
@ -101,12 +90,9 @@ AegiVideoFrame::AegiVideoFrame(int width,int height,VideoFrameFormat fmt) {
|
||||||
memset(data[0],0,size);
|
memset(data[0],0,size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Allocate memory if needed
|
||||||
|
|
||||||
/// @brief Allocate
|
|
||||||
///
|
|
||||||
void AegiVideoFrame::Allocate() {
|
void AegiVideoFrame::Allocate() {
|
||||||
// Check consistency
|
// Check for sanity
|
||||||
wxASSERT(pitch[0] > 0 && pitch[0] < 10000);
|
wxASSERT(pitch[0] > 0 && pitch[0] < 10000);
|
||||||
wxASSERT(w > 0 && w < 10000);
|
wxASSERT(w > 0 && w < 10000);
|
||||||
wxASSERT(h > 0 && h < 10000);
|
wxASSERT(h > 0 && h < 10000);
|
||||||
|
@ -123,8 +109,8 @@ void AegiVideoFrame::Allocate() {
|
||||||
else size = pitch[0] * height;
|
else size = pitch[0] * height;
|
||||||
|
|
||||||
// Reallocate, if necessary
|
// Reallocate, if necessary
|
||||||
if (memSize != size) {
|
if (memSize != size || !ownMem) {
|
||||||
if (data[0]) {
|
if (data[0] && ownMem) {
|
||||||
delete[] data[0];
|
delete[] data[0];
|
||||||
}
|
}
|
||||||
data[0] = new unsigned char[size];
|
data[0] = new unsigned char[size];
|
||||||
|
@ -137,32 +123,19 @@ void AegiVideoFrame::Allocate() {
|
||||||
data[2] = data[0] + (pitch[0]*height+pitch[1]*height/2);
|
data[2] = data[0] + (pitch[0]*height+pitch[1]*height/2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ownMem = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief Clear
|
/// @brief Clear
|
||||||
void AegiVideoFrame::Clear() {
|
void AegiVideoFrame::Clear() {
|
||||||
delete[] data[0];
|
if (ownMem) delete[] data[0];
|
||||||
|
|
||||||
// Zero variables
|
Reset();
|
||||||
for (int i=0;i<4;i++) {
|
|
||||||
data[i] = NULL;
|
|
||||||
pitch[i] = 0;
|
|
||||||
}
|
|
||||||
memSize = 0;
|
|
||||||
w = 0;
|
|
||||||
h = 0;
|
|
||||||
|
|
||||||
// Reset properties
|
|
||||||
format = FORMAT_NONE;
|
|
||||||
flipped = false;
|
|
||||||
invertChannels = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Copy from an AegiVideoFrame
|
||||||
|
/// @param source The frame to copy from
|
||||||
/// @brief Create copy
|
|
||||||
/// @param source
|
|
||||||
///
|
|
||||||
void AegiVideoFrame::CopyFrom(const AegiVideoFrame &source) {
|
void AegiVideoFrame::CopyFrom(const AegiVideoFrame &source) {
|
||||||
w = source.w;
|
w = source.w;
|
||||||
h = source.h;
|
h = source.h;
|
||||||
|
@ -174,20 +147,40 @@ void AegiVideoFrame::CopyFrom(const AegiVideoFrame &source) {
|
||||||
invertChannels = source.invertChannels;
|
invertChannels = source.invertChannels;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Set the frame to an externally allocated block of memory
|
||||||
|
/// @param source Target frame data
|
||||||
|
/// @param width The frame width in pixels
|
||||||
|
/// @param height The frame height in pixels
|
||||||
|
/// @param pitch The frame's pitch
|
||||||
|
/// @param format The fram'e format
|
||||||
|
void AegiVideoFrame::SetTo(const unsigned char *const source[], int width, int height, const int pitch[4], VideoFrameFormat format) {
|
||||||
|
wxASSERT(pitch[0] > 0 && pitch[0] < 10000);
|
||||||
|
wxASSERT(width > 0 && width < 10000);
|
||||||
|
wxASSERT(height > 0 && height < 10000);
|
||||||
|
wxASSERT(format != FORMAT_NONE);
|
||||||
|
|
||||||
|
ownMem = false;
|
||||||
|
w = width;
|
||||||
|
h = height;
|
||||||
|
this->format = format;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
// Note that despite this cast, the contents of data should still never be modified
|
||||||
|
data[i] = const_cast<unsigned char*>(source[i]);
|
||||||
|
this->pitch[i] = pitch[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// @brief This function is only used on screenshots, so it doesn't have to be fast ------ Get wxImage
|
/// @brief This function is only used on screenshots, so it doesn't have to be fast ------ Get wxImage
|
||||||
/// @return
|
/// @return
|
||||||
///
|
|
||||||
wxImage AegiVideoFrame::GetImage() const {
|
wxImage AegiVideoFrame::GetImage() const {
|
||||||
// RGB
|
|
||||||
if (format == FORMAT_RGB32 || format == FORMAT_RGB24) {
|
if (format == FORMAT_RGB32 || format == FORMAT_RGB24) {
|
||||||
// Create
|
// Create
|
||||||
unsigned char *buf = (unsigned char*)malloc(w*h*3);
|
unsigned char *buf = (unsigned char*)malloc(w*h*3);
|
||||||
const unsigned char *src = data[0];
|
const unsigned char *src = data[0];
|
||||||
unsigned char *dst = buf;
|
unsigned char *dst = buf;
|
||||||
|
|
||||||
// Bytes per pixel
|
|
||||||
int Bpp = GetBpp();
|
int Bpp = GetBpp();
|
||||||
|
|
||||||
// Convert
|
// Convert
|
||||||
|
@ -208,18 +201,11 @@ wxImage AegiVideoFrame::GetImage() const {
|
||||||
img.SetData(buf);
|
img.SetData(buf);
|
||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
|
|
||||||
// YV12
|
|
||||||
else if (format == FORMAT_YV12) {
|
else if (format == FORMAT_YV12) {
|
||||||
AegiVideoFrame temp;
|
AegiVideoFrame temp;
|
||||||
temp.w = w;
|
temp.ConvertFrom(*this);
|
||||||
temp.h = h;
|
return temp.GetImage();
|
||||||
temp.pitch[0] = w*4;
|
|
||||||
temp.format = FORMAT_RGB32;
|
|
||||||
temp.ConvertFrom(*this);
|
|
||||||
return temp.GetImage();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
return wxImage(w,h);
|
return wxImage(w,h);
|
||||||
}
|
}
|
||||||
|
@ -240,19 +226,18 @@ int AegiVideoFrame::GetBpp(int plane) const {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// @brief Convert from another frame
|
/// @brief Convert from another frame
|
||||||
/// @param source
|
/// @param source The frame to convert from
|
||||||
///
|
/// @param newFormat The format to convert to
|
||||||
void AegiVideoFrame::ConvertFrom(const AegiVideoFrame &source) {
|
void AegiVideoFrame::ConvertFrom(const AegiVideoFrame &source, VideoFrameFormat newFormat) {
|
||||||
// Ensure compatibility
|
if (newFormat != FORMAT_RGB32) throw _T("AegiVideoFrame::ConvertFrom: Unsupported destination format.");
|
||||||
if (w != source.w) throw _T("AegiVideoFrame::ConvertFrom: Widths don't match.");
|
if (source.format != FORMAT_YV12) throw _T("AegiVideoFrame::ConvertFrom: Unsupported source format.");
|
||||||
if (h != source.h) throw _T("AegiVideoFrame::ConvertFrom: Heights don't match.");
|
|
||||||
if (format != FORMAT_RGB32) throw _T("AegiVideoFrame::ConvertFrom: Unsupported destination format.");
|
w = source.w;
|
||||||
if (source.format != FORMAT_YV12) throw _T("AegiVideoFrame::ConvertFrom: Unsupported source format.");
|
h = source.h;
|
||||||
|
format = newFormat;
|
||||||
|
pitch[0] = w * 4;
|
||||||
|
|
||||||
// Allocate
|
|
||||||
Allocate();
|
Allocate();
|
||||||
|
|
||||||
// Set up pointers
|
// Set up pointers
|
||||||
|
@ -297,5 +282,3 @@ void AegiVideoFrame::ConvertFrom(const AegiVideoFrame &source) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -77,12 +77,13 @@ enum VideoFrameFormat {
|
||||||
/// DOCME
|
/// DOCME
|
||||||
class AegiVideoFrame {
|
class AegiVideoFrame {
|
||||||
private:
|
private:
|
||||||
|
unsigned int memSize; /// The size in bytes of the frame buffer
|
||||||
/// DOCME
|
/// Whether the object owns its buffer. If this is false, **data should never be modified
|
||||||
unsigned int memSize;
|
bool ownMem;
|
||||||
void Reset();
|
void Reset();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
void Allocate();
|
||||||
|
|
||||||
/// Pointers to the data planes. Interleaved formats only use data[0]
|
/// Pointers to the data planes. Interleaved formats only use data[0]
|
||||||
unsigned char *data[4];
|
unsigned char *data[4];
|
||||||
|
@ -108,10 +109,11 @@ public:
|
||||||
AegiVideoFrame();
|
AegiVideoFrame();
|
||||||
AegiVideoFrame(int width,int height,VideoFrameFormat format=FORMAT_RGB32);
|
AegiVideoFrame(int width,int height,VideoFrameFormat format=FORMAT_RGB32);
|
||||||
|
|
||||||
void Allocate();
|
|
||||||
void Clear();
|
void Clear();
|
||||||
void CopyFrom(const AegiVideoFrame &source);
|
void CopyFrom(const AegiVideoFrame &source);
|
||||||
void ConvertFrom(const AegiVideoFrame &source);
|
void ConvertFrom(const AegiVideoFrame &source, VideoFrameFormat newFormat=FORMAT_RGB32);
|
||||||
|
void SetTo(const unsigned char *const source[], int width, int height, const int pitch[4], VideoFrameFormat format);
|
||||||
|
|
||||||
|
|
||||||
wxImage GetImage() const;
|
wxImage GetImage() const;
|
||||||
int GetBpp(int plane=0) const;
|
int GetBpp(int plane=0) const;
|
||||||
|
|
|
@ -259,7 +259,7 @@ void VideoOutGL::InitTextures(int width, int height, GLenum format, int bpp) {
|
||||||
frameFormat = format;
|
frameFormat = format;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VideoOutGL::DisplayFrame(AegiVideoFrame frame, int frameNumber, int sw, int sh) {
|
void VideoOutGL::DisplayFrame(const AegiVideoFrame& frame, int frameNumber, int sw, int sh) {
|
||||||
if (frame.h == 0 || frame.w == 0) return;
|
if (frame.h == 0 || frame.w == 0) return;
|
||||||
|
|
||||||
if (frameNumber == -1) frameNumber = lastFrame;
|
if (frameNumber == -1) frameNumber = lastFrame;
|
||||||
|
|
|
@ -88,7 +88,7 @@ public:
|
||||||
/// @param frameNumber The frame number of the frame to be displayed
|
/// @param frameNumber The frame number of the frame to be displayed
|
||||||
/// @param sw The current script width
|
/// @param sw The current script width
|
||||||
/// @param sh The current script height
|
/// @param sh The current script height
|
||||||
void DisplayFrame(AegiVideoFrame frame, int frameNumber, int sw, int sh);
|
void DisplayFrame(const AegiVideoFrame& frame, int frameNumber, int sw, int sh);
|
||||||
|
|
||||||
/// @brief Force the redisplay of the frame the next time DisplayFrame is called even if the frame number has not changed
|
/// @brief Force the redisplay of the frame the next time DisplayFrame is called even if the frame number has not changed
|
||||||
void InvalidateFrame() { lastFrame = -1; }
|
void InvalidateFrame() { lastFrame = -1; }
|
||||||
|
|
|
@ -312,9 +312,6 @@ const AegiVideoFrame FFmpegSourceVideoProvider::GetFrame(int _n) {
|
||||||
// set position
|
// set position
|
||||||
FrameNumber = n;
|
FrameNumber = n;
|
||||||
|
|
||||||
// this is what we'll return eventually
|
|
||||||
AegiVideoFrame &DstFrame = CurFrame;
|
|
||||||
|
|
||||||
// decode frame
|
// decode frame
|
||||||
const FFMS_Frame *SrcFrame = FFMS_GetFrame(VideoSource, n, &ErrInfo);
|
const FFMS_Frame *SrcFrame = FFMS_GetFrame(VideoSource, n, &ErrInfo);
|
||||||
if (SrcFrame == NULL) {
|
if (SrcFrame == NULL) {
|
||||||
|
@ -322,22 +319,8 @@ const AegiVideoFrame FFmpegSourceVideoProvider::GetFrame(int _n) {
|
||||||
throw ErrorMsg;
|
throw ErrorMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
// set some properties
|
CurFrame.SetTo(SrcFrame->Data, Width, Height, SrcFrame->Linesize, FORMAT_RGB32);
|
||||||
DstFrame.format = FORMAT_RGB32;
|
return CurFrame;
|
||||||
DstFrame.w = Width;
|
|
||||||
DstFrame.h = Height;
|
|
||||||
DstFrame.flipped = false;
|
|
||||||
DstFrame.invertChannels = true;
|
|
||||||
|
|
||||||
// allocate destination
|
|
||||||
for (int i = 0; i < 4; i++)
|
|
||||||
DstFrame.pitch[i] = SrcFrame->Linesize[i];
|
|
||||||
DstFrame.Allocate();
|
|
||||||
|
|
||||||
// copy data to destination
|
|
||||||
memcpy(DstFrame.data[0], SrcFrame->Data[0], DstFrame.pitch[0] * DstFrame.h);
|
|
||||||
|
|
||||||
return DstFrame;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -421,12 +421,8 @@ const AegiVideoFrame YUV4MPEGVideoProvider::GetFrame(int n) {
|
||||||
}
|
}
|
||||||
|
|
||||||
AegiVideoFrame dst_frame;
|
AegiVideoFrame dst_frame;
|
||||||
dst_frame.format = dst_fmt;
|
|
||||||
dst_frame.w = w;
|
|
||||||
dst_frame.h = h;
|
|
||||||
dst_frame.invertChannels = true;
|
dst_frame.invertChannels = true;
|
||||||
dst_frame.pitch[0] = w * 4;
|
dst_frame.ConvertFrom(tmp_frame, dst_fmt);
|
||||||
dst_frame.ConvertFrom(tmp_frame);
|
|
||||||
|
|
||||||
tmp_frame.Clear();
|
tmp_frame.Clear();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue