00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "libavutil/common.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->coded_frame= avcodec_alloc_frame();
00052 avctx->coded_frame->key_frame= 1;
00053
00054 return 0;
00055 }
00056
00057 static av_cold int pcm_encode_close(AVCodecContext *avctx)
00058 {
00059 av_freep(&avctx->coded_frame);
00060
00061 return 0;
00062 }
00063
00074 #define ENCODE(type, endian, src, dst, n, shift, offset) \
00075 samples_##type = (const type*) src; \
00076 for(;n>0;n--) { \
00077 register type v = (*samples_##type++ >> shift) + offset; \
00078 bytestream_put_##endian(&dst, v); \
00079 }
00080
00081 static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00082 const AVFrame *frame, int *got_packet_ptr)
00083 {
00084 int n, sample_size, v, ret;
00085 const short *samples;
00086 unsigned char *dst;
00087 const uint8_t *srcu8;
00088 const int16_t *samples_int16_t;
00089 const int32_t *samples_int32_t;
00090 const int64_t *samples_int64_t;
00091 const uint16_t *samples_uint16_t;
00092 const uint32_t *samples_uint32_t;
00093
00094 sample_size = av_get_bits_per_sample(avctx->codec->id)/8;
00095 n = frame->nb_samples * avctx->channels;
00096 samples = (const short *)frame->data[0];
00097
00098 if ((ret = ff_alloc_packet(avpkt, n * sample_size))) {
00099 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
00100 return ret;
00101 }
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 avpkt->size = frame->nb_samples * avctx->channels * sample_size;
00197 *got_packet_ptr = 1;
00198 return 0;
00199 }
00200
00201 typedef struct PCMDecode {
00202 AVFrame frame;
00203 short table[256];
00204 } PCMDecode;
00205
00206 static av_cold int pcm_decode_init(AVCodecContext * avctx)
00207 {
00208 PCMDecode *s = avctx->priv_data;
00209 int i;
00210
00211 if (avctx->channels <= 0 || avctx->channels > MAX_CHANNELS) {
00212 av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
00213 return AVERROR(EINVAL);
00214 }
00215
00216 switch(avctx->codec->id) {
00217 case CODEC_ID_PCM_ALAW:
00218 for(i=0;i<256;i++)
00219 s->table[i] = alaw2linear(i);
00220 break;
00221 case CODEC_ID_PCM_MULAW:
00222 for(i=0;i<256;i++)
00223 s->table[i] = ulaw2linear(i);
00224 break;
00225 default:
00226 break;
00227 }
00228
00229 avctx->sample_fmt = avctx->codec->sample_fmts[0];
00230
00231 if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
00232 avctx->bits_per_raw_sample = av_get_bits_per_sample(avctx->codec->id);
00233
00234 avcodec_get_frame_defaults(&s->frame);
00235 avctx->coded_frame = &s->frame;
00236
00237 return 0;
00238 }
00239
00250 #define DECODE(size, endian, src, dst, n, shift, offset) \
00251 for(;n>0;n--) { \
00252 uint##size##_t v = bytestream_get_##endian(&src); \
00253 AV_WN##size##A(dst, (v - offset) << shift); \
00254 dst += size / 8; \
00255 }
00256
00257 static int pcm_decode_frame(AVCodecContext *avctx, void *data,
00258 int *got_frame_ptr, AVPacket *avpkt)
00259 {
00260 const uint8_t *src = avpkt->data;
00261 int buf_size = avpkt->size;
00262 PCMDecode *s = avctx->priv_data;
00263 int sample_size, c, n, ret, samples_per_block;
00264 uint8_t *samples;
00265 int32_t *dst_int32_t;
00266
00267 sample_size = av_get_bits_per_sample(avctx->codec_id)/8;
00268
00269
00270 samples_per_block = 1;
00271 if (CODEC_ID_PCM_DVD == avctx->codec_id) {
00272 if (avctx->bits_per_coded_sample != 20 &&
00273 avctx->bits_per_coded_sample != 24) {
00274 av_log(avctx, AV_LOG_ERROR,
00275 "PCM DVD unsupported sample depth %i\n",
00276 avctx->bits_per_coded_sample);
00277 return AVERROR(EINVAL);
00278 }
00279
00280 samples_per_block = 2;
00281 sample_size = avctx->bits_per_coded_sample * 2 / 8;
00282 } else if (avctx->codec_id == CODEC_ID_PCM_LXF) {
00283
00284 samples_per_block = 2;
00285 sample_size = 5;
00286 }
00287
00288 if (sample_size == 0) {
00289 av_log(avctx, AV_LOG_ERROR, "Invalid sample_size\n");
00290 return AVERROR(EINVAL);
00291 }
00292
00293 n = avctx->channels * sample_size;
00294
00295 if(n && buf_size % n){
00296 if (buf_size < n) {
00297 av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n");
00298 return -1;
00299 }else
00300 buf_size -= buf_size % n;
00301 }
00302
00303 n = buf_size/sample_size;
00304
00305
00306 s->frame.nb_samples = n * samples_per_block / avctx->channels;
00307 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00308 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00309 return ret;
00310 }
00311 samples = s->frame.data[0];
00312
00313 switch(avctx->codec->id) {
00314 case CODEC_ID_PCM_U32LE:
00315 DECODE(32, le32, src, samples, n, 0, 0x80000000)
00316 break;
00317 case CODEC_ID_PCM_U32BE:
00318 DECODE(32, be32, src, samples, n, 0, 0x80000000)
00319 break;
00320 case CODEC_ID_PCM_S24LE:
00321 DECODE(32, le24, src, samples, n, 8, 0)
00322 break;
00323 case CODEC_ID_PCM_S24BE:
00324 DECODE(32, be24, src, samples, n, 8, 0)
00325 break;
00326 case CODEC_ID_PCM_U24LE:
00327 DECODE(32, le24, src, samples, n, 8, 0x800000)
00328 break;
00329 case CODEC_ID_PCM_U24BE:
00330 DECODE(32, be24, src, samples, n, 8, 0x800000)
00331 break;
00332 case CODEC_ID_PCM_S24DAUD:
00333 for(;n>0;n--) {
00334 uint32_t v = bytestream_get_be24(&src);
00335 v >>= 4;
00336 AV_WN16A(samples, av_reverse[(v >> 8) & 0xff] +
00337 (av_reverse[v & 0xff] << 8));
00338 samples += 2;
00339 }
00340 break;
00341 case CODEC_ID_PCM_S16LE_PLANAR:
00342 {
00343 const uint8_t *src2[MAX_CHANNELS];
00344 n /= avctx->channels;
00345 for(c=0;c<avctx->channels;c++)
00346 src2[c] = &src[c*n*2];
00347 for(;n>0;n--)
00348 for(c=0;c<avctx->channels;c++) {
00349 AV_WN16A(samples, bytestream_get_le16(&src2[c]));
00350 samples += 2;
00351 }
00352 break;
00353 }
00354 case CODEC_ID_PCM_U16LE:
00355 DECODE(16, le16, src, samples, n, 0, 0x8000)
00356 break;
00357 case CODEC_ID_PCM_U16BE:
00358 DECODE(16, be16, src, samples, n, 0, 0x8000)
00359 break;
00360 case CODEC_ID_PCM_S8:
00361 for(;n>0;n--) {
00362 *samples++ = *src++ + 128;
00363 }
00364 break;
00365 #if HAVE_BIGENDIAN
00366 case CODEC_ID_PCM_F64LE:
00367 DECODE(64, le64, src, samples, n, 0, 0)
00368 break;
00369 case CODEC_ID_PCM_S32LE:
00370 case CODEC_ID_PCM_F32LE:
00371 DECODE(32, le32, src, samples, n, 0, 0)
00372 break;
00373 case CODEC_ID_PCM_S16LE:
00374 DECODE(16, le16, src, samples, n, 0, 0)
00375 break;
00376 case CODEC_ID_PCM_F64BE:
00377 case CODEC_ID_PCM_F32BE:
00378 case CODEC_ID_PCM_S32BE:
00379 case CODEC_ID_PCM_S16BE:
00380 #else
00381 case CODEC_ID_PCM_F64BE:
00382 DECODE(64, be64, src, samples, n, 0, 0)
00383 break;
00384 case CODEC_ID_PCM_F32BE:
00385 case CODEC_ID_PCM_S32BE:
00386 DECODE(32, be32, src, samples, n, 0, 0)
00387 break;
00388 case CODEC_ID_PCM_S16BE:
00389 DECODE(16, be16, src, samples, n, 0, 0)
00390 break;
00391 case CODEC_ID_PCM_F64LE:
00392 case CODEC_ID_PCM_F32LE:
00393 case CODEC_ID_PCM_S32LE:
00394 case CODEC_ID_PCM_S16LE:
00395 #endif
00396 case CODEC_ID_PCM_U8:
00397 memcpy(samples, src, n*sample_size);
00398 break;
00399 case 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 CODEC_ID_PCM_ALAW:
00408 case CODEC_ID_PCM_MULAW:
00409 for(;n>0;n--) {
00410 AV_WN16A(samples, s->table[*src++]);
00411 samples += 2;
00412 }
00413 break;
00414 case 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 CODEC_ID_PCM_LXF:
00446 {
00447 int i;
00448 const uint8_t *src8;
00449 dst_int32_t = (int32_t *)s->frame.data[0];
00450 n /= avctx->channels;
00451
00452 for (i = 0; i < n; i++) {
00453 for (c = 0, src8 = src + i*5; c < avctx->channels; c++, src8 += n*5) {
00454
00455 *dst_int32_t++ = (src8[2] << 28) | (src8[1] << 20) | (src8[0] << 12) |
00456 ((src8[2] & 0xF) << 8) | src8[1];
00457 }
00458
00459 for (c = 0, src8 = src + i*5; c < avctx->channels; c++, src8 += n*5) {
00460
00461 *dst_int32_t++ = (src8[4] << 24) | (src8[3] << 16) |
00462 ((src8[2] & 0xF0) << 8) | (src8[4] << 4) | (src8[3] >> 4);
00463 }
00464 }
00465 break;
00466 }
00467 default:
00468 return -1;
00469 }
00470
00471 *got_frame_ptr = 1;
00472 *(AVFrame *)data = s->frame;
00473
00474 return buf_size;
00475 }
00476
00477 #if CONFIG_ENCODERS
00478 #define PCM_ENCODER(id_,sample_fmt_,name_,long_name_) \
00479 AVCodec ff_ ## name_ ## _encoder = { \
00480 .name = #name_, \
00481 .type = AVMEDIA_TYPE_AUDIO, \
00482 .id = id_, \
00483 .init = pcm_encode_init, \
00484 .encode2 = pcm_encode_frame, \
00485 .close = pcm_encode_close, \
00486 .capabilities = CODEC_CAP_VARIABLE_FRAME_SIZE, \
00487 .sample_fmts = (const enum AVSampleFormat[]){sample_fmt_,AV_SAMPLE_FMT_NONE}, \
00488 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
00489 }
00490 #else
00491 #define PCM_ENCODER(id,sample_fmt_,name,long_name_)
00492 #endif
00493
00494 #if CONFIG_DECODERS
00495 #define PCM_DECODER(id_,sample_fmt_,name_,long_name_) \
00496 AVCodec ff_ ## name_ ## _decoder = { \
00497 .name = #name_, \
00498 .type = AVMEDIA_TYPE_AUDIO, \
00499 .id = id_, \
00500 .priv_data_size = sizeof(PCMDecode), \
00501 .init = pcm_decode_init, \
00502 .decode = pcm_decode_frame, \
00503 .capabilities = CODEC_CAP_DR1, \
00504 .sample_fmts = (const enum AVSampleFormat[]){sample_fmt_,AV_SAMPLE_FMT_NONE}, \
00505 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
00506 }
00507 #else
00508 #define PCM_DECODER(id,sample_fmt_,name,long_name_)
00509 #endif
00510
00511 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
00512 PCM_ENCODER(id,sample_fmt_,name,long_name_); PCM_DECODER(id,sample_fmt_,name,long_name_)
00513
00514
00515 PCM_CODEC (CODEC_ID_PCM_ALAW, AV_SAMPLE_FMT_S16, pcm_alaw, "PCM A-law");
00516 PCM_DECODER(CODEC_ID_PCM_DVD, AV_SAMPLE_FMT_S32, pcm_dvd, "PCM signed 20|24-bit big-endian");
00517 PCM_CODEC (CODEC_ID_PCM_F32BE, AV_SAMPLE_FMT_FLT, pcm_f32be, "PCM 32-bit floating point big-endian");
00518 PCM_CODEC (CODEC_ID_PCM_F32LE, AV_SAMPLE_FMT_FLT, pcm_f32le, "PCM 32-bit floating point little-endian");
00519 PCM_CODEC (CODEC_ID_PCM_F64BE, AV_SAMPLE_FMT_DBL, pcm_f64be, "PCM 64-bit floating point big-endian");
00520 PCM_CODEC (CODEC_ID_PCM_F64LE, AV_SAMPLE_FMT_DBL, pcm_f64le, "PCM 64-bit floating point little-endian");
00521 PCM_DECODER(CODEC_ID_PCM_LXF, AV_SAMPLE_FMT_S32, pcm_lxf, "PCM signed 20-bit little-endian planar");
00522 PCM_CODEC (CODEC_ID_PCM_MULAW, AV_SAMPLE_FMT_S16, pcm_mulaw, "PCM mu-law");
00523 PCM_CODEC (CODEC_ID_PCM_S8, AV_SAMPLE_FMT_U8, pcm_s8, "PCM signed 8-bit");
00524 PCM_CODEC (CODEC_ID_PCM_S16BE, AV_SAMPLE_FMT_S16, pcm_s16be, "PCM signed 16-bit big-endian");
00525 PCM_CODEC (CODEC_ID_PCM_S16LE, AV_SAMPLE_FMT_S16, pcm_s16le, "PCM signed 16-bit little-endian");
00526 PCM_DECODER(CODEC_ID_PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16, pcm_s16le_planar, "PCM 16-bit little-endian planar");
00527 PCM_CODEC (CODEC_ID_PCM_S24BE, AV_SAMPLE_FMT_S32, pcm_s24be, "PCM signed 24-bit big-endian");
00528 PCM_CODEC (CODEC_ID_PCM_S24DAUD, AV_SAMPLE_FMT_S16, pcm_s24daud, "PCM D-Cinema audio signed 24-bit");
00529 PCM_CODEC (CODEC_ID_PCM_S24LE, AV_SAMPLE_FMT_S32, pcm_s24le, "PCM signed 24-bit little-endian");
00530 PCM_CODEC (CODEC_ID_PCM_S32BE, AV_SAMPLE_FMT_S32, pcm_s32be, "PCM signed 32-bit big-endian");
00531 PCM_CODEC (CODEC_ID_PCM_S32LE, AV_SAMPLE_FMT_S32, pcm_s32le, "PCM signed 32-bit little-endian");
00532 PCM_CODEC (CODEC_ID_PCM_U8, AV_SAMPLE_FMT_U8, pcm_u8, "PCM unsigned 8-bit");
00533 PCM_CODEC (CODEC_ID_PCM_U16BE, AV_SAMPLE_FMT_S16, pcm_u16be, "PCM unsigned 16-bit big-endian");
00534 PCM_CODEC (CODEC_ID_PCM_U16LE, AV_SAMPLE_FMT_S16, pcm_u16le, "PCM unsigned 16-bit little-endian");
00535 PCM_CODEC (CODEC_ID_PCM_U24BE, AV_SAMPLE_FMT_S32, pcm_u24be, "PCM unsigned 24-bit big-endian");
00536 PCM_CODEC (CODEC_ID_PCM_U24LE, AV_SAMPLE_FMT_S32, pcm_u24le, "PCM unsigned 24-bit little-endian");
00537 PCM_CODEC (CODEC_ID_PCM_U32BE, AV_SAMPLE_FMT_S32, pcm_u32be, "PCM unsigned 32-bit big-endian");
00538 PCM_CODEC (CODEC_ID_PCM_U32LE, AV_SAMPLE_FMT_S32, pcm_u32le, "PCM unsigned 32-bit little-endian");
00539 PCM_DECODER(CODEC_ID_PCM_ZORK, AV_SAMPLE_FMT_U8, pcm_zork, "PCM Zork");