ADPCM Codecs
Loading...
Searching...
No Matches
ADPCMEncoder.h
1#pragma once
2#include "ADPCM.h"
3#include "ADPCMCodec.h"
4#include "adpcm/BitWriter.h"
5#include "adpcm/ByteWriter.h"
6
7
8namespace adpcm_ffmpeg {
9
21 uint8_t *out;
22 int pos = 0;
23 void put(int nibble) {
24 if (pos & 1) {
25 out[pos >> 1] |= nibble & 0x0F;
26 } else {
27 out[pos >> 1] = (uint8_t)((nibble & 0x0F) << 4);
28 }
29 pos++;
30 }
31};
32
33class ADPCMEncoder : public ADPCMCodec {
34 public:
36 setBlockSize(ADAPCM_DEFAULT_BLOCK_SIZE);
37 avctx.priv_data = (uint8_t *)&enc_ctx;
38 }
39
40 // release encoder buffers even if end() was not called explicitly
41 ~ADPCMEncoder() override { adpcm_encode_close(); }
42
43 bool begin(int sampleRate, int channels) {
44 // extended_data holds one pointer per channel
45 if (channels <= 0 || channels > ADPCM_MAX_CHANNELS) return false;
46 avctx.sample_rate = sampleRate;
47 avctx.nb_channels = channels;
48 avctx.sample_fmt = sample_formats[0];
49 bool rc = adpcm_encode_init() == 0;
50 return rc;
51 }
52
53 void end() { adpcm_encode_close(); }
54
55 AVPacket &encode(int16_t *data, size_t sampleCount) {
56 frame.nb_samples = sampleCount / avctx.nb_channels;
57 // fill data
58 frame.data[0] = (uint8_t *)data;
59
60 // fill extended_data
61 frame.extended_data = extended_data;
62 if (channels() == 1 || !isPlanar()) {
63 extended_data[0] = data;
64 } else {
65 // planar codec with multiple channels: split the interleaved samples
66 // into one separate array per channel
67 frame_extended_data_vectors.resize(channels());
68 for (int ch=0;ch<channels();ch++){
69 frame_extended_data_vectors[ch].resize(sampleCount/channels());
70 extended_data[ch] = &frame_extended_data_vectors[ch][0];
71 }
72
73 // fill with data
74 for (int j = 0; j < sampleCount / channels(); j++) {
75 for (int ch=0;ch<channels();ch++){
76 frame_extended_data_vectors[ch][j] = data[(j * channels()) + ch];
77 }
78 }
79 }
80
81 int got_packet_ptr = 0;
82 int buf_size = sampleCount > (size_t)avctx.block_align ? sampleCount : avctx.block_align;
83 av_packet_data.resize(buf_size);
84 result.data = &av_packet_data[0];
85
86 int rc = adpcm_encode_frame(&result, &frame, &got_packet_ptr);
87 if (rc != 0 || !got_packet_ptr) {
88 result.size = 0;
89 }
90 return result;
91 }
92
93
94 virtual bool isTrellis() { return false; }
95
96 int blockAlign() { return avctx.block_align;}
97
98 protected:
99
108 const int16_t *coef1, const int16_t *coef2,
110 int &best_exp) {
111 long long best_err = -1;
112 int best_nib[32] = {0};
113 int best_s1 = 0, best_s2 = 0;
114 best_index = 0;
115 best_exp = 0;
116
117 for (int index = 0; index < npairs; index++) {
118 const int f1 = coef1[index];
119 const int f2 = coef2[index];
120 for (int exp = 0; exp < 16; exp++) {
121 const int scale = 1 << exp;
122 int nib[32];
123 int s1 = cs->sample1, s2 = cs->sample2;
124 long long err = 0;
125 for (int i = 0; i < n; i++) {
126 // same prediction and reconstruction as the DSP decoders
127 const int pred = (s1 * f1 + s2 * f2) >> 11;
128 const int diff = in[i] - pred;
129 const int base = diff >= 0 ? (diff + scale / 2) / scale
130 : -((-diff + scale / 2) / scale);
131 int best_n = 0, best_e = 0x7fffffff, best_c = 0;
132 for (int cand = base - 1; cand <= base + 1; cand++) {
133 const int v = cand < -8 ? -8 : (cand > 7 ? 7 : cand);
134 const int clipped = av_clip_int16(pred + v * scale);
135 int e = in[i] - clipped;
136 if (e < 0) e = -e;
137 if (e < best_e) {
138 best_e = e;
139 best_n = v;
140 best_c = clipped;
141 }
142 }
143 nib[i] = best_n;
144 err += (long long)best_e * best_e;
145 s2 = s1;
146 s1 = best_c;
147 }
148 if (best_err < 0 || err < best_err) {
149 best_err = err;
150 best_index = index;
151 best_exp = exp;
152 best_s1 = s1;
153 best_s2 = s2;
154 memcpy(best_nib, nib, sizeof(int) * n);
155 }
156 }
157 }
158
159 for (int k = 0; k < (n + 1) / 2; k++) {
160 const int hi = best_nib[2 * k] & 0x0F;
161 const int lo = (2 * k + 1 < n) ? (best_nib[2 * k + 1] & 0x0F) : 0;
162 nibbles_out[k] = (uint8_t)((hi << 4) | lo);
163 }
164 cs->sample1 = best_s1;
165 cs->sample2 = best_s2;
166 }
167 AVPacket result;
168 AVFrame frame;
169 int16_t *extended_data[ADPCM_MAX_CHANNELS] = {0};
170 ADPCMVector<uint8_t> av_packet_data;
171 ADPCMVector<ADPCMVector<int16_t>> frame_extended_data_vectors;
172 // encoding data
173 int st, pkt_size, ret;
174 const int16_t *samples;
175 const int16_t *const *samples_p;
176 uint8_t *dst;
177 ADPCMEncodeContext *c;
178 ADPCMEncodeContext *s;
179
180 virtual int adpcm_encode_init_impl() = 0;
181
184 virtual int packetSize(const AVFrame *frame) {
185 if (avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
186 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
187 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
188 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_WS)
189 return (frame->nb_samples * channels() + 1) / 2;
190 return avctx.block_align;
191 }
192
193 virtual int adpcm_encode_init() {
194 s = (ADPCMEncodeContext *)avctx.priv_data;
195
196 if (s == NULL) {
197 return -1;
198 }
199
200 /*
201 * AMV's block size has to match that of the corresponding video
202 * stream. Relax the POT requirement.
203 */
204 if (avctx.codec_id != AV_CODEC_ID_ADPCM_IMA_AMV &&
205 (s->block_size & (s->block_size - 1))) {
206 av_log(avctx, AV_LOG_ERROR, "block size must be power of 2: %d\n",
207 s->block_size);
208 return AVERROR(AVERROR_INVALID);
209 }
210
211 if (avctx.trellis) {
212 int frontier, max_paths;
213
214 if ((unsigned)avctx.trellis > 16U) {
215 av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
216 return AVERROR(AVERROR_INVALID);
217 }
218
219 if (avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
220 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
221 avctx.codec_id == AV_CODEC_ID_ADPCM_ARGO ||
222 avctx.codec_id == AV_CODEC_ID_ADPCM_IMA_WS) {
223 /*
224 * The current trellis implementation doesn't work for extended
225 * runs of samples without periodic resets. Disallow it.
226 */
227 av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
228 return AVERROR_PATCHWELCOME;
229 }
230
231 frontier = 1 << avctx.trellis;
232 max_paths = frontier * FREEZE_INTERVAL;
233 freeArray(s->paths);
234 freeArray(s->node_buf);
235 freeArray(s->nodep_buf);
236 freeArray(s->trellis_hash);
237 if (!allocArray(s->paths, max_paths) ||
238 !allocArray(s->node_buf, 2 * frontier) ||
239 !allocArray(s->nodep_buf, 2 * frontier) ||
240 !allocArray(s->trellis_hash, 65536))
241 return AVERROR(AVERROR_MEMORY);
242 }
243
244 avctx.bits_per_coded_sample = av_get_bits_per_sample();
245
246 return adpcm_encode_init_impl();
247 }
248
249 int adpcm_encode_close() {
250 ADPCMEncodeContext *s = (ADPCMEncodeContext *)avctx.priv_data;
251 freeArray(s->paths);
252 freeArray(s->node_buf);
253 freeArray(s->nodep_buf);
254 freeArray(s->trellis_hash);
255 // extradata is allocated by some encoders' init (e.g. MS, APM)
256 freeArray(avctx.extradata);
257 avctx.extradata_size = 0;
258
259 return 0;
260 }
261
262 inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
263 int16_t sample) {
264 int delta = sample - c->prev_sample;
265 int nibble = FFMIN(7, abs(delta) * 4 / ff_adpcm_step_table[c->step_index]) +
266 (delta < 0) * 8;
267 c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
268 ff_adpcm_yamaha_difflookup[nibble]) /
269 8);
270 c->prev_sample = av_clip_int16(c->prev_sample);
271 c->step_index =
272 av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
273 return nibble;
274 }
275
279 int shift) {
280 int step = ff_adpcm_step_table[cs->step_index];
281 int diff = target - cs->predictor;
282 int mag = diff < 0 ? -diff : diff;
283
284 int best = 0, best_e = 0x7fffffff;
285 for (int d = 0; d < 8; d++) {
286 int rec = ((2 * d + 1) * step) >> shift;
287 int e = mag > rec ? mag - rec : rec - mag;
288 if (e < best_e) {
289 best_e = e;
290 best = d;
291 }
292 }
293 int nibble = best | (diff < 0 ? 8 : 0);
294
295 int rec = ((2 * best + 1) * step) >> shift;
296 cs->predictor = av_clip_int16(cs->predictor + (nibble & 8 ? -rec : rec));
297 cs->step_index =
298 av_clip(cs->step_index + ff_adpcm_index_table[nibble], 0, 88);
299 return (uint8_t)nibble;
300 }
301
302 inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
303 int16_t sample) {
304 int delta = sample - c->prev_sample;
305 int diff, step = ff_adpcm_step_table[c->step_index];
306 int nibble = 8 * (delta < 0);
307
308 delta = abs(delta);
309 diff = delta + (step >> 3);
310
311 if (delta >= step) {
312 nibble |= 4;
313 delta -= step;
314 }
315 step >>= 1;
316 if (delta >= step) {
317 nibble |= 2;
318 delta -= step;
319 }
320 step >>= 1;
321 if (delta >= step) {
322 nibble |= 1;
323 delta -= step;
324 }
325 diff -= delta;
326
327 if (nibble & 8)
328 c->prev_sample -= diff;
329 else
330 c->prev_sample += diff;
331
332 c->prev_sample = av_clip_int16(c->prev_sample);
333 c->step_index =
334 av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
335
336 return nibble;
337 }
338
339 inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
340 int16_t sample) {
341 int nibble, delta;
342
343 if (!c->step) {
344 c->predictor = 0;
345 c->step = 127;
346 }
347
348 delta = sample - c->predictor;
349
350 nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
351
352 c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
353 c->predictor = av_clip_int16(c->predictor);
354 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
355 c->step = av_clip(c->step, 127, 24576);
356
357 return nibble;
358 }
359
360 virtual int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
361 int *got_packet_ptr) = 0;
362
363 virtual int adpcm_encode_frame(AVPacket *avpkt, const AVFrame *frame,
364 int *got_packet_ptr) {
365 c = (ADPCMEncodeContext *)avctx.priv_data;
366
367 samples = (const int16_t *)frame->data[0];
368 samples_p = (const int16_t *const *)frame->extended_data;
369 assert(samples_p != NULL);
370 assert(samples != NULL);
371 st = channels() == 2;
372
373 pkt_size = packetSize(frame);
374 if ((ret = ff_get_encode_buffer(&avctx, avpkt, pkt_size, 0)) < 0)
375 return ret;
376 dst = avpkt->data;
377
378 int rc = adpcm_encode_frame_impl(avpkt, frame, got_packet_ptr);
379 if (rc != AV_OK) return rc;
380
381 *got_packet_ptr = 1;
382 return 0;
383 }
384};
385
387 public:
388 bool isTrellis() { return avctx.trellis; }
389 void setTrellis(bool flag) { avctx.trellis = flag;}
390 protected:
391
392 void store_node(int STEP_INDEX, int cur_nibble) {
393 int d;
394 uint32_t ssd;
395 int pos;
396 TrellisNode *u;
397 uint8_t *h;
398 dec_sample = av_clip_int16(dec_sample);
399 d = sample - dec_sample;
400 ssd = nodes[j]->ssd +
401 d * (unsigned)d; /* Check for wraparound, skip such samples
402 * completely. \
403 * Note, changing ssd to a 64 bit variable would be \
404 * simpler, avoiding this check, but it's slower on \
405 * x86 32 bit at the moment. */
406 if (ssd < nodes[j]->ssd) {
407 return;
408 }
409 h = &hash[(uint16_t)dec_sample];
410 if (*h == generation) return;
411 if (heap_pos < frontier) {
412 pos = heap_pos++;
413 } else { /* Try to replace one of the leaf nodes with the new \
414 * one, but try a different slot each time. */
415 pos = (frontier >> 1) + (heap_pos & ((frontier >> 1) - 1));
416 if (ssd > nodes_next[pos]->ssd) return;
417 heap_pos++;
418 }
419 *h = generation;
420 u = nodes_next[pos];
421 if (!u) {
422 av_assert(pathn < FREEZE_INTERVAL << avctx.trellis);
423 u = t++;
424 nodes_next[pos] = u;
425 u->path = pathn++;
426 }
427 u->ssd = ssd;
428 u->step = STEP_INDEX;
429 u->sample2 = nodes[j]->sample1;
430 u->sample1 = dec_sample;
431 paths[u->path].nibble = cur_nibble;
432 paths[u->path].prev =
433 nodes[j]->path; /* Sift the newly inserted node up in the heap to \
434 * restore the heap property. */
435 while (pos > 0) {
436 int parent = (pos - 1) >> 1;
437 if (nodes_next[parent]->ssd <= ssd) break;
438 FFSWAP(TrellisNode *, nodes_next[parent], nodes_next[pos]);
439 pos = parent;
440 }
441 }
442
443 void loop_nodes(int16_t STEP_TABLE, int step) {
444 const int predictor = nodes[j]->sample1;
445 const int div = (sample - predictor) * 4 / STEP_TABLE;
446 int nmin = av_clip(div - range, -7, 6);
447 int nmax = av_clip(div + range, -6, 7);
448 if (nmin <= 0) nmin--; /* distinguish -0 from +0 */
449 if (nmax < 0) nmax--;
450 for (nidx = nmin; nidx <= nmax; nidx++) {
451 nibble = nidx < 0 ? 7 - nidx : nidx;
452 dec_sample =
453 predictor + (STEP_TABLE * ff_adpcm_yamaha_difflookup[nibble]) / 8;
454 if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
455 version == AV_CODEC_ID_ADPCM_IMA_QT ||
456 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
457 version == AV_CODEC_ID_ADPCM_SWF) {
458 store_node(av_clip(step + ff_adpcm_index_table[nibble], 0, 88), nibble);
459 } else {
460 store_node(av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
461 127, 24576), nibble);
462 }
463 }
464 }
465
466 void adpcm_compress_trellis(const int16_t *samples, uint8_t *dst,
467 ADPCMChannelStatus *c, int n, int stride) {
468 // FIXME 6% faster if frontier is a compile-time constant
469 s = (ADPCMEncodeContext *)avctx.priv_data;
470 frontier = 1 << avctx.trellis;
471 version = avctx.codec_id;
472 paths = s->paths;
473 node_buf = s->node_buf;
474 nodep_buf = s->nodep_buf;
475 nodes = nodep_buf; // nodes[] is always sorted by .ssd
476 nodes_next = nodep_buf + frontier;
477 pathn = 0;
478 froze = -1;
479 generation = 0;
480 hash = s->trellis_hash;
481 memset(hash, 0xff, 65536 * sizeof(*hash));
482
483 memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
484 nodes[0] = node_buf + frontier;
485 nodes[0]->ssd = 0;
486 nodes[0]->path = 0;
487 nodes[0]->step = c->step_index;
488 nodes[0]->sample1 = c->sample1;
489 nodes[0]->sample2 = c->sample2;
490 if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
491 version == AV_CODEC_ID_ADPCM_IMA_QT ||
492 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
493 version == AV_CODEC_ID_ADPCM_SWF)
494 nodes[0]->sample1 = c->prev_sample;
495 if (version == AV_CODEC_ID_ADPCM_MS) nodes[0]->step = c->idelta;
496 if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
497 if (c->step == 0) {
498 nodes[0]->step = 127;
499 nodes[0]->sample1 = 0;
500 } else {
501 nodes[0]->step = c->step;
502 nodes[0]->sample1 = c->predictor;
503 }
504 }
505
506 for (i = 0; i < n; i++) {
507 t = node_buf + frontier * (i & 1);
508 sample = samples[i * stride];
509 heap_pos = 0;
510 memset(nodes_next, 0, frontier * sizeof(TrellisNode *));
511 for (j = 0; j < frontier && nodes[j]; j++) {
512 // higher j have higher ssd already, so they're likely
513 // to yield a suboptimal next sample too
514 range = (j < frontier / 2) ? 1 : 0;
515 const int step = nodes[j]->step;
516 if (version == AV_CODEC_ID_ADPCM_MS) {
517 const int predictor = ((nodes[j]->sample1 * c->coeff1) +
518 (nodes[j]->sample2 * c->coeff2)) /
519 64;
520 const int div = (sample - predictor) / step;
521 const int nmin = av_clip(div - range, -8, 6);
522 const int nmax = av_clip(div + range, -7, 7);
523 for (nidx = nmin; nidx <= nmax; nidx++) {
524 nibble = nidx & 0xf;
525 dec_sample = predictor + nidx * step;
526 store_node(
527 FFMAX(16, (ff_adpcm_AdaptationTable[nibble] * step) >> 8), nibble);
528 }
529 } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
530 version == AV_CODEC_ID_ADPCM_IMA_QT ||
531 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
532 version == AV_CODEC_ID_ADPCM_SWF) {
533 loop_nodes(ff_adpcm_step_table[step], step);
534 } else { // AV_CODEC_ID_ADPCM_YAMAHA
535 loop_nodes(step, step);
536 }
537 }
538
539 u = nodes;
540 nodes = nodes_next;
541 nodes_next = u;
542
543 generation++;
544 if (generation == 255) {
545 memset(hash, 0xff, 65536 * sizeof(*hash));
546 generation = 0;
547 }
548
549 // prevent overflow
550 if (nodes[0]->ssd > (1 << 28)) {
551 for (j = 1; j < frontier && nodes[j]; j++)
552 nodes[j]->ssd -= nodes[0]->ssd;
553 nodes[0]->ssd = 0;
554 }
555
556 // merge old paths to save memory
557 if (i == froze + FREEZE_INTERVAL) {
558 p = &paths[nodes[0]->path];
559 for (k = i; k > froze; k--) {
560 dst[k] = p->nibble;
561 p = &paths[p->prev];
562 }
563 froze = i;
564 pathn = 0;
565 // other nodes might use paths that don't coincide with the frozen one.
566 // checking which nodes do so is too slow, so just kill them all.
567 // this also slightly improves quality, but I don't know why.
568 memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode *));
569 }
570 }
571
572 p = &paths[nodes[0]->path];
573 for (i = n - 1; i > froze; i--) {
574 dst[i] = p->nibble;
575 p = &paths[p->prev];
576 }
577
578 c->predictor = nodes[0]->sample1;
579 c->sample1 = nodes[0]->sample1;
580 c->sample2 = nodes[0]->sample2;
581 c->step_index = nodes[0]->step;
582 c->step = nodes[0]->step;
583 c->idelta = nodes[0]->step;
584 }
585
586 int frontier;
587 int version;
588 TrellisPath *paths, *p;
589 TrellisNode *node_buf;
590 TrellisNode **nodep_buf;
591 TrellisNode **nodes; // nodes[] is always sorted by .ssd
592 TrellisNode **nodes_next;
593 TrellisNode **u;
594 TrellisNode *t;
595
596 int pathn = 0, froze = -1, i, j, k, generation = 0;
597 uint8_t *hash;
598 int dec_sample = 0;
599 int sample = 0;
600 int nibble = 0;
601 int nidx = 0;
602 int range = 0;
603 int heap_pos = 0;
604};
605
607 public:
609 setCodecID(AV_CODEC_ID_ADPCM_IMA_WAV);
610 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
611 }
612 protected:
613 int adpcm_encode_init_impl() {
614 /* each 16 bits sample gives one nibble
615 and we have 4 bytes per channel overhead */
616 avctx.frame_size = (s->block_size - 4 * channels()) * 8 / (4 * channels()) + 1;
617 /* seems frame_size isn't taken into account...
618 have to buffer the samples :-( */
619 avctx.block_align = s->block_size;
620 avctx.bits_per_coded_sample = 4;
621 return AV_OK;
622 }
623
624 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
625 int *got_packet_ptr) {
626 int blocks = (frame->nb_samples - 1) / 8;
627
628 for (int ch = 0; ch < channels(); ch++) {
629 ADPCMChannelStatus *status = &c->status[ch];
630 status->prev_sample = samples_p[ch][0];
631 /* status->step_index = 0;
632 XXX: not sure how to init the state machine */
633 ByteWriter(dst).putLe16(status->prev_sample);
634 *dst++ = status->step_index;
635 *dst++ = 0; /* unknown */
636 }
637
638 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
639 if (avctx.trellis > 0) {
640 uint8_t *buf;
641 if (!allocArray(buf, channels() * blocks * 8))
642 return AVERROR(AVERROR_MEMORY);
643 for (int ch = 0; ch < channels(); ch++) {
644 adpcm_compress_trellis(&samples_p[ch][1], buf + ch * blocks * 8,
645 &c->status[ch], blocks * 8, 1);
646 }
647 for (int i = 0; i < blocks; i++) {
648 for (int ch = 0; ch < channels(); ch++) {
649 uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
650 for (int j = 0; j < 8; j += 2) *dst++ = buf1[j] | (buf1[j + 1] << 4);
651 }
652 }
653 delete[] buf;
654 } else {
655 for (int i = 0; i < blocks; i++) {
656 for (int ch = 0; ch < channels(); ch++) {
657 ADPCMChannelStatus *status = &c->status[ch];
658 const int16_t *smp = &samples_p[ch][1 + i * 8];
659 for (int j = 0; j < 8; j += 2) {
660 uint8_t v = adpcm_ima_compress_sample(status, smp[j]);
661 v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
662 *dst++ = v;
663 }
664 }
665 }
666 }
667 return AV_OK;
668 }
669};
670
679 public:
681 setCodecID(AV_CODEC_ID_ADPCM_IMA_QT);
682 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
683 }
684
685
686 protected:
687 int adpcm_encode_init_impl() {
688 avctx.frame_size = 64;
689 avctx.block_align = 34 * channels();
690 return AV_OK;
691 }
692
693 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
694 int *got_packet_ptr) {
695 uint8_t *out = dst;
696 for (int ch = 0; ch < channels(); ch++) {
697 ADPCMChannelStatus *status = &c->status[ch];
698 // the decoder keeps its own predictor when the step index matches and
699 // the header predictor is within 127, so the header only needs the
700 // predictor at the start of the block (its low 7 bits are dropped)
701 uint16_t hdr = (uint16_t)((status->prev_sample & ~0x7F) |
702 (status->step_index & 0x7F));
703 *out++ = (uint8_t)(hdr >> 8);
704 *out++ = (uint8_t)(hdr & 0xFF);
705
706 if (avctx.trellis > 0) {
707 uint8_t nibbles[64];
708 adpcm_compress_trellis(&samples_p[ch][0], nibbles, status, 64, 1);
709 for (int i = 0; i < 64; i += 2)
710 *out++ = (uint8_t)((nibbles[i] & 0x0F) | ((nibbles[i + 1] & 0x0F) << 4));
711 status->prev_sample = status->predictor;
712 } else {
713 for (int i = 0; i < 64; i += 2) {
714 int t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i]);
715 int t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
716 *out++ = (uint8_t)((t1 & 0x0F) | ((t2 & 0x0F) << 4));
717 }
718 }
719 }
720 assert(out - dst == pkt_size);
721 return AV_OK;
722 }
723};
724
726 public:
728 setCodecID(AV_CODEC_ID_ADPCM_IMA_SSI);
729 sample_formats.push_back(AV_SAMPLE_FMT_S16);
730 }
731
732 protected:
733 int adpcm_encode_init_impl() {
734 avctx.frame_size = s->block_size * 2 / channels();
735 avctx.block_align = s->block_size;
736 return AV_OK;
737 }
738
739 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
740 int *got_packet_ptr) {
741 NibbleWriter pb{dst};
742
743 av_assert(avctx.trellis == 0);
744
745 for (int i = 0; i < frame->nb_samples; i++) {
746 for (int ch = 0; ch < channels(); ch++) {
747 pb.put(
748 adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
749 }
750 }
751
752 return AV_OK;
753 }
754};
755
757 public:
759 setCodecID(AV_CODEC_ID_ADPCM_IMA_ALP);
760 sample_formats.push_back(AV_SAMPLE_FMT_S16);
761 }
762
763 inline uint8_t adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c,
764 int16_t sample) {
765 const int delta = sample - c->prev_sample;
766 const int step = ff_adpcm_step_table[c->step_index];
767 const int sign = (delta < 0) * 8;
768
769 int nibble = FFMIN(abs(delta) * 4 / step, 7);
770 int diff = (step * nibble) >> 2;
771 if (sign) diff = -diff;
772
773 nibble = sign | nibble;
774
775 c->prev_sample += diff;
776 c->prev_sample = av_clip_int16(c->prev_sample);
777 c->step_index =
778 av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
779 return nibble;
780 }
781
782 protected:
783 int adpcm_encode_init_impl() {
784 avctx.frame_size = s->block_size * 2 / channels();
785 avctx.block_align = s->block_size;
786 return AV_OK;
787 }
788
789 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
790 int *got_packet_ptr) {
791 NibbleWriter pb{dst};
792
793 av_assert(avctx.trellis == 0);
794
795 for (int n = frame->nb_samples / 2; n > 0; n--) {
796 for (int ch = 0; ch < channels(); ch++) {
797 pb.put(
798 adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
799 pb.put(
800 adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
801 }
802 samples += channels();
803 }
804
805 return AV_OK;
806 }
807};
808
810 public:
812 setCodecID(AV_CODEC_ID_ADPCM_MS);
813 sample_formats.push_back(AV_SAMPLE_FMT_S16);
814 }
815
816 inline uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
817 int16_t sample) {
818 int predictor, nibble, bias;
819
820 predictor =
821 (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
822
823 nibble = sample - predictor;
824 if (nibble >= 0)
825 bias = c->idelta / 2;
826 else
827 bias = -c->idelta / 2;
828
829 nibble = (nibble + bias) / c->idelta;
830 nibble = av_clip_intp2(nibble, 3) & 0x0F;
831
832 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
833
834 c->sample2 = c->sample1;
835 c->sample1 = av_clip_int16(predictor);
836
837 c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
838 if (c->idelta < 16) c->idelta = 16;
839
840 return nibble;
841 }
842
843 protected:
844 int adpcm_encode_init_impl() {
845 uint8_t *extradata;
846 /* each 16 bits sample gives one nibble
847 and we have 7 bytes per channel overhead */
848 avctx.frame_size = (s->block_size - 7 * channels()) * 2 / channels() + 2;
849 avctx.bits_per_coded_sample = 4;
850 avctx.block_align = s->block_size;
851 if (!(avctx.extradata =
852 new uint8_t[32 + AV_INPUT_BUFFER_PADDING_SIZE]))
853 return AVERROR(AVERROR_MEMORY);
854 avctx.extradata_size = 32;
855 extradata = avctx.extradata;
856 ByteWriter(extradata).putLe16(avctx.frame_size);
857 ByteWriter(extradata).putLe16(7); /* wNumCoef */
858 for (int i = 0; i < 7; i++) {
859 ByteWriter(extradata).putLe16(ff_adpcm_AdaptCoeff1[i] * 4);
860 ByteWriter(extradata).putLe16(ff_adpcm_AdaptCoeff2[i] * 4);
861 }
862 return AV_OK;
863 }
864
865 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
866 int *got_packet_ptr) {
867 for (int i = 0; i < channels(); i++) {
868 int predictor = 0;
869 *dst++ = predictor;
870 c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
871 c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
872 }
873 for (int i = 0; i < channels(); i++) {
874 if (c->status[i].idelta < 16) c->status[i].idelta = 16;
875 ByteWriter(dst).putLe16(c->status[i].idelta);
876 }
877 for (int i = 0; i < channels(); i++) c->status[i].sample2 = *samples++;
878 for (int i = 0; i < channels(); i++) {
879 c->status[i].sample1 = *samples++;
880 ByteWriter(dst).putLe16(c->status[i].sample1);
881 }
882 for (int i = 0; i < channels(); i++)
883 ByteWriter(dst).putLe16(c->status[i].sample2);
884
885 if (avctx.trellis > 0) {
886 // samples per channel in this block: each byte holds two nibbles, and
887 // the 7 bytes per channel of header were already written
888 const int n = (avctx.block_align - 7 * channels()) * 2 / channels();
889 uint8_t *buf = new uint8_t[2 * n];
890 if (!buf) return AVERROR(AVERROR_MEMORY);
891 if (channels() == 1) {
892 adpcm_compress_trellis(samples, buf, &c->status[0], n, channels());
893 for (int i = 0; i < n; i += 2) *dst++ = (buf[i] << 4) | buf[i + 1];
894 } else {
895 adpcm_compress_trellis(samples, buf, &c->status[0], n, channels());
896 adpcm_compress_trellis(samples + 1, buf + n, &c->status[1], n,
897 channels());
898 for (int i = 0; i < n; i++) *dst++ = (buf[i] << 4) | buf[n + i];
899 }
900 delete[] buf;
901 } else {
902 for (int i = 7 * channels(); i < avctx.block_align; i++) {
903 int nibble;
904 nibble = adpcm_ms_compress_sample(&c->status[0], *samples++) << 4;
905 nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
906 *dst++ = nibble;
907 }
908 } /* End of CASE */
909 return AV_OK;
910 }
911};
912
914 public:
916 setCodecID(AV_CODEC_ID_ADPCM_SWF);
917 sample_formats.push_back(AV_SAMPLE_FMT_S16);
918 }
919
920 protected:
921 int adpcm_encode_init_impl() {
922 if (avctx.sample_rate != 11025 && avctx.sample_rate != 22050 &&
923 avctx.sample_rate != 44100) {
924 av_log(avctx, AV_LOG_ERROR,
925 "Sample rate must be 11025, "
926 "22050 or 44100\n");
927 return AVERROR(AVERROR_INVALID);
928 }
929 avctx.frame_size = 4096; /* Hardcoded according to the SWF spec. */
930 avctx.block_align =
931 (2 + channels() * (22 + 4 * (avctx.frame_size - 1)) + 7) / 8;
932 return AV_OK;
933 }
934
935 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
936 int *got_packet_ptr) {
937 const int n = frame->nb_samples - 1;
938 BitWriter pb(dst, pkt_size);
939 pb.setMsbFirst(true); // SWF bit fields are most significant bit first
940
941 /* NB: This is safe as we don't have AV_CODEC_CAP_SMALL_LAST_FRAME. */
942 av_assert(n == 4095);
943
944 // store AdpcmCodeSize
945 pb.put(2, 2); // set 4-bit flash adpcm format
946
947 // init the encoder state
948 for (int i = 0; i < channels(); i++) {
949 // clip step so it fits 6 bits
950 c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
951 pb.putSigned(16, samples[i]);
952 pb.put(6, c->status[i].step_index);
953 c->status[i].prev_sample = samples[i];
954 }
955
956 if (avctx.trellis > 0) {
957 uint8_t buf[8190 /* = 2 * n */];
958 adpcm_compress_trellis(samples + channels(), buf, &c->status[0], n,
959 channels());
960 if (channels() == 2)
961 adpcm_compress_trellis(samples + channels() + 1, buf + n, &c->status[1],
962 n, channels());
963 for (int i = 0; i < n; i++) {
964 pb.put(4, buf[i]);
965 if (channels() == 2) pb.put(4, buf[n + i]);
966 }
967 } else {
968 for (int i = 1; i < frame->nb_samples; i++) {
969 pb.put(4, adpcm_ima_compress_sample(&c->status[0], samples[channels() * i]));
970 if (channels() == 2)
971 pb.put(4, adpcm_ima_compress_sample(&c->status[1], samples[2 * i + 1]));
972 }
973 }
974 pb.flush();
975 return AV_OK;
976 }
977};
978
980 public:
982 setCodecID(AV_CODEC_ID_ADPCM_YAMAHA);
983 sample_formats.push_back(AV_SAMPLE_FMT_S16);
984 }
985
986 protected:
987 int adpcm_encode_init_impl() {
988 avctx.frame_size = s->block_size * 2 / channels();
989 avctx.block_align = s->block_size;
990 return AV_OK;
991 }
992
993 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
994 int *got_packet_ptr) {
995 int n = frame->nb_samples / 2;
996 if (avctx.trellis > 0) {
997 uint8_t *buf = new uint8_t[2 * n * 2];
998 if (!buf) return AVERROR(AVERROR_MEMORY);
999 n *= 2;
1000 if (channels() == 1) {
1001 adpcm_compress_trellis(samples, buf, &c->status[0], n, channels());
1002 for (int i = 0; i < n; i += 2) *dst++ = buf[i] | (buf[i + 1] << 4);
1003 } else {
1004 adpcm_compress_trellis(samples, buf, &c->status[0], n, channels());
1005 adpcm_compress_trellis(samples + 1, buf + n, &c->status[1], n,
1006 channels());
1007 for (int i = 0; i < n; i++) *dst++ = buf[i] | (buf[n + i] << 4);
1008 }
1009 delete[] buf;
1010 } else
1011 for (n *= channels(); n > 0; n--) {
1012 int nibble;
1013 nibble = adpcm_yamaha_compress_sample(&c->status[0], *samples++);
1014 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
1015 *dst++ = nibble;
1016 }
1017 return AV_OK;
1018 }
1019};
1020
1022 public:
1024 setCodecID(AV_CODEC_ID_ADPCM_IMA_APM);
1025 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1026 }
1027
1028 protected:
1029 int adpcm_encode_init_impl() {
1030 avctx.frame_size = s->block_size * 2 / channels();
1031 avctx.block_align = s->block_size;
1032
1033 if (!(avctx.extradata =
1034 new uint8_t[28 + AV_INPUT_BUFFER_PADDING_SIZE]()))
1035 return AVERROR(AVERROR_MEMORY);
1036 avctx.extradata_size = 28;
1037 return AV_OK;
1038 }
1039
1040 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1041 int *got_packet_ptr) {
1042 NibbleWriter pb{dst};
1043
1044 av_assert(avctx.trellis == 0);
1045
1046 for (int n = frame->nb_samples / 2; n > 0; n--) {
1047 for (int ch = 0; ch < channels(); ch++) {
1048 pb.put(
1049 adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
1050 pb.put(
1051 adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
1052 }
1053 samples += channels();
1054 }
1055
1056 return AV_OK;
1057 }
1058};
1059
1061 public:
1063 setCodecID(AV_CODEC_ID_ADPCM_IMA_AMV);
1064 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1065 }
1066
1067 protected:
1068 int adpcm_encode_init_impl() {
1069 if (avctx.sample_rate != 22050) {
1070 av_log(avctx, AV_LOG_ERROR, "Sample rate must be 22050\n");
1071 return AVERROR(AVERROR_INVALID);
1072 }
1073
1074 if (channels() != 1) {
1075 av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
1076 return AVERROR(AVERROR_INVALID);
1077 }
1078
1079 avctx.frame_size = s->block_size;
1080 avctx.block_align = 8 + (FFALIGN(avctx.frame_size, 2) / 2);
1081 return AV_OK;
1082 }
1083
1084 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1085 int *got_packet_ptr) {
1086 av_assert(channels() == 1);
1087
1088 c->status[0].prev_sample = *samples;
1089 ByteWriter(dst).putLe16(c->status[0].prev_sample);
1090 ByteWriter(dst).putByte(c->status[0].step_index);
1091 ByteWriter(dst).putByte(0);
1092 ByteWriter(dst).putLe32(avctx.frame_size);
1093
1094 if (avctx.trellis > 0) {
1095 const int n = frame->nb_samples >> 1;
1096 uint8_t *buf = new uint8_t[2 * n];
1097
1098 if (!buf) return AVERROR(AVERROR_MEMORY);
1099
1100 adpcm_compress_trellis(samples, buf, &c->status[0], 2 * n, channels());
1101 for (int i = 0; i < n; i++)
1102 ByteWriter(dst).putByte((buf[2 * i] << 4) | buf[2 * i + 1]);
1103
1104 samples += 2 * n;
1105 delete[] buf;
1106 } else
1107 for (int n = frame->nb_samples >> 1; n > 0; n--) {
1108 int nibble;
1109 nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
1110 nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
1111 ByteWriter(dst).putByte(nibble);
1112 }
1113
1114 if (avctx.frame_size & 1) {
1115 int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
1116 ByteWriter(dst).putByte(nibble);
1117 }
1118 return AV_OK;
1119 }
1120};
1121
1123 public:
1125 setCodecID(AV_CODEC_ID_ADPCM_ARGO);
1126 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1127 }
1128
1129 int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
1130 int shift, int flag) {
1131 int nibble;
1132
1133 if (flag)
1134 nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
1135 else
1136 nibble = 4 * s - 4 * cs->sample1;
1137
1138 return (nibble >> shift) & 0x0F;
1139 }
1140
1141 int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble,
1142 int shift, int flag) {
1143 int sample = sign_extend(nibble, 4) * (1 << shift);
1144
1145 if (flag)
1146 sample += (8 * cs->sample1) - (4 * cs->sample2);
1147 else
1148 sample += 4 * cs->sample1;
1149
1150 sample = av_clip_int16(sample >> 2);
1151
1152 cs->sample2 = cs->sample1;
1153 cs->sample1 = sample;
1154
1155 return sample;
1156 }
1157
1164 const int16_t *samples, int nsamples,
1165 int shift, int flag) {
1166 int64_t error = 0;
1167 uint8_t nibbles[32] = {0};
1168
1169 for (int n = 0; n < nsamples; n++) {
1170 /* Compress the nibble, then expand it to see how much precision we've
1171 * lost. */
1172 int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
1173 int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
1174
1175 error += abs(samples[n] - sample);
1176 nibbles[n] = (uint8_t)nibble;
1177 }
1178
1179 if (out) {
1180 // control byte: shift - 2 in the high nibble, flag in bit 2
1181 out[0] = (uint8_t)(((shift - 2) << 4) | (!!flag << 2));
1182 for (int n = 0; n < 16; n++) {
1183 out[1 + n] = (uint8_t)((nibbles[2 * n] << 4) | nibbles[2 * n + 1]);
1184 }
1185 }
1186
1187 return error;
1188 }
1189
1190 protected:
1191 int adpcm_encode_init_impl() {
1192 avctx.frame_size = 32;
1193 avctx.block_align = 17 * channels();
1194 return AV_OK;
1195 }
1196
1197 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1198 int *got_packet_ptr) {
1199 av_assert(frame->nb_samples == 32);
1200
1201 for (int ch = 0; ch < channels(); ch++) {
1202 int64_t error = INT64_MAX, tmperr = INT64_MAX;
1203 int shift = 2, flag = 0;
1204 int saved1 = c->status[ch].sample1;
1205 int saved2 = c->status[ch].sample2;
1206
1207 /* Find the optimal coefficients, bail early if we find a perfect
1208 * result. */
1209 for (int s = 2; s < 18 && tmperr != 0; s++) {
1210 for (int f = 0; f < 2 && tmperr != 0; f++) {
1211 c->status[ch].sample1 = saved1;
1212 c->status[ch].sample2 = saved2;
1214 c->status + ch, NULL, samples_p[ch], frame->nb_samples, s, f);
1215 if (tmperr < error) {
1216 shift = s;
1217 flag = f;
1218 error = tmperr;
1219 }
1220 }
1221 }
1222
1223 /* Now actually do the encode. Each channel occupies 17 bytes. */
1224 c->status[ch].sample1 = saved1;
1225 c->status[ch].sample2 = saved2;
1226 adpcm_argo_compress_block(c->status + ch, dst + ch * 17,
1227 samples_p[ch], frame->nb_samples, shift, flag);
1228 }
1229
1230 return AV_OK;
1231 }
1232};
1233
1235 public:
1237 setCodecID(AV_CODEC_ID_ADPCM_IMA_WS);
1238 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1239 }
1240
1241 protected:
1242 int adpcm_encode_init_impl() {
1243 /* each 16 bits sample gives one nibble */
1244 avctx.frame_size = s->block_size * 2 / channels();
1245 avctx.block_align = s->block_size;
1246 return AV_OK;
1247 }
1248
1249 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1250 int *got_packet_ptr) {
1251 NibbleWriter pb{dst};
1252
1253 av_assert(avctx.trellis == 0);
1254 for (int n = frame->nb_samples / 2; n > 0; n--) {
1255 /* stereo: 1 byte (2 samples) for left, 1 byte for right */
1256 for (int ch = 0; ch < channels(); ch++) {
1257 int t1, t2;
1258 t1 = adpcm_ima_compress_sample(&c->status[ch], *samples++);
1259 t2 = adpcm_ima_compress_sample(&c->status[ch], samples[st]);
1260 pb.put( t2);
1261 pb.put( t1);
1262 }
1263 samples += channels();
1264 }
1265 return AV_OK;
1266 }
1267};
1268
1276 public:
1278 setCodecID(AV_CODEC_ID_ADPCM_PSX);
1279 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1280 }
1281
1282
1283
1284 protected:
1285
1286 int adpcm_encode_init_impl() {
1287 if (channels() < 1 || channels() > 6) return AVERROR(AVERROR_INVALID);
1288 memset(&s->status, 0, sizeof(s->status));
1289 avctx.frame_size = 28;
1290 avctx.block_align = 16 * channels();
1291 return AV_OK;
1292 }
1293
1294 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1295 int *got_packet_ptr) {
1296 if (frame->nb_samples != 28) return AVERROR(AVERROR_INVALID);
1297 for (int ch = 0; ch < channels(); ch++) {
1298 encodeBlock(&c->status[ch], samples_p[ch], dst + ch * 16);
1299 }
1300 return AV_OK;
1301 }
1302 void encodeBlock(ADPCMChannelStatus *cs, const int16_t *in, uint8_t *out) {
1303 long long best_err = -1;
1304 int best_nib[28] = {0};
1305 int best_filter = 0, best_shift = 0, best_s1 = 0, best_s2 = 0;
1306
1307 for (int filter = 0; filter < 5; filter++) {
1308 for (int shift = 0; shift <= 12; shift++) {
1309 int nib[28];
1310 int s1 = cs->sample1, s2 = cs->sample2;
1311 long long err = 0;
1312 for (int n = 0; n < 28; n++) {
1313 int pred = (s1 * xa_adpcm_table[filter][0] +
1314 s2 * xa_adpcm_table[filter][1]) / 64;
1315 int base = ((in[n] - pred) * (1 << shift)) / 4096;
1316 int best_n = 0, best_e = 0x7fffffff, best_u = 0;
1317 for (int cand = base - 1; cand <= base + 1; cand++) {
1318 int v = cand < -8 ? -8 : (cand > 7 ? 7 : cand);
1319 // same reconstruction as DecoderADPCM_PSX
1320 int unclipped = pred + ((v * 4096) >> shift);
1321 int e = in[n] - av_clip_int16(unclipped);
1322 if (e < 0) e = -e;
1323 if (e < best_e) {
1324 best_e = e;
1325 best_n = v;
1326 best_u = unclipped;
1327 }
1328 }
1329 nib[n] = best_n;
1330 err += (long long)best_e * best_e;
1331 s2 = s1;
1332 s1 = best_u;
1333 }
1334 if (best_err < 0 || err < best_err) {
1335 best_err = err;
1337 best_shift = shift;
1338 best_s1 = s1;
1339 best_s2 = s2;
1340 memcpy(best_nib, nib, sizeof(nib));
1341 }
1342 }
1343 }
1344
1345 out[0] = (uint8_t)((best_filter << 4) | best_shift);
1346 out[1] = 0; // flag: regular sample data
1347 for (int k = 0; k < 14; k++) {
1348 out[2 + k] = (uint8_t)((best_nib[2 * k] & 0x0F) |
1349 ((best_nib[2 * k + 1] & 0x0F) << 4));
1350 }
1351 cs->sample1 = best_s1;
1352 cs->sample2 = best_s2;
1353 }
1354};
1355
1362 public:
1363 static const int FRAME_SIZE = 1025;
1364
1366 setCodecID(AV_CODEC_ID_ADPCM_IMA_DK4);
1367 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1368 }
1369
1370
1371 protected:
1372 int adpcm_encode_init_impl() {
1373 // the decoder only supports mono and stereo
1374 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
1375 memset(&s->status, 0, sizeof(s->status));
1376 avctx.frame_size = FRAME_SIZE;
1377 avctx.block_align = 4 * channels() + (FRAME_SIZE - 1) * channels() / 2;
1378 return AV_OK;
1379 }
1380
1381 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1382 int *got_packet_ptr) {
1383 const int nch = channels();
1384 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
1385 uint8_t *out = dst;
1386
1387 // header: first sample (le16) and step index (le16) per channel
1388 for (int ch = 0; ch < nch; ch++) {
1389 ADPCMChannelStatus *cs = &c->status[ch];
1390 int16_t first = samples[ch];
1391 cs->prev_sample = first;
1392 cs->step_index = 0;
1393 *out++ = first & 0xFF;
1394 *out++ = (first >> 8) & 0xFF;
1395 *out++ = 0;
1396 *out++ = 0;
1397 }
1398
1399 // nibbles, in the order the decoder consumes them
1400 if (nch == 2) {
1401 for (int t = 1; t < FRAME_SIZE; t++) {
1402 uint8_t left = adpcm_ima_compress_sample(&c->status[0], samples[t * 2]);
1403 uint8_t right =
1404 adpcm_ima_compress_sample(&c->status[1], samples[t * 2 + 1]);
1405 *out++ = (left << 4) | right;
1406 }
1407 } else {
1408 for (int t = 1; t < FRAME_SIZE; t += 2) {
1409 uint8_t hi = adpcm_ima_compress_sample(&c->status[0], samples[t]);
1410 uint8_t lo = adpcm_ima_compress_sample(&c->status[0], samples[t + 1]);
1411 *out++ = (hi << 4) | lo;
1412 }
1413 }
1414 return AV_OK;
1415 }
1416};
1417
1423 public:
1424 static const int FRAME_SIZE = 1024;
1425
1427 setCodecID(AV_CODEC_ID_ADPCM_AICA);
1428 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1429 }
1430
1431
1432 protected:
1433 int adpcm_encode_init_impl() {
1434 if (channels() < 1 || channels() > 6) return AVERROR(AVERROR_INVALID);
1435 memset(&s->status, 0, sizeof(s->status));
1436 avctx.frame_size = FRAME_SIZE;
1437 avctx.block_align = (FRAME_SIZE / 2) * channels();
1438 return AV_OK;
1439 }
1440
1441 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1442 int *got_packet_ptr) {
1443 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
1444 for (int ch = 0; ch < channels(); ch++) {
1445 uint8_t *out = dst + ch * (FRAME_SIZE / 2);
1446 for (int k = 0; k < FRAME_SIZE / 2; k++) {
1447 uint8_t lo = adpcm_yamaha_compress_sample(&c->status[ch],
1448 samples_p[ch][2 * k]);
1449 uint8_t hi = adpcm_yamaha_compress_sample(&c->status[ch],
1450 samples_p[ch][2 * k + 1]);
1451 out[k] = (hi << 4) | lo;
1452 }
1453 }
1454 return AV_OK;
1455 }
1456};
1457
1463 public:
1464 static const int FRAME_SIZE = 1024;
1465
1467 setCodecID(AV_CODEC_ID_ADPCM_IMA_OKI);
1468 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1469 }
1470
1471
1472
1473 protected:
1474
1475 int adpcm_encode_init_impl() {
1476 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
1477 memset(&s->status, 0, sizeof(s->status));
1478 avctx.frame_size = FRAME_SIZE;
1479 avctx.block_align = (FRAME_SIZE / 2) * channels();
1480 return AV_OK;
1481 }
1482
1483 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1484 int *got_packet_ptr) {
1485 const int nch = channels();
1486 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
1487 uint8_t *out = dst;
1488 if (nch == 2) {
1489 for (int t = 0; t < FRAME_SIZE; t++) {
1490 uint8_t left = quantize(0, samples[t * 2]);
1491 uint8_t right = quantize(1, samples[t * 2 + 1]);
1492 *out++ = (left << 4) | right;
1493 }
1494 } else {
1495 for (int t = 0; t < FRAME_SIZE; t += 2) {
1496 uint8_t hi = quantize(0, samples[t]);
1497 uint8_t lo = quantize(0, samples[t + 1]);
1498 *out++ = (hi << 4) | lo;
1499 }
1500 }
1501 return AV_OK;
1502 }
1503 // picks the nibble closest to the input and advances the state exactly as
1504 // DecoderADPCM_IMA_OKI::adpcm_ima_oki_expand_nibble does
1505 uint8_t quantize(int ch, int16_t sample) {
1506 ADPCMChannelStatus *cs = &c->status[ch];
1507 int target = sample >> 4;
1508 int diff = target - cs->predictor;
1509 int step = oki_step_table[cs->step_index];
1510 int mag = diff < 0 ? -diff : diff;
1511
1512 int best = 0, best_e = 0x7fffffff;
1513 for (int d = 0; d < 8; d++) {
1514 int rec = ((2 * d + 1) * step) >> 3;
1515 int e = mag > rec ? mag - rec : rec - mag;
1516 if (e < best_e) {
1517 best_e = e;
1518 best = d;
1519 }
1520 }
1521 int nibble = best | (diff < 0 ? 8 : 0);
1522
1523 int rec = ((2 * best + 1) * step) >> 3;
1524 cs->predictor = av_clip_intp2(cs->predictor + (nibble & 8 ? -rec : rec), 11);
1525 cs->step_index =
1526 av_clip(cs->step_index + ff_adpcm_index_table[nibble], 0, 48);
1527 return (uint8_t)nibble;
1528 }
1529};
1530
1537 public:
1538 static const int FRAME_SIZE = 1024; // samples per channel
1539
1541 setCodecID(AV_CODEC_ID_ADPCM_IMA_EA_SEAD);
1542 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1543 }
1544
1545
1546
1547 protected:
1548
1549 int adpcm_encode_init_impl() {
1550 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
1551 memset(&s->status, 0, sizeof(s->status));
1552 avctx.frame_size = FRAME_SIZE;
1553 avctx.block_align = (FRAME_SIZE / 2) * channels();
1554 return AV_OK;
1555 }
1556
1557 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1558 int *got_packet_ptr) {
1559 const int nch = channels();
1560 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
1561 uint8_t *out = dst;
1562 if (nch == 2) {
1563 for (int t = 0; t < FRAME_SIZE; t++) {
1564 uint8_t left = quantize(0, samples[t * 2]);
1565 uint8_t right = quantize(1, samples[t * 2 + 1]);
1566 *out++ = (left << 4) | right;
1567 }
1568 } else {
1569 for (int t = 0; t < FRAME_SIZE; t += 2) {
1570 uint8_t hi = quantize(0, samples[t]);
1571 uint8_t lo = quantize(0, samples[t + 1]);
1572 *out++ = (hi << 4) | lo;
1573 }
1574 }
1575 return AV_OK;
1576 }
1577 uint8_t quantize(int ch, int16_t sample) {
1578 return adpcm_ima_quantize(&s->status[ch], sample, 6);
1579 }
1580};
1581
1587 public:
1588 static const int FRAME_SIZE = 1024; // samples per channel
1589
1591 setCodecID(AV_CODEC_ID_ADPCM_IMA_ACORN);
1592 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1593 }
1594
1595
1596
1597 protected:
1598
1599 int adpcm_encode_init_impl() {
1600 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
1601 memset(&s->status, 0, sizeof(s->status));
1602 avctx.frame_size = FRAME_SIZE;
1603 avctx.block_align = 4 * channels() + (FRAME_SIZE / 2) * channels();
1604 return AV_OK;
1605 }
1606
1607 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1608 int *got_packet_ptr) {
1609 const int nch = channels();
1610 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
1611 uint8_t *nib = dst + 4 * nch;
1612 memset(dst, 0, avctx.block_align);
1613
1614 for (int ch = 0; ch < nch; ch++) {
1615 int16_t first = samples[ch];
1616 s->status[ch].predictor = first;
1617 s->status[ch].step_index = 0;
1618 dst[ch * 4 + 0] = first & 0xFF;
1619 dst[ch * 4 + 1] = (first >> 8) & 0xFF;
1620 dst[ch * 4 + 2] = 0;
1621 dst[ch * 4 + 3] = 0;
1622 }
1623
1624 if (nch == 2) {
1625 for (int t = 0; t < FRAME_SIZE; t++) {
1626 uint8_t left = quantize(0, samples[t * 2]);
1627 uint8_t right = quantize(1, samples[t * 2 + 1]);
1628 nib[t] = left | (right << 4);
1629 }
1630 } else {
1631 for (int t = 0; t < FRAME_SIZE; t += 2) {
1632 uint8_t first = quantize(0, samples[t]);
1633 uint8_t second = quantize(0, samples[t + 1]);
1634 nib[t / 2] = first | (second << 4);
1635 }
1636 }
1637 return AV_OK;
1638 }
1639 uint8_t quantize(int ch, int16_t sample) {
1640 return adpcm_ima_quantize(&s->status[ch], sample, 3);
1641 }
1642};
1643
1650 public:
1651 static const int FRAME_SIZE = 1024; // samples per channel
1652
1654 setCodecID(AV_CODEC_ID_ADPCM_IMA_DAT4);
1655 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1656 }
1657
1658
1659
1660 protected:
1661
1662 int adpcm_encode_init_impl() {
1663 if (channels() < 1 || channels() > ADPCM_MAX_CHANNELS)
1664 return AVERROR(AVERROR_INVALID);
1665 memset(&s->status, 0, sizeof(s->status));
1666 avctx.frame_size = FRAME_SIZE;
1667 avctx.block_align = channels() * (4 + FRAME_SIZE / 2);
1668 return AV_OK;
1669 }
1670
1671 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1672 int *got_packet_ptr) {
1673 const int nch = channels();
1674 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
1675 memset(dst, 0, avctx.block_align);
1676 const int chunk = 4 + FRAME_SIZE / 2;
1677 for (int ch = 0; ch < nch; ch++) {
1678 uint8_t *out = dst + ch * chunk + 4;
1679 for (int t = 0; t < FRAME_SIZE; t += 2) {
1680 uint8_t hi = quantize(ch, samples[t * nch + ch]);
1681 uint8_t lo = quantize(ch, samples[(t + 1) * nch + ch]);
1682 *out++ = (hi << 4) | lo;
1683 }
1684 }
1685 return AV_OK;
1686 }
1687 uint8_t quantize(int ch, int16_t sample) {
1688 return adpcm_ima_quantize(&s->status[ch], sample, 3);
1689 }
1690};
1691
1699 public:
1700 static const int FRAME_SIZE = 1024; // samples per channel
1701
1703 setCodecID(AV_CODEC_ID_ADPCM_AGM);
1704 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1705 }
1706
1707
1708
1709 protected:
1710
1711 int adpcm_encode_init_impl() {
1712 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
1713 memset(&s->status, 0, sizeof(s->status));
1714 avctx.frame_size = FRAME_SIZE;
1715 avctx.block_align = channels() * (4 + FRAME_SIZE / 2);
1716 return AV_OK;
1717 }
1718
1719 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1720 int *got_packet_ptr) {
1721 const int nch = channels();
1722 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
1723 memset(dst, 0, avctx.block_align);
1724
1725 // header: predictors (first samples), then steps (minimum 127)
1726 for (int ch = 0; ch < nch; ch++) {
1727 int16_t first = samples[ch];
1728 s->status[ch].predictor = first;
1729 s->status[ch].step = 127;
1730 dst[ch * 2] = first & 0xFF;
1731 dst[ch * 2 + 1] = ((uint16_t)first >> 8) & 0xFF;
1732 dst[2 * nch + ch * 2] = 127;
1733 dst[2 * nch + ch * 2 + 1] = 0;
1734 }
1735
1736 uint8_t *out = dst + 4 * nch;
1737 if (nch == 2) {
1738 for (int t = 0; t < FRAME_SIZE; t++) {
1739 uint8_t ch0 = quantize(0, samples[t * 2]);
1740 uint8_t ch1 = quantize(1, samples[t * 2 + 1]);
1741 *out++ = ch0 | (ch1 << 4);
1742 }
1743 } else {
1744 for (int t = 0; t < FRAME_SIZE; t += 2) {
1745 uint8_t first = quantize(0, samples[t]);
1746 uint8_t second = quantize(0, samples[t + 1]);
1747 *out++ = first | (second << 4);
1748 }
1749 }
1750 return AV_OK;
1751 }
1752 // mirrors DecoderADPCM_AGM::adpcm_agm_expand_nibble without changing state
1753 static int agm_reconstruct(int pred, int step0, int nibble, int *next_step) {
1754 int delta = nibble & 7;
1755 int step = step0;
1756 int add = (delta * 2 + 1) * step;
1757 if (add < 0) add = add + 7;
1758 if ((nibble & 8) == 0)
1759 pred = av_clip(pred + (add >> 3), -32767, 32767);
1760 else
1761 pred = av_clip(pred - (add >> 3), -32767, 32767);
1762
1763 switch (delta) {
1764 case 7:
1765 step *= 0x99;
1766 break;
1767 case 6:
1768 *next_step = av_clip(step0 * 2, 127, 24576);
1769 return pred;
1770 case 5:
1771 step *= 0x66;
1772 break;
1773 case 4:
1774 step *= 0x4d;
1775 break;
1776 default:
1777 step *= 0x39;
1778 break;
1779 }
1780 if (step < 0) step += 0x3f;
1781 *next_step = av_clip(step >> 6, 127, 24576);
1782 return pred;
1783 }
1784
1785 uint8_t quantize(int ch, int16_t target) {
1786 ADPCMChannelStatus *cs = &s->status[ch];
1787 int best = 0, best_e = 0x7fffffff, best_pred = 0, best_step = 127;
1788 for (int nibble = 0; nibble < 16; nibble++) {
1789 int next_step = cs->step;
1790 int pred = agm_reconstruct(cs->predictor, cs->step, nibble, &next_step);
1791 int e = pred > target ? pred - target : target - pred;
1792 if (e < best_e) {
1793 best_e = e;
1794 best = nibble;
1795 best_pred = pred;
1797 }
1798 }
1799 cs->predictor = best_pred;
1800 cs->step = best_step;
1801 return (uint8_t)best;
1802 }
1803};
1804
1813 public:
1814 static const int FRAME_SIZE = 1024; // samples per channel
1815 static const int HEADER = 16;
1816
1818 setCodecID(AV_CODEC_ID_ADPCM_MTAF);
1819 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1820 }
1821
1822
1823
1824 protected:
1825
1826 int adpcm_encode_init_impl() {
1827 // the decoder pairs channels, so only even channel counts up to 8 work
1828 if (channels() < 2 || channels() > 8 || (channels() & 1))
1829 return AVERROR(AVERROR_INVALID);
1830 memset(&s->status, 0, sizeof(s->status));
1831 avctx.frame_size = FRAME_SIZE;
1832 avctx.block_align = (channels() / 2) * (HEADER + FRAME_SIZE);
1833 return AV_OK;
1834 }
1835
1836 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1837 int *got_packet_ptr) {
1838 const int nch = channels();
1839 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
1840 memset(dst, 0, avctx.block_align);
1841
1842 uint8_t *block = dst;
1843 for (int ch = 0; ch < nch; ch += 2) {
1844 uint8_t *h = block;
1845 int16_t first0 = samples[ch];
1846 int16_t first1 = samples[ch + 1];
1847 s->status[ch].predictor = first0;
1848 s->status[ch].step = 0;
1849 s->status[ch + 1].predictor = first1;
1850 s->status[ch + 1].step = 0;
1851 // header: steps (0), predictors at offsets 8 and 12
1852 h[4] = 0;
1853 h[5] = 0;
1854 h[6] = 0;
1855 h[7] = 0;
1856 h[8] = first0 & 0xFF;
1857 h[9] = ((uint16_t)first0 >> 8) & 0xFF;
1858 h[12] = first1 & 0xFF;
1859 h[13] = ((uint16_t)first1 >> 8) & 0xFF;
1860
1861 uint8_t *out = block + HEADER;
1862 for (int c = ch; c < ch + 2; c++) {
1863 for (int t = 0; t < FRAME_SIZE; t += 2) {
1864 uint8_t lo = quantize(c, samples[t * nch + c]);
1865 uint8_t hi = quantize(c, samples[(t + 1) * nch + c]);
1866 *out++ = lo | (hi << 4);
1867 }
1868 }
1869 block += HEADER + FRAME_SIZE;
1870 }
1871 return AV_OK;
1872 }
1873 // mirrors DecoderADPCM_MTAF::adpcm_mtaf_expand_nibble without changing state
1874 uint8_t quantize(int ch, int16_t target) {
1875 ADPCMChannelStatus *cs = &s->status[ch];
1876 int best = 0, best_e = 0x7fffffff, best_pred = 0, best_step = 0;
1877 for (int nibble = 0; nibble < 16; nibble++) {
1878 int pred = av_clip_int16(cs->predictor + mtaf_stepsize[cs->step][nibble]);
1879 int next_step = av_clip_uintp2(cs->step + ff_adpcm_index_table[nibble], 5);
1880 int e = pred > target ? pred - target : target - pred;
1881 if (e < best_e) {
1882 best_e = e;
1883 best = nibble;
1884 best_pred = pred;
1886 }
1887 }
1888 cs->predictor = best_pred;
1889 cs->step = best_step;
1890 return (uint8_t)best;
1891 }
1892};
1893
1903 public:
1904 static const int UNIT_SAMPLES = 28;
1905 static const int UNIT_BYTES = 32;
1906 static const int UNITS = 36;
1907 static const int FRAME_SIZE = UNIT_SAMPLES * UNITS; // 1008 per channel
1908
1910 setCodecID(AV_CODEC_ID_ADPCM_DTK);
1911 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1912 }
1913
1914
1915
1916 protected:
1917
1918 int adpcm_encode_init_impl() {
1919 if (channels() != 2) return AVERROR(AVERROR_INVALID);
1920 memset(&s->status, 0, sizeof(s->status));
1921 avctx.frame_size = FRAME_SIZE;
1922 avctx.block_align = UNITS * UNIT_BYTES;
1923 return AV_OK;
1924 }
1925
1926 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
1927 int *got_packet_ptr) {
1928 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
1929 memset(dst, 0, avctx.block_align);
1930 for (int unit = 0; unit < UNITS; unit++) {
1931 uint8_t *u = dst + unit * UNIT_BYTES;
1932 for (int ch = 0; ch < 2; ch++) {
1933 int header = encodeUnit(ch, unit * UNIT_SAMPLES);
1934 u[ch] = (uint8_t)header;
1935 for (int k = 0; k < UNIT_SAMPLES; k++) {
1936 // nibbles were stored in unit_nibbles_ by encodeUnit()
1937 u[4 + k] |= (uint8_t)((unit_nibbles_[k] & 0x0F) << (ch * 4));
1938 }
1939 }
1940 }
1941 return AV_OK;
1942 }
1943 int unit_nibbles_[UNIT_SAMPLES];
1944
1945 // decoder's predictor for selector p
1946 static int predict(int p, int s1, int s2) {
1947 switch (p) {
1948 case 1:
1949 return s1 * 0x3c;
1950 case 2:
1951 return s1 * 0x73 - s2 * 0x34;
1952 case 3:
1953 return s1 * 0x62 - s2 * 0x37;
1954 default:
1955 return 0;
1956 }
1957 }
1958
1959 // Encodes one unit of one channel, leaves its nibbles in unit_nibbles_,
1960 // commits the state and returns the header byte.
1961 int encodeUnit(int ch, int start) {
1962 ADPCMChannelStatus *cs = &s->status[ch];
1964 int best_header = 0;
1965 int best_nib[UNIT_SAMPLES];
1966 int best_s1 = cs->sample1, best_s2 = cs->sample2;
1967 for (int p = 0; p < 4; p++) {
1968 for (int sh = 0; sh <= 12; sh++) {
1969 int s1 = cs->sample1, s2 = cs->sample2;
1970 int nib[UNIT_SAMPLES];
1971 int64_t err = 0;
1972 for (int k = 0; k < UNIT_SAMPLES; k++) {
1973 int target = samples[(start + k) * 2 + ch];
1974 int prev = av_clip_intp2((predict(p, s1, s2) + 0x20) >> 6, 21);
1975 int best_n = 0, best_e = 0x7fffffff, best_sd = 0;
1976 for (int n = -8; n <= 7; n++) {
1977 int sampledat = ((n * (1 << 12)) >> sh) * (1 << 6) + prev;
1978 int out = av_clip_int16(sampledat >> 6);
1979 int e = out > target ? out - target : target - out;
1980 if (e < best_e) {
1981 best_e = e;
1982 best_n = n;
1984 }
1985 }
1986 nib[k] = best_n;
1987 err += (int64_t)best_e * best_e;
1988 s2 = s1;
1989 s1 = best_sd;
1990 }
1991 if (err < best_err) {
1992 best_err = err;
1993 best_header = (p << 4) | sh;
1994 memcpy(best_nib, nib, sizeof(nib));
1995 best_s1 = s1;
1996 best_s2 = s2;
1997 }
1998 }
1999 }
2000 for (int k = 0; k < UNIT_SAMPLES; k++) unit_nibbles_[k] = best_nib[k];
2001 cs->sample1 = best_s1;
2002 cs->sample2 = best_s2;
2003 return best_header;
2004 }
2005};
2006
2013 public:
2014 static const int FRAME_SIZE = 1024; // samples per channel
2015
2017 setCodecID(AV_CODEC_ID_ADPCM_ZORK);
2018 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2019 }
2020
2021
2022
2023 protected:
2024
2025 int adpcm_encode_init_impl() {
2026 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
2027 memset(&s->status, 0, sizeof(s->status));
2028 avctx.frame_size = FRAME_SIZE;
2029 avctx.block_align = FRAME_SIZE * channels();
2030 return AV_OK;
2031 }
2032
2033 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
2034 int *got_packet_ptr) {
2035 const int nch = channels();
2036 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
2037 for (int t = 0; t < FRAME_SIZE * nch; t++) {
2038 dst[t] = quantize(t % nch, samples[t]);
2039 }
2040 return AV_OK;
2041 }
2042 // mirrors DecoderADPCM_ZORK::adpcm_zork_expand_nibble without touching the
2043 // state; returns the reconstructed sample and the step index it leads to
2044 static int zork_reconstruct(int predictor, int step_index, uint8_t nibble,
2045 int *next_index) {
2046 uint32_t lookup_sample = ff_adpcm_step_table[step_index];
2047 int32_t sample = 0;
2048 if (nibble & 0x40) sample += lookup_sample;
2049 if (nibble & 0x20) sample += lookup_sample >> 1;
2050 if (nibble & 0x10) sample += lookup_sample >> 2;
2051 if (nibble & 0x08) sample += lookup_sample >> 3;
2052 if (nibble & 0x04) sample += lookup_sample >> 4;
2053 if (nibble & 0x02) sample += lookup_sample >> 5;
2054 if (nibble & 0x01) sample += lookup_sample >> 6;
2055 if (nibble & 0x80) sample = -sample;
2056 sample += predictor;
2057 *next_index = av_clip(step_index + zork_index_table[(nibble >> 4) & 7], 0, 88);
2058 return av_clip_int16(sample);
2059 }
2060
2061 uint8_t quantize(int ch, int16_t target) {
2062 ADPCMChannelStatus *cs = &s->status[ch];
2063 int best = 0, best_e = 0x7fffffff, best_pred = 0, best_index = 0;
2064 for (int b = 0; b < 256; b++) {
2065 int next_index = 0;
2066 int pred = zork_reconstruct(cs->predictor, cs->step_index, (uint8_t)b,
2067 &next_index);
2068 int e = pred > target ? pred - target : target - pred;
2069 if (e < best_e) {
2070 best_e = e;
2071 best = b;
2072 best_pred = pred;
2074 }
2075 }
2076 cs->predictor = best_pred;
2077 cs->step_index = best_index;
2078 return (uint8_t)best;
2079 }
2080};
2081
2089 public:
2090 static const int FRAME_SIZE = 1024; // samples per channel
2091 static const int BLOCK = 32;
2092 static const int BLOCK_BYTES = 21;
2093
2095 setCodecID(AV_CODEC_ID_ADPCM_XMD);
2096 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2097 }
2098
2099
2100
2101 protected:
2102
2103 int adpcm_encode_init_impl() {
2104 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
2105 avctx.frame_size = FRAME_SIZE;
2106 avctx.block_align = (FRAME_SIZE / BLOCK) * BLOCK_BYTES * channels();
2107 return AV_OK;
2108 }
2109
2110 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
2111 int *got_packet_ptr) {
2112 const int nch = channels();
2113 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
2114 uint8_t *out = dst;
2115 for (int block = 0; block < FRAME_SIZE / BLOCK; block++) {
2116 for (int ch = 0; ch < nch; ch++) {
2117 encodeBlock(out, ch, block * BLOCK, nch);
2118 out += BLOCK_BYTES;
2119 }
2120 }
2121 return AV_OK;
2122 }
2123 // x(k) returns the k-th sample of this block for channel ch
2124 void encodeBlock(uint8_t *out, int ch, int start, int nch) {
2125 int x[BLOCK];
2126 for (int k = 0; k < BLOCK; k++) x[k] = samples[(start + k) * nch + ch];
2127
2128 // open-loop residual estimate sets the scale
2129 int maxr = 1;
2130 for (int k = 2; k < BLOCK; k++) {
2131 int pred = (x[k - 1] * 3667 - x[k - 2] * 1642) >> 11;
2132 int r = x[k] - pred;
2133 if (r < 0) r = -r;
2134 if (r > maxr) maxr = r;
2135 }
2136 int scale = (maxr + 6) / 7;
2137 if (scale < 1) scale = 1;
2138 if (scale > 0xFFFF) scale = 0xFFFF;
2139
2140 // header: raw history (decoder takes history[1] = x0, history[0] = x1)
2141 int16_t h1 = (int16_t)x[0], h0 = (int16_t)x[1];
2142 out[0] = (uint16_t)h1 & 0xFF;
2143 out[1] = ((uint16_t)h1 >> 8) & 0xFF;
2144 out[2] = (uint16_t)h0 & 0xFF;
2145 out[3] = ((uint16_t)h0 >> 8) & 0xFF;
2146 out[4] = scale & 0xFF;
2147 out[5] = (scale >> 8) & 0xFF;
2148
2149 uint8_t *nib = out + 6;
2150 memset(nib, 0, 15);
2151 for (int n = 0; n < 15; n++) {
2152 int nibbles[2];
2153 for (int j = 0; j < 2; j++) {
2154 int k = 2 + n * 2 + j;
2155 int pred = (h0 * 3667 - h1 * 1642) >> 11;
2156 int q = (x[k] - pred) / scale;
2157 if ((x[k] - pred) % scale * 2 >= scale) q += (x[k] >= pred) ? 1 : -1;
2158 if (q < -8) q = -8;
2159 if (q > 7) q = 7;
2160 nibbles[j] = q & 0x0F;
2161 int16_t decoded = (int16_t)(q * scale + pred);
2162 h1 = h0;
2163 h0 = decoded;
2164 }
2165 nib[n] = (uint8_t)(nibbles[0] | (nibbles[1] << 4));
2166 }
2167 }
2168};
2169
2176 public:
2177 static const int FRAME_SIZE = 1024; // samples per channel
2178
2180 setCodecID(AV_CODEC_ID_ADPCM_IMA_CUNNING);
2181 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2182 }
2183
2184
2185
2186 protected:
2187
2188 int adpcm_encode_init_impl() {
2189 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
2190 memset(&s->status, 0, sizeof(s->status));
2191 avctx.frame_size = FRAME_SIZE;
2192 avctx.block_align = (FRAME_SIZE / 2) * channels();
2193 return AV_OK;
2194 }
2195
2196 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
2197 int *got_packet_ptr) {
2198 const int nch = channels();
2199 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
2200 uint8_t *out = dst;
2201 for (int ch = 0; ch < nch; ch++) {
2202 for (int t = 0; t < FRAME_SIZE; t += 2) {
2203 uint8_t lo = quantize(ch, samples[t * nch + ch]);
2204 uint8_t hi = quantize(ch, samples[(t + 1) * nch + ch]);
2205 *out++ = (lo & 0x0F) | ((hi & 0x0F) << 4);
2206 }
2207 }
2208 return AV_OK;
2209 }
2210 // mirrors DecoderADPCM_IMA_CUNNING::adpcm_ima_cunning_expand_nibble
2211 static int cunning_reconstruct(int predictor, int step_index, int nibble,
2212 int *next_index) {
2213 nibble = sign_extend(nibble & 0xF, 4);
2214 const int step = ima_cunning_step_table[step_index];
2215 *next_index = av_clip(step_index + ima_cunning_index_table[abs(nibble)], 0, 60);
2216 return av_clip_int16(predictor + step * nibble);
2217 }
2218
2219 uint8_t quantize(int ch, int16_t target) {
2220 ADPCMChannelStatus *cs = &s->status[ch];
2221 int best = 0, best_e = 0x7fffffff, best_pred = 0, best_index = 0;
2222 for (int v = 0; v < 16; v++) {
2223 int next_index = 0;
2224 int pred = cunning_reconstruct(cs->predictor, cs->step_index, v, &next_index);
2225 int e = pred > target ? pred - target : target - pred;
2226 if (e < best_e) {
2227 best_e = e;
2228 best = v;
2229 best_pred = pred;
2231 }
2232 }
2233 cs->predictor = best_pred;
2234 cs->step_index = best_index;
2235 return (uint8_t)best;
2236 }
2237};
2238
2246 public:
2247 static const int FRAME_SIZE = 1024; // samples per channel
2248
2250 setCodecID(AV_CODEC_ID_ADPCM_IMA_MTF);
2251 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2252 }
2253
2254
2255
2256 protected:
2257
2258 int adpcm_encode_init_impl() {
2259 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
2260 memset(&s->status, 0, sizeof(s->status));
2261 avctx.frame_size = FRAME_SIZE;
2262 avctx.block_align = (FRAME_SIZE / 2) * channels();
2263 return AV_OK;
2264 }
2265
2266 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
2267 int *got_packet_ptr) {
2268 const int nch = channels();
2269 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
2270 uint8_t *out = dst;
2271 for (int t = 0; t < FRAME_SIZE; t += 2) {
2272 for (int ch = 0; ch < nch; ch++) {
2273 uint8_t hi = quantize(ch, samples[t * nch + ch]);
2274 uint8_t lo = quantize(ch, samples[(t + 1) * nch + ch]);
2275 *out++ = (uint8_t)((hi << 4) | lo);
2276 }
2277 }
2278 return AV_OK;
2279 }
2280 // mirrors DecoderADPCM_IMA_MTF::adpcm_ima_mtf_expand_nibble
2281 static int mtf_reconstruct(int predictor, int step_index, int nibble,
2282 int *next_index) {
2283 const int step = ff_adpcm_step_table[step_index];
2284 const int delta = step * (2 * nibble - 15);
2285 *next_index = av_clip(step_index + mtf_index_table[nibble], 0, 88);
2286 return av_clip_int16((predictor + delta) >> 4);
2287 }
2288
2289 uint8_t quantize(int ch, int16_t target) {
2290 ADPCMChannelStatus *cs = &s->status[ch];
2291 int best = 0, best_e = 0x7fffffff, best_pred = 0, best_index = 0;
2292 for (int v = 0; v < 16; v++) {
2293 int next_index = 0;
2294 int pred = mtf_reconstruct(cs->predictor, cs->step_index, v, &next_index);
2295 int e = pred > target ? pred - target : target - pred;
2296 if (e < best_e) {
2297 best_e = e;
2298 best = v;
2299 best_pred = pred;
2301 }
2302 }
2303 cs->predictor = best_pred;
2304 cs->step_index = best_index;
2305 return (uint8_t)best;
2306 }
2307};
2308
2321 public:
2322 explicit EncoderADPCM_SBPRO_X(AVCodecID id) {
2323 setCodecID(id);
2324 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2325 }
2326
2327
2328 int packetSize(const AVFrame *frame) {
2329 return started() ? avctx.block_align : firstPacketBytes();
2330 }
2331
2332
2333 protected:
2334
2335 int adpcm_encode_init_impl() {
2336 const int ch = channels();
2337 if (!(ch == 1 || (ch == 2 && codecID() != AV_CODEC_ID_ADPCM_SBPRO_3)))
2338 return AVERROR(AVERROR_INVALID);
2339 frame_ = frameSizeFor(codecID());
2340 if ((frame_ * ch) % samplesPerByte() != 0)
2341 return AVERROR(AVERROR_INVALID);
2342 memset(&s->status, 0, sizeof(s->status));
2343 carry_ = 0;
2344 avctx.frame_size = frame_;
2345 avctx.block_align = frame_ * ch / samplesPerByte();
2346 return AV_OK;
2347 }
2348
2349 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
2350 int *got_packet_ptr) {
2351 if (frame->nb_samples != frame_) return AVERROR(AVERROR_INVALID);
2352 const int ch = channels();
2353 const int spb = samplesPerByte();
2354 const bool first = !started();
2355 // samples per channel taken from the work buffer (carry + frame)
2356 const int emit = first ? firstPacketSamples() : frame_;
2357 uint8_t *out = dst;
2358 int t0 = 0;
2359 if (first) {
2360 for (int c = 0; c < ch; c++) {
2361 int b = ((sampleAt(c, 0) + 64) >> 7) + 0x80;
2362 *out++ = (uint8_t)(b < 0 ? 0 : b > 255 ? 255 : b);
2363 s->status[c].predictor = 0;
2364 s->status[c].step = 0;
2365 }
2366 s->status[0].step_index = 1; // the decoder's "started" flag
2367 t0 = 1;
2368 }
2369 // packed body: values are interleaved (time major), value v of the packet
2370 // goes to channel v % ch and to bit field v % spb of its byte
2371 int v = 0;
2372 unsigned acc = 0;
2373 for (int t = t0; t < emit; t++) {
2374 for (int c = 0; c < ch; c++, v++) {
2375 const int pos = v % spb;
2376 const int size = valueSize(pos);
2377 uint8_t nib = quantize(c, sampleAt(c, t), size, shift());
2378 acc = (acc << size) | nib;
2379 if (pos == spb - 1) {
2380 *out++ = (uint8_t)acc;
2381 acc = 0;
2382 }
2383 }
2384 }
2385 assert(out - dst == pkt_size);
2386
2387 // keep the samples that did not fit for the next packet
2388 const int carry_new = carry_ + frame_ - emit;
2389 assert(carry_new >= 0 && carry_new <= 3);
2390 int16_t next[2][3];
2391 for (int c = 0; c < ch; c++)
2392 for (int j = 0; j < carry_new; j++) next[c][j] = sampleAt(c, emit + j);
2393 for (int c = 0; c < ch; c++)
2394 for (int j = 0; j < carry_new; j++) carry_buf_[c][j] = next[c][j];
2395 carry_ = carry_new;
2396 return AV_OK;
2397 }
2398 int frame_ = 0; // samples per channel in a full packet
2399 int carry_ = 0; // samples per channel kept from the previous frame
2400 int16_t carry_buf_[2][3] = {{0}};
2401
2402 static int frameSizeFor(AVCodecID id) {
2403 return id == AV_CODEC_ID_ADPCM_SBPRO_3 ? 1023 : 1024;
2404 }
2405
2406 int samplesPerByte() {
2407 switch (codecID()) {
2408 case AV_CODEC_ID_ADPCM_SBPRO_2:
2409 return 4;
2410 case AV_CODEC_ID_ADPCM_SBPRO_3:
2411 return 3;
2412 default:
2413 return 2;
2414 }
2415 }
2416
2417 // bits of the value at position pos within a byte
2418 int valueSize(int pos) {
2419 switch (codecID()) {
2420 case AV_CODEC_ID_ADPCM_SBPRO_2:
2421 return 2;
2422 case AV_CODEC_ID_ADPCM_SBPRO_3:
2423 return pos < 2 ? 3 : 2;
2424 default:
2425 return 4;
2426 }
2427 }
2428
2429 // mirrors the decoder: SBPRO_2 uses shift 2, the others shift 0
2430 int shift() { return codecID() == AV_CODEC_ID_ADPCM_SBPRO_2 ? 2 : 0; }
2431
2432 // the decoder's "started" flag is status[0].step_index
2433 bool started() { return s->status[0].step_index != 0; }
2434
2435 // samples per channel in the first packet, including the raw first sample:
2436 // the decoder gives 1 + (bytes - ch) * spb / ch, so the body count must make
2437 // the body an exact number of bytes
2438 int firstPacketSamples() {
2439 const int ch = channels();
2440 const int spb = samplesPerByte();
2441 const int step = spb / ((ch == 2 && spb % 2 == 0) ? 2 : 1);
2442 const int body = ((frame_ - 1) / step) * step;
2443 return body + 1;
2444 }
2445
2446 int firstPacketBytes() {
2447 const int ch = channels();
2448 return ch + (firstPacketSamples() - 1) * ch / samplesPerByte();
2449 }
2450
2451 // work buffer: carried samples first, then the interleaved input frame
2452 int16_t sampleAt(int c, int t) {
2453 if (t < carry_) return carry_buf_[c][t];
2454 return samples[(t - carry_) * channels() + c];
2455 }
2456
2457 // mirrors DecoderADPCM_SBPRO_X::adpcm_sbpro_expand_nibble
2458 static void reconstruct(int predictor, int step, int nibble, int size,
2459 int shift, int *next_pred, int *next_step) {
2460 const int sign = nibble & (1 << (size - 1));
2461 const int delta = nibble & ((1 << (size - 1)) - 1);
2462 const int diff = delta << (7 + step + shift);
2463 *next_pred = av_clip(predictor + (sign ? -diff : diff), -16384, 16256);
2464 *next_step = step;
2465 if (delta >= 2 * size - 3 && step < 3)
2466 *next_step = step + 1;
2467 else if (delta == 0 && step > 0)
2468 *next_step = step - 1;
2469 }
2470
2471 // exhaustive search over all nibbles of the given size
2472 uint8_t quantize(int ch, int target, int size, int shift) {
2473 ADPCMChannelStatus *cs = &s->status[ch];
2474 int best = 0, best_e = 0x7fffffff, best_pred = 0, best_step = 0;
2475 for (int nib = 0; nib < (1 << size); nib++) {
2476 int pred = 0, step = 0;
2477 reconstruct(cs->predictor, cs->step, nib, size, shift, &pred, &step);
2478 int e = pred > target ? pred - target : target - pred;
2479 if (e < best_e) {
2480 best_e = e;
2481 best = nib;
2482 best_pred = pred;
2483 best_step = step;
2484 }
2485 }
2486 cs->predictor = best_pred;
2487 cs->step = best_step;
2488 return (uint8_t)best;
2489 }
2490};
2491
2503 public:
2504 static const int FRAME_SIZE = 224; // samples per channel
2505 static const int BLOCK = 128;
2506
2507 EncoderADPCM_XA() {
2508 setCodecID(AV_CODEC_ID_ADPCM_XA);
2509 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
2510 }
2511
2512
2513
2514 protected:
2515
2516 int adpcm_encode_init_impl() {
2517 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
2518 memset(&s->status, 0, sizeof(s->status));
2519 avctx.frame_size = FRAME_SIZE;
2520 avctx.block_align = BLOCK * channels();
2521 return AV_OK;
2522 }
2523
2524 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
2525 int *got_packet_ptr) {
2526 const int nch = channels();
2527 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
2528 memset(dst, 0, avctx.block_align);
2529
2530 int x[28];
2531 uint8_t nl[28], nr[28];
2532 for (int b = 0; b < nch; b++) { // one block per channel
2533 uint8_t *blk = dst + b * BLOCK;
2534 if (nch == 1) {
2535 for (int i = 0; i < 4; i++) {
2536 const int base = i * 56;
2537 for (int j = 0; j < 28; j++) x[j] = samples[base + j];
2538 blk[4 + 2 * i] = encodeSegment(0, x, nl);
2539 for (int j = 0; j < 28; j++) x[j] = samples[base + 28 + j];
2540 blk[5 + 2 * i] = encodeSegment(0, x, nr);
2541 for (int j = 0; j < 28; j++) {
2542 // first segment in the low nibbles, second in the high nibbles
2543 blk[16 + i + 4 * j] = (uint8_t)((nl[j] & 0x0F) | ((nr[j] & 0x0F) << 4));
2544 }
2545 }
2546 } else {
2547 for (int i = 0; i < 4; i++) {
2548 const int base = b * 112 + i * 28;
2549 int ctl[2];
2550 for (int ch = 0; ch < 2; ch++) {
2551 for (int j = 0; j < 28; j++) x[j] = samples[(base + j) * 2 + ch];
2552 ctl[ch] = encodeSegment(ch, x, ch == 0 ? nl : nr);
2553 blk[4 + 2 * i + ch] = (uint8_t)ctl[ch];
2554 }
2555 for (int j = 0; j < 28; j++) {
2556 blk[16 + i + 4 * j] = (uint8_t)((nl[j] & 0x0F) | ((nr[j] & 0x0F) << 4));
2557 }
2558 }
2559 }
2560 }
2561 return AV_OK;
2562 }
2563 // Searches filter and shift for one 28 sample segment on channel ch, writes
2564 // the nibbles and commits the history. Returns the header byte.
2565 int encodeSegment(int ch, const int *x, uint8_t *nib) {
2566 ADPCMChannelStatus *cs = &s->status[ch];
2568 int best_h = 0;
2569 uint8_t best_nib[28];
2570 int best_s1 = cs->sample1, best_s2 = cs->sample2;
2571
2572 for (int f = 0; f < 5; f++) {
2573 for (int sf = 0; sf <= 12; sf++) {
2574 const int f0 = xa_adpcm_table[f][0];
2575 const int f1 = xa_adpcm_table[f][1];
2576 const int shift = 12 - sf;
2577 int s1 = cs->sample1, s2 = cs->sample2;
2578 int64_t err = 0;
2579 uint8_t nb[28];
2580 for (int j = 0; j < 28; j++) {
2581 const int pred = (s1 * f0 + s2 * f1 + 32) >> 6;
2582 int best_n = 0, best_e = 0x7fffffff, best_v = 0;
2583 for (int n = -8; n <= 7; n++) {
2584 int v = av_clip_int16(n * (1 << shift) + pred);
2585 int e = v > x[j] ? v - x[j] : x[j] - v;
2586 if (e < best_e) {
2587 best_e = e;
2588 best_n = n;
2589 best_v = v;
2590 }
2591 }
2592 nb[j] = (uint8_t)(best_n & 0x0F);
2593 err += (int64_t)best_e * best_e;
2594 s2 = s1;
2595 s1 = best_v;
2596 }
2597 if (err < best_err) {
2598 best_err = err;
2599 best_h = (f << 4) | sf;
2600 memcpy(best_nib, nb, sizeof(nb));
2601 best_s1 = s1;
2602 best_s2 = s2;
2603 }
2604 }
2605 }
2606 memcpy(nib, best_nib, sizeof(best_nib));
2607 cs->sample1 = best_s1;
2608 cs->sample2 = best_s2;
2609 return best_h;
2610 }
2611};
2612
2623 public:
2624 static const int FRAME_SIZE = 1008; // 36 pieces of 28 samples
2625 static const int PIECES = FRAME_SIZE / 28;
2626
2627 EncoderADPCM_EA() {
2628 setCodecID(AV_CODEC_ID_ADPCM_EA);
2629 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2630 }
2631
2632
2633
2634 protected:
2635
2636 int adpcm_encode_init_impl() {
2637 if (channels() != 2) return AVERROR(AVERROR_INVALID);
2638 memset(&s->status, 0, sizeof(s->status));
2639 avctx.frame_size = FRAME_SIZE;
2640 avctx.block_align = 4 + 8 + PIECES * 30 + 2;
2641 return AV_OK;
2642 }
2643
2644 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
2645 int *got_packet_ptr) {
2646 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
2647 memset(dst, 0, avctx.block_align);
2648 // count (le32), then the running state of both channels
2649 dst[0] = FRAME_SIZE & 0xFF;
2650 dst[1] = (FRAME_SIZE >> 8) & 0xFF;
2651 for (int ch = 0; ch < 2; ch++) {
2652 put16(dst + 4 + 4 * ch, s->status[ch].predictor);
2653 put16(dst + 6 + 4 * ch, s->status[ch].prev_sample);
2654 }
2655
2656 uint8_t *piece = dst + 12;
2657 for (int p = 0; p < PIECES; p++, piece += 30) {
2658 int header[2];
2659 uint8_t nibs[2][28];
2660 for (int ch = 0; ch < 2; ch++) {
2661 int x[28];
2662 for (int k = 0; k < 28; k++) x[k] = samples[(p * 28 + k) * 2 + ch];
2663 header[ch] = encodeChannel(ch, x, nibs[ch]);
2664 }
2665 piece[0] = (uint8_t)(((header[0] >> 4) << 4) | (header[1] >> 4));
2666 // the decoder computes shift = 20 - nibble, so the nibble is the search
2667 // value sf itself
2668 piece[1] = (uint8_t)(((header[0] & 0x0F) << 4) | (header[1] & 0x0F));
2669 for (int k = 0; k < 28; k++) {
2670 piece[2 + k] = (uint8_t)(((nibs[0][k] & 0x0F) << 4) |
2671 (nibs[1][k] & 0x0F));
2672 }
2673 }
2674 // terminator stays zero from the memset
2675 return AV_OK;
2676 }
2677 static void put16(uint8_t *p, int v) {
2678 p[0] = v & 0xFF;
2679 p[1] = ((uint16_t)v >> 8) & 0xFF;
2680 }
2681
2682 // Returns the coefficient index (high nibble) and shift nibble (low nibble)
2683 // as a header value in the form (index << 4) | shift_nibble; the caller
2684 // packs them. Nibbles are stored in nib; the running state is committed.
2685 int encodeChannel(int ch, const int *x, uint8_t *nib) {
2686 ADPCMChannelStatus *cs = &s->status[ch];
2688 int best_h = 0;
2689 uint8_t best_nib[28];
2690 int best_cur = cs->predictor, best_prev = cs->prev_sample;
2691
2692 for (int idx = 0; idx < 16; idx++) {
2693 for (int sf = 0; sf < 16; sf++) {
2694 const int c1 = ea_adpcm_table[idx];
2695 const int c2 = ea_adpcm_table[idx + 4];
2696 const int shift = 20 - sf;
2697 int cu = cs->predictor, pv = cs->prev_sample;
2698 int64_t err = 0;
2699 uint8_t nb[28];
2700 for (int k = 0; k < 28; k++) {
2701 int best_n = 0, best_e = 0x7fffffff, best_out = 0;
2702 for (int n = -8; n <= 7; n++) {
2703 int v = av_clip_int16(((n * (1 << shift)) + cu * c1 + pv * c2 + 0x80) >> 8);
2704 int e = v > x[k] ? v - x[k] : x[k] - v;
2705 if (e < best_e) {
2706 best_e = e;
2707 best_n = n;
2708 best_out = v;
2709 }
2710 }
2711 nb[k] = (uint8_t)(best_n & 0x0F);
2712 err += (int64_t)best_e * best_e;
2713 pv = cu;
2714 cu = best_out;
2715 }
2716 if (err < best_err) {
2717 best_err = err;
2718 best_h = (idx << 4) | sf;
2719 memcpy(best_nib, nb, sizeof(nb));
2720 best_cur = cu;
2721 best_prev = pv;
2722 }
2723 }
2724 }
2725 memcpy(nib, best_nib, sizeof(best_nib));
2726 cs->predictor = best_cur;
2727 cs->prev_sample = best_prev;
2728 return best_h;
2729 }
2730};
2731
2743 public:
2744 static const int FRAME_SIZE = 1008; // 36 blocks of 28 samples
2745 static const int BLOCKS = FRAME_SIZE / 28;
2746 static const int BLOCK_BYTES = 15;
2747
2748 EncoderADPCM_EA_RX(AVCodecID id) : variant_(id) {
2749 setCodecID(id);
2750 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
2751 }
2752
2753
2754
2755 protected:
2756
2757 int adpcm_encode_init_impl() {
2758 if (channels() < 1 || channels() > 6) return AVERROR(AVERROR_INVALID);
2759 memset(&s->status, 0, sizeof(s->status));
2760 avctx.frame_size = FRAME_SIZE;
2761 avctx.block_align = headerSize() + channels() * channelBytes();
2762 return AV_OK;
2763 }
2764
2765 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
2766 int *got_packet_ptr) {
2767 const int nch = channels();
2768 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
2769 memset(dst, 0, avctx.block_align);
2770 const bool be = variant_ == AV_CODEC_ID_ADPCM_EA_R3;
2771 put32(dst, FRAME_SIZE, be);
2772
2773 int pos = headerSize();
2774 for (int ch = 0; ch < nch; ch++) {
2775 put32(dst + 4 + 4 * ch, pos - headerSize(), be);
2776 uint8_t *d = dst + pos;
2777 ADPCMChannelStatus *cs = &s->status[ch];
2778 int cur = 0, prev = 0;
2779 if (variant_ == AV_CODEC_ID_ADPCM_EA_R1) {
2780 d[0] = 0;
2781 d[1] = 0;
2782 d[2] = 0;
2783 d[3] = 0;
2784 d += 4;
2785 } else {
2786 cur = cs->predictor;
2787 prev = cs->prev_sample;
2788 }
2789 int x[28];
2790 for (int b = 0; b < BLOCKS; b++) {
2791 for (int k = 0; k < 28; k++) x[k] = samples[(b * 28 + k) * nch + ch];
2792 encodeBlock(x, &cur, &prev, d + b * BLOCK_BYTES);
2793 }
2794 if (variant_ != AV_CODEC_ID_ADPCM_EA_R1) {
2795 cs->predictor = cur;
2796 cs->prev_sample = prev;
2797 }
2798 pos += channelBytes();
2799 }
2800 return AV_OK;
2801 }
2802 AVCodecID variant_;
2803
2804 int headerSize() { return 4 + 4 * channels(); }
2805
2806 int channelBytes() {
2807 return BLOCKS * BLOCK_BYTES +
2808 (variant_ == AV_CODEC_ID_ADPCM_EA_R1 ? 4 : 0);
2809 }
2810
2811 static void put32(uint8_t *p, uint32_t v, bool be) {
2812 for (int i = 0; i < 4; i++) {
2813 int shift = be ? (3 - i) * 8 : i * 8;
2814 p[i] = (v >> shift) & 0xFF;
2815 }
2816 }
2817
2818 // mirrors DecoderADPCM_EA_RX: picks the header and nibbles for 28 samples
2819 void encodeBlock(const int *x, int *cur, int *prev, uint8_t *out) {
2821 int best_header = 0;
2822 uint8_t best_bytes[14];
2823 int best_cur = *cur, best_prev = *prev;
2824
2825 for (int h = 0; h < 256; h++) {
2826 if (h == 0xEE) continue;
2827 const int c1 = ea_adpcm_table[h >> 4];
2828 const int c2 = ea_adpcm_table[(h >> 4) + 4];
2829 const int shift = 20 - (h & 0x0F);
2830 int cu = *cur, pv = *prev;
2831 int64_t err = 0;
2832 int nib[28];
2833 for (int k = 0; k < 28; k++) {
2834 int best_n = 0, best_e = 0x7fffffff, best_out = 0;
2835 for (int n = -8; n <= 7; n++) {
2836 int v = av_clip_int16(((n * (1 << shift)) + cu * c1 + pv * c2) >> 8);
2837 int e = v > x[k] ? v - x[k] : x[k] - v;
2838 if (e < best_e) {
2839 best_e = e;
2840 best_n = n;
2841 best_out = v;
2842 }
2843 }
2844 nib[k] = best_n;
2845 err += (int64_t)best_e * best_e;
2846 pv = cu;
2847 cu = best_out;
2848 }
2849 if (err < best_err) {
2850 best_err = err;
2851 best_header = h;
2852 for (int k = 0; k < 14; k++) {
2853 best_bytes[k] = (uint8_t)(((nib[2 * k] & 0x0F) << 4) |
2854 (nib[2 * k + 1] & 0x0F));
2855 }
2856 best_cur = cu;
2857 best_prev = pv;
2858 }
2859 }
2860 out[0] = (uint8_t)best_header;
2861 memcpy(out + 1, best_bytes, 14);
2862 *cur = best_cur;
2863 *prev = best_prev;
2864 }
2865};
2866
2876 public:
2877 static const int FRAME_SIZE = 128; // samples per channel
2878 static const int GROUP = 32;
2879 static const int CHUNK = 76;
2880
2882 setCodecID(AV_CODEC_ID_ADPCM_EA_XAS);
2883 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2884 }
2885
2886
2887
2888 protected:
2889
2890 int adpcm_encode_init_impl() {
2891 if (channels() < 1 || channels() > 6) return AVERROR(AVERROR_INVALID);
2892 avctx.frame_size = FRAME_SIZE;
2893 avctx.block_align = CHUNK * channels();
2894 return AV_OK;
2895 }
2896
2897 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
2898 int *got_packet_ptr) {
2899 const int nch = channels();
2900 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
2901 memset(dst, 0, avctx.block_align);
2902 for (int ch = 0; ch < nch; ch++) {
2903 int x[FRAME_SIZE];
2904 for (int t = 0; t < FRAME_SIZE; t++) x[t] = samples[t * nch + ch];
2905 encodeChunk(x, dst + ch * CHUNK);
2906 }
2907 return AV_OK;
2908 }
2909 void encodeChunk(const int *x, uint8_t *out) {
2910 uint8_t data[4][15];
2911 for (int g = 0; g < 4; g++) {
2912 int w0, w1;
2913 encodeGroup(x + g * GROUP, &w0, &w1, data[g]);
2914 out[g * 4 + 0] = w0 & 0xFF;
2915 out[g * 4 + 1] = (w0 >> 8) & 0xFF;
2916 out[g * 4 + 2] = w1 & 0xFF;
2917 out[g * 4 + 3] = (w1 >> 8) & 0xFF;
2918 }
2919 uint8_t *d = out + 16;
2920 for (int k = 0; k < 15; k++) {
2921 for (int g = 0; g < 4; g++) d[k * 4 + g] = data[g][k];
2922 }
2923 }
2924
2925 // mirrors DecoderADPCM_EA_XAS::decode_frame_impl for one group of 32 samples
2926 static void encodeGroup(const int *x, int *w0, int *w1, uint8_t *bytes) {
2927 const int raw0 = x[0] & ~0x0F;
2928 const int raw1 = x[1] & ~0x0F;
2930 int best_idx = 0, best_sf = 0;
2931 uint8_t best_bytes[15];
2932
2933 for (int idx = 0; idx < 16; idx++) {
2934 for (int sf = 0; sf < 16; sf++) {
2935 const int c0 = ea_adpcm_table[idx];
2936 const int c1 = ea_adpcm_table[idx + 4];
2937 const int shift = 20 - sf;
2938 int h1 = raw0; // s[-2]
2939 int h0 = raw1; // s[-1]
2940 int64_t err = 0;
2941 uint8_t nb[15];
2942 for (int k = 0; k < 15; k++) {
2943 const int m = 2 + 2 * k;
2944 const int pred0 = h0 * c0 + h1 * c1 + 0x80;
2945 int n0 = 0, e0 = 0x7fffffff, out0 = 0;
2946 for (int n = -8; n <= 7; n++) {
2947 int v = av_clip_int16((n * (1 << shift) + pred0) >> 8);
2948 int e = v > x[m] ? v - x[m] : x[m] - v;
2949 if (e < e0) {
2950 e0 = e;
2951 n0 = n;
2952 out0 = v;
2953 }
2954 }
2955 const int pred1 = out0 * c0 + h0 * c1 + 0x80;
2956 int n1 = 0, e1 = 0x7fffffff, out1 = 0;
2957 for (int n = -8; n <= 7; n++) {
2958 int v = av_clip_int16((n * (1 << shift) + pred1) >> 8);
2959 int e = v > x[m + 1] ? v - x[m + 1] : x[m + 1] - v;
2960 if (e < e1) {
2961 e1 = e;
2962 n1 = n;
2963 out1 = v;
2964 }
2965 }
2966 err += (int64_t)e0 * e0 + (int64_t)e1 * e1;
2967 nb[k] = (uint8_t)(((n0 & 0x0F) << 4) | (n1 & 0x0F));
2968 h1 = out0;
2969 h0 = out1;
2970 }
2971 if (err < best_err) {
2972 best_err = err;
2973 best_idx = idx;
2974 best_sf = sf;
2975 memcpy(best_bytes, nb, sizeof(nb));
2976 }
2977 }
2978 }
2979 *w0 = (raw0 & 0xFFFF) | best_idx;
2980 *w1 = (raw1 & 0xFFFF) | best_sf;
2981 memcpy(bytes, best_bytes, sizeof(best_bytes));
2982 }
2983};
2984
2994 public:
2995 static const int FRAME_SIZE = 1024; // samples per channel
2996
2998 setCodecID(AV_CODEC_ID_ADPCM_EA_MAXIS_XA);
2999 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3000 }
3001
3002
3003
3004 protected:
3005
3006 int adpcm_encode_init_impl() {
3007 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
3008 memset(&s->status, 0, sizeof(s->status));
3009 avctx.frame_size = FRAME_SIZE;
3010 avctx.block_align = channels() * (1 + FRAME_SIZE / 2);
3011 return AV_OK;
3012 }
3013
3014 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
3015 int *got_packet_ptr) {
3016 const int nch = channels();
3017 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
3018 memset(dst, 0, avctx.block_align);
3019
3020 int nibbles[2][FRAME_SIZE];
3021 for (int ch = 0; ch < nch; ch++) {
3022 dst[ch] = (uint8_t)encodeChannel(ch, nibbles[ch]);
3023 }
3024 uint8_t *out = dst + nch;
3025 for (int k = 0; k < FRAME_SIZE / 2; k++) {
3026 for (int ch = 0; ch < nch; ch++) {
3027 *out++ = (uint8_t)(((nibbles[ch][2 * k] & 0x0F) << 4) |
3028 (nibbles[ch][2 * k + 1] & 0x0F));
3029 }
3030 }
3031 return AV_OK;
3032 }
3033 // best header for one channel; stores its nibbles and commits the history
3034 int encodeChannel(int ch, int *nibbles) {
3035 ADPCMChannelStatus *cs = &s->status[ch];
3037 int best_header = 0;
3038 int best_nib[FRAME_SIZE];
3039 int best_s1 = cs->sample1, best_s2 = cs->sample2;
3040
3041 for (int header = 0; header < 256; header++) {
3042 int c0 = ea_adpcm_table[(header >> 4)];
3043 int c1 = ea_adpcm_table[(header >> 4) + 4];
3044 int shift = 20 - (header & 0x0F);
3045 int s1 = cs->sample1, s2 = cs->sample2;
3046 int64_t err = 0;
3047 int nib[FRAME_SIZE];
3048 for (int t = 0; t < FRAME_SIZE; t++) {
3049 int target = samples[t * channels() + ch];
3050 int best_n = 0, best_e = 0x7fffffff, best_out = 0;
3051 for (int n = -8; n <= 7; n++) {
3052 int level = n * (1 << shift);
3053 int out = av_clip_int16((level + s1 * c0 + s2 * c1 + 0x80) >> 8);
3054 int e = out > target ? out - target : target - out;
3055 if (e < best_e) {
3056 best_e = e;
3057 best_n = n;
3058 best_out = out;
3059 }
3060 }
3061 nib[t] = best_n;
3062 err += (int64_t)best_e * best_e;
3063 s2 = s1;
3064 s1 = best_out;
3065 }
3066 if (err < best_err) {
3067 best_err = err;
3069 memcpy(best_nib, nib, sizeof(nib));
3070 best_s1 = s1;
3071 best_s2 = s2;
3072 }
3073 }
3074 memcpy(nibbles, best_nib, sizeof(best_nib));
3075 cs->sample1 = best_s1;
3076 cs->sample2 = best_s2;
3077 return best_header;
3078 }
3079};
3080
3088 public:
3089 static const int FRAME_SIZE = 1024; // samples per channel
3090
3092 setCodecID(AV_CODEC_ID_ADPCM_4XM);
3093 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3094 }
3095
3096
3097
3098 protected:
3099
3100 int adpcm_encode_init_impl() {
3101 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
3102 memset(&s->status, 0, sizeof(s->status));
3103 avctx.frame_size = FRAME_SIZE;
3104 avctx.block_align = channels() * (4 + FRAME_SIZE / 2);
3105 return AV_OK;
3106 }
3107
3108 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
3109 int *got_packet_ptr) {
3110 const int nch = channels();
3111 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
3112 memset(dst, 0, avctx.block_align);
3113
3114 for (int ch = 0; ch < nch; ch++) {
3115 int16_t first = samples[ch];
3116 s->status[ch].predictor = first;
3117 s->status[ch].step_index = 0;
3118 dst[ch * 2] = first & 0xFF;
3119 dst[ch * 2 + 1] = ((uint16_t)first >> 8) & 0xFF;
3120 // steps are already zero
3121 }
3122
3123 uint8_t *out = dst + 4 * nch;
3124 for (int ch = 0; ch < nch; ch++) {
3125 for (int t = 0; t < FRAME_SIZE; t += 2) {
3126 uint8_t lo = quantize(ch, samples[t * nch + ch]);
3127 uint8_t hi = quantize(ch, samples[(t + 1) * nch + ch]);
3128 *out++ = lo | (hi << 4);
3129 }
3130 }
3131 return AV_OK;
3132 }
3133 uint8_t quantize(int ch, int16_t sample) {
3134 return adpcm_ima_quantize(&s->status[ch], sample, 4);
3135 }
3136};
3137
3145 public:
3146 static const int FRAME_SIZE = 1024; // samples per channel
3147
3149 setCodecID(AV_CODEC_ID_ADPCM_IMA_EA_EACS);
3150 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3151 }
3152
3153
3154
3155 protected:
3156
3157 int adpcm_encode_init_impl() {
3158 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
3159 memset(&s->status, 0, sizeof(s->status));
3160 avctx.frame_size = FRAME_SIZE;
3161 avctx.block_align = 4 + 8 * channels() + (FRAME_SIZE / 2) * channels();
3162 return AV_OK;
3163 }
3164
3165 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
3166 int *got_packet_ptr) {
3167 const int nch = channels();
3168 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
3169 const int hdr = 4 + 8 * nch;
3170 memset(dst, 0, avctx.block_align);
3171
3172 // sample count, step indices (0) and predictors (first sample)
3173 dst[0] = FRAME_SIZE & 0xFF;
3174 dst[1] = (FRAME_SIZE >> 8) & 0xFF;
3175 dst[2] = 0;
3176 dst[3] = 0;
3177 for (int ch = 0; ch < nch; ch++) {
3178 int32_t first = samples[ch];
3179 s->status[ch].predictor = first;
3180 s->status[ch].step_index = 0;
3181 uint8_t *pred = dst + 4 + 4 * nch + 4 * ch; // predictors follow steps
3182 pred[0] = first & 0xFF;
3183 pred[1] = (first >> 8) & 0xFF;
3184 pred[2] = (first < 0) ? 0xFF : 0;
3185 pred[3] = (first < 0) ? 0xFF : 0;
3186 }
3187
3188 uint8_t *out = dst + hdr;
3189 if (nch == 2) {
3190 for (int t = 0; t < FRAME_SIZE; t++) {
3191 uint8_t left = quantize(0, samples[t * 2]);
3192 uint8_t right = quantize(1, samples[t * 2 + 1]);
3193 *out++ = (left << 4) | right;
3194 }
3195 } else {
3196 for (int t = 0; t < FRAME_SIZE; t += 2) {
3197 uint8_t hi = quantize(0, samples[t]);
3198 uint8_t lo = quantize(0, samples[t + 1]);
3199 *out++ = (hi << 4) | lo;
3200 }
3201 }
3202 return AV_OK;
3203 }
3204 uint8_t quantize(int ch, int16_t sample) {
3205 return adpcm_ima_quantize(&s->status[ch], sample, 3);
3206 }
3207};
3208
3216 public:
3217 static const int FRAME_SIZE = 1024; // samples per channel
3218
3220 setCodecID(AV_CODEC_ID_ADPCM_IMA_APC);
3221 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3222 }
3223
3224
3225
3226 protected:
3227
3228 int adpcm_encode_init_impl() {
3229 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
3230 memset(&s->status, 0, sizeof(s->status));
3231 avctx.frame_size = FRAME_SIZE;
3232 avctx.block_align = (FRAME_SIZE / 2) * channels();
3233 return AV_OK;
3234 }
3235
3236 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
3237 int *got_packet_ptr) {
3238 const int nch = channels();
3239 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
3240 uint8_t *out = dst;
3241 if (nch == 2) {
3242 for (int t = 0; t < FRAME_SIZE; t++) {
3243 uint8_t left = quantize(0, samples[t * 2]);
3244 uint8_t right = quantize(1, samples[t * 2 + 1]);
3245 *out++ = (left << 4) | right;
3246 }
3247 } else {
3248 for (int t = 0; t < FRAME_SIZE; t += 2) {
3249 uint8_t hi = quantize(0, samples[t]);
3250 uint8_t lo = quantize(0, samples[t + 1]);
3251 *out++ = (hi << 4) | lo;
3252 }
3253 }
3254 return AV_OK;
3255 }
3256 uint8_t quantize(int ch, int16_t sample) {
3257 return adpcm_ima_quantize(&s->status[ch], sample, 3);
3258 }
3259};
3260
3268 public:
3269 static const int FRAME_SIZE = 1024; // samples per channel
3270
3272 setCodecID(AV_CODEC_ID_ADPCM_IMA_SMJPEG);
3273 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3274 }
3275
3276
3277
3278 protected:
3279
3280 int adpcm_encode_init_impl() {
3281 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
3282 memset(&s->status, 0, sizeof(s->status));
3283 avctx.frame_size = FRAME_SIZE;
3284 avctx.block_align = channels() * (4 + FRAME_SIZE / 2);
3285 return AV_OK;
3286 }
3287
3288 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
3289 int *got_packet_ptr) {
3290 const int nch = channels();
3291 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
3292 memset(dst, 0, avctx.block_align);
3293
3294 for (int ch = 0; ch < nch; ch++) {
3295 int16_t first = samples[ch];
3296 s->status[ch].predictor = first;
3297 s->status[ch].step_index = 0;
3298 dst[ch * 4 + 0] = (first >> 8) & 0xFF; // predictor (be16)
3299 dst[ch * 4 + 1] = first & 0xFF;
3300 dst[ch * 4 + 2] = 0; // step index
3301 dst[ch * 4 + 3] = 0; // unused
3302 }
3303
3304 uint8_t *out = dst + 4 * nch;
3305 if (nch == 2) {
3306 for (int t = 0; t < FRAME_SIZE; t++) {
3307 uint8_t left = quantize(0, samples[t * 2]);
3308 uint8_t right = quantize(1, samples[t * 2 + 1]);
3309 *out++ = (left << 4) | right;
3310 }
3311 } else {
3312 for (int t = 0; t < FRAME_SIZE; t += 2) {
3313 uint8_t hi = quantize(0, samples[t]);
3314 uint8_t lo = quantize(0, samples[t + 1]);
3315 *out++ = (hi << 4) | lo;
3316 }
3317 }
3318 return AV_OK;
3319 }
3320 // evaluates every nibble with the decoder's QT reconstruction and keeps the
3321 // closest one, then advances the state exactly as the decoder does
3322 uint8_t quantize(int ch, int16_t sample) {
3323 ADPCMChannelStatus *cs = &s->status[ch];
3324 int step = ff_adpcm_step_table[cs->step_index];
3325 int best = 0, best_pred = 0, best_e = 0x7fffffff;
3326 for (int nibble = 0; nibble < 16; nibble++) {
3327 int diff = step >> 3;
3328 if (nibble & 4) diff += step;
3329 if (nibble & 2) diff += step >> 1;
3330 if (nibble & 1) diff += step >> 2;
3331 int pred = av_clip_int16((nibble & 8) ? cs->predictor - diff
3332 : cs->predictor + diff);
3333 int e = pred > sample ? pred - sample : sample - pred;
3334 if (e < best_e) {
3335 best_e = e;
3336 best = nibble;
3337 best_pred = pred;
3338 }
3339 }
3340 cs->predictor = best_pred;
3341 cs->step_index =
3342 av_clip(cs->step_index + ff_adpcm_index_table[best], 0, 88);
3343 return (uint8_t)best;
3344 }
3345};
3346
3354 public:
3355 static const int FRAME_SIZE = 1024; // samples per channel
3356 static const int SUBFRAME = 256;
3357
3359 setCodecID(AV_CODEC_ID_ADPCM_IMA_MOFLEX);
3360 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3361 }
3362
3363
3364
3365 protected:
3366
3367 int adpcm_encode_init_impl() {
3368 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
3369 memset(&s->status, 0, sizeof(s->status));
3370 avctx.frame_size = FRAME_SIZE;
3371 avctx.block_align = channels() * (4 + FRAME_SIZE / 2);
3372 return AV_OK;
3373 }
3374
3375 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
3376 int *got_packet_ptr) {
3377 const int nch = channels();
3378 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
3379 memset(dst, 0, avctx.block_align);
3380
3381 for (int ch = 0; ch < nch; ch++) {
3382 int16_t first = samples[ch];
3383 s->status[ch].predictor = first;
3384 s->status[ch].step_index = 0;
3385 // header: step index (le16) then predictor (le16)
3386 dst[ch * 4 + 0] = 0;
3387 dst[ch * 4 + 1] = 0;
3388 dst[ch * 4 + 2] = first & 0xFF;
3389 dst[ch * 4 + 3] = (first >> 8) & 0xFF;
3390 }
3391
3392 uint8_t *out = dst + 4 * nch;
3393 for (int sub = 0; sub < FRAME_SIZE / SUBFRAME; sub++) {
3394 for (int ch = 0; ch < nch; ch++) {
3395 for (int n = 0; n < SUBFRAME; n += 2) {
3396 int t = sub * SUBFRAME + n;
3397 uint8_t lo = quantize(ch, samples[t * nch + ch]);
3398 uint8_t hi = quantize(ch, samples[(t + 1) * nch + ch]);
3399 *out++ = lo | (hi << 4);
3400 }
3401 }
3402 }
3403 return AV_OK;
3404 }
3405 uint8_t quantize(int ch, int16_t sample) {
3406 return adpcm_ima_quantize(&s->status[ch], sample, 3);
3407 }
3408};
3409
3417 public:
3418 static const int FRAME_SIZE = 1024; // samples per channel
3419
3421 setCodecID(AV_CODEC_ID_ADPCM_IMA_RAD);
3422 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3423 }
3424
3425
3426
3427 protected:
3428
3429 int adpcm_encode_init_impl() {
3430 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
3431 memset(&s->status, 0, sizeof(s->status));
3432 avctx.frame_size = FRAME_SIZE;
3433 avctx.block_align = channels() * (4 + FRAME_SIZE / 2);
3434 return AV_OK;
3435 }
3436
3437 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
3438 int *got_packet_ptr) {
3439 const int nch = channels();
3440 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
3441 memset(dst, 0, avctx.block_align);
3442
3443 for (int ch = 0; ch < nch; ch++) {
3444 int16_t first = samples[ch];
3445 s->status[ch].predictor = first;
3446 s->status[ch].step_index = 0;
3447 dst[ch * 4 + 0] = 0;
3448 dst[ch * 4 + 1] = 0;
3449 dst[ch * 4 + 2] = first & 0xFF;
3450 dst[ch * 4 + 3] = (first >> 8) & 0xFF;
3451 }
3452
3453 uint8_t *out = dst + 4 * nch;
3454 for (int n = 0; n < FRAME_SIZE; n += 2) {
3455 for (int ch = 0; ch < nch; ch++) {
3456 uint8_t lo = quantize(ch, samples[n * nch + ch]);
3457 uint8_t hi = quantize(ch, samples[(n + 1) * nch + ch]);
3458 *out++ = lo | (hi << 4);
3459 }
3460 }
3461 return AV_OK;
3462 }
3463 uint8_t quantize(int ch, int16_t sample) {
3464 return adpcm_ima_quantize(&s->status[ch], sample, 3);
3465 }
3466};
3467
3475 public:
3476 static const int FRAME_SIZE = 1024; // samples per channel
3477
3479 setCodecID(AV_CODEC_ID_ADPCM_IMA_ISS);
3480 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3481 }
3482
3483
3484
3485 protected:
3486
3487 int adpcm_encode_init_impl() {
3488 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
3489 memset(&s->status, 0, sizeof(s->status));
3490 avctx.frame_size = FRAME_SIZE;
3491 avctx.block_align = 4 * channels() + (FRAME_SIZE / 2) * channels();
3492 return AV_OK;
3493 }
3494
3495 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
3496 int *got_packet_ptr) {
3497 const int nch = channels();
3498 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
3499 uint8_t *nib = dst + 4 * nch;
3500 memset(dst, 0, avctx.block_align);
3501
3502 // header: every packet restarts from the first sample of each channel
3503 for (int ch = 0; ch < nch; ch++) {
3504 int16_t first = samples[ch];
3505 s->status[ch].predictor = first;
3506 s->status[ch].step_index = 0;
3507 dst[ch * 4 + 0] = first & 0xFF;
3508 dst[ch * 4 + 1] = (first >> 8) & 0xFF;
3509 dst[ch * 4 + 2] = 0;
3510 dst[ch * 4 + 3] = 0;
3511 }
3512
3513 if (nch == 2) {
3514 for (int t = 0; t < FRAME_SIZE; t++) {
3515 uint8_t left = quantize(0, samples[t * 2]);
3516 uint8_t right = quantize(1, samples[t * 2 + 1]);
3517 nib[t] = (left << 4) | right;
3518 }
3519 } else {
3520 for (int t = 0; t < FRAME_SIZE; t += 2) {
3521 uint8_t first = quantize(0, samples[t]);
3522 uint8_t second = quantize(0, samples[t + 1]);
3523 nib[t / 2] = first | (second << 4);
3524 }
3525 }
3526 return AV_OK;
3527 }
3528 uint8_t quantize(int ch, int16_t sample) {
3529 return adpcm_ima_quantize(&s->status[ch], sample, 3);
3530 }
3531};
3532
3540 public:
3541 static const int FRAME_SIZE = 1024; // samples per channel
3542 static const int HEADER_SIZE = 16;
3543
3545 setCodecID(AV_CODEC_ID_ADPCM_IMA_DK3);
3546 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3547 }
3548
3549
3550
3551 protected:
3552
3553 int adpcm_encode_init_impl() {
3554 // the decoder works on a sum/diff pair, so only stereo is supported
3555 if (channels() != 2) return AVERROR(AVERROR_INVALID);
3556 memset(&s->status, 0, sizeof(s->status));
3557 avctx.frame_size = FRAME_SIZE;
3558 avctx.block_align = HEADER_SIZE + FRAME_SIZE * 3 / 4;
3559 return AV_OK;
3560 }
3561
3562 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
3563 int *got_packet_ptr) {
3564 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
3565 uint8_t *out = dst;
3566 uint8_t *nib = dst + HEADER_SIZE;
3567 memset(dst, 0, avctx.block_align);
3568
3569 // each frame restarts from the header, so reset the state from the
3570 // first stereo pair
3571 int sum0 = (samples[0] + samples[1]) >> 1;
3572 int diff0 = (samples[0] - samples[1]) >> 1;
3573 s->status[0].predictor = sum0;
3574 s->status[1].predictor = diff0;
3575 s->status[0].step_index = 0;
3576 s->status[1].step_index = 0;
3577
3578 // header: 10 unused bytes, predictors and step indices
3579 out = dst + 10;
3580 ByteWriter(out).putLe16(sum0);
3581 ByteWriter(out).putLe16(diff0);
3582 *out++ = 0;
3583 *out++ = 0;
3584
3585 int nidx = 0;
3586 auto put = [&](int nibble) {
3587 if (nidx & 1) {
3588 nib[nidx >> 1] |= nibble << 4;
3589 } else {
3590 nib[nidx >> 1] = nibble;
3591 }
3592 nidx++;
3593 };
3594
3595 for (int t = 0; t < FRAME_SIZE; t += 2) {
3596 const int16_t *f0 = samples + t * 2;
3597 const int16_t *f1 = f0 + 2;
3598 int sum_a = (f0[0] + f0[1]) >> 1;
3599 int sum_c = (f1[0] + f1[1]) >> 1;
3600 int diff = ((f0[0] - f0[1]) + (f1[0] - f1[1])) >> 2;
3601 put(quantize(0, sum_a));
3602 put(quantize(1, diff));
3603 put(quantize(0, sum_c));
3604 }
3605 return AV_OK;
3606 }
3607 uint8_t quantize(int ch, int target) {
3608 return adpcm_ima_quantize(&s->status[ch], target, 3);
3609 }
3610};
3611
3620 public:
3621 static const int FRAME_SIZE = 1024; // samples per channel
3622
3623 EncoderADPCM_CT() {
3624 setCodecID(AV_CODEC_ID_ADPCM_CT);
3625 sample_formats.push_back(AV_SAMPLE_FMT_S16);
3626 }
3627
3628
3629
3630 protected:
3631
3632 int adpcm_encode_init_impl() {
3633 if (channels() < 1 || channels() > 2) return AVERROR(AVERROR_INVALID);
3634 memset(&s->status, 0, sizeof(s->status));
3635 s->status[0].step = s->status[1].step = 511;
3636 avctx.frame_size = FRAME_SIZE;
3637 avctx.block_align = (FRAME_SIZE / 2) * channels();
3638 return AV_OK;
3639 }
3640
3641 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
3642 int *got_packet_ptr) {
3643 const int nch = channels();
3644 if (frame->nb_samples != FRAME_SIZE) return AVERROR(AVERROR_INVALID);
3645 uint8_t *out = dst;
3646 if (nch == 2) {
3647 for (int t = 0; t < FRAME_SIZE; t++) {
3648 uint8_t left = quantize(0, samples[t * 2]);
3649 uint8_t right = quantize(1, samples[t * 2 + 1]);
3650 *out++ = (left << 4) | right;
3651 }
3652 } else {
3653 for (int t = 0; t < FRAME_SIZE; t += 2) {
3654 uint8_t hi = quantize(0, samples[t]);
3655 uint8_t lo = quantize(0, samples[t + 1]);
3656 *out++ = (hi << 4) | lo;
3657 }
3658 }
3659 return AV_OK;
3660 }
3661 // evaluates the decoder's update for every nibble, picks the one whose
3662 // reconstructed value is closest to the input, then advances the state
3663 uint8_t quantize(int ch, int16_t sample) {
3664 ADPCMChannelStatus *cs = &c->status[ch];
3665 int best = 0, best_pred = 0;
3666 int best_e = 0x7fffffff;
3667 for (int nibble = 0; nibble < 16; nibble++) {
3668 int diff = ((2 * (nibble & 7) + 1) * cs->step) >> 3;
3669 int pred = ((cs->predictor * 254) >> 8) + (nibble & 8 ? -diff : diff);
3670 pred = av_clip_int16(pred);
3671 int e = pred > sample ? pred - sample : sample - pred;
3672 if (e < best_e) {
3673 best_e = e;
3674 best = nibble;
3675 best_pred = pred;
3676 }
3677 }
3678 int new_step = (ff_adpcm_AdaptationTable[best & 7] * cs->step) >> 8;
3679 cs->predictor = best_pred;
3680 cs->step = av_clip(new_step, 511, 32767);
3681 return (uint8_t)best;
3682 }
3683};
3684
3691 public:
3692 static const int SUBFRAME_SAMPLES = 16;
3693
3695 setCodecID(AV_CODEC_ID_ADPCM_AFC);
3696 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
3697 }
3698
3699
3700
3701 protected:
3702
3703 int adpcm_encode_init_impl() {
3704 if (channels() < 1 || channels() > 6) return AVERROR(AVERROR_INVALID);
3705 memset(&s->status, 0, sizeof(s->status));
3706 avctx.frame_size = SUBFRAME_SAMPLES;
3707 avctx.block_align = 9 * channels();
3708 return AV_OK;
3709 }
3710
3711 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
3712 int *got_packet_ptr) {
3713 if (frame->nb_samples != SUBFRAME_SAMPLES) return AVERROR(AVERROR_INVALID);
3714 // channel-major layout: each channel's subframe is contiguous
3715 for (int ch = 0; ch < channels(); ch++) {
3716 encodeSubframe(&c->status[ch], samples_p[ch], dst + ch * 9);
3717 }
3718 return AV_OK;
3719 }
3720 void encodeSubframe(ADPCMChannelStatus *cs, const int16_t *in, uint8_t *out) {
3721 int index = 0, exp = 0;
3722 encodeDSPSubframe(cs, in, SUBFRAME_SAMPLES, afc_coeffs[0], afc_coeffs[1],
3723 16, out + 1, index, exp);
3724 out[0] = (uint8_t)((exp << 4) | index);
3725 }
3726};
3727
3735 public:
3736 static const int SUBFRAMES = 32; // per channel and packet
3737 static const int FRAME_SAMPLES = 14 * SUBFRAMES;
3738
3739 EncoderADPCM_THP(AVCodecID id) {
3740 setCodecID(id);
3741 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
3742 }
3743
3744
3745
3746 protected:
3747
3748 int adpcm_encode_init_impl() {
3749 if (channels() < 1 || channels() > 6) return AVERROR(AVERROR_INVALID);
3750 memset(&s->status, 0, sizeof(s->status));
3751 avctx.frame_size = FRAME_SAMPLES;
3752 avctx.block_align = 8 + 36 * channels() + 8 * SUBFRAMES * channels();
3753 return AV_OK;
3754 }
3755
3756 int adpcm_encode_frame_impl(AVPacket *avpkt, const AVFrame *frame,
3757 int *got_packet_ptr) {
3758 if (frame->nb_samples != FRAME_SAMPLES) return AVERROR(AVERROR_INVALID);
3759 const int nch = channels();
3760 uint8_t *out = dst;
3761
3762 // header: 4 bytes of unused channel size, then the coded sample count
3763 put32(out, 0);
3764 put32(out + 4, FRAME_SAMPLES);
3765 out += 8;
3766
3767 // coefficient table: 8 coefficient pairs per channel, 16 values
3768 for (int ch = 0; ch < nch; ch++) {
3769 for (int i = 0; i < 8; i++) {
3770 put16(out, afc_coeffs[0][i]);
3771 put16(out + 2, afc_coeffs[1][i]);
3772 out += 4;
3773 }
3774 }
3775
3776 // initial state per channel (only read by the first packet)
3777 for (int ch = 0; ch < nch; ch++) {
3778 put16(out, c->status[ch].sample1);
3779 put16(out + 2, c->status[ch].sample2);
3780 out += 4;
3781 }
3782
3783 // subframes, channel by channel
3784 for (int ch = 0; ch < nch; ch++) {
3785 for (int k = 0; k < SUBFRAMES; k++) {
3786 int index = 0, exp = 0;
3787 encodeDSPSubframe(&c->status[ch], samples_p[ch] + k * 14, 14,
3788 afc_coeffs[0], afc_coeffs[1], 8, out + 1, index,
3789 exp);
3790 out[0] = (uint8_t)((index << 4) | exp);
3791 out += 8;
3792 }
3793 }
3794 return AV_OK;
3795 }
3796 bool isLE() { return codecID() == AV_CODEC_ID_ADPCM_THP_LE; }
3797
3798 void put16(uint8_t *p, int v) {
3799 if (isLE()) {
3800 p[0] = v & 0xFF;
3801 p[1] = (v >> 8) & 0xFF;
3802 } else {
3803 p[0] = (v >> 8) & 0xFF;
3804 p[1] = v & 0xFF;
3805 }
3806 }
3807
3808 void put32(uint8_t *p, uint32_t v) {
3809 if (isLE()) {
3810 for (int i = 0; i < 4; i++) p[i] = (v >> (8 * i)) & 0xFF;
3811 } else {
3812 for (int i = 0; i < 4; i++) p[i] = (v >> (24 - 8 * i)) & 0xFF;
3813 }
3814 }
3815};
3816
3818 public:
3819 static ADPCMEncoder *create(AVCodecID id) {
3820 switch (id) {
3821 case AV_CODEC_ID_ADPCM_IMA_WAV:
3822 return new EncoderADPCM_IMA_WAV();
3823 case AV_CODEC_ID_ADPCM_IMA_QT:
3824 return new EncoderADPCM_IMA_QT();
3825 case AV_CODEC_ID_ADPCM_IMA_SSI:
3826 return new EncoderADPCM_IMA_SSI();
3827 case AV_CODEC_ID_ADPCM_IMA_ALP:
3828 return new EncoderADPCM_IMA_ALP();
3829 case AV_CODEC_ID_ADPCM_MS:
3830 return new EncoderADPCM_MS();
3831 case AV_CODEC_ID_ADPCM_SWF:
3832 return new EncoderADPCM_SWF();
3833 case AV_CODEC_ID_ADPCM_YAMAHA:
3834 return new EncoderADPCM_YAMAHA();
3835 case AV_CODEC_ID_ADPCM_IMA_APM:
3836 return new EncoderADPCM_IMA_APM();
3837 case AV_CODEC_ID_ADPCM_IMA_AMV:
3838 return new EncoderADPCM_IMA_AMV();
3839 case AV_CODEC_ID_ADPCM_ARGO:
3840 return new EncoderADPCM_ARGO();
3841 case AV_CODEC_ID_ADPCM_IMA_WS:
3842 return new EncoderADPCM_IMA_WS();
3843 case AV_CODEC_ID_ADPCM_PSX:
3844 return new EncoderADPCM_PSX();
3845 case AV_CODEC_ID_ADPCM_IMA_DK4:
3846 return new EncoderADPCM_IMA_DK4();
3847 case AV_CODEC_ID_ADPCM_AICA:
3848 return new EncoderADPCM_AICA();
3849 case AV_CODEC_ID_ADPCM_IMA_OKI:
3850 return new EncoderADPCM_IMA_OKI();
3851 case AV_CODEC_ID_ADPCM_AFC:
3852 return new EncoderADPCM_AFC();
3853 case AV_CODEC_ID_ADPCM_THP:
3854 return new EncoderADPCM_THP(AV_CODEC_ID_ADPCM_THP);
3855 case AV_CODEC_ID_ADPCM_THP_LE:
3856 return new EncoderADPCM_THP(AV_CODEC_ID_ADPCM_THP_LE);
3857 case AV_CODEC_ID_ADPCM_CT:
3858 return new EncoderADPCM_CT();
3859 case AV_CODEC_ID_ADPCM_IMA_DK3:
3860 return new EncoderADPCM_IMA_DK3();
3861 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
3862 return new EncoderADPCM_IMA_EA_SEAD();
3863 case AV_CODEC_ID_ADPCM_IMA_ISS:
3864 return new EncoderADPCM_IMA_ISS();
3865 case AV_CODEC_ID_ADPCM_IMA_ACORN:
3866 return new EncoderADPCM_IMA_ACORN();
3867 case AV_CODEC_ID_ADPCM_IMA_DAT4:
3868 return new EncoderADPCM_IMA_DAT4();
3869 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
3870 return new EncoderADPCM_IMA_MOFLEX();
3871 case AV_CODEC_ID_ADPCM_IMA_RAD:
3872 return new EncoderADPCM_IMA_RAD();
3873 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
3874 return new EncoderADPCM_IMA_SMJPEG();
3875 case AV_CODEC_ID_ADPCM_IMA_APC:
3876 return new EncoderADPCM_IMA_APC();
3877 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
3878 return new EncoderADPCM_IMA_EACS();
3879 case AV_CODEC_ID_ADPCM_ZORK:
3880 return new EncoderADPCM_ZORK();
3881 case AV_CODEC_ID_ADPCM_XMD:
3882 return new EncoderADPCM_XMD();
3883 case AV_CODEC_ID_ADPCM_AGM:
3884 return new EncoderADPCM_AGM();
3885 case AV_CODEC_ID_ADPCM_MTAF:
3886 return new EncoderADPCM_MTAF();
3887 case AV_CODEC_ID_ADPCM_DTK:
3888 return new EncoderADPCM_DTK();
3889 case AV_CODEC_ID_ADPCM_4XM:
3890 return new EncoderADPCM_4XM();
3891 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
3892 return new EncoderADPCM_EA_MAXIS_XA();
3893 case AV_CODEC_ID_ADPCM_EA_XAS:
3894 return new EncoderADPCM_EA_XAS();
3895 case AV_CODEC_ID_ADPCM_EA_R1:
3896 return new EncoderADPCM_EA_RX(AV_CODEC_ID_ADPCM_EA_R1);
3897 case AV_CODEC_ID_ADPCM_EA_R2:
3898 return new EncoderADPCM_EA_RX(AV_CODEC_ID_ADPCM_EA_R2);
3899 case AV_CODEC_ID_ADPCM_EA_R3:
3900 return new EncoderADPCM_EA_RX(AV_CODEC_ID_ADPCM_EA_R3);
3901 case AV_CODEC_ID_ADPCM_EA:
3902 return new EncoderADPCM_EA();
3903 case AV_CODEC_ID_ADPCM_XA:
3904 return new EncoderADPCM_XA();
3905 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
3906 return new EncoderADPCM_IMA_CUNNING();
3907 case AV_CODEC_ID_ADPCM_IMA_MTF:
3908 return new EncoderADPCM_IMA_MTF();
3909 case AV_CODEC_ID_ADPCM_SBPRO_2:
3910 case AV_CODEC_ID_ADPCM_SBPRO_3:
3911 case AV_CODEC_ID_ADPCM_SBPRO_4:
3912 return new EncoderADPCM_SBPRO_X(id);
3913
3914 default:
3915 av_log(avctx, AV_LOG_ERROR, "ERROR: encoder [%d] not implemented\n", id);
3916 return nullptr;
3917 };
3918 }
3919};
3920
3921} // namespace adpcm_ffmpeg
Common ADPCM Functionality.
Definition ADPCMCodec.h:23
Definition ADPCMEncoder.h:3817
Definition ADPCMEncoder.h:386
Definition ADPCMEncoder.h:33
void encodeDSPSubframe(ADPCMChannelStatus *cs, const int16_t *in, int n, const int16_t *coef1, const int16_t *coef2, int npairs, uint8_t *nibbles_out, int &best_index, int &best_exp)
Definition ADPCMEncoder.h:107
virtual int packetSize(const AVFrame *frame)
Definition ADPCMEncoder.h:184
uint8_t adpcm_ima_quantize(ADPCMChannelStatus *cs, int target, int shift)
Definition ADPCMEncoder.h:278
Vector implementation which provides the most important methods as defined by std::vector....
Definition ADPCMVector.h:16
Writes bits into a buffer. By default the first bit written is the least significant bit of the first...
Definition BitWriter.h:27
Writer for a memory buffer. The writer advances the caller's pointer, so it can be combined with dire...
Definition ByteWriter.h:26
4XM ADPCM encoder. Header: le16 predictor per channel, then le16 step index per channel....
Definition ADPCMEncoder.h:3087
GameCube AFC ADPCM encoder. Each channel holds one 9 byte subframe per 16 samples: a scale/coefficien...
Definition ADPCMEncoder.h:3690
AGM encoder. Header per packet: le16 predictor and le16 step per channel. Each byte holds one time st...
Definition ADPCMEncoder.h:1698
Yamaha ADPCM with planar output as used by AICA (Dreamcast). Each channel is stored as a contiguous r...
Definition ADPCMEncoder.h:1422
Definition ADPCMEncoder.h:1122
int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, uint8_t *out, const int16_t *samples, int nsamples, int shift, int flag)
Definition ADPCMEncoder.h:1163
Creative (CT) 4-bit ADPCM encoder. Two nibbles per byte; mono packs two consecutive samples (high nib...
Definition ADPCMEncoder.h:3619
DTK encoder (stereo only). A 32 byte unit holds 28 samples per channel: byte 0 is the channel 0 heade...
Definition ADPCMEncoder.h:1902
EA MAXIS XA encoder (mono or stereo). Each packet starts with one header byte per channel: high nibbl...
Definition ADPCMEncoder.h:2993
EA R1/R2/R3 encoder. Packet: a 4 byte sample count, one 4 byte offset per channel (relative to the en...
Definition ADPCMEncoder.h:2742
EA XAS encoder. Each packet holds 128 samples per channel, one 76 byte chunk per channel: four groups...
Definition ADPCMEncoder.h:2875
EA (stereo only) encoder. Packet: le32 sample count, then four le16 state values (current and previou...
Definition ADPCMEncoder.h:2622
IMA ACORN encoder. Same 4 byte per channel header as ISS, but the stereo nibbles are swapped: the lef...
Definition ADPCMEncoder.h:1586
Definition ADPCMEncoder.h:756
Definition ADPCMEncoder.h:1060
IMA APC encoder. No per-packet header: the decoder keeps its state across packets (starting from zero...
Definition ADPCMEncoder.h:3215
Definition ADPCMEncoder.h:1021
IMA CUNNING encoder (planar). No header: channel 0 has FRAME_SIZE/2 bytes (low nibble first),...
Definition ADPCMEncoder.h:2175
IMA DAT4 encoder. Planar layout: each channel has a 4 byte unused header followed by FRAME_SIZE/2 byt...
Definition ADPCMEncoder.h:1649
IMA DK3 encoder (stereo only). The 16 byte block header holds 10 unused bytes, the sum and diff predi...
Definition ADPCMEncoder.h:3539
IMA ADPCM with a 4 byte per channel header (DK4). The header holds the first sample and the step inde...
Definition ADPCMEncoder.h:1361
IMA EACS encoder. Header: le32 sample count, then per channel an le32 step index followed by an le32 ...
Definition ADPCMEncoder.h:3144
EA SEAD IMA encoder: stateful 4-bit IMA with a shift-6 reconstruction (see DecoderADPCM_IMA_EA_SEAD)....
Definition ADPCMEncoder.h:1536
IMA ISS encoder. Each packet starts with a 4 byte header per channel (le16 predictor,...
Definition ADPCMEncoder.h:3474
IMA MOFLEX encoder. Header per channel (le16 step index, le16 predictor) resets the decoder for each ...
Definition ADPCMEncoder.h:3353
IMA MTF encoder. Each byte holds one nibble per channel for a pair of samples: the high nibble is the...
Definition ADPCMEncoder.h:2245
OKI / MSM6258 ADPCM. The predictor works on 12 bit values and the step table is the OKI table,...
Definition ADPCMEncoder.h:1462
QuickTime IMA ADPCM encoder (mono or stereo). Each channel is a 34 byte block: a big-endian 16-bit he...
Definition ADPCMEncoder.h:678
IMA RAD encoder. Header per channel (le16 step index, le16 predictor). Each byte holds the low nibble...
Definition ADPCMEncoder.h:3416
IMA SMJPEG encoder. Header per channel: be16 predictor, step index byte, one unused byte....
Definition ADPCMEncoder.h:3267
Definition ADPCMEncoder.h:725
Definition ADPCMEncoder.h:606
Definition ADPCMEncoder.h:1234
Definition ADPCMEncoder.h:809
MTAF encoder (even channel counts). Each channel pair starts with a 16 byte header: 4 unused bytes,...
Definition ADPCMEncoder.h:1812
PlayStation ADPCM (VAG style) encoder. Each packet holds one 16 byte block per channel,...
Definition ADPCMEncoder.h:1275
Sound Blaster Pro ADPCM encoder for SBPRO_2 (mono or stereo), SBPRO_3 (mono only) and SBPRO_4 (mono o...
Definition ADPCMEncoder.h:2320
int packetSize(const AVFrame *frame)
Definition ADPCMEncoder.h:2328
Definition ADPCMEncoder.h:913
GameCube THP ADPCM encoder (big and little endian variants). A packet holds an 8 byte header (unused ...
Definition ADPCMEncoder.h:3734
CD-XA ADPCM encoder (mono or stereo). A packet holds 224 samples per channel in ch 128 byte blocks....
Definition ADPCMEncoder.h:2502
Microsoft XMD encoder. Blocks of 32 samples per channel, 21 bytes each: le16 history[1],...
Definition ADPCMEncoder.h:2088
Definition ADPCMEncoder.h:979
LucasArts ZORK encoder. One byte per sample, interleaved, with the predictor and step index carried a...
Definition ADPCMEncoder.h:2012
Definition adpcm.h:185
Definition adpcm.h:200
int frame_size
Number of samples per channel in an audio frame.
Definition adpcm.h:218
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition adpcm.h:221
This structure provides the uncompressed PCM data.
Definition adpcm.h:130
int16_t ** extended_data
Definition adpcm.h:164
int nb_samples
Definition adpcm.h:148
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition adpcm.h:143
This structure stores compressed data. It is typically exported by demuxers and then passed as input ...
Definition adpcm.h:122
ADPCM Encoder.
Definition ADPCMEncoder.h:20
Definition adpcm.h:172
Definition adpcm.h:167