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 (avctx->codec_id == AV_CODEC_ID_PCM_LXF) {
302  /* we process 40-bit blocks per channel for LXF */
303  samples_per_block = 2;
304  sample_size = 5;
305  }
306 
307  if (sample_size == 0) {
308  av_log(avctx, AV_LOG_ERROR, "Invalid sample_size\n");
309  return AVERROR(EINVAL);
310  }
311 
312  if (avctx->channels == 0) {
313  av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
314  return AVERROR(EINVAL);
315  }
316 
317  if (avctx->codec_id != avctx->codec->id) {
318  av_log(avctx, AV_LOG_ERROR, "codec ids mismatch\n");
319  return AVERROR(EINVAL);
320  }
321 
322  n = avctx->channels * sample_size;
323 
324  if (n && buf_size % n) {
325  if (buf_size < n) {
326  av_log(avctx, AV_LOG_ERROR,
327  "Invalid PCM packet, data has size %d but at least a size of %d was expected\n",
328  buf_size, n);
329  return AVERROR_INVALIDDATA;
330  } else
331  buf_size -= buf_size % n;
332  }
333 
334  n = buf_size / sample_size;
335 
336  /* get output buffer */
337  frame->nb_samples = n * samples_per_block / avctx->channels;
338  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
339  return ret;
340  samples = frame->data[0];
341 
342  switch (avctx->codec_id) {
344  DECODE(32, le32, src, samples, n, 0, 0x80000000)
345  break;
347  DECODE(32, be32, src, samples, n, 0, 0x80000000)
348  break;
350  DECODE(32, le24, src, samples, n, 8, 0)
351  break;
353  DECODE_PLANAR(32, le24, src, samples, n, 8, 0);
354  break;
356  DECODE(32, be24, src, samples, n, 8, 0)
357  break;
359  DECODE(32, le24, src, samples, n, 8, 0x800000)
360  break;
362  DECODE(32, be24, src, samples, n, 8, 0x800000)
363  break;
365  for (; n > 0; n--) {
366  uint32_t v = bytestream_get_be24(&src);
367  v >>= 4; // sync flags are here
368  AV_WN16A(samples, ff_reverse[(v >> 8) & 0xff] +
369  (ff_reverse[v & 0xff] << 8));
370  samples += 2;
371  }
372  break;
374  DECODE(16, le16, src, samples, n, 0, 0x8000)
375  break;
377  DECODE(16, be16, src, samples, n, 0, 0x8000)
378  break;
379  case AV_CODEC_ID_PCM_S8:
380  for (; n > 0; n--)
381  *samples++ = *src++ + 128;
382  break;
384  n /= avctx->channels;
385  for (c = 0; c < avctx->channels; c++) {
386  int i;
387  samples = frame->extended_data[c];
388  for (i = n; i > 0; i--)
389  *samples++ = *src++ + 128;
390  }
391  break;
392 #if HAVE_BIGENDIAN
394  DECODE(64, le64, src, samples, n, 0, 0)
395  break;
398  DECODE(32, le32, src, samples, n, 0, 0)
399  break;
401  DECODE_PLANAR(32, le32, src, samples, n, 0, 0);
402  break;
404  DECODE(16, le16, src, samples, n, 0, 0)
405  break;
407  DECODE_PLANAR(16, le16, src, samples, n, 0, 0);
408  break;
413 #else
415  DECODE(64, be64, src, samples, n, 0, 0)
416  break;
419  DECODE(32, be32, src, samples, n, 0, 0)
420  break;
422  DECODE(16, be16, src, samples, n, 0, 0)
423  break;
425  DECODE_PLANAR(16, be16, src, samples, n, 0, 0);
426  break;
431 #endif /* HAVE_BIGENDIAN */
432  case AV_CODEC_ID_PCM_U8:
433  memcpy(samples, src, n * sample_size);
434  break;
435 #if HAVE_BIGENDIAN
437 #else
440 #endif /* HAVE_BIGENDIAN */
441  n /= avctx->channels;
442  for (c = 0; c < avctx->channels; c++) {
443  samples = frame->extended_data[c];
444  bytestream_get_buffer(&src, samples, n * sample_size);
445  }
446  break;
448  for (; n > 0; n--) {
449  int v = *src++;
450  if (v < 128)
451  v = 128 - v;
452  *samples++ = v;
453  }
454  break;
457  for (; n > 0; n--) {
458  AV_WN16A(samples, s->table[*src++]);
459  samples += 2;
460  }
461  break;
462  case AV_CODEC_ID_PCM_LXF:
463  {
464  int i;
465  n /= avctx->channels;
466  for (c = 0; c < avctx->channels; c++) {
467  dst_int32_t = (int32_t *)frame->extended_data[c];
468  for (i = 0; i < n; i++) {
469  // extract low 20 bits and expand to 32 bits
470  *dst_int32_t++ = (src[2] << 28) |
471  (src[1] << 20) |
472  (src[0] << 12) |
473  ((src[2] & 0x0F) << 8) |
474  src[1];
475  // extract high 20 bits and expand to 32 bits
476  *dst_int32_t++ = (src[4] << 24) |
477  (src[3] << 16) |
478  ((src[2] & 0xF0) << 8) |
479  (src[4] << 4) |
480  (src[3] >> 4);
481  src += 5;
482  }
483  }
484  break;
485  }
486  default:
487  return -1;
488  }
489 
490  *got_frame_ptr = 1;
491 
492  return buf_size;
493 }
494 
495 #define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_)
496 #define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_) \
497 AVCodec ff_ ## name_ ## _encoder = { \
498  .name = #name_, \
499  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
500  .type = AVMEDIA_TYPE_AUDIO, \
501  .id = AV_CODEC_ID_ ## id_, \
502  .init = pcm_encode_init, \
503  .encode2 = pcm_encode_frame, \
504  .capabilities = CODEC_CAP_VARIABLE_FRAME_SIZE, \
505  .sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \
506  AV_SAMPLE_FMT_NONE }, \
507 }
508 
509 #define PCM_ENCODER_2(cf, id, sample_fmt, name, long_name) \
510  PCM_ENCODER_ ## cf(id, sample_fmt, name, long_name)
511 #define PCM_ENCODER_3(cf, id, sample_fmt, name, long_name) \
512  PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)
513 #define PCM_ENCODER(id, sample_fmt, name, long_name) \
514  PCM_ENCODER_3(CONFIG_ ## id ## _ENCODER, id, sample_fmt, name, long_name)
515 
516 #define PCM_DECODER_0(id, sample_fmt, name, long_name)
517 #define PCM_DECODER_1(id_, sample_fmt_, name_, long_name_) \
518 AVCodec ff_ ## name_ ## _decoder = { \
519  .name = #name_, \
520  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
521  .type = AVMEDIA_TYPE_AUDIO, \
522  .id = AV_CODEC_ID_ ## id_, \
523  .priv_data_size = sizeof(PCMDecode), \
524  .init = pcm_decode_init, \
525  .decode = pcm_decode_frame, \
526  .capabilities = CODEC_CAP_DR1, \
527  .sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \
528  AV_SAMPLE_FMT_NONE }, \
529 }
530 
531 #define PCM_DECODER_2(cf, id, sample_fmt, name, long_name) \
532  PCM_DECODER_ ## cf(id, sample_fmt, name, long_name)
533 #define PCM_DECODER_3(cf, id, sample_fmt, name, long_name) \
534  PCM_DECODER_2(cf, id, sample_fmt, name, long_name)
535 #define PCM_DECODER(id, sample_fmt, name, long_name) \
536  PCM_DECODER_3(CONFIG_ ## id ## _DECODER, id, sample_fmt, name, long_name)
537 
538 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
539  PCM_ENCODER(id, sample_fmt_, name, long_name_); \
540  PCM_DECODER(id, sample_fmt_, name, long_name_)
541 
542 /* Note: Do not forget to add new entries to the Makefile as well. */
543 PCM_CODEC (PCM_ALAW, AV_SAMPLE_FMT_S16, pcm_alaw, "PCM A-law / G.711 A-law");
544 PCM_CODEC (PCM_F32BE, AV_SAMPLE_FMT_FLT, pcm_f32be, "PCM 32-bit floating point big-endian");
545 PCM_CODEC (PCM_F32LE, AV_SAMPLE_FMT_FLT, pcm_f32le, "PCM 32-bit floating point little-endian");
546 PCM_CODEC (PCM_F64BE, AV_SAMPLE_FMT_DBL, pcm_f64be, "PCM 64-bit floating point big-endian");
547 PCM_CODEC (PCM_F64LE, AV_SAMPLE_FMT_DBL, pcm_f64le, "PCM 64-bit floating point little-endian");
548 PCM_DECODER(PCM_LXF, AV_SAMPLE_FMT_S32P,pcm_lxf, "PCM signed 20-bit little-endian planar");
549 PCM_CODEC (PCM_MULAW, AV_SAMPLE_FMT_S16, pcm_mulaw, "PCM mu-law / G.711 mu-law");
550 PCM_CODEC (PCM_S8, AV_SAMPLE_FMT_U8, pcm_s8, "PCM signed 8-bit");
551 PCM_CODEC (PCM_S8_PLANAR, AV_SAMPLE_FMT_U8P, pcm_s8_planar, "PCM signed 8-bit planar");
552 PCM_CODEC (PCM_S16BE, AV_SAMPLE_FMT_S16, pcm_s16be, "PCM signed 16-bit big-endian");
553 PCM_CODEC (PCM_S16BE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16be_planar, "PCM signed 16-bit big-endian planar");
554 PCM_CODEC (PCM_S16LE, AV_SAMPLE_FMT_S16, pcm_s16le, "PCM signed 16-bit little-endian");
555 PCM_CODEC (PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16le_planar, "PCM signed 16-bit little-endian planar");
556 PCM_CODEC (PCM_S24BE, AV_SAMPLE_FMT_S32, pcm_s24be, "PCM signed 24-bit big-endian");
557 PCM_CODEC (PCM_S24DAUD, AV_SAMPLE_FMT_S16, pcm_s24daud, "PCM D-Cinema audio signed 24-bit");
558 PCM_CODEC (PCM_S24LE, AV_SAMPLE_FMT_S32, pcm_s24le, "PCM signed 24-bit little-endian");
559 PCM_CODEC (PCM_S24LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s24le_planar, "PCM signed 24-bit little-endian planar");
560 PCM_CODEC (PCM_S32BE, AV_SAMPLE_FMT_S32, pcm_s32be, "PCM signed 32-bit big-endian");
561 PCM_CODEC (PCM_S32LE, AV_SAMPLE_FMT_S32, pcm_s32le, "PCM signed 32-bit little-endian");
562 PCM_CODEC (PCM_S32LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s32le_planar, "PCM signed 32-bit little-endian planar");
563 PCM_CODEC (PCM_U8, AV_SAMPLE_FMT_U8, pcm_u8, "PCM unsigned 8-bit");
564 PCM_CODEC (PCM_U16BE, AV_SAMPLE_FMT_S16, pcm_u16be, "PCM unsigned 16-bit big-endian");
565 PCM_CODEC (PCM_U16LE, AV_SAMPLE_FMT_S16, pcm_u16le, "PCM unsigned 16-bit little-endian");
566 PCM_CODEC (PCM_U24BE, AV_SAMPLE_FMT_S32, pcm_u24be, "PCM unsigned 24-bit big-endian");
567 PCM_CODEC (PCM_U24LE, AV_SAMPLE_FMT_S32, pcm_u24le, "PCM unsigned 24-bit little-endian");
568 PCM_CODEC (PCM_U32BE, AV_SAMPLE_FMT_S32, pcm_u32be, "PCM unsigned 32-bit big-endian");
569 PCM_CODEC (PCM_U32LE, AV_SAMPLE_FMT_S32, pcm_u32le, "PCM unsigned 32-bit little-endian");
570 PCM_DECODER(PCM_ZORK, AV_SAMPLE_FMT_U8, pcm_zork, "PCM Zork");
571