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.

354 lines
10KB

  1. /*
  2. * MLP parser
  3. * Copyright (c) 2007 Ian Caulfield
  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. /**
  22. * @file
  23. * MLP parser
  24. */
  25. #include <stdint.h>
  26. #include "libavutil/crc.h"
  27. #include "libavutil/audioconvert.h"
  28. #include "get_bits.h"
  29. #include "parser.h"
  30. #include "mlp_parser.h"
  31. #include "mlp.h"
  32. static const uint8_t mlp_quants[16] = {
  33. 16, 20, 24, 0, 0, 0, 0, 0,
  34. 0, 0, 0, 0, 0, 0, 0, 0,
  35. };
  36. static const uint8_t mlp_channels[32] = {
  37. 1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
  38. 5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  39. };
  40. static const uint64_t mlp_layout[32] = {
  41. AV_CH_LAYOUT_MONO,
  42. AV_CH_LAYOUT_STEREO,
  43. AV_CH_LAYOUT_2_1,
  44. AV_CH_LAYOUT_2_2,
  45. AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
  46. AV_CH_LAYOUT_2_1|AV_CH_LOW_FREQUENCY,
  47. AV_CH_LAYOUT_2_2|AV_CH_LOW_FREQUENCY,
  48. AV_CH_LAYOUT_SURROUND,
  49. AV_CH_LAYOUT_4POINT0,
  50. AV_CH_LAYOUT_5POINT0,
  51. AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
  52. AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
  53. AV_CH_LAYOUT_5POINT1,
  54. AV_CH_LAYOUT_4POINT0,
  55. AV_CH_LAYOUT_5POINT0,
  56. AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
  57. AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
  58. AV_CH_LAYOUT_5POINT1,
  59. AV_CH_LAYOUT_2_2|AV_CH_LOW_FREQUENCY,
  60. AV_CH_LAYOUT_5POINT0,
  61. AV_CH_LAYOUT_5POINT1,
  62. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  63. };
  64. static const uint8_t thd_chancount[13] = {
  65. // LR C LFE LRs LRvh LRc LRrs Cs Ts LRsd LRw Cvh LFE2
  66. 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1
  67. };
  68. static const uint64_t thd_layout[13] = {
  69. AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT, // LR
  70. AV_CH_FRONT_CENTER, // C
  71. AV_CH_LOW_FREQUENCY, // LFE
  72. AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, // LRs
  73. AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT, // LRvh
  74. AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, // LRc
  75. AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT, // LRrs
  76. AV_CH_BACK_CENTER, // Cs
  77. AV_CH_TOP_BACK_CENTER, // Ts
  78. AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, // LRsd
  79. AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER, // LRw
  80. AV_CH_TOP_BACK_CENTER, // Cvh
  81. AV_CH_LOW_FREQUENCY // LFE2
  82. };
  83. static int mlp_samplerate(int in)
  84. {
  85. if (in == 0xF)
  86. return 0;
  87. return (in & 8 ? 44100 : 48000) << (in & 7) ;
  88. }
  89. static int truehd_channels(int chanmap)
  90. {
  91. int channels = 0, i;
  92. for (i = 0; i < 13; i++)
  93. channels += thd_chancount[i] * ((chanmap >> i) & 1);
  94. return channels;
  95. }
  96. static int64_t truehd_layout(int chanmap)
  97. {
  98. int layout = 0, i;
  99. for (i = 0; i < 13; i++)
  100. layout |= thd_layout[i] * ((chanmap >> i) & 1);
  101. return layout;
  102. }
  103. /** Read a major sync info header - contains high level information about
  104. * the stream - sample rate, channel arrangement etc. Most of this
  105. * information is not actually necessary for decoding, only for playback.
  106. * gb must be a freshly initialized GetBitContext with no bits read.
  107. */
  108. int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
  109. {
  110. int ratebits;
  111. uint16_t checksum;
  112. assert(get_bits_count(gb) == 0);
  113. if (gb->size_in_bits < 28 << 3) {
  114. av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
  115. return -1;
  116. }
  117. checksum = ff_mlp_checksum16(gb->buffer, 26);
  118. if (checksum != AV_RL16(gb->buffer+26)) {
  119. av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
  120. return -1;
  121. }
  122. if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
  123. return -1;
  124. mh->stream_type = get_bits(gb, 8);
  125. if (mh->stream_type == 0xbb) {
  126. mh->group1_bits = mlp_quants[get_bits(gb, 4)];
  127. mh->group2_bits = mlp_quants[get_bits(gb, 4)];
  128. ratebits = get_bits(gb, 4);
  129. mh->group1_samplerate = mlp_samplerate(ratebits);
  130. mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4));
  131. skip_bits(gb, 11);
  132. mh->channels_mlp = get_bits(gb, 5);
  133. } else if (mh->stream_type == 0xba) {
  134. mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
  135. mh->group2_bits = 0;
  136. ratebits = get_bits(gb, 4);
  137. mh->group1_samplerate = mlp_samplerate(ratebits);
  138. mh->group2_samplerate = 0;
  139. skip_bits(gb, 8);
  140. mh->channels_thd_stream1 = get_bits(gb, 5);
  141. skip_bits(gb, 2);
  142. mh->channels_thd_stream2 = get_bits(gb, 13);
  143. } else
  144. return -1;
  145. mh->access_unit_size = 40 << (ratebits & 7);
  146. mh->access_unit_size_pow2 = 64 << (ratebits & 7);
  147. skip_bits_long(gb, 48);
  148. mh->is_vbr = get_bits1(gb);
  149. mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
  150. mh->num_substreams = get_bits(gb, 4);
  151. skip_bits_long(gb, 4 + 11 * 8);
  152. return 0;
  153. }
  154. typedef struct MLPParseContext
  155. {
  156. ParseContext pc;
  157. int bytes_left;
  158. int in_sync;
  159. int num_substreams;
  160. } MLPParseContext;
  161. static av_cold int mlp_init(AVCodecParserContext *s)
  162. {
  163. ff_mlp_init_crc();
  164. return 0;
  165. }
  166. static int mlp_parse(AVCodecParserContext *s,
  167. AVCodecContext *avctx,
  168. const uint8_t **poutbuf, int *poutbuf_size,
  169. const uint8_t *buf, int buf_size)
  170. {
  171. MLPParseContext *mp = s->priv_data;
  172. int sync_present;
  173. uint8_t parity_bits;
  174. int next;
  175. int i, p = 0;
  176. *poutbuf_size = 0;
  177. if (buf_size == 0)
  178. return 0;
  179. if (!mp->in_sync) {
  180. // Not in sync - find a major sync header
  181. for (i = 0; i < buf_size; i++) {
  182. mp->pc.state = (mp->pc.state << 8) | buf[i];
  183. if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
  184. // ignore if we do not have the data for the start of header
  185. mp->pc.index + i >= 7) {
  186. mp->in_sync = 1;
  187. mp->bytes_left = 0;
  188. break;
  189. }
  190. }
  191. if (!mp->in_sync) {
  192. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  193. return buf_size;
  194. }
  195. ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
  196. return i - 7;
  197. }
  198. if (mp->bytes_left == 0) {
  199. // Find length of this packet
  200. /* Copy overread bytes from last frame into buffer. */
  201. for(; mp->pc.overread>0; mp->pc.overread--) {
  202. mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
  203. }
  204. if (mp->pc.index + buf_size < 2) {
  205. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  206. return buf_size;
  207. }
  208. mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
  209. | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
  210. mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
  211. mp->bytes_left -= mp->pc.index;
  212. }
  213. next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
  214. if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
  215. mp->bytes_left -= buf_size;
  216. return buf_size;
  217. }
  218. mp->bytes_left = 0;
  219. sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
  220. if (!sync_present) {
  221. /* The first nibble of a frame is a parity check of the 4-byte
  222. * access unit header and all the 2- or 4-byte substream headers. */
  223. // Only check when this isn't a sync frame - syncs have a checksum.
  224. parity_bits = 0;
  225. for (i = -1; i < mp->num_substreams; i++) {
  226. parity_bits ^= buf[p++];
  227. parity_bits ^= buf[p++];
  228. if (i < 0 || buf[p-2] & 0x80) {
  229. parity_bits ^= buf[p++];
  230. parity_bits ^= buf[p++];
  231. }
  232. }
  233. if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
  234. av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
  235. goto lost_sync;
  236. }
  237. } else {
  238. GetBitContext gb;
  239. MLPHeaderInfo mh;
  240. init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
  241. if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
  242. goto lost_sync;
  243. avctx->bits_per_raw_sample = mh.group1_bits;
  244. if (avctx->bits_per_raw_sample > 16)
  245. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  246. else
  247. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  248. avctx->sample_rate = mh.group1_samplerate;
  249. avctx->frame_size = mh.access_unit_size;
  250. if (mh.stream_type == 0xbb) {
  251. /* MLP stream */
  252. avctx->channels = mlp_channels[mh.channels_mlp];
  253. avctx->channel_layout = mlp_layout[mh.channels_mlp];
  254. } else { /* mh.stream_type == 0xba */
  255. /* TrueHD stream */
  256. if (mh.channels_thd_stream2) {
  257. avctx->channels = truehd_channels(mh.channels_thd_stream2);
  258. avctx->channel_layout = truehd_layout(mh.channels_thd_stream2);
  259. } else {
  260. avctx->channels = truehd_channels(mh.channels_thd_stream1);
  261. avctx->channel_layout = truehd_layout(mh.channels_thd_stream1);
  262. }
  263. }
  264. if (!mh.is_vbr) /* Stream is CBR */
  265. avctx->bit_rate = mh.peak_bitrate;
  266. mp->num_substreams = mh.num_substreams;
  267. }
  268. *poutbuf = buf;
  269. *poutbuf_size = buf_size;
  270. return next;
  271. lost_sync:
  272. mp->in_sync = 0;
  273. return 1;
  274. }
  275. AVCodecParser ff_mlp_parser = {
  276. { CODEC_ID_MLP, CODEC_ID_TRUEHD },
  277. sizeof(MLPParseContext),
  278. mlp_init,
  279. mlp_parse,
  280. ff_parse_close,
  281. };