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"
39
41 {
43 #if !CONFIG_HARDCODED_TABLES
45 #define INIT_ONCE(id, name) \
46 case AV_CODEC_ID_PCM_ ## id: \
47 if (CONFIG_PCM_ ## id ## _ENCODER) { \
48 static AVOnce init_static_once = AV_ONCE_INIT; \
49 ff_thread_once(&init_static_once, pcm_ ## name ## _tableinit); \
50 } \
51 break
55 default:
56 break;
57 }
58 #endif
59
63
64 return 0;
65 }
66
67 /**
68 * Write PCM samples macro
69 * @param type Datatype of native machine format
70 * @param endian bytestream_put_xxx() suffix
71 * @param src Source pointer (variable name)
72 * @param dst Destination pointer (variable name)
73 * @param n Total number of samples (variable name)
74 * @param shift Bitshift (bits)
75 * @param offset Sample value offset
76 */
77 #define ENCODE(type, endian, src, dst, n, shift, offset) \
78 samples_ ## type = (const type *) src; \
79 for (; n > 0; n--) { \
80 register type v = (*samples_ ## type++ >> shift) + offset; \
81 bytestream_put_ ## endian(&dst, v); \
82 }
83
84 #define ENCODE_PLANAR(type, endian, dst, n, shift, offset) \
85 n /= avctx->ch_layout.nb_channels; \
86 for (c = 0; c < avctx->ch_layout.nb_channels; c++) { \
87 int i; \
88 samples_ ## type = (const type *) frame->extended_data[c]; \
89 for (i = n; i > 0; i--) { \
90 register type v = (*samples_ ## type++ >> shift) + offset; \
91 bytestream_put_ ## endian(&dst, v); \
92 } \
93 }
94
97 {
98 int n,
c, sample_size, v,
ret;
100 unsigned char *dst;
101 const uint8_t *samples_uint8_t;
102 const int16_t *samples_int16_t;
103 const int32_t *samples_int32_t;
104 const int64_t *samples_int64_t;
105 const uint16_t *samples_uint16_t;
106 const uint32_t *samples_uint32_t;
107
111
115
119 break;
122 break;
125 break;
128 break;
131 break;
134 break;
137 break;
139 for (; n > 0; n--) {
142 tmp <<= 4;
// sync flags would go here
143 bytestream_put_be24(&dst,
tmp);
145 }
146 break;
149 break;
152 break;
155 break;
158 break;
159 #if HAVE_BIGENDIAN
163 break;
167 break;
170 break;
173 break;
176 break;
182 #else
186 break;
190 break;
193 break;
196 break;
202 #endif /* HAVE_BIGENDIAN */
204 memcpy(dst,
samples, n * sample_size);
205 break;
206 #if HAVE_BIGENDIAN
208 #else
211 #endif /* HAVE_BIGENDIAN */
214 const uint8_t *
src =
frame->extended_data[
c];
216 }
217 break;
219 for (; n > 0; n--) {
222 }
223 break;
225 for (; n > 0; n--) {
228 }
229 break;
231 for (; n > 0; n--) {
234 }
235 break;
236 default:
237 return -1;
238 }
239
240 *got_packet_ptr = 1;
241 return 0;
242 }
243
250
252 {
256
259 for (
i = 0;
i < 256;
i++)
261 break;
263 for (
i = 0;
i < 256;
i++)
265 break;
267 for (
i = 0;
i < 256;
i++)
269 break;
274
277 if (!fdsp)
281 break;
282 default:
283 break;
284 }
285
287
290
291 return 0;
292 }
293
294 /**
295 * Read PCM samples macro
296 * @param size Data size of native machine format
297 * @param endian bytestream_get_xxx() endian suffix
298 * @param src Source pointer (variable name)
299 * @param dst Destination pointer (variable name)
300 * @param n Total number of samples (variable name)
301 * @param shift Bitshift (bits)
302 * @param offset Sample value offset
303 */
304 #define DECODE(size, endian, src, dst, n, shift, offset) \
305 for (; n > 0; n--) { \
306 uint ## size ## _t v = bytestream_get_ ## endian(&src); \
307 AV_WN ## size ## A(dst, (uint ## size ## _t)(v - offset) << shift); \
308 dst += size / 8; \
309 }
310
311 #define DECODE_PLANAR(size, endian, src, dst, n, shift, offset) \
312 n /= channels; \
313 for (c = 0; c < avctx->ch_layout.nb_channels; c++) { \
314 int i; \
315 dst = frame->extended_data[c]; \
316 for (i = n; i > 0; i--) { \
317 uint ## size ## _t v = bytestream_get_ ## endian(&src); \
318 AV_WN ## size ## A(dst, (uint ## size ##_t)(v - offset) << shift); \
319 dst += size / 8; \
320 } \
321 }
322
324 int *got_frame_ptr,
AVPacket *avpkt)
325 {
326 const uint8_t *
src = avpkt->
data;
327 int buf_size = avpkt->
size;
330 int sample_size,
c, n,
ret, samples_per_block;
333
335
336 /* av_get_bits_per_sample returns 0 for AV_CODEC_ID_PCM_DVD */
337 samples_per_block = 1;
339 /* we process 40-bit blocks per channel for LXF */
340 samples_per_block = 2;
341 sample_size = 5;
342 }
343
344 if (sample_size == 0) {
347 }
348
352 }
353
357 }
358
360
361 if (n && buf_size % n) {
362 if (buf_size < n) {
364 "Invalid PCM packet, data has size %d but at least a size of %d was expected\n",
365 buf_size, n);
367 } else
368 buf_size -= buf_size % n;
369 }
370
371 n = buf_size / sample_size;
372
373 /* get output buffer */
378
382 break;
385 break;
388 break;
391 break;
394 break;
397 break;
400 break;
402 for (; n > 0; n--) {
403 uint32_t v = bytestream_get_be24(&
src);
404 v >>= 4; // sync flags are here
408 }
409 break;
412 break;
415 break;
417 for (; n > 0; n--)
419 break;
421 for (; n > 0; n--) {
422 int sign = *
src >> 7;
423 int magn = *
src & 0x7f;
424 *
samples++ = sign ? 128 - magn : 128 + magn;
426 }
427 break;
433 for (
i = n;
i > 0;
i--)
435 }
436 break;
437 #if HAVE_BIGENDIAN
441 break;
447 break;
450 break;
453 break;
456 break;
462 #else
466 break;
470 break;
473 break;
476 break;
484 #endif /* HAVE_BIGENDIAN */
487 break;
488 #if HAVE_BIGENDIAN
490 #else
493 #endif /* HAVE_BIGENDIAN */
498 }
499 break;
503 for (; n > 0; n--) {
506 }
507 break;
509 {
515 // extract low 20 bits and expand to 32 bits
516 *dst_int32_t++ = ((uint32_t)
src[2]<<28) |
519 ((
src[2] & 0x0F) << 8) |
521 // extract high 20 bits and expand to 32 bits
522 *dst_int32_t++ = ((uint32_t)
src[4]<<24) |
524 ((
src[2] & 0xF0) << 8) |
528 }
529 }
530 break;
531 }
532 default:
533 return -1;
534 }
535
538 s->vector_fmul_scalar((
float *)
frame->extended_data[0],
539 (
const float *)
frame->extended_data[0],
541 emms_c();
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, \
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. */