#include "libavutil/common.h"
#include "libavutil/float_dsp.h"
#include "libavutil/mathematics.h"
+#include "libavutil/thread.h"
#include "audio_frame_queue.h"
#include "avcodec.h"
ff_mdct_end(&s->mdct_ctx);
- if (s->avctx->trellis) {
- av_freep(&s->opt);
- av_freep(&s->path);
- }
+ av_freep(&s->opt);
+ av_freep(&s->path);
ff_af_queue_close(&s->afq);
av_freep(&s->fdsp);
return 0;
}
+static av_cold void nellymoser_init_static(void)
+{
+ /* faster way of doing
+ for (int i = 0; i < POW_TABLE_SIZE; i++)
+ pow_table[i] = 2^(-i / 2048.0 - 3.0 + POW_TABLE_OFFSET); */
+ pow_table[0] = 1;
+ pow_table[1024] = M_SQRT1_2;
+ for (int i = 1; i < 513; i++) {
+ double tmp = exp2(-i / 2048.0);
+ pow_table[i] = tmp;
+ pow_table[1024-i] = M_SQRT1_2 / tmp;
+ pow_table[1024+i] = tmp * M_SQRT1_2;
+ pow_table[2048-i] = 0.5 / tmp;
+ }
+ /* Generate overlap window */
+ ff_init_ff_sine_windows(7);
+}
+
static av_cold int encode_init(AVCodecContext *avctx)
{
+ static AVOnce init_static_once = AV_ONCE_INIT;
NellyMoserEncodeContext *s = avctx->priv_data;
- int i, ret;
+ int ret;
if (avctx->channels != 1) {
av_log(avctx, AV_LOG_ERROR, "Nellymoser supports only 1 channel\n");
ff_af_queue_init(avctx, &s->afq);
s->avctx = avctx;
if ((ret = ff_mdct_init(&s->mdct_ctx, 8, 0, 32768.0)) < 0)
- goto error;
+ return ret;
s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
- if (!s->fdsp) {
- ret = AVERROR(ENOMEM);
- goto error;
- }
-
- /* Generate overlap window */
- ff_init_ff_sine_windows(7);
- /* faster way of doing
- for (i = 0; i < POW_TABLE_SIZE; i++)
- pow_table[i] = 2^(-i / 2048.0 - 3.0 + POW_TABLE_OFFSET); */
- pow_table[0] = 1;
- pow_table[1024] = M_SQRT1_2;
- for (i = 1; i < 513; i++) {
- double tmp = exp2(-i / 2048.0);
- pow_table[i] = tmp;
- pow_table[1024-i] = M_SQRT1_2 / tmp;
- pow_table[1024+i] = tmp * M_SQRT1_2;
- pow_table[2048-i] = 0.5 / tmp;
- }
+ if (!s->fdsp)
+ return AVERROR(ENOMEM);
if (s->avctx->trellis) {
s->opt = av_malloc(NELLY_BANDS * OPT_SIZE * sizeof(float ));
s->path = av_malloc(NELLY_BANDS * OPT_SIZE * sizeof(uint8_t));
- if (!s->opt || !s->path) {
- ret = AVERROR(ENOMEM);
- goto error;
- }
+ if (!s->opt || !s->path)
+ return AVERROR(ENOMEM);
}
+ ff_thread_once(&init_static_once, nellymoser_init_static);
+
return 0;
-error:
- encode_end(avctx);
- return ret;
}
#define find_best(val, table, LUT, LUT_add, LUT_size) \
return 0;
}
-AVCodec ff_nellymoser_encoder = {
+const AVCodec ff_nellymoser_encoder = {
.name = "nellymoser",
.long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
.type = AVMEDIA_TYPE_AUDIO,
.capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY,
.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
AV_SAMPLE_FMT_NONE },
+ .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
};