00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022 #include "get_bits.h"
00023 #include "put_bits.h"
00024 #include "bytestream.h"
00025 #include "adpcm.h"
00026 #include "adpcm_data.h"
00027
00038 typedef struct TrellisPath {
00039 int nibble;
00040 int prev;
00041 } TrellisPath;
00042
00043 typedef struct TrellisNode {
00044 uint32_t ssd;
00045 int path;
00046 int sample1;
00047 int sample2;
00048 int step;
00049 } TrellisNode;
00050
00051 typedef struct ADPCMEncodeContext {
00052 ADPCMChannelStatus status[6];
00053 TrellisPath *paths;
00054 TrellisNode *node_buf;
00055 TrellisNode **nodep_buf;
00056 uint8_t *trellis_hash;
00057 } ADPCMEncodeContext;
00058
00059 #define FREEZE_INTERVAL 128
00060
00061 static av_cold int adpcm_encode_init(AVCodecContext *avctx)
00062 {
00063 ADPCMEncodeContext *s = avctx->priv_data;
00064 uint8_t *extradata;
00065 int i;
00066 if (avctx->channels > 2)
00067 return -1;
00068
00069 if (avctx->trellis && (unsigned)avctx->trellis > 16U) {
00070 av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
00071 return -1;
00072 }
00073
00074 if (avctx->trellis) {
00075 int frontier = 1 << avctx->trellis;
00076 int max_paths = frontier * FREEZE_INTERVAL;
00077 FF_ALLOC_OR_GOTO(avctx, s->paths,
00078 max_paths * sizeof(*s->paths), error);
00079 FF_ALLOC_OR_GOTO(avctx, s->node_buf,
00080 2 * frontier * sizeof(*s->node_buf), error);
00081 FF_ALLOC_OR_GOTO(avctx, s->nodep_buf,
00082 2 * frontier * sizeof(*s->nodep_buf), error);
00083 FF_ALLOC_OR_GOTO(avctx, s->trellis_hash,
00084 65536 * sizeof(*s->trellis_hash), error);
00085 }
00086
00087 avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
00088
00089 switch (avctx->codec->id) {
00090 case CODEC_ID_ADPCM_IMA_WAV:
00091
00092
00093 avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 /
00094 (4 * avctx->channels) + 1;
00095
00096
00097 avctx->block_align = BLKSIZE;
00098 avctx->bits_per_coded_sample = 4;
00099 break;
00100 case CODEC_ID_ADPCM_IMA_QT:
00101 avctx->frame_size = 64;
00102 avctx->block_align = 34 * avctx->channels;
00103 break;
00104 case CODEC_ID_ADPCM_MS:
00105
00106
00107 avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2;
00108 avctx->block_align = BLKSIZE;
00109 avctx->bits_per_coded_sample = 4;
00110 avctx->extradata_size = 32;
00111 extradata = avctx->extradata = av_malloc(avctx->extradata_size);
00112 if (!extradata)
00113 return AVERROR(ENOMEM);
00114 bytestream_put_le16(&extradata, avctx->frame_size);
00115 bytestream_put_le16(&extradata, 7);
00116 for (i = 0; i < 7; i++) {
00117 bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
00118 bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
00119 }
00120 break;
00121 case CODEC_ID_ADPCM_YAMAHA:
00122 avctx->frame_size = BLKSIZE * avctx->channels;
00123 avctx->block_align = BLKSIZE;
00124 break;
00125 case CODEC_ID_ADPCM_SWF:
00126 if (avctx->sample_rate != 11025 &&
00127 avctx->sample_rate != 22050 &&
00128 avctx->sample_rate != 44100) {
00129 av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
00130 "22050 or 44100\n");
00131 goto error;
00132 }
00133 avctx->frame_size = 512 * (avctx->sample_rate / 11025);
00134 break;
00135 default:
00136 goto error;
00137 }
00138
00139 avctx->coded_frame = avcodec_alloc_frame();
00140 avctx->coded_frame->key_frame= 1;
00141
00142 return 0;
00143 error:
00144 av_freep(&s->paths);
00145 av_freep(&s->node_buf);
00146 av_freep(&s->nodep_buf);
00147 av_freep(&s->trellis_hash);
00148 return -1;
00149 }
00150
00151 static av_cold int adpcm_encode_close(AVCodecContext *avctx)
00152 {
00153 ADPCMEncodeContext *s = avctx->priv_data;
00154 av_freep(&avctx->coded_frame);
00155 av_freep(&s->paths);
00156 av_freep(&s->node_buf);
00157 av_freep(&s->nodep_buf);
00158 av_freep(&s->trellis_hash);
00159
00160 return 0;
00161 }
00162
00163
00164 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c,
00165 short sample)
00166 {
00167 int delta = sample - c->prev_sample;
00168 int nibble = FFMIN(7, abs(delta) * 4 /
00169 ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
00170 c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
00171 ff_adpcm_yamaha_difflookup[nibble]) / 8);
00172 c->prev_sample = av_clip_int16(c->prev_sample);
00173 c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
00174 return nibble;
00175 }
00176
00177 static inline unsigned char adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
00178 short sample)
00179 {
00180 int delta = sample - c->prev_sample;
00181 int diff, step = ff_adpcm_step_table[c->step_index];
00182 int nibble = 8*(delta < 0);
00183
00184 delta= abs(delta);
00185 diff = delta + (step >> 3);
00186
00187 if (delta >= step) {
00188 nibble |= 4;
00189 delta -= step;
00190 }
00191 step >>= 1;
00192 if (delta >= step) {
00193 nibble |= 2;
00194 delta -= step;
00195 }
00196 step >>= 1;
00197 if (delta >= step) {
00198 nibble |= 1;
00199 delta -= step;
00200 }
00201 diff -= delta;
00202
00203 if (nibble & 8)
00204 c->prev_sample -= diff;
00205 else
00206 c->prev_sample += diff;
00207
00208 c->prev_sample = av_clip_int16(c->prev_sample);
00209 c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
00210
00211 return nibble;
00212 }
00213
00214 static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c,
00215 short sample)
00216 {
00217 int predictor, nibble, bias;
00218
00219 predictor = (((c->sample1) * (c->coeff1)) +
00220 (( c->sample2) * (c->coeff2))) / 64;
00221
00222 nibble = sample - predictor;
00223 if (nibble >= 0)
00224 bias = c->idelta / 2;
00225 else
00226 bias = -c->idelta / 2;
00227
00228 nibble = (nibble + bias) / c->idelta;
00229 nibble = av_clip(nibble, -8, 7) & 0x0F;
00230
00231 predictor += (signed)((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
00232
00233 c->sample2 = c->sample1;
00234 c->sample1 = av_clip_int16(predictor);
00235
00236 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
00237 if (c->idelta < 16)
00238 c->idelta = 16;
00239
00240 return nibble;
00241 }
00242
00243 static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
00244 short sample)
00245 {
00246 int nibble, delta;
00247
00248 if (!c->step) {
00249 c->predictor = 0;
00250 c->step = 127;
00251 }
00252
00253 delta = sample - c->predictor;
00254
00255 nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
00256
00257 c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
00258 c->predictor = av_clip_int16(c->predictor);
00259 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
00260 c->step = av_clip(c->step, 127, 24567);
00261
00262 return nibble;
00263 }
00264
00265 static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
00266 uint8_t *dst, ADPCMChannelStatus *c, int n)
00267 {
00268
00269 ADPCMEncodeContext *s = avctx->priv_data;
00270 const int frontier = 1 << avctx->trellis;
00271 const int stride = avctx->channels;
00272 const int version = avctx->codec->id;
00273 TrellisPath *paths = s->paths, *p;
00274 TrellisNode *node_buf = s->node_buf;
00275 TrellisNode **nodep_buf = s->nodep_buf;
00276 TrellisNode **nodes = nodep_buf;
00277 TrellisNode **nodes_next = nodep_buf + frontier;
00278 int pathn = 0, froze = -1, i, j, k, generation = 0;
00279 uint8_t *hash = s->trellis_hash;
00280 memset(hash, 0xff, 65536 * sizeof(*hash));
00281
00282 memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
00283 nodes[0] = node_buf + frontier;
00284 nodes[0]->ssd = 0;
00285 nodes[0]->path = 0;
00286 nodes[0]->step = c->step_index;
00287 nodes[0]->sample1 = c->sample1;
00288 nodes[0]->sample2 = c->sample2;
00289 if (version == CODEC_ID_ADPCM_IMA_WAV ||
00290 version == CODEC_ID_ADPCM_IMA_QT ||
00291 version == CODEC_ID_ADPCM_SWF)
00292 nodes[0]->sample1 = c->prev_sample;
00293 if (version == CODEC_ID_ADPCM_MS)
00294 nodes[0]->step = c->idelta;
00295 if (version == CODEC_ID_ADPCM_YAMAHA) {
00296 if (c->step == 0) {
00297 nodes[0]->step = 127;
00298 nodes[0]->sample1 = 0;
00299 } else {
00300 nodes[0]->step = c->step;
00301 nodes[0]->sample1 = c->predictor;
00302 }
00303 }
00304
00305 for (i = 0; i < n; i++) {
00306 TrellisNode *t = node_buf + frontier*(i&1);
00307 TrellisNode **u;
00308 int sample = samples[i * stride];
00309 int heap_pos = 0;
00310 memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
00311 for (j = 0; j < frontier && nodes[j]; j++) {
00312
00313
00314 const int range = (j < frontier / 2) ? 1 : 0;
00315 const int step = nodes[j]->step;
00316 int nidx;
00317 if (version == CODEC_ID_ADPCM_MS) {
00318 const int predictor = ((nodes[j]->sample1 * c->coeff1) +
00319 (nodes[j]->sample2 * c->coeff2)) / 64;
00320 const int div = (sample - predictor) / step;
00321 const int nmin = av_clip(div-range, -8, 6);
00322 const int nmax = av_clip(div+range, -7, 7);
00323 for (nidx = nmin; nidx <= nmax; nidx++) {
00324 const int nibble = nidx & 0xf;
00325 int dec_sample = predictor + nidx * step;
00326 #define STORE_NODE(NAME, STEP_INDEX)\
00327 int d;\
00328 uint32_t ssd;\
00329 int pos;\
00330 TrellisNode *u;\
00331 uint8_t *h;\
00332 dec_sample = av_clip_int16(dec_sample);\
00333 d = sample - dec_sample;\
00334 ssd = nodes[j]->ssd + d*d;\
00335
00336
00337
00338 \
00339 if (ssd < nodes[j]->ssd)\
00340 goto next_##NAME;\
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351 \
00352 h = &hash[(uint16_t) dec_sample];\
00353 if (*h == generation)\
00354 goto next_##NAME;\
00355 if (heap_pos < frontier) {\
00356 pos = heap_pos++;\
00357 } else {\
00358
00359 \
00360 pos = (frontier >> 1) +\
00361 (heap_pos & ((frontier >> 1) - 1));\
00362 if (ssd > nodes_next[pos]->ssd)\
00363 goto next_##NAME;\
00364 heap_pos++;\
00365 }\
00366 *h = generation;\
00367 u = nodes_next[pos];\
00368 if (!u) {\
00369 assert(pathn < FREEZE_INTERVAL << avctx->trellis);\
00370 u = t++;\
00371 nodes_next[pos] = u;\
00372 u->path = pathn++;\
00373 }\
00374 u->ssd = ssd;\
00375 u->step = STEP_INDEX;\
00376 u->sample2 = nodes[j]->sample1;\
00377 u->sample1 = dec_sample;\
00378 paths[u->path].nibble = nibble;\
00379 paths[u->path].prev = nodes[j]->path;\
00380
00381 \
00382 while (pos > 0) {\
00383 int parent = (pos - 1) >> 1;\
00384 if (nodes_next[parent]->ssd <= ssd)\
00385 break;\
00386 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
00387 pos = parent;\
00388 }\
00389 next_##NAME:;
00390 STORE_NODE(ms, FFMAX(16,
00391 (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
00392 }
00393 } else if (version == CODEC_ID_ADPCM_IMA_WAV ||
00394 version == CODEC_ID_ADPCM_IMA_QT ||
00395 version == CODEC_ID_ADPCM_SWF) {
00396 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
00397 const int predictor = nodes[j]->sample1;\
00398 const int div = (sample - predictor) * 4 / STEP_TABLE;\
00399 int nmin = av_clip(div - range, -7, 6);\
00400 int nmax = av_clip(div + range, -6, 7);\
00401 if (nmin <= 0)\
00402 nmin--; \
00403 if (nmax < 0)\
00404 nmax--;\
00405 for (nidx = nmin; nidx <= nmax; nidx++) {\
00406 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
00407 int dec_sample = predictor +\
00408 (STEP_TABLE *\
00409 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
00410 STORE_NODE(NAME, STEP_INDEX);\
00411 }
00412 LOOP_NODES(ima, ff_adpcm_step_table[step],
00413 av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
00414 } else {
00415 LOOP_NODES(yamaha, step,
00416 av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
00417 127, 24567));
00418 #undef LOOP_NODES
00419 #undef STORE_NODE
00420 }
00421 }
00422
00423 u = nodes;
00424 nodes = nodes_next;
00425 nodes_next = u;
00426
00427 generation++;
00428 if (generation == 255) {
00429 memset(hash, 0xff, 65536 * sizeof(*hash));
00430 generation = 0;
00431 }
00432
00433
00434 if (nodes[0]->ssd > (1 << 28)) {
00435 for (j = 1; j < frontier && nodes[j]; j++)
00436 nodes[j]->ssd -= nodes[0]->ssd;
00437 nodes[0]->ssd = 0;
00438 }
00439
00440
00441 if (i == froze + FREEZE_INTERVAL) {
00442 p = &paths[nodes[0]->path];
00443 for (k = i; k > froze; k--) {
00444 dst[k] = p->nibble;
00445 p = &paths[p->prev];
00446 }
00447 froze = i;
00448 pathn = 0;
00449
00450
00451
00452 memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
00453 }
00454 }
00455
00456 p = &paths[nodes[0]->path];
00457 for (i = n - 1; i > froze; i--) {
00458 dst[i] = p->nibble;
00459 p = &paths[p->prev];
00460 }
00461
00462 c->predictor = nodes[0]->sample1;
00463 c->sample1 = nodes[0]->sample1;
00464 c->sample2 = nodes[0]->sample2;
00465 c->step_index = nodes[0]->step;
00466 c->step = nodes[0]->step;
00467 c->idelta = nodes[0]->step;
00468 }
00469
00470 static int adpcm_encode_frame(AVCodecContext *avctx,
00471 unsigned char *frame, int buf_size, void *data)
00472 {
00473 int n, i, st;
00474 short *samples;
00475 unsigned char *dst;
00476 ADPCMEncodeContext *c = avctx->priv_data;
00477 uint8_t *buf;
00478
00479 dst = frame;
00480 samples = (short *)data;
00481 st = avctx->channels == 2;
00482
00483
00484 switch(avctx->codec->id) {
00485 case CODEC_ID_ADPCM_IMA_WAV:
00486 n = avctx->frame_size / 8;
00487 c->status[0].prev_sample = (signed short)samples[0];
00488
00489
00490 bytestream_put_le16(&dst, c->status[0].prev_sample);
00491 *dst++ = (unsigned char)c->status[0].step_index;
00492 *dst++ = 0;
00493 samples++;
00494 if (avctx->channels == 2) {
00495 c->status[1].prev_sample = (signed short)samples[0];
00496
00497 bytestream_put_le16(&dst, c->status[1].prev_sample);
00498 *dst++ = (unsigned char)c->status[1].step_index;
00499 *dst++ = 0;
00500 samples++;
00501 }
00502
00503
00504
00505 if (avctx->trellis > 0) {
00506 FF_ALLOC_OR_GOTO(avctx, buf, 2 * n * 8, error);
00507 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n * 8);
00508 if (avctx->channels == 2)
00509 adpcm_compress_trellis(avctx, samples + 1, buf + n * 8,
00510 &c->status[1], n * 8);
00511 for (i = 0; i < n; i++) {
00512 *dst++ = buf[8 * i + 0] | (buf[8 * i + 1] << 4);
00513 *dst++ = buf[8 * i + 2] | (buf[8 * i + 3] << 4);
00514 *dst++ = buf[8 * i + 4] | (buf[8 * i + 5] << 4);
00515 *dst++ = buf[8 * i + 6] | (buf[8 * i + 7] << 4);
00516 if (avctx->channels == 2) {
00517 uint8_t *buf1 = buf + n * 8;
00518 *dst++ = buf1[8 * i + 0] | (buf1[8 * i + 1] << 4);
00519 *dst++ = buf1[8 * i + 2] | (buf1[8 * i + 3] << 4);
00520 *dst++ = buf1[8 * i + 4] | (buf1[8 * i + 5] << 4);
00521 *dst++ = buf1[8 * i + 6] | (buf1[8 * i + 7] << 4);
00522 }
00523 }
00524 av_free(buf);
00525 } else {
00526 for (; n > 0; n--) {
00527 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
00528 *dst++ |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels ]) << 4;
00529 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
00530 *dst++ |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
00531 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
00532 *dst++ |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
00533 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
00534 *dst++ |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
00535
00536 if (avctx->channels == 2) {
00537 *dst = adpcm_ima_compress_sample(&c->status[1], samples[1 ]);
00538 *dst++ |= adpcm_ima_compress_sample(&c->status[1], samples[3 ]) << 4;
00539 *dst = adpcm_ima_compress_sample(&c->status[1], samples[5 ]);
00540 *dst++ |= adpcm_ima_compress_sample(&c->status[1], samples[7 ]) << 4;
00541 *dst = adpcm_ima_compress_sample(&c->status[1], samples[9 ]);
00542 *dst++ |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
00543 *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
00544 *dst++ |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
00545 }
00546 samples += 8 * avctx->channels;
00547 }
00548 }
00549 break;
00550 case CODEC_ID_ADPCM_IMA_QT:
00551 {
00552 int ch, i;
00553 PutBitContext pb;
00554 init_put_bits(&pb, dst, buf_size * 8);
00555
00556 for (ch = 0; ch < avctx->channels; ch++) {
00557 put_bits(&pb, 9, (c->status[ch].prev_sample + 0x10000) >> 7);
00558 put_bits(&pb, 7, c->status[ch].step_index);
00559 if (avctx->trellis > 0) {
00560 uint8_t buf[64];
00561 adpcm_compress_trellis(avctx, samples+ch, buf, &c->status[ch], 64);
00562 for (i = 0; i < 64; i++)
00563 put_bits(&pb, 4, buf[i ^ 1]);
00564 } else {
00565 for (i = 0; i < 64; i += 2) {
00566 int t1, t2;
00567 t1 = adpcm_ima_qt_compress_sample(&c->status[ch],
00568 samples[avctx->channels * (i + 0) + ch]);
00569 t2 = adpcm_ima_qt_compress_sample(&c->status[ch],
00570 samples[avctx->channels * (i + 1) + ch]);
00571 put_bits(&pb, 4, t2);
00572 put_bits(&pb, 4, t1);
00573 }
00574 }
00575 }
00576
00577 flush_put_bits(&pb);
00578 dst += put_bits_count(&pb) >> 3;
00579 break;
00580 }
00581 case CODEC_ID_ADPCM_SWF:
00582 {
00583 int i;
00584 PutBitContext pb;
00585 init_put_bits(&pb, dst, buf_size * 8);
00586
00587 n = avctx->frame_size - 1;
00588
00589
00590 put_bits(&pb, 2, 2);
00591
00592
00593 for (i = 0; i < avctx->channels; i++) {
00594
00595 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63);
00596 put_sbits(&pb, 16, samples[i]);
00597 put_bits(&pb, 6, c->status[i].step_index);
00598 c->status[i].prev_sample = (signed short)samples[i];
00599 }
00600
00601 if (avctx->trellis > 0) {
00602 FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
00603 adpcm_compress_trellis(avctx, samples + 2, buf, &c->status[0], n);
00604 if (avctx->channels == 2)
00605 adpcm_compress_trellis(avctx, samples + 3, buf + n,
00606 &c->status[1], n);
00607 for (i = 0; i < n; i++) {
00608 put_bits(&pb, 4, buf[i]);
00609 if (avctx->channels == 2)
00610 put_bits(&pb, 4, buf[n + i]);
00611 }
00612 av_free(buf);
00613 } else {
00614 for (i = 1; i < avctx->frame_size; i++) {
00615 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
00616 samples[avctx->channels * i]));
00617 if (avctx->channels == 2)
00618 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
00619 samples[2 * i + 1]));
00620 }
00621 }
00622 flush_put_bits(&pb);
00623 dst += put_bits_count(&pb) >> 3;
00624 break;
00625 }
00626 case CODEC_ID_ADPCM_MS:
00627 for (i = 0; i < avctx->channels; i++) {
00628 int predictor = 0;
00629 *dst++ = predictor;
00630 c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
00631 c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
00632 }
00633 for (i = 0; i < avctx->channels; i++) {
00634 if (c->status[i].idelta < 16)
00635 c->status[i].idelta = 16;
00636 bytestream_put_le16(&dst, c->status[i].idelta);
00637 }
00638 for (i = 0; i < avctx->channels; i++)
00639 c->status[i].sample2= *samples++;
00640 for (i = 0; i < avctx->channels; i++) {
00641 c->status[i].sample1 = *samples++;
00642 bytestream_put_le16(&dst, c->status[i].sample1);
00643 }
00644 for (i = 0; i < avctx->channels; i++)
00645 bytestream_put_le16(&dst, c->status[i].sample2);
00646
00647 if (avctx->trellis > 0) {
00648 int n = avctx->block_align - 7 * avctx->channels;
00649 FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
00650 if (avctx->channels == 1) {
00651 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
00652 for (i = 0; i < n; i += 2)
00653 *dst++ = (buf[i] << 4) | buf[i + 1];
00654 } else {
00655 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
00656 adpcm_compress_trellis(avctx, samples + 1, buf + n, &c->status[1], n);
00657 for (i = 0; i < n; i++)
00658 *dst++ = (buf[i] << 4) | buf[n + i];
00659 }
00660 av_free(buf);
00661 } else {
00662 for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
00663 int nibble;
00664 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
00665 nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
00666 *dst++ = nibble;
00667 }
00668 }
00669 break;
00670 case CODEC_ID_ADPCM_YAMAHA:
00671 n = avctx->frame_size / 2;
00672 if (avctx->trellis > 0) {
00673 FF_ALLOC_OR_GOTO(avctx, buf, 2 * n * 2, error);
00674 n *= 2;
00675 if (avctx->channels == 1) {
00676 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
00677 for (i = 0; i < n; i += 2)
00678 *dst++ = buf[i] | (buf[i + 1] << 4);
00679 } else {
00680 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
00681 adpcm_compress_trellis(avctx, samples + 1, buf + n, &c->status[1], n);
00682 for (i = 0; i < n; i++)
00683 *dst++ = buf[i] | (buf[n + i] << 4);
00684 }
00685 av_free(buf);
00686 } else
00687 for (n *= avctx->channels; n > 0; n--) {
00688 int nibble;
00689 nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
00690 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
00691 *dst++ = nibble;
00692 }
00693 break;
00694 default:
00695 error:
00696 return -1;
00697 }
00698 return dst - frame;
00699 }
00700
00701
00702 #define ADPCM_ENCODER(id_, name_, long_name_) \
00703 AVCodec ff_ ## name_ ## _encoder = { \
00704 .name = #name_, \
00705 .type = AVMEDIA_TYPE_AUDIO, \
00706 .id = id_, \
00707 .priv_data_size = sizeof(ADPCMEncodeContext), \
00708 .init = adpcm_encode_init, \
00709 .encode = adpcm_encode_frame, \
00710 .close = adpcm_encode_close, \
00711 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, \
00712 AV_SAMPLE_FMT_NONE}, \
00713 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
00714 }
00715
00716 ADPCM_ENCODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
00717 ADPCM_ENCODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
00718 ADPCM_ENCODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
00719 ADPCM_ENCODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
00720 ADPCM_ENCODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");