FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavutil/attributes.h"
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "internal.h"
31 #include "mathops.h"
32 #include "pcm_tablegen.h"
33 
35 {
36  avctx->frame_size = 0;
37  switch (avctx->codec->id) {
40  break;
43  break;
44  default:
45  break;
46  }
47 
49  avctx->block_align = avctx->channels * avctx->bits_per_coded_sample / 8;
50  avctx->bit_rate = avctx->block_align * avctx->sample_rate * 8;
51 
52  return 0;
53 }
54 
55 /**
56  * Write PCM samples macro
57  * @param type Datatype of native machine format
58  * @param endian bytestream_put_xxx() suffix
59  * @param src Source pointer (variable name)
60  * @param dst Destination pointer (variable name)
61  * @param n Total number of samples (variable name)
62  * @param shift Bitshift (bits)
63  * @param offset Sample value offset
64  */
65 #define ENCODE(type, endian, src, dst, n, shift, offset) \
66  samples_ ## type = (const type *) src; \
67  for (; n > 0; n--) { \
68  register type v = (*samples_ ## type++ >> shift) + offset; \
69  bytestream_put_ ## endian(&dst, v); \
70  }
71 
72 #define ENCODE_PLANAR(type, endian, dst, n, shift, offset) \
73  n /= avctx->channels; \
74  for (c = 0; c < avctx->channels; c++) { \
75  int i; \
76  samples_ ## type = (const type *) frame->extended_data[c]; \
77  for (i = n; i > 0; i--) { \
78  register type v = (*samples_ ## type++ >> shift) + offset; \
79  bytestream_put_ ## endian(&dst, v); \
80  } \
81  }
82 
83 static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
84  const AVFrame *frame, int *got_packet_ptr)
85 {
86  int n, c, sample_size, v, ret;
87  const short *samples;
88  unsigned char *dst;
89  const uint8_t *samples_uint8_t;
90  const int16_t *samples_int16_t;
91  const int32_t *samples_int32_t;
92  const int64_t *samples_int64_t;
93  const uint16_t *samples_uint16_t;
94  const uint32_t *samples_uint32_t;
95 
96  sample_size = av_get_bits_per_sample(avctx->codec->id) / 8;
97  n = frame->nb_samples * avctx->channels;
98  samples = (const short *)frame->data[0];
99 
100  if ((ret = ff_alloc_packet2(avctx, avpkt, n * sample_size)) < 0)
101  return ret;
102  dst = avpkt->data;
103 
104  switch (avctx->codec->id) {
106  ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
107  break;
109  ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
110  break;
112  ENCODE(int32_t, le24, samples, dst, n, 8, 0)
113  break;
115  ENCODE_PLANAR(int32_t, le24, dst, n, 8, 0)
116  break;
118  ENCODE(int32_t, be24, samples, dst, n, 8, 0)
119  break;
121  ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
122  break;
124  ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
125  break;
127  for (; n > 0; n--) {
128  uint32_t tmp = ff_reverse[(*samples >> 8) & 0xff] +
129  (ff_reverse[*samples & 0xff] << 8);
130  tmp <<= 4; // sync flags would go here
131  bytestream_put_be24(&dst, tmp);
132  samples++;
133  }
134  break;
136  ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
137  break;
139  ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
140  break;
141  case AV_CODEC_ID_PCM_S8:
142  ENCODE(uint8_t, byte, samples, dst, n, 0, -128)
143  break;
145  ENCODE_PLANAR(uint8_t, byte, dst, n, 0, -128)
146  break;
147 #if HAVE_BIGENDIAN
149  ENCODE(int64_t, le64, samples, dst, n, 0, 0)
150  break;
153  ENCODE(int32_t, le32, samples, dst, n, 0, 0)
154  break;
156  ENCODE_PLANAR(int32_t, le32, dst, n, 0, 0)
157  break;
159  ENCODE(int16_t, le16, samples, dst, n, 0, 0)
160  break;
162  ENCODE_PLANAR(int16_t, le16, dst, n, 0, 0)
163  break;
168 #else
170  ENCODE(int64_t, be64, samples, dst, n, 0, 0)
171  break;
174  ENCODE(int32_t, be32, samples, dst, n, 0, 0)
175  break;
177  ENCODE(int16_t, be16, samples, dst, n, 0, 0)
178  break;
180  ENCODE_PLANAR(int16_t, be16, dst, n, 0, 0)
181  break;
186 #endif /* HAVE_BIGENDIAN */
187  case AV_CODEC_ID_PCM_U8:
188  memcpy(dst, samples, n * sample_size);
189  break;
190 #if HAVE_BIGENDIAN
192 #else
195 #endif /* HAVE_BIGENDIAN */
196  n /= avctx->channels;
197  for (c = 0; c < avctx->channels; c++) {
198  const uint8_t *src = frame->extended_data[c];
199  bytestream_put_buffer(&dst, src, n * sample_size);
200  }
201  break;
203  for (; n > 0; n--) {
204  v = *samples++;
205  *dst++ = linear_to_alaw[(v + 32768) >> 2];
206  }
207  break;
209  for (; n > 0; n--) {
210  v = *samples++;
211  *dst++ = linear_to_ulaw[(v + 32768) >> 2];
212  }
213  break;
214  default:
215  return -1;
216  }
217 
218  *got_packet_ptr = 1;
219  return 0;
220 }
221 
222 typedef struct PCMDecode {
223  short table[256];
224 } PCMDecode;
225 
227 {
228  PCMDecode *s = avctx->priv_data;
229  int i;
230 
231  if (avctx->channels <= 0) {
232  av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
233  return AVERROR(EINVAL);
234  }
235 
236  switch (avctx->codec_id) {
238  for (i = 0; i < 256; i++)
239  s->table[i] = alaw2linear(i);
240  break;
242  for (i = 0; i < 256; i++)
243  s->table[i] = ulaw2linear(i);
244  break;
245  default:
246  break;
247  }
248 
249  avctx->sample_fmt = avctx->codec->sample_fmts[0];
250 
251  if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
253 
254  return 0;
255 }
256 
257 /**
258  * Read PCM samples macro
259  * @param size Data size of native machine format
260  * @param endian bytestream_get_xxx() endian suffix
261  * @param src Source pointer (variable name)
262  * @param dst Destination pointer (variable name)
263  * @param n Total number of samples (variable name)
264  * @param shift Bitshift (bits)
265  * @param offset Sample value offset
266  */
267 #define DECODE(size, endian, src, dst, n, shift, offset) \
268  for (; n > 0; n--) { \
269  uint ## size ## _t v = bytestream_get_ ## endian(&src); \
270  AV_WN ## size ## A(dst, (v - offset) << shift); \
271  dst += size / 8; \
272  }
273 
274 #define DECODE_PLANAR(size, endian, src, dst, n, shift, offset) \
275  n /= avctx->channels; \
276  for (c = 0; c < avctx->channels; c++) { \
277  int i; \
278  dst = frame->extended_data[c]; \
279  for (i = n; i > 0; i--) { \
280  uint ## size ## _t v = bytestream_get_ ## endian(&src); \
281  AV_WN ## size ## A(dst, (v - offset) << shift); \
282  dst += size / 8; \
283  } \
284  }
285 
286 static int pcm_decode_frame(AVCodecContext *avctx, void *data,
287  int *got_frame_ptr, AVPacket *avpkt)
288 {
289  const uint8_t *src = avpkt->data;
290  int buf_size = avpkt->size;
291  PCMDecode *s = avctx->priv_data;
292  AVFrame *frame = data;
293  int sample_size, c, n, ret, samples_per_block;
294  uint8_t *samples;
295  int32_t *dst_int32_t;
296 
297  sample_size = av_get_bits_per_sample(avctx->codec_id) / 8;
298 
299  /* av_get_bits_per_sample returns 0 for AV_CODEC_ID_PCM_DVD */
300  samples_per_block = 1;
301  if (AV_CODEC_ID_PCM_DVD == avctx->codec_id) {
302  if (avctx->bits_per_coded_sample != 20 &&
303  avctx->bits_per_coded_sample != 24) {
304  av_log(avctx, AV_LOG_ERROR,
305  "PCM DVD unsupported sample depth %i\n",
306  avctx->bits_per_coded_sample);
307  return AVERROR(EINVAL);
308  }
309  /* 2 samples are interleaved per block in PCM_DVD */
310  samples_per_block = 2;
311  sample_size = avctx->bits_per_coded_sample * 2 / 8;
312  } else if (avctx->codec_id == AV_CODEC_ID_PCM_LXF) {
313  /* we process 40-bit blocks per channel for LXF */
314  samples_per_block = 2;
315  sample_size = 5;
316  }
317 
318  if (sample_size == 0) {
319  av_log(avctx, AV_LOG_ERROR, "Invalid sample_size\n");
320  return AVERROR(EINVAL);
321  }
322 
323  if (avctx->channels == 0) {
324  av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
325  return AVERROR(EINVAL);
326  }
327 
328  if (avctx->codec_id != avctx->codec->id) {
329  av_log(avctx, AV_LOG_ERROR, "codec ids mismatch\n");
330  return AVERROR(EINVAL);
331  }
332 
333  n = avctx->channels * sample_size;
334 
335  if (n && buf_size % n) {
336  if (buf_size < n) {
337  av_log(avctx, AV_LOG_ERROR,
338  "Invalid PCM packet, data has size %d but at least a size of %d was expected\n",
339  buf_size, n);
340  return AVERROR_INVALIDDATA;
341  } else
342  buf_size -= buf_size % n;
343  }
344 
345  n = buf_size / sample_size;
346 
347  /* get output buffer */
348  frame->nb_samples = n * samples_per_block / avctx->channels;
349  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
350  return ret;
351  samples = frame->data[0];
352 
353  switch (avctx->codec_id) {
355  DECODE(32, le32, src, samples, n, 0, 0x80000000)
356  break;
358  DECODE(32, be32, src, samples, n, 0, 0x80000000)
359  break;
361  DECODE(32, le24, src, samples, n, 8, 0)
362  break;
364  DECODE_PLANAR(32, le24, src, samples, n, 8, 0);
365  break;
367  DECODE(32, be24, src, samples, n, 8, 0)
368  break;
370  DECODE(32, le24, src, samples, n, 8, 0x800000)
371  break;
373  DECODE(32, be24, src, samples, n, 8, 0x800000)
374  break;
376  for (; n > 0; n--) {
377  uint32_t v = bytestream_get_be24(&src);
378  v >>= 4; // sync flags are here
379  AV_WN16A(samples, ff_reverse[(v >> 8) & 0xff] +
380  (ff_reverse[v & 0xff] << 8));
381  samples += 2;
382  }
383  break;
385  DECODE(16, le16, src, samples, n, 0, 0x8000)
386  break;
388  DECODE(16, be16, src, samples, n, 0, 0x8000)
389  break;
390  case AV_CODEC_ID_PCM_S8:
391  for (; n > 0; n--)
392  *samples++ = *src++ + 128;
393  break;
395  n /= avctx->channels;
396  for (c = 0; c < avctx->channels; c++) {
397  int i;
398  samples = frame->extended_data[c];
399  for (i = n; i > 0; i--)
400  *samples++ = *src++ + 128;
401  }
402  break;
403 #if HAVE_BIGENDIAN
405  DECODE(64, le64, src, samples, n, 0, 0)
406  break;
409  DECODE(32, le32, src, samples, n, 0, 0)
410  break;
412  DECODE_PLANAR(32, le32, src, samples, n, 0, 0);
413  break;
415  DECODE(16, le16, src, samples, n, 0, 0)
416  break;
418  DECODE_PLANAR(16, le16, src, samples, n, 0, 0);
419  break;
424 #else
426  DECODE(64, be64, src, samples, n, 0, 0)
427  break;
430  DECODE(32, be32, src, samples, n, 0, 0)
431  break;
433  DECODE(16, be16, src, samples, n, 0, 0)
434  break;
436  DECODE_PLANAR(16, be16, src, samples, n, 0, 0);
437  break;
442 #endif /* HAVE_BIGENDIAN */
443  case AV_CODEC_ID_PCM_U8:
444  memcpy(samples, src, n * sample_size);
445  break;
446 #if HAVE_BIGENDIAN
448 #else
451 #endif /* HAVE_BIGENDIAN */
452  n /= avctx->channels;
453  for (c = 0; c < avctx->channels; c++) {
454  samples = frame->extended_data[c];
455  bytestream_get_buffer(&src, samples, n * sample_size);
456  }
457  break;
459  for (; n > 0; n--) {
460  int v = *src++;
461  if (v < 128)
462  v = 128 - v;
463  *samples++ = v;
464  }
465  break;
468  for (; n > 0; n--) {
469  AV_WN16A(samples, s->table[*src++]);
470  samples += 2;
471  }
472  break;
473  case AV_CODEC_ID_PCM_DVD:
474  {
475  const uint8_t *src8;
476  dst_int32_t = (int32_t *)frame->data[0];
477  n /= avctx->channels;
478  switch (avctx->bits_per_coded_sample) {
479  case 20:
480  while (n--) {
481  c = avctx->channels;
482  src8 = src + 4 * c;
483  while (c--) {
484  *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8 & 0xf0) << 8);
485  *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++ & 0x0f) << 12);
486  }
487  src = src8;
488  }
489  break;
490  case 24:
491  while (n--) {
492  c = avctx->channels;
493  src8 = src + 4 * c;
494  while (c--) {
495  *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
496  *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
497  }
498  src = src8;
499  }
500  break;
501  }
502  break;
503  }
504  case AV_CODEC_ID_PCM_LXF:
505  {
506  int i;
507  n /= avctx->channels;
508  for (c = 0; c < avctx->channels; c++) {
509  dst_int32_t = (int32_t *)frame->extended_data[c];
510  for (i = 0; i < n; i++) {
511  // extract low 20 bits and expand to 32 bits
512  *dst_int32_t++ = (src[2] << 28) |
513  (src[1] << 20) |
514  (src[0] << 12) |
515  ((src[2] & 0x0F) << 8) |
516  src[1];
517  // extract high 20 bits and expand to 32 bits
518  *dst_int32_t++ = (src[4] << 24) |
519  (src[3] << 16) |
520  ((src[2] & 0xF0) << 8) |
521  (src[4] << 4) |
522  (src[3] >> 4);
523  src += 5;
524  }
525  }
526  break;
527  }
528  default:
529  return -1;
530  }
531 
532  *got_frame_ptr = 1;
533 
534  return buf_size;
535 }
536 
537 #define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_)
538 #define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_) \
539 AVCodec ff_ ## name_ ## _encoder = { \
540  .name = #name_, \
541  .type = AVMEDIA_TYPE_AUDIO, \
542  .id = AV_CODEC_ID_ ## id_, \
543  .init = pcm_encode_init, \
544  .encode2 = pcm_encode_frame, \
545  .capabilities = CODEC_CAP_VARIABLE_FRAME_SIZE, \
546  .sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \
547  AV_SAMPLE_FMT_NONE }, \
548  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
549 }
550 
551 #define PCM_ENCODER_2(cf, id, sample_fmt, name, long_name) \
552  PCM_ENCODER_ ## cf(id, sample_fmt, name, long_name)
553 #define PCM_ENCODER_3(cf, id, sample_fmt, name, long_name) \
554  PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)
555 #define PCM_ENCODER(id, sample_fmt, name, long_name) \
556  PCM_ENCODER_3(CONFIG_ ## id ## _ENCODER, id, sample_fmt, name, long_name)
557 
558 #define PCM_DECODER_0(id, sample_fmt, name, long_name)
559 #define PCM_DECODER_1(id_, sample_fmt_, name_, long_name_) \
560 AVCodec ff_ ## name_ ## _decoder = { \
561  .name = #name_, \
562  .type = AVMEDIA_TYPE_AUDIO, \
563  .id = AV_CODEC_ID_ ## id_, \
564  .priv_data_size = sizeof(PCMDecode), \
565  .init = pcm_decode_init, \
566  .decode = pcm_decode_frame, \
567  .capabilities = CODEC_CAP_DR1, \
568  .sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \
569  AV_SAMPLE_FMT_NONE }, \
570  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
571 }
572 
573 #define PCM_DECODER_2(cf, id, sample_fmt, name, long_name) \
574  PCM_DECODER_ ## cf(id, sample_fmt, name, long_name)
575 #define PCM_DECODER_3(cf, id, sample_fmt, name, long_name) \
576  PCM_DECODER_2(cf, id, sample_fmt, name, long_name)
577 #define PCM_DECODER(id, sample_fmt, name, long_name) \
578  PCM_DECODER_3(CONFIG_ ## id ## _DECODER, id, sample_fmt, name, long_name)
579 
580 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
581  PCM_ENCODER(id, sample_fmt_, name, long_name_); \
582  PCM_DECODER(id, sample_fmt_, name, long_name_)
583 
584 /* Note: Do not forget to add new entries to the Makefile as well. */
585 PCM_CODEC (PCM_ALAW, AV_SAMPLE_FMT_S16, pcm_alaw, "PCM A-law / G.711 A-law");
586 PCM_DECODER(PCM_DVD, AV_SAMPLE_FMT_S32, pcm_dvd, "PCM signed 20|24-bit big-endian");
587 PCM_CODEC (PCM_F32BE, AV_SAMPLE_FMT_FLT, pcm_f32be, "PCM 32-bit floating point big-endian");
588 PCM_CODEC (PCM_F32LE, AV_SAMPLE_FMT_FLT, pcm_f32le, "PCM 32-bit floating point little-endian");
589 PCM_CODEC (PCM_F64BE, AV_SAMPLE_FMT_DBL, pcm_f64be, "PCM 64-bit floating point big-endian");
590 PCM_CODEC (PCM_F64LE, AV_SAMPLE_FMT_DBL, pcm_f64le, "PCM 64-bit floating point little-endian");
591 PCM_DECODER(PCM_LXF, AV_SAMPLE_FMT_S32P,pcm_lxf, "PCM signed 20-bit little-endian planar");
592 PCM_CODEC (PCM_MULAW, AV_SAMPLE_FMT_S16, pcm_mulaw, "PCM mu-law / G.711 mu-law");
593 PCM_CODEC (PCM_S8, AV_SAMPLE_FMT_U8, pcm_s8, "PCM signed 8-bit");
594 PCM_CODEC (PCM_S8_PLANAR, AV_SAMPLE_FMT_U8P, pcm_s8_planar, "PCM signed 8-bit planar");
595 PCM_CODEC (PCM_S16BE, AV_SAMPLE_FMT_S16, pcm_s16be, "PCM signed 16-bit big-endian");
596 PCM_CODEC (PCM_S16BE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16be_planar, "PCM signed 16-bit big-endian planar");
597 PCM_CODEC (PCM_S16LE, AV_SAMPLE_FMT_S16, pcm_s16le, "PCM signed 16-bit little-endian");
598 PCM_CODEC (PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16le_planar, "PCM signed 16-bit little-endian planar");
599 PCM_CODEC (PCM_S24BE, AV_SAMPLE_FMT_S32, pcm_s24be, "PCM signed 24-bit big-endian");
600 PCM_CODEC (PCM_S24DAUD, AV_SAMPLE_FMT_S16, pcm_s24daud, "PCM D-Cinema audio signed 24-bit");
601 PCM_CODEC (PCM_S24LE, AV_SAMPLE_FMT_S32, pcm_s24le, "PCM signed 24-bit little-endian");
602 PCM_CODEC (PCM_S24LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s24le_planar, "PCM signed 24-bit little-endian planar");
603 PCM_CODEC (PCM_S32BE, AV_SAMPLE_FMT_S32, pcm_s32be, "PCM signed 32-bit big-endian");
604 PCM_CODEC (PCM_S32LE, AV_SAMPLE_FMT_S32, pcm_s32le, "PCM signed 32-bit little-endian");
605 PCM_CODEC (PCM_S32LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s32le_planar, "PCM signed 32-bit little-endian planar");
606 PCM_CODEC (PCM_U8, AV_SAMPLE_FMT_U8, pcm_u8, "PCM unsigned 8-bit");
607 PCM_CODEC (PCM_U16BE, AV_SAMPLE_FMT_S16, pcm_u16be, "PCM unsigned 16-bit big-endian");
608 PCM_CODEC (PCM_U16LE, AV_SAMPLE_FMT_S16, pcm_u16le, "PCM unsigned 16-bit little-endian");
609 PCM_CODEC (PCM_U24BE, AV_SAMPLE_FMT_S32, pcm_u24be, "PCM unsigned 24-bit big-endian");
610 PCM_CODEC (PCM_U24LE, AV_SAMPLE_FMT_S32, pcm_u24le, "PCM unsigned 24-bit little-endian");
611 PCM_CODEC (PCM_U32BE, AV_SAMPLE_FMT_S32, pcm_u32be, "PCM unsigned 32-bit big-endian");
612 PCM_CODEC (PCM_U32LE, AV_SAMPLE_FMT_S32, pcm_u32le, "PCM unsigned 32-bit little-endian");
613 PCM_DECODER(PCM_ZORK, AV_SAMPLE_FMT_U8, pcm_zork, "PCM Zork");
614