You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.9KB

  1. /*
  2. * TAK decoder/demuxer common code
  3. * Copyright (c) 2012 Paul B Mahol
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * TAK (Tom's lossless Audio Kompressor) decoder/demuxer common functions
  24. */
  25. #ifndef AVCODEC_TAK_H
  26. #define AVCODEC_TAK_H
  27. #include <stdint.h>
  28. #include "avcodec.h"
  29. #include "bitstream.h"
  30. #define TAK_FORMAT_DATA_TYPE_BITS 3
  31. #define TAK_FORMAT_SAMPLE_RATE_BITS 18
  32. #define TAK_FORMAT_BPS_BITS 5
  33. #define TAK_FORMAT_CHANNEL_BITS 4
  34. #define TAK_FORMAT_VALID_BITS 5
  35. #define TAK_FORMAT_CH_LAYOUT_BITS 6
  36. #define TAK_SIZE_FRAME_DURATION_BITS 4
  37. #define TAK_SIZE_SAMPLES_NUM_BITS 35
  38. #define TAK_LAST_FRAME_POS_BITS 40
  39. #define TAK_LAST_FRAME_SIZE_BITS 24
  40. #define TAK_ENCODER_CODEC_BITS 6
  41. #define TAK_ENCODER_PROFILE_BITS 4
  42. #define TAK_ENCODER_VERSION_BITS 24
  43. #define TAK_SAMPLE_RATE_MIN 6000
  44. #define TAK_CHANNELS_MIN 1
  45. #define TAK_BPS_MIN 8
  46. #define TAK_FRAME_HEADER_FLAGS_BITS 3
  47. #define TAK_FRAME_HEADER_SYNC_ID 0xA0FF
  48. #define TAK_FRAME_HEADER_SYNC_ID_BITS 16
  49. #define TAK_FRAME_HEADER_SAMPLE_COUNT_BITS 14
  50. #define TAK_FRAME_HEADER_NO_BITS 21
  51. #define TAK_FRAME_DURATION_QUANT_SHIFT 5
  52. #define TAK_CRC24_BITS 24
  53. #define TAK_FRAME_FLAG_IS_LAST 0x1
  54. #define TAK_FRAME_FLAG_HAS_INFO 0x2
  55. #define TAK_FRAME_FLAG_HAS_METADATA 0x4
  56. #define TAK_MAX_CHANNELS (1 << TAK_FORMAT_CHANNEL_BITS)
  57. #define TAK_MIN_FRAME_HEADER_BITS (TAK_FRAME_HEADER_SYNC_ID_BITS + \
  58. TAK_FRAME_HEADER_FLAGS_BITS + \
  59. TAK_FRAME_HEADER_NO_BITS + \
  60. TAK_CRC24_BITS)
  61. #define TAK_MIN_FRAME_HEADER_LAST_BITS (TAK_MIN_FRAME_HEADER_BITS + 2 + \
  62. TAK_FRAME_HEADER_SAMPLE_COUNT_BITS)
  63. #define TAK_ENCODER_BITS (TAK_ENCODER_CODEC_BITS + \
  64. TAK_ENCODER_PROFILE_BITS)
  65. #define TAK_SIZE_BITS (TAK_SIZE_SAMPLES_NUM_BITS + \
  66. TAK_SIZE_FRAME_DURATION_BITS)
  67. #define TAK_FORMAT_BITS (TAK_FORMAT_DATA_TYPE_BITS + \
  68. TAK_FORMAT_SAMPLE_RATE_BITS + \
  69. TAK_FORMAT_BPS_BITS + \
  70. TAK_FORMAT_CHANNEL_BITS + 1 + \
  71. TAK_FORMAT_VALID_BITS + 1 + \
  72. TAK_FORMAT_CH_LAYOUT_BITS * \
  73. TAK_MAX_CHANNELS)
  74. #define TAK_STREAMINFO_BITS (TAK_ENCODER_BITS + \
  75. TAK_SIZE_BITS + \
  76. TAK_FORMAT_BITS)
  77. #define TAK_MAX_FRAME_HEADER_BITS (TAK_MIN_FRAME_HEADER_LAST_BITS + \
  78. TAK_STREAMINFO_BITS + 31)
  79. #define TAK_STREAMINFO_BYTES ((TAK_STREAMINFO_BITS + 7) / 8)
  80. #define TAK_MAX_FRAME_HEADER_BYTES ((TAK_MAX_FRAME_HEADER_BITS + 7) / 8)
  81. #define TAK_MIN_FRAME_HEADER_BYTES ((TAK_MIN_FRAME_HEADER_BITS + 7) / 8)
  82. enum TAKCodecType {
  83. TAK_CODEC_MONO_STEREO = 2,
  84. TAK_CODEC_MULTICHANNEL = 4
  85. };
  86. enum TAKMetaDataType {
  87. TAK_METADATA_END = 0,
  88. TAK_METADATA_STREAMINFO,
  89. TAK_METADATA_SEEKTABLE,
  90. TAK_METADATA_SIMPLE_WAVE_DATA,
  91. TAK_METADATA_ENCODER,
  92. TAK_METADATA_PADDING,
  93. TAK_METADATA_MD5,
  94. TAK_METADATA_LAST_FRAME,
  95. };
  96. enum TAKFrameSizeType {
  97. TAK_FST_94ms = 0,
  98. TAK_FST_125ms,
  99. TAK_FST_188ms,
  100. TAK_FST_250ms,
  101. TAK_FST_4096,
  102. TAK_FST_8192,
  103. TAK_FST_16384,
  104. TAK_FST_512,
  105. TAK_FST_1024,
  106. TAK_FST_2048,
  107. };
  108. typedef struct TAKStreamInfo {
  109. int flags;
  110. enum TAKCodecType codec;
  111. int data_type;
  112. int sample_rate;
  113. int channels;
  114. int bps;
  115. int frame_num;
  116. int frame_samples;
  117. int last_frame_samples;
  118. uint64_t ch_layout;
  119. int64_t samples;
  120. } TAKStreamInfo;
  121. void ff_tak_init_crc(void);
  122. int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size);
  123. /**
  124. * Parse the Streaminfo metadata block.
  125. * @param[in] bc pointer to BitstreamContext
  126. * @param[out] s storage for parsed information
  127. */
  128. void avpriv_tak_parse_streaminfo(BitstreamContext *bc, TAKStreamInfo *s);
  129. /**
  130. * Validate and decode a frame header.
  131. * @param avctx AVCodecContext to use as av_log() context
  132. * @param[in] bc BitstreamContext from which to read frame header
  133. * @param[out] s frame information
  134. * @param log_level_offset log level offset, can be used to silence
  135. * error messages.
  136. * @return non-zero on error, 0 if OK
  137. */
  138. int ff_tak_decode_frame_header(AVCodecContext *avctx, BitstreamContext *bc,
  139. TAKStreamInfo *s, int log_level_offset);
  140. #endif /* AVCODEC_TAK_H */