From 3c2532b4deb51fee22338f70c094a5f058bf9c7f Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Thu, 20 Aug 2020 16:50:12 -0400 Subject: [PATCH 1/7] Use std::make_shared to allocate shared ptrs std::make_shared does in a single allocation what the constructors for std::shared_ptr usually take at least 2 allocations to do. May give us an infinitesimal performance/memory improvement. https://www.modernescpp.com/index.php/memory-and-performance-overhead-of-smart-pointer --- include/DummyReader.h | 2 +- include/Frame.h | 2 +- src/CacheDisk.cpp | 6 +-- src/ChunkWriter.cpp | 4 +- src/Clip.cpp | 7 +++- src/DecklinkInput.cpp | 8 ++-- src/DecklinkWriter.cpp | 2 +- src/FFmpegReader.cpp | 6 +-- src/Frame.cpp | 84 +++++++++++++++++++++++-------------- src/FrameMapper.cpp | 8 ++-- src/ImageReader.cpp | 6 ++- src/QtHtmlReader.cpp | 9 ++-- src/QtImageReader.cpp | 23 ++++++---- src/QtTextReader.cpp | 8 ++-- src/TextReader.cpp | 11 +++-- src/effects/Bars.cpp | 3 +- src/effects/Crop.cpp | 3 +- src/effects/Deinterlace.cpp | 4 +- src/effects/Mask.cpp | 11 ++--- 19 files changed, 129 insertions(+), 78 deletions(-) diff --git a/include/DummyReader.h b/include/DummyReader.h index 9a75751d..af06656d 100644 --- a/include/DummyReader.h +++ b/include/DummyReader.h @@ -69,7 +69,7 @@ namespace openshot * // Create blank frame (with specific frame #, samples, and channels) * // Sample count should be 44100 / 30 fps = 1470 samples per frame * int sample_count = 1470; - * std::shared_ptr f(new openshot::Frame(frame_number, sample_count, 2)); + * auto f = std::make_shared(frame_number, sample_count, 2); * * // Create test samples with incrementing value * float *audio_buffer = new float[sample_count]; diff --git a/include/Frame.h b/include/Frame.h index f4ff54d4..7e7866c0 100644 --- a/include/Frame.h +++ b/include/Frame.h @@ -99,7 +99,7 @@ namespace openshot * ); * * // Some methods require a shared pointer to an openshot::Frame object. - * std::shared_ptr f(new Frame(1, 720, 480, "#000000", 44100, 2)); + * auto f = std::make_shared(1, 720, 480, "#000000", 44100, 2); * * @endcode */ diff --git a/src/CacheDisk.cpp b/src/CacheDisk.cpp index bb2e12c4..409d8c4e 100644 --- a/src/CacheDisk.cpp +++ b/src/CacheDisk.cpp @@ -227,14 +227,14 @@ std::shared_ptr CacheDisk::GetFrame(int64_t frame_number) if (path.exists(frame_path)) { // Load image file - std::shared_ptr image = std::shared_ptr(new QImage()); + auto image = std::make_shared(); image->load(frame_path); // Set pixel formatimage-> - image = std::shared_ptr(new QImage(image->convertToFormat(QImage::Format_RGBA8888))); + image = std::make_shared(image->convertToFormat(QImage::Format_RGBA8888)); // Create frame object - std::shared_ptr frame(new Frame()); + auto frame = std::make_shared(); frame->number = frame_number; frame->AddImage(image); diff --git a/src/ChunkWriter.cpp b/src/ChunkWriter.cpp index f9d653b0..c7752cd5 100644 --- a/src/ChunkWriter.cpp +++ b/src/ChunkWriter.cpp @@ -134,7 +134,9 @@ void ChunkWriter::WriteFrame(std::shared_ptr frame) writer_thumb->WriteFrame(last_frame); } else { // Write the 1st frame (of the 1st chunk)... since no previous chunk is available - std::shared_ptr blank_frame(new Frame(1, info.width, info.height, "#000000", info.sample_rate, info.channels)); + auto blank_frame = std::make_shared( + 1, info.width, info.height, "#000000", + info.sample_rate, info.channels); blank_frame->AddColor(info.width, info.height, "#000000"); writer_final->WriteFrame(blank_frame); writer_preview->WriteFrame(blank_frame); diff --git a/src/Clip.cpp b/src/Clip.cpp index d9f69440..f20f32fb 100644 --- a/src/Clip.cpp +++ b/src/Clip.cpp @@ -339,7 +339,10 @@ std::shared_ptr Clip::GetFrame(int64_t requested_frame) original_frame = GetOrCreateFrame(new_frame_number); // Create a new frame - std::shared_ptr frame(new Frame(new_frame_number, 1, 1, "#000000", original_frame->GetAudioSamplesCount(), original_frame->GetAudioChannelsCount())); + auto frame = std::make_shared( + new_frame_number, 1, 1, "#000000", + original_frame->GetAudioSamplesCount(), + original_frame->GetAudioChannelsCount()); { frame->SampleRate(original_frame->SampleRate()); frame->ChannelsLayout(original_frame->ChannelsLayout()); @@ -347,7 +350,7 @@ std::shared_ptr Clip::GetFrame(int64_t requested_frame) // Copy the image from the odd field if (enabled_video) - frame->AddImage(std::shared_ptr(new QImage(*original_frame->GetImage()))); + frame->AddImage(std::make_shared(*original_frame->GetImage())); // Loop through each channel, add audio if (enabled_audio && reader->info.has_audio) diff --git a/src/DecklinkInput.cpp b/src/DecklinkInput.cpp index da5a8d00..b03ad8e4 100644 --- a/src/DecklinkInput.cpp +++ b/src/DecklinkInput.cpp @@ -139,7 +139,7 @@ HRESULT DeckLinkInputDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* { // Handle Video Frame if(videoFrame) - { + { if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) { @@ -245,7 +245,8 @@ omp_set_nested(true); m_rgbFrame->GetBytes(&frameBytes); // *********** CREATE OPENSHOT FRAME ********** - std::shared_ptr f(new openshot::Frame(copy_frameCount, width, height, "#000000", 2048, 2)); + auto f = std::make_shared( + copy_frameCount, width, height, "#000000", 2048, 2); // Add Image data to openshot frame // TODO: Fix Decklink support with QImage Upgrade @@ -289,6 +290,3 @@ HRESULT DeckLinkInputDelegate::VideoInputFormatChanged(BMDVideoInputFormatChange { return S_OK; } - - - diff --git a/src/DecklinkWriter.cpp b/src/DecklinkWriter.cpp index 4ebbd1f0..3eafda1c 100644 --- a/src/DecklinkWriter.cpp +++ b/src/DecklinkWriter.cpp @@ -142,7 +142,7 @@ void DecklinkWriter::Open() // throw DecklinkError("Failed to enable audio output. Is another application using the card?"); // Begin video preroll by scheduling a second of frames in hardware - //std::shared_ptr f(new Frame(1, displayMode->GetWidth(), displayMode->GetHeight(), "Blue")); + //auto f = std::make_shared(1, displayMode->GetWidth(), displayMode->GetHeight(), "Blue"); //f->AddColor(displayMode->GetWidth(), displayMode->GetHeight(), "Blue"); // Preroll 1 second of video diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index c8ce141f..1952e8d6 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -2134,7 +2134,7 @@ bool FFmpegReader::CheckMissingFrame(int64_t requested_frame) { // Add this frame to the processed map (since it's already done) std::shared_ptr parent_image = parent_frame->GetImage(); if (parent_image) { - missing_frame->AddImage(std::shared_ptr(new QImage(*parent_image))); + missing_frame->AddImage(std::make_shared(*parent_image)); processed_video_frames[missing_frame->number] = missing_frame->number; } } @@ -2224,7 +2224,7 @@ void FFmpegReader::CheckWorkingFrames(bool end_of_stream, int64_t requested_fram if (info.has_video && !is_video_ready && last_video_frame) { // Copy image from last frame - f->AddImage(std::shared_ptr(new QImage(*last_video_frame->GetImage()))); + f->AddImage(std::make_shared(*last_video_frame->GetImage())); is_video_ready = true; } @@ -2246,7 +2246,7 @@ void FFmpegReader::CheckWorkingFrames(bool end_of_stream, int64_t requested_fram // Add missing image (if needed - sometimes end_of_stream causes frames with only audio) if (info.has_video && !is_video_ready && last_video_frame) // Copy image from last frame - f->AddImage(std::shared_ptr(new QImage(*last_video_frame->GetImage()))); + f->AddImage(std::make_shared(*last_video_frame->GetImage())); // Reset counter since last 'final' frame num_checks_since_final = 0; diff --git a/src/Frame.cpp b/src/Frame.cpp index cf47556a..16f420bc 100644 --- a/src/Frame.cpp +++ b/src/Frame.cpp @@ -39,7 +39,7 @@ Frame::Frame() : number(1), pixel_ratio(1,1), channels(2), width(1), height(1), max_audio_sample(0) { // Init the image magic and audio buffer - audio = std::shared_ptr(new juce::AudioSampleBuffer(channels, 0)); + audio = std::make_shared(channels, 0); // initialize the audio samples to zero (silence) audio->clear(); @@ -52,7 +52,7 @@ Frame::Frame(int64_t number, int width, int height, std::string color) max_audio_sample(0) { // Init the image magic and audio buffer - audio = std::shared_ptr(new juce::AudioSampleBuffer(channels, 0)); + audio = std::make_shared(channels, 0); // initialize the audio samples to zero (silence) audio->clear(); @@ -65,7 +65,7 @@ Frame::Frame(int64_t number, int samples, int channels) : max_audio_sample(0) { // Init the image magic and audio buffer - audio = std::shared_ptr(new juce::AudioSampleBuffer(channels, samples)); + audio = std::make_shared(channels, samples); // initialize the audio samples to zero (silence) audio->clear(); @@ -78,7 +78,7 @@ Frame::Frame(int64_t number, int width, int height, std::string color, int sampl max_audio_sample(0) { // Init the image magic and audio buffer - audio = std::shared_ptr(new juce::AudioSampleBuffer(channels, samples)); + audio = std::make_shared(channels, samples); // initialize the audio samples to zero (silence) audio->clear(); @@ -117,11 +117,11 @@ void Frame::DeepCopy(const Frame& other) max_audio_sample = other.max_audio_sample; if (other.image) - image = std::shared_ptr(new QImage(*(other.image))); + image = std::make_shared(*(other.image)); if (other.audio) - audio = std::shared_ptr(new juce::AudioSampleBuffer(*(other.audio))); + audio = std::make_shared(*(other.audio)); if (other.wave_image) - wave_image = std::shared_ptr(new QImage(*(other.wave_image))); + wave_image = std::make_shared(*(other.wave_image)); } // Destructor @@ -138,7 +138,7 @@ void Frame::Display() // Only create the QApplication once static int argc = 1; static char* argv[1] = {NULL}; - previewApp = std::shared_ptr(new QApplication(argc, argv)); + previewApp = std::make_shared(argc, argv); } // Get preview image @@ -152,7 +152,8 @@ void Frame::Display() int new_height = previewImage->size().height() * pixel_ratio.Reciprocal().ToDouble(); // Resize to fix DAR - previewImage = std::shared_ptr(new QImage(previewImage->scaled(new_width, new_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); + previewImage = std::make_shared(previewImage->scaled( + new_width, new_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); } // Create window @@ -228,7 +229,8 @@ std::shared_ptr Frame::GetWaveform(int width, int height, int Red, int G } // Create blank image - wave_image = std::shared_ptr(new QImage(total_width, total_height, QImage::Format_RGBA8888)); + wave_image = std::make_shared( + total_width, total_height, QImage::Format_RGBA8888); wave_image->fill(QColor(0,0,0,0)); // Load QPainter with wave_image device @@ -253,13 +255,13 @@ std::shared_ptr Frame::GetWaveform(int width, int height, int Red, int G // Resize Image (if requested) if (width != total_width || height != total_height) { QImage scaled_wave_image = wave_image->scaled(width, height, Qt::IgnoreAspectRatio, Qt::FastTransformation); - wave_image = std::shared_ptr(new QImage(scaled_wave_image)); + wave_image = std::make_shared(scaled_wave_image); } } else { // No audio samples present - wave_image = std::shared_ptr(new QImage(width, height, QImage::Format_RGBA8888)); + wave_image = std::make_shared(width, height, QImage::Format_RGBA8888); wave_image->fill(QColor(QString::fromStdString("#000000"))); } @@ -294,7 +296,7 @@ void Frame::DisplayWaveform() // Only create the QApplication once static int argc = 1; static char* argv[1] = {NULL}; - previewApp = std::shared_ptr(new QApplication(argc, argv)); + previewApp = std::make_shared(argc, argv); } // Create window @@ -599,11 +601,15 @@ void Frame::Save(std::string path, float scale, std::string format, int quality) int new_height = previewImage->size().height() * pixel_ratio.Reciprocal().ToDouble(); // Resize to fix DAR - previewImage = std::shared_ptr(new QImage(previewImage->scaled(new_width, new_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); + previewImage = std::make_shared(previewImage->scaled( + new_width, new_height, + Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); } // Resize image - previewImage = std::shared_ptr(new QImage(previewImage->scaled(new_width * scale, new_height * scale, Qt::KeepAspectRatio, Qt::SmoothTransformation))); + previewImage = std::make_shared(previewImage->scaled( + new_width * scale, new_height * scale, + Qt::KeepAspectRatio, Qt::SmoothTransformation)); } // Save image @@ -615,7 +621,8 @@ void Frame::Thumbnail(std::string path, int new_width, int new_height, std::stri std::string background_color, bool ignore_aspect, std::string format, int quality, float rotate) { // Create blank thumbnail image & fill background color - std::shared_ptr thumbnail = std::shared_ptr(new QImage(new_width, new_height, QImage::Format_RGBA8888)); + auto thumbnail = std::make_shared( + new_width, new_height, QImage::Format_RGBA8888); thumbnail->fill(QColor(QString::fromStdString(background_color))); // Create painter @@ -633,16 +640,22 @@ void Frame::Thumbnail(std::string path, int new_width, int new_height, std::stri int aspect_height = previewImage->size().height() * pixel_ratio.Reciprocal().ToDouble(); // Resize to fix DAR - previewImage = std::shared_ptr(new QImage(previewImage->scaled(aspect_width, aspect_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); + previewImage = std::make_shared(previewImage->scaled( + aspect_width, aspect_height, + Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); } // Resize frame image if (ignore_aspect) // Ignore aspect ratio - previewImage = std::shared_ptr(new QImage(previewImage->scaled(new_width, new_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); + previewImage = std::make_shared(previewImage->scaled( + new_width, new_height, + Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); else // Maintain aspect ratio - previewImage = std::shared_ptr(new QImage(previewImage->scaled(new_width, new_height, Qt::KeepAspectRatio, Qt::SmoothTransformation))); + previewImage = std::make_shared(previewImage->scaled( + new_width, new_height, + Qt::KeepAspectRatio, Qt::SmoothTransformation)); // Composite frame image onto background (centered) int x = (new_width - previewImage->size().width()) / 2.0; // center @@ -666,14 +679,16 @@ void Frame::Thumbnail(std::string path, int new_width, int new_height, std::stri // Overlay Image (if any) if (overlay_path != "") { // Open overlay - std::shared_ptr overlay = std::shared_ptr(new QImage()); + auto overlay = std::make_shared(); overlay->load(QString::fromStdString(overlay_path)); // Set pixel format - overlay = std::shared_ptr(new QImage(overlay->convertToFormat(QImage::Format_RGBA8888))); + overlay = std::make_shared( + overlay->convertToFormat(QImage::Format_RGBA8888)); // Resize to fit - overlay = std::shared_ptr(new QImage(overlay->scaled(new_width, new_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); + overlay = std::make_shared(overlay->scaled( + new_width, new_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); // Composite onto thumbnail painter.setCompositionMode(QPainter::CompositionMode_SourceOver); @@ -684,14 +699,16 @@ void Frame::Thumbnail(std::string path, int new_width, int new_height, std::stri // Mask Image (if any) if (mask_path != "") { // Open mask - std::shared_ptr mask = std::shared_ptr(new QImage()); + auto mask = std::make_shared(); mask->load(QString::fromStdString(mask_path)); // Set pixel format - mask = std::shared_ptr(new QImage(mask->convertToFormat(QImage::Format_RGBA8888))); + mask = std::make_shared( + mask->convertToFormat(QImage::Format_RGBA8888)); // Resize to fit - mask = std::shared_ptr(new QImage(mask->scaled(new_width, new_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); + mask = std::make_shared(mask->scaled( + new_width, new_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); // Negate mask mask->invertPixels(); @@ -744,7 +761,7 @@ void Frame::AddColor(int new_width, int new_height, std::string new_color) const GenericScopedLock lock(addingImageSection); #pragma omp critical (AddImage) { - image = std::shared_ptr(new QImage(new_width, new_height, QImage::Format_RGBA8888)); + image = std::make_shared(new_width, new_height, QImage::Format_RGBA8888); // Fill with solid color image->fill(QColor(QString::fromStdString(color))); @@ -769,11 +786,12 @@ void Frame::AddImage(int new_width, int new_height, int bytes_per_pixel, QImage: // Create new image object, and fill with pixel data #pragma omp critical (AddImage) { - image = std::shared_ptr(new QImage(qbuffer, new_width, new_height, new_width * bytes_per_pixel, type, (QImageCleanupFunction) &openshot::Frame::cleanUpBuffer, (void*) qbuffer)); + image = std::make_shared( + qbuffer, new_width, new_height, new_width * bytes_per_pixel, type, (QImageCleanupFunction) &openshot::Frame::cleanUpBuffer, (void*) qbuffer); // Always convert to RGBA8888 (if different) if (image->format() != QImage::Format_RGBA8888) - *image = image->convertToFormat(QImage::Format_RGBA8888); + *image = image->convertToFormat(QImage::Format_RGBA8888); // Update height and width width = image->width(); @@ -828,7 +846,8 @@ void Frame::AddImage(std::shared_ptr new_image, bool only_odd_lines) ret = true; } else if (new_image->format() != image->format()) { - new_image = std::shared_ptr(new QImage(new_image->convertToFormat(image->format()))); + new_image = std::make_shared( + new_image->convertToFormat(image->format())); } } if (ret) { @@ -938,7 +957,8 @@ std::shared_ptr Frame::GetMagickImage() const QRgb *tmpBits = (const QRgb*)image->constBits(); // Create new image object, and fill with pixel data - std::shared_ptr magick_image = std::shared_ptr(new Magick::Image(image->width(), image->height(),"RGBA", Magick::CharPixel, tmpBits)); + auto magick_image = std::make_shared( + image->width(), image->height(),"RGBA", Magick::CharPixel, tmpBits); // Give image a transparent background color magick_image->backgroundColor(Magick::Color("none")); @@ -967,7 +987,9 @@ void Frame::AddMagickImage(std::shared_ptr new_image) MagickCore::ExportImagePixels(new_image->constImage(), 0, 0, new_image->columns(), new_image->rows(), "RGBA", Magick::CharPixel, buffer, &exception); // Create QImage of frame data - image = std::shared_ptr(new QImage(qbuffer, width, height, width * BPP, QImage::Format_RGBA8888, (QImageCleanupFunction) &cleanUpBuffer, (void*) qbuffer)); + image = std::make_shared( + qbuffer, width, height, width * BPP, QImage::Format_RGBA8888, + (QImageCleanupFunction) &cleanUpBuffer, (void*) qbuffer); // Update height and width width = image->width(); diff --git a/src/FrameMapper.cpp b/src/FrameMapper.cpp index e1e5700c..4946a5c1 100644 --- a/src/FrameMapper.cpp +++ b/src/FrameMapper.cpp @@ -450,7 +450,8 @@ std::shared_ptr FrameMapper::GetFrame(int64_t requested_frame) } // Create a new frame - std::shared_ptr frame = std::make_shared(frame_number, 1, 1, "#000000", samples_in_frame, channels_in_frame); + auto frame = std::make_shared( + frame_number, 1, 1, "#000000", samples_in_frame, channels_in_frame); frame->SampleRate(mapped_frame->SampleRate()); frame->ChannelsLayout(mapped_frame->ChannelsLayout()); @@ -460,13 +461,14 @@ std::shared_ptr FrameMapper::GetFrame(int64_t requested_frame) odd_frame = GetOrCreateFrame(mapped.Odd.Frame); if (odd_frame) - frame->AddImage(std::shared_ptr(new QImage(*odd_frame->GetImage())), true); + frame->AddImage(std::make_shared(*odd_frame->GetImage()), true); if (mapped.Odd.Frame != mapped.Even.Frame) { // Add even lines (if different than the previous image) std::shared_ptr even_frame; even_frame = GetOrCreateFrame(mapped.Even.Frame); if (even_frame) - frame->AddImage(std::shared_ptr(new QImage(*even_frame->GetImage())), false); + frame->AddImage( + std::make_shared(*even_frame->GetImage()), false); } // Resample audio on frame (if needed) diff --git a/src/ImageReader.cpp b/src/ImageReader.cpp index 9ce3a70f..9a001879 100644 --- a/src/ImageReader.cpp +++ b/src/ImageReader.cpp @@ -61,7 +61,7 @@ void ImageReader::Open() try { // load image - image = std::shared_ptr(new Magick::Image(path)); + image = std::make_shared(path); // Give image a transparent background color image->backgroundColor(Magick::Color("none")); @@ -126,7 +126,9 @@ std::shared_ptr ImageReader::GetFrame(int64_t requested_frame) throw ReaderClosed("The FFmpegReader is closed. Call Open() before calling this method.", path); // Create or get frame object - std::shared_ptr image_frame(new Frame(requested_frame, image->size().width(), image->size().height(), "#000000", 0, 2)); + auto image_frame = std::make_shared( + requested_frame, image->size().width(), image->size().height(), + "#000000", 0, 2); // Add Image data to frame image_frame->AddMagickImage(image); diff --git a/src/QtHtmlReader.cpp b/src/QtHtmlReader.cpp index 6b502fbd..4925d3a8 100644 --- a/src/QtHtmlReader.cpp +++ b/src/QtHtmlReader.cpp @@ -62,7 +62,7 @@ void QtHtmlReader::Open() if (!is_open) { // create image - image = std::shared_ptr(new QImage(width, height, QImage::Format_RGBA8888)); + image = std::make_shared(width, height, QImage::Format_RGBA8888); image->fill(QColor(background_color.c_str())); //start painting @@ -162,7 +162,9 @@ std::shared_ptr QtHtmlReader::GetFrame(int64_t requested_frame) if (image) { // Create or get frame object - std::shared_ptr image_frame(new Frame(requested_frame, image->size().width(), image->size().height(), background_color, 0, 2)); + auto image_frame = std::make_shared( + requested_frame, image->size().width(), image->size().height(), + background_color, 0, 2); // Add Image data to frame image_frame->AddImage(image); @@ -171,7 +173,8 @@ std::shared_ptr QtHtmlReader::GetFrame(int64_t requested_frame) return image_frame; } else { // return empty frame - std::shared_ptr image_frame(new Frame(1, 640, 480, background_color, 0, 2)); + auto image_frame = std::make_shared( + 1, 640, 480, background_color, 0, 2); // return frame object return image_frame; diff --git a/src/QtImageReader.cpp b/src/QtImageReader.cpp index cf64ef93..b6dd7ee0 100644 --- a/src/QtImageReader.cpp +++ b/src/QtImageReader.cpp @@ -82,7 +82,8 @@ void QtImageReader::Open() ResvgRenderer renderer(path); if (renderer.isValid()) { - image = std::shared_ptr(new QImage(renderer.defaultSize(), QImage::Format_ARGB32_Premultiplied)); + image = std::make_shared( + renderer.defaultSize(), QImage::Format_ARGB32_Premultiplied); image->fill(Qt::transparent); QPainter p(image.get()); @@ -95,7 +96,7 @@ void QtImageReader::Open() if (!loaded) { // Attempt to open file using Qt's build in image processing capabilities - image = std::shared_ptr(new QImage()); + image = std::make_shared(); success = image->load(path); } @@ -105,7 +106,8 @@ void QtImageReader::Open() } // Convert to proper format - image = std::shared_ptr(new QImage(image->convertToFormat(QImage::Format_RGBA8888))); + image = std::make_shared( + image->convertToFormat(QImage::Format_RGBA8888)); // Update image properties info.has_audio = false; @@ -238,7 +240,9 @@ std::shared_ptr QtImageReader::GetFrame(int64_t requested_frame) svg_size.scale(max_width, max_height, Qt::KeepAspectRatio); // Create empty QImage - cached_image = std::shared_ptr(new QImage(QSize(svg_size.width(), svg_size.height()), QImage::Format_ARGB32_Premultiplied)); + cached_image = std::make_shared( + QSize(svg_size.width(), svg_size.height()), + QImage::Format_ARGB32_Premultiplied); cached_image->fill(Qt::transparent); // Render SVG into QImage @@ -253,10 +257,12 @@ std::shared_ptr QtImageReader::GetFrame(int64_t requested_frame) if (!rendered) { // We need to resize the original image to a smaller image (for performance reasons) // Only do this once, to prevent tons of unneeded scaling operations - cached_image = std::shared_ptr(new QImage(image->scaled(max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation))); + cached_image = std::make_shared(image->scaled( + max_width, max_height, Qt::KeepAspectRatio, Qt::SmoothTransformation)); } - cached_image = std::shared_ptr(new QImage(cached_image->convertToFormat(QImage::Format_RGBA8888))); + cached_image = std::make_shared( + cached_image->convertToFormat(QImage::Format_RGBA8888)); // Set max size (to later determine if max_size is changed) max_size.setWidth(max_width); @@ -264,7 +270,10 @@ std::shared_ptr QtImageReader::GetFrame(int64_t requested_frame) } // Create or get frame object - std::shared_ptr image_frame(new Frame(requested_frame, cached_image->width(), cached_image->height(), "#000000", Frame::GetSamplesPerFrame(requested_frame, info.fps, info.sample_rate, info.channels), info.channels)); + auto image_frame = std::make_shared( + requested_frame, cached_image->width(), cached_image->height(), "#000000", + Frame::GetSamplesPerFrame(requested_frame, info.fps, info.sample_rate, info.channels), + info.channels); // Add Image data to frame image_frame->AddImage(cached_image); diff --git a/src/QtTextReader.cpp b/src/QtTextReader.cpp index d91d164e..bd157ebd 100644 --- a/src/QtTextReader.cpp +++ b/src/QtTextReader.cpp @@ -67,7 +67,7 @@ void QtTextReader::Open() if (!is_open) { // create image - image = std::shared_ptr(new QImage(width, height, QImage::Format_RGBA8888)); + image = std::make_shared(width, height, QImage::Format_RGBA8888); image->fill(QColor(background_color.c_str())); QPainter painter; @@ -179,7 +179,9 @@ std::shared_ptr QtTextReader::GetFrame(int64_t requested_frame) if (image) { // Create or get frame object - std::shared_ptr image_frame(new Frame(requested_frame, image->size().width(), image->size().height(), background_color, 0, 2)); + auto image_frame = std::make_shared( + requested_frame, image->size().width(), image->size().height(), + background_color, 0, 2); // Add Image data to frame image_frame->AddImage(image); @@ -188,7 +190,7 @@ std::shared_ptr QtTextReader::GetFrame(int64_t requested_frame) return image_frame; } else { // return empty frame - std::shared_ptr image_frame(new Frame(1, 640, 480, background_color, 0, 2)); + auto image_frame = std::make_shared(1, 640, 480, background_color, 0, 2); // return frame object return image_frame; diff --git a/src/TextReader.cpp b/src/TextReader.cpp index e317700c..be8c7375 100644 --- a/src/TextReader.cpp +++ b/src/TextReader.cpp @@ -66,7 +66,8 @@ void TextReader::Open() if (!is_open) { // create image - image = std::shared_ptr(new Magick::Image(Magick::Geometry(width,height), Magick::Color(background_color))); + image = std::make_shared( + Magick::Geometry(width,height), Magick::Color(background_color)); // Give image a transparent background color image->backgroundColor(Magick::Color("none")); @@ -166,10 +167,12 @@ std::shared_ptr TextReader::GetFrame(int64_t requested_frame) if (image) { // Create or get frame object - std::shared_ptr image_frame(new Frame(requested_frame, image->size().width(), image->size().height(), "#000000", 0, 2)); + auto image_frame = std::make_shared( + requested_frame, image->size().width(), image->size().height(), + "#000000", 0, 2); // Add Image data to frame - std::shared_ptr copy_image(new Magick::Image(*image.get())); + auto copy_image = std::make_shared(*image.get()); copy_image->modifyImage(); // actually copy the image data to this object //TODO: Reimplement this with QImage image_frame->AddMagickImage(copy_image); @@ -178,7 +181,7 @@ std::shared_ptr TextReader::GetFrame(int64_t requested_frame) return image_frame; } else { // return empty frame - std::shared_ptr image_frame(new Frame(1, 640, 480, "#000000", 0, 2)); + auto image_frame = std::make_shared(1, 640, 480, "#000000", 0, 2); // return frame object return image_frame; diff --git a/src/effects/Bars.cpp b/src/effects/Bars.cpp index 3f9aac34..e653b7dd 100644 --- a/src/effects/Bars.cpp +++ b/src/effects/Bars.cpp @@ -68,7 +68,8 @@ std::shared_ptr Bars::GetFrame(std::shared_ptr frame, int64_t fram std::shared_ptr frame_image = frame->GetImage(); // Get bar color (and create small color image) - std::shared_ptr tempColor = std::shared_ptr(new QImage(frame_image->width(), 1, QImage::Format_RGBA8888)); + auto tempColor = std::make_shared( + frame_image->width(), 1, QImage::Format_RGBA8888); tempColor->fill(QColor(QString::fromStdString(color.GetColorHex(frame_number)))); // Get current keyframe values diff --git a/src/effects/Crop.cpp b/src/effects/Crop.cpp index b1c3d38d..f0e0aa95 100644 --- a/src/effects/Crop.cpp +++ b/src/effects/Crop.cpp @@ -68,7 +68,8 @@ std::shared_ptr Crop::GetFrame(std::shared_ptr frame, int64_t fram std::shared_ptr frame_image = frame->GetImage(); // Get transparent color (and create small transparent image) - std::shared_ptr tempColor = std::shared_ptr(new QImage(frame_image->width(), 1, QImage::Format_RGBA8888)); + auto tempColor = std::make_shared( + frame_image->width(), 1, QImage::Format_RGBA8888); tempColor->fill(QColor(QString::fromStdString("transparent"))); // Get current keyframe values diff --git a/src/effects/Deinterlace.cpp b/src/effects/Deinterlace.cpp index 39b3316a..984336aa 100644 --- a/src/effects/Deinterlace.cpp +++ b/src/effects/Deinterlace.cpp @@ -86,7 +86,9 @@ std::shared_ptr Deinterlace::GetFrame(std::shared_ptr frame, int64 } // Resize deinterlaced image back to original size, and update frame's image - image = std::shared_ptr(new QImage(deinterlaced_image.scaled(original_width, original_height, Qt::IgnoreAspectRatio, Qt::FastTransformation))); + image = std::make_shared(deinterlaced_image.scaled( + original_width, original_height, + Qt::IgnoreAspectRatio, Qt::FastTransformation)); // Update image on frame frame->AddImage(image); diff --git a/src/effects/Mask.cpp b/src/effects/Mask.cpp index 11c37f05..f270e410 100644 --- a/src/effects/Mask.cpp +++ b/src/effects/Mask.cpp @@ -84,13 +84,14 @@ std::shared_ptr Mask::GetFrame(std::shared_ptr frame, int64_t fram (original_mask && original_mask->size() != frame_image->size())) { // Only get mask if needed - std::shared_ptr mask_without_sizing = std::shared_ptr( - new QImage(*reader->GetFrame(frame_number)->GetImage())); + auto mask_without_sizing = std::make_shared( + *reader->GetFrame(frame_number)->GetImage()); // Resize mask image to match frame size - original_mask = std::shared_ptr(new QImage( - mask_without_sizing->scaled(frame_image->width(), frame_image->height(), Qt::IgnoreAspectRatio, - Qt::SmoothTransformation))); + original_mask = std::make_shared( + mask_without_sizing->scaled( + frame_image->width(), frame_image->height(), + Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); } } From c14922d57ea8226b2876a822dab9911ae2f1fbf9 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Thu, 20 Aug 2020 20:40:55 -0400 Subject: [PATCH 2/7] Frame.cpp/h: Fix a bunch of wrong comments Best reason not to narrate the code in the comments: The code gets changed, but the documentation doesn't. --- include/Frame.h | 12 ++++++------ src/Frame.cpp | 22 +++++++--------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/include/Frame.h b/include/Frame.h index 7e7866c0..8987dcb4 100644 --- a/include/Frame.h +++ b/include/Frame.h @@ -73,17 +73,17 @@ namespace openshot * There are many ways to create an instance of an openshot::Frame: * @code * - * // Most basic: a blank frame (300x200 blank image, 48kHz audio silence) + * // Most basic: a blank frame (all default values) * Frame(); * - * // Image only settings (48kHz audio silence) + * // Image only settings * Frame(1, // Frame number * 720, // Width of image * 480, // Height of image * "#000000" // HTML color code of background color * ); * - * // Audio only (300x200 blank image) + * // Audio only * Frame(number, // Frame number * 44100, // Sample rate of audio stream * 2 // Number of audio channels @@ -131,13 +131,13 @@ namespace openshot bool has_image_data; ///< This frame has been loaded with pixel data - /// Constructor - blank frame (300x200 blank image, 48kHz audio silence) + /// Constructor - blank frame Frame(); - /// Constructor - image only (48kHz audio silence) + /// Constructor - image only Frame(int64_t number, int width, int height, std::string color); - /// Constructor - audio only (300x200 blank image) + /// Constructor - audio only Frame(int64_t number, int samples, int channels); /// Constructor - image & audio diff --git a/src/Frame.cpp b/src/Frame.cpp index 16f420bc..54901cc5 100644 --- a/src/Frame.cpp +++ b/src/Frame.cpp @@ -33,41 +33,35 @@ using namespace std; using namespace openshot; -// Constructor - blank frame (300x200 blank image, 48kHz audio silence) +// Constructor - blank frame Frame::Frame() : number(1), pixel_ratio(1,1), channels(2), width(1), height(1), color("#000000"), channel_layout(LAYOUT_STEREO), sample_rate(44100), qbuffer(NULL), has_audio_data(false), has_image_data(false), max_audio_sample(0) { - // Init the image magic and audio buffer + // Allocate and zero (fill with silence) the audio buffer audio = std::make_shared(channels, 0); - - // initialize the audio samples to zero (silence) audio->clear(); } -// Constructor - image only (48kHz audio silence) +// Constructor - image only Frame::Frame(int64_t number, int width, int height, std::string color) : number(number), pixel_ratio(1,1), channels(2), width(width), height(height), color(color), channel_layout(LAYOUT_STEREO), sample_rate(44100), qbuffer(NULL), has_audio_data(false), has_image_data(false), max_audio_sample(0) { - // Init the image magic and audio buffer + // Allocate and zero (fill with silence) the audio buffer audio = std::make_shared(channels, 0); - - // initialize the audio samples to zero (silence) audio->clear(); } -// Constructor - audio only (300x200 blank image) +// Constructor - audio only Frame::Frame(int64_t number, int samples, int channels) : number(number), pixel_ratio(1,1), channels(channels), width(1), height(1), color("#000000"), channel_layout(LAYOUT_STEREO), sample_rate(44100), qbuffer(NULL), has_audio_data(false), has_image_data(false), max_audio_sample(0) { - // Init the image magic and audio buffer + // Allocate and zero (fill with silence) the audio buffer audio = std::make_shared(channels, samples); - - // initialize the audio samples to zero (silence) audio->clear(); } @@ -77,10 +71,8 @@ Frame::Frame(int64_t number, int width, int height, std::string color, int sampl channel_layout(LAYOUT_STEREO), sample_rate(44100), qbuffer(NULL), has_audio_data(false), has_image_data(false), max_audio_sample(0) { - // Init the image magic and audio buffer + // Allocate and zero (fill with silence) the audio buffer audio = std::make_shared(channels, samples); - - // initialize the audio samples to zero (silence) audio->clear(); } From 1c8aea94d0a86985934fb07e28dfdd4ecdf850ee Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Sun, 13 Sep 2020 16:28:31 -0400 Subject: [PATCH 3/7] Frame: Put Qt includes where they're used --- include/Frame.h | 12 ++---------- src/Frame.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/include/Frame.h b/include/Frame.h index 8987dcb4..b3416d38 100644 --- a/include/Frame.h +++ b/include/Frame.h @@ -34,16 +34,8 @@ #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include #include #include #include "ZmqLogger.h" diff --git a/src/Frame.cpp b/src/Frame.cpp index 54901cc5..48af2a28 100644 --- a/src/Frame.cpp +++ b/src/Frame.cpp @@ -29,6 +29,21 @@ */ #include "../include/Frame.h" +#include "JuceHeader.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include using namespace std; using namespace openshot; From 9c83429ab1042a992511a30ede220214c593c31b Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Sun, 13 Sep 2020 16:33:36 -0400 Subject: [PATCH 4/7] Frame: Use delegating constructors --- src/Frame.cpp | 54 +++++++++++++++++---------------------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/src/Frame.cpp b/src/Frame.cpp index 48af2a28..67371655 100644 --- a/src/Frame.cpp +++ b/src/Frame.cpp @@ -48,49 +48,31 @@ using namespace std; using namespace openshot; -// Constructor - blank frame -Frame::Frame() : number(1), pixel_ratio(1,1), channels(2), width(1), height(1), color("#000000"), - channel_layout(LAYOUT_STEREO), sample_rate(44100), qbuffer(NULL), has_audio_data(false), has_image_data(false), - max_audio_sample(0) -{ - // Allocate and zero (fill with silence) the audio buffer - audio = std::make_shared(channels, 0); - audio->clear(); -} - -// Constructor - image only -Frame::Frame(int64_t number, int width, int height, std::string color) - : number(number), pixel_ratio(1,1), channels(2), width(width), height(height), color(color), - channel_layout(LAYOUT_STEREO), sample_rate(44100), qbuffer(NULL), has_audio_data(false), has_image_data(false), - max_audio_sample(0) -{ - // Allocate and zero (fill with silence) the audio buffer - audio = std::make_shared(channels, 0); - audio->clear(); -} - -// Constructor - audio only -Frame::Frame(int64_t number, int samples, int channels) : - number(number), pixel_ratio(1,1), channels(channels), width(1), height(1), color("#000000"), - channel_layout(LAYOUT_STEREO), sample_rate(44100), qbuffer(NULL), has_audio_data(false), has_image_data(false), - max_audio_sample(0) -{ - // Allocate and zero (fill with silence) the audio buffer - audio = std::make_shared(channels, samples); - audio->clear(); -} - // Constructor - image & audio Frame::Frame(int64_t number, int width, int height, std::string color, int samples, int channels) - : number(number), pixel_ratio(1,1), channels(channels), width(width), height(height), color(color), - channel_layout(LAYOUT_STEREO), sample_rate(44100), qbuffer(NULL), has_audio_data(false), has_image_data(false), + : audio(std::make_shared(channels, samples)), + number(number), width(width), height(height), + pixel_ratio(1,1), color(color), qbuffer(NULL), + channels(channels), channel_layout(LAYOUT_STEREO), + sample_rate(44100), + has_audio_data(false), has_image_data(false), max_audio_sample(0) { - // Allocate and zero (fill with silence) the audio buffer - audio = std::make_shared(channels, samples); + // zero (fill with silence) the audio buffer audio->clear(); } +// Delegating Constructor - blank frame +Frame::Frame() : Frame::Frame(1, 1, 1, "#000000", 0, 2) {}; + +// Delegating Constructor - image only +Frame::Frame(int64_t number, int width, int height, std::string color) + : Frame::Frame(number, width, height, color, 0, 2) {}; + +// Delegating Constructor - audio only +Frame::Frame(int64_t number, int samples, int channels) + : Frame::Frame(number, 1, 1, "#000000", samples, channels) {}; + // Copy constructor Frame::Frame ( const Frame &other ) From 0974637a3a71a1acd115bac33634ea6d646c7472 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Sun, 13 Sep 2020 16:33:16 -0400 Subject: [PATCH 5/7] Pixelate: Fix missing includes --- include/effects/Pixelate.h | 1 - src/effects/Pixelate.cpp | 6 ++++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/effects/Pixelate.h b/include/effects/Pixelate.h index 4cdd440f..793f4d46 100644 --- a/include/effects/Pixelate.h +++ b/include/effects/Pixelate.h @@ -36,7 +36,6 @@ #include #include #include -#include "../Color.h" #include "../Json.h" #include "../KeyFrame.h" diff --git a/src/effects/Pixelate.cpp b/src/effects/Pixelate.cpp index c993915c..41d97c5c 100644 --- a/src/effects/Pixelate.cpp +++ b/src/effects/Pixelate.cpp @@ -29,6 +29,12 @@ */ #include "../../include/effects/Pixelate.h" +#include "Json.h" + +#include +#include +#include +#include using namespace openshot; From 92d33a1ebd5a7da249195db37afb7edb4a3abceb Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Sun, 13 Sep 2020 16:38:23 -0400 Subject: [PATCH 6/7] VideoRenderWidget: missing includes --- include/Qt/VideoRenderWidget.h | 6 ++++-- src/Qt/VideoRenderWidget.cpp | 8 +++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/Qt/VideoRenderWidget.h b/include/Qt/VideoRenderWidget.h index 07c61037..4d9ac17f 100644 --- a/include/Qt/VideoRenderWidget.h +++ b/include/Qt/VideoRenderWidget.h @@ -31,11 +31,13 @@ #ifndef OPENSHOT_VIDEO_RENDERER_WIDGET_H #define OPENSHOT_VIDEO_RENDERER_WIDGET_H -#include -#include #include "../Fraction.h" #include "VideoRenderer.h" +#include +#include +#include +#include class VideoRenderWidget : public QWidget { diff --git a/src/Qt/VideoRenderWidget.cpp b/src/Qt/VideoRenderWidget.cpp index 2bfe8fa2..4af1ac6a 100644 --- a/src/Qt/VideoRenderWidget.cpp +++ b/src/Qt/VideoRenderWidget.cpp @@ -29,7 +29,13 @@ */ #include "../../include/Qt/VideoRenderWidget.h" -#include +#include +#include +#include +#include +#include +#include + VideoRenderWidget::VideoRenderWidget(QWidget *parent) : QWidget(parent), renderer(new VideoRenderer(this)) From 0bcf1e49247977e2821e093573d9d64cf7585caf Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Sun, 13 Sep 2020 19:56:40 -0400 Subject: [PATCH 7/7] Frame: Reduce code duplication --- src/Frame.cpp | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/Frame.cpp b/src/Frame.cpp index 67371655..c3c50bcd 100644 --- a/src/Frame.cpp +++ b/src/Frame.cpp @@ -762,31 +762,31 @@ void Frame::AddColor(int new_width, int new_height, std::string new_color) } // Add (or replace) pixel data to the frame -void Frame::AddImage(int new_width, int new_height, int bytes_per_pixel, QImage::Format type, const unsigned char *pixels_) +void Frame::AddImage( + int new_width, int new_height, int bytes_per_pixel, + QImage::Format type, const unsigned char *pixels_) { // Create new buffer - const GenericScopedLock lock(addingImageSection); - int buffer_size = new_width * new_height * bytes_per_pixel; - qbuffer = new unsigned char[buffer_size](); - - // Copy buffer data - memcpy((unsigned char*)qbuffer, pixels_, buffer_size); - - // Create new image object, and fill with pixel data - #pragma omp critical (AddImage) { - image = std::make_shared( - qbuffer, new_width, new_height, new_width * bytes_per_pixel, type, (QImageCleanupFunction) &openshot::Frame::cleanUpBuffer, (void*) qbuffer); + const GenericScopedLock lock(addingImageSection); + int buffer_size = new_width * new_height * bytes_per_pixel; + qbuffer = new unsigned char[buffer_size](); - // Always convert to RGBA8888 (if different) - if (image->format() != QImage::Format_RGBA8888) - *image = image->convertToFormat(QImage::Format_RGBA8888); + // Copy buffer data + memcpy((unsigned char*)qbuffer, pixels_, buffer_size); - // Update height and width - width = image->width(); - height = image->height(); - has_image_data = true; - } + } // Release addingImageSection lock + + // Create new image object from pixel data + auto new_image = std::make_shared( + qbuffer, + new_width, new_height, + new_width * bytes_per_pixel, + type, + (QImageCleanupFunction) &openshot::Frame::cleanUpBuffer, + (void*) qbuffer + ); + AddImage(new_image); } // Add (or replace) pixel data to the frame @@ -826,7 +826,6 @@ void Frame::AddImage(std::shared_ptr new_image, bool only_odd_lines) AddImage(new_image); } else { - // Ignore image of different sizes or formats bool ret=false; #pragma omp critical (AddImage)