*/
#include "avcodec.h"
#include "bitstream.h"
+#include "bytestream.h"
/**
* @file adpcm.c
avctx->frame_size = BLKSIZE * avctx->channels;
avctx->block_align = BLKSIZE;
break;
+ case CODEC_ID_ADPCM_SWF:
+ if (avctx->sample_rate != 11025 &&
+ avctx->sample_rate != 22050 &&
+ avctx->sample_rate != 44100) {
+ av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
+ return -1;
+ }
+ avctx->frame_size = 512 * (avctx->sample_rate / 11025);
+ break;
default:
return -1;
break;
n = avctx->frame_size / 8;
c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
/* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
- *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
- *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
+ bytestream_put_le16(&dst, c->status[0].prev_sample);
*dst++ = (unsigned char)c->status[0].step_index;
*dst++ = 0; /* unknown */
samples++;
if (avctx->channels == 2) {
c->status[1].prev_sample = (signed short)samples[1];
/* c->status[1].step_index = 0; */
- *dst++ = (c->status[1].prev_sample) & 0xFF;
- *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
+ bytestream_put_le16(&dst, c->status[1].prev_sample);
*dst++ = (unsigned char)c->status[1].step_index;
*dst++ = 0;
samples++;
samples += 8 * avctx->channels;
}
break;
+ case CODEC_ID_ADPCM_SWF:
+ {
+ int i;
+ PutBitContext pb;
+ init_put_bits(&pb, dst, buf_size*8);
+
+ //Store AdpcmCodeSize
+ put_bits(&pb, 2, 2); //Set 4bits flash adpcm format
+
+ //Init the encoder state
+ for(i=0; i<avctx->channels; i++){
+ c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63); // clip step so it fits 6 bits
+ put_bits(&pb, 16, samples[i] & 0xFFFF);
+ put_bits(&pb, 6, c->status[i].step_index);
+ c->status[i].prev_sample = (signed short)samples[i];
+ }
+
+ for (i=0; i<avctx->frame_size; i++) {
+ put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]) & 0xF);
+ if (avctx->channels == 2)
+ put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]) & 0xF);
+ }
+ flush_put_bits(&pb);
+ dst += put_bits_count(&pb)>>3;
+ break;
+ }
case CODEC_ID_ADPCM_MS:
for(i=0; i<avctx->channels; i++){
int predictor=0;
if (c->status[i].idelta < 16)
c->status[i].idelta = 16;
- *dst++ = c->status[i].idelta & 0xFF;
- *dst++ = c->status[i].idelta >> 8;
+ bytestream_put_le16(&dst, c->status[i].idelta);
}
for(i=0; i<avctx->channels; i++){
c->status[i].sample1= *samples++;
- *dst++ = c->status[i].sample1 & 0xFF;
- *dst++ = c->status[i].sample1 >> 8;
+ bytestream_put_le16(&dst, c->status[i].sample1);
}
for(i=0; i<avctx->channels; i++){
c->status[i].sample2= *samples++;
- *dst++ = c->status[i].sample2 & 0xFF;
- *dst++ = c->status[i].sample2 >> 8;
+ bytestream_put_le16(&dst, c->status[i].sample2);
}
if(avctx->trellis > 0) {
case CODEC_ID_ADPCM_CT:
c->status[0].step = c->status[1].step = 511;
break;
+ case CODEC_ID_ADPCM_IMA_WS:
+ if (avctx->extradata && avctx->extradata_size == 2 * 4) {
+ c->status[0].predictor = AV_RL32(avctx->extradata);
+ c->status[1].predictor = AV_RL32(avctx->extradata + 4);
+ }
+ break;
default:
break;
}
init_get_bits(&gb, buf, size);
- //read bits & inital values
+ //read bits & initial values
nb_bits = get_bits(&gb, 2)+2;
//av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
table = swf_index_tables[nb_bits-2];
}
break;
case CODEC_ID_ADPCM_THP:
- {
- GetBitContext gb;
+ {
int table[2][16];
unsigned int samplecnt;
int prev[2][2];
return -1;
}
- init_get_bits(&gb, src, buf_size * 8);
- src += buf_size;
-
- get_bits_long(&gb, 32); /* Channel size */
- samplecnt = get_bits_long(&gb, 32);
+ src+=4;
+ samplecnt = bytestream_get_be32(&src);
for (i = 0; i < 32; i++)
- table[0][i] = get_sbits(&gb, 16);
+ table[0][i] = (int16_t)bytestream_get_be16(&src);
/* Initialize the previous sample. */
- for (ch = 0; ch < 2; ch++) {
- prev[ch][0] = get_sbits(&gb, 16);
- prev[ch][1] = get_sbits(&gb, 16);
- }
+ for (i = 0; i < 4; i++)
+ prev[0][i] = (int16_t)bytestream_get_be16(&src);
if (samplecnt >= (samples_end - samples) / (st + 1)) {
av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
/* Read in every sample for this channel. */
for (i = 0; i < samplecnt / 14; i++) {
- int index = get_bits (&gb, 4) & 7;
- unsigned int exp = get_bits (&gb, 4);
+ int index = (*src >> 4) & 7;
+ unsigned int exp = 28 - (*src++ & 15);
int factor1 = table[ch][index * 2];
int factor2 = table[ch][index * 2 + 1];
/* Decode 14 samples. */
for (n = 0; n < 14; n++) {
- int sampledat = get_sbits (&gb, 4);
-
- *samples = ((prev[ch][0]*factor1
- + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
+ int32_t sampledat;
+ if(n&1) sampledat= *src++ <<28;
+ else sampledat= (*src&0xF0)<<24;
+
+ sampledat = ((prev[ch][0]*factor1
+ + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
+ CLAMP_TO_SHORT(sampledat);
+ *samples = sampledat;
prev[ch][1] = prev[ch][0];
prev[ch][0] = *samples++;
increased exactly one time too often. */
samples -= st;
break;
- }
+ }
default:
return -1;