Loading...
Searching...
No Matches
Deinterlace.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "Deinterlace.h"
#include "Exceptions.h"
#include <omp.h>
using namespace openshot;
Deinterlace::Deinterlace() : isOdd(true)
{
// Init effect properties
init_effect_details();
}
// Default constructor
Deinterlace::Deinterlace(bool UseOddLines) : isOdd(UseOddLines)
{
// Init effect properties
init_effect_details();
}
// Init effect settings
void Deinterlace::init_effect_details()
{
InitEffectInfo();
info.class_name = "Deinterlace";
info.name = "Deinterlace";
info.description = "Remove interlacing from a video (i.e. even or odd horizontal lines)";
info.has_audio = false;
info.has_video = true;
}
// This method is required for all derived classes of EffectBase, and returns a
// modified openshot::Frame object
std::shared_ptr<openshot::Frame> Deinterlace::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
{
// Get original size of frame's image
int original_width = frame->GetImage()->width();
int original_height = frame->GetImage()->height();
// Access the current QImage and its raw pixel data
auto image = frame->GetImage();
const unsigned char* pixels = image->bits();
int line_bytes = image->bytesPerLine();
// Decide whether to copy even lines (start = 0) or odd lines (start = 1)
int start = isOdd ? 1 : 0;
// Compute how many rows we will end up copying
// If start = 0, rows_to_copy = ceil(original_height / 2.0)
// If start = 1, rows_to_copy = floor(original_height / 2.0)
int rows_to_copy = (original_height - start + 1) / 2;
// Create a new image with exactly 'rows_to_copy' scanlines
QImage deinterlaced_image(
original_width,
rows_to_copy,
QImage::Format_RGBA8888_Premultiplied
);
unsigned char* deinterlaced_pixels = deinterlaced_image.bits();
// Copy every other row from the source into the new image
// Parallelize over 'i' so each thread writes to a distinct slice of memory
#pragma omp parallel for
for (int i = 0; i < rows_to_copy; i++) {
int row = start + 2 * i;
const unsigned char* src = pixels + (row * line_bytes);
unsigned char* dst = deinterlaced_pixels + (i * line_bytes);
memcpy(dst, src, line_bytes);
}
// Resize deinterlaced image back to original size, and update frame's image
image = std::make_shared<QImage>(deinterlaced_image.scaled(
original_width, original_height,
Qt::IgnoreAspectRatio, Qt::FastTransformation));
// Update image on frame
frame->AddImage(image);
// return the modified frame
return frame;
}
// Generate JSON string of this object
std::string Deinterlace::Json() const {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value Deinterlace::JsonValue() const {
// Create root json object
Json::Value root = EffectBase::JsonValue(); // get parent properties
root["type"] = info.class_name;
root["isOdd"] = isOdd;
// return JsonValue
return root;
}
// Load JSON string into this object
void Deinterlace::SetJson(const std::string value) {
// Parse JSON string into JSON objects
try
{
const Json::Value root = openshot::stringToJson(value);
// Set all values that match
SetJsonValue(root);
}
catch (const std::exception& e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
}
}
// Load Json::Value into this object
void Deinterlace::SetJsonValue(const Json::Value root) {
// Set parent data
EffectBase::SetJsonValue(root);
// Set data from Json (if key is found)
if (!root["isOdd"].isNull())
isOdd = root["isOdd"].asBool();
}
// Get all properties for a specific frame
std::string Deinterlace::PropertiesJSON(int64_t requested_frame) const {
// Generate JSON properties list
Json::Value root = BasePropertiesJSON(requested_frame);
// Add Is Odd Frame choices (dropdown style)
root["isOdd"] = add_property_json("Is Odd Frame", isOdd, "bool", "", NULL, 0, 1, false, requested_frame);
root["isOdd"]["choices"].append(add_property_choice_json("Yes", true, isOdd));
root["isOdd"]["choices"].append(add_property_choice_json("No", false, isOdd));
// Return formatted string
return root.toStyledString();
}
Deinterlace.h
Header file for De-interlace class.
Exceptions.h
Header file for all Exception classes.
openshot::ClipBase::start
float start
The position in seconds to start playing (used to trim the beginning of a clip).
Definition ClipBase.h:37
openshot::ClipBase::add_property_choice_json
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties).
Definition ClipBase.cpp:132
openshot::ClipBase::add_property_json
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const
Generate JSON for a property.
Definition ClipBase.cpp:96
openshot::Deinterlace::Json
std::string Json() const override
Generate JSON string of this object.
Definition Deinterlace.cpp:99
openshot::Deinterlace::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition Deinterlace.cpp:135
openshot::Deinterlace::Deinterlace
Deinterlace()
Default constructor, useful when using Json to load the effect properties.
Definition Deinterlace.cpp:20
openshot::Deinterlace::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number) override
This method is required for all derived classes of ClipBase, and returns a new openshot::Frame object...
Definition Deinterlace.h:56
openshot::Deinterlace::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition Deinterlace.cpp:118
openshot::Deinterlace::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition Deinterlace.cpp:146
openshot::Deinterlace::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition Deinterlace.cpp:106
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition EffectBase.cpp:79
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition EffectBase.cpp:24
openshot::EffectBase::BasePropertiesJSON
Json::Value BasePropertiesJSON(int64_t requested_frame) const
Generate JSON object of base properties (recommended to be used by all effects).
Definition EffectBase.cpp:179
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition EffectBase.cpp:115
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition EffectBase.h:69
openshot::InvalidJSON
Exception for invalid JSON.
Definition Exceptions.h:218
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition EffectBase.h:40
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition EffectBase.h:41
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition EffectBase.h:36
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition EffectBase.h:37
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition EffectBase.h:38