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.

154 lines
4.6KB

  1. /*
  2. * TAK 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. #include "libavutil/bswap.h"
  22. #include "libavutil/crc.h"
  23. #include "libavutil/intreadwrite.h"
  24. #define BITSTREAM_READER_LE
  25. #include "bitstream.h"
  26. #include "tak.h"
  27. static const uint16_t frame_duration_type_quants[] = {
  28. 3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048,
  29. };
  30. static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
  31. {
  32. int nb_samples, max_nb_samples;
  33. if (type <= TAK_FST_250ms) {
  34. nb_samples = sample_rate * frame_duration_type_quants[type] >>
  35. TAK_FRAME_DURATION_QUANT_SHIFT;
  36. max_nb_samples = 16384;
  37. } else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) {
  38. nb_samples = frame_duration_type_quants[type];
  39. max_nb_samples = sample_rate *
  40. frame_duration_type_quants[TAK_FST_250ms] >>
  41. TAK_FRAME_DURATION_QUANT_SHIFT;
  42. } else {
  43. return AVERROR_INVALIDDATA;
  44. }
  45. if (nb_samples <= 0 || nb_samples > max_nb_samples)
  46. return AVERROR_INVALIDDATA;
  47. return nb_samples;
  48. }
  49. static int crc_init = 0;
  50. #if CONFIG_SMALL
  51. #define CRC_TABLE_SIZE 257
  52. #else
  53. #define CRC_TABLE_SIZE 1024
  54. #endif
  55. static AVCRC crc_24[CRC_TABLE_SIZE];
  56. av_cold void ff_tak_init_crc(void)
  57. {
  58. if (!crc_init) {
  59. av_crc_init(crc_24, 0, 24, 0x864CFBU, sizeof(crc_24));
  60. crc_init = 1;
  61. }
  62. }
  63. int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
  64. {
  65. uint32_t crc, CRC;
  66. if (buf_size < 4)
  67. return AVERROR_INVALIDDATA;
  68. buf_size -= 3;
  69. CRC = av_bswap32(AV_RL24(buf + buf_size)) >> 8;
  70. crc = av_crc(crc_24, 0xCE04B7U, buf, buf_size);
  71. if (CRC != crc)
  72. return AVERROR_INVALIDDATA;
  73. return 0;
  74. }
  75. void avpriv_tak_parse_streaminfo(BitstreamContext *bc, TAKStreamInfo *s)
  76. {
  77. uint64_t channel_mask = 0;
  78. int frame_type, i;
  79. s->codec = bitstream_read(bc, TAK_ENCODER_CODEC_BITS);
  80. bitstream_skip(bc, TAK_ENCODER_PROFILE_BITS);
  81. frame_type = bitstream_read(bc, TAK_SIZE_FRAME_DURATION_BITS);
  82. s->samples = bitstream_read_63(bc, TAK_SIZE_SAMPLES_NUM_BITS);
  83. s->data_type = bitstream_read(bc, TAK_FORMAT_DATA_TYPE_BITS);
  84. s->sample_rate = bitstream_read(bc, TAK_FORMAT_SAMPLE_RATE_BITS) +
  85. TAK_SAMPLE_RATE_MIN;
  86. s->bps = bitstream_read(bc, TAK_FORMAT_BPS_BITS) +
  87. TAK_BPS_MIN;
  88. s->channels = bitstream_read(bc, TAK_FORMAT_CHANNEL_BITS) +
  89. TAK_CHANNELS_MIN;
  90. if (bitstream_read_bit(bc)) {
  91. bitstream_skip(bc, TAK_FORMAT_VALID_BITS);
  92. if (bitstream_read_bit(bc)) {
  93. for (i = 0; i < s->channels; i++) {
  94. int value = bitstream_read(bc, TAK_FORMAT_CH_LAYOUT_BITS);
  95. if (value > 0 && value <= 18)
  96. channel_mask |= 1 << (value - 1);
  97. }
  98. }
  99. }
  100. s->ch_layout = channel_mask;
  101. s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
  102. }
  103. int ff_tak_decode_frame_header(AVCodecContext *avctx, BitstreamContext *bc,
  104. TAKStreamInfo *ti, int log_level_offset)
  105. {
  106. if (bitstream_read(bc, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) {
  107. av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n");
  108. return AVERROR_INVALIDDATA;
  109. }
  110. ti->flags = bitstream_read(bc, TAK_FRAME_HEADER_FLAGS_BITS);
  111. ti->frame_num = bitstream_read(bc, TAK_FRAME_HEADER_NO_BITS);
  112. if (ti->flags & TAK_FRAME_FLAG_IS_LAST) {
  113. ti->last_frame_samples = bitstream_read(bc, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1;
  114. bitstream_skip(bc, 2);
  115. } else {
  116. ti->last_frame_samples = 0;
  117. }
  118. if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) {
  119. avpriv_tak_parse_streaminfo(bc, ti);
  120. if (bitstream_read(bc, 6))
  121. bitstream_skip(bc, 25);
  122. bitstream_align(bc);
  123. }
  124. bitstream_skip(bc, 24);
  125. return 0;
  126. }