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.

163 lines
4.8KB

  1. /*
  2. * DCA compatible decoder data
  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. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include "libavutil/error.h"
  27. #include "dca.h"
  28. #include "dca_core.h"
  29. #include "dca_syncwords.h"
  30. #include "get_bits.h"
  31. #include "put_bits.h"
  32. const uint32_t avpriv_dca_sample_rates[16] = {
  33. 0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0,
  34. 12000, 24000, 48000, 96000, 192000
  35. };
  36. const uint32_t ff_dca_sampling_freqs[16] = {
  37. 8000, 16000, 32000, 64000, 128000, 22050, 44100, 88200,
  38. 176400, 352800, 12000, 24000, 48000, 96000, 192000, 384000,
  39. };
  40. const uint8_t ff_dca_freq_ranges[16] = {
  41. 0, 1, 2, 3, 4, 1, 2, 3, 4, 4, 0, 1, 2, 3, 4, 4
  42. };
  43. const uint8_t ff_dca_bits_per_sample[8] = {
  44. 16, 16, 20, 20, 0, 24, 24, 0
  45. };
  46. int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
  47. int max_size)
  48. {
  49. uint32_t mrk;
  50. int i, tmp;
  51. PutBitContext pb;
  52. if ((unsigned) src_size > (unsigned) max_size)
  53. src_size = max_size;
  54. mrk = AV_RB32(src);
  55. switch (mrk) {
  56. case DCA_SYNCWORD_CORE_BE:
  57. case DCA_SYNCWORD_SUBSTREAM:
  58. memcpy(dst, src, src_size);
  59. return src_size;
  60. case DCA_SYNCWORD_CORE_LE:
  61. for (i = 0; i < (src_size + 1) >> 1; i++) {
  62. AV_WB16(dst, AV_RL16(src));
  63. src += 2;
  64. dst += 2;
  65. }
  66. return src_size;
  67. case DCA_SYNCWORD_CORE_14B_BE:
  68. case DCA_SYNCWORD_CORE_14B_LE:
  69. init_put_bits(&pb, dst, max_size);
  70. for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
  71. tmp = ((mrk == DCA_SYNCWORD_CORE_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
  72. put_bits(&pb, 14, tmp);
  73. }
  74. flush_put_bits(&pb);
  75. return (put_bits_count(&pb) + 7) >> 3;
  76. default:
  77. return AVERROR_INVALIDDATA;
  78. }
  79. }
  80. int ff_dca_parse_core_frame_header(DCACoreFrameHeader *h, GetBitContext *gb)
  81. {
  82. if (get_bits_long(gb, 32) != DCA_SYNCWORD_CORE_BE)
  83. return DCA_PARSE_ERROR_SYNC_WORD;
  84. h->normal_frame = get_bits1(gb);
  85. h->deficit_samples = get_bits(gb, 5) + 1;
  86. if (h->deficit_samples != DCA_PCMBLOCK_SAMPLES)
  87. return DCA_PARSE_ERROR_DEFICIT_SAMPLES;
  88. h->crc_present = get_bits1(gb);
  89. h->npcmblocks = get_bits(gb, 7) + 1;
  90. if (h->npcmblocks & (DCA_SUBBAND_SAMPLES - 1))
  91. return DCA_PARSE_ERROR_PCM_BLOCKS;
  92. h->frame_size = get_bits(gb, 14) + 1;
  93. if (h->frame_size < 96)
  94. return DCA_PARSE_ERROR_FRAME_SIZE;
  95. h->audio_mode = get_bits(gb, 6);
  96. if (h->audio_mode >= DCA_AMODE_COUNT)
  97. return DCA_PARSE_ERROR_AMODE;
  98. h->sr_code = get_bits(gb, 4);
  99. if (!avpriv_dca_sample_rates[h->sr_code])
  100. return DCA_PARSE_ERROR_SAMPLE_RATE;
  101. h->br_code = get_bits(gb, 5);
  102. if (get_bits1(gb))
  103. return DCA_PARSE_ERROR_RESERVED_BIT;
  104. h->drc_present = get_bits1(gb);
  105. h->ts_present = get_bits1(gb);
  106. h->aux_present = get_bits1(gb);
  107. h->hdcd_master = get_bits1(gb);
  108. h->ext_audio_type = get_bits(gb, 3);
  109. h->ext_audio_present = get_bits1(gb);
  110. h->sync_ssf = get_bits1(gb);
  111. h->lfe_present = get_bits(gb, 2);
  112. if (h->lfe_present == DCA_LFE_FLAG_INVALID)
  113. return DCA_PARSE_ERROR_LFE_FLAG;
  114. h->predictor_history = get_bits1(gb);
  115. if (h->crc_present)
  116. skip_bits(gb, 16);
  117. h->filter_perfect = get_bits1(gb);
  118. h->encoder_rev = get_bits(gb, 4);
  119. h->copy_hist = get_bits(gb, 2);
  120. h->pcmr_code = get_bits(gb, 3);
  121. if (!ff_dca_bits_per_sample[h->pcmr_code])
  122. return DCA_PARSE_ERROR_PCM_RES;
  123. h->sumdiff_front = get_bits1(gb);
  124. h->sumdiff_surround = get_bits1(gb);
  125. h->dn_code = get_bits(gb, 4);
  126. return 0;
  127. }
  128. int avpriv_dca_parse_core_frame_header(DCACoreFrameHeader *h, const uint8_t *buf, int size)
  129. {
  130. GetBitContext gb;
  131. int ret;
  132. ret = init_get_bits8(&gb, buf, size);
  133. if (ret < 0)
  134. return ret;
  135. if (ff_dca_parse_core_frame_header(h, &gb) < 0)
  136. return AVERROR_INVALIDDATA;
  137. return 0;
  138. }