| 1 |
/* |
| 2 |
* Get Audio routines source file |
| 3 |
* |
| 4 |
* Copyright (c) 1999 Albert L Faber |
| 5 |
* 2008-2017 Robert Hegemann |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Library General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Library General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Library General Public |
| 18 |
* License along with this library; if not, write to the |
| 19 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 |
* Boston, MA 02111-1307, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* $Id$ */ |
| 24 |
|
| 25 |
|
| 26 |
#ifdef HAVE_CONFIG_H |
| 27 |
# include <config.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <assert.h> |
| 31 |
|
| 32 |
#ifdef HAVE_LIMITS_H |
| 33 |
# include <limits.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
|
| 38 |
#ifdef STDC_HEADERS |
| 39 |
# include <stdlib.h> |
| 40 |
# include <string.h> |
| 41 |
#else |
| 42 |
# ifndef HAVE_STRCHR |
| 43 |
# define strchr index |
| 44 |
# define strrchr rindex |
| 45 |
# endif |
| 46 |
char *strchr(), *strrchr(); |
| 47 |
# ifndef HAVE_MEMCPY |
| 48 |
# define memcpy(d, s, n) bcopy ((s), (d), (n)) |
| 49 |
# define memmove(d, s, n) bcopy ((s), (d), (n)) |
| 50 |
# endif |
| 51 |
#endif |
| 52 |
|
| 53 |
#ifdef HAVE_INTTYPES_H |
| 54 |
# include <inttypes.h> |
| 55 |
#else |
| 56 |
# ifdef HAVE_STDINT_H |
| 57 |
# include <stdint.h> |
| 58 |
# endif |
| 59 |
#endif |
| 60 |
|
| 61 |
#if defined(HAVE_MPGLIB) || defined(HAVE_MPG123) |
| 62 |
#define hip_global_struct mpstr_tag |
| 63 |
#endif |
| 64 |
#ifdef HAVE_MPG123 |
| 65 |
#include <mpg123.h> |
| 66 |
/* for mpstr_tag */ |
| 67 |
#include "mpglib/mpglib.h" |
| 68 |
|
| 69 |
#endif |
| 70 |
|
| 71 |
#define MAX_U_32_NUM 0xFFFFFFFF |
| 72 |
|
| 73 |
|
| 74 |
#include <math.h> |
| 75 |
|
| 76 |
#if defined(__riscos__) |
| 77 |
# include <kernel.h> |
| 78 |
# include <sys/swis.h> |
| 79 |
#elif defined(_WIN32) |
| 80 |
# include <sys/types.h> |
| 81 |
# include <sys/stat.h> |
| 82 |
#else |
| 83 |
# include <sys/stat.h> |
| 84 |
#endif |
| 85 |
|
| 86 |
#ifdef __sun__ |
| 87 |
/* woraround for SunOS 4.x, it has SEEK_* defined here */ |
| 88 |
#include <unistd.h> |
| 89 |
#endif |
| 90 |
|
| 91 |
#include "lame.h" |
| 92 |
#include "main.h" |
| 93 |
#include "get_audio.h" |
| 94 |
#include "lametime.h" |
| 95 |
#include "console.h" |
| 96 |
#include "machine.h" |
| 97 |
#include "encoder.h" |
| 98 |
#include "lame-analysis.h" |
| 99 |
|
| 100 |
#ifdef WITH_DMALLOC |
| 101 |
#include <dmalloc.h> |
| 102 |
#endif |
| 103 |
|
| 104 |
/* In util.h, but including it here needs much more includes than util.h. */ |
| 105 |
extern void hip_set_pinfo(hip_t hip, plotting_data* pinfo); |
| 106 |
extern void hip_finish_pinfo(hip_t hip); |
| 107 |
|
| 108 |
#ifndef STR |
| 109 |
# define __STR(x) #x |
| 110 |
# define STR(x) __STR(x) |
| 111 |
#define __LOC__ __FILE__ "("STR(__LINE__)") : " |
| 112 |
#endif |
| 113 |
|
| 114 |
|
| 115 |
#define FLOAT_TO_UNSIGNED(f) ((unsigned long)(((long)((f) - 2147483648.0)) + 2147483647L + 1)) |
| 116 |
#define UNSIGNED_TO_FLOAT(u) (((double)((long)((u) - 2147483647L - 1))) + 2147483648.0) |
| 117 |
|
| 118 |
static uint32_t uint32_high_low(unsigned char const *bytes) |
| 119 |
{ |
| 120 |
uint32_t const hh = bytes[0]; |
| 121 |
uint32_t const hl = bytes[1]; |
| 122 |
uint32_t const lh = bytes[2]; |
| 123 |
uint32_t const ll = bytes[3]; |
| 124 |
return (hh << 24) | (hl << 16) | (lh << 8) | ll; |
| 125 |
} |
| 126 |
|
| 127 |
static double |
| 128 |
read_ieee_extended_high_low(FILE * fp) |
| 129 |
{ |
| 130 |
unsigned char bytes[10]; |
| 131 |
memset(bytes, 0, 10); |
| 132 |
fread(bytes, 1, 10, fp); |
| 133 |
{ |
| 134 |
int32_t const s = (bytes[0] & 0x80); |
| 135 |
int32_t const e_h = (bytes[0] & 0x7F); |
| 136 |
int32_t const e_l = bytes[1]; |
| 137 |
int32_t e = (e_h << 8) | e_l; |
| 138 |
uint32_t const hm = uint32_high_low(bytes + 2); |
| 139 |
uint32_t const lm = uint32_high_low(bytes + 6); |
| 140 |
double result = 0; |
| 141 |
if (e != 0 || hm != 0 || lm != 0) { |
| 142 |
if (e == 0x7fff) { |
| 143 |
result = HUGE_VAL; |
| 144 |
} |
| 145 |
else { |
| 146 |
double mantissa_h = UNSIGNED_TO_FLOAT(hm); |
| 147 |
double mantissa_l = UNSIGNED_TO_FLOAT(lm); |
| 148 |
e -= 0x3fff; |
| 149 |
e -= 31; |
| 150 |
result = ldexp(mantissa_h, e); |
| 151 |
e -= 32; |
| 152 |
result += ldexp(mantissa_l, e); |
| 153 |
} |
| 154 |
} |
| 155 |
return s ? -result : result; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
static uint16_t |
| 161 |
read_16_bits_low_high(FILE * fp) |
| 162 |
{ |
| 163 |
unsigned char bytes[2] = { 0, 0 }; |
| 164 |
fread(bytes, 1, 2, fp); |
| 165 |
{ |
| 166 |
uint16_t const l = bytes[0]; |
| 167 |
uint16_t const h = bytes[1]; |
| 168 |
return (h << 8) | l; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
static uint32_t |
| 174 |
read_32_bits_low_high(FILE * fp) |
| 175 |
{ |
| 176 |
unsigned char bytes[4] = { 0, 0, 0, 0 }; |
| 177 |
fread(bytes, 1, 4, fp); |
| 178 |
{ |
| 179 |
uint32_t const ll = bytes[0]; |
| 180 |
uint32_t const lh = bytes[1]; |
| 181 |
uint32_t const hl = bytes[2]; |
| 182 |
uint32_t const hh = bytes[3]; |
| 183 |
return (hh << 24) | (hl << 16) | (lh << 8) | ll; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
static uint16_t |
| 188 |
read_16_bits_high_low(FILE * fp) |
| 189 |
{ |
| 190 |
unsigned char bytes[2] = { 0, 0 }; |
| 191 |
fread(bytes, 1, 2, fp); |
| 192 |
{ |
| 193 |
uint16_t const h = bytes[0]; |
| 194 |
uint16_t const l = bytes[1]; |
| 195 |
return (h << 8) | l; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
static uint32_t |
| 200 |
read_32_bits_high_low(FILE * fp) |
| 201 |
{ |
| 202 |
unsigned char bytes[4] = { 0, 0, 0, 0 }; |
| 203 |
fread(bytes, 1, 4, fp); |
| 204 |
return uint32_high_low(bytes); |
| 205 |
} |
| 206 |
|
| 207 |
static void |
| 208 |
write_16_bits_low_high(FILE * fp, int val) |
| 209 |
{ |
| 210 |
unsigned char bytes[2]; |
| 211 |
bytes[0] = (val & 0xff); |
| 212 |
bytes[1] = ((val >> 8) & 0xff); |
| 213 |
fwrite(bytes, 1, 2, fp); |
| 214 |
} |
| 215 |
|
| 216 |
static void |
| 217 |
write_32_bits_low_high(FILE * fp, int val) |
| 218 |
{ |
| 219 |
unsigned char bytes[4]; |
| 220 |
bytes[0] = (val & 0xff); |
| 221 |
bytes[1] = ((val >> 8) & 0xff); |
| 222 |
bytes[2] = ((val >> 16) & 0xff); |
| 223 |
bytes[3] = ((val >> 24) & 0xff); |
| 224 |
fwrite(bytes, 1, 4, fp); |
| 225 |
} |
| 226 |
|
| 227 |
#ifdef LIBSNDFILE |
| 228 |
|
| 229 |
#include <sndfile.h> |
| 230 |
|
| 231 |
|
| 232 |
#else |
| 233 |
|
| 234 |
typedef void SNDFILE; |
| 235 |
|
| 236 |
#endif /* ifdef LIBSNDFILE */ |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
typedef struct blockAlign_struct { |
| 241 |
uint32_t offset; |
| 242 |
uint32_t blockSize; |
| 243 |
} blockAlign; |
| 244 |
|
| 245 |
typedef struct IFF_AIFF_struct { |
| 246 |
short numChannels; |
| 247 |
unsigned long numSampleFrames; |
| 248 |
short sampleSize; |
| 249 |
double sampleRate; |
| 250 |
uint32_t sampleType; |
| 251 |
uint32_t sampleFormat; |
| 252 |
blockAlign blkAlgn; |
| 253 |
} IFF_AIFF; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
struct PcmBuffer { |
| 258 |
void *ch[2]; /* buffer for each channel */ |
| 259 |
int w; /* sample width */ |
| 260 |
int n; /* number samples allocated */ |
| 261 |
int u; /* number samples used */ |
| 262 |
int skip_start; /* number samples to ignore at the beginning */ |
| 263 |
int skip_end; /* number samples to ignore at the end */ |
| 264 |
}; |
| 265 |
|
| 266 |
typedef struct PcmBuffer PcmBuffer; |
| 267 |
|
| 268 |
static void |
| 269 |
initPcmBuffer(PcmBuffer * b, int w) |
| 270 |
{ |
| 271 |
b->ch[0] = 0; |
| 272 |
b->ch[1] = 0; |
| 273 |
b->w = w; |
| 274 |
b->n = 0; |
| 275 |
b->u = 0; |
| 276 |
b->skip_start = 0; |
| 277 |
b->skip_end = 0; |
| 278 |
} |
| 279 |
|
| 280 |
static void |
| 281 |
freePcmBuffer(PcmBuffer * b) |
| 282 |
{ |
| 283 |
if (b != 0) { |
| 284 |
free(b->ch[0]); |
| 285 |
free(b->ch[1]); |
| 286 |
b->ch[0] = 0; |
| 287 |
b->ch[1] = 0; |
| 288 |
b->n = 0; |
| 289 |
b->u = 0; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
static int |
| 294 |
addPcmBuffer(PcmBuffer * b, void *a0, void *a1, int read) |
| 295 |
{ |
| 296 |
int a_n; |
| 297 |
|
| 298 |
if (b == 0) { |
| 299 |
return 0; |
| 300 |
} |
| 301 |
if (read < 0) { |
| 302 |
return b->u - b->skip_end; |
| 303 |
} |
| 304 |
if (b->skip_start >= read) { |
| 305 |
b->skip_start -= read; |
| 306 |
return b->u - b->skip_end; |
| 307 |
} |
| 308 |
a_n = read - b->skip_start; |
| 309 |
|
| 310 |
if (a_n > 0) { |
| 311 |
int const a_skip = b->w * b->skip_start; |
| 312 |
int const a_want = b->w * a_n; |
| 313 |
int const b_used = b->w * b->u; |
| 314 |
int const b_have = b->w * b->n; |
| 315 |
int const b_need = b->w * (b->u + a_n); |
| 316 |
if (b_have < b_need) { |
| 317 |
b->n = b->u + a_n; |
| 318 |
b->ch[0] = realloc(b->ch[0], b_need); |
| 319 |
b->ch[1] = realloc(b->ch[1], b_need); |
| 320 |
} |
| 321 |
b->u += a_n; |
| 322 |
if (b->ch[0] != 0 && a0 != 0) { |
| 323 |
char *src = a0; |
| 324 |
char *dst = b->ch[0]; |
| 325 |
memcpy(dst + b_used, src + a_skip, a_want); |
| 326 |
} |
| 327 |
if (b->ch[1] != 0 && a1 != 0) { |
| 328 |
char *src = a1; |
| 329 |
char *dst = b->ch[1]; |
| 330 |
memcpy(dst + b_used, src + a_skip, a_want); |
| 331 |
} |
| 332 |
} |
| 333 |
b->skip_start = 0; |
| 334 |
return b->u - b->skip_end; |
| 335 |
} |
| 336 |
|
| 337 |
static int |
| 338 |
takePcmBuffer(PcmBuffer * b, void *a0, void *a1, int a_n, int mm) |
| 339 |
{ |
| 340 |
if (a_n > mm) { |
| 341 |
a_n = mm; |
| 342 |
} |
| 343 |
if (b != 0 && a_n > 0) { |
| 344 |
int const a_take = b->w * a_n; |
| 345 |
if (a0 != 0 && b->ch[0] != 0) { |
| 346 |
memcpy(a0, b->ch[0], a_take); |
| 347 |
} |
| 348 |
if (a1 != 0 && b->ch[1] != 0) { |
| 349 |
memcpy(a1, b->ch[1], a_take); |
| 350 |
} |
| 351 |
b->u -= a_n; |
| 352 |
if (b->u < 0) { |
| 353 |
b->u = 0; |
| 354 |
return a_n; |
| 355 |
} |
| 356 |
if (b->ch[0] != 0) { |
| 357 |
memmove(b->ch[0], (char *) b->ch[0] + a_take, b->w * b->u); |
| 358 |
} |
| 359 |
if (b->ch[1] != 0) { |
| 360 |
memmove(b->ch[1], (char *) b->ch[1] + a_take, b->w * b->u); |
| 361 |
} |
| 362 |
} |
| 363 |
return a_n; |
| 364 |
} |
| 365 |
|
| 366 |
/* global data for get_audio.c. */ |
| 367 |
typedef struct get_audio_global_data_struct { |
| 368 |
int count_samples_carefully; |
| 369 |
int pcmbitwidth; |
| 370 |
int pcmswapbytes; |
| 371 |
int pcm_is_unsigned_8bit; |
| 372 |
int pcm_is_ieee_float; |
| 373 |
unsigned int num_samples_read; |
| 374 |
FILE *music_in; |
| 375 |
SNDFILE *snd_file; |
| 376 |
#ifdef HAVE_MPG123 |
| 377 |
mpg123_handle* mh; |
| 378 |
#endif |
| 379 |
hip_t hip; |
| 380 |
PcmBuffer pcm32; |
| 381 |
PcmBuffer pcm16; |
| 382 |
size_t in_id3v2_size; |
| 383 |
unsigned char* in_id3v2_tag; |
| 384 |
} get_audio_global_data; |
| 385 |
|
| 386 |
static get_audio_global_data global; |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
#ifdef AMIGA_MPEGA |
| 391 |
int lame_decode_initfile_amiga(const char *fullname, mp3data_struct * const mp3data); |
| 392 |
#else |
| 393 |
int lame_decode_initfile(FILE * fd, mp3data_struct * mp3data, int *enc_delay, int *enc_padding); |
| 394 |
#endif |
| 395 |
#ifdef HAVE_MPG123 |
| 396 |
int lame123_decode_initfile(FILE * fd, mp3data_struct * mp3data, int *enc_delay, int *enc_padding); |
| 397 |
#endif |
| 398 |
|
| 399 |
/* read mp3 file until mpglib returns one frame of PCM data */ |
| 400 |
static int lame_decode_fromfile(FILE * fd, short int pcm_l[], short int pcm_r[], |
| 401 |
mp3data_struct * mp3data); |
| 402 |
|
| 403 |
|
| 404 |
static int read_samples_pcm(FILE * musicin, int sample_buffer[2304], int samples_to_read); |
| 405 |
static int read_samples_mp3(lame_t gfp, FILE * musicin, short int mpg123pcm[2][1152]); |
| 406 |
#ifdef LIBSNDFILE |
| 407 |
static SNDFILE *open_snd_file(lame_t gfp, char const *inPath); |
| 408 |
#endif |
| 409 |
static FILE *open_mpeg_file(lame_t gfp, char const *inPath, int *enc_delay, int *enc_padding); |
| 410 |
static FILE *open_wave_file(lame_t gfp, char const *inPath, int *enc_delay, int *enc_padding); |
| 411 |
static int close_input_file(FILE * musicin); |
| 412 |
|
| 413 |
|
| 414 |
static size_t |
| 415 |
min_size_t(size_t a, size_t b) |
| 416 |
{ |
| 417 |
if (a < b) { |
| 418 |
return a; |
| 419 |
} |
| 420 |
return b; |
| 421 |
} |
| 422 |
|
| 423 |
enum ByteOrder machine_byte_order(void); |
| 424 |
|
| 425 |
enum ByteOrder |
| 426 |
machine_byte_order(void) |
| 427 |
{ |
| 428 |
long one = 1; |
| 429 |
return !(*((char *) (&one))) ? ByteOrderBigEndian : ByteOrderLittleEndian; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
/* Replacement for forward fseek(,,SEEK_CUR), because fseek() fails on pipes */ |
| 435 |
|
| 436 |
|
| 437 |
static int |
| 438 |
fskip_long(FILE * fp, long offset, int whence) |
| 439 |
{ |
| 440 |
#ifndef PIPE_BUF |
| 441 |
char buffer[4096]; |
| 442 |
#else |
| 443 |
char buffer[PIPE_BUF]; |
| 444 |
#endif |
| 445 |
|
| 446 |
/* S_ISFIFO macro is defined on newer Linuxes */ |
| 447 |
#ifndef S_ISFIFO |
| 448 |
# ifdef _S_IFIFO |
| 449 |
/* _S_IFIFO is defined on Win32 and Cygwin */ |
| 450 |
# define S_ISFIFO(m) (((m)&_S_IFIFO) == _S_IFIFO) |
| 451 |
# endif |
| 452 |
#endif |
| 453 |
|
| 454 |
#ifdef S_ISFIFO |
| 455 |
/* fseek is known to fail on pipes with several C-Library implementations |
| 456 |
workaround: 1) test for pipe |
| 457 |
2) for pipes, only relatvie seeking is possible |
| 458 |
3) and only in forward direction! |
| 459 |
else fallback to old code |
| 460 |
*/ |
| 461 |
{ |
| 462 |
int const fd = fileno(fp); |
| 463 |
struct stat file_stat; |
| 464 |
|
| 465 |
if (fstat(fd, &file_stat) == 0) { |
| 466 |
if (S_ISFIFO(file_stat.st_mode)) { |
| 467 |
if (whence != SEEK_CUR || offset < 0) { |
| 468 |
return -1; |
| 469 |
} |
| 470 |
while (offset > 0) { |
| 471 |
size_t const bytes_to_skip = min_size_t(sizeof(buffer), offset); |
| 472 |
size_t const read = fread(buffer, 1, bytes_to_skip, fp); |
| 473 |
if (read < 1) { |
| 474 |
return -1; |
| 475 |
} |
| 476 |
assert( read <= LONG_MAX ); |
| 477 |
offset -= (long) read; |
| 478 |
} |
| 479 |
return 0; |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
#endif |
| 484 |
if (0 == fseek(fp, offset, whence)) { |
| 485 |
return 0; |
| 486 |
} |
| 487 |
|
| 488 |
if (whence != SEEK_CUR || offset < 0) { |
| 489 |
if (global_ui_config.silent < 10) { |
| 490 |
error_printf |
| 491 |
("fskip problem: Mostly the return status of functions is not evaluated, so it is more secure to pollute <stderr>.\n"); |
| 492 |
} |
| 493 |
return -1; |
| 494 |
} |
| 495 |
|
| 496 |
while (offset > 0) { |
| 497 |
size_t const bytes_to_skip = min_size_t(sizeof(buffer), offset); |
| 498 |
size_t const read = fread(buffer, 1, bytes_to_skip, fp); |
| 499 |
if (read < 1) { |
| 500 |
return -1; |
| 501 |
} |
| 502 |
assert( read <= LONG_MAX ); |
| 503 |
offset -= (long) read; |
| 504 |
} |
| 505 |
|
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
static int |
| 510 |
fskip_uint32(FILE * fp, uint32_t offset) |
| 511 |
{ |
| 512 |
int ret = 0; |
| 513 |
while (offset > INT_MAX && ret == 0) { |
| 514 |
offset -= INT_MAX; |
| 515 |
ret = fskip_long(fp, INT_MAX, SEEK_CUR); |
| 516 |
} |
| 517 |
if (offset > 0 && ret == 0) { |
| 518 |
ret = fskip_long(fp, offset, SEEK_CUR); |
| 519 |
} |
| 520 |
return ret; |
| 521 |
} |
| 522 |
|
| 523 |
static off_t |
| 524 |
lame_get_file_size(FILE * fp) |
| 525 |
{ |
| 526 |
struct stat sb; |
| 527 |
int fd = fileno(fp); |
| 528 |
|
| 529 |
if (0 == fstat(fd, &sb)) |
| 530 |
return sb.st_size; |
| 531 |
return (off_t) - 1; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
FILE * |
| 536 |
init_outfile(char const *outPath, int decode) |
| 537 |
{ |
| 538 |
FILE *outf; |
| 539 |
|
| 540 |
/* open the output file */ |
| 541 |
if (0 == strcmp(outPath, "-")) { |
| 542 |
outf = stdout; |
| 543 |
lame_set_stream_binary_mode(outf); |
| 544 |
} |
| 545 |
else { |
| 546 |
outf = lame_fopen(outPath, "w+b"); |
| 547 |
#ifdef __riscos__ |
| 548 |
/* Assign correct file type */ |
| 549 |
if (outf != NULL) { |
| 550 |
char *p, *out_path = strdup(outPath); |
| 551 |
for (p = out_path; *p; p++) { /* ugly, ugly to modify a string */ |
| 552 |
switch (*p) { |
| 553 |
case '.': |
| 554 |
*p = '/'; |
| 555 |
break; |
| 556 |
case '/': |
| 557 |
*p = '.'; |
| 558 |
break; |
| 559 |
} |
| 560 |
} |
| 561 |
SetFiletype(out_path, decode ? 0xFB1 /*WAV*/ : 0x1AD /*AMPEG*/); |
| 562 |
free(out_path); |
| 563 |
} |
| 564 |
#else |
| 565 |
(void) decode; |
| 566 |
#endif |
| 567 |
} |
| 568 |
return outf; |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
static void |
| 573 |
setSkipStartAndEnd(lame_t gfp, int enc_delay, int enc_padding) |
| 574 |
{ |
| 575 |
int skip_start = 0, skip_end = 0; |
| 576 |
long dec_delay = -1; |
| 577 |
|
| 578 |
if (global_decoder.mp3_delay_set) |
| 579 |
skip_start = global_decoder.mp3_delay; |
| 580 |
|
| 581 |
#if 0 |
| 582 |
/* We should ask mpg123 for the delay, but we know it is 529 samples and |
| 583 |
will not change unless we enable gapless mode. Also, global.hip is not |
| 584 |
always the correct handle, so avoid this for now. */ |
| 585 |
/* Will use it for layer III only, mpg123 does not deal with layer I and II |
| 586 |
gapless stuff (yet?) */ |
| 587 |
mpg123_getstate(global.hip->mh, MPG123_DEC_DELAY, &dec_delay, NULL); |
| 588 |
#else |
| 589 |
if(dec_delay < 0) |
| 590 |
dec_delay = 528 + 1; /* Same value as above, actually. */ |
| 591 |
#endif |
| 592 |
|
| 593 |
switch (global_reader.input_format) { |
| 594 |
case sf_mp123: |
| 595 |
break; |
| 596 |
|
| 597 |
case sf_mp3: |
| 598 |
if (skip_start == 0) { |
| 599 |
if (enc_delay > -1 || enc_padding > -1) { |
| 600 |
if (enc_delay > -1) |
| 601 |
skip_start = enc_delay + dec_delay; |
| 602 |
if (enc_padding > -1) |
| 603 |
skip_end = enc_padding - dec_delay; |
| 604 |
} |
| 605 |
else |
| 606 |
skip_start = lame_get_encoder_delay(gfp) + dec_delay; |
| 607 |
} |
| 608 |
else { |
| 609 |
/* user specified a value of skip. just add for decoder */ |
| 610 |
skip_start += dec_delay; /* mp3 decoder has a 528 sample delay, plus user supplied "skip" */ |
| 611 |
} |
| 612 |
break; |
| 613 |
case sf_mp2: |
| 614 |
skip_start += 240 + 1; |
| 615 |
break; |
| 616 |
case sf_mp1: |
| 617 |
skip_start += 240 + 1; |
| 618 |
break; |
| 619 |
case sf_raw: |
| 620 |
skip_start += 0; /* other formats have no delay *//* is += 0 not better ??? */ |
| 621 |
break; |
| 622 |
case sf_wave: |
| 623 |
skip_start += 0; /* other formats have no delay *//* is += 0 not better ??? */ |
| 624 |
break; |
| 625 |
case sf_aiff: |
| 626 |
skip_start += 0; /* other formats have no delay *//* is += 0 not better ??? */ |
| 627 |
break; |
| 628 |
default: |
| 629 |
skip_start += 0; /* other formats have no delay *//* is += 0 not better ??? */ |
| 630 |
break; |
| 631 |
} |
| 632 |
skip_start = skip_start < 0 ? 0 : skip_start; |
| 633 |
skip_end = skip_end < 0 ? 0 : skip_end; |
| 634 |
global. pcm16.skip_start = global.pcm32.skip_start = skip_start; |
| 635 |
global. pcm16.skip_end = global.pcm32.skip_end = skip_end; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
int |
| 641 |
init_infile(lame_t gfp, char const *inPath) |
| 642 |
{ |
| 643 |
int enc_delay = 0, enc_padding = 0; |
| 644 |
/* open the input file */ |
| 645 |
global. count_samples_carefully = 0; |
| 646 |
global. num_samples_read = 0; |
| 647 |
global. pcmbitwidth = global_raw_pcm.in_bitwidth; |
| 648 |
global. pcmswapbytes = global_reader.swapbytes; |
| 649 |
global. pcm_is_unsigned_8bit = global_raw_pcm.in_signed == 1 ? 0 : 1; |
| 650 |
global. pcm_is_ieee_float = 0; |
| 651 |
global. hip = 0; |
| 652 |
global. music_in = 0; |
| 653 |
global. snd_file = 0; |
| 654 |
global. in_id3v2_size = 0; |
| 655 |
global. in_id3v2_tag = 0; |
| 656 |
if (is_mpeg_file_format(global_reader.input_format)) { |
| 657 |
global. music_in = open_mpeg_file(gfp, inPath, &enc_delay, &enc_padding); |
| 658 |
} |
| 659 |
else { |
| 660 |
#ifdef LIBSNDFILE |
| 661 |
if (strcmp(inPath, "-") != 0) { /* not for stdin */ |
| 662 |
global. snd_file = open_snd_file(gfp, inPath); |
| 663 |
} |
| 664 |
#endif |
| 665 |
if (global.snd_file == 0) { |
| 666 |
global. music_in = open_wave_file(gfp, inPath, &enc_delay, &enc_padding); |
| 667 |
} |
| 668 |
} |
| 669 |
initPcmBuffer(&global.pcm32, sizeof(int)); |
| 670 |
initPcmBuffer(&global.pcm16, sizeof(short)); |
| 671 |
setSkipStartAndEnd(gfp, enc_delay, enc_padding); |
| 672 |
{ |
| 673 |
unsigned long n = lame_get_num_samples(gfp); |
| 674 |
if (n != MAX_U_32_NUM) { |
| 675 |
unsigned long const discard = global.pcm32.skip_start + global.pcm32.skip_end; |
| 676 |
lame_set_num_samples(gfp, n > discard ? n - discard : 0); |
| 677 |
} |
| 678 |
} |
| 679 |
return (global.snd_file != NULL || global.music_in != NULL) ? 1 : -1; |
| 680 |
} |
| 681 |
|
| 682 |
int |
| 683 |
samples_to_skip_at_start(void) |
| 684 |
{ |
| 685 |
return global.pcm32.skip_start; |
| 686 |
} |
| 687 |
|
| 688 |
int |
| 689 |
samples_to_skip_at_end(void) |
| 690 |
{ |
| 691 |
return global.pcm32.skip_end; |
| 692 |
} |
| 693 |
|
| 694 |
void |
| 695 |
close_infile(void) |
| 696 |
{ |
| 697 |
#if defined(HAVE_MPGLIB) || defined(HAVE_MPG123) |
| 698 |
if (global.hip != 0) { |
| 699 |
hip_decode_exit(global.hip); /* release mp3decoder memory */ |
| 700 |
global. hip = 0; |
| 701 |
} |
| 702 |
#endif |
| 703 |
close_input_file(global.music_in); |
| 704 |
#ifdef LIBSNDFILE |
| 705 |
if (global.snd_file) { |
| 706 |
if (sf_close(global.snd_file) != 0) { |
| 707 |
if (global_ui_config.silent < 10) { |
| 708 |
error_printf("Could not close sound file \n"); |
| 709 |
} |
| 710 |
} |
| 711 |
global. snd_file = 0; |
| 712 |
} |
| 713 |
#endif |
| 714 |
freePcmBuffer(&global.pcm32); |
| 715 |
freePcmBuffer(&global.pcm16); |
| 716 |
global. music_in = 0; |
| 717 |
free(global.in_id3v2_tag); |
| 718 |
global.in_id3v2_tag = 0; |
| 719 |
global.in_id3v2_size = 0; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
static int |
| 724 |
get_audio_common(lame_t gfp, int buffer[2][1152], short buffer16[2][1152]); |
| 725 |
|
| 726 |
/************************************************************************ |
| 727 |
* |
| 728 |
* get_audio() |
| 729 |
* |
| 730 |
* PURPOSE: reads a frame of audio data from a file to the buffer, |
| 731 |
* aligns the data for future processing, and separates the |
| 732 |
* left and right channels |
| 733 |
* |
| 734 |
************************************************************************/ |
| 735 |
int |
| 736 |
get_audio(lame_t gfp, int buffer[2][1152]) |
| 737 |
{ |
| 738 |
int used = 0, read = 0; |
| 739 |
do { |
| 740 |
read = get_audio_common(gfp, buffer, NULL); |
| 741 |
used = addPcmBuffer(&global.pcm32, buffer[0], buffer[1], read); |
| 742 |
} while (used <= 0 && read > 0); |
| 743 |
if (read < 0) { |
| 744 |
return read; |
| 745 |
} |
| 746 |
if (global_reader.swap_channel == 0) |
| 747 |
return takePcmBuffer(&global.pcm32, buffer[0], buffer[1], used, 1152); |
| 748 |
else |
| 749 |
return takePcmBuffer(&global.pcm32, buffer[1], buffer[0], used, 1152); |
| 750 |
} |
| 751 |
|
| 752 |
/* |
| 753 |
get_audio16 - behave as the original get_audio function, with a limited |
| 754 |
16 bit per sample output |
| 755 |
*/ |
| 756 |
int |
| 757 |
get_audio16(lame_t gfp, short buffer[2][1152]) |
| 758 |
{ |
| 759 |
int used = 0, read = 0; |
| 760 |
do { |
| 761 |
read = get_audio_common(gfp, NULL, buffer); |
| 762 |
used = addPcmBuffer(&global.pcm16, buffer[0], buffer[1], read); |
| 763 |
} while (used <= 0 && read > 0); |
| 764 |
if (read < 0) { |
| 765 |
return read; |
| 766 |
} |
| 767 |
if (global_reader.swap_channel == 0) |
| 768 |
return takePcmBuffer(&global.pcm16, buffer[0], buffer[1], used, 1152); |
| 769 |
else |
| 770 |
return takePcmBuffer(&global.pcm16, buffer[1], buffer[0], used, 1152); |
| 771 |
} |
| 772 |
|
| 773 |
/************************************************************************ |
| 774 |
get_audio_common - central functionality of get_audio* |
| 775 |
in: gfp |
| 776 |
buffer output to the int buffer or 16-bit buffer |
| 777 |
out: buffer int output (if buffer != NULL) |
| 778 |
buffer16 16-bit output (if buffer == NULL) |
| 779 |
returns: samples read |
| 780 |
note: either buffer or buffer16 must be allocated upon call |
| 781 |
*/ |
| 782 |
static int |
| 783 |
get_audio_common(lame_t gfp, int buffer[2][1152], short buffer16[2][1152]) |
| 784 |
{ |
| 785 |
const int num_channels = lame_get_num_channels(gfp); |
| 786 |
const int framesize = lame_get_framesize(gfp); |
| 787 |
int insamp[2 * 1152]; |
| 788 |
short buf_tmp16[2][1152]; |
| 789 |
int samples_read; |
| 790 |
int samples_to_read; |
| 791 |
int i; |
| 792 |
|
| 793 |
/* sanity checks, that's what we expect to be true */ |
| 794 |
if ((num_channels < 1 || 2 < num_channels) |
| 795 |
||(framesize < 1 || 1152 < framesize)) { |
| 796 |
if (global_ui_config.silent < 10) { |
| 797 |
error_printf("Error: internal problem!\n"); |
| 798 |
} |
| 799 |
return -1; |
| 800 |
} |
| 801 |
|
| 802 |
/* |
| 803 |
* NOTE: LAME can now handle arbritray size input data packets, |
| 804 |
* so there is no reason to read the input data in chuncks of |
| 805 |
* size "framesize". EXCEPT: the LAME graphical frame analyzer |
| 806 |
* will get out of sync if we read more than framesize worth of data. |
| 807 |
*/ |
| 808 |
|
| 809 |
samples_to_read = framesize; |
| 810 |
|
| 811 |
/* if this flag has been set, then we are carefull to read |
| 812 |
* exactly num_samples and no more. This is useful for .wav and .aiff |
| 813 |
* files which have id3 or other tags at the end. Note that if you |
| 814 |
* are using LIBSNDFILE, this is not necessary |
| 815 |
*/ |
| 816 |
if (global.count_samples_carefully) { |
| 817 |
unsigned int tmp_num_samples, remaining; |
| 818 |
/* get num_samples */ |
| 819 |
if (is_mpeg_file_format(global_reader.input_format)) { |
| 820 |
tmp_num_samples = global_decoder.mp3input_data.nsamp; |
| 821 |
} |
| 822 |
else { |
| 823 |
tmp_num_samples = lame_get_num_samples(gfp); |
| 824 |
} |
| 825 |
if (global.num_samples_read < tmp_num_samples) { |
| 826 |
remaining = tmp_num_samples - global.num_samples_read; |
| 827 |
} |
| 828 |
else { |
| 829 |
remaining = 0; |
| 830 |
} |
| 831 |
if (remaining < (unsigned int) framesize && 0 != tmp_num_samples) |
| 832 |
/* in case the input is a FIFO (at least it's reproducible with |
| 833 |
a FIFO) tmp_num_samples may be 0 and therefore remaining |
| 834 |
would be 0, but we need to read some samples, so don't |
| 835 |
change samples_to_read to the wrong value in this case */ |
| 836 |
samples_to_read = remaining; |
| 837 |
} |
| 838 |
|
| 839 |
if (is_mpeg_file_format(global_reader.input_format)) { |
| 840 |
if (buffer != NULL) |
| 841 |
samples_read = read_samples_mp3(gfp, global.music_in, buf_tmp16); |
| 842 |
else |
| 843 |
samples_read = read_samples_mp3(gfp, global.music_in, buffer16); |
| 844 |
if (samples_read < 0) { |
| 845 |
return samples_read; |
| 846 |
} |
| 847 |
} |
| 848 |
else { |
| 849 |
int *p; |
| 850 |
if (global.snd_file) { |
| 851 |
#ifdef LIBSNDFILE |
| 852 |
samples_read = sf_read_int(global.snd_file, insamp, num_channels * samples_to_read); |
| 853 |
#else |
| 854 |
samples_read = 0; |
| 855 |
#endif |
| 856 |
} |
| 857 |
else { |
| 858 |
samples_read = |
| 859 |
read_samples_pcm(global.music_in, insamp, num_channels * samples_to_read); |
| 860 |
} |
| 861 |
if (samples_read < 0) { |
| 862 |
return samples_read; |
| 863 |
} |
| 864 |
p = insamp + samples_read; |
| 865 |
samples_read /= num_channels; |
| 866 |
if (buffer != NULL) { /* output to int buffer */ |
| 867 |
if (num_channels == 2) { |
| 868 |
for (i = samples_read; --i >= 0;) { |
| 869 |
buffer[1][i] = *--p; |
| 870 |
buffer[0][i] = *--p; |
| 871 |
} |
| 872 |
} |
| 873 |
else if (num_channels == 1) { |
| 874 |
memset(buffer[1], 0, samples_read * sizeof(int)); |
| 875 |
for (i = samples_read; --i >= 0;) { |
| 876 |
buffer[0][i] = *--p; |
| 877 |
} |
| 878 |
} |
| 879 |
else |
| 880 |
assert(0); |
| 881 |
} |
| 882 |
else { /* convert from int; output to 16-bit buffer */ |
| 883 |
if (num_channels == 2) { |
| 884 |
for (i = samples_read; --i >= 0;) { |
| 885 |
buffer16[1][i] = *--p >> (8 * sizeof(int) - 16); |
| 886 |
buffer16[0][i] = *--p >> (8 * sizeof(int) - 16); |
| 887 |
} |
| 888 |
} |
| 889 |
else if (num_channels == 1) { |
| 890 |
memset(buffer16[1], 0, samples_read * sizeof(short)); |
| 891 |
for (i = samples_read; --i >= 0;) { |
| 892 |
buffer16[0][i] = *--p >> (8 * sizeof(int) - 16); |
| 893 |
} |
| 894 |
} |
| 895 |
else |
| 896 |
assert(0); |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
/* LAME mp3 output 16bit - convert to int, if necessary */ |
| 901 |
if (is_mpeg_file_format(global_reader.input_format)) { |
| 902 |
if (buffer != NULL) { |
| 903 |
for (i = samples_read; --i >= 0;) |
| 904 |
buffer[0][i] = buf_tmp16[0][i] << (8 * sizeof(int) - 16); |
| 905 |
if (num_channels == 2) { |
| 906 |
for (i = samples_read; --i >= 0;) |
| 907 |
buffer[1][i] = buf_tmp16[1][i] << (8 * sizeof(int) - 16); |
| 908 |
} |
| 909 |
else if (num_channels == 1) { |
| 910 |
memset(buffer[1], 0, samples_read * sizeof(int)); |
| 911 |
} |
| 912 |
else |
| 913 |
assert(0); |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
/* if ... then it is considered infinitely long. |
| 919 |
Don't count the samples */ |
| 920 |
if (global.count_samples_carefully) |
| 921 |
global. num_samples_read += samples_read; |
| 922 |
|
| 923 |
return samples_read; |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
static int |
| 929 |
read_samples_mp3(lame_t gfp, FILE * musicin, short int mpg123pcm[2][1152]) |
| 930 |
{ |
| 931 |
int out; |
| 932 |
#ifdef HAVE_MPG123 |
| 933 |
short int *outbuf; |
| 934 |
size_t outbytes; |
| 935 |
#endif |
| 936 |
#if defined(AMIGA_MPEGA) || defined(HAVE_MPGLIB) || defined(HAVE_MPG123) |
| 937 |
int samplerate; |
| 938 |
static const char type_name[] = "MP3 file"; |
| 939 |
|
| 940 |
#ifdef HAVE_MPG123 |
| 941 |
/* Need to deinterleave so rather use mpg123_decode_frame() to decode the |
| 942 |
current frame and deinterleave from the internal buffer. */ |
| 943 |
out = mpg123_decode_frame(global.hip->mh, NULL, (unsigned char**)&outbuf, &outbytes); |
| 944 |
if (out != MPG123_OK && out != MPG123_DONE) |
| 945 |
{ |
| 946 |
if (out == MPG123_NEW_FORMAT) |
| 947 |
{ |
| 948 |
if (global_ui_config.silent < 10) { |
| 949 |
error_printf("Error: format changed in %s - not supported\n", |
| 950 |
type_name); |
| 951 |
} |
| 952 |
} |
| 953 |
return -1; |
| 954 |
} |
| 955 |
out = outbytes/(sizeof(short)*global_decoder.mp3input_data.stereo); |
| 956 |
if (global_decoder.mp3input_data.stereo == 2) { |
| 957 |
int i; |
| 958 |
for (i=0; i<out; ++i) { |
| 959 |
mpg123pcm[0][i] = *outbuf++; |
| 960 |
mpg123pcm[1][i] = *outbuf++; |
| 961 |
} |
| 962 |
} |
| 963 |
else |
| 964 |
memcpy(mpg123pcm[0], outbuf, sizeof(short)*out); |
| 965 |
if(global.hip->pinfo) |
| 966 |
hip_finish_pinfo(global.hip); |
| 967 |
#else |
| 968 |
out = lame_decode_fromfile(musicin, mpg123pcm[0], mpg123pcm[1], &global_decoder.mp3input_data); |
| 969 |
/* |
| 970 |
* out < 0: error, probably EOF |
| 971 |
* out = 0: not possible with lame_decode_fromfile() ??? |
| 972 |
* out > 0: number of output samples |
| 973 |
*/ |
| 974 |
if (out < 0) { |
| 975 |
memset(mpg123pcm, 0, sizeof(**mpg123pcm) * 2 * 1152); |
| 976 |
return 0; |
| 977 |
} |
| 978 |
|
| 979 |
if (lame_get_num_channels(gfp) != global_decoder.mp3input_data.stereo) { |
| 980 |
if (global_ui_config.silent < 10) { |
| 981 |
error_printf("Error: number of channels has changed in %s - not supported\n", |
| 982 |
type_name); |
| 983 |
} |
| 984 |
out = -1; |
| 985 |
} |
| 986 |
samplerate = global_reader.input_samplerate; |
| 987 |
if (samplerate == 0) { |
| 988 |
samplerate = global_decoder.mp3input_data.samplerate; |
| 989 |
} |
| 990 |
if (lame_get_in_samplerate(gfp) != samplerate) { |
| 991 |
if (global_ui_config.silent < 10) { |
| 992 |
error_printf("Error: sample frequency has changed in %s - not supported\n", type_name); |
| 993 |
} |
| 994 |
out = -1; |
| 995 |
} |
| 996 |
#endif |
| 997 |
#else |
| 998 |
out = -1; |
| 999 |
#endif |
| 1000 |
return out; |
| 1001 |
} |
| 1002 |
|
| 1003 |
static |
| 1004 |
int set_input_num_channels(lame_t gfp, int num_channels) |
| 1005 |
{ |
| 1006 |
if (gfp) { |
| 1007 |
if (-1 == lame_set_num_channels(gfp, num_channels)) { |
| 1008 |
if (global_ui_config.silent < 10) { |
| 1009 |
error_printf("Unsupported number of channels: %d\n", num_channels); |
| 1010 |
} |
| 1011 |
return 0; |
| 1012 |
} |
| 1013 |
} |
| 1014 |
return 1; |
| 1015 |
} |
| 1016 |
|
| 1017 |
static |
| 1018 |
int set_input_samplerate(lame_t gfp, int input_samplerate) |
| 1019 |
{ |
| 1020 |
if (gfp) { |
| 1021 |
int sr = global_reader.input_samplerate; |
| 1022 |
if (sr == 0) sr = input_samplerate; |
| 1023 |
if (-1 == lame_set_in_samplerate(gfp, sr)) { |
| 1024 |
if (global_ui_config.silent < 10) { |
| 1025 |
error_printf("Unsupported sample rate: %d\n", sr); |
| 1026 |
} |
| 1027 |
return 0; |
| 1028 |
} |
| 1029 |
} |
| 1030 |
return 1; |
| 1031 |
} |
| 1032 |
|
| 1033 |
int |
| 1034 |
WriteWaveHeader(FILE * const fp, int pcmbytes, int freq, int channels, int bits) |
| 1035 |
{ |
| 1036 |
int bytes = (bits + 7) / 8; |
| 1037 |
|
| 1038 |
/* quick and dirty, but documented */ |
| 1039 |
fwrite("RIFF", 1, 4, fp); /* label */ |
| 1040 |
write_32_bits_low_high(fp, pcmbytes + 44 - 8); /* length in bytes without header */ |
| 1041 |
fwrite("WAVEfmt ", 2, 4, fp); /* 2 labels */ |
| 1042 |
write_32_bits_low_high(fp, 2 + 2 + 4 + 4 + 2 + 2); /* length of PCM format declaration area */ |
| 1043 |
write_16_bits_low_high(fp, 1); /* is PCM? */ |
| 1044 |
write_16_bits_low_high(fp, channels); /* number of channels */ |
| 1045 |
write_32_bits_low_high(fp, freq); /* sample frequency in [Hz] */ |
| 1046 |
write_32_bits_low_high(fp, freq * channels * bytes); /* bytes per second */ |
| 1047 |
write_16_bits_low_high(fp, channels * bytes); /* bytes per sample time */ |
| 1048 |
write_16_bits_low_high(fp, bits); /* bits per sample */ |
| 1049 |
fwrite("data", 1, 4, fp); /* label */ |
| 1050 |
write_32_bits_low_high(fp, pcmbytes); /* length in bytes of raw PCM data */ |
| 1051 |
|
| 1052 |
return ferror(fp) ? -1 : 0; |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
#if defined(LIBSNDFILE) |
| 1059 |
|
| 1060 |
extern SNDFILE *sf_wchar_open(wchar_t const *wpath, int mode, SF_INFO * sfinfo); |
| 1061 |
|
| 1062 |
static SNDFILE * |
| 1063 |
open_snd_file(lame_t gfp, char const *inPath) |
| 1064 |
{ |
| 1065 |
char const *lpszFileName = inPath; |
| 1066 |
SNDFILE *gs_pSndFileIn = NULL; |
| 1067 |
SF_INFO gs_wfInfo; |
| 1068 |
|
| 1069 |
{ |
| 1070 |
#if defined( _WIN32 ) && !defined(__MINGW32__) |
| 1071 |
wchar_t *file_name = utf8ToUnicode(lpszFileName); |
| 1072 |
#endif |
| 1073 |
/* Try to open the sound file */ |
| 1074 |
memset(&gs_wfInfo, 0, sizeof(gs_wfInfo)); |
| 1075 |
#if defined( _WIN32 ) && !defined(__MINGW32__) |
| 1076 |
gs_pSndFileIn = sf_wchar_open(file_name, SFM_READ, &gs_wfInfo); |
| 1077 |
#else |
| 1078 |
gs_pSndFileIn = sf_open(lpszFileName, SFM_READ, &gs_wfInfo); |
| 1079 |
#endif |
| 1080 |
|
| 1081 |
if (gs_pSndFileIn == NULL) { |
| 1082 |
if (global_raw_pcm.in_signed == 0 && global_raw_pcm.in_bitwidth != 8) { |
| 1083 |
error_printf("Unsigned input only supported with bitwidth 8\n"); |
| 1084 |
#if defined( _WIN32 ) && !defined(__MINGW32__) |
| 1085 |
free(file_name); |
| 1086 |
#endif |
| 1087 |
return 0; |
| 1088 |
} |
| 1089 |
/* set some defaults incase input is raw PCM */ |
| 1090 |
gs_wfInfo.seekable = (global_reader.input_format != sf_raw); /* if user specified -r, set to not seekable */ |
| 1091 |
gs_wfInfo.samplerate = lame_get_in_samplerate(gfp); |
| 1092 |
gs_wfInfo.channels = lame_get_num_channels(gfp); |
| 1093 |
gs_wfInfo.format = SF_FORMAT_RAW; |
| 1094 |
if ((global_raw_pcm.in_endian == ByteOrderLittleEndian) ^ (global_reader.swapbytes != |
| 1095 |
0)) { |
| 1096 |
gs_wfInfo.format |= SF_ENDIAN_LITTLE; |
| 1097 |
} |
| 1098 |
else { |
| 1099 |
gs_wfInfo.format |= SF_ENDIAN_BIG; |
| 1100 |
} |
| 1101 |
switch (global_raw_pcm.in_bitwidth) { |
| 1102 |
case 8: |
| 1103 |
gs_wfInfo.format |= |
| 1104 |
global_raw_pcm.in_signed == 0 ? SF_FORMAT_PCM_U8 : SF_FORMAT_PCM_S8; |
| 1105 |
break; |
| 1106 |
case 16: |
| 1107 |
gs_wfInfo.format |= SF_FORMAT_PCM_16; |
| 1108 |
break; |
| 1109 |
case 24: |
| 1110 |
gs_wfInfo.format |= SF_FORMAT_PCM_24; |
| 1111 |
break; |
| 1112 |
case 32: |
| 1113 |
gs_wfInfo.format |= SF_FORMAT_PCM_32; |
| 1114 |
break; |
| 1115 |
default: |
| 1116 |
break; |
| 1117 |
} |
| 1118 |
#if defined( _WIN32 ) && !defined(__MINGW32__) |
| 1119 |
gs_pSndFileIn = sf_wchar_open(file_name, SFM_READ, &gs_wfInfo); |
| 1120 |
#else |
| 1121 |
gs_pSndFileIn = sf_open(lpszFileName, SFM_READ, &gs_wfInfo); |
| 1122 |
#endif |
| 1123 |
} |
| 1124 |
#if defined( _WIN32 ) && !defined(__MINGW32__) |
| 1125 |
free(file_name); |
| 1126 |
#endif |
| 1127 |
|
| 1128 |
/* Check result */ |
| 1129 |
if (gs_pSndFileIn == NULL) { |
| 1130 |
sf_perror(gs_pSndFileIn); |
| 1131 |
if (global_ui_config.silent < 10) { |
| 1132 |
error_printf("Could not open sound file \"%s\".\n", lpszFileName); |
| 1133 |
} |
| 1134 |
return 0; |
| 1135 |
} |
| 1136 |
sf_command(gs_pSndFileIn, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE); |
| 1137 |
|
| 1138 |
if ((gs_wfInfo.format & SF_FORMAT_RAW) == SF_FORMAT_RAW) { |
| 1139 |
global_reader.input_format = sf_raw; |
| 1140 |
} |
| 1141 |
|
| 1142 |
#ifdef _DEBUG_SND_FILE |
| 1143 |
printf("\n\nSF_INFO structure\n"); |
| 1144 |
printf("samplerate :%d\n", gs_wfInfo.samplerate); |
| 1145 |
printf("samples :%d\n", gs_wfInfo.frames); |
| 1146 |
printf("channels :%d\n", gs_wfInfo.channels); |
| 1147 |
printf("format :"); |
| 1148 |
|
| 1149 |
/* new formats from [email protected] 1/2000 */ |
| 1150 |
|
| 1151 |
switch (gs_wfInfo.format & SF_FORMAT_TYPEMASK) { |
| 1152 |
case SF_FORMAT_WAV: |
| 1153 |
printf("Microsoft WAV format (big endian). "); |
| 1154 |
break; |
| 1155 |
case SF_FORMAT_AIFF: |
| 1156 |
printf("Apple/SGI AIFF format (little endian). "); |
| 1157 |
break; |
| 1158 |
case SF_FORMAT_AU: |
| 1159 |
printf("Sun/NeXT AU format (big endian). "); |
| 1160 |
break; |
| 1161 |
/* |
| 1162 |
case SF_FORMAT_AULE: |
| 1163 |
DEBUGF("DEC AU format (little endian). "); |
| 1164 |
break; |
| 1165 |
*/ |
| 1166 |
case SF_FORMAT_RAW: |
| 1167 |
printf("RAW PCM data. "); |
| 1168 |
break; |
| 1169 |
case SF_FORMAT_PAF: |
| 1170 |
printf("Ensoniq PARIS file format. "); |
| 1171 |
break; |
| 1172 |
case SF_FORMAT_SVX: |
| 1173 |
printf("Amiga IFF / SVX8 / SV16 format. "); |
| 1174 |
break; |
| 1175 |
case SF_FORMAT_NIST: |
| 1176 |
printf("Sphere NIST format. "); |
| 1177 |
break; |
| 1178 |
default: |
| 1179 |
assert(0); |
| 1180 |
break; |
| 1181 |
} |
| 1182 |
|
| 1183 |
switch (gs_wfInfo.format & SF_FORMAT_SUBMASK) { |
| 1184 |
/* |
| 1185 |
case SF_FORMAT_PCM: |
| 1186 |
DEBUGF("PCM data in 8, 16, 24 or 32 bits."); |
| 1187 |
break; |
| 1188 |
*/ |
| 1189 |
case SF_FORMAT_FLOAT: |
| 1190 |
printf("32 bit Intel x86 floats."); |
| 1191 |
break; |
| 1192 |
case SF_FORMAT_ULAW: |
| 1193 |
printf("U-Law encoded."); |
| 1194 |
break; |
| 1195 |
case SF_FORMAT_ALAW: |
| 1196 |
printf("A-Law encoded."); |
| 1197 |
break; |
| 1198 |
case SF_FORMAT_IMA_ADPCM: |
| 1199 |
printf("IMA ADPCM."); |
| 1200 |
break; |
| 1201 |
case SF_FORMAT_MS_ADPCM: |
| 1202 |
printf("Microsoft ADPCM."); |
| 1203 |
break; |
| 1204 |
/* |
| 1205 |
case SF_FORMAT_PCM_BE: |
| 1206 |
DEBUGF("Big endian PCM data."); |
| 1207 |
break; |
| 1208 |
case SF_FORMAT_PCM_LE: |
| 1209 |
DEBUGF("Little endian PCM data."); |
| 1210 |
break; |
| 1211 |
*/ |
| 1212 |
case SF_FORMAT_PCM_S8: |
| 1213 |
printf("Signed 8 bit PCM."); |
| 1214 |
break; |
| 1215 |
case SF_FORMAT_PCM_U8: |
| 1216 |
printf("Unsigned 8 bit PCM."); |
| 1217 |
break; |
| 1218 |
case SF_FORMAT_PCM_16: |
| 1219 |
printf("Signed 16 bit PCM."); |
| 1220 |
break; |
| 1221 |
case SF_FORMAT_PCM_24: |
| 1222 |
printf("Signed 24 bit PCM."); |
| 1223 |
break; |
| 1224 |
case SF_FORMAT_PCM_32: |
| 1225 |
printf("Signed 32 bit PCM."); |
| 1226 |
break; |
| 1227 |
/* |
| 1228 |
case SF_FORMAT_SVX_FIB: |
| 1229 |
DEBUGF("SVX Fibonacci Delta encoding."); |
| 1230 |
break; |
| 1231 |
case SF_FORMAT_SVX_EXP: |
| 1232 |
DEBUGF("SVX Exponential Delta encoding."); |
| 1233 |
break; |
| 1234 |
*/ |
| 1235 |
default: |
| 1236 |
assert(0); |
| 1237 |
break; |
| 1238 |
} |
| 1239 |
|
| 1240 |
printf("\n"); |
| 1241 |
printf("sections :%d\n", gs_wfInfo.sections); |
| 1242 |
printf("seekable :%d\n", gs_wfInfo.seekable); |
| 1243 |
#endif |
| 1244 |
/* Check result */ |
| 1245 |
if (gs_pSndFileIn == NULL) { |
| 1246 |
sf_perror(gs_pSndFileIn); |
| 1247 |
if (global_ui_config.silent < 10) { |
| 1248 |
error_printf("Could not open sound file \"%s\".\n", lpszFileName); |
| 1249 |
} |
| 1250 |
return 0; |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
if(gs_wfInfo.frames >= 0 && gs_wfInfo.frames < (sf_count_t)(unsigned)MAX_U_32_NUM) |
| 1255 |
(void) lame_set_num_samples(gfp, gs_wfInfo.frames); |
| 1256 |
else |
| 1257 |
(void) lame_set_num_samples(gfp, MAX_U_32_NUM); |
| 1258 |
if (!set_input_num_channels(gfp, gs_wfInfo.channels)) { |
| 1259 |
sf_close(gs_pSndFileIn); |
| 1260 |
return 0; |
| 1261 |
} |
| 1262 |
if (!set_input_samplerate(gfp, gs_wfInfo.samplerate)) { |
| 1263 |
sf_close(gs_pSndFileIn); |
| 1264 |
return 0; |
| 1265 |
} |
| 1266 |
global. pcmbitwidth = 32; |
| 1267 |
} |
| 1268 |
#if 0 |
| 1269 |
if (lame_get_num_samples(gfp) == MAX_U_32_NUM) { |
| 1270 |
/* try to figure out num_samples */ |
| 1271 |
double const flen = lame_get_file_size(lpszFileName); |
| 1272 |
if (flen >= 0) { |
| 1273 |
/* try file size, assume 2 bytes per sample */ |
| 1274 |
lame_set_num_samples(gfp, flen / (2 * lame_get_num_channels(gfp))); |
| 1275 |
} |
| 1276 |
} |
| 1277 |
#endif |
| 1278 |
return gs_pSndFileIn; |
| 1279 |
} |
| 1280 |
|
| 1281 |
#endif /* defined(LIBSNDFILE) */ |
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
/************************************************************************ |
| 1286 |
unpack_read_samples - read and unpack signed low-to-high byte or unsigned |
| 1287 |
single byte input. (used for read_samples function) |
| 1288 |
Output integers are stored in the native byte order |
| 1289 |
(little or big endian). -jd |
| 1290 |
in: samples_to_read |
| 1291 |
bytes_per_sample |
| 1292 |
swap_order - set for high-to-low byte order input stream |
| 1293 |
i/o: pcm_in |
| 1294 |
out: sample_buffer (must be allocated up to samples_to_read upon call) |
| 1295 |
returns: number of samples read |
| 1296 |
*/ |
| 1297 |
static int |
| 1298 |
unpack_read_samples(const int samples_to_read, const int bytes_per_sample, |
| 1299 |
const int swap_order, int *sample_buffer, FILE * pcm_in) |
| 1300 |
{ |
| 1301 |
int samples_read; |
| 1302 |
int i; |
| 1303 |
int *op; /* output pointer */ |
| 1304 |
unsigned char *ip = (unsigned char *) sample_buffer; /* input pointer */ |
| 1305 |
const int b = sizeof(int) * 8; |
| 1306 |
|
| 1307 |
{ |
| 1308 |
size_t samples_read_ = fread(sample_buffer, bytes_per_sample, samples_to_read, pcm_in); |
| 1309 |
assert( samples_read_ <= INT_MAX ); |
| 1310 |
samples_read = (int) samples_read_; |
| 1311 |
} |
| 1312 |
op = sample_buffer + samples_read; |
| 1313 |
|
| 1314 |
#define GA_URS_IFLOOP( ga_urs_bps ) \ |
| 1315 |
if( bytes_per_sample == ga_urs_bps ) \ |
| 1316 |
for( i = samples_read * bytes_per_sample; (i -= bytes_per_sample) >=0;) |
| 1317 |
|
| 1318 |
if (swap_order == 0) { |
| 1319 |
GA_URS_IFLOOP(1) |
| 1320 |
* --op = ip[i] << (b - 8); |
| 1321 |
GA_URS_IFLOOP(2) |
| 1322 |
* --op = ip[i] << (b - 16) | ip[i + 1] << (b - 8); |
| 1323 |
GA_URS_IFLOOP(3) |
| 1324 |
* --op = ip[i] << (b - 24) | ip[i + 1] << (b - 16) | ip[i + 2] << (b - 8); |
| 1325 |
GA_URS_IFLOOP(4) |
| 1326 |
* --op = |
| 1327 |
ip[i] << (b - 32) | ip[i + 1] << (b - 24) | ip[i + 2] << (b - 16) | ip[i + 3] << (b - |
| 1328 |
8); |
| 1329 |
} |
| 1330 |
else { |
| 1331 |
GA_URS_IFLOOP(1) |
| 1332 |
* --op = (ip[i] ^ 0x80) << (b - 8) | 0x7f << (b - 16); /* convert from unsigned */ |
| 1333 |
GA_URS_IFLOOP(2) |
| 1334 |
* --op = ip[i] << (b - 8) | ip[i + 1] << (b - 16); |
| 1335 |
GA_URS_IFLOOP(3) |
| 1336 |
* --op = ip[i] << (b - 8) | ip[i + 1] << (b - 16) | ip[i + 2] << (b - 24); |
| 1337 |
GA_URS_IFLOOP(4) |
| 1338 |
* --op = |
| 1339 |
ip[i] << (b - 8) | ip[i + 1] << (b - 16) | ip[i + 2] << (b - 24) | ip[i + 3] << (b - |
| 1340 |
32); |
| 1341 |
} |
| 1342 |
#undef GA_URS_IFLOOP |
| 1343 |
if (global.pcm_is_ieee_float) { |
| 1344 |
ieee754_float32_t const m_max = INT_MAX; |
| 1345 |
ieee754_float32_t const m_min = -(ieee754_float32_t) INT_MIN; |
| 1346 |
ieee754_float32_t *x = (ieee754_float32_t *) sample_buffer; |
| 1347 |
assert(sizeof(ieee754_float32_t) == sizeof(int)); |
| 1348 |
for (i = 0; i < samples_to_read; ++i) { |
| 1349 |
ieee754_float32_t const u = x[i]; |
| 1350 |
int v; |
| 1351 |
if (u >= 1) { |
| 1352 |
v = INT_MAX; |
| 1353 |
} |
| 1354 |
else if (u <= -1) { |
| 1355 |
v = INT_MIN; |
| 1356 |
} |
| 1357 |
else if (u >= 0) { |
| 1358 |
v = (int) (u * m_max + 0.5f); |
| 1359 |
} |
| 1360 |
else { |
| 1361 |
v = (int) (u * m_min - 0.5f); |
| 1362 |
} |
| 1363 |
sample_buffer[i] = v; |
| 1364 |
} |
| 1365 |
} |
| 1366 |
return (samples_read); |
| 1367 |
} |
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
/************************************************************************ |
| 1372 |
* |
| 1373 |
* read_samples() |
| 1374 |
* |
| 1375 |
* PURPOSE: reads the PCM samples from a file to the buffer |
| 1376 |
* |
| 1377 |
* SEMANTICS: |
| 1378 |
* Reads #samples_read# number of shorts from #musicin# filepointer |
| 1379 |
* into #sample_buffer[]#. Returns the number of samples read. |
| 1380 |
* |
| 1381 |
************************************************************************/ |
| 1382 |
|
| 1383 |
static int |
| 1384 |
read_samples_pcm(FILE * musicin, int sample_buffer[2304], int samples_to_read) |
| 1385 |
{ |
| 1386 |
int samples_read; |
| 1387 |
int bytes_per_sample = global.pcmbitwidth / 8; |
| 1388 |
int swap_byte_order; /* byte order of input stream */ |
| 1389 |
|
| 1390 |
switch (global.pcmbitwidth) { |
| 1391 |
case 32: |
| 1392 |
case 24: |
| 1393 |
case 16: |
| 1394 |
if (global_raw_pcm.in_signed == 0) { |
| 1395 |
if (global_ui_config.silent < 10) { |
| 1396 |
error_printf("Unsigned input only supported with bitwidth 8\n"); |
| 1397 |
} |
| 1398 |
return -1; |
| 1399 |
} |
| 1400 |
swap_byte_order = (global_raw_pcm.in_endian != ByteOrderLittleEndian) ? 1 : 0; |
| 1401 |
if (global.pcmswapbytes) { |
| 1402 |
swap_byte_order = !swap_byte_order; |
| 1403 |
} |
| 1404 |
break; |
| 1405 |
|
| 1406 |
case 8: |
| 1407 |
swap_byte_order = global.pcm_is_unsigned_8bit; |
| 1408 |
break; |
| 1409 |
|
| 1410 |
default: |
| 1411 |
if (global_ui_config.silent < 10) { |
| 1412 |
error_printf("Only 8, 16, 24 and 32 bit input files supported \n"); |
| 1413 |
} |
| 1414 |
return -1; |
| 1415 |
} |
| 1416 |
if (samples_to_read < 0 || samples_to_read > 2304) { |
| 1417 |
if (global_ui_config.silent < 10) { |
| 1418 |
error_printf("Error: unexpected number of samples to read: %d\n", samples_to_read); |
| 1419 |
} |
| 1420 |
return -1; |
| 1421 |
} |
| 1422 |
samples_read = unpack_read_samples(samples_to_read, bytes_per_sample, swap_byte_order, |
| 1423 |
sample_buffer, musicin); |
| 1424 |
if (ferror(musicin)) { |
| 1425 |
if (global_ui_config.silent < 10) { |
| 1426 |
error_printf("Error reading input file\n"); |
| 1427 |
} |
| 1428 |
return -1; |
| 1429 |
} |
| 1430 |
|
| 1431 |
return samples_read; |
| 1432 |
} |
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
/* AIFF Definitions */ |
| 1437 |
|
| 1438 |
static uint32_t const IFF_ID_FORM = 0x464f524d; /* "FORM" */ |
| 1439 |
static uint32_t const IFF_ID_AIFF = 0x41494646; /* "AIFF" */ |
| 1440 |
static uint32_t const IFF_ID_AIFC = 0x41494643; /* "AIFC" */ |
| 1441 |
static uint32_t const IFF_ID_COMM = 0x434f4d4d; /* "COMM" */ |
| 1442 |
static uint32_t const IFF_ID_SSND = 0x53534e44; /* "SSND" */ |
| 1443 |
static uint32_t const IFF_ID_MPEG = 0x4d504547; /* "MPEG" */ |
| 1444 |
|
| 1445 |
static uint32_t const IFF_ID_NONE = 0x4e4f4e45; /* "NONE" *//* AIFF-C data format */ |
| 1446 |
static uint32_t const IFF_ID_2CBE = 0x74776f73; /* "twos" *//* AIFF-C data format */ |
| 1447 |
static uint32_t const IFF_ID_2CLE = 0x736f7774; /* "sowt" *//* AIFF-C data format */ |
| 1448 |
static uint32_t const IFF_ID_FL32 = 0x666C3332; /* "fl32" *//* AIFF-C data format */ |
| 1449 |
static uint32_t const IFF_ID_FL64 = 0x666C3634; /* "fl64" *//* AIFF-C data format */ |
| 1450 |
|
| 1451 |
static uint32_t const WAV_ID_RIFF = 0x52494646; /* "RIFF" */ |
| 1452 |
static uint32_t const WAV_ID_WAVE = 0x57415645; /* "WAVE" */ |
| 1453 |
static uint32_t const WAV_ID_FMT = 0x666d7420; /* "fmt " */ |
| 1454 |
static uint32_t const WAV_ID_DATA = 0x64617461; /* "data" */ |
| 1455 |
|
| 1456 |
#ifndef WAVE_FORMAT_PCM |
| 1457 |
static uint16_t const WAVE_FORMAT_PCM = 0x0001; |
| 1458 |
#endif |
| 1459 |
#ifndef WAVE_FORMAT_IEEE_FLOAT |
| 1460 |
static uint16_t const WAVE_FORMAT_IEEE_FLOAT = 0x0003; |
| 1461 |
#endif |
| 1462 |
#ifndef WAVE_FORMAT_EXTENSIBLE |
| 1463 |
static uint16_t const WAVE_FORMAT_EXTENSIBLE = 0xFFFE; |
| 1464 |
#endif |
| 1465 |
|
| 1466 |
|
| 1467 |
static uint32_t |
| 1468 |
make_even_number_of_bytes_in_length(uint32_t x) |
| 1469 |
{ |
| 1470 |
return x + (x & 0x01); |
| 1471 |
} |
| 1472 |
|
| 1473 |
|
| 1474 |
/***************************************************************************** |
| 1475 |
* |
| 1476 |
* Read Microsoft Wave headers |
| 1477 |
* |
| 1478 |
* By the time we get here the first 32-bits of the file have already been |
| 1479 |
* read, and we're pretty sure that we're looking at a WAV file. |
| 1480 |
* |
| 1481 |
*****************************************************************************/ |
| 1482 |
|
| 1483 |
static int |
| 1484 |
parse_wave_header(lame_global_flags * gfp, FILE * sf) |
| 1485 |
{ |
| 1486 |
uint32_t ui32_nSamplesPerSec = 0; |
| 1487 |
uint32_t ui32_DataChunkSize = 0; |
| 1488 |
uint16_t ui16_wFormatTag = 0; |
| 1489 |
uint16_t ui16_nChannels = 0; |
| 1490 |
uint16_t ui16_wBitsPerSample = 0; |
| 1491 |
|
| 1492 |
int is_wav = 0; |
| 1493 |
int loop_sanity = 0; |
| 1494 |
|
| 1495 |
uint32_t ui32_chunkSize = read_32_bits_high_low(sf); /* file_length */ |
| 1496 |
uint32_t ui32_WAVEID = read_32_bits_high_low(sf); |
| 1497 |
if (ui32_WAVEID != WAV_ID_WAVE || ui32_chunkSize < 1) |
| 1498 |
return -1; |
| 1499 |
|
| 1500 |
for (loop_sanity = 0; loop_sanity < 20; ++loop_sanity) { |
| 1501 |
uint32_t ui32_ckID = read_32_bits_high_low(sf); |
| 1502 |
if (ui32_ckID == WAV_ID_FMT) { |
| 1503 |
uint32_t ui32_nAvgBytesPerSec = 0; |
| 1504 |
uint32_t ui32_cksize = 0; |
| 1505 |
uint16_t ui16_nBlockAlign = 0; |
| 1506 |
|
| 1507 |
ui32_cksize = read_32_bits_low_high(sf); |
| 1508 |
ui32_cksize = make_even_number_of_bytes_in_length(ui32_cksize); |
| 1509 |
if (ui32_cksize < 16u) { |
| 1510 |
/*DEBUGF("'fmt' chunk too short (only %ld bytes)!", ui32_cksize);*/ |
| 1511 |
return -1; |
| 1512 |
} |
| 1513 |
ui16_wFormatTag = read_16_bits_low_high(sf); |
| 1514 |
ui16_nChannels = read_16_bits_low_high(sf); |
| 1515 |
ui32_nSamplesPerSec = read_32_bits_low_high(sf); |
| 1516 |
ui32_nAvgBytesPerSec = read_32_bits_low_high(sf); |
| 1517 |
ui16_nBlockAlign = read_16_bits_low_high(sf); |
| 1518 |
ui16_wBitsPerSample = read_16_bits_low_high(sf); |
| 1519 |
ui32_cksize -= 16u; |
| 1520 |
/* WAVE_FORMAT_EXTENSIBLE support */ |
| 1521 |
if ((ui32_cksize > 9u) && (ui16_wFormatTag == WAVE_FORMAT_EXTENSIBLE)) { |
| 1522 |
uint16_t ui16_cbSize = read_16_bits_low_high(sf); |
| 1523 |
uint16_t ui16_wValidBitsPerSample = read_16_bits_low_high(sf); |
| 1524 |
uint32_t ui32_dwChannelMask = read_32_bits_low_high(sf); |
| 1525 |
uint16_t ui16_SubFormat = read_16_bits_low_high(sf); |
| 1526 |
ui32_cksize -= 10u; |
| 1527 |
ui16_wFormatTag = ui16_SubFormat; /* SubType coincident with format_tag for PCM int or float */ |
| 1528 |
(void) (ui16_cbSize, ui16_wValidBitsPerSample, ui32_dwChannelMask); /* unused */ |
| 1529 |
} |
| 1530 |
/* DEBUGF(" skipping %d bytes\n", ui32_cksize); */ |
| 1531 |
if (ui32_cksize > 0) { |
| 1532 |
if (fskip_uint32(sf, ui32_cksize) != 0) |
| 1533 |
return -1; |
| 1534 |
}; |
| 1535 |
} |
| 1536 |
else if (ui32_ckID == WAV_ID_DATA) { |
| 1537 |
ui32_DataChunkSize = read_32_bits_low_high(sf); |
| 1538 |
is_wav = 1; |
| 1539 |
/* We've found the audio data. Read no further! */ |
| 1540 |
break; |
| 1541 |
} |
| 1542 |
else { |
| 1543 |
uint32_t ui32_cksize = read_32_bits_low_high(sf); |
| 1544 |
ui32_cksize = make_even_number_of_bytes_in_length(ui32_cksize); |
| 1545 |
if (fskip_uint32(sf, ui32_cksize) != 0) { |
| 1546 |
return -1; |
| 1547 |
} |
| 1548 |
} |
| 1549 |
} |
| 1550 |
if (is_wav) { |
| 1551 |
if (ui16_wFormatTag == 0x0050 || ui16_wFormatTag == 0x0055) { |
| 1552 |
return sf_mp123; |
| 1553 |
} |
| 1554 |
if (ui16_wFormatTag != WAVE_FORMAT_PCM && ui16_wFormatTag != WAVE_FORMAT_IEEE_FLOAT) { |
| 1555 |
if (global_ui_config.silent < 10) { |
| 1556 |
error_printf("Unsupported data format: 0x%04X\n", ui16_wFormatTag); |
| 1557 |
} |
| 1558 |
return 0; /* oh no! non-supported format */ |
| 1559 |
} |
| 1560 |
|
| 1561 |
/* make sure the header is sane */ |
| 1562 |
if (!set_input_num_channels(gfp, ui16_nChannels)) |
| 1563 |
return 0; |
| 1564 |
if (!set_input_samplerate(gfp, ui32_nSamplesPerSec)) |
| 1565 |
return 0; |
| 1566 |
/* avoid division by zero */ |
| 1567 |
if (ui16_wBitsPerSample < 1) { |
| 1568 |
if (global_ui_config.silent < 10) |
| 1569 |
error_printf("Unsupported bits per sample: %d\n", ui16_wBitsPerSample); |
| 1570 |
return -1; |
| 1571 |
} |
| 1572 |
global. pcmbitwidth = ui16_wBitsPerSample; |
| 1573 |
global. pcm_is_unsigned_8bit = 1; |
| 1574 |
global. pcm_is_ieee_float = (ui16_wFormatTag == WAVE_FORMAT_IEEE_FLOAT ? 1 : 0); |
| 1575 |
if (ui32_DataChunkSize == MAX_U_32_NUM) |
| 1576 |
(void) lame_set_num_samples(gfp, MAX_U_32_NUM); |
| 1577 |
else |
| 1578 |
(void) lame_set_num_samples(gfp, ui32_DataChunkSize / (ui16_nChannels * ((ui16_wBitsPerSample + 7u) / 8u))); |
| 1579 |
return 1; |
| 1580 |
} |
| 1581 |
return -1; |
| 1582 |
} |
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
/************************************************************************ |
| 1587 |
* aiff_check2 |
| 1588 |
* |
| 1589 |
* PURPOSE: Checks AIFF header information to make sure it is valid. |
| 1590 |
* returns 0 on success, 1 on errors |
| 1591 |
************************************************************************/ |
| 1592 |
|
| 1593 |
static int |
| 1594 |
aiff_check2(IFF_AIFF * const pcm_aiff_data) |
| 1595 |
{ |
| 1596 |
if (pcm_aiff_data->sampleType != IFF_ID_SSND) { |
| 1597 |
if (global_ui_config.silent < 10) { |
| 1598 |
error_printf("ERROR: input sound data is not PCM\n"); |
| 1599 |
} |
| 1600 |
return 1; |
| 1601 |
} |
| 1602 |
switch (pcm_aiff_data->sampleSize) { |
| 1603 |
case 32: |
| 1604 |
case 24: |
| 1605 |
case 16: |
| 1606 |
case 8: |
| 1607 |
break; |
| 1608 |
default: |
| 1609 |
if (global_ui_config.silent < 10) { |
| 1610 |
error_printf("ERROR: input sound data is not 8, 16, 24 or 32 bits\n"); |
| 1611 |
} |
| 1612 |
return 1; |
| 1613 |
} |
| 1614 |
if (pcm_aiff_data->numChannels != 1 && pcm_aiff_data->numChannels != 2) { |
| 1615 |
if (global_ui_config.silent < 10) { |
| 1616 |
error_printf("ERROR: input sound data is not mono or stereo\n"); |
| 1617 |
} |
| 1618 |
return 1; |
| 1619 |
} |
| 1620 |
if (pcm_aiff_data->blkAlgn.blockSize != 0) { |
| 1621 |
if (global_ui_config.silent < 10) { |
| 1622 |
error_printf("ERROR: block size of input sound data is not 0 bytes\n"); |
| 1623 |
} |
| 1624 |
return 1; |
| 1625 |
} |
| 1626 |
/* A bug, since we correctly skip the offset earlier in the code. |
| 1627 |
if (pcm_aiff_data->blkAlgn.offset != 0) { |
| 1628 |
error_printf("Block offset is not 0 bytes in '%s'\n", file_name); |
| 1629 |
return 1; |
| 1630 |
} */ |
| 1631 |
|
| 1632 |
return 0; |
| 1633 |
} |
| 1634 |
|
| 1635 |
|
| 1636 |
/***************************************************************************** |
| 1637 |
* |
| 1638 |
* Read Audio Interchange File Format (AIFF) headers. |
| 1639 |
* |
| 1640 |
* By the time we get here the first 32 bits of the file have already been |
| 1641 |
* read, and we're pretty sure that we're looking at an AIFF file. |
| 1642 |
* |
| 1643 |
*****************************************************************************/ |
| 1644 |
|
| 1645 |
static int |
| 1646 |
parse_aiff_header(lame_global_flags * gfp, FILE * sf) |
| 1647 |
{ |
| 1648 |
uint32_t ui32_ChunkSize = 0; |
| 1649 |
uint32_t ui32_TypeID = 0; |
| 1650 |
IFF_AIFF aiff_info; |
| 1651 |
int seen_comm_chunk = 0, seen_ssnd_chunk = 0; |
| 1652 |
long pcm_data_pos = -1; |
| 1653 |
|
| 1654 |
memset(&aiff_info, 0, sizeof(aiff_info)); |
| 1655 |
aiff_info.sampleFormat = IFF_ID_NONE; |
| 1656 |
ui32_ChunkSize = read_32_bits_high_low(sf); |
| 1657 |
|
| 1658 |
ui32_TypeID = read_32_bits_high_low(sf); |
| 1659 |
ui32_ChunkSize -= 4; |
| 1660 |
if ((ui32_TypeID != IFF_ID_AIFF) && (ui32_TypeID != IFF_ID_AIFC)) |
| 1661 |
return -1; |
| 1662 |
|
| 1663 |
while (ui32_ChunkSize >= 8) { |
| 1664 |
uint32_t ui32_type = read_32_bits_high_low(sf); |
| 1665 |
ui32_ChunkSize -= 4; |
| 1666 |
|
| 1667 |
/* DEBUGF( |
| 1668 |
"found chunk type %08x '%4.4s'\n", ui32_type, (char*)&ui32_type); */ |
| 1669 |
|
| 1670 |
/* don't use a switch here to make it easier to use 'break' for SSND */ |
| 1671 |
if (ui32_type == IFF_ID_COMM) { |
| 1672 |
uint32_t ui32_cksize = read_32_bits_high_low(sf); |
| 1673 |
ui32_ChunkSize -= 4; |
| 1674 |
ui32_cksize = make_even_number_of_bytes_in_length(ui32_cksize); |
| 1675 |
if (ui32_cksize < 18 || ui32_ChunkSize < ui32_cksize) |
| 1676 |
return -1; |
| 1677 |
ui32_ChunkSize -= ui32_cksize; |
| 1678 |
seen_comm_chunk = seen_ssnd_chunk + 1; |
| 1679 |
|
| 1680 |
aiff_info.numChannels = read_16_bits_high_low(sf); |
| 1681 |
aiff_info.numSampleFrames = read_32_bits_high_low(sf); |
| 1682 |
aiff_info.sampleSize = read_16_bits_high_low(sf); |
| 1683 |
aiff_info.sampleRate = read_ieee_extended_high_low(sf); |
| 1684 |
ui32_cksize -= 18; |
| 1685 |
if (ui32_TypeID == IFF_ID_AIFC) { |
| 1686 |
if (ui32_cksize < 4) |
| 1687 |
return -1; |
| 1688 |
aiff_info.sampleFormat = read_32_bits_high_low(sf); |
| 1689 |
ui32_cksize -= 4; |
| 1690 |
} |
| 1691 |
if (fskip_uint32(sf, ui32_cksize) != 0) |
| 1692 |
return -1; |
| 1693 |
} |
| 1694 |
else if (ui32_type == IFF_ID_SSND) { |
| 1695 |
uint32_t ui32_cksize = read_32_bits_high_low(sf); |
| 1696 |
ui32_ChunkSize -= 4; |
| 1697 |
ui32_cksize = make_even_number_of_bytes_in_length(ui32_cksize); |
| 1698 |
if (ui32_cksize < 8 || ui32_ChunkSize < ui32_cksize) |
| 1699 |
return -1; |
| 1700 |
ui32_ChunkSize -= ui32_cksize; |
| 1701 |
seen_ssnd_chunk = 1; |
| 1702 |
|
| 1703 |
aiff_info.sampleType = IFF_ID_SSND; |
| 1704 |
aiff_info.blkAlgn.offset = read_32_bits_high_low(sf); |
| 1705 |
aiff_info.blkAlgn.blockSize = read_32_bits_high_low(sf); |
| 1706 |
ui32_cksize -= 8; |
| 1707 |
if (seen_comm_chunk > 0) { |
| 1708 |
if (fskip_uint32(sf, aiff_info.blkAlgn.offset) != 0) |
| 1709 |
return -1; |
| 1710 |
/* We've found the audio data. Read no further! */ |
| 1711 |
break; |
| 1712 |
} |
| 1713 |
pcm_data_pos = ftell(sf); |
| 1714 |
if (pcm_data_pos >= 0) { |
| 1715 |
pcm_data_pos += aiff_info.blkAlgn.offset; |
| 1716 |
} |
| 1717 |
if (fskip_uint32(sf, ui32_cksize) != 0) |
| 1718 |
return -1; |
| 1719 |
} |
| 1720 |
else { |
| 1721 |
uint32_t ui32_cksize; |
| 1722 |
ui32_cksize = read_32_bits_high_low(sf); |
| 1723 |
ui32_ChunkSize -= 4; |
| 1724 |
ui32_cksize = make_even_number_of_bytes_in_length(ui32_cksize); |
| 1725 |
if (ui32_ChunkSize < ui32_cksize) |
| 1726 |
return -1; |
| 1727 |
ui32_ChunkSize -= ui32_cksize; |
| 1728 |
if (fskip_uint32(sf, ui32_cksize) != 0) |
| 1729 |
return -1; |
| 1730 |
} |
| 1731 |
} |
| 1732 |
if (aiff_info.sampleFormat == IFF_ID_2CLE) { |
| 1733 |
global. pcm_is_ieee_float = 0; |
| 1734 |
global. pcmswapbytes = global_reader.swapbytes; |
| 1735 |
} |
| 1736 |
else if (aiff_info.sampleFormat == IFF_ID_2CBE) { |
| 1737 |
global. pcm_is_ieee_float = 0; |
| 1738 |
global. pcmswapbytes = !global_reader.swapbytes; |
| 1739 |
} |
| 1740 |
else if (aiff_info.sampleFormat == IFF_ID_NONE) { |
| 1741 |
global. pcm_is_ieee_float = 0; |
| 1742 |
global. pcmswapbytes = !global_reader.swapbytes; |
| 1743 |
} |
| 1744 |
else if (aiff_info.sampleFormat == IFF_ID_FL32) { |
| 1745 |
global. pcm_is_ieee_float = 1; |
| 1746 |
global. pcmswapbytes = !global_reader.swapbytes; |
| 1747 |
} |
| 1748 |
/* 64 bit floating point reading is still missing |
| 1749 |
else if (aiff_info.sampleFormat == IFF_ID_FL64) { |
| 1750 |
global. pcm_is_ieee_float = 1; |
| 1751 |
global. pcmswapbytes = !global_reader.swapbytes; |
| 1752 |
} |
| 1753 |
*/ |
| 1754 |
else { |
| 1755 |
return -1; |
| 1756 |
} |
| 1757 |
|
| 1758 |
/* DEBUGF("Parsed AIFF %d\n", is_aiff); */ |
| 1759 |
if (seen_comm_chunk && (seen_ssnd_chunk > 0 || aiff_info.numSampleFrames == 0)) { |
| 1760 |
/* make sure the header is sane */ |
| 1761 |
if (0 != aiff_check2(&aiff_info)) |
| 1762 |
return 0; |
| 1763 |
if (!set_input_num_channels(gfp, aiff_info.numChannels)) |
| 1764 |
return 0; |
| 1765 |
if (!set_input_samplerate(gfp, (int) aiff_info.sampleRate)) |
| 1766 |
return 0; |
| 1767 |
(void) lame_set_num_samples(gfp, aiff_info.numSampleFrames); |
| 1768 |
global. pcmbitwidth = aiff_info.sampleSize; |
| 1769 |
global. pcm_is_unsigned_8bit = 0; |
| 1770 |
if (pcm_data_pos >= 0) { |
| 1771 |
if (fseek(sf, pcm_data_pos, SEEK_SET) != 0) { |
| 1772 |
if (global_ui_config.silent < 10) { |
| 1773 |
error_printf("Can't rewind stream to audio data position\n"); |
| 1774 |
} |
| 1775 |
return 0; |
| 1776 |
} |
| 1777 |
} |
| 1778 |
|
| 1779 |
return 1; |
| 1780 |
} |
| 1781 |
return -1; |
| 1782 |
} |
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
/************************************************************************ |
| 1787 |
* |
| 1788 |
* parse_file_header |
| 1789 |
* |
| 1790 |
* PURPOSE: Read the header from a bytestream. Try to determine whether |
| 1791 |
* it's a WAV file or AIFF without rewinding, since rewind |
| 1792 |
* doesn't work on pipes and there's a good chance we're reading |
| 1793 |
* from stdin (otherwise we'd probably be using libsndfile). |
| 1794 |
* |
| 1795 |
* When this function returns, the file offset will be positioned at the |
| 1796 |
* beginning of the sound data. |
| 1797 |
* |
| 1798 |
************************************************************************/ |
| 1799 |
|
| 1800 |
static int |
| 1801 |
parse_file_header(lame_global_flags * gfp, FILE * sf) |
| 1802 |
{ |
| 1803 |
uint32_t ui32_type = read_32_bits_high_low(sf); |
| 1804 |
/* |
| 1805 |
DEBUGF( |
| 1806 |
"First word of input stream: %08x '%4.4s'\n", ui32_type, (char*) &type); |
| 1807 |
*/ |
| 1808 |
global. count_samples_carefully = 0; |
| 1809 |
global. pcm_is_unsigned_8bit = global_raw_pcm.in_signed == 1 ? 0 : 1; |
| 1810 |
/*global_reader.input_format = sf_raw; commented out, because it is better to fail |
| 1811 |
here as to encode some hundreds of input files not supported by LAME |
| 1812 |
If you know you have RAW PCM data, use the -r switch |
| 1813 |
*/ |
| 1814 |
|
| 1815 |
if (ui32_type == WAV_ID_RIFF) { |
| 1816 |
/* It's probably a WAV file */ |
| 1817 |
int const ret = parse_wave_header(gfp, sf); |
| 1818 |
if (ret == sf_mp123) { |
| 1819 |
global. count_samples_carefully = 1; |
| 1820 |
return sf_mp123; |
| 1821 |
} |
| 1822 |
if (ret > 0) { |
| 1823 |
if (lame_get_num_samples(gfp) == MAX_U_32_NUM || global_reader.ignorewavlength == 1) |
| 1824 |
{ |
| 1825 |
global. count_samples_carefully = 0; |
| 1826 |
lame_set_num_samples(gfp, MAX_U_32_NUM); |
| 1827 |
} |
| 1828 |
else |
| 1829 |
global. count_samples_carefully = 1; |
| 1830 |
return sf_wave; |
| 1831 |
} |
| 1832 |
if (ret < 0) { |
| 1833 |
if (global_ui_config.silent < 10) { |
| 1834 |
error_printf("Warning: corrupt or unsupported WAVE format\n"); |
| 1835 |
} |
| 1836 |
} |
| 1837 |
} |
| 1838 |
else if (ui32_type == IFF_ID_FORM) { |
| 1839 |
/* It's probably an AIFF file */ |
| 1840 |
int const ret = parse_aiff_header(gfp, sf); |
| 1841 |
if (ret > 0) { |
| 1842 |
global. count_samples_carefully = 1; |
| 1843 |
return sf_aiff; |
| 1844 |
} |
| 1845 |
if (ret < 0) { |
| 1846 |
if (global_ui_config.silent < 10) { |
| 1847 |
error_printf("Warning: corrupt or unsupported AIFF format\n"); |
| 1848 |
} |
| 1849 |
} |
| 1850 |
} |
| 1851 |
else { |
| 1852 |
if (global_ui_config.silent < 10) { |
| 1853 |
error_printf("Warning: unsupported audio format\n"); |
| 1854 |
} |
| 1855 |
} |
| 1856 |
return sf_unknown; |
| 1857 |
} |
| 1858 |
|
| 1859 |
|
| 1860 |
static int |
| 1861 |
open_mpeg_file_part2(lame_t gfp, FILE* musicin, char const *inPath, int *enc_delay, int *enc_padding) |
| 1862 |
{ |
| 1863 |
#ifdef HAVE_MPG123 |
| 1864 |
if (-1 == lame123_decode_initfile(musicin, &global_decoder.mp3input_data, enc_delay, enc_padding)) { |
| 1865 |
if (global_ui_config.silent < 10) { |
| 1866 |
error_printf("Error opening MPEG input file %s.\n", inPath); |
| 1867 |
} |
| 1868 |
return 0; |
| 1869 |
} |
| 1870 |
#else |
| 1871 |
#ifdef HAVE_MPGLIB |
| 1872 |
if (-1 == lame_decode_initfile(musicin, &global_decoder.mp3input_data, enc_delay, enc_padding)) { |
| 1873 |
if (global_ui_config.silent < 10) { |
| 1874 |
error_printf("Error reading headers in mp3 input file %s.\n", inPath); |
| 1875 |
} |
| 1876 |
return 0; |
| 1877 |
} |
| 1878 |
#endif |
| 1879 |
#endif |
| 1880 |
if (!set_input_num_channels(gfp, global_decoder.mp3input_data.stereo)) { |
| 1881 |
return 0; |
| 1882 |
} |
| 1883 |
if (!set_input_samplerate(gfp, global_decoder.mp3input_data.samplerate)) { |
| 1884 |
return 0; |
| 1885 |
} |
| 1886 |
(void) lame_set_num_samples(gfp, global_decoder.mp3input_data.nsamp); |
| 1887 |
return 1; |
| 1888 |
} |
| 1889 |
|
| 1890 |
|
| 1891 |
static FILE * |
| 1892 |
open_wave_file(lame_t gfp, char const *inPath, int *enc_delay, int *enc_padding) |
| 1893 |
{ |
| 1894 |
FILE *musicin; |
| 1895 |
|
| 1896 |
/* set the defaults from info incase we cannot determine them from file */ |
| 1897 |
lame_set_num_samples(gfp, MAX_U_32_NUM); |
| 1898 |
|
| 1899 |
if (!strcmp(inPath, "-")) { |
| 1900 |
lame_set_stream_binary_mode(musicin = stdin); /* Read from standard input. */ |
| 1901 |
} |
| 1902 |
else { |
| 1903 |
if ((musicin = lame_fopen(inPath, "rb")) == NULL) { |
| 1904 |
if (global_ui_config.silent < 10) { |
| 1905 |
error_printf("Could not find \"%s\".\n", inPath); |
| 1906 |
} |
| 1907 |
return 0; |
| 1908 |
} |
| 1909 |
} |
| 1910 |
|
| 1911 |
if (global_reader.input_format == sf_ogg) { |
| 1912 |
if (global_ui_config.silent < 10) { |
| 1913 |
error_printf("sorry, vorbis support in LAME is deprecated.\n"); |
| 1914 |
} |
| 1915 |
close_input_file(musicin); |
| 1916 |
return 0; |
| 1917 |
} |
| 1918 |
else if (global_reader.input_format == sf_raw) { |
| 1919 |
/* assume raw PCM */ |
| 1920 |
if (global_ui_config.silent < 9) { |
| 1921 |
console_printf("Assuming raw pcm input file"); |
| 1922 |
if (global_reader.swapbytes) |
| 1923 |
console_printf(" : Forcing byte-swapping\n"); |
| 1924 |
else |
| 1925 |
console_printf("\n"); |
| 1926 |
} |
| 1927 |
global. pcmswapbytes = global_reader.swapbytes; |
| 1928 |
} |
| 1929 |
else { |
| 1930 |
global_reader.input_format = parse_file_header(gfp, musicin); |
| 1931 |
} |
| 1932 |
if (global_reader.input_format == sf_mp123) { |
| 1933 |
if (open_mpeg_file_part2(gfp, musicin, inPath, enc_delay, enc_padding)) |
| 1934 |
return musicin; |
| 1935 |
close_input_file(musicin); |
| 1936 |
return 0; |
| 1937 |
} |
| 1938 |
if (global_reader.input_format == sf_unknown) { |
| 1939 |
close_input_file(musicin); |
| 1940 |
return 0; |
| 1941 |
} |
| 1942 |
|
| 1943 |
if (lame_get_num_samples(gfp) == MAX_U_32_NUM && musicin != stdin) { |
| 1944 |
int const tmp_num_channels = lame_get_num_channels(gfp); |
| 1945 |
double const flen = lame_get_file_size(musicin); /* try to figure out num_samples */ |
| 1946 |
if (flen >= 0 && tmp_num_channels > 0 ) { |
| 1947 |
/* try file size, assume 2 bytes per sample */ |
| 1948 |
unsigned long fsize = (unsigned long) (flen / (2 * tmp_num_channels)); |
| 1949 |
(void) lame_set_num_samples(gfp, fsize); |
| 1950 |
global. count_samples_carefully = 0; |
| 1951 |
} |
| 1952 |
} |
| 1953 |
return musicin; |
| 1954 |
} |
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
static FILE * |
| 1959 |
open_mpeg_file(lame_t gfp, char const *inPath, int *enc_delay, int *enc_padding) |
| 1960 |
{ |
| 1961 |
FILE *musicin; |
| 1962 |
|
| 1963 |
/* set the defaults from info incase we cannot determine them from file */ |
| 1964 |
lame_set_num_samples(gfp, MAX_U_32_NUM); |
| 1965 |
|
| 1966 |
if (strcmp(inPath, "-") == 0) { |
| 1967 |
musicin = stdin; |
| 1968 |
lame_set_stream_binary_mode(musicin); /* Read from standard input. */ |
| 1969 |
} |
| 1970 |
else { |
| 1971 |
musicin = lame_fopen(inPath, "rb"); |
| 1972 |
if (musicin == NULL) { |
| 1973 |
if (global_ui_config.silent < 10) { |
| 1974 |
error_printf("Could not find \"%s\".\n", inPath); |
| 1975 |
} |
| 1976 |
return 0; |
| 1977 |
} |
| 1978 |
} |
| 1979 |
#ifdef AMIGA_MPEGA |
| 1980 |
if (-1 == lame_decode_initfile_amiga(inPath, &global_decoder.mp3input_data)) { |
| 1981 |
if (global_ui_config.silent < 10) { |
| 1982 |
error_printf("Error reading headers in mp3 input file %s.\n", inPath); |
| 1983 |
} |
| 1984 |
close_input_file(musicin); |
| 1985 |
return 0; |
| 1986 |
} |
| 1987 |
#endif |
| 1988 |
if ( 0 == open_mpeg_file_part2(gfp, musicin, inPath, enc_delay, enc_padding) ) { |
| 1989 |
close_input_file(musicin); |
| 1990 |
return 0; |
| 1991 |
} |
| 1992 |
if (lame_get_num_samples(gfp) == MAX_U_32_NUM && musicin != stdin) { |
| 1993 |
double flen = lame_get_file_size(musicin); /* try to figure out num_samples */ |
| 1994 |
if (flen >= 0) { |
| 1995 |
/* try file size, assume 2 bytes per sample */ |
| 1996 |
if (global_decoder.mp3input_data.bitrate > 0) { |
| 1997 |
double totalseconds = |
| 1998 |
(flen * 8.0 / (1000.0 * global_decoder.mp3input_data.bitrate)); |
| 1999 |
unsigned long tmp_num_samples = |
| 2000 |
(unsigned long) (totalseconds * lame_get_in_samplerate(gfp)); |
| 2001 |
|
| 2002 |
(void) lame_set_num_samples(gfp, tmp_num_samples); |
| 2003 |
global_decoder.mp3input_data.nsamp = tmp_num_samples; |
| 2004 |
global. count_samples_carefully = 0; |
| 2005 |
} |
| 2006 |
} |
| 2007 |
} |
| 2008 |
return musicin; |
| 2009 |
} |
| 2010 |
|
| 2011 |
|
| 2012 |
static int |
| 2013 |
close_input_file(FILE * musicin) |
| 2014 |
{ |
| 2015 |
int ret = 0; |
| 2016 |
|
| 2017 |
if (musicin != stdin && musicin != 0) { |
| 2018 |
ret = fclose(musicin); |
| 2019 |
} |
| 2020 |
if (ret != 0) { |
| 2021 |
if (global_ui_config.silent < 10) { |
| 2022 |
error_printf("Could not close audio input file\n"); |
| 2023 |
} |
| 2024 |
} |
| 2025 |
return ret; |
| 2026 |
} |
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 2030 |
#if defined(HAVE_MPGLIB) |
| 2031 |
static int |
| 2032 |
check_aid(const unsigned char *header) |
| 2033 |
{ |
| 2034 |
return 0 == memcmp(header, "AiD\1", 4); |
| 2035 |
} |
| 2036 |
|
| 2037 |
/* |
| 2038 |
* Please check this and don't kill me if there's a bug |
| 2039 |
* This is a (nearly?) complete header analysis for a MPEG-1/2/2.5 Layer I, II or III |
| 2040 |
* data stream |
| 2041 |
*/ |
| 2042 |
|
| 2043 |
static int |
| 2044 |
is_syncword_mp123(const void *const headerptr) |
| 2045 |
{ |
| 2046 |
const unsigned char *const p = headerptr; |
| 2047 |
static const char abl2[16] = { 0, 7, 7, 7, 0, 7, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8 }; |
| 2048 |
|
| 2049 |
if ((p[0] & 0xFF) != 0xFF) |
| 2050 |
return 0; /* first 8 bits must be '1' */ |
| 2051 |
if ((p[1] & 0xE0) != 0xE0) |
| 2052 |
return 0; /* next 3 bits are also */ |
| 2053 |
if ((p[1] & 0x18) == 0x08) |
| 2054 |
return 0; /* no MPEG-1, -2 or -2.5 */ |
| 2055 |
switch (p[1] & 0x06) { |
| 2056 |
default: |
| 2057 |
case 0x00: /* illegal Layer */ |
| 2058 |
return 0; |
| 2059 |
|
| 2060 |
case 0x02: /* Layer3 */ |
| 2061 |
if (global_reader.input_format != sf_mp3 && global_reader.input_format != sf_mp123) { |
| 2062 |
return 0; |
| 2063 |
} |
| 2064 |
global_reader.input_format = sf_mp3; |
| 2065 |
break; |
| 2066 |
|
| 2067 |
case 0x04: /* Layer2 */ |
| 2068 |
if (global_reader.input_format != sf_mp2 && global_reader.input_format != sf_mp123) { |
| 2069 |
return 0; |
| 2070 |
} |
| 2071 |
global_reader.input_format = sf_mp2; |
| 2072 |
break; |
| 2073 |
|
| 2074 |
case 0x06: /* Layer1 */ |
| 2075 |
if (global_reader.input_format != sf_mp1 && global_reader.input_format != sf_mp123) { |
| 2076 |
return 0; |
| 2077 |
} |
| 2078 |
global_reader.input_format = sf_mp1; |
| 2079 |
break; |
| 2080 |
} |
| 2081 |
if ((p[1] & 0x06) == 0x00) |
| 2082 |
return 0; /* no Layer I, II and III */ |
| 2083 |
if ((p[2] & 0xF0) == 0xF0) |
| 2084 |
return 0; /* bad bitrate */ |
| 2085 |
if ((p[2] & 0x0C) == 0x0C) |
| 2086 |
return 0; /* no sample frequency with (32,44.1,48)/(1,2,4) */ |
| 2087 |
if ((p[1] & 0x18) == 0x18 && (p[1] & 0x06) == 0x04 && abl2[p[2] >> 4] & (1 << (p[3] >> 6))) |
| 2088 |
return 0; |
| 2089 |
if ((p[3] & 3) == 2) |
| 2090 |
return 0; /* reserved enphasis mode */ |
| 2091 |
return 1; |
| 2092 |
} |
| 2093 |
|
| 2094 |
static size_t |
| 2095 |
lenOfId3v2Tag(unsigned char const* buf) |
| 2096 |
{ |
| 2097 |
unsigned int b0 = buf[0] & 127; |
| 2098 |
unsigned int b1 = buf[1] & 127; |
| 2099 |
unsigned int b2 = buf[2] & 127; |
| 2100 |
unsigned int b3 = buf[3] & 127; |
| 2101 |
return (((((b0 << 7) + b1) << 7) + b2) << 7) + b3; |
| 2102 |
} |
| 2103 |
#endif |
| 2104 |
|
| 2105 |
#ifdef HAVE_MPG123 |
| 2106 |
#define CHECK123(code) if(MPG123_OK != (code)) return -1 |
| 2107 |
|
| 2108 |
#ifdef _WIN32 |
| 2109 |
static mpg123_ssize_t lame123_read_from_file(void* handle, void* buffer, size_t size) |
| 2110 |
{ |
| 2111 |
return fread(buffer, 1, size, (FILE*)handle); |
| 2112 |
} |
| 2113 |
|
| 2114 |
static off_t lame123_seek_in_file(void* handle, off_t offset, int direction) |
| 2115 |
{ |
| 2116 |
if (fseek((FILE*)handle, offset, direction) != 0) |
| 2117 |
return (off_t)-1; |
| 2118 |
return ftell((FILE*)handle); |
| 2119 |
} |
| 2120 |
|
| 2121 |
static void lame123_cleanup_file(void* handle) |
| 2122 |
{ |
| 2123 |
/* don't call fclose(); close_input_file() will do that */ |
| 2124 |
} |
| 2125 |
#endif |
| 2126 |
|
| 2127 |
int lame123_decode_initfile(FILE *fd, mp3data_struct *mp3data, int *enc_delay, int *enc_padding) |
| 2128 |
{ |
| 2129 |
off_t len; |
| 2130 |
unsigned char *id3buf; |
| 2131 |
size_t id3size; |
| 2132 |
struct mpg123_frameinfo fi; |
| 2133 |
long rate, val; |
| 2134 |
int channels; |
| 2135 |
|
| 2136 |
mpg123_init(); |
| 2137 |
memset(mp3data, 0, sizeof(mp3data_struct)); |
| 2138 |
if (global.hip) { |
| 2139 |
hip_decode_exit(global.hip); |
| 2140 |
} |
| 2141 |
global. hip = hip_decode_init(); |
| 2142 |
if(!global.hip->mh) |
| 2143 |
return -1; |
| 2144 |
/* TODO: enforce float format ... optionally be careful for builds |
| 2145 |
that only know 16 bit output. */ |
| 2146 |
mpg123_param(global.hip->mh, MPG123_ADD_FLAGS, MPG123_STORE_RAW_ID3, 0.); |
| 2147 |
mpg123_param(global.hip->mh, MPG123_ADD_FLAGS, MPG123_QUIET, 0.); |
| 2148 |
mpg123_format_none(global.hip->mh); |
| 2149 |
/* TODO: switch to MPG123_ENC_FLOAT_32, always! */ |
| 2150 |
CHECK123(mpg123_format2(global.hip->mh, |
| 2151 |
0, MPG123_MONO|MPG123_STEREO, MPG123_ENC_SIGNED_16)); |
| 2152 |
/* TODO: verboseness / silence set up */ |
| 2153 |
#ifdef _WIN32 |
| 2154 |
/* On Win32 compiles it can happen that lame.exe and libmp3lame.dll use |
| 2155 |
different C++ runtimes, which maintail different FILE* lists, and |
| 2156 |
fileno() would produce invalid file numbers for those msvcrt instances. |
| 2157 |
So use mpg123_replace_reader_handle() / mpg123_open_handle() here |
| 2158 |
instead of mpg123_open_fd(). */ |
| 2159 |
CHECK123(mpg123_replace_reader_handle(global.hip->mh, lame123_read_from_file, lame123_seek_in_file, lame123_cleanup_file)); |
| 2160 |
CHECK123(mpg123_open_handle(global.hip->mh, fd)); |
| 2161 |
#else |
| 2162 |
CHECK123(mpg123_open_fd(global.hip->mh, fileno(fd))); |
| 2163 |
#endif |
| 2164 |
/* Seek to get past Info frame and ID3v2. */ |
| 2165 |
CHECK123(mpg123_seek(global.hip->mh, SEEK_SET, 0)); |
| 2166 |
/* TODO: Figure out if MPG123_GAPLESS is desired or not. */ |
| 2167 |
/* Guessing seems to be OK, so we do not have to insist on knowing |
| 2168 |
if libmpg123 got that info from Info tag or not. */ |
| 2169 |
/* I am paranoid about off_t being larger than long or int. */ |
| 2170 |
len = mpg123_framelength(global.hip->mh); |
| 2171 |
if(len <= (unsigned long)-1) |
| 2172 |
mp3data->totalframes = len; |
| 2173 |
else |
| 2174 |
return -1; |
| 2175 |
len = mpg123_length(global.hip->mh); |
| 2176 |
if(len <= ((unsigned int)-1)/2) |
| 2177 |
mp3data->nsamp = len; |
| 2178 |
else |
| 2179 |
return -1; |
| 2180 |
/* Encoder delay and padding are not needed when libmpg123 handles gapless |
| 2181 |
decoding itself. So let's see if we get away with that. */ |
| 2182 |
mpg123_getstate(global.hip->mh, MPG123_ENC_DELAY, &val, NULL); |
| 2183 |
*enc_delay = val; |
| 2184 |
mpg123_getstate(global.hip->mh, MPG123_ENC_PADDING, &val, NULL); |
| 2185 |
*enc_padding = val; |
| 2186 |
if(global.in_id3v2_tag) |
| 2187 |
free(global.in_id3v2_tag); |
| 2188 |
global.in_id3v2_size = 0; |
| 2189 |
if( MPG123_OK == mpg123_id3_raw(global.hip->mh, NULL, NULL, |
| 2190 |
&id3buf, &id3size) && id3buf && id3size ) { |
| 2191 |
global.in_id3v2_tag = malloc(id3size); |
| 2192 |
if(global.in_id3v2_tag) { |
| 2193 |
memcpy(global.in_id3v2_tag, id3buf, id3size); |
| 2194 |
global.in_id3v2_size = id3size; |
| 2195 |
} |
| 2196 |
} |
| 2197 |
CHECK123(mpg123_info(global.hip->mh, &fi)); |
| 2198 |
CHECK123(mpg123_getformat(global.hip->mh, &rate, &channels, NULL)); |
| 2199 |
/* How much of this is actually needed for the frontend? */ |
| 2200 |
mp3data->header_parsed = 1; |
| 2201 |
mp3data->stereo = channels; /* Channel count correct? Or is dual mono different? */ |
| 2202 |
mp3data->samplerate = rate; |
| 2203 |
mp3data->mode = fi.mode; |
| 2204 |
mp3data->mode_ext = fi.mode_ext; |
| 2205 |
mp3data->framesize = mpg123_spf(global.hip->mh); |
| 2206 |
mp3data->bitrate = fi.bitrate; |
| 2207 |
if(global_reader.input_format == sf_mp123) switch(fi.layer) { |
| 2208 |
case 1: |
| 2209 |
global_reader.input_format = sf_mp1; |
| 2210 |
break; |
| 2211 |
case 2: |
| 2212 |
global_reader.input_format = sf_mp2; |
| 2213 |
break; |
| 2214 |
case 3: |
| 2215 |
global_reader.input_format = sf_mp3; |
| 2216 |
break; |
| 2217 |
} |
| 2218 |
|
| 2219 |
return 0; |
| 2220 |
} |
| 2221 |
#endif |
| 2222 |
|
| 2223 |
#ifdef HAVE_MPGLIB |
| 2224 |
int |
| 2225 |
lame_decode_initfile(FILE * fd, mp3data_struct * mp3data, int *enc_delay, int *enc_padding) |
| 2226 |
{ |
| 2227 |
/* VBRTAGDATA pTagData; */ |
| 2228 |
/* int xing_header,len2,num_frames; */ |
| 2229 |
unsigned char buf[100]; |
| 2230 |
int ret; |
| 2231 |
size_t len; |
| 2232 |
int aid_header; |
| 2233 |
short int pcm_l[1152], pcm_r[1152]; |
| 2234 |
int freeformat = 0; |
| 2235 |
|
| 2236 |
memset(mp3data, 0, sizeof(mp3data_struct)); |
| 2237 |
if (global.hip) { |
| 2238 |
hip_decode_exit(global.hip); |
| 2239 |
} |
| 2240 |
global. hip = hip_decode_init(); |
| 2241 |
hip_set_msgf(global.hip, global_ui_config.silent < 10 ? &frontend_msgf : 0); |
| 2242 |
hip_set_errorf(global.hip, global_ui_config.silent < 10 ? &frontend_errorf : 0); |
| 2243 |
hip_set_debugf(global.hip, &frontend_debugf); |
| 2244 |
|
| 2245 |
len = 4; |
| 2246 |
if (fread(buf, 1, len, fd) != len) |
| 2247 |
return -1; /* failed */ |
| 2248 |
while (buf[0] == 'I' && buf[1] == 'D' && buf[2] == '3') { |
| 2249 |
len = 6; |
| 2250 |
if (fread(&buf[4], 1, len, fd) != len) |
| 2251 |
return -1; /* failed */ |
| 2252 |
len = lenOfId3v2Tag(&buf[6]); |
| 2253 |
if (global.in_id3v2_size < 1) { |
| 2254 |
global.in_id3v2_size = 10 + len; |
| 2255 |
global.in_id3v2_tag = malloc(global.in_id3v2_size); |
| 2256 |
if (global.in_id3v2_tag) { |
| 2257 |
memcpy(global.in_id3v2_tag, buf, 10); |
| 2258 |
if (fread(&global.in_id3v2_tag[10], 1, len, fd) != len) |
| 2259 |
return -1; /* failed */ |
| 2260 |
len = 0; /* copied, nothing to skip */ |
| 2261 |
} |
| 2262 |
else { |
| 2263 |
global.in_id3v2_size = 0; |
| 2264 |
} |
| 2265 |
} |
| 2266 |
if (len > LONG_MAX) |
| 2267 |
return -1; |
| 2268 |
if (fskip_long(fd, (long) len, SEEK_CUR) != 0) |
| 2269 |
return -1; |
| 2270 |
len = 4; |
| 2271 |
if (fread(&buf, 1, len, fd) != len) |
| 2272 |
return -1; /* failed */ |
| 2273 |
} |
| 2274 |
aid_header = check_aid(buf); |
| 2275 |
if (aid_header) { |
| 2276 |
if (fread(&buf, 1, 2, fd) != 2) |
| 2277 |
return -1; /* failed */ |
| 2278 |
aid_header = (unsigned char) buf[0] + 256 * (unsigned char) buf[1]; |
| 2279 |
if (global_ui_config.silent < 9) { |
| 2280 |
console_printf("Album ID found. length=%i \n", aid_header); |
| 2281 |
} |
| 2282 |
/* skip rest of AID, except for 6 bytes we have already read */ |
| 2283 |
fskip_long(fd, aid_header - 6, SEEK_CUR); |
| 2284 |
|
| 2285 |
/* read 4 more bytes to set up buffer for MP3 header check */ |
| 2286 |
if (fread(&buf, 1, len, fd) != len) |
| 2287 |
return -1; /* failed */ |
| 2288 |
} |
| 2289 |
len = 4; |
| 2290 |
while (!is_syncword_mp123(buf)) { |
| 2291 |
unsigned int i; |
| 2292 |
for (i = 0; i < len - 1; i++) |
| 2293 |
buf[i] = buf[i + 1]; |
| 2294 |
if (fread(buf + len - 1, 1, 1, fd) != 1) |
| 2295 |
return -1; /* failed */ |
| 2296 |
} |
| 2297 |
|
| 2298 |
if ((buf[2] & 0xf0) == 0) { |
| 2299 |
if (global_ui_config.silent < 9) { |
| 2300 |
console_printf("Input file is freeformat.\n"); |
| 2301 |
} |
| 2302 |
freeformat = 1; |
| 2303 |
} |
| 2304 |
/* now parse the current buffer looking for MP3 headers. */ |
| 2305 |
/* (as of 11/00: mpglib modified so that for the first frame where */ |
| 2306 |
/* headers are parsed, no data will be decoded. */ |
| 2307 |
/* However, for freeformat, we need to decode an entire frame, */ |
| 2308 |
/* so mp3data->bitrate will be 0 until we have decoded the first */ |
| 2309 |
/* frame. Cannot decode first frame here because we are not */ |
| 2310 |
/* yet prepared to handle the output. */ |
| 2311 |
ret = hip_decode1_headersB(global.hip, buf, len, pcm_l, pcm_r, mp3data, enc_delay, enc_padding); |
| 2312 |
if (-1 == ret) |
| 2313 |
return -1; |
| 2314 |
|
| 2315 |
/* repeat until we decode a valid mp3 header. */ |
| 2316 |
while (!mp3data->header_parsed) { |
| 2317 |
len = fread(buf, 1, sizeof(buf), fd); |
| 2318 |
if (len != sizeof(buf)) |
| 2319 |
return -1; |
| 2320 |
ret = |
| 2321 |
hip_decode1_headersB(global.hip, buf, len, pcm_l, pcm_r, mp3data, enc_delay, |
| 2322 |
enc_padding); |
| 2323 |
if (-1 == ret) |
| 2324 |
return -1; |
| 2325 |
} |
| 2326 |
|
| 2327 |
if (mp3data->bitrate == 0 && !freeformat) { |
| 2328 |
if (global_ui_config.silent < 10) { |
| 2329 |
error_printf("fail to sync...\n"); |
| 2330 |
} |
| 2331 |
return lame_decode_initfile(fd, mp3data, enc_delay, enc_padding); |
| 2332 |
} |
| 2333 |
|
| 2334 |
if (mp3data->totalframes > 0) { |
| 2335 |
/* mpglib found a Xing VBR header and computed nsamp & totalframes */ |
| 2336 |
} |
| 2337 |
else { |
| 2338 |
/* set as unknown. Later, we will take a guess based on file size |
| 2339 |
* ant bitrate */ |
| 2340 |
mp3data->nsamp = MAX_U_32_NUM; |
| 2341 |
} |
| 2342 |
|
| 2343 |
|
| 2344 |
/* |
| 2345 |
report_printf("ret = %i NEED_MORE=%i \n",ret,MP3_NEED_MORE); |
| 2346 |
report_printf("stereo = %i \n",mp.fr.stereo); |
| 2347 |
report_printf("samp = %i \n",freqs[mp.fr.sampling_frequency]); |
| 2348 |
report_printf("framesize = %i \n",framesize); |
| 2349 |
report_printf("bitrate = %i \n",mp3data->bitrate); |
| 2350 |
report_printf("num frames = %ui \n",num_frames); |
| 2351 |
report_printf("num samp = %ui \n",mp3data->nsamp); |
| 2352 |
report_printf("mode = %i \n",mp.fr.mode); |
| 2353 |
*/ |
| 2354 |
|
| 2355 |
return 0; |
| 2356 |
} |
| 2357 |
|
| 2358 |
/* |
| 2359 |
For lame_decode_fromfile: return code |
| 2360 |
-1 error |
| 2361 |
n number of samples output. either 576 or 1152 depending on MP3 file. |
| 2362 |
|
| 2363 |
|
| 2364 |
For lame_decode1_headers(): return code |
| 2365 |
-1 error |
| 2366 |
0 ok, but need more data before outputing any samples |
| 2367 |
n number of samples output. either 576 or 1152 depending on MP3 file. |
| 2368 |
*/ |
| 2369 |
static int |
| 2370 |
lame_decode_fromfile(FILE * fd, short pcm_l[], short pcm_r[], mp3data_struct * mp3data) |
| 2371 |
{ |
| 2372 |
int ret = 0; |
| 2373 |
size_t len = 0; |
| 2374 |
unsigned char buf[1024]; |
| 2375 |
|
| 2376 |
/* first see if we still have data buffered in the decoder: */ |
| 2377 |
ret = hip_decode1_headers(global.hip, buf, len, pcm_l, pcm_r, mp3data); |
| 2378 |
if (ret != 0) |
| 2379 |
return ret; |
| 2380 |
|
| 2381 |
|
| 2382 |
/* read until we get a valid output frame */ |
| 2383 |
for (;;) { |
| 2384 |
len = fread(buf, 1, 1024, fd); |
| 2385 |
if (len == 0) { |
| 2386 |
/* we are done reading the file, but check for buffered data */ |
| 2387 |
ret = hip_decode1_headers(global.hip, buf, len, pcm_l, pcm_r, mp3data); |
| 2388 |
if (ret <= 0) { |
| 2389 |
return -1; /* done with file */ |
| 2390 |
} |
| 2391 |
break; |
| 2392 |
} |
| 2393 |
|
| 2394 |
ret = hip_decode1_headers(global.hip, buf, len, pcm_l, pcm_r, mp3data); |
| 2395 |
if (ret == -1) { |
| 2396 |
return -1; |
| 2397 |
} |
| 2398 |
if (ret > 0) |
| 2399 |
break; |
| 2400 |
} |
| 2401 |
return ret; |
| 2402 |
} |
| 2403 |
#endif /* defined(HAVE_MPGLIB) */ |
| 2404 |
|
| 2405 |
|
| 2406 |
int |
| 2407 |
is_mpeg_file_format(int input_file_format) |
| 2408 |
{ |
| 2409 |
switch (input_file_format) { |
| 2410 |
case sf_mp1: |
| 2411 |
return 1; |
| 2412 |
case sf_mp2: |
| 2413 |
return 2; |
| 2414 |
case sf_mp3: |
| 2415 |
return 3; |
| 2416 |
case sf_mp123: |
| 2417 |
return -1; |
| 2418 |
default: |
| 2419 |
break; |
| 2420 |
} |
| 2421 |
return 0; |
| 2422 |
} |
| 2423 |
|
| 2424 |
|
| 2425 |
#define LOW__BYTE(x) (x & 0x00ff) |
| 2426 |
#define HIGH_BYTE(x) ((x >> 8) & 0x00ff) |
| 2427 |
|
| 2428 |
void |
| 2429 |
put_audio16(FILE * outf, short Buffer[2][1152], int iread, int nch) |
| 2430 |
{ |
| 2431 |
char data[2 * 1152 * 2]; |
| 2432 |
int i, m = 0; |
| 2433 |
|
| 2434 |
if (global_decoder.disable_wav_header && global_reader.swapbytes) { |
| 2435 |
if (nch == 1) { |
| 2436 |
for (i = 0; i < iread; i++) { |
| 2437 |
short x = Buffer[0][i]; |
| 2438 |
/* write 16 Bits High Low */ |
| 2439 |
data[m++] = HIGH_BYTE(x); |
| 2440 |
data[m++] = LOW__BYTE(x); |
| 2441 |
} |
| 2442 |
} |
| 2443 |
else { |
| 2444 |
for (i = 0; i < iread; i++) { |
| 2445 |
short x = Buffer[0][i], y = Buffer[1][i]; |
| 2446 |
/* write 16 Bits High Low */ |
| 2447 |
data[m++] = HIGH_BYTE(x); |
| 2448 |
data[m++] = LOW__BYTE(x); |
| 2449 |
/* write 16 Bits High Low */ |
| 2450 |
data[m++] = HIGH_BYTE(y); |
| 2451 |
data[m++] = LOW__BYTE(y); |
| 2452 |
} |
| 2453 |
} |
| 2454 |
} |
| 2455 |
else { |
| 2456 |
if (nch == 1) { |
| 2457 |
for (i = 0; i < iread; i++) { |
| 2458 |
short x = Buffer[0][i]; |
| 2459 |
/* write 16 Bits Low High */ |
| 2460 |
data[m++] = LOW__BYTE(x); |
| 2461 |
data[m++] = HIGH_BYTE(x); |
| 2462 |
} |
| 2463 |
} |
| 2464 |
else { |
| 2465 |
for (i = 0; i < iread; i++) { |
| 2466 |
short x = Buffer[0][i], y = Buffer[1][i]; |
| 2467 |
/* write 16 Bits Low High */ |
| 2468 |
data[m++] = LOW__BYTE(x); |
| 2469 |
data[m++] = HIGH_BYTE(x); |
| 2470 |
/* write 16 Bits Low High */ |
| 2471 |
data[m++] = LOW__BYTE(y); |
| 2472 |
data[m++] = HIGH_BYTE(y); |
| 2473 |
} |
| 2474 |
} |
| 2475 |
} |
| 2476 |
if (m > 0) { |
| 2477 |
fwrite(data, 1, m, outf); |
| 2478 |
} |
| 2479 |
if (global_writer.flush_write == 1) { |
| 2480 |
fflush(outf); |
| 2481 |
} |
| 2482 |
} |
| 2483 |
|
| 2484 |
hip_t |
| 2485 |
get_hip(void) |
| 2486 |
{ |
| 2487 |
return global.hip; |
| 2488 |
} |
| 2489 |
|
| 2490 |
size_t |
| 2491 |
sizeOfOldTag(lame_t gf) |
| 2492 |
{ |
| 2493 |
(void) gf; |
| 2494 |
return global.in_id3v2_size; |
| 2495 |
} |
| 2496 |
|
| 2497 |
unsigned char* |
| 2498 |
getOldTag(lame_t gf) |
| 2499 |
{ |
| 2500 |
(void) gf; |
| 2501 |
return global.in_id3v2_tag; |
| 2502 |
} |
| 2503 |
|
| 2504 |
/* end of get_audio.c */ |