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"
37
39 {
41 #if !CONFIG_HARDCODED_TABLES
43 #define INIT_ONCE(id, name) \
44 case AV_CODEC_ID_PCM_ ## id: \
45 if (CONFIG_PCM_ ## id ## _ENCODER) { \
46 static AVOnce init_static_once = AV_ONCE_INIT; \
47 ff_thread_once(&init_static_once, pcm_ ## name ## _tableinit); \
48 } \
49 break
53 default:
54 break;
55 }
56 #endif
57
61
62 return 0;
63 }
64
65 /**
66 * Write PCM samples macro
67 * @param type Datatype of native machine format
68 * @param endian bytestream_put_xxx() suffix
69 * @param src Source pointer (variable name)
70 * @param dst Destination pointer (variable name)
71 * @param n Total number of samples (variable name)
72 * @param shift Bitshift (bits)
73 * @param offset Sample value offset
74 */
75 #define ENCODE(type, endian, src, dst, n, shift, offset) \
76 samples_ ## type = (const type *) src; \
77 for (; n > 0; n--) { \
78 register type v = (*samples_ ## type++ >> shift) + offset; \
79 bytestream_put_ ## endian(&dst, v); \
80 }
81
82 #define ENCODE_PLANAR(type, endian, dst, n, shift, offset) \
83 n /= avctx->channels; \
84 for (c = 0; c < avctx->channels; c++) { \
85 int i; \
86 samples_ ## type = (const type *) frame->extended_data[c]; \
87 for (i = n; i > 0; i--) { \
88 register type v = (*samples_ ## type++ >> shift) + offset; \
89 bytestream_put_ ## endian(&dst, v); \
90 } \
91 }
92
95 {
96 int n,
c, sample_size, v,
ret;
98 unsigned char *dst;
99 const uint8_t *samples_uint8_t;
100 const int16_t *samples_int16_t;
101 const int32_t *samples_int32_t;
102 const int64_t *samples_int64_t;
103 const uint16_t *samples_uint16_t;
104 const uint32_t *samples_uint32_t;
105
109
113
117 break;
120 break;
123 break;
126 break;
129 break;
132 break;
135 break;
137 for (; n > 0; n--) {
140 tmp <<= 4;
// sync flags would go here
141 bytestream_put_be24(&dst,
tmp);
143 }
144 break;
147 break;
150 break;
153 break;
156 break;
157 #if HAVE_BIGENDIAN
161 break;
165 break;
168 break;
171 break;
174 break;
180 #else
184 break;
188 break;
191 break;
194 break;
200 #endif /* HAVE_BIGENDIAN */
202 memcpy(dst,
samples, n * sample_size);
203 break;
204 #if HAVE_BIGENDIAN
206 #else
209 #endif /* HAVE_BIGENDIAN */
212 const uint8_t *
src =
frame->extended_data[
c];
214 }
215 break;
217 for (; n > 0; n--) {
220 }
221 break;
223 for (; n > 0; n--) {
226 }
227 break;
229 for (; n > 0; n--) {
232 }
233 break;
234 default:
235 return -1;
236 }
237
238 *got_packet_ptr = 1;
239 return 0;
240 }
241
248
250 {
254
258 }
259
262 for (
i = 0;
i < 256;
i++)
264 break;
266 for (
i = 0;
i < 256;
i++)
268 break;
270 for (
i = 0;
i < 256;
i++)
272 break;
277
280 if (!fdsp)
284 break;
285 default:
286 break;
287 }
288
290
293
294 return 0;
295 }
296
297 /**
298 * Read PCM samples macro
299 * @param size Data size of native machine format
300 * @param endian bytestream_get_xxx() endian suffix
301 * @param src Source pointer (variable name)
302 * @param dst Destination pointer (variable name)
303 * @param n Total number of samples (variable name)
304 * @param shift Bitshift (bits)
305 * @param offset Sample value offset
306 */
307 #define DECODE(size, endian, src, dst, n, shift, offset) \
308 for (; n > 0; n--) { \
309 uint ## size ## _t v = bytestream_get_ ## endian(&src); \
310 AV_WN ## size ## A(dst, (uint ## size ## _t)(v - offset) << shift); \
311 dst += size / 8; \
312 }
313
314 #define DECODE_PLANAR(size, endian, src, dst, n, shift, offset) \
315 n /= avctx->channels; \
316 for (c = 0; c < avctx->channels; c++) { \
317 int i; \
318 dst = frame->extended_data[c]; \
319 for (i = n; i > 0; i--) { \
320 uint ## size ## _t v = bytestream_get_ ## endian(&src); \
321 AV_WN ## size ## A(dst, (uint ## size ##_t)(v - offset) << shift); \
322 dst += size / 8; \
323 } \
324 }
325
327 int *got_frame_ptr,
AVPacket *avpkt)
328 {
329 const uint8_t *
src = avpkt->
data;
330 int buf_size = avpkt->
size;
333 int sample_size,
c, n,
ret, samples_per_block;
336
338
339 /* av_get_bits_per_sample returns 0 for AV_CODEC_ID_PCM_DVD */
340 samples_per_block = 1;
342 /* we process 40-bit blocks per channel for LXF */
343 samples_per_block = 2;
344 sample_size = 5;
345 }
346
347 if (sample_size == 0) {
350 }
351
355 }
356
360 }
361
363
364 if (n && buf_size % n) {
365 if (buf_size < n) {
367 "Invalid PCM packet, data has size %d but at least a size of %d was expected\n",
368 buf_size, n);
370 } else
371 buf_size -= buf_size % n;
372 }
373
374 n = buf_size / sample_size;
375
376 /* get output buffer */
381
385 break;
388 break;
391 break;
394 break;
397 break;
400 break;
403 break;
405 for (; n > 0; n--) {
406 uint32_t v = bytestream_get_be24(&
src);
407 v >>= 4; // sync flags are here
411 }
412 break;
415 break;
418 break;
420 for (; n > 0; n--)
422 break;
424 for (; n > 0; n--) {
425 int sign = *
src >> 7;
426 int magn = *
src & 0x7f;
427 *
samples++ = sign ? 128 - magn : 128 + magn;
429 }
430 break;
436 for (
i = n;
i > 0;
i--)
438 }
439 break;
440 #if HAVE_BIGENDIAN
444 break;
450 break;
453 break;
456 break;
459 break;
465 #else
469 break;
473 break;
476 break;
479 break;
487 #endif /* HAVE_BIGENDIAN */
490 break;
491 #if HAVE_BIGENDIAN
493 #else
496 #endif /* HAVE_BIGENDIAN */
501 }
502 break;
506 for (; n > 0; n--) {
509 }
510 break;
512 {
518 // extract low 20 bits and expand to 32 bits
519 *dst_int32_t++ = ((uint32_t)
src[2]<<28) |
522 ((
src[2] & 0x0F) << 8) |
524 // extract high 20 bits and expand to 32 bits
525 *dst_int32_t++ = ((uint32_t)
src[4]<<24) |
527 ((
src[2] & 0xF0) << 8) |
531 }
532 }
533 break;
534 }
535 default:
536 return -1;
537 }
538
541 s->vector_fmul_scalar((
float *)
frame->extended_data[0],
542 (
const float *)
frame->extended_data[0],
544 emms_c();
545 }
546
547 *got_frame_ptr = 1;
548
549 return buf_size;
550 }
551
552 #define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_)
553 #define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_) \
554 const AVCodec ff_ ## name_ ## _encoder = { \
555 .name = #name_, \
556 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
557 .type = AVMEDIA_TYPE_AUDIO, \
558 .id = AV_CODEC_ID_ ## id_, \
559 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_VARIABLE_FRAME_SIZE, \
560 .init = pcm_encode_init, \
561 .encode2 = pcm_encode_frame, \
562 .sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \
563 AV_SAMPLE_FMT_NONE }, \
564 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
565 }
566
567 #define PCM_ENCODER_2(cf, id, sample_fmt, name, long_name) \
568 PCM_ENCODER_ ## cf(id, sample_fmt, name, long_name)
569 #define PCM_ENCODER_3(cf, id, sample_fmt, name, long_name) \
570 PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)
571 #define PCM_ENCODER(id, sample_fmt, name, long_name) \
572 PCM_ENCODER_3(CONFIG_ ## id ## _ENCODER, id, sample_fmt, name, long_name)
573
574 #define PCM_DECODER_0(id, sample_fmt, name, long_name)
575 #define PCM_DECODER_1(id_, sample_fmt_, name_, long_name_) \
576 const AVCodec ff_ ## name_ ## _decoder = { \
577 .name = #name_, \
578 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
579 .type = AVMEDIA_TYPE_AUDIO, \
580 .id = AV_CODEC_ID_ ## id_, \
581 .priv_data_size = sizeof(PCMDecode), \
582 .init = pcm_decode_init, \
583 .decode = pcm_decode_frame, \
584 .capabilities = AV_CODEC_CAP_DR1, \
585 .sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \
586 AV_SAMPLE_FMT_NONE }, \
587 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
588 }
589
590 #define PCM_DECODER_2(cf, id, sample_fmt, name, long_name) \
591 PCM_DECODER_ ## cf(id, sample_fmt, name, long_name)
592 #define PCM_DECODER_3(cf, id, sample_fmt, name, long_name) \
593 PCM_DECODER_2(cf, id, sample_fmt, name, long_name)
594 #define PCM_DECODER(id, sample_fmt, name, long_name) \
595 PCM_DECODER_3(CONFIG_ ## id ## _DECODER, id, sample_fmt, name, long_name)
596
597 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
598 PCM_ENCODER(id, sample_fmt_, name, long_name_); \
599 PCM_DECODER(id, sample_fmt_, name, long_name_)
600
601 /* Note: Do not forget to add new entries to the Makefile as well. */