get_audio.c 68 KB

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