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)
00278 {
00279
00280 ADPCMEncodeContext *s = avctx->priv_data;
00281 const int frontier = 1 << avctx->trellis;
00282 const int stride = avctx->channels;
00283 const int version = avctx->codec->id;
00284 TrellisPath *paths = s->paths, *p;
00285 TrellisNode *node_buf = s->node_buf;
00286 TrellisNode **nodep_buf = s->nodep_buf;
00287 TrellisNode **nodes = nodep_buf;
00288 TrellisNode **nodes_next = nodep_buf + frontier;
00289 int pathn = 0, froze = -1, i, j, k, generation = 0;
00290 uint8_t *hash = s->trellis_hash;
00291 memset(hash, 0xff, 65536 * sizeof(*hash));
00292
00293 memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
00294 nodes[0] = node_buf + frontier;
00295 nodes[0]->ssd = 0;
00296 nodes[0]->path = 0;
00297 nodes[0]->step = c->step_index;
00298 nodes[0]->sample1 = c->sample1;
00299 nodes[0]->sample2 = c->sample2;
00300 if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
00301 version == AV_CODEC_ID_ADPCM_IMA_QT ||
00302 version == AV_CODEC_ID_ADPCM_SWF)
00303 nodes[0]->sample1 = c->prev_sample;
00304 if (version == AV_CODEC_ID_ADPCM_MS)
00305 nodes[0]->step = c->idelta;
00306 if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
00307 if (c->step == 0) {
00308 nodes[0]->step = 127;
00309 nodes[0]->sample1 = 0;
00310 } else {
00311 nodes[0]->step = c->step;
00312 nodes[0]->sample1 = c->predictor;
00313 }
00314 }
00315
00316 for (i = 0; i < n; i++) {
00317 TrellisNode *t = node_buf + frontier*(i&1);
00318 TrellisNode **u;
00319 int sample = samples[i * stride];
00320 int heap_pos = 0;
00321 memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
00322 for (j = 0; j < frontier && nodes[j]; j++) {
00323
00324
00325 const int range = (j < frontier / 2) ? 1 : 0;
00326 const int step = nodes[j]->step;
00327 int nidx;
00328 if (version == AV_CODEC_ID_ADPCM_MS) {
00329 const int predictor = ((nodes[j]->sample1 * c->coeff1) +
00330 (nodes[j]->sample2 * c->coeff2)) / 64;
00331 const int div = (sample - predictor) / step;
00332 const int nmin = av_clip(div-range, -8, 6);
00333 const int nmax = av_clip(div+range, -7, 7);
00334 for (nidx = nmin; nidx <= nmax; nidx++) {
00335 const int nibble = nidx & 0xf;
00336 int dec_sample = predictor + nidx * step;
00337 #define STORE_NODE(NAME, STEP_INDEX)\
00338 int d;\
00339 uint32_t ssd;\
00340 int pos;\
00341 TrellisNode *u;\
00342 uint8_t *h;\
00343 dec_sample = av_clip_int16(dec_sample);\
00344 d = sample - dec_sample;\
00345 ssd = nodes[j]->ssd + d*d;\
00346
00347
00348
00349 \
00350 if (ssd < nodes[j]->ssd)\
00351 goto next_##NAME;\
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362 \
00363 h = &hash[(uint16_t) dec_sample];\
00364 if (*h == generation)\
00365 goto next_##NAME;\
00366 if (heap_pos < frontier) {\
00367 pos = heap_pos++;\
00368 } else {\
00369
00370 \
00371 pos = (frontier >> 1) +\
00372 (heap_pos & ((frontier >> 1) - 1));\
00373 if (ssd > nodes_next[pos]->ssd)\
00374 goto next_##NAME;\
00375 heap_pos++;\
00376 }\
00377 *h = generation;\
00378 u = nodes_next[pos];\
00379 if (!u) {\
00380 assert(pathn < FREEZE_INTERVAL << avctx->trellis);\
00381 u = t++;\
00382 nodes_next[pos] = u;\
00383 u->path = pathn++;\
00384 }\
00385 u->ssd = ssd;\
00386 u->step = STEP_INDEX;\
00387 u->sample2 = nodes[j]->sample1;\
00388 u->sample1 = dec_sample;\
00389 paths[u->path].nibble = nibble;\
00390 paths[u->path].prev = nodes[j]->path;\
00391
00392 \
00393 while (pos > 0) {\
00394 int parent = (pos - 1) >> 1;\
00395 if (nodes_next[parent]->ssd <= ssd)\
00396 break;\
00397 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
00398 pos = parent;\
00399 }\
00400 next_##NAME:;
00401 STORE_NODE(ms, FFMAX(16,
00402 (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
00403 }
00404 } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
00405 version == AV_CODEC_ID_ADPCM_IMA_QT ||
00406 version == AV_CODEC_ID_ADPCM_SWF) {
00407 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
00408 const int predictor = nodes[j]->sample1;\
00409 const int div = (sample - predictor) * 4 / STEP_TABLE;\
00410 int nmin = av_clip(div - range, -7, 6);\
00411 int nmax = av_clip(div + range, -6, 7);\
00412 if (nmin <= 0)\
00413 nmin--; \
00414 if (nmax < 0)\
00415 nmax--;\
00416 for (nidx = nmin; nidx <= nmax; nidx++) {\
00417 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
00418 int dec_sample = predictor +\
00419 (STEP_TABLE *\
00420 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
00421 STORE_NODE(NAME, STEP_INDEX);\
00422 }
00423 LOOP_NODES(ima, ff_adpcm_step_table[step],
00424 av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
00425 } else {
00426 LOOP_NODES(yamaha, step,
00427 av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
00428 127, 24567));
00429 #undef LOOP_NODES
00430 #undef STORE_NODE
00431 }
00432 }
00433
00434 u = nodes;
00435 nodes = nodes_next;
00436 nodes_next = u;
00437
00438 generation++;
00439 if (generation == 255) {
00440 memset(hash, 0xff, 65536 * sizeof(*hash));
00441 generation = 0;
00442 }
00443
00444
00445 if (nodes[0]->ssd > (1 << 28)) {
00446 for (j = 1; j < frontier && nodes[j]; j++)
00447 nodes[j]->ssd -= nodes[0]->ssd;
00448 nodes[0]->ssd = 0;
00449 }
00450
00451
00452 if (i == froze + FREEZE_INTERVAL) {
00453 p = &paths[nodes[0]->path];
00454 for (k = i; k > froze; k--) {
00455 dst[k] = p->nibble;
00456 p = &paths[p->prev];
00457 }
00458 froze = i;
00459 pathn = 0;
00460
00461
00462
00463 memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
00464 }
00465 }
00466
00467 p = &paths[nodes[0]->path];
00468 for (i = n - 1; i > froze; i--) {
00469 dst[i] = p->nibble;
00470 p = &paths[p->prev];
00471 }
00472
00473 c->predictor = nodes[0]->sample1;
00474 c->sample1 = nodes[0]->sample1;
00475 c->sample2 = nodes[0]->sample2;
00476 c->step_index = nodes[0]->step;
00477 c->step = nodes[0]->step;
00478 c->idelta = nodes[0]->step;
00479 }
00480
00481 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00482 const AVFrame *frame, int *got_packet_ptr)
00483 {
00484 int n, i, st, pkt_size, ret;
00485 const int16_t *samples;
00486 uint8_t *dst;
00487 ADPCMEncodeContext *c = avctx->priv_data;
00488 uint8_t *buf;
00489
00490 samples = (const int16_t *)frame->data[0];
00491 st = avctx->channels == 2;
00492
00493 if (avctx->codec_id == AV_CODEC_ID_ADPCM_SWF)
00494 pkt_size = (2 + avctx->channels * (22 + 4 * (frame->nb_samples - 1)) + 7) / 8;
00495 else
00496 pkt_size = avctx->block_align;
00497 if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size)))
00498 return ret;
00499 dst = avpkt->data;
00500
00501 switch(avctx->codec->id) {
00502 case AV_CODEC_ID_ADPCM_IMA_WAV:
00503 n = frame->nb_samples / 8;
00504 c->status[0].prev_sample = samples[0];
00505
00506
00507 bytestream_put_le16(&dst, c->status[0].prev_sample);
00508 *dst++ = c->status[0].step_index;
00509 *dst++ = 0;
00510 samples++;
00511 if (avctx->channels == 2) {
00512 c->status[1].prev_sample = samples[0];
00513
00514 bytestream_put_le16(&dst, c->status[1].prev_sample);
00515 *dst++ = c->status[1].step_index;
00516 *dst++ = 0;
00517 samples++;
00518 }
00519
00520
00521
00522 if (avctx->trellis > 0) {
00523 FF_ALLOC_OR_GOTO(avctx, buf, 2 * n * 8, error);
00524 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n * 8);
00525 if (avctx->channels == 2)
00526 adpcm_compress_trellis(avctx, samples + 1, buf + n * 8,
00527 &c->status[1], n * 8);
00528 for (i = 0; i < n; i++) {
00529 *dst++ = buf[8 * i + 0] | (buf[8 * i + 1] << 4);
00530 *dst++ = buf[8 * i + 2] | (buf[8 * i + 3] << 4);
00531 *dst++ = buf[8 * i + 4] | (buf[8 * i + 5] << 4);
00532 *dst++ = buf[8 * i + 6] | (buf[8 * i + 7] << 4);
00533 if (avctx->channels == 2) {
00534 uint8_t *buf1 = buf + n * 8;
00535 *dst++ = buf1[8 * i + 0] | (buf1[8 * i + 1] << 4);
00536 *dst++ = buf1[8 * i + 2] | (buf1[8 * i + 3] << 4);
00537 *dst++ = buf1[8 * i + 4] | (buf1[8 * i + 5] << 4);
00538 *dst++ = buf1[8 * i + 6] | (buf1[8 * i + 7] << 4);
00539 }
00540 }
00541 av_free(buf);
00542 } else {
00543 for (; n > 0; n--) {
00544 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
00545 *dst++ |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels ]) << 4;
00546 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
00547 *dst++ |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
00548 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
00549 *dst++ |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
00550 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
00551 *dst++ |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
00552
00553 if (avctx->channels == 2) {
00554 *dst = adpcm_ima_compress_sample(&c->status[1], samples[1 ]);
00555 *dst++ |= adpcm_ima_compress_sample(&c->status[1], samples[3 ]) << 4;
00556 *dst = adpcm_ima_compress_sample(&c->status[1], samples[5 ]);
00557 *dst++ |= adpcm_ima_compress_sample(&c->status[1], samples[7 ]) << 4;
00558 *dst = adpcm_ima_compress_sample(&c->status[1], samples[9 ]);
00559 *dst++ |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
00560 *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
00561 *dst++ |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
00562 }
00563 samples += 8 * avctx->channels;
00564 }
00565 }
00566 break;
00567 case AV_CODEC_ID_ADPCM_IMA_QT:
00568 {
00569 int ch, i;
00570 PutBitContext pb;
00571 init_put_bits(&pb, dst, pkt_size * 8);
00572
00573 for (ch = 0; ch < avctx->channels; ch++) {
00574 put_bits(&pb, 9, (c->status[ch].prev_sample & 0xFFFF) >> 7);
00575 put_bits(&pb, 7, c->status[ch].step_index);
00576 if (avctx->trellis > 0) {
00577 uint8_t buf[64];
00578 adpcm_compress_trellis(avctx, samples+ch, buf, &c->status[ch], 64);
00579 for (i = 0; i < 64; i++)
00580 put_bits(&pb, 4, buf[i ^ 1]);
00581 } else {
00582 for (i = 0; i < 64; i += 2) {
00583 int t1, t2;
00584 t1 = adpcm_ima_qt_compress_sample(&c->status[ch],
00585 samples[avctx->channels * (i + 0) + ch]);
00586 t2 = adpcm_ima_qt_compress_sample(&c->status[ch],
00587 samples[avctx->channels * (i + 1) + ch]);
00588 put_bits(&pb, 4, t2);
00589 put_bits(&pb, 4, t1);
00590 }
00591 }
00592 }
00593
00594 flush_put_bits(&pb);
00595 break;
00596 }
00597 case AV_CODEC_ID_ADPCM_SWF:
00598 {
00599 int i;
00600 PutBitContext pb;
00601 init_put_bits(&pb, dst, pkt_size * 8);
00602
00603 n = frame->nb_samples - 1;
00604
00605
00606 put_bits(&pb, 2, 2);
00607
00608
00609 for (i = 0; i < avctx->channels; i++) {
00610
00611 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63);
00612 put_sbits(&pb, 16, samples[i]);
00613 put_bits(&pb, 6, c->status[i].step_index);
00614 c->status[i].prev_sample = samples[i];
00615 }
00616
00617 if (avctx->trellis > 0) {
00618 FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
00619 adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
00620 &c->status[0], n);
00621 if (avctx->channels == 2)
00622 adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
00623 buf + n, &c->status[1], n);
00624 for (i = 0; i < n; i++) {
00625 put_bits(&pb, 4, buf[i]);
00626 if (avctx->channels == 2)
00627 put_bits(&pb, 4, buf[n + i]);
00628 }
00629 av_free(buf);
00630 } else {
00631 for (i = 1; i < frame->nb_samples; i++) {
00632 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
00633 samples[avctx->channels * i]));
00634 if (avctx->channels == 2)
00635 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
00636 samples[2 * i + 1]));
00637 }
00638 }
00639 flush_put_bits(&pb);
00640 break;
00641 }
00642 case AV_CODEC_ID_ADPCM_MS:
00643 for (i = 0; i < avctx->channels; i++) {
00644 int predictor = 0;
00645 *dst++ = predictor;
00646 c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
00647 c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
00648 }
00649 for (i = 0; i < avctx->channels; i++) {
00650 if (c->status[i].idelta < 16)
00651 c->status[i].idelta = 16;
00652 bytestream_put_le16(&dst, c->status[i].idelta);
00653 }
00654 for (i = 0; i < avctx->channels; i++)
00655 c->status[i].sample2= *samples++;
00656 for (i = 0; i < avctx->channels; i++) {
00657 c->status[i].sample1 = *samples++;
00658 bytestream_put_le16(&dst, c->status[i].sample1);
00659 }
00660 for (i = 0; i < avctx->channels; i++)
00661 bytestream_put_le16(&dst, c->status[i].sample2);
00662
00663 if (avctx->trellis > 0) {
00664 int n = avctx->block_align - 7 * avctx->channels;
00665 FF_ALLOC_OR_GOTO(avctx, buf, 2 * n, error);
00666 if (avctx->channels == 1) {
00667 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
00668 for (i = 0; i < n; i += 2)
00669 *dst++ = (buf[i] << 4) | buf[i + 1];
00670 } else {
00671 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
00672 adpcm_compress_trellis(avctx, samples + 1, buf + n, &c->status[1], n);
00673 for (i = 0; i < n; i++)
00674 *dst++ = (buf[i] << 4) | buf[n + i];
00675 }
00676 av_free(buf);
00677 } else {
00678 for (i = 7 * avctx->channels; i < avctx->block_align; i++) {
00679 int nibble;
00680 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
00681 nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
00682 *dst++ = nibble;
00683 }
00684 }
00685 break;
00686 case AV_CODEC_ID_ADPCM_YAMAHA:
00687 n = frame->nb_samples / 2;
00688 if (avctx->trellis > 0) {
00689 FF_ALLOC_OR_GOTO(avctx, buf, 2 * n * 2, error);
00690 n *= 2;
00691 if (avctx->channels == 1) {
00692 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
00693 for (i = 0; i < n; i += 2)
00694 *dst++ = buf[i] | (buf[i + 1] << 4);
00695 } else {
00696 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
00697 adpcm_compress_trellis(avctx, samples + 1, buf + n, &c->status[1], n);
00698 for (i = 0; i < n; i++)
00699 *dst++ = buf[i] | (buf[n + i] << 4);
00700 }
00701 av_free(buf);
00702 } else
00703 for (n *= avctx->channels; n > 0; n--) {
00704 int nibble;
00705 nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
00706 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
00707 *dst++ = nibble;
00708 }
00709 break;
00710 default:
00711 return AVERROR(EINVAL);
00712 }
00713
00714 avpkt->size = pkt_size;
00715 *got_packet_ptr = 1;
00716 return 0;
00717 error:
00718 return AVERROR(ENOMEM);
00719 }
00720
00721 static const enum AVSampleFormat sample_fmts[] = {
00722 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
00723 };
00724
00725 #define ADPCM_ENCODER(id_, name_, long_name_) \
00726 AVCodec ff_ ## name_ ## _encoder = { \
00727 .name = #name_, \
00728 .type = AVMEDIA_TYPE_AUDIO, \
00729 .id = id_, \
00730 .priv_data_size = sizeof(ADPCMEncodeContext), \
00731 .init = adpcm_encode_init, \
00732 .encode2 = adpcm_encode_frame, \
00733 .close = adpcm_encode_close, \
00734 .sample_fmts = sample_fmts, \
00735 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
00736 }
00737
00738 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
00739 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
00740 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
00741 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
00742 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");