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.

216 lines
6.9KB

  1. /*
  2. * Copyright (c) 2007 Ian Caulfield
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include "libavutil/channel_layout.h"
  22. #include "get_bits.h"
  23. #include "mlp_parse.h"
  24. #include "mlp.h"
  25. static const uint8_t mlp_quants[16] = {
  26. 16, 20, 24, 0, 0, 0, 0, 0,
  27. 0, 0, 0, 0, 0, 0, 0, 0,
  28. };
  29. static const uint8_t mlp_channels[32] = {
  30. 1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
  31. 5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32. };
  33. static const uint64_t mlp_layout[32] = {
  34. AV_CH_LAYOUT_MONO,
  35. AV_CH_LAYOUT_STEREO,
  36. AV_CH_LAYOUT_2_1,
  37. AV_CH_LAYOUT_QUAD,
  38. AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
  39. AV_CH_LAYOUT_2_1|AV_CH_LOW_FREQUENCY,
  40. AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY,
  41. AV_CH_LAYOUT_SURROUND,
  42. AV_CH_LAYOUT_4POINT0,
  43. AV_CH_LAYOUT_5POINT0_BACK,
  44. AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
  45. AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
  46. AV_CH_LAYOUT_5POINT1_BACK,
  47. AV_CH_LAYOUT_4POINT0,
  48. AV_CH_LAYOUT_5POINT0_BACK,
  49. AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
  50. AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
  51. AV_CH_LAYOUT_5POINT1_BACK,
  52. AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY,
  53. AV_CH_LAYOUT_5POINT0_BACK,
  54. AV_CH_LAYOUT_5POINT1_BACK,
  55. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  56. };
  57. static const uint8_t thd_chancount[13] = {
  58. // LR C LFE LRs LRvh LRc LRrs Cs Ts LRsd LRw Cvh LFE2
  59. 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1
  60. };
  61. static const uint64_t thd_layout[13] = {
  62. AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT, // LR
  63. AV_CH_FRONT_CENTER, // C
  64. AV_CH_LOW_FREQUENCY, // LFE
  65. AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, // LRs
  66. AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT, // LRvh
  67. AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER, // LRc
  68. AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT, // LRrs
  69. AV_CH_BACK_CENTER, // Cs
  70. AV_CH_TOP_CENTER, // Ts
  71. AV_CH_SURROUND_DIRECT_LEFT|AV_CH_SURROUND_DIRECT_RIGHT, // LRsd
  72. AV_CH_WIDE_LEFT|AV_CH_WIDE_RIGHT, // LRw
  73. AV_CH_TOP_FRONT_CENTER, // Cvh
  74. AV_CH_LOW_FREQUENCY_2, // LFE2
  75. };
  76. static int mlp_samplerate(int in)
  77. {
  78. if (in == 0xF)
  79. return 0;
  80. return (in & 8 ? 44100 : 48000) << (in & 7) ;
  81. }
  82. static int truehd_channels(int chanmap)
  83. {
  84. int channels = 0, i;
  85. for (i = 0; i < 13; i++)
  86. channels += thd_chancount[i] * ((chanmap >> i) & 1);
  87. return channels;
  88. }
  89. static uint64_t truehd_layout(int chanmap)
  90. {
  91. int i;
  92. uint64_t layout = 0;
  93. for (i = 0; i < 13; i++)
  94. layout |= thd_layout[i] * ((chanmap >> i) & 1);
  95. return layout;
  96. }
  97. static int mlp_get_major_sync_size(const uint8_t * buf, int bufsize)
  98. {
  99. int has_extension, extensions = 0;
  100. int size = 28;
  101. if (bufsize < 28)
  102. return -1;
  103. if (AV_RB32(buf) == 0xf8726fba) {
  104. has_extension = buf[25] & 1;
  105. if (has_extension) {
  106. extensions = buf[26] >> 4;
  107. size += 2 + extensions * 2;
  108. }
  109. }
  110. return size;
  111. }
  112. /** Read a major sync info header - contains high level information about
  113. * the stream - sample rate, channel arrangement etc. Most of this
  114. * information is not actually necessary for decoding, only for playback.
  115. * gb must be a freshly initialized GetBitContext with no bits read.
  116. */
  117. int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
  118. {
  119. int ratebits, channel_arrangement, header_size;
  120. uint16_t checksum;
  121. av_assert1(get_bits_count(gb) == 0);
  122. header_size = mlp_get_major_sync_size(gb->buffer, gb->size_in_bits >> 3);
  123. if (header_size < 0 || gb->size_in_bits < header_size << 3) {
  124. av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
  125. return -1;
  126. }
  127. checksum = ff_mlp_checksum16(gb->buffer, header_size - 2);
  128. if (checksum != AV_RL16(gb->buffer+header_size-2)) {
  129. av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
  130. return AVERROR_INVALIDDATA;
  131. }
  132. if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
  133. return AVERROR_INVALIDDATA;
  134. mh->stream_type = get_bits(gb, 8);
  135. mh->header_size = header_size;
  136. if (mh->stream_type == 0xbb) {
  137. mh->group1_bits = mlp_quants[get_bits(gb, 4)];
  138. mh->group2_bits = mlp_quants[get_bits(gb, 4)];
  139. ratebits = get_bits(gb, 4);
  140. mh->group1_samplerate = mlp_samplerate(ratebits);
  141. mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4));
  142. skip_bits(gb, 11);
  143. mh->channel_arrangement=
  144. channel_arrangement = get_bits(gb, 5);
  145. mh->channels_mlp = mlp_channels[channel_arrangement];
  146. mh->channel_layout_mlp = mlp_layout[channel_arrangement];
  147. } else if (mh->stream_type == 0xba) {
  148. mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
  149. mh->group2_bits = 0;
  150. ratebits = get_bits(gb, 4);
  151. mh->group1_samplerate = mlp_samplerate(ratebits);
  152. mh->group2_samplerate = 0;
  153. skip_bits(gb, 4);
  154. mh->channel_modifier_thd_stream0 = get_bits(gb, 2);
  155. mh->channel_modifier_thd_stream1 = get_bits(gb, 2);
  156. mh->channel_arrangement=
  157. channel_arrangement = get_bits(gb, 5);
  158. mh->channels_thd_stream1 = truehd_channels(channel_arrangement);
  159. mh->channel_layout_thd_stream1 = truehd_layout(channel_arrangement);
  160. mh->channel_modifier_thd_stream2 = get_bits(gb, 2);
  161. channel_arrangement = get_bits(gb, 13);
  162. mh->channels_thd_stream2 = truehd_channels(channel_arrangement);
  163. mh->channel_layout_thd_stream2 = truehd_layout(channel_arrangement);
  164. } else
  165. return AVERROR_INVALIDDATA;
  166. mh->access_unit_size = 40 << (ratebits & 7);
  167. mh->access_unit_size_pow2 = 64 << (ratebits & 7);
  168. skip_bits_long(gb, 48);
  169. mh->is_vbr = get_bits1(gb);
  170. mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
  171. mh->num_substreams = get_bits(gb, 4);
  172. skip_bits_long(gb, 4 + (header_size - 17) * 8);
  173. return 0;
  174. }