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 AV_CODEC_ID_PCM_ALAW:
00040 pcm_alaw_tableinit();
00041 break;
00042 case AV_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 AV_CODEC_ID_PCM_U32LE:
00106 ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
00107 break;
00108 case AV_CODEC_ID_PCM_U32BE:
00109 ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
00110 break;
00111 case AV_CODEC_ID_PCM_S24LE:
00112 ENCODE(int32_t, le24, samples, dst, n, 8, 0)
00113 break;
00114 case AV_CODEC_ID_PCM_S24BE:
00115 ENCODE(int32_t, be24, samples, dst, n, 8, 0)
00116 break;
00117 case AV_CODEC_ID_PCM_U24LE:
00118 ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
00119 break;
00120 case AV_CODEC_ID_PCM_U24BE:
00121 ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
00122 break;
00123 case AV_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 AV_CODEC_ID_PCM_U16LE:
00133 ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
00134 break;
00135 case AV_CODEC_ID_PCM_U16BE:
00136 ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
00137 break;
00138 case AV_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 AV_CODEC_ID_PCM_F64LE:
00147 ENCODE(int64_t, le64, samples, dst, n, 0, 0)
00148 break;
00149 case AV_CODEC_ID_PCM_S32LE:
00150 case AV_CODEC_ID_PCM_F32LE:
00151 ENCODE(int32_t, le32, samples, dst, n, 0, 0)
00152 break;
00153 case AV_CODEC_ID_PCM_S16LE:
00154 ENCODE(int16_t, le16, samples, dst, n, 0, 0)
00155 break;
00156 case AV_CODEC_ID_PCM_F64BE:
00157 case AV_CODEC_ID_PCM_F32BE:
00158 case AV_CODEC_ID_PCM_S32BE:
00159 case AV_CODEC_ID_PCM_S16BE:
00160 #else
00161 case AV_CODEC_ID_PCM_F64BE:
00162 ENCODE(int64_t, be64, samples, dst, n, 0, 0)
00163 break;
00164 case AV_CODEC_ID_PCM_F32BE:
00165 case AV_CODEC_ID_PCM_S32BE:
00166 ENCODE(int32_t, be32, samples, dst, n, 0, 0)
00167 break;
00168 case AV_CODEC_ID_PCM_S16BE:
00169 ENCODE(int16_t, be16, samples, dst, n, 0, 0)
00170 break;
00171 case AV_CODEC_ID_PCM_F64LE:
00172 case AV_CODEC_ID_PCM_F32LE:
00173 case AV_CODEC_ID_PCM_S32LE:
00174 case AV_CODEC_ID_PCM_S16LE:
00175 #endif
00176 case AV_CODEC_ID_PCM_U8:
00177 memcpy(dst, samples, n * sample_size);
00178 dst += n * sample_size;
00179 break;
00180 case AV_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 AV_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 AV_CODEC_ID_PCM_ALAW:
00217 for (i = 0; i < 256; i++)
00218 s->table[i] = alaw2linear(i);
00219 break;
00220 case AV_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 (AV_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 == AV_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,
00297 "Invalid PCM packet, data has size %d but at least a size of %d was expected\n",
00298 buf_size, n);
00299 return AVERROR_INVALIDDATA;
00300 } else
00301 buf_size -= buf_size % n;
00302 }
00303
00304 n = buf_size / sample_size;
00305
00306
00307 s->frame.nb_samples = n * samples_per_block / avctx->channels;
00308 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00309 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00310 return ret;
00311 }
00312 samples = s->frame.data[0];
00313
00314 switch (avctx->codec->id) {
00315 case AV_CODEC_ID_PCM_U32LE:
00316 DECODE(32, le32, src, samples, n, 0, 0x80000000)
00317 break;
00318 case AV_CODEC_ID_PCM_U32BE:
00319 DECODE(32, be32, src, samples, n, 0, 0x80000000)
00320 break;
00321 case AV_CODEC_ID_PCM_S24LE:
00322 DECODE(32, le24, src, samples, n, 8, 0)
00323 break;
00324 case AV_CODEC_ID_PCM_S24BE:
00325 DECODE(32, be24, src, samples, n, 8, 0)
00326 break;
00327 case AV_CODEC_ID_PCM_U24LE:
00328 DECODE(32, le24, src, samples, n, 8, 0x800000)
00329 break;
00330 case AV_CODEC_ID_PCM_U24BE:
00331 DECODE(32, be24, src, samples, n, 8, 0x800000)
00332 break;
00333 case AV_CODEC_ID_PCM_S24DAUD:
00334 for (; n > 0; n--) {
00335 uint32_t v = bytestream_get_be24(&src);
00336 v >>= 4;
00337 AV_WN16A(samples, av_reverse[(v >> 8) & 0xff] +
00338 (av_reverse[v & 0xff] << 8));
00339 samples += 2;
00340 }
00341 break;
00342 case AV_CODEC_ID_PCM_S16LE_PLANAR:
00343 {
00344 int i;
00345 n /= avctx->channels;
00346 for (c = 0; c < avctx->channels; c++) {
00347 samples = s->frame.data[c];
00348 for (i = n; i > 0; i--) {
00349 AV_WN16A(samples, bytestream_get_le16(&src));
00350 samples += 2;
00351 }
00352 }
00353 break;
00354 }
00355 case AV_CODEC_ID_PCM_U16LE:
00356 DECODE(16, le16, src, samples, n, 0, 0x8000)
00357 break;
00358 case AV_CODEC_ID_PCM_U16BE:
00359 DECODE(16, be16, src, samples, n, 0, 0x8000)
00360 break;
00361 case AV_CODEC_ID_PCM_S8:
00362 for (; n > 0; n--)
00363 *samples++ = *src++ + 128;
00364 break;
00365 #if HAVE_BIGENDIAN
00366 case AV_CODEC_ID_PCM_F64LE:
00367 DECODE(64, le64, src, samples, n, 0, 0)
00368 break;
00369 case AV_CODEC_ID_PCM_S32LE:
00370 case AV_CODEC_ID_PCM_F32LE:
00371 DECODE(32, le32, src, samples, n, 0, 0)
00372 break;
00373 case AV_CODEC_ID_PCM_S16LE:
00374 DECODE(16, le16, src, samples, n, 0, 0)
00375 break;
00376 case AV_CODEC_ID_PCM_F64BE:
00377 case AV_CODEC_ID_PCM_F32BE:
00378 case AV_CODEC_ID_PCM_S32BE:
00379 case AV_CODEC_ID_PCM_S16BE:
00380 #else
00381 case AV_CODEC_ID_PCM_F64BE:
00382 DECODE(64, be64, src, samples, n, 0, 0)
00383 break;
00384 case AV_CODEC_ID_PCM_F32BE:
00385 case AV_CODEC_ID_PCM_S32BE:
00386 DECODE(32, be32, src, samples, n, 0, 0)
00387 break;
00388 case AV_CODEC_ID_PCM_S16BE:
00389 DECODE(16, be16, src, samples, n, 0, 0)
00390 break;
00391 case AV_CODEC_ID_PCM_F64LE:
00392 case AV_CODEC_ID_PCM_F32LE:
00393 case AV_CODEC_ID_PCM_S32LE:
00394 case AV_CODEC_ID_PCM_S16LE:
00395 #endif
00396 case AV_CODEC_ID_PCM_U8:
00397 memcpy(samples, src, n * sample_size);
00398 break;
00399 case AV_CODEC_ID_PCM_ZORK:
00400 for (; n > 0; n--) {
00401 int v = *src++;
00402 if (v < 128)
00403 v = 128 - v;
00404 *samples++ = v;
00405 }
00406 break;
00407 case AV_CODEC_ID_PCM_ALAW:
00408 case AV_CODEC_ID_PCM_MULAW:
00409 for (; n > 0; n--) {
00410 AV_WN16A(samples, s->table[*src++]);
00411 samples += 2;
00412 }
00413 break;
00414 case AV_CODEC_ID_PCM_DVD:
00415 {
00416 const uint8_t *src8;
00417 dst_int32_t = (int32_t *)s->frame.data[0];
00418 n /= avctx->channels;
00419 switch (avctx->bits_per_coded_sample) {
00420 case 20:
00421 while (n--) {
00422 c = avctx->channels;
00423 src8 = src + 4 * c;
00424 while (c--) {
00425 *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8 & 0xf0) << 8);
00426 *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++ & 0x0f) << 12);
00427 }
00428 src = src8;
00429 }
00430 break;
00431 case 24:
00432 while (n--) {
00433 c = avctx->channels;
00434 src8 = src + 4 * c;
00435 while (c--) {
00436 *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
00437 *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
00438 }
00439 src = src8;
00440 }
00441 break;
00442 }
00443 break;
00444 }
00445 case AV_CODEC_ID_PCM_LXF:
00446 {
00447 int i;
00448 n /= avctx->channels;
00449
00450 for (c = 0; c < avctx->channels; c++) {
00451 dst_int32_t = (int32_t *)s->frame.data[c];
00452 for (i = 0; i < n; i++) {
00453
00454 *dst_int32_t++ = (src[2] << 28) |
00455 (src[1] << 20) |
00456 (src[0] << 12) |
00457 ((src[2] & 0xF) << 8) |
00458 src[1];
00459
00460 *dst_int32_t++ = (src[4] << 24) |
00461 (src[3] << 16) |
00462 ((src[2] & 0xF0) << 8) |
00463 (src[4] << 4) |
00464 (src[3] >> 4);
00465 src += 5;
00466 }
00467 }
00468 break;
00469 }
00470 default:
00471 return -1;
00472 }
00473
00474 *got_frame_ptr = 1;
00475 *(AVFrame *)data = s->frame;
00476
00477 return buf_size;
00478 }
00479
00480 #if CONFIG_ENCODERS
00481 #define PCM_ENCODER(id_, sample_fmt_, name_, long_name_) \
00482 AVCodec ff_ ## name_ ## _encoder = { \
00483 .name = #name_, \
00484 .type = AVMEDIA_TYPE_AUDIO, \
00485 .id = id_, \
00486 .init = pcm_encode_init, \
00487 .encode2 = pcm_encode_frame, \
00488 .close = pcm_encode_close, \
00489 .capabilities = CODEC_CAP_VARIABLE_FRAME_SIZE, \
00490 .sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \
00491 AV_SAMPLE_FMT_NONE }, \
00492 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
00493 }
00494 #else
00495 #define PCM_ENCODER(id, sample_fmt_, name, long_name_)
00496 #endif
00497
00498 #if CONFIG_DECODERS
00499 #define PCM_DECODER(id_, sample_fmt_, name_, long_name_) \
00500 AVCodec ff_ ## name_ ## _decoder = { \
00501 .name = #name_, \
00502 .type = AVMEDIA_TYPE_AUDIO, \
00503 .id = id_, \
00504 .priv_data_size = sizeof(PCMDecode), \
00505 .init = pcm_decode_init, \
00506 .decode = pcm_decode_frame, \
00507 .capabilities = CODEC_CAP_DR1, \
00508 .sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \
00509 AV_SAMPLE_FMT_NONE }, \
00510 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
00511 }
00512 #else
00513 #define PCM_DECODER(id, sample_fmt_, name, long_name_)
00514 #endif
00515
00516 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
00517 PCM_ENCODER(id, sample_fmt_, name, long_name_); \
00518 PCM_DECODER(id, sample_fmt_, name, long_name_)
00519
00520
00521 PCM_CODEC (AV_CODEC_ID_PCM_ALAW, AV_SAMPLE_FMT_S16, pcm_alaw, "PCM A-law / G.711 A-law");
00522 PCM_DECODER(AV_CODEC_ID_PCM_DVD, AV_SAMPLE_FMT_S32, pcm_dvd, "PCM signed 20|24-bit big-endian");
00523 PCM_CODEC (AV_CODEC_ID_PCM_F32BE, AV_SAMPLE_FMT_FLT, pcm_f32be, "PCM 32-bit floating point big-endian");
00524 PCM_CODEC (AV_CODEC_ID_PCM_F32LE, AV_SAMPLE_FMT_FLT, pcm_f32le, "PCM 32-bit floating point little-endian");
00525 PCM_CODEC (AV_CODEC_ID_PCM_F64BE, AV_SAMPLE_FMT_DBL, pcm_f64be, "PCM 64-bit floating point big-endian");
00526 PCM_CODEC (AV_CODEC_ID_PCM_F64LE, AV_SAMPLE_FMT_DBL, pcm_f64le, "PCM 64-bit floating point little-endian");
00527 PCM_DECODER(AV_CODEC_ID_PCM_LXF, AV_SAMPLE_FMT_S32P,pcm_lxf, "PCM signed 20-bit little-endian planar");
00528 PCM_CODEC (AV_CODEC_ID_PCM_MULAW, AV_SAMPLE_FMT_S16, pcm_mulaw, "PCM mu-law / G.711 mu-law");
00529 PCM_CODEC (AV_CODEC_ID_PCM_S8, AV_SAMPLE_FMT_U8, pcm_s8, "PCM signed 8-bit");
00530 PCM_CODEC (AV_CODEC_ID_PCM_S16BE, AV_SAMPLE_FMT_S16, pcm_s16be, "PCM signed 16-bit big-endian");
00531 PCM_CODEC (AV_CODEC_ID_PCM_S16LE, AV_SAMPLE_FMT_S16, pcm_s16le, "PCM signed 16-bit little-endian");
00532 PCM_DECODER(AV_CODEC_ID_PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16le_planar, "PCM 16-bit little-endian planar");
00533 PCM_CODEC (AV_CODEC_ID_PCM_S24BE, AV_SAMPLE_FMT_S32, pcm_s24be, "PCM signed 24-bit big-endian");
00534 PCM_CODEC (AV_CODEC_ID_PCM_S24DAUD, AV_SAMPLE_FMT_S16, pcm_s24daud, "PCM D-Cinema audio signed 24-bit");
00535 PCM_CODEC (AV_CODEC_ID_PCM_S24LE, AV_SAMPLE_FMT_S32, pcm_s24le, "PCM signed 24-bit little-endian");
00536 PCM_CODEC (AV_CODEC_ID_PCM_S32BE, AV_SAMPLE_FMT_S32, pcm_s32be, "PCM signed 32-bit big-endian");
00537 PCM_CODEC (AV_CODEC_ID_PCM_S32LE, AV_SAMPLE_FMT_S32, pcm_s32le, "PCM signed 32-bit little-endian");
00538 PCM_CODEC (AV_CODEC_ID_PCM_U8, AV_SAMPLE_FMT_U8, pcm_u8, "PCM unsigned 8-bit");
00539 PCM_CODEC (AV_CODEC_ID_PCM_U16BE, AV_SAMPLE_FMT_S16, pcm_u16be, "PCM unsigned 16-bit big-endian");
00540 PCM_CODEC (AV_CODEC_ID_PCM_U16LE, AV_SAMPLE_FMT_S16, pcm_u16le, "PCM unsigned 16-bit little-endian");
00541 PCM_CODEC (AV_CODEC_ID_PCM_U24BE, AV_SAMPLE_FMT_S32, pcm_u24be, "PCM unsigned 24-bit big-endian");
00542 PCM_CODEC (AV_CODEC_ID_PCM_U24LE, AV_SAMPLE_FMT_S32, pcm_u24le, "PCM unsigned 24-bit little-endian");
00543 PCM_CODEC (AV_CODEC_ID_PCM_U32BE, AV_SAMPLE_FMT_S32, pcm_u32be, "PCM unsigned 32-bit big-endian");
00544 PCM_CODEC (AV_CODEC_ID_PCM_U32LE, AV_SAMPLE_FMT_S32, pcm_u32le, "PCM unsigned 32-bit little-endian");
00545 PCM_DECODER(AV_CODEC_ID_PCM_ZORK, AV_SAMPLE_FMT_U8, pcm_zork, "PCM Zork");
00546