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
31
32 /**
33 * @file
34 * ADPCM encoders
35 * See ADPCM decoder reference documents for codec information.
36 */
37
42
50
58
59 #define FREEZE_INTERVAL 128
60
62
64 {
67 int i;
69
73 }
74
78 }
79
81 int frontier = 1 << avctx->
trellis;
84 max_paths *
sizeof(*s->
paths), error);
86 2 * frontier *
sizeof(*s->
node_buf), error);
88 2 * frontier *
sizeof(*s->
nodep_buf), error);
91 }
92
94
97 /* each 16 bits sample gives one nibble
98 and we have 4 bytes per channel overhead */
101 /* seems frame_size isn't taken into account...
102 have to buffer the samples :-( */
105 break;
109 break;
111 /* each 16 bits sample gives one nibble
112 and we have 7 bytes per channel overhead */
117 goto error;
120 bytestream_put_le16(&extradata, avctx->
frame_size);
121 bytestream_put_le16(&extradata, 7); /* wNumCoef */
122 for (i = 0; i < 7; i++) {
125 }
126 break;
130 break;
136 "22050 or 44100\n");
138 goto error;
139 }
141 break;
142 default:
144 goto error;
145 }
146
147 return 0;
148 error:
151 }
152
154 {
160
161 return 0;
162 }
163
164
167 {
169 int nibble =
FFMIN(7, abs(delta) * 4 /
175 return nibble;
176 }
177
180 {
183 int nibble = 8*(delta < 0);
184
185 delta= abs(delta);
186 diff = delta + (step >> 3);
187
188 if (delta >= step) {
189 nibble |= 4;
190 delta -= step;
191 }
192 step >>= 1;
193 if (delta >= step) {
194 nibble |= 2;
195 delta -= step;
196 }
197 step >>= 1;
198 if (delta >= step) {
199 nibble |= 1;
200 delta -= step;
201 }
203
204 if (nibble & 8)
206 else
208
211
212 return nibble;
213 }
214
217 {
219
222
224 if (nibble >= 0)
226 else
228
229 nibble = (nibble + bias) / c->
idelta;
230 nibble = av_clip(nibble, -8, 7) & 0x0F;
231
232 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->
idelta;
233
235 c->
sample1 = av_clip_int16(predictor);
236
240
241 return nibble;
242 }
243
246 {
248
252 }
253
255
256 nibble =
FFMIN(7, abs(delta) * 4 / c->
step) + (delta < 0) * 8;
257
261 c->
step = av_clip(c->
step, 127, 24567);
262
263 return nibble;
264 }
265
267 const int16_t *samples,
uint8_t *dst,
269 {
270 //FIXME 6% faster if frontier is a compile-time constant
272 const int frontier = 1 << avctx->
trellis;
277 TrellisNode **nodes = nodep_buf;
// nodes[] is always sorted by .ssd
279 int pathn = 0, froze = -1, i, j, k, generation = 0;
281 memset(hash, 0xff, 65536 * sizeof(*hash));
282
283 memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
284 nodes[0] = node_buf + frontier;
298 nodes[0]->
step = 127;
300 } else {
303 }
304 }
305
306 for (i = 0; i <
n; i++) {
309 int sample = samples[i * stride];
310 int heap_pos = 0;
311 memset(nodes_next, 0, frontier *
sizeof(
TrellisNode*));
312 for (j = 0; j < frontier && nodes[j]; j++) {
313 // higher j have higher ssd already, so they're likely
314 // to yield a suboptimal next sample too
315 const int range = (j < frontier / 2) ? 1 : 0;
316 const int step = nodes[j]->step;
317 int nidx;
320 (nodes[j]->sample2 * c->
coeff2)) / 64;
321 const int div = (sample -
predictor) / step;
322 const int nmin = av_clip(div-range, -8, 6);
323 const int nmax = av_clip(div+range, -7, 7);
324 for (nidx = nmin; nidx <= nmax; nidx++) {
325 const int nibble = nidx & 0xf;
326 int dec_sample = predictor + nidx * step;
327 #define STORE_NODE(NAME, STEP_INDEX)\
328 int d;\
329 uint32_t ssd;\
330 int pos;\
331 TrellisNode *u;\
332 uint8_t *h;\
333 dec_sample = av_clip_int16(dec_sample);\
334 d = sample - dec_sample;\
335 ssd = nodes[j]->ssd + d*(unsigned)d;\
336 /* Check for wraparound, skip such samples completely. \
337 * Note, changing ssd to a 64 bit variable would be \
338 * simpler, avoiding this check, but it's slower on \
339 * x86 32 bit at the moment. */\
340 if (ssd < nodes[j]->ssd)\
341 goto next_##NAME;\
342 /* Collapse any two states with the same previous sample value. \
343 * One could also distinguish states by step and by 2nd to last
344 * sample, but the effects of that are negligible.
345 * Since nodes in the previous generation are iterated
346 * through a heap, they're roughly ordered from better to
347 * worse, but not strictly ordered. Therefore, an earlier
348 * node with the same sample value is better in most cases
349 * (and thus the current is skipped), but not strictly
350 * in all cases. Only skipping samples where ssd >=
351 * ssd of the earlier node with the same sample gives
352 * slightly worse quality, though, for some reason. */ \
353 h = &hash[(uint16_t) dec_sample];\
354 if (*h == generation)\
355 goto next_##NAME;\
356 if (heap_pos < frontier) {\
357 pos = heap_pos++;\
358 } else {\
359 /* Try to replace one of the leaf nodes with the new \
360 * one, but try a different slot each time. */\
361 pos = (frontier >> 1) +\
362 (heap_pos & ((frontier >> 1) - 1));\
363 if (ssd > nodes_next[pos]->ssd)\
364 goto next_##NAME;\
365 heap_pos++;\
366 }\
367 *h = generation;\
368 u = nodes_next[pos];\
369 if (!u) {\
370 av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
371 u = t++;\
372 nodes_next[pos] = u;\
373 u->path = pathn++;\
374 }\
375 u->ssd = ssd;\
376 u->step = STEP_INDEX;\
377 u->sample2 = nodes[j]->sample1;\
378 u->sample1 = dec_sample;\
379 paths[u->path].nibble = nibble;\
380 paths[u->path].prev = nodes[j]->path;\
381 /* Sift the newly inserted node up in the heap to \
382 * restore the heap property. */\
383 while (pos > 0) {\
384 int parent = (pos - 1) >> 1;\
385 if (nodes_next[parent]->ssd <= ssd)\
386 break;\
387 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
388 pos = parent;\
389 }\
390 next_##NAME:;
393 }
397 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
398 const int predictor = nodes[j]->sample1;\
399 const int div = (sample - predictor) * 4 / STEP_TABLE;\
400 int nmin = av_clip(div - range, -7, 6);\
401 int nmax = av_clip(div + range, -6, 7);\
402 if (nmin <= 0)\
403 nmin--; /* distinguish -0 from +0 */\
404 if (nmax < 0)\
405 nmax--;\
406 for (nidx = nmin; nidx <= nmax; nidx++) {\
407 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
408 int dec_sample = predictor +\
409 (STEP_TABLE *\
410 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
411 STORE_NODE(NAME, STEP_INDEX);\
412 }
415 } else { //AV_CODEC_ID_ADPCM_YAMAHA
418 127, 24567));
419 #undef LOOP_NODES
420 #undef STORE_NODE
421 }
422 }
423
424 u = nodes;
425 nodes = nodes_next;
427
428 generation++;
429 if (generation == 255) {
430 memset(hash, 0xff, 65536 * sizeof(*hash));
431 generation = 0;
432 }
433
434 // prevent overflow
435 if (nodes[0]->ssd > (1 << 28)) {
436 for (j = 1; j < frontier && nodes[j]; j++)
437 nodes[j]->ssd -= nodes[0]->ssd;
438 nodes[0]->ssd = 0;
439 }
440
441 // merge old paths to save memory
443 p = &paths[nodes[0]->path];
444 for (k = i; k > froze; k--) {
446 p = &paths[p->prev];
447 }
448 froze = i;
449 pathn = 0;
450 // other nodes might use paths that don't coincide with the frozen one.
451 // checking which nodes do so is too slow, so just kill them all.
452 // this also slightly improves quality, but I don't know why.
453 memset(nodes + 1, 0, (frontier - 1) *
sizeof(
TrellisNode*));
454 }
455 }
457 p = &paths[nodes[0]->
path];
458 for (i = n - 1; i > froze; i--) {
460 p = &paths[p->prev];
461 }
462
464 c->
sample1 = nodes[0]->sample1;
465 c->
sample2 = nodes[0]->sample2;
467 c->
step = nodes[0]->step;
468 c->
idelta = nodes[0]->step;
469 }
470
473 {
474 int n, i, ch, st, pkt_size,
ret;
475 const int16_t *samples;
476 int16_t **samples_p;
480
481 samples = (
const int16_t *)frame->
data[0];
484
487 else
492
495 {
496 int blocks, j;
497
499
500 for (ch = 0; ch < avctx->
channels; ch++) {
503 /* status->step_index = 0;
504 XXX: not sure how to init the state machine */
507 *dst++ = 0; /* unknown */
508 }
509
510 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
513 for (ch = 0; ch < avctx->
channels; ch++) {
515 buf + ch * blocks * 8, &c->
status[ch],
516 blocks * 8, 1);
517 }
518 for (i = 0; i < blocks; i++) {
519 for (ch = 0; ch < avctx->
channels; ch++) {
520 uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
521 for (j = 0; j < 8; j += 2)
522 *dst++ = buf1[j] | (buf1[j + 1] << 4);
523 }
524 }
526 } else {
527 for (i = 0; i < blocks; i++) {
528 for (ch = 0; ch < avctx->
channels; ch++) {
530 const int16_t *smp = &samples_p[ch][1 + i * 8];
531 for (j = 0; j < 8; j += 2) {
535 }
536 }
537 }
538 }
539 break;
540 }
542 {
545
546 for (ch = 0; ch < avctx->
channels; ch++) {
553 64, 1);
554 for (i = 0; i < 64; i++)
557 } else {
558 for (i = 0; i < 64; i += 2) {
564 }
565 }
566 }
567
569 break;
570 }
572 {
575
577
578 // store AdpcmCodeSize
579 put_bits(&pb, 2, 2);
// set 4-bit flash adpcm format
580
581 // init the encoder state
582 for (i = 0; i < avctx->
channels; i++) {
583 // clip step so it fits 6 bits
588 }
589
596 buf + n, &c->
status[1], n,
598 for (i = 0; i <
n; i++) {
602 }
604 } else {
610 samples[2 * i + 1]));
611 }
612 }
614 break;
615 }
617 for (i = 0; i < avctx->
channels; i++) {
622 }
623 for (i = 0; i < avctx->
channels; i++) {
627 }
628 for (i = 0; i < avctx->
channels; i++)
633 }
634 for (i = 0; i < avctx->
channels; i++)
636
643 for (i = 0; i <
n; i += 2)
644 *dst++ = (buf[i] << 4) | buf[i + 1];
645 } else {
650 for (i = 0; i <
n; i++)
651 *dst++ = (buf[i] << 4) | buf[n + i];
652 }
654 } else {
655 for (i = 7 * avctx->
channels; i < avctx->block_align; i++) {
656 int nibble;
659 *dst++ = nibble;
660 }
661 }
662 break;
667 n *= 2;
671 for (i = 0; i <
n; i += 2)
672 *dst++ = buf[i] | (buf[i + 1] << 4);
673 } else {
678 for (i = 0; i <
n; i++)
679 *dst++ = buf[i] | (buf[n + i] << 4);
680 }
682 } else
683 for (n *= avctx->
channels; n > 0; n--) {
684 int nibble;
687 *dst++ = nibble;
688 }
689 break;
692 }
693
695 *got_packet_ptr = 1;
696 return 0;
697 error:
699 }
700
703 };
704
707 };
708
709 #define ADPCM_ENCODER(id_, name_, sample_fmts_, long_name_) \
710 AVCodec ff_ ## name_ ## _encoder = { \
711 .name = #name_, \
712 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
713 .type = AVMEDIA_TYPE_AUDIO, \
714 .id = id_, \
715 .priv_data_size = sizeof(ADPCMEncodeContext), \
716 .init = adpcm_encode_init, \
717 .encode2 = adpcm_encode_frame, \
718 .close = adpcm_encode_close, \
719 .sample_fmts = sample_fmts_, \
720 }
721