Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5b94e48
moved library files to the common directory structure
wvaarle Dec 14, 2018
3c94fde
moved example files to the common directory structure
wvaarle Dec 14, 2018
465a076
removed all visual studio project files
wvaarle Dec 14, 2018
de143d4
update gitignore file
wvaarle Dec 14, 2018
d76cc3e
Fixed include paths
wvaarle Dec 14, 2018
add307f
added cmake files
wvaarle Dec 17, 2018
0534884
use nullptr instead of NULL or 0 (clang-tidy: modernize-use-nullptr)
wvaarle Dec 17, 2018
498ad8f
use the override keyword on overridden functions (clang-tidy: moderni…
wvaarle Dec 17, 2018
25fc7da
replaced std::endl with '\n'
wvaarle Dec 17, 2018
145c709
examples: use smart pointers instead of raw pointers
wvaarle Dec 17, 2018
5d5b477
examples: removed using namespace std
wvaarle Dec 17, 2018
785c64d
examples: removed using namespace ffmpegcpp
wvaarle Dec 17, 2018
dace788
smart pointerify all uses of AVCodecContext*
wvaarle Dec 18, 2018
01872f6
smart pointerify all uses of AVPacket*
wvaarle Dec 18, 2018
165c80e
smart pointerify all uses of AVFrame*
wvaarle Dec 18, 2018
0edf8db
smart pointerify all uses of AVFilterGraph*
wvaarle Dec 18, 2018
09288a8
smart pointerify all uses of AVAudioFifo*
wvaarle Dec 18, 2018
203808a
smart pointerify all uses of SwsContext*
wvaarle Dec 18, 2018
edc7549
smart pointerify all uses of SwrContext*
wvaarle Dec 18, 2018
1117ca4
smart pointerify all uses of AVCodecParserContext*
wvaarle Dec 18, 2018
09172b8
smart pointerify all uses of AVFormatContext*
wvaarle Dec 18, 2018
efed9f8
removed more cleanup() functions by managing member variables with sm…
wvaarle Dec 18, 2018
cee50cc
use smart pointer for OpenCodec
wvaarle Dec 19, 2018
d5b3e56
forward declare as much as possible
wvaarle Dec 19, 2018
5b5bd80
replaced InputStream** with vector<unique_ptr>
wvaarle Dec 19, 2018
0659b07
replaced for loops with algorithms where possible
wvaarle Dec 19, 2018
9520784
added const specifier to const member functions
wvaarle Dec 19, 2018
c070af8
removed std.h
wvaarle Dec 20, 2018
d49c1c7
removed some compile warnings
wvaarle Dec 20, 2018
9b0be3c
replaced char * with std::strings
wvaarle Dec 20, 2018
814276e
code cleanup
wvaarle Dec 21, 2018
87b1be7
fixed compile time issues
wvaarle Dec 21, 2018
974b36c
fixed crashes
wvaarle Dec 21, 2018
bce29c8
don't free resources when not owner
wvaarle Dec 21, 2018
a2cc28f
fixed memory issue
wvaarle Dec 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
examples: use smart pointers instead of raw pointers
This solves a bunch of memory leaks where objects didn't get deleted when an exception was thrown.
  • Loading branch information
wvaarle committed Dec 17, 2018
commit 145c709799bbb87966110f33c256833e035b3644
16 changes: 7 additions & 9 deletions examples/decode_audio/decode_audio.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include <iostream>
#include <memory>

#include "ffmpegcpp.h"

Expand Down Expand Up @@ -52,26 +53,23 @@ int main()
try
{
// Load this container file so we can extract audio from it.
Demuxer* demuxer = new Demuxer("samples/big_buck_bunny.mp4");
Demuxer demuxer("samples/big_buck_bunny.mp4");

// Create a file sink that will just output the raw audio data.
RawAudioFileSink* fileSink = new RawAudioFileSink("rawaudio");
auto fileSink = std::make_unique<RawAudioFileSink>("rawaudio");

// tie the file sink to the best audio stream in the input container.
demuxer->DecodeBestAudioStream(fileSink);
demuxer.DecodeBestAudioStream(fileSink.get());

// Prepare the output pipeline. This will push a small amount of frames to the file sink until it IsPrimed returns true.
demuxer->PreparePipeline();
demuxer.PreparePipeline();

// Push all the remaining frames through.
while (!demuxer->IsDone())
while (!demuxer.IsDone())
{
demuxer->Step();
demuxer.Step();
}

// done
delete demuxer;
delete fileSink;
}
catch (FFmpegException e)
{
Expand Down
17 changes: 7 additions & 10 deletions examples/decode_video/decode_video.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include <iostream>
#include <memory>

#include "ffmpegcpp.h"

Expand Down Expand Up @@ -67,26 +68,22 @@ int main()
try
{
// Load this container file so we can extract video from it.
Demuxer* demuxer = new Demuxer("samples/big_buck_bunny.mp4");
Demuxer demuxer("samples/big_buck_bunny.mp4");

// Create a file sink that will just output the raw frame data in one PGM file per frame.
PGMFileSink* fileSink = new PGMFileSink();
auto fileSink = std::make_unique<PGMFileSink>();

// tie the file sink to the best video stream in the input container.
demuxer->DecodeBestVideoStream(fileSink);
demuxer.DecodeBestVideoStream(fileSink.get());

// Prepare the output pipeline. This will push a small amount of frames to the file sink until it IsPrimed returns true.
demuxer->PreparePipeline();
demuxer.PreparePipeline();

// Push all the remaining frames through.
while (!demuxer->IsDone())
while (!demuxer.IsDone())
{
demuxer->Step();
demuxer.Step();
}

// done
delete demuxer;
delete fileSink;
}
catch (FFmpegException e)
{
Expand Down
3 changes: 1 addition & 2 deletions examples/demo/GeneratedAudioSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ GeneratedAudioSource::GeneratedAudioSource(AudioFrameSink* frameSink)

// generate a raw video source that will convert the raw format to any other format and pass it on to the encoder
// or any other sink (might be a filter as well).
output = new RawAudioDataSource(format, this->sampleRate, this->channels, frameSink);
output = std::make_unique<RawAudioDataSource>(format, this->sampleRate, this->channels, frameSink);

samples = new uint16_t[channels * 2 * sampleCount];

}

GeneratedAudioSource::~GeneratedAudioSource()
{
delete output;
delete samples;
}

Expand Down
4 changes: 3 additions & 1 deletion examples/demo/GeneratedAudioSource.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <memory>

#include "ffmpegcpp.h"

using namespace ffmpegcpp;
Expand All @@ -21,7 +23,7 @@ class GeneratedAudioSource : public InputSource
int channels;
AVSampleFormat format;

RawAudioDataSource* output;
std::unique_ptr<RawAudioDataSource> output;

int sampleCount = 735;

Expand Down
3 changes: 1 addition & 2 deletions examples/demo/GeneratedVideoSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ GeneratedVideoSource::GeneratedVideoSource(int width, int height, VideoFrameSink
{
// generate a raw video source that will convert the raw format to any other format and pass it on to the encoder
// or any other sink (might be a filter as well).
output = new RawVideoDataSource(width, height, AV_PIX_FMT_RGBA, AV_PIX_FMT_RGBA, 30, frameSink);
output = std::make_unique<RawVideoDataSource>(width, height, AV_PIX_FMT_RGBA, AV_PIX_FMT_RGBA, 30, frameSink);
}

GeneratedVideoSource::~GeneratedVideoSource()
{
delete output;
delete rgb;
}

Expand Down
4 changes: 3 additions & 1 deletion examples/demo/GeneratedVideoSource.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <memory>

#include "ffmpegcpp.h"

using namespace ffmpegcpp;
Expand All @@ -17,7 +19,7 @@ class GeneratedVideoSource : public InputSource

private:

RawVideoDataSource* output;
std::unique_ptr<RawVideoDataSource> output;

int frameNumber = 0;

Expand Down
91 changes: 32 additions & 59 deletions examples/demo/demo.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include <iostream>
#include <memory>

#include "GeneratedVideoSource.h"
#include "GeneratedAudioSource.h"
Expand Down Expand Up @@ -63,24 +64,24 @@ void PlayDemo(int argc, char** argv)
*/

// create the output muxer - we'll be adding encoders to it later
Muxer* muxer = new Muxer(outputContainerName.c_str());
auto muxer = std::make_unique<Muxer>(outputContainerName.c_str());

/**
* CONFIGURE AUDIO OUTPUT
*/

// create the output encoder based on our setting above
AudioCodec* audioCodec = nullptr;
std::unique_ptr<AudioCodec> audioCodec;
if (outputAudioCodec == "MP2")
{
printf("Encoding audio as MP2...\n");
audioCodec = new AudioCodec(AV_CODEC_ID_MP2);
audioCodec = std::make_unique<AudioCodec>(AV_CODEC_ID_MP2);

}
else if (outputAudioCodec == "AAC")
{
printf("Encoding audio as AAC...\n");
audioCodec = new AudioCodec(AV_CODEC_ID_AAC);
audioCodec = std::make_unique<AudioCodec>(AV_CODEC_ID_AAC);

}
else if (outputAudioCodec == "NONE")
Expand All @@ -90,38 +91,38 @@ void PlayDemo(int argc, char** argv)

// create an encoder - this encoder will receive raw data from any source (filter, file, container, memory, etc),
// encode it and send it to the muxer (the output container).
AudioEncoder* audioEncoder = nullptr;
std::unique_ptr<AudioEncoder> audioEncoder;
if (audioCodec != nullptr)
{
audioEncoder = new AudioEncoder(audioCodec, muxer);
audioEncoder = std::make_unique<AudioEncoder>(audioCodec.get(), muxer.get());
}

/**
* CONFIGURE VIDEO OUTPUT
*/

// create the output encoder based on our setting above
VideoCodec* videoCodec = nullptr;
std::unique_ptr<VideoCodec> videoCodec;
if (outputVideoCodec == "H264")
{
printf("Encoding video as H264 on Nvidia GPU...\n");
H264NVEncCodec* h264Codec = new H264NVEncCodec();
auto h264Codec = std::make_unique<H264NVEncCodec>();
h264Codec->SetPreset("hq");
videoCodec = h264Codec;
videoCodec = std::move(h264Codec);
}
else if (outputVideoCodec == "H265")
{
printf("Encoding video as H265 on Nvidia GPU...\n");
H265NVEncCodec* h265Codec = new H265NVEncCodec();
auto h265Codec = std::make_unique<H265NVEncCodec>();
h265Codec->SetPreset("hq");
videoCodec = h265Codec;
videoCodec = std::move(h265Codec);
}
else if (outputVideoCodec == "VP9")
{
printf("Encoding video as VP9...\n");
VP9Codec* vp9Codec = new VP9Codec();
auto vp9Codec = std::make_unique<VP9Codec>();
vp9Codec->SetLossless(true);
videoCodec = vp9Codec;
videoCodec = std::move(vp9Codec);
}
else if (outputVideoCodec == "NONE")
{
Expand All @@ -130,91 +131,88 @@ void PlayDemo(int argc, char** argv)

// create an encoder for the codec and tie it to the muxer
// this encoder will receive data from an input source (file, raw, filter, etc), encode it and send it to the output container (muxer)
VideoEncoder* videoEncoder = nullptr;
std::unique_ptr<VideoEncoder> videoEncoder;
if (videoCodec != nullptr)
{
videoEncoder = new VideoEncoder(videoCodec, muxer);
videoEncoder = std::make_unique<VideoEncoder>(videoCodec.get(), muxer.get());
}

/**
* CONFIGURE AUDIO INPUT
*/

// only do this when there is an output - otherwise there is no point in reading audio
InputSource* audioInputSource = nullptr;
std::unique_ptr<InputSource> audioInputSource;
if (audioEncoder != nullptr)
{
if (inputAudioSource == "RAW")
{
printf("Pulling audio from %s...\n", rawAudioFile);
audioInputSource = new RawAudioFileSource(rawAudioFile, rawAudioFormat, rawAudioSampleRate, rawAudioChannels, audioEncoder);
audioInputSource = std::make_unique<RawAudioFileSource>(rawAudioFile, rawAudioFormat, rawAudioSampleRate, rawAudioChannels, audioEncoder.get());
}
else if (inputAudioSource == "ENCODED")
{
printf("Pulling audio from %s...\n", encodedAudioFile);
audioInputSource = new EncodedFileSource(encodedAudioFile, AV_CODEC_ID_MP3, audioEncoder);
audioInputSource = std::make_unique<EncodedFileSource>(encodedAudioFile, AV_CODEC_ID_MP3, audioEncoder.get());
}
else if (inputAudioSource == "CONTAINER")
{
// if the input comes from a container, we use the demuxer class - it is just an input source like any other
printf("Pulling audio from %s...\n", containerWithAudioFile);
Demuxer* demuxer = new Demuxer(containerWithAudioFile);
demuxer->DecodeBestAudioStream(audioEncoder);
audioInputSource = demuxer;
auto demuxer = std::make_unique<Demuxer>(containerWithAudioFile);
demuxer->DecodeBestAudioStream(audioEncoder.get());
audioInputSource = std::move(demuxer);
}
else if (inputAudioSource == "GENERATED")
{
printf("Generating 440Hz audio tone...\n");
audioInputSource = new GeneratedAudioSource(audioEncoder);
audioInputSource = std::make_unique<GeneratedAudioSource>(audioEncoder.get());
}
}

/**
* CONFIGURE VIDEO FILTER IF IT IS USED
*/

VideoFrameSink* videoFrameSink = videoEncoder;

// If a video filter was specified, we inject it into the pipeline here.
// Instead of feeding the video source directly to the encoder, we feed it to
// the video filter instead, which will pass it on to the encoder.
VideoFilter* videoFilter = nullptr;
std::unique_ptr<VideoFilter> videoFilter;
if (videoFilterConfig != nullptr && videoEncoder != nullptr)
{
printf("Applying filter %s to video...\n", videoFilterConfig);
videoFilter = new VideoFilter(videoFilterConfig, videoEncoder);
videoFrameSink = videoFilter; // used to feed the source below
videoFilter = std::make_unique<VideoFilter>(videoFilterConfig, videoEncoder.get());
}

/**
* CONFIGURE VIDEO INPUT
*/

// only do this when there is video output
InputSource* videoInputSource = nullptr;
std::unique_ptr<InputSource> videoInputSource;
if (videoEncoder != nullptr)
{
if (inputVideoSource == "RAW")
{
printf("Pulling video from %s...\n", rawVideoFile);
videoInputSource = new RawVideoFileSource(rawVideoFile, videoFrameSink);
videoInputSource = std::make_unique<RawVideoFileSource>(rawVideoFile, videoFilter.get());
}
else if (inputVideoSource == "ENCODED")
{
printf("Pulling video from %s...\n", encodedVideoFile);
videoInputSource = new RawVideoFileSource(encodedVideoFile, videoFrameSink);
videoInputSource = std::make_unique<RawVideoFileSource>(encodedVideoFile, videoFilter.get());
}
else if (inputVideoSource == "CONTAINER")
{
printf("Pulling video from %s...\n", containerWithVideoAndAudioFile);
Demuxer* demuxer = new Demuxer(containerWithVideoAndAudioFile);
demuxer->DecodeBestVideoStream(videoFrameSink);
videoInputSource = demuxer;
auto demuxer = std::make_unique<Demuxer>(containerWithVideoAndAudioFile);
demuxer->DecodeBestVideoStream(videoFilter.get());
videoInputSource = std::move(demuxer);
}
else if (inputVideoSource == "GENERATED")
{
printf("Generating checkerboard video pattern...\n");
videoInputSource = new GeneratedVideoSource(640, 480, videoFrameSink);
videoInputSource = std::make_unique<GeneratedVideoSource>(640, 480, videoFilter.get());
}
}

Expand Down Expand Up @@ -249,31 +247,6 @@ void PlayDemo(int argc, char** argv)
// close the muxer and save the file to disk
muxer->Close();

// all done
if (audioCodec != nullptr)
{
delete audioCodec;
delete audioEncoder;
}
if (videoCodec != nullptr)
{
delete videoCodec;
delete videoEncoder;
if (videoFilter != nullptr) delete videoFilter;
}

if (audioInputSource != nullptr)
{
delete audioInputSource;
}

if (videoInputSource != nullptr)
{
delete videoInputSource;
}


delete muxer;
}
catch (FFmpegException e)
{
Expand Down
Loading