1
0
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:
Paul Licameli
2016-08-16 13:53:21 -04:00
parent 58574f2f78
commit 53b9869268
3 changed files with 15 additions and 26 deletions

View File

@@ -386,10 +386,10 @@ int import_ffmpeg_decode_frame(streamContext *sc, bool flushing)
}
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;
unsigned int newsize = sc->m_samplesize * frame->nb_samples * channels;
auto newsize = sc->m_samplesize * frame->nb_samples * channels;
sc->m_decodedAudioSamplesValidSiz = newsize;
// Reallocate the audio sample buffer if it's smaller than the frame size.
if (newsize > sc->m_decodedAudioSamplesSiz )

View File

@@ -982,13 +982,13 @@ struct streamContext
int m_frameValid{}; // is m_decodedVideoFrame/m_decodedAudioSamples valid?
AVMallocHolder<uint8_t> m_decodedAudioSamples; // decoded audio samples stored here
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_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
int m_osamplesize{}; // output sample size in bytes
size_t m_osamplesize{}; // output sample size in bytes
sampleFormat m_osamplefmt{ floatSample }; // output sample format
streamContext() { memset(this, 0, sizeof(*this)); }

View File

@@ -732,13 +732,10 @@ ProgressResult FFmpegImportFileHandle::WriteData(streamContext *sc)
}
// Allocate the buffer to store audio.
int 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;
uint8_t **tmp = (uint8_t **) malloc(sizeof(uint8_t *) * nChannels);
for (int chn = 0; chn < nChannels; chn++)
{
tmp[chn] = (uint8_t *) malloc(sc->m_osamplesize * (insamples / nChannels));
}
auto insamples = sc->m_decodedAudioSamplesValidSiz / sc->m_samplesize;
size_t nChannels = std::min(sc->m_stream->codec->channels, sc->m_initialchannels);
ArraysOf<uint8_t> tmp{ nChannels, sc->m_osamplesize * (insamples / nChannels) };
// Separate the channels and convert input sample format to 16-bit
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_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;
case AV_SAMPLE_FMT_S16:
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;
case AV_SAMPLE_FMT_S32:
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;
case AV_SAMPLE_FMT_FLT:
case AV_SAMPLE_FMT_FLTP:
((float *)tmp[chn])[index] = (float) *(float *)in;
((float *)tmp[chn].get())[index] = (float) *(float *)in;
break;
case AV_SAMPLE_FMT_DBL:
case AV_SAMPLE_FMT_DBLP:
((float *)tmp[chn])[index] = (float) *(double *)in;
((float *)tmp[chn].get())[index] = (float) *(double *)in;
break;
default:
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;
break;
}
@@ -798,12 +790,9 @@ ProgressResult FFmpegImportFileHandle::WriteData(streamContext *sc)
auto iter2 = iter->begin();
for (int chn=0; chn < nChannels; ++iter2, ++chn)
{
iter2->get()->Append((samplePtr)tmp[chn],sc->m_osamplefmt,index);
free(tmp[chn]);
iter2->get()->Append((samplePtr)tmp[chn].get(), sc->m_osamplefmt, index);
}
free(tmp);
// Try to update the progress indicator (and see if user wants to cancel)
auto updateResult = ProgressResult::Success;
int64_t filesize = avio_size(mFormatContext->pb);