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