| 1 |
#ifndef ARDUINO |
| 2 |
|
| 3 |
//////////////////////////////////////////////////////////////////////////// |
| 4 |
// **** ADPCM-XQ **** // |
| 5 |
// Xtreme Quality ADPCM Encoder/Decoder // |
| 6 |
// Copyright (c) 2022 David Bryant. // |
| 7 |
// All Rights Reserved. // |
| 8 |
// Distributed under the BSD Software License (see license.txt) // |
| 9 |
//////////////////////////////////////////////////////////////////////////// |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <string.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <ctype.h> |
| 15 |
|
| 16 |
#include "adpcm-lib.h" |
| 17 |
|
| 18 |
// This runtime macro is not strictly needed because the code is endian-safe, |
| 19 |
// but including it improves performance on little-endian systems because we |
| 20 |
// can avoid a couple loops through the audio. |
| 21 |
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x0100) |
| 22 |
|
| 23 |
static const char *sign_on = "\n" |
| 24 |
" ADPCM-XQ Xtreme Quality IMA-ADPCM WAV Encoder / Decoder Version 0.4\n" |
| 25 |
" Copyright (c) 2022 David Bryant. All Rights Reserved.\n\n"; |
| 26 |
|
| 27 |
static const char *usage = |
| 28 |
" Usage: ADPCM-XQ [-options] infile.wav outfile.wav\n\n" |
| 29 |
" Operation: conversion is performed based on the type of the infile\n" |
| 30 |
" (either encode 16-bit PCM to 4-bit IMA-ADPCM or decode back)\n\n" |
| 31 |
" Options: -[0-8] = encode lookahead samples (default = 3)\n" |
| 32 |
" -bn = override auto block size, 2^n bytes (n = 8-15)\n" |
| 33 |
" -d = decode only (fail on WAV file already PCM)\n" |
| 34 |
" -e = encode only (fail on WAV file already ADPCM)\n" |
| 35 |
" -f = encode flat noise (no dynamic noise shaping)\n" |
| 36 |
" -h = display this help message\n" |
| 37 |
" -q = quiet mode (display errors only)\n" |
| 38 |
" -r = raw output (little-endian, no WAV header written)\n" |
| 39 |
" -v = verbose (display lots of info)\n" |
| 40 |
" -y = overwrite outfile if it exists\n\n" |
| 41 |
" Web: Visit www.github.com/dbry/adpcm-xq for latest version and info\n\n"; |
| 42 |
|
| 43 |
#define ADPCM_FLAG_NOISE_SHAPING 0x1 |
| 44 |
#define ADPCM_FLAG_RAW_OUTPUT 0x2 |
| 45 |
|
| 46 |
static int adpcm_converter (char *infilename, char *outfilename, int flags, int blocksize_pow2, int lookahead); |
| 47 |
static int verbosity = 0, decode_only = 0, encode_only = 0; |
| 48 |
|
| 49 |
int main (argc, argv) int argc; char **argv; |
| 50 |
{ |
| 51 |
int lookahead = 3, flags = ADPCM_FLAG_NOISE_SHAPING, blocksize_pow2 = 0, overwrite = 0, asked_help = 0; |
| 52 |
char *infilename = NULL, *outfilename = NULL; |
| 53 |
FILE *outfile; |
| 54 |
|
| 55 |
// if the name of the executable ends in "encoder" or "decoder", just do that function |
| 56 |
encode_only = argc && strstr (argv [0], "encoder") && strlen (strstr (argv [0], "encoder")) == strlen ("encoder"); |
| 57 |
decode_only = argc && strstr (argv [0], "decoder") && strlen (strstr (argv [0], "decoder")) == strlen ("decoder"); |
| 58 |
|
| 59 |
// loop through command-line arguments |
| 60 |
|
| 61 |
while (--argc) { |
| 62 |
#if defined (_WIN32) |
| 63 |
if ((**++argv == '-' || **argv == '/') && (*argv)[1]) |
| 64 |
#else |
| 65 |
if ((**++argv == '-') && (*argv)[1]) |
| 66 |
#endif |
| 67 |
while (*++*argv) |
| 68 |
switch (**argv) { |
| 69 |
|
| 70 |
case '0': case '1': case '2': |
| 71 |
case '3': case '4': case '5': |
| 72 |
case '6': case '7': case '8': |
| 73 |
lookahead = **argv - '0'; |
| 74 |
break; |
| 75 |
|
| 76 |
case 'B': case 'b': |
| 77 |
blocksize_pow2 = strtol (++*argv, argv, 10); |
| 78 |
|
| 79 |
if (blocksize_pow2 < 8 || blocksize_pow2 > 15) { |
| 80 |
fprintf (stderr, "\nblock size power must be 8 to 15!\n"); |
| 81 |
return -1; |
| 82 |
} |
| 83 |
|
| 84 |
--*argv; |
| 85 |
break; |
| 86 |
|
| 87 |
case 'D': case 'd': |
| 88 |
decode_only = 1; |
| 89 |
break; |
| 90 |
|
| 91 |
case 'E': case 'e': |
| 92 |
encode_only = 1; |
| 93 |
break; |
| 94 |
|
| 95 |
case 'F': case 'f': |
| 96 |
flags &= ~ADPCM_FLAG_NOISE_SHAPING; |
| 97 |
break; |
| 98 |
|
| 99 |
case 'H': case 'h': |
| 100 |
asked_help = 0; |
| 101 |
break; |
| 102 |
|
| 103 |
case 'Q': case 'q': |
| 104 |
verbosity = -1; |
| 105 |
break; |
| 106 |
|
| 107 |
case 'R': case 'r': |
| 108 |
flags |= ADPCM_FLAG_RAW_OUTPUT; |
| 109 |
break; |
| 110 |
|
| 111 |
case 'V': case 'v': |
| 112 |
verbosity = 1; |
| 113 |
break; |
| 114 |
|
| 115 |
case 'Y': case 'y': |
| 116 |
overwrite = 1; |
| 117 |
break; |
| 118 |
|
| 119 |
default: |
| 120 |
fprintf (stderr, "\nillegal option: %c !\n", **argv); |
| 121 |
return 1; |
| 122 |
} |
| 123 |
else if (!infilename) { |
| 124 |
infilename = malloc (strlen (*argv) + 10); |
| 125 |
strcpy (infilename, *argv); |
| 126 |
} |
| 127 |
else if (!outfilename) { |
| 128 |
outfilename = malloc (strlen (*argv) + 10); |
| 129 |
strcpy (outfilename, *argv); |
| 130 |
} |
| 131 |
else { |
| 132 |
fprintf (stderr, "\nextra unknown argument: %s !\n", *argv); |
| 133 |
return 1; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if (verbosity >= 0) |
| 138 |
fprintf (stderr, "%s", sign_on); |
| 139 |
|
| 140 |
if (!outfilename || asked_help) { |
| 141 |
printf ("%s", usage); |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
if (!strcmp (infilename, outfilename)) { |
| 146 |
fprintf (stderr, "can't overwrite input file (specify different/new output file name)\n"); |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
if (!overwrite && (outfile = fopen (outfilename, "r"))) { |
| 151 |
fclose (outfile); |
| 152 |
fprintf (stderr, "output file \"%s\" exists (use -y to overwrite)\n", outfilename); |
| 153 |
return -1; |
| 154 |
} |
| 155 |
|
| 156 |
return adpcm_converter (infilename, outfilename, flags, blocksize_pow2, lookahead); |
| 157 |
} |
| 158 |
|
| 159 |
typedef struct { |
| 160 |
char ckID [4]; |
| 161 |
uint32_t ckSize; |
| 162 |
char formType [4]; |
| 163 |
} RiffChunkHeader; |
| 164 |
|
| 165 |
typedef struct { |
| 166 |
char ckID [4]; |
| 167 |
uint32_t ckSize; |
| 168 |
} ChunkHeader; |
| 169 |
|
| 170 |
#define ChunkHeaderFormat "4L" |
| 171 |
|
| 172 |
typedef struct { |
| 173 |
uint16_t FormatTag, NumChannels; |
| 174 |
uint32_t SampleRate, BytesPerSecond; |
| 175 |
uint16_t BlockAlign, BitsPerSample; |
| 176 |
uint16_t cbSize; |
| 177 |
union { |
| 178 |
uint16_t ValidBitsPerSample; |
| 179 |
uint16_t SamplesPerBlock; |
| 180 |
uint16_t Reserved; |
| 181 |
} Samples; |
| 182 |
int32_t ChannelMask; |
| 183 |
uint16_t SubFormat; |
| 184 |
char GUID [14]; |
| 185 |
} WaveHeader; |
| 186 |
|
| 187 |
#define WaveHeaderFormat "SSLLSSSSLS" |
| 188 |
|
| 189 |
typedef struct { |
| 190 |
char ckID [4]; |
| 191 |
uint32_t ckSize; |
| 192 |
uint32_t TotalSamples; |
| 193 |
} FactHeader; |
| 194 |
|
| 195 |
#define FactHeaderFormat "4LL" |
| 196 |
|
| 197 |
#define WAVE_FORMAT_PCM 0x1 |
| 198 |
#define WAVE_FORMAT_IMA_ADPCM 0x11 |
| 199 |
#define WAVE_FORMAT_EXTENSIBLE 0xfffe |
| 200 |
|
| 201 |
static int write_pcm_wav_header (FILE *outfile, int num_channels, uint32_t num_samples, uint32_t sample_rate); |
| 202 |
static int write_adpcm_wav_header (FILE *outfile, int num_channels, uint32_t num_samples, uint32_t sample_rate, int samples_per_block); |
| 203 |
static int adpcm_decode_data (FILE *infile, FILE *outfile, int num_channels, uint32_t num_samples, int block_size); |
| 204 |
static int adpcm_encode_data (FILE *infile, FILE *outfile, int num_channels, uint32_t num_samples, int samples_per_block, int lookahead, int noise_shaping); |
| 205 |
static void little_endian_to_native (void *data, char *format); |
| 206 |
static void native_to_little_endian (void *data, char *format); |
| 207 |
|
| 208 |
static int adpcm_converter (char *infilename, char *outfilename, int flags, int blocksize_pow2, int lookahead) |
| 209 |
{ |
| 210 |
int format = 0, res = 0, bits_per_sample, num_channels; |
| 211 |
uint32_t fact_samples = 0, num_samples = 0, sample_rate; |
| 212 |
FILE *infile, *outfile; |
| 213 |
RiffChunkHeader riff_chunk_header; |
| 214 |
ChunkHeader chunk_header; |
| 215 |
WaveHeader WaveHeader; |
| 216 |
|
| 217 |
if (!(infile = fopen (infilename, "rb"))) { |
| 218 |
fprintf (stderr, "can't open file \"%s\" for reading!\n", infilename); |
| 219 |
return -1; |
| 220 |
} |
| 221 |
|
| 222 |
// read initial RIFF form header |
| 223 |
|
| 224 |
if (!fread (&riff_chunk_header, sizeof (RiffChunkHeader), 1, infile) || |
| 225 |
strncmp (riff_chunk_header.ckID, "RIFF", 4) || |
| 226 |
strncmp (riff_chunk_header.formType, "WAVE", 4)) { |
| 227 |
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename); |
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
// loop through all elements of the RIFF wav header (until the data chuck) |
| 232 |
|
| 233 |
while (1) { |
| 234 |
|
| 235 |
if (!fread (&chunk_header, sizeof (ChunkHeader), 1, infile)) { |
| 236 |
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename); |
| 237 |
return -1; |
| 238 |
} |
| 239 |
|
| 240 |
little_endian_to_native (&chunk_header, ChunkHeaderFormat); |
| 241 |
|
| 242 |
// if it's the format chunk, we want to get some info out of there and |
| 243 |
// make sure it's a .wav file we can handle |
| 244 |
|
| 245 |
if (!strncmp (chunk_header.ckID, "fmt ", 4)) { |
| 246 |
int supported = 1; |
| 247 |
|
| 248 |
if (chunk_header.ckSize < 16 || chunk_header.ckSize > sizeof (WaveHeader) || |
| 249 |
!fread (&WaveHeader, chunk_header.ckSize, 1, infile)) { |
| 250 |
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename); |
| 251 |
return -1; |
| 252 |
} |
| 253 |
|
| 254 |
little_endian_to_native (&WaveHeader, WaveHeaderFormat); |
| 255 |
|
| 256 |
format = (WaveHeader.FormatTag == WAVE_FORMAT_EXTENSIBLE && chunk_header.ckSize == 40) ? |
| 257 |
WaveHeader.SubFormat : WaveHeader.FormatTag; |
| 258 |
|
| 259 |
bits_per_sample = (chunk_header.ckSize == 40 && WaveHeader.Samples.ValidBitsPerSample) ? |
| 260 |
WaveHeader.Samples.ValidBitsPerSample : WaveHeader.BitsPerSample; |
| 261 |
|
| 262 |
if (WaveHeader.NumChannels < 1 || WaveHeader.NumChannels > 2) |
| 263 |
supported = 0; |
| 264 |
else if (format == WAVE_FORMAT_PCM) { |
| 265 |
if (decode_only) { |
| 266 |
fprintf (stderr, "\"%s\" is PCM .WAV file, invalid in decode-only mode!\n", infilename); |
| 267 |
return -1; |
| 268 |
} |
| 269 |
|
| 270 |
if (bits_per_sample < 9 || bits_per_sample > 16) |
| 271 |
supported = 0; |
| 272 |
|
| 273 |
if (WaveHeader.BlockAlign != WaveHeader.NumChannels * 2) |
| 274 |
supported = 0; |
| 275 |
} |
| 276 |
else if (format == WAVE_FORMAT_IMA_ADPCM) { |
| 277 |
if (encode_only) { |
| 278 |
fprintf (stderr, "\"%s\" is ADPCM .WAV file, invalid in encode-only mode!\n", infilename); |
| 279 |
return -1; |
| 280 |
} |
| 281 |
|
| 282 |
if (bits_per_sample != 4) |
| 283 |
supported = 0; |
| 284 |
|
| 285 |
if (WaveHeader.Samples.SamplesPerBlock != (WaveHeader.BlockAlign - WaveHeader.NumChannels * 4) * (WaveHeader.NumChannels ^ 3) + 1) { |
| 286 |
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename); |
| 287 |
return -1; |
| 288 |
} |
| 289 |
} |
| 290 |
else |
| 291 |
supported = 0; |
| 292 |
|
| 293 |
if (!supported) { |
| 294 |
fprintf (stderr, "\"%s\" is an unsupported .WAV format!\n", infilename); |
| 295 |
return -1; |
| 296 |
} |
| 297 |
|
| 298 |
if (verbosity > 0) { |
| 299 |
fprintf (stderr, "format tag size = %d\n", chunk_header.ckSize); |
| 300 |
fprintf (stderr, "FormatTag = 0x%x, NumChannels = %u, BitsPerSample = %u\n", |
| 301 |
WaveHeader.FormatTag, WaveHeader.NumChannels, WaveHeader.BitsPerSample); |
| 302 |
fprintf (stderr, "BlockAlign = %u, SampleRate = %lu, BytesPerSecond = %lu\n", |
| 303 |
WaveHeader.BlockAlign, (unsigned long) WaveHeader.SampleRate, (unsigned long) WaveHeader.BytesPerSecond); |
| 304 |
|
| 305 |
if (chunk_header.ckSize > 16) { |
| 306 |
if (format == WAVE_FORMAT_PCM) |
| 307 |
fprintf (stderr, "cbSize = %d, ValidBitsPerSample = %d\n", WaveHeader.cbSize, |
| 308 |
WaveHeader.Samples.ValidBitsPerSample); |
| 309 |
else if (format == WAVE_FORMAT_IMA_ADPCM) |
| 310 |
fprintf (stderr, "cbSize = %d, SamplesPerBlock = %d\n", WaveHeader.cbSize, |
| 311 |
WaveHeader.Samples.SamplesPerBlock); |
| 312 |
} |
| 313 |
|
| 314 |
if (chunk_header.ckSize > 20) |
| 315 |
fprintf (stderr, "ChannelMask = %x, SubFormat = %d\n", |
| 316 |
WaveHeader.ChannelMask, WaveHeader.SubFormat); |
| 317 |
} |
| 318 |
} |
| 319 |
else if (!strncmp (chunk_header.ckID, "fact", 4)) { |
| 320 |
|
| 321 |
if (chunk_header.ckSize < 4 || !fread (&fact_samples, sizeof (fact_samples), 1, infile)) { |
| 322 |
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename); |
| 323 |
return -1; |
| 324 |
} |
| 325 |
|
| 326 |
little_endian_to_native (&fact_samples, "L"); |
| 327 |
|
| 328 |
if (chunk_header.ckSize > 4) { |
| 329 |
int bytes_to_skip = chunk_header.ckSize - 4; |
| 330 |
char dummy; |
| 331 |
|
| 332 |
while (bytes_to_skip--) |
| 333 |
if (!fread (&dummy, 1, 1, infile)) { |
| 334 |
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename); |
| 335 |
return -1; |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
else if (!strncmp (chunk_header.ckID, "data", 4)) { |
| 340 |
|
| 341 |
// on the data chunk, get size and exit parsing loop |
| 342 |
|
| 343 |
if (!WaveHeader.NumChannels) { // make sure we saw a "fmt" chunk... |
| 344 |
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename); |
| 345 |
return -1; |
| 346 |
} |
| 347 |
|
| 348 |
if (!chunk_header.ckSize) { |
| 349 |
fprintf (stderr, "this .WAV file has no audio samples, probably is corrupt!\n"); |
| 350 |
return -1; |
| 351 |
} |
| 352 |
|
| 353 |
if (format == WAVE_FORMAT_PCM) { |
| 354 |
if (chunk_header.ckSize % WaveHeader.BlockAlign) { |
| 355 |
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename); |
| 356 |
return -1; |
| 357 |
} |
| 358 |
|
| 359 |
num_samples = chunk_header.ckSize / WaveHeader.BlockAlign; |
| 360 |
} |
| 361 |
else { |
| 362 |
uint32_t complete_blocks = chunk_header.ckSize / WaveHeader.BlockAlign; |
| 363 |
int leftover_bytes = chunk_header.ckSize % WaveHeader.BlockAlign; |
| 364 |
int samples_last_block; |
| 365 |
|
| 366 |
num_samples = complete_blocks * WaveHeader.Samples.SamplesPerBlock; |
| 367 |
|
| 368 |
if (leftover_bytes) { |
| 369 |
if (leftover_bytes % (WaveHeader.NumChannels * 4)) { |
| 370 |
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename); |
| 371 |
return -1; |
| 372 |
} |
| 373 |
if (verbosity > 0) fprintf (stderr, "data chunk has %d bytes left over for final ADPCM block\n", leftover_bytes); |
| 374 |
samples_last_block = (leftover_bytes - (WaveHeader.NumChannels * 4)) * (WaveHeader.NumChannels ^ 3) + 1; |
| 375 |
num_samples += samples_last_block; |
| 376 |
} |
| 377 |
else |
| 378 |
samples_last_block = WaveHeader.Samples.SamplesPerBlock; |
| 379 |
|
| 380 |
if (fact_samples) { |
| 381 |
if (fact_samples < num_samples && fact_samples > num_samples - samples_last_block) { |
| 382 |
if (verbosity > 0) fprintf (stderr, "total samples reduced %lu by FACT chunk\n", (unsigned long) (num_samples - fact_samples)); |
| 383 |
num_samples = fact_samples; |
| 384 |
} |
| 385 |
else if (WaveHeader.NumChannels == 2 && (fact_samples >>= 1) < num_samples && fact_samples > num_samples - samples_last_block) { |
| 386 |
if (verbosity > 0) fprintf (stderr, "num samples reduced %lu by [incorrect] FACT chunk\n", (unsigned long) (num_samples - fact_samples)); |
| 387 |
num_samples = fact_samples; |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
if (!num_samples) { |
| 393 |
fprintf (stderr, "this .WAV file has no audio samples, probably is corrupt!\n"); |
| 394 |
return -1; |
| 395 |
} |
| 396 |
|
| 397 |
if (verbosity > 0) |
| 398 |
fprintf (stderr, "num samples = %lu\n", (unsigned long) num_samples); |
| 399 |
|
| 400 |
num_channels = WaveHeader.NumChannels; |
| 401 |
sample_rate = WaveHeader.SampleRate; |
| 402 |
break; |
| 403 |
} |
| 404 |
else { // just ignore unknown chunks |
| 405 |
int bytes_to_eat = (chunk_header.ckSize + 1) & ~1L; |
| 406 |
char dummy; |
| 407 |
|
| 408 |
if (verbosity > 0) |
| 409 |
fprintf (stderr, "extra unknown chunk \"%c%c%c%c\" of %d bytes\n", |
| 410 |
chunk_header.ckID [0], chunk_header.ckID [1], chunk_header.ckID [2], |
| 411 |
chunk_header.ckID [3], chunk_header.ckSize); |
| 412 |
|
| 413 |
while (bytes_to_eat--) |
| 414 |
if (!fread (&dummy, 1, 1, infile)) { |
| 415 |
fprintf (stderr, "\"%s\" is not a valid .WAV file!\n", infilename); |
| 416 |
return -1; |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
if (!(outfile = fopen (outfilename, "wb"))) { |
| 422 |
fprintf (stderr, "can't open file \"%s\" for writing!\n", outfilename); |
| 423 |
return -1; |
| 424 |
} |
| 425 |
|
| 426 |
if (format == WAVE_FORMAT_PCM) { |
| 427 |
int block_size, samples_per_block; |
| 428 |
|
| 429 |
if (blocksize_pow2) |
| 430 |
block_size = 1 << blocksize_pow2; |
| 431 |
else |
| 432 |
block_size = 256 * num_channels * (sample_rate < 11000 ? 1 : sample_rate / 11000); |
| 433 |
|
| 434 |
samples_per_block = (block_size - num_channels * 4) * (num_channels ^ 3) + 1; |
| 435 |
|
| 436 |
if (verbosity > 0) |
| 437 |
fprintf (stderr, "each %d byte ADPCM block will contain %d samples * %d channels\n", |
| 438 |
block_size, samples_per_block, num_channels); |
| 439 |
|
| 440 |
if (!(flags & ADPCM_FLAG_RAW_OUTPUT) && !write_adpcm_wav_header (outfile, num_channels, num_samples, sample_rate, samples_per_block)) { |
| 441 |
fprintf (stderr, "can't write header to file \"%s\" !\n", outfilename); |
| 442 |
return -1; |
| 443 |
} |
| 444 |
|
| 445 |
if (verbosity >= 0) fprintf (stderr, "encoding PCM file \"%s\" to%sADPCM file \"%s\"...\n", |
| 446 |
infilename, (flags & ADPCM_FLAG_RAW_OUTPUT) ? " raw " : " ", outfilename); |
| 447 |
|
| 448 |
res = adpcm_encode_data (infile, outfile, num_channels, num_samples, samples_per_block, lookahead, |
| 449 |
(flags & ADPCM_FLAG_NOISE_SHAPING) ? (sample_rate > 64000 ? NOISE_SHAPING_STATIC : NOISE_SHAPING_DYNAMIC) : NOISE_SHAPING_OFF); |
| 450 |
} |
| 451 |
else if (format == WAVE_FORMAT_IMA_ADPCM) { |
| 452 |
if (!(flags & ADPCM_FLAG_RAW_OUTPUT) && !write_pcm_wav_header (outfile, num_channels, num_samples, sample_rate)) { |
| 453 |
fprintf (stderr, "can't write header to file \"%s\" !\n", outfilename); |
| 454 |
return -1; |
| 455 |
} |
| 456 |
|
| 457 |
if (verbosity >= 0) fprintf (stderr, "decoding ADPCM file \"%s\" to%sPCM file \"%s\"...\n", |
| 458 |
infilename, (flags & ADPCM_FLAG_RAW_OUTPUT) ? " raw " : " ", outfilename); |
| 459 |
|
| 460 |
res = adpcm_decode_data (infile, outfile, num_channels, num_samples, WaveHeader.BlockAlign); |
| 461 |
} |
| 462 |
|
| 463 |
fclose (outfile); |
| 464 |
fclose (infile); |
| 465 |
return res; |
| 466 |
} |
| 467 |
|
| 468 |
static int write_pcm_wav_header (FILE *outfile, int num_channels, uint32_t num_samples, uint32_t sample_rate) |
| 469 |
{ |
| 470 |
RiffChunkHeader riffhdr; |
| 471 |
ChunkHeader datahdr, fmthdr; |
| 472 |
WaveHeader wavhdr; |
| 473 |
|
| 474 |
int wavhdrsize = 16; |
| 475 |
int bytes_per_sample = 2; |
| 476 |
uint32_t total_data_bytes = num_samples * bytes_per_sample * num_channels; |
| 477 |
|
| 478 |
memset (&wavhdr, 0, sizeof (wavhdr)); |
| 479 |
|
| 480 |
wavhdr.FormatTag = WAVE_FORMAT_PCM; |
| 481 |
wavhdr.NumChannels = num_channels; |
| 482 |
wavhdr.SampleRate = sample_rate; |
| 483 |
wavhdr.BytesPerSecond = sample_rate * num_channels * bytes_per_sample; |
| 484 |
wavhdr.BlockAlign = bytes_per_sample * num_channels; |
| 485 |
wavhdr.BitsPerSample = 16; |
| 486 |
|
| 487 |
strncpy (riffhdr.ckID, "RIFF", sizeof (riffhdr.ckID)); |
| 488 |
strncpy (riffhdr.formType, "WAVE", sizeof (riffhdr.formType)); |
| 489 |
riffhdr.ckSize = sizeof (riffhdr) + wavhdrsize + sizeof (datahdr) + total_data_bytes; |
| 490 |
strncpy (fmthdr.ckID, "fmt ", sizeof (fmthdr.ckID)); |
| 491 |
fmthdr.ckSize = wavhdrsize; |
| 492 |
|
| 493 |
strncpy (datahdr.ckID, "data", sizeof (datahdr.ckID)); |
| 494 |
datahdr.ckSize = total_data_bytes; |
| 495 |
|
| 496 |
// write the RIFF chunks up to just before the data starts |
| 497 |
|
| 498 |
native_to_little_endian (&riffhdr, ChunkHeaderFormat); |
| 499 |
native_to_little_endian (&fmthdr, ChunkHeaderFormat); |
| 500 |
native_to_little_endian (&wavhdr, WaveHeaderFormat); |
| 501 |
native_to_little_endian (&datahdr, ChunkHeaderFormat); |
| 502 |
|
| 503 |
return fwrite (&riffhdr, sizeof (riffhdr), 1, outfile) && |
| 504 |
fwrite (&fmthdr, sizeof (fmthdr), 1, outfile) && |
| 505 |
fwrite (&wavhdr, wavhdrsize, 1, outfile) && |
| 506 |
fwrite (&datahdr, sizeof (datahdr), 1, outfile); |
| 507 |
} |
| 508 |
|
| 509 |
static int write_adpcm_wav_header (FILE *outfile, int num_channels, uint32_t num_samples, uint32_t sample_rate, int samples_per_block) |
| 510 |
{ |
| 511 |
RiffChunkHeader riffhdr; |
| 512 |
ChunkHeader datahdr, fmthdr; |
| 513 |
WaveHeader wavhdr; |
| 514 |
FactHeader facthdr; |
| 515 |
|
| 516 |
int wavhdrsize = 20; |
| 517 |
int block_size = (samples_per_block - 1) / (num_channels ^ 3) + (num_channels * 4); |
| 518 |
uint32_t num_blocks = num_samples / samples_per_block; |
| 519 |
int leftover_samples = num_samples % samples_per_block; |
| 520 |
uint32_t total_data_bytes = num_blocks * block_size; |
| 521 |
|
| 522 |
if (leftover_samples) { |
| 523 |
int last_block_samples = ((leftover_samples + 6) & ~7) + 1; |
| 524 |
int last_block_size = (last_block_samples - 1) / (num_channels ^ 3) + (num_channels * 4); |
| 525 |
total_data_bytes += last_block_size; |
| 526 |
} |
| 527 |
|
| 528 |
memset (&wavhdr, 0, sizeof (wavhdr)); |
| 529 |
|
| 530 |
wavhdr.FormatTag = WAVE_FORMAT_IMA_ADPCM; |
| 531 |
wavhdr.NumChannels = num_channels; |
| 532 |
wavhdr.SampleRate = sample_rate; |
| 533 |
wavhdr.BytesPerSecond = sample_rate * block_size / samples_per_block; |
| 534 |
wavhdr.BlockAlign = block_size; |
| 535 |
wavhdr.BitsPerSample = 4; |
| 536 |
wavhdr.cbSize = 2; |
| 537 |
wavhdr.Samples.SamplesPerBlock = samples_per_block; |
| 538 |
|
| 539 |
strncpy (riffhdr.ckID, "RIFF", sizeof (riffhdr.ckID)); |
| 540 |
strncpy (riffhdr.formType, "WAVE", sizeof (riffhdr.formType)); |
| 541 |
riffhdr.ckSize = sizeof (riffhdr) + wavhdrsize + sizeof (facthdr) + sizeof (datahdr) + total_data_bytes; |
| 542 |
strncpy (fmthdr.ckID, "fmt ", sizeof (fmthdr.ckID)); |
| 543 |
fmthdr.ckSize = wavhdrsize; |
| 544 |
strncpy (facthdr.ckID, "fact", sizeof (facthdr.ckID)); |
| 545 |
facthdr.TotalSamples = num_samples; |
| 546 |
facthdr.ckSize = 4; |
| 547 |
|
| 548 |
strncpy (datahdr.ckID, "data", sizeof (datahdr.ckID)); |
| 549 |
datahdr.ckSize = total_data_bytes; |
| 550 |
|
| 551 |
// write the RIFF chunks up to just before the data starts |
| 552 |
|
| 553 |
native_to_little_endian (&riffhdr, ChunkHeaderFormat); |
| 554 |
native_to_little_endian (&fmthdr, ChunkHeaderFormat); |
| 555 |
native_to_little_endian (&wavhdr, WaveHeaderFormat); |
| 556 |
native_to_little_endian (&facthdr, FactHeaderFormat); |
| 557 |
native_to_little_endian (&datahdr, ChunkHeaderFormat); |
| 558 |
|
| 559 |
return fwrite (&riffhdr, sizeof (riffhdr), 1, outfile) && |
| 560 |
fwrite (&fmthdr, sizeof (fmthdr), 1, outfile) && |
| 561 |
fwrite (&wavhdr, wavhdrsize, 1, outfile) && |
| 562 |
fwrite (&facthdr, sizeof (facthdr), 1, outfile) && |
| 563 |
fwrite (&datahdr, sizeof (datahdr), 1, outfile); |
| 564 |
} |
| 565 |
|
| 566 |
static int adpcm_decode_data (FILE *infile, FILE *outfile, int num_channels, uint32_t num_samples, int block_size) |
| 567 |
{ |
| 568 |
int samples_per_block = (block_size - num_channels * 4) * (num_channels ^ 3) + 1, percent; |
| 569 |
void *pcm_block = malloc (samples_per_block * num_channels * 2); |
| 570 |
void *adpcm_block = malloc (block_size); |
| 571 |
uint32_t progress_divider = 0; |
| 572 |
|
| 573 |
if (!pcm_block || !adpcm_block) { |
| 574 |
fprintf (stderr, "could not allocate memory for buffers!\n"); |
| 575 |
return -1; |
| 576 |
} |
| 577 |
|
| 578 |
if (verbosity >= 0 && num_samples > 1000) { |
| 579 |
progress_divider = (num_samples + 50) / 100; |
| 580 |
fprintf (stderr, "\rprogress: %d%% ", percent = 0); |
| 581 |
fflush (stderr); |
| 582 |
} |
| 583 |
|
| 584 |
while (num_samples) { |
| 585 |
int this_block_adpcm_samples = samples_per_block; |
| 586 |
int this_block_pcm_samples = samples_per_block; |
| 587 |
|
| 588 |
if (this_block_adpcm_samples > num_samples) { |
| 589 |
this_block_adpcm_samples = ((num_samples + 6) & ~7) + 1; |
| 590 |
block_size = (this_block_adpcm_samples - 1) / (num_channels ^ 3) + (num_channels * 4); |
| 591 |
this_block_pcm_samples = num_samples; |
| 592 |
} |
| 593 |
|
| 594 |
if (!fread (adpcm_block, block_size, 1, infile)) { |
| 595 |
fprintf (stderr, "could not read all audio data from input file!\n"); |
| 596 |
return -1; |
| 597 |
} |
| 598 |
|
| 599 |
if (adpcm_decode_block (pcm_block, adpcm_block, block_size, num_channels) != this_block_adpcm_samples) { |
| 600 |
fprintf (stderr, "adpcm_decode_block() did not return expected value!\n"); |
| 601 |
return -1; |
| 602 |
} |
| 603 |
|
| 604 |
if (IS_BIG_ENDIAN) { |
| 605 |
int scount = this_block_pcm_samples * num_channels; |
| 606 |
unsigned char *cp = (unsigned char *) pcm_block; |
| 607 |
|
| 608 |
while (scount--) { |
| 609 |
int16_t temp = * (int16_t *) cp; |
| 610 |
*cp++ = (unsigned char) temp; |
| 611 |
*cp++ = (unsigned char) (temp >> 8); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
if (!fwrite (pcm_block, this_block_pcm_samples * num_channels * 2, 1, outfile)) { |
| 616 |
fprintf (stderr, "could not write all audio data to output file!\n"); |
| 617 |
return -1; |
| 618 |
} |
| 619 |
|
| 620 |
num_samples -= this_block_pcm_samples; |
| 621 |
|
| 622 |
if (progress_divider) { |
| 623 |
int new_percent = 100 - num_samples / progress_divider; |
| 624 |
|
| 625 |
if (new_percent != percent) { |
| 626 |
fprintf (stderr, "\rprogress: %d%% ", percent = new_percent); |
| 627 |
fflush (stderr); |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
if (verbosity >= 0) |
| 633 |
fprintf (stderr, "\r...completed successfully\n"); |
| 634 |
|
| 635 |
free (adpcm_block); |
| 636 |
free (pcm_block); |
| 637 |
return 0; |
| 638 |
} |
| 639 |
|
| 640 |
static int adpcm_encode_data (FILE *infile, FILE *outfile, int num_channels, uint32_t num_samples, int samples_per_block, int lookahead, int noise_shaping) |
| 641 |
{ |
| 642 |
int block_size = (samples_per_block - 1) / (num_channels ^ 3) + (num_channels * 4), percent; |
| 643 |
int16_t *pcm_block = malloc (samples_per_block * num_channels * 2); |
| 644 |
void *adpcm_block = malloc (block_size); |
| 645 |
uint32_t progress_divider = 0; |
| 646 |
void *adpcm_cnxt = NULL; |
| 647 |
|
| 648 |
if (!pcm_block || !adpcm_block) { |
| 649 |
fprintf (stderr, "could not allocate memory for buffers!\n"); |
| 650 |
return -1; |
| 651 |
} |
| 652 |
|
| 653 |
if (verbosity >= 0 && num_samples > 1000) { |
| 654 |
progress_divider = (num_samples + 50) / 100; |
| 655 |
fprintf (stderr, "\rprogress: %d%% ", percent = 0); |
| 656 |
fflush (stderr); |
| 657 |
} |
| 658 |
|
| 659 |
while (num_samples) { |
| 660 |
int this_block_adpcm_samples = samples_per_block; |
| 661 |
int this_block_pcm_samples = samples_per_block; |
| 662 |
size_t num_bytes; |
| 663 |
|
| 664 |
if (this_block_pcm_samples > num_samples) { |
| 665 |
this_block_adpcm_samples = ((num_samples + 6) & ~7) + 1; |
| 666 |
block_size = (this_block_adpcm_samples - 1) / (num_channels ^ 3) + (num_channels * 4); |
| 667 |
this_block_pcm_samples = num_samples; |
| 668 |
} |
| 669 |
|
| 670 |
if (!fread (pcm_block, this_block_pcm_samples * num_channels * 2, 1, infile)) { |
| 671 |
fprintf (stderr, "\rcould not read all audio data from input file!\n"); |
| 672 |
return -1; |
| 673 |
} |
| 674 |
|
| 675 |
if (IS_BIG_ENDIAN) { |
| 676 |
int scount = this_block_pcm_samples * num_channels; |
| 677 |
unsigned char *cp = (unsigned char *) pcm_block; |
| 678 |
|
| 679 |
while (scount--) { |
| 680 |
int16_t temp = cp [0] + (cp [1] << 8); |
| 681 |
* (int16_t *) cp = temp; |
| 682 |
cp += 2; |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
// if this is the last block and it's not full, duplicate the last sample(s) so we don't |
| 687 |
// create problems for the lookahead |
| 688 |
|
| 689 |
if (this_block_adpcm_samples > this_block_pcm_samples) { |
| 690 |
int16_t *dst = pcm_block + this_block_pcm_samples * num_channels, *src = dst - num_channels; |
| 691 |
int dups = (this_block_adpcm_samples - this_block_pcm_samples) * num_channels; |
| 692 |
|
| 693 |
while (dups--) |
| 694 |
*dst++ = *src++; |
| 695 |
} |
| 696 |
|
| 697 |
// if this is the first block, compute a decaying average (in reverse) so that we can let the |
| 698 |
// encoder know what kind of initial deltas to expect (helps initializing index) |
| 699 |
|
| 700 |
if (!adpcm_cnxt) { |
| 701 |
int32_t average_deltas [2]; |
| 702 |
int i; |
| 703 |
|
| 704 |
average_deltas [0] = average_deltas [1] = 0; |
| 705 |
|
| 706 |
for (i = this_block_adpcm_samples * num_channels; i -= num_channels;) { |
| 707 |
average_deltas [0] -= average_deltas [0] >> 3; |
| 708 |
average_deltas [0] += abs ((int32_t) pcm_block [i] - pcm_block [i - num_channels]); |
| 709 |
|
| 710 |
if (num_channels == 2) { |
| 711 |
average_deltas [1] -= average_deltas [1] >> 3; |
| 712 |
average_deltas [1] += abs ((int32_t) pcm_block [i-1] - pcm_block [i+1]); |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
average_deltas [0] >>= 3; |
| 717 |
average_deltas [1] >>= 3; |
| 718 |
|
| 719 |
adpcm_cnxt = adpcm_create_context (num_channels, lookahead, noise_shaping, average_deltas); |
| 720 |
} |
| 721 |
|
| 722 |
adpcm_encode_block (adpcm_cnxt, adpcm_block, &num_bytes, pcm_block, this_block_adpcm_samples); |
| 723 |
|
| 724 |
if (num_bytes != block_size) { |
| 725 |
fprintf (stderr, "\radpcm_encode_block() did not return expected value (expected %d, got %d)!\n", block_size, (int) num_bytes); |
| 726 |
return -1; |
| 727 |
} |
| 728 |
|
| 729 |
if (!fwrite (adpcm_block, block_size, 1, outfile)) { |
| 730 |
fprintf (stderr, "\rcould not write all audio data to output file!\n"); |
| 731 |
return -1; |
| 732 |
} |
| 733 |
|
| 734 |
num_samples -= this_block_pcm_samples; |
| 735 |
|
| 736 |
if (progress_divider) { |
| 737 |
int new_percent = 100 - num_samples / progress_divider; |
| 738 |
|
| 739 |
if (new_percent != percent) { |
| 740 |
fprintf (stderr, "\rprogress: %d%% ", percent = new_percent); |
| 741 |
fflush (stderr); |
| 742 |
} |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
if (verbosity >= 0) |
| 747 |
fprintf (stderr, "\r...completed successfully\n"); |
| 748 |
|
| 749 |
if (adpcm_cnxt) |
| 750 |
adpcm_free_context (adpcm_cnxt); |
| 751 |
|
| 752 |
free (adpcm_block); |
| 753 |
free (pcm_block); |
| 754 |
return 0; |
| 755 |
} |
| 756 |
|
| 757 |
static void little_endian_to_native (void *data, char *format) |
| 758 |
{ |
| 759 |
unsigned char *cp = (unsigned char *) data; |
| 760 |
int32_t temp; |
| 761 |
|
| 762 |
while (*format) { |
| 763 |
switch (*format) { |
| 764 |
case 'L': |
| 765 |
temp = cp [0] + ((int32_t) cp [1] << 8) + ((int32_t) cp [2] << 16) + ((int32_t) cp [3] << 24); |
| 766 |
* (int32_t *) cp = temp; |
| 767 |
cp += 4; |
| 768 |
break; |
| 769 |
|
| 770 |
case 'S': |
| 771 |
temp = cp [0] + (cp [1] << 8); |
| 772 |
* (short *) cp = (short) temp; |
| 773 |
cp += 2; |
| 774 |
break; |
| 775 |
|
| 776 |
default: |
| 777 |
if (isdigit ((unsigned char) *format)) |
| 778 |
cp += *format - '0'; |
| 779 |
|
| 780 |
break; |
| 781 |
} |
| 782 |
|
| 783 |
format++; |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
static void native_to_little_endian (void *data, char *format) |
| 788 |
{ |
| 789 |
unsigned char *cp = (unsigned char *) data; |
| 790 |
int32_t temp; |
| 791 |
|
| 792 |
while (*format) { |
| 793 |
switch (*format) { |
| 794 |
case 'L': |
| 795 |
temp = * (int32_t *) cp; |
| 796 |
*cp++ = (unsigned char) temp; |
| 797 |
*cp++ = (unsigned char) (temp >> 8); |
| 798 |
*cp++ = (unsigned char) (temp >> 16); |
| 799 |
*cp++ = (unsigned char) (temp >> 24); |
| 800 |
break; |
| 801 |
|
| 802 |
case 'S': |
| 803 |
temp = * (short *) cp; |
| 804 |
*cp++ = (unsigned char) temp; |
| 805 |
*cp++ = (unsigned char) (temp >> 8); |
| 806 |
break; |
| 807 |
|
| 808 |
default: |
| 809 |
if (isdigit ((unsigned char) *format)) |
| 810 |
cp += *format - '0'; |
| 811 |
|
| 812 |
break; |
| 813 |
} |
| 814 |
|
| 815 |
format++; |
| 816 |
} |
| 817 |
} |
| 818 |
#endif |
| 819 |
|