mirror of
https://github.com/cookiengineer/audacity
synced 2025-10-13 22:21:11 +02:00
Remove naked malloc (or similar) and free in: FFmpeg
This commit is contained in:
@@ -386,10 +386,10 @@ int import_ffmpeg_decode_frame(streamContext *sc, bool flushing)
|
|||||||
}
|
}
|
||||||
|
|
||||||
sc->m_samplefmt = sc->m_codecCtx->sample_fmt;
|
sc->m_samplefmt = sc->m_codecCtx->sample_fmt;
|
||||||
sc->m_samplesize = av_get_bytes_per_sample(sc->m_samplefmt);
|
sc->m_samplesize = static_cast<size_t>(av_get_bytes_per_sample(sc->m_samplefmt));
|
||||||
|
|
||||||
int channels = sc->m_codecCtx->channels;
|
int channels = sc->m_codecCtx->channels;
|
||||||
unsigned int newsize = sc->m_samplesize * frame->nb_samples * channels;
|
auto newsize = sc->m_samplesize * frame->nb_samples * channels;
|
||||||
sc->m_decodedAudioSamplesValidSiz = newsize;
|
sc->m_decodedAudioSamplesValidSiz = newsize;
|
||||||
// Reallocate the audio sample buffer if it's smaller than the frame size.
|
// Reallocate the audio sample buffer if it's smaller than the frame size.
|
||||||
if (newsize > sc->m_decodedAudioSamplesSiz )
|
if (newsize > sc->m_decodedAudioSamplesSiz )
|
||||||
|
@@ -982,13 +982,13 @@ struct streamContext
|
|||||||
int m_frameValid{}; // is m_decodedVideoFrame/m_decodedAudioSamples valid?
|
int m_frameValid{}; // is m_decodedVideoFrame/m_decodedAudioSamples valid?
|
||||||
AVMallocHolder<uint8_t> m_decodedAudioSamples; // decoded audio samples stored here
|
AVMallocHolder<uint8_t> m_decodedAudioSamples; // decoded audio samples stored here
|
||||||
unsigned int m_decodedAudioSamplesSiz{}; // current size of m_decodedAudioSamples
|
unsigned int m_decodedAudioSamplesSiz{}; // current size of m_decodedAudioSamples
|
||||||
int m_decodedAudioSamplesValidSiz{}; // # valid bytes in m_decodedAudioSamples
|
size_t m_decodedAudioSamplesValidSiz{}; // # valid bytes in m_decodedAudioSamples
|
||||||
int m_initialchannels{}; // number of channels allocated when we begin the importing. Assumes that number of channels doesn't change on the fly.
|
int m_initialchannels{}; // number of channels allocated when we begin the importing. Assumes that number of channels doesn't change on the fly.
|
||||||
|
|
||||||
int m_samplesize{}; // input sample size in bytes
|
size_t m_samplesize{}; // input sample size in bytes
|
||||||
AVSampleFormat m_samplefmt{ AV_SAMPLE_FMT_NONE }; // input sample format
|
AVSampleFormat m_samplefmt{ AV_SAMPLE_FMT_NONE }; // input sample format
|
||||||
|
|
||||||
int m_osamplesize{}; // output sample size in bytes
|
size_t m_osamplesize{}; // output sample size in bytes
|
||||||
sampleFormat m_osamplefmt{ floatSample }; // output sample format
|
sampleFormat m_osamplefmt{ floatSample }; // output sample format
|
||||||
|
|
||||||
streamContext() { memset(this, 0, sizeof(*this)); }
|
streamContext() { memset(this, 0, sizeof(*this)); }
|
||||||
|
@@ -732,13 +732,10 @@ ProgressResult FFmpegImportFileHandle::WriteData(streamContext *sc)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Allocate the buffer to store audio.
|
// Allocate the buffer to store audio.
|
||||||
int insamples = sc->m_decodedAudioSamplesValidSiz / sc->m_samplesize;
|
auto insamples = sc->m_decodedAudioSamplesValidSiz / sc->m_samplesize;
|
||||||
int nChannels = sc->m_stream->codec->channels < sc->m_initialchannels ? sc->m_stream->codec->channels : sc->m_initialchannels;
|
size_t nChannels = std::min(sc->m_stream->codec->channels, sc->m_initialchannels);
|
||||||
uint8_t **tmp = (uint8_t **) malloc(sizeof(uint8_t *) * nChannels);
|
|
||||||
for (int chn = 0; chn < nChannels; chn++)
|
ArraysOf<uint8_t> tmp{ nChannels, sc->m_osamplesize * (insamples / nChannels) };
|
||||||
{
|
|
||||||
tmp[chn] = (uint8_t *) malloc(sc->m_osamplesize * (insamples / nChannels));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Separate the channels and convert input sample format to 16-bit
|
// Separate the channels and convert input sample format to 16-bit
|
||||||
uint8_t *in = sc->m_decodedAudioSamples.get();
|
uint8_t *in = sc->m_decodedAudioSamples.get();
|
||||||
@@ -754,36 +751,31 @@ ProgressResult FFmpegImportFileHandle::WriteData(streamContext *sc)
|
|||||||
{
|
{
|
||||||
case AV_SAMPLE_FMT_U8:
|
case AV_SAMPLE_FMT_U8:
|
||||||
case AV_SAMPLE_FMT_U8P:
|
case AV_SAMPLE_FMT_U8P:
|
||||||
((int16_t *)tmp[chn])[index] = (int16_t) (*(uint8_t *)in - 0x80) << 8;
|
((int16_t *)tmp[chn].get())[index] = (int16_t) (*(uint8_t *)in - 0x80) << 8;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AV_SAMPLE_FMT_S16:
|
case AV_SAMPLE_FMT_S16:
|
||||||
case AV_SAMPLE_FMT_S16P:
|
case AV_SAMPLE_FMT_S16P:
|
||||||
((int16_t *)tmp[chn])[index] = (int16_t) *(int16_t *)in;
|
((int16_t *)tmp[chn].get())[index] = (int16_t) *(int16_t *)in;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AV_SAMPLE_FMT_S32:
|
case AV_SAMPLE_FMT_S32:
|
||||||
case AV_SAMPLE_FMT_S32P:
|
case AV_SAMPLE_FMT_S32P:
|
||||||
((float *)tmp[chn])[index] = (float) *(int32_t *)in * (1.0 / (1u << 31));
|
((float *)tmp[chn].get())[index] = (float) *(int32_t *)in * (1.0 / (1u << 31));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AV_SAMPLE_FMT_FLT:
|
case AV_SAMPLE_FMT_FLT:
|
||||||
case AV_SAMPLE_FMT_FLTP:
|
case AV_SAMPLE_FMT_FLTP:
|
||||||
((float *)tmp[chn])[index] = (float) *(float *)in;
|
((float *)tmp[chn].get())[index] = (float) *(float *)in;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AV_SAMPLE_FMT_DBL:
|
case AV_SAMPLE_FMT_DBL:
|
||||||
case AV_SAMPLE_FMT_DBLP:
|
case AV_SAMPLE_FMT_DBLP:
|
||||||
((float *)tmp[chn])[index] = (float) *(double *)in;
|
((float *)tmp[chn].get())[index] = (float) *(double *)in;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
wxLogError(wxT("Stream %d has unrecognized sample format %d."), streamid, sc->m_samplefmt);
|
wxLogError(wxT("Stream %d has unrecognized sample format %d."), streamid, sc->m_samplefmt);
|
||||||
for (int chn=0; chn < nChannels; chn++)
|
|
||||||
{
|
|
||||||
free(tmp[chn]);
|
|
||||||
}
|
|
||||||
free(tmp);
|
|
||||||
return ProgressResult::Success;
|
return ProgressResult::Success;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -798,12 +790,9 @@ ProgressResult FFmpegImportFileHandle::WriteData(streamContext *sc)
|
|||||||
auto iter2 = iter->begin();
|
auto iter2 = iter->begin();
|
||||||
for (int chn=0; chn < nChannels; ++iter2, ++chn)
|
for (int chn=0; chn < nChannels; ++iter2, ++chn)
|
||||||
{
|
{
|
||||||
iter2->get()->Append((samplePtr)tmp[chn],sc->m_osamplefmt,index);
|
iter2->get()->Append((samplePtr)tmp[chn].get(), sc->m_osamplefmt, index);
|
||||||
free(tmp[chn]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(tmp);
|
|
||||||
|
|
||||||
// Try to update the progress indicator (and see if user wants to cancel)
|
// Try to update the progress indicator (and see if user wants to cancel)
|
||||||
auto updateResult = ProgressResult::Success;
|
auto updateResult = ProgressResult::Success;
|
||||||
int64_t filesize = avio_size(mFormatContext->pb);
|
int64_t filesize = avio_size(mFormatContext->pb);
|
||||||
|
Reference in New Issue
Block a user