read and utilize the aspect ratio metadata from video files
This commit is contained in:
@@ -143,6 +143,18 @@ bool FFmpegDecoder::Open()
|
|||||||
// We should never get here, but just in case...
|
// We should never get here, but just in case...
|
||||||
qFatal("Invalid output format");
|
qFatal("Invalid output format");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine sample aspect ratio
|
||||||
|
AVRational sar = av_guess_sample_aspect_ratio(fmt_ctx_, avstream_, nullptr);
|
||||||
|
|
||||||
|
// Use it to determine the display aspect ratio
|
||||||
|
// I'll be honest, I'm not entirely sure how this works or what it does. This code is the DAR code from ffprobe
|
||||||
|
// and seems to retrieve the DAR accurately.
|
||||||
|
av_reduce(&display_aspect_ratio_.num,
|
||||||
|
&display_aspect_ratio_.den,
|
||||||
|
avstream_->codecpar->width * sar.num,
|
||||||
|
avstream_->codecpar->height * sar.den,
|
||||||
|
1024*1024);
|
||||||
}
|
}
|
||||||
|
|
||||||
// All allocation succeeded so we set the state to open
|
// All allocation succeeded so we set the state to open
|
||||||
@@ -180,6 +192,7 @@ FramePtr FFmpegDecoder::RetrieveVideo(const rational &timecode)
|
|||||||
frame_container->set_height(avstream_->codecpar->height);
|
frame_container->set_height(avstream_->codecpar->height);
|
||||||
frame_container->set_format(native_pix_fmt_);
|
frame_container->set_format(native_pix_fmt_);
|
||||||
frame_container->set_timestamp(Timecode::timestamp_to_time(target_ts, avstream_->time_base));
|
frame_container->set_timestamp(Timecode::timestamp_to_time(target_ts, avstream_->time_base));
|
||||||
|
frame_container->set_aspect_ratio(display_aspect_ratio_);
|
||||||
frame_container->allocate();
|
frame_container->allocate();
|
||||||
|
|
||||||
memcpy(frame_container->data(), frame_loader.constData(), frame_loader.size());
|
memcpy(frame_container->data(), frame_loader.constData(), frame_loader.size());
|
||||||
|
|||||||
@@ -147,6 +147,8 @@ private:
|
|||||||
AVPixelFormat ideal_pix_fmt_;
|
AVPixelFormat ideal_pix_fmt_;
|
||||||
PixelFormat::Format native_pix_fmt_;
|
PixelFormat::Format native_pix_fmt_;
|
||||||
|
|
||||||
|
AVRational display_aspect_ratio_;
|
||||||
|
|
||||||
QVector<int64_t> frame_index_;
|
QVector<int64_t> frame_index_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
+20
-9
@@ -30,7 +30,8 @@ Frame::Frame() :
|
|||||||
height_(0),
|
height_(0),
|
||||||
format_(PixelFormat::PIX_FMT_INVALID),
|
format_(PixelFormat::PIX_FMT_INVALID),
|
||||||
sample_count_(0),
|
sample_count_(0),
|
||||||
timestamp_(0)
|
timestamp_(0),
|
||||||
|
aspect_ratio_(1)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +40,7 @@ FramePtr Frame::Create()
|
|||||||
return std::make_shared<Frame>();
|
return std::make_shared<Frame>();
|
||||||
}
|
}
|
||||||
|
|
||||||
const int &Frame::width()
|
const int &Frame::width() const
|
||||||
{
|
{
|
||||||
return width_;
|
return width_;
|
||||||
}
|
}
|
||||||
@@ -49,7 +50,7 @@ void Frame::set_width(const int &width)
|
|||||||
width_ = width;
|
width_ = width;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int &Frame::height()
|
const int &Frame::height() const
|
||||||
{
|
{
|
||||||
return height_;
|
return height_;
|
||||||
}
|
}
|
||||||
@@ -59,7 +60,17 @@ void Frame::set_height(const int &height)
|
|||||||
height_ = height;
|
height_ = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
const AudioRenderingParams &Frame::audio_params()
|
const rational &Frame::aspect_ratio() const
|
||||||
|
{
|
||||||
|
return aspect_ratio_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Frame::set_aspect_ratio(const rational &aspect_ratio)
|
||||||
|
{
|
||||||
|
aspect_ratio_ = aspect_ratio;
|
||||||
|
}
|
||||||
|
|
||||||
|
const AudioRenderingParams &Frame::audio_params() const
|
||||||
{
|
{
|
||||||
return audio_params_;
|
return audio_params_;
|
||||||
}
|
}
|
||||||
@@ -69,7 +80,7 @@ void Frame::set_audio_params(const AudioRenderingParams ¶ms)
|
|||||||
audio_params_ = params;
|
audio_params_ = params;
|
||||||
}
|
}
|
||||||
|
|
||||||
const rational &Frame::timestamp()
|
const rational &Frame::timestamp() const
|
||||||
{
|
{
|
||||||
return timestamp_;
|
return timestamp_;
|
||||||
}
|
}
|
||||||
@@ -89,7 +100,7 @@ void Frame::set_native_timestamp(const int64_t ×tamp)
|
|||||||
native_timestamp_ = timestamp;
|
native_timestamp_ = timestamp;
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
const PixelFormat::Format &Frame::format()
|
const PixelFormat::Format &Frame::format() const
|
||||||
{
|
{
|
||||||
return format_;
|
return format_;
|
||||||
}
|
}
|
||||||
@@ -99,12 +110,12 @@ void Frame::set_format(const PixelFormat::Format &format)
|
|||||||
format_ = format;
|
format_ = format;
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray Frame::ToByteArray()
|
QByteArray Frame::ToByteArray() const
|
||||||
{
|
{
|
||||||
return data_;
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int &Frame::sample_count()
|
const int &Frame::sample_count() const
|
||||||
{
|
{
|
||||||
return sample_count_;
|
return sample_count_;
|
||||||
}
|
}
|
||||||
@@ -119,7 +130,7 @@ char *Frame::data()
|
|||||||
return data_.data();
|
return data_.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *Frame::const_data()
|
const char *Frame::const_data() const
|
||||||
{
|
{
|
||||||
return data_.constData();
|
return data_.constData();
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-13
@@ -33,15 +33,10 @@ using FramePtr = std::shared_ptr<Frame>;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Video frame data or audio sample data from a Decoder
|
* @brief Video frame data or audio sample data from a Decoder
|
||||||
*
|
|
||||||
* Abstraction from AVFrame. Currently a simple AVFrame wrapper.
|
|
||||||
*
|
|
||||||
* This class does not support copying at this time.
|
|
||||||
*/
|
*/
|
||||||
class Frame
|
class Frame
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Normal constructor
|
|
||||||
Frame();
|
Frame();
|
||||||
|
|
||||||
static FramePtr Create();
|
static FramePtr Create();
|
||||||
@@ -49,19 +44,22 @@ public:
|
|||||||
/**
|
/**
|
||||||
* @brief Get frame's width in pixels
|
* @brief Get frame's width in pixels
|
||||||
*/
|
*/
|
||||||
const int& width();
|
const int& width() const;
|
||||||
void set_width(const int& width);
|
void set_width(const int& width);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get frame's height in pixels
|
* @brief Get frame's height in pixels
|
||||||
*/
|
*/
|
||||||
const int& height();
|
const int& height() const;
|
||||||
void set_height(const int& height);
|
void set_height(const int& height);
|
||||||
|
|
||||||
const AudioRenderingParams& audio_params();
|
const rational& aspect_ratio() const;
|
||||||
|
void set_aspect_ratio(const rational& aspect_ratio);
|
||||||
|
|
||||||
|
const AudioRenderingParams& audio_params() const;
|
||||||
void set_audio_params(const AudioRenderingParams& params);
|
void set_audio_params(const AudioRenderingParams& params);
|
||||||
|
|
||||||
const int &sample_count();
|
const int &sample_count() const;
|
||||||
void set_sample_count(const int &sample_count);
|
void set_sample_count(const int &sample_count);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,7 +67,7 @@ public:
|
|||||||
*
|
*
|
||||||
* This timestamp is always a rational that will equate to the time in seconds.
|
* This timestamp is always a rational that will equate to the time in seconds.
|
||||||
*/
|
*/
|
||||||
const rational& timestamp();
|
const rational& timestamp() const;
|
||||||
void set_timestamp(const rational& timestamp);
|
void set_timestamp(const rational& timestamp);
|
||||||
|
|
||||||
/*const int64_t& native_timestamp();
|
/*const int64_t& native_timestamp();
|
||||||
@@ -82,7 +80,7 @@ public:
|
|||||||
*
|
*
|
||||||
* Currently this will either be an olive::PixelFormat (video) or an olive::SampleFormat (audio).
|
* Currently this will either be an olive::PixelFormat (video) or an olive::SampleFormat (audio).
|
||||||
*/
|
*/
|
||||||
const PixelFormat::Format& format();
|
const PixelFormat::Format& format() const;
|
||||||
void set_format(const PixelFormat::Format& format);
|
void set_format(const PixelFormat::Format& format);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,7 +88,7 @@ public:
|
|||||||
*
|
*
|
||||||
* Will always do a deep copy. If you want to affect the data directly, use data() instead.
|
* Will always do a deep copy. If you want to affect the data directly, use data() instead.
|
||||||
*/
|
*/
|
||||||
QByteArray ToByteArray();
|
QByteArray ToByteArray() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the data buffer of this frame
|
* @brief Get the data buffer of this frame
|
||||||
@@ -100,7 +98,7 @@ public:
|
|||||||
/**
|
/**
|
||||||
* @brief Get the const data buffer of this frame
|
* @brief Get the const data buffer of this frame
|
||||||
*/
|
*/
|
||||||
const char* const_data();
|
const char* const_data() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Allocate memory buffer to store data based on parameters
|
* @brief Allocate memory buffer to store data based on parameters
|
||||||
@@ -138,6 +136,8 @@ private:
|
|||||||
|
|
||||||
rational timestamp_;
|
rational timestamp_;
|
||||||
|
|
||||||
|
rational aspect_ratio_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(FramePtr)
|
Q_DECLARE_METATYPE(FramePtr)
|
||||||
|
|||||||
@@ -101,6 +101,31 @@ void OpenGLWorker::FrameToValue(StreamPtr stream, FramePtr frame, NodeValueTable
|
|||||||
color_processor->Enable(ctx_, video_stream->premultiplied_alpha());
|
color_processor->Enable(ctx_, video_stream->premultiplied_alpha());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check frame aspect ratio
|
||||||
|
rational literal_ar(frame->width(), frame->height());
|
||||||
|
|
||||||
|
if (literal_ar != frame->aspect_ratio()) {
|
||||||
|
qDebug() << "NON EQUAL ASPECT RATIO, adjusting!";
|
||||||
|
|
||||||
|
int new_width = frame->width();
|
||||||
|
int new_height = frame->height();
|
||||||
|
|
||||||
|
// Scale the frame in a way that does not reduce the resolution
|
||||||
|
if (frame->aspect_ratio() > literal_ar) {
|
||||||
|
// Make wider
|
||||||
|
new_width = qRound(static_cast<double>(new_width) * frame->aspect_ratio().toDouble() / literal_ar.toDouble());
|
||||||
|
} else {
|
||||||
|
// Make taller
|
||||||
|
new_height = qRound(static_cast<double>(new_height) * literal_ar.toDouble() / frame->aspect_ratio().toDouble());
|
||||||
|
}
|
||||||
|
|
||||||
|
footage_params = VideoRenderingParams(new_width,
|
||||||
|
new_height,
|
||||||
|
footage_params.time_base(),
|
||||||
|
footage_params.format(),
|
||||||
|
footage_params.mode());
|
||||||
|
}
|
||||||
|
|
||||||
// Create destination texture
|
// Create destination texture
|
||||||
OpenGLTextureCache::ReferencePtr associated_tex_ref = texture_cache_->Get(ctx_, footage_params);
|
OpenGLTextureCache::ReferencePtr associated_tex_ref = texture_cache_->Get(ctx_, footage_params);
|
||||||
|
|
||||||
@@ -109,7 +134,7 @@ void OpenGLWorker::FrameToValue(StreamPtr stream, FramePtr frame, NodeValueTable
|
|||||||
footage_tex_ref->texture()->Bind();
|
footage_tex_ref->texture()->Bind();
|
||||||
|
|
||||||
// Set viewport for texture size
|
// Set viewport for texture size
|
||||||
functions_->glViewport(0, 0, footage_tex_ref->texture()->width(), footage_tex_ref->texture()->height());
|
functions_->glViewport(0, 0, associated_tex_ref->texture()->width(), associated_tex_ref->texture()->height());
|
||||||
|
|
||||||
// Blit old texture to new texture through OCIO shader
|
// Blit old texture to new texture through OCIO shader
|
||||||
color_processor->ProcessOpenGL();
|
color_processor->ProcessOpenGL();
|
||||||
|
|||||||
Reference in New Issue
Block a user