1/* 2** Copyright (C) 1999-2014 Erik de Castro Lopo <erikd@mega-nerd.com> 3** Copyright (C) 2017 Arthur Taylor <art@ified.ca> 4** 5** This program is free software; you can redistribute it and/or modify 6** it under the terms of the GNU Lesser General Public License as published by 7** the Free Software Foundation; either version 2.1 of the License, or 8** (at your option) any later version. 9** 10** This program is distributed in the hope that it will be useful, 11** but WITHOUT ANY WARRANTY; without even the implied warranty of 12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13** GNU Lesser General Public License for more details. 14** 15** You should have received a copy of the GNU Lesser General Public License 16** along with this program; if not, write to the Free Software 17** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18*/ 19 20/* 21** This is a Natural MicroSystems ADPCM encoder/decoder. It converts 14 bit linear 22** PCM to and from either a 2, 3, or 4 bit ADPCM. NMS-ADPCM does not have appeared 23** to have ever been publicly documented, and appears to have debuted in the early 24** 90s in the Natural Access suite of PC-based telephony products. Raw NMS ADPCM 25** files usually have a .vce extension, although this does not encode what bitrate 26** is used. 27** 28** NMS-ADPCM is an 'optimised variation' of the ITU G.726 ADPCM scheme. The dominant 29** variation is that it removes the tone (modem) operation mode, and it's associated 30** voice/modem transition detection. This simplifies the computation of the step 31** size multiplier, as all operations on it remain in a log domain. 32*/ 33 34#include "sfconfig.h" 35 36#include <math.h> 37 38#include "sndfile.h" 39#include "sfendian.h" 40#include "common.h" 41 42 43#define NMS_SAMPLES_PER_BLOCK 160 44#define NMS_BLOCK_SHORTS_32 41 45#define NMS_BLOCK_SHORTS_24 31 46#define NMS_BLOCK_SHORTS_16 21 47 48/* Variable names from ITU G.726 spec */ 49struct nms_adpcm_state 50{ /* Log of the step size multiplier. Operated on by codewords. */ 51 int yl ; 52 53 /* Quantizer step size multiplier. Generated from yl. */ 54 int y ; 55 56 /* Coefficents of the pole predictor */ 57 int a [2] ; 58 59 /* Coefficents of the zero predictor */ 60 int b [6] ; 61 62 /* Previous quantized deltas (multiplied by 2^14) */ 63 int d_q [7] ; 64 65 /* d_q [x] + s_ez [x], used by the pole-predictor for signs only. */ 66 int p [3] ; 67 68 /* Previous reconstructed signal values. */ 69 int s_r [2] ; 70 71 /* Zero predictor components of the signal estimate. */ 72 int s_ez ; 73 74 /* Signal estimate, (including s_ez). */ 75 int s_e ; 76 77 /* The most recent codeword (enc:generated, dec:inputted) */ 78 int Ik ; 79 80 int parity ; 81 82 /* 83 ** Offset into code tables for the bitrate. 84 ** 2-bit words: +0 85 ** 3-bit words: +8 86 ** 4-bit words: +16 87 */ 88 int t_off ; 89} ; 90 91enum nms_enc_type 92{ NMS16, 93 NMS24, 94 NMS32 95} ; 96 97typedef struct 98{ struct nms_adpcm_state state ; 99 100 /* The encoding type */ 101 enum nms_enc_type type ; 102 103 int shortsperblock ; 104 int blocks_total ; 105 int block_curr, sample_curr ; 106 107 unsigned short block [NMS_BLOCK_SHORTS_32] ; 108 short samples [NMS_SAMPLES_PER_BLOCK] ; 109} NMS_ADPCM_PRIVATE ; 110 111/* Pre-computed exponential interval used in the antilog approximation. */ 112static unsigned int table_expn [] = 113{ 0x4000, 0x4167, 0x42d5, 0x444c, 0x45cb, 0x4752, 0x48e2, 0x4a7a, 114 0x4c1b, 0x4dc7, 0x4f7a, 0x5138, 0x52ff, 0x54d1, 0x56ac, 0x5892, 115 0x5a82, 0x5c7e, 0x5e84, 0x6096, 0x62b4, 0x64dd, 0x6712, 0x6954, 116 0x6ba2, 0x6dfe, 0x7066, 0x72dc, 0x7560, 0x77f2, 0x7a93, 0x7d42, 117} ; 118 119/* Table mapping codewords to scale factor deltas. */ 120static int table_scale_factor_step [] = 121{ 0x0, 0x0, 0x0, 0x0, 0x4b0, 0x0, 0x0, 0x0, /* 2-bit */ 122 -0x3c, 0x0, 0x90, 0x0, 0x2ee, 0x0, 0x898, 0x0, /* 3-bit */ 123 -0x30, 0x12, 0x6b, 0xc8, 0x188, 0x2e0, 0x551, 0x1150, /* 4-bit */ 124} ; 125 126/* Table mapping codewords to quantized delta interval steps. */ 127static unsigned int table_step [] = 128{ 0x73F, 0, 0, 0, 0x1829, 0, 0, 0, /* 2-bit */ 129 0x3EB, 0, 0xC18, 0, 0x1581, 0, 0x226E, 0, /* 3-bit */ 130 0x20C, 0x635, 0xA83, 0xF12, 0x1418, 0x19E3, 0x211A, 0x2BBA, /* 4-bit */ 131} ; 132 133/* Binary search lookup table for quantizing using table_step. */ 134static int table_step_search [] = 135{ 0, 0x1F6D, 0, -0x1F6D, 0, 0, 0, 0, /* 2-bit */ 136 0x1008, 0x1192, 0, -0x219A, 0x1656, -0x1656, 0, 0, /* 3-bit */ 137 0x872, 0x1277, -0x8E6, -0x232B, 0xD06, -0x17D7, -0x11D3, 0, /* 4-bit */ 138} ; 139 140 141/*============================================================================================ 142** Static functions. 143*/ 144 145static void nms_adpcm_update (struct nms_adpcm_state *s) ; 146static void nms_adpcm_codec_init (struct nms_adpcm_state *s, enum nms_enc_type type) ; 147 148static int16_t nms_adpcm_reconstruct_sample (struct nms_adpcm_state *s, uint8_t I) ; 149static uint8_t nms_adpcm_encode_sample (struct nms_adpcm_state *s, int16_t sl) ; 150static int16_t nms_adpcm_decode_sample (struct nms_adpcm_state *s, uint8_t code) ; 151 152static void nms_adpcm_block_pack_16 (const int16_t codewords [], uint16_t block [], int16_t rms) ; 153static void nms_adpcm_block_pack_24 (const int16_t codewords [], uint16_t block [], int16_t rms) ; 154static void nms_adpcm_block_pack_32 (const int16_t codewords [], uint16_t block [], int16_t rms) ; 155 156static void nms_adpcm_block_unpack_16 (const uint16_t block [], int16_t codewords [], int16_t *rms) ; 157static void nms_adpcm_block_unpack_24 (const uint16_t block [], int16_t codewords [], int16_t *rms) ; 158static void nms_adpcm_block_unpack_32 (const uint16_t block [], int16_t codewords [], int16_t *rms) ; 159 160static int nms_adpcm_decode_block (SF_PRIVATE *psf, NMS_ADPCM_PRIVATE *pnms, uint16_t block [], int16_t samples []) ; 161static int nms_adpcm_encode_block (SF_PRIVATE *psf, NMS_ADPCM_PRIVATE *pnms, int16_t samples [], uint16_t block []) ; 162 163static sf_count_t nms_adpcm_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ; 164static sf_count_t nms_adpcm_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ; 165static sf_count_t nms_adpcm_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ; 166static sf_count_t nms_adpcm_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ; 167 168static sf_count_t nms_adpcm_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ; 169static sf_count_t nms_adpcm_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ; 170static sf_count_t nms_adpcm_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ; 171static sf_count_t nms_adpcm_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ; 172 173static int nms_adpcm_close (SF_PRIVATE *psf) ; 174static sf_count_t nms_adpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ; 175 176/* 177** An exponential function (antilog) approximation. 178** 179** Maps [1,20480] to [1,1024] in an exponential relationship. This is 180** approximately ret = b^exp where b = e^(ln(1024)/ln(20480)) ~= 1.0003385 181*/ 182static inline int 183nms_adpcm_antilog (int exp) 184{ int ret ; 185 186 ret = 0x1000 ; 187 ret += (((exp & 0x3f) * 0x166b) >> 12) ; 188 ret *= table_expn [(exp & 0x7c0) >> 6] ; 189 ret >>= (26 - (exp >> 11)) ; 190 191 return ret ; 192} /* nms_adpcm_antilog */ 193 194static void 195nms_adpcm_update (struct nms_adpcm_state *s) 196{ /* Variable names from ITU G.726 spec */ 197 int a1ul ; 198 int fa1 ; 199 int i ; 200 201 /* Decay and Modify the scale factor in the log domain based on the codeword. */ 202 s->yl = ((s->yl *0xf8) >> 8) + table_scale_factor_step [s->t_off + (s->Ik & 7)] ; 203 if (s->yl < 2171) 204 s->yl = 2171 ; 205 else if (s->yl > 20480) 206 s->yl = 20480 ; 207 s->y = nms_adpcm_antilog (s->yl) ; 208 209 /* Update the zero predictor coefficents. */ 210 for (i = 0 ; i < 6 ; i++) 211 { s->b [i] = (s->b [i] * 0xff) >> 8 ; 212 if ((s->d_q [0] ^ s->d_q [i + 1]) >= 0) 213 s->b [i] += 128 ; 214 else 215 s->b [i] -= 128 ; 216 } 217 218 /* Update the pole predictor coefficents. */ 219 fa1 = s->a [0] >> 5 ; 220 if (fa1 < -256) 221 fa1 = -256 ; 222 else if (fa1 > 256) 223 fa1 = 256 ; 224 225 s->a [0] = (0xff * s->a [0]) >> 8 ; 226 if (s->p [0] != 0 && s->p [1] != 0 && ((s->p [0] ^ s->p [1]) < 0)) 227 s->a [0] -= 192 ; 228 else 229 { s->a [0] += 192 ; 230 fa1 = -fa1 ; 231 } 232 233 s->a [1] = fa1 + ((0xfe * s->a [1]) >> 8) ; 234 if (s->p [0] != 0 && s->p [2] != 0 && ((s->p [0] ^ s->p [2]) < 0)) 235 s->a [1] -= 128 ; 236 else 237 s->a [1] += 128 ; 238 239 /* Stability constraints. */ 240 if (s->a [1] < -12288) 241 s->a [1] = -12288 ; 242 else if (s->a [1] > 12288) 243 s->a [1] = 12288 ; 244 a1ul = 15360 - s->a [1] ; 245 if (s->a [0] >= a1ul) 246 s->a [0] = a1ul ; 247 else 248 { a1ul = -a1ul ; 249 if (s->a [0] < a1ul) 250 s->a [0] = a1ul ; 251 } ; 252 253 /* Compute the zero predictor estimate. Rotate past deltas too. */ 254 s->s_ez = 0 ; 255 for (i = 5 ; i >= 0 ; i--) 256 { s->s_ez += s->d_q [i] * s->b [i] ; 257 s->d_q [i + 1] = s->d_q [i] ; 258 } ; 259 260 /* Compute the signal estimate. */ 261 s->s_e = s->a [0] * s->s_r [0] + s->a [1] * s->s_r [1] + s->s_ez ; 262 263 /* Return to scale */ 264 s->s_ez >>= 14 ; 265 s->s_e >>= 14 ; 266 267 /* Rotate members to prepare for next iteration. */ 268 s->s_r [1] = s->s_r [0] ; 269 s->p [2] = s->p [1] ; 270 s->p [1] = s->p [0] ; 271} /* nms_adpcm_update */ 272 273 274static int16_t 275nms_adpcm_reconstruct_sample (struct nms_adpcm_state *s, uint8_t I) 276{ /* Variable names from ITU G.726 spec */ 277 int dqx ; 278 279 /* 280 ** The ordering of the 12-bit right-shift is a precision loss. It agrees 281 ** with the output of a 16-bit NMSVCE.DLL, but disagrees with the output 282 ** of a CG6565 board. 283 */ 284 285 /* Look up the delta, scale and sign it. */ 286 dqx = table_step [s->t_off + (I & 7)] * s->y ; 287 if (I & 8) 288 dqx = -dqx ; 289 290 /* Take from delta scale to actual scale. */ 291 dqx >>= 12 ; 292 293 /* Set variables used as input for the next predictor update. */ 294 s->d_q [0] = dqx ; 295 s->s_r [0] = s->s_e + dqx ; 296 s->Ik = I & 0xf ; 297 s->p [0] = s->s_ez + dqx ; 298 299 return s->s_r [0] ; 300} /* nms_adpcm_reconstruct_sample */ 301 302static void 303nms_adpcm_codec_init (struct nms_adpcm_state *s, enum nms_enc_type type) 304{ memset (s, 0, sizeof (struct nms_adpcm_state)) ; 305 s->t_off = (type == NMS32) ? 16 : (type == NMS24) ? 8 : 0 ; 306} /* nms_adpcm_codec_init */ 307 308/* 309** nms_adpcm_encode_sample() 310** 311** Encode a linear 16-bit pcm sample into a 2,3, or 4 bit NMS-ADPCM codeword 312** using and updating the predictor state. 313*/ 314static uint8_t 315nms_adpcm_encode_sample (struct nms_adpcm_state *s, int16_t sl) 316{ /* Variable names from ITU G.726 spec */ 317 int d ; 318 uint8_t I ; 319 320 /* Down scale the sample from 16 => ~14 bits. */ 321 sl = (sl * 0x1fdf) / 0x7fff ; 322 323 /* Compute estimate, and delta from actual value */ 324 nms_adpcm_update (s) ; 325 d = sl - s->s_e ; 326 327 /* 328 ** Vary the input signal. Not sure why. It agrees with NMSVCE.DLL and 329 ** a CG6565 board. 330 */ 331 if (s->parity ^= 1) 332 d -= 2 ; 333 334 /* Encode the delta signed-ness (Codeword bit 4) */ 335 if (d < 0) 336 { d = -d ; 337 I = 8 ; 338 } 339 else 340 I = 0 ; 341 342 /* Increase magnitude to be in the range of the delta steps */ 343 d <<= 13 ; 344 345 /* Quantize the delta using a binary search. */ 346 d += table_step_search [s->t_off + 3] * s->y ; 347 /* Codeword bit 3 */ 348 if (d >= 0) 349 { d += table_step_search [s->t_off + 5] * s->y ; 350 /* Codeword bit 2 */ 351 if (d >= 0) 352 { d += table_step_search [s->t_off + 6] * s->y ; 353 /* Codeword bit 1 */ 354 if (d >= 0) 355 I |= 7 ; 356 else 357 I |= 6 ; 358 } 359 else 360 { d += table_step_search [s->t_off + 4] * s->y ; 361 /* Codeword bit 1 */ 362 if (d >= 0) 363 I |= 5 ; 364 else 365 I |= 4 ; 366 } ; 367 } 368 else { 369 d += table_step_search [s->t_off + 1] * s->y ; 370 /* Codeword bit 2 */ 371 if (d >= 0) 372 { d += table_step_search [s->t_off + 2] * s->y ; 373 /* Codeword bit 1 */ 374 if (d >= 0) 375 I |= 3 ; 376 else 377 I |= 2 ; 378 } 379 else { 380 d += table_step_search [s->t_off + 0] * s->y ; 381 /* Codeword bit 1 */ 382 if (d >= 0) 383 I |= 1 ; 384 else 385 I |= 0 ; 386 } ; 387 } ; 388 /* What's left in d is actually our quantizer noise. */ 389 390 /* Reduce the codeword size for the bitrate accordingly. */ 391 if (s->t_off == 8) 392 I &= 0xe ; 393 else if (s->t_off == 0) 394 I &= 0xc ; 395 396 /* Call reconstruct for side effects preparing for the next update. */ 397 nms_adpcm_reconstruct_sample (s, I) ; 398 399 return I ; 400} /* nms_adpcm_encode_sample */ 401 402/* 403** nms_adpcm_decode_sample() 404** 405** Given a 2,3 or 4-bit NMS-ADPCM codeword, decode the next 16-bit linear PCM 406** sample using and updating the predictor state. 407*/ 408static int16_t 409nms_adpcm_decode_sample (struct nms_adpcm_state *s, uint8_t I) 410{ int sl ; 411 412 nms_adpcm_update (s) ; 413 sl = nms_adpcm_reconstruct_sample (s, I) ; 414 415 /* Clamp to [-0x1fdf, 0x1fdf] (just under 14 bits resolution) */ 416 if (sl < -0x1fdf) 417 sl = -0x1fdf ; 418 else if (sl > 0x1fdf) 419 sl = 0x1fdf ; 420 421 /* Expand from 14 to 16 bits */ 422 sl = (sl * 0x7fff) / 0x1fdf ; 423 424 return (int16_t) sl ; 425} /* nms_adpcm_decode_sample */ 426 427/** 428** NMS ADPCM Codeword packing scheme. 429** 430** The serialized form of NMS-ADPCM operates on blocks of 160 mono samples 431** (20ms at 8000Hz.) Blocks are 42, 62 and 82 bytes in size for the 2, 3, and 432** 4 bit codeword sizes respectively. The data is treated as an array of 433** little-endian 2-byte shorts, and the data is packed into the first 20, 30 434** or 40 shorts. The last short represents the block's root-mean-square 435** average. This is apparently an optimization so that energy/silence 436** detection processes can avoid decoding a block. 437** 438** All codewords are nibbles, with the least significant bits dropped as 439** required for the 3 and 2 bit codeword sizes. 440** 441** Nibbles are packed into shorts in order of most significant to least. The 442** 4-bit scheme is trivial. The three bit scheme reconstructs a fourth sample 443** from the leftover bits of the proceeding three samples. The 2-bit scheme 444** uses a two-pass, left two bit shift. 445*/ 446 447/* 448** Reads 21 shorts from block, unpacks 160 codewords of 2-bits each, writing 449** each to its sequential array index of codewords. If rms is non-null, the 450** read block rms is copied to its location. 451*/ 452static void 453nms_adpcm_block_unpack_16 (const uint16_t block [], int16_t codewords [], int16_t *rms) 454{ int k ; 455 uint16_t w = 0 ; 456 457 for (k = 0 ; k < NMS_SAMPLES_PER_BLOCK ; ) 458 { /* 459 ** k % 8 == [0-3]: Top 2-bits of a nibble 460 ** k % 8 == [4-7]: Bottom 2-bits of a nibble 461 */ 462 if ((k & 4) == 0) 463 w = *(block++) ; 464 else 465 w <<= 2 ; 466 codewords [k++] = (w >> 12) & 0xc ; 467 codewords [k++] = (w >> 8) & 0xc ; 468 codewords [k++] = (w >> 4) & 0xc ; 469 codewords [k++] = w & 0xc ; 470 } ; 471 472 /* 473 ** Every block ends with a short representing a RMS-approximation for the 474 ** block. 475 **/ 476 if (rms) 477 *rms = *block ; 478} /* nms_adpcm_unpack_16 */ 479 480/* 481** Reads 31 shorts from block, unpacks 160 codewords of 3-bits each, writing 482** each to its sequential array index of codewords. If rms is non-null, the 483** read block rms is copied to its location. 484*/ 485static void 486nms_adpcm_block_unpack_24 (const uint16_t block [], int16_t codewords [], int16_t *rms) 487{ int k ; 488 uint16_t w = 0, residual = 0 ; 489 490 for (k = 0 ; k < NMS_SAMPLES_PER_BLOCK ; ) 491 { /* 492 ** k % 16 == [0, 11]: Unpack new nibble, build residual 493 ** k % 16 == [12, 15]: Unpack residual 494 */ 495 if ((k & 12) != 12) 496 { w = *(block++) ; 497 residual = (residual << 1) | (w & 0x1111) ; 498 } 499 else 500 { w = residual << 1 ; 501 residual = 0 ; 502 } ; 503 codewords [k++] = (w >> 12) & 0xe ; 504 codewords [k++] = (w >> 8) & 0xe ; 505 codewords [k++] = (w >> 4) & 0xe ; 506 codewords [k++] = w & 0xe ; 507 } ; 508 509 /* 510 ** Every block ends with a short representing a RMS-approximation for the 511 ** block. 512 **/ 513 if (rms) 514 *rms = *block ; 515} /* nms_adpcm_unpack_24 */ 516 517/* 518** Reads 41 shorts from block, unpacks 160 codewords of 4-bits each, writing 519** each to its sequential array index of codewords. If rms is non-null, the 520** read block rms is copied to its location. 521*/ 522static void 523nms_adpcm_block_unpack_32 (const uint16_t block [], int16_t codewords [], int16_t *rms) 524{ int k ; 525 uint16_t w = 0 ; 526 527 for (k = 0 ; k < NMS_SAMPLES_PER_BLOCK ; ) 528 { w = *(block++) ; 529 codewords [k++] = (w >> 12) & 0xf ; 530 codewords [k++] = (w >> 8) & 0xf ; 531 codewords [k++] = (w >> 4) & 0xf ; 532 codewords [k++] = w & 0xf ; 533 } ; 534 /* 535 ** Every block ends with a short representing a RMS-approximation for the 536 ** block. 537 **/ 538 if (rms) 539 *rms = *block ; 540} /* nms_adpcm_unpack_32 */ 541 542/* 543** Reads 160 indicies of codewords for one 2-bit codeword each, packing them 544** into 20 shorts of block, and writes the short rms for a total of 42 bytes. 545*/ 546static void 547nms_adpcm_block_pack_16 (const int16_t codewords [], uint16_t block [], int16_t rms) 548{ int k ; 549 uint16_t w ; 550 551 for (k = 0 ; k < NMS_SAMPLES_PER_BLOCK ; ) 552 { w = codewords [k++] << 12 ; 553 w |= codewords [k++] << 8 ; 554 w |= codewords [k++] << 4 ; 555 w |= codewords [k++] ; 556 w |= codewords [k++] << 10 ; 557 w |= codewords [k++] << 6 ; 558 w |= codewords [k++] << 2 ; 559 w |= codewords [k++] >> 2 ; 560 561 *(block++) = w ; 562 } ; 563 564 /* Every block ends with a short representing the blocks RMS */ 565 *block = rms ; 566} /* nms_adpcm_pack_16 */ 567 568/* 569** Reads 160 indicies of codewords for one 3-bit codeword each, packing them 570** into 30 shorts of block, and writes the short rms for a total of 62 bytes. 571*/ 572static void 573nms_adpcm_block_pack_24 (const int16_t codewords [], uint16_t block [], int16_t rms) 574{ int k ; 575 uint16_t w [3] ; 576 uint16_t residual ; 577 578 for (k = 0 ; k < NMS_SAMPLES_PER_BLOCK ; ) 579 { w [0] = codewords [k++] << 12 ; 580 w [0] |= codewords [k++] << 8 ; 581 w [0] |= codewords [k++] << 4 ; 582 w [0] |= codewords [k++] ; 583 584 w [1] = codewords [k++] << 12 ; 585 w [1] |= codewords [k++] << 8 ; 586 w [1] |= codewords [k++] << 4 ; 587 w [1] |= codewords [k++] ; 588 589 w [2] = codewords [k++] << 12 ; 590 w [2] |= codewords [k++] << 8 ; 591 w [2] |= codewords [k++] << 4 ; 592 w [2] |= codewords [k++] ; 593 594 residual = codewords [k++] << 12 ; 595 residual |= codewords [k++] << 8 ; 596 residual |= codewords [k++] << 4 ; 597 residual |= codewords [k++] ; 598 599 residual >>= 1 ; 600 w [2] |= (residual & 0x1111) ; 601 residual >>= 1 ; 602 w [1] |= (residual & 0x1111) ; 603 residual >>= 1 ; 604 w [0] |= (residual & 0x1111) ; 605 606 *(block++) = w [0] ; 607 *(block++) = w [1] ; 608 *(block++) = w [2] ; 609 } ; 610 611 /* Every block ends with a short representing the blocks RMS */ 612 *block = rms ; 613} /* nms_adpcm_pack_24 */ 614 615/* 616** Reads 160 indicies of codewords for one 4-bit codeword each, packing them 617** into 40 shorts of block, and writes the short rms for a total of 82 bytes. 618*/ 619static void 620nms_adpcm_block_pack_32 (const int16_t codewords [], uint16_t block [], int16_t rms) 621{ int k ; 622 uint16_t w ; 623 624 for (k = 0 ; k < NMS_SAMPLES_PER_BLOCK ; ) 625 { w = codewords [k++] << 12 ; 626 w |= codewords [k++] << 8 ; 627 w |= codewords [k++] << 4 ; 628 w |= codewords [k++] ; 629 630 *(block++) = w ; 631 } ; 632 633 /* Every block ends with a short representing the blocks RMS */ 634 *block = rms ; 635} /*nms_adpcm_block_pack_32 */ 636 637static int 638nms_adpcm_decode_block (SF_PRIVATE *psf, NMS_ADPCM_PRIVATE *pnms, uint16_t block [], int16_t samples []) 639{ int k ; 640 641 switch (pnms->type) 642 { case NMS16 : 643 nms_adpcm_block_unpack_16 (block, samples, NULL) ; 644 break ; 645 case NMS24 : 646 nms_adpcm_block_unpack_24 (block, samples, NULL) ; 647 break ; 648 case NMS32 : 649 nms_adpcm_block_unpack_32 (block, samples, NULL) ; 650 break ; 651 652 default : 653 psf_log_printf (psf, "*** Error : Unhandled NMS ADPCM type %d.\n", pnms->type) ; 654 return 0 ; 655 } ; 656 657 for (k = 0 ; k < NMS_SAMPLES_PER_BLOCK ; k++) 658 samples [k] = nms_adpcm_decode_sample (&pnms->state, samples [k]) ; 659 660 return NMS_SAMPLES_PER_BLOCK ; 661} /* nms_adpcm_decode_block */ 662 663static int 664nms_adpcm_encode_block (SF_PRIVATE *psf, NMS_ADPCM_PRIVATE *pnms, int16_t samples [], uint16_t block []) 665{ int k ; 666 unsigned int rms = 0 ; 667 668 /* 669 ** The rms we write is a complete lie. Considering that the various 670 ** other implementations I've tested don't completely agree, that this data 671 ** is usually ignored, and except for some weird offloading of "energy 672 ** detection", so long as we don't write zeros for non-zero data, I don't 673 ** think it really matters. 674 */ 675 676 for (k = 0 ; k < NMS_SAMPLES_PER_BLOCK ; k++) 677 { rms += (samples [k] * samples [k]) >> 2 ; 678 samples [k] = nms_adpcm_encode_sample (&pnms->state, samples [k]) ; 679 } ; 680 681 rms <<= 12 ; 682 switch (pnms->type) 683 { case NMS16 : 684 nms_adpcm_block_pack_16 (samples, block, rms) ; 685 break ; 686 case NMS24 : 687 nms_adpcm_block_pack_24 (samples, block, rms) ; 688 break ; 689 case NMS32 : 690 nms_adpcm_block_pack_32 (samples, block, rms) ; 691 break ; 692 693 default : 694 psf_log_printf (psf, "*** Error : Unhandled NMS ADPCM type %d.\n", pnms->type) ; 695 return 0 ; 696 } ; 697 698 return NMS_SAMPLES_PER_BLOCK ; 699} /* nms_adpcm_encode_block */ 700 701static int 702psf_nms_adpcm_decode_block (SF_PRIVATE *psf, NMS_ADPCM_PRIVATE *pnms) 703{ int k ; 704 705 if ((k = (int) psf_fread (pnms->block, sizeof (short), pnms->shortsperblock, psf)) != pnms->shortsperblock) 706 { psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pnms->shortsperblock) ; 707 memset (pnms->block + (k * sizeof (short)), 0, (pnms->shortsperblock - k) * sizeof (short)) ; 708 } ; 709 710 if (CPU_IS_BIG_ENDIAN) 711 endswap_short_array ((signed short *) pnms->block, pnms->shortsperblock) ; 712 713 nms_adpcm_decode_block (psf, pnms, pnms->block, pnms->samples) ; 714 715 return 1 ; 716} /* nms_adpcm_decode_block */ 717 718static int 719nms_adpcm_read_block (SF_PRIVATE *psf, NMS_ADPCM_PRIVATE *pnms, short *ptr, int len) 720{ int count, indx = 0 ; 721 722 while (indx < len) 723 { if (pnms->sample_curr >= NMS_SAMPLES_PER_BLOCK) 724 { pnms->block_curr ++ ; 725 pnms->sample_curr = 0 ; 726 } ; 727 728 if (pnms->block_curr > pnms->blocks_total) 729 { memset (&(ptr [indx]), 0, (len - indx) * sizeof (short)) ; 730 return indx ; 731 } ; 732 733 if (pnms->sample_curr == 0) 734 psf_nms_adpcm_decode_block (psf, pnms) ; 735 736 count = NMS_SAMPLES_PER_BLOCK - pnms->sample_curr ; 737 if (len - indx < count) 738 count = len - indx ; 739 740 memcpy (&(ptr [indx]), &(pnms->samples [pnms->sample_curr]), count * sizeof (short)) ; 741 indx += count ; 742 pnms->sample_curr += count ; 743 } ; 744 745 return indx ; 746} /* nms_adpcm_read_block */ 747 748static sf_count_t 749nms_adpcm_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) 750{ NMS_ADPCM_PRIVATE *pnms ; 751 int readcount, count ; 752 sf_count_t total = 0 ; 753 754 if (psf->codec_data == NULL) 755 return 0 ; 756 pnms = (NMS_ADPCM_PRIVATE*) psf->codec_data ; 757 758 while (len > 0) 759 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ; 760 761 count = nms_adpcm_read_block (psf, pnms, ptr, readcount) ; 762 763 total += count ; 764 len -= count ; 765 766 if (count != readcount) 767 break ; 768 } ; 769 770 return total ; 771} /* nms_adpcm_read_s */ 772 773static sf_count_t 774nms_adpcm_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) 775{ BUF_UNION ubuf ; 776 NMS_ADPCM_PRIVATE *pnms ; 777 short *sptr ; 778 int k, bufferlen, readcount = 0, count ; 779 sf_count_t total = 0 ; 780 781 if (psf->codec_data == NULL) 782 return 0 ; 783 pnms = (NMS_ADPCM_PRIVATE *) psf->codec_data ; 784 785 sptr = ubuf.sbuf ; 786 bufferlen = SF_BUFFER_LEN / sizeof (short) ; 787 while (len > 0) 788 { readcount = (len >= bufferlen) ? bufferlen : (int) len ; 789 count = nms_adpcm_read_block (psf, pnms, sptr, readcount) ; 790 791 for (k = 0 ; k < readcount ; k++) 792 ptr [total + k] = arith_shift_left (sptr [k], 16) ; 793 794 total += count ; 795 len -= readcount ; 796 if (count != readcount) 797 break ; 798 } ; 799 800 return total ; 801} /* nms_adpcm_read_i */ 802 803static sf_count_t 804nms_adpcm_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) 805{ BUF_UNION ubuf ; 806 NMS_ADPCM_PRIVATE *pnms ; 807 short *sptr ; 808 int k, bufferlen, readcount = 0, count ; 809 sf_count_t total = 0 ; 810 float normfact ; 811 812 if (psf->codec_data == NULL) 813 return 0 ; 814 pnms = (NMS_ADPCM_PRIVATE*) psf->codec_data ; 815 816 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ; 817 818 sptr = ubuf.sbuf ; 819 bufferlen = SF_BUFFER_LEN / sizeof (short) ; 820 while (len > 0) 821 { readcount = (len >= bufferlen) ? bufferlen : (int) len ; 822 count = nms_adpcm_read_block (psf, pnms, sptr, readcount) ; 823 for (k = 0 ; k < readcount ; k++) 824 ptr [total + k] = normfact * sptr [k] ; 825 826 total += count ; 827 len -= readcount ; 828 if (count != readcount) 829 break ; 830 } ; 831 832 return total ; 833} /* nms_adpcm_read_f */ 834 835static sf_count_t 836nms_adpcm_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) 837{ BUF_UNION ubuf ; 838 NMS_ADPCM_PRIVATE *pnms ; 839 short *sptr ; 840 int k, bufferlen, readcount = 0, count ; 841 sf_count_t total = 0 ; 842 double normfact ; 843 844 if (psf->codec_data == NULL) 845 return 0 ; 846 pnms = (NMS_ADPCM_PRIVATE*) psf->codec_data ; 847 848 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ; 849 850 sptr = ubuf.sbuf ; 851 bufferlen = SF_BUFFER_LEN / sizeof (short) ; 852 while (len > 0) 853 { readcount = (len >= bufferlen) ? bufferlen : (int) len ; 854 count = nms_adpcm_read_block (psf, pnms, sptr, readcount) ; 855 for (k = 0 ; k < readcount ; k++) 856 ptr [total + k] = normfact * (double) (sptr [k]) ; 857 858 total += count ; 859 len -= readcount ; 860 if (count != readcount) 861 break ; 862 } ; 863 864 return total ; 865} /* nms_adpcm_read_d */ 866 867static int 868psf_nms_adpcm_encode_block (SF_PRIVATE *psf, NMS_ADPCM_PRIVATE *pnms) 869{ int k ; 870 871 /* Encode the samples. */ 872 nms_adpcm_encode_block (psf, pnms, pnms->samples, pnms->block) ; 873 874 if (CPU_IS_BIG_ENDIAN) 875 endswap_short_array ((signed short *) pnms->block, pnms->shortsperblock) ; 876 877 /* Write the block to disk. */ 878 if ((k = (int) psf_fwrite (pnms->block, sizeof (short), pnms->shortsperblock, psf)) != pnms->shortsperblock) 879 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pnms->shortsperblock) ; 880 881 pnms->sample_curr = 0 ; 882 pnms->block_curr ++ ; 883 884 return 1 ; 885} /* psf_nms_adpcm_encode_block */ 886 887static int 888nms_adpcm_write_block (SF_PRIVATE *psf, NMS_ADPCM_PRIVATE *pnms, const short *ptr, int len) 889{ int count, total = 0, indx = 0 ; 890 891 while (indx < len) 892 { count = NMS_SAMPLES_PER_BLOCK - pnms->sample_curr ; 893 894 if (count > len - indx) 895 count = len - indx ; 896 897 memcpy (&(pnms->samples [pnms->sample_curr]), &(ptr [indx]), count * sizeof (short)) ; 898 indx += count ; 899 pnms->sample_curr += count ; 900 total = indx ; 901 902 if (pnms->sample_curr >= NMS_SAMPLES_PER_BLOCK) 903 psf_nms_adpcm_encode_block (psf, pnms) ; 904 } ; 905 906 return total ; 907} /* nms_adpcm_write_block */ 908 909static sf_count_t 910nms_adpcm_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) 911{ NMS_ADPCM_PRIVATE *pnms ; 912 int writecount, count ; 913 sf_count_t total = 0 ; 914 915 if (psf->codec_data == NULL) 916 return 0 ; 917 pnms = (NMS_ADPCM_PRIVATE*) psf->codec_data ; 918 919 while (len > 0) 920 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ; 921 922 count = nms_adpcm_write_block (psf, pnms, ptr, writecount) ; 923 924 total += count ; 925 len -= count ; 926 if (count != writecount) 927 break ; 928 } ; 929 930 return total ; 931} /* nms_adpcm_write_s */ 932 933static sf_count_t 934nms_adpcm_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) 935{ BUF_UNION ubuf ; 936 NMS_ADPCM_PRIVATE *pnms ; 937 short *sptr ; 938 int k, bufferlen, writecount = 0, count ; 939 sf_count_t total = 0 ; 940 941 if (psf->codec_data == NULL) 942 return 0 ; 943 pnms = (NMS_ADPCM_PRIVATE*) psf->codec_data ; 944 945 sptr = ubuf.sbuf ; 946 bufferlen = SF_BUFFER_LEN / sizeof (short) ; 947 while (len > 0) 948 { writecount = (len >= bufferlen) ? bufferlen : (int) len ; 949 for (k = 0 ; k < writecount ; k++) 950 sptr [k] = ptr [total + k] >> 16 ; 951 count = nms_adpcm_write_block (psf, pnms, sptr, writecount) ; 952 953 total += count ; 954 len -= writecount ; 955 if (count != writecount) 956 break ; 957 } ; 958 return total ; 959} /* nms_adpcm_write_i */ 960 961static sf_count_t 962nms_adpcm_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) 963{ BUF_UNION ubuf ; 964 NMS_ADPCM_PRIVATE *pnms ; 965 short *sptr ; 966 int k, bufferlen, writecount = 0, count ; 967 sf_count_t total = 0 ; 968 float normfact ; 969 970 if (psf->codec_data == NULL) 971 return 0 ; 972 pnms = (NMS_ADPCM_PRIVATE*) psf->codec_data ; 973 974 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x8000) : 1.0 ; 975 976 sptr = ubuf.sbuf ; 977 bufferlen = SF_BUFFER_LEN / sizeof (short) ; 978 while (len > 0) 979 { writecount = (len >= bufferlen) ? bufferlen : (int) len ; 980 for (k = 0 ; k < writecount ; k++) 981 sptr [k] = psf_lrintf (normfact * ptr [total + k]) ; 982 count = nms_adpcm_write_block (psf, pnms, sptr, writecount) ; 983 984 total += count ; 985 len -= writecount ; 986 if (count != writecount) 987 break ; 988 } ; 989 990 return total ; 991} /* nms_adpcm_write_f */ 992 993static sf_count_t 994nms_adpcm_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) 995{ BUF_UNION ubuf ; 996 NMS_ADPCM_PRIVATE *pnms ; 997 short *sptr ; 998 int k, bufferlen, writecount = 0, count ; 999 sf_count_t total = 0 ; 1000 double normfact ; 1001 1002 if (psf->codec_data == NULL) 1003 return 0 ; 1004 pnms = (NMS_ADPCM_PRIVATE*) psf->codec_data ; 1005 1006 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x8000) : 1.0 ; 1007 1008 sptr = ubuf.sbuf ; 1009 bufferlen = SF_BUFFER_LEN / sizeof (short) ; 1010 while (len > 0) 1011 { writecount = (len >= bufferlen) ? bufferlen : (int) len ; 1012 for (k = 0 ; k < writecount ; k++) 1013 sptr [k] = psf_lrint (normfact * ptr [total + k]) ; 1014 count = nms_adpcm_write_block (psf, pnms, sptr, writecount) ; 1015 1016 total += count ; 1017 len -= writecount ; 1018 if (count != writecount) 1019 break ; 1020 } ; 1021 1022 return total ; 1023} /* nms_adpcm_write_d */ 1024 1025int 1026nms_adpcm_init (SF_PRIVATE *psf) 1027{ NMS_ADPCM_PRIVATE *pnms ; 1028 1029 if (psf->codec_data != NULL) 1030 { psf_log_printf (psf, "*** psf->codec_data is not NULL.\n") ; 1031 return SFE_INTERNAL ; 1032 } ; 1033 1034 psf->sf.seekable = SF_FALSE ; 1035 1036 if (psf->sf.channels != 1) 1037 return SFE_NMS_ADPCM_NOT_MONO ; 1038 1039 if ((pnms = calloc (1, sizeof (NMS_ADPCM_PRIVATE))) == NULL) 1040 return SFE_MALLOC_FAILED ; 1041 1042 psf->codec_data = (void*) pnms ; 1043 1044 pnms->block_curr = 0 ; 1045 pnms->sample_curr = 0 ; 1046 1047 switch (SF_CODEC (psf->sf.format)) 1048 { case SF_FORMAT_NMS_ADPCM_16 : 1049 pnms->type = NMS16 ; 1050 pnms->shortsperblock = NMS_BLOCK_SHORTS_16 ; 1051 break ; 1052 case SF_FORMAT_NMS_ADPCM_24 : 1053 pnms->type = NMS24 ; 1054 pnms->shortsperblock = NMS_BLOCK_SHORTS_24 ; 1055 break ; 1056 case SF_FORMAT_NMS_ADPCM_32 : 1057 pnms->type = NMS32 ; 1058 pnms->shortsperblock = NMS_BLOCK_SHORTS_32 ; 1059 break ; 1060 1061 default : return SFE_UNIMPLEMENTED ; 1062 } ; 1063 nms_adpcm_codec_init (&pnms->state, pnms->type) ; 1064 1065 psf->filelength = psf_get_filelen (psf) ; 1066 if (psf->filelength < psf->dataoffset) 1067 psf->filelength = psf->dataoffset ; 1068 1069 psf->datalength = psf->filelength - psf->dataoffset ; 1070 if (psf->dataend > 0) 1071 psf->datalength -= psf->filelength - psf->dataend ; 1072 1073 if (psf->file.mode == SFM_READ) 1074 { psf->read_short = nms_adpcm_read_s ; 1075 psf->read_int = nms_adpcm_read_i ; 1076 psf->read_float = nms_adpcm_read_f ; 1077 psf->read_double = nms_adpcm_read_d ; 1078 } 1079 else if (psf->file.mode == SFM_WRITE) 1080 { psf->write_short = nms_adpcm_write_s ; 1081 psf->write_int = nms_adpcm_write_i ; 1082 psf->write_float = nms_adpcm_write_f ; 1083 psf->write_double = nms_adpcm_write_d ; 1084 } ; 1085 1086 if (psf->datalength % (pnms->shortsperblock * sizeof (short))) 1087 { psf_log_printf (psf, "*** Odd psf->datalength (%D) should be a multiple of %d\n", 1088 psf->datalength, pnms->shortsperblock * sizeof (short)) ; 1089 pnms->blocks_total = (psf->datalength / (pnms->shortsperblock * sizeof (short))) + 1 ; 1090 } 1091 else 1092 pnms->blocks_total = psf->datalength / (pnms->shortsperblock * sizeof (short)) ; 1093 1094 psf->sf.frames = pnms->blocks_total * NMS_SAMPLES_PER_BLOCK ; 1095 psf->codec_close = nms_adpcm_close ; 1096 psf->seek = nms_adpcm_seek ; 1097 1098 return 0 ; 1099} /* nms_adpcm_init */ 1100 1101static int 1102nms_adpcm_close (SF_PRIVATE *psf) 1103{ NMS_ADPCM_PRIVATE *pnms ; 1104 1105 pnms = (NMS_ADPCM_PRIVATE*) psf->codec_data ; 1106 1107 /* 1108 ** If a block has been partially assembled, write it out as the final 1109 ** block. 1110 */ 1111 if (psf->file.mode == SFM_WRITE) 1112 { if (pnms->sample_curr && pnms->sample_curr < NMS_SAMPLES_PER_BLOCK) 1113 { memset (pnms->samples + pnms->sample_curr, 0, (NMS_SAMPLES_PER_BLOCK - pnms->sample_curr) * sizeof (short)) ; 1114 psf_nms_adpcm_encode_block (psf, pnms) ; 1115 } 1116 1117 if (psf->write_header) 1118 psf->write_header (psf, SF_FALSE) ; 1119 } 1120 1121 return 0 ; 1122} /* nms_adpcm_close */ 1123 1124static sf_count_t 1125nms_adpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) 1126{ NMS_ADPCM_PRIVATE *pnms ; 1127 1128 pnms = (NMS_ADPCM_PRIVATE *) psf->codec_data ; 1129 1130 /* 1131 ** NMS ADPCM is symmetric, so transitioning from reading and writing is 1132 ** possible, but unimplemented, as it would require syncing partial blocks. 1133 */ 1134 if (mode != psf->file.mode) 1135 { psf->error = SFE_BAD_SEEK ; 1136 return PSF_SEEK_ERROR ; 1137 } ; 1138 1139 /* 1140 ** NMS ADPCM cannot be seek'ed, as codec state depends on previous samples, 1141 ** so only a seek to 0 is supported. 1142 */ 1143 if (offset != 0) 1144 { psf->error = SFE_BAD_SEEK ; 1145 return PSF_SEEK_ERROR ; 1146 } ; 1147 1148 if (psf_fseek (psf, psf->dataoffset, SEEK_SET) == PSF_SEEK_ERROR) 1149 return PSF_SEEK_ERROR ; 1150 1151 nms_adpcm_codec_init (&pnms->state, pnms->type) ; 1152 pnms->block_curr = 0 ; 1153 pnms->sample_curr = 0 ; 1154 return 0 ; 1155} /* nms_adpcm_seek */ 1156 1157