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.

310 lines
8.4KB

  1. /*
  2. * MLP parser
  3. * Copyright (c) 2007 Ian Caulfield
  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. /**
  22. * @file mlp_parser.c
  23. * MLP parser
  24. */
  25. #include "libavutil/crc.h"
  26. #include "bitstream.h"
  27. #include "parser.h"
  28. #include "mlp_parser.h"
  29. static const uint8_t mlp_quants[16] = {
  30. 16, 20, 24, 0, 0, 0, 0, 0,
  31. 0, 0, 0, 0, 0, 0, 0, 0,
  32. };
  33. static const uint8_t mlp_channels[32] = {
  34. 1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
  35. 5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  36. };
  37. static const uint8_t thd_chancount[13] = {
  38. // LR C LFE LRs LRvh LRc LRrs Cs Ts LRsd LRw Cvh LFE2
  39. 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1
  40. };
  41. static int mlp_samplerate(int in)
  42. {
  43. if (in == 0xF)
  44. return 0;
  45. return (in & 8 ? 44100 : 48000) << (in & 7) ;
  46. }
  47. static int truehd_channels(int chanmap)
  48. {
  49. int channels = 0, i;
  50. for (i = 0; i < 13; i++)
  51. channels += thd_chancount[i] * ((chanmap >> i) & 1);
  52. return channels;
  53. }
  54. static int crc_init = 0;
  55. static AVCRC crc_2D[1024];
  56. /** MLP uses checksums that seem to be based on the standard CRC algorithm,
  57. * but not (in implementation terms, the table lookup and XOR are reversed).
  58. * We can implement this behavior using a standard av_crc on all but the
  59. * last element, then XOR that with the last element.
  60. */
  61. static uint16_t mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
  62. {
  63. uint16_t crc;
  64. if (!crc_init) {
  65. av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D));
  66. crc_init = 1;
  67. }
  68. crc = av_crc(crc_2D, 0, buf, buf_size - 2);
  69. crc ^= AV_RL16(buf + buf_size - 2);
  70. return crc;
  71. }
  72. /** Read a major sync info header - contains high level information about
  73. * the stream - sample rate, channel arrangement etc. Most of this
  74. * information is not actually necessary for decoding, only for playback.
  75. * gb must be a freshly initialized GetBitContext with no bits read.
  76. */
  77. int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
  78. {
  79. int ratebits;
  80. uint16_t checksum;
  81. assert(get_bits_count(gb) == 0);
  82. if (gb->size_in_bits < 28 << 3) {
  83. av_log(log, AV_LOG_ERROR, "Packet too short, unable to read major sync\n");
  84. return -1;
  85. }
  86. checksum = mlp_checksum16(gb->buffer, 26);
  87. if (checksum != AV_RL16(gb->buffer+26)) {
  88. av_log(log, AV_LOG_ERROR, "Major sync info header checksum error\n");
  89. return -1;
  90. }
  91. if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
  92. return -1;
  93. mh->stream_type = get_bits(gb, 8);
  94. if (mh->stream_type == 0xbb) {
  95. mh->group1_bits = mlp_quants[get_bits(gb, 4)];
  96. mh->group2_bits = mlp_quants[get_bits(gb, 4)];
  97. ratebits = get_bits(gb, 4);
  98. mh->group1_samplerate = mlp_samplerate(ratebits);
  99. mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4));
  100. skip_bits(gb, 11);
  101. mh->channels_mlp = get_bits(gb, 5);
  102. } else if (mh->stream_type == 0xba) {
  103. mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
  104. mh->group2_bits = 0;
  105. ratebits = get_bits(gb, 4);
  106. mh->group1_samplerate = mlp_samplerate(ratebits);
  107. mh->group2_samplerate = 0;
  108. skip_bits(gb, 8);
  109. mh->channels_thd_stream1 = get_bits(gb, 5);
  110. skip_bits(gb, 2);
  111. mh->channels_thd_stream2 = get_bits(gb, 13);
  112. } else
  113. return -1;
  114. mh->access_unit_size = 40 << (ratebits & 7);
  115. mh->access_unit_size_pow2 = 64 << (ratebits & 7);
  116. skip_bits_long(gb, 48);
  117. mh->is_vbr = get_bits1(gb);
  118. mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
  119. mh->num_substreams = get_bits(gb, 4);
  120. skip_bits_long(gb, 4 + 11 * 8);
  121. return 0;
  122. }
  123. typedef struct MLPParseContext
  124. {
  125. ParseContext pc;
  126. int bytes_left;
  127. int in_sync;
  128. int num_substreams;
  129. } MLPParseContext;
  130. static int mlp_parse(AVCodecParserContext *s,
  131. AVCodecContext *avctx,
  132. const uint8_t **poutbuf, int *poutbuf_size,
  133. const uint8_t *buf, int buf_size)
  134. {
  135. MLPParseContext *mp = s->priv_data;
  136. int sync_present;
  137. uint8_t parity_bits;
  138. int next;
  139. int i, p = 0;
  140. *poutbuf_size = 0;
  141. if (buf_size == 0)
  142. return 0;
  143. if (!mp->in_sync) {
  144. // Not in sync - find a major sync header
  145. for (i = 0; i < buf_size; i++) {
  146. mp->pc.state = (mp->pc.state << 8) | buf[i];
  147. if ((mp->pc.state & 0xfffffffe) == 0xf8726fba) {
  148. mp->in_sync = 1;
  149. mp->bytes_left = 0;
  150. break;
  151. }
  152. }
  153. if (!mp->in_sync) {
  154. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  155. return buf_size;
  156. }
  157. ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
  158. return i - 7;
  159. }
  160. if (mp->bytes_left == 0) {
  161. // Find length of this packet
  162. /* Copy overread bytes from last frame into buffer. */
  163. for(; mp->pc.overread>0; mp->pc.overread--) {
  164. mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
  165. }
  166. if (mp->pc.index + buf_size < 2) {
  167. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  168. return buf_size;
  169. }
  170. mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
  171. | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
  172. mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
  173. mp->bytes_left -= mp->pc.index;
  174. }
  175. next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
  176. if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
  177. mp->bytes_left -= buf_size;
  178. return buf_size;
  179. }
  180. mp->bytes_left = 0;
  181. sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
  182. if (!sync_present) {
  183. /* The first nibble of a frame is a parity check of the 4-byte
  184. * access unit header and all the 2- or 4-byte substream headers. */
  185. // Only check when this isn't a sync frame - syncs have a checksum.
  186. parity_bits = 0;
  187. for (i = -1; i < mp->num_substreams; i++) {
  188. parity_bits ^= buf[p++];
  189. parity_bits ^= buf[p++];
  190. if (i < 0 || buf[p-2] & 0x80) {
  191. parity_bits ^= buf[p++];
  192. parity_bits ^= buf[p++];
  193. }
  194. }
  195. if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
  196. av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
  197. goto lost_sync;
  198. }
  199. } else {
  200. GetBitContext gb;
  201. MLPHeaderInfo mh;
  202. init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
  203. if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
  204. goto lost_sync;
  205. #ifdef CONFIG_AUDIO_NONSHORT
  206. avctx->bits_per_sample = mh.group1_bits;
  207. if (avctx->bits_per_sample > 16)
  208. avctx->sample_fmt = SAMPLE_FMT_S32;
  209. #endif
  210. avctx->sample_rate = mh.group1_samplerate;
  211. avctx->frame_size = mh.access_unit_size;
  212. if (mh.stream_type == 0xbb) {
  213. /* MLP stream */
  214. avctx->channels = mlp_channels[mh.channels_mlp];
  215. } else { /* mh.stream_type == 0xba */
  216. /* TrueHD stream */
  217. if (mh.channels_thd_stream2)
  218. avctx->channels = truehd_channels(mh.channels_thd_stream2);
  219. else
  220. avctx->channels = truehd_channels(mh.channels_thd_stream1);
  221. }
  222. if (!mh.is_vbr) /* Stream is CBR */
  223. avctx->bit_rate = mh.peak_bitrate;
  224. mp->num_substreams = mh.num_substreams;
  225. }
  226. *poutbuf = buf;
  227. *poutbuf_size = buf_size;
  228. return next;
  229. lost_sync:
  230. mp->in_sync = 0;
  231. return 1;
  232. }
  233. AVCodecParser mlp_parser = {
  234. { CODEC_ID_MLP },
  235. sizeof(MLPParseContext),
  236. NULL,
  237. mlp_parse,
  238. NULL,
  239. };