1 /*
2 * Copyright (c) 2001-2003 The FFmpeg project
3 *
4 * first version by Francois Revol (revol@free.fr)
5 * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
6 * by Mike Melanson (melanson@pcisys.net)
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "config_components.h"
26
29
37
38 /**
39 * @file
40 * ADPCM encoders
41 * See ADPCM decoder reference documents for codec information.
42 */
43
44 #define CASE_0(codec_id, ...)
45 #define CASE_1(codec_id, ...) \
46 case codec_id: \
47 { __VA_ARGS__ } \
48 break;
49 #define CASE_2(enabled, codec_id, ...) \
50 CASE_ ## enabled(codec_id, __VA_ARGS__)
51 #define CASE_3(config, codec_id, ...) \
52 CASE_2(config, codec_id, __VA_ARGS__)
53 #define CASE(codec, ...) \
54 CASE_3(CONFIG_ ## codec ## _ENCODER, AV_CODEC_ID_ ## codec, __VA_ARGS__)
55
60
68
72
79
80 #define FREEZE_INTERVAL 128
81
83 {
86
87 /*
88 * AMV's block size has to match that of the corresponding video
89 * stream. Relax the POT requirement.
90 */
92 (
s->block_size & (
s->block_size - 1))) {
95 }
96
98 int frontier, max_paths;
99
100 if ((
unsigned)avctx->
trellis > 16
U) {
103 }
104
109 /*
110 * The current trellis implementation doesn't work for extended
111 * runs of samples without periodic resets. Disallow it.
112 */
115 }
116
117 frontier = 1 << avctx->
trellis;
124 }
125
127
130 /* each 16 bits sample gives one nibble
131 and we have 4 bytes per channel overhead */
134 /* seems frame_size isn't taken into account...
135 have to buffer the samples :-( */
138 ) /* End of CASE */
142 ) /* End of CASE */
144 uint8_t *extradata;
145 /* each 16 bits sample gives one nibble
146 and we have 7 bytes per channel overhead */
154 bytestream_put_le16(&extradata, avctx->
frame_size);
155 bytestream_put_le16(&extradata, 7); /* wNumCoef */
156 for (
int i = 0;
i < 7;
i++) {
159 }
160 ) /* End of CASE */
164 ) /* End of CASE */
169 av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
170 "22050 or 44100\n");
171 return AVERROR(EINVAL);
172 }
173 avctx->
frame_size = 4096;
/* Hardcoded according to the SWF spec. */
175 ) /* End of CASE */
180 break;
183 av_log(avctx, AV_LOG_ERROR, "Sample rate must be 22050\n");
184 return AVERROR(EINVAL);
185 }
186
190 }
191
194 ) /* End of CASE */
198
202 ) /* End of CASE */
206 ) /* End of CASE */
208 /* each 16 bits sample gives one nibble */
211 ) /* End of CASE */
212 default:
214 }
215
216 return 0;
217 }
218
220 {
226
227 return 0;
228 }
229
230
233 {
241 return nibble;
242 }
243
245 {
248 const int sign = (
delta < 0) * 8;
249
252 if (sign)
254
255 nibble = sign | nibble;
256
257 c->prev_sample +=
diff;
260 return nibble;
261 }
262
265 {
268 int nibble = 8*(
delta < 0);
269
272
274 nibble |= 4;
276 }
279 nibble |= 2;
281 }
284 nibble |= 1;
286 }
288
289 if (nibble & 8)
290 c->prev_sample -=
diff;
291 else
292 c->prev_sample +=
diff;
293
296
297 return nibble;
298 }
299
302 {
304
306 ((
c->sample2) * (
c->coeff2))) / 64;
307
309 if (nibble >= 0)
310 bias =
c->idelta / 2;
311 else
312 bias = -
c->idelta / 2;
313
314 nibble = (nibble +
bias) /
c->idelta;
316
317 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) *
c->idelta;
318
319 c->sample2 =
c->sample1;
321
325
326 return nibble;
327 }
328
331 {
333
337 }
338
340
342
347
348 return nibble;
349 }
350
354 {
355 //FIXME 6% faster if frontier is a compile-time constant
357 const int frontier = 1 << avctx->
trellis;
362 TrellisNode **nodes = nodep_buf;
// nodes[] is always sorted by .ssd
364 int pathn = 0, froze = -1,
i, j, k, generation = 0;
365 uint8_t *
hash =
s->trellis_hash;
366 memset(
hash, 0xff, 65536 *
sizeof(*
hash));
367
368 memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
369 nodes[0] = node_buf + frontier;
372 nodes[0]->
step =
c->step_index;
381 nodes[0]->
step =
c->idelta;
384 nodes[0]->
step = 127;
386 } else {
387 nodes[0]->
step =
c->step;
389 }
390 }
391
392 for (
i = 0;
i < n;
i++) {
396 int heap_pos = 0;
397 memset(nodes_next, 0, frontier *
sizeof(
TrellisNode*));
398 for (j = 0; j < frontier && nodes[j]; j++) {
399 // higher j have higher ssd already, so they're likely
400 // to yield a suboptimal next sample too
401 const int range = (j < frontier / 2) ? 1 : 0;
402 const int step = nodes[j]->step;
403 int nidx;
405 const int predictor = ((nodes[j]->sample1 *
c->coeff1) +
406 (nodes[j]->sample2 *
c->coeff2)) / 64;
410 for (nidx = nmin; nidx <= nmax; nidx++) {
411 const int nibble = nidx & 0xf;
413 #define STORE_NODE(NAME, STEP_INDEX)\
414 int d;\
415 uint32_t ssd;\
416 int pos;\
417 TrellisNode *u;\
418 uint8_t *h;\
419 dec_sample = av_clip_int16(dec_sample);\
420 d = sample - dec_sample;\
421 ssd = nodes[j]->ssd + d*(unsigned)d;\
422 /* Check for wraparound, skip such samples completely. \
423 * Note, changing ssd to a 64 bit variable would be \
424 * simpler, avoiding this check, but it's slower on \
425 * x86 32 bit at the moment. */\
426 if (ssd < nodes[j]->ssd)\
427 goto next_##NAME;\
428 /* Collapse any two states with the same previous sample value. \
429 * One could also distinguish states by step and by 2nd to last
430 * sample, but the effects of that are negligible.
431 * Since nodes in the previous generation are iterated
432 * through a heap, they're roughly ordered from better to
433 * worse, but not strictly ordered. Therefore, an earlier
434 * node with the same sample value is better in most cases
435 * (and thus the current is skipped), but not strictly
436 * in all cases. Only skipping samples where ssd >=
437 * ssd of the earlier node with the same sample gives
438 * slightly worse quality, though, for some reason. */ \
439 h = &hash[(uint16_t) dec_sample];\
440 if (*h == generation)\
441 goto next_##NAME;\
442 if (heap_pos < frontier) {\
443 pos = heap_pos++;\
444 } else {\
445 /* Try to replace one of the leaf nodes with the new \
446 * one, but try a different slot each time. */\
447 pos = (frontier >> 1) +\
448 (heap_pos & ((frontier >> 1) - 1));\
449 if (ssd > nodes_next[pos]->ssd)\
450 goto next_##NAME;\
451 heap_pos++;\
452 }\
453 *h = generation;\
454 u = nodes_next[pos];\
455 if (!u) {\
456 av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
457 u = t++;\
458 nodes_next[pos] = u;\
459 u->path = pathn++;\
460 }\
461 u->ssd = ssd;\
462 u->step = STEP_INDEX;\
463 u->sample2 = nodes[j]->sample1;\
464 u->sample1 = dec_sample;\
465 paths[u->path].nibble = nibble;\
466 paths[u->path].prev = nodes[j]->path;\
467 /* Sift the newly inserted node up in the heap to \
468 * restore the heap property. */\
469 while (pos > 0) {\
470 int parent = (pos - 1) >> 1;\
471 if (nodes_next[parent]->ssd <= ssd)\
472 break;\
473 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
474 pos = parent;\
475 }\
476 next_##NAME:;
479 }
484 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
485 const int predictor = nodes[j]->sample1;\
486 const int div = (sample - predictor) * 4 / STEP_TABLE;\
487 int nmin = av_clip(div - range, -7, 6);\
488 int nmax = av_clip(div + range, -6, 7);\
489 if (nmin <= 0)\
490 nmin--; /* distinguish -0 from +0 */\
491 if (nmax < 0)\
492 nmax--;\
493 for (nidx = nmin; nidx <= nmax; nidx++) {\
494 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
495 int dec_sample = predictor +\
496 (STEP_TABLE *\
497 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
498 STORE_NODE(NAME, STEP_INDEX);\
499 }
502 } else { //AV_CODEC_ID_ADPCM_YAMAHA
505 127, 24576));
506 #undef LOOP_NODES
507 #undef STORE_NODE
508 }
509 }
510
512 nodes = nodes_next;
514
515 generation++;
516 if (generation == 255) {
517 memset(
hash, 0xff, 65536 *
sizeof(*
hash));
518 generation = 0;
519 }
520
521 // prevent overflow
522 if (nodes[0]->ssd > (1 << 28)) {
523 for (j = 1; j < frontier && nodes[j]; j++)
524 nodes[j]->ssd -= nodes[0]->ssd;
525 nodes[0]->ssd = 0;
526 }
527
528 // merge old paths to save memory
530 p = &paths[nodes[0]->path];
531 for (k =
i; k > froze; k--) {
533 p = &paths[p->prev];
534 }
536 pathn = 0;
537 // other nodes might use paths that don't coincide with the frozen one.
538 // checking which nodes do so is too slow, so just kill them all.
539 // this also slightly improves quality, but I don't know why.
540 memset(nodes + 1, 0, (frontier - 1) *
sizeof(
TrellisNode*));
541 }
542 }
543
544 p = &paths[nodes[0]->
path];
545 for (
i = n - 1;
i > froze;
i--) {
547 p = &paths[p->prev];
548 }
549
550 c->predictor = nodes[0]->sample1;
551 c->sample1 = nodes[0]->sample1;
552 c->sample2 = nodes[0]->sample2;
553 c->step_index = nodes[0]->step;
554 c->step = nodes[0]->step;
555 c->idelta = nodes[0]->step;
556 }
557
558 #if CONFIG_ADPCM_ARGO_ENCODER
561 {
562 int nibble;
563
566 else
568
569 return (nibble >>
shift) & 0x0F;
570 }
571
573 const int16_t *
samples,
int nsamples,
575 {
577
578 if (pb) {
583 }
584
585 for (
int n = 0; n < nsamples; n++) {
586 /* Compress the nibble, then expand it to see how much precision we've lost. */
589
591
592 if (pb)
594 }
595
597 }
598 #endif
599
602 {
603 int st, pkt_size,
ret;
605 const int16_t *const *samples_p;
609
611 samples_p = (
const int16_t *
const *)
frame->extended_data;
613
619 else
624
627 int blocks = (
frame->nb_samples - 1) / 8;
628
631 status->prev_sample = samples_p[ch][0];
632 /* status->step_index = 0;
633 XXX: not sure how to init the state machine */
634 bytestream_put_le16(&
dst,
status->prev_sample);
636 *
dst++ = 0;
/* unknown */
637 }
638
639 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
641 uint8_t *buf;
644 for (
int ch = 0; ch <
channels; ch++) {
646 buf + ch * blocks * 8, &
c->status[ch],
647 blocks * 8, 1);
648 }
649 for (
int i = 0;
i < blocks;
i++) {
650 for (
int ch = 0; ch <
channels; ch++) {
651 uint8_t *buf1 = buf + ch * blocks * 8 +
i * 8;
652 for (int j = 0; j < 8; j += 2)
653 *
dst++ = buf1[j] | (buf1[j + 1] << 4);
654 }
655 }
657 } else {
658 for (
int i = 0;
i < blocks;
i++) {
659 for (
int ch = 0; ch <
channels; ch++) {
661 const int16_t *smp = &samples_p[ch][1 +
i * 8];
662 for (int j = 0; j < 8; j += 2) {
666 }
667 }
668 }
669 }
670 ) /* End of CASE */
674
675 for (
int ch = 0; ch <
channels; ch++) {
680 uint8_t buf[64];
682 64, 1);
683 for (
int i = 0;
i < 64;
i++)
686 } else {
687 for (
int i = 0;
i < 64;
i += 2) {
688 int t1, t2;
693 }
694 }
695 }
696
698 ) /* End of CASE */
702
704
705 for (
int i = 0;
i <
frame->nb_samples;
i++) {
706 for (
int ch = 0; ch <
channels; ch++) {
708 }
709 }
710
712 ) /* End of CASE */
716
718
719 for (
int n =
frame->nb_samples / 2; n > 0; n--) {
720 for (
int ch = 0; ch <
channels; ch++) {
723 }
725 }
726
728 ) /* End of CASE */
730 const int n =
frame->nb_samples - 1;
733
734 /* NB: This is safe as we don't have AV_CODEC_CAP_SMALL_LAST_FRAME. */
736
737 // store AdpcmCodeSize
738 put_bits(&pb, 2, 2);
// set 4-bit flash adpcm format
739
740 // init the encoder state
742 // clip step so it fits 6 bits
747 }
748
750 uint8_t buf[8190 /* = 2 * n */];
755 buf + n, &
c->status[1], n,
757 for (
int i = 0;
i < n;
i++) {
761 }
762 } else {
763 for (
int i = 1;
i <
frame->nb_samples;
i++) {
769 }
770 }
772 ) /* End of CASE */
779 }
781 if (
c->status[
i].idelta < 16)
782 c->status[
i].idelta = 16;
783 bytestream_put_le16(&
dst,
c->status[
i].idelta);
784 }
789 bytestream_put_le16(&
dst,
c->status[
i].sample1);
790 }
792 bytestream_put_le16(&
dst,
c->status[
i].sample2);
793
797 if (!buf)
802 for (
int i = 0;
i < n;
i += 2)
803 *
dst++ = (buf[
i] << 4) | buf[
i + 1];
804 } else {
809 for (
int i = 0;
i < n;
i++)
810 *
dst++ = (buf[
i] << 4) | buf[n +
i];
811 }
813 } else {
815 int nibble;
819 }
820 }
821 ) /* End of CASE */
823 int n =
frame->nb_samples / 2;
826 if (!buf)
828 n *= 2;
832 for (
int i = 0;
i < n;
i += 2)
833 *
dst++ = buf[
i] | (buf[
i + 1] << 4);
834 } else {
839 for (
int i = 0;
i < n;
i++)
840 *
dst++ = buf[
i] | (buf[n +
i] << 4);
841 }
843 } else
845 int nibble;
849 }
850 ) /* End of CASE */
854
856
857 for (
int n =
frame->nb_samples / 2; n > 0; n--) {
858 for (
int ch = 0; ch <
channels; ch++) {
861 }
863 }
864
866 ) /* End of CASE */
869
870 c->status[0].prev_sample = *
samples;
871 bytestream_put_le16(&
dst,
c->status[0].prev_sample);
872 bytestream_put_byte(&
dst,
c->status[0].step_index);
873 bytestream_put_byte(&
dst, 0);
875
877 const int n =
frame->nb_samples >> 1;
879
880 if (!buf)
882
884 for (
int i = 0;
i < n;
i++)
885 bytestream_put_byte(&
dst, (buf[2 *
i] << 4) | buf[2 *
i + 1]);
886
889 }
else for (
int n =
frame->nb_samples >> 1; n > 0; n--) {
890 int nibble;
893 bytestream_put_byte(&
dst, nibble);
894 }
895
898 bytestream_put_byte(&
dst, nibble);
899 }
900 ) /* End of CASE */
904
906
907 for (
int ch = 0; ch <
channels; ch++) {
910 int saved1 =
c->status[ch].sample1;
911 int saved2 =
c->status[ch].sample2;
912
913 /* Find the optimal coefficients, bail early if we find a perfect result. */
914 for (
int s = 2;
s < 18 && tmperr != 0;
s++) {
915 for (
int f = 0;
f < 2 && tmperr != 0;
f++) {
916 c->status[ch].sample1 = saved1;
917 c->status[ch].sample2 = saved2;
918 tmperr = adpcm_argo_compress_block(
c->status + ch,
NULL, samples_p[ch],
920 if (tmperr <
error) {
924 }
925 }
926 }
927
928 /* Now actually do the encode. */
929 c->status[ch].sample1 = saved1;
930 c->status[ch].sample2 = saved2;
931 adpcm_argo_compress_block(
c->status + ch, &pb, samples_p[ch],
933 }
934
936 ) /* End of CASE */
940
942 for (
int n =
frame->nb_samples / 2; n > 0; n--) {
943 /* stereo: 1 byte (2 samples) for left, 1 byte for right */
944 for (
int ch = 0; ch <
channels; ch++) {
945 int t1, t2;
950 }
954 ) /* End of CASE */
955 default:
957 }
958
959 *got_packet_ptr = 1;
960 return 0;
961 }
965 };
966
969 };
970
974 { 0 },
975 };
978 {
979 .
name =
"block_size",
980 .help = "set the block size",
983 .default_val = {.i64 = 1024},
985 .max = 8192, /* Is this a reasonable upper limit? */
987 },
989 };
990
996 };
997
998 #define ADPCM_ENCODER_0(id_, name_, sample_fmts_, capabilities_, long_name_)
999 #define ADPCM_ENCODER_1(id_, name_, sample_fmts_, capabilities_, long_name_) \
1000 const FFCodec ff_ ## name_ ## _encoder = { \
1002 CODEC_LONG_NAME(long_name_), \
1003 .p.type = AVMEDIA_TYPE_AUDIO, \
1004 .p.id = id_, \
1005 .p.sample_fmts = sample_fmts_, \
1006 .p.ch_layouts = ch_layouts, \
1007 .p.capabilities = capabilities_ | AV_CODEC_CAP_DR1 | \
1008 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, \
1009 .p.priv_class = &adpcm_encoder_class, \
1010 .priv_data_size = sizeof(ADPCMEncodeContext), \
1011 .init = adpcm_encode_init, \
1012 FF_CODEC_ENCODE_CB(adpcm_encode_frame), \
1013 .close = adpcm_encode_close, \
1014 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
1015 };
1016 #define ADPCM_ENCODER_2(enabled, codec_id, name, sample_fmts, capabilities, long_name) \
1017 ADPCM_ENCODER_ ## enabled(codec_id, name, sample_fmts, capabilities, long_name)
1018 #define ADPCM_ENCODER_3(config, codec_id, name, sample_fmts, capabilities, long_name) \
1019 ADPCM_ENCODER_2(config, codec_id, name, sample_fmts, capabilities, long_name)
1020 #define ADPCM_ENCODER(codec, name, sample_fmts, capabilities, long_name) \
1021 ADPCM_ENCODER_3(CONFIG_ ## codec ## _ENCODER, AV_CODEC_ID_ ## codec, \
1022 name, sample_fmts, capabilities, long_name)
1023