fmt123.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. libmpg123: MPEG Audio Decoder library
  3. separate header just for audio format definitions not tied to
  4. library code
  5. copyright 1995-2020 by the mpg123 project
  6. free software under the terms of the LGPL 2.1
  7. see COPYING and AUTHORS files in distribution or http://mpg123.org
  8. */
  9. #ifndef MPG123_ENC_H
  10. #define MPG123_ENC_H
  11. /** \file fmt123.h Audio format definitions. */
  12. /** \defgroup mpg123_enc mpg123 PCM sample encodings
  13. * These are definitions for audio formats used by libmpg123 and
  14. * libout123.
  15. *
  16. * @{
  17. */
  18. /** An enum over all sample types possibly known to mpg123.
  19. * The values are designed as bit flags to allow bitmasking for encoding
  20. * families.
  21. * This is also why the enum is not used as type for actual encoding variables,
  22. * plain integers (at least 16 bit, 15 bit being used) cover the possible
  23. * combinations of these flags.
  24. *
  25. * Note that (your build of) libmpg123 does not necessarily support all these.
  26. * Usually, you can expect the 8bit encodings and signed 16 bit.
  27. * Also 32bit float will be usual beginning with mpg123-1.7.0 .
  28. * What you should bear in mind is that (SSE, etc) optimized routines may be
  29. * absent for some formats. We do have SSE for 16, 32 bit and float, though.
  30. * 24 bit integer is done via postprocessing of 32 bit output -- just cutting
  31. * the last byte, no rounding, even. If you want better, do it yourself.
  32. *
  33. * All formats are in native byte order. If you need different endinaness, you
  34. * can simply postprocess the output buffers (libmpg123 wouldn't do anything
  35. * else). The macro MPG123_SAMPLESIZE() can be helpful there.
  36. */
  37. enum mpg123_enc_enum
  38. {
  39. /* 0000 0000 0000 1111 Some 8 bit integer encoding. */
  40. MPG123_ENC_8 = 0x00f
  41. /* 0000 0000 0100 0000 Some 16 bit integer encoding. */
  42. , MPG123_ENC_16 = 0x040
  43. /* 0100 0000 0000 0000 Some 24 bit integer encoding. */
  44. , MPG123_ENC_24 = 0x4000
  45. /* 0000 0001 0000 0000 Some 32 bit integer encoding. */
  46. , MPG123_ENC_32 = 0x100
  47. /* 0000 0000 1000 0000 Some signed integer encoding. */
  48. , MPG123_ENC_SIGNED = 0x080
  49. /* 0000 1110 0000 0000 Some float encoding. */
  50. , MPG123_ENC_FLOAT = 0xe00
  51. /* 0000 0000 1101 0000 signed 16 bit */
  52. , MPG123_ENC_SIGNED_16 = (MPG123_ENC_16|MPG123_ENC_SIGNED|0x10)
  53. /* 0000 0000 0110 0000 unsigned 16 bit */
  54. , MPG123_ENC_UNSIGNED_16 = (MPG123_ENC_16|0x20)
  55. /* 0000 0000 0000 0001 unsigned 8 bit */
  56. , MPG123_ENC_UNSIGNED_8 = 0x01
  57. /* 0000 0000 1000 0010 signed 8 bit */
  58. , MPG123_ENC_SIGNED_8 = (MPG123_ENC_SIGNED|0x02)
  59. /* 0000 0000 0000 0100 ulaw 8 bit */
  60. , MPG123_ENC_ULAW_8 = 0x04
  61. /* 0000 0000 0000 1000 alaw 8 bit */
  62. , MPG123_ENC_ALAW_8 = 0x08
  63. /* 0001 0001 1000 0000 signed 32 bit */
  64. , MPG123_ENC_SIGNED_32 = MPG123_ENC_32|MPG123_ENC_SIGNED|0x1000
  65. /* 0010 0001 0000 0000 unsigned 32 bit */
  66. , MPG123_ENC_UNSIGNED_32 = MPG123_ENC_32|0x2000
  67. /* 0101 0000 1000 0000 signed 24 bit */
  68. , MPG123_ENC_SIGNED_24 = MPG123_ENC_24|MPG123_ENC_SIGNED|0x1000
  69. /* 0110 0000 0000 0000 unsigned 24 bit */
  70. , MPG123_ENC_UNSIGNED_24 = MPG123_ENC_24|0x2000
  71. /* 0000 0010 0000 0000 32bit float */
  72. , MPG123_ENC_FLOAT_32 = 0x200
  73. /* 0000 0100 0000 0000 64bit float */
  74. , MPG123_ENC_FLOAT_64 = 0x400
  75. /* Any possibly known encoding from the list above. */
  76. , MPG123_ENC_ANY = ( MPG123_ENC_SIGNED_16 | MPG123_ENC_UNSIGNED_16
  77. | MPG123_ENC_UNSIGNED_8 | MPG123_ENC_SIGNED_8
  78. | MPG123_ENC_ULAW_8 | MPG123_ENC_ALAW_8
  79. | MPG123_ENC_SIGNED_32 | MPG123_ENC_UNSIGNED_32
  80. | MPG123_ENC_SIGNED_24 | MPG123_ENC_UNSIGNED_24
  81. | MPG123_ENC_FLOAT_32 | MPG123_ENC_FLOAT_64 )
  82. };
  83. /** Get size of one PCM sample with given encoding.
  84. * This is included both in libmpg123 and libout123. Both offer
  85. * an API function to provide the macro results from library
  86. * compile-time, not that of you application. This most likely
  87. * does not matter as I do not expect any fresh PCM sample
  88. * encoding to appear. But who knows? Perhaps the encoding type
  89. * will be abused for funny things in future, not even plain PCM.
  90. * And, by the way: Thomas really likes the ?: operator.
  91. * \param enc the encoding (mpg123_enc_enum value)
  92. * \return size of one sample in bytes
  93. */
  94. #define MPG123_SAMPLESIZE(enc) ( \
  95. (enc) < 1 \
  96. ? 0 \
  97. : ( (enc) & MPG123_ENC_8 \
  98. ? 1 \
  99. : ( (enc) & MPG123_ENC_16 \
  100. ? 2 \
  101. : ( (enc) & MPG123_ENC_24 \
  102. ? 3 \
  103. : ( ( (enc) & MPG123_ENC_32 \
  104. || (enc) == MPG123_ENC_FLOAT_32 ) \
  105. ? 4 \
  106. : ( (enc) == MPG123_ENC_FLOAT_64 \
  107. ? 8 \
  108. : 0 \
  109. ) ) ) ) ) )
  110. /** Representation of zero in differing encodings.
  111. * This exists to define proper silence in various encodings without
  112. * having to link to libsyn123 to do actual conversions at runtime.
  113. * You have to handle big/little endian order yourself, though.
  114. * This takes the shortcut that any signed encoding has a zero with
  115. * all-zero bits. Unsigned linear encodings just have the highest bit set
  116. * (2^(n-1) for n bits), while the nonlinear 8-bit ones are special.
  117. * \param enc the encoding (mpg123_enc_enum value)
  118. * \param siz bytes per sample (return value of MPG123_SAMPLESIZE(enc))
  119. * \param off byte (octet) offset counted from LSB
  120. * \return unsigned byte value for the designated octet
  121. */
  122. #define MPG123_ZEROSAMPLE(enc, siz, off) ( \
  123. (enc) == MPG123_ENC_ULAW_8 \
  124. ? (off == 0 ? 0xff : 0x00) \
  125. : ( (enc) == MPG123_ENC_ALAW_8 \
  126. ? (off == 0 ? 0xd5 : 0x00) \
  127. : ( (((enc) & (MPG123_ENC_SIGNED|MPG123_ENC_FLOAT)) || (siz) != ((off)+1)) \
  128. ? 0x00 \
  129. : 0x80 \
  130. ) ) )
  131. /** Structure defining an audio format.
  132. * Providing the members as individual function arguments to define a certain
  133. * output format is easy enough. This struct makes is more comfortable to deal
  134. * with a list of formats.
  135. * Negative values for the members might be used to communicate use of default
  136. * values.
  137. */
  138. struct mpg123_fmt
  139. {
  140. long rate; /**< sampling rate in Hz */
  141. int channels; /**< channel count */
  142. /** encoding code, can be single value or bitwise or of members of
  143. * mpg123_enc_enum */
  144. int encoding;
  145. };
  146. /* @} */
  147. #endif