Libav 0.7.1
|
00001 /* 00002 * PCM codecs 00003 * Copyright (c) 2001 Fabrice Bellard 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00027 #include "avcodec.h" 00028 #include "libavutil/common.h" /* for av_reverse */ 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 = (const 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 const short *samples; 00085 unsigned char *dst; 00086 const uint8_t *srcu8; 00087 const int16_t *samples_int16_t; 00088 const int32_t *samples_int32_t; 00089 const int64_t *samples_int64_t; 00090 const uint16_t *samples_uint16_t; 00091 const 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; // sync flags would go here 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 /* HAVE_BIGENDIAN */ 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 //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels); 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 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 return 0; 00235 } 00236 00247 #define DECODE(type, endian, src, dst, n, shift, offset) \ 00248 dst_##type = (type*)dst; \ 00249 for(;n>0;n--) { \ 00250 register type v = bytestream_get_##endian(&src); \ 00251 *dst_##type++ = (v - offset) << shift; \ 00252 } \ 00253 dst = (short*)dst_##type; 00254 00255 static int pcm_decode_frame(AVCodecContext *avctx, 00256 void *data, int *data_size, 00257 AVPacket *avpkt) 00258 { 00259 const uint8_t *buf = avpkt->data; 00260 int buf_size = avpkt->size; 00261 PCMDecode *s = avctx->priv_data; 00262 int sample_size, c, n, i; 00263 short *samples; 00264 const uint8_t *src, *src8, *src2[MAX_CHANNELS]; 00265 uint8_t *dstu8; 00266 int16_t *dst_int16_t; 00267 int32_t *dst_int32_t; 00268 int64_t *dst_int64_t; 00269 uint16_t *dst_uint16_t; 00270 uint32_t *dst_uint32_t; 00271 00272 samples = data; 00273 src = buf; 00274 00275 if (avctx->sample_fmt!=avctx->codec->sample_fmts[0]) { 00276 av_log(avctx, AV_LOG_ERROR, "invalid sample_fmt\n"); 00277 return -1; 00278 } 00279 00280 if(avctx->channels <= 0 || avctx->channels > MAX_CHANNELS){ 00281 av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n"); 00282 return -1; 00283 } 00284 00285 sample_size = av_get_bits_per_sample(avctx->codec_id)/8; 00286 00287 /* av_get_bits_per_sample returns 0 for CODEC_ID_PCM_DVD */ 00288 if (CODEC_ID_PCM_DVD == avctx->codec_id) 00289 /* 2 samples are interleaved per block in PCM_DVD */ 00290 sample_size = avctx->bits_per_coded_sample * 2 / 8; 00291 else if (avctx->codec_id == CODEC_ID_PCM_LXF) 00292 /* we process 40-bit blocks per channel for LXF */ 00293 sample_size = 5; 00294 00295 if (sample_size == 0) { 00296 av_log(avctx, AV_LOG_ERROR, "Invalid sample_size\n"); 00297 return AVERROR(EINVAL); 00298 } 00299 00300 n = avctx->channels * sample_size; 00301 00302 if(n && buf_size % n){ 00303 if (buf_size < n) { 00304 av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n"); 00305 return -1; 00306 }else 00307 buf_size -= buf_size % n; 00308 } 00309 00310 buf_size= FFMIN(buf_size, *data_size/2); 00311 *data_size=0; 00312 00313 n = buf_size/sample_size; 00314 00315 switch(avctx->codec->id) { 00316 case CODEC_ID_PCM_U32LE: 00317 DECODE(uint32_t, le32, src, samples, n, 0, 0x80000000) 00318 break; 00319 case CODEC_ID_PCM_U32BE: 00320 DECODE(uint32_t, be32, src, samples, n, 0, 0x80000000) 00321 break; 00322 case CODEC_ID_PCM_S24LE: 00323 DECODE(int32_t, le24, src, samples, n, 8, 0) 00324 break; 00325 case CODEC_ID_PCM_S24BE: 00326 DECODE(int32_t, be24, src, samples, n, 8, 0) 00327 break; 00328 case CODEC_ID_PCM_U24LE: 00329 DECODE(uint32_t, le24, src, samples, n, 8, 0x800000) 00330 break; 00331 case CODEC_ID_PCM_U24BE: 00332 DECODE(uint32_t, be24, src, samples, n, 8, 0x800000) 00333 break; 00334 case CODEC_ID_PCM_S24DAUD: 00335 for(;n>0;n--) { 00336 uint32_t v = bytestream_get_be24(&src); 00337 v >>= 4; // sync flags are here 00338 *samples++ = av_reverse[(v >> 8) & 0xff] + 00339 (av_reverse[v & 0xff] << 8); 00340 } 00341 break; 00342 case CODEC_ID_PCM_S16LE_PLANAR: 00343 n /= avctx->channels; 00344 for(c=0;c<avctx->channels;c++) 00345 src2[c] = &src[c*n*2]; 00346 for(;n>0;n--) 00347 for(c=0;c<avctx->channels;c++) 00348 *samples++ = bytestream_get_le16(&src2[c]); 00349 src = src2[avctx->channels-1]; 00350 break; 00351 case CODEC_ID_PCM_U16LE: 00352 DECODE(uint16_t, le16, src, samples, n, 0, 0x8000) 00353 break; 00354 case CODEC_ID_PCM_U16BE: 00355 DECODE(uint16_t, be16, src, samples, n, 0, 0x8000) 00356 break; 00357 case CODEC_ID_PCM_S8: 00358 dstu8= (uint8_t*)samples; 00359 for(;n>0;n--) { 00360 *dstu8++ = *src++ + 128; 00361 } 00362 samples= (short*)dstu8; 00363 break; 00364 #if HAVE_BIGENDIAN 00365 case CODEC_ID_PCM_F64LE: 00366 DECODE(int64_t, le64, src, samples, n, 0, 0) 00367 break; 00368 case CODEC_ID_PCM_S32LE: 00369 case CODEC_ID_PCM_F32LE: 00370 DECODE(int32_t, le32, src, samples, n, 0, 0) 00371 break; 00372 case CODEC_ID_PCM_S16LE: 00373 DECODE(int16_t, le16, src, samples, n, 0, 0) 00374 break; 00375 case CODEC_ID_PCM_F64BE: 00376 case CODEC_ID_PCM_F32BE: 00377 case CODEC_ID_PCM_S32BE: 00378 case CODEC_ID_PCM_S16BE: 00379 #else 00380 case CODEC_ID_PCM_F64BE: 00381 DECODE(int64_t, be64, src, samples, n, 0, 0) 00382 break; 00383 case CODEC_ID_PCM_F32BE: 00384 case CODEC_ID_PCM_S32BE: 00385 DECODE(int32_t, be32, src, samples, n, 0, 0) 00386 break; 00387 case CODEC_ID_PCM_S16BE: 00388 DECODE(int16_t, be16, src, samples, n, 0, 0) 00389 break; 00390 case CODEC_ID_PCM_F64LE: 00391 case CODEC_ID_PCM_F32LE: 00392 case CODEC_ID_PCM_S32LE: 00393 case CODEC_ID_PCM_S16LE: 00394 #endif /* HAVE_BIGENDIAN */ 00395 case CODEC_ID_PCM_U8: 00396 memcpy(samples, src, n*sample_size); 00397 src += n*sample_size; 00398 samples = (short*)((uint8_t*)data + n*sample_size); 00399 break; 00400 case CODEC_ID_PCM_ZORK: 00401 for(;n>0;n--) { 00402 int x= *src++; 00403 if(x&128) x-= 128; 00404 else x = -x; 00405 *samples++ = x << 8; 00406 } 00407 break; 00408 case CODEC_ID_PCM_ALAW: 00409 case CODEC_ID_PCM_MULAW: 00410 for(;n>0;n--) { 00411 *samples++ = s->table[*src++]; 00412 } 00413 break; 00414 case CODEC_ID_PCM_DVD: 00415 dst_int32_t = data; 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 default: 00441 av_log(avctx, AV_LOG_ERROR, "PCM DVD unsupported sample depth\n"); 00442 return -1; 00443 break; 00444 } 00445 samples = (short *) dst_int32_t; 00446 break; 00447 case CODEC_ID_PCM_LXF: 00448 dst_int32_t = data; 00449 n /= avctx->channels; 00450 //unpack and de-planerize 00451 for (i = 0; i < n; i++) { 00452 for (c = 0, src8 = src + i*5; c < avctx->channels; c++, src8 += n*5) { 00453 //extract low 20 bits and expand to 32 bits 00454 *dst_int32_t++ = (src8[2] << 28) | (src8[1] << 20) | (src8[0] << 12) | 00455 ((src8[2] & 0xF) << 8) | src8[1]; 00456 } 00457 00458 for (c = 0, src8 = src + i*5; c < avctx->channels; c++, src8 += n*5) { 00459 //extract high 20 bits and expand to 32 bits 00460 *dst_int32_t++ = (src8[4] << 24) | (src8[3] << 16) | 00461 ((src8[2] & 0xF0) << 8) | (src8[4] << 4) | (src8[3] >> 4); 00462 } 00463 } 00464 src += n * avctx->channels * 5; 00465 samples = (short *) dst_int32_t; 00466 break; 00467 default: 00468 return -1; 00469 } 00470 *data_size = (uint8_t *)samples - (uint8_t *)data; 00471 return src - buf; 00472 } 00473 00474 #if CONFIG_ENCODERS 00475 #define PCM_ENCODER(id_,sample_fmt_,name_,long_name_) \ 00476 AVCodec ff_ ## name_ ## _encoder = { \ 00477 .name = #name_, \ 00478 .type = AVMEDIA_TYPE_AUDIO, \ 00479 .id = id_, \ 00480 .init = pcm_encode_init, \ 00481 .encode = pcm_encode_frame, \ 00482 .close = pcm_encode_close, \ 00483 .sample_fmts = (const enum AVSampleFormat[]){sample_fmt_,AV_SAMPLE_FMT_NONE}, \ 00484 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \ 00485 } 00486 #else 00487 #define PCM_ENCODER(id,sample_fmt_,name,long_name_) 00488 #endif 00489 00490 #if CONFIG_DECODERS 00491 #define PCM_DECODER(id_,sample_fmt_,name_,long_name_) \ 00492 AVCodec ff_ ## name_ ## _decoder = { \ 00493 .name = #name_, \ 00494 .type = AVMEDIA_TYPE_AUDIO, \ 00495 .id = id_, \ 00496 .priv_data_size = sizeof(PCMDecode), \ 00497 .init = pcm_decode_init, \ 00498 .decode = pcm_decode_frame, \ 00499 .sample_fmts = (const enum AVSampleFormat[]){sample_fmt_,AV_SAMPLE_FMT_NONE}, \ 00500 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \ 00501 } 00502 #else 00503 #define PCM_DECODER(id,sample_fmt_,name,long_name_) 00504 #endif 00505 00506 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \ 00507 PCM_ENCODER(id,sample_fmt_,name,long_name_); PCM_DECODER(id,sample_fmt_,name,long_name_) 00508 00509 /* Note: Do not forget to add new entries to the Makefile as well. */ 00510 PCM_CODEC (CODEC_ID_PCM_ALAW, AV_SAMPLE_FMT_S16, pcm_alaw, "PCM A-law"); 00511 PCM_DECODER(CODEC_ID_PCM_DVD, AV_SAMPLE_FMT_S32, pcm_dvd, "PCM signed 20|24-bit big-endian"); 00512 PCM_CODEC (CODEC_ID_PCM_F32BE, AV_SAMPLE_FMT_FLT, pcm_f32be, "PCM 32-bit floating point big-endian"); 00513 PCM_CODEC (CODEC_ID_PCM_F32LE, AV_SAMPLE_FMT_FLT, pcm_f32le, "PCM 32-bit floating point little-endian"); 00514 PCM_CODEC (CODEC_ID_PCM_F64BE, AV_SAMPLE_FMT_DBL, pcm_f64be, "PCM 64-bit floating point big-endian"); 00515 PCM_CODEC (CODEC_ID_PCM_F64LE, AV_SAMPLE_FMT_DBL, pcm_f64le, "PCM 64-bit floating point little-endian"); 00516 PCM_DECODER(CODEC_ID_PCM_LXF, AV_SAMPLE_FMT_S32, pcm_lxf, "PCM signed 20-bit little-endian planar"); 00517 PCM_CODEC (CODEC_ID_PCM_MULAW, AV_SAMPLE_FMT_S16, pcm_mulaw, "PCM mu-law"); 00518 PCM_CODEC (CODEC_ID_PCM_S8, AV_SAMPLE_FMT_U8, pcm_s8, "PCM signed 8-bit"); 00519 PCM_CODEC (CODEC_ID_PCM_S16BE, AV_SAMPLE_FMT_S16, pcm_s16be, "PCM signed 16-bit big-endian"); 00520 PCM_CODEC (CODEC_ID_PCM_S16LE, AV_SAMPLE_FMT_S16, pcm_s16le, "PCM signed 16-bit little-endian"); 00521 PCM_DECODER(CODEC_ID_PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16, pcm_s16le_planar, "PCM 16-bit little-endian planar"); 00522 PCM_CODEC (CODEC_ID_PCM_S24BE, AV_SAMPLE_FMT_S32, pcm_s24be, "PCM signed 24-bit big-endian"); 00523 PCM_CODEC (CODEC_ID_PCM_S24DAUD, AV_SAMPLE_FMT_S16, pcm_s24daud, "PCM D-Cinema audio signed 24-bit"); 00524 PCM_CODEC (CODEC_ID_PCM_S24LE, AV_SAMPLE_FMT_S32, pcm_s24le, "PCM signed 24-bit little-endian"); 00525 PCM_CODEC (CODEC_ID_PCM_S32BE, AV_SAMPLE_FMT_S32, pcm_s32be, "PCM signed 32-bit big-endian"); 00526 PCM_CODEC (CODEC_ID_PCM_S32LE, AV_SAMPLE_FMT_S32, pcm_s32le, "PCM signed 32-bit little-endian"); 00527 PCM_CODEC (CODEC_ID_PCM_U8, AV_SAMPLE_FMT_U8, pcm_u8, "PCM unsigned 8-bit"); 00528 PCM_CODEC (CODEC_ID_PCM_U16BE, AV_SAMPLE_FMT_S16, pcm_u16be, "PCM unsigned 16-bit big-endian"); 00529 PCM_CODEC (CODEC_ID_PCM_U16LE, AV_SAMPLE_FMT_S16, pcm_u16le, "PCM unsigned 16-bit little-endian"); 00530 PCM_CODEC (CODEC_ID_PCM_U24BE, AV_SAMPLE_FMT_S32, pcm_u24be, "PCM unsigned 24-bit big-endian"); 00531 PCM_CODEC (CODEC_ID_PCM_U24LE, AV_SAMPLE_FMT_S32, pcm_u24le, "PCM unsigned 24-bit little-endian"); 00532 PCM_CODEC (CODEC_ID_PCM_U32BE, AV_SAMPLE_FMT_S32, pcm_u32be, "PCM unsigned 32-bit big-endian"); 00533 PCM_CODEC (CODEC_ID_PCM_U32LE, AV_SAMPLE_FMT_S32, pcm_u32le, "PCM unsigned 32-bit little-endian"); 00534 PCM_CODEC (CODEC_ID_PCM_ZORK, AV_SAMPLE_FMT_S16, pcm_zork, "PCM Zork");