3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
27 #include "libavutil/attributes.h"
29 #include "bytestream.h"
32 #include "pcm_tablegen.h"
34 static av_cold int pcm_encode_init(AVCodecContext *avctx)
36 avctx->frame_size = 0;
37 switch (avctx->codec->id) {
38 case AV_CODEC_ID_PCM_ALAW:
41 case AV_CODEC_ID_PCM_MULAW:
48 avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
49 avctx->block_align = avctx->channels * avctx->bits_per_coded_sample / 8;
50 avctx->bit_rate = avctx->block_align * avctx->sample_rate * 8;
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
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); \
72 #define ENCODE_PLANAR(type, endian, dst, n, shift, offset) \
73 n /= avctx->channels; \
74 for (c = 0; c < avctx->channels; c++) { \
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); \
83 static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
84 const AVFrame *frame, int *got_packet_ptr)
86 int n, c, sample_size, v, ret;
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;
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];
100 if ((ret = ff_alloc_packet2(avctx, avpkt, n * sample_size)) < 0)
104 switch (avctx->codec->id) {
105 case AV_CODEC_ID_PCM_U32LE:
106 ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
108 case AV_CODEC_ID_PCM_U32BE:
109 ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
111 case AV_CODEC_ID_PCM_S24LE:
112 ENCODE(int32_t, le24, samples, dst, n, 8, 0)
114 case AV_CODEC_ID_PCM_S24LE_PLANAR:
115 ENCODE_PLANAR(int32_t, le24, dst, n, 8, 0)
117 case AV_CODEC_ID_PCM_S24BE:
118 ENCODE(int32_t, be24, samples, dst, n, 8, 0)
120 case AV_CODEC_ID_PCM_U24LE:
121 ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
123 case AV_CODEC_ID_PCM_U24BE:
124 ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
126 case AV_CODEC_ID_PCM_S24DAUD:
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);
135 case AV_CODEC_ID_PCM_U16LE:
136 ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
138 case AV_CODEC_ID_PCM_U16BE:
139 ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
141 case AV_CODEC_ID_PCM_S8:
142 ENCODE(uint8_t, byte, samples, dst, n, 0, -128)
144 case AV_CODEC_ID_PCM_S8_PLANAR:
145 ENCODE_PLANAR(uint8_t, byte, dst, n, 0, -128)
148 case AV_CODEC_ID_PCM_F64LE:
149 ENCODE(int64_t, le64, samples, dst, n, 0, 0)
151 case AV_CODEC_ID_PCM_S32LE:
152 case AV_CODEC_ID_PCM_F32LE:
153 ENCODE(int32_t, le32, samples, dst, n, 0, 0)
155 case AV_CODEC_ID_PCM_S32LE_PLANAR:
156 ENCODE_PLANAR(int32_t, le32, dst, n, 0, 0)
158 case AV_CODEC_ID_PCM_S16LE:
159 ENCODE(int16_t, le16, samples, dst, n, 0, 0)
161 case AV_CODEC_ID_PCM_S16LE_PLANAR:
162 ENCODE_PLANAR(int16_t, le16, dst, n, 0, 0)
164 case AV_CODEC_ID_PCM_F64BE:
165 case AV_CODEC_ID_PCM_F32BE:
166 case AV_CODEC_ID_PCM_S32BE:
167 case AV_CODEC_ID_PCM_S16BE:
169 case AV_CODEC_ID_PCM_F64BE:
170 ENCODE(int64_t, be64, samples, dst, n, 0, 0)
172 case AV_CODEC_ID_PCM_F32BE:
173 case AV_CODEC_ID_PCM_S32BE:
174 ENCODE(int32_t, be32, samples, dst, n, 0, 0)
176 case AV_CODEC_ID_PCM_S16BE:
177 ENCODE(int16_t, be16, samples, dst, n, 0, 0)
179 case AV_CODEC_ID_PCM_S16BE_PLANAR:
180 ENCODE_PLANAR(int16_t, be16, dst, n, 0, 0)
182 case AV_CODEC_ID_PCM_F64LE:
183 case AV_CODEC_ID_PCM_F32LE:
184 case AV_CODEC_ID_PCM_S32LE:
185 case AV_CODEC_ID_PCM_S16LE:
186 #endif /* HAVE_BIGENDIAN */
187 case AV_CODEC_ID_PCM_U8:
188 memcpy(dst, samples, n * sample_size);
191 case AV_CODEC_ID_PCM_S16BE_PLANAR:
193 case AV_CODEC_ID_PCM_S16LE_PLANAR:
194 case AV_CODEC_ID_PCM_S32LE_PLANAR:
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);
202 case AV_CODEC_ID_PCM_ALAW:
205 *dst++ = linear_to_alaw[(v + 32768) >> 2];
208 case AV_CODEC_ID_PCM_MULAW:
211 *dst++ = linear_to_ulaw[(v + 32768) >> 2];
222 typedef struct PCMDecode {
226 static av_cold int pcm_decode_init(AVCodecContext *avctx)
228 PCMDecode *s = avctx->priv_data;
231 if (avctx->channels <= 0) {
232 av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
233 return AVERROR(EINVAL);
236 switch (avctx->codec_id) {
237 case AV_CODEC_ID_PCM_ALAW:
238 for (i = 0; i < 256; i++)
239 s->table[i] = alaw2linear(i);
241 case AV_CODEC_ID_PCM_MULAW:
242 for (i = 0; i < 256; i++)
243 s->table[i] = ulaw2linear(i);
249 avctx->sample_fmt = avctx->codec->sample_fmts[0];
251 if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
252 avctx->bits_per_raw_sample = av_get_bits_per_sample(avctx->codec_id);
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
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); \
274 #define DECODE_PLANAR(size, endian, src, dst, n, shift, offset) \
275 n /= avctx->channels; \
276 for (c = 0; c < avctx->channels; c++) { \
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); \
286 static int pcm_decode_frame(AVCodecContext *avctx, void *data,
287 int *got_frame_ptr, AVPacket *avpkt)
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;
295 int32_t *dst_int32_t;
297 sample_size = av_get_bits_per_sample(avctx->codec_id) / 8;
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;
307 if (sample_size == 0) {
308 av_log(avctx, AV_LOG_ERROR, "Invalid sample_size\n");
309 return AVERROR(EINVAL);
312 if (avctx->channels == 0) {
313 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
314 return AVERROR(EINVAL);
317 if (avctx->codec_id != avctx->codec->id) {
318 av_log(avctx, AV_LOG_ERROR, "codec ids mismatch\n");
319 return AVERROR(EINVAL);
322 n = avctx->channels * sample_size;
324 if (n && 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",
329 return AVERROR_INVALIDDATA;
331 buf_size -= buf_size % n;
334 n = buf_size / sample_size;
336 /* get output buffer */
337 frame->nb_samples = n * samples_per_block / avctx->channels;
338 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
340 samples = frame->data[0];
342 switch (avctx->codec_id) {
343 case AV_CODEC_ID_PCM_U32LE:
344 DECODE(32, le32, src, samples, n, 0, 0x80000000)
346 case AV_CODEC_ID_PCM_U32BE:
347 DECODE(32, be32, src, samples, n, 0, 0x80000000)
349 case AV_CODEC_ID_PCM_S24LE:
350 DECODE(32, le24, src, samples, n, 8, 0)
352 case AV_CODEC_ID_PCM_S24LE_PLANAR:
353 DECODE_PLANAR(32, le24, src, samples, n, 8, 0);
355 case AV_CODEC_ID_PCM_S24BE:
356 DECODE(32, be24, src, samples, n, 8, 0)
358 case AV_CODEC_ID_PCM_U24LE:
359 DECODE(32, le24, src, samples, n, 8, 0x800000)
361 case AV_CODEC_ID_PCM_U24BE:
362 DECODE(32, be24, src, samples, n, 8, 0x800000)
364 case AV_CODEC_ID_PCM_S24DAUD:
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));
373 case AV_CODEC_ID_PCM_U16LE:
374 DECODE(16, le16, src, samples, n, 0, 0x8000)
376 case AV_CODEC_ID_PCM_U16BE:
377 DECODE(16, be16, src, samples, n, 0, 0x8000)
379 case AV_CODEC_ID_PCM_S8:
381 *samples++ = *src++ + 128;
383 case AV_CODEC_ID_PCM_S8_PLANAR:
384 n /= avctx->channels;
385 for (c = 0; c < avctx->channels; c++) {
387 samples = frame->extended_data[c];
388 for (i = n; i > 0; i--)
389 *samples++ = *src++ + 128;
393 case AV_CODEC_ID_PCM_F64LE:
394 DECODE(64, le64, src, samples, n, 0, 0)
396 case AV_CODEC_ID_PCM_S32LE:
397 case AV_CODEC_ID_PCM_F32LE:
398 DECODE(32, le32, src, samples, n, 0, 0)
400 case AV_CODEC_ID_PCM_S32LE_PLANAR:
401 DECODE_PLANAR(32, le32, src, samples, n, 0, 0);
403 case AV_CODEC_ID_PCM_S16LE:
404 DECODE(16, le16, src, samples, n, 0, 0)
406 case AV_CODEC_ID_PCM_S16LE_PLANAR:
407 DECODE_PLANAR(16, le16, src, samples, n, 0, 0);
409 case AV_CODEC_ID_PCM_F64BE:
410 case AV_CODEC_ID_PCM_F32BE:
411 case AV_CODEC_ID_PCM_S32BE:
412 case AV_CODEC_ID_PCM_S16BE:
414 case AV_CODEC_ID_PCM_F64BE:
415 DECODE(64, be64, src, samples, n, 0, 0)
417 case AV_CODEC_ID_PCM_F32BE:
418 case AV_CODEC_ID_PCM_S32BE:
419 DECODE(32, be32, src, samples, n, 0, 0)
421 case AV_CODEC_ID_PCM_S16BE:
422 DECODE(16, be16, src, samples, n, 0, 0)
424 case AV_CODEC_ID_PCM_S16BE_PLANAR:
425 DECODE_PLANAR(16, be16, src, samples, n, 0, 0);
427 case AV_CODEC_ID_PCM_F64LE:
428 case AV_CODEC_ID_PCM_F32LE:
429 case AV_CODEC_ID_PCM_S32LE:
430 case AV_CODEC_ID_PCM_S16LE:
431 #endif /* HAVE_BIGENDIAN */
432 case AV_CODEC_ID_PCM_U8:
433 memcpy(samples, src, n * sample_size);
436 case AV_CODEC_ID_PCM_S16BE_PLANAR:
438 case AV_CODEC_ID_PCM_S16LE_PLANAR:
439 case AV_CODEC_ID_PCM_S32LE_PLANAR:
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);
447 case AV_CODEC_ID_PCM_ZORK:
455 case AV_CODEC_ID_PCM_ALAW:
456 case AV_CODEC_ID_PCM_MULAW:
458 AV_WN16A(samples, s->table[*src++]);
462 case AV_CODEC_ID_PCM_LXF:
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) |
473 ((src[2] & 0x0F) << 8) |
475 // extract high 20 bits and expand to 32 bits
476 *dst_int32_t++ = (src[4] << 24) |
478 ((src[2] & 0xF0) << 8) |
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 = { \
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 }, \
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)
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 = { \
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 }, \
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)
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_)
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");