00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/common.h"
00028 #include "avcodec.h"
00029 #include "bytestream.h"
00030 #include "internal.h"
00031 #include "pcm_tablegen.h"
00032
00033 #define MAX_CHANNELS 64
00034
00035 static av_cold int pcm_encode_init(AVCodecContext *avctx)
00036 {
00037 avctx->frame_size = 0;
00038 switch (avctx->codec->id) {
00039 case CODEC_ID_PCM_ALAW:
00040 pcm_alaw_tableinit();
00041 break;
00042 case CODEC_ID_PCM_MULAW:
00043 pcm_ulaw_tableinit();
00044 break;
00045 default:
00046 break;
00047 }
00048
00049 avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
00050 avctx->block_align = avctx->channels * avctx->bits_per_coded_sample / 8;
00051 avctx->bit_rate = avctx->block_align * avctx->sample_rate * 8;
00052 avctx->coded_frame = avcodec_alloc_frame();
00053 if (!avctx->coded_frame)
00054 return AVERROR(ENOMEM);
00055
00056 return 0;
00057 }
00058
00059 static av_cold int pcm_encode_close(AVCodecContext *avctx)
00060 {
00061 av_freep(&avctx->coded_frame);
00062
00063 return 0;
00064 }
00065
00076 #define ENCODE(type, endian, src, dst, n, shift, offset) \
00077 samples_ ## type = (const type *) src; \
00078 for (; n > 0; n--) { \
00079 register type v = (*samples_ ## type++ >> shift) + offset; \
00080 bytestream_put_ ## endian(&dst, v); \
00081 }
00082
00083 static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00084 const AVFrame *frame, int *got_packet_ptr)
00085 {
00086 int n, sample_size, v, ret;
00087 const short *samples;
00088 unsigned char *dst;
00089 const uint8_t *srcu8;
00090 const int16_t *samples_int16_t;
00091 const int32_t *samples_int32_t;
00092 const int64_t *samples_int64_t;
00093 const uint16_t *samples_uint16_t;
00094 const uint32_t *samples_uint32_t;
00095
00096 sample_size = av_get_bits_per_sample(avctx->codec->id) / 8;
00097 n = frame->nb_samples * avctx->channels;
00098 samples = (const short *)frame->data[0];
00099
00100 if ((ret = ff_alloc_packet2(avctx, avpkt, n * sample_size)))
00101 return ret;
00102 dst = avpkt->data;
00103
00104 switch (avctx->codec->id) {
00105 case CODEC_ID_PCM_U32LE:
00106 ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
00107 break;
00108 case CODEC_ID_PCM_U32BE:
00109 ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
00110 break;
00111 case CODEC_ID_PCM_S24LE:
00112 ENCODE(int32_t, le24, samples, dst, n, 8, 0)
00113 break;
00114 case CODEC_ID_PCM_S24BE:
00115 ENCODE(int32_t, be24, samples, dst, n, 8, 0)
00116 break;
00117 case CODEC_ID_PCM_U24LE:
00118 ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
00119 break;
00120 case CODEC_ID_PCM_U24BE:
00121 ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
00122 break;
00123 case CODEC_ID_PCM_S24DAUD:
00124 for (; n > 0; n--) {
00125 uint32_t tmp = av_reverse[(*samples >> 8) & 0xff] +
00126 (av_reverse[*samples & 0xff] << 8);
00127 tmp <<= 4;
00128 bytestream_put_be24(&dst, tmp);
00129 samples++;
00130 }
00131 break;
00132 case CODEC_ID_PCM_U16LE:
00133 ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
00134 break;
00135 case CODEC_ID_PCM_U16BE:
00136 ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
00137 break;
00138 case CODEC_ID_PCM_S8:
00139 srcu8 = frame->data[0];
00140 for (; n > 0; n--) {
00141 v = *srcu8++;
00142 *dst++ = v - 128;
00143 }
00144 break;
00145 #if HAVE_BIGENDIAN
00146 case CODEC_ID_PCM_F64LE:
00147 ENCODE(int64_t, le64, samples, dst, n, 0, 0)
00148 break;
00149 case CODEC_ID_PCM_S32LE:
00150 case CODEC_ID_PCM_F32LE:
00151 ENCODE(int32_t, le32, samples, dst, n, 0, 0)
00152 break;
00153 case CODEC_ID_PCM_S16LE:
00154 ENCODE(int16_t, le16, samples, dst, n, 0, 0)
00155 break;
00156 case CODEC_ID_PCM_F64BE:
00157 case CODEC_ID_PCM_F32BE:
00158 case CODEC_ID_PCM_S32BE:
00159 case CODEC_ID_PCM_S16BE:
00160 #else
00161 case CODEC_ID_PCM_F64BE:
00162 ENCODE(int64_t, be64, samples, dst, n, 0, 0)
00163 break;
00164 case CODEC_ID_PCM_F32BE:
00165 case CODEC_ID_PCM_S32BE:
00166 ENCODE(int32_t, be32, samples, dst, n, 0, 0)
00167 break;
00168 case CODEC_ID_PCM_S16BE:
00169 ENCODE(int16_t, be16, samples, dst, n, 0, 0)
00170 break;
00171 case CODEC_ID_PCM_F64LE:
00172 case CODEC_ID_PCM_F32LE:
00173 case CODEC_ID_PCM_S32LE:
00174 case CODEC_ID_PCM_S16LE:
00175 #endif
00176 case CODEC_ID_PCM_U8:
00177 memcpy(dst, samples, n * sample_size);
00178 dst += n * sample_size;
00179 break;
00180 case CODEC_ID_PCM_ALAW:
00181 for (; n > 0; n--) {
00182 v = *samples++;
00183 *dst++ = linear_to_alaw[(v + 32768) >> 2];
00184 }
00185 break;
00186 case CODEC_ID_PCM_MULAW:
00187 for (; n > 0; n--) {
00188 v = *samples++;
00189 *dst++ = linear_to_ulaw[(v + 32768) >> 2];
00190 }
00191 break;
00192 default:
00193 return -1;
00194 }
00195
00196 *got_packet_ptr = 1;
00197 return 0;
00198 }
00199
00200 typedef struct PCMDecode {
00201 AVFrame frame;
00202 short table[256];
00203 } PCMDecode;
00204
00205 static av_cold int pcm_decode_init(AVCodecContext *avctx)
00206 {
00207 PCMDecode *s = avctx->priv_data;
00208 int i;
00209
00210 if (avctx->channels <= 0 || avctx->channels > MAX_CHANNELS) {
00211 av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
00212 return AVERROR(EINVAL);
00213 }
00214
00215 switch (avctx->codec->id) {
00216 case CODEC_ID_PCM_ALAW:
00217 for (i = 0; i < 256; i++)
00218 s->table[i] = alaw2linear(i);
00219 break;
00220 case CODEC_ID_PCM_MULAW:
00221 for (i = 0; i < 256; i++)
00222 s->table[i] = ulaw2linear(i);
00223 break;
00224 default:
00225 break;
00226 }
00227
00228 avctx->sample_fmt = avctx->codec->sample_fmts[0];
00229
00230 if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
00231 avctx->bits_per_raw_sample = av_get_bits_per_sample(avctx->codec->id);
00232
00233 avcodec_get_frame_defaults(&s->frame);
00234 avctx->coded_frame = &s->frame;
00235
00236 return 0;
00237 }
00238
00249 #define DECODE(size, endian, src, dst, n, shift, offset) \
00250 for (; n > 0; n--) { \
00251 uint ## size ## _t v = bytestream_get_ ## endian(&src); \
00252 AV_WN ## size ## A(dst, (v - offset) << shift); \
00253 dst += size / 8; \
00254 }
00255
00256 static int pcm_decode_frame(AVCodecContext *avctx, void *data,
00257 int *got_frame_ptr, AVPacket *avpkt)
00258 {
00259 const uint8_t *src = avpkt->data;
00260 int buf_size = avpkt->size;
00261 PCMDecode *s = avctx->priv_data;
00262 int sample_size, c, n, ret, samples_per_block;
00263 uint8_t *samples;
00264 int32_t *dst_int32_t;
00265
00266 sample_size = av_get_bits_per_sample(avctx->codec_id) / 8;
00267
00268
00269 samples_per_block = 1;
00270 if (CODEC_ID_PCM_DVD == avctx->codec_id) {
00271 if (avctx->bits_per_coded_sample != 20 &&
00272 avctx->bits_per_coded_sample != 24) {
00273 av_log(avctx, AV_LOG_ERROR,
00274 "PCM DVD unsupported sample depth %i\n",
00275 avctx->bits_per_coded_sample);
00276 return AVERROR(EINVAL);
00277 }
00278
00279 samples_per_block = 2;
00280 sample_size = avctx->bits_per_coded_sample * 2 / 8;
00281 } else if (avctx->codec_id == CODEC_ID_PCM_LXF) {
00282
00283 samples_per_block = 2;
00284 sample_size = 5;
00285 }
00286
00287 if (sample_size == 0) {
00288 av_log(avctx, AV_LOG_ERROR, "Invalid sample_size\n");
00289 return AVERROR(EINVAL);
00290 }
00291
00292 n = avctx->channels * sample_size;
00293
00294 if (n && buf_size % n) {
00295 if (buf_size < n) {
00296 av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n");
00297 return -1;
00298 } else
00299 buf_size -= buf_size % n;
00300 }
00301
00302 n = buf_size / sample_size;
00303
00304
00305 s->frame.nb_samples = n * samples_per_block / avctx->channels;
00306 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00307 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00308 return ret;
00309 }
00310 samples = s->frame.data[0];
00311
00312 switch (avctx->codec->id) {
00313 case CODEC_ID_PCM_U32LE:
00314 DECODE(32, le32, src, samples, n, 0, 0x80000000)
00315 break;
00316 case CODEC_ID_PCM_U32BE:
00317 DECODE(32, be32, src, samples, n, 0, 0x80000000)
00318 break;
00319 case CODEC_ID_PCM_S24LE:
00320 DECODE(32, le24, src, samples, n, 8, 0)
00321 break;
00322 case CODEC_ID_PCM_S24BE:
00323 DECODE(32, be24, src, samples, n, 8, 0)
00324 break;
00325 case CODEC_ID_PCM_U24LE:
00326 DECODE(32, le24, src, samples, n, 8, 0x800000)
00327 break;
00328 case CODEC_ID_PCM_U24BE:
00329 DECODE(32, be24, src, samples, n, 8, 0x800000)
00330 break;
00331 case CODEC_ID_PCM_S24DAUD:
00332 for (; n > 0; n--) {
00333 uint32_t v = bytestream_get_be24(&src);
00334 v >>= 4;
00335 AV_WN16A(samples, av_reverse[(v >> 8) & 0xff] +
00336 (av_reverse[v & 0xff] << 8));
00337 samples += 2;
00338 }
00339 break;
00340 case CODEC_ID_PCM_S16LE_PLANAR:
00341 {
00342 int i;
00343 n /= avctx->channels;
00344 for (c = 0; c < avctx->channels; c++) {
00345 samples = s->frame.data[c];
00346 for (i = n; i > 0; i--) {
00347 AV_WN16A(samples, bytestream_get_le16(&src));
00348 samples += 2;
00349 }
00350 }
00351 break;
00352 }
00353 case CODEC_ID_PCM_U16LE:
00354 DECODE(16, le16, src, samples, n, 0, 0x8000)
00355 break;
00356 case CODEC_ID_PCM_U16BE:
00357 DECODE(16, be16, src, samples, n, 0, 0x8000)
00358 break;
00359 case CODEC_ID_PCM_S8:
00360 for (; n > 0; n--)
00361 *samples++ = *src++ + 128;
00362 break;
00363 #if HAVE_BIGENDIAN
00364 case CODEC_ID_PCM_F64LE:
00365 DECODE(64, le64, src, samples, n, 0, 0)
00366 break;
00367 case CODEC_ID_PCM_S32LE:
00368 case CODEC_ID_PCM_F32LE:
00369 DECODE(32, le32, src, samples, n, 0, 0)
00370 break;
00371 case CODEC_ID_PCM_S16LE:
00372 DECODE(16, le16, src, samples, n, 0, 0)
00373 break;
00374 case CODEC_ID_PCM_F64BE:
00375 case CODEC_ID_PCM_F32BE:
00376 case CODEC_ID_PCM_S32BE:
00377 case CODEC_ID_PCM_S16BE:
00378 #else
00379 case CODEC_ID_PCM_F64BE:
00380 DECODE(64, be64, src, samples, n, 0, 0)
00381 break;
00382 case CODEC_ID_PCM_F32BE:
00383 case CODEC_ID_PCM_S32BE:
00384 DECODE(32, be32, src, samples, n, 0, 0)
00385 break;
00386 case CODEC_ID_PCM_S16BE:
00387 DECODE(16, be16, src, samples, n, 0, 0)
00388 break;
00389 case CODEC_ID_PCM_F64LE:
00390 case CODEC_ID_PCM_F32LE:
00391 case CODEC_ID_PCM_S32LE:
00392 case CODEC_ID_PCM_S16LE:
00393 #endif
00394 case CODEC_ID_PCM_U8:
00395 memcpy(samples, src, n * sample_size);
00396 break;
00397 case CODEC_ID_PCM_ZORK:
00398 for (; n > 0; n--) {
00399 int v = *src++;
00400 if (v < 128)
00401 v = 128 - v;
00402 *samples++ = v;
00403 }
00404 break;
00405 case CODEC_ID_PCM_ALAW:
00406 case CODEC_ID_PCM_MULAW:
00407 for (; n > 0; n--) {
00408 AV_WN16A(samples, s->table[*src++]);
00409 samples += 2;
00410 }
00411 break;
00412 case CODEC_ID_PCM_DVD:
00413 {
00414 const uint8_t *src8;
00415 dst_int32_t = (int32_t *)s->frame.data[0];
00416 n /= avctx->channels;
00417 switch (avctx->bits_per_coded_sample) {
00418 case 20:
00419 while (n--) {
00420 c = avctx->channels;
00421 src8 = src + 4 * c;
00422 while (c--) {
00423 *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8 & 0xf0) << 8);
00424 *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++ & 0x0f) << 12);
00425 }
00426 src = src8;
00427 }
00428 break;
00429 case 24:
00430 while (n--) {
00431 c = avctx->channels;
00432 src8 = src + 4 * c;
00433 while (c--) {
00434 *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
00435 *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
00436 }
00437 src = src8;
00438 }
00439 break;
00440 }
00441 break;
00442 }
00443 case CODEC_ID_PCM_LXF:
00444 {
00445 int i;
00446 n /= avctx->channels;
00447
00448 for (c = 0; c < avctx->channels; c++) {
00449 dst_int32_t = (int32_t *)s->frame.data[c];
00450 for (i = 0; i < n; i++) {
00451
00452 *dst_int32_t++ = (src[2] << 28) |
00453 (src[1] << 20) |
00454 (src[0] << 12) |
00455 ((src[2] & 0xF) << 8) |
00456 src[1];
00457
00458 *dst_int32_t++ = (src[4] << 24) |
00459 (src[3] << 16) |
00460 ((src[2] & 0xF0) << 8) |
00461 (src[4] << 4) |
00462 (src[3] >> 4);
00463 src += 5;
00464 }
00465 }
00466 break;
00467 }
00468 default:
00469 return -1;
00470 }
00471
00472 *got_frame_ptr = 1;
00473 *(AVFrame *)data = s->frame;
00474
00475 return buf_size;
00476 }
00477
00478 #if CONFIG_ENCODERS
00479 #define PCM_ENCODER(id_, sample_fmt_, name_, long_name_) \
00480 AVCodec ff_ ## name_ ## _encoder = { \
00481 .name = #name_, \
00482 .type = AVMEDIA_TYPE_AUDIO, \
00483 .id = id_, \
00484 .init = pcm_encode_init, \
00485 .encode2 = pcm_encode_frame, \
00486 .close = pcm_encode_close, \
00487 .capabilities = CODEC_CAP_VARIABLE_FRAME_SIZE, \
00488 .sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \
00489 AV_SAMPLE_FMT_NONE }, \
00490 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
00491 }
00492 #else
00493 #define PCM_ENCODER(id, sample_fmt_, name, long_name_)
00494 #endif
00495
00496 #if CONFIG_DECODERS
00497 #define PCM_DECODER(id_, sample_fmt_, name_, long_name_) \
00498 AVCodec ff_ ## name_ ## _decoder = { \
00499 .name = #name_, \
00500 .type = AVMEDIA_TYPE_AUDIO, \
00501 .id = id_, \
00502 .priv_data_size = sizeof(PCMDecode), \
00503 .init = pcm_decode_init, \
00504 .decode = pcm_decode_frame, \
00505 .capabilities = CODEC_CAP_DR1, \
00506 .sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \
00507 AV_SAMPLE_FMT_NONE }, \
00508 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
00509 }
00510 #else
00511 #define PCM_DECODER(id, sample_fmt_, name, long_name_)
00512 #endif
00513
00514 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
00515 PCM_ENCODER(id, sample_fmt_, name, long_name_); \
00516 PCM_DECODER(id, sample_fmt_, name, long_name_)
00517
00518
00519 PCM_CODEC (CODEC_ID_PCM_ALAW, AV_SAMPLE_FMT_S16, pcm_alaw, "PCM A-law / G.711 A-law");
00520 PCM_DECODER(CODEC_ID_PCM_DVD, AV_SAMPLE_FMT_S32, pcm_dvd, "PCM signed 20|24-bit big-endian");
00521 PCM_CODEC (CODEC_ID_PCM_F32BE, AV_SAMPLE_FMT_FLT, pcm_f32be, "PCM 32-bit floating point big-endian");
00522 PCM_CODEC (CODEC_ID_PCM_F32LE, AV_SAMPLE_FMT_FLT, pcm_f32le, "PCM 32-bit floating point little-endian");
00523 PCM_CODEC (CODEC_ID_PCM_F64BE, AV_SAMPLE_FMT_DBL, pcm_f64be, "PCM 64-bit floating point big-endian");
00524 PCM_CODEC (CODEC_ID_PCM_F64LE, AV_SAMPLE_FMT_DBL, pcm_f64le, "PCM 64-bit floating point little-endian");
00525 PCM_DECODER(CODEC_ID_PCM_LXF, AV_SAMPLE_FMT_S32P,pcm_lxf, "PCM signed 20-bit little-endian planar");
00526 PCM_CODEC (CODEC_ID_PCM_MULAW, AV_SAMPLE_FMT_S16, pcm_mulaw, "PCM mu-law / G.711 mu-law");
00527 PCM_CODEC (CODEC_ID_PCM_S8, AV_SAMPLE_FMT_U8, pcm_s8, "PCM signed 8-bit");
00528 PCM_CODEC (CODEC_ID_PCM_S16BE, AV_SAMPLE_FMT_S16, pcm_s16be, "PCM signed 16-bit big-endian");
00529 PCM_CODEC (CODEC_ID_PCM_S16LE, AV_SAMPLE_FMT_S16, pcm_s16le, "PCM signed 16-bit little-endian");
00530 PCM_DECODER(CODEC_ID_PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16le_planar, "PCM 16-bit little-endian planar");
00531 PCM_CODEC (CODEC_ID_PCM_S24BE, AV_SAMPLE_FMT_S32, pcm_s24be, "PCM signed 24-bit big-endian");
00532 PCM_CODEC (CODEC_ID_PCM_S24DAUD, AV_SAMPLE_FMT_S16, pcm_s24daud, "PCM D-Cinema audio signed 24-bit");
00533 PCM_CODEC (CODEC_ID_PCM_S24LE, AV_SAMPLE_FMT_S32, pcm_s24le, "PCM signed 24-bit little-endian");
00534 PCM_CODEC (CODEC_ID_PCM_S32BE, AV_SAMPLE_FMT_S32, pcm_s32be, "PCM signed 32-bit big-endian");
00535 PCM_CODEC (CODEC_ID_PCM_S32LE, AV_SAMPLE_FMT_S32, pcm_s32le, "PCM signed 32-bit little-endian");
00536 PCM_CODEC (CODEC_ID_PCM_U8, AV_SAMPLE_FMT_U8, pcm_u8, "PCM unsigned 8-bit");
00537 PCM_CODEC (CODEC_ID_PCM_U16BE, AV_SAMPLE_FMT_S16, pcm_u16be, "PCM unsigned 16-bit big-endian");
00538 PCM_CODEC (CODEC_ID_PCM_U16LE, AV_SAMPLE_FMT_S16, pcm_u16le, "PCM unsigned 16-bit little-endian");
00539 PCM_CODEC (CODEC_ID_PCM_U24BE, AV_SAMPLE_FMT_S32, pcm_u24be, "PCM unsigned 24-bit big-endian");
00540 PCM_CODEC (CODEC_ID_PCM_U24LE, AV_SAMPLE_FMT_S32, pcm_u24le, "PCM unsigned 24-bit little-endian");
00541 PCM_CODEC (CODEC_ID_PCM_U32BE, AV_SAMPLE_FMT_S32, pcm_u32be, "PCM unsigned 32-bit big-endian");
00542 PCM_CODEC (CODEC_ID_PCM_U32LE, AV_SAMPLE_FMT_S32, pcm_u32le, "PCM unsigned 32-bit little-endian");
00543 PCM_DECODER(CODEC_ID_PCM_ZORK, AV_SAMPLE_FMT_U8, pcm_zork, "PCM Zork");
00544