History

avcodec/pcm: Don't allocate LUT when unused

Instead use a dedicated context for the few decoders that use the LUT. Also use a dedicated init function for them.

Andreas Rheinhardt committed 1 year ago

Commit e76daf61 parent f2168f41 file · +62 −36Browse files

Showing the changes to libavcodec (1 of 1 file). Show all

Unified · Split

libavcodec/pcm.c+62−36
@@ -244,31 +244,17 @@ static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 243 unmodified lines
244244
245245 typedef struct PCMDecode {
246246 int sample_size;
247− short table[256];
248247 void (*vector_fmul_scalar)(float *dst, const float *src, float mul,
249248 int len);
250249 float scale;
251250 } PCMDecode;
252251
253−static av_cold int pcm_decode_init(AVCodecContext *avctx)
252+static av_cold av_unused int pcm_decode_init(AVCodecContext *avctx)
254253 {
255254 PCMDecode *s = avctx->priv_data;
256255 AVFloatDSPContext *fdsp;
257− int i;
258256
259257 switch (avctx->codec_id) {
260− case AV_CODEC_ID_PCM_ALAW:
261− for (i = 0; i < 256; i++)
262− s->table[i] = alaw2linear(i);
263− break;
264− case AV_CODEC_ID_PCM_MULAW:
265− for (i = 0; i < 256; i++)
266− s->table[i] = ulaw2linear(i);
267− break;
268− case AV_CODEC_ID_PCM_VIDC:
269− for (i = 0; i < 256; i++)
270− s->table[i] = vidc2linear(i);
271− break;
272258 case AV_CODEC_ID_PCM_F16LE:
273259 case AV_CODEC_ID_PCM_F24LE:
274260 if (avctx->bits_per_coded_sample < 1 || avctx->bits_per_coded_sample > 24)
@@ -299,6 +285,36 @@ static av_cold int pcm_decode_init(AVCodecContext *avctx) · 24 unmodified lines
299285 return 0;
300286 }
301287
288+typedef struct PCMLUTDecode {
289+ PCMDecode base;
290+ int16_t table[256];
291+} PCMLUTDecode;
292+
293+static av_cold av_unused int pcm_lut_decode_init(AVCodecContext *avctx)
294+{
295+ PCMLUTDecode *s = avctx->priv_data;
296+
297+ switch (avctx->codec_id) {
298+ case AV_CODEC_ID_PCM_ALAW:
299+ for (int i = 0; i < 256; i++)
300+ s->table[i] = alaw2linear(i);
301+ break;
302+ case AV_CODEC_ID_PCM_MULAW:
303+ for (int i = 0; i < 256; i++)
304+ s->table[i] = ulaw2linear(i);
305+ break;
306+ case AV_CODEC_ID_PCM_VIDC:
307+ for (int i = 0; i < 256; i++)
308+ s->table[i] = vidc2linear(i);
309+ break;
310+ }
311+
312+ avctx->sample_fmt = AV_SAMPLE_FMT_S16;
313+ s->base.sample_size = 1;
314+
315+ return 0;
316+}
317+
302318 /**
303319 * Read PCM samples macro
304320 * @param size Data size of native machine format
@@ -499,12 +515,14 @@ static int pcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 194 unmodified lines
499515 break;
500516 case AV_CODEC_ID_PCM_ALAW:
501517 case AV_CODEC_ID_PCM_MULAW:
502− case AV_CODEC_ID_PCM_VIDC:
503− for (; n > 0; n--) {
504− AV_WN16A(samples, s->table[*src++]);
505− samples += 2;
506− }
518+ case AV_CODEC_ID_PCM_VIDC: {
519+ const int16_t *const lut = ((PCMLUTDecode*)avctx->priv_data)->table;
520+ int16_t *restrict samples_16 = (int16_t*)samples;
521+
522+ for (; n > 0; n--)
523+ *samples_16++ = lut[*src++];
507524 break;
525+ }
508526 case AV_CODEC_ID_PCM_LXF:
509527 {
510528 int i;
@@ -567,36 +585,44 @@ const FFCodec ff_ ## name_ ## _encoder = { \ · 56 unmodified lines
567585 PCM_ENCODER_3(CONFIG_PCM_ ## id ## _ENCODER, AV_CODEC_ID_PCM_ ## id, \
568586 AV_SAMPLE_FMT_ ## sample_fmt, pcm_ ## name, long_name)
569587
570−#define PCM_DECODER_0(id, sample_fmt, name, long_name)
571−#define PCM_DECODER_1(id_, sample_fmt_, name_, long_name_) \
588+#define PCM_DECODER_0(id, sample_fmt, name, long_name, Context, init_func)
589+#define PCM_DECODER_1(id_, sample_fmt, name_, long_name, Context, init_func) \
572590 const FFCodec ff_ ## name_ ## _decoder = { \
573591 .p.name = #name_, \
574− CODEC_LONG_NAME(long_name_), \
592+ CODEC_LONG_NAME(long_name), \
575593 .p.type = AVMEDIA_TYPE_AUDIO, \
576594 .p.id = id_, \
577− .priv_data_size = sizeof(PCMDecode), \
578− .init = pcm_decode_init, \
595+ .priv_data_size = sizeof(Context), \
596+ .init = init_func, \
579597 FF_CODEC_DECODE_CB(pcm_decode_frame), \
580598 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE, \
581− CODEC_SAMPLEFMTS(sample_fmt_), \
599+ CODEC_SAMPLEFMTS(sample_fmt), \
582600 }
583601
584−#define PCM_DECODER_2(cf, id, sample_fmt, name, long_name) \
585− PCM_DECODER_ ## cf(id, sample_fmt, name, long_name)
586−#define PCM_DECODER_3(cf, id, sample_fmt, name, long_name) \
587− PCM_DECODER_2(cf, id, sample_fmt, name, long_name)
588−#define PCM_DECODER(id, sample_fmt, name, long_name) \
602+#define PCM_DECODER_2(cf, id, sample_fmt, name, long_name, Context, init_func) \
603+ PCM_DECODER_ ## cf(id, sample_fmt, name, long_name, Context, init_func)
604+#define PCM_DECODER_3(cf, id, sample_fmt, name, long_name, Context, init_func) \
605+ PCM_DECODER_2(cf, id, sample_fmt, name, long_name, Context, init_func)
606+#define PCM_DEC_EXT(id, sample_fmt, name, long_name, Context, init_func) \
589607 PCM_DECODER_3(CONFIG_PCM_ ## id ## _DECODER, AV_CODEC_ID_PCM_ ## id, \
590− AV_SAMPLE_FMT_ ## sample_fmt, pcm_ ## name, long_name)
608+ AV_SAMPLE_FMT_ ## sample_fmt, pcm_ ## name, long_name, \
609+ Context, init_func)
610+
611+#define PCM_DECODER(id, sample_fmt, name, long_name) \
612+ PCM_DEC_EXT(id, sample_fmt, name, long_name, PCMDecode, pcm_decode_init)
591613
592614 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
593615 PCM_ENCODER(id, sample_fmt_, name, long_name_); \
594616 PCM_DECODER(id, sample_fmt_, name, long_name_)
595617
618+#define PCM_CODEC_EXT(id, sample_fmt, name, long_name, DecContext, dec_init_func) \
619+ PCM_DEC_EXT(id, sample_fmt, name, long_name, DecContext, dec_init_func); \
620+ PCM_ENCODER(id, sample_fmt, name, long_name)
621+
596622 /* Note: Do not forget to add new entries to the Makefile as well. */
597623 // AV_CODEC_ID_* pcm_* name
598−// AV_SAMPLE_FMT_* long name
599−PCM_CODEC (ALAW, S16, alaw, "PCM A-law / G.711 A-law");
624+// AV_SAMPLE_FMT_* long name DecodeContext decode init func
625+PCM_CODEC_EXT(ALAW, S16, alaw, "PCM A-law / G.711 A-law", PCMLUTDecode, pcm_lut_decode_init);
600626 PCM_DECODER (F16LE, FLT, f16le, "PCM 16.8 floating point little-endian");
601627 PCM_DECODER (F24LE, FLT, f24le, "PCM 24.0 floating point little-endian");
602628 PCM_CODEC (F32BE, FLT, f32be, "PCM 32-bit floating point big-endian");
@@ -604,7 +630,7 @@ PCM_CODEC (F32LE, FLT, f32le, "PCM 32-bit floating point little · 1 unmodified line
604630 PCM_CODEC (F64BE, DBL, f64be, "PCM 64-bit floating point big-endian");
605631 PCM_CODEC (F64LE, DBL, f64le, "PCM 64-bit floating point little-endian");
606632 PCM_DECODER (LXF, S32P,lxf, "PCM signed 20-bit little-endian planar");
607−PCM_CODEC (MULAW, S16, mulaw, "PCM mu-law / G.711 mu-law");
633+PCM_CODEC_EXT(MULAW, S16, mulaw, "PCM mu-law / G.711 mu-law", PCMLUTDecode, pcm_lut_decode_init);
608634 PCM_CODEC (S8, U8, s8, "PCM signed 8-bit");
609635 PCM_CODEC (S8_PLANAR, U8P, s8_planar, "PCM signed 8-bit planar");
610636 PCM_CODEC (S16BE, S16, s16be, "PCM signed 16-bit big-endian");
@@ -627,5 +653,5 @@ PCM_CODEC (U32BE, S32, u32be, "PCM unsigned 32-bit big-endian") · 16 unmodified lines
627653 PCM_CODEC (U32LE, S32, u32le, "PCM unsigned 32-bit little-endian");
628654 PCM_CODEC (S64BE, S64, s64be, "PCM signed 64-bit big-endian");
629655 PCM_CODEC (S64LE, S64, s64le, "PCM signed 64-bit little-endian");
630−PCM_CODEC (VIDC, S16, vidc, "PCM Archimedes VIDC");
656+PCM_CODEC_EXT(VIDC, S16, vidc, "PCM Archimedes VIDC", PCMLUTDecode, pcm_lut_decode_init);
631657 PCM_DECODER (SGA, U8, sga, "PCM SGA");

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t