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 "pcm_tablegen.h"
00031
00032 #define MAX_CHANNELS 64
00033
00034 static av_cold int pcm_encode_init(AVCodecContext *avctx)
00035 {
00036 avctx->frame_size = 1;
00037 switch(avctx->codec->id) {
00038 case CODEC_ID_PCM_ALAW:
00039 pcm_alaw_tableinit();
00040 break;
00041 case CODEC_ID_PCM_MULAW:
00042 pcm_ulaw_tableinit();
00043 break;
00044 default:
00045 break;
00046 }
00047
00048 avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
00049 avctx->block_align = avctx->channels * avctx->bits_per_coded_sample/8;
00050 avctx->coded_frame= avcodec_alloc_frame();
00051 avctx->coded_frame->key_frame= 1;
00052
00053 return 0;
00054 }
00055
00056 static av_cold int pcm_encode_close(AVCodecContext *avctx)
00057 {
00058 av_freep(&avctx->coded_frame);
00059
00060 return 0;
00061 }
00062
00073 #define ENCODE(type, endian, src, dst, n, shift, offset) \
00074 samples_##type = (type*)src; \
00075 for(;n>0;n--) { \
00076 register type v = (*samples_##type++ >> shift) + offset; \
00077 bytestream_put_##endian(&dst, v); \
00078 }
00079
00080 static int pcm_encode_frame(AVCodecContext *avctx,
00081 unsigned char *frame, int buf_size, void *data)
00082 {
00083 int n, sample_size, v;
00084 short *samples;
00085 unsigned char *dst;
00086 uint8_t *srcu8;
00087 int16_t *samples_int16_t;
00088 int32_t *samples_int32_t;
00089 int64_t *samples_int64_t;
00090 uint16_t *samples_uint16_t;
00091 uint32_t *samples_uint32_t;
00092
00093 sample_size = av_get_bits_per_sample(avctx->codec->id)/8;
00094 n = buf_size / sample_size;
00095 samples = data;
00096 dst = frame;
00097
00098 if (avctx->sample_fmt!=avctx->codec->sample_fmts[0]) {
00099 av_log(avctx, AV_LOG_ERROR, "invalid sample_fmt\n");
00100 return -1;
00101 }
00102
00103 switch(avctx->codec->id) {
00104 case CODEC_ID_PCM_U32LE:
00105 ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
00106 break;
00107 case CODEC_ID_PCM_U32BE:
00108 ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
00109 break;
00110 case CODEC_ID_PCM_S24LE:
00111 ENCODE(int32_t, le24, samples, dst, n, 8, 0)
00112 break;
00113 case CODEC_ID_PCM_S24BE:
00114 ENCODE(int32_t, be24, samples, dst, n, 8, 0)
00115 break;
00116 case CODEC_ID_PCM_U24LE:
00117 ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
00118 break;
00119 case CODEC_ID_PCM_U24BE:
00120 ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
00121 break;
00122 case CODEC_ID_PCM_S24DAUD:
00123 for(;n>0;n--) {
00124 uint32_t tmp = av_reverse[(*samples >> 8) & 0xff] +
00125 (av_reverse[*samples & 0xff] << 8);
00126 tmp <<= 4;
00127 bytestream_put_be24(&dst, tmp);
00128 samples++;
00129 }
00130 break;
00131 case CODEC_ID_PCM_U16LE:
00132 ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
00133 break;
00134 case CODEC_ID_PCM_U16BE:
00135 ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
00136 break;
00137 case CODEC_ID_PCM_S8:
00138 srcu8= data;
00139 for(;n>0;n--) {
00140 v = *srcu8++;
00141 *dst++ = v - 128;
00142 }
00143 break;
00144 #if HAVE_BIGENDIAN
00145 case CODEC_ID_PCM_F64LE:
00146 ENCODE(int64_t, le64, samples, dst, n, 0, 0)
00147 break;
00148 case CODEC_ID_PCM_S32LE:
00149 case CODEC_ID_PCM_F32LE:
00150 ENCODE(int32_t, le32, samples, dst, n, 0, 0)
00151 break;
00152 case CODEC_ID_PCM_S16LE:
00153 ENCODE(int16_t, le16, samples, dst, n, 0, 0)
00154 break;
00155 case CODEC_ID_PCM_F64BE:
00156 case CODEC_ID_PCM_F32BE:
00157 case CODEC_ID_PCM_S32BE:
00158 case CODEC_ID_PCM_S16BE:
00159 #else
00160 case CODEC_ID_PCM_F64BE:
00161 ENCODE(int64_t, be64, samples, dst, n, 0, 0)
00162 break;
00163 case CODEC_ID_PCM_F32BE:
00164 case CODEC_ID_PCM_S32BE:
00165 ENCODE(int32_t, be32, samples, dst, n, 0, 0)
00166 break;
00167 case CODEC_ID_PCM_S16BE:
00168 ENCODE(int16_t, be16, samples, dst, n, 0, 0)
00169 break;
00170 case CODEC_ID_PCM_F64LE:
00171 case CODEC_ID_PCM_F32LE:
00172 case CODEC_ID_PCM_S32LE:
00173 case CODEC_ID_PCM_S16LE:
00174 #endif
00175 case CODEC_ID_PCM_U8:
00176 memcpy(dst, samples, n*sample_size);
00177 dst += n*sample_size;
00178 break;
00179 case CODEC_ID_PCM_ZORK:
00180 for(;n>0;n--) {
00181 v= *samples++ >> 8;
00182 if(v<0) v = -v;
00183 else v+= 128;
00184 *dst++ = v;
00185 }
00186 break;
00187 case CODEC_ID_PCM_ALAW:
00188 for(;n>0;n--) {
00189 v = *samples++;
00190 *dst++ = linear_to_alaw[(v + 32768) >> 2];
00191 }
00192 break;
00193 case CODEC_ID_PCM_MULAW:
00194 for(;n>0;n--) {
00195 v = *samples++;
00196 *dst++ = linear_to_ulaw[(v + 32768) >> 2];
00197 }
00198 break;
00199 default:
00200 return -1;
00201 }
00202
00203
00204 return dst - frame;
00205 }
00206
00207 typedef struct PCMDecode {
00208 short table[256];
00209 } PCMDecode;
00210
00211 static av_cold int pcm_decode_init(AVCodecContext * avctx)
00212 {
00213 PCMDecode *s = avctx->priv_data;
00214 int i;
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 return 0;
00231 }
00232
00243 #define DECODE(type, endian, src, dst, n, shift, offset) \
00244 dst_##type = (type*)dst; \
00245 for(;n>0;n--) { \
00246 register type v = bytestream_get_##endian(&src); \
00247 *dst_##type++ = (v - offset) << shift; \
00248 } \
00249 dst = (short*)dst_##type;
00250
00251 static int pcm_decode_frame(AVCodecContext *avctx,
00252 void *data, int *data_size,
00253 AVPacket *avpkt)
00254 {
00255 const uint8_t *buf = avpkt->data;
00256 int buf_size = avpkt->size;
00257 PCMDecode *s = avctx->priv_data;
00258 int sample_size, c, n;
00259 short *samples;
00260 const uint8_t *src, *src8, *src2[MAX_CHANNELS];
00261 uint8_t *dstu8;
00262 int16_t *dst_int16_t;
00263 int32_t *dst_int32_t;
00264 int64_t *dst_int64_t;
00265 uint16_t *dst_uint16_t;
00266 uint32_t *dst_uint32_t;
00267
00268 samples = data;
00269 src = buf;
00270
00271 if (avctx->sample_fmt!=avctx->codec->sample_fmts[0]) {
00272 av_log(avctx, AV_LOG_ERROR, "invalid sample_fmt\n");
00273 return -1;
00274 }
00275
00276 if(avctx->channels <= 0 || avctx->channels > MAX_CHANNELS){
00277 av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
00278 return -1;
00279 }
00280
00281 sample_size = av_get_bits_per_sample(avctx->codec_id)/8;
00282
00283
00284 if (CODEC_ID_PCM_DVD == avctx->codec_id)
00285
00286 sample_size = avctx->bits_per_coded_sample * 2 / 8;
00287
00288 n = avctx->channels * sample_size;
00289
00290 if(n && buf_size % n){
00291 if (buf_size < n) {
00292 av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n");
00293 return -1;
00294 }else
00295 buf_size -= buf_size % n;
00296 }
00297
00298 buf_size= FFMIN(buf_size, *data_size/2);
00299 *data_size=0;
00300
00301 n = buf_size/sample_size;
00302
00303 switch(avctx->codec->id) {
00304 case CODEC_ID_PCM_U32LE:
00305 DECODE(uint32_t, le32, src, samples, n, 0, 0x80000000)
00306 break;
00307 case CODEC_ID_PCM_U32BE:
00308 DECODE(uint32_t, be32, src, samples, n, 0, 0x80000000)
00309 break;
00310 case CODEC_ID_PCM_S24LE:
00311 DECODE(int32_t, le24, src, samples, n, 8, 0)
00312 break;
00313 case CODEC_ID_PCM_S24BE:
00314 DECODE(int32_t, be24, src, samples, n, 8, 0)
00315 break;
00316 case CODEC_ID_PCM_U24LE:
00317 DECODE(uint32_t, le24, src, samples, n, 8, 0x800000)
00318 break;
00319 case CODEC_ID_PCM_U24BE:
00320 DECODE(uint32_t, be24, src, samples, n, 8, 0x800000)
00321 break;
00322 case CODEC_ID_PCM_S24DAUD:
00323 for(;n>0;n--) {
00324 uint32_t v = bytestream_get_be24(&src);
00325 v >>= 4;
00326 *samples++ = av_reverse[(v >> 8) & 0xff] +
00327 (av_reverse[v & 0xff] << 8);
00328 }
00329 break;
00330 case CODEC_ID_PCM_S16LE_PLANAR:
00331 n /= avctx->channels;
00332 for(c=0;c<avctx->channels;c++)
00333 src2[c] = &src[c*n*2];
00334 for(;n>0;n--)
00335 for(c=0;c<avctx->channels;c++)
00336 *samples++ = bytestream_get_le16(&src2[c]);
00337 src = src2[avctx->channels-1];
00338 break;
00339 case CODEC_ID_PCM_U16LE:
00340 DECODE(uint16_t, le16, src, samples, n, 0, 0x8000)
00341 break;
00342 case CODEC_ID_PCM_U16BE:
00343 DECODE(uint16_t, be16, src, samples, n, 0, 0x8000)
00344 break;
00345 case CODEC_ID_PCM_S8:
00346 dstu8= (uint8_t*)samples;
00347 for(;n>0;n--) {
00348 *dstu8++ = *src++ + 128;
00349 }
00350 samples= (short*)dstu8;
00351 break;
00352 #if HAVE_BIGENDIAN
00353 case CODEC_ID_PCM_F64LE:
00354 DECODE(int64_t, le64, src, samples, n, 0, 0)
00355 break;
00356 case CODEC_ID_PCM_S32LE:
00357 case CODEC_ID_PCM_F32LE:
00358 DECODE(int32_t, le32, src, samples, n, 0, 0)
00359 break;
00360 case CODEC_ID_PCM_S16LE:
00361 DECODE(int16_t, le16, src, samples, n, 0, 0)
00362 break;
00363 case CODEC_ID_PCM_F64BE:
00364 case CODEC_ID_PCM_F32BE:
00365 case CODEC_ID_PCM_S32BE:
00366 case CODEC_ID_PCM_S16BE:
00367 #else
00368 case CODEC_ID_PCM_F64BE:
00369 DECODE(int64_t, be64, src, samples, n, 0, 0)
00370 break;
00371 case CODEC_ID_PCM_F32BE:
00372 case CODEC_ID_PCM_S32BE:
00373 DECODE(int32_t, be32, src, samples, n, 0, 0)
00374 break;
00375 case CODEC_ID_PCM_S16BE:
00376 DECODE(int16_t, be16, src, samples, n, 0, 0)
00377 break;
00378 case CODEC_ID_PCM_F64LE:
00379 case CODEC_ID_PCM_F32LE:
00380 case CODEC_ID_PCM_S32LE:
00381 case CODEC_ID_PCM_S16LE:
00382 #endif
00383 case CODEC_ID_PCM_U8:
00384 memcpy(samples, src, n*sample_size);
00385 src += n*sample_size;
00386 samples = (short*)((uint8_t*)data + n*sample_size);
00387 break;
00388 case CODEC_ID_PCM_ZORK:
00389 for(;n>0;n--) {
00390 int x= *src++;
00391 if(x&128) x-= 128;
00392 else x = -x;
00393 *samples++ = x << 8;
00394 }
00395 break;
00396 case CODEC_ID_PCM_ALAW:
00397 case CODEC_ID_PCM_MULAW:
00398 for(;n>0;n--) {
00399 *samples++ = s->table[*src++];
00400 }
00401 break;
00402 case CODEC_ID_PCM_DVD:
00403 dst_int32_t = data;
00404 n /= avctx->channels;
00405 switch (avctx->bits_per_coded_sample) {
00406 case 20:
00407 while (n--) {
00408 c = avctx->channels;
00409 src8 = src + 4*c;
00410 while (c--) {
00411 *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8 &0xf0) << 8);
00412 *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++ &0x0f) << 12);
00413 }
00414 src = src8;
00415 }
00416 break;
00417 case 24:
00418 while (n--) {
00419 c = avctx->channels;
00420 src8 = src + 4*c;
00421 while (c--) {
00422 *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
00423 *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
00424 }
00425 src = src8;
00426 }
00427 break;
00428 default:
00429 av_log(avctx, AV_LOG_ERROR, "PCM DVD unsupported sample depth\n");
00430 return -1;
00431 break;
00432 }
00433 samples = (short *) dst_int32_t;
00434 break;
00435 default:
00436 return -1;
00437 }
00438 *data_size = (uint8_t *)samples - (uint8_t *)data;
00439 return src - buf;
00440 }
00441
00442 #if CONFIG_ENCODERS
00443 #define PCM_ENCODER(id,sample_fmt_,name,long_name_) \
00444 AVCodec name ## _encoder = { \
00445 #name, \
00446 AVMEDIA_TYPE_AUDIO, \
00447 id, \
00448 0, \
00449 pcm_encode_init, \
00450 pcm_encode_frame, \
00451 pcm_encode_close, \
00452 NULL, \
00453 .sample_fmts = (const enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
00454 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
00455 };
00456 #else
00457 #define PCM_ENCODER(id,sample_fmt_,name,long_name_)
00458 #endif
00459
00460 #if CONFIG_DECODERS
00461 #define PCM_DECODER(id,sample_fmt_,name,long_name_) \
00462 AVCodec name ## _decoder = { \
00463 #name, \
00464 AVMEDIA_TYPE_AUDIO, \
00465 id, \
00466 sizeof(PCMDecode), \
00467 pcm_decode_init, \
00468 NULL, \
00469 NULL, \
00470 pcm_decode_frame, \
00471 .sample_fmts = (const enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
00472 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
00473 };
00474 #else
00475 #define PCM_DECODER(id,sample_fmt_,name,long_name_)
00476 #endif
00477
00478 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
00479 PCM_ENCODER(id,sample_fmt_,name,long_name_) PCM_DECODER(id,sample_fmt_,name,long_name_)
00480
00481
00482 PCM_CODEC (CODEC_ID_PCM_ALAW, SAMPLE_FMT_S16, pcm_alaw, "PCM A-law");
00483 PCM_CODEC (CODEC_ID_PCM_DVD, SAMPLE_FMT_S32, pcm_dvd, "PCM signed 20|24-bit big-endian");
00484 PCM_CODEC (CODEC_ID_PCM_F32BE, SAMPLE_FMT_FLT, pcm_f32be, "PCM 32-bit floating point big-endian");
00485 PCM_CODEC (CODEC_ID_PCM_F32LE, SAMPLE_FMT_FLT, pcm_f32le, "PCM 32-bit floating point little-endian");
00486 PCM_CODEC (CODEC_ID_PCM_F64BE, SAMPLE_FMT_DBL, pcm_f64be, "PCM 64-bit floating point big-endian");
00487 PCM_CODEC (CODEC_ID_PCM_F64LE, SAMPLE_FMT_DBL, pcm_f64le, "PCM 64-bit floating point little-endian");
00488 PCM_CODEC (CODEC_ID_PCM_MULAW, SAMPLE_FMT_S16, pcm_mulaw, "PCM mu-law");
00489 PCM_CODEC (CODEC_ID_PCM_S8, SAMPLE_FMT_U8, pcm_s8, "PCM signed 8-bit");
00490 PCM_CODEC (CODEC_ID_PCM_S16BE, SAMPLE_FMT_S16, pcm_s16be, "PCM signed 16-bit big-endian");
00491 PCM_CODEC (CODEC_ID_PCM_S16LE, SAMPLE_FMT_S16, pcm_s16le, "PCM signed 16-bit little-endian");
00492 PCM_DECODER(CODEC_ID_PCM_S16LE_PLANAR, SAMPLE_FMT_S16, pcm_s16le_planar, "PCM 16-bit little-endian planar");
00493 PCM_CODEC (CODEC_ID_PCM_S24BE, SAMPLE_FMT_S32, pcm_s24be, "PCM signed 24-bit big-endian");
00494 PCM_CODEC (CODEC_ID_PCM_S24DAUD, SAMPLE_FMT_S16, pcm_s24daud, "PCM D-Cinema audio signed 24-bit");
00495 PCM_CODEC (CODEC_ID_PCM_S24LE, SAMPLE_FMT_S32, pcm_s24le, "PCM signed 24-bit little-endian");
00496 PCM_CODEC (CODEC_ID_PCM_S32BE, SAMPLE_FMT_S32, pcm_s32be, "PCM signed 32-bit big-endian");
00497 PCM_CODEC (CODEC_ID_PCM_S32LE, SAMPLE_FMT_S32, pcm_s32le, "PCM signed 32-bit little-endian");
00498 PCM_CODEC (CODEC_ID_PCM_U8, SAMPLE_FMT_U8, pcm_u8, "PCM unsigned 8-bit");
00499 PCM_CODEC (CODEC_ID_PCM_U16BE, SAMPLE_FMT_S16, pcm_u16be, "PCM unsigned 16-bit big-endian");
00500 PCM_CODEC (CODEC_ID_PCM_U16LE, SAMPLE_FMT_S16, pcm_u16le, "PCM unsigned 16-bit little-endian");
00501 PCM_CODEC (CODEC_ID_PCM_U24BE, SAMPLE_FMT_S32, pcm_u24be, "PCM unsigned 24-bit big-endian");
00502 PCM_CODEC (CODEC_ID_PCM_U24LE, SAMPLE_FMT_S32, pcm_u24le, "PCM unsigned 24-bit little-endian");
00503 PCM_CODEC (CODEC_ID_PCM_U32BE, SAMPLE_FMT_S32, pcm_u32be, "PCM unsigned 32-bit big-endian");
00504 PCM_CODEC (CODEC_ID_PCM_U32LE, SAMPLE_FMT_S32, pcm_u32le, "PCM unsigned 32-bit little-endian");
00505 PCM_CODEC (CODEC_ID_PCM_ZORK, SAMPLE_FMT_S16, pcm_zork, "PCM Zork");