FFmpeg
pcm.c
Go to the documentation of this file.
1 /*
2  * PCM codecs
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * PCM codecs
25  */
26 
27 #include "config.h"
28 #include "config_components.h"
29 #include "libavutil/attributes.h"
30 #include "libavutil/float_dsp.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/reverse.h"
33 #include "libavutil/thread.h"
34 #include "avcodec.h"
35 #include "bytestream.h"
36 #include "codec_internal.h"
37 #include "decode.h"
38 #include "encode.h"
39 #include "pcm_tablegen.h"
40 
42 {
43  avctx->frame_size = 0;
44 #if !CONFIG_HARDCODED_TABLES
45  switch (avctx->codec->id) {
46 #if CONFIG_PCM_ALAW_ENCODER
47  case AV_CODEC_ID_PCM_ALAW: {
48  static AVOnce once_alaw = AV_ONCE_INIT;
49  ff_thread_once(&once_alaw, pcm_alaw_tableinit);
50  break;
51  }
52 #endif
53 #if CONFIG_PCM_MULAW_ENCODER
54  case AV_CODEC_ID_PCM_MULAW: {
55  static AVOnce once_mulaw = AV_ONCE_INIT;
56  ff_thread_once(&once_mulaw, pcm_ulaw_tableinit);
57  break;
58  }
59 #endif
60 #if CONFIG_PCM_VIDC_ENCODER
61  case AV_CODEC_ID_PCM_VIDC: {
62  static AVOnce once_vidc = AV_ONCE_INIT;
63  ff_thread_once(&once_vidc, pcm_vidc_tableinit);
64  break;
65  }
66 #endif
67  default:
68  break;
69  }
70 #endif
71 
73  avctx->block_align = avctx->ch_layout.nb_channels * avctx->bits_per_coded_sample / 8;
74  avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate;
75 
76  return 0;
77 }
78 
79 /**
80  * Write PCM samples macro
81  * @param type Datatype of native machine format
82  * @param endian bytestream_put_xxx() suffix
83  * @param src Source pointer (variable name)
84  * @param dst Destination pointer (variable name)
85  * @param n Total number of samples (variable name)
86  * @param shift Bitshift (bits)
87  * @param offset Sample value offset
88  */
89 #define ENCODE(type, endian, src, dst, n, shift, offset) \
90  samples_ ## type = (const type *) src; \
91  for (; n > 0; n--) { \
92  register type v = (*samples_ ## type++ >> shift) + offset; \
93  bytestream_put_ ## endian(&dst, v); \
94  }
95 
96 #define ENCODE_PLANAR(type, endian, dst, n, shift, offset) \
97  n /= avctx->ch_layout.nb_channels; \
98  for (c = 0; c < avctx->ch_layout.nb_channels; c++) { \
99  int i; \
100  samples_ ## type = (const type *) frame->extended_data[c]; \
101  for (i = n; i > 0; i--) { \
102  register type v = (*samples_ ## type++ >> shift) + offset; \
103  bytestream_put_ ## endian(&dst, v); \
104  } \
105  }
106 
107 static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
108  const AVFrame *frame, int *got_packet_ptr)
109 {
110  int n, c, sample_size, v, ret;
111  const short *samples;
112  unsigned char *dst;
113  const uint8_t *samples_uint8_t;
114  const int16_t *samples_int16_t;
115  const int32_t *samples_int32_t;
116  const int64_t *samples_int64_t;
117  const uint16_t *samples_uint16_t;
118  const uint32_t *samples_uint32_t;
119 
120  sample_size = av_get_bits_per_sample(avctx->codec->id) / 8;
121  n = frame->nb_samples * avctx->ch_layout.nb_channels;
122  samples = (const short *)frame->data[0];
123 
124  if ((ret = ff_get_encode_buffer(avctx, avpkt, n * sample_size, 0)) < 0)
125  return ret;
126  dst = avpkt->data;
127 
128  switch (avctx->codec->id) {
130  ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
131  break;
133  ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
134  break;
136  ENCODE(int32_t, le24, samples, dst, n, 8, 0)
137  break;
139  ENCODE_PLANAR(int32_t, le24, dst, n, 8, 0)
140  break;
142  ENCODE(int32_t, be24, samples, dst, n, 8, 0)
143  break;
145  ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
146  break;
148  ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
149  break;
151  for (; n > 0; n--) {
152  uint32_t tmp = ff_reverse[(*samples >> 8) & 0xff] +
153  (ff_reverse[*samples & 0xff] << 8);
154  tmp <<= 4; // sync flags would go here
155  bytestream_put_be24(&dst, tmp);
156  samples++;
157  }
158  break;
160  ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
161  break;
163  ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
164  break;
165  case AV_CODEC_ID_PCM_S8:
166  ENCODE(uint8_t, byte, samples, dst, n, 0, -128)
167  break;
169  ENCODE_PLANAR(uint8_t, byte, dst, n, 0, -128)
170  break;
171 #if HAVE_BIGENDIAN
174  ENCODE(int64_t, le64, samples, dst, n, 0, 0)
175  break;
178  ENCODE(int32_t, le32, samples, dst, n, 0, 0)
179  break;
181  ENCODE_PLANAR(int32_t, le32, dst, n, 0, 0)
182  break;
184  ENCODE(int16_t, le16, samples, dst, n, 0, 0)
185  break;
187  ENCODE_PLANAR(int16_t, le16, dst, n, 0, 0)
188  break;
194 #else
197  ENCODE(int64_t, be64, samples, dst, n, 0, 0)
198  break;
201  ENCODE(int32_t, be32, samples, dst, n, 0, 0)
202  break;
204  ENCODE(int16_t, be16, samples, dst, n, 0, 0)
205  break;
207  ENCODE_PLANAR(int16_t, be16, dst, n, 0, 0)
208  break;
214 #endif /* HAVE_BIGENDIAN */
215  case AV_CODEC_ID_PCM_U8:
216  memcpy(dst, samples, n * sample_size);
217  break;
218 #if HAVE_BIGENDIAN
220 #else
223 #endif /* HAVE_BIGENDIAN */
224  n /= avctx->ch_layout.nb_channels;
225  for (c = 0; c < avctx->ch_layout.nb_channels; c++) {
226  const uint8_t *src = frame->extended_data[c];
227  bytestream_put_buffer(&dst, src, n * sample_size);
228  }
229  break;
230 #if CONFIG_PCM_ALAW_DECODER || CONFIG_PCM_ALAW_ENCODER
232  for (; n > 0; n--) {
233  v = *samples++;
234  *dst++ = linear_to_alaw[(v + 32768) >> 2];
235  }
236  break;
237 #endif
238 #if CONFIG_PCM_MULAW_DECODER || CONFIG_PCM_MULAW_ENCODER
240  for (; n > 0; n--) {
241  v = *samples++;
242  *dst++ = linear_to_ulaw[(v + 32768) >> 2];
243  }
244  break;
245 #endif
246 #if CONFIG_PCM_VIDC_DECODER || CONFIG_PCM_VIDC_ENCODER
248  for (; n > 0; n--) {
249  v = *samples++;
250  *dst++ = linear_to_vidc[(v + 32768) >> 2];
251  }
252  break;
253 #endif
254  default:
255  return -1;
256  }
257 
258  *got_packet_ptr = 1;
259  return 0;
260 }
261 
262 typedef struct PCMDecode {
264 } PCMDecode;
265 
267 {
268  PCMDecode *s = avctx->priv_data;
269  static const struct {
270  enum AVCodecID codec_id;
271  int8_t sample_fmt;
272  uint8_t sample_size;
273  uint8_t bits_per_sample;
274  } codec_id_to_samplefmt[] = {
275  #define ENTRY(CODEC_ID, SAMPLE_FMT, BITS_PER_SAMPLE) \
276  { AV_CODEC_ID_PCM_ ## CODEC_ID, AV_SAMPLE_FMT_ ## SAMPLE_FMT, \
277  BITS_PER_SAMPLE / 8, BITS_PER_SAMPLE }
278  ENTRY(S8, U8, 8), ENTRY(S8_PLANAR, U8P, 8),
279  ENTRY(S16BE, S16, 16), ENTRY(S16BE_PLANAR, S16P, 16),
280  ENTRY(S16LE, S16, 16), ENTRY(S16LE_PLANAR, S16P, 16),
281  ENTRY(S24DAUD, S16, 24), ENTRY(S24BE, S32, 24),
282  ENTRY(S24LE, S32, 24), ENTRY(S24LE_PLANAR, S32P, 24),
283  ENTRY(S32BE, S32, 32), ENTRY(S32LE, S32, 32),
284  ENTRY(S32LE_PLANAR, S32P, 32),
285  ENTRY(S64BE, S64, 64), ENTRY(S64LE, S64, 64),
286  ENTRY(SGA, U8, 8), ENTRY(U8, U8, 8),
287  ENTRY(U16BE, S16, 16), ENTRY(U16LE, S16, 16),
288  ENTRY(U24BE, S32, 24), ENTRY(U24LE, S32, 24),
289  ENTRY(U32BE, S32, 32), ENTRY(U32LE, S32, 32),
290  ENTRY(F32BE, FLT, 32), ENTRY(F32LE, FLT, 32),
291  ENTRY(F64BE, DBL, 64), ENTRY(F64LE, DBL, 64),
292  { .codec_id = AV_CODEC_ID_PCM_LXF, .sample_fmt = AV_SAMPLE_FMT_S32P, .sample_size = 5 },
293  };
294 
295  for (unsigned i = 0; i < FF_ARRAY_ELEMS(codec_id_to_samplefmt); ++i) {
296  if (codec_id_to_samplefmt[i].codec_id == avctx->codec_id) {
297  s->sample_size = codec_id_to_samplefmt[i].sample_size;
298  avctx->sample_fmt = codec_id_to_samplefmt[i].sample_fmt;
299  if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
300  avctx->bits_per_raw_sample = codec_id_to_samplefmt[i].bits_per_sample;
301  break;
302  }
303  av_assert1(i + 1 < FF_ARRAY_ELEMS(codec_id_to_samplefmt));
304  }
305 
306  return 0;
307 }
308 
309 typedef struct PCMScaleDecode {
311  void (*vector_fmul_scalar)(float *dst, const float *src, float mul,
312  int len);
313  float scale;
315 
317 {
318  PCMScaleDecode *s = avctx->priv_data;
319  AVFloatDSPContext *fdsp;
320 
321  avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
322  s->base.sample_size = 4;
323 
324  if (avctx->bits_per_coded_sample < 1 || avctx->bits_per_coded_sample > 24)
325  return AVERROR_INVALIDDATA;
326 
327  s->scale = 1. / (1 << (avctx->bits_per_coded_sample - 1));
328  fdsp = avpriv_float_dsp_alloc(0);
329  if (!fdsp)
330  return AVERROR(ENOMEM);
331  s->vector_fmul_scalar = fdsp->vector_fmul_scalar;
332  av_free(fdsp);
333 
334  return 0;
335 }
336 
337 typedef struct PCMLUTDecode {
339  int16_t table[256];
340 } PCMLUTDecode;
341 
343 {
344  PCMLUTDecode *s = avctx->priv_data;
345 
346  switch (avctx->codec_id) {
347  default:
348  av_unreachable("pcm_lut_decode_init() only used with alaw, mulaw and vidc");
349 #if CONFIG_PCM_ALAW_DECODER || CONFIG_PCM_ALAW_ENCODER
351  for (int i = 0; i < 256; i++)
352  s->table[i] = alaw2linear(i);
353  break;
354 #endif
355 #if CONFIG_PCM_MULAW_DECODER || CONFIG_PCM_MULAW_ENCODER
357  for (int i = 0; i < 256; i++)
358  s->table[i] = ulaw2linear(i);
359  break;
360 #endif
361 #if CONFIG_PCM_VIDC_DECODER || CONFIG_PCM_VIDC_ENCODER
363  for (int i = 0; i < 256; i++)
364  s->table[i] = vidc2linear(i);
365  break;
366 #endif
367  }
368 
369  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
370  s->base.sample_size = 1;
371 
372  return 0;
373 }
374 
375 /**
376  * Read PCM samples macro
377  * @param size Data size of native machine format
378  * @param endian bytestream_get_xxx() endian suffix
379  * @param src Source pointer (variable name)
380  * @param dst Destination pointer (variable name)
381  * @param n Total number of samples (variable name)
382  * @param shift Bitshift (bits)
383  * @param offset Sample value offset
384  */
385 #define DECODE(size, endian, src, dst, n, shift, offset) \
386  for (; n > 0; n--) { \
387  uint ## size ## _t v = bytestream_get_ ## endian(&src); \
388  AV_WN ## size ## A(dst, (uint ## size ## _t)(v - offset) << shift); \
389  dst += size / 8; \
390  }
391 
392 #define DECODE_PLANAR(size, endian, src, dst, n, shift, offset) \
393  n /= channels; \
394  for (c = 0; c < avctx->ch_layout.nb_channels; c++) { \
395  int i; \
396  dst = frame->extended_data[c]; \
397  for (i = n; i > 0; i--) { \
398  uint ## size ## _t v = bytestream_get_ ## endian(&src); \
399  AV_WN ## size ## A(dst, (uint ## size ##_t)(v - offset) << shift); \
400  dst += size / 8; \
401  } \
402  }
403 
405  int *got_frame_ptr, AVPacket *avpkt)
406 {
407  const uint8_t *src = avpkt->data;
408  int buf_size = avpkt->size;
409  PCMDecode *s = avctx->priv_data;
410  int channels = avctx->ch_layout.nb_channels;
411  int sample_size = s->sample_size;
412  int c, n, ret, samples_per_block;
413  uint8_t *samples;
414  int32_t *dst_int32_t;
415 
416  samples_per_block = 1;
417  if (avctx->codec_id == AV_CODEC_ID_PCM_LXF) {
418  /* we process 40-bit blocks per channel for LXF */
419  samples_per_block = 2;
420  }
421 
422  if (channels == 0) {
423  av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
424  return AVERROR(EINVAL);
425  }
426 
427  if (avctx->codec_id != avctx->codec->id) {
428  av_log(avctx, AV_LOG_ERROR, "codec ids mismatch\n");
429  return AVERROR(EINVAL);
430  }
431 
432  n = channels * sample_size;
433 
434  if (n && buf_size % n) {
435  if (buf_size < n) {
436  av_log(avctx, AV_LOG_ERROR,
437  "Invalid PCM packet, data has size %d but at least a size of %d was expected\n",
438  buf_size, n);
439  return AVERROR_INVALIDDATA;
440  } else
441  buf_size -= buf_size % n;
442  }
443 
444  n = buf_size / sample_size;
445 
446  /* get output buffer */
447  frame->nb_samples = n * samples_per_block / channels;
448  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
449  return ret;
450  samples = frame->data[0];
451 
452  switch (avctx->codec_id) {
454  DECODE(32, le32, src, samples, n, 0, 0x80000000)
455  break;
457  DECODE(32, be32, src, samples, n, 0, 0x80000000)
458  break;
460  DECODE(32, le24, src, samples, n, 8, 0)
461  break;
463  DECODE_PLANAR(32, le24, src, samples, n, 8, 0);
464  break;
466  DECODE(32, be24, src, samples, n, 8, 0)
467  break;
469  DECODE(32, le24, src, samples, n, 8, 0x800000)
470  break;
472  DECODE(32, be24, src, samples, n, 8, 0x800000)
473  break;
475  for (; n > 0; n--) {
476  uint32_t v = bytestream_get_be24(&src);
477  v >>= 4; // sync flags are here
478  AV_WN16A(samples, ff_reverse[(v >> 8) & 0xff] +
479  (ff_reverse[v & 0xff] << 8));
480  samples += 2;
481  }
482  break;
484  DECODE(16, le16, src, samples, n, 0, 0x8000)
485  break;
487  DECODE(16, be16, src, samples, n, 0, 0x8000)
488  break;
489  case AV_CODEC_ID_PCM_S8:
490  for (; n > 0; n--)
491  *samples++ = *src++ + 128;
492  break;
493  case AV_CODEC_ID_PCM_SGA:
494  for (; n > 0; n--) {
495  int sign = *src >> 7;
496  int magn = *src & 0x7f;
497  *samples++ = sign ? 128 - magn : 128 + magn;
498  src++;
499  }
500  break;
502  n /= avctx->ch_layout.nb_channels;
503  for (c = 0; c < avctx->ch_layout.nb_channels; c++) {
504  int i;
505  samples = frame->extended_data[c];
506  for (i = n; i > 0; i--)
507  *samples++ = *src++ + 128;
508  }
509  break;
510 #if HAVE_BIGENDIAN
513  DECODE(64, le64, src, samples, n, 0, 0)
514  break;
519  DECODE(32, le32, src, samples, n, 0, 0)
520  break;
522  DECODE_PLANAR(32, le32, src, samples, n, 0, 0);
523  break;
525  DECODE(16, le16, src, samples, n, 0, 0)
526  break;
528  DECODE_PLANAR(16, le16, src, samples, n, 0, 0);
529  break;
535 #else
538  DECODE(64, be64, src, samples, n, 0, 0)
539  break;
542  DECODE(32, be32, src, samples, n, 0, 0)
543  break;
545  DECODE(16, be16, src, samples, n, 0, 0)
546  break;
548  DECODE_PLANAR(16, be16, src, samples, n, 0, 0);
549  break;
557 #endif /* HAVE_BIGENDIAN */
558  case AV_CODEC_ID_PCM_U8:
559  memcpy(samples, src, n * sample_size);
560  break;
561 #if HAVE_BIGENDIAN
563 #else
566 #endif /* HAVE_BIGENDIAN */
567  n /= avctx->ch_layout.nb_channels;
568  for (c = 0; c < avctx->ch_layout.nb_channels; c++) {
569  samples = frame->extended_data[c];
570  bytestream_get_buffer(&src, samples, n * sample_size);
571  }
572  break;
573 #if CONFIG_PCM_ALAW_DECODER || CONFIG_PCM_ALAW_ENCODER || \
574  CONFIG_PCM_MULAW_DECODER || CONFIG_PCM_MULAW_ENCODER || \
575  CONFIG_PCM_VIDC_DECODER || CONFIG_PCM_VIDC_ENCODER
578  case AV_CODEC_ID_PCM_VIDC: {
579  const int16_t *const lut = ((PCMLUTDecode*)avctx->priv_data)->table;
580  int16_t *restrict samples_16 = (int16_t*)samples;
581 
582  for (; n > 0; n--)
583  *samples_16++ = lut[*src++];
584  break;
585  }
586 #endif
587  case AV_CODEC_ID_PCM_LXF:
588  {
589  int i;
590  n /= channels;
591  for (c = 0; c < channels; c++) {
592  dst_int32_t = (int32_t *)frame->extended_data[c];
593  for (i = 0; i < n; i++) {
594  // extract low 20 bits and expand to 32 bits
595  *dst_int32_t++ = ((uint32_t)src[2]<<28) |
596  (src[1] << 20) |
597  (src[0] << 12) |
598  ((src[2] & 0x0F) << 8) |
599  src[1];
600  // extract high 20 bits and expand to 32 bits
601  *dst_int32_t++ = ((uint32_t)src[4]<<24) |
602  (src[3] << 16) |
603  ((src[2] & 0xF0) << 8) |
604  (src[4] << 4) |
605  (src[3] >> 4);
606  src += 5;
607  }
608  }
609  break;
610  }
611  default:
612  return -1;
613  }
614 
615  if (avctx->codec_id == AV_CODEC_ID_PCM_F16LE ||
616  avctx->codec_id == AV_CODEC_ID_PCM_F24LE) {
617  PCMScaleDecode *s2 = avctx->priv_data;
618  s2->vector_fmul_scalar((float *)frame->extended_data[0],
619  (const float *)frame->extended_data[0],
620  s2->scale, FFALIGN(frame->nb_samples * avctx->ch_layout.nb_channels, 4));
621  }
622 
623  *got_frame_ptr = 1;
624 
625  return buf_size;
626 }
627 
628 #define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_)
629 #define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_) \
630 const FFCodec ff_ ## name_ ## _encoder = { \
631  .p.name = #name_, \
632  CODEC_LONG_NAME(long_name_), \
633  .p.type = AVMEDIA_TYPE_AUDIO, \
634  .p.id = id_, \
635  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_VARIABLE_FRAME_SIZE | \
636  AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, \
637  .init = pcm_encode_init, \
638  FF_CODEC_ENCODE_CB(pcm_encode_frame), \
639  CODEC_SAMPLEFMTS(sample_fmt_), \
640 }
641 
642 #define PCM_ENCODER_2(cf, id, sample_fmt, name, long_name) \
643  PCM_ENCODER_ ## cf(id, sample_fmt, name, long_name)
644 #define PCM_ENCODER_3(cf, id, sample_fmt, name, long_name) \
645  PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)
646 #define PCM_ENCODER(id, sample_fmt, name, long_name) \
647  PCM_ENCODER_3(CONFIG_PCM_ ## id ## _ENCODER, AV_CODEC_ID_PCM_ ## id, \
648  AV_SAMPLE_FMT_ ## sample_fmt, pcm_ ## name, long_name)
649 
650 #define PCM_DECODER_0(id, sample_fmt, name, long_name, Context, init_func)
651 #define PCM_DECODER_1(id_, sample_fmt, name_, long_name, Context, init_func)\
652 const FFCodec ff_ ## name_ ## _decoder = { \
653  .p.name = #name_, \
654  CODEC_LONG_NAME(long_name), \
655  .p.type = AVMEDIA_TYPE_AUDIO, \
656  .p.id = id_, \
657  .priv_data_size = sizeof(Context), \
658  .init = init_func, \
659  FF_CODEC_DECODE_CB(pcm_decode_frame), \
660  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE, \
661 }
662 
663 #define PCM_DECODER_2(cf, id, sample_fmt, name, long_name, Context, init_func) \
664  PCM_DECODER_ ## cf(id, sample_fmt, name, long_name, Context, init_func)
665 #define PCM_DECODER_3(cf, id, sample_fmt, name, long_name, Context, init_func) \
666  PCM_DECODER_2(cf, id, sample_fmt, name, long_name, Context, init_func)
667 #define PCM_DEC_EXT(id, sample_fmt, name, long_name, Context, init_func) \
668  PCM_DECODER_3(CONFIG_PCM_ ## id ## _DECODER, AV_CODEC_ID_PCM_ ## id, \
669  AV_SAMPLE_FMT_ ## sample_fmt, pcm_ ## name, long_name, \
670  Context, init_func)
671 
672 #define PCM_DECODER(id, sample_fmt, name, long_name) \
673  PCM_DEC_EXT(id, sample_fmt, name, long_name, PCMDecode, pcm_decode_init)
674 
675 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
676  PCM_ENCODER(id, sample_fmt_, name, long_name_); \
677  PCM_DECODER(id, sample_fmt_, name, long_name_)
678 
679 #define PCM_CODEC_EXT(id, sample_fmt, name, long_name, DecContext, dec_init_func) \
680  PCM_DEC_EXT(id, sample_fmt, name, long_name, DecContext, dec_init_func); \
681  PCM_ENCODER(id, sample_fmt, name, long_name)
682 
683 /* Note: Do not forget to add new entries to the Makefile and
684  * to the table in pcm_decode_init() as well. */
685 // AV_CODEC_ID_* pcm_* name
686 // AV_SAMPLE_FMT_* long name DecodeContext decode init func
687 #if CONFIG_PCM_ALAW_DECODER || CONFIG_PCM_ALAW_ENCODER
688 PCM_CODEC_EXT(ALAW, S16, alaw, "PCM A-law / G.711 A-law", PCMLUTDecode, pcm_lut_decode_init);
689 #endif
690 PCM_DEC_EXT (F16LE, FLT, f16le, "PCM 16.8 floating point little-endian", PCMScaleDecode, pcm_scale_decode_init);
691 PCM_DEC_EXT (F24LE, FLT, f24le, "PCM 24.0 floating point little-endian", PCMScaleDecode, pcm_scale_decode_init);
692 PCM_CODEC (F32BE, FLT, f32be, "PCM 32-bit floating point big-endian");
693 PCM_CODEC (F32LE, FLT, f32le, "PCM 32-bit floating point little-endian");
694 PCM_CODEC (F64BE, DBL, f64be, "PCM 64-bit floating point big-endian");
695 PCM_CODEC (F64LE, DBL, f64le, "PCM 64-bit floating point little-endian");
696 PCM_DECODER (LXF, S32P,lxf, "PCM signed 20-bit little-endian planar");
697 #if CONFIG_PCM_MULAW_DECODER || CONFIG_PCM_MULAW_ENCODER
698 PCM_CODEC_EXT(MULAW, S16, mulaw, "PCM mu-law / G.711 mu-law", PCMLUTDecode, pcm_lut_decode_init);
699 #endif
700 PCM_CODEC (S8, U8, s8, "PCM signed 8-bit");
701 PCM_CODEC (S8_PLANAR, U8P, s8_planar, "PCM signed 8-bit planar");
702 PCM_CODEC (S16BE, S16, s16be, "PCM signed 16-bit big-endian");
703 PCM_CODEC (S16BE_PLANAR, S16P,s16be_planar, "PCM signed 16-bit big-endian planar");
704 PCM_CODEC (S16LE, S16, s16le, "PCM signed 16-bit little-endian");
705 PCM_CODEC (S16LE_PLANAR, S16P,s16le_planar, "PCM signed 16-bit little-endian planar");
706 PCM_CODEC (S24BE, S32, s24be, "PCM signed 24-bit big-endian");
707 PCM_CODEC (S24DAUD, S16, s24daud, "PCM D-Cinema audio signed 24-bit");
708 PCM_CODEC (S24LE, S32, s24le, "PCM signed 24-bit little-endian");
709 PCM_CODEC (S24LE_PLANAR, S32P,s24le_planar, "PCM signed 24-bit little-endian planar");
710 PCM_CODEC (S32BE, S32, s32be, "PCM signed 32-bit big-endian");
711 PCM_CODEC (S32LE, S32, s32le, "PCM signed 32-bit little-endian");
712 PCM_CODEC (S32LE_PLANAR, S32P,s32le_planar, "PCM signed 32-bit little-endian planar");
713 PCM_CODEC (U8, U8, u8, "PCM unsigned 8-bit");
714 PCM_CODEC (U16BE, S16, u16be, "PCM unsigned 16-bit big-endian");
715 PCM_CODEC (U16LE, S16, u16le, "PCM unsigned 16-bit little-endian");
716 PCM_CODEC (U24BE, S32, u24be, "PCM unsigned 24-bit big-endian");
717 PCM_CODEC (U24LE, S32, u24le, "PCM unsigned 24-bit little-endian");
718 PCM_CODEC (U32BE, S32, u32be, "PCM unsigned 32-bit big-endian");
719 PCM_CODEC (U32LE, S32, u32le, "PCM unsigned 32-bit little-endian");
720 PCM_CODEC (S64BE, S64, s64be, "PCM signed 64-bit big-endian");
721 PCM_CODEC (S64LE, S64, s64le, "PCM signed 64-bit little-endian");
722 #if CONFIG_PCM_VIDC_DECODER || CONFIG_PCM_VIDC_ENCODER
723 PCM_CODEC_EXT(VIDC, S16, vidc, "PCM Archimedes VIDC", PCMLUTDecode, pcm_lut_decode_init);
724 #endif
725 PCM_DECODER (SGA, U8, sga, "PCM SGA");
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:337
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1051
PCM_CODEC_EXT
#define PCM_CODEC_EXT(id, sample_fmt, name, long_name, DecContext, dec_init_func)
Definition: pcm.c:679
PCM_CODEC
#define PCM_CODEC(id, sample_fmt_, name, long_name_)
Definition: pcm.c:675
AV_CODEC_ID_PCM_F32BE
@ AV_CODEC_ID_PCM_F32BE
Definition: codec_id.h:357
le32
uint64_t_TMPL AV_WL64 unsigned int_TMPL le32
Definition: bytestream.h:92
pcm_decode_init
static av_cold av_unused int pcm_decode_init(AVCodecContext *avctx)
Definition: pcm.c:266
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ENCODE
#define ENCODE(type, endian, src, dst, n, shift, offset)
Write PCM samples macro.
Definition: pcm.c:89
pcm_tablegen.h
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1024
thread.h
int64_t
long long int64_t
Definition: coverity.c:34
av_unused
#define av_unused
Definition: attributes.h:131
AV_CODEC_ID_PCM_S32LE_PLANAR
@ AV_CODEC_ID_PCM_S32LE_PLANAR
Definition: codec_id.h:366
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
AVPacket::data
uint8_t * data
Definition: packet.h:558
AV_CODEC_ID_PCM_S16BE_PLANAR
@ AV_CODEC_ID_PCM_S16BE_PLANAR
Definition: codec_id.h:367
encode.h
ff_reverse
const uint8_t ff_reverse[256]
Definition: reverse.c:23
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:65
AV_CODEC_ID_PCM_U24LE
@ AV_CODEC_ID_PCM_U24LE
Definition: codec_id.h:351
AV_CODEC_ID_PCM_SGA
@ AV_CODEC_ID_PCM_SGA
Definition: codec_id.h:373
reverse.h
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
AV_CODEC_ID_PCM_S16LE_PLANAR
@ AV_CODEC_ID_PCM_S16LE_PLANAR
Definition: codec_id.h:355
AV_CODEC_ID_PCM_S64LE
@ AV_CODEC_ID_PCM_S64LE
Definition: codec_id.h:368
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:440
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:338
PCMScaleDecode::base
PCMDecode base
Definition: pcm.c:310
PCMScaleDecode::scale
float scale
Definition: pcm.c:313
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1039
be24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL be24
Definition: bytestream.h:97
le24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL le24
Definition: bytestream.h:93
PCMLUTDecode
Definition: pcm.c:337
PCMScaleDecode::vector_fmul_scalar
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Definition: pcm.c:311
PCM_DECODER
#define PCM_DECODER(id, sample_fmt, name, long_name)
Definition: pcm.c:672
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:547
AV_CODEC_ID_PCM_S8
@ AV_CODEC_ID_PCM_S8
Definition: codec_id.h:341
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_CODEC_ID_PCM_LXF
@ AV_CODEC_ID_PCM_LXF
Definition: codec_id.h:362
U8
@ U8
Definition: sw_ops.c:39
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1553
AV_CODEC_ID_PCM_F24LE
@ AV_CODEC_ID_PCM_F24LE
Definition: codec_id.h:371
channels
channels
Definition: aptx.h:31
decode.h
AV_WN16A
#define AV_WN16A(p, v)
Definition: intreadwrite.h:530
AV_CODEC_ID_PCM_MULAW
@ AV_CODEC_ID_PCM_MULAW
Definition: codec_id.h:343
AV_CODEC_ID_PCM_U16BE
@ AV_CODEC_ID_PCM_U16BE
Definition: codec_id.h:340
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:410
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:441
if
if(ret)
Definition: filter_design.txt:179
AV_CODEC_ID_PCM_ALAW
@ AV_CODEC_ID_PCM_ALAW
Definition: codec_id.h:344
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
tmp
static uint8_t tmp[20]
Definition: aes_ctr.c:47
AV_CODEC_ID_PCM_U24BE
@ AV_CODEC_ID_PCM_U24BE
Definition: codec_id.h:352
AVFloatDSPContext::vector_fmul_scalar
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: float_dsp.h:85
ENTRY
#define ENTRY(CODEC_ID, SAMPLE_FMT, BITS_PER_SAMPLE)
av_unreachable
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition: avassert.h:108
AV_CODEC_ID_PCM_U32BE
@ AV_CODEC_ID_PCM_U32BE
Definition: codec_id.h:348
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:481
AV_CODEC_ID_PCM_S64BE
@ AV_CODEC_ID_PCM_S64BE
Definition: codec_id.h:369
be64
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL be64
Definition: bytestream.h:95
pcm_lut_decode_init
static av_cold av_unused int pcm_lut_decode_init(AVCodecContext *avctx)
Definition: pcm.c:342
DECODE_PLANAR
#define DECODE_PLANAR(size, endian, src, dst, n, shift, offset)
Definition: pcm.c:392
pcm_decode_frame
static int pcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: pcm.c:404
be32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL be32
Definition: bytestream.h:96
AVOnce
#define AVOnce
Definition: thread.h:202
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AV_CODEC_ID_PCM_S24LE_PLANAR
@ AV_CODEC_ID_PCM_S24LE_PLANAR
Definition: codec_id.h:365
float_dsp.h
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
AV_CODEC_ID_PCM_VIDC
@ AV_CODEC_ID_PCM_VIDC
Definition: codec_id.h:372
pcm_encode_frame
static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: pcm.c:107
AV_CODEC_ID_PCM_S24LE
@ AV_CODEC_ID_PCM_S24LE
Definition: codec_id.h:349
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1720
AVPacket::size
int size
Definition: packet.h:559
codec_internal.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:424
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1031
AVFloatDSPContext
Definition: float_dsp.h:24
DECODE
#define DECODE(size, endian, src, dst, n, shift, offset)
Read PCM samples macro.
Definition: pcm.c:385
PCMLUTDecode::table
int16_t table[256]
Definition: pcm.c:339
ENCODE_PLANAR
#define ENCODE_PLANAR(type, endian, dst, n, shift, offset)
Definition: pcm.c:96
attributes.h
AVCodec::id
enum AVCodecID id
Definition: codec.h:186
PCMLUTDecode::base
PCMDecode base
Definition: pcm.c:338
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1546
bytestream_put_buffer
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:372
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:57
AV_CODEC_ID_PCM_F64BE
@ AV_CODEC_ID_PCM_F64BE
Definition: codec_id.h:359
AV_CODEC_ID_PCM_S32BE
@ AV_CODEC_ID_PCM_S32BE
Definition: codec_id.h:346
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
le64
uint64_t_TMPL le64
Definition: bytestream.h:91
AV_CODEC_ID_PCM_F16LE
@ AV_CODEC_ID_PCM_F16LE
Definition: codec_id.h:370
len
int len
Definition: vorbis_enc_data.h:426
avcodec.h
bytestream_get_buffer
static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
Definition: bytestream.h:363
ret
ret
Definition: filter_design.txt:187
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: avcodec.h:1057
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
be16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL be16
Definition: bytestream.h:98
PCMDecode
Definition: pcm.c:262
le16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL le16
Definition: bytestream.h:94
PCM_DEC_EXT
#define PCM_DEC_EXT(id, sample_fmt, name, long_name, Context, init_func)
Definition: pcm.c:667
AVCodecContext
main external API structure.
Definition: avcodec.h:431
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:106
AV_CODEC_ID_PCM_U32LE
@ AV_CODEC_ID_PCM_U32LE
Definition: codec_id.h:347
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
AV_CODEC_ID_PCM_S32LE
@ AV_CODEC_ID_PCM_S32LE
Definition: codec_id.h:345
mem.h
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:342
AV_CODEC_ID_PCM_S24DAUD
@ AV_CODEC_ID_PCM_S24DAUD
Definition: codec_id.h:353
pcm_encode_init
static av_cold int pcm_encode_init(AVCodecContext *avctx)
Definition: pcm.c:41
AV_CODEC_ID_PCM_F64LE
@ AV_CODEC_ID_PCM_F64LE
Definition: codec_id.h:360
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
AVPacket
This structure stores compressed data.
Definition: packet.h:535
avpriv_float_dsp_alloc
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
PCMScaleDecode
Definition: pcm.c:309
AV_CODEC_ID_PCM_S8_PLANAR
@ AV_CODEC_ID_PCM_S8_PLANAR
Definition: codec_id.h:364
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
AV_CODEC_ID_PCM_U16LE
@ AV_CODEC_ID_PCM_U16LE
Definition: codec_id.h:339
S8
static const uint32_t S8[256]
Definition: cast5.c:328
AV_CODEC_ID_PCM_F32LE
@ AV_CODEC_ID_PCM_F32LE
Definition: codec_id.h:358
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
PCMDecode::sample_size
int sample_size
Definition: pcm.c:263
pcm_scale_decode_init
static av_cold av_unused int pcm_scale_decode_init(AVCodecContext *avctx)
Definition: pcm.c:316
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:59
AV_CODEC_ID_PCM_S24BE
@ AV_CODEC_ID_PCM_S24BE
Definition: codec_id.h:350
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60
src
#define src
Definition: vp8dsp.c:248