Document the Y4M video provider.

Originally committed to SVN as r3376.
This commit is contained in:
Karl Blomster 2009-08-07 19:59:37 +00:00
parent c6c99a1fe6
commit 1a182098fc
2 changed files with 103 additions and 227 deletions

View file

@ -42,20 +42,15 @@
// (yes, really) // (yes, really)
// With cstdio it's at least possible to work around the problem... // With cstdio it's at least possible to work around the problem...
#ifdef _MSC_VER #ifdef _MSC_VER
/// DOCME
#define fseeko _fseeki64 #define fseeko _fseeki64
/// DOCME
#define ftello _ftelli64 #define ftello _ftelli64
#endif #endif
/// @brief DOCME /// @brief Constructor
/// @param filename /// @param filename The filename to open
///
YUV4MPEGVideoProvider::YUV4MPEGVideoProvider(wxString filename) { YUV4MPEGVideoProvider::YUV4MPEGVideoProvider(wxString filename) {
sf = NULL; sf = NULL;
w = 0; w = 0;
@ -86,18 +81,14 @@ YUV4MPEGVideoProvider::YUV4MPEGVideoProvider(wxString filename) {
} }
/// @brief Destructor
/// @brief DOCME
///
YUV4MPEGVideoProvider::~YUV4MPEGVideoProvider() { YUV4MPEGVideoProvider::~YUV4MPEGVideoProvider() {
Close(); Close();
} }
/// @brief Open a video file
/// @brief DOCME /// @param _filename The video file to open
/// @param _filename
///
void YUV4MPEGVideoProvider::LoadVideo(const wxString _filename) { void YUV4MPEGVideoProvider::LoadVideo(const wxString _filename) {
Close(); Close();
@ -128,17 +119,19 @@ void YUV4MPEGVideoProvider::LoadVideo(const wxString _filename) {
if (imode == Y4M_ILACE_NOTSET) if (imode == Y4M_ILACE_NOTSET)
imode = Y4M_ILACE_UNKNOWN; imode = Y4M_ILACE_UNKNOWN;
luma_sz = w * h;
switch (pixfmt) { switch (pixfmt) {
case Y4M_PIXFMT_420JPEG: case Y4M_PIXFMT_420JPEG:
case Y4M_PIXFMT_420MPEG2: case Y4M_PIXFMT_420MPEG2:
case Y4M_PIXFMT_420PALDV: case Y4M_PIXFMT_420PALDV:
frame_sz = (w * h * 3) / 2; break; chroma_sz = (w * h) / 2; break;
case Y4M_PIXFMT_422: case Y4M_PIXFMT_422:
frame_sz = (w * h * 2); break; chroma_sz = (w / 2) * h; break; // should be safe to assume that width is mod2
// TODO: add support for more pixel formats /// @todo add support for more pixel formats
default: default:
throw wxString(_T("Unsupported colorspace")); throw wxString(_T("Unsupported pixel format"));
} }
frame_sz = luma_sz + chroma_sz*2;
num_frames = IndexFile(); num_frames = IndexFile();
if (num_frames <= 0 || seek_table.empty()) if (num_frames <= 0 || seek_table.empty())
@ -149,9 +142,7 @@ void YUV4MPEGVideoProvider::LoadVideo(const wxString _filename) {
} }
/// @brief Closes the currently open file (if any) and resets reader state
/// @brief DOCME
///
void YUV4MPEGVideoProvider::Close() { void YUV4MPEGVideoProvider::Close() {
seek_table.clear(); seek_table.clear();
if (sf) if (sf)
@ -160,10 +151,9 @@ void YUV4MPEGVideoProvider::Close() {
} }
/// @brief Checks if the file is an YUV4MPEG file or not
/// @brief verify that the file is actually a YUV4MPEG file /// Note that it reports the error by throwing an exception,
/// @return /// not by returning a false value.
///
void YUV4MPEGVideoProvider::CheckFileFormat() { void YUV4MPEGVideoProvider::CheckFileFormat() {
char buf[10]; char buf[10];
if (fread(buf, 10, 1, sf) != 1) if (fread(buf, 10, 1, sf) != 1)
@ -175,12 +165,10 @@ void YUV4MPEGVideoProvider::CheckFileFormat() {
} }
/// @brief Read a frame or file header at a given file position
/// @brief read a frame or file header and return a list of its parameters /// @param startpos The byte offset at where to start reading
/// @param startpos /// @param reset_pos If true, the function will reset the file position to what it was before the function call before returning
/// @param reset_pos /// @return A list of parameters
/// @return
///
std::vector<wxString> YUV4MPEGVideoProvider::ReadHeader(int64_t startpos, bool reset_pos) { std::vector<wxString> YUV4MPEGVideoProvider::ReadHeader(int64_t startpos, bool reset_pos) {
int64_t oldpos = ftello(sf); int64_t oldpos = ftello(sf);
std::vector<wxString> tags; std::vector<wxString> tags;
@ -230,10 +218,8 @@ std::vector<wxString> YUV4MPEGVideoProvider::ReadHeader(int64_t startpos, bool r
} }
/// @brief Parses a list of parameters and sets reader state accordingly
/// @brief parse a file header and set file properties /// @param tags The list of parameters to parse
/// @param tags
///
void YUV4MPEGVideoProvider::ParseFileHeader(const std::vector<wxString>& tags) { void YUV4MPEGVideoProvider::ParseFileHeader(const std::vector<wxString>& tags) {
if (tags.size() <= 1) if (tags.size() <= 1)
throw wxString(_T("ParseFileHeader: contentless header")); throw wxString(_T("ParseFileHeader: contentless header"));
@ -318,32 +304,32 @@ void YUV4MPEGVideoProvider::ParseFileHeader(const std::vector<wxString>& tags) {
h = t_h; h = t_h;
fps_rat.num = t_fps_num; fps_rat.num = t_fps_num;
fps_rat.den = t_fps_den; fps_rat.den = t_fps_den;
pixfmt = t_pixfmt != Y4M_PIXFMT_NONE ? t_pixfmt : Y4M_PIXFMT_420JPEG; pixfmt = t_pixfmt != Y4M_PIXFMT_NONE ? t_pixfmt : Y4M_PIXFMT_420JPEG;
imode = t_imode != Y4M_ILACE_NOTSET ? t_imode : Y4M_ILACE_UNKNOWN; imode = t_imode != Y4M_ILACE_NOTSET ? t_imode : Y4M_ILACE_UNKNOWN;
inited = true; inited = true;
} }
} }
/// @brief Parses a frame header
/// @brief parse a frame header (currently unused) /// @param tags The list of parameters to parse
/// @param tags /// @return The flags set, as a binary mask
/// @return /// This function is currently unimplemented (it will always return Y4M_FFLAG_NONE).
///
YUV4MPEGVideoProvider::Y4M_FrameFlags YUV4MPEGVideoProvider::ParseFrameHeader(const std::vector<wxString>& tags) { YUV4MPEGVideoProvider::Y4M_FrameFlags YUV4MPEGVideoProvider::ParseFrameHeader(const std::vector<wxString>& tags) {
if (tags.front().Cmp(_("FRAME"))) if (tags.front().Cmp(_("FRAME")))
throw wxString(_T("ParseFrameHeader: malformed frame header (bad magic)")); throw wxString(_T("ParseFrameHeader: malformed frame header (bad magic)"));
// TODO: implement parsing of rff flags etc /// @todo implement parsing of frame flags
return Y4M_FFLAG_NONE; return Y4M_FFLAG_NONE;
} }
/// @brief Indexes the file
/// @brief index the file, i.e. find all frames and their flags /// @return The number of frames found in the file
/// @return /// This function goes through the file, finds and parses all file and frame headers,
/// /// and creates a seek table that lists the byte positions of all frames so seeking
/// can easily be done.
int YUV4MPEGVideoProvider::IndexFile() { int YUV4MPEGVideoProvider::IndexFile() {
int framecount = 0; int framecount = 0;
int64_t curpos = ftello(sf); int64_t curpos = ftello(sf);
@ -375,7 +361,7 @@ int YUV4MPEGVideoProvider::IndexFile() {
throw wxString::Format(_T("IndexFile: failed seeking to position %d"), curpos + frame_sz); throw wxString::Format(_T("IndexFile: failed seeking to position %d"), curpos + frame_sz);
} }
else { else {
// TODO: implement this /// @todo implement rff flags etc
} }
} }
@ -384,10 +370,9 @@ int YUV4MPEGVideoProvider::IndexFile() {
/// @brief DOCME /// @brief Gets a given frame
/// @param n /// @param n The frame number to return
/// @return /// @return The video frame
///
const AegiVideoFrame YUV4MPEGVideoProvider::GetFrame(int n) { const AegiVideoFrame YUV4MPEGVideoProvider::GetFrame(int n) {
// don't try to seek to insane places // don't try to seek to insane places
if (n < 0) if (n < 0)
@ -399,15 +384,15 @@ const AegiVideoFrame YUV4MPEGVideoProvider::GetFrame(int n) {
VideoFrameFormat src_fmt, dst_fmt; VideoFrameFormat src_fmt, dst_fmt;
dst_fmt = FORMAT_RGB32; dst_fmt = FORMAT_RGB32;
int uv_width, uv_height; int uv_width;
switch (pixfmt) { switch (pixfmt) {
case Y4M_PIXFMT_420JPEG: case Y4M_PIXFMT_420JPEG:
case Y4M_PIXFMT_420MPEG2: case Y4M_PIXFMT_420MPEG2:
case Y4M_PIXFMT_420PALDV: case Y4M_PIXFMT_420PALDV:
src_fmt = FORMAT_YV12; uv_width = w / 2; uv_height = h / 2; break; src_fmt = FORMAT_YV12; uv_width = w / 2; break;
case Y4M_PIXFMT_422: case Y4M_PIXFMT_422:
src_fmt = FORMAT_YUY2; uv_width = w / 2; uv_height = h; break; src_fmt = FORMAT_YUY2; uv_width = w / 2; break;
// TODO: add support for more pixel formats /// @todo add support for more pixel formats
default: default:
throw wxString(_T("YUV4MPEG video provider: GetFrame: Unsupported source colorspace")); throw wxString(_T("YUV4MPEG video provider: GetFrame: Unsupported source colorspace"));
} }
@ -419,16 +404,17 @@ const AegiVideoFrame YUV4MPEGVideoProvider::GetFrame(int n) {
tmp_frame.h = h; tmp_frame.h = h;
tmp_frame.invertChannels = false; tmp_frame.invertChannels = false;
tmp_frame.pitch[0] = w; tmp_frame.pitch[0] = w;
for (int i=1;i<=2;i++) tmp_frame.pitch[i] = uv_width; for (int i=1;i<=2;i++)
tmp_frame.pitch[i] = uv_width;
tmp_frame.Allocate(); tmp_frame.Allocate();
fseeko(sf, seek_table[n], SEEK_SET); fseeko(sf, seek_table[n], SEEK_SET);
size_t ret; size_t ret;
ret = fread(tmp_frame.data[0], w * h, 1, sf); ret = fread(tmp_frame.data[0], luma_sz, 1, sf);
if (ret != 1 || feof(sf) || ferror(sf)) if (ret != 1 || feof(sf) || ferror(sf))
throw wxString(_T("YUV4MPEG video provider: GetFrame: failed to read luma plane")); throw wxString(_T("YUV4MPEG video provider: GetFrame: failed to read luma plane"));
for (int i = 1; i <= 2; i++) { for (int i = 1; i <= 2; i++) {
ret = fread(tmp_frame.data[i], uv_width * uv_height, 1, sf); ret = fread(tmp_frame.data[i], chroma_sz, 1, sf);
if (ret != 1 || feof(sf) || ferror(sf)) if (ret != 1 || feof(sf) || ferror(sf))
throw wxString(_T("YUV4MPEG video provider: GetFrame: failed to read chroma planes")); throw wxString(_T("YUV4MPEG video provider: GetFrame: failed to read chroma planes"));
} }
@ -448,44 +434,24 @@ const AegiVideoFrame YUV4MPEGVideoProvider::GetFrame(int n) {
// Utility functions
/// @brief Utility functions
/// @return
///
int YUV4MPEGVideoProvider::GetWidth() { int YUV4MPEGVideoProvider::GetWidth() {
return w; return w;
} }
/// @brief DOCME
/// @return
///
int YUV4MPEGVideoProvider::GetHeight() { int YUV4MPEGVideoProvider::GetHeight() {
return h; return h;
} }
/// @brief DOCME
/// @return
///
int YUV4MPEGVideoProvider::GetFrameCount() { int YUV4MPEGVideoProvider::GetFrameCount() {
return num_frames; return num_frames;
} }
/// @brief DOCME
/// @return
///
int YUV4MPEGVideoProvider::GetPosition() { int YUV4MPEGVideoProvider::GetPosition() {
return cur_fn; return cur_fn;
} }
/// @brief DOCME
///
double YUV4MPEGVideoProvider::GetFPS() { double YUV4MPEGVideoProvider::GetFPS() {
return double(fps_rat.num) / double(fps_rat.den); return double(fps_rat.num) / double(fps_rat.den);
} }

View file

@ -45,163 +45,95 @@
#include <vector> #include <vector>
/// DOCME /// the maximum allowed header length, in bytes
#define YUV4MPEG_HEADER_MAXLEN 128 #define YUV4MPEG_HEADER_MAXLEN 128
/// @class YUV4MPEGVideoProvider /// @class YUV4MPEGVideoProvider
/// @brief DOCME /// @brief Implements reading of YUV4MPEG uncompressed video files
///
/// DOCME
class YUV4MPEGVideoProvider : public VideoProvider { class YUV4MPEGVideoProvider : public VideoProvider {
private: private:
/// Pixel formats
/// DOCME
enum Y4M_PixelFormat { enum Y4M_PixelFormat {
Y4M_PIXFMT_NONE = -1, /// not set/unknown
/// DOCME /// 4:2:0 sampling variants.
Y4M_PIXFMT_NONE = -1, /// afaict the only difference between these three
/// is the chroma sample location, and nobody cares about that.
Y4M_PIXFMT_420JPEG, /// 4:2:0, H/V centered, for JPEG/MPEG-1
Y4M_PIXFMT_420MPEG2, /// 4:2:0, H cosited, for MPEG-2
Y4M_PIXFMT_420PALDV, /// 4:2:0, alternating Cb/Cr, for PAL-DV
/// DOCME Y4M_PIXFMT_411, /// 4:1:1, H cosited
Y4M_PIXFMT_420JPEG, // afaict the only difference between Y4M_PIXFMT_422, /// 4:2:2, H cosited
Y4M_PIXFMT_444, /// 4:4:4, i.e. no chroma subsampling
Y4M_PIXFMT_444ALPHA, /// 4:4:4 plus alpha channel
/// DOCME Y4M_PIXFMT_MONO, /// luma only (grayscale)
Y4M_PIXFMT_420MPEG2, // these three is the chroma sample location,
/// DOCME
Y4M_PIXFMT_420PALDV, // and nobody cares about that.
/// DOCME
Y4M_PIXFMT_411,
/// DOCME
Y4M_PIXFMT_422,
/// DOCME
Y4M_PIXFMT_444,
/// DOCME
Y4M_PIXFMT_444ALPHA,
/// DOCME
Y4M_PIXFMT_MONO,
}; };
/// DOCME /// Interlacing mode for an entire stream
enum Y4M_InterlacingMode { enum Y4M_InterlacingMode {
Y4M_ILACE_NOTSET = -1, /// undefined
Y4M_ILACE_PROGRESSIVE, /// progressive (no interlacing)
/// DOCME Y4M_ILACE_TFF, /// interlaced, top field first
Y4M_ILACE_NOTSET = -1, // not to be confused with Y4M_ILACE_UNKNOWN Y4M_ILACE_BFF, /// interlaced, bottom field first
/// DOCME Y4M_ILACE_MIXED, /// mixed interlaced/progressive, possibly with RFF flags
Y4M_ILACE_PROGRESSIVE, Y4M_ILACE_UNKNOWN, /// unknown interlacing mode (not the same as undefined)
/// DOCME
Y4M_ILACE_TFF,
/// DOCME
Y4M_ILACE_BFF,
/// DOCME
Y4M_ILACE_MIXED,
/// DOCME
Y4M_ILACE_UNKNOWN,
}; };
/// DOCME /// Frame information flags
enum Y4M_FrameFlags { enum Y4M_FrameFlags {
Y4M_FFLAG_NOTSET = -1, /// undefined
Y4M_FFLAG_NONE = 0x0000, /// no flags set
/// DOCME /// field order/repeat field flags
Y4M_FFLAG_NOTSET = -1, Y4M_FFLAG_R_TFF = 0x0001, /// top field first
Y4M_FFLAG_R_TFF_R = 0x0002, /// top field first, and repeat that field
Y4M_FFLAG_R_BFF = 0x0004, /// bottom field first
Y4M_FFLAG_R_BFF_R = 0x0008, /// bottom field first, and repeat that field
Y4M_FFLAG_R_P = 0x0010, /// progressive
Y4M_FFLAG_R_P_R = 0x0020, /// progressive, and repeat frame once
Y4M_FFLAG_R_P_RR = 0x0040, /// progressive, and repeat frame twice
/// DOCME /// temporal sampling flags
Y4M_FFLAG_NONE = 0x0000, Y4M_FFLAG_T_P = 0x0080, /// progressive (fields sampled at the same time)
Y4M_FFLAG_T_I = 0x0100, /// interlaced (fields sampled at different times)
/// DOCME /// chroma subsampling flags
Y4M_FFLAG_R_TFF = 0x0001, // TFF Y4M_FFLAG_C_P = 0x0200, /// progressive (whole frame subsampled)
Y4M_FFLAG_C_I = 0x0400, /// interlaced (fields subsampled independently)
/// DOCME Y4M_FFLAG_C_UNKNOWN = 0x0800, /// unknown (only allowed for non-4:2:0 sampling)
Y4M_FFLAG_R_TFF_R = 0x0002, // TFF and repeat
/// DOCME
Y4M_FFLAG_R_BFF = 0x0004, // BFF
/// DOCME
Y4M_FFLAG_R_BFF_R = 0x0008, // BFF and repeat
/// DOCME
Y4M_FFLAG_R_P = 0x0010, // progressive
/// DOCME
Y4M_FFLAG_R_P_R = 0x0020, // progressive and repeat once
/// DOCME
Y4M_FFLAG_R_P_RR = 0x0040, // progressive and repeat twice
/// DOCME
Y4M_FFLAG_T_P = 0x0080, // progressive (fields sampled at the same time)
/// DOCME
Y4M_FFLAG_T_I = 0x0100, // interlaced (fields sampled at different times)
/// DOCME
Y4M_FFLAG_C_P = 0x0200, // progressive (whole frame subsampled)
/// DOCME
Y4M_FFLAG_C_I = 0x0400, // interlaced (fields subsampled independently)
/// DOCME
Y4M_FFLAG_C_UNKNOWN = 0x0800, // unknown (only allowed for non-4:2:0 sampling)
}; };
/// DOCME FILE *sf; /// source file
FILE *sf; // source file bool inited; /// initialization state
/// DOCME int w, h; /// frame width/height
bool inited; int num_frames; /// length of file in frames
int frame_sz; /// size of each frame in bytes
int luma_sz; /// size of the luma plane of each frame, in bytes
int chroma_sz; /// size of one of the two chroma planes of each frame, in bytes
int cur_fn; /// current frame number
/// DOCME Y4M_PixelFormat pixfmt; /// colorspace/pixel format
Y4M_InterlacingMode imode; /// interlacing mode (for the entire stream)
/// DOCME
int w, h; // width/height
/// DOCME
int num_frames; // length of file in frames
/// DOCME
int frame_sz; // size of each frame in bytes
/// DOCME
Y4M_PixelFormat pixfmt; // colorspace/pixel format
/// DOCME
Y4M_InterlacingMode imode; // interlacing mode
struct { struct {
int num; /// numerator
int den; /// denominator
} fps_rat; /// framerate
/// DOCME /// a list of byte positions detailing where in the file
int num; /// each frame header can be found
std::vector<int64_t> seek_table;
/// DOCME wxString errmsg; /// error message
int den;
/// DOCME
} fps_rat; // framerate
/// DOCME
std::vector<int64_t> seek_table; // the position in the file of each frame, in bytes
/// DOCME
int cur_fn; // current frame number
/// DOCME
wxString errmsg;
void LoadVideo(const wxString filename); void LoadVideo(const wxString filename);
void Close(); void Close();
@ -224,33 +156,11 @@ public:
int GetHeight(); int GetHeight();
double GetFPS(); double GetFPS();
/// @brief DOCME
/// @return
///
bool AreKeyFramesLoaded() { return false; } bool AreKeyFramesLoaded() { return false; }
/// @brief DOCME
/// @return
///
wxArrayInt GetKeyFrames() { return wxArrayInt(); } wxArrayInt GetKeyFrames() { return wxArrayInt(); }
/// @brief DOCME
/// @return
///
bool IsVFR() { return false; }; bool IsVFR() { return false; };
/// @brief DOCME
/// @return
///
FrameRate GetTrueFrameRate() { return FrameRate(); } FrameRate GetTrueFrameRate() { return FrameRate(); }
/// @brief DOCME
/// @return
///
wxString GetDecoderName() { return L"YUV4MPEG"; } wxString GetDecoderName() { return L"YUV4MPEG"; }
/// @brief DOCME
///
int GetDesiredCacheSize() { return 8; } int GetDesiredCacheSize() { return 8; }
}; };