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 #define INIT_ONCE(id, name) \
47 case AV_CODEC_ID_PCM_ ## id: \
48 if (CONFIG_PCM_ ## id ## _ENCODER) { \
49 static AVOnce init_static_once = AV_ONCE_INIT; \
50 ff_thread_once(&init_static_once, pcm_ ## name ## _tableinit); \
51 } \
52 break
56 default:
57 break;
58 }
59 #endif
60
64
65 return 0;
66 }
67
68 /**
69 * Write PCM samples macro
70 * @param type Datatype of native machine format
71 * @param endian bytestream_put_xxx() suffix
72 * @param src Source pointer (variable name)
73 * @param dst Destination pointer (variable name)
74 * @param n Total number of samples (variable name)
75 * @param shift Bitshift (bits)
76 * @param offset Sample value offset
77 */
78 #define ENCODE(type, endian, src, dst, n, shift, offset) \
79 samples_ ## type = (const type *) src; \
80 for (; n > 0; n--) { \
81 register type v = (*samples_ ## type++ >> shift) + offset; \
82 bytestream_put_ ## endian(&dst, v); \
83 }
84
85 #define ENCODE_PLANAR(type, endian, dst, n, shift, offset) \
86 n /= avctx->ch_layout.nb_channels; \
87 for (c = 0; c < avctx->ch_layout.nb_channels; c++) { \
88 int i; \
89 samples_ ## type = (const type *) frame->extended_data[c]; \
90 for (i = n; i > 0; i--) { \
91 register type v = (*samples_ ## type++ >> shift) + offset; \
92 bytestream_put_ ## endian(&dst, v); \
93 } \
94 }
95
98 {
99 int n,
c, sample_size, v,
ret;
102 const uint8_t *samples_uint8_t;
103 const int16_t *samples_int16_t;
104 const int32_t *samples_int32_t;
105 const int64_t *samples_int64_t;
106 const uint16_t *samples_uint16_t;
107 const uint32_t *samples_uint32_t;
108
112
116
120 break;
123 break;
126 break;
129 break;
132 break;
135 break;
138 break;
140 for (; n > 0; n--) {
143 tmp <<= 4;
// sync flags would go here
144 bytestream_put_be24(&
dst,
tmp);
146 }
147 break;
150 break;
153 break;
156 break;
159 break;
160 #if HAVE_BIGENDIAN
164 break;
168 break;
171 break;
174 break;
177 break;
183 #else
187 break;
191 break;
194 break;
197 break;
203 #endif /* HAVE_BIGENDIAN */
206 break;
207 #if HAVE_BIGENDIAN
209 #else
212 #endif /* HAVE_BIGENDIAN */
215 const uint8_t *
src =
frame->extended_data[
c];
217 }
218 break;
220 for (; n > 0; n--) {
223 }
224 break;
226 for (; n > 0; n--) {
229 }
230 break;
232 for (; n > 0; n--) {
235 }
236 break;
237 default:
238 return -1;
239 }
240
241 *got_packet_ptr = 1;
242 return 0;
243 }
244
251
253 {
257
260 for (
i = 0;
i < 256;
i++)
262 break;
264 for (
i = 0;
i < 256;
i++)
266 break;
268 for (
i = 0;
i < 256;
i++)
270 break;
275
278 if (!fdsp)
282 break;
283 default:
284 break;
285 }
286
288
291
292 return 0;
293 }
294
295 /**
296 * Read PCM samples macro
297 * @param size Data size of native machine format
298 * @param endian bytestream_get_xxx() endian suffix
299 * @param src Source pointer (variable name)
300 * @param dst Destination pointer (variable name)
301 * @param n Total number of samples (variable name)
302 * @param shift Bitshift (bits)
303 * @param offset Sample value offset
304 */
305 #define DECODE(size, endian, src, dst, n, shift, offset) \
306 for (; n > 0; n--) { \
307 uint ## size ## _t v = bytestream_get_ ## endian(&src); \
308 AV_WN ## size ## A(dst, (uint ## size ## _t)(v - offset) << shift); \
309 dst += size / 8; \
310 }
311
312 #define DECODE_PLANAR(size, endian, src, dst, n, shift, offset) \
313 n /= channels; \
314 for (c = 0; c < avctx->ch_layout.nb_channels; c++) { \
315 int i; \
316 dst = frame->extended_data[c]; \
317 for (i = n; i > 0; i--) { \
318 uint ## size ## _t v = bytestream_get_ ## endian(&src); \
319 AV_WN ## size ## A(dst, (uint ## size ##_t)(v - offset) << shift); \
320 dst += size / 8; \
321 } \
322 }
323
325 int *got_frame_ptr,
AVPacket *avpkt)
326 {
327 const uint8_t *
src = avpkt->
data;
328 int buf_size = avpkt->
size;
331 int sample_size,
c, n,
ret, samples_per_block;
334
336
337 /* av_get_bits_per_sample returns 0 for AV_CODEC_ID_PCM_DVD */
338 samples_per_block = 1;
340 /* we process 40-bit blocks per channel for LXF */
341 samples_per_block = 2;
342 sample_size = 5;
343 }
344
345 if (sample_size == 0) {
348 }
349
353 }
354
358 }
359
361
362 if (n && buf_size % n) {
363 if (buf_size < n) {
365 "Invalid PCM packet, data has size %d but at least a size of %d was expected\n",
366 buf_size, n);
368 } else
369 buf_size -= buf_size % n;
370 }
371
372 n = buf_size / sample_size;
373
374 /* get output buffer */
379
383 break;
386 break;
389 break;
392 break;
395 break;
398 break;
401 break;
403 for (; n > 0; n--) {
404 uint32_t v = bytestream_get_be24(&
src);
405 v >>= 4; // sync flags are here
409 }
410 break;
413 break;
416 break;
418 for (; n > 0; n--)
420 break;
422 for (; n > 0; n--) {
423 int sign = *
src >> 7;
424 int magn = *
src & 0x7f;
425 *
samples++ = sign ? 128 - magn : 128 + magn;
427 }
428 break;
434 for (
i = n;
i > 0;
i--)
436 }
437 break;
438 #if HAVE_BIGENDIAN
442 break;
448 break;
451 break;
454 break;
457 break;
463 #else
467 break;
471 break;
474 break;
477 break;
485 #endif /* HAVE_BIGENDIAN */
488 break;
489 #if HAVE_BIGENDIAN
491 #else
494 #endif /* HAVE_BIGENDIAN */
499 }
500 break;
504 for (; n > 0; n--) {
507 }
508 break;
510 {
516 // extract low 20 bits and expand to 32 bits
517 *dst_int32_t++ = ((uint32_t)
src[2]<<28) |
520 ((
src[2] & 0x0F) << 8) |
522 // extract high 20 bits and expand to 32 bits
523 *dst_int32_t++ = ((uint32_t)
src[4]<<24) |
525 ((
src[2] & 0xF0) << 8) |
529 }
530 }
531 break;
532 }
533 default:
534 return -1;
535 }
536
539 s->vector_fmul_scalar((
float *)
frame->extended_data[0],
540 (
const float *)
frame->extended_data[0],
542 }
543
544 *got_frame_ptr = 1;
545
546 return buf_size;
547 }
548
549 #define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_)
550 #define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_) \
551 const FFCodec ff_ ## name_ ## _encoder = { \
552 .p.name = #name_, \
553 CODEC_LONG_NAME(long_name_), \
554 .p.type = AVMEDIA_TYPE_AUDIO, \
555 .p.id = AV_CODEC_ID_ ## id_, \
556 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_VARIABLE_FRAME_SIZE | \
557 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, \
558 .init = pcm_encode_init, \
559 FF_CODEC_ENCODE_CB(pcm_encode_frame), \
560 .p.sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \
561 AV_SAMPLE_FMT_NONE }, \
562 }
563
564 #define PCM_ENCODER_2(cf, id, sample_fmt, name, long_name) \
565 PCM_ENCODER_ ## cf(id, sample_fmt, name, long_name)
566 #define PCM_ENCODER_3(cf, id, sample_fmt, name, long_name) \
567 PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)
568 #define PCM_ENCODER(id, sample_fmt, name, long_name) \
569 PCM_ENCODER_3(CONFIG_ ## id ## _ENCODER, id, sample_fmt, name, long_name)
570
571 #define PCM_DECODER_0(id, sample_fmt, name, long_name)
572 #define PCM_DECODER_1(id_, sample_fmt_, name_, long_name_) \
573 const FFCodec ff_ ## name_ ## _decoder = { \
574 .p.name = #name_, \
575 CODEC_LONG_NAME(long_name_), \
576 .p.type = AVMEDIA_TYPE_AUDIO, \
577 .p.id = AV_CODEC_ID_ ## id_, \
578 .priv_data_size = sizeof(PCMDecode), \
579 .init = pcm_decode_init, \
580 FF_CODEC_DECODE_CB(pcm_decode_frame), \
581 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE, \
582 .p.sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \
583 AV_SAMPLE_FMT_NONE }, \
584 }
585
586 #define PCM_DECODER_2(cf, id, sample_fmt, name, long_name) \
587 PCM_DECODER_ ## cf(id, sample_fmt, name, long_name)
588 #define PCM_DECODER_3(cf, id, sample_fmt, name, long_name) \
589 PCM_DECODER_2(cf, id, sample_fmt, name, long_name)
590 #define PCM_DECODER(id, sample_fmt, name, long_name) \
591 PCM_DECODER_3(CONFIG_ ## id ## _DECODER, id, sample_fmt, name, long_name)
592
593 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
594 PCM_ENCODER(id, sample_fmt_, name, long_name_); \
595 PCM_DECODER(id, sample_fmt_, name, long_name_)
596
597 /* Note: Do not forget to add new entries to the Makefile as well. */