2009-07-19 06:13:46 +02:00
|
|
|
// Copyright (c) 2009, Karl Blomster
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file video_provider_yuv4mpeg.cpp
|
|
|
|
/// @brief Video provider reading YUV4MPEG files directly without depending on external libraries
|
|
|
|
/// @ingroup video_input
|
|
|
|
///
|
2009-07-19 06:13:46 +02:00
|
|
|
|
2014-03-24 15:05:01 +01:00
|
|
|
#include "include/aegisub/video_provider.h"
|
2012-01-18 21:08:42 +01:00
|
|
|
|
2010-12-31 22:03:11 +01:00
|
|
|
#include "utils.h"
|
2012-01-18 21:08:42 +01:00
|
|
|
#include "video_frame.h"
|
2009-07-19 06:13:46 +02:00
|
|
|
|
2014-03-21 18:26:06 +01:00
|
|
|
#include <libaegisub/file_mapping.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/log.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/util.h>
|
2013-08-27 16:37:19 +02:00
|
|
|
#include <libaegisub/ycbcr_conv.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2014-03-24 15:05:01 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
/// the maximum allowed header length, in bytes
|
|
|
|
#define YUV4MPEG_HEADER_MAXLEN 128
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// @class YUV4MPEGVideoProvider
|
|
|
|
/// @brief Implements reading of YUV4MPEG uncompressed video files
|
|
|
|
class YUV4MPEGVideoProvider final : public VideoProvider {
|
|
|
|
/// Pixel formats
|
|
|
|
enum Y4M_PixelFormat {
|
|
|
|
Y4M_PIXFMT_NONE = -1, /// not set/unknown
|
|
|
|
|
|
|
|
/// 4:2:0 sampling variants.
|
|
|
|
/// 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
|
|
|
|
|
|
|
|
Y4M_PIXFMT_411, /// 4:1:1, H cosited
|
|
|
|
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
|
|
|
|
|
|
|
|
Y4M_PIXFMT_MONO /// luma only (grayscale)
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Interlacing mode for an entire stream
|
|
|
|
enum Y4M_InterlacingMode {
|
|
|
|
Y4M_ILACE_NOTSET = -1, /// undefined
|
|
|
|
Y4M_ILACE_PROGRESSIVE, /// progressive (no interlacing)
|
|
|
|
|
|
|
|
Y4M_ILACE_TFF, /// interlaced, top field first
|
|
|
|
Y4M_ILACE_BFF, /// interlaced, bottom field first
|
|
|
|
|
|
|
|
Y4M_ILACE_MIXED, /// mixed interlaced/progressive, possibly with RFF flags
|
|
|
|
Y4M_ILACE_UNKNOWN /// unknown interlacing mode (not the same as undefined)
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Frame information flags
|
|
|
|
enum Y4M_FrameFlags {
|
|
|
|
Y4M_FFLAG_NOTSET = -1, /// undefined
|
|
|
|
Y4M_FFLAG_NONE = 0x0000, /// no flags set
|
|
|
|
|
|
|
|
/// field order/repeat field flags
|
|
|
|
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
|
|
|
|
|
|
|
|
/// temporal sampling flags
|
|
|
|
Y4M_FFLAG_T_P = 0x0080, /// progressive (fields sampled at the same time)
|
|
|
|
Y4M_FFLAG_T_I = 0x0100, /// interlaced (fields sampled at different times)
|
|
|
|
|
|
|
|
/// chroma subsampling flags
|
|
|
|
Y4M_FFLAG_C_P = 0x0200, /// progressive (whole frame subsampled)
|
|
|
|
Y4M_FFLAG_C_I = 0x0400, /// interlaced (fields subsampled independently)
|
|
|
|
Y4M_FFLAG_C_UNKNOWN = 0x0800 /// unknown (only allowed for non-4:2:0 sampling)
|
|
|
|
};
|
|
|
|
|
|
|
|
agi::read_file_mapping file;
|
|
|
|
bool inited = false; /// initialization state
|
|
|
|
|
|
|
|
int w = 0, h = 0; /// frame width/height
|
|
|
|
int num_frames = -1; /// 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
|
|
|
|
|
|
|
|
Y4M_PixelFormat pixfmt = Y4M_PIXFMT_NONE; /// colorspace/pixel format
|
|
|
|
Y4M_InterlacingMode imode = Y4M_ILACE_NOTSET; /// interlacing mode (for the entire stream)
|
|
|
|
struct {
|
|
|
|
int num = -1; /// numerator
|
|
|
|
int den = 1; /// denominator
|
|
|
|
} fps_rat; /// framerate
|
|
|
|
|
|
|
|
agi::vfr::Framerate fps;
|
|
|
|
|
2013-08-27 16:37:19 +02:00
|
|
|
agi::ycbcr_converter conv{agi::ycbcr_matrix::bt601, agi::ycbcr_range::tv};
|
|
|
|
|
2014-03-24 15:05:01 +01:00
|
|
|
/// a list of byte positions detailing where in the file
|
|
|
|
/// each frame header can be found
|
|
|
|
std::vector<uint64_t> seek_table;
|
|
|
|
|
|
|
|
void CheckFileFormat();
|
|
|
|
void ParseFileHeader(const std::vector<std::string>& tags);
|
|
|
|
Y4M_FrameFlags ParseFrameHeader(const std::vector<std::string>& tags);
|
|
|
|
std::vector<std::string> ReadHeader(uint64_t &startpos);
|
|
|
|
int IndexFile(uint64_t pos);
|
|
|
|
|
|
|
|
public:
|
|
|
|
YUV4MPEGVideoProvider(agi::fs::path const& filename);
|
|
|
|
|
|
|
|
std::shared_ptr<VideoFrame> GetFrame(int n) override;
|
2014-05-20 04:21:50 +02:00
|
|
|
void SetColorSpace(std::string const&) override { }
|
2014-03-24 15:05:01 +01:00
|
|
|
|
|
|
|
int GetFrameCount() const override { return num_frames; }
|
|
|
|
int GetWidth() const override { return w; }
|
|
|
|
int GetHeight() const override { return h; }
|
|
|
|
double GetDAR() const override { return 0; }
|
|
|
|
agi::vfr::Framerate GetFPS() const override { return fps; }
|
|
|
|
std::vector<int> GetKeyFrames() const override { return {}; }
|
|
|
|
std::string GetColorSpace() const override { return "TV.601"; }
|
|
|
|
std::string GetDecoderName() const override { return "YU4MPEG"; }
|
|
|
|
bool WantsCaching() const override { return true; }
|
|
|
|
};
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2009-08-07 21:59:37 +02:00
|
|
|
/// @brief Constructor
|
|
|
|
/// @param filename The filename to open
|
2014-03-24 15:05:01 +01:00
|
|
|
YUV4MPEGVideoProvider::YUV4MPEGVideoProvider(agi::fs::path const& filename)
|
|
|
|
: file(filename)
|
2014-03-21 18:26:06 +01:00
|
|
|
{
|
|
|
|
CheckFileFormat();
|
|
|
|
|
|
|
|
uint64_t pos = 0;
|
|
|
|
ParseFileHeader(ReadHeader(pos));
|
|
|
|
|
|
|
|
if (w <= 0 || h <= 0)
|
|
|
|
throw VideoOpenError("Invalid resolution");
|
|
|
|
if (fps_rat.num <= 0 || fps_rat.den <= 0) {
|
|
|
|
fps_rat.num = 25;
|
|
|
|
fps_rat.den = 1;
|
|
|
|
LOG_D("provider/video/yuv4mpeg") << "framerate info unavailable, assuming 25fps";
|
2010-08-02 08:32:01 +02:00
|
|
|
}
|
2014-03-21 18:26:06 +01:00
|
|
|
if (pixfmt == Y4M_PIXFMT_NONE)
|
|
|
|
pixfmt = Y4M_PIXFMT_420JPEG;
|
|
|
|
if (imode == Y4M_ILACE_NOTSET)
|
|
|
|
imode = Y4M_ILACE_UNKNOWN;
|
|
|
|
|
|
|
|
luma_sz = w * h;
|
|
|
|
switch (pixfmt) {
|
|
|
|
case Y4M_PIXFMT_420JPEG:
|
|
|
|
case Y4M_PIXFMT_420MPEG2:
|
|
|
|
case Y4M_PIXFMT_420PALDV:
|
|
|
|
chroma_sz = (w * h) >> 2; break;
|
|
|
|
case Y4M_PIXFMT_422:
|
|
|
|
chroma_sz = (w * h) >> 1; break;
|
|
|
|
/// @todo add support for more pixel formats
|
|
|
|
default:
|
|
|
|
throw VideoOpenError("Unsupported pixel format");
|
2010-08-02 08:32:01 +02:00
|
|
|
}
|
2014-03-21 18:26:06 +01:00
|
|
|
frame_sz = luma_sz + chroma_sz*2;
|
2009-07-19 06:13:46 +02:00
|
|
|
|
2014-03-21 18:26:06 +01:00
|
|
|
num_frames = IndexFile(pos);
|
|
|
|
if (num_frames <= 0 || seek_table.empty())
|
|
|
|
throw VideoOpenError("Unable to determine file length");
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
|
|
|
|
2009-08-07 21:59:37 +02:00
|
|
|
/// @brief Checks if the file is an YUV4MPEG file or not
|
|
|
|
/// Note that it reports the error by throwing an exception,
|
|
|
|
/// not by returning a false value.
|
2009-07-19 06:13:46 +02:00
|
|
|
void YUV4MPEGVideoProvider::CheckFileFormat() {
|
2014-03-24 15:05:01 +01:00
|
|
|
if (file.size() < 10)
|
2014-03-21 18:26:06 +01:00
|
|
|
throw VideoNotSupported("CheckFileFormat: File is not a YUV4MPEG file (too small)");
|
2014-03-24 15:05:01 +01:00
|
|
|
if (strncmp("YUV4MPEG2 ", file.read(0, 10), 10))
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoNotSupported("CheckFileFormat: File is not a YUV4MPEG file (bad magic)");
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
|
|
|
|
2009-08-07 21:59:37 +02:00
|
|
|
/// @brief Read a frame or file header at a given file position
|
|
|
|
/// @param startpos The byte offset at where to start reading
|
|
|
|
/// @return A list of parameters
|
2014-03-21 18:26:06 +01:00
|
|
|
std::vector<std::string> YUV4MPEGVideoProvider::ReadHeader(uint64_t &pos) {
|
2013-01-04 16:01:50 +01:00
|
|
|
std::vector<std::string> tags;
|
2014-03-24 15:05:01 +01:00
|
|
|
if (pos >= file.size())
|
2014-03-21 18:26:06 +01:00
|
|
|
return tags;
|
2009-07-19 06:13:46 +02:00
|
|
|
|
2014-03-24 15:05:01 +01:00
|
|
|
auto len = std::min<uint64_t>(YUV4MPEG_HEADER_MAXLEN, file.size() - pos);
|
|
|
|
auto buff = file.read(pos, len);
|
2009-07-19 06:13:46 +02:00
|
|
|
|
|
|
|
// read header until terminating newline (0x0A) is found
|
2014-03-21 19:08:26 +01:00
|
|
|
auto curtag = buff;
|
2014-03-21 18:26:06 +01:00
|
|
|
auto end = buff + len;
|
|
|
|
for (; buff < end && *buff != 0x0A; ++buff, ++pos) {
|
|
|
|
if (*buff == 0)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ReadHeader: Malformed header (unexpected NUL)");
|
2009-07-19 06:13:46 +02:00
|
|
|
|
2014-03-21 18:26:06 +01:00
|
|
|
if (*buff == 0x20) {
|
|
|
|
if (curtag != buff)
|
|
|
|
tags.emplace_back(curtag, buff);
|
|
|
|
curtag = buff + 1;
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-21 18:26:06 +01:00
|
|
|
|
|
|
|
if (buff == end)
|
|
|
|
throw VideoOpenError("ReadHeader: Malformed header (no terminating newline found)");
|
|
|
|
|
2009-07-19 06:13:46 +02:00
|
|
|
// if only one tag with no trailing space was found (possible in the
|
|
|
|
// FRAME header case), make sure we get it
|
2014-03-21 18:26:06 +01:00
|
|
|
if (curtag != buff)
|
|
|
|
tags.emplace_back(curtag, buff);
|
|
|
|
|
|
|
|
pos += 1; // Move past newline
|
2009-07-19 06:13:46 +02:00
|
|
|
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
2009-08-07 21:59:37 +02:00
|
|
|
/// @brief Parses a list of parameters and sets reader state accordingly
|
|
|
|
/// @param tags The list of parameters to parse
|
2013-01-04 16:01:50 +01:00
|
|
|
void YUV4MPEGVideoProvider::ParseFileHeader(const std::vector<std::string>& tags) {
|
2009-07-19 06:13:46 +02:00
|
|
|
if (tags.size() <= 1)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFileHeader: contentless header");
|
2013-01-04 16:01:50 +01:00
|
|
|
if (tags.front() != "YUV4MPEG2")
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFileHeader: malformed header (bad magic)");
|
2009-07-19 06:13:46 +02:00
|
|
|
|
|
|
|
// temporary stuff
|
|
|
|
int t_w = -1;
|
|
|
|
int t_h = -1;
|
|
|
|
int t_fps_num = -1;
|
|
|
|
int t_fps_den = -1;
|
|
|
|
Y4M_InterlacingMode t_imode = Y4M_ILACE_NOTSET;
|
2009-07-20 00:51:19 +02:00
|
|
|
Y4M_PixelFormat t_pixfmt = Y4M_PIXFMT_NONE;
|
2009-07-19 06:13:46 +02:00
|
|
|
|
|
|
|
for (unsigned i = 1; i < tags.size(); i++) {
|
2013-01-04 16:01:50 +01:00
|
|
|
char type = tags[i][0];
|
|
|
|
std::string tag = tags[i].substr(1);
|
2009-07-19 06:13:46 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
if (type == 'W') {
|
|
|
|
if (!agi::util::try_parse(tag, &t_w))
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFileHeader: invalid width");
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
else if (type == 'H') {
|
|
|
|
if (!agi::util::try_parse(tag, &t_h))
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFileHeader: invalid height");
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
else if (type == 'F') {
|
|
|
|
size_t pos = tag.find(':');
|
|
|
|
if (pos == tag.npos)
|
|
|
|
throw VideoOpenError("ParseFileHeader: invalid framerate");
|
|
|
|
|
|
|
|
if (!agi::util::try_parse(tag.substr(0, pos), &t_fps_num) ||
|
|
|
|
!agi::util::try_parse(tag.substr(pos + 1), &t_fps_den))
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFileHeader: invalid framerate");
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
else if (type == 'C') {
|
2009-07-19 06:13:46 +02:00
|
|
|
// technically this should probably be case sensitive,
|
|
|
|
// but being liberal in what you accept doesn't hurt
|
2013-01-04 16:01:50 +01:00
|
|
|
boost::to_lower(tag);
|
2010-12-31 22:03:11 +01:00
|
|
|
if (tag == "420") t_pixfmt = Y4M_PIXFMT_420JPEG; // is this really correct?
|
|
|
|
else if (tag == "420jpeg") t_pixfmt = Y4M_PIXFMT_420JPEG;
|
|
|
|
else if (tag == "420mpeg2") t_pixfmt = Y4M_PIXFMT_420MPEG2;
|
|
|
|
else if (tag == "420paldv") t_pixfmt = Y4M_PIXFMT_420PALDV;
|
|
|
|
else if (tag == "411") t_pixfmt = Y4M_PIXFMT_411;
|
|
|
|
else if (tag == "422") t_pixfmt = Y4M_PIXFMT_422;
|
|
|
|
else if (tag == "444") t_pixfmt = Y4M_PIXFMT_444;
|
|
|
|
else if (tag == "444alpha") t_pixfmt = Y4M_PIXFMT_444ALPHA;
|
|
|
|
else if (tag == "mono") t_pixfmt = Y4M_PIXFMT_MONO;
|
2009-07-19 06:13:46 +02:00
|
|
|
else
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFileHeader: invalid or unknown colorspace");
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
else if (type == 'I') {
|
|
|
|
boost::to_lower(tag);
|
2010-12-31 22:03:11 +01:00
|
|
|
if (tag == "p") t_imode = Y4M_ILACE_PROGRESSIVE;
|
|
|
|
else if (tag == "t") t_imode = Y4M_ILACE_TFF;
|
|
|
|
else if (tag == "b") t_imode = Y4M_ILACE_BFF;
|
|
|
|
else if (tag == "m") t_imode = Y4M_ILACE_MIXED;
|
|
|
|
else if (tag == "?") t_imode = Y4M_ILACE_UNKNOWN;
|
2009-07-19 06:13:46 +02:00
|
|
|
else
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFileHeader: invalid or unknown interlacing mode");
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
|
|
|
else
|
2013-01-04 16:01:50 +01:00
|
|
|
LOG_D("provider/video/yuv4mpeg") << "Unparsed tag: " << tags[i];
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The point of all this is to allow multiple YUV4MPEG2 headers in a single file
|
|
|
|
// (can happen if you concat several files) as long as they have identical
|
|
|
|
// header flags. The spec doesn't explicitly say you have to allow this,
|
|
|
|
// but the "reference implementation" (mjpegtools) does, so I'm doing it too.
|
|
|
|
if (inited) {
|
|
|
|
if (t_w > 0 && t_w != w)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFileHeader: illegal width change");
|
2009-07-19 06:13:46 +02:00
|
|
|
if (t_h > 0 && t_h != h)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFileHeader: illegal height change");
|
2009-07-19 06:13:46 +02:00
|
|
|
if ((t_fps_num > 0 && t_fps_den > 0) && (t_fps_num != fps_rat.num || t_fps_den != fps_rat.den))
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFileHeader: illegal framerate change");
|
2009-07-19 06:13:46 +02:00
|
|
|
if (t_pixfmt != Y4M_PIXFMT_NONE && t_pixfmt != pixfmt)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFileHeader: illegal colorspace change");
|
2009-07-19 06:13:46 +02:00
|
|
|
if (t_imode != Y4M_ILACE_NOTSET && t_imode != imode)
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFileHeader: illegal interlacing mode change");
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
w = t_w;
|
|
|
|
h = t_h;
|
|
|
|
fps_rat.num = t_fps_num;
|
|
|
|
fps_rat.den = t_fps_den;
|
2009-08-07 21:59:37 +02:00
|
|
|
pixfmt = t_pixfmt != Y4M_PIXFMT_NONE ? t_pixfmt : Y4M_PIXFMT_420JPEG;
|
|
|
|
imode = t_imode != Y4M_ILACE_NOTSET ? t_imode : Y4M_ILACE_UNKNOWN;
|
2010-07-08 06:29:04 +02:00
|
|
|
fps = double(fps_rat.num) / fps_rat.den;
|
2009-07-19 06:13:46 +02:00
|
|
|
inited = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-07 21:59:37 +02:00
|
|
|
/// @brief Parses a frame header
|
|
|
|
/// @param tags The list of parameters to parse
|
|
|
|
/// @return The flags set, as a binary mask
|
|
|
|
/// This function is currently unimplemented (it will always return Y4M_FFLAG_NONE).
|
2013-01-04 16:01:50 +01:00
|
|
|
YUV4MPEGVideoProvider::Y4M_FrameFlags YUV4MPEGVideoProvider::ParseFrameHeader(const std::vector<std::string>& tags) {
|
|
|
|
if (tags.front() != "FRAME")
|
2010-08-02 08:32:01 +02:00
|
|
|
throw VideoOpenError("ParseFrameHeader: malformed frame header (bad magic)");
|
2009-07-19 06:13:46 +02:00
|
|
|
|
2009-08-07 21:59:37 +02:00
|
|
|
/// @todo implement parsing of frame flags
|
2009-07-19 06:13:46 +02:00
|
|
|
|
|
|
|
return Y4M_FFLAG_NONE;
|
|
|
|
}
|
|
|
|
|
2009-08-07 21:59:37 +02:00
|
|
|
/// @brief Indexes the file
|
|
|
|
/// @return The number of frames found in the file
|
|
|
|
/// 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.
|
2014-03-21 18:26:06 +01:00
|
|
|
int YUV4MPEGVideoProvider::IndexFile(uint64_t pos) {
|
2009-07-19 06:13:46 +02:00
|
|
|
int framecount = 0;
|
|
|
|
|
|
|
|
// the ParseFileHeader() call in LoadVideo() will already have read
|
2012-03-25 06:05:06 +02:00
|
|
|
// the file header for us and set the seek position correctly
|
2009-07-19 06:13:46 +02:00
|
|
|
while (true) {
|
|
|
|
// continue reading headers until no more are found
|
2014-03-21 18:26:06 +01:00
|
|
|
std::vector<std::string> tags = ReadHeader(pos);
|
2009-07-19 06:13:46 +02:00
|
|
|
if (tags.empty())
|
|
|
|
break; // no more headers
|
|
|
|
|
|
|
|
Y4M_FrameFlags flags = Y4M_FFLAG_NOTSET;
|
2013-01-04 16:01:50 +01:00
|
|
|
if (tags.front() == "YUV4MPEG2") {
|
2009-07-19 06:13:46 +02:00
|
|
|
ParseFileHeader(tags);
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
else if (tags.front() == "FRAME")
|
2009-07-19 06:13:46 +02:00
|
|
|
flags = ParseFrameHeader(tags);
|
|
|
|
|
|
|
|
if (flags == Y4M_FFLAG_NONE) {
|
|
|
|
framecount++;
|
2014-03-21 18:26:06 +01:00
|
|
|
seek_table.push_back(pos);
|
|
|
|
pos += frame_sz;
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
|
|
|
else {
|
2009-08-07 21:59:37 +02:00
|
|
|
/// @todo implement rff flags etc
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return framecount;
|
|
|
|
}
|
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
std::shared_ptr<VideoFrame> YUV4MPEGVideoProvider::GetFrame(int n) {
|
|
|
|
n = mid(0, n, num_frames - 1);
|
2010-12-31 22:03:11 +01:00
|
|
|
|
|
|
|
int uv_width = w / 2;
|
2009-07-19 06:13:46 +02:00
|
|
|
switch (pixfmt) {
|
|
|
|
case Y4M_PIXFMT_420JPEG:
|
|
|
|
case Y4M_PIXFMT_420MPEG2:
|
|
|
|
case Y4M_PIXFMT_420PALDV:
|
2010-12-31 22:03:11 +01:00
|
|
|
break;
|
2009-08-07 21:59:37 +02:00
|
|
|
/// @todo add support for more pixel formats
|
2009-07-19 06:13:46 +02:00
|
|
|
default:
|
2010-12-31 22:03:11 +01:00
|
|
|
throw "YUV4MPEG video provider: GetFrame: Unsupported source colorspace";
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
2010-12-31 22:03:11 +01:00
|
|
|
|
2014-03-24 15:05:01 +01:00
|
|
|
auto src_y = reinterpret_cast<const unsigned char *>(file.read(seek_table[n], luma_sz + chroma_sz * 2));
|
2014-03-21 18:26:06 +01:00
|
|
|
auto src_u = src_y + luma_sz;
|
|
|
|
auto src_v = src_u + chroma_sz;
|
2013-07-01 05:15:43 +02:00
|
|
|
std::vector<unsigned char> data;
|
|
|
|
data.resize(w * h * 4);
|
|
|
|
unsigned char *dst = &data[0];
|
2010-12-31 22:03:11 +01:00
|
|
|
|
|
|
|
for (int py = 0; py < h; ++py) {
|
|
|
|
for (int px = 0; px < w / 2; ++px) {
|
2013-08-27 16:37:19 +02:00
|
|
|
const uint8_t u = *src_u++;
|
|
|
|
const uint8_t v = *src_v++;
|
2010-12-31 22:03:11 +01:00
|
|
|
for (unsigned int i = 0; i < 2; ++i) {
|
2013-08-27 16:37:19 +02:00
|
|
|
const uint8_t y = *src_y++;
|
|
|
|
auto rgb = conv.ycbcr_to_rgb({{y, u, v}});
|
|
|
|
*dst++ = rgb[2];
|
|
|
|
*dst++ = rgb[1];
|
|
|
|
*dst++ = rgb[0];
|
|
|
|
*dst++ = 0;
|
2010-12-31 22:03:11 +01:00
|
|
|
}
|
|
|
|
}
|
2009-07-19 06:13:46 +02:00
|
|
|
|
2010-12-31 22:03:11 +01:00
|
|
|
// Roll back u/v on even lines
|
|
|
|
if (!(py & 1)) {
|
|
|
|
src_u -= uv_width;
|
|
|
|
src_v -= uv_width;
|
|
|
|
}
|
|
|
|
}
|
2009-07-19 06:13:46 +02:00
|
|
|
|
2013-08-27 16:37:19 +02:00
|
|
|
return std::make_shared<VideoFrame>(std::move(data), w, h, w * 4, false);
|
2009-07-19 06:13:46 +02:00
|
|
|
}
|
2014-03-24 15:05:01 +01:00
|
|
|
}
|
|
|
|
|
2014-03-25 17:51:38 +01:00
|
|
|
namespace agi { class BackgroundRunner; }
|
|
|
|
std::unique_ptr<VideoProvider> CreateYUV4MPEGVideoProvider(agi::fs::path const& path, std::string const&, agi::BackgroundRunner *) {
|
2014-04-23 22:53:24 +02:00
|
|
|
return agi::make_unique<YUV4MPEGVideoProvider>(path);
|
2014-03-24 15:05:01 +01:00
|
|
|
}
|