pcm.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*{{{ #defines */
  10. #include <limits.h>
  11. #include "lame.h"
  12. #include "util.h"
  13. #define ENCDELAY 576
  14. #define MDCTDELAY 48
  15. #define BLKSIZE 1024
  16. #define HBLKSIZE (BLKSIZE/2 + 1)
  17. #define BLKSIZE_s 256
  18. #define HBLKSIZE_s (BLKSIZE_s/2 + 1)
  19. #define MAX_TABLES 1002
  20. #define TAPS 32
  21. #define WINDOW_SIZE 15.5
  22. #define WINDOW hanning
  23. #define inline __inline
  24. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  25. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  26. #ifndef M_PIl
  27. # define M_PIl 3.1415926535897932384626433832795029L
  28. #endif
  29. #define SIN sin
  30. #define COS cos
  31. /*}}}*/
  32. /*{{{ object ID's */
  33. #define RESAMPLE_ID 0x52455341LU
  34. #define PSYCHO_ID 0x50535943LU
  35. #define BITSTREAM_ID 0x42495453LU
  36. /*}}}*/
  37. /*{{{ typedef's */
  38. typedef float float32_t; // IEEE-754 32 bit floating point
  39. typedef double float64_t; // IEEE-754 64 bit floating point
  40. typedef long double float80_t; // IEEE-854 80 bit floating point, if available
  41. typedef long double float_t; // temporarly results of float operations
  42. typedef long double double_t; // temporarly results of double operations
  43. typedef long double longdouble_t; // temporarly results of long double operations
  44. typedef float_t (*scalar_t) ( const sample_t* p, const sample_t* q );
  45. typedef float_t (*scalarn_t) ( const sample_t* p, const sample_t* q, size_t len );
  46. /*}}}*/
  47. /*{{{ data direction attributes */
  48. /*
  49. * These are data stream direction attributes like used in Ada83/Ada95 and in RPC
  50. * The data direction is seen from the caller to the calling function.
  51. * Examples:
  52. *
  53. * size_t fread ( void INOUT* buffer, size_t items, size_t itemsize, FILE INOUT* fp );
  54. * size_t fwrite ( void OUT * buffer, size_t items, size_t itemsize, FILE INOUT* fp );
  55. * size_t memset ( void IN * buffer, unsigned char value, size_t size );
  56. *
  57. * Return values are implizit IN (note that here C uses the opposite attribute).
  58. * Arguments not transmitted via references are implizit OUT.
  59. */
  60. #define OUT /* [out] */ const
  61. #define INOUT /* [inout] */
  62. #define IN /* [in] */
  63. #define OUTTR /* [out]: data is modified like [inout], but you don't get any useful back */
  64. /*}}}*/
  65. /*{{{ Test some error conditions */
  66. #ifndef __LOC__
  67. # define _STR2(x) #x
  68. # define _STR1(x) _STR2(x)
  69. # define __LOC__ __FILE__ "(" _STR1(__LINE__) ") : warning: "
  70. #endif
  71. /* The current code doesn't work on machines with non 8 bit char's in any way, so abort */
  72. #if CHAR_BIT != 8
  73. # pragma message ( __LOC__ "Machines with CHAR_BIT != 8 not yet supported" )
  74. # pragma error
  75. #endif
  76. /*}}}*/
  77. /*
  78. * Now some information how PCM data can be specified. PCM data
  79. * is specified by 3 attributes: pointer, length information
  80. * and attributes.
  81. * - Audio is always stored in 2D arrays, which are collapsing to 1D
  82. * in the case of monaural input
  83. * - 2D arrays can be stored as 2D arrays or as pointers to 1D arrays.
  84. * - 2D data can be stored as samples*channels or as channels*samples
  85. * - This gives 4 kinds of storing PCM data:
  86. * + pcm [samples][channels] (LAME_INTERLEAVED)
  87. * + pcm [channels][samples] (LAME_CHAINED)
  88. * + (*pcm) [samples] (LAME_INDIRECT)
  89. * + (*pcm) [channels]
  90. * - The last I have never seen and it have a huge overhead (67% ... 200%),
  91. * so the first three are implemented.
  92. * - The monaural 1D cases can also be handled by the first two attributes
  93. */
  94. #define LAME_INTERLEAVED 0x10000000
  95. #define LAME_CHAINED 0x20000000
  96. #define LAME_INDIRECT 0x30000000
  97. /*
  98. * Now we need some information about the byte order of the data.
  99. * There are 4 cases possible (if you are not fully support such strange
  100. * Machines like the PDPs):
  101. * - You know the absolute byte order of the data (LAME_LITTLE_ENDIAN, LAME_BIG_ENDIAN)
  102. * - You know the byte order from the view of the current machine
  103. * (LAME_NATIVE_ENDIAN, LAME_OPPOSITE_ENDIAN)
  104. * - The use of LAME_OPPOSITE_ENDIAN is NOT recommended because it is
  105. * is a breakable attribute, use LAME_LITTLE_ENDIAN or LAME_BIG_ENDIAN
  106. * instead
  107. */
  108. #define LAME_NATIVE_ENDIAN 0x00000000
  109. #define LAME_OPPOSITE_ENDIAN 0x01000000
  110. #define LAME_LITTLE_ENDIAN 0x02000000
  111. #define LAME_BIG_ENDIAN 0x03000000
  112. /*
  113. * The next attribute is the data type of the input data.
  114. * There are currently 2 kinds of input data:
  115. * - C based:
  116. * LAME_{SHORT,INT,LONG}
  117. * LAME_{FLOAT,DOUBLE,LONGDOUBLE}
  118. * - Binary representation based:
  119. * LAME_{UINT,INT}{8,16,24,32}
  120. * LAME_{A,U}LAW
  121. * LAME_FLOAT{32,64,80_ALIGN{2,4,8}}
  122. *
  123. * Don't use the C based for external data.
  124. */
  125. #define LAME_SILENCE 0x00010000
  126. #define LAME_UINT8 0x00020000
  127. #define LAME_INT8 0x00030000
  128. #define LAME_UINT16 0x00040000
  129. #define LAME_INT16 0x00050000
  130. #define LAME_UINT24 0x00060000
  131. #define LAME_INT24 0x00070000
  132. #define LAME_UINT32 0x00080000
  133. #define LAME_INT32 0x00090000
  134. #define LAME_FLOAT32 0x00140000
  135. #define LAME_FLOAT64 0x00180000
  136. #define LAME_FLOAT80_ALIGN2 0x001A0000
  137. #define LAME_FLOAT80_ALIGN4 0x001C0000
  138. #define LAME_FLOAT80_ALIGN8 0x00200000
  139. #define LAME_SHORT 0x00210000
  140. #define LAME_INT 0x00220000
  141. #define LAME_LONG 0x00230000
  142. #define LAME_FLOAT 0x00240000
  143. #define LAME_DOUBLE 0x00250000
  144. #define LAME_LONGDOUBLE 0x00260000
  145. #define LAME_ALAW 0x00310000
  146. #define LAME_ULAW 0x00320000
  147. /*
  148. * The last attribute is the number of input channels. Currently
  149. * 1...65535 channels are possible, but only 1 and 2 are supported.
  150. * So matrixing or MPEG-2 MultiChannelSupport are not a big problem.
  151. *
  152. * Note: Some people use the word 'stereo' for 2 channel stereophonic sound.
  153. * But stereophonic sound is a collection of ALL methods to restore the
  154. * stereophonic sound field starting from 2 channels up to audio
  155. * holography.
  156. */
  157. #define LAME_MONO 0x00000001
  158. #define LAME_STEREO 0x00000002
  159. #define LAME_STEREO_2_CHANNELS 0x00000002
  160. #define LAME_STEREO_3_CHANNELS 0x00000003
  161. #define LAME_STEREO_4_CHANNELS 0x00000004
  162. #define LAME_STEREO_5_CHANNELS 0x00000005
  163. #define LAME_STEREO_6_CHANNELS 0x00000006
  164. #define LAME_STEREO_7_CHANNELS 0x00000007
  165. #define LAME_STEREO_65535_CHANNELS\
  166. 0x0000FFFF
  167. extern scalar_t scalar4;
  168. extern scalar_t scalar8;
  169. extern scalar_t scalar12;
  170. extern scalar_t scalar16;
  171. extern scalar_t scalar20;
  172. extern scalar_t scalar24;
  173. extern scalar_t scalar32;
  174. extern scalarn_t scalar4n;
  175. extern scalarn_t scalar1n;
  176. float_t scalar04_float32_i387 ( const float32_t* p, const float32_t* q );
  177. float_t scalar08_float32_i387 ( const float32_t* p, const float32_t* q );
  178. float_t scalar12_float32_i387 ( const float32_t* p, const float32_t* q );
  179. float_t scalar16_float32_i387 ( const float32_t* p, const float32_t* q );
  180. float_t scalar20_float32_i387 ( const float32_t* p, const float32_t* q );
  181. float_t scalar24_float32_i387 ( const float32_t* p, const float32_t* q );
  182. float_t scalar32_float32_i387 ( const float32_t* p, const float32_t* q );
  183. float_t scalar4n_float32_i387 ( const float32_t* p, const float32_t* q, const size_t len );
  184. float_t scalar1n_float32_i387 ( const float32_t* p, const float32_t* q, const size_t len );
  185. float_t scalar04_float32_3DNow ( const float32_t* p, const float32_t* q );
  186. float_t scalar08_float32_3DNow ( const float32_t* p, const float32_t* q );
  187. float_t scalar12_float32_3DNow ( const float32_t* p, const float32_t* q );
  188. float_t scalar16_float32_3DNow ( const float32_t* p, const float32_t* q );
  189. float_t scalar20_float32_3DNow ( const float32_t* p, const float32_t* q );
  190. float_t scalar24_float32_3DNow ( const float32_t* p, const float32_t* q );
  191. float_t scalar32_float32_3DNow ( const float32_t* p, const float32_t* q );
  192. float_t scalar4n_float32_3DNow ( const float32_t* p, const float32_t* q, const size_t len );
  193. float_t scalar1n_float32_3DNow ( const float32_t* p, const float32_t* q, const size_t len );
  194. float_t scalar04_float32_SIMD ( const float32_t* p, const float32_t* q );
  195. float_t scalar08_float32_SIMD ( const float32_t* p, const float32_t* q );
  196. float_t scalar12_float32_SIMD ( const float32_t* p, const float32_t* q );
  197. float_t scalar16_float32_SIMD ( const float32_t* p, const float32_t* q );
  198. float_t scalar20_float32_SIMD ( const float32_t* p, const float32_t* q );
  199. float_t scalar24_float32_SIMD ( const float32_t* p, const float32_t* q );
  200. float_t scalar32_float32_SIMD ( const float32_t* p, const float32_t* q );
  201. float_t scalar4n_float32_SIMD ( const float32_t* p, const float32_t* q, const size_t len );
  202. float_t scalar1n_float32_SIMD ( const float32_t* p, const float32_t* q, const size_t len );
  203. float_t scalar04_float32 ( const float32_t* p, const float32_t* q );
  204. float_t scalar08_float32 ( const float32_t* p, const float32_t* q );
  205. float_t scalar12_float32 ( const float32_t* p, const float32_t* q );
  206. float_t scalar16_float32 ( const float32_t* p, const float32_t* q );
  207. float_t scalar20_float32 ( const float32_t* p, const float32_t* q );
  208. float_t scalar24_float32 ( const float32_t* p, const float32_t* q );
  209. float_t scalar32_float32 ( const float32_t* p, const float32_t* q );
  210. float_t scalar4n_float32 ( const float32_t* p, const float32_t* q, const size_t len );
  211. float_t scalar1n_float32 ( const float32_t* p, const float32_t* q, const size_t len );
  212. /*{{{ some prototypes */
  213. resample_t* resample_open (
  214. OUT long double sampfreq_in, // [Hz]
  215. OUT long double sampfreq_out, // [Hz]
  216. OUT double lowpass_freq, // [Hz] or <0 for auto mode
  217. OUT int quality ); // Proposal: 0 default, 1 sample select, 2 linear interpol, 4 4-point interpolation, 32 32-point interpolation
  218. int resample_buffer ( // return code, 0 for success
  219. INOUT resample_t *const r, // internal structure
  220. IN sample_t *const out, // where to write the output data
  221. INOUT size_t *const out_req_len, // requested output data len/really written output data len
  222. OUT sample_t *const in, // where are the input data?
  223. INOUT size_t *const in_avail_len, // available input data len/consumed input data len
  224. OUT size_t channel ); // number of the channel (needed for buffering)
  225. int resample_close ( INOUT resample_t* const r );
  226. void init_scalar_functions ( OUT lame_t* const lame );
  227. long double unround_samplefrequency ( OUT long double freq );
  228. #if 0
  229. int lame_encode_mp3_frame ( // return code, 0 for success
  230. INOUT lame_global_flags*, // internal context structure
  231. OUTTR sample_t * inbuf_l, // data for left channel
  232. OUTTR sample_t * inbuf_r, // data for right channel
  233. IN uint8_t * mp3buf, // where to write the coded data
  234. OUT size_t mp3buf_size ); // maximum size of coded data
  235. #endif
  236. int lame_encode_ogg_frame ( // return code, 0 for success
  237. INOUT lame_global_flags*, // internal context structure
  238. OUT sample_t * inbuf_l, // data for left channel
  239. OUT sample_t * inbuf_r, // data for right channel
  240. IN uint8_t * mp3buf, // where to write the coded data
  241. OUT size_t mp3buf_size ); // maximum size of coded data
  242. /*}}}*/
  243. /* end of pcm.h */