mad.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. {
  2. Translation of the libmad headers for FreePascal
  3. Copyright (C) 2006 by Ivo Steinmann
  4. }
  5. (*
  6. * libmad - MPEG audio decoder library
  7. * Copyright (C) 2000-2003 Underbit Technologies, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * If you would like to negotiate alternate licensing terms, you may do
  24. * so by contacting: Underbit Technologies, Inc. <[email protected]>
  25. *)
  26. unit mad;
  27. {$mode objfpc}
  28. {$MINENUMSIZE 4}
  29. interface
  30. uses
  31. ctypes;
  32. {$IFDEF WINDOWS}
  33. {$DEFINE DYNLINK}
  34. {$ENDIF}
  35. {$DEFINE MAD_DISABLE_BUILTIN_DECODER}
  36. {$IFDEF DYNLINK}
  37. const
  38. {$IF Defined(WINDOWS)}
  39. madlib = 'libmad.dll';
  40. {$ELSEIF Defined(UNIX)}
  41. madlib = 'libmad.so';
  42. {$ELSE}
  43. {$MESSAGE ERROR 'DYNLINK not supported'}
  44. {$IFEND}
  45. {$ELSE}
  46. {$LINKLIB mad}
  47. {$ENDIF}
  48. (***********************************************************************)
  49. (* Header : version.h *)
  50. (***********************************************************************)
  51. const
  52. MAD_VERSION_MAJOR = 0;
  53. MAD_VERSION_MINOR = 15;
  54. MAD_VERSION_PATCH = 1;
  55. MAD_VERSION_EXTRA = ' (beta)';
  56. function MAD_VERSION_STRINGIZE(num: cint): String;
  57. function MAD_VERSION_STRING(num: cint): String;
  58. function MAD_VERSION: String;
  59. const
  60. MAD_PUBLISHYEAR = '2000-2004';
  61. MAD_AUTHOR = 'Underbit Technologies, Inc.';
  62. MAD_EMAIL = '[email protected]';
  63. (***********************************************************************)
  64. (* Header : fixed.h *)
  65. (***********************************************************************)
  66. type
  67. mad_fixed_t = csint;
  68. mad_fixed64hi_t = csint;
  69. mad_fixed64lo_t = cuint;
  70. mad_fixed64_t = cslonglong;
  71. mad_sample_t = mad_fixed_t;
  72. {*
  73. * Fixed-point format: 0xABBBBBBB
  74. * A == whole part (sign + 3 bits)
  75. * B == fractional part (28 bits)
  76. *
  77. * Values are signed two's complement, so the effective range is:
  78. * 0x80000000 to 0x7fffffff
  79. * -8.0 to +7.9999999962747097015380859375
  80. *
  81. * The smallest representable value is:
  82. * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
  83. *
  84. * 28 bits of fractional accuracy represent about
  85. * 8.6 digits of decimal accuracy.
  86. *
  87. * Fixed-point numbers can be added or subtracted as normal
  88. * integers, but multiplication requires shifting the 64-bit result
  89. * from 56 fractional bits back to 28 (and rounding.)
  90. *
  91. * Changing the definition of MAD_F_FRACBITS is only partially
  92. * supported, and must be done with care.
  93. *}
  94. const
  95. MAD_F_FRACBITS = 28;
  96. // MAD_F_MIN = mad_fixed_t(-$80000000);
  97. MAD_F_MAX = mad_fixed_t(+$7fffffff);
  98. MAD_F_ONE = mad_fixed_t( $10000000);
  99. MAD_F_SCALEBITS = MAD_F_FRACBITS;
  100. //function mad_f_tofixed(x: double): mad_fixed_t;
  101. (***********************************************************************)
  102. (* Header : bit.h *)
  103. (***********************************************************************)
  104. type
  105. mad_bitptr = record
  106. byte : ^cuchar;
  107. cache : cushort;
  108. left : cushort;
  109. end;
  110. procedure mad_bit_init(var bitptr: mad_bitptr; byte: pcuchar); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  111. procedure mad_bit_finish(var bitptr: mad_bitptr);
  112. function mad_bit_length(var begin_, end_: mad_bitptr): cuint; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  113. function mad_bit_bitsleft(var bitptr: mad_bitptr): cushort;
  114. function mad_bit_nextbyte(var bitptr: mad_bitptr): pcuchar; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  115. procedure mad_bit_skip(var bitptr: mad_bitptr); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  116. function mad_bit_read(var bitptr: mad_bitptr; len: cuint): culong; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  117. procedure mad_bit_write(var bitptr: mad_bitptr; len: cuint; value: culong); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  118. function mad_bit_crc(bitptr: mad_bitptr; len: cuint; init: cushort): cushort; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  119. (***********************************************************************)
  120. (* Header : timer.h *)
  121. (***********************************************************************)
  122. type
  123. mad_timer_t = record
  124. seconds : cslong; { whole seconds }
  125. fraction : culong; { 1/MAD_TIMER_RESOLUTION seconds }
  126. end;
  127. const
  128. MAD_TIMER_RESOLUTION = 352800000;
  129. mad_timer_zero : mad_timer_t = (seconds:0; fraction:0);
  130. type
  131. mad_units = (
  132. MAD_UNITS_HOURS = -2,
  133. MAD_UNITS_MINUTES = -1,
  134. MAD_UNITS_SECONDS = 0,
  135. { metric units }
  136. MAD_UNITS_DECISECONDS = 10,
  137. MAD_UNITS_CENTISECONDS = 100,
  138. MAD_UNITS_MILLISECONDS = 1000,
  139. { audio sample units }
  140. MAD_UNITS_8000_HZ = 8000,
  141. MAD_UNITS_11025_HZ = 11025,
  142. MAD_UNITS_12000_HZ = 12000,
  143. MAD_UNITS_16000_HZ = 16000,
  144. MAD_UNITS_22050_HZ = 22050,
  145. MAD_UNITS_24000_HZ = 24000,
  146. MAD_UNITS_32000_HZ = 32000,
  147. MAD_UNITS_44100_HZ = 44100,
  148. MAD_UNITS_48000_HZ = 48000,
  149. { video frame/field units }
  150. MAD_UNITS_24_FPS = 24,
  151. MAD_UNITS_25_FPS = 25,
  152. MAD_UNITS_30_FPS = 30,
  153. MAD_UNITS_48_FPS = 48,
  154. MAD_UNITS_50_FPS = 50,
  155. MAD_UNITS_60_FPS = 60,
  156. { CD audio frames }
  157. MAD_UNITS_75_FPS = 75,
  158. { video drop-frame units }
  159. MAD_UNITS_23_976_FPS = -24,
  160. MAD_UNITS_24_975_FPS = -25,
  161. MAD_UNITS_29_97_FPS = -30,
  162. MAD_UNITS_47_952_FPS = -48,
  163. MAD_UNITS_49_95_FPS = -50,
  164. MAD_UNITS_59_94_FPS = -60
  165. );
  166. procedure mad_timer_reset(var timer: mad_timer_t);
  167. function mad_timer_compare(timer1: mad_timer_t; timer2: mad_timer_t): cint; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  168. function mad_timer_sign(timer: mad_timer_t): cint;
  169. procedure mad_timer_negate(var timer: mad_timer_t); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  170. function mad_timer_abs(timer: mad_timer_t): mad_timer_t; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  171. procedure mad_timer_set(var timer: mad_timer_t; seconds, numer, denom: culong); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  172. procedure mad_timer_add(var timer: mad_timer_t; incr: mad_timer_t); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  173. procedure mad_timer_multiply(var timer: mad_timer_t; scalar: cslong); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  174. function mad_timer_count(timer: mad_timer_t; units: mad_units): cslong; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  175. function mad_timer_fraction(timer: mad_timer_t; denom: culong): culong; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  176. procedure mad_timer_string(timer: mad_timer_t; dest, format: pcchar; units, fracunits: mad_units; subparts: culong); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  177. (***********************************************************************)
  178. (* Header : stream.h *)
  179. (***********************************************************************)
  180. type
  181. mad_error = (
  182. MAD_ERROR_NONE = $0000, { no error }
  183. MAD_ERROR_BUFLEN = $0001, { input buffer too small (or EOF) }
  184. MAD_ERROR_BUFPTR = $0002, { invalid (null) buffer pointer }
  185. MAD_ERROR_NOMEM = $0031, { not enough memory }
  186. MAD_ERROR_LOSTSYNC = $0101, { lost synchronization }
  187. MAD_ERROR_BADLAYER = $0102, { reserved header layer value }
  188. MAD_ERROR_BADBITRATE = $0103, { forbidden bitrate value }
  189. MAD_ERROR_BADSAMPLERATE = $0104, { reserved sample frequency value }
  190. MAD_ERROR_BADEMPHASIS = $0105, { reserved emphasis value }
  191. MAD_ERROR_BADCRC = $0201, { CRC check failed }
  192. MAD_ERROR_BADBITALLOC = $0211, { forbidden bit allocation value }
  193. MAD_ERROR_BADSCALEFACTOR = $0221, { bad scalefactor index }
  194. MAD_ERROR_BADFRAMELEN = $0231, { bad frame length }
  195. MAD_ERROR_BADBIGVALUES = $0232, { bad big_values count }
  196. MAD_ERROR_BADBLOCKTYPE = $0233, { reserved block_type }
  197. MAD_ERROR_BADSCFSI = $0234, { bad scalefactor selection info }
  198. MAD_ERROR_BADDATAPTR = $0235, { bad main_data_begin pointer }
  199. MAD_ERROR_BADPART3LEN = $0236, { bad audio data length }
  200. MAD_ERROR_BADHUFFTABLE = $0237, { bad Huffman table select }
  201. MAD_ERROR_BADHUFFDATA = $0238, { Huffman data overrun }
  202. MAD_ERROR_BADSTEREO = $0239 { incompatible block_type for JS }
  203. );
  204. function MAD_RECOVERABLE(error: mad_error) : Boolean;
  205. type
  206. mad_stream = record
  207. buffer : pointer; { input bitstream buffer }
  208. bufend : pointer; { end of buffer }
  209. skiplen : culong; { bytes to skip before next frame }
  210. sync : cint; { stream sync found }
  211. freerate : culong; { free bitrate (fixed) }
  212. this_frame : pointer; { start of current frame }
  213. next_frame : pointer; { start of next frame }
  214. ptr : mad_bitptr; { current processing bit pointer }
  215. anc_ptr : mad_bitptr; { ancillary bits pointer }
  216. anc_bitlen : cuint; { number of ancillary bits }
  217. main_data : pointer; { Layer III main_data() }
  218. md_len : cuint; { bytes in main_data }
  219. options : cint; { decoding options (see below) }
  220. error : mad_error; { error code (see above) }
  221. end;
  222. const
  223. MAD_BUFFER_GUARD = 8;
  224. MAD_BUFFER_MDLEN = 511 + 2048 + MAD_BUFFER_GUARD;
  225. MAD_OPTION_IGNORECRC = $0001; { ignore CRC errors }
  226. MAD_OPTION_HALFSAMPLERATE = $0002; { generate PCM at 1/2 sample rate }
  227. {$if defined(false)} { not yet implemented }
  228. MAD_OPTION_LEFTCHANNEL = $0010; { decode left channel only }
  229. MAD_OPTION_RIGHTCHANNEL = $0020; { decode right channel only }
  230. MAD_OPTION_SINGLECHANNEL = $0030; { combine channels }
  231. {$ifend}
  232. procedure mad_stream_init(var stream: mad_stream); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  233. procedure mad_stream_finish(var stream: mad_stream); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  234. procedure mad_stream_options(stream: mad_stream; opts: cint);
  235. procedure mad_stream_buffer(var stream: mad_stream; buffer: pcuchar; length: culong); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  236. procedure mad_stream_skip(var stream: mad_stream; length: culong); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  237. function mad_stream_sync(var stream: mad_stream): cint; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  238. function mad_stream_errorstr(var stream: mad_stream): pcchar; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  239. (***********************************************************************)
  240. (* Header : frame.h *)
  241. (***********************************************************************)
  242. type
  243. mad_layer = (
  244. MAD_LAYER_I = 1, { Layer I }
  245. MAD_LAYER_II = 2, { Layer II }
  246. MAD_LAYER_III = 3 { Layer III }
  247. );
  248. mad_mode = (
  249. MAD_MODE_SINGLE_CHANNEL = 0, { single channel }
  250. MAD_MODE_DUAL_CHANNEL = 1, { dual channel }
  251. MAD_MODE_JOINT_STEREO = 2, { joint (MS/intensity) stereo }
  252. MAD_MODE_STEREO = 3 { normal LR stereo }
  253. );
  254. mad_emphasis = (
  255. MAD_EMPHASIS_NONE = 0, { no emphasis }
  256. MAD_EMPHASIS_50_15_US = 1, { 50/15 microseconds emphasis }
  257. MAD_EMPHASIS_CCITT_J_17 = 3, { CCITT J.17 emphasis }
  258. MAD_EMPHASIS_RESERVED = 2 { unknown emphasis }
  259. );
  260. mad_header = record
  261. layer : mad_layer; { audio layer (1, 2, or 3) }
  262. mode : mad_mode; { channel mode (see above) }
  263. mode_extension : cint; { additional mode info }
  264. emphasis : mad_emphasis; { de-emphasis to use (see above) }
  265. bitrate : culong; { stream bitrate (bps) }
  266. samplerate : cuint; { sampling frequency (Hz) }
  267. crc_check : cushort; { frame CRC accumulator }
  268. crc_target : cushort; { final target CRC checksum }
  269. flags : cint; { flags (see below) }
  270. private_bits : cint; { private bits (see below) }
  271. duration : mad_timer_t; { audio playing time of frame }
  272. end;
  273. mad_overlap = array[0..1, 0..31, 0..17] of mad_fixed_t;
  274. mad_frame = record
  275. header : mad_header; { MPEG audio header }
  276. options : cint; { decoding options (from stream) }
  277. { synthesis subband filter samples }
  278. sbsample : packed array[0..1, 0..35, 0..31] of mad_fixed_t;
  279. overlap : ^mad_overlap; { Layer III block overlap data }
  280. end;
  281. const
  282. MAD_FLAG_NPRIVATE_III = $0007; { number of Layer III private bits }
  283. MAD_FLAG_INCOMPLETE = $0008; { header but not data is decoded }
  284. MAD_FLAG_PROTECTION = $0010; { frame has CRC protection }
  285. MAD_FLAG_COPYRIGHT = $0020; { frame is copyright }
  286. MAD_FLAG_ORIGINAL = $0040; { frame is original (else copy) }
  287. MAD_FLAG_PADDING = $0080; { frame has additional slot }
  288. MAD_FLAG_I_STEREO = $0100; { uses intensity joint stereo }
  289. MAD_FLAG_MS_STEREO = $0200; { uses middle/side joint stereo }
  290. MAD_FLAG_FREEFORMAT = $0400; { uses free format bitrate }
  291. MAD_FLAG_LSF_EXT = $1000; { lower sampling freq. extension }
  292. MAD_FLAG_MC_EXT = $2000; { multichannel audio extension }
  293. MAD_FLAG_MPEG_2_5_EXT = $4000; { MPEG 2.5 (unofficial) extension }
  294. MAD_PRIVATE_HEADER = $0100; { header private bit }
  295. MAD_PRIVATE_III = $001f; { Layer III private bits (up to 5) }
  296. function mad_nchannels(header: mad_header): cint;
  297. function mad_nsbsamples(header: mad_header): cint;
  298. procedure mad_header_init(var header: mad_header); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  299. procedure mad_header_finish(var header: mad_header);
  300. function mad_header_decode(var header: mad_header; var stream: mad_stream): cint; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  301. procedure mad_frame_init(var frame: mad_frame); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  302. procedure mad_frame_finish(var frame: mad_frame); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  303. function mad_frame_decode(var frame: mad_frame; var stream: mad_stream): cint; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  304. procedure mad_frame_mute(var frame: mad_frame); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  305. (***********************************************************************)
  306. (* Header : synth.h *)
  307. (***********************************************************************)
  308. type
  309. mad_pcm = record
  310. samplerate : cuint; { sampling frequency (Hz) }
  311. channels : cushort; { number of channels }
  312. length : cushort; { number of samples per channel }
  313. { PCM output samples [ch][sample] }
  314. samples : packed array [0..1, 0..1151] of mad_fixed_t;
  315. end;
  316. mad_synth = record
  317. { polyphase filterbank outputs [ch][eo][peo][s][v] }
  318. filter : array[0..1, 0..1, 0..1, 0..15, 0..7] of mad_fixed_t;
  319. phase : cuint; { current processing phase }
  320. pcm : mad_pcm; { PCM output }
  321. end;
  322. const
  323. { single channel PCM selector }
  324. MAD_PCM_CHANNEL_SINGLE = 0;
  325. { dual channel PCM selector }
  326. MAD_PCM_CHANNEL_DUAL_1 = 0;
  327. MAD_PCM_CHANNEL_DUAL_2 = 1;
  328. { stereo PCM selector }
  329. MAD_PCM_CHANNEL_STEREO_LEFT = 0;
  330. MAD_PCM_CHANNEL_STEREO_RIGHT = 1;
  331. procedure mad_synth_init(var synth: mad_synth); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  332. procedure mad_synth_finish(var synth: mad_synth);
  333. procedure mad_synth_mute(var synth: mad_synth); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  334. procedure mad_synth_frame(var synth: mad_synth; var frame: mad_frame); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  335. (***********************************************************************)
  336. (* Header : decoder.h *)
  337. (***********************************************************************)
  338. {$IFNDEF MAD_DISABLE_BUILTIN_DECODER}
  339. type
  340. mad_decoder_mode = (
  341. MAD_DECODER_MODE_SYNC = 0,
  342. MAD_DECODER_MODE_ASYNC = 1
  343. );
  344. mad_flow = (
  345. MAD_FLOW_CONTINUE = $0000, { continue normally }
  346. MAD_FLOW_STOP = $0010, { stop decoding normally }
  347. MAD_FLOW_BREAK = $0011, { stop decoding and signal an error }
  348. MAD_FLOW_IGNORE = $0020 { ignore the current frame }
  349. );
  350. async_struct = record
  351. pid : clong;
  352. _in : cint;
  353. _out : cint;
  354. end;
  355. sync_struct = record
  356. stream : mad_stream;
  357. frame : mad_frame;
  358. synth : mad_synth;
  359. end;
  360. mad_input_func = function(user: Pointer; var stream: mad_stream): mad_flow; cdecl;
  361. mad_header_func = function(user: Pointer; var header: mad_header): mad_flow; cdecl;
  362. mad_filter_func = function(user: Pointer; var frame: mad_frame): mad_flow; cdecl;
  363. mad_output_func = function(user: Pointer; var header: mad_header; var pcm: mad_pcm): mad_flow; cdecl;
  364. mad_error_func = function(user: Pointer; var stream: mad_stream; var frame: mad_frame): mad_flow; cdecl;
  365. mad_message_func = function(user, msg: Pointer; var l: cuint): mad_flow; cdecl;
  366. mad_decoder = record
  367. mode : mad_decoder_mode;
  368. options : cint;
  369. async : async_struct;
  370. sync : ^sync_struct;
  371. data : pointer;
  372. InputFunc : mad_input_func;
  373. HeaderFunc : mad_header_func;
  374. FilterFunc : mad_filter_func;
  375. OutputFunc : mad_output_func;
  376. ErrorFunc : mad_error_func;
  377. MessageFunc : mad_message_func;
  378. end;
  379. procedure mad_decoder_init(var decoder: mad_decoder; user: pointer; Input: mad_input_func; Header: mad_header_func; Filter: mad_filter_func; Output: mad_output_func; Error: mad_error_func; Message: mad_message_func); cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  380. function mad_decoder_finish(var decoder: mad_decoder): cint; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  381. function mad_decoder_run(var decoder: mad_decoder; mode: mad_decoder_mode): cint; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  382. function mad_decoder_message(var decoder: mad_decoder; msg: Pointer; var l: cuint): cint; cdecl; external {$IFDEF DYNLINK}madlib{$ENDIF};
  383. {$ENDIF}
  384. {
  385. Developer of the MAD helpers for FreePascal
  386. Copyright (C) 2006 by Ivo Steinmann
  387. }
  388. {$IFDEF MAD_DISABLE_BUILTIN_DECODER}
  389. const
  390. MAD_INPUT_BUFFER_SIZE = 5*8192;
  391. type
  392. mad_read_func = function(user: pointer; ptr: pointer; size: cuint): cuint; cdecl;
  393. mad_seek_func = function(user: pointer; offset: clong; whence: cint): clong; cdecl;
  394. mad_close_func = function(user: pointer): cint; cdecl;
  395. mad_tell_func = function(user: pointer): clong; cdecl;
  396. pmad_decoder = ^mad_decoder;
  397. mad_decoder = record
  398. inbuf : array[0..MAD_INPUT_BUFFER_SIZE-1] of cuint8;
  399. stream : mad_stream;
  400. frame : mad_frame;
  401. synth : mad_synth;
  402. samplecnt : cint;
  403. sampleofs : cint;
  404. user : pointer;
  405. read : mad_read_func;
  406. seek : mad_seek_func;
  407. close : mad_close_func;
  408. tell : mad_tell_func;
  409. // Userinfo
  410. sample_rate : cint;
  411. end;
  412. function mad_decoder_init(user: pointer; read: mad_read_func; seek: mad_seek_func; close: mad_close_func; tell: mad_tell_func): pmad_decoder;
  413. function mad_decoder_read(decoder: pmad_decoder; buffer: pointer; length: cint): cint;
  414. procedure mad_decoder_free(decoder: pmad_decoder);
  415. {$ENDIF}
  416. implementation
  417. function MAD_VERSION_STRINGIZE(num: cint): String;
  418. begin
  419. MAD_VERSION_STRINGIZE := '';
  420. Str(num, MAD_VERSION_STRINGIZE);
  421. end;
  422. function MAD_VERSION_STRING(num: cint): String;
  423. begin
  424. MAD_VERSION_STRING := MAD_VERSION_STRINGIZE(num);
  425. end;
  426. function MAD_VERSION: String;
  427. begin
  428. MAD_VERSION :=
  429. MAD_VERSION_STRING(MAD_VERSION_MAJOR) + '.' +
  430. MAD_VERSION_STRING(MAD_VERSION_MINOR) + '.' +
  431. MAD_VERSION_STRING(MAD_VERSION_PATCH) +
  432. MAD_VERSION_EXTRA;
  433. end;
  434. {function mad_f_tofixed(x: double): mad_fixed_t;
  435. begin
  436. Result := mad_fixed_t(x * double(1 shl MAD_F_FRACBITS) + 0.5);
  437. end;}
  438. procedure mad_bit_finish(var bitptr: mad_bitptr);
  439. begin
  440. end;
  441. function mad_bit_bitsleft(var bitptr: mad_bitptr): cushort;
  442. begin
  443. mad_bit_bitsleft := bitptr.left;
  444. end;
  445. procedure mad_timer_reset(var timer: mad_timer_t);
  446. begin
  447. timer := mad_timer_zero;
  448. end;
  449. function mad_timer_sign(timer: mad_timer_t): cint;
  450. begin
  451. mad_timer_sign := mad_timer_compare(timer, mad_timer_zero);
  452. end;
  453. function MAD_RECOVERABLE(error: mad_error): Boolean;
  454. begin
  455. MAD_RECOVERABLE := word(error) and $ff00 > 0;
  456. end;
  457. procedure mad_stream_options(stream: mad_stream; opts: cint);
  458. begin
  459. stream.options := opts;
  460. end;
  461. procedure mad_header_finish(var header: mad_header);
  462. begin
  463. FillChar(header, sizeof(mad_header), 0);
  464. end;
  465. function mad_nchannels(header: mad_header): cint;
  466. begin
  467. if longword(header.mode) <> 0 then
  468. mad_nchannels := 2 else
  469. mad_nchannels := 1;
  470. end;
  471. function mad_nsbsamples(header: mad_header): cint;
  472. begin
  473. if header.layer = MAD_LAYER_I then mad_nsbsamples := 12 else
  474. if (header.layer = MAD_LAYER_III) and (header.flags and MAD_FLAG_LSF_EXT > 0)
  475. then mad_nsbsamples := 18
  476. else mad_nsbsamples := 36;
  477. end;
  478. procedure mad_synth_finish(var synth: mad_synth);
  479. begin
  480. FillChar(synth, sizeof(mad_synth), 0);
  481. end;
  482. {$IFDEF MAD_DISABLE_BUILTIN_DECODER}
  483. function mad_decoder_init(user: pointer; read: mad_read_func; seek: mad_seek_func; close: mad_close_func; tell: mad_tell_func): pmad_decoder;
  484. begin
  485. GetMem(Result, Sizeof(mad_decoder));
  486. FillChar(Result^, Sizeof(mad_decoder), 0);
  487. mad_stream_init(Result^.stream);
  488. mad_frame_init(Result^.frame);
  489. mad_synth_init(Result^.synth);
  490. Result^.user := user;
  491. Result^.read := read;
  492. Result^.seek := seek;
  493. Result^.close := close;
  494. Result^.tell := tell;
  495. end;
  496. procedure mad_decoder_free(decoder: pmad_decoder);
  497. begin
  498. if not Assigned(decoder) then
  499. Exit;
  500. mad_synth_finish(decoder^.synth);
  501. mad_frame_finish(decoder^.frame);
  502. mad_stream_finish(decoder^.stream);
  503. decoder^.close(decoder^.user);
  504. FreeMem(decoder);
  505. end;
  506. function mad_decoder_read(decoder: pmad_decoder; buffer: pointer; length: cint): cint;
  507. var
  508. ofs, num, i: cint;
  509. inbuf_ptr: pointer;
  510. len, remaining: cint;
  511. begin
  512. // check blocksize here!
  513. ofs := 0;
  514. num := length;
  515. while num > 0 do
  516. begin
  517. if decoder^.samplecnt = 0 then
  518. begin
  519. if (decoder^.stream.buffer = nil) or (decoder^.stream.error = MAD_ERROR_BUFLEN) then
  520. begin
  521. if Assigned(decoder^.stream.next_frame) then
  522. begin
  523. remaining := ptrint(decoder^.stream.bufend) - ptrint(decoder^.stream.next_frame);
  524. inbuf_ptr := pointer(ptrint(@decoder^.inbuf) + remaining);
  525. len := MAD_INPUT_BUFFER_SIZE - remaining;
  526. Move(decoder^.stream.next_frame^, decoder^.inbuf, remaining);
  527. end else begin
  528. remaining := 0;
  529. len := MAD_INPUT_BUFFER_SIZE;
  530. inbuf_ptr := @decoder^.inbuf;
  531. end;
  532. len := decoder^.read(decoder^.user, inbuf_ptr, len);
  533. if len <= 0 then
  534. Exit(ofs);
  535. mad_stream_buffer(decoder^.stream, decoder^.inbuf, len+remaining);
  536. decoder^.stream.error := MAD_ERROR_NONE;
  537. end;
  538. if mad_frame_decode(decoder^.frame, decoder^.stream) <> 0 then
  539. begin
  540. if MAD_RECOVERABLE(decoder^.stream.error) or (decoder^.stream.error = MAD_ERROR_BUFLEN) then
  541. Continue;
  542. Exit(ofs);
  543. end;
  544. mad_synth_frame(decoder^.synth, decoder^.frame);
  545. with decoder^.synth do
  546. if pcm.channels = 2 then
  547. begin
  548. for i := 0 to pcm.length -1 do
  549. begin
  550. if pcm.samples[0][i] >= MAD_F_ONE then
  551. pcm.samples[0][i] := MAD_F_ONE - 1;
  552. if pcm.samples[0][i] < -MAD_F_ONE then
  553. pcm.samples[0][i] := -MAD_F_ONE;
  554. pcm.samples[0][i] := pcm.samples[0][i] shr (MAD_F_FRACBITS + 1 - 16 + 1);
  555. if pcm.samples[1][i] >= MAD_F_ONE then
  556. pcm.samples[1][i] := MAD_F_ONE - 1;
  557. if pcm.samples[1][i] < -MAD_F_ONE then
  558. pcm.samples[1][i] := -MAD_F_ONE;
  559. pcm.samples[1][i] := pcm.samples[1][i] shr (MAD_F_FRACBITS + 1 - 16 + 1);
  560. end;
  561. end else begin
  562. for i := 0 to pcm.length -1 do
  563. begin
  564. if pcm.samples[0][i] >= MAD_F_ONE then
  565. pcm.samples[0][i] := MAD_F_ONE - 1;
  566. if pcm.samples[0][i] < -MAD_F_ONE then
  567. pcm.samples[0][i] := -MAD_F_ONE;
  568. pcm.samples[0][i] := pcm.samples[0][i] shr (MAD_F_FRACBITS + 1 - 16 + 1);
  569. pcm.samples[1][i] := pcm.samples[0][i];
  570. end;
  571. end;
  572. decoder^.sampleofs := 0;
  573. decoder^.samplecnt := decoder^.synth.pcm.length;
  574. decoder^.sample_rate := decoder^.synth.pcm.samplerate;
  575. end;
  576. len := num div 4;
  577. if len > decoder^.samplecnt then
  578. len := decoder^.samplecnt;
  579. for i := 0 to len - 1 do
  580. begin
  581. pcint16(ptrint(buffer) + ofs + 0)^ := decoder^.synth.pcm.samples[0][decoder^.sampleofs];
  582. pcint16(ptrint(buffer) + ofs + 2)^ := decoder^.synth.pcm.samples[1][decoder^.sampleofs];
  583. Inc(decoder^.sampleofs);
  584. Dec(decoder^.samplecnt);
  585. ofs := ofs + 4;
  586. num := num - 4;
  587. end;
  588. end;
  589. Result := ofs;
  590. end;
  591. {$ENDIF}
  592. end.