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"
40
42 {
44 #if !CONFIG_HARDCODED_TABLES
46 #if CONFIG_PCM_ALAW_ENCODER
50 break;
51 }
52 #endif
53 #if CONFIG_PCM_MULAW_ENCODER
57 break;
58 }
59 #endif
60 #if CONFIG_PCM_VIDC_ENCODER
64 break;
65 }
66 #endif
67 default:
68 break;
69 }
70 #endif
71
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
109 {
110 int n,
c, sample_size,
ret;
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
123
127
131 break;
134 break;
137 break;
140 break;
143 break;
146 break;
149 break;
151 for (; n > 0; n--) {
154 tmp <<= 4;
// sync flags would go here
155 bytestream_put_be24(&
dst,
tmp);
157 }
158 break;
161 break;
164 break;
167 break;
170 break;
171 #if HAVE_BIGENDIAN
175 break;
179 break;
182 break;
185 break;
188 break;
194 #else
198 break;
202 break;
205 break;
208 break;
214 #endif /* HAVE_BIGENDIAN */
217 break;
218 #if HAVE_BIGENDIAN
220 #else
223 #endif /* HAVE_BIGENDIAN */
226 const uint8_t *
src =
frame->extended_data[
c];
228 }
229 break;
230 #if CONFIG_PCM_ALAW_ENCODER
232 for (; n > 0; n--) {
234 *
dst++ = linear_to_alaw[(v + 32768) >> 2];
235 }
236 break;
237 #endif
238 #if CONFIG_PCM_MULAW_ENCODER
240 for (; n > 0; n--) {
242 *
dst++ = linear_to_ulaw[(v + 32768) >> 2];
243 }
244 break;
245 #endif
246 #if CONFIG_PCM_VIDC_ENCODER
248 for (; n > 0; n--) {
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
265
267 {
269 static const struct {
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 }
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),
284 ENTRY(S32LE_PLANAR, S32P, 32),
293 };
294
297 s->sample_size = codec_id_to_samplefmt[
i].sample_size;
298 avctx->
sample_fmt = codec_id_to_samplefmt[
i].sample_fmt;
301 break;
302 }
304 }
305
306 return 0;
307 }
308
315
317 {
320
322 s->base.sample_size = 4;
323
326
329 if (!fdsp)
333
334 return 0;
335 }
336
341
343 {
345
347 default:
348 av_unreachable(
"pcm_lut_decode_init() only used with alaw, mulaw and vidc");
349 #if CONFIG_PCM_ALAW_DECODER
351 for (
int i = 0;
i < 256;
i++)
352 s->table[
i] = alaw2linear(
i);
353 break;
354 #endif
355 #if CONFIG_PCM_MULAW_DECODER
357 for (
int i = 0;
i < 256;
i++)
358 s->table[
i] = ulaw2linear(
i);
359 break;
360 #endif
361 #if CONFIG_PCM_VIDC_DECODER
363 for (
int i = 0;
i < 256;
i++)
364 s->table[
i] = vidc2linear(
i);
365 break;
366 #endif
367 }
368
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;
411 int sample_size =
s->sample_size;
412 int c, n,
ret, samples_per_block;
415
416 samples_per_block = 1;
418 /* we process 40-bit blocks per channel for LXF */
419 samples_per_block = 2;
420 }
421
425 }
426
430 }
431
433
434 if (n && buf_size % n) {
435 if (buf_size < n) {
437 "Invalid PCM packet, data has size %d but at least a size of %d was expected\n",
438 buf_size, n);
440 } else
441 buf_size -= buf_size % n;
442 }
443
444 n = buf_size / sample_size;
445
446 /* get output buffer */
451
455 break;
458 break;
461 break;
464 break;
467 break;
470 break;
473 break;
475 for (; n > 0; n--) {
476 uint32_t v = bytestream_get_be24(&
src);
477 v >>= 4; // sync flags are here
481 }
482 break;
485 break;
488 break;
490 for (; n > 0; n--)
492 break;
494 for (; n > 0; n--) {
495 int sign = *
src >> 7;
496 int magn = *
src & 0x7f;
497 *
samples++ = sign ? 128 - magn : 128 + magn;
499 }
500 break;
506 for (
i = n;
i > 0;
i--)
508 }
509 break;
510 #if HAVE_BIGENDIAN
514 break;
520 break;
523 break;
526 break;
529 break;
535 #else
539 break;
543 break;
546 break;
549 break;
557 #endif /* HAVE_BIGENDIAN */
560 break;
561 #if HAVE_BIGENDIAN
563 #else
566 #endif /* HAVE_BIGENDIAN */
571 }
572 break;
573 #if CONFIG_PCM_ALAW_DECODER || CONFIG_PCM_MULAW_DECODER || \
574 CONFIG_PCM_VIDC_DECODER
579 int16_t *restrict samples_16 = (int16_t*)
samples;
580
581 for (; n > 0; n--)
582 *samples_16++ = lut[*
src++];
583 break;
584 }
585 #endif
587 {
593 // extract low 20 bits and expand to 32 bits
594 *dst_int32_t++ = ((uint32_t)
src[2]<<28) |
597 ((
src[2] & 0x0F) << 8) |
599 // extract high 20 bits and expand to 32 bits
600 *dst_int32_t++ = ((uint32_t)
src[4]<<24) |
602 ((
src[2] & 0xF0) << 8) |
606 }
607 }
608 break;
609 }
610 default:
611 return -1;
612 }
613
618 (
const float *)
frame->extended_data[0],
620 }
621
622 *got_frame_ptr = 1;
623
624 return buf_size;
625 }
626
627 #define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_)
628 #define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_) \
629 const FFCodec ff_ ## name_ ## _encoder = { \
630 .p.name = #name_, \
631 CODEC_LONG_NAME(long_name_), \
632 .p.type = AVMEDIA_TYPE_AUDIO, \
633 .p.id = id_, \
634 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_VARIABLE_FRAME_SIZE | \
635 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, \
636 .init = pcm_encode_init, \
637 FF_CODEC_ENCODE_CB(pcm_encode_frame), \
638 CODEC_SAMPLEFMTS(sample_fmt_), \
639 }
640
641 #define PCM_ENCODER_2(cf, id, sample_fmt, name, long_name) \
642 PCM_ENCODER_ ## cf(id, sample_fmt, name, long_name)
643 #define PCM_ENCODER_3(cf, id, sample_fmt, name, long_name) \
644 PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)
645 #define PCM_ENCODER(id, sample_fmt, name, long_name) \
646 PCM_ENCODER_3(CONFIG_PCM_ ## id ## _ENCODER, AV_CODEC_ID_PCM_ ## id, \
647 AV_SAMPLE_FMT_ ## sample_fmt, pcm_ ## name, long_name)
648
649 #define PCM_DECODER_0(id, sample_fmt, name, long_name, Context, init_func)
650 #define PCM_DECODER_1(id_, sample_fmt, name_, long_name, Context, init_func)\
651 const FFCodec ff_ ## name_ ## _decoder = { \
652 .p.name = #name_, \
653 CODEC_LONG_NAME(long_name), \
654 .p.type = AVMEDIA_TYPE_AUDIO, \
655 .p.id = id_, \
656 .priv_data_size = sizeof(Context), \
657 .init = init_func, \
658 FF_CODEC_DECODE_CB(pcm_decode_frame), \
659 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE, \
660 }
661
662 #define PCM_DECODER_2(cf, id, sample_fmt, name, long_name, Context, init_func) \
663 PCM_DECODER_ ## cf(id, sample_fmt, name, long_name, Context, init_func)
664 #define PCM_DECODER_3(cf, id, sample_fmt, name, long_name, Context, init_func) \
665 PCM_DECODER_2(cf, id, sample_fmt, name, long_name, Context, init_func)
666 #define PCM_DEC_EXT(id, sample_fmt, name, long_name, Context, init_func) \
667 PCM_DECODER_3(CONFIG_PCM_ ## id ## _DECODER, AV_CODEC_ID_PCM_ ## id, \
668 AV_SAMPLE_FMT_ ## sample_fmt, pcm_ ## name, long_name, \
669 Context, init_func)
670
671 #define PCM_DECODER(id, sample_fmt, name, long_name) \
672 PCM_DEC_EXT(id, sample_fmt, name, long_name, PCMDecode, pcm_decode_init)
673
674 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
675 PCM_ENCODER(id, sample_fmt_, name, long_name_); \
676 PCM_DECODER(id, sample_fmt_, name, long_name_)
677
678 #define PCM_CODEC_EXT(id, sample_fmt, name, long_name, DecContext, dec_init_func) \
679 PCM_DEC_EXT(id, sample_fmt, name, long_name, DecContext, dec_init_func); \
680 PCM_ENCODER(id, sample_fmt, name, long_name)
681
682 /* Note: Do not forget to add new entries to the Makefile and
683 * to the table in pcm_decode_init() as well. */
684 // AV_CODEC_ID_* pcm_* name
685 // AV_SAMPLE_FMT_* long name DecodeContext decode init func
689 PCM_CODEC (F32BE, FLT, f32be,
"PCM 32-bit floating point big-endian");
690 PCM_CODEC (F32LE, FLT, f32le,
"PCM 32-bit floating point little-endian");
691 PCM_CODEC (F64BE, DBL, f64be,
"PCM 64-bit floating point big-endian");
692 PCM_CODEC (F64LE, DBL, f64le,
"PCM 64-bit floating point little-endian");
693 PCM_DECODER (LXF, S32P,lxf,
"PCM signed 20-bit little-endian planar");
696 PCM_CODEC (S8_PLANAR, U8P, s8_planar,
"PCM signed 8-bit planar");
697 PCM_CODEC (S16BE, S16, s16be,
"PCM signed 16-bit big-endian");
698 PCM_CODEC (S16BE_PLANAR, S16P,s16be_planar,
"PCM signed 16-bit big-endian planar");
699 PCM_CODEC (S16LE, S16, s16le,
"PCM signed 16-bit little-endian");
700 PCM_CODEC (S16LE_PLANAR, S16P,s16le_planar,
"PCM signed 16-bit little-endian planar");
701 PCM_CODEC (S24BE, S32, s24be,
"PCM signed 24-bit big-endian");
702 PCM_CODEC (S24DAUD, S16, s24daud,
"PCM D-Cinema audio signed 24-bit");
703 PCM_CODEC (S24LE, S32, s24le,
"PCM signed 24-bit little-endian");
704 PCM_CODEC (S24LE_PLANAR, S32P,s24le_planar,
"PCM signed 24-bit little-endian planar");
705 PCM_CODEC (S32BE, S32, s32be,
"PCM signed 32-bit big-endian");
706 PCM_CODEC (S32LE, S32, s32le,
"PCM signed 32-bit little-endian");
707 PCM_CODEC (S32LE_PLANAR, S32P,s32le_planar,
"PCM signed 32-bit little-endian planar");
709 PCM_CODEC (U16BE, S16, u16be,
"PCM unsigned 16-bit big-endian");
710 PCM_CODEC (U16LE, S16, u16le,
"PCM unsigned 16-bit little-endian");
711 PCM_CODEC (U24BE, S32, u24be,
"PCM unsigned 24-bit big-endian");
712 PCM_CODEC (U24LE, S32, u24le,
"PCM unsigned 24-bit little-endian");
713 PCM_CODEC (U32BE, S32, u32be,
"PCM unsigned 32-bit big-endian");
714 PCM_CODEC (U32LE, S32, u32le,
"PCM unsigned 32-bit little-endian");
715 PCM_CODEC (S64BE, S64, s64be,
"PCM signed 64-bit big-endian");
716 PCM_CODEC (S64LE, S64, s64le,
"PCM signed 64-bit little-endian");