1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-09-19 01:20:54 +02:00

Remove some naked new amd delete in: FFmpeg

This commit is contained in:
Paul Licameli 2016-08-01 11:44:49 -04:00
parent 942c62b6f6
commit fd2e36e0c8
7 changed files with 54 additions and 72 deletions

View File

@ -40,44 +40,41 @@ wxString GetFFmpegVersion(wxWindow *parent)
/** This pointer to the shared object has global scope and is used to track the /** This pointer to the shared object has global scope and is used to track the
* singleton object which wraps the FFmpeg codecs */ * singleton object which wraps the FFmpeg codecs */
FFmpegLibs *FFmpegLibsInst = NULL; std::unique_ptr<FFmpegLibs> FFmpegLibsPtr{};
FFmpegLibs *FFmpegLibsInst()
{
return FFmpegLibsPtr.get();
}
FFmpegLibs *PickFFmpegLibs() FFmpegLibs *PickFFmpegLibs()
{ {
if (FFmpegLibsInst != NULL) if (FFmpegLibsPtr)
{ FFmpegLibsPtr->refcount++;
FFmpegLibsInst->refcount++;
return FFmpegLibsInst;
}
else else
{ FFmpegLibsPtr = std::make_unique<FFmpegLibs>();
FFmpegLibsInst = new FFmpegLibs();
return FFmpegLibsInst; return FFmpegLibsPtr.get();
}
} }
void DropFFmpegLibs() void DropFFmpegLibs()
{ {
if (FFmpegLibsInst != NULL) if (FFmpegLibsPtr)
{ {
FFmpegLibsInst->refcount--; FFmpegLibsPtr->refcount--;
if (FFmpegLibsInst->refcount == 0) if (FFmpegLibsPtr->refcount == 0)
{ FFmpegLibsPtr.reset();
delete FFmpegLibsInst;
FFmpegLibsInst = NULL;
}
} }
} }
bool LoadFFmpeg(bool showerror) bool LoadFFmpeg(bool showerror)
{ {
PickFFmpegLibs(); PickFFmpegLibs();
if (FFmpegLibsInst->ValidLibsLoaded()) if (FFmpegLibsInst()->ValidLibsLoaded())
{ {
DropFFmpegLibs(); DropFFmpegLibs();
return true; return true;
} }
if (!FFmpegLibsInst->LoadLibs(NULL,showerror)) if (!FFmpegLibsInst()->LoadLibs(NULL, showerror))
{ {
DropFFmpegLibs(); DropFFmpegLibs();
gPrefs->Write(wxT("/FFmpeg/Enabled"), false); gPrefs->Write(wxT("/FFmpeg/Enabled"), false);
@ -116,8 +113,8 @@ wxString GetFFmpegVersion(wxWindow * WXUNUSED(parent))
wxString versionString = _("FFmpeg library not found"); wxString versionString = _("FFmpeg library not found");
if (FFmpegLibsInst->ValidLibsLoaded()) { if (FFmpegLibsInst()->ValidLibsLoaded()) {
versionString = FFmpegLibsInst->GetLibraryVersion(); versionString = FFmpegLibsInst()->GetLibraryVersion();
} }
DropFFmpegLibs(); DropFFmpegLibs();
@ -295,7 +292,7 @@ fail:
FFmpegContext::~FFmpegContext() FFmpegContext::~FFmpegContext()
{ {
if (FFmpegLibsInst->ValidLibsLoaded()) if (FFmpegLibsInst()->ValidLibsLoaded())
{ {
if (ic_ptr) if (ic_ptr)
avformat_close_input(&ic_ptr); avformat_close_input(&ic_ptr);
@ -304,7 +301,7 @@ FFmpegContext::~FFmpegContext()
if (pb) { if (pb) {
ufile_close(pb); ufile_close(pb);
if (FFmpegLibsInst->ValidLibsLoaded()) if (FFmpegLibsInst()->ValidLibsLoaded())
{ {
av_free(pb->buffer); av_free(pb->buffer);
av_free(pb); av_free(pb);
@ -581,7 +578,6 @@ FFmpegLibs::FFmpegLibs()
{ {
mLibsLoaded = false; mLibsLoaded = false;
refcount = 1; refcount = 1;
avformat = avcodec = avutil = NULL;
if (gPrefs) { if (gPrefs) {
mLibAVFormatPath = gPrefs->Read(wxT("/FFmpeg/FFmpegLibPath"), wxT("")); mLibAVFormatPath = gPrefs->Read(wxT("/FFmpeg/FFmpegLibPath"), wxT(""));
} }
@ -780,7 +776,7 @@ bool FFmpegLibs::InitLibs(const wxString &libpath_format, bool WXUNUSED(showerr)
bool gotError = false; bool gotError = false;
// Check for a monolithic avformat // Check for a monolithic avformat
avformat = new wxDynamicLibrary(); avformat = std::make_unique<wxDynamicLibrary>();
wxLogMessage(wxT("Checking for monolithic avformat from '%s'."), nameFull.c_str()); wxLogMessage(wxT("Checking for monolithic avformat from '%s'."), nameFull.c_str());
gotError = !avformat->Load(nameFull, wxDL_LAZY); gotError = !avformat->Load(nameFull, wxDL_LAZY);
@ -790,8 +786,8 @@ bool FFmpegLibs::InitLibs(const wxString &libpath_format, bool WXUNUSED(showerr)
avcodec_filename = FileNames::PathFromAddr(avformat->GetSymbol(wxT("avcodec_version"))); avcodec_filename = FileNames::PathFromAddr(avformat->GetSymbol(wxT("avcodec_version")));
if (avutil_filename.GetFullPath().IsSameAs(nameFull)) { if (avutil_filename.GetFullPath().IsSameAs(nameFull)) {
if (avcodec_filename.GetFullPath().IsSameAs(nameFull)) { if (avcodec_filename.GetFullPath().IsSameAs(nameFull)) {
util = avformat; util = avformat.get();
codec = avformat; codec = avformat.get();
} }
} }
if (!avcodec_filename.FileExists()) { if (!avcodec_filename.FileExists()) {
@ -817,13 +813,13 @@ bool FFmpegLibs::InitLibs(const wxString &libpath_format, bool WXUNUSED(showerr)
const wxString avutil_filename_full{ avutil_filename.GetFullPath() }; const wxString avutil_filename_full{ avutil_filename.GetFullPath() };
if (!util) { if (!util) {
avutil = util = new wxDynamicLibrary(); util = (avutil = std::make_unique<wxDynamicLibrary>()).get();
wxLogMessage(wxT("Loading avutil from '%s'."), avutil_filename_full.c_str()); wxLogMessage(wxT("Loading avutil from '%s'."), avutil_filename_full.c_str());
util->Load(avutil_filename_full, wxDL_LAZY); util->Load(avutil_filename_full, wxDL_LAZY);
} }
if (!codec) { if (!codec) {
avcodec = codec = new wxDynamicLibrary(); codec = (avcodec = std::make_unique<wxDynamicLibrary>()).get();
wxLogMessage(wxT("Loading avcodec from '%s'."), avcodec_filename_full.c_str()); wxLogMessage(wxT("Loading avcodec from '%s'."), avcodec_filename_full.c_str());
codec->Load(avcodec_filename_full, wxDL_LAZY); codec->Load(avcodec_filename_full, wxDL_LAZY);
} }
@ -967,23 +963,10 @@ bool FFmpegLibs::InitLibs(const wxString &libpath_format, bool WXUNUSED(showerr)
void FFmpegLibs::FreeLibs() void FFmpegLibs::FreeLibs()
{ {
if (avformat != NULL) { avformat.reset();
delete avformat; avcodec.reset();
avformat = NULL; avutil.reset();
}
if (avcodec != NULL) {
delete avcodec;
avcodec = NULL;
}
if (avutil != NULL) {
delete avutil;
avutil = NULL;
}
mLibsLoaded = false; mLibsLoaded = false;
return; return;
} }

View File

@ -376,9 +376,7 @@ private:
wxString mAVUtilVersion; wxString mAVUtilVersion;
///! wx interfaces for dynamic libraries ///! wx interfaces for dynamic libraries
wxDynamicLibrary *avformat; std::unique_ptr<wxDynamicLibrary> avformat, avcodec, avutil;
wxDynamicLibrary *avcodec;
wxDynamicLibrary *avutil;
///! true if libraries are loaded, false otherwise ///! true if libraries are loaded, false otherwise
bool mLibsLoaded; bool mLibsLoaded;

View File

@ -53,13 +53,13 @@ function.
#if defined(USE_FFMPEG) #if defined(USE_FFMPEG)
extern FFmpegLibs *FFmpegLibsInst; extern FFmpegLibs *FFmpegLibsInst();
static bool CheckFFmpegPresence(bool quiet = false) static bool CheckFFmpegPresence(bool quiet = false)
{ {
bool result = true; bool result = true;
PickFFmpegLibs(); PickFFmpegLibs();
if (!FFmpegLibsInst->ValidLibsLoaded()) if (!FFmpegLibsInst()->ValidLibsLoaded())
{ {
if (!quiet) if (!quiet)
{ {
@ -185,14 +185,14 @@ ExportFFmpeg::ExportFFmpeg()
mSupportsUTF8 = true; mSupportsUTF8 = true;
PickFFmpegLibs(); // DropFFmpegLibs() call is in ExportFFmpeg destructor PickFFmpegLibs(); // DropFFmpegLibs() call is in ExportFFmpeg destructor
int avfver = FFmpegLibsInst->ValidLibsLoaded() ? avformat_version() : 0; int avfver = FFmpegLibsInst()->ValidLibsLoaded() ? avformat_version() : 0;
int newfmt; int newfmt;
// Adds export types from the export type list // Adds export types from the export type list
for (newfmt = 0; newfmt < FMT_LAST; newfmt++) for (newfmt = 0; newfmt < FMT_LAST; newfmt++)
{ {
wxString shortname(ExportFFmpegOptions::fmts[newfmt].shortname); wxString shortname(ExportFFmpegOptions::fmts[newfmt].shortname);
//Don't hide export types when there's no av-libs, and don't hide FMT_OTHER //Don't hide export types when there's no av-libs, and don't hide FMT_OTHER
if (newfmt < FMT_OTHER && FFmpegLibsInst->ValidLibsLoaded()) if (newfmt < FMT_OTHER && FFmpegLibsInst()->ValidLibsLoaded())
{ {
// Format/Codec support is compiled in? // Format/Codec support is compiled in?
AVOutputFormat *avoformat = av_guess_format(shortname.mb_str(), NULL, NULL); AVOutputFormat *avoformat = av_guess_format(shortname.mb_str(), NULL, NULL);
@ -249,8 +249,8 @@ bool ExportFFmpeg::CheckFileName(wxFileName & WXUNUSED(filename), int WXUNUSED(f
// Show "Locate FFmpeg" dialog // Show "Locate FFmpeg" dialog
if (!CheckFFmpegPresence(true)) if (!CheckFFmpegPresence(true))
{ {
FFmpegLibsInst->FindLibs(NULL); FFmpegLibsInst()->FindLibs(NULL);
FFmpegLibsInst->FreeLibs(); FFmpegLibsInst()->FreeLibs();
return LoadFFmpeg(true); return LoadFFmpeg(true);
} }
@ -267,9 +267,9 @@ bool ExportFFmpeg::Init(const char *shortname, AudacityProject *project, const T
std::unique_ptr<ExportFFmpeg, decltype(deleter)> cleanup{ this, deleter }; std::unique_ptr<ExportFFmpeg, decltype(deleter)> cleanup{ this, deleter };
int err; int err;
//FFmpegLibsInst->LoadLibs(NULL,true); //Loaded at startup or from Prefs now //FFmpegLibsInst()->LoadLibs(NULL,true); //Loaded at startup or from Prefs now
if (!FFmpegLibsInst->ValidLibsLoaded()) if (!FFmpegLibsInst()->ValidLibsLoaded())
return false; return false;
av_log_set_callback(av_log_wx_callback); av_log_set_callback(av_log_wx_callback);

View File

@ -65,7 +65,7 @@
#if defined(USE_FFMPEG) #if defined(USE_FFMPEG)
extern FFmpegLibs *FFmpegLibsInst; extern FFmpegLibs *FFmpegLibsInst();
/// This construction defines a enumeration of UI element IDs, and a static /// This construction defines a enumeration of UI element IDs, and a static
/// array of their string representations (this way they're always synchronized). /// array of their string representations (this way they're always synchronized).
@ -455,10 +455,10 @@ void ExportFFmpegCustomOptions::OnOpen(wxCommandEvent & WXUNUSED(evt))
{ {
// Show "Locate FFmpeg" dialog // Show "Locate FFmpeg" dialog
PickFFmpegLibs(); PickFFmpegLibs();
if (!FFmpegLibsInst->ValidLibsLoaded()) if (!FFmpegLibsInst()->ValidLibsLoaded())
{ {
FFmpegLibsInst->FindLibs(NULL); FFmpegLibsInst()->FindLibs(NULL);
FFmpegLibsInst->FreeLibs(); FFmpegLibsInst()->FreeLibs();
if (!LoadFFmpeg(true)) if (!LoadFFmpeg(true))
{ {
return; return;
@ -1309,12 +1309,12 @@ ExportFFmpegOptions::ExportFFmpegOptions(wxWindow *parent)
SetName(GetTitle()); SetName(GetTitle());
ShuttleGui S(this, eIsCreatingFromPrefs); ShuttleGui S(this, eIsCreatingFromPrefs);
PickFFmpegLibs(); PickFFmpegLibs();
//FFmpegLibsInst->LoadLibs(NULL,true); //Loaded at startup or from Prefs now //FFmpegLibsInst()->LoadLibs(NULL,true); //Loaded at startup or from Prefs now
mPresets = new FFmpegPresets(); mPresets = new FFmpegPresets();
mPresetNames = mPresets->GetPresetList(); mPresetNames = mPresets->GetPresetList();
if (FFmpegLibsInst->ValidLibsLoaded()) if (FFmpegLibsInst()->ValidLibsLoaded())
{ {
FetchFormatList(); FetchFormatList();
FetchCodecList(); FetchCodecList();

View File

@ -162,7 +162,7 @@ static const wxChar *exts[] =
#include "../ondemand/ODDecodeFFmpegTask.h" #include "../ondemand/ODDecodeFFmpegTask.h"
#endif #endif
extern FFmpegLibs *FFmpegLibsInst; extern FFmpegLibs *FFmpegLibsInst();
class FFmpegImportFileHandle; class FFmpegImportFileHandle;
@ -309,7 +309,7 @@ std::unique_ptr<ImportFileHandle> FFmpegImportPlugin::Open(const wxString &filen
//insdead of usual wxMessageBox() //insdead of usual wxMessageBox()
bool newsession = false; bool newsession = false;
gPrefs->Read(wxT("/NewImportingSession"), &newsession); gPrefs->Read(wxT("/NewImportingSession"), &newsession);
if (!FFmpegLibsInst->ValidLibsLoaded()) if (!FFmpegLibsInst()->ValidLibsLoaded())
{ {
int dontShowDlg; int dontShowDlg;
gPrefs->Read(wxT("/FFmpeg/NotFoundDontShow"),&dontShowDlg,0); gPrefs->Read(wxT("/FFmpeg/NotFoundDontShow"),&dontShowDlg,0);
@ -321,7 +321,7 @@ std::unique_ptr<ImportFileHandle> FFmpegImportPlugin::Open(const wxString &filen
} }
} }
} }
if (!FFmpegLibsInst->ValidLibsLoaded()) if (!FFmpegLibsInst()->ValidLibsLoaded())
{ {
return nullptr; return nullptr;
} }
@ -352,9 +352,10 @@ FFmpegImportFileHandle::FFmpegImportFileHandle(const wxString & name)
bool FFmpegImportFileHandle::Init() bool FFmpegImportFileHandle::Init()
{ {
//FFmpegLibsInst->LoadLibs(NULL,false); //Loaded at startup or from Prefs now //FFmpegLibsInst()->LoadLibs(NULL,false); //Loaded at startup or from Prefs now
if (!FFmpegLibsInst->ValidLibsLoaded()) return false; if (!FFmpegLibsInst()->ValidLibsLoaded())
return false;
av_log_set_callback(av_log_wx_callback); av_log_set_callback(av_log_wx_callback);

View File

@ -30,7 +30,7 @@
#include "../import/ImportFFmpeg.h" #include "../import/ImportFFmpeg.h"
extern FFmpegLibs *FFmpegLibsInst; extern FFmpegLibs *FFmpegLibsInst();
#include "ODDecodeFFmpegTask.h" #include "ODDecodeFFmpegTask.h"

View File

@ -192,7 +192,7 @@ void LibraryPrefs::SetFFmpegVersionText()
void LibraryPrefs::OnFFmpegFindButton(wxCommandEvent & WXUNUSED(event)) void LibraryPrefs::OnFFmpegFindButton(wxCommandEvent & WXUNUSED(event))
{ {
#ifdef USE_FFMPEG #ifdef USE_FFMPEG
FFmpegLibs* FFmpegLibsInst = PickFFmpegLibs(); FFmpegLibs* FFmpegLibsPtr = PickFFmpegLibs();
bool showerrs = bool showerrs =
#if defined(__WXDEBUG__) #if defined(__WXDEBUG__)
true; true;
@ -200,7 +200,7 @@ void LibraryPrefs::OnFFmpegFindButton(wxCommandEvent & WXUNUSED(event))
false; false;
#endif #endif
FFmpegLibsInst->FreeLibs(); FFmpegLibsPtr->FreeLibs();
// Load the libs ('true' means that all errors will be shown) // Load the libs ('true' means that all errors will be shown)
bool locate = !LoadFFmpeg(showerrs); bool locate = !LoadFFmpeg(showerrs);
@ -216,8 +216,8 @@ void LibraryPrefs::OnFFmpegFindButton(wxCommandEvent & WXUNUSED(event))
if (locate) { if (locate) {
// Show "Locate FFmpeg" dialog // Show "Locate FFmpeg" dialog
FFmpegLibsInst->FindLibs(this); FFmpegLibsPtr->FindLibs(this);
FFmpegLibsInst->FreeLibs(); FFmpegLibsPtr->FreeLibs();
LoadFFmpeg(showerrs); LoadFFmpeg(showerrs);
} }
SetFFmpegVersionText(); SetFFmpegVersionText();