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.

159 lines
4.7KB

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