audio-format.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* GStreamer
  2. * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
  3. * Library <2001> Thomas Vander Stichele <thomas@apestaart.org>
  4. * <2011> Wim Taymans <wim.taymans@gmail.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  19. * Boston, MA 02110-1301, USA.
  20. */
  21. #ifndef __GST_AUDIO_AUDIO_H__
  22. #include <gst/audio/audio.h>
  23. #endif
  24. #ifndef __GST_AUDIO_FORMAT_H__
  25. #define __GST_AUDIO_FORMAT_H__
  26. G_BEGIN_DECLS
  27. #if G_BYTE_ORDER == G_BIG_ENDIAN
  28. #define _GST_AUDIO_FORMAT_NE(fmt) GST_AUDIO_FORMAT_ ## fmt ## BE
  29. #define _GST_AUDIO_FORMAT_OE(fmt) GST_AUDIO_FORMAT_ ## fmt ## LE
  30. #elif G_BYTE_ORDER == G_LITTLE_ENDIAN
  31. #define _GST_AUDIO_FORMAT_NE(fmt) GST_AUDIO_FORMAT_ ## fmt ## LE
  32. #define _GST_AUDIO_FORMAT_OE(fmt) GST_AUDIO_FORMAT_ ## fmt ## BE
  33. #endif
  34. /**
  35. * GstAudioFormat:
  36. * @GST_AUDIO_FORMAT_UNKNOWN: unknown or unset audio format
  37. * @GST_AUDIO_FORMAT_ENCODED: encoded audio format
  38. * @GST_AUDIO_FORMAT_S8: 8 bits in 8 bits, signed
  39. * @GST_AUDIO_FORMAT_U8: 8 bits in 8 bits, unsigned
  40. * @GST_AUDIO_FORMAT_S16LE: 16 bits in 16 bits, signed, little endian
  41. * @GST_AUDIO_FORMAT_S16BE: 16 bits in 16 bits, signed, big endian
  42. * @GST_AUDIO_FORMAT_U16LE: 16 bits in 16 bits, unsigned, little endian
  43. * @GST_AUDIO_FORMAT_U16BE: 16 bits in 16 bits, unsigned, big endian
  44. * @GST_AUDIO_FORMAT_S24_32LE: 24 bits in 32 bits, signed, little endian
  45. * @GST_AUDIO_FORMAT_S24_32BE: 24 bits in 32 bits, signed, big endian
  46. * @GST_AUDIO_FORMAT_U24_32LE: 24 bits in 32 bits, unsigned, little endian
  47. * @GST_AUDIO_FORMAT_U24_32BE: 24 bits in 32 bits, unsigned, big endian
  48. * @GST_AUDIO_FORMAT_S32LE: 32 bits in 32 bits, signed, little endian
  49. * @GST_AUDIO_FORMAT_S32BE: 32 bits in 32 bits, signed, big endian
  50. * @GST_AUDIO_FORMAT_U32LE: 32 bits in 32 bits, unsigned, little endian
  51. * @GST_AUDIO_FORMAT_U32BE: 32 bits in 32 bits, unsigned, big endian
  52. * @GST_AUDIO_FORMAT_S24LE: 24 bits in 24 bits, signed, little endian
  53. * @GST_AUDIO_FORMAT_S24BE: 24 bits in 24 bits, signed, big endian
  54. * @GST_AUDIO_FORMAT_U24LE: 24 bits in 24 bits, unsigned, little endian
  55. * @GST_AUDIO_FORMAT_U24BE: 24 bits in 24 bits, unsigned, big endian
  56. * @GST_AUDIO_FORMAT_S20LE: 20 bits in 24 bits, signed, little endian
  57. * @GST_AUDIO_FORMAT_S20BE: 20 bits in 24 bits, signed, big endian
  58. * @GST_AUDIO_FORMAT_U20LE: 20 bits in 24 bits, unsigned, little endian
  59. * @GST_AUDIO_FORMAT_U20BE: 20 bits in 24 bits, unsigned, big endian
  60. * @GST_AUDIO_FORMAT_S18LE: 18 bits in 24 bits, signed, little endian
  61. * @GST_AUDIO_FORMAT_S18BE: 18 bits in 24 bits, signed, big endian
  62. * @GST_AUDIO_FORMAT_U18LE: 18 bits in 24 bits, unsigned, little endian
  63. * @GST_AUDIO_FORMAT_U18BE: 18 bits in 24 bits, unsigned, big endian
  64. * @GST_AUDIO_FORMAT_F32LE: 32-bit floating point samples, little endian
  65. * @GST_AUDIO_FORMAT_F32BE: 32-bit floating point samples, big endian
  66. * @GST_AUDIO_FORMAT_F64LE: 64-bit floating point samples, little endian
  67. * @GST_AUDIO_FORMAT_F64BE: 64-bit floating point samples, big endian
  68. * @GST_AUDIO_FORMAT_S16: 16 bits in 16 bits, signed, native endianness
  69. * @GST_AUDIO_FORMAT_U16: 16 bits in 16 bits, unsigned, native endianness
  70. * @GST_AUDIO_FORMAT_S24_32: 24 bits in 32 bits, signed, native endianness
  71. * @GST_AUDIO_FORMAT_U24_32: 24 bits in 32 bits, unsigned, native endianness
  72. * @GST_AUDIO_FORMAT_S32: 32 bits in 32 bits, signed, native endianness
  73. * @GST_AUDIO_FORMAT_U32: 32 bits in 32 bits, unsigned, native endianness
  74. * @GST_AUDIO_FORMAT_S24: 24 bits in 24 bits, signed, native endianness
  75. * @GST_AUDIO_FORMAT_U24: 24 bits in 24 bits, unsigned, native endianness
  76. * @GST_AUDIO_FORMAT_S20: 20 bits in 24 bits, signed, native endianness
  77. * @GST_AUDIO_FORMAT_U20: 20 bits in 24 bits, unsigned, native endianness
  78. * @GST_AUDIO_FORMAT_S18: 18 bits in 24 bits, signed, native endianness
  79. * @GST_AUDIO_FORMAT_U18: 18 bits in 24 bits, unsigned, native endianness
  80. * @GST_AUDIO_FORMAT_F32: 32-bit floating point samples, native endianness
  81. * @GST_AUDIO_FORMAT_F64: 64-bit floating point samples, native endianness
  82. *
  83. * Enum value describing the most common audio formats.
  84. */
  85. typedef enum {
  86. GST_AUDIO_FORMAT_UNKNOWN,
  87. GST_AUDIO_FORMAT_ENCODED,
  88. /* 8 bit */
  89. GST_AUDIO_FORMAT_S8,
  90. GST_AUDIO_FORMAT_U8,
  91. /* 16 bit */
  92. GST_AUDIO_FORMAT_S16LE,
  93. GST_AUDIO_FORMAT_S16BE,
  94. GST_AUDIO_FORMAT_U16LE,
  95. GST_AUDIO_FORMAT_U16BE,
  96. /* 24 bit in low 3 bytes of 32 bits*/
  97. GST_AUDIO_FORMAT_S24_32LE,
  98. GST_AUDIO_FORMAT_S24_32BE,
  99. GST_AUDIO_FORMAT_U24_32LE,
  100. GST_AUDIO_FORMAT_U24_32BE,
  101. /* 32 bit */
  102. GST_AUDIO_FORMAT_S32LE,
  103. GST_AUDIO_FORMAT_S32BE,
  104. GST_AUDIO_FORMAT_U32LE,
  105. GST_AUDIO_FORMAT_U32BE,
  106. /* 24 bit in 3 bytes*/
  107. GST_AUDIO_FORMAT_S24LE,
  108. GST_AUDIO_FORMAT_S24BE,
  109. GST_AUDIO_FORMAT_U24LE,
  110. GST_AUDIO_FORMAT_U24BE,
  111. /* 20 bit in 3 bytes*/
  112. GST_AUDIO_FORMAT_S20LE,
  113. GST_AUDIO_FORMAT_S20BE,
  114. GST_AUDIO_FORMAT_U20LE,
  115. GST_AUDIO_FORMAT_U20BE,
  116. /* 18 bit in 3 bytes*/
  117. GST_AUDIO_FORMAT_S18LE,
  118. GST_AUDIO_FORMAT_S18BE,
  119. GST_AUDIO_FORMAT_U18LE,
  120. GST_AUDIO_FORMAT_U18BE,
  121. /* float */
  122. GST_AUDIO_FORMAT_F32LE,
  123. GST_AUDIO_FORMAT_F32BE,
  124. GST_AUDIO_FORMAT_F64LE,
  125. GST_AUDIO_FORMAT_F64BE,
  126. /* native endianness equivalents */
  127. GST_AUDIO_FORMAT_S16 = _GST_AUDIO_FORMAT_NE(S16),
  128. GST_AUDIO_FORMAT_U16 = _GST_AUDIO_FORMAT_NE(U16),
  129. GST_AUDIO_FORMAT_S24_32 = _GST_AUDIO_FORMAT_NE(S24_32),
  130. GST_AUDIO_FORMAT_U24_32 = _GST_AUDIO_FORMAT_NE(U24_32),
  131. GST_AUDIO_FORMAT_S32 = _GST_AUDIO_FORMAT_NE(S32),
  132. GST_AUDIO_FORMAT_U32 = _GST_AUDIO_FORMAT_NE(U32),
  133. GST_AUDIO_FORMAT_S24 = _GST_AUDIO_FORMAT_NE(S24),
  134. GST_AUDIO_FORMAT_U24 = _GST_AUDIO_FORMAT_NE(U24),
  135. GST_AUDIO_FORMAT_S20 = _GST_AUDIO_FORMAT_NE(S20),
  136. GST_AUDIO_FORMAT_U20 = _GST_AUDIO_FORMAT_NE(U20),
  137. GST_AUDIO_FORMAT_S18 = _GST_AUDIO_FORMAT_NE(S18),
  138. GST_AUDIO_FORMAT_U18 = _GST_AUDIO_FORMAT_NE(U18),
  139. GST_AUDIO_FORMAT_F32 = _GST_AUDIO_FORMAT_NE(F32),
  140. GST_AUDIO_FORMAT_F64 = _GST_AUDIO_FORMAT_NE(F64)
  141. } GstAudioFormat;
  142. typedef struct _GstAudioFormatInfo GstAudioFormatInfo;
  143. /**
  144. * GstAudioFormatFlags:
  145. * @GST_AUDIO_FORMAT_FLAG_INTEGER: integer samples
  146. * @GST_AUDIO_FORMAT_FLAG_FLOAT: float samples
  147. * @GST_AUDIO_FORMAT_FLAG_SIGNED: signed samples
  148. * @GST_AUDIO_FORMAT_FLAG_COMPLEX: complex layout
  149. * @GST_AUDIO_FORMAT_FLAG_UNPACK: the format can be used in
  150. * #GstAudioFormatUnpack and #GstAudioFormatPack functions
  151. *
  152. * The different audio flags that a format info can have.
  153. */
  154. typedef enum
  155. {
  156. GST_AUDIO_FORMAT_FLAG_INTEGER = (1 << 0),
  157. GST_AUDIO_FORMAT_FLAG_FLOAT = (1 << 1),
  158. GST_AUDIO_FORMAT_FLAG_SIGNED = (1 << 2),
  159. GST_AUDIO_FORMAT_FLAG_COMPLEX = (1 << 4),
  160. GST_AUDIO_FORMAT_FLAG_UNPACK = (1 << 5)
  161. } GstAudioFormatFlags;
  162. /**
  163. * GstAudioPackFlags:
  164. * @GST_AUDIO_PACK_FLAG_NONE: No flag
  165. * @GST_AUDIO_PACK_FLAG_TRUNCATE_RANGE: When the source has a smaller depth
  166. * than the target format, set the least significant bits of the target
  167. * to 0. This is likely sightly faster but less accurate. When this flag
  168. * is not specified, the most significant bits of the source are duplicated
  169. * in the least significant bits of the destination.
  170. *
  171. * The different flags that can be used when packing and unpacking.
  172. */
  173. typedef enum
  174. {
  175. GST_AUDIO_PACK_FLAG_NONE = 0,
  176. GST_AUDIO_PACK_FLAG_TRUNCATE_RANGE = (1 << 0)
  177. } GstAudioPackFlags;
  178. /**
  179. * GstAudioFormatUnpack:
  180. * @info: a #GstAudioFormatInfo
  181. * @dest: (array) (element-type guint8): a destination array
  182. * @data: (array) (element-type guint8): pointer to the audio data
  183. * @length: the amount of samples to unpack.
  184. *
  185. * Unpacks @length samples from the given data of format @info.
  186. * The samples will be unpacked into @dest which each channel
  187. * interleaved. @dest should at least be big enough to hold @length *
  188. * channels * size(unpack_format) bytes.
  189. */
  190. typedef void (*GstAudioFormatUnpack) (const GstAudioFormatInfo *info,
  191. GstAudioPackFlags flags, gpointer dest,
  192. const gpointer data, gint length);
  193. /**
  194. * GstAudioFormatPack:
  195. * @info: a #GstAudioFormatInfo
  196. * @src: (array) (element-type guint8): a source array
  197. * @data: (array) (element-type guint8): pointer to the destination
  198. * data
  199. * @length: the amount of samples to pack.
  200. *
  201. * Packs @length samples from @src to the data array in format @info.
  202. * The samples from source have each channel interleaved
  203. * and will be packed into @data.
  204. */
  205. typedef void (*GstAudioFormatPack) (const GstAudioFormatInfo *info,
  206. GstAudioPackFlags flags, const gpointer src,
  207. gpointer data, gint length);
  208. /**
  209. * GstAudioFormatInfo:
  210. * @format: #GstAudioFormat
  211. * @name: string representation of the format
  212. * @description: user readable description of the format
  213. * @flags: #GstAudioFormatFlags
  214. * @endianness: the endianness
  215. * @width: amount of bits used for one sample
  216. * @depth: amount of valid bits in @width
  217. * @silence: @width/8 bytes with 1 silent sample
  218. * @unpack_format: the format of the unpacked samples
  219. * @unpack_func: function to unpack samples
  220. * @pack_func: function to pack samples
  221. *
  222. * Information for an audio format.
  223. */
  224. struct _GstAudioFormatInfo {
  225. GstAudioFormat format;
  226. const gchar *name;
  227. const gchar *description;
  228. GstAudioFormatFlags flags;
  229. gint endianness;
  230. gint width;
  231. gint depth;
  232. guint8 silence[8];
  233. GstAudioFormat unpack_format;
  234. GstAudioFormatUnpack unpack_func;
  235. GstAudioFormatPack pack_func;
  236. /*< private >*/
  237. gpointer _gst_reserved[GST_PADDING];
  238. };
  239. GType gst_audio_format_info_get_type (void);
  240. #define GST_AUDIO_FORMAT_INFO_FORMAT(info) ((info)->format)
  241. #define GST_AUDIO_FORMAT_INFO_NAME(info) ((info)->name)
  242. #define GST_AUDIO_FORMAT_INFO_FLAGS(info) ((info)->flags)
  243. #define GST_AUDIO_FORMAT_INFO_IS_INTEGER(info) !!((info)->flags & GST_AUDIO_FORMAT_FLAG_INTEGER)
  244. #define GST_AUDIO_FORMAT_INFO_IS_FLOAT(info) !!((info)->flags & GST_AUDIO_FORMAT_FLAG_FLOAT)
  245. #define GST_AUDIO_FORMAT_INFO_IS_SIGNED(info) !!((info)->flags & GST_AUDIO_FORMAT_FLAG_SIGNED)
  246. #define GST_AUDIO_FORMAT_INFO_ENDIANNESS(info) ((info)->endianness)
  247. #define GST_AUDIO_FORMAT_INFO_IS_LITTLE_ENDIAN(info) ((info)->endianness == G_LITTLE_ENDIAN)
  248. #define GST_AUDIO_FORMAT_INFO_IS_BIG_ENDIAN(info) ((info)->endianness == G_BIG_ENDIAN)
  249. #define GST_AUDIO_FORMAT_INFO_WIDTH(info) ((info)->width)
  250. #define GST_AUDIO_FORMAT_INFO_DEPTH(info) ((info)->depth)
  251. GstAudioFormat gst_audio_format_build_integer (gboolean sign, gint endianness,
  252. gint width, gint depth) G_GNUC_CONST;
  253. GstAudioFormat gst_audio_format_from_string (const gchar *format) G_GNUC_CONST;
  254. const gchar * gst_audio_format_to_string (GstAudioFormat format) G_GNUC_CONST;
  255. const GstAudioFormatInfo *
  256. gst_audio_format_get_info (GstAudioFormat format) G_GNUC_CONST;
  257. void gst_audio_format_fill_silence (const GstAudioFormatInfo *info,
  258. gpointer dest, gsize length);
  259. /**
  260. * GST_AUDIO_RATE_RANGE:
  261. *
  262. * Maximum range of allowed sample rates, for use in template caps strings.
  263. */
  264. #define GST_AUDIO_RATE_RANGE "(int) [ 1, max ]"
  265. /**
  266. * GST_AUDIO_CHANNELS_RANGE:
  267. *
  268. * Maximum range of allowed channels, for use in template caps strings.
  269. */
  270. #define GST_AUDIO_CHANNELS_RANGE "(int) [ 1, max ]"
  271. /**
  272. * GST_AUDIO_NE:
  273. * @s: format string without endianness marker
  274. *
  275. * Turns audio format string @s into the format string for native endianness.
  276. */
  277. /**
  278. * GST_AUDIO_OE:
  279. * @s: format string without endianness marker
  280. *
  281. * Turns audio format string @s into the format string for other endianness.
  282. */
  283. #if G_BYTE_ORDER == G_LITTLE_ENDIAN
  284. # define GST_AUDIO_NE(s) G_STRINGIFY(s)"LE"
  285. # define GST_AUDIO_OE(s) G_STRINGIFY(s)"BE"
  286. #else
  287. # define GST_AUDIO_NE(s) G_STRINGIFY(s)"BE"
  288. # define GST_AUDIO_OE(s) G_STRINGIFY(s)"LE"
  289. #endif
  290. /**
  291. * GST_AUDIO_FORMATS_ALL:
  292. *
  293. * List of all audio formats, for use in template caps strings.
  294. */
  295. #define GST_AUDIO_FORMATS_ALL " { S8, U8, " \
  296. "S16LE, S16BE, U16LE, U16BE, " \
  297. "S24_32LE, S24_32BE, U24_32LE, U24_32BE, " \
  298. "S32LE, S32BE, U32LE, U32BE, " \
  299. "S24LE, S24BE, U24LE, U24BE, " \
  300. "S20LE, S20BE, U20LE, U20BE, " \
  301. "S18LE, S18BE, U18LE, U18BE, " \
  302. "F32LE, F32BE, F64LE, F64BE }"
  303. /**
  304. * GST_AUDIO_CAPS_MAKE:
  305. * @format: string format that describes the sample layout, as string
  306. * (e.g. "S16LE", "S8", etc.)
  307. *
  308. * Generic caps string for audio, for use in pad templates.
  309. */
  310. #define GST_AUDIO_CAPS_MAKE(format) \
  311. "audio/x-raw, " \
  312. "format = (string) " format ", " \
  313. "rate = " GST_AUDIO_RATE_RANGE ", " \
  314. "channels = " GST_AUDIO_CHANNELS_RANGE
  315. /**
  316. * GST_AUDIO_DEF_RATE:
  317. *
  318. * Standard sampling rate used in consumer audio.
  319. */
  320. #define GST_AUDIO_DEF_RATE 44100
  321. /**
  322. * GST_AUDIO_DEF_CHANNELS:
  323. *
  324. * Standard number of channels used in consumer audio.
  325. */
  326. #define GST_AUDIO_DEF_CHANNELS 2
  327. /**
  328. * GST_AUDIO_DEF_FORMAT:
  329. *
  330. * Standard format used in consumer audio.
  331. */
  332. #define GST_AUDIO_DEF_FORMAT "S16LE"
  333. G_END_DECLS
  334. #endif /* __GST_AUDIO_FORMAT_H__ */