dca.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * DCA compatible decoder
  3. * Copyright (C) 2004 Gildas Bazin
  4. * Copyright (C) 2004 Benjamin Zores
  5. * Copyright (C) 2006 Benjamin Larsson
  6. * Copyright (C) 2007 Konstantin Shishkov
  7. * Copyright (C) 2016 foo86
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #ifndef AVCODEC_DCA_H
  26. #define AVCODEC_DCA_H
  27. #include <stdint.h>
  28. #include "libavutil/common.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "get_bits.h"
  31. #include "internal.h"
  32. #define DCA_CORE_FRAME_HEADER_SIZE 18
  33. enum DCAParseError {
  34. DCA_PARSE_ERROR_SYNC_WORD = -1,
  35. DCA_PARSE_ERROR_DEFICIT_SAMPLES = -2,
  36. DCA_PARSE_ERROR_PCM_BLOCKS = -3,
  37. DCA_PARSE_ERROR_FRAME_SIZE = -4,
  38. DCA_PARSE_ERROR_AMODE = -5,
  39. DCA_PARSE_ERROR_SAMPLE_RATE = -6,
  40. DCA_PARSE_ERROR_RESERVED_BIT = -7,
  41. DCA_PARSE_ERROR_LFE_FLAG = -8,
  42. DCA_PARSE_ERROR_PCM_RES = -9,
  43. };
  44. typedef struct DCACoreFrameHeader {
  45. uint8_t normal_frame; ///< Frame type
  46. uint8_t deficit_samples; ///< Deficit sample count
  47. uint8_t crc_present; ///< CRC present flag
  48. uint8_t npcmblocks; ///< Number of PCM sample blocks
  49. uint16_t frame_size; ///< Primary frame byte size
  50. uint8_t audio_mode; ///< Audio channel arrangement
  51. uint8_t sr_code; ///< Core audio sampling frequency
  52. uint8_t br_code; ///< Transmission bit rate
  53. uint8_t drc_present; ///< Embedded dynamic range flag
  54. uint8_t ts_present; ///< Embedded time stamp flag
  55. uint8_t aux_present; ///< Auxiliary data flag
  56. uint8_t hdcd_master; ///< HDCD mastering flag
  57. uint8_t ext_audio_type; ///< Extension audio descriptor flag
  58. uint8_t ext_audio_present; ///< Extended coding flag
  59. uint8_t sync_ssf; ///< Audio sync word insertion flag
  60. uint8_t lfe_present; ///< Low frequency effects flag
  61. uint8_t predictor_history; ///< Predictor history flag switch
  62. uint8_t filter_perfect; ///< Multirate interpolator switch
  63. uint8_t encoder_rev; ///< Encoder software revision
  64. uint8_t copy_hist; ///< Copy history
  65. uint8_t pcmr_code; ///< Source PCM resolution
  66. uint8_t sumdiff_front; ///< Front sum/difference flag
  67. uint8_t sumdiff_surround; ///< Surround sum/difference flag
  68. uint8_t dn_code; ///< Dialog normalization / unspecified
  69. } DCACoreFrameHeader;
  70. enum DCASpeaker {
  71. DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls,
  72. DCA_SPEAKER_Rs, DCA_SPEAKER_LFE1, DCA_SPEAKER_Cs, DCA_SPEAKER_Lsr,
  73. DCA_SPEAKER_Rsr, DCA_SPEAKER_Lss, DCA_SPEAKER_Rss, DCA_SPEAKER_Lc,
  74. DCA_SPEAKER_Rc, DCA_SPEAKER_Lh, DCA_SPEAKER_Ch, DCA_SPEAKER_Rh,
  75. DCA_SPEAKER_LFE2, DCA_SPEAKER_Lw, DCA_SPEAKER_Rw, DCA_SPEAKER_Oh,
  76. DCA_SPEAKER_Lhs, DCA_SPEAKER_Rhs, DCA_SPEAKER_Chr, DCA_SPEAKER_Lhr,
  77. DCA_SPEAKER_Rhr, DCA_SPEAKER_Cl, DCA_SPEAKER_Ll, DCA_SPEAKER_Rl,
  78. DCA_SPEAKER_RSV1, DCA_SPEAKER_RSV2, DCA_SPEAKER_RSV3, DCA_SPEAKER_RSV4,
  79. DCA_SPEAKER_COUNT
  80. };
  81. enum DCASpeakerMask {
  82. DCA_SPEAKER_MASK_C = 0x00000001,
  83. DCA_SPEAKER_MASK_L = 0x00000002,
  84. DCA_SPEAKER_MASK_R = 0x00000004,
  85. DCA_SPEAKER_MASK_Ls = 0x00000008,
  86. DCA_SPEAKER_MASK_Rs = 0x00000010,
  87. DCA_SPEAKER_MASK_LFE1 = 0x00000020,
  88. DCA_SPEAKER_MASK_Cs = 0x00000040,
  89. DCA_SPEAKER_MASK_Lsr = 0x00000080,
  90. DCA_SPEAKER_MASK_Rsr = 0x00000100,
  91. DCA_SPEAKER_MASK_Lss = 0x00000200,
  92. DCA_SPEAKER_MASK_Rss = 0x00000400,
  93. DCA_SPEAKER_MASK_Lc = 0x00000800,
  94. DCA_SPEAKER_MASK_Rc = 0x00001000,
  95. DCA_SPEAKER_MASK_Lh = 0x00002000,
  96. DCA_SPEAKER_MASK_Ch = 0x00004000,
  97. DCA_SPEAKER_MASK_Rh = 0x00008000,
  98. DCA_SPEAKER_MASK_LFE2 = 0x00010000,
  99. DCA_SPEAKER_MASK_Lw = 0x00020000,
  100. DCA_SPEAKER_MASK_Rw = 0x00040000,
  101. DCA_SPEAKER_MASK_Oh = 0x00080000,
  102. DCA_SPEAKER_MASK_Lhs = 0x00100000,
  103. DCA_SPEAKER_MASK_Rhs = 0x00200000,
  104. DCA_SPEAKER_MASK_Chr = 0x00400000,
  105. DCA_SPEAKER_MASK_Lhr = 0x00800000,
  106. DCA_SPEAKER_MASK_Rhr = 0x01000000,
  107. DCA_SPEAKER_MASK_Cl = 0x02000000,
  108. DCA_SPEAKER_MASK_Ll = 0x04000000,
  109. DCA_SPEAKER_MASK_Rl = 0x08000000,
  110. };
  111. #define DCA_SPEAKER_LAYOUT_MONO (DCA_SPEAKER_MASK_C)
  112. #define DCA_SPEAKER_LAYOUT_STEREO (DCA_SPEAKER_MASK_L | DCA_SPEAKER_MASK_R)
  113. #define DCA_SPEAKER_LAYOUT_2POINT1 (DCA_SPEAKER_LAYOUT_STEREO | DCA_SPEAKER_MASK_LFE1)
  114. #define DCA_SPEAKER_LAYOUT_3_0 (DCA_SPEAKER_LAYOUT_STEREO | DCA_SPEAKER_MASK_C)
  115. #define DCA_SPEAKER_LAYOUT_2_1 (DCA_SPEAKER_LAYOUT_STEREO | DCA_SPEAKER_MASK_Cs)
  116. #define DCA_SPEAKER_LAYOUT_3_1 (DCA_SPEAKER_LAYOUT_3_0 | DCA_SPEAKER_MASK_Cs)
  117. #define DCA_SPEAKER_LAYOUT_2_2 (DCA_SPEAKER_LAYOUT_STEREO | DCA_SPEAKER_MASK_Ls | DCA_SPEAKER_MASK_Rs)
  118. #define DCA_SPEAKER_LAYOUT_5POINT0 (DCA_SPEAKER_LAYOUT_3_0 | DCA_SPEAKER_MASK_Ls | DCA_SPEAKER_MASK_Rs)
  119. #define DCA_SPEAKER_LAYOUT_5POINT1 (DCA_SPEAKER_LAYOUT_5POINT0 | DCA_SPEAKER_MASK_LFE1)
  120. #define DCA_SPEAKER_LAYOUT_7POINT0_WIDE (DCA_SPEAKER_LAYOUT_5POINT0 | DCA_SPEAKER_MASK_Lw | DCA_SPEAKER_MASK_Rw)
  121. #define DCA_SPEAKER_LAYOUT_7POINT1_WIDE (DCA_SPEAKER_LAYOUT_7POINT0_WIDE | DCA_SPEAKER_MASK_LFE1)
  122. #define DCA_HAS_STEREO(mask) \
  123. ((mask & DCA_SPEAKER_LAYOUT_STEREO) == DCA_SPEAKER_LAYOUT_STEREO)
  124. enum DCASpeakerPair {
  125. DCA_SPEAKER_PAIR_C = 0x0001,
  126. DCA_SPEAKER_PAIR_LR = 0x0002,
  127. DCA_SPEAKER_PAIR_LsRs = 0x0004,
  128. DCA_SPEAKER_PAIR_LFE1 = 0x0008,
  129. DCA_SPEAKER_PAIR_Cs = 0x0010,
  130. DCA_SPEAKER_PAIR_LhRh = 0x0020,
  131. DCA_SPEAKER_PAIR_LsrRsr = 0x0040,
  132. DCA_SPEAKER_PAIR_Ch = 0x0080,
  133. DCA_SPEAKER_PAIR_Oh = 0x0100,
  134. DCA_SPEAKER_PAIR_LcRc = 0x0200,
  135. DCA_SPEAKER_PAIR_LwRw = 0x0400,
  136. DCA_SPEAKER_PAIR_LssRss = 0x0800,
  137. DCA_SPEAKER_PAIR_LFE2 = 0x1000,
  138. DCA_SPEAKER_PAIR_LhsRhs = 0x2000,
  139. DCA_SPEAKER_PAIR_Chr = 0x4000,
  140. DCA_SPEAKER_PAIR_LhrRhr = 0x8000
  141. };
  142. /**
  143. * Return number of individual channels in DCASpeakerPair mask
  144. */
  145. static inline int ff_dca_count_chs_for_mask(unsigned int mask)
  146. {
  147. return av_popcount((mask & 0xffff) | ((mask & 0xae66) << 16));
  148. }
  149. enum DCARepresentationType {
  150. DCA_REPR_TYPE_LtRt = 2,
  151. DCA_REPR_TYPE_LhRh = 3
  152. };
  153. enum DCAExtensionMask {
  154. DCA_CSS_CORE = 0x001,
  155. DCA_CSS_XXCH = 0x002,
  156. DCA_CSS_X96 = 0x004,
  157. DCA_CSS_XCH = 0x008,
  158. DCA_CSS_MASK = 0x00f,
  159. DCA_EXSS_CORE = 0x010,
  160. DCA_EXSS_XBR = 0x020,
  161. DCA_EXSS_XXCH = 0x040,
  162. DCA_EXSS_X96 = 0x080,
  163. DCA_EXSS_LBR = 0x100,
  164. DCA_EXSS_XLL = 0x200,
  165. DCA_EXSS_RSV1 = 0x400,
  166. DCA_EXSS_RSV2 = 0x800,
  167. DCA_EXSS_MASK = 0xff0,
  168. };
  169. enum DCADownMixType {
  170. DCA_DMIX_TYPE_1_0,
  171. DCA_DMIX_TYPE_LoRo,
  172. DCA_DMIX_TYPE_LtRt,
  173. DCA_DMIX_TYPE_3_0,
  174. DCA_DMIX_TYPE_2_1,
  175. DCA_DMIX_TYPE_2_2,
  176. DCA_DMIX_TYPE_3_1,
  177. DCA_DMIX_TYPE_COUNT
  178. };
  179. extern av_export_avcodec const uint32_t avpriv_dca_sample_rates[16];
  180. extern const uint32_t ff_dca_sampling_freqs[16];
  181. extern const uint8_t ff_dca_freq_ranges[16];
  182. extern const uint8_t ff_dca_bits_per_sample[8];
  183. /**
  184. * Convert bitstream to one representation based on sync marker
  185. */
  186. int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
  187. int max_size);
  188. /**
  189. * Parse and validate core frame header
  190. * @param[out] h Pointer to struct where header info is written.
  191. * @param[in] buf Pointer to the data buffer
  192. * @param[in] size Size of the data buffer
  193. * @return 0 on success, negative AVERROR code on failure
  194. */
  195. int avpriv_dca_parse_core_frame_header(DCACoreFrameHeader *h, const uint8_t *buf, int size);
  196. /**
  197. * Parse and validate core frame header
  198. * @param[out] h Pointer to struct where header info is written.
  199. * @param[in] gbc BitContext containing the first 120 bits of the frame.
  200. * @return 0 on success, negative DCA_PARSE_ERROR_ code on failure
  201. */
  202. int ff_dca_parse_core_frame_header(DCACoreFrameHeader *h, GetBitContext *gb);
  203. #endif /* AVCODEC_DCA_H */