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
28
36
37 /**
38 * @file
39 * ADPCM encoders
40 * See ADPCM decoder reference documents for codec information.
41 */
42
43 #define CASE_0(codec_id, ...)
44 #define CASE_1(codec_id, ...) \
45 case codec_id: \
46 { __VA_ARGS__ } \
47 break;
48 #define CASE_2(enabled, codec_id, ...) \
49 CASE_ ## enabled(codec_id, __VA_ARGS__)
50 #define CASE_3(config, codec_id, ...) \
51 CASE_2(config, codec_id, __VA_ARGS__)
52 #define CASE(codec, ...) \
53 CASE_3(CONFIG_ ## codec ## _ENCODER, AV_CODEC_ID_ ## codec, __VA_ARGS__)
54
59
67
71
78
79 #define FREEZE_INTERVAL 128
80
82 {
85
86 /*
87 * AMV's block size has to match that of the corresponding video
88 * stream. Relax the POT requirement.
89 */
91 (
s->block_size & (
s->block_size - 1))) {
94 }
95
97 int frontier, max_paths;
98
99 if ((
unsigned)avctx->
trellis > 16
U) {
102 }
103
108 /*
109 * The current trellis implementation doesn't work for extended
110 * runs of samples without periodic resets. Disallow it.
111 */
114 }
115
116 frontier = 1 << avctx->
trellis;
123 }
124
126
129 /* each 16 bits sample gives one nibble
130 and we have 4 bytes per channel overhead */
133 /* seems frame_size isn't taken into account...
134 have to buffer the samples :-( */
137 ) /* End of CASE */
141 ) /* End of CASE */
143 uint8_t *extradata;
144 /* each 16 bits sample gives one nibble
145 and we have 7 bytes per channel overhead */
153 bytestream_put_le16(&extradata, avctx->
frame_size);
154 bytestream_put_le16(&extradata, 7); /* wNumCoef */
155 for (
int i = 0;
i < 7;
i++) {
158 }
159 ) /* End of CASE */
163 ) /* End of CASE */
168 av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, "
169 "22050 or 44100\n");
170 return AVERROR(EINVAL);
171 }
172 avctx->
frame_size = 4096;
/* Hardcoded according to the SWF spec. */
174 ) /* End of CASE */
179 break;
182 av_log(avctx, AV_LOG_ERROR, "Sample rate must be 22050\n");
183 return AVERROR(EINVAL);
184 }
185
189 }
190
193 ) /* End of CASE */
197
201 ) /* End of CASE */
205 ) /* End of CASE */
207 /* each 16 bits sample gives one nibble */
210 ) /* End of CASE */
211 default:
213 }
214
215 return 0;
216 }
217
219 {
225
226 return 0;
227 }
228
229
232 {
240 return nibble;
241 }
242
244 {
247 const int sign = (
delta < 0) * 8;
248
251 if (sign)
253
254 nibble = sign | nibble;
255
256 c->prev_sample +=
diff;
259 return nibble;
260 }
261
264 {
267 int nibble = 8*(
delta < 0);
268
271
273 nibble |= 4;
275 }
278 nibble |= 2;
280 }
283 nibble |= 1;
285 }
287
288 if (nibble & 8)
289 c->prev_sample -=
diff;
290 else
291 c->prev_sample +=
diff;
292
295
296 return nibble;
297 }
298
301 {
303
305 ((
c->sample2) * (
c->coeff2))) / 64;
306
308 if (nibble >= 0)
309 bias =
c->idelta / 2;
310 else
311 bias = -
c->idelta / 2;
312
313 nibble = (nibble +
bias) /
c->idelta;
315
316 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) *
c->idelta;
317
318 c->sample2 =
c->sample1;
320
324
325 return nibble;
326 }
327
330 {
332
336 }
337
339
341
346
347 return nibble;
348 }
349
351 const int16_t *
samples, uint8_t *dst,
353 {
354 //FIXME 6% faster if frontier is a compile-time constant
356 const int frontier = 1 << avctx->
trellis;
361 TrellisNode **nodes = nodep_buf;
// nodes[] is always sorted by .ssd
363 int pathn = 0, froze = -1,
i, j, k, generation = 0;
364 uint8_t *
hash =
s->trellis_hash;
365 memset(
hash, 0xff, 65536 *
sizeof(*
hash));
366
367 memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
368 nodes[0] = node_buf + frontier;
371 nodes[0]->
step =
c->step_index;
380 nodes[0]->
step =
c->idelta;
383 nodes[0]->
step = 127;
385 } else {
386 nodes[0]->
step =
c->step;
388 }
389 }
390
391 for (
i = 0;
i < n;
i++) {
395 int heap_pos = 0;
396 memset(nodes_next, 0, frontier *
sizeof(
TrellisNode*));
397 for (j = 0; j < frontier && nodes[j]; j++) {
398 // higher j have higher ssd already, so they're likely
399 // to yield a suboptimal next sample too
400 const int range = (j < frontier / 2) ? 1 : 0;
401 const int step = nodes[j]->step;
402 int nidx;
404 const int predictor = ((nodes[j]->sample1 *
c->coeff1) +
405 (nodes[j]->sample2 *
c->coeff2)) / 64;
407 const int nmin =
av_clip(div-range, -8, 6);
408 const int nmax =
av_clip(div+range, -7, 7);
409 for (nidx = nmin; nidx <= nmax; nidx++) {
410 const int nibble = nidx & 0xf;
412 #define STORE_NODE(NAME, STEP_INDEX)\
413 int d;\
414 uint32_t ssd;\
415 int pos;\
416 TrellisNode *u;\
417 uint8_t *h;\
418 dec_sample = av_clip_int16(dec_sample);\
419 d = sample - dec_sample;\
420 ssd = nodes[j]->ssd + d*(unsigned)d;\
421 /* Check for wraparound, skip such samples completely. \
422 * Note, changing ssd to a 64 bit variable would be \
423 * simpler, avoiding this check, but it's slower on \
424 * x86 32 bit at the moment. */\
425 if (ssd < nodes[j]->ssd)\
426 goto next_##NAME;\
427 /* Collapse any two states with the same previous sample value. \
428 * One could also distinguish states by step and by 2nd to last
429 * sample, but the effects of that are negligible.
430 * Since nodes in the previous generation are iterated
431 * through a heap, they're roughly ordered from better to
432 * worse, but not strictly ordered. Therefore, an earlier
433 * node with the same sample value is better in most cases
434 * (and thus the current is skipped), but not strictly
435 * in all cases. Only skipping samples where ssd >=
436 * ssd of the earlier node with the same sample gives
437 * slightly worse quality, though, for some reason. */ \
438 h = &hash[(uint16_t) dec_sample];\
439 if (*h == generation)\
440 goto next_##NAME;\
441 if (heap_pos < frontier) {\
442 pos = heap_pos++;\
443 } else {\
444 /* Try to replace one of the leaf nodes with the new \
445 * one, but try a different slot each time. */\
446 pos = (frontier >> 1) +\
447 (heap_pos & ((frontier >> 1) - 1));\
448 if (ssd > nodes_next[pos]->ssd)\
449 goto next_##NAME;\
450 heap_pos++;\
451 }\
452 *h = generation;\
453 u = nodes_next[pos];\
454 if (!u) {\
455 av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
456 u = t++;\
457 nodes_next[pos] = u;\
458 u->path = pathn++;\
459 }\
460 u->ssd = ssd;\
461 u->step = STEP_INDEX;\
462 u->sample2 = nodes[j]->sample1;\
463 u->sample1 = dec_sample;\
464 paths[u->path].nibble = nibble;\
465 paths[u->path].prev = nodes[j]->path;\
466 /* Sift the newly inserted node up in the heap to \
467 * restore the heap property. */\
468 while (pos > 0) {\
469 int parent = (pos - 1) >> 1;\
470 if (nodes_next[parent]->ssd <= ssd)\
471 break;\
472 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
473 pos = parent;\
474 }\
475 next_##NAME:;
478 }
483 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
484 const int predictor = nodes[j]->sample1;\
485 const int div = (sample - predictor) * 4 / STEP_TABLE;\
486 int nmin = av_clip(div - range, -7, 6);\
487 int nmax = av_clip(div + range, -6, 7);\
488 if (nmin <= 0)\
489 nmin--; /* distinguish -0 from +0 */\
490 if (nmax < 0)\
491 nmax--;\
492 for (nidx = nmin; nidx <= nmax; nidx++) {\
493 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
494 int dec_sample = predictor +\
495 (STEP_TABLE *\
496 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
497 STORE_NODE(NAME, STEP_INDEX);\
498 }
501 } else { //AV_CODEC_ID_ADPCM_YAMAHA
504 127, 24576));
505 #undef LOOP_NODES
506 #undef STORE_NODE
507 }
508 }
509
511 nodes = nodes_next;
513
514 generation++;
515 if (generation == 255) {
516 memset(
hash, 0xff, 65536 *
sizeof(*
hash));
517 generation = 0;
518 }
519
520 // prevent overflow
521 if (nodes[0]->ssd > (1 << 28)) {
522 for (j = 1; j < frontier && nodes[j]; j++)
523 nodes[j]->ssd -= nodes[0]->ssd;
524 nodes[0]->ssd = 0;
525 }
526
527 // merge old paths to save memory
529 p = &paths[nodes[0]->path];
530 for (k =
i; k > froze; k--) {
532 p = &paths[p->prev];
533 }
535 pathn = 0;
536 // other nodes might use paths that don't coincide with the frozen one.
537 // checking which nodes do so is too slow, so just kill them all.
538 // this also slightly improves quality, but I don't know why.
539 memset(nodes + 1, 0, (frontier - 1) *
sizeof(
TrellisNode*));
540 }
541 }
542
543 p = &paths[nodes[0]->
path];
544 for (
i = n - 1;
i > froze;
i--) {
546 p = &paths[p->prev];
547 }
548
549 c->predictor = nodes[0]->sample1;
550 c->sample1 = nodes[0]->sample1;
551 c->sample2 = nodes[0]->sample2;
552 c->step_index = nodes[0]->step;
553 c->step = nodes[0]->step;
554 c->idelta = nodes[0]->step;
555 }
556
557 #if CONFIG_ADPCM_ARGO_ENCODER
560 {
561 int nibble;
562
565 else
567
568 return (nibble >>
shift) & 0x0F;
569 }
570
572 const int16_t *
samples,
int nsamples,
574 {
576
577 if (pb) {
582 }
583
584 for (
int n = 0; n < nsamples; n++) {
585 /* Compress the nibble, then expand it to see how much precision we've lost. */
588
590
591 if (pb)
593 }
594
596 }
597 #endif
598
601 {
602 int st, pkt_size,
ret;
604 const int16_t *const *samples_p;
605 uint8_t *dst;
608
610 samples_p = (
const int16_t *
const *)
frame->extended_data;
612
618 else
623
626 int blocks = (
frame->nb_samples - 1) / 8;
627
630 status->prev_sample = samples_p[ch][0];
631 /* status->step_index = 0;
632 XXX: not sure how to init the state machine */
633 bytestream_put_le16(&dst,
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 */
640 uint8_t *buf;
643 for (
int ch = 0; ch <
channels; ch++) {
645 buf + ch * blocks * 8, &
c->status[ch],
646 blocks * 8, 1);
647 }
648 for (
int i = 0;
i < blocks;
i++) {
649 for (
int ch = 0; ch <
channels; ch++) {
650 uint8_t *buf1 = buf + ch * blocks * 8 +
i * 8;
651 for (int j = 0; j < 8; j += 2)
652 *dst++ = buf1[j] | (buf1[j + 1] << 4);
653 }
654 }
656 } else {
657 for (
int i = 0;
i < blocks;
i++) {
658 for (
int ch = 0; ch <
channels; ch++) {
660 const int16_t *smp = &samples_p[ch][1 +
i * 8];
661 for (int j = 0; j < 8; j += 2) {
664 *dst++ = v;
665 }
666 }
667 }
668 }
669 ) /* End of CASE */
673
674 for (
int ch = 0; ch <
channels; ch++) {
679 uint8_t buf[64];
681 64, 1);
682 for (
int i = 0;
i < 64;
i++)
685 } else {
686 for (
int i = 0;
i < 64;
i += 2) {
692 }
693 }
694 }
695
697 ) /* End of CASE */
701
703
704 for (
int i = 0;
i <
frame->nb_samples;
i++) {
705 for (
int ch = 0; ch <
channels; ch++) {
707 }
708 }
709
711 ) /* End of CASE */
715
717
718 for (
int n =
frame->nb_samples / 2; n > 0; n--) {
719 for (
int ch = 0; ch <
channels; ch++) {
722 }
724 }
725
727 ) /* End of CASE */
729 const int n =
frame->nb_samples - 1;
732
733 /* NB: This is safe as we don't have AV_CODEC_CAP_SMALL_LAST_FRAME. */
735
736 // store AdpcmCodeSize
737 put_bits(&pb, 2, 2);
// set 4-bit flash adpcm format
738
739 // init the encoder state
741 // clip step so it fits 6 bits
746 }
747
749 uint8_t buf[8190 /* = 2 * n */];
754 buf + n, &
c->status[1], n,
756 for (
int i = 0;
i < n;
i++) {
760 }
761 } else {
762 for (
int i = 1;
i <
frame->nb_samples;
i++) {
768 }
769 }
771 ) /* End of CASE */
778 }
780 if (
c->status[
i].idelta < 16)
781 c->status[
i].idelta = 16;
782 bytestream_put_le16(&dst,
c->status[
i].idelta);
783 }
788 bytestream_put_le16(&dst,
c->status[
i].sample1);
789 }
791 bytestream_put_le16(&dst,
c->status[
i].sample2);
792
796 if (!buf)
801 for (
int i = 0;
i < n;
i += 2)
802 *dst++ = (buf[
i] << 4) | buf[
i + 1];
803 } else {
808 for (
int i = 0;
i < n;
i++)
809 *dst++ = (buf[
i] << 4) | buf[n +
i];
810 }
812 } else {
814 int nibble;
817 *dst++ = nibble;
818 }
819 }
820 ) /* End of CASE */
822 int n =
frame->nb_samples / 2;
825 if (!buf)
827 n *= 2;
831 for (
int i = 0;
i < n;
i += 2)
832 *dst++ = buf[
i] | (buf[
i + 1] << 4);
833 } else {
838 for (
int i = 0;
i < n;
i++)
839 *dst++ = buf[
i] | (buf[n +
i] << 4);
840 }
842 } else
844 int nibble;
847 *dst++ = nibble;
848 }
849 ) /* End of CASE */
853
855
856 for (
int n =
frame->nb_samples / 2; n > 0; n--) {
857 for (
int ch = 0; ch <
channels; ch++) {
860 }
862 }
863
865 ) /* End of CASE */
868
869 c->status[0].prev_sample = *
samples;
870 bytestream_put_le16(&dst,
c->status[0].prev_sample);
871 bytestream_put_byte(&dst,
c->status[0].step_index);
872 bytestream_put_byte(&dst, 0);
874
876 const int n =
frame->nb_samples >> 1;
878
879 if (!buf)
881
883 for (
int i = 0;
i < n;
i++)
884 bytestream_put_byte(&dst, (buf[2 *
i] << 4) | buf[2 *
i + 1]);
885
888 }
else for (
int n =
frame->nb_samples >> 1; n > 0; n--) {
889 int nibble;
892 bytestream_put_byte(&dst, nibble);
893 }
894
897 bytestream_put_byte(&dst, nibble);
898 }
899 ) /* End of CASE */
903
905
906 for (
int ch = 0; ch <
channels; ch++) {
907 int64_t
error = INT64_MAX, tmperr = INT64_MAX;
909 int saved1 =
c->status[ch].sample1;
910 int saved2 =
c->status[ch].sample2;
911
912 /* Find the optimal coefficients, bail early if we find a perfect result. */
913 for (
int s = 2;
s < 18 && tmperr != 0;
s++) {
914 for (
int f = 0;
f < 2 && tmperr != 0;
f++) {
915 c->status[ch].sample1 = saved1;
916 c->status[ch].sample2 = saved2;
917 tmperr = adpcm_argo_compress_block(
c->status + ch,
NULL, samples_p[ch],
919 if (tmperr <
error) {
923 }
924 }
925 }
926
927 /* Now actually do the encode. */
928 c->status[ch].sample1 = saved1;
929 c->status[ch].sample2 = saved2;
930 adpcm_argo_compress_block(
c->status + ch, &pb, samples_p[ch],
932 }
933
935 ) /* End of CASE */
939
941 for (
int n =
frame->nb_samples / 2; n > 0; n--) {
942 /* stereo: 1 byte (2 samples) for left, 1 byte for right */
943 for (
int ch = 0; ch <
channels; ch++) {
949 }
953 ) /* End of CASE */
954 default:
956 }
957
958 *got_packet_ptr = 1;
959 return 0;
960 }
964 };
965
968 };
969
973 { 0 },
974 };
977 {
978 .
name =
"block_size",
979 .help = "set the block size",
982 .default_val = {.i64 = 1024},
984 .max = 8192, /* Is this a reasonable upper limit? */
986 },
988 };
989
995 };
996
997 #define ADPCM_ENCODER_0(id_, name_, sample_fmts_, capabilities_, long_name_)
998 #define ADPCM_ENCODER_1(id_, name_, sample_fmts_, capabilities_, long_name_) \
999 const FFCodec ff_ ## name_ ## _encoder = { \
1001 CODEC_LONG_NAME(long_name_), \
1002 .p.type = AVMEDIA_TYPE_AUDIO, \
1003 .p.id = id_, \
1004 .p.sample_fmts = sample_fmts_, \
1005 .p.ch_layouts = ch_layouts, \
1006 .p.capabilities = capabilities_ | AV_CODEC_CAP_DR1 | \
1007 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, \
1008 .p.priv_class = &adpcm_encoder_class, \
1009 .priv_data_size = sizeof(ADPCMEncodeContext), \
1010 .init = adpcm_encode_init, \
1011 FF_CODEC_ENCODE_CB(adpcm_encode_frame), \
1012 .close = adpcm_encode_close, \
1013 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
1014 };
1015 #define ADPCM_ENCODER_2(enabled, codec_id, name, sample_fmts, capabilities, long_name) \
1016 ADPCM_ENCODER_ ## enabled(codec_id, name, sample_fmts, capabilities, long_name)
1017 #define ADPCM_ENCODER_3(config, codec_id, name, sample_fmts, capabilities, long_name) \
1018 ADPCM_ENCODER_2(config, codec_id, name, sample_fmts, capabilities, long_name)
1019 #define ADPCM_ENCODER(codec, name, sample_fmts, capabilities, long_name) \
1020 ADPCM_ENCODER_3(CONFIG_ ## codec ## _ENCODER, AV_CODEC_ID_ ## codec, \
1021 name, sample_fmts, capabilities, long_name)
1022