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.

161 lines
4.8KB

  1. /*
  2. * TAK common code
  3. * Copyright (c) 2012 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/crc.h"
  22. #include "libavutil/intreadwrite.h"
  23. #define BITSTREAM_READER_LE
  24. #include "tak.h"
  25. static const int64_t tak_channel_layouts[] = {
  26. 0,
  27. AV_CH_FRONT_LEFT,
  28. AV_CH_FRONT_RIGHT,
  29. AV_CH_FRONT_CENTER,
  30. AV_CH_LOW_FREQUENCY,
  31. AV_CH_BACK_LEFT,
  32. AV_CH_BACK_RIGHT,
  33. AV_CH_FRONT_LEFT_OF_CENTER,
  34. AV_CH_FRONT_RIGHT_OF_CENTER,
  35. AV_CH_BACK_CENTER,
  36. AV_CH_SIDE_LEFT,
  37. AV_CH_SIDE_RIGHT,
  38. AV_CH_TOP_CENTER,
  39. AV_CH_TOP_FRONT_LEFT,
  40. AV_CH_TOP_FRONT_CENTER,
  41. AV_CH_TOP_FRONT_RIGHT,
  42. AV_CH_TOP_BACK_LEFT,
  43. AV_CH_TOP_BACK_CENTER,
  44. AV_CH_TOP_BACK_RIGHT,
  45. };
  46. static const uint16_t frame_duration_type_quants[] = {
  47. 3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048,
  48. };
  49. static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
  50. {
  51. int nb_samples, max_nb_samples;
  52. if (type <= TAK_FST_250ms) {
  53. nb_samples = sample_rate * frame_duration_type_quants[type] >>
  54. TAK_FRAME_DURATION_QUANT_SHIFT;
  55. max_nb_samples = 16384;
  56. } else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) {
  57. nb_samples = frame_duration_type_quants[type];
  58. max_nb_samples = sample_rate *
  59. frame_duration_type_quants[TAK_FST_250ms] >>
  60. TAK_FRAME_DURATION_QUANT_SHIFT;
  61. } else {
  62. return AVERROR_INVALIDDATA;
  63. }
  64. if (nb_samples <= 0 || nb_samples > max_nb_samples)
  65. return AVERROR_INVALIDDATA;
  66. return nb_samples;
  67. }
  68. int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
  69. {
  70. uint32_t crc, CRC;
  71. if (buf_size < 4)
  72. return AVERROR_INVALIDDATA;
  73. buf_size -= 3;
  74. CRC = AV_RB24(buf + buf_size);
  75. crc = av_crc(av_crc_get_table(AV_CRC_24_IEEE), 0xCE04B7U, buf, buf_size);
  76. if (CRC != crc)
  77. return AVERROR_INVALIDDATA;
  78. return 0;
  79. }
  80. void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
  81. {
  82. uint64_t channel_mask = 0;
  83. int frame_type, i;
  84. s->codec = get_bits(gb, TAK_ENCODER_CODEC_BITS);
  85. skip_bits(gb, TAK_ENCODER_PROFILE_BITS);
  86. frame_type = get_bits(gb, TAK_SIZE_FRAME_DURATION_BITS);
  87. s->samples = get_bits64(gb, TAK_SIZE_SAMPLES_NUM_BITS);
  88. s->data_type = get_bits(gb, TAK_FORMAT_DATA_TYPE_BITS);
  89. s->sample_rate = get_bits(gb, TAK_FORMAT_SAMPLE_RATE_BITS) +
  90. TAK_SAMPLE_RATE_MIN;
  91. s->bps = get_bits(gb, TAK_FORMAT_BPS_BITS) +
  92. TAK_BPS_MIN;
  93. s->channels = get_bits(gb, TAK_FORMAT_CHANNEL_BITS) +
  94. TAK_CHANNELS_MIN;
  95. if (get_bits1(gb)) {
  96. skip_bits(gb, TAK_FORMAT_VALID_BITS);
  97. if (get_bits1(gb)) {
  98. for (i = 0; i < s->channels; i++) {
  99. int value = get_bits(gb, TAK_FORMAT_CH_LAYOUT_BITS);
  100. if (value < FF_ARRAY_ELEMS(tak_channel_layouts))
  101. channel_mask |= tak_channel_layouts[value];
  102. }
  103. }
  104. }
  105. s->ch_layout = channel_mask;
  106. s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
  107. }
  108. int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
  109. TAKStreamInfo *ti, int log_level_offset)
  110. {
  111. if (get_bits(gb, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) {
  112. av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n");
  113. return AVERROR_INVALIDDATA;
  114. }
  115. ti->flags = get_bits(gb, TAK_FRAME_HEADER_FLAGS_BITS);
  116. ti->frame_num = get_bits(gb, TAK_FRAME_HEADER_NO_BITS);
  117. if (ti->flags & TAK_FRAME_FLAG_IS_LAST) {
  118. ti->last_frame_samples = get_bits(gb, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1;
  119. skip_bits(gb, 2);
  120. } else {
  121. ti->last_frame_samples = 0;
  122. }
  123. if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) {
  124. avpriv_tak_parse_streaminfo(gb, ti);
  125. if (get_bits(gb, 6))
  126. skip_bits(gb, 25);
  127. align_get_bits(gb);
  128. }
  129. if (ti->flags & TAK_FRAME_FLAG_HAS_METADATA)
  130. return AVERROR_INVALIDDATA;
  131. skip_bits(gb, 24);
  132. return 0;
  133. }